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 1654c31984SVladimir Oltean #define OCELOT_RSV_VLAN_RANGE_START 4000 17639c1b26SSteen Hegelund 18a556c76aSAlexandre Belloni struct ocelot_mact_entry { 19a556c76aSAlexandre Belloni u8 mac[ETH_ALEN]; 20a556c76aSAlexandre Belloni u16 vid; 21a556c76aSAlexandre Belloni enum macaccess_entry_type type; 22a556c76aSAlexandre Belloni }; 23a556c76aSAlexandre Belloni 242468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */ 25639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) 26639c1b26SSteen Hegelund { 27639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_MACACCESS); 28639c1b26SSteen Hegelund } 29639c1b26SSteen Hegelund 302468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */ 31a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) 32a556c76aSAlexandre Belloni { 33639c1b26SSteen Hegelund u32 val; 34a556c76aSAlexandre Belloni 35639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_mact_read_macaccess, 36639c1b26SSteen Hegelund ocelot, val, 37639c1b26SSteen Hegelund (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == 38639c1b26SSteen Hegelund MACACCESS_CMD_IDLE, 39639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 40a556c76aSAlexandre Belloni } 41a556c76aSAlexandre Belloni 422468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */ 43a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot, 44a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 45a556c76aSAlexandre Belloni unsigned int vid) 46a556c76aSAlexandre Belloni { 47a556c76aSAlexandre Belloni u32 macl = 0, mach = 0; 48a556c76aSAlexandre Belloni 49a556c76aSAlexandre Belloni /* Set the MAC address to handle and the vlan associated in a format 50a556c76aSAlexandre Belloni * understood by the hardware. 51a556c76aSAlexandre Belloni */ 52a556c76aSAlexandre Belloni mach |= vid << 16; 53a556c76aSAlexandre Belloni mach |= mac[0] << 8; 54a556c76aSAlexandre Belloni mach |= mac[1] << 0; 55a556c76aSAlexandre Belloni macl |= mac[2] << 24; 56a556c76aSAlexandre Belloni macl |= mac[3] << 16; 57a556c76aSAlexandre Belloni macl |= mac[4] << 8; 58a556c76aSAlexandre Belloni macl |= mac[5] << 0; 59a556c76aSAlexandre Belloni 60a556c76aSAlexandre Belloni ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); 61a556c76aSAlexandre Belloni ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); 62a556c76aSAlexandre Belloni 63a556c76aSAlexandre Belloni } 64a556c76aSAlexandre Belloni 650568c3bfSXiaoliang Yang static int __ocelot_mact_learn(struct ocelot *ocelot, int port, 66a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 679c90eea3SVladimir Oltean unsigned int vid, enum macaccess_entry_type type) 68a556c76aSAlexandre Belloni { 69584b7cfcSAlban Bedel u32 cmd = ANA_TABLES_MACACCESS_VALID | 70584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_DEST_IDX(port) | 71584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_ENTRYTYPE(type) | 72584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); 73584b7cfcSAlban Bedel unsigned int mc_ports; 742468346cSVladimir Oltean int err; 75584b7cfcSAlban Bedel 76584b7cfcSAlban Bedel /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ 77584b7cfcSAlban Bedel if (type == ENTRYTYPE_MACv4) 78584b7cfcSAlban Bedel mc_ports = (mac[1] << 8) | mac[2]; 79584b7cfcSAlban Bedel else if (type == ENTRYTYPE_MACv6) 80584b7cfcSAlban Bedel mc_ports = (mac[0] << 8) | mac[1]; 81584b7cfcSAlban Bedel else 82584b7cfcSAlban Bedel mc_ports = 0; 83584b7cfcSAlban Bedel 84584b7cfcSAlban Bedel if (mc_ports & BIT(ocelot->num_phys_ports)) 85584b7cfcSAlban Bedel cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; 86584b7cfcSAlban Bedel 87a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 88a556c76aSAlexandre Belloni 89a556c76aSAlexandre Belloni /* Issue a write command */ 90584b7cfcSAlban Bedel ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); 91a556c76aSAlexandre Belloni 922468346cSVladimir Oltean err = ocelot_mact_wait_for_completion(ocelot); 932468346cSVladimir Oltean 940568c3bfSXiaoliang Yang return err; 950568c3bfSXiaoliang Yang } 960568c3bfSXiaoliang Yang 970568c3bfSXiaoliang Yang int ocelot_mact_learn(struct ocelot *ocelot, int port, 980568c3bfSXiaoliang Yang const unsigned char mac[ETH_ALEN], 990568c3bfSXiaoliang Yang unsigned int vid, enum macaccess_entry_type type) 1000568c3bfSXiaoliang Yang { 1010568c3bfSXiaoliang Yang int ret; 1020568c3bfSXiaoliang Yang 1030568c3bfSXiaoliang Yang mutex_lock(&ocelot->mact_lock); 1040568c3bfSXiaoliang Yang ret = __ocelot_mact_learn(ocelot, port, mac, vid, type); 1052468346cSVladimir Oltean mutex_unlock(&ocelot->mact_lock); 1062468346cSVladimir Oltean 1070568c3bfSXiaoliang Yang return ret; 108a556c76aSAlexandre Belloni } 1099c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn); 110a556c76aSAlexandre Belloni 1119c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot, 1129c90eea3SVladimir Oltean const unsigned char mac[ETH_ALEN], unsigned int vid) 113a556c76aSAlexandre Belloni { 1142468346cSVladimir Oltean int err; 1152468346cSVladimir Oltean 1162468346cSVladimir Oltean mutex_lock(&ocelot->mact_lock); 1172468346cSVladimir Oltean 118a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 119a556c76aSAlexandre Belloni 120a556c76aSAlexandre Belloni /* Issue a forget command */ 121a556c76aSAlexandre Belloni ocelot_write(ocelot, 122a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), 123a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 124a556c76aSAlexandre Belloni 1252468346cSVladimir Oltean err = ocelot_mact_wait_for_completion(ocelot); 1262468346cSVladimir Oltean 1272468346cSVladimir Oltean mutex_unlock(&ocelot->mact_lock); 1282468346cSVladimir Oltean 1292468346cSVladimir Oltean return err; 130a556c76aSAlexandre Belloni } 1319c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget); 132a556c76aSAlexandre Belloni 1330568c3bfSXiaoliang Yang int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx, 1340568c3bfSXiaoliang Yang const unsigned char mac[ETH_ALEN], 1350568c3bfSXiaoliang Yang unsigned int vid, enum macaccess_entry_type *type) 1360568c3bfSXiaoliang Yang { 1370568c3bfSXiaoliang Yang int val; 1380568c3bfSXiaoliang Yang 1390568c3bfSXiaoliang Yang mutex_lock(&ocelot->mact_lock); 1400568c3bfSXiaoliang Yang 1410568c3bfSXiaoliang Yang ocelot_mact_select(ocelot, mac, vid); 1420568c3bfSXiaoliang Yang 1430568c3bfSXiaoliang Yang /* Issue a read command with MACACCESS_VALID=1. */ 1440568c3bfSXiaoliang Yang ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID | 1450568c3bfSXiaoliang Yang ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 1460568c3bfSXiaoliang Yang ANA_TABLES_MACACCESS); 1470568c3bfSXiaoliang Yang 1480568c3bfSXiaoliang Yang if (ocelot_mact_wait_for_completion(ocelot)) { 1490568c3bfSXiaoliang Yang mutex_unlock(&ocelot->mact_lock); 1500568c3bfSXiaoliang Yang return -ETIMEDOUT; 1510568c3bfSXiaoliang Yang } 1520568c3bfSXiaoliang Yang 1530568c3bfSXiaoliang Yang /* Read back the entry flags */ 1540568c3bfSXiaoliang Yang val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 1550568c3bfSXiaoliang Yang 1560568c3bfSXiaoliang Yang mutex_unlock(&ocelot->mact_lock); 1570568c3bfSXiaoliang Yang 1580568c3bfSXiaoliang Yang if (!(val & ANA_TABLES_MACACCESS_VALID)) 1590568c3bfSXiaoliang Yang return -ENOENT; 1600568c3bfSXiaoliang Yang 1610568c3bfSXiaoliang Yang *dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val); 1620568c3bfSXiaoliang Yang *type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val); 1630568c3bfSXiaoliang Yang 1640568c3bfSXiaoliang Yang return 0; 1650568c3bfSXiaoliang Yang } 1660568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_lookup); 1670568c3bfSXiaoliang Yang 1680568c3bfSXiaoliang Yang int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx, 1690568c3bfSXiaoliang Yang const unsigned char mac[ETH_ALEN], 1700568c3bfSXiaoliang Yang unsigned int vid, 1710568c3bfSXiaoliang Yang enum macaccess_entry_type type, 1720568c3bfSXiaoliang Yang int sfid, int ssid) 1730568c3bfSXiaoliang Yang { 1740568c3bfSXiaoliang Yang int ret; 1750568c3bfSXiaoliang Yang 1760568c3bfSXiaoliang Yang mutex_lock(&ocelot->mact_lock); 1770568c3bfSXiaoliang Yang 1780568c3bfSXiaoliang Yang ocelot_write(ocelot, 1790568c3bfSXiaoliang Yang (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) | 1800568c3bfSXiaoliang Yang ANA_TABLES_STREAMDATA_SFID(sfid) | 1810568c3bfSXiaoliang Yang (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) | 1820568c3bfSXiaoliang Yang ANA_TABLES_STREAMDATA_SSID(ssid), 1830568c3bfSXiaoliang Yang ANA_TABLES_STREAMDATA); 1840568c3bfSXiaoliang Yang 1850568c3bfSXiaoliang Yang ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type); 1860568c3bfSXiaoliang Yang 1870568c3bfSXiaoliang Yang mutex_unlock(&ocelot->mact_lock); 1880568c3bfSXiaoliang Yang 1890568c3bfSXiaoliang Yang return ret; 1900568c3bfSXiaoliang Yang } 1910568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_learn_streamdata); 1920568c3bfSXiaoliang Yang 193a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot) 194a556c76aSAlexandre Belloni { 195a556c76aSAlexandre Belloni /* Configure the learning mode entries attributes: 196a556c76aSAlexandre Belloni * - Do not copy the frame to the CPU extraction queues. 197a556c76aSAlexandre Belloni * - Use the vlan and mac_cpoy for dmac lookup. 198a556c76aSAlexandre Belloni */ 199a556c76aSAlexandre Belloni ocelot_rmw(ocelot, 0, 200a556c76aSAlexandre Belloni ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS 201a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_FWD_KILL 202a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_IGNORE_VLAN, 203a556c76aSAlexandre Belloni ANA_AGENCTRL); 204a556c76aSAlexandre Belloni 2052468346cSVladimir Oltean /* Clear the MAC table. We are not concurrent with anyone, so 2062468346cSVladimir Oltean * holding &ocelot->mact_lock is pointless. 2072468346cSVladimir Oltean */ 208a556c76aSAlexandre Belloni ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); 209a556c76aSAlexandre Belloni } 210a556c76aSAlexandre Belloni 211f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port) 212b5962294SHoratiu Vultur { 213b5962294SHoratiu Vultur ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | 214b5962294SHoratiu Vultur ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), 215f270dbfaSVladimir Oltean ANA_PORT_VCAP_S2_CFG, port); 21675944fdaSXiaoliang Yang 21775944fdaSXiaoliang Yang ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, 21875944fdaSXiaoliang Yang ANA_PORT_VCAP_CFG, port); 2192f17c050SXiaoliang Yang 2202f17c050SXiaoliang Yang ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, 2212f17c050SXiaoliang Yang REW_PORT_CFG_ES0_EN, 2222f17c050SXiaoliang Yang REW_PORT_CFG, port); 223b5962294SHoratiu Vultur } 224b5962294SHoratiu Vultur 22554c31984SVladimir Oltean static int ocelot_single_vlan_aware_bridge(struct ocelot *ocelot, 22654c31984SVladimir Oltean struct netlink_ext_ack *extack) 22754c31984SVladimir Oltean { 22854c31984SVladimir Oltean struct net_device *bridge = NULL; 22954c31984SVladimir Oltean int port; 23054c31984SVladimir Oltean 23154c31984SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 23254c31984SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 23354c31984SVladimir Oltean 23454c31984SVladimir Oltean if (!ocelot_port || !ocelot_port->bridge || 23554c31984SVladimir Oltean !br_vlan_enabled(ocelot_port->bridge)) 23654c31984SVladimir Oltean continue; 23754c31984SVladimir Oltean 23854c31984SVladimir Oltean if (!bridge) { 23954c31984SVladimir Oltean bridge = ocelot_port->bridge; 24054c31984SVladimir Oltean continue; 24154c31984SVladimir Oltean } 24254c31984SVladimir Oltean 24354c31984SVladimir Oltean if (bridge == ocelot_port->bridge) 24454c31984SVladimir Oltean continue; 24554c31984SVladimir Oltean 24654c31984SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 24754c31984SVladimir Oltean "Only one VLAN-aware bridge is supported"); 24854c31984SVladimir Oltean return -EBUSY; 24954c31984SVladimir Oltean } 25054c31984SVladimir Oltean 25154c31984SVladimir Oltean return 0; 25254c31984SVladimir Oltean } 25354c31984SVladimir Oltean 254639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) 255639c1b26SSteen Hegelund { 256639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); 257639c1b26SSteen Hegelund } 258639c1b26SSteen Hegelund 259a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) 260a556c76aSAlexandre Belloni { 261639c1b26SSteen Hegelund u32 val; 262a556c76aSAlexandre Belloni 263639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_vlant_read_vlanaccess, 264639c1b26SSteen Hegelund ocelot, 265639c1b26SSteen Hegelund val, 266639c1b26SSteen Hegelund (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == 267639c1b26SSteen Hegelund ANA_TABLES_VLANACCESS_CMD_IDLE, 268639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 269a556c76aSAlexandre Belloni } 270a556c76aSAlexandre Belloni 2717142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) 2727142529fSAntoine Tenart { 2737142529fSAntoine Tenart /* Select the VID to configure */ 2747142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), 2757142529fSAntoine Tenart ANA_TABLES_VLANTIDX); 2767142529fSAntoine Tenart /* Set the vlan port members mask and issue a write command */ 2777142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | 2787142529fSAntoine Tenart ANA_TABLES_VLANACCESS_CMD_WRITE, 2797142529fSAntoine Tenart ANA_TABLES_VLANACCESS); 2807142529fSAntoine Tenart 2817142529fSAntoine Tenart return ocelot_vlant_wait_for_completion(ocelot); 2827142529fSAntoine Tenart } 2837142529fSAntoine Tenart 2840da1a1c4SVladimir Oltean static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port) 2850da1a1c4SVladimir Oltean { 2860da1a1c4SVladimir Oltean struct ocelot_bridge_vlan *vlan; 2870da1a1c4SVladimir Oltean int num_untagged = 0; 2880da1a1c4SVladimir Oltean 2890da1a1c4SVladimir Oltean list_for_each_entry(vlan, &ocelot->vlans, list) { 2900da1a1c4SVladimir Oltean if (!(vlan->portmask & BIT(port))) 2910da1a1c4SVladimir Oltean continue; 2920da1a1c4SVladimir Oltean 2930da1a1c4SVladimir Oltean if (vlan->untagged & BIT(port)) 2940da1a1c4SVladimir Oltean num_untagged++; 2950da1a1c4SVladimir Oltean } 2960da1a1c4SVladimir Oltean 2970da1a1c4SVladimir Oltean return num_untagged; 2980da1a1c4SVladimir Oltean } 2990da1a1c4SVladimir Oltean 3000da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port) 3010da1a1c4SVladimir Oltean { 3020da1a1c4SVladimir Oltean struct ocelot_bridge_vlan *vlan; 3030da1a1c4SVladimir Oltean int num_tagged = 0; 3040da1a1c4SVladimir Oltean 3050da1a1c4SVladimir Oltean list_for_each_entry(vlan, &ocelot->vlans, list) { 3060da1a1c4SVladimir Oltean if (!(vlan->portmask & BIT(port))) 3070da1a1c4SVladimir Oltean continue; 3080da1a1c4SVladimir Oltean 3090da1a1c4SVladimir Oltean if (!(vlan->untagged & BIT(port))) 3100da1a1c4SVladimir Oltean num_tagged++; 3110da1a1c4SVladimir Oltean } 3120da1a1c4SVladimir Oltean 3130da1a1c4SVladimir Oltean return num_tagged; 3140da1a1c4SVladimir Oltean } 3150da1a1c4SVladimir Oltean 3160da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly 3170da1a1c4SVladimir Oltean * _one_ egress-untagged VLAN (_the_ native VLAN) 3180da1a1c4SVladimir Oltean */ 3190da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port) 3200da1a1c4SVladimir Oltean { 3210da1a1c4SVladimir Oltean return ocelot_port_num_tagged_vlans(ocelot, port) && 3220da1a1c4SVladimir Oltean ocelot_port_num_untagged_vlans(ocelot, port) == 1; 3230da1a1c4SVladimir Oltean } 3240da1a1c4SVladimir Oltean 3250da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan * 3260da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port) 3270da1a1c4SVladimir Oltean { 3280da1a1c4SVladimir Oltean struct ocelot_bridge_vlan *vlan; 3290da1a1c4SVladimir Oltean 3300da1a1c4SVladimir Oltean list_for_each_entry(vlan, &ocelot->vlans, list) 3310da1a1c4SVladimir Oltean if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port)) 3320da1a1c4SVladimir Oltean return vlan; 3330da1a1c4SVladimir Oltean 3340da1a1c4SVladimir Oltean return NULL; 3350da1a1c4SVladimir Oltean } 3360da1a1c4SVladimir Oltean 3370da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable, 3380da1a1c4SVladimir Oltean * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness 3390da1a1c4SVladimir Oltean * state of the port. 3400da1a1c4SVladimir Oltean */ 3410da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port) 34297bb69e1SVladimir Oltean { 34397bb69e1SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 34462a22bcbSVladimir Oltean enum ocelot_port_tag_config tag_cfg; 3450da1a1c4SVladimir Oltean bool uses_native_vlan = false; 34697bb69e1SVladimir Oltean 34787b0f983SVladimir Oltean if (ocelot_port->vlan_aware) { 3480da1a1c4SVladimir Oltean uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port); 3490da1a1c4SVladimir Oltean 3500da1a1c4SVladimir Oltean if (uses_native_vlan) 35162a22bcbSVladimir Oltean tag_cfg = OCELOT_PORT_TAG_NATIVE; 3520da1a1c4SVladimir Oltean else if (ocelot_port_num_untagged_vlans(ocelot, port)) 3530da1a1c4SVladimir Oltean tag_cfg = OCELOT_PORT_TAG_DISABLED; 35487b0f983SVladimir Oltean else 35562a22bcbSVladimir Oltean tag_cfg = OCELOT_PORT_TAG_TRUNK; 35687b0f983SVladimir Oltean } else { 35762a22bcbSVladimir Oltean tag_cfg = OCELOT_PORT_TAG_DISABLED; 35887b0f983SVladimir Oltean } 3590da1a1c4SVladimir Oltean 36062a22bcbSVladimir Oltean ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg), 36187b0f983SVladimir Oltean REW_TAG_CFG_TAG_CFG_M, 36287b0f983SVladimir Oltean REW_TAG_CFG, port); 3630da1a1c4SVladimir Oltean 3640da1a1c4SVladimir Oltean if (uses_native_vlan) { 3650da1a1c4SVladimir Oltean struct ocelot_bridge_vlan *native_vlan; 3660da1a1c4SVladimir Oltean 3670da1a1c4SVladimir Oltean /* Not having a native VLAN is impossible, because 3680da1a1c4SVladimir Oltean * ocelot_port_num_untagged_vlans has returned 1. 3690da1a1c4SVladimir Oltean * So there is no use in checking for NULL here. 3700da1a1c4SVladimir Oltean */ 3710da1a1c4SVladimir Oltean native_vlan = ocelot_port_find_native_vlan(ocelot, port); 3720da1a1c4SVladimir Oltean 3730da1a1c4SVladimir Oltean ocelot_rmw_gix(ocelot, 3740da1a1c4SVladimir Oltean REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid), 3750da1a1c4SVladimir Oltean REW_PORT_VLAN_CFG_PORT_VID_M, 3760da1a1c4SVladimir Oltean REW_PORT_VLAN_CFG, port); 3770da1a1c4SVladimir Oltean } 37897bb69e1SVladimir Oltean } 37997bb69e1SVladimir Oltean 38054c31984SVladimir Oltean int ocelot_bridge_num_find(struct ocelot *ocelot, 38154c31984SVladimir Oltean const struct net_device *bridge) 38254c31984SVladimir Oltean { 38354c31984SVladimir Oltean int port; 38454c31984SVladimir Oltean 38554c31984SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 38654c31984SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 38754c31984SVladimir Oltean 38854c31984SVladimir Oltean if (ocelot_port && ocelot_port->bridge == bridge) 38954c31984SVladimir Oltean return ocelot_port->bridge_num; 39054c31984SVladimir Oltean } 39154c31984SVladimir Oltean 39254c31984SVladimir Oltean return -1; 39354c31984SVladimir Oltean } 39454c31984SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bridge_num_find); 39554c31984SVladimir Oltean 39654c31984SVladimir Oltean static u16 ocelot_vlan_unaware_pvid(struct ocelot *ocelot, 39754c31984SVladimir Oltean const struct net_device *bridge) 39854c31984SVladimir Oltean { 39954c31984SVladimir Oltean int bridge_num; 40054c31984SVladimir Oltean 40154c31984SVladimir Oltean /* Standalone ports use VID 0 */ 40254c31984SVladimir Oltean if (!bridge) 40354c31984SVladimir Oltean return 0; 40454c31984SVladimir Oltean 40554c31984SVladimir Oltean bridge_num = ocelot_bridge_num_find(ocelot, bridge); 40654c31984SVladimir Oltean if (WARN_ON(bridge_num < 0)) 40754c31984SVladimir Oltean return 0; 40854c31984SVladimir Oltean 40954c31984SVladimir Oltean /* VLAN-unaware bridges use a reserved VID going from 4095 downwards */ 41054c31984SVladimir Oltean return VLAN_N_VID - bridge_num - 1; 41154c31984SVladimir Oltean } 41254c31984SVladimir Oltean 41375e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */ 414c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, 415d4004422SVladimir Oltean const struct ocelot_bridge_vlan *pvid_vlan) 41675e5a554SVladimir Oltean { 41775e5a554SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 41854c31984SVladimir Oltean u16 pvid = ocelot_vlan_unaware_pvid(ocelot, ocelot_port->bridge); 419be0576feSVladimir Oltean u32 val = 0; 42075e5a554SVladimir Oltean 421c3e58a75SVladimir Oltean ocelot_port->pvid_vlan = pvid_vlan; 42275e5a554SVladimir Oltean 423d4004422SVladimir Oltean if (ocelot_port->vlan_aware && pvid_vlan) 424d4004422SVladimir Oltean pvid = pvid_vlan->vid; 42575e5a554SVladimir Oltean 42675e5a554SVladimir Oltean ocelot_rmw_gix(ocelot, 427d4004422SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID(pvid), 42875e5a554SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID_M, 42975e5a554SVladimir Oltean ANA_PORT_VLAN_CFG, port); 430be0576feSVladimir Oltean 431be0576feSVladimir Oltean /* If there's no pvid, we should drop not only untagged traffic (which 432be0576feSVladimir Oltean * happens automatically), but also 802.1p traffic which gets 433be0576feSVladimir Oltean * classified to VLAN 0, but that is always in our RX filter, so it 434be0576feSVladimir Oltean * would get accepted were it not for this setting. 435be0576feSVladimir Oltean */ 436d4004422SVladimir Oltean if (!pvid_vlan && ocelot_port->vlan_aware) 437be0576feSVladimir Oltean val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 438be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; 439be0576feSVladimir Oltean 440be0576feSVladimir Oltean ocelot_rmw_gix(ocelot, val, 441be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 442be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, 443be0576feSVladimir Oltean ANA_PORT_DROP_CFG, port); 44475e5a554SVladimir Oltean } 44575e5a554SVladimir Oltean 44690e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot, 44790e0aa8dSVladimir Oltean u16 vid) 448bbf6a2d9SVladimir Oltean { 44990e0aa8dSVladimir Oltean struct ocelot_bridge_vlan *vlan; 450bbf6a2d9SVladimir Oltean 45190e0aa8dSVladimir Oltean list_for_each_entry(vlan, &ocelot->vlans, list) 45290e0aa8dSVladimir Oltean if (vlan->vid == vid) 45390e0aa8dSVladimir Oltean return vlan; 454bbf6a2d9SVladimir Oltean 45590e0aa8dSVladimir Oltean return NULL; 456bbf6a2d9SVladimir Oltean } 457bbf6a2d9SVladimir Oltean 4580da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid, 4590da1a1c4SVladimir Oltean bool untagged) 460bbf6a2d9SVladimir Oltean { 46190e0aa8dSVladimir Oltean struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid); 46290e0aa8dSVladimir Oltean unsigned long portmask; 46390e0aa8dSVladimir Oltean int err; 46490e0aa8dSVladimir Oltean 46590e0aa8dSVladimir Oltean if (vlan) { 46690e0aa8dSVladimir Oltean portmask = vlan->portmask | BIT(port); 46790e0aa8dSVladimir Oltean 46890e0aa8dSVladimir Oltean err = ocelot_vlant_set_mask(ocelot, vid, portmask); 46990e0aa8dSVladimir Oltean if (err) 47090e0aa8dSVladimir Oltean return err; 47190e0aa8dSVladimir Oltean 47290e0aa8dSVladimir Oltean vlan->portmask = portmask; 4730da1a1c4SVladimir Oltean /* Bridge VLANs can be overwritten with a different 4740da1a1c4SVladimir Oltean * egress-tagging setting, so make sure to override an untagged 4750da1a1c4SVladimir Oltean * with a tagged VID if that's going on. 4760da1a1c4SVladimir Oltean */ 4770da1a1c4SVladimir Oltean if (untagged) 4780da1a1c4SVladimir Oltean vlan->untagged |= BIT(port); 4790da1a1c4SVladimir Oltean else 4800da1a1c4SVladimir Oltean vlan->untagged &= ~BIT(port); 48190e0aa8dSVladimir Oltean 48290e0aa8dSVladimir Oltean return 0; 48390e0aa8dSVladimir Oltean } 48490e0aa8dSVladimir Oltean 48590e0aa8dSVladimir Oltean vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); 48690e0aa8dSVladimir Oltean if (!vlan) 48790e0aa8dSVladimir Oltean return -ENOMEM; 48890e0aa8dSVladimir Oltean 48990e0aa8dSVladimir Oltean portmask = BIT(port); 49090e0aa8dSVladimir Oltean 49190e0aa8dSVladimir Oltean err = ocelot_vlant_set_mask(ocelot, vid, portmask); 49290e0aa8dSVladimir Oltean if (err) { 49390e0aa8dSVladimir Oltean kfree(vlan); 49490e0aa8dSVladimir Oltean return err; 49590e0aa8dSVladimir Oltean } 49690e0aa8dSVladimir Oltean 49790e0aa8dSVladimir Oltean vlan->vid = vid; 49890e0aa8dSVladimir Oltean vlan->portmask = portmask; 4990da1a1c4SVladimir Oltean if (untagged) 5000da1a1c4SVladimir Oltean vlan->untagged = BIT(port); 50190e0aa8dSVladimir Oltean INIT_LIST_HEAD(&vlan->list); 50290e0aa8dSVladimir Oltean list_add_tail(&vlan->list, &ocelot->vlans); 50390e0aa8dSVladimir Oltean 50490e0aa8dSVladimir Oltean return 0; 505bbf6a2d9SVladimir Oltean } 506bbf6a2d9SVladimir Oltean 507bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid) 508bbf6a2d9SVladimir Oltean { 50990e0aa8dSVladimir Oltean struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid); 51090e0aa8dSVladimir Oltean unsigned long portmask; 51190e0aa8dSVladimir Oltean int err; 51290e0aa8dSVladimir Oltean 51390e0aa8dSVladimir Oltean if (!vlan) 51490e0aa8dSVladimir Oltean return 0; 51590e0aa8dSVladimir Oltean 51690e0aa8dSVladimir Oltean portmask = vlan->portmask & ~BIT(port); 51790e0aa8dSVladimir Oltean 51890e0aa8dSVladimir Oltean err = ocelot_vlant_set_mask(ocelot, vid, portmask); 51990e0aa8dSVladimir Oltean if (err) 52090e0aa8dSVladimir Oltean return err; 52190e0aa8dSVladimir Oltean 52290e0aa8dSVladimir Oltean vlan->portmask = portmask; 52390e0aa8dSVladimir Oltean if (vlan->portmask) 52490e0aa8dSVladimir Oltean return 0; 52590e0aa8dSVladimir Oltean 52690e0aa8dSVladimir Oltean list_del(&vlan->list); 52790e0aa8dSVladimir Oltean kfree(vlan); 52890e0aa8dSVladimir Oltean 52990e0aa8dSVladimir Oltean return 0; 530bbf6a2d9SVladimir Oltean } 531bbf6a2d9SVladimir Oltean 53254c31984SVladimir Oltean static int ocelot_add_vlan_unaware_pvid(struct ocelot *ocelot, int port, 53354c31984SVladimir Oltean const struct net_device *bridge) 53454c31984SVladimir Oltean { 53554c31984SVladimir Oltean u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 53654c31984SVladimir Oltean 53754c31984SVladimir Oltean return ocelot_vlan_member_add(ocelot, port, vid, true); 53854c31984SVladimir Oltean } 53954c31984SVladimir Oltean 54054c31984SVladimir Oltean static int ocelot_del_vlan_unaware_pvid(struct ocelot *ocelot, int port, 54154c31984SVladimir Oltean const struct net_device *bridge) 54254c31984SVladimir Oltean { 54354c31984SVladimir Oltean u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 54454c31984SVladimir Oltean 54554c31984SVladimir Oltean return ocelot_vlan_member_del(ocelot, port, vid); 54654c31984SVladimir Oltean } 54754c31984SVladimir Oltean 5482e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, 5493b95d1b2SVladimir Oltean bool vlan_aware, struct netlink_ext_ack *extack) 55087b0f983SVladimir Oltean { 55170edfae1SVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; 552bae33f2bSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 55370edfae1SVladimir Oltean struct ocelot_vcap_filter *filter; 5541fcb8fb3SVladimir Oltean int err = 0; 555bae33f2bSVladimir Oltean u32 val; 55670edfae1SVladimir Oltean 55770edfae1SVladimir Oltean list_for_each_entry(filter, &block->rules, list) { 55870edfae1SVladimir Oltean if (filter->ingress_port_mask & BIT(port) && 55970edfae1SVladimir Oltean filter->action.vid_replace_ena) { 5603b95d1b2SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 5613b95d1b2SVladimir Oltean "Cannot change VLAN state with vlan modify rules active"); 56270edfae1SVladimir Oltean return -EBUSY; 56370edfae1SVladimir Oltean } 56470edfae1SVladimir Oltean } 56570edfae1SVladimir Oltean 56654c31984SVladimir Oltean err = ocelot_single_vlan_aware_bridge(ocelot, extack); 56754c31984SVladimir Oltean if (err) 56854c31984SVladimir Oltean return err; 56954c31984SVladimir Oltean 57054c31984SVladimir Oltean if (vlan_aware) 57154c31984SVladimir Oltean err = ocelot_del_vlan_unaware_pvid(ocelot, port, 57254c31984SVladimir Oltean ocelot_port->bridge); 5731fcb8fb3SVladimir Oltean else if (ocelot_port->bridge) 57454c31984SVladimir Oltean err = ocelot_add_vlan_unaware_pvid(ocelot, port, 57554c31984SVladimir Oltean ocelot_port->bridge); 57654c31984SVladimir Oltean if (err) 57754c31984SVladimir Oltean return err; 57854c31984SVladimir Oltean 57987b0f983SVladimir Oltean ocelot_port->vlan_aware = vlan_aware; 58087b0f983SVladimir Oltean 58187b0f983SVladimir Oltean if (vlan_aware) 58287b0f983SVladimir Oltean val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 58387b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); 58487b0f983SVladimir Oltean else 58587b0f983SVladimir Oltean val = 0; 58687b0f983SVladimir Oltean ocelot_rmw_gix(ocelot, val, 58787b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 58887b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, 58987b0f983SVladimir Oltean ANA_PORT_VLAN_CFG, port); 59087b0f983SVladimir Oltean 591c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); 5920da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 5932e554a7aSVladimir Oltean 5942e554a7aSVladimir Oltean return 0; 59587b0f983SVladimir Oltean } 59687b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering); 59787b0f983SVladimir Oltean 5982f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, 59901af940eSVladimir Oltean bool untagged, struct netlink_ext_ack *extack) 6002f0402feSVladimir Oltean { 6010da1a1c4SVladimir Oltean if (untagged) { 6020da1a1c4SVladimir Oltean /* We are adding an egress-tagged VLAN */ 6030da1a1c4SVladimir Oltean if (ocelot_port_uses_native_vlan(ocelot, port)) { 60401af940eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 6050da1a1c4SVladimir Oltean "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN"); 6062f0402feSVladimir Oltean return -EBUSY; 6072f0402feSVladimir Oltean } 6080da1a1c4SVladimir Oltean } else { 6090da1a1c4SVladimir Oltean /* We are adding an egress-tagged VLAN */ 6100da1a1c4SVladimir Oltean if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) { 6110da1a1c4SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 6120da1a1c4SVladimir Oltean "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs"); 6130da1a1c4SVladimir Oltean return -EBUSY; 6140da1a1c4SVladimir Oltean } 6150da1a1c4SVladimir Oltean } 6162f0402feSVladimir Oltean 61754c31984SVladimir Oltean if (vid > OCELOT_RSV_VLAN_RANGE_START) { 61854c31984SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 61954c31984SVladimir Oltean "VLAN range 4000-4095 reserved for VLAN-unaware bridging"); 62054c31984SVladimir Oltean return -EBUSY; 62154c31984SVladimir Oltean } 62254c31984SVladimir Oltean 6232f0402feSVladimir Oltean return 0; 6242f0402feSVladimir Oltean } 6252f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare); 6262f0402feSVladimir Oltean 6275e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, 6287142529fSAntoine Tenart bool untagged) 6297142529fSAntoine Tenart { 630bbf6a2d9SVladimir Oltean int err; 6317142529fSAntoine Tenart 6329323ac36SVladimir Oltean /* Ignore VID 0 added to our RX filter by the 8021q module, since 6339323ac36SVladimir Oltean * that collides with OCELOT_STANDALONE_PVID and changes it from 6349323ac36SVladimir Oltean * egress-untagged to egress-tagged. 6359323ac36SVladimir Oltean */ 6369323ac36SVladimir Oltean if (!vid) 6379323ac36SVladimir Oltean return 0; 6389323ac36SVladimir Oltean 6390da1a1c4SVladimir Oltean err = ocelot_vlan_member_add(ocelot, port, vid, untagged); 640bbf6a2d9SVladimir Oltean if (err) 641bbf6a2d9SVladimir Oltean return err; 6427142529fSAntoine Tenart 6437142529fSAntoine Tenart /* Default ingress vlan classification */ 644d4004422SVladimir Oltean if (pvid) 645d4004422SVladimir Oltean ocelot_port_set_pvid(ocelot, port, 646d4004422SVladimir Oltean ocelot_bridge_vlan_find(ocelot, vid)); 6477142529fSAntoine Tenart 6487142529fSAntoine Tenart /* Untagged egress vlan clasification */ 6490da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 6507142529fSAntoine Tenart 6517142529fSAntoine Tenart return 0; 6527142529fSAntoine Tenart } 6535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add); 6547142529fSAntoine Tenart 6555e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) 6569855934cSVladimir Oltean { 6579855934cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 658ef576405SVladimir Oltean bool del_pvid = false; 659bbf6a2d9SVladimir Oltean int err; 6607142529fSAntoine Tenart 6619323ac36SVladimir Oltean if (!vid) 6629323ac36SVladimir Oltean return 0; 6639323ac36SVladimir Oltean 664ef576405SVladimir Oltean if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid) 665ef576405SVladimir Oltean del_pvid = true; 666ef576405SVladimir Oltean 667bbf6a2d9SVladimir Oltean err = ocelot_vlan_member_del(ocelot, port, vid); 668bbf6a2d9SVladimir Oltean if (err) 669bbf6a2d9SVladimir Oltean return err; 6707142529fSAntoine Tenart 671be0576feSVladimir Oltean /* Ingress */ 672ef576405SVladimir Oltean if (del_pvid) 673d4004422SVladimir Oltean ocelot_port_set_pvid(ocelot, port, NULL); 674be0576feSVladimir Oltean 6757142529fSAntoine Tenart /* Egress */ 6760da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 6777142529fSAntoine Tenart 6787142529fSAntoine Tenart return 0; 6797142529fSAntoine Tenart } 6805e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del); 6817142529fSAntoine Tenart 682a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot) 683a556c76aSAlexandre Belloni { 684bbf6a2d9SVladimir Oltean unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0); 6857142529fSAntoine Tenart u16 port, vid; 6867142529fSAntoine Tenart 687a556c76aSAlexandre Belloni /* Clear VLAN table, by default all ports are members of all VLANs */ 688a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, 689a556c76aSAlexandre Belloni ANA_TABLES_VLANACCESS); 690a556c76aSAlexandre Belloni ocelot_vlant_wait_for_completion(ocelot); 6917142529fSAntoine Tenart 6927142529fSAntoine Tenart /* Configure the port VLAN memberships */ 693bbf6a2d9SVladimir Oltean for (vid = 1; vid < VLAN_N_VID; vid++) 69490e0aa8dSVladimir Oltean ocelot_vlant_set_mask(ocelot, vid, 0); 6957142529fSAntoine Tenart 69654c31984SVladimir Oltean /* We need VID 0 to get traffic on standalone ports. 69754c31984SVladimir Oltean * It is added automatically if the 8021q module is loaded, but we 69854c31984SVladimir Oltean * can't rely on that since it might not be. 6997142529fSAntoine Tenart */ 70054c31984SVladimir Oltean ocelot_vlant_set_mask(ocelot, OCELOT_STANDALONE_PVID, all_ports); 7017142529fSAntoine Tenart 7027142529fSAntoine Tenart /* Set vlan ingress filter mask to all ports but the CPU port by 7037142529fSAntoine Tenart * default. 7047142529fSAntoine Tenart */ 705bbf6a2d9SVladimir Oltean ocelot_write(ocelot, all_ports, ANA_VLANMASK); 7067142529fSAntoine Tenart 7077142529fSAntoine Tenart for (port = 0; port < ocelot->num_phys_ports; port++) { 7087142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); 7097142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); 7107142529fSAntoine Tenart } 711a556c76aSAlexandre Belloni } 712a556c76aSAlexandre Belloni 713eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port) 714eb4733d7SVladimir Oltean { 715eb4733d7SVladimir Oltean return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port); 716eb4733d7SVladimir Oltean } 717eb4733d7SVladimir Oltean 718e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port) 719eb4733d7SVladimir Oltean { 7201650bdb1SVladimir Oltean unsigned int pause_ena; 721eb4733d7SVladimir Oltean int err, val; 722eb4733d7SVladimir Oltean 723eb4733d7SVladimir Oltean /* Disable dequeuing from the egress queues */ 724eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS, 725eb4733d7SVladimir Oltean QSYS_PORT_MODE_DEQUEUE_DIS, 726eb4733d7SVladimir Oltean QSYS_PORT_MODE, port); 727eb4733d7SVladimir Oltean 728eb4733d7SVladimir Oltean /* Disable flow control */ 7291650bdb1SVladimir Oltean ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena); 730eb4733d7SVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 731eb4733d7SVladimir Oltean 732eb4733d7SVladimir Oltean /* Disable priority flow control */ 733eb4733d7SVladimir Oltean ocelot_fields_write(ocelot, port, 734eb4733d7SVladimir Oltean QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0); 735eb4733d7SVladimir Oltean 736eb4733d7SVladimir Oltean /* Wait at least the time it takes to receive a frame of maximum length 737eb4733d7SVladimir Oltean * at the port. 738eb4733d7SVladimir Oltean * Worst-case delays for 10 kilobyte jumbo frames are: 739eb4733d7SVladimir Oltean * 8 ms on a 10M port 740eb4733d7SVladimir Oltean * 800 μs on a 100M port 741eb4733d7SVladimir Oltean * 80 μs on a 1G port 742eb4733d7SVladimir Oltean * 32 μs on a 2.5G port 743eb4733d7SVladimir Oltean */ 744eb4733d7SVladimir Oltean usleep_range(8000, 10000); 745eb4733d7SVladimir Oltean 746eb4733d7SVladimir Oltean /* Disable half duplex backpressure. */ 747eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE, 748eb4733d7SVladimir Oltean SYS_FRONT_PORT_MODE, port); 749eb4733d7SVladimir Oltean 750eb4733d7SVladimir Oltean /* Flush the queues associated with the port. */ 751eb4733d7SVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA, 752eb4733d7SVladimir Oltean REW_PORT_CFG, port); 753eb4733d7SVladimir Oltean 754eb4733d7SVladimir Oltean /* Enable dequeuing from the egress queues. */ 755eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE, 756eb4733d7SVladimir Oltean port); 757eb4733d7SVladimir Oltean 758eb4733d7SVladimir Oltean /* Wait until flushing is complete. */ 759eb4733d7SVladimir Oltean err = read_poll_timeout(ocelot_read_eq_avail, val, !val, 760eb4733d7SVladimir Oltean 100, 2000000, false, ocelot, port); 761eb4733d7SVladimir Oltean 762eb4733d7SVladimir Oltean /* Clear flushing again. */ 763eb4733d7SVladimir Oltean ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port); 764eb4733d7SVladimir Oltean 7651650bdb1SVladimir Oltean /* Re-enable flow control */ 7661650bdb1SVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena); 7671650bdb1SVladimir Oltean 768eb4733d7SVladimir Oltean return err; 769eb4733d7SVladimir Oltean } 770eb4733d7SVladimir Oltean 771e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port, 772e6e12df6SVladimir Oltean unsigned int link_an_mode, 773e6e12df6SVladimir Oltean phy_interface_t interface, 774e6e12df6SVladimir Oltean unsigned long quirks) 775a556c76aSAlexandre Belloni { 77626f4dbabSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 777e6e12df6SVladimir Oltean int err; 778a556c76aSAlexandre Belloni 7798abe1970SVladimir Oltean ocelot_port->speed = SPEED_UNKNOWN; 7808abe1970SVladimir Oltean 781e6e12df6SVladimir Oltean ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA, 782e6e12df6SVladimir Oltean DEV_MAC_ENA_CFG); 783e6e12df6SVladimir Oltean 7848abe1970SVladimir Oltean if (ocelot->ops->cut_through_fwd) { 7858abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 7868abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 7878abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 7888abe1970SVladimir Oltean } 7898abe1970SVladimir Oltean 790e6e12df6SVladimir Oltean ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); 791e6e12df6SVladimir Oltean 792e6e12df6SVladimir Oltean err = ocelot_port_flush(ocelot, port); 793e6e12df6SVladimir Oltean if (err) 794e6e12df6SVladimir Oltean dev_err(ocelot->dev, "failed to flush port %d: %d\n", 795e6e12df6SVladimir Oltean port, err); 796e6e12df6SVladimir Oltean 797e6e12df6SVladimir Oltean /* Put the port in reset. */ 798e6e12df6SVladimir Oltean if (interface != PHY_INTERFACE_MODE_QSGMII || 799e6e12df6SVladimir Oltean !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP)) 800e6e12df6SVladimir Oltean ocelot_port_rmwl(ocelot_port, 801e6e12df6SVladimir Oltean DEV_CLOCK_CFG_MAC_TX_RST | 80274a3bc42SWan Jiabing DEV_CLOCK_CFG_MAC_RX_RST, 803e6e12df6SVladimir Oltean DEV_CLOCK_CFG_MAC_TX_RST | 80474a3bc42SWan Jiabing DEV_CLOCK_CFG_MAC_RX_RST, 805e6e12df6SVladimir Oltean DEV_CLOCK_CFG); 806e6e12df6SVladimir Oltean } 807e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down); 808e6e12df6SVladimir Oltean 809e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port, 810e6e12df6SVladimir Oltean struct phy_device *phydev, 811e6e12df6SVladimir Oltean unsigned int link_an_mode, 812e6e12df6SVladimir Oltean phy_interface_t interface, 813e6e12df6SVladimir Oltean int speed, int duplex, 814e6e12df6SVladimir Oltean bool tx_pause, bool rx_pause, 815e6e12df6SVladimir Oltean unsigned long quirks) 816e6e12df6SVladimir Oltean { 817e6e12df6SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 818e6e12df6SVladimir Oltean int mac_speed, mode = 0; 819e6e12df6SVladimir Oltean u32 mac_fc_cfg; 820e6e12df6SVladimir Oltean 8218abe1970SVladimir Oltean ocelot_port->speed = speed; 8228abe1970SVladimir Oltean 823e6e12df6SVladimir Oltean /* The MAC might be integrated in systems where the MAC speed is fixed 824e6e12df6SVladimir Oltean * and it's the PCS who is performing the rate adaptation, so we have 825e6e12df6SVladimir Oltean * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG 826e6e12df6SVladimir Oltean * (which is also its default value). 827e6e12df6SVladimir Oltean */ 828e6e12df6SVladimir Oltean if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) || 829e6e12df6SVladimir Oltean speed == SPEED_1000) { 830e6e12df6SVladimir Oltean mac_speed = OCELOT_SPEED_1000; 831e6e12df6SVladimir Oltean mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 832e6e12df6SVladimir Oltean } else if (speed == SPEED_2500) { 833e6e12df6SVladimir Oltean mac_speed = OCELOT_SPEED_2500; 834e6e12df6SVladimir Oltean mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 835e6e12df6SVladimir Oltean } else if (speed == SPEED_100) { 836e6e12df6SVladimir Oltean mac_speed = OCELOT_SPEED_100; 837e6e12df6SVladimir Oltean } else { 838e6e12df6SVladimir Oltean mac_speed = OCELOT_SPEED_10; 839e6e12df6SVladimir Oltean } 840e6e12df6SVladimir Oltean 841e6e12df6SVladimir Oltean if (duplex == DUPLEX_FULL) 842e6e12df6SVladimir Oltean mode |= DEV_MAC_MODE_CFG_FDX_ENA; 843e6e12df6SVladimir Oltean 844e6e12df6SVladimir Oltean ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG); 845e6e12df6SVladimir Oltean 846e6e12df6SVladimir Oltean /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and 847e6e12df6SVladimir Oltean * PORT_RST bits in DEV_CLOCK_CFG. 848e6e12df6SVladimir Oltean */ 849e6e12df6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed), 850e6e12df6SVladimir Oltean DEV_CLOCK_CFG); 851e6e12df6SVladimir Oltean 852e6e12df6SVladimir Oltean switch (speed) { 853a556c76aSAlexandre Belloni case SPEED_10: 854e6e12df6SVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10); 855a556c76aSAlexandre Belloni break; 856a556c76aSAlexandre Belloni case SPEED_100: 857e6e12df6SVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100); 858a556c76aSAlexandre Belloni break; 859a556c76aSAlexandre Belloni case SPEED_1000: 860a556c76aSAlexandre Belloni case SPEED_2500: 861e6e12df6SVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000); 862a556c76aSAlexandre Belloni break; 863a556c76aSAlexandre Belloni default: 864e6e12df6SVladimir Oltean dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n", 865e6e12df6SVladimir Oltean port, speed); 866a556c76aSAlexandre Belloni return; 867a556c76aSAlexandre Belloni } 868a556c76aSAlexandre Belloni 869e6e12df6SVladimir Oltean /* Handle RX pause in all cases, with 2500base-X this is used for rate 870e6e12df6SVladimir Oltean * adaptation. 871e6e12df6SVladimir Oltean */ 872e6e12df6SVladimir Oltean mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA; 873a556c76aSAlexandre Belloni 874e6e12df6SVladimir Oltean if (tx_pause) 875e6e12df6SVladimir Oltean mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA | 876e6e12df6SVladimir Oltean SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 877e6e12df6SVladimir Oltean SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 878e6e12df6SVladimir Oltean SYS_MAC_FC_CFG_ZERO_PAUSE_ENA; 879a556c76aSAlexandre Belloni 880e6e12df6SVladimir Oltean /* Flow control. Link speed is only used here to evaluate the time 881e6e12df6SVladimir Oltean * specification in incoming pause frames. 882e6e12df6SVladimir Oltean */ 883e6e12df6SVladimir Oltean ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port); 884a556c76aSAlexandre Belloni 885e6e12df6SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); 8861ba8f656SVladimir Oltean 88733cb0ff3SVladimir Oltean /* Don't attempt to send PAUSE frames on the NPI port, it's broken */ 88833cb0ff3SVladimir Oltean if (port != ocelot->npi) 88933cb0ff3SVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 89033cb0ff3SVladimir Oltean tx_pause); 8911ba8f656SVladimir Oltean 892e6e12df6SVladimir Oltean /* Undo the effects of ocelot_phylink_mac_link_down: 893e6e12df6SVladimir Oltean * enable MAC module 894e6e12df6SVladimir Oltean */ 895004d44f6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | 896a556c76aSAlexandre Belloni DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 897a556c76aSAlexandre Belloni 8988abe1970SVladimir Oltean /* If the port supports cut-through forwarding, update the masks before 8998abe1970SVladimir Oltean * enabling forwarding on the port. 9008abe1970SVladimir Oltean */ 9018abe1970SVladimir Oltean if (ocelot->ops->cut_through_fwd) { 9028abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 9038abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 9048abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 9058abe1970SVladimir Oltean } 9068abe1970SVladimir Oltean 907a556c76aSAlexandre Belloni /* Core: Enable port for frame transfer */ 908886e1387SVladimir Oltean ocelot_fields_write(ocelot, port, 909886e1387SVladimir Oltean QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 910a556c76aSAlexandre Belloni } 911e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up); 912889b8950SVladimir Oltean 91352849bcfSVladimir Oltean static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, 914e2f9a8feSVladimir Oltean struct sk_buff *clone) 915400928bfSYangbo Lu { 916e2f9a8feSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 91752849bcfSVladimir Oltean unsigned long flags; 918400928bfSYangbo Lu 91952849bcfSVladimir Oltean spin_lock_irqsave(&ocelot->ts_id_lock, flags); 92052849bcfSVladimir Oltean 92152849bcfSVladimir Oltean if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID || 92252849bcfSVladimir Oltean ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) { 92352849bcfSVladimir Oltean spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); 92452849bcfSVladimir Oltean return -EBUSY; 92552849bcfSVladimir Oltean } 9266565243cSVladimir Oltean 927e2f9a8feSVladimir Oltean skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; 928c4b364ceSYangbo Lu /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */ 929c4b364ceSYangbo Lu OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id; 93052849bcfSVladimir Oltean 931c57fe003SVladimir Oltean ocelot_port->ts_id++; 932c57fe003SVladimir Oltean if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID) 933c57fe003SVladimir Oltean ocelot_port->ts_id = 0; 93452849bcfSVladimir Oltean 93552849bcfSVladimir Oltean ocelot_port->ptp_skbs_in_flight++; 93652849bcfSVladimir Oltean ocelot->ptp_skbs_in_flight++; 93752849bcfSVladimir Oltean 938e2f9a8feSVladimir Oltean skb_queue_tail(&ocelot_port->tx_skbs, clone); 9396565243cSVladimir Oltean 94052849bcfSVladimir Oltean spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); 94152849bcfSVladimir Oltean 94252849bcfSVladimir Oltean return 0; 943400928bfSYangbo Lu } 944682eaad9SYangbo Lu 945fba01283SVladimir Oltean static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb, 946fba01283SVladimir Oltean unsigned int ptp_class) 94739e5308bSYangbo Lu { 94839e5308bSYangbo Lu struct ptp_header *hdr; 94939e5308bSYangbo Lu u8 msgtype, twostep; 95039e5308bSYangbo Lu 95139e5308bSYangbo Lu hdr = ptp_parse_header(skb, ptp_class); 95239e5308bSYangbo Lu if (!hdr) 95339e5308bSYangbo Lu return false; 95439e5308bSYangbo Lu 95539e5308bSYangbo Lu msgtype = ptp_get_msgtype(hdr, ptp_class); 95639e5308bSYangbo Lu twostep = hdr->flag_field[0] & 0x2; 95739e5308bSYangbo Lu 95839e5308bSYangbo Lu if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) 95939e5308bSYangbo Lu return true; 96039e5308bSYangbo Lu 96139e5308bSYangbo Lu return false; 96239e5308bSYangbo Lu } 96339e5308bSYangbo Lu 964682eaad9SYangbo Lu int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port, 965682eaad9SYangbo Lu struct sk_buff *skb, 966682eaad9SYangbo Lu struct sk_buff **clone) 967682eaad9SYangbo Lu { 968682eaad9SYangbo Lu struct ocelot_port *ocelot_port = ocelot->ports[port]; 969682eaad9SYangbo Lu u8 ptp_cmd = ocelot_port->ptp_cmd; 970fba01283SVladimir Oltean unsigned int ptp_class; 97152849bcfSVladimir Oltean int err; 972682eaad9SYangbo Lu 973fba01283SVladimir Oltean /* Don't do anything if PTP timestamping not enabled */ 974fba01283SVladimir Oltean if (!ptp_cmd) 975fba01283SVladimir Oltean return 0; 976fba01283SVladimir Oltean 977fba01283SVladimir Oltean ptp_class = ptp_classify_raw(skb); 978fba01283SVladimir Oltean if (ptp_class == PTP_CLASS_NONE) 979fba01283SVladimir Oltean return -EINVAL; 980682eaad9SYangbo Lu 98139e5308bSYangbo Lu /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */ 98239e5308bSYangbo Lu if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) { 983fba01283SVladimir Oltean if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) { 98439e5308bSYangbo Lu OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; 98539e5308bSYangbo Lu return 0; 98639e5308bSYangbo Lu } 98739e5308bSYangbo Lu 98839e5308bSYangbo Lu /* Fall back to two-step timestamping */ 98939e5308bSYangbo Lu ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 99039e5308bSYangbo Lu } 99139e5308bSYangbo Lu 992682eaad9SYangbo Lu if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) { 993682eaad9SYangbo Lu *clone = skb_clone_sk(skb); 994682eaad9SYangbo Lu if (!(*clone)) 995682eaad9SYangbo Lu return -ENOMEM; 996682eaad9SYangbo Lu 99752849bcfSVladimir Oltean err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone); 99852849bcfSVladimir Oltean if (err) 99952849bcfSVladimir Oltean return err; 100052849bcfSVladimir Oltean 100139e5308bSYangbo Lu OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; 1002ebb4c6a9SVladimir Oltean OCELOT_SKB_CB(*clone)->ptp_class = ptp_class; 1003682eaad9SYangbo Lu } 1004682eaad9SYangbo Lu 1005682eaad9SYangbo Lu return 0; 1006682eaad9SYangbo Lu } 1007682eaad9SYangbo Lu EXPORT_SYMBOL(ocelot_port_txtstamp_request); 1008400928bfSYangbo Lu 1009e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot, 1010e23a7b3eSYangbo Lu struct timespec64 *ts) 10114e3b0468SAntoine Tenart { 10124e3b0468SAntoine Tenart unsigned long flags; 10134e3b0468SAntoine Tenart u32 val; 10144e3b0468SAntoine Tenart 10154e3b0468SAntoine Tenart spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); 10164e3b0468SAntoine Tenart 10174e3b0468SAntoine Tenart /* Read current PTP time to get seconds */ 10184e3b0468SAntoine Tenart val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); 10194e3b0468SAntoine Tenart 10204e3b0468SAntoine Tenart val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); 10214e3b0468SAntoine Tenart val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); 10224e3b0468SAntoine Tenart ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); 10234e3b0468SAntoine Tenart ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); 10244e3b0468SAntoine Tenart 10254e3b0468SAntoine Tenart /* Read packet HW timestamp from FIFO */ 10264e3b0468SAntoine Tenart val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); 10274e3b0468SAntoine Tenart ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); 10284e3b0468SAntoine Tenart 10294e3b0468SAntoine Tenart /* Sec has incremented since the ts was registered */ 10304e3b0468SAntoine Tenart if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) 10314e3b0468SAntoine Tenart ts->tv_sec--; 10324e3b0468SAntoine Tenart 10334e3b0468SAntoine Tenart spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); 10344e3b0468SAntoine Tenart } 1035e23a7b3eSYangbo Lu 1036ebb4c6a9SVladimir Oltean static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid) 1037ebb4c6a9SVladimir Oltean { 1038ebb4c6a9SVladimir Oltean struct ptp_header *hdr; 1039ebb4c6a9SVladimir Oltean 1040ebb4c6a9SVladimir Oltean hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class); 1041ebb4c6a9SVladimir Oltean if (WARN_ON(!hdr)) 1042ebb4c6a9SVladimir Oltean return false; 1043ebb4c6a9SVladimir Oltean 1044ebb4c6a9SVladimir Oltean return seqid == ntohs(hdr->sequence_id); 1045ebb4c6a9SVladimir Oltean } 1046ebb4c6a9SVladimir Oltean 1047e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot) 1048e23a7b3eSYangbo Lu { 1049e23a7b3eSYangbo Lu int budget = OCELOT_PTP_QUEUE_SZ; 1050e23a7b3eSYangbo Lu 1051e23a7b3eSYangbo Lu while (budget--) { 1052b049da13SYangbo Lu struct sk_buff *skb, *skb_tmp, *skb_match = NULL; 1053e23a7b3eSYangbo Lu struct skb_shared_hwtstamps shhwtstamps; 1054ebb4c6a9SVladimir Oltean u32 val, id, seqid, txport; 1055e23a7b3eSYangbo Lu struct ocelot_port *port; 1056e23a7b3eSYangbo Lu struct timespec64 ts; 1057b049da13SYangbo Lu unsigned long flags; 1058e23a7b3eSYangbo Lu 1059e23a7b3eSYangbo Lu val = ocelot_read(ocelot, SYS_PTP_STATUS); 1060e23a7b3eSYangbo Lu 1061e23a7b3eSYangbo Lu /* Check if a timestamp can be retrieved */ 1062e23a7b3eSYangbo Lu if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) 1063e23a7b3eSYangbo Lu break; 1064e23a7b3eSYangbo Lu 1065e23a7b3eSYangbo Lu WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); 1066e23a7b3eSYangbo Lu 1067e23a7b3eSYangbo Lu /* Retrieve the ts ID and Tx port */ 1068e23a7b3eSYangbo Lu id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); 1069e23a7b3eSYangbo Lu txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); 1070ebb4c6a9SVladimir Oltean seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val); 1071e23a7b3eSYangbo Lu 1072e23a7b3eSYangbo Lu port = ocelot->ports[txport]; 1073e23a7b3eSYangbo Lu 107452849bcfSVladimir Oltean spin_lock(&ocelot->ts_id_lock); 107552849bcfSVladimir Oltean port->ptp_skbs_in_flight--; 107652849bcfSVladimir Oltean ocelot->ptp_skbs_in_flight--; 107752849bcfSVladimir Oltean spin_unlock(&ocelot->ts_id_lock); 107852849bcfSVladimir Oltean 107952849bcfSVladimir Oltean /* Retrieve its associated skb */ 1080ebb4c6a9SVladimir Oltean try_again: 1081b049da13SYangbo Lu spin_lock_irqsave(&port->tx_skbs.lock, flags); 1082b049da13SYangbo Lu 1083b049da13SYangbo Lu skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { 1084c4b364ceSYangbo Lu if (OCELOT_SKB_CB(skb)->ts_id != id) 1085e23a7b3eSYangbo Lu continue; 1086b049da13SYangbo Lu __skb_unlink(skb, &port->tx_skbs); 1087b049da13SYangbo Lu skb_match = skb; 1088fc62c094SYangbo Lu break; 1089e23a7b3eSYangbo Lu } 1090e23a7b3eSYangbo Lu 1091b049da13SYangbo Lu spin_unlock_irqrestore(&port->tx_skbs.lock, flags); 1092b049da13SYangbo Lu 10939fde506eSVladimir Oltean if (WARN_ON(!skb_match)) 10949fde506eSVladimir Oltean continue; 10959fde506eSVladimir Oltean 1096ebb4c6a9SVladimir Oltean if (!ocelot_validate_ptp_skb(skb_match, seqid)) { 1097ebb4c6a9SVladimir Oltean dev_err_ratelimited(ocelot->dev, 1098ebb4c6a9SVladimir Oltean "port %d received stale TX timestamp for seqid %d, discarding\n", 1099ebb4c6a9SVladimir Oltean txport, seqid); 1100ebb4c6a9SVladimir Oltean dev_kfree_skb_any(skb); 1101ebb4c6a9SVladimir Oltean goto try_again; 1102ebb4c6a9SVladimir Oltean } 1103ebb4c6a9SVladimir Oltean 11045fd82200Slaurent brando /* Get the h/w timestamp */ 11055fd82200Slaurent brando ocelot_get_hwtimestamp(ocelot, &ts); 1106e23a7b3eSYangbo Lu 1107e23a7b3eSYangbo Lu /* Set the timestamp into the skb */ 1108e23a7b3eSYangbo Lu memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 1109e23a7b3eSYangbo Lu shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1110e2f9a8feSVladimir Oltean skb_complete_tx_timestamp(skb_match, &shhwtstamps); 11115fd82200Slaurent brando 11125fd82200Slaurent brando /* Next ts */ 11135fd82200Slaurent brando ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); 1114e23a7b3eSYangbo Lu } 1115e23a7b3eSYangbo Lu } 1116e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp); 11174e3b0468SAntoine Tenart 1118924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh, 1119924ee317SVladimir Oltean u32 *rval) 1120924ee317SVladimir Oltean { 1121924ee317SVladimir Oltean u32 bytes_valid, val; 1122924ee317SVladimir Oltean 1123924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1124924ee317SVladimir Oltean if (val == XTR_NOT_READY) { 1125924ee317SVladimir Oltean if (ifh) 1126924ee317SVladimir Oltean return -EIO; 1127924ee317SVladimir Oltean 1128924ee317SVladimir Oltean do { 1129924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1130924ee317SVladimir Oltean } while (val == XTR_NOT_READY); 1131924ee317SVladimir Oltean } 1132924ee317SVladimir Oltean 1133924ee317SVladimir Oltean switch (val) { 1134924ee317SVladimir Oltean case XTR_ABORT: 1135924ee317SVladimir Oltean return -EIO; 1136924ee317SVladimir Oltean case XTR_EOF_0: 1137924ee317SVladimir Oltean case XTR_EOF_1: 1138924ee317SVladimir Oltean case XTR_EOF_2: 1139924ee317SVladimir Oltean case XTR_EOF_3: 1140924ee317SVladimir Oltean case XTR_PRUNED: 1141924ee317SVladimir Oltean bytes_valid = XTR_VALID_BYTES(val); 1142924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1143924ee317SVladimir Oltean if (val == XTR_ESCAPE) 1144924ee317SVladimir Oltean *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1145924ee317SVladimir Oltean else 1146924ee317SVladimir Oltean *rval = val; 1147924ee317SVladimir Oltean 1148924ee317SVladimir Oltean return bytes_valid; 1149924ee317SVladimir Oltean case XTR_ESCAPE: 1150924ee317SVladimir Oltean *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1151924ee317SVladimir Oltean 1152924ee317SVladimir Oltean return 4; 1153924ee317SVladimir Oltean default: 1154924ee317SVladimir Oltean *rval = val; 1155924ee317SVladimir Oltean 1156924ee317SVladimir Oltean return 4; 1157924ee317SVladimir Oltean } 1158924ee317SVladimir Oltean } 1159924ee317SVladimir Oltean 1160924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh) 1161924ee317SVladimir Oltean { 1162924ee317SVladimir Oltean int i, err = 0; 1163924ee317SVladimir Oltean 1164924ee317SVladimir Oltean for (i = 0; i < OCELOT_TAG_LEN / 4; i++) { 1165924ee317SVladimir Oltean err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]); 1166924ee317SVladimir Oltean if (err != 4) 1167924ee317SVladimir Oltean return (err < 0) ? err : -EIO; 1168924ee317SVladimir Oltean } 1169924ee317SVladimir Oltean 1170924ee317SVladimir Oltean return 0; 1171924ee317SVladimir Oltean } 1172924ee317SVladimir Oltean 1173b471a71eSClément Léger void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb, 1174b471a71eSClément Léger u64 timestamp) 1175924ee317SVladimir Oltean { 1176924ee317SVladimir Oltean struct skb_shared_hwtstamps *shhwtstamps; 11772ed2c5f0SHoratiu Vultur u64 tod_in_ns, full_ts_in_ns; 1178b471a71eSClément Léger struct timespec64 ts; 1179b471a71eSClément Léger 1180b471a71eSClément Léger ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1181b471a71eSClément Léger 1182b471a71eSClément Léger tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec); 1183b471a71eSClément Léger if ((tod_in_ns & 0xffffffff) < timestamp) 1184b471a71eSClément Léger full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) | 1185b471a71eSClément Léger timestamp; 1186b471a71eSClément Léger else 1187b471a71eSClément Léger full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) | 1188b471a71eSClément Léger timestamp; 1189b471a71eSClément Léger 1190b471a71eSClément Léger shhwtstamps = skb_hwtstamps(skb); 1191b471a71eSClément Léger memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1192b471a71eSClément Léger shhwtstamps->hwtstamp = full_ts_in_ns; 1193b471a71eSClément Léger } 1194b471a71eSClément Léger EXPORT_SYMBOL(ocelot_ptp_rx_timestamp); 1195b471a71eSClément Léger 1196b471a71eSClément Léger int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb) 1197b471a71eSClément Léger { 1198924ee317SVladimir Oltean u64 timestamp, src_port, len; 1199924ee317SVladimir Oltean u32 xfh[OCELOT_TAG_LEN / 4]; 1200924ee317SVladimir Oltean struct net_device *dev; 1201924ee317SVladimir Oltean struct sk_buff *skb; 1202924ee317SVladimir Oltean int sz, buf_len; 1203924ee317SVladimir Oltean u32 val, *buf; 1204924ee317SVladimir Oltean int err; 1205924ee317SVladimir Oltean 1206924ee317SVladimir Oltean err = ocelot_xtr_poll_xfh(ocelot, grp, xfh); 1207924ee317SVladimir Oltean if (err) 1208924ee317SVladimir Oltean return err; 1209924ee317SVladimir Oltean 1210924ee317SVladimir Oltean ocelot_xfh_get_src_port(xfh, &src_port); 1211924ee317SVladimir Oltean ocelot_xfh_get_len(xfh, &len); 1212924ee317SVladimir Oltean ocelot_xfh_get_rew_val(xfh, ×tamp); 1213924ee317SVladimir Oltean 1214924ee317SVladimir Oltean if (WARN_ON(src_port >= ocelot->num_phys_ports)) 1215924ee317SVladimir Oltean return -EINVAL; 1216924ee317SVladimir Oltean 1217924ee317SVladimir Oltean dev = ocelot->ops->port_to_netdev(ocelot, src_port); 1218924ee317SVladimir Oltean if (!dev) 1219924ee317SVladimir Oltean return -EINVAL; 1220924ee317SVladimir Oltean 1221924ee317SVladimir Oltean skb = netdev_alloc_skb(dev, len); 1222924ee317SVladimir Oltean if (unlikely(!skb)) { 1223924ee317SVladimir Oltean netdev_err(dev, "Unable to allocate sk_buff\n"); 1224924ee317SVladimir Oltean return -ENOMEM; 1225924ee317SVladimir Oltean } 1226924ee317SVladimir Oltean 1227924ee317SVladimir Oltean buf_len = len - ETH_FCS_LEN; 1228924ee317SVladimir Oltean buf = (u32 *)skb_put(skb, buf_len); 1229924ee317SVladimir Oltean 1230924ee317SVladimir Oltean len = 0; 1231924ee317SVladimir Oltean do { 1232924ee317SVladimir Oltean sz = ocelot_rx_frame_word(ocelot, grp, false, &val); 1233924ee317SVladimir Oltean if (sz < 0) { 1234924ee317SVladimir Oltean err = sz; 1235924ee317SVladimir Oltean goto out_free_skb; 1236924ee317SVladimir Oltean } 1237924ee317SVladimir Oltean *buf++ = val; 1238924ee317SVladimir Oltean len += sz; 1239924ee317SVladimir Oltean } while (len < buf_len); 1240924ee317SVladimir Oltean 1241924ee317SVladimir Oltean /* Read the FCS */ 1242924ee317SVladimir Oltean sz = ocelot_rx_frame_word(ocelot, grp, false, &val); 1243924ee317SVladimir Oltean if (sz < 0) { 1244924ee317SVladimir Oltean err = sz; 1245924ee317SVladimir Oltean goto out_free_skb; 1246924ee317SVladimir Oltean } 1247924ee317SVladimir Oltean 1248924ee317SVladimir Oltean /* Update the statistics if part of the FCS was read before */ 1249924ee317SVladimir Oltean len -= ETH_FCS_LEN - sz; 1250924ee317SVladimir Oltean 1251924ee317SVladimir Oltean if (unlikely(dev->features & NETIF_F_RXFCS)) { 1252924ee317SVladimir Oltean buf = (u32 *)skb_put(skb, ETH_FCS_LEN); 1253924ee317SVladimir Oltean *buf = val; 1254924ee317SVladimir Oltean } 1255924ee317SVladimir Oltean 1256b471a71eSClément Léger if (ocelot->ptp) 1257b471a71eSClément Léger ocelot_ptp_rx_timestamp(ocelot, skb, timestamp); 1258924ee317SVladimir Oltean 1259924ee317SVladimir Oltean /* Everything we see on an interface that is in the HW bridge 1260924ee317SVladimir Oltean * has already been forwarded. 1261924ee317SVladimir Oltean */ 1262df291e54SVladimir Oltean if (ocelot->ports[src_port]->bridge) 1263924ee317SVladimir Oltean skb->offload_fwd_mark = 1; 1264924ee317SVladimir Oltean 1265924ee317SVladimir Oltean skb->protocol = eth_type_trans(skb, dev); 1266d8ea7ff3SHoratiu Vultur 1267924ee317SVladimir Oltean *nskb = skb; 1268924ee317SVladimir Oltean 1269924ee317SVladimir Oltean return 0; 1270924ee317SVladimir Oltean 1271924ee317SVladimir Oltean out_free_skb: 1272924ee317SVladimir Oltean kfree_skb(skb); 1273924ee317SVladimir Oltean return err; 1274924ee317SVladimir Oltean } 1275924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame); 1276924ee317SVladimir Oltean 1277137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp) 1278137ffbc4SVladimir Oltean { 1279137ffbc4SVladimir Oltean u32 val = ocelot_read(ocelot, QS_INJ_STATUS); 1280137ffbc4SVladimir Oltean 1281137ffbc4SVladimir Oltean if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp)))) 1282137ffbc4SVladimir Oltean return false; 1283137ffbc4SVladimir Oltean if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))) 1284137ffbc4SVladimir Oltean return false; 1285137ffbc4SVladimir Oltean 1286137ffbc4SVladimir Oltean return true; 1287137ffbc4SVladimir Oltean } 1288137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject); 1289137ffbc4SVladimir Oltean 1290e5150f00SClément Léger void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag) 1291e5150f00SClément Léger { 1292e5150f00SClément Léger ocelot_ifh_set_bypass(ifh, 1); 1293e5150f00SClément Léger ocelot_ifh_set_dest(ifh, BIT_ULL(port)); 1294e5150f00SClément Léger ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C); 1295e5150f00SClément Léger if (vlan_tag) 1296e5150f00SClément Léger ocelot_ifh_set_vlan_tci(ifh, vlan_tag); 1297e5150f00SClément Léger if (rew_op) 1298e5150f00SClément Léger ocelot_ifh_set_rew_op(ifh, rew_op); 1299e5150f00SClément Léger } 1300e5150f00SClément Léger EXPORT_SYMBOL(ocelot_ifh_port_set); 1301e5150f00SClément Léger 1302137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp, 1303137ffbc4SVladimir Oltean u32 rew_op, struct sk_buff *skb) 1304137ffbc4SVladimir Oltean { 130540d3f295SVladimir Oltean u32 ifh[OCELOT_TAG_LEN / 4] = {0}; 1306137ffbc4SVladimir Oltean unsigned int i, count, last; 1307137ffbc4SVladimir Oltean 1308137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 1309137ffbc4SVladimir Oltean QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp); 1310137ffbc4SVladimir Oltean 1311e5150f00SClément Léger ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb)); 1312137ffbc4SVladimir Oltean 1313137ffbc4SVladimir Oltean for (i = 0; i < OCELOT_TAG_LEN / 4; i++) 131440d3f295SVladimir Oltean ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp); 1315137ffbc4SVladimir Oltean 1316137ffbc4SVladimir Oltean count = DIV_ROUND_UP(skb->len, 4); 1317137ffbc4SVladimir Oltean last = skb->len % 4; 1318137ffbc4SVladimir Oltean for (i = 0; i < count; i++) 1319137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp); 1320137ffbc4SVladimir Oltean 1321137ffbc4SVladimir Oltean /* Add padding */ 1322137ffbc4SVladimir Oltean while (i < (OCELOT_BUFFER_CELL_SZ / 4)) { 1323137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 1324137ffbc4SVladimir Oltean i++; 1325137ffbc4SVladimir Oltean } 1326137ffbc4SVladimir Oltean 1327137ffbc4SVladimir Oltean /* Indicate EOF and valid bytes in last word */ 1328137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 1329137ffbc4SVladimir Oltean QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) | 1330137ffbc4SVladimir Oltean QS_INJ_CTRL_EOF, 1331137ffbc4SVladimir Oltean QS_INJ_CTRL, grp); 1332137ffbc4SVladimir Oltean 1333137ffbc4SVladimir Oltean /* Add dummy CRC */ 1334137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 1335137ffbc4SVladimir Oltean skb_tx_timestamp(skb); 1336137ffbc4SVladimir Oltean 1337137ffbc4SVladimir Oltean skb->dev->stats.tx_packets++; 1338137ffbc4SVladimir Oltean skb->dev->stats.tx_bytes += skb->len; 1339137ffbc4SVladimir Oltean } 1340137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame); 1341137ffbc4SVladimir Oltean 13420a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp) 13430a6f17c6SVladimir Oltean { 13440a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) 13450a6f17c6SVladimir Oltean ocelot_read_rix(ocelot, QS_XTR_RD, grp); 13460a6f17c6SVladimir Oltean } 13470a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue); 13480a6f17c6SVladimir Oltean 134954c31984SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, const unsigned char *addr, 135054c31984SVladimir Oltean u16 vid, const struct net_device *bridge) 1351a556c76aSAlexandre Belloni { 135254c31984SVladimir Oltean if (!vid) 135354c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 135454c31984SVladimir Oltean 1355e9b3ba43SVladimir Oltean return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED); 1356a556c76aSAlexandre Belloni } 13575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add); 1358a556c76aSAlexandre Belloni 135954c31984SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, const unsigned char *addr, 136054c31984SVladimir Oltean u16 vid, const struct net_device *bridge) 1361531ee1a6SVladimir Oltean { 136254c31984SVladimir Oltean if (!vid) 136354c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 136454c31984SVladimir Oltean 1365531ee1a6SVladimir Oltean return ocelot_mact_forget(ocelot, addr, vid); 1366531ee1a6SVladimir Oltean } 13675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del); 1368531ee1a6SVladimir Oltean 13699c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, 1370531ee1a6SVladimir Oltean bool is_static, void *data) 1371a556c76aSAlexandre Belloni { 1372531ee1a6SVladimir Oltean struct ocelot_dump_ctx *dump = data; 1373a556c76aSAlexandre Belloni u32 portid = NETLINK_CB(dump->cb->skb).portid; 1374a556c76aSAlexandre Belloni u32 seq = dump->cb->nlh->nlmsg_seq; 1375a556c76aSAlexandre Belloni struct nlmsghdr *nlh; 1376a556c76aSAlexandre Belloni struct ndmsg *ndm; 1377a556c76aSAlexandre Belloni 1378a556c76aSAlexandre Belloni if (dump->idx < dump->cb->args[2]) 1379a556c76aSAlexandre Belloni goto skip; 1380a556c76aSAlexandre Belloni 1381a556c76aSAlexandre Belloni nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 1382a556c76aSAlexandre Belloni sizeof(*ndm), NLM_F_MULTI); 1383a556c76aSAlexandre Belloni if (!nlh) 1384a556c76aSAlexandre Belloni return -EMSGSIZE; 1385a556c76aSAlexandre Belloni 1386a556c76aSAlexandre Belloni ndm = nlmsg_data(nlh); 1387a556c76aSAlexandre Belloni ndm->ndm_family = AF_BRIDGE; 1388a556c76aSAlexandre Belloni ndm->ndm_pad1 = 0; 1389a556c76aSAlexandre Belloni ndm->ndm_pad2 = 0; 1390a556c76aSAlexandre Belloni ndm->ndm_flags = NTF_SELF; 1391a556c76aSAlexandre Belloni ndm->ndm_type = 0; 1392a556c76aSAlexandre Belloni ndm->ndm_ifindex = dump->dev->ifindex; 1393531ee1a6SVladimir Oltean ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 1394a556c76aSAlexandre Belloni 1395531ee1a6SVladimir Oltean if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 1396a556c76aSAlexandre Belloni goto nla_put_failure; 1397a556c76aSAlexandre Belloni 1398531ee1a6SVladimir Oltean if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 1399a556c76aSAlexandre Belloni goto nla_put_failure; 1400a556c76aSAlexandre Belloni 1401a556c76aSAlexandre Belloni nlmsg_end(dump->skb, nlh); 1402a556c76aSAlexandre Belloni 1403a556c76aSAlexandre Belloni skip: 1404a556c76aSAlexandre Belloni dump->idx++; 1405a556c76aSAlexandre Belloni return 0; 1406a556c76aSAlexandre Belloni 1407a556c76aSAlexandre Belloni nla_put_failure: 1408a556c76aSAlexandre Belloni nlmsg_cancel(dump->skb, nlh); 1409a556c76aSAlexandre Belloni return -EMSGSIZE; 1410a556c76aSAlexandre Belloni } 14119c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump); 1412a556c76aSAlexandre Belloni 14132468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */ 1414531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, 1415a556c76aSAlexandre Belloni struct ocelot_mact_entry *entry) 1416a556c76aSAlexandre Belloni { 1417a556c76aSAlexandre Belloni u32 val, dst, macl, mach; 1418531ee1a6SVladimir Oltean char mac[ETH_ALEN]; 1419a556c76aSAlexandre Belloni 1420a556c76aSAlexandre Belloni /* Set row and column to read from */ 1421a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); 1422a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); 1423a556c76aSAlexandre Belloni 1424a556c76aSAlexandre Belloni /* Issue a read command */ 1425a556c76aSAlexandre Belloni ocelot_write(ocelot, 1426a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 1427a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 1428a556c76aSAlexandre Belloni 1429a556c76aSAlexandre Belloni if (ocelot_mact_wait_for_completion(ocelot)) 1430a556c76aSAlexandre Belloni return -ETIMEDOUT; 1431a556c76aSAlexandre Belloni 1432a556c76aSAlexandre Belloni /* Read the entry flags */ 1433a556c76aSAlexandre Belloni val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 1434a556c76aSAlexandre Belloni if (!(val & ANA_TABLES_MACACCESS_VALID)) 1435a556c76aSAlexandre Belloni return -EINVAL; 1436a556c76aSAlexandre Belloni 1437a556c76aSAlexandre Belloni /* If the entry read has another port configured as its destination, 1438a556c76aSAlexandre Belloni * do not report it. 1439a556c76aSAlexandre Belloni */ 1440a556c76aSAlexandre Belloni dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; 1441531ee1a6SVladimir Oltean if (dst != port) 1442a556c76aSAlexandre Belloni return -EINVAL; 1443a556c76aSAlexandre Belloni 1444a556c76aSAlexandre Belloni /* Get the entry's MAC address and VLAN id */ 1445a556c76aSAlexandre Belloni macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); 1446a556c76aSAlexandre Belloni mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); 1447a556c76aSAlexandre Belloni 1448a556c76aSAlexandre Belloni mac[0] = (mach >> 8) & 0xff; 1449a556c76aSAlexandre Belloni mac[1] = (mach >> 0) & 0xff; 1450a556c76aSAlexandre Belloni mac[2] = (macl >> 24) & 0xff; 1451a556c76aSAlexandre Belloni mac[3] = (macl >> 16) & 0xff; 1452a556c76aSAlexandre Belloni mac[4] = (macl >> 8) & 0xff; 1453a556c76aSAlexandre Belloni mac[5] = (macl >> 0) & 0xff; 1454a556c76aSAlexandre Belloni 1455a556c76aSAlexandre Belloni entry->vid = (mach >> 16) & 0xfff; 1456a556c76aSAlexandre Belloni ether_addr_copy(entry->mac, mac); 1457a556c76aSAlexandre Belloni 1458a556c76aSAlexandre Belloni return 0; 1459a556c76aSAlexandre Belloni } 1460a556c76aSAlexandre Belloni 14615cad43a5SVladimir Oltean int ocelot_mact_flush(struct ocelot *ocelot, int port) 14625cad43a5SVladimir Oltean { 14635cad43a5SVladimir Oltean int err; 14645cad43a5SVladimir Oltean 14655cad43a5SVladimir Oltean mutex_lock(&ocelot->mact_lock); 14665cad43a5SVladimir Oltean 14675cad43a5SVladimir Oltean /* Program ageing filter for a single port */ 14685cad43a5SVladimir Oltean ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port), 14695cad43a5SVladimir Oltean ANA_ANAGEFIL); 14705cad43a5SVladimir Oltean 14715cad43a5SVladimir Oltean /* Flushing dynamic FDB entries requires two successive age scans */ 14725cad43a5SVladimir Oltean ocelot_write(ocelot, 14735cad43a5SVladimir Oltean ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE), 14745cad43a5SVladimir Oltean ANA_TABLES_MACACCESS); 14755cad43a5SVladimir Oltean 14765cad43a5SVladimir Oltean err = ocelot_mact_wait_for_completion(ocelot); 14775cad43a5SVladimir Oltean if (err) { 14785cad43a5SVladimir Oltean mutex_unlock(&ocelot->mact_lock); 14795cad43a5SVladimir Oltean return err; 14805cad43a5SVladimir Oltean } 14815cad43a5SVladimir Oltean 14825cad43a5SVladimir Oltean /* And second... */ 14835cad43a5SVladimir Oltean ocelot_write(ocelot, 14845cad43a5SVladimir Oltean ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE), 14855cad43a5SVladimir Oltean ANA_TABLES_MACACCESS); 14865cad43a5SVladimir Oltean 14875cad43a5SVladimir Oltean err = ocelot_mact_wait_for_completion(ocelot); 14885cad43a5SVladimir Oltean 14895cad43a5SVladimir Oltean /* Restore ageing filter */ 14905cad43a5SVladimir Oltean ocelot_write(ocelot, 0, ANA_ANAGEFIL); 14915cad43a5SVladimir Oltean 14925cad43a5SVladimir Oltean mutex_unlock(&ocelot->mact_lock); 14935cad43a5SVladimir Oltean 14945cad43a5SVladimir Oltean return err; 14955cad43a5SVladimir Oltean } 14965cad43a5SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_mact_flush); 14975cad43a5SVladimir Oltean 14985e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port, 1499531ee1a6SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1500a556c76aSAlexandre Belloni { 15012468346cSVladimir Oltean int err = 0; 1502531ee1a6SVladimir Oltean int i, j; 1503a556c76aSAlexandre Belloni 15042468346cSVladimir Oltean /* We could take the lock just around ocelot_mact_read, but doing so 15052468346cSVladimir Oltean * thousands of times in a row seems rather pointless and inefficient. 15062468346cSVladimir Oltean */ 15072468346cSVladimir Oltean mutex_lock(&ocelot->mact_lock); 15082468346cSVladimir Oltean 150921ce7f3eSVladimir Oltean /* Loop through all the mac tables entries. */ 151021ce7f3eSVladimir Oltean for (i = 0; i < ocelot->num_mact_rows; i++) { 1511a556c76aSAlexandre Belloni for (j = 0; j < 4; j++) { 1512531ee1a6SVladimir Oltean struct ocelot_mact_entry entry; 1513531ee1a6SVladimir Oltean bool is_static; 1514531ee1a6SVladimir Oltean 15152468346cSVladimir Oltean err = ocelot_mact_read(ocelot, port, i, j, &entry); 1516a556c76aSAlexandre Belloni /* If the entry is invalid (wrong port, invalid...), 1517a556c76aSAlexandre Belloni * skip it. 1518a556c76aSAlexandre Belloni */ 15192468346cSVladimir Oltean if (err == -EINVAL) 1520a556c76aSAlexandre Belloni continue; 15212468346cSVladimir Oltean else if (err) 15222468346cSVladimir Oltean break; 1523a556c76aSAlexandre Belloni 1524531ee1a6SVladimir Oltean is_static = (entry.type == ENTRYTYPE_LOCKED); 1525531ee1a6SVladimir Oltean 152654c31984SVladimir Oltean /* Hide the reserved VLANs used for 152754c31984SVladimir Oltean * VLAN-unaware bridging. 152854c31984SVladimir Oltean */ 152954c31984SVladimir Oltean if (entry.vid > OCELOT_RSV_VLAN_RANGE_START) 153054c31984SVladimir Oltean entry.vid = 0; 153154c31984SVladimir Oltean 15322468346cSVladimir Oltean err = cb(entry.mac, entry.vid, is_static, data); 15332468346cSVladimir Oltean if (err) 15342468346cSVladimir Oltean break; 1535a556c76aSAlexandre Belloni } 1536a556c76aSAlexandre Belloni } 1537a556c76aSAlexandre Belloni 15382468346cSVladimir Oltean mutex_unlock(&ocelot->mact_lock); 15392468346cSVladimir Oltean 15402468346cSVladimir Oltean return err; 1541531ee1a6SVladimir Oltean } 15425e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump); 1543531ee1a6SVladimir Oltean 154496ca08c0SVladimir Oltean static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap) 154596ca08c0SVladimir Oltean { 154696ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_ETYPE; 154796ca08c0SVladimir Oltean *(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588); 154896ca08c0SVladimir Oltean *(__be16 *)trap->key.etype.etype.mask = htons(0xffff); 154996ca08c0SVladimir Oltean } 155096ca08c0SVladimir Oltean 155196ca08c0SVladimir Oltean static void 155296ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap) 155396ca08c0SVladimir Oltean { 155496ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_IPV4; 155559085208SVladimir Oltean trap->key.ipv4.proto.value[0] = IPPROTO_UDP; 155659085208SVladimir Oltean trap->key.ipv4.proto.mask[0] = 0xff; 155796ca08c0SVladimir Oltean trap->key.ipv4.dport.value = PTP_EV_PORT; 155896ca08c0SVladimir Oltean trap->key.ipv4.dport.mask = 0xffff; 155996ca08c0SVladimir Oltean } 156096ca08c0SVladimir Oltean 156196ca08c0SVladimir Oltean static void 156296ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap) 156396ca08c0SVladimir Oltean { 156496ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_IPV6; 156559085208SVladimir Oltean trap->key.ipv4.proto.value[0] = IPPROTO_UDP; 156659085208SVladimir Oltean trap->key.ipv4.proto.mask[0] = 0xff; 156796ca08c0SVladimir Oltean trap->key.ipv6.dport.value = PTP_EV_PORT; 156896ca08c0SVladimir Oltean trap->key.ipv6.dport.mask = 0xffff; 156996ca08c0SVladimir Oltean } 157096ca08c0SVladimir Oltean 157196ca08c0SVladimir Oltean static void 157296ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap) 157396ca08c0SVladimir Oltean { 157496ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_IPV4; 157559085208SVladimir Oltean trap->key.ipv4.proto.value[0] = IPPROTO_UDP; 157659085208SVladimir Oltean trap->key.ipv4.proto.mask[0] = 0xff; 157796ca08c0SVladimir Oltean trap->key.ipv4.dport.value = PTP_GEN_PORT; 157896ca08c0SVladimir Oltean trap->key.ipv4.dport.mask = 0xffff; 157996ca08c0SVladimir Oltean } 158096ca08c0SVladimir Oltean 158196ca08c0SVladimir Oltean static void 158296ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap) 158396ca08c0SVladimir Oltean { 158496ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_IPV6; 158559085208SVladimir Oltean trap->key.ipv4.proto.value[0] = IPPROTO_UDP; 158659085208SVladimir Oltean trap->key.ipv4.proto.mask[0] = 0xff; 158796ca08c0SVladimir Oltean trap->key.ipv6.dport.value = PTP_GEN_PORT; 158896ca08c0SVladimir Oltean trap->key.ipv6.dport.mask = 0xffff; 158996ca08c0SVladimir Oltean } 159096ca08c0SVladimir Oltean 15919d75b881SVladimir Oltean int ocelot_trap_add(struct ocelot *ocelot, int port, 15929d75b881SVladimir Oltean unsigned long cookie, bool take_ts, 159396ca08c0SVladimir Oltean void (*populate)(struct ocelot_vcap_filter *f)) 159496ca08c0SVladimir Oltean { 159596ca08c0SVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 159696ca08c0SVladimir Oltean struct ocelot_vcap_filter *trap; 159796ca08c0SVladimir Oltean bool new = false; 159896ca08c0SVladimir Oltean int err; 159996ca08c0SVladimir Oltean 160096ca08c0SVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 160196ca08c0SVladimir Oltean 160296ca08c0SVladimir Oltean trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie, 160396ca08c0SVladimir Oltean false); 160496ca08c0SVladimir Oltean if (!trap) { 160596ca08c0SVladimir Oltean trap = kzalloc(sizeof(*trap), GFP_KERNEL); 160696ca08c0SVladimir Oltean if (!trap) 160796ca08c0SVladimir Oltean return -ENOMEM; 160896ca08c0SVladimir Oltean 160996ca08c0SVladimir Oltean populate(trap); 161096ca08c0SVladimir Oltean trap->prio = 1; 161196ca08c0SVladimir Oltean trap->id.cookie = cookie; 161296ca08c0SVladimir Oltean trap->id.tc_offload = false; 161396ca08c0SVladimir Oltean trap->block_id = VCAP_IS2; 161496ca08c0SVladimir Oltean trap->type = OCELOT_VCAP_FILTER_OFFLOAD; 161596ca08c0SVladimir Oltean trap->lookup = 0; 161696ca08c0SVladimir Oltean trap->action.cpu_copy_ena = true; 161796ca08c0SVladimir Oltean trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 161896ca08c0SVladimir Oltean trap->action.port_mask = 0; 16199d75b881SVladimir Oltean trap->take_ts = take_ts; 1620e1846cffSVladimir Oltean trap->is_trap = true; 162196ca08c0SVladimir Oltean new = true; 162296ca08c0SVladimir Oltean } 162396ca08c0SVladimir Oltean 162496ca08c0SVladimir Oltean trap->ingress_port_mask |= BIT(port); 162596ca08c0SVladimir Oltean 162696ca08c0SVladimir Oltean if (new) 162796ca08c0SVladimir Oltean err = ocelot_vcap_filter_add(ocelot, trap, NULL); 162896ca08c0SVladimir Oltean else 162996ca08c0SVladimir Oltean err = ocelot_vcap_filter_replace(ocelot, trap); 163096ca08c0SVladimir Oltean if (err) { 163196ca08c0SVladimir Oltean trap->ingress_port_mask &= ~BIT(port); 1632e1846cffSVladimir Oltean if (!trap->ingress_port_mask) 163396ca08c0SVladimir Oltean kfree(trap); 163496ca08c0SVladimir Oltean return err; 163596ca08c0SVladimir Oltean } 163696ca08c0SVladimir Oltean 163796ca08c0SVladimir Oltean return 0; 163896ca08c0SVladimir Oltean } 163996ca08c0SVladimir Oltean 1640b9bace6eSVladimir Oltean int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie) 164196ca08c0SVladimir Oltean { 164296ca08c0SVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 164396ca08c0SVladimir Oltean struct ocelot_vcap_filter *trap; 164496ca08c0SVladimir Oltean 164596ca08c0SVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 164696ca08c0SVladimir Oltean 164796ca08c0SVladimir Oltean trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie, 164896ca08c0SVladimir Oltean false); 164996ca08c0SVladimir Oltean if (!trap) 165096ca08c0SVladimir Oltean return 0; 165196ca08c0SVladimir Oltean 165296ca08c0SVladimir Oltean trap->ingress_port_mask &= ~BIT(port); 1653e1846cffSVladimir Oltean if (!trap->ingress_port_mask) 165496ca08c0SVladimir Oltean return ocelot_vcap_filter_del(ocelot, trap); 165596ca08c0SVladimir Oltean 165696ca08c0SVladimir Oltean return ocelot_vcap_filter_replace(ocelot, trap); 165796ca08c0SVladimir Oltean } 165896ca08c0SVladimir Oltean 165996ca08c0SVladimir Oltean static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port) 166096ca08c0SVladimir Oltean { 1661c518afecSVladimir Oltean unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot); 166296ca08c0SVladimir Oltean 16639d75b881SVladimir Oltean return ocelot_trap_add(ocelot, port, l2_cookie, true, 166496ca08c0SVladimir Oltean ocelot_populate_l2_ptp_trap_key); 166596ca08c0SVladimir Oltean } 166696ca08c0SVladimir Oltean 166796ca08c0SVladimir Oltean static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port) 166896ca08c0SVladimir Oltean { 1669c518afecSVladimir Oltean unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot); 167096ca08c0SVladimir Oltean 167196ca08c0SVladimir Oltean return ocelot_trap_del(ocelot, port, l2_cookie); 167296ca08c0SVladimir Oltean } 167396ca08c0SVladimir Oltean 167496ca08c0SVladimir Oltean static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port) 167596ca08c0SVladimir Oltean { 1676c518afecSVladimir Oltean unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot); 1677c518afecSVladimir Oltean unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot); 167896ca08c0SVladimir Oltean int err; 167996ca08c0SVladimir Oltean 16809d75b881SVladimir Oltean err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, true, 168196ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_event_trap_key); 168296ca08c0SVladimir Oltean if (err) 168396ca08c0SVladimir Oltean return err; 168496ca08c0SVladimir Oltean 16859d75b881SVladimir Oltean err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, false, 168696ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_general_trap_key); 168796ca08c0SVladimir Oltean if (err) 168896ca08c0SVladimir Oltean ocelot_trap_del(ocelot, port, ipv4_ev_cookie); 168996ca08c0SVladimir Oltean 169096ca08c0SVladimir Oltean return err; 169196ca08c0SVladimir Oltean } 169296ca08c0SVladimir Oltean 169396ca08c0SVladimir Oltean static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port) 169496ca08c0SVladimir Oltean { 1695c518afecSVladimir Oltean unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot); 1696c518afecSVladimir Oltean unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot); 169796ca08c0SVladimir Oltean int err; 169896ca08c0SVladimir Oltean 169996ca08c0SVladimir Oltean err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie); 170096ca08c0SVladimir Oltean err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie); 170196ca08c0SVladimir Oltean return err; 170296ca08c0SVladimir Oltean } 170396ca08c0SVladimir Oltean 170496ca08c0SVladimir Oltean static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port) 170596ca08c0SVladimir Oltean { 1706c518afecSVladimir Oltean unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot); 1707c518afecSVladimir Oltean unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot); 170896ca08c0SVladimir Oltean int err; 170996ca08c0SVladimir Oltean 17109d75b881SVladimir Oltean err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, true, 171196ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_event_trap_key); 171296ca08c0SVladimir Oltean if (err) 171396ca08c0SVladimir Oltean return err; 171496ca08c0SVladimir Oltean 17159d75b881SVladimir Oltean err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, false, 171696ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_general_trap_key); 171796ca08c0SVladimir Oltean if (err) 171896ca08c0SVladimir Oltean ocelot_trap_del(ocelot, port, ipv6_ev_cookie); 171996ca08c0SVladimir Oltean 172096ca08c0SVladimir Oltean return err; 172196ca08c0SVladimir Oltean } 172296ca08c0SVladimir Oltean 172396ca08c0SVladimir Oltean static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port) 172496ca08c0SVladimir Oltean { 1725c518afecSVladimir Oltean unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot); 1726c518afecSVladimir Oltean unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot); 172796ca08c0SVladimir Oltean int err; 172896ca08c0SVladimir Oltean 172996ca08c0SVladimir Oltean err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie); 173096ca08c0SVladimir Oltean err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie); 173196ca08c0SVladimir Oltean return err; 173296ca08c0SVladimir Oltean } 173396ca08c0SVladimir Oltean 173496ca08c0SVladimir Oltean static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port, 173596ca08c0SVladimir Oltean bool l2, bool l4) 173696ca08c0SVladimir Oltean { 173796ca08c0SVladimir Oltean int err; 173896ca08c0SVladimir Oltean 173996ca08c0SVladimir Oltean if (l2) 174096ca08c0SVladimir Oltean err = ocelot_l2_ptp_trap_add(ocelot, port); 174196ca08c0SVladimir Oltean else 174296ca08c0SVladimir Oltean err = ocelot_l2_ptp_trap_del(ocelot, port); 174396ca08c0SVladimir Oltean if (err) 174496ca08c0SVladimir Oltean return err; 174596ca08c0SVladimir Oltean 174696ca08c0SVladimir Oltean if (l4) { 174796ca08c0SVladimir Oltean err = ocelot_ipv4_ptp_trap_add(ocelot, port); 174896ca08c0SVladimir Oltean if (err) 174996ca08c0SVladimir Oltean goto err_ipv4; 175096ca08c0SVladimir Oltean 175196ca08c0SVladimir Oltean err = ocelot_ipv6_ptp_trap_add(ocelot, port); 175296ca08c0SVladimir Oltean if (err) 175396ca08c0SVladimir Oltean goto err_ipv6; 175496ca08c0SVladimir Oltean } else { 175596ca08c0SVladimir Oltean err = ocelot_ipv4_ptp_trap_del(ocelot, port); 175696ca08c0SVladimir Oltean 175796ca08c0SVladimir Oltean err |= ocelot_ipv6_ptp_trap_del(ocelot, port); 175896ca08c0SVladimir Oltean } 175996ca08c0SVladimir Oltean if (err) 176096ca08c0SVladimir Oltean return err; 176196ca08c0SVladimir Oltean 176296ca08c0SVladimir Oltean return 0; 176396ca08c0SVladimir Oltean 176496ca08c0SVladimir Oltean err_ipv6: 176596ca08c0SVladimir Oltean ocelot_ipv4_ptp_trap_del(ocelot, port); 176696ca08c0SVladimir Oltean err_ipv4: 176796ca08c0SVladimir Oltean if (l2) 176896ca08c0SVladimir Oltean ocelot_l2_ptp_trap_del(ocelot, port); 176996ca08c0SVladimir Oltean return err; 177096ca08c0SVladimir Oltean } 177196ca08c0SVladimir Oltean 1772f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) 17734e3b0468SAntoine Tenart { 17744e3b0468SAntoine Tenart return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, 17754e3b0468SAntoine Tenart sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; 17764e3b0468SAntoine Tenart } 1777f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get); 17784e3b0468SAntoine Tenart 1779f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) 17804e3b0468SAntoine Tenart { 1781306fd44bSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 178296ca08c0SVladimir Oltean bool l2 = false, l4 = false; 17834e3b0468SAntoine Tenart struct hwtstamp_config cfg; 178496ca08c0SVladimir Oltean int err; 17854e3b0468SAntoine Tenart 17864e3b0468SAntoine Tenart if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 17874e3b0468SAntoine Tenart return -EFAULT; 17884e3b0468SAntoine Tenart 17894e3b0468SAntoine Tenart /* Tx type sanity check */ 17904e3b0468SAntoine Tenart switch (cfg.tx_type) { 17914e3b0468SAntoine Tenart case HWTSTAMP_TX_ON: 1792306fd44bSVladimir Oltean ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 17934e3b0468SAntoine Tenart break; 17944e3b0468SAntoine Tenart case HWTSTAMP_TX_ONESTEP_SYNC: 17954e3b0468SAntoine Tenart /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we 17964e3b0468SAntoine Tenart * need to update the origin time. 17974e3b0468SAntoine Tenart */ 1798306fd44bSVladimir Oltean ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; 17994e3b0468SAntoine Tenart break; 18004e3b0468SAntoine Tenart case HWTSTAMP_TX_OFF: 1801306fd44bSVladimir Oltean ocelot_port->ptp_cmd = 0; 18024e3b0468SAntoine Tenart break; 18034e3b0468SAntoine Tenart default: 18044e3b0468SAntoine Tenart return -ERANGE; 18054e3b0468SAntoine Tenart } 18064e3b0468SAntoine Tenart 18074e3b0468SAntoine Tenart mutex_lock(&ocelot->ptp_lock); 18084e3b0468SAntoine Tenart 18094e3b0468SAntoine Tenart switch (cfg.rx_filter) { 18104e3b0468SAntoine Tenart case HWTSTAMP_FILTER_NONE: 18114e3b0468SAntoine Tenart break; 18124e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 18134e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 18144e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 181596ca08c0SVladimir Oltean l4 = true; 181696ca08c0SVladimir Oltean break; 18174e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 18184e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 18194e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 182096ca08c0SVladimir Oltean l2 = true; 182196ca08c0SVladimir Oltean break; 18224e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_EVENT: 18234e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_SYNC: 18244e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 182596ca08c0SVladimir Oltean l2 = true; 182696ca08c0SVladimir Oltean l4 = true; 18274e3b0468SAntoine Tenart break; 18284e3b0468SAntoine Tenart default: 18294e3b0468SAntoine Tenart mutex_unlock(&ocelot->ptp_lock); 18304e3b0468SAntoine Tenart return -ERANGE; 18314e3b0468SAntoine Tenart } 18324e3b0468SAntoine Tenart 183396ca08c0SVladimir Oltean err = ocelot_setup_ptp_traps(ocelot, port, l2, l4); 18349c32950fSLv Ruyi if (err) { 18359c32950fSLv Ruyi mutex_unlock(&ocelot->ptp_lock); 183696ca08c0SVladimir Oltean return err; 18379c32950fSLv Ruyi } 183896ca08c0SVladimir Oltean 183996ca08c0SVladimir Oltean if (l2 && l4) 184096ca08c0SVladimir Oltean cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 184196ca08c0SVladimir Oltean else if (l2) 184296ca08c0SVladimir Oltean cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; 184396ca08c0SVladimir Oltean else if (l4) 184496ca08c0SVladimir Oltean cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; 184596ca08c0SVladimir Oltean else 184696ca08c0SVladimir Oltean cfg.rx_filter = HWTSTAMP_FILTER_NONE; 184796ca08c0SVladimir Oltean 18484e3b0468SAntoine Tenart /* Commit back the result & save it */ 18494e3b0468SAntoine Tenart memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); 18504e3b0468SAntoine Tenart mutex_unlock(&ocelot->ptp_lock); 18514e3b0468SAntoine Tenart 18524e3b0468SAntoine Tenart return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 18534e3b0468SAntoine Tenart } 1854f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set); 18554e3b0468SAntoine Tenart 18565e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) 1857a556c76aSAlexandre Belloni { 1858a556c76aSAlexandre Belloni int i; 1859a556c76aSAlexandre Belloni 1860a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 1861a556c76aSAlexandre Belloni return; 1862a556c76aSAlexandre Belloni 186391904600SVladimir Oltean for (i = 0; i < OCELOT_NUM_STATS; i++) { 186491904600SVladimir Oltean if (ocelot->stats_layout[i].name[0] == '\0') 186591904600SVladimir Oltean continue; 186691904600SVladimir Oltean 1867a556c76aSAlexandre Belloni memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, 1868a556c76aSAlexandre Belloni ETH_GSTRING_LEN); 1869a556c76aSAlexandre Belloni } 187091904600SVladimir Oltean } 18715e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings); 1872a556c76aSAlexandre Belloni 18737fbf6795SColin Foster /* Caller must hold &ocelot->stats_lock */ 1874d87b1c08SColin Foster static int ocelot_port_update_stats(struct ocelot *ocelot, int port) 1875a556c76aSAlexandre Belloni { 187691904600SVladimir Oltean unsigned int idx = port * OCELOT_NUM_STATS; 1877d87b1c08SColin Foster struct ocelot_stats_region *region; 1878d87b1c08SColin Foster int err, j; 1879a556c76aSAlexandre Belloni 1880a556c76aSAlexandre Belloni /* Configure the port to read the stats from */ 1881e27d785eSColin Foster ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port), SYS_STAT_CFG); 1882a556c76aSAlexandre Belloni 1883d87b1c08SColin Foster list_for_each_entry(region, &ocelot->stats_regions, node) { 1884d4c36765SVladimir Oltean err = ocelot_bulk_read(ocelot, region->base, region->buf, 1885d87b1c08SColin Foster region->count); 1886d87b1c08SColin Foster if (err) 1887d87b1c08SColin Foster return err; 1888a556c76aSAlexandre Belloni 1889d87b1c08SColin Foster for (j = 0; j < region->count; j++) { 1890d87b1c08SColin Foster u64 *stat = &ocelot->stats[idx + j]; 1891d87b1c08SColin Foster u64 val = region->buf[j]; 1892a556c76aSAlexandre Belloni 1893d87b1c08SColin Foster if (val < (*stat & U32_MAX)) 1894d87b1c08SColin Foster *stat += (u64)1 << 32; 1895a556c76aSAlexandre Belloni 1896d87b1c08SColin Foster *stat = (*stat & ~(u64)U32_MAX) + val; 1897a556c76aSAlexandre Belloni } 1898d87b1c08SColin Foster 1899d87b1c08SColin Foster idx += region->count; 1900d87b1c08SColin Foster } 1901d87b1c08SColin Foster 1902d87b1c08SColin Foster return err; 19031e1caa97SClaudiu Manoil } 19041e1caa97SClaudiu Manoil 19051e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work) 19061e1caa97SClaudiu Manoil { 19071e1caa97SClaudiu Manoil struct delayed_work *del_work = to_delayed_work(work); 19081e1caa97SClaudiu Manoil struct ocelot *ocelot = container_of(del_work, struct ocelot, 19091e1caa97SClaudiu Manoil stats_work); 1910d87b1c08SColin Foster int i, err; 19111e1caa97SClaudiu Manoil 191222d842e3SVladimir Oltean spin_lock(&ocelot->stats_lock); 1913d87b1c08SColin Foster for (i = 0; i < ocelot->num_phys_ports; i++) { 1914d87b1c08SColin Foster err = ocelot_port_update_stats(ocelot, i); 1915d87b1c08SColin Foster if (err) 1916d87b1c08SColin Foster break; 1917d87b1c08SColin Foster } 191822d842e3SVladimir Oltean spin_unlock(&ocelot->stats_lock); 19191e1caa97SClaudiu Manoil 1920d87b1c08SColin Foster if (err) 1921d87b1c08SColin Foster dev_err(ocelot->dev, "Error %d updating ethtool stats\n", err); 1922d87b1c08SColin Foster 1923a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1924a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 1925a556c76aSAlexandre Belloni } 1926a556c76aSAlexandre Belloni 19275e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) 1928a556c76aSAlexandre Belloni { 1929d87b1c08SColin Foster int i, err; 1930a556c76aSAlexandre Belloni 193122d842e3SVladimir Oltean spin_lock(&ocelot->stats_lock); 19327fbf6795SColin Foster 1933a556c76aSAlexandre Belloni /* check and update now */ 1934d87b1c08SColin Foster err = ocelot_port_update_stats(ocelot, port); 1935a556c76aSAlexandre Belloni 193691904600SVladimir Oltean /* Copy all supported counters */ 193791904600SVladimir Oltean for (i = 0; i < OCELOT_NUM_STATS; i++) { 193891904600SVladimir Oltean int index = port * OCELOT_NUM_STATS + i; 193991904600SVladimir Oltean 194091904600SVladimir Oltean if (ocelot->stats_layout[i].name[0] == '\0') 194191904600SVladimir Oltean continue; 194291904600SVladimir Oltean 194391904600SVladimir Oltean *data++ = ocelot->stats[index]; 194491904600SVladimir Oltean } 19457fbf6795SColin Foster 194622d842e3SVladimir Oltean spin_unlock(&ocelot->stats_lock); 1947d87b1c08SColin Foster 1948d87b1c08SColin Foster if (err) 1949d87b1c08SColin Foster dev_err(ocelot->dev, "Error %d updating ethtool stats\n", err); 1950a556c76aSAlexandre Belloni } 19515e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats); 1952a556c76aSAlexandre Belloni 19535e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) 1954c7282d38SVladimir Oltean { 195591904600SVladimir Oltean int i, num_stats = 0; 195691904600SVladimir Oltean 1957a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 1958a556c76aSAlexandre Belloni return -EOPNOTSUPP; 1959c7282d38SVladimir Oltean 196091904600SVladimir Oltean for (i = 0; i < OCELOT_NUM_STATS; i++) 196191904600SVladimir Oltean if (ocelot->stats_layout[i].name[0] != '\0') 196291904600SVladimir Oltean num_stats++; 196391904600SVladimir Oltean 196491904600SVladimir Oltean return num_stats; 1965a556c76aSAlexandre Belloni } 19665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count); 1967a556c76aSAlexandre Belloni 1968d87b1c08SColin Foster static int ocelot_prepare_stats_regions(struct ocelot *ocelot) 1969d87b1c08SColin Foster { 1970d87b1c08SColin Foster struct ocelot_stats_region *region = NULL; 1971d87b1c08SColin Foster unsigned int last; 1972d87b1c08SColin Foster int i; 1973d87b1c08SColin Foster 1974d87b1c08SColin Foster INIT_LIST_HEAD(&ocelot->stats_regions); 1975d87b1c08SColin Foster 197691904600SVladimir Oltean for (i = 0; i < OCELOT_NUM_STATS; i++) { 197791904600SVladimir Oltean if (ocelot->stats_layout[i].name[0] == '\0') 197891904600SVladimir Oltean continue; 197991904600SVladimir Oltean 1980d4c36765SVladimir Oltean if (region && ocelot->stats_layout[i].reg == last + 4) { 1981d87b1c08SColin Foster region->count++; 1982d87b1c08SColin Foster } else { 1983d87b1c08SColin Foster region = devm_kzalloc(ocelot->dev, sizeof(*region), 1984d87b1c08SColin Foster GFP_KERNEL); 1985d87b1c08SColin Foster if (!region) 1986d87b1c08SColin Foster return -ENOMEM; 1987d87b1c08SColin Foster 1988d4c36765SVladimir Oltean region->base = ocelot->stats_layout[i].reg; 1989d87b1c08SColin Foster region->count = 1; 1990d87b1c08SColin Foster list_add_tail(®ion->node, &ocelot->stats_regions); 1991d87b1c08SColin Foster } 1992d87b1c08SColin Foster 1993d4c36765SVladimir Oltean last = ocelot->stats_layout[i].reg; 1994d87b1c08SColin Foster } 1995d87b1c08SColin Foster 1996d87b1c08SColin Foster list_for_each_entry(region, &ocelot->stats_regions, node) { 1997d87b1c08SColin Foster region->buf = devm_kcalloc(ocelot->dev, region->count, 1998d87b1c08SColin Foster sizeof(*region->buf), GFP_KERNEL); 1999d87b1c08SColin Foster if (!region->buf) 2000d87b1c08SColin Foster return -ENOMEM; 2001d87b1c08SColin Foster } 2002d87b1c08SColin Foster 2003d87b1c08SColin Foster return 0; 2004d87b1c08SColin Foster } 2005d87b1c08SColin Foster 20065e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port, 2007c7282d38SVladimir Oltean struct ethtool_ts_info *info) 2008c7282d38SVladimir Oltean { 20094e3b0468SAntoine Tenart info->phc_index = ocelot->ptp_clock ? 20104e3b0468SAntoine Tenart ptp_clock_index(ocelot->ptp_clock) : -1; 2011d2b09a8eSYangbo Lu if (info->phc_index == -1) { 2012d2b09a8eSYangbo Lu info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 2013d2b09a8eSYangbo Lu SOF_TIMESTAMPING_RX_SOFTWARE | 2014d2b09a8eSYangbo Lu SOF_TIMESTAMPING_SOFTWARE; 2015d2b09a8eSYangbo Lu return 0; 2016d2b09a8eSYangbo Lu } 20174e3b0468SAntoine Tenart info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 20184e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_SOFTWARE | 20194e3b0468SAntoine Tenart SOF_TIMESTAMPING_SOFTWARE | 20204e3b0468SAntoine Tenart SOF_TIMESTAMPING_TX_HARDWARE | 20214e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_HARDWARE | 20224e3b0468SAntoine Tenart SOF_TIMESTAMPING_RAW_HARDWARE; 20234e3b0468SAntoine Tenart info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | 20244e3b0468SAntoine Tenart BIT(HWTSTAMP_TX_ONESTEP_SYNC); 2025c49a35eeSVladimir Oltean info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | 2026c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) | 2027c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 2028c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT); 20294e3b0468SAntoine Tenart 20304e3b0468SAntoine Tenart return 0; 20314e3b0468SAntoine Tenart } 20325e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info); 20334e3b0468SAntoine Tenart 2034a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond) 2035b80af659SVladimir Oltean { 2036b80af659SVladimir Oltean u32 mask = 0; 2037b80af659SVladimir Oltean int port; 2038b80af659SVladimir Oltean 2039961d8b69SVladimir Oltean lockdep_assert_held(&ocelot->fwd_domain_lock); 2040961d8b69SVladimir Oltean 2041b80af659SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2042b80af659SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2043b80af659SVladimir Oltean 2044b80af659SVladimir Oltean if (!ocelot_port) 2045b80af659SVladimir Oltean continue; 2046b80af659SVladimir Oltean 2047a14e6b69SVladimir Oltean if (ocelot_port->bond == bond) 2048b80af659SVladimir Oltean mask |= BIT(port); 2049b80af659SVladimir Oltean } 2050b80af659SVladimir Oltean 2051b80af659SVladimir Oltean return mask; 2052b80af659SVladimir Oltean } 2053b80af659SVladimir Oltean 2054961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical 2055961d8b69SVladimir Oltean * port ID present in that LAG. It may change if that port ever leaves the LAG. 2056961d8b69SVladimir Oltean */ 2057961d8b69SVladimir Oltean static int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond) 2058961d8b69SVladimir Oltean { 2059961d8b69SVladimir Oltean int bond_mask = ocelot_get_bond_mask(ocelot, bond); 2060961d8b69SVladimir Oltean 2061961d8b69SVladimir Oltean if (!bond_mask) 2062961d8b69SVladimir Oltean return -ENOENT; 2063961d8b69SVladimir Oltean 2064961d8b69SVladimir Oltean return __ffs(bond_mask); 2065961d8b69SVladimir Oltean } 2066961d8b69SVladimir Oltean 2067c295f983SVladimir Oltean static u32 ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot *ocelot, 2068c295f983SVladimir Oltean struct ocelot_port *cpu) 2069c295f983SVladimir Oltean { 2070c295f983SVladimir Oltean u32 mask = 0; 2071c295f983SVladimir Oltean int port; 2072c295f983SVladimir Oltean 2073c295f983SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2074c295f983SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2075c295f983SVladimir Oltean 2076c295f983SVladimir Oltean if (!ocelot_port) 2077c295f983SVladimir Oltean continue; 2078c295f983SVladimir Oltean 2079c295f983SVladimir Oltean if (ocelot_port->dsa_8021q_cpu == cpu) 2080c295f983SVladimir Oltean mask |= BIT(port); 2081c295f983SVladimir Oltean } 2082c295f983SVladimir Oltean 2083c295f983SVladimir Oltean return mask; 2084c295f983SVladimir Oltean } 2085c295f983SVladimir Oltean 2086c295f983SVladimir Oltean u32 ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot *ocelot, int port) 2087c295f983SVladimir Oltean { 2088c295f983SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2089c295f983SVladimir Oltean struct ocelot_port *cpu_port = ocelot_port->dsa_8021q_cpu; 2090c295f983SVladimir Oltean 2091c295f983SVladimir Oltean if (!cpu_port) 2092c295f983SVladimir Oltean return 0; 2093c295f983SVladimir Oltean 2094c295f983SVladimir Oltean return BIT(cpu_port->index); 2095c295f983SVladimir Oltean } 2096c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assigned_dsa_8021q_cpu_mask); 2097c295f983SVladimir Oltean 20988abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port) 2099df291e54SVladimir Oltean { 2100acc64f52SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[src_port]; 2101a8bd9fa5SVladimir Oltean const struct net_device *bridge; 2102df291e54SVladimir Oltean u32 mask = 0; 2103df291e54SVladimir Oltean int port; 2104df291e54SVladimir Oltean 2105a8bd9fa5SVladimir Oltean if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING) 2106a8bd9fa5SVladimir Oltean return 0; 2107a8bd9fa5SVladimir Oltean 2108a8bd9fa5SVladimir Oltean bridge = ocelot_port->bridge; 2109a8bd9fa5SVladimir Oltean if (!bridge) 2110acc64f52SVladimir Oltean return 0; 2111acc64f52SVladimir Oltean 2112df291e54SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2113acc64f52SVladimir Oltean ocelot_port = ocelot->ports[port]; 2114df291e54SVladimir Oltean 2115df291e54SVladimir Oltean if (!ocelot_port) 2116df291e54SVladimir Oltean continue; 2117df291e54SVladimir Oltean 2118df291e54SVladimir Oltean if (ocelot_port->stp_state == BR_STATE_FORWARDING && 2119df291e54SVladimir Oltean ocelot_port->bridge == bridge) 2120df291e54SVladimir Oltean mask |= BIT(port); 2121df291e54SVladimir Oltean } 2122df291e54SVladimir Oltean 2123df291e54SVladimir Oltean return mask; 2124df291e54SVladimir Oltean } 21258abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask); 2126df291e54SVladimir Oltean 2127a72e23ddSVladimir Oltean static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining) 2128e21268efSVladimir Oltean { 2129e21268efSVladimir Oltean int port; 2130e21268efSVladimir Oltean 21318abe1970SVladimir Oltean lockdep_assert_held(&ocelot->fwd_domain_lock); 21328abe1970SVladimir Oltean 21338abe1970SVladimir Oltean /* If cut-through forwarding is supported, update the masks before a 21348abe1970SVladimir Oltean * port joins the forwarding domain, to avoid potential underruns if it 21358abe1970SVladimir Oltean * has the highest speed from the new domain. 21368abe1970SVladimir Oltean */ 21378abe1970SVladimir Oltean if (joining && ocelot->ops->cut_through_fwd) 21388abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 21398abe1970SVladimir Oltean 21409b521250SVladimir Oltean /* Apply FWD mask. The loop is needed to add/remove the current port as 21419b521250SVladimir Oltean * a source for the other ports. 21429b521250SVladimir Oltean */ 21439b521250SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2144e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2145e21268efSVladimir Oltean unsigned long mask; 2146e21268efSVladimir Oltean 2147e21268efSVladimir Oltean if (!ocelot_port) { 2148e21268efSVladimir Oltean /* Unused ports can't send anywhere */ 2149e21268efSVladimir Oltean mask = 0; 2150e21268efSVladimir Oltean } else if (ocelot_port->is_dsa_8021q_cpu) { 2151e21268efSVladimir Oltean /* The DSA tag_8021q CPU ports need to be able to 2152c295f983SVladimir Oltean * forward packets to all ports assigned to them. 2153e21268efSVladimir Oltean */ 2154c295f983SVladimir Oltean mask = ocelot_dsa_8021q_cpu_assigned_ports(ocelot, 2155c295f983SVladimir Oltean ocelot_port); 2156df291e54SVladimir Oltean } else if (ocelot_port->bridge) { 2157528d3f19SVladimir Oltean struct net_device *bond = ocelot_port->bond; 21589b521250SVladimir Oltean 2159a8bd9fa5SVladimir Oltean mask = ocelot_get_bridge_fwd_mask(ocelot, port); 2160df291e54SVladimir Oltean mask &= ~BIT(port); 2161c295f983SVladimir Oltean 2162c295f983SVladimir Oltean mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot, 2163c295f983SVladimir Oltean port); 2164c295f983SVladimir Oltean 2165a14e6b69SVladimir Oltean if (bond) 2166a14e6b69SVladimir Oltean mask &= ~ocelot_get_bond_mask(ocelot, bond); 21679b521250SVladimir Oltean } else { 2168e21268efSVladimir Oltean /* Standalone ports forward only to DSA tag_8021q CPU 2169e21268efSVladimir Oltean * ports (if those exist), or to the hardware CPU port 2170e21268efSVladimir Oltean * module otherwise. 2171e21268efSVladimir Oltean */ 2172c295f983SVladimir Oltean mask = ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot, 2173c295f983SVladimir Oltean port); 2174e21268efSVladimir Oltean } 2175e21268efSVladimir Oltean 2176e21268efSVladimir Oltean ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); 21779b521250SVladimir Oltean } 21788abe1970SVladimir Oltean 21798abe1970SVladimir Oltean /* If cut-through forwarding is supported and a port is leaving, there 21808abe1970SVladimir Oltean * is a chance that cut-through was disabled on the other ports due to 21818abe1970SVladimir Oltean * the port which is leaving (it has a higher link speed). We need to 21828abe1970SVladimir Oltean * update the cut-through masks of the remaining ports no earlier than 21838abe1970SVladimir Oltean * after the port has left, to prevent underruns from happening between 21848abe1970SVladimir Oltean * the cut-through update and the forwarding domain update. 21858abe1970SVladimir Oltean */ 21868abe1970SVladimir Oltean if (!joining && ocelot->ops->cut_through_fwd) 21878abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 21889b521250SVladimir Oltean } 21899b521250SVladimir Oltean 219061be79baSVladimir Oltean /* Update PGID_CPU which is the destination port mask used for whitelisting 219161be79baSVladimir Oltean * unicast addresses filtered towards the host. In the normal and NPI modes, 219261be79baSVladimir Oltean * this points to the analyzer entry for the CPU port module, while in DSA 219361be79baSVladimir Oltean * tag_8021q mode, it is a bit mask of all active CPU ports. 219461be79baSVladimir Oltean * PGID_SRC will take care of forwarding a packet from one user port to 219561be79baSVladimir Oltean * no more than a single CPU port. 219661be79baSVladimir Oltean */ 219761be79baSVladimir Oltean static void ocelot_update_pgid_cpu(struct ocelot *ocelot) 219861be79baSVladimir Oltean { 219961be79baSVladimir Oltean int pgid_cpu = 0; 220061be79baSVladimir Oltean int port; 220161be79baSVladimir Oltean 220261be79baSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 220361be79baSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 220461be79baSVladimir Oltean 220561be79baSVladimir Oltean if (!ocelot_port || !ocelot_port->is_dsa_8021q_cpu) 220661be79baSVladimir Oltean continue; 220761be79baSVladimir Oltean 220861be79baSVladimir Oltean pgid_cpu |= BIT(port); 220961be79baSVladimir Oltean } 221061be79baSVladimir Oltean 221161be79baSVladimir Oltean if (!pgid_cpu) 221261be79baSVladimir Oltean pgid_cpu = BIT(ocelot->num_phys_ports); 221361be79baSVladimir Oltean 221461be79baSVladimir Oltean ocelot_write_rix(ocelot, pgid_cpu, ANA_PGID_PGID, PGID_CPU); 221561be79baSVladimir Oltean } 221661be79baSVladimir Oltean 2217*36a0bf44SVladimir Oltean void ocelot_port_setup_dsa_8021q_cpu(struct ocelot *ocelot, int cpu) 221854c31984SVladimir Oltean { 2219c295f983SVladimir Oltean struct ocelot_port *cpu_port = ocelot->ports[cpu]; 222054c31984SVladimir Oltean u16 vid; 222154c31984SVladimir Oltean 22228c166acbSVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 22238c166acbSVladimir Oltean 2224c295f983SVladimir Oltean cpu_port->is_dsa_8021q_cpu = true; 222554c31984SVladimir Oltean 222654c31984SVladimir Oltean for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++) 2227c295f983SVladimir Oltean ocelot_vlan_member_add(ocelot, cpu, vid, true); 222861be79baSVladimir Oltean 222961be79baSVladimir Oltean ocelot_update_pgid_cpu(ocelot); 2230a72e23ddSVladimir Oltean 2231*36a0bf44SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2232*36a0bf44SVladimir Oltean } 2233*36a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_setup_dsa_8021q_cpu); 2234*36a0bf44SVladimir Oltean 2235*36a0bf44SVladimir Oltean void ocelot_port_teardown_dsa_8021q_cpu(struct ocelot *ocelot, int cpu) 2236*36a0bf44SVladimir Oltean { 2237*36a0bf44SVladimir Oltean struct ocelot_port *cpu_port = ocelot->ports[cpu]; 2238*36a0bf44SVladimir Oltean u16 vid; 2239*36a0bf44SVladimir Oltean 2240*36a0bf44SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2241*36a0bf44SVladimir Oltean 2242*36a0bf44SVladimir Oltean cpu_port->is_dsa_8021q_cpu = false; 2243*36a0bf44SVladimir Oltean 2244*36a0bf44SVladimir Oltean for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++) 2245*36a0bf44SVladimir Oltean ocelot_vlan_member_del(ocelot, cpu_port->index, vid); 2246*36a0bf44SVladimir Oltean 2247*36a0bf44SVladimir Oltean ocelot_update_pgid_cpu(ocelot); 2248*36a0bf44SVladimir Oltean 2249*36a0bf44SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2250*36a0bf44SVladimir Oltean } 2251*36a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_teardown_dsa_8021q_cpu); 2252*36a0bf44SVladimir Oltean 2253*36a0bf44SVladimir Oltean void ocelot_port_assign_dsa_8021q_cpu(struct ocelot *ocelot, int port, 2254*36a0bf44SVladimir Oltean int cpu) 2255*36a0bf44SVladimir Oltean { 2256*36a0bf44SVladimir Oltean struct ocelot_port *cpu_port = ocelot->ports[cpu]; 2257*36a0bf44SVladimir Oltean 2258*36a0bf44SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2259*36a0bf44SVladimir Oltean 2260*36a0bf44SVladimir Oltean ocelot->ports[port]->dsa_8021q_cpu = cpu_port; 2261a72e23ddSVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 22628c166acbSVladimir Oltean 22638c166acbSVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 226454c31984SVladimir Oltean } 2265c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assign_dsa_8021q_cpu); 226654c31984SVladimir Oltean 2267c295f983SVladimir Oltean void ocelot_port_unassign_dsa_8021q_cpu(struct ocelot *ocelot, int port) 226854c31984SVladimir Oltean { 22698c166acbSVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 22708c166acbSVladimir Oltean 2271c295f983SVladimir Oltean ocelot->ports[port]->dsa_8021q_cpu = NULL; 2272a72e23ddSVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 22738c166acbSVladimir Oltean 22748c166acbSVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 227554c31984SVladimir Oltean } 2276c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_unassign_dsa_8021q_cpu); 227754c31984SVladimir Oltean 22785e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) 2279a556c76aSAlexandre Belloni { 2280421741eaSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2281df291e54SVladimir Oltean u32 learn_ena = 0; 2282a556c76aSAlexandre Belloni 22838abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 22848abe1970SVladimir Oltean 2285df291e54SVladimir Oltean ocelot_port->stp_state = state; 2286a556c76aSAlexandre Belloni 2287df291e54SVladimir Oltean if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) && 2288df291e54SVladimir Oltean ocelot_port->learn_ena) 2289df291e54SVladimir Oltean learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA; 2290a556c76aSAlexandre Belloni 2291df291e54SVladimir Oltean ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA, 2292df291e54SVladimir Oltean ANA_PORT_PORT_CFG, port); 2293a556c76aSAlexandre Belloni 22948abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING); 22958abe1970SVladimir Oltean 22968abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2297a556c76aSAlexandre Belloni } 22985e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set); 2299a556c76aSAlexandre Belloni 23005e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) 23014bda1415SVladimir Oltean { 2302c0d7eccbSVladimir Oltean unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); 2303c0d7eccbSVladimir Oltean 2304c0d7eccbSVladimir Oltean /* Setting AGE_PERIOD to zero effectively disables automatic aging, 2305c0d7eccbSVladimir Oltean * which is clearly not what our intention is. So avoid that. 2306c0d7eccbSVladimir Oltean */ 2307c0d7eccbSVladimir Oltean if (!age_period) 2308c0d7eccbSVladimir Oltean age_period = 1; 2309c0d7eccbSVladimir Oltean 2310c0d7eccbSVladimir Oltean ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); 2311a556c76aSAlexandre Belloni } 23125e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time); 2313a556c76aSAlexandre Belloni 2314a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 2315a556c76aSAlexandre Belloni const unsigned char *addr, 2316a556c76aSAlexandre Belloni u16 vid) 2317a556c76aSAlexandre Belloni { 2318a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 2319a556c76aSAlexandre Belloni 2320a556c76aSAlexandre Belloni list_for_each_entry(mc, &ocelot->multicast, list) { 2321a556c76aSAlexandre Belloni if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 2322a556c76aSAlexandre Belloni return mc; 2323a556c76aSAlexandre Belloni } 2324a556c76aSAlexandre Belloni 2325a556c76aSAlexandre Belloni return NULL; 2326a556c76aSAlexandre Belloni } 2327a556c76aSAlexandre Belloni 23289403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) 23299403c158SVladimir Oltean { 23309403c158SVladimir Oltean if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) 23319403c158SVladimir Oltean return ENTRYTYPE_MACv4; 23329403c158SVladimir Oltean if (addr[0] == 0x33 && addr[1] == 0x33) 23339403c158SVladimir Oltean return ENTRYTYPE_MACv6; 23347c313143SVladimir Oltean return ENTRYTYPE_LOCKED; 23359403c158SVladimir Oltean } 23369403c158SVladimir Oltean 2337e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, 2338e5d1f896SVladimir Oltean unsigned long ports) 2339e5d1f896SVladimir Oltean { 2340e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2341e5d1f896SVladimir Oltean 2342e5d1f896SVladimir Oltean pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); 2343e5d1f896SVladimir Oltean if (!pgid) 2344e5d1f896SVladimir Oltean return ERR_PTR(-ENOMEM); 2345e5d1f896SVladimir Oltean 2346e5d1f896SVladimir Oltean pgid->ports = ports; 2347e5d1f896SVladimir Oltean pgid->index = index; 2348e5d1f896SVladimir Oltean refcount_set(&pgid->refcount, 1); 2349e5d1f896SVladimir Oltean list_add_tail(&pgid->list, &ocelot->pgids); 2350e5d1f896SVladimir Oltean 2351e5d1f896SVladimir Oltean return pgid; 2352e5d1f896SVladimir Oltean } 2353e5d1f896SVladimir Oltean 2354e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) 2355e5d1f896SVladimir Oltean { 2356e5d1f896SVladimir Oltean if (!refcount_dec_and_test(&pgid->refcount)) 2357e5d1f896SVladimir Oltean return; 2358e5d1f896SVladimir Oltean 2359e5d1f896SVladimir Oltean list_del(&pgid->list); 2360e5d1f896SVladimir Oltean kfree(pgid); 2361e5d1f896SVladimir Oltean } 2362e5d1f896SVladimir Oltean 2363e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, 2364bb8d53fdSVladimir Oltean const struct ocelot_multicast *mc) 23659403c158SVladimir Oltean { 2366e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2367e5d1f896SVladimir Oltean int index; 23689403c158SVladimir Oltean 23699403c158SVladimir Oltean /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and 23709403c158SVladimir Oltean * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the 23719403c158SVladimir Oltean * destination mask table (PGID), the destination set is programmed as 23729403c158SVladimir Oltean * part of the entry MAC address.", and the DEST_IDX is set to 0. 23739403c158SVladimir Oltean */ 2374bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4 || 2375bb8d53fdSVladimir Oltean mc->entry_type == ENTRYTYPE_MACv6) 2376e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, 0, mc->ports); 23779403c158SVladimir Oltean 2378e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 2379e5d1f896SVladimir Oltean /* When searching for a nonreserved multicast PGID, ignore the 2380e5d1f896SVladimir Oltean * dummy PGID of zero that we have for MACv4/MACv6 entries 2381e5d1f896SVladimir Oltean */ 2382e5d1f896SVladimir Oltean if (pgid->index && pgid->ports == mc->ports) { 2383e5d1f896SVladimir Oltean refcount_inc(&pgid->refcount); 2384e5d1f896SVladimir Oltean return pgid; 2385e5d1f896SVladimir Oltean } 2386e5d1f896SVladimir Oltean } 2387e5d1f896SVladimir Oltean 2388e5d1f896SVladimir Oltean /* Search for a free index in the nonreserved multicast PGID area */ 2389e5d1f896SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, index) { 23909403c158SVladimir Oltean bool used = false; 23919403c158SVladimir Oltean 2392e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 2393e5d1f896SVladimir Oltean if (pgid->index == index) { 23949403c158SVladimir Oltean used = true; 23959403c158SVladimir Oltean break; 23969403c158SVladimir Oltean } 23979403c158SVladimir Oltean } 23989403c158SVladimir Oltean 23999403c158SVladimir Oltean if (!used) 2400e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, index, mc->ports); 24019403c158SVladimir Oltean } 24029403c158SVladimir Oltean 2403e5d1f896SVladimir Oltean return ERR_PTR(-ENOSPC); 24049403c158SVladimir Oltean } 24059403c158SVladimir Oltean 24069403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr, 2407bb8d53fdSVladimir Oltean struct ocelot_multicast *mc) 24089403c158SVladimir Oltean { 2409ebbd860eSVladimir Oltean ether_addr_copy(addr, mc->addr); 24109403c158SVladimir Oltean 2411bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4) { 24129403c158SVladimir Oltean addr[0] = 0; 24139403c158SVladimir Oltean addr[1] = mc->ports >> 8; 24149403c158SVladimir Oltean addr[2] = mc->ports & 0xff; 2415bb8d53fdSVladimir Oltean } else if (mc->entry_type == ENTRYTYPE_MACv6) { 24169403c158SVladimir Oltean addr[0] = mc->ports >> 8; 24179403c158SVladimir Oltean addr[1] = mc->ports & 0xff; 24189403c158SVladimir Oltean } 24199403c158SVladimir Oltean } 24209403c158SVladimir Oltean 2421209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port, 242254c31984SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 242354c31984SVladimir Oltean const struct net_device *bridge) 2424a556c76aSAlexandre Belloni { 2425a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 2426004d44f6SVladimir Oltean struct ocelot_multicast *mc; 2427e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2428a556c76aSAlexandre Belloni u16 vid = mdb->vid; 2429a556c76aSAlexandre Belloni 243054c31984SVladimir Oltean if (!vid) 243154c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 243254c31984SVladimir Oltean 2433a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 2434a556c76aSAlexandre Belloni if (!mc) { 2435728e69aeSVladimir Oltean /* New entry */ 2436bb8d53fdSVladimir Oltean mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 2437bb8d53fdSVladimir Oltean if (!mc) 2438bb8d53fdSVladimir Oltean return -ENOMEM; 2439bb8d53fdSVladimir Oltean 2440bb8d53fdSVladimir Oltean mc->entry_type = ocelot_classify_mdb(mdb->addr); 2441bb8d53fdSVladimir Oltean ether_addr_copy(mc->addr, mdb->addr); 2442bb8d53fdSVladimir Oltean mc->vid = vid; 2443bb8d53fdSVladimir Oltean 2444a556c76aSAlexandre Belloni list_add_tail(&mc->list, &ocelot->multicast); 2445728e69aeSVladimir Oltean } else { 2446e5d1f896SVladimir Oltean /* Existing entry. Clean up the current port mask from 2447e5d1f896SVladimir Oltean * hardware now, because we'll be modifying it. 2448e5d1f896SVladimir Oltean */ 2449e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 2450bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2451a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 2452a556c76aSAlexandre Belloni } 2453a556c76aSAlexandre Belloni 2454004d44f6SVladimir Oltean mc->ports |= BIT(port); 2455e5d1f896SVladimir Oltean 2456e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 2457e5d1f896SVladimir Oltean if (IS_ERR(pgid)) { 2458e5d1f896SVladimir Oltean dev_err(ocelot->dev, 2459e5d1f896SVladimir Oltean "Cannot allocate PGID for mdb %pM vid %d\n", 2460e5d1f896SVladimir Oltean mc->addr, mc->vid); 2461e5d1f896SVladimir Oltean devm_kfree(ocelot->dev, mc); 2462e5d1f896SVladimir Oltean return PTR_ERR(pgid); 2463e5d1f896SVladimir Oltean } 2464e5d1f896SVladimir Oltean mc->pgid = pgid; 2465e5d1f896SVladimir Oltean 2466bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2467a556c76aSAlexandre Belloni 2468e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 2469e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 2470e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 2471e5d1f896SVladimir Oltean pgid->index); 2472e5d1f896SVladimir Oltean 2473e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 2474bb8d53fdSVladimir Oltean mc->entry_type); 2475a556c76aSAlexandre Belloni } 2476209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add); 2477a556c76aSAlexandre Belloni 2478209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port, 247954c31984SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 248054c31984SVladimir Oltean const struct net_device *bridge) 2481a556c76aSAlexandre Belloni { 2482a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 2483004d44f6SVladimir Oltean struct ocelot_multicast *mc; 2484e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2485a556c76aSAlexandre Belloni u16 vid = mdb->vid; 2486a556c76aSAlexandre Belloni 248754c31984SVladimir Oltean if (!vid) 248854c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 248954c31984SVladimir Oltean 2490a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 2491a556c76aSAlexandre Belloni if (!mc) 2492a556c76aSAlexandre Belloni return -ENOENT; 2493a556c76aSAlexandre Belloni 2494bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2495a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 2496a556c76aSAlexandre Belloni 2497e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 2498004d44f6SVladimir Oltean mc->ports &= ~BIT(port); 2499a556c76aSAlexandre Belloni if (!mc->ports) { 2500a556c76aSAlexandre Belloni list_del(&mc->list); 2501a556c76aSAlexandre Belloni devm_kfree(ocelot->dev, mc); 2502a556c76aSAlexandre Belloni return 0; 2503a556c76aSAlexandre Belloni } 2504a556c76aSAlexandre Belloni 2505e5d1f896SVladimir Oltean /* We have a PGID with fewer ports now */ 2506e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 2507e5d1f896SVladimir Oltean if (IS_ERR(pgid)) 2508e5d1f896SVladimir Oltean return PTR_ERR(pgid); 2509e5d1f896SVladimir Oltean mc->pgid = pgid; 2510e5d1f896SVladimir Oltean 2511bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2512a556c76aSAlexandre Belloni 2513e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 2514e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 2515e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 2516e5d1f896SVladimir Oltean pgid->index); 2517e5d1f896SVladimir Oltean 2518e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 2519bb8d53fdSVladimir Oltean mc->entry_type); 2520a556c76aSAlexandre Belloni } 2521209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del); 2522a556c76aSAlexandre Belloni 252354c31984SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port, 252454c31984SVladimir Oltean struct net_device *bridge, int bridge_num, 252554c31984SVladimir Oltean struct netlink_ext_ack *extack) 2526a556c76aSAlexandre Belloni { 2527df291e54SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 252854c31984SVladimir Oltean int err; 252954c31984SVladimir Oltean 253054c31984SVladimir Oltean err = ocelot_single_vlan_aware_bridge(ocelot, extack); 253154c31984SVladimir Oltean if (err) 253254c31984SVladimir Oltean return err; 2533a556c76aSAlexandre Belloni 25348abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 25358abe1970SVladimir Oltean 2536df291e54SVladimir Oltean ocelot_port->bridge = bridge; 253754c31984SVladimir Oltean ocelot_port->bridge_num = bridge_num; 2538a556c76aSAlexandre Belloni 25398abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 25408abe1970SVladimir Oltean 25418abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 254254c31984SVladimir Oltean 254354c31984SVladimir Oltean if (br_vlan_enabled(bridge)) 254454c31984SVladimir Oltean return 0; 254554c31984SVladimir Oltean 254654c31984SVladimir Oltean return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge); 2547a556c76aSAlexandre Belloni } 25485e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join); 2549a556c76aSAlexandre Belloni 2550e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port, 2551a556c76aSAlexandre Belloni struct net_device *bridge) 2552a556c76aSAlexandre Belloni { 2553df291e54SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 25542e554a7aSVladimir Oltean 25558abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 25568abe1970SVladimir Oltean 255754c31984SVladimir Oltean if (!br_vlan_enabled(bridge)) 255854c31984SVladimir Oltean ocelot_del_vlan_unaware_pvid(ocelot, port, bridge); 255954c31984SVladimir Oltean 2560df291e54SVladimir Oltean ocelot_port->bridge = NULL; 256154c31984SVladimir Oltean ocelot_port->bridge_num = -1; 25627142529fSAntoine Tenart 2563d4004422SVladimir Oltean ocelot_port_set_pvid(ocelot, port, NULL); 25640da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 25658abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, false); 25668abe1970SVladimir Oltean 25678abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2568a556c76aSAlexandre Belloni } 25695e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave); 2570a556c76aSAlexandre Belloni 2571dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 2572dc96ee37SAlexandre Belloni { 2573528d3f19SVladimir Oltean unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0); 2574dc96ee37SAlexandre Belloni int i, port, lag; 2575dc96ee37SAlexandre Belloni 2576dc96ee37SAlexandre Belloni /* Reset destination and aggregation PGIDS */ 257796b029b0SVladimir Oltean for_each_unicast_dest_pgid(ocelot, port) 2578dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 2579dc96ee37SAlexandre Belloni 258096b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) 2581dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 2582dc96ee37SAlexandre Belloni ANA_PGID_PGID, i); 2583dc96ee37SAlexandre Belloni 2584528d3f19SVladimir Oltean /* The visited ports bitmask holds the list of ports offloading any 2585528d3f19SVladimir Oltean * bonding interface. Initially we mark all these ports as unvisited, 2586528d3f19SVladimir Oltean * then every time we visit a port in this bitmask, we know that it is 2587528d3f19SVladimir Oltean * the lowest numbered port, i.e. the one whose logical ID == physical 2588528d3f19SVladimir Oltean * port ID == LAG ID. So we mark as visited all further ports in the 2589528d3f19SVladimir Oltean * bitmask that are offloading the same bonding interface. This way, 2590528d3f19SVladimir Oltean * we set up the aggregation PGIDs only once per bonding interface. 2591528d3f19SVladimir Oltean */ 2592528d3f19SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2593528d3f19SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2594528d3f19SVladimir Oltean 2595528d3f19SVladimir Oltean if (!ocelot_port || !ocelot_port->bond) 2596528d3f19SVladimir Oltean continue; 2597528d3f19SVladimir Oltean 2598528d3f19SVladimir Oltean visited &= ~BIT(port); 2599528d3f19SVladimir Oltean } 2600528d3f19SVladimir Oltean 2601528d3f19SVladimir Oltean /* Now, set PGIDs for each active LAG */ 2602dc96ee37SAlexandre Belloni for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 2603528d3f19SVladimir Oltean struct net_device *bond = ocelot->ports[lag]->bond; 260423ca3b72SVladimir Oltean int num_active_ports = 0; 2605dc96ee37SAlexandre Belloni unsigned long bond_mask; 2606dc96ee37SAlexandre Belloni u8 aggr_idx[16]; 2607dc96ee37SAlexandre Belloni 2608528d3f19SVladimir Oltean if (!bond || (visited & BIT(lag))) 2609dc96ee37SAlexandre Belloni continue; 2610dc96ee37SAlexandre Belloni 2611a14e6b69SVladimir Oltean bond_mask = ocelot_get_bond_mask(ocelot, bond); 2612528d3f19SVladimir Oltean 2613dc96ee37SAlexandre Belloni for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 2614a14e6b69SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2615a14e6b69SVladimir Oltean 2616dc96ee37SAlexandre Belloni // Destination mask 2617dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, bond_mask, 2618dc96ee37SAlexandre Belloni ANA_PGID_PGID, port); 2619a14e6b69SVladimir Oltean 2620a14e6b69SVladimir Oltean if (ocelot_port->lag_tx_active) 262123ca3b72SVladimir Oltean aggr_idx[num_active_ports++] = port; 2622dc96ee37SAlexandre Belloni } 2623dc96ee37SAlexandre Belloni 262496b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) { 2625dc96ee37SAlexandre Belloni u32 ac; 2626dc96ee37SAlexandre Belloni 2627dc96ee37SAlexandre Belloni ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 2628dc96ee37SAlexandre Belloni ac &= ~bond_mask; 262923ca3b72SVladimir Oltean /* Don't do division by zero if there was no active 263023ca3b72SVladimir Oltean * port. Just make all aggregation codes zero. 263123ca3b72SVladimir Oltean */ 263223ca3b72SVladimir Oltean if (num_active_ports) 263323ca3b72SVladimir Oltean ac |= BIT(aggr_idx[i % num_active_ports]); 2634dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 2635dc96ee37SAlexandre Belloni } 2636528d3f19SVladimir Oltean 2637528d3f19SVladimir Oltean /* Mark all ports in the same LAG as visited to avoid applying 2638528d3f19SVladimir Oltean * the same config again. 2639528d3f19SVladimir Oltean */ 2640528d3f19SVladimir Oltean for (port = lag; port < ocelot->num_phys_ports; port++) { 2641528d3f19SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2642528d3f19SVladimir Oltean 2643528d3f19SVladimir Oltean if (!ocelot_port) 2644528d3f19SVladimir Oltean continue; 2645528d3f19SVladimir Oltean 2646528d3f19SVladimir Oltean if (ocelot_port->bond == bond) 2647528d3f19SVladimir Oltean visited |= BIT(port); 2648528d3f19SVladimir Oltean } 2649dc96ee37SAlexandre Belloni } 2650dc96ee37SAlexandre Belloni } 2651dc96ee37SAlexandre Belloni 26522527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the 26532527f2e8SVladimir Oltean * same bond must have the same logical port ID, equal to the physical port ID 26542527f2e8SVladimir Oltean * of the lowest numbered physical port in that bond. Otherwise, in standalone/ 26552527f2e8SVladimir Oltean * bridged mode, each port has a logical port ID equal to its physical port ID. 26562527f2e8SVladimir Oltean */ 26572527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) 2658dc96ee37SAlexandre Belloni { 26592527f2e8SVladimir Oltean int port; 2660dc96ee37SAlexandre Belloni 26612527f2e8SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 26622527f2e8SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 26632527f2e8SVladimir Oltean struct net_device *bond; 2664dc96ee37SAlexandre Belloni 26652527f2e8SVladimir Oltean if (!ocelot_port) 26662527f2e8SVladimir Oltean continue; 2667dc96ee37SAlexandre Belloni 26682527f2e8SVladimir Oltean bond = ocelot_port->bond; 26692527f2e8SVladimir Oltean if (bond) { 2670961d8b69SVladimir Oltean int lag = ocelot_bond_get_id(ocelot, bond); 26712527f2e8SVladimir Oltean 26722527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 2673dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(lag), 26742527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 26752527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 26762527f2e8SVladimir Oltean } else { 26772527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 26782527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 26792527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 26802527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 26812527f2e8SVladimir Oltean } 2682dc96ee37SAlexandre Belloni } 2683dc96ee37SAlexandre Belloni } 2684dc96ee37SAlexandre Belloni 268528de0f9fSVladimir Oltean static int ocelot_migrate_mc(struct ocelot *ocelot, struct ocelot_multicast *mc, 268628de0f9fSVladimir Oltean unsigned long from_mask, unsigned long to_mask) 268728de0f9fSVladimir Oltean { 268828de0f9fSVladimir Oltean unsigned char addr[ETH_ALEN]; 268928de0f9fSVladimir Oltean struct ocelot_pgid *pgid; 269028de0f9fSVladimir Oltean u16 vid = mc->vid; 269128de0f9fSVladimir Oltean 269228de0f9fSVladimir Oltean dev_dbg(ocelot->dev, 269328de0f9fSVladimir Oltean "Migrating multicast %pM vid %d from port mask 0x%lx to 0x%lx\n", 269428de0f9fSVladimir Oltean mc->addr, mc->vid, from_mask, to_mask); 269528de0f9fSVladimir Oltean 269628de0f9fSVladimir Oltean /* First clean up the current port mask from hardware, because 269728de0f9fSVladimir Oltean * we'll be modifying it. 269828de0f9fSVladimir Oltean */ 269928de0f9fSVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 270028de0f9fSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 270128de0f9fSVladimir Oltean ocelot_mact_forget(ocelot, addr, vid); 270228de0f9fSVladimir Oltean 270328de0f9fSVladimir Oltean mc->ports &= ~from_mask; 270428de0f9fSVladimir Oltean mc->ports |= to_mask; 270528de0f9fSVladimir Oltean 270628de0f9fSVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 270728de0f9fSVladimir Oltean if (IS_ERR(pgid)) { 270828de0f9fSVladimir Oltean dev_err(ocelot->dev, 270928de0f9fSVladimir Oltean "Cannot allocate PGID for mdb %pM vid %d\n", 271028de0f9fSVladimir Oltean mc->addr, mc->vid); 271128de0f9fSVladimir Oltean devm_kfree(ocelot->dev, mc); 271228de0f9fSVladimir Oltean return PTR_ERR(pgid); 271328de0f9fSVladimir Oltean } 271428de0f9fSVladimir Oltean mc->pgid = pgid; 271528de0f9fSVladimir Oltean 271628de0f9fSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 271728de0f9fSVladimir Oltean 271828de0f9fSVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 271928de0f9fSVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 272028de0f9fSVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 272128de0f9fSVladimir Oltean pgid->index); 272228de0f9fSVladimir Oltean 272328de0f9fSVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 272428de0f9fSVladimir Oltean mc->entry_type); 272528de0f9fSVladimir Oltean } 272628de0f9fSVladimir Oltean 272728de0f9fSVladimir Oltean int ocelot_migrate_mdbs(struct ocelot *ocelot, unsigned long from_mask, 272828de0f9fSVladimir Oltean unsigned long to_mask) 272928de0f9fSVladimir Oltean { 273028de0f9fSVladimir Oltean struct ocelot_multicast *mc; 273128de0f9fSVladimir Oltean int err; 273228de0f9fSVladimir Oltean 273328de0f9fSVladimir Oltean list_for_each_entry(mc, &ocelot->multicast, list) { 273428de0f9fSVladimir Oltean if (!(mc->ports & from_mask)) 273528de0f9fSVladimir Oltean continue; 273628de0f9fSVladimir Oltean 273728de0f9fSVladimir Oltean err = ocelot_migrate_mc(ocelot, mc, from_mask, to_mask); 273828de0f9fSVladimir Oltean if (err) 273928de0f9fSVladimir Oltean return err; 274028de0f9fSVladimir Oltean } 274128de0f9fSVladimir Oltean 274228de0f9fSVladimir Oltean return 0; 274328de0f9fSVladimir Oltean } 274428de0f9fSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_migrate_mdbs); 274528de0f9fSVladimir Oltean 2746961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says: 2747961d8b69SVladimir Oltean * Logical port number for front port. If port is not a member of a LLAG, 2748961d8b69SVladimir Oltean * then PORTID must be set to the physical port number. 2749961d8b69SVladimir Oltean * If port is a member of a LLAG, then PORTID must be set to the common 2750961d8b69SVladimir Oltean * PORTID_VAL used for all member ports of the LLAG. 2751961d8b69SVladimir Oltean * The value must not exceed the number of physical ports on the device. 2752961d8b69SVladimir Oltean * 2753961d8b69SVladimir Oltean * This means we have little choice but to migrate FDB entries pointing towards 2754961d8b69SVladimir Oltean * a logical port when that changes. 2755961d8b69SVladimir Oltean */ 2756961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot, 2757961d8b69SVladimir Oltean struct net_device *bond, 2758961d8b69SVladimir Oltean int lag) 2759961d8b69SVladimir Oltean { 2760961d8b69SVladimir Oltean struct ocelot_lag_fdb *fdb; 2761961d8b69SVladimir Oltean int err; 2762961d8b69SVladimir Oltean 2763961d8b69SVladimir Oltean lockdep_assert_held(&ocelot->fwd_domain_lock); 2764961d8b69SVladimir Oltean 2765961d8b69SVladimir Oltean list_for_each_entry(fdb, &ocelot->lag_fdbs, list) { 2766961d8b69SVladimir Oltean if (fdb->bond != bond) 2767961d8b69SVladimir Oltean continue; 2768961d8b69SVladimir Oltean 2769961d8b69SVladimir Oltean err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid); 2770961d8b69SVladimir Oltean if (err) { 2771961d8b69SVladimir Oltean dev_err(ocelot->dev, 2772961d8b69SVladimir Oltean "failed to delete LAG %s FDB %pM vid %d: %pe\n", 2773961d8b69SVladimir Oltean bond->name, fdb->addr, fdb->vid, ERR_PTR(err)); 2774961d8b69SVladimir Oltean } 2775961d8b69SVladimir Oltean 2776961d8b69SVladimir Oltean err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid, 2777961d8b69SVladimir Oltean ENTRYTYPE_LOCKED); 2778961d8b69SVladimir Oltean if (err) { 2779961d8b69SVladimir Oltean dev_err(ocelot->dev, 2780961d8b69SVladimir Oltean "failed to migrate LAG %s FDB %pM vid %d: %pe\n", 2781961d8b69SVladimir Oltean bond->name, fdb->addr, fdb->vid, ERR_PTR(err)); 2782961d8b69SVladimir Oltean } 2783961d8b69SVladimir Oltean } 2784961d8b69SVladimir Oltean } 2785961d8b69SVladimir Oltean 27869c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port, 2787583cbbe3SVladimir Oltean struct net_device *bond, 2788583cbbe3SVladimir Oltean struct netdev_lag_upper_info *info) 2789dc96ee37SAlexandre Belloni { 2790583cbbe3SVladimir Oltean if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) 2791583cbbe3SVladimir Oltean return -EOPNOTSUPP; 2792583cbbe3SVladimir Oltean 27938abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 27948abe1970SVladimir Oltean 2795b80af659SVladimir Oltean ocelot->ports[port]->bond = bond; 2796dc96ee37SAlexandre Belloni 27972527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 27988abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2799dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 2800dc96ee37SAlexandre Belloni 28018abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 28028abe1970SVladimir Oltean 2803dc96ee37SAlexandre Belloni return 0; 2804dc96ee37SAlexandre Belloni } 28059c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join); 2806dc96ee37SAlexandre Belloni 28079c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port, 2808dc96ee37SAlexandre Belloni struct net_device *bond) 2809dc96ee37SAlexandre Belloni { 2810961d8b69SVladimir Oltean int old_lag_id, new_lag_id; 2811961d8b69SVladimir Oltean 28128abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 28138abe1970SVladimir Oltean 2814961d8b69SVladimir Oltean old_lag_id = ocelot_bond_get_id(ocelot, bond); 2815961d8b69SVladimir Oltean 2816b80af659SVladimir Oltean ocelot->ports[port]->bond = NULL; 2817b80af659SVladimir Oltean 28182527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 28198abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, false); 2820dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 28218abe1970SVladimir Oltean 2822961d8b69SVladimir Oltean new_lag_id = ocelot_bond_get_id(ocelot, bond); 2823961d8b69SVladimir Oltean 2824961d8b69SVladimir Oltean if (new_lag_id >= 0 && old_lag_id != new_lag_id) 2825961d8b69SVladimir Oltean ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id); 2826961d8b69SVladimir Oltean 28278abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2828dc96ee37SAlexandre Belloni } 28299c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave); 28300e332c85SPetr Machata 283123ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active) 283223ca3b72SVladimir Oltean { 283323ca3b72SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 283423ca3b72SVladimir Oltean 2835961d8b69SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2836961d8b69SVladimir Oltean 283723ca3b72SVladimir Oltean ocelot_port->lag_tx_active = lag_tx_active; 283823ca3b72SVladimir Oltean 283923ca3b72SVladimir Oltean /* Rebalance the LAGs */ 284023ca3b72SVladimir Oltean ocelot_set_aggr_pgids(ocelot); 2841961d8b69SVladimir Oltean 2842961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 284323ca3b72SVladimir Oltean } 284423ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change); 284523ca3b72SVladimir Oltean 2846961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond, 284754c31984SVladimir Oltean const unsigned char *addr, u16 vid, 284854c31984SVladimir Oltean const struct net_device *bridge) 2849961d8b69SVladimir Oltean { 2850961d8b69SVladimir Oltean struct ocelot_lag_fdb *fdb; 2851961d8b69SVladimir Oltean int lag, err; 2852961d8b69SVladimir Oltean 2853961d8b69SVladimir Oltean fdb = kzalloc(sizeof(*fdb), GFP_KERNEL); 2854961d8b69SVladimir Oltean if (!fdb) 2855961d8b69SVladimir Oltean return -ENOMEM; 2856961d8b69SVladimir Oltean 285754c31984SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 285854c31984SVladimir Oltean 285954c31984SVladimir Oltean if (!vid) 286054c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 286154c31984SVladimir Oltean 2862961d8b69SVladimir Oltean ether_addr_copy(fdb->addr, addr); 2863961d8b69SVladimir Oltean fdb->vid = vid; 2864961d8b69SVladimir Oltean fdb->bond = bond; 2865961d8b69SVladimir Oltean 2866961d8b69SVladimir Oltean lag = ocelot_bond_get_id(ocelot, bond); 2867961d8b69SVladimir Oltean 2868961d8b69SVladimir Oltean err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED); 2869961d8b69SVladimir Oltean if (err) { 2870961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2871961d8b69SVladimir Oltean kfree(fdb); 2872961d8b69SVladimir Oltean return err; 2873961d8b69SVladimir Oltean } 2874961d8b69SVladimir Oltean 2875961d8b69SVladimir Oltean list_add_tail(&fdb->list, &ocelot->lag_fdbs); 2876961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2877961d8b69SVladimir Oltean 2878961d8b69SVladimir Oltean return 0; 2879961d8b69SVladimir Oltean } 2880961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add); 2881961d8b69SVladimir Oltean 2882961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond, 288354c31984SVladimir Oltean const unsigned char *addr, u16 vid, 288454c31984SVladimir Oltean const struct net_device *bridge) 2885961d8b69SVladimir Oltean { 2886961d8b69SVladimir Oltean struct ocelot_lag_fdb *fdb, *tmp; 2887961d8b69SVladimir Oltean 2888961d8b69SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2889961d8b69SVladimir Oltean 289054c31984SVladimir Oltean if (!vid) 289154c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 289254c31984SVladimir Oltean 2893961d8b69SVladimir Oltean list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) { 2894961d8b69SVladimir Oltean if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid || 2895961d8b69SVladimir Oltean fdb->bond != bond) 2896961d8b69SVladimir Oltean continue; 2897961d8b69SVladimir Oltean 2898961d8b69SVladimir Oltean ocelot_mact_forget(ocelot, addr, vid); 2899961d8b69SVladimir Oltean list_del(&fdb->list); 2900961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2901961d8b69SVladimir Oltean kfree(fdb); 2902961d8b69SVladimir Oltean 2903961d8b69SVladimir Oltean return 0; 2904961d8b69SVladimir Oltean } 2905961d8b69SVladimir Oltean 2906961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2907961d8b69SVladimir Oltean 2908961d8b69SVladimir Oltean return -ENOENT; 2909961d8b69SVladimir Oltean } 2910961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del); 2911961d8b69SVladimir Oltean 2912a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. 2913a8015dedSVladimir Oltean * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. 29140b912fc9SVladimir Oltean * In the special case that it's the NPI port that we're configuring, the 29150b912fc9SVladimir Oltean * length of the tag and optional prefix needs to be accounted for privately, 29160b912fc9SVladimir Oltean * in order to be able to sustain communication at the requested @sdu. 2917a8015dedSVladimir Oltean */ 29180b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) 291931350d7fSVladimir Oltean { 292031350d7fSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2921a8015dedSVladimir Oltean int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; 2922e8e6e73dSVladimir Oltean int pause_start, pause_stop; 2923601e984fSVladimir Oltean int atop, atop_tot; 292431350d7fSVladimir Oltean 29250b912fc9SVladimir Oltean if (port == ocelot->npi) { 29260b912fc9SVladimir Oltean maxlen += OCELOT_TAG_LEN; 29270b912fc9SVladimir Oltean 2928cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 29290b912fc9SVladimir Oltean maxlen += OCELOT_SHORT_PREFIX_LEN; 2930cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 29310b912fc9SVladimir Oltean maxlen += OCELOT_LONG_PREFIX_LEN; 29320b912fc9SVladimir Oltean } 29330b912fc9SVladimir Oltean 2934a8015dedSVladimir Oltean ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); 2935fa914e9cSVladimir Oltean 2936e8e6e73dSVladimir Oltean /* Set Pause watermark hysteresis */ 2937e8e6e73dSVladimir Oltean pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; 2938e8e6e73dSVladimir Oltean pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; 2939541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, 2940541132f0SMaxim Kochetkov pause_start); 2941541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, 2942541132f0SMaxim Kochetkov pause_stop); 2943fa914e9cSVladimir Oltean 2944601e984fSVladimir Oltean /* Tail dropping watermarks */ 2945f6fe01d6SVladimir Oltean atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / 2946a8015dedSVladimir Oltean OCELOT_BUFFER_CELL_SZ; 2947601e984fSVladimir Oltean atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; 2948601e984fSVladimir Oltean ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); 2949601e984fSVladimir Oltean ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); 2950fa914e9cSVladimir Oltean } 29510b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen); 29520b912fc9SVladimir Oltean 29530b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port) 29540b912fc9SVladimir Oltean { 29550b912fc9SVladimir Oltean int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; 29560b912fc9SVladimir Oltean 29570b912fc9SVladimir Oltean if (port == ocelot->npi) { 29580b912fc9SVladimir Oltean max_mtu -= OCELOT_TAG_LEN; 29590b912fc9SVladimir Oltean 2960cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 29610b912fc9SVladimir Oltean max_mtu -= OCELOT_SHORT_PREFIX_LEN; 2962cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 29630b912fc9SVladimir Oltean max_mtu -= OCELOT_LONG_PREFIX_LEN; 29640b912fc9SVladimir Oltean } 29650b912fc9SVladimir Oltean 29660b912fc9SVladimir Oltean return max_mtu; 29670b912fc9SVladimir Oltean } 29680b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu); 2969fa914e9cSVladimir Oltean 2970421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port, 2971421741eaSVladimir Oltean bool enabled) 2972421741eaSVladimir Oltean { 2973421741eaSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2974421741eaSVladimir Oltean u32 val = 0; 2975421741eaSVladimir Oltean 2976421741eaSVladimir Oltean if (enabled) 2977421741eaSVladimir Oltean val = ANA_PORT_PORT_CFG_LEARN_ENA; 2978421741eaSVladimir Oltean 2979421741eaSVladimir Oltean ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA, 2980421741eaSVladimir Oltean ANA_PORT_PORT_CFG, port); 2981421741eaSVladimir Oltean 2982421741eaSVladimir Oltean ocelot_port->learn_ena = enabled; 2983421741eaSVladimir Oltean } 2984421741eaSVladimir Oltean 2985421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port, 2986421741eaSVladimir Oltean bool enabled) 2987421741eaSVladimir Oltean { 2988421741eaSVladimir Oltean u32 val = 0; 2989421741eaSVladimir Oltean 2990421741eaSVladimir Oltean if (enabled) 2991421741eaSVladimir Oltean val = BIT(port); 2992421741eaSVladimir Oltean 2993421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC); 2994421741eaSVladimir Oltean } 2995421741eaSVladimir Oltean 2996421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port, 2997421741eaSVladimir Oltean bool enabled) 2998421741eaSVladimir Oltean { 2999421741eaSVladimir Oltean u32 val = 0; 3000421741eaSVladimir Oltean 3001421741eaSVladimir Oltean if (enabled) 3002421741eaSVladimir Oltean val = BIT(port); 3003421741eaSVladimir Oltean 3004421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC); 30054cf35a2bSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4); 30064cf35a2bSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6); 3007421741eaSVladimir Oltean } 3008421741eaSVladimir Oltean 3009421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port, 3010421741eaSVladimir Oltean bool enabled) 3011421741eaSVladimir Oltean { 3012421741eaSVladimir Oltean u32 val = 0; 3013421741eaSVladimir Oltean 3014421741eaSVladimir Oltean if (enabled) 3015421741eaSVladimir Oltean val = BIT(port); 3016421741eaSVladimir Oltean 3017421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC); 3018421741eaSVladimir Oltean } 3019421741eaSVladimir Oltean 3020421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port, 3021421741eaSVladimir Oltean struct switchdev_brport_flags flags) 3022421741eaSVladimir Oltean { 3023421741eaSVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 3024421741eaSVladimir Oltean BR_BCAST_FLOOD)) 3025421741eaSVladimir Oltean return -EINVAL; 3026421741eaSVladimir Oltean 3027421741eaSVladimir Oltean return 0; 3028421741eaSVladimir Oltean } 3029421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags); 3030421741eaSVladimir Oltean 3031421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port, 3032421741eaSVladimir Oltean struct switchdev_brport_flags flags) 3033421741eaSVladimir Oltean { 3034421741eaSVladimir Oltean if (flags.mask & BR_LEARNING) 3035421741eaSVladimir Oltean ocelot_port_set_learning(ocelot, port, 3036421741eaSVladimir Oltean !!(flags.val & BR_LEARNING)); 3037421741eaSVladimir Oltean 3038421741eaSVladimir Oltean if (flags.mask & BR_FLOOD) 3039421741eaSVladimir Oltean ocelot_port_set_ucast_flood(ocelot, port, 3040421741eaSVladimir Oltean !!(flags.val & BR_FLOOD)); 3041421741eaSVladimir Oltean 3042421741eaSVladimir Oltean if (flags.mask & BR_MCAST_FLOOD) 3043421741eaSVladimir Oltean ocelot_port_set_mcast_flood(ocelot, port, 3044421741eaSVladimir Oltean !!(flags.val & BR_MCAST_FLOOD)); 3045421741eaSVladimir Oltean 3046421741eaSVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) 3047421741eaSVladimir Oltean ocelot_port_set_bcast_flood(ocelot, port, 3048421741eaSVladimir Oltean !!(flags.val & BR_BCAST_FLOOD)); 3049421741eaSVladimir Oltean } 3050421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags); 3051421741eaSVladimir Oltean 3052978777d0SVladimir Oltean int ocelot_port_get_default_prio(struct ocelot *ocelot, int port) 3053978777d0SVladimir Oltean { 3054978777d0SVladimir Oltean int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port); 3055978777d0SVladimir Oltean 3056978777d0SVladimir Oltean return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val); 3057978777d0SVladimir Oltean } 3058978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio); 3059978777d0SVladimir Oltean 3060978777d0SVladimir Oltean int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio) 3061978777d0SVladimir Oltean { 306272f56fdbSVladimir Oltean if (prio >= OCELOT_NUM_TC) 3063978777d0SVladimir Oltean return -ERANGE; 3064978777d0SVladimir Oltean 3065978777d0SVladimir Oltean ocelot_rmw_gix(ocelot, 3066978777d0SVladimir Oltean ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio), 3067978777d0SVladimir Oltean ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M, 3068978777d0SVladimir Oltean ANA_PORT_QOS_CFG, 3069978777d0SVladimir Oltean port); 3070978777d0SVladimir Oltean 3071978777d0SVladimir Oltean return 0; 3072978777d0SVladimir Oltean } 3073978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio); 3074978777d0SVladimir Oltean 3075978777d0SVladimir Oltean int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp) 3076978777d0SVladimir Oltean { 3077978777d0SVladimir Oltean int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port); 3078978777d0SVladimir Oltean int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp); 3079978777d0SVladimir Oltean 3080978777d0SVladimir Oltean /* Return error if DSCP prioritization isn't enabled */ 3081978777d0SVladimir Oltean if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA)) 3082978777d0SVladimir Oltean return -EOPNOTSUPP; 3083978777d0SVladimir Oltean 3084978777d0SVladimir Oltean if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) { 3085978777d0SVladimir Oltean dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg); 3086978777d0SVladimir Oltean /* Re-read ANA_DSCP_CFG for the translated DSCP */ 3087978777d0SVladimir Oltean dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp); 3088978777d0SVladimir Oltean } 3089978777d0SVladimir Oltean 3090978777d0SVladimir Oltean /* If the DSCP value is not trusted, the QoS classification falls back 3091978777d0SVladimir Oltean * to VLAN PCP or port-based default. 3092978777d0SVladimir Oltean */ 3093978777d0SVladimir Oltean if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)) 3094978777d0SVladimir Oltean return -EOPNOTSUPP; 3095978777d0SVladimir Oltean 3096978777d0SVladimir Oltean return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg); 3097978777d0SVladimir Oltean } 3098978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio); 3099978777d0SVladimir Oltean 3100978777d0SVladimir Oltean int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio) 3101978777d0SVladimir Oltean { 3102978777d0SVladimir Oltean int mask, val; 3103978777d0SVladimir Oltean 310472f56fdbSVladimir Oltean if (prio >= OCELOT_NUM_TC) 3105978777d0SVladimir Oltean return -ERANGE; 3106978777d0SVladimir Oltean 3107978777d0SVladimir Oltean /* There is at least one app table priority (this one), so we need to 3108978777d0SVladimir Oltean * make sure DSCP prioritization is enabled on the port. 3109978777d0SVladimir Oltean * Also make sure DSCP translation is disabled 3110978777d0SVladimir Oltean * (dcbnl doesn't support it). 3111978777d0SVladimir Oltean */ 3112978777d0SVladimir Oltean mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA | 3113978777d0SVladimir Oltean ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA; 3114978777d0SVladimir Oltean 3115978777d0SVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask, 3116978777d0SVladimir Oltean ANA_PORT_QOS_CFG, port); 3117978777d0SVladimir Oltean 3118978777d0SVladimir Oltean /* Trust this DSCP value and map it to the given QoS class */ 3119978777d0SVladimir Oltean val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio); 3120978777d0SVladimir Oltean 3121978777d0SVladimir Oltean ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp); 3122978777d0SVladimir Oltean 3123978777d0SVladimir Oltean return 0; 3124978777d0SVladimir Oltean } 3125978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio); 3126978777d0SVladimir Oltean 3127978777d0SVladimir Oltean int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio) 3128978777d0SVladimir Oltean { 3129978777d0SVladimir Oltean int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp); 3130978777d0SVladimir Oltean int mask, i; 3131978777d0SVladimir Oltean 3132978777d0SVladimir Oltean /* During a "dcb app replace" command, the new app table entry will be 3133978777d0SVladimir Oltean * added first, then the old one will be deleted. But the hardware only 3134978777d0SVladimir Oltean * supports one QoS class per DSCP value (duh), so if we blindly delete 3135978777d0SVladimir Oltean * the app table entry for this DSCP value, we end up deleting the 3136978777d0SVladimir Oltean * entry with the new priority. Avoid that by checking whether user 3137978777d0SVladimir Oltean * space wants to delete the priority which is currently configured, or 3138978777d0SVladimir Oltean * something else which is no longer current. 3139978777d0SVladimir Oltean */ 3140978777d0SVladimir Oltean if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio) 3141978777d0SVladimir Oltean return 0; 3142978777d0SVladimir Oltean 3143978777d0SVladimir Oltean /* Untrust this DSCP value */ 3144978777d0SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp); 3145978777d0SVladimir Oltean 3146978777d0SVladimir Oltean for (i = 0; i < 64; i++) { 3147978777d0SVladimir Oltean int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i); 3148978777d0SVladimir Oltean 3149978777d0SVladimir Oltean /* There are still app table entries on the port, so we need to 3150978777d0SVladimir Oltean * keep DSCP enabled, nothing to do. 3151978777d0SVladimir Oltean */ 3152978777d0SVladimir Oltean if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA) 3153978777d0SVladimir Oltean return 0; 3154978777d0SVladimir Oltean } 3155978777d0SVladimir Oltean 3156978777d0SVladimir Oltean /* Disable DSCP QoS classification if there isn't any trusted 3157978777d0SVladimir Oltean * DSCP value left. 3158978777d0SVladimir Oltean */ 3159978777d0SVladimir Oltean mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA | 3160978777d0SVladimir Oltean ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA; 3161978777d0SVladimir Oltean 3162978777d0SVladimir Oltean ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port); 3163978777d0SVladimir Oltean 3164978777d0SVladimir Oltean return 0; 3165978777d0SVladimir Oltean } 3166978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio); 3167978777d0SVladimir Oltean 3168f2a0e216SVladimir Oltean struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to, 3169ccb6ed42SVladimir Oltean struct netlink_ext_ack *extack) 3170ccb6ed42SVladimir Oltean { 3171ccb6ed42SVladimir Oltean struct ocelot_mirror *m = ocelot->mirror; 3172ccb6ed42SVladimir Oltean 3173ccb6ed42SVladimir Oltean if (m) { 3174ccb6ed42SVladimir Oltean if (m->to != to) { 3175ccb6ed42SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 3176ccb6ed42SVladimir Oltean "Mirroring already configured towards different egress port"); 3177ccb6ed42SVladimir Oltean return ERR_PTR(-EBUSY); 3178ccb6ed42SVladimir Oltean } 3179ccb6ed42SVladimir Oltean 3180ccb6ed42SVladimir Oltean refcount_inc(&m->refcount); 3181ccb6ed42SVladimir Oltean return m; 3182ccb6ed42SVladimir Oltean } 3183ccb6ed42SVladimir Oltean 3184ccb6ed42SVladimir Oltean m = kzalloc(sizeof(*m), GFP_KERNEL); 3185ccb6ed42SVladimir Oltean if (!m) 3186ccb6ed42SVladimir Oltean return ERR_PTR(-ENOMEM); 3187ccb6ed42SVladimir Oltean 3188ccb6ed42SVladimir Oltean m->to = to; 3189ccb6ed42SVladimir Oltean refcount_set(&m->refcount, 1); 3190ccb6ed42SVladimir Oltean ocelot->mirror = m; 3191ccb6ed42SVladimir Oltean 3192ccb6ed42SVladimir Oltean /* Program the mirror port to hardware */ 3193ccb6ed42SVladimir Oltean ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS); 3194ccb6ed42SVladimir Oltean 3195ccb6ed42SVladimir Oltean return m; 3196ccb6ed42SVladimir Oltean } 3197ccb6ed42SVladimir Oltean 3198f2a0e216SVladimir Oltean void ocelot_mirror_put(struct ocelot *ocelot) 3199ccb6ed42SVladimir Oltean { 3200ccb6ed42SVladimir Oltean struct ocelot_mirror *m = ocelot->mirror; 3201ccb6ed42SVladimir Oltean 3202ccb6ed42SVladimir Oltean if (!refcount_dec_and_test(&m->refcount)) 3203ccb6ed42SVladimir Oltean return; 3204ccb6ed42SVladimir Oltean 3205ccb6ed42SVladimir Oltean ocelot_write(ocelot, 0, ANA_MIRRORPORTS); 3206ccb6ed42SVladimir Oltean ocelot->mirror = NULL; 3207ccb6ed42SVladimir Oltean kfree(m); 3208ccb6ed42SVladimir Oltean } 3209ccb6ed42SVladimir Oltean 3210ccb6ed42SVladimir Oltean int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to, 3211ccb6ed42SVladimir Oltean bool ingress, struct netlink_ext_ack *extack) 3212ccb6ed42SVladimir Oltean { 3213ccb6ed42SVladimir Oltean struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack); 3214ccb6ed42SVladimir Oltean 3215ccb6ed42SVladimir Oltean if (IS_ERR(m)) 3216ccb6ed42SVladimir Oltean return PTR_ERR(m); 3217ccb6ed42SVladimir Oltean 3218ccb6ed42SVladimir Oltean if (ingress) { 3219ccb6ed42SVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA, 3220ccb6ed42SVladimir Oltean ANA_PORT_PORT_CFG_SRC_MIRROR_ENA, 3221ccb6ed42SVladimir Oltean ANA_PORT_PORT_CFG, from); 3222ccb6ed42SVladimir Oltean } else { 3223ccb6ed42SVladimir Oltean ocelot_rmw(ocelot, BIT(from), BIT(from), 3224ccb6ed42SVladimir Oltean ANA_EMIRRORPORTS); 3225ccb6ed42SVladimir Oltean } 3226ccb6ed42SVladimir Oltean 3227ccb6ed42SVladimir Oltean return 0; 3228ccb6ed42SVladimir Oltean } 3229ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_add); 3230ccb6ed42SVladimir Oltean 3231ccb6ed42SVladimir Oltean void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress) 3232ccb6ed42SVladimir Oltean { 3233ccb6ed42SVladimir Oltean if (ingress) { 3234ccb6ed42SVladimir Oltean ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA, 3235ccb6ed42SVladimir Oltean ANA_PORT_PORT_CFG, from); 3236ccb6ed42SVladimir Oltean } else { 3237ccb6ed42SVladimir Oltean ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS); 3238ccb6ed42SVladimir Oltean } 3239ccb6ed42SVladimir Oltean 3240ccb6ed42SVladimir Oltean ocelot_mirror_put(ocelot); 3241ccb6ed42SVladimir Oltean } 3242ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_del); 3243ccb6ed42SVladimir Oltean 32445e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port) 3245fa914e9cSVladimir Oltean { 3246fa914e9cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 3247fa914e9cSVladimir Oltean 3248b049da13SYangbo Lu skb_queue_head_init(&ocelot_port->tx_skbs); 324931350d7fSVladimir Oltean 325031350d7fSVladimir Oltean /* Basic L2 initialization */ 325131350d7fSVladimir Oltean 32525bc9d2e6SVladimir Oltean /* Set MAC IFG Gaps 32535bc9d2e6SVladimir Oltean * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 32545bc9d2e6SVladimir Oltean * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 32555bc9d2e6SVladimir Oltean */ 32565bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), 32575bc9d2e6SVladimir Oltean DEV_MAC_IFG_CFG); 32585bc9d2e6SVladimir Oltean 32595bc9d2e6SVladimir Oltean /* Load seed (0) and set MAC HDX late collision */ 32605bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 32615bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG_SEED_LOAD, 32625bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 32635bc9d2e6SVladimir Oltean mdelay(1); 32645bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 32655bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 32665bc9d2e6SVladimir Oltean 32675bc9d2e6SVladimir Oltean /* Set Max Length and maximum tags allowed */ 3268a8015dedSVladimir Oltean ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); 32695bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 32705bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 3271a8015dedSVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | 32725bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 32735bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG); 32745bc9d2e6SVladimir Oltean 32755bc9d2e6SVladimir Oltean /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 32765bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 32775bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); 32785bc9d2e6SVladimir Oltean 3279e8e6e73dSVladimir Oltean /* Enable transmission of pause frames */ 3280541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 3281e8e6e73dSVladimir Oltean 328231350d7fSVladimir Oltean /* Drop frames with multicast source address */ 328331350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 328431350d7fSVladimir Oltean ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 328531350d7fSVladimir Oltean ANA_PORT_DROP_CFG, port); 328631350d7fSVladimir Oltean 328731350d7fSVladimir Oltean /* Set default VLAN and tag type to 8021Q. */ 328831350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), 328931350d7fSVladimir Oltean REW_PORT_VLAN_CFG_PORT_TPID_M, 329031350d7fSVladimir Oltean REW_PORT_VLAN_CFG, port); 329131350d7fSVladimir Oltean 3292421741eaSVladimir Oltean /* Disable source address learning for standalone mode */ 3293421741eaSVladimir Oltean ocelot_port_set_learning(ocelot, port, false); 3294421741eaSVladimir Oltean 329546efe4efSVladimir Oltean /* Set the port's initial logical port ID value, enable receiving 329646efe4efSVladimir Oltean * frames on it, and configure the MAC address learning type to 329746efe4efSVladimir Oltean * automatic. 329846efe4efSVladimir Oltean */ 329946efe4efSVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 330046efe4efSVladimir Oltean ANA_PORT_PORT_CFG_RECV_ENA | 330146efe4efSVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 330246efe4efSVladimir Oltean ANA_PORT_PORT_CFG, port); 330346efe4efSVladimir Oltean 330431350d7fSVladimir Oltean /* Enable vcap lookups */ 330531350d7fSVladimir Oltean ocelot_vcap_enable(ocelot, port); 330631350d7fSVladimir Oltean } 33075e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port); 330831350d7fSVladimir Oltean 33092d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues 33102d44b097SVladimir Oltean * accessible through register MMIO, frame DMA or Ethernet (in case 33112d44b097SVladimir Oltean * NPI mode is used). 331269df578cSVladimir Oltean */ 33132d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot) 331421468199SVladimir Oltean { 331569df578cSVladimir Oltean int cpu = ocelot->num_phys_ports; 331669df578cSVladimir Oltean 331769df578cSVladimir Oltean /* The unicast destination PGID for the CPU port module is unused */ 331821468199SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 331969df578cSVladimir Oltean /* Instead set up a multicast destination PGID for traffic copied to 332069df578cSVladimir Oltean * the CPU. Whitelisted MAC addresses like the port netdevice MAC 332169df578cSVladimir Oltean * addresses will be copied to the CPU via this PGID. 332269df578cSVladimir Oltean */ 332321468199SVladimir Oltean ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 332421468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 332521468199SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 332621468199SVladimir Oltean ANA_PORT_PORT_CFG, cpu); 332721468199SVladimir Oltean 332869df578cSVladimir Oltean /* Enable CPU port module */ 3329886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 333069df578cSVladimir Oltean /* CPU port Injection/Extraction configuration */ 3331886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, 3332cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 3333886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, 3334cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 333521468199SVladimir Oltean 333621468199SVladimir Oltean /* Configure the CPU port to be VLAN aware */ 3337bfbab310SVladimir Oltean ocelot_write_gix(ocelot, 333854c31984SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) | 333921468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 334021468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 334121468199SVladimir Oltean ANA_PORT_VLAN_CFG, cpu); 334221468199SVladimir Oltean } 334321468199SVladimir Oltean 3344f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot) 3345f6fe01d6SVladimir Oltean { 3346f6fe01d6SVladimir Oltean int mmgt, eq_ctrl; 3347f6fe01d6SVladimir Oltean 3348f6fe01d6SVladimir Oltean /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds 3349f6fe01d6SVladimir Oltean * the number of 240-byte free memory words (aka 4-cell chunks) and not 3350f6fe01d6SVladimir Oltean * 192 bytes as the documentation incorrectly says. 3351f6fe01d6SVladimir Oltean */ 3352f6fe01d6SVladimir Oltean mmgt = ocelot_read(ocelot, SYS_MMGT); 3353f6fe01d6SVladimir Oltean ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); 3354f6fe01d6SVladimir Oltean 3355f6fe01d6SVladimir Oltean eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); 3356f6fe01d6SVladimir Oltean ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); 3357f6fe01d6SVladimir Oltean } 3358f6fe01d6SVladimir Oltean 3359a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot) 3360a556c76aSAlexandre Belloni { 3361a556c76aSAlexandre Belloni char queue_name[32]; 336221468199SVladimir Oltean int i, ret; 336321468199SVladimir Oltean u32 port; 3364a556c76aSAlexandre Belloni 33653a77b593SVladimir Oltean if (ocelot->ops->reset) { 33663a77b593SVladimir Oltean ret = ocelot->ops->reset(ocelot); 33673a77b593SVladimir Oltean if (ret) { 33683a77b593SVladimir Oltean dev_err(ocelot->dev, "Switch reset failed\n"); 33693a77b593SVladimir Oltean return ret; 33703a77b593SVladimir Oltean } 33713a77b593SVladimir Oltean } 33723a77b593SVladimir Oltean 3373a556c76aSAlexandre Belloni ocelot->stats = devm_kcalloc(ocelot->dev, 337491904600SVladimir Oltean ocelot->num_phys_ports * OCELOT_NUM_STATS, 3375a556c76aSAlexandre Belloni sizeof(u64), GFP_KERNEL); 3376a556c76aSAlexandre Belloni if (!ocelot->stats) 3377a556c76aSAlexandre Belloni return -ENOMEM; 3378a556c76aSAlexandre Belloni 337922d842e3SVladimir Oltean spin_lock_init(&ocelot->stats_lock); 33804e3b0468SAntoine Tenart mutex_init(&ocelot->ptp_lock); 33812468346cSVladimir Oltean mutex_init(&ocelot->mact_lock); 33828abe1970SVladimir Oltean mutex_init(&ocelot->fwd_domain_lock); 33838670dc33SXiaoliang Yang mutex_init(&ocelot->tas_lock); 33844e3b0468SAntoine Tenart spin_lock_init(&ocelot->ptp_clock_lock); 338552849bcfSVladimir Oltean spin_lock_init(&ocelot->ts_id_lock); 3386a556c76aSAlexandre Belloni snprintf(queue_name, sizeof(queue_name), "%s-stats", 3387a556c76aSAlexandre Belloni dev_name(ocelot->dev)); 3388a556c76aSAlexandre Belloni ocelot->stats_queue = create_singlethread_workqueue(queue_name); 3389a556c76aSAlexandre Belloni if (!ocelot->stats_queue) 3390a556c76aSAlexandre Belloni return -ENOMEM; 3391a556c76aSAlexandre Belloni 3392ca0b272bSVladimir Oltean ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); 3393ca0b272bSVladimir Oltean if (!ocelot->owq) { 3394ca0b272bSVladimir Oltean destroy_workqueue(ocelot->stats_queue); 3395ca0b272bSVladimir Oltean return -ENOMEM; 3396ca0b272bSVladimir Oltean } 3397ca0b272bSVladimir Oltean 33982b120ddeSClaudiu Manoil INIT_LIST_HEAD(&ocelot->multicast); 3399e5d1f896SVladimir Oltean INIT_LIST_HEAD(&ocelot->pgids); 340090e0aa8dSVladimir Oltean INIT_LIST_HEAD(&ocelot->vlans); 3401961d8b69SVladimir Oltean INIT_LIST_HEAD(&ocelot->lag_fdbs); 3402f6fe01d6SVladimir Oltean ocelot_detect_features(ocelot); 3403a556c76aSAlexandre Belloni ocelot_mact_init(ocelot); 3404a556c76aSAlexandre Belloni ocelot_vlan_init(ocelot); 3405aae4e500SVladimir Oltean ocelot_vcap_init(ocelot); 34062d44b097SVladimir Oltean ocelot_cpu_port_init(ocelot); 3407a556c76aSAlexandre Belloni 340823e2c506SXiaoliang Yang if (ocelot->ops->psfp_init) 340923e2c506SXiaoliang Yang ocelot->ops->psfp_init(ocelot); 341023e2c506SXiaoliang Yang 3411a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 3412a556c76aSAlexandre Belloni /* Clear all counters (5 groups) */ 3413a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 3414a556c76aSAlexandre Belloni SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 3415a556c76aSAlexandre Belloni SYS_STAT_CFG); 3416a556c76aSAlexandre Belloni } 3417a556c76aSAlexandre Belloni 3418a556c76aSAlexandre Belloni /* Only use S-Tag */ 3419a556c76aSAlexandre Belloni ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 3420a556c76aSAlexandre Belloni 3421a556c76aSAlexandre Belloni /* Aggregation mode */ 3422a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 3423a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_DMAC_ENA | 3424a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 3425f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | 3426f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | 3427f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, 3428f79c20c8SVladimir Oltean ANA_AGGR_CFG); 3429a556c76aSAlexandre Belloni 3430a556c76aSAlexandre Belloni /* Set MAC age time to default value. The entry is aged after 3431a556c76aSAlexandre Belloni * 2*AGE_PERIOD 3432a556c76aSAlexandre Belloni */ 3433a556c76aSAlexandre Belloni ocelot_write(ocelot, 3434a556c76aSAlexandre Belloni ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 3435a556c76aSAlexandre Belloni ANA_AUTOAGE); 3436a556c76aSAlexandre Belloni 3437a556c76aSAlexandre Belloni /* Disable learning for frames discarded by VLAN ingress filtering */ 3438a556c76aSAlexandre Belloni regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 3439a556c76aSAlexandre Belloni 3440a556c76aSAlexandre Belloni /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 3441a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 3442a556c76aSAlexandre Belloni SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 3443a556c76aSAlexandre Belloni 3444a556c76aSAlexandre Belloni /* Setup flooding PGIDs */ 3445edd2410bSVladimir Oltean for (i = 0; i < ocelot->num_flooding_pgids; i++) 3446a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 3447b360d94fSVladimir Oltean ANA_FLOODING_FLD_BROADCAST(PGID_BC) | 3448a556c76aSAlexandre Belloni ANA_FLOODING_FLD_UNICAST(PGID_UC), 3449edd2410bSVladimir Oltean ANA_FLOODING, i); 3450a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 3451a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 3452a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 3453a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 3454a556c76aSAlexandre Belloni ANA_FLOODING_IPMC); 3455a556c76aSAlexandre Belloni 3456a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 3457a556c76aSAlexandre Belloni /* Transmit the frame to the local port. */ 3458a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 3459a556c76aSAlexandre Belloni /* Do not forward BPDU frames to the front ports. */ 3460a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, 3461a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 3462a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG, 3463a556c76aSAlexandre Belloni port); 3464a556c76aSAlexandre Belloni /* Ensure bridging is disabled */ 3465a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 3466a556c76aSAlexandre Belloni } 3467a556c76aSAlexandre Belloni 346896b029b0SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, i) { 3469a556c76aSAlexandre Belloni u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 3470a556c76aSAlexandre Belloni 3471a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 3472a556c76aSAlexandre Belloni } 3473ebb1bb40SHoratiu Vultur 3474ebb1bb40SHoratiu Vultur ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE); 3475ebb1bb40SHoratiu Vultur 3476b360d94fSVladimir Oltean /* Allow broadcast and unknown L2 multicast to the CPU. */ 3477b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 3478b360d94fSVladimir Oltean ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 3479a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_MC); 3480b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 3481b360d94fSVladimir Oltean ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 3482b360d94fSVladimir Oltean ANA_PGID_PGID, PGID_BC); 3483a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 3484a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 3485a556c76aSAlexandre Belloni 3486a556c76aSAlexandre Belloni /* Allow manual injection via DEVCPU_QS registers, and byte swap these 3487a556c76aSAlexandre Belloni * registers endianness. 3488a556c76aSAlexandre Belloni */ 3489a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 3490a556c76aSAlexandre Belloni QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 3491a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 3492a556c76aSAlexandre Belloni QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 3493a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 3494a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LRN(2) | 3495a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 3496a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 3497a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 3498a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 3499a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 3500a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IGMP(6) | 3501a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 3502a556c76aSAlexandre Belloni for (i = 0; i < 16; i++) 3503a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 3504a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 3505a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG, i); 3506a556c76aSAlexandre Belloni 3507d87b1c08SColin Foster ret = ocelot_prepare_stats_regions(ocelot); 3508d87b1c08SColin Foster if (ret) { 3509d87b1c08SColin Foster destroy_workqueue(ocelot->stats_queue); 3510d87b1c08SColin Foster destroy_workqueue(ocelot->owq); 3511d87b1c08SColin Foster return ret; 3512d87b1c08SColin Foster } 3513d87b1c08SColin Foster 35141e1caa97SClaudiu Manoil INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 3515a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 3516a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 35174e3b0468SAntoine Tenart 3518a556c76aSAlexandre Belloni return 0; 3519a556c76aSAlexandre Belloni } 3520a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init); 3521a556c76aSAlexandre Belloni 3522a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot) 3523a556c76aSAlexandre Belloni { 3524c5d13969SClaudiu Manoil cancel_delayed_work(&ocelot->stats_work); 3525a556c76aSAlexandre Belloni destroy_workqueue(ocelot->stats_queue); 3526ca0b272bSVladimir Oltean destroy_workqueue(ocelot->owq); 3527a556c76aSAlexandre Belloni } 3528a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit); 3529a556c76aSAlexandre Belloni 3530e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port) 3531e5fb512dSVladimir Oltean { 3532e5fb512dSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 3533e5fb512dSVladimir Oltean 3534e5fb512dSVladimir Oltean skb_queue_purge(&ocelot_port->tx_skbs); 3535e5fb512dSVladimir Oltean } 3536e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port); 3537e5fb512dSVladimir Oltean 3538a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL"); 3539