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 1863a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 1864a556c76aSAlexandre Belloni memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, 1865a556c76aSAlexandre Belloni ETH_GSTRING_LEN); 1866a556c76aSAlexandre Belloni } 18675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings); 1868a556c76aSAlexandre Belloni 18697fbf6795SColin Foster /* Caller must hold &ocelot->stats_lock */ 1870d87b1c08SColin Foster static int ocelot_port_update_stats(struct ocelot *ocelot, int port) 1871a556c76aSAlexandre Belloni { 1872d87b1c08SColin Foster unsigned int idx = port * ocelot->num_stats; 1873d87b1c08SColin Foster struct ocelot_stats_region *region; 1874d87b1c08SColin Foster int err, j; 1875a556c76aSAlexandre Belloni 1876a556c76aSAlexandre Belloni /* Configure the port to read the stats from */ 1877e27d785eSColin Foster ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port), SYS_STAT_CFG); 1878a556c76aSAlexandre Belloni 1879d87b1c08SColin Foster list_for_each_entry(region, &ocelot->stats_regions, node) { 1880d87b1c08SColin Foster err = ocelot_bulk_read_rix(ocelot, SYS_COUNT_RX_OCTETS, 1881d87b1c08SColin Foster region->offset, region->buf, 1882d87b1c08SColin Foster region->count); 1883d87b1c08SColin Foster if (err) 1884d87b1c08SColin Foster return err; 1885a556c76aSAlexandre Belloni 1886d87b1c08SColin Foster for (j = 0; j < region->count; j++) { 1887d87b1c08SColin Foster u64 *stat = &ocelot->stats[idx + j]; 1888d87b1c08SColin Foster u64 val = region->buf[j]; 1889a556c76aSAlexandre Belloni 1890d87b1c08SColin Foster if (val < (*stat & U32_MAX)) 1891d87b1c08SColin Foster *stat += (u64)1 << 32; 1892a556c76aSAlexandre Belloni 1893d87b1c08SColin Foster *stat = (*stat & ~(u64)U32_MAX) + val; 1894a556c76aSAlexandre Belloni } 1895d87b1c08SColin Foster 1896d87b1c08SColin Foster idx += region->count; 1897d87b1c08SColin Foster } 1898d87b1c08SColin Foster 1899d87b1c08SColin Foster return err; 19001e1caa97SClaudiu Manoil } 19011e1caa97SClaudiu Manoil 19021e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work) 19031e1caa97SClaudiu Manoil { 19041e1caa97SClaudiu Manoil struct delayed_work *del_work = to_delayed_work(work); 19051e1caa97SClaudiu Manoil struct ocelot *ocelot = container_of(del_work, struct ocelot, 19061e1caa97SClaudiu Manoil stats_work); 1907d87b1c08SColin Foster int i, err; 19081e1caa97SClaudiu Manoil 19097fbf6795SColin Foster mutex_lock(&ocelot->stats_lock); 1910d87b1c08SColin Foster for (i = 0; i < ocelot->num_phys_ports; i++) { 1911d87b1c08SColin Foster err = ocelot_port_update_stats(ocelot, i); 1912d87b1c08SColin Foster if (err) 1913d87b1c08SColin Foster break; 1914d87b1c08SColin Foster } 19157fbf6795SColin Foster mutex_unlock(&ocelot->stats_lock); 19161e1caa97SClaudiu Manoil 1917d87b1c08SColin Foster if (err) 1918d87b1c08SColin Foster dev_err(ocelot->dev, "Error %d updating ethtool stats\n", err); 1919d87b1c08SColin Foster 1920a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1921a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 1922a556c76aSAlexandre Belloni } 1923a556c76aSAlexandre Belloni 19245e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) 1925a556c76aSAlexandre Belloni { 1926d87b1c08SColin Foster int i, err; 1927a556c76aSAlexandre Belloni 19287fbf6795SColin Foster mutex_lock(&ocelot->stats_lock); 19297fbf6795SColin Foster 1930a556c76aSAlexandre Belloni /* check and update now */ 1931d87b1c08SColin Foster err = ocelot_port_update_stats(ocelot, port); 1932a556c76aSAlexandre Belloni 1933a556c76aSAlexandre Belloni /* Copy all counters */ 1934a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 1935004d44f6SVladimir Oltean *data++ = ocelot->stats[port * ocelot->num_stats + i]; 19367fbf6795SColin Foster 19377fbf6795SColin Foster mutex_unlock(&ocelot->stats_lock); 1938d87b1c08SColin Foster 1939d87b1c08SColin Foster if (err) 1940d87b1c08SColin Foster dev_err(ocelot->dev, "Error %d updating ethtool stats\n", err); 1941a556c76aSAlexandre Belloni } 19425e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats); 1943a556c76aSAlexandre Belloni 19445e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) 1945c7282d38SVladimir Oltean { 1946a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 1947a556c76aSAlexandre Belloni return -EOPNOTSUPP; 1948c7282d38SVladimir Oltean 1949a556c76aSAlexandre Belloni return ocelot->num_stats; 1950a556c76aSAlexandre Belloni } 19515e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count); 1952a556c76aSAlexandre Belloni 1953d87b1c08SColin Foster static int ocelot_prepare_stats_regions(struct ocelot *ocelot) 1954d87b1c08SColin Foster { 1955d87b1c08SColin Foster struct ocelot_stats_region *region = NULL; 1956d87b1c08SColin Foster unsigned int last; 1957d87b1c08SColin Foster int i; 1958d87b1c08SColin Foster 1959d87b1c08SColin Foster INIT_LIST_HEAD(&ocelot->stats_regions); 1960d87b1c08SColin Foster 1961d87b1c08SColin Foster for (i = 0; i < ocelot->num_stats; i++) { 1962d87b1c08SColin Foster if (region && ocelot->stats_layout[i].offset == last + 1) { 1963d87b1c08SColin Foster region->count++; 1964d87b1c08SColin Foster } else { 1965d87b1c08SColin Foster region = devm_kzalloc(ocelot->dev, sizeof(*region), 1966d87b1c08SColin Foster GFP_KERNEL); 1967d87b1c08SColin Foster if (!region) 1968d87b1c08SColin Foster return -ENOMEM; 1969d87b1c08SColin Foster 1970d87b1c08SColin Foster region->offset = ocelot->stats_layout[i].offset; 1971d87b1c08SColin Foster region->count = 1; 1972d87b1c08SColin Foster list_add_tail(®ion->node, &ocelot->stats_regions); 1973d87b1c08SColin Foster } 1974d87b1c08SColin Foster 1975d87b1c08SColin Foster last = ocelot->stats_layout[i].offset; 1976d87b1c08SColin Foster } 1977d87b1c08SColin Foster 1978d87b1c08SColin Foster list_for_each_entry(region, &ocelot->stats_regions, node) { 1979d87b1c08SColin Foster region->buf = devm_kcalloc(ocelot->dev, region->count, 1980d87b1c08SColin Foster sizeof(*region->buf), GFP_KERNEL); 1981d87b1c08SColin Foster if (!region->buf) 1982d87b1c08SColin Foster return -ENOMEM; 1983d87b1c08SColin Foster } 1984d87b1c08SColin Foster 1985d87b1c08SColin Foster return 0; 1986d87b1c08SColin Foster } 1987d87b1c08SColin Foster 19885e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port, 1989c7282d38SVladimir Oltean struct ethtool_ts_info *info) 1990c7282d38SVladimir Oltean { 19914e3b0468SAntoine Tenart info->phc_index = ocelot->ptp_clock ? 19924e3b0468SAntoine Tenart ptp_clock_index(ocelot->ptp_clock) : -1; 1993d2b09a8eSYangbo Lu if (info->phc_index == -1) { 1994d2b09a8eSYangbo Lu info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 1995d2b09a8eSYangbo Lu SOF_TIMESTAMPING_RX_SOFTWARE | 1996d2b09a8eSYangbo Lu SOF_TIMESTAMPING_SOFTWARE; 1997d2b09a8eSYangbo Lu return 0; 1998d2b09a8eSYangbo Lu } 19994e3b0468SAntoine Tenart info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 20004e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_SOFTWARE | 20014e3b0468SAntoine Tenart SOF_TIMESTAMPING_SOFTWARE | 20024e3b0468SAntoine Tenart SOF_TIMESTAMPING_TX_HARDWARE | 20034e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_HARDWARE | 20044e3b0468SAntoine Tenart SOF_TIMESTAMPING_RAW_HARDWARE; 20054e3b0468SAntoine Tenart info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | 20064e3b0468SAntoine Tenart BIT(HWTSTAMP_TX_ONESTEP_SYNC); 2007c49a35eeSVladimir Oltean info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | 2008c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) | 2009c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 2010c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT); 20114e3b0468SAntoine Tenart 20124e3b0468SAntoine Tenart return 0; 20134e3b0468SAntoine Tenart } 20145e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info); 20154e3b0468SAntoine Tenart 2016a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond) 2017b80af659SVladimir Oltean { 2018b80af659SVladimir Oltean u32 mask = 0; 2019b80af659SVladimir Oltean int port; 2020b80af659SVladimir Oltean 2021961d8b69SVladimir Oltean lockdep_assert_held(&ocelot->fwd_domain_lock); 2022961d8b69SVladimir Oltean 2023b80af659SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2024b80af659SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2025b80af659SVladimir Oltean 2026b80af659SVladimir Oltean if (!ocelot_port) 2027b80af659SVladimir Oltean continue; 2028b80af659SVladimir Oltean 2029a14e6b69SVladimir Oltean if (ocelot_port->bond == bond) 2030b80af659SVladimir Oltean mask |= BIT(port); 2031b80af659SVladimir Oltean } 2032b80af659SVladimir Oltean 2033b80af659SVladimir Oltean return mask; 2034b80af659SVladimir Oltean } 2035b80af659SVladimir Oltean 2036961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical 2037961d8b69SVladimir Oltean * port ID present in that LAG. It may change if that port ever leaves the LAG. 2038961d8b69SVladimir Oltean */ 2039961d8b69SVladimir Oltean static int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond) 2040961d8b69SVladimir Oltean { 2041961d8b69SVladimir Oltean int bond_mask = ocelot_get_bond_mask(ocelot, bond); 2042961d8b69SVladimir Oltean 2043961d8b69SVladimir Oltean if (!bond_mask) 2044961d8b69SVladimir Oltean return -ENOENT; 2045961d8b69SVladimir Oltean 2046961d8b69SVladimir Oltean return __ffs(bond_mask); 2047961d8b69SVladimir Oltean } 2048961d8b69SVladimir Oltean 2049c295f983SVladimir Oltean static u32 ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot *ocelot, 2050c295f983SVladimir Oltean struct ocelot_port *cpu) 2051c295f983SVladimir Oltean { 2052c295f983SVladimir Oltean u32 mask = 0; 2053c295f983SVladimir Oltean int port; 2054c295f983SVladimir Oltean 2055c295f983SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2056c295f983SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2057c295f983SVladimir Oltean 2058c295f983SVladimir Oltean if (!ocelot_port) 2059c295f983SVladimir Oltean continue; 2060c295f983SVladimir Oltean 2061c295f983SVladimir Oltean if (ocelot_port->dsa_8021q_cpu == cpu) 2062c295f983SVladimir Oltean mask |= BIT(port); 2063c295f983SVladimir Oltean } 2064c295f983SVladimir Oltean 2065c295f983SVladimir Oltean return mask; 2066c295f983SVladimir Oltean } 2067c295f983SVladimir Oltean 2068c295f983SVladimir Oltean u32 ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot *ocelot, int port) 2069c295f983SVladimir Oltean { 2070c295f983SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2071c295f983SVladimir Oltean struct ocelot_port *cpu_port = ocelot_port->dsa_8021q_cpu; 2072c295f983SVladimir Oltean 2073c295f983SVladimir Oltean if (!cpu_port) 2074c295f983SVladimir Oltean return 0; 2075c295f983SVladimir Oltean 2076c295f983SVladimir Oltean return BIT(cpu_port->index); 2077c295f983SVladimir Oltean } 2078c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assigned_dsa_8021q_cpu_mask); 2079c295f983SVladimir Oltean 20808abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port) 2081df291e54SVladimir Oltean { 2082acc64f52SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[src_port]; 2083a8bd9fa5SVladimir Oltean const struct net_device *bridge; 2084df291e54SVladimir Oltean u32 mask = 0; 2085df291e54SVladimir Oltean int port; 2086df291e54SVladimir Oltean 2087a8bd9fa5SVladimir Oltean if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING) 2088a8bd9fa5SVladimir Oltean return 0; 2089a8bd9fa5SVladimir Oltean 2090a8bd9fa5SVladimir Oltean bridge = ocelot_port->bridge; 2091a8bd9fa5SVladimir Oltean if (!bridge) 2092acc64f52SVladimir Oltean return 0; 2093acc64f52SVladimir Oltean 2094df291e54SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2095acc64f52SVladimir Oltean ocelot_port = ocelot->ports[port]; 2096df291e54SVladimir Oltean 2097df291e54SVladimir Oltean if (!ocelot_port) 2098df291e54SVladimir Oltean continue; 2099df291e54SVladimir Oltean 2100df291e54SVladimir Oltean if (ocelot_port->stp_state == BR_STATE_FORWARDING && 2101df291e54SVladimir Oltean ocelot_port->bridge == bridge) 2102df291e54SVladimir Oltean mask |= BIT(port); 2103df291e54SVladimir Oltean } 2104df291e54SVladimir Oltean 2105df291e54SVladimir Oltean return mask; 2106df291e54SVladimir Oltean } 21078abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask); 2108df291e54SVladimir Oltean 2109a72e23ddSVladimir Oltean static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining) 2110e21268efSVladimir Oltean { 2111e21268efSVladimir Oltean int port; 2112e21268efSVladimir Oltean 21138abe1970SVladimir Oltean lockdep_assert_held(&ocelot->fwd_domain_lock); 21148abe1970SVladimir Oltean 21158abe1970SVladimir Oltean /* If cut-through forwarding is supported, update the masks before a 21168abe1970SVladimir Oltean * port joins the forwarding domain, to avoid potential underruns if it 21178abe1970SVladimir Oltean * has the highest speed from the new domain. 21188abe1970SVladimir Oltean */ 21198abe1970SVladimir Oltean if (joining && ocelot->ops->cut_through_fwd) 21208abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 21218abe1970SVladimir Oltean 21229b521250SVladimir Oltean /* Apply FWD mask. The loop is needed to add/remove the current port as 21239b521250SVladimir Oltean * a source for the other ports. 21249b521250SVladimir Oltean */ 21259b521250SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2126e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2127e21268efSVladimir Oltean unsigned long mask; 2128e21268efSVladimir Oltean 2129e21268efSVladimir Oltean if (!ocelot_port) { 2130e21268efSVladimir Oltean /* Unused ports can't send anywhere */ 2131e21268efSVladimir Oltean mask = 0; 2132e21268efSVladimir Oltean } else if (ocelot_port->is_dsa_8021q_cpu) { 2133e21268efSVladimir Oltean /* The DSA tag_8021q CPU ports need to be able to 2134c295f983SVladimir Oltean * forward packets to all ports assigned to them. 2135e21268efSVladimir Oltean */ 2136c295f983SVladimir Oltean mask = ocelot_dsa_8021q_cpu_assigned_ports(ocelot, 2137c295f983SVladimir Oltean ocelot_port); 2138df291e54SVladimir Oltean } else if (ocelot_port->bridge) { 2139528d3f19SVladimir Oltean struct net_device *bond = ocelot_port->bond; 21409b521250SVladimir Oltean 2141a8bd9fa5SVladimir Oltean mask = ocelot_get_bridge_fwd_mask(ocelot, port); 2142df291e54SVladimir Oltean mask &= ~BIT(port); 2143c295f983SVladimir Oltean 2144c295f983SVladimir Oltean mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot, 2145c295f983SVladimir Oltean port); 2146c295f983SVladimir Oltean 2147a14e6b69SVladimir Oltean if (bond) 2148a14e6b69SVladimir Oltean mask &= ~ocelot_get_bond_mask(ocelot, bond); 21499b521250SVladimir Oltean } else { 2150e21268efSVladimir Oltean /* Standalone ports forward only to DSA tag_8021q CPU 2151e21268efSVladimir Oltean * ports (if those exist), or to the hardware CPU port 2152e21268efSVladimir Oltean * module otherwise. 2153e21268efSVladimir Oltean */ 2154c295f983SVladimir Oltean mask = ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot, 2155c295f983SVladimir Oltean port); 2156e21268efSVladimir Oltean } 2157e21268efSVladimir Oltean 2158e21268efSVladimir Oltean ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); 21599b521250SVladimir Oltean } 21608abe1970SVladimir Oltean 21618abe1970SVladimir Oltean /* If cut-through forwarding is supported and a port is leaving, there 21628abe1970SVladimir Oltean * is a chance that cut-through was disabled on the other ports due to 21638abe1970SVladimir Oltean * the port which is leaving (it has a higher link speed). We need to 21648abe1970SVladimir Oltean * update the cut-through masks of the remaining ports no earlier than 21658abe1970SVladimir Oltean * after the port has left, to prevent underruns from happening between 21668abe1970SVladimir Oltean * the cut-through update and the forwarding domain update. 21678abe1970SVladimir Oltean */ 21688abe1970SVladimir Oltean if (!joining && ocelot->ops->cut_through_fwd) 21698abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 21709b521250SVladimir Oltean } 21719b521250SVladimir Oltean 217261be79baSVladimir Oltean /* Update PGID_CPU which is the destination port mask used for whitelisting 217361be79baSVladimir Oltean * unicast addresses filtered towards the host. In the normal and NPI modes, 217461be79baSVladimir Oltean * this points to the analyzer entry for the CPU port module, while in DSA 217561be79baSVladimir Oltean * tag_8021q mode, it is a bit mask of all active CPU ports. 217661be79baSVladimir Oltean * PGID_SRC will take care of forwarding a packet from one user port to 217761be79baSVladimir Oltean * no more than a single CPU port. 217861be79baSVladimir Oltean */ 217961be79baSVladimir Oltean static void ocelot_update_pgid_cpu(struct ocelot *ocelot) 218061be79baSVladimir Oltean { 218161be79baSVladimir Oltean int pgid_cpu = 0; 218261be79baSVladimir Oltean int port; 218361be79baSVladimir Oltean 218461be79baSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 218561be79baSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 218661be79baSVladimir Oltean 218761be79baSVladimir Oltean if (!ocelot_port || !ocelot_port->is_dsa_8021q_cpu) 218861be79baSVladimir Oltean continue; 218961be79baSVladimir Oltean 219061be79baSVladimir Oltean pgid_cpu |= BIT(port); 219161be79baSVladimir Oltean } 219261be79baSVladimir Oltean 219361be79baSVladimir Oltean if (!pgid_cpu) 219461be79baSVladimir Oltean pgid_cpu = BIT(ocelot->num_phys_ports); 219561be79baSVladimir Oltean 219661be79baSVladimir Oltean ocelot_write_rix(ocelot, pgid_cpu, ANA_PGID_PGID, PGID_CPU); 219761be79baSVladimir Oltean } 219861be79baSVladimir Oltean 2199c295f983SVladimir Oltean void ocelot_port_assign_dsa_8021q_cpu(struct ocelot *ocelot, int port, 2200c295f983SVladimir Oltean int cpu) 220154c31984SVladimir Oltean { 2202c295f983SVladimir Oltean struct ocelot_port *cpu_port = ocelot->ports[cpu]; 220354c31984SVladimir Oltean u16 vid; 220454c31984SVladimir Oltean 22058c166acbSVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 22068c166acbSVladimir Oltean 2207c295f983SVladimir Oltean ocelot->ports[port]->dsa_8021q_cpu = cpu_port; 2208c295f983SVladimir Oltean 2209c295f983SVladimir Oltean if (!cpu_port->is_dsa_8021q_cpu) { 2210c295f983SVladimir Oltean cpu_port->is_dsa_8021q_cpu = true; 221154c31984SVladimir Oltean 221254c31984SVladimir Oltean for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++) 2213c295f983SVladimir Oltean ocelot_vlan_member_add(ocelot, cpu, vid, true); 221461be79baSVladimir Oltean 221561be79baSVladimir Oltean ocelot_update_pgid_cpu(ocelot); 2216c295f983SVladimir Oltean } 2217a72e23ddSVladimir Oltean 2218a72e23ddSVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 22198c166acbSVladimir Oltean 22208c166acbSVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 222154c31984SVladimir Oltean } 2222c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assign_dsa_8021q_cpu); 222354c31984SVladimir Oltean 2224c295f983SVladimir Oltean void ocelot_port_unassign_dsa_8021q_cpu(struct ocelot *ocelot, int port) 222554c31984SVladimir Oltean { 2226c295f983SVladimir Oltean struct ocelot_port *cpu_port = ocelot->ports[port]->dsa_8021q_cpu; 2227c295f983SVladimir Oltean bool keep = false; 222854c31984SVladimir Oltean u16 vid; 2229c295f983SVladimir Oltean int p; 223054c31984SVladimir Oltean 22318c166acbSVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 22328c166acbSVladimir Oltean 2233c295f983SVladimir Oltean ocelot->ports[port]->dsa_8021q_cpu = NULL; 2234c295f983SVladimir Oltean 2235c295f983SVladimir Oltean for (p = 0; p < ocelot->num_phys_ports; p++) { 2236c295f983SVladimir Oltean if (!ocelot->ports[p]) 2237c295f983SVladimir Oltean continue; 2238c295f983SVladimir Oltean 2239c295f983SVladimir Oltean if (ocelot->ports[p]->dsa_8021q_cpu == cpu_port) { 2240c295f983SVladimir Oltean keep = true; 2241c295f983SVladimir Oltean break; 2242c295f983SVladimir Oltean } 2243c295f983SVladimir Oltean } 2244c295f983SVladimir Oltean 2245c295f983SVladimir Oltean if (!keep) { 2246c295f983SVladimir Oltean cpu_port->is_dsa_8021q_cpu = false; 224754c31984SVladimir Oltean 224854c31984SVladimir Oltean for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++) 2249c295f983SVladimir Oltean ocelot_vlan_member_del(ocelot, cpu_port->index, vid); 225061be79baSVladimir Oltean 225161be79baSVladimir Oltean ocelot_update_pgid_cpu(ocelot); 2252c295f983SVladimir Oltean } 2253a72e23ddSVladimir Oltean 2254a72e23ddSVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 22558c166acbSVladimir Oltean 22568c166acbSVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 225754c31984SVladimir Oltean } 2258c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_unassign_dsa_8021q_cpu); 225954c31984SVladimir Oltean 22605e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) 2261a556c76aSAlexandre Belloni { 2262421741eaSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2263df291e54SVladimir Oltean u32 learn_ena = 0; 2264a556c76aSAlexandre Belloni 22658abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 22668abe1970SVladimir Oltean 2267df291e54SVladimir Oltean ocelot_port->stp_state = state; 2268a556c76aSAlexandre Belloni 2269df291e54SVladimir Oltean if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) && 2270df291e54SVladimir Oltean ocelot_port->learn_ena) 2271df291e54SVladimir Oltean learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA; 2272a556c76aSAlexandre Belloni 2273df291e54SVladimir Oltean ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA, 2274df291e54SVladimir Oltean ANA_PORT_PORT_CFG, port); 2275a556c76aSAlexandre Belloni 22768abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING); 22778abe1970SVladimir Oltean 22788abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2279a556c76aSAlexandre Belloni } 22805e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set); 2281a556c76aSAlexandre Belloni 22825e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) 22834bda1415SVladimir Oltean { 2284c0d7eccbSVladimir Oltean unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); 2285c0d7eccbSVladimir Oltean 2286c0d7eccbSVladimir Oltean /* Setting AGE_PERIOD to zero effectively disables automatic aging, 2287c0d7eccbSVladimir Oltean * which is clearly not what our intention is. So avoid that. 2288c0d7eccbSVladimir Oltean */ 2289c0d7eccbSVladimir Oltean if (!age_period) 2290c0d7eccbSVladimir Oltean age_period = 1; 2291c0d7eccbSVladimir Oltean 2292c0d7eccbSVladimir Oltean ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); 2293a556c76aSAlexandre Belloni } 22945e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time); 2295a556c76aSAlexandre Belloni 2296a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 2297a556c76aSAlexandre Belloni const unsigned char *addr, 2298a556c76aSAlexandre Belloni u16 vid) 2299a556c76aSAlexandre Belloni { 2300a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 2301a556c76aSAlexandre Belloni 2302a556c76aSAlexandre Belloni list_for_each_entry(mc, &ocelot->multicast, list) { 2303a556c76aSAlexandre Belloni if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 2304a556c76aSAlexandre Belloni return mc; 2305a556c76aSAlexandre Belloni } 2306a556c76aSAlexandre Belloni 2307a556c76aSAlexandre Belloni return NULL; 2308a556c76aSAlexandre Belloni } 2309a556c76aSAlexandre Belloni 23109403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) 23119403c158SVladimir Oltean { 23129403c158SVladimir Oltean if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) 23139403c158SVladimir Oltean return ENTRYTYPE_MACv4; 23149403c158SVladimir Oltean if (addr[0] == 0x33 && addr[1] == 0x33) 23159403c158SVladimir Oltean return ENTRYTYPE_MACv6; 23167c313143SVladimir Oltean return ENTRYTYPE_LOCKED; 23179403c158SVladimir Oltean } 23189403c158SVladimir Oltean 2319e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, 2320e5d1f896SVladimir Oltean unsigned long ports) 2321e5d1f896SVladimir Oltean { 2322e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2323e5d1f896SVladimir Oltean 2324e5d1f896SVladimir Oltean pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); 2325e5d1f896SVladimir Oltean if (!pgid) 2326e5d1f896SVladimir Oltean return ERR_PTR(-ENOMEM); 2327e5d1f896SVladimir Oltean 2328e5d1f896SVladimir Oltean pgid->ports = ports; 2329e5d1f896SVladimir Oltean pgid->index = index; 2330e5d1f896SVladimir Oltean refcount_set(&pgid->refcount, 1); 2331e5d1f896SVladimir Oltean list_add_tail(&pgid->list, &ocelot->pgids); 2332e5d1f896SVladimir Oltean 2333e5d1f896SVladimir Oltean return pgid; 2334e5d1f896SVladimir Oltean } 2335e5d1f896SVladimir Oltean 2336e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) 2337e5d1f896SVladimir Oltean { 2338e5d1f896SVladimir Oltean if (!refcount_dec_and_test(&pgid->refcount)) 2339e5d1f896SVladimir Oltean return; 2340e5d1f896SVladimir Oltean 2341e5d1f896SVladimir Oltean list_del(&pgid->list); 2342e5d1f896SVladimir Oltean kfree(pgid); 2343e5d1f896SVladimir Oltean } 2344e5d1f896SVladimir Oltean 2345e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, 2346bb8d53fdSVladimir Oltean const struct ocelot_multicast *mc) 23479403c158SVladimir Oltean { 2348e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2349e5d1f896SVladimir Oltean int index; 23509403c158SVladimir Oltean 23519403c158SVladimir Oltean /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and 23529403c158SVladimir Oltean * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the 23539403c158SVladimir Oltean * destination mask table (PGID), the destination set is programmed as 23549403c158SVladimir Oltean * part of the entry MAC address.", and the DEST_IDX is set to 0. 23559403c158SVladimir Oltean */ 2356bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4 || 2357bb8d53fdSVladimir Oltean mc->entry_type == ENTRYTYPE_MACv6) 2358e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, 0, mc->ports); 23599403c158SVladimir Oltean 2360e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 2361e5d1f896SVladimir Oltean /* When searching for a nonreserved multicast PGID, ignore the 2362e5d1f896SVladimir Oltean * dummy PGID of zero that we have for MACv4/MACv6 entries 2363e5d1f896SVladimir Oltean */ 2364e5d1f896SVladimir Oltean if (pgid->index && pgid->ports == mc->ports) { 2365e5d1f896SVladimir Oltean refcount_inc(&pgid->refcount); 2366e5d1f896SVladimir Oltean return pgid; 2367e5d1f896SVladimir Oltean } 2368e5d1f896SVladimir Oltean } 2369e5d1f896SVladimir Oltean 2370e5d1f896SVladimir Oltean /* Search for a free index in the nonreserved multicast PGID area */ 2371e5d1f896SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, index) { 23729403c158SVladimir Oltean bool used = false; 23739403c158SVladimir Oltean 2374e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 2375e5d1f896SVladimir Oltean if (pgid->index == index) { 23769403c158SVladimir Oltean used = true; 23779403c158SVladimir Oltean break; 23789403c158SVladimir Oltean } 23799403c158SVladimir Oltean } 23809403c158SVladimir Oltean 23819403c158SVladimir Oltean if (!used) 2382e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, index, mc->ports); 23839403c158SVladimir Oltean } 23849403c158SVladimir Oltean 2385e5d1f896SVladimir Oltean return ERR_PTR(-ENOSPC); 23869403c158SVladimir Oltean } 23879403c158SVladimir Oltean 23889403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr, 2389bb8d53fdSVladimir Oltean struct ocelot_multicast *mc) 23909403c158SVladimir Oltean { 2391ebbd860eSVladimir Oltean ether_addr_copy(addr, mc->addr); 23929403c158SVladimir Oltean 2393bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4) { 23949403c158SVladimir Oltean addr[0] = 0; 23959403c158SVladimir Oltean addr[1] = mc->ports >> 8; 23969403c158SVladimir Oltean addr[2] = mc->ports & 0xff; 2397bb8d53fdSVladimir Oltean } else if (mc->entry_type == ENTRYTYPE_MACv6) { 23989403c158SVladimir Oltean addr[0] = mc->ports >> 8; 23999403c158SVladimir Oltean addr[1] = mc->ports & 0xff; 24009403c158SVladimir Oltean } 24019403c158SVladimir Oltean } 24029403c158SVladimir Oltean 2403209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port, 240454c31984SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 240554c31984SVladimir Oltean const struct net_device *bridge) 2406a556c76aSAlexandre Belloni { 2407a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 2408004d44f6SVladimir Oltean struct ocelot_multicast *mc; 2409e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2410a556c76aSAlexandre Belloni u16 vid = mdb->vid; 2411a556c76aSAlexandre Belloni 241254c31984SVladimir Oltean if (!vid) 241354c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 241454c31984SVladimir Oltean 2415a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 2416a556c76aSAlexandre Belloni if (!mc) { 2417728e69aeSVladimir Oltean /* New entry */ 2418bb8d53fdSVladimir Oltean mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 2419bb8d53fdSVladimir Oltean if (!mc) 2420bb8d53fdSVladimir Oltean return -ENOMEM; 2421bb8d53fdSVladimir Oltean 2422bb8d53fdSVladimir Oltean mc->entry_type = ocelot_classify_mdb(mdb->addr); 2423bb8d53fdSVladimir Oltean ether_addr_copy(mc->addr, mdb->addr); 2424bb8d53fdSVladimir Oltean mc->vid = vid; 2425bb8d53fdSVladimir Oltean 2426a556c76aSAlexandre Belloni list_add_tail(&mc->list, &ocelot->multicast); 2427728e69aeSVladimir Oltean } else { 2428e5d1f896SVladimir Oltean /* Existing entry. Clean up the current port mask from 2429e5d1f896SVladimir Oltean * hardware now, because we'll be modifying it. 2430e5d1f896SVladimir Oltean */ 2431e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 2432bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2433a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 2434a556c76aSAlexandre Belloni } 2435a556c76aSAlexandre Belloni 2436004d44f6SVladimir Oltean mc->ports |= BIT(port); 2437e5d1f896SVladimir Oltean 2438e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 2439e5d1f896SVladimir Oltean if (IS_ERR(pgid)) { 2440e5d1f896SVladimir Oltean dev_err(ocelot->dev, 2441e5d1f896SVladimir Oltean "Cannot allocate PGID for mdb %pM vid %d\n", 2442e5d1f896SVladimir Oltean mc->addr, mc->vid); 2443e5d1f896SVladimir Oltean devm_kfree(ocelot->dev, mc); 2444e5d1f896SVladimir Oltean return PTR_ERR(pgid); 2445e5d1f896SVladimir Oltean } 2446e5d1f896SVladimir Oltean mc->pgid = pgid; 2447e5d1f896SVladimir Oltean 2448bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2449a556c76aSAlexandre Belloni 2450e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 2451e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 2452e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 2453e5d1f896SVladimir Oltean pgid->index); 2454e5d1f896SVladimir Oltean 2455e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 2456bb8d53fdSVladimir Oltean mc->entry_type); 2457a556c76aSAlexandre Belloni } 2458209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add); 2459a556c76aSAlexandre Belloni 2460209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port, 246154c31984SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 246254c31984SVladimir Oltean const struct net_device *bridge) 2463a556c76aSAlexandre Belloni { 2464a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 2465004d44f6SVladimir Oltean struct ocelot_multicast *mc; 2466e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2467a556c76aSAlexandre Belloni u16 vid = mdb->vid; 2468a556c76aSAlexandre Belloni 246954c31984SVladimir Oltean if (!vid) 247054c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 247154c31984SVladimir Oltean 2472a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 2473a556c76aSAlexandre Belloni if (!mc) 2474a556c76aSAlexandre Belloni return -ENOENT; 2475a556c76aSAlexandre Belloni 2476bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2477a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 2478a556c76aSAlexandre Belloni 2479e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 2480004d44f6SVladimir Oltean mc->ports &= ~BIT(port); 2481a556c76aSAlexandre Belloni if (!mc->ports) { 2482a556c76aSAlexandre Belloni list_del(&mc->list); 2483a556c76aSAlexandre Belloni devm_kfree(ocelot->dev, mc); 2484a556c76aSAlexandre Belloni return 0; 2485a556c76aSAlexandre Belloni } 2486a556c76aSAlexandre Belloni 2487e5d1f896SVladimir Oltean /* We have a PGID with fewer ports now */ 2488e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 2489e5d1f896SVladimir Oltean if (IS_ERR(pgid)) 2490e5d1f896SVladimir Oltean return PTR_ERR(pgid); 2491e5d1f896SVladimir Oltean mc->pgid = pgid; 2492e5d1f896SVladimir Oltean 2493bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2494a556c76aSAlexandre Belloni 2495e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 2496e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 2497e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 2498e5d1f896SVladimir Oltean pgid->index); 2499e5d1f896SVladimir Oltean 2500e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 2501bb8d53fdSVladimir Oltean mc->entry_type); 2502a556c76aSAlexandre Belloni } 2503209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del); 2504a556c76aSAlexandre Belloni 250554c31984SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port, 250654c31984SVladimir Oltean struct net_device *bridge, int bridge_num, 250754c31984SVladimir Oltean struct netlink_ext_ack *extack) 2508a556c76aSAlexandre Belloni { 2509df291e54SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 251054c31984SVladimir Oltean int err; 251154c31984SVladimir Oltean 251254c31984SVladimir Oltean err = ocelot_single_vlan_aware_bridge(ocelot, extack); 251354c31984SVladimir Oltean if (err) 251454c31984SVladimir Oltean return err; 2515a556c76aSAlexandre Belloni 25168abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 25178abe1970SVladimir Oltean 2518df291e54SVladimir Oltean ocelot_port->bridge = bridge; 251954c31984SVladimir Oltean ocelot_port->bridge_num = bridge_num; 2520a556c76aSAlexandre Belloni 25218abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 25228abe1970SVladimir Oltean 25238abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 252454c31984SVladimir Oltean 252554c31984SVladimir Oltean if (br_vlan_enabled(bridge)) 252654c31984SVladimir Oltean return 0; 252754c31984SVladimir Oltean 252854c31984SVladimir Oltean return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge); 2529a556c76aSAlexandre Belloni } 25305e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join); 2531a556c76aSAlexandre Belloni 2532e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port, 2533a556c76aSAlexandre Belloni struct net_device *bridge) 2534a556c76aSAlexandre Belloni { 2535df291e54SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 25362e554a7aSVladimir Oltean 25378abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 25388abe1970SVladimir Oltean 253954c31984SVladimir Oltean if (!br_vlan_enabled(bridge)) 254054c31984SVladimir Oltean ocelot_del_vlan_unaware_pvid(ocelot, port, bridge); 254154c31984SVladimir Oltean 2542df291e54SVladimir Oltean ocelot_port->bridge = NULL; 254354c31984SVladimir Oltean ocelot_port->bridge_num = -1; 25447142529fSAntoine Tenart 2545d4004422SVladimir Oltean ocelot_port_set_pvid(ocelot, port, NULL); 25460da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 25478abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, false); 25488abe1970SVladimir Oltean 25498abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2550a556c76aSAlexandre Belloni } 25515e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave); 2552a556c76aSAlexandre Belloni 2553dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 2554dc96ee37SAlexandre Belloni { 2555528d3f19SVladimir Oltean unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0); 2556dc96ee37SAlexandre Belloni int i, port, lag; 2557dc96ee37SAlexandre Belloni 2558dc96ee37SAlexandre Belloni /* Reset destination and aggregation PGIDS */ 255996b029b0SVladimir Oltean for_each_unicast_dest_pgid(ocelot, port) 2560dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 2561dc96ee37SAlexandre Belloni 256296b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) 2563dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 2564dc96ee37SAlexandre Belloni ANA_PGID_PGID, i); 2565dc96ee37SAlexandre Belloni 2566528d3f19SVladimir Oltean /* The visited ports bitmask holds the list of ports offloading any 2567528d3f19SVladimir Oltean * bonding interface. Initially we mark all these ports as unvisited, 2568528d3f19SVladimir Oltean * then every time we visit a port in this bitmask, we know that it is 2569528d3f19SVladimir Oltean * the lowest numbered port, i.e. the one whose logical ID == physical 2570528d3f19SVladimir Oltean * port ID == LAG ID. So we mark as visited all further ports in the 2571528d3f19SVladimir Oltean * bitmask that are offloading the same bonding interface. This way, 2572528d3f19SVladimir Oltean * we set up the aggregation PGIDs only once per bonding interface. 2573528d3f19SVladimir Oltean */ 2574528d3f19SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2575528d3f19SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2576528d3f19SVladimir Oltean 2577528d3f19SVladimir Oltean if (!ocelot_port || !ocelot_port->bond) 2578528d3f19SVladimir Oltean continue; 2579528d3f19SVladimir Oltean 2580528d3f19SVladimir Oltean visited &= ~BIT(port); 2581528d3f19SVladimir Oltean } 2582528d3f19SVladimir Oltean 2583528d3f19SVladimir Oltean /* Now, set PGIDs for each active LAG */ 2584dc96ee37SAlexandre Belloni for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 2585528d3f19SVladimir Oltean struct net_device *bond = ocelot->ports[lag]->bond; 258623ca3b72SVladimir Oltean int num_active_ports = 0; 2587dc96ee37SAlexandre Belloni unsigned long bond_mask; 2588dc96ee37SAlexandre Belloni u8 aggr_idx[16]; 2589dc96ee37SAlexandre Belloni 2590528d3f19SVladimir Oltean if (!bond || (visited & BIT(lag))) 2591dc96ee37SAlexandre Belloni continue; 2592dc96ee37SAlexandre Belloni 2593a14e6b69SVladimir Oltean bond_mask = ocelot_get_bond_mask(ocelot, bond); 2594528d3f19SVladimir Oltean 2595dc96ee37SAlexandre Belloni for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 2596a14e6b69SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2597a14e6b69SVladimir Oltean 2598dc96ee37SAlexandre Belloni // Destination mask 2599dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, bond_mask, 2600dc96ee37SAlexandre Belloni ANA_PGID_PGID, port); 2601a14e6b69SVladimir Oltean 2602a14e6b69SVladimir Oltean if (ocelot_port->lag_tx_active) 260323ca3b72SVladimir Oltean aggr_idx[num_active_ports++] = port; 2604dc96ee37SAlexandre Belloni } 2605dc96ee37SAlexandre Belloni 260696b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) { 2607dc96ee37SAlexandre Belloni u32 ac; 2608dc96ee37SAlexandre Belloni 2609dc96ee37SAlexandre Belloni ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 2610dc96ee37SAlexandre Belloni ac &= ~bond_mask; 261123ca3b72SVladimir Oltean /* Don't do division by zero if there was no active 261223ca3b72SVladimir Oltean * port. Just make all aggregation codes zero. 261323ca3b72SVladimir Oltean */ 261423ca3b72SVladimir Oltean if (num_active_ports) 261523ca3b72SVladimir Oltean ac |= BIT(aggr_idx[i % num_active_ports]); 2616dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 2617dc96ee37SAlexandre Belloni } 2618528d3f19SVladimir Oltean 2619528d3f19SVladimir Oltean /* Mark all ports in the same LAG as visited to avoid applying 2620528d3f19SVladimir Oltean * the same config again. 2621528d3f19SVladimir Oltean */ 2622528d3f19SVladimir Oltean for (port = lag; port < ocelot->num_phys_ports; port++) { 2623528d3f19SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2624528d3f19SVladimir Oltean 2625528d3f19SVladimir Oltean if (!ocelot_port) 2626528d3f19SVladimir Oltean continue; 2627528d3f19SVladimir Oltean 2628528d3f19SVladimir Oltean if (ocelot_port->bond == bond) 2629528d3f19SVladimir Oltean visited |= BIT(port); 2630528d3f19SVladimir Oltean } 2631dc96ee37SAlexandre Belloni } 2632dc96ee37SAlexandre Belloni } 2633dc96ee37SAlexandre Belloni 26342527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the 26352527f2e8SVladimir Oltean * same bond must have the same logical port ID, equal to the physical port ID 26362527f2e8SVladimir Oltean * of the lowest numbered physical port in that bond. Otherwise, in standalone/ 26372527f2e8SVladimir Oltean * bridged mode, each port has a logical port ID equal to its physical port ID. 26382527f2e8SVladimir Oltean */ 26392527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) 2640dc96ee37SAlexandre Belloni { 26412527f2e8SVladimir Oltean int port; 2642dc96ee37SAlexandre Belloni 26432527f2e8SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 26442527f2e8SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 26452527f2e8SVladimir Oltean struct net_device *bond; 2646dc96ee37SAlexandre Belloni 26472527f2e8SVladimir Oltean if (!ocelot_port) 26482527f2e8SVladimir Oltean continue; 2649dc96ee37SAlexandre Belloni 26502527f2e8SVladimir Oltean bond = ocelot_port->bond; 26512527f2e8SVladimir Oltean if (bond) { 2652961d8b69SVladimir Oltean int lag = ocelot_bond_get_id(ocelot, bond); 26532527f2e8SVladimir Oltean 26542527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 2655dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(lag), 26562527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 26572527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 26582527f2e8SVladimir Oltean } else { 26592527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 26602527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 26612527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 26622527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 26632527f2e8SVladimir Oltean } 2664dc96ee37SAlexandre Belloni } 2665dc96ee37SAlexandre Belloni } 2666dc96ee37SAlexandre Belloni 266728de0f9fSVladimir Oltean static int ocelot_migrate_mc(struct ocelot *ocelot, struct ocelot_multicast *mc, 266828de0f9fSVladimir Oltean unsigned long from_mask, unsigned long to_mask) 266928de0f9fSVladimir Oltean { 267028de0f9fSVladimir Oltean unsigned char addr[ETH_ALEN]; 267128de0f9fSVladimir Oltean struct ocelot_pgid *pgid; 267228de0f9fSVladimir Oltean u16 vid = mc->vid; 267328de0f9fSVladimir Oltean 267428de0f9fSVladimir Oltean dev_dbg(ocelot->dev, 267528de0f9fSVladimir Oltean "Migrating multicast %pM vid %d from port mask 0x%lx to 0x%lx\n", 267628de0f9fSVladimir Oltean mc->addr, mc->vid, from_mask, to_mask); 267728de0f9fSVladimir Oltean 267828de0f9fSVladimir Oltean /* First clean up the current port mask from hardware, because 267928de0f9fSVladimir Oltean * we'll be modifying it. 268028de0f9fSVladimir Oltean */ 268128de0f9fSVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 268228de0f9fSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 268328de0f9fSVladimir Oltean ocelot_mact_forget(ocelot, addr, vid); 268428de0f9fSVladimir Oltean 268528de0f9fSVladimir Oltean mc->ports &= ~from_mask; 268628de0f9fSVladimir Oltean mc->ports |= to_mask; 268728de0f9fSVladimir Oltean 268828de0f9fSVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 268928de0f9fSVladimir Oltean if (IS_ERR(pgid)) { 269028de0f9fSVladimir Oltean dev_err(ocelot->dev, 269128de0f9fSVladimir Oltean "Cannot allocate PGID for mdb %pM vid %d\n", 269228de0f9fSVladimir Oltean mc->addr, mc->vid); 269328de0f9fSVladimir Oltean devm_kfree(ocelot->dev, mc); 269428de0f9fSVladimir Oltean return PTR_ERR(pgid); 269528de0f9fSVladimir Oltean } 269628de0f9fSVladimir Oltean mc->pgid = pgid; 269728de0f9fSVladimir Oltean 269828de0f9fSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 269928de0f9fSVladimir Oltean 270028de0f9fSVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 270128de0f9fSVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 270228de0f9fSVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 270328de0f9fSVladimir Oltean pgid->index); 270428de0f9fSVladimir Oltean 270528de0f9fSVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 270628de0f9fSVladimir Oltean mc->entry_type); 270728de0f9fSVladimir Oltean } 270828de0f9fSVladimir Oltean 270928de0f9fSVladimir Oltean int ocelot_migrate_mdbs(struct ocelot *ocelot, unsigned long from_mask, 271028de0f9fSVladimir Oltean unsigned long to_mask) 271128de0f9fSVladimir Oltean { 271228de0f9fSVladimir Oltean struct ocelot_multicast *mc; 271328de0f9fSVladimir Oltean int err; 271428de0f9fSVladimir Oltean 271528de0f9fSVladimir Oltean list_for_each_entry(mc, &ocelot->multicast, list) { 271628de0f9fSVladimir Oltean if (!(mc->ports & from_mask)) 271728de0f9fSVladimir Oltean continue; 271828de0f9fSVladimir Oltean 271928de0f9fSVladimir Oltean err = ocelot_migrate_mc(ocelot, mc, from_mask, to_mask); 272028de0f9fSVladimir Oltean if (err) 272128de0f9fSVladimir Oltean return err; 272228de0f9fSVladimir Oltean } 272328de0f9fSVladimir Oltean 272428de0f9fSVladimir Oltean return 0; 272528de0f9fSVladimir Oltean } 272628de0f9fSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_migrate_mdbs); 272728de0f9fSVladimir Oltean 2728961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says: 2729961d8b69SVladimir Oltean * Logical port number for front port. If port is not a member of a LLAG, 2730961d8b69SVladimir Oltean * then PORTID must be set to the physical port number. 2731961d8b69SVladimir Oltean * If port is a member of a LLAG, then PORTID must be set to the common 2732961d8b69SVladimir Oltean * PORTID_VAL used for all member ports of the LLAG. 2733961d8b69SVladimir Oltean * The value must not exceed the number of physical ports on the device. 2734961d8b69SVladimir Oltean * 2735961d8b69SVladimir Oltean * This means we have little choice but to migrate FDB entries pointing towards 2736961d8b69SVladimir Oltean * a logical port when that changes. 2737961d8b69SVladimir Oltean */ 2738961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot, 2739961d8b69SVladimir Oltean struct net_device *bond, 2740961d8b69SVladimir Oltean int lag) 2741961d8b69SVladimir Oltean { 2742961d8b69SVladimir Oltean struct ocelot_lag_fdb *fdb; 2743961d8b69SVladimir Oltean int err; 2744961d8b69SVladimir Oltean 2745961d8b69SVladimir Oltean lockdep_assert_held(&ocelot->fwd_domain_lock); 2746961d8b69SVladimir Oltean 2747961d8b69SVladimir Oltean list_for_each_entry(fdb, &ocelot->lag_fdbs, list) { 2748961d8b69SVladimir Oltean if (fdb->bond != bond) 2749961d8b69SVladimir Oltean continue; 2750961d8b69SVladimir Oltean 2751961d8b69SVladimir Oltean err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid); 2752961d8b69SVladimir Oltean if (err) { 2753961d8b69SVladimir Oltean dev_err(ocelot->dev, 2754961d8b69SVladimir Oltean "failed to delete LAG %s FDB %pM vid %d: %pe\n", 2755961d8b69SVladimir Oltean bond->name, fdb->addr, fdb->vid, ERR_PTR(err)); 2756961d8b69SVladimir Oltean } 2757961d8b69SVladimir Oltean 2758961d8b69SVladimir Oltean err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid, 2759961d8b69SVladimir Oltean ENTRYTYPE_LOCKED); 2760961d8b69SVladimir Oltean if (err) { 2761961d8b69SVladimir Oltean dev_err(ocelot->dev, 2762961d8b69SVladimir Oltean "failed to migrate LAG %s FDB %pM vid %d: %pe\n", 2763961d8b69SVladimir Oltean bond->name, fdb->addr, fdb->vid, ERR_PTR(err)); 2764961d8b69SVladimir Oltean } 2765961d8b69SVladimir Oltean } 2766961d8b69SVladimir Oltean } 2767961d8b69SVladimir Oltean 27689c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port, 2769583cbbe3SVladimir Oltean struct net_device *bond, 2770583cbbe3SVladimir Oltean struct netdev_lag_upper_info *info) 2771dc96ee37SAlexandre Belloni { 2772583cbbe3SVladimir Oltean if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) 2773583cbbe3SVladimir Oltean return -EOPNOTSUPP; 2774583cbbe3SVladimir Oltean 27758abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 27768abe1970SVladimir Oltean 2777b80af659SVladimir Oltean ocelot->ports[port]->bond = bond; 2778dc96ee37SAlexandre Belloni 27792527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 27808abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2781dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 2782dc96ee37SAlexandre Belloni 27838abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 27848abe1970SVladimir Oltean 2785dc96ee37SAlexandre Belloni return 0; 2786dc96ee37SAlexandre Belloni } 27879c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join); 2788dc96ee37SAlexandre Belloni 27899c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port, 2790dc96ee37SAlexandre Belloni struct net_device *bond) 2791dc96ee37SAlexandre Belloni { 2792961d8b69SVladimir Oltean int old_lag_id, new_lag_id; 2793961d8b69SVladimir Oltean 27948abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 27958abe1970SVladimir Oltean 2796961d8b69SVladimir Oltean old_lag_id = ocelot_bond_get_id(ocelot, bond); 2797961d8b69SVladimir Oltean 2798b80af659SVladimir Oltean ocelot->ports[port]->bond = NULL; 2799b80af659SVladimir Oltean 28002527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 28018abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, false); 2802dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 28038abe1970SVladimir Oltean 2804961d8b69SVladimir Oltean new_lag_id = ocelot_bond_get_id(ocelot, bond); 2805961d8b69SVladimir Oltean 2806961d8b69SVladimir Oltean if (new_lag_id >= 0 && old_lag_id != new_lag_id) 2807961d8b69SVladimir Oltean ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id); 2808961d8b69SVladimir Oltean 28098abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2810dc96ee37SAlexandre Belloni } 28119c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave); 28120e332c85SPetr Machata 281323ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active) 281423ca3b72SVladimir Oltean { 281523ca3b72SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 281623ca3b72SVladimir Oltean 2817961d8b69SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2818961d8b69SVladimir Oltean 281923ca3b72SVladimir Oltean ocelot_port->lag_tx_active = lag_tx_active; 282023ca3b72SVladimir Oltean 282123ca3b72SVladimir Oltean /* Rebalance the LAGs */ 282223ca3b72SVladimir Oltean ocelot_set_aggr_pgids(ocelot); 2823961d8b69SVladimir Oltean 2824961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 282523ca3b72SVladimir Oltean } 282623ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change); 282723ca3b72SVladimir Oltean 2828961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond, 282954c31984SVladimir Oltean const unsigned char *addr, u16 vid, 283054c31984SVladimir Oltean const struct net_device *bridge) 2831961d8b69SVladimir Oltean { 2832961d8b69SVladimir Oltean struct ocelot_lag_fdb *fdb; 2833961d8b69SVladimir Oltean int lag, err; 2834961d8b69SVladimir Oltean 2835961d8b69SVladimir Oltean fdb = kzalloc(sizeof(*fdb), GFP_KERNEL); 2836961d8b69SVladimir Oltean if (!fdb) 2837961d8b69SVladimir Oltean return -ENOMEM; 2838961d8b69SVladimir Oltean 283954c31984SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 284054c31984SVladimir Oltean 284154c31984SVladimir Oltean if (!vid) 284254c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 284354c31984SVladimir Oltean 2844961d8b69SVladimir Oltean ether_addr_copy(fdb->addr, addr); 2845961d8b69SVladimir Oltean fdb->vid = vid; 2846961d8b69SVladimir Oltean fdb->bond = bond; 2847961d8b69SVladimir Oltean 2848961d8b69SVladimir Oltean lag = ocelot_bond_get_id(ocelot, bond); 2849961d8b69SVladimir Oltean 2850961d8b69SVladimir Oltean err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED); 2851961d8b69SVladimir Oltean if (err) { 2852961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2853961d8b69SVladimir Oltean kfree(fdb); 2854961d8b69SVladimir Oltean return err; 2855961d8b69SVladimir Oltean } 2856961d8b69SVladimir Oltean 2857961d8b69SVladimir Oltean list_add_tail(&fdb->list, &ocelot->lag_fdbs); 2858961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2859961d8b69SVladimir Oltean 2860961d8b69SVladimir Oltean return 0; 2861961d8b69SVladimir Oltean } 2862961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add); 2863961d8b69SVladimir Oltean 2864961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond, 286554c31984SVladimir Oltean const unsigned char *addr, u16 vid, 286654c31984SVladimir Oltean const struct net_device *bridge) 2867961d8b69SVladimir Oltean { 2868961d8b69SVladimir Oltean struct ocelot_lag_fdb *fdb, *tmp; 2869961d8b69SVladimir Oltean 2870961d8b69SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 2871961d8b69SVladimir Oltean 287254c31984SVladimir Oltean if (!vid) 287354c31984SVladimir Oltean vid = ocelot_vlan_unaware_pvid(ocelot, bridge); 287454c31984SVladimir Oltean 2875961d8b69SVladimir Oltean list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) { 2876961d8b69SVladimir Oltean if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid || 2877961d8b69SVladimir Oltean fdb->bond != bond) 2878961d8b69SVladimir Oltean continue; 2879961d8b69SVladimir Oltean 2880961d8b69SVladimir Oltean ocelot_mact_forget(ocelot, addr, vid); 2881961d8b69SVladimir Oltean list_del(&fdb->list); 2882961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2883961d8b69SVladimir Oltean kfree(fdb); 2884961d8b69SVladimir Oltean 2885961d8b69SVladimir Oltean return 0; 2886961d8b69SVladimir Oltean } 2887961d8b69SVladimir Oltean 2888961d8b69SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2889961d8b69SVladimir Oltean 2890961d8b69SVladimir Oltean return -ENOENT; 2891961d8b69SVladimir Oltean } 2892961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del); 2893961d8b69SVladimir Oltean 2894a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. 2895a8015dedSVladimir Oltean * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. 28960b912fc9SVladimir Oltean * In the special case that it's the NPI port that we're configuring, the 28970b912fc9SVladimir Oltean * length of the tag and optional prefix needs to be accounted for privately, 28980b912fc9SVladimir Oltean * in order to be able to sustain communication at the requested @sdu. 2899a8015dedSVladimir Oltean */ 29000b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) 290131350d7fSVladimir Oltean { 290231350d7fSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2903a8015dedSVladimir Oltean int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; 2904e8e6e73dSVladimir Oltean int pause_start, pause_stop; 2905601e984fSVladimir Oltean int atop, atop_tot; 290631350d7fSVladimir Oltean 29070b912fc9SVladimir Oltean if (port == ocelot->npi) { 29080b912fc9SVladimir Oltean maxlen += OCELOT_TAG_LEN; 29090b912fc9SVladimir Oltean 2910cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 29110b912fc9SVladimir Oltean maxlen += OCELOT_SHORT_PREFIX_LEN; 2912cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 29130b912fc9SVladimir Oltean maxlen += OCELOT_LONG_PREFIX_LEN; 29140b912fc9SVladimir Oltean } 29150b912fc9SVladimir Oltean 2916a8015dedSVladimir Oltean ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); 2917fa914e9cSVladimir Oltean 2918e8e6e73dSVladimir Oltean /* Set Pause watermark hysteresis */ 2919e8e6e73dSVladimir Oltean pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; 2920e8e6e73dSVladimir Oltean pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; 2921541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, 2922541132f0SMaxim Kochetkov pause_start); 2923541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, 2924541132f0SMaxim Kochetkov pause_stop); 2925fa914e9cSVladimir Oltean 2926601e984fSVladimir Oltean /* Tail dropping watermarks */ 2927f6fe01d6SVladimir Oltean atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / 2928a8015dedSVladimir Oltean OCELOT_BUFFER_CELL_SZ; 2929601e984fSVladimir Oltean atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; 2930601e984fSVladimir Oltean ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); 2931601e984fSVladimir Oltean ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); 2932fa914e9cSVladimir Oltean } 29330b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen); 29340b912fc9SVladimir Oltean 29350b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port) 29360b912fc9SVladimir Oltean { 29370b912fc9SVladimir Oltean int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; 29380b912fc9SVladimir Oltean 29390b912fc9SVladimir Oltean if (port == ocelot->npi) { 29400b912fc9SVladimir Oltean max_mtu -= OCELOT_TAG_LEN; 29410b912fc9SVladimir Oltean 2942cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 29430b912fc9SVladimir Oltean max_mtu -= OCELOT_SHORT_PREFIX_LEN; 2944cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 29450b912fc9SVladimir Oltean max_mtu -= OCELOT_LONG_PREFIX_LEN; 29460b912fc9SVladimir Oltean } 29470b912fc9SVladimir Oltean 29480b912fc9SVladimir Oltean return max_mtu; 29490b912fc9SVladimir Oltean } 29500b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu); 2951fa914e9cSVladimir Oltean 2952421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port, 2953421741eaSVladimir Oltean bool enabled) 2954421741eaSVladimir Oltean { 2955421741eaSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2956421741eaSVladimir Oltean u32 val = 0; 2957421741eaSVladimir Oltean 2958421741eaSVladimir Oltean if (enabled) 2959421741eaSVladimir Oltean val = ANA_PORT_PORT_CFG_LEARN_ENA; 2960421741eaSVladimir Oltean 2961421741eaSVladimir Oltean ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA, 2962421741eaSVladimir Oltean ANA_PORT_PORT_CFG, port); 2963421741eaSVladimir Oltean 2964421741eaSVladimir Oltean ocelot_port->learn_ena = enabled; 2965421741eaSVladimir Oltean } 2966421741eaSVladimir Oltean 2967421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port, 2968421741eaSVladimir Oltean bool enabled) 2969421741eaSVladimir Oltean { 2970421741eaSVladimir Oltean u32 val = 0; 2971421741eaSVladimir Oltean 2972421741eaSVladimir Oltean if (enabled) 2973421741eaSVladimir Oltean val = BIT(port); 2974421741eaSVladimir Oltean 2975421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC); 2976421741eaSVladimir Oltean } 2977421741eaSVladimir Oltean 2978421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port, 2979421741eaSVladimir Oltean bool enabled) 2980421741eaSVladimir Oltean { 2981421741eaSVladimir Oltean u32 val = 0; 2982421741eaSVladimir Oltean 2983421741eaSVladimir Oltean if (enabled) 2984421741eaSVladimir Oltean val = BIT(port); 2985421741eaSVladimir Oltean 2986421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC); 29874cf35a2bSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4); 29884cf35a2bSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6); 2989421741eaSVladimir Oltean } 2990421741eaSVladimir Oltean 2991421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port, 2992421741eaSVladimir Oltean bool enabled) 2993421741eaSVladimir Oltean { 2994421741eaSVladimir Oltean u32 val = 0; 2995421741eaSVladimir Oltean 2996421741eaSVladimir Oltean if (enabled) 2997421741eaSVladimir Oltean val = BIT(port); 2998421741eaSVladimir Oltean 2999421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC); 3000421741eaSVladimir Oltean } 3001421741eaSVladimir Oltean 3002421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port, 3003421741eaSVladimir Oltean struct switchdev_brport_flags flags) 3004421741eaSVladimir Oltean { 3005421741eaSVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 3006421741eaSVladimir Oltean BR_BCAST_FLOOD)) 3007421741eaSVladimir Oltean return -EINVAL; 3008421741eaSVladimir Oltean 3009421741eaSVladimir Oltean return 0; 3010421741eaSVladimir Oltean } 3011421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags); 3012421741eaSVladimir Oltean 3013421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port, 3014421741eaSVladimir Oltean struct switchdev_brport_flags flags) 3015421741eaSVladimir Oltean { 3016421741eaSVladimir Oltean if (flags.mask & BR_LEARNING) 3017421741eaSVladimir Oltean ocelot_port_set_learning(ocelot, port, 3018421741eaSVladimir Oltean !!(flags.val & BR_LEARNING)); 3019421741eaSVladimir Oltean 3020421741eaSVladimir Oltean if (flags.mask & BR_FLOOD) 3021421741eaSVladimir Oltean ocelot_port_set_ucast_flood(ocelot, port, 3022421741eaSVladimir Oltean !!(flags.val & BR_FLOOD)); 3023421741eaSVladimir Oltean 3024421741eaSVladimir Oltean if (flags.mask & BR_MCAST_FLOOD) 3025421741eaSVladimir Oltean ocelot_port_set_mcast_flood(ocelot, port, 3026421741eaSVladimir Oltean !!(flags.val & BR_MCAST_FLOOD)); 3027421741eaSVladimir Oltean 3028421741eaSVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) 3029421741eaSVladimir Oltean ocelot_port_set_bcast_flood(ocelot, port, 3030421741eaSVladimir Oltean !!(flags.val & BR_BCAST_FLOOD)); 3031421741eaSVladimir Oltean } 3032421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags); 3033421741eaSVladimir Oltean 3034978777d0SVladimir Oltean int ocelot_port_get_default_prio(struct ocelot *ocelot, int port) 3035978777d0SVladimir Oltean { 3036978777d0SVladimir Oltean int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port); 3037978777d0SVladimir Oltean 3038978777d0SVladimir Oltean return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val); 3039978777d0SVladimir Oltean } 3040978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio); 3041978777d0SVladimir Oltean 3042978777d0SVladimir Oltean int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio) 3043978777d0SVladimir Oltean { 304472f56fdbSVladimir Oltean if (prio >= OCELOT_NUM_TC) 3045978777d0SVladimir Oltean return -ERANGE; 3046978777d0SVladimir Oltean 3047978777d0SVladimir Oltean ocelot_rmw_gix(ocelot, 3048978777d0SVladimir Oltean ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio), 3049978777d0SVladimir Oltean ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M, 3050978777d0SVladimir Oltean ANA_PORT_QOS_CFG, 3051978777d0SVladimir Oltean port); 3052978777d0SVladimir Oltean 3053978777d0SVladimir Oltean return 0; 3054978777d0SVladimir Oltean } 3055978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio); 3056978777d0SVladimir Oltean 3057978777d0SVladimir Oltean int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp) 3058978777d0SVladimir Oltean { 3059978777d0SVladimir Oltean int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port); 3060978777d0SVladimir Oltean int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp); 3061978777d0SVladimir Oltean 3062978777d0SVladimir Oltean /* Return error if DSCP prioritization isn't enabled */ 3063978777d0SVladimir Oltean if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA)) 3064978777d0SVladimir Oltean return -EOPNOTSUPP; 3065978777d0SVladimir Oltean 3066978777d0SVladimir Oltean if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) { 3067978777d0SVladimir Oltean dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg); 3068978777d0SVladimir Oltean /* Re-read ANA_DSCP_CFG for the translated DSCP */ 3069978777d0SVladimir Oltean dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp); 3070978777d0SVladimir Oltean } 3071978777d0SVladimir Oltean 3072978777d0SVladimir Oltean /* If the DSCP value is not trusted, the QoS classification falls back 3073978777d0SVladimir Oltean * to VLAN PCP or port-based default. 3074978777d0SVladimir Oltean */ 3075978777d0SVladimir Oltean if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)) 3076978777d0SVladimir Oltean return -EOPNOTSUPP; 3077978777d0SVladimir Oltean 3078978777d0SVladimir Oltean return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg); 3079978777d0SVladimir Oltean } 3080978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio); 3081978777d0SVladimir Oltean 3082978777d0SVladimir Oltean int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio) 3083978777d0SVladimir Oltean { 3084978777d0SVladimir Oltean int mask, val; 3085978777d0SVladimir Oltean 308672f56fdbSVladimir Oltean if (prio >= OCELOT_NUM_TC) 3087978777d0SVladimir Oltean return -ERANGE; 3088978777d0SVladimir Oltean 3089978777d0SVladimir Oltean /* There is at least one app table priority (this one), so we need to 3090978777d0SVladimir Oltean * make sure DSCP prioritization is enabled on the port. 3091978777d0SVladimir Oltean * Also make sure DSCP translation is disabled 3092978777d0SVladimir Oltean * (dcbnl doesn't support it). 3093978777d0SVladimir Oltean */ 3094978777d0SVladimir Oltean mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA | 3095978777d0SVladimir Oltean ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA; 3096978777d0SVladimir Oltean 3097978777d0SVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask, 3098978777d0SVladimir Oltean ANA_PORT_QOS_CFG, port); 3099978777d0SVladimir Oltean 3100978777d0SVladimir Oltean /* Trust this DSCP value and map it to the given QoS class */ 3101978777d0SVladimir Oltean val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio); 3102978777d0SVladimir Oltean 3103978777d0SVladimir Oltean ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp); 3104978777d0SVladimir Oltean 3105978777d0SVladimir Oltean return 0; 3106978777d0SVladimir Oltean } 3107978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio); 3108978777d0SVladimir Oltean 3109978777d0SVladimir Oltean int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio) 3110978777d0SVladimir Oltean { 3111978777d0SVladimir Oltean int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp); 3112978777d0SVladimir Oltean int mask, i; 3113978777d0SVladimir Oltean 3114978777d0SVladimir Oltean /* During a "dcb app replace" command, the new app table entry will be 3115978777d0SVladimir Oltean * added first, then the old one will be deleted. But the hardware only 3116978777d0SVladimir Oltean * supports one QoS class per DSCP value (duh), so if we blindly delete 3117978777d0SVladimir Oltean * the app table entry for this DSCP value, we end up deleting the 3118978777d0SVladimir Oltean * entry with the new priority. Avoid that by checking whether user 3119978777d0SVladimir Oltean * space wants to delete the priority which is currently configured, or 3120978777d0SVladimir Oltean * something else which is no longer current. 3121978777d0SVladimir Oltean */ 3122978777d0SVladimir Oltean if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio) 3123978777d0SVladimir Oltean return 0; 3124978777d0SVladimir Oltean 3125978777d0SVladimir Oltean /* Untrust this DSCP value */ 3126978777d0SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp); 3127978777d0SVladimir Oltean 3128978777d0SVladimir Oltean for (i = 0; i < 64; i++) { 3129978777d0SVladimir Oltean int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i); 3130978777d0SVladimir Oltean 3131978777d0SVladimir Oltean /* There are still app table entries on the port, so we need to 3132978777d0SVladimir Oltean * keep DSCP enabled, nothing to do. 3133978777d0SVladimir Oltean */ 3134978777d0SVladimir Oltean if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA) 3135978777d0SVladimir Oltean return 0; 3136978777d0SVladimir Oltean } 3137978777d0SVladimir Oltean 3138978777d0SVladimir Oltean /* Disable DSCP QoS classification if there isn't any trusted 3139978777d0SVladimir Oltean * DSCP value left. 3140978777d0SVladimir Oltean */ 3141978777d0SVladimir Oltean mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA | 3142978777d0SVladimir Oltean ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA; 3143978777d0SVladimir Oltean 3144978777d0SVladimir Oltean ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port); 3145978777d0SVladimir Oltean 3146978777d0SVladimir Oltean return 0; 3147978777d0SVladimir Oltean } 3148978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio); 3149978777d0SVladimir Oltean 3150f2a0e216SVladimir Oltean struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to, 3151ccb6ed42SVladimir Oltean struct netlink_ext_ack *extack) 3152ccb6ed42SVladimir Oltean { 3153ccb6ed42SVladimir Oltean struct ocelot_mirror *m = ocelot->mirror; 3154ccb6ed42SVladimir Oltean 3155ccb6ed42SVladimir Oltean if (m) { 3156ccb6ed42SVladimir Oltean if (m->to != to) { 3157ccb6ed42SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 3158ccb6ed42SVladimir Oltean "Mirroring already configured towards different egress port"); 3159ccb6ed42SVladimir Oltean return ERR_PTR(-EBUSY); 3160ccb6ed42SVladimir Oltean } 3161ccb6ed42SVladimir Oltean 3162ccb6ed42SVladimir Oltean refcount_inc(&m->refcount); 3163ccb6ed42SVladimir Oltean return m; 3164ccb6ed42SVladimir Oltean } 3165ccb6ed42SVladimir Oltean 3166ccb6ed42SVladimir Oltean m = kzalloc(sizeof(*m), GFP_KERNEL); 3167ccb6ed42SVladimir Oltean if (!m) 3168ccb6ed42SVladimir Oltean return ERR_PTR(-ENOMEM); 3169ccb6ed42SVladimir Oltean 3170ccb6ed42SVladimir Oltean m->to = to; 3171ccb6ed42SVladimir Oltean refcount_set(&m->refcount, 1); 3172ccb6ed42SVladimir Oltean ocelot->mirror = m; 3173ccb6ed42SVladimir Oltean 3174ccb6ed42SVladimir Oltean /* Program the mirror port to hardware */ 3175ccb6ed42SVladimir Oltean ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS); 3176ccb6ed42SVladimir Oltean 3177ccb6ed42SVladimir Oltean return m; 3178ccb6ed42SVladimir Oltean } 3179ccb6ed42SVladimir Oltean 3180f2a0e216SVladimir Oltean void ocelot_mirror_put(struct ocelot *ocelot) 3181ccb6ed42SVladimir Oltean { 3182ccb6ed42SVladimir Oltean struct ocelot_mirror *m = ocelot->mirror; 3183ccb6ed42SVladimir Oltean 3184ccb6ed42SVladimir Oltean if (!refcount_dec_and_test(&m->refcount)) 3185ccb6ed42SVladimir Oltean return; 3186ccb6ed42SVladimir Oltean 3187ccb6ed42SVladimir Oltean ocelot_write(ocelot, 0, ANA_MIRRORPORTS); 3188ccb6ed42SVladimir Oltean ocelot->mirror = NULL; 3189ccb6ed42SVladimir Oltean kfree(m); 3190ccb6ed42SVladimir Oltean } 3191ccb6ed42SVladimir Oltean 3192ccb6ed42SVladimir Oltean int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to, 3193ccb6ed42SVladimir Oltean bool ingress, struct netlink_ext_ack *extack) 3194ccb6ed42SVladimir Oltean { 3195ccb6ed42SVladimir Oltean struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack); 3196ccb6ed42SVladimir Oltean 3197ccb6ed42SVladimir Oltean if (IS_ERR(m)) 3198ccb6ed42SVladimir Oltean return PTR_ERR(m); 3199ccb6ed42SVladimir Oltean 3200ccb6ed42SVladimir Oltean if (ingress) { 3201ccb6ed42SVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA, 3202ccb6ed42SVladimir Oltean ANA_PORT_PORT_CFG_SRC_MIRROR_ENA, 3203ccb6ed42SVladimir Oltean ANA_PORT_PORT_CFG, from); 3204ccb6ed42SVladimir Oltean } else { 3205ccb6ed42SVladimir Oltean ocelot_rmw(ocelot, BIT(from), BIT(from), 3206ccb6ed42SVladimir Oltean ANA_EMIRRORPORTS); 3207ccb6ed42SVladimir Oltean } 3208ccb6ed42SVladimir Oltean 3209ccb6ed42SVladimir Oltean return 0; 3210ccb6ed42SVladimir Oltean } 3211ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_add); 3212ccb6ed42SVladimir Oltean 3213ccb6ed42SVladimir Oltean void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress) 3214ccb6ed42SVladimir Oltean { 3215ccb6ed42SVladimir Oltean if (ingress) { 3216ccb6ed42SVladimir Oltean ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA, 3217ccb6ed42SVladimir Oltean ANA_PORT_PORT_CFG, from); 3218ccb6ed42SVladimir Oltean } else { 3219ccb6ed42SVladimir Oltean ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS); 3220ccb6ed42SVladimir Oltean } 3221ccb6ed42SVladimir Oltean 3222ccb6ed42SVladimir Oltean ocelot_mirror_put(ocelot); 3223ccb6ed42SVladimir Oltean } 3224ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_del); 3225ccb6ed42SVladimir Oltean 32265e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port) 3227fa914e9cSVladimir Oltean { 3228fa914e9cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 3229fa914e9cSVladimir Oltean 3230b049da13SYangbo Lu skb_queue_head_init(&ocelot_port->tx_skbs); 323131350d7fSVladimir Oltean 323231350d7fSVladimir Oltean /* Basic L2 initialization */ 323331350d7fSVladimir Oltean 32345bc9d2e6SVladimir Oltean /* Set MAC IFG Gaps 32355bc9d2e6SVladimir Oltean * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 32365bc9d2e6SVladimir Oltean * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 32375bc9d2e6SVladimir Oltean */ 32385bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), 32395bc9d2e6SVladimir Oltean DEV_MAC_IFG_CFG); 32405bc9d2e6SVladimir Oltean 32415bc9d2e6SVladimir Oltean /* Load seed (0) and set MAC HDX late collision */ 32425bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 32435bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG_SEED_LOAD, 32445bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 32455bc9d2e6SVladimir Oltean mdelay(1); 32465bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 32475bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 32485bc9d2e6SVladimir Oltean 32495bc9d2e6SVladimir Oltean /* Set Max Length and maximum tags allowed */ 3250a8015dedSVladimir Oltean ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); 32515bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 32525bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 3253a8015dedSVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | 32545bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 32555bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG); 32565bc9d2e6SVladimir Oltean 32575bc9d2e6SVladimir Oltean /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 32585bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 32595bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); 32605bc9d2e6SVladimir Oltean 3261e8e6e73dSVladimir Oltean /* Enable transmission of pause frames */ 3262541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 3263e8e6e73dSVladimir Oltean 326431350d7fSVladimir Oltean /* Drop frames with multicast source address */ 326531350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 326631350d7fSVladimir Oltean ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 326731350d7fSVladimir Oltean ANA_PORT_DROP_CFG, port); 326831350d7fSVladimir Oltean 326931350d7fSVladimir Oltean /* Set default VLAN and tag type to 8021Q. */ 327031350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), 327131350d7fSVladimir Oltean REW_PORT_VLAN_CFG_PORT_TPID_M, 327231350d7fSVladimir Oltean REW_PORT_VLAN_CFG, port); 327331350d7fSVladimir Oltean 3274421741eaSVladimir Oltean /* Disable source address learning for standalone mode */ 3275421741eaSVladimir Oltean ocelot_port_set_learning(ocelot, port, false); 3276421741eaSVladimir Oltean 327746efe4efSVladimir Oltean /* Set the port's initial logical port ID value, enable receiving 327846efe4efSVladimir Oltean * frames on it, and configure the MAC address learning type to 327946efe4efSVladimir Oltean * automatic. 328046efe4efSVladimir Oltean */ 328146efe4efSVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 328246efe4efSVladimir Oltean ANA_PORT_PORT_CFG_RECV_ENA | 328346efe4efSVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 328446efe4efSVladimir Oltean ANA_PORT_PORT_CFG, port); 328546efe4efSVladimir Oltean 328631350d7fSVladimir Oltean /* Enable vcap lookups */ 328731350d7fSVladimir Oltean ocelot_vcap_enable(ocelot, port); 328831350d7fSVladimir Oltean } 32895e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port); 329031350d7fSVladimir Oltean 32912d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues 32922d44b097SVladimir Oltean * accessible through register MMIO, frame DMA or Ethernet (in case 32932d44b097SVladimir Oltean * NPI mode is used). 329469df578cSVladimir Oltean */ 32952d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot) 329621468199SVladimir Oltean { 329769df578cSVladimir Oltean int cpu = ocelot->num_phys_ports; 329869df578cSVladimir Oltean 329969df578cSVladimir Oltean /* The unicast destination PGID for the CPU port module is unused */ 330021468199SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 330169df578cSVladimir Oltean /* Instead set up a multicast destination PGID for traffic copied to 330269df578cSVladimir Oltean * the CPU. Whitelisted MAC addresses like the port netdevice MAC 330369df578cSVladimir Oltean * addresses will be copied to the CPU via this PGID. 330469df578cSVladimir Oltean */ 330521468199SVladimir Oltean ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 330621468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 330721468199SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 330821468199SVladimir Oltean ANA_PORT_PORT_CFG, cpu); 330921468199SVladimir Oltean 331069df578cSVladimir Oltean /* Enable CPU port module */ 3311886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 331269df578cSVladimir Oltean /* CPU port Injection/Extraction configuration */ 3313886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, 3314cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 3315886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, 3316cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 331721468199SVladimir Oltean 331821468199SVladimir Oltean /* Configure the CPU port to be VLAN aware */ 3319bfbab310SVladimir Oltean ocelot_write_gix(ocelot, 332054c31984SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) | 332121468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 332221468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 332321468199SVladimir Oltean ANA_PORT_VLAN_CFG, cpu); 332421468199SVladimir Oltean } 332521468199SVladimir Oltean 3326f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot) 3327f6fe01d6SVladimir Oltean { 3328f6fe01d6SVladimir Oltean int mmgt, eq_ctrl; 3329f6fe01d6SVladimir Oltean 3330f6fe01d6SVladimir Oltean /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds 3331f6fe01d6SVladimir Oltean * the number of 240-byte free memory words (aka 4-cell chunks) and not 3332f6fe01d6SVladimir Oltean * 192 bytes as the documentation incorrectly says. 3333f6fe01d6SVladimir Oltean */ 3334f6fe01d6SVladimir Oltean mmgt = ocelot_read(ocelot, SYS_MMGT); 3335f6fe01d6SVladimir Oltean ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); 3336f6fe01d6SVladimir Oltean 3337f6fe01d6SVladimir Oltean eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); 3338f6fe01d6SVladimir Oltean ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); 3339f6fe01d6SVladimir Oltean } 3340f6fe01d6SVladimir Oltean 3341a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot) 3342a556c76aSAlexandre Belloni { 33432f187bfaSColin Foster const struct ocelot_stat_layout *stat; 3344a556c76aSAlexandre Belloni char queue_name[32]; 334521468199SVladimir Oltean int i, ret; 334621468199SVladimir Oltean u32 port; 3347a556c76aSAlexandre Belloni 33483a77b593SVladimir Oltean if (ocelot->ops->reset) { 33493a77b593SVladimir Oltean ret = ocelot->ops->reset(ocelot); 33503a77b593SVladimir Oltean if (ret) { 33513a77b593SVladimir Oltean dev_err(ocelot->dev, "Switch reset failed\n"); 33523a77b593SVladimir Oltean return ret; 33533a77b593SVladimir Oltean } 33543a77b593SVladimir Oltean } 33553a77b593SVladimir Oltean 33562f187bfaSColin Foster ocelot->num_stats = 0; 33572f187bfaSColin Foster for_each_stat(ocelot, stat) 33582f187bfaSColin Foster ocelot->num_stats++; 33592f187bfaSColin Foster 3360a556c76aSAlexandre Belloni ocelot->stats = devm_kcalloc(ocelot->dev, 3361a556c76aSAlexandre Belloni ocelot->num_phys_ports * ocelot->num_stats, 3362a556c76aSAlexandre Belloni sizeof(u64), GFP_KERNEL); 3363a556c76aSAlexandre Belloni if (!ocelot->stats) 3364a556c76aSAlexandre Belloni return -ENOMEM; 3365a556c76aSAlexandre Belloni 3366a556c76aSAlexandre Belloni mutex_init(&ocelot->stats_lock); 33674e3b0468SAntoine Tenart mutex_init(&ocelot->ptp_lock); 33682468346cSVladimir Oltean mutex_init(&ocelot->mact_lock); 33698abe1970SVladimir Oltean mutex_init(&ocelot->fwd_domain_lock); 3370*8670dc33SXiaoliang Yang mutex_init(&ocelot->tas_lock); 33714e3b0468SAntoine Tenart spin_lock_init(&ocelot->ptp_clock_lock); 337252849bcfSVladimir Oltean spin_lock_init(&ocelot->ts_id_lock); 3373a556c76aSAlexandre Belloni snprintf(queue_name, sizeof(queue_name), "%s-stats", 3374a556c76aSAlexandre Belloni dev_name(ocelot->dev)); 3375a556c76aSAlexandre Belloni ocelot->stats_queue = create_singlethread_workqueue(queue_name); 3376a556c76aSAlexandre Belloni if (!ocelot->stats_queue) 3377a556c76aSAlexandre Belloni return -ENOMEM; 3378a556c76aSAlexandre Belloni 3379ca0b272bSVladimir Oltean ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); 3380ca0b272bSVladimir Oltean if (!ocelot->owq) { 3381ca0b272bSVladimir Oltean destroy_workqueue(ocelot->stats_queue); 3382ca0b272bSVladimir Oltean return -ENOMEM; 3383ca0b272bSVladimir Oltean } 3384ca0b272bSVladimir Oltean 33852b120ddeSClaudiu Manoil INIT_LIST_HEAD(&ocelot->multicast); 3386e5d1f896SVladimir Oltean INIT_LIST_HEAD(&ocelot->pgids); 338790e0aa8dSVladimir Oltean INIT_LIST_HEAD(&ocelot->vlans); 3388961d8b69SVladimir Oltean INIT_LIST_HEAD(&ocelot->lag_fdbs); 3389f6fe01d6SVladimir Oltean ocelot_detect_features(ocelot); 3390a556c76aSAlexandre Belloni ocelot_mact_init(ocelot); 3391a556c76aSAlexandre Belloni ocelot_vlan_init(ocelot); 3392aae4e500SVladimir Oltean ocelot_vcap_init(ocelot); 33932d44b097SVladimir Oltean ocelot_cpu_port_init(ocelot); 3394a556c76aSAlexandre Belloni 339523e2c506SXiaoliang Yang if (ocelot->ops->psfp_init) 339623e2c506SXiaoliang Yang ocelot->ops->psfp_init(ocelot); 339723e2c506SXiaoliang Yang 3398a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 3399a556c76aSAlexandre Belloni /* Clear all counters (5 groups) */ 3400a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 3401a556c76aSAlexandre Belloni SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 3402a556c76aSAlexandre Belloni SYS_STAT_CFG); 3403a556c76aSAlexandre Belloni } 3404a556c76aSAlexandre Belloni 3405a556c76aSAlexandre Belloni /* Only use S-Tag */ 3406a556c76aSAlexandre Belloni ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 3407a556c76aSAlexandre Belloni 3408a556c76aSAlexandre Belloni /* Aggregation mode */ 3409a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 3410a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_DMAC_ENA | 3411a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 3412f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | 3413f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | 3414f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, 3415f79c20c8SVladimir Oltean ANA_AGGR_CFG); 3416a556c76aSAlexandre Belloni 3417a556c76aSAlexandre Belloni /* Set MAC age time to default value. The entry is aged after 3418a556c76aSAlexandre Belloni * 2*AGE_PERIOD 3419a556c76aSAlexandre Belloni */ 3420a556c76aSAlexandre Belloni ocelot_write(ocelot, 3421a556c76aSAlexandre Belloni ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 3422a556c76aSAlexandre Belloni ANA_AUTOAGE); 3423a556c76aSAlexandre Belloni 3424a556c76aSAlexandre Belloni /* Disable learning for frames discarded by VLAN ingress filtering */ 3425a556c76aSAlexandre Belloni regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 3426a556c76aSAlexandre Belloni 3427a556c76aSAlexandre Belloni /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 3428a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 3429a556c76aSAlexandre Belloni SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 3430a556c76aSAlexandre Belloni 3431a556c76aSAlexandre Belloni /* Setup flooding PGIDs */ 3432edd2410bSVladimir Oltean for (i = 0; i < ocelot->num_flooding_pgids; i++) 3433a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 3434b360d94fSVladimir Oltean ANA_FLOODING_FLD_BROADCAST(PGID_BC) | 3435a556c76aSAlexandre Belloni ANA_FLOODING_FLD_UNICAST(PGID_UC), 3436edd2410bSVladimir Oltean ANA_FLOODING, i); 3437a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 3438a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 3439a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 3440a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 3441a556c76aSAlexandre Belloni ANA_FLOODING_IPMC); 3442a556c76aSAlexandre Belloni 3443a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 3444a556c76aSAlexandre Belloni /* Transmit the frame to the local port. */ 3445a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 3446a556c76aSAlexandre Belloni /* Do not forward BPDU frames to the front ports. */ 3447a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, 3448a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 3449a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG, 3450a556c76aSAlexandre Belloni port); 3451a556c76aSAlexandre Belloni /* Ensure bridging is disabled */ 3452a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 3453a556c76aSAlexandre Belloni } 3454a556c76aSAlexandre Belloni 345596b029b0SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, i) { 3456a556c76aSAlexandre Belloni u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 3457a556c76aSAlexandre Belloni 3458a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 3459a556c76aSAlexandre Belloni } 3460ebb1bb40SHoratiu Vultur 3461ebb1bb40SHoratiu Vultur ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE); 3462ebb1bb40SHoratiu Vultur 3463b360d94fSVladimir Oltean /* Allow broadcast and unknown L2 multicast to the CPU. */ 3464b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 3465b360d94fSVladimir Oltean ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 3466a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_MC); 3467b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 3468b360d94fSVladimir Oltean ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 3469b360d94fSVladimir Oltean ANA_PGID_PGID, PGID_BC); 3470a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 3471a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 3472a556c76aSAlexandre Belloni 3473a556c76aSAlexandre Belloni /* Allow manual injection via DEVCPU_QS registers, and byte swap these 3474a556c76aSAlexandre Belloni * registers endianness. 3475a556c76aSAlexandre Belloni */ 3476a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 3477a556c76aSAlexandre Belloni QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 3478a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 3479a556c76aSAlexandre Belloni QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 3480a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 3481a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LRN(2) | 3482a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 3483a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 3484a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 3485a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 3486a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 3487a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IGMP(6) | 3488a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 3489a556c76aSAlexandre Belloni for (i = 0; i < 16; i++) 3490a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 3491a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 3492a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG, i); 3493a556c76aSAlexandre Belloni 3494d87b1c08SColin Foster ret = ocelot_prepare_stats_regions(ocelot); 3495d87b1c08SColin Foster if (ret) { 3496d87b1c08SColin Foster destroy_workqueue(ocelot->stats_queue); 3497d87b1c08SColin Foster destroy_workqueue(ocelot->owq); 3498d87b1c08SColin Foster return ret; 3499d87b1c08SColin Foster } 3500d87b1c08SColin Foster 35011e1caa97SClaudiu Manoil INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 3502a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 3503a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 35044e3b0468SAntoine Tenart 3505a556c76aSAlexandre Belloni return 0; 3506a556c76aSAlexandre Belloni } 3507a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init); 3508a556c76aSAlexandre Belloni 3509a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot) 3510a556c76aSAlexandre Belloni { 3511c5d13969SClaudiu Manoil cancel_delayed_work(&ocelot->stats_work); 3512a556c76aSAlexandre Belloni destroy_workqueue(ocelot->stats_queue); 3513ca0b272bSVladimir Oltean destroy_workqueue(ocelot->owq); 3514a556c76aSAlexandre Belloni mutex_destroy(&ocelot->stats_lock); 3515a556c76aSAlexandre Belloni } 3516a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit); 3517a556c76aSAlexandre Belloni 3518e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port) 3519e5fb512dSVladimir Oltean { 3520e5fb512dSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 3521e5fb512dSVladimir Oltean 3522e5fb512dSVladimir Oltean skb_queue_purge(&ocelot_port->tx_skbs); 3523e5fb512dSVladimir Oltean } 3524e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port); 3525e5fb512dSVladimir Oltean 3526a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL"); 3527