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 */ 7a556c76aSAlexandre Belloni #include <linux/if_bridge.h> 820968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h> 9a556c76aSAlexandre Belloni #include "ocelot.h" 103c83654fSVladimir Oltean #include "ocelot_vcap.h" 11a556c76aSAlexandre Belloni 12639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10 13639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000 14639c1b26SSteen Hegelund 15a556c76aSAlexandre Belloni struct ocelot_mact_entry { 16a556c76aSAlexandre Belloni u8 mac[ETH_ALEN]; 17a556c76aSAlexandre Belloni u16 vid; 18a556c76aSAlexandre Belloni enum macaccess_entry_type type; 19a556c76aSAlexandre Belloni }; 20a556c76aSAlexandre Belloni 21639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) 22639c1b26SSteen Hegelund { 23639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_MACACCESS); 24639c1b26SSteen Hegelund } 25639c1b26SSteen Hegelund 26a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) 27a556c76aSAlexandre Belloni { 28639c1b26SSteen Hegelund u32 val; 29a556c76aSAlexandre Belloni 30639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_mact_read_macaccess, 31639c1b26SSteen Hegelund ocelot, val, 32639c1b26SSteen Hegelund (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == 33639c1b26SSteen Hegelund MACACCESS_CMD_IDLE, 34639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 35a556c76aSAlexandre Belloni } 36a556c76aSAlexandre Belloni 37a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot, 38a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 39a556c76aSAlexandre Belloni unsigned int vid) 40a556c76aSAlexandre Belloni { 41a556c76aSAlexandre Belloni u32 macl = 0, mach = 0; 42a556c76aSAlexandre Belloni 43a556c76aSAlexandre Belloni /* Set the MAC address to handle and the vlan associated in a format 44a556c76aSAlexandre Belloni * understood by the hardware. 45a556c76aSAlexandre Belloni */ 46a556c76aSAlexandre Belloni mach |= vid << 16; 47a556c76aSAlexandre Belloni mach |= mac[0] << 8; 48a556c76aSAlexandre Belloni mach |= mac[1] << 0; 49a556c76aSAlexandre Belloni macl |= mac[2] << 24; 50a556c76aSAlexandre Belloni macl |= mac[3] << 16; 51a556c76aSAlexandre Belloni macl |= mac[4] << 8; 52a556c76aSAlexandre Belloni macl |= mac[5] << 0; 53a556c76aSAlexandre Belloni 54a556c76aSAlexandre Belloni ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); 55a556c76aSAlexandre Belloni ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); 56a556c76aSAlexandre Belloni 57a556c76aSAlexandre Belloni } 58a556c76aSAlexandre Belloni 599c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port, 60a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 619c90eea3SVladimir Oltean unsigned int vid, enum macaccess_entry_type type) 62a556c76aSAlexandre Belloni { 63584b7cfcSAlban Bedel u32 cmd = ANA_TABLES_MACACCESS_VALID | 64584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_DEST_IDX(port) | 65584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_ENTRYTYPE(type) | 66584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); 67584b7cfcSAlban Bedel unsigned int mc_ports; 68584b7cfcSAlban Bedel 69584b7cfcSAlban Bedel /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ 70584b7cfcSAlban Bedel if (type == ENTRYTYPE_MACv4) 71584b7cfcSAlban Bedel mc_ports = (mac[1] << 8) | mac[2]; 72584b7cfcSAlban Bedel else if (type == ENTRYTYPE_MACv6) 73584b7cfcSAlban Bedel mc_ports = (mac[0] << 8) | mac[1]; 74584b7cfcSAlban Bedel else 75584b7cfcSAlban Bedel mc_ports = 0; 76584b7cfcSAlban Bedel 77584b7cfcSAlban Bedel if (mc_ports & BIT(ocelot->num_phys_ports)) 78584b7cfcSAlban Bedel cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; 79584b7cfcSAlban Bedel 80a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 81a556c76aSAlexandre Belloni 82a556c76aSAlexandre Belloni /* Issue a write command */ 83584b7cfcSAlban Bedel ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); 84a556c76aSAlexandre Belloni 85a556c76aSAlexandre Belloni return ocelot_mact_wait_for_completion(ocelot); 86a556c76aSAlexandre Belloni } 879c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn); 88a556c76aSAlexandre Belloni 899c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot, 909c90eea3SVladimir Oltean const unsigned char mac[ETH_ALEN], unsigned int vid) 91a556c76aSAlexandre Belloni { 92a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 93a556c76aSAlexandre Belloni 94a556c76aSAlexandre Belloni /* Issue a forget command */ 95a556c76aSAlexandre Belloni ocelot_write(ocelot, 96a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), 97a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 98a556c76aSAlexandre Belloni 99a556c76aSAlexandre Belloni return ocelot_mact_wait_for_completion(ocelot); 100a556c76aSAlexandre Belloni } 1019c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget); 102a556c76aSAlexandre Belloni 103a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot) 104a556c76aSAlexandre Belloni { 105a556c76aSAlexandre Belloni /* Configure the learning mode entries attributes: 106a556c76aSAlexandre Belloni * - Do not copy the frame to the CPU extraction queues. 107a556c76aSAlexandre Belloni * - Use the vlan and mac_cpoy for dmac lookup. 108a556c76aSAlexandre Belloni */ 109a556c76aSAlexandre Belloni ocelot_rmw(ocelot, 0, 110a556c76aSAlexandre Belloni ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS 111a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_FWD_KILL 112a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_IGNORE_VLAN, 113a556c76aSAlexandre Belloni ANA_AGENCTRL); 114a556c76aSAlexandre Belloni 115a556c76aSAlexandre Belloni /* Clear the MAC table */ 116a556c76aSAlexandre Belloni ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); 117a556c76aSAlexandre Belloni } 118a556c76aSAlexandre Belloni 119f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port) 120b5962294SHoratiu Vultur { 121b5962294SHoratiu Vultur ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | 122b5962294SHoratiu Vultur ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), 123f270dbfaSVladimir Oltean ANA_PORT_VCAP_S2_CFG, port); 12475944fdaSXiaoliang Yang 12575944fdaSXiaoliang Yang ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, 12675944fdaSXiaoliang Yang ANA_PORT_VCAP_CFG, port); 1272f17c050SXiaoliang Yang 1282f17c050SXiaoliang Yang ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, 1292f17c050SXiaoliang Yang REW_PORT_CFG_ES0_EN, 1302f17c050SXiaoliang Yang REW_PORT_CFG, port); 131b5962294SHoratiu Vultur } 132b5962294SHoratiu Vultur 133639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) 134639c1b26SSteen Hegelund { 135639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); 136639c1b26SSteen Hegelund } 137639c1b26SSteen Hegelund 138a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) 139a556c76aSAlexandre Belloni { 140639c1b26SSteen Hegelund u32 val; 141a556c76aSAlexandre Belloni 142639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_vlant_read_vlanaccess, 143639c1b26SSteen Hegelund ocelot, 144639c1b26SSteen Hegelund val, 145639c1b26SSteen Hegelund (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == 146639c1b26SSteen Hegelund ANA_TABLES_VLANACCESS_CMD_IDLE, 147639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 148a556c76aSAlexandre Belloni } 149a556c76aSAlexandre Belloni 1507142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) 1517142529fSAntoine Tenart { 1527142529fSAntoine Tenart /* Select the VID to configure */ 1537142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), 1547142529fSAntoine Tenart ANA_TABLES_VLANTIDX); 1557142529fSAntoine Tenart /* Set the vlan port members mask and issue a write command */ 1567142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | 1577142529fSAntoine Tenart ANA_TABLES_VLANACCESS_CMD_WRITE, 1587142529fSAntoine Tenart ANA_TABLES_VLANACCESS); 1597142529fSAntoine Tenart 1607142529fSAntoine Tenart return ocelot_vlant_wait_for_completion(ocelot); 1617142529fSAntoine Tenart } 1627142529fSAntoine Tenart 1632f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port, 164c3e58a75SVladimir Oltean struct ocelot_vlan native_vlan) 16597bb69e1SVladimir Oltean { 16697bb69e1SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 16787b0f983SVladimir Oltean u32 val = 0; 16897bb69e1SVladimir Oltean 169c3e58a75SVladimir Oltean ocelot_port->native_vlan = native_vlan; 17097bb69e1SVladimir Oltean 171c3e58a75SVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid), 1727142529fSAntoine Tenart REW_PORT_VLAN_CFG_PORT_VID_M, 17397bb69e1SVladimir Oltean REW_PORT_VLAN_CFG, port); 17497bb69e1SVladimir Oltean 17587b0f983SVladimir Oltean if (ocelot_port->vlan_aware) { 176e2b2e83eSVladimir Oltean if (native_vlan.valid) 17787b0f983SVladimir Oltean /* Tag all frames except when VID == DEFAULT_VLAN */ 17887b0f983SVladimir Oltean val = REW_TAG_CFG_TAG_CFG(1); 17987b0f983SVladimir Oltean else 18087b0f983SVladimir Oltean /* Tag all frames */ 18187b0f983SVladimir Oltean val = REW_TAG_CFG_TAG_CFG(3); 18287b0f983SVladimir Oltean } else { 18387b0f983SVladimir Oltean /* Port tagging disabled. */ 18487b0f983SVladimir Oltean val = REW_TAG_CFG_TAG_CFG(0); 18587b0f983SVladimir Oltean } 18687b0f983SVladimir Oltean ocelot_rmw_gix(ocelot, val, 18787b0f983SVladimir Oltean REW_TAG_CFG_TAG_CFG_M, 18887b0f983SVladimir Oltean REW_TAG_CFG, port); 18997bb69e1SVladimir Oltean } 19097bb69e1SVladimir Oltean 19175e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */ 192c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, 193c3e58a75SVladimir Oltean struct ocelot_vlan pvid_vlan) 19475e5a554SVladimir Oltean { 19575e5a554SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 196be0576feSVladimir Oltean u32 val = 0; 19775e5a554SVladimir Oltean 198c3e58a75SVladimir Oltean ocelot_port->pvid_vlan = pvid_vlan; 19975e5a554SVladimir Oltean 20075e5a554SVladimir Oltean if (!ocelot_port->vlan_aware) 201c3e58a75SVladimir Oltean pvid_vlan.vid = 0; 20275e5a554SVladimir Oltean 20375e5a554SVladimir Oltean ocelot_rmw_gix(ocelot, 204c3e58a75SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid), 20575e5a554SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID_M, 20675e5a554SVladimir Oltean ANA_PORT_VLAN_CFG, port); 207be0576feSVladimir Oltean 208be0576feSVladimir Oltean /* If there's no pvid, we should drop not only untagged traffic (which 209be0576feSVladimir Oltean * happens automatically), but also 802.1p traffic which gets 210be0576feSVladimir Oltean * classified to VLAN 0, but that is always in our RX filter, so it 211be0576feSVladimir Oltean * would get accepted were it not for this setting. 212be0576feSVladimir Oltean */ 213be0576feSVladimir Oltean if (!pvid_vlan.valid && ocelot_port->vlan_aware) 214be0576feSVladimir Oltean val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 215be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; 216be0576feSVladimir Oltean 217be0576feSVladimir Oltean ocelot_rmw_gix(ocelot, val, 218be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 219be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, 220be0576feSVladimir Oltean ANA_PORT_DROP_CFG, port); 22175e5a554SVladimir Oltean } 22275e5a554SVladimir Oltean 2232e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, 224bae33f2bSVladimir Oltean bool vlan_aware) 22587b0f983SVladimir Oltean { 22670edfae1SVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; 227bae33f2bSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 22870edfae1SVladimir Oltean struct ocelot_vcap_filter *filter; 229bae33f2bSVladimir Oltean u32 val; 23070edfae1SVladimir Oltean 23170edfae1SVladimir Oltean list_for_each_entry(filter, &block->rules, list) { 23270edfae1SVladimir Oltean if (filter->ingress_port_mask & BIT(port) && 23370edfae1SVladimir Oltean filter->action.vid_replace_ena) { 23470edfae1SVladimir Oltean dev_err(ocelot->dev, 23570edfae1SVladimir Oltean "Cannot change VLAN state with vlan modify rules active\n"); 23670edfae1SVladimir Oltean return -EBUSY; 23770edfae1SVladimir Oltean } 23870edfae1SVladimir Oltean } 23970edfae1SVladimir Oltean 24087b0f983SVladimir Oltean ocelot_port->vlan_aware = vlan_aware; 24187b0f983SVladimir Oltean 24287b0f983SVladimir Oltean if (vlan_aware) 24387b0f983SVladimir Oltean val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 24487b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); 24587b0f983SVladimir Oltean else 24687b0f983SVladimir Oltean val = 0; 24787b0f983SVladimir Oltean ocelot_rmw_gix(ocelot, val, 24887b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 24987b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, 25087b0f983SVladimir Oltean ANA_PORT_VLAN_CFG, port); 25187b0f983SVladimir Oltean 252c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); 253c3e58a75SVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan); 2542e554a7aSVladimir Oltean 2552e554a7aSVladimir Oltean return 0; 25687b0f983SVladimir Oltean } 25787b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering); 25887b0f983SVladimir Oltean 2592f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, 2602f0402feSVladimir Oltean bool untagged) 2612f0402feSVladimir Oltean { 2622f0402feSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2632f0402feSVladimir Oltean 2642f0402feSVladimir Oltean /* Deny changing the native VLAN, but always permit deleting it */ 2652f0402feSVladimir Oltean if (untagged && ocelot_port->native_vlan.vid != vid && 2662f0402feSVladimir Oltean ocelot_port->native_vlan.valid) { 2672f0402feSVladimir Oltean dev_err(ocelot->dev, 2682f0402feSVladimir Oltean "Port already has a native VLAN: %d\n", 2692f0402feSVladimir Oltean ocelot_port->native_vlan.vid); 2702f0402feSVladimir Oltean return -EBUSY; 2712f0402feSVladimir Oltean } 2722f0402feSVladimir Oltean 2732f0402feSVladimir Oltean return 0; 2742f0402feSVladimir Oltean } 2752f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare); 2762f0402feSVladimir Oltean 2775e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, 2787142529fSAntoine Tenart bool untagged) 2797142529fSAntoine Tenart { 2807142529fSAntoine Tenart int ret; 2817142529fSAntoine Tenart 2827142529fSAntoine Tenart /* Make the port a member of the VLAN */ 28397bb69e1SVladimir Oltean ocelot->vlan_mask[vid] |= BIT(port); 2847142529fSAntoine Tenart ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 2857142529fSAntoine Tenart if (ret) 2867142529fSAntoine Tenart return ret; 2877142529fSAntoine Tenart 2887142529fSAntoine Tenart /* Default ingress vlan classification */ 289c3e58a75SVladimir Oltean if (pvid) { 290c3e58a75SVladimir Oltean struct ocelot_vlan pvid_vlan; 291c3e58a75SVladimir Oltean 292c3e58a75SVladimir Oltean pvid_vlan.vid = vid; 293e2b2e83eSVladimir Oltean pvid_vlan.valid = true; 294c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, pvid_vlan); 295c3e58a75SVladimir Oltean } 2967142529fSAntoine Tenart 2977142529fSAntoine Tenart /* Untagged egress vlan clasification */ 29897bb69e1SVladimir Oltean if (untagged) { 299c3e58a75SVladimir Oltean struct ocelot_vlan native_vlan; 300c3e58a75SVladimir Oltean 301c3e58a75SVladimir Oltean native_vlan.vid = vid; 302e2b2e83eSVladimir Oltean native_vlan.valid = true; 3032f0402feSVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, native_vlan); 304b9cd75e6SVladimir Oltean } 3057142529fSAntoine Tenart 3067142529fSAntoine Tenart return 0; 3077142529fSAntoine Tenart } 3085e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add); 3097142529fSAntoine Tenart 3105e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) 3119855934cSVladimir Oltean { 3129855934cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 3139855934cSVladimir Oltean int ret; 3147142529fSAntoine Tenart 3157142529fSAntoine Tenart /* Stop the port from being a member of the vlan */ 31697bb69e1SVladimir Oltean ocelot->vlan_mask[vid] &= ~BIT(port); 3177142529fSAntoine Tenart ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 3187142529fSAntoine Tenart if (ret) 3197142529fSAntoine Tenart return ret; 3207142529fSAntoine Tenart 321be0576feSVladimir Oltean /* Ingress */ 322be0576feSVladimir Oltean if (ocelot_port->pvid_vlan.vid == vid) { 323be0576feSVladimir Oltean struct ocelot_vlan pvid_vlan = {0}; 324be0576feSVladimir Oltean 325be0576feSVladimir Oltean ocelot_port_set_pvid(ocelot, port, pvid_vlan); 326be0576feSVladimir Oltean } 327be0576feSVladimir Oltean 3287142529fSAntoine Tenart /* Egress */ 329c3e58a75SVladimir Oltean if (ocelot_port->native_vlan.vid == vid) { 330e2b2e83eSVladimir Oltean struct ocelot_vlan native_vlan = {0}; 331c3e58a75SVladimir Oltean 332c3e58a75SVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, native_vlan); 333c3e58a75SVladimir Oltean } 3347142529fSAntoine Tenart 3357142529fSAntoine Tenart return 0; 3367142529fSAntoine Tenart } 3375e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del); 3387142529fSAntoine Tenart 339a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot) 340a556c76aSAlexandre Belloni { 3417142529fSAntoine Tenart u16 port, vid; 3427142529fSAntoine Tenart 343a556c76aSAlexandre Belloni /* Clear VLAN table, by default all ports are members of all VLANs */ 344a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, 345a556c76aSAlexandre Belloni ANA_TABLES_VLANACCESS); 346a556c76aSAlexandre Belloni ocelot_vlant_wait_for_completion(ocelot); 3477142529fSAntoine Tenart 3487142529fSAntoine Tenart /* Configure the port VLAN memberships */ 3497142529fSAntoine Tenart for (vid = 1; vid < VLAN_N_VID; vid++) { 3507142529fSAntoine Tenart ocelot->vlan_mask[vid] = 0; 3517142529fSAntoine Tenart ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 3527142529fSAntoine Tenart } 3537142529fSAntoine Tenart 3547142529fSAntoine Tenart /* Because VLAN filtering is enabled, we need VID 0 to get untagged 3557142529fSAntoine Tenart * traffic. It is added automatically if 8021q module is loaded, but 3567142529fSAntoine Tenart * we can't rely on it since module may be not loaded. 3577142529fSAntoine Tenart */ 3587142529fSAntoine Tenart ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0); 3597142529fSAntoine Tenart ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]); 3607142529fSAntoine Tenart 3617142529fSAntoine Tenart /* Set vlan ingress filter mask to all ports but the CPU port by 3627142529fSAntoine Tenart * default. 3637142529fSAntoine Tenart */ 364714d0ffaSVladimir Oltean ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 365714d0ffaSVladimir Oltean ANA_VLANMASK); 3667142529fSAntoine Tenart 3677142529fSAntoine Tenart for (port = 0; port < ocelot->num_phys_ports; port++) { 3687142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); 3697142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); 3707142529fSAntoine Tenart } 371a556c76aSAlexandre Belloni } 372a556c76aSAlexandre Belloni 3735e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port, 37426f4dbabSVladimir Oltean struct phy_device *phydev) 375a556c76aSAlexandre Belloni { 37626f4dbabSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 3775bc9d2e6SVladimir Oltean int speed, mode = 0; 378a556c76aSAlexandre Belloni 37926f4dbabSVladimir Oltean switch (phydev->speed) { 380a556c76aSAlexandre Belloni case SPEED_10: 381a556c76aSAlexandre Belloni speed = OCELOT_SPEED_10; 382a556c76aSAlexandre Belloni break; 383a556c76aSAlexandre Belloni case SPEED_100: 384a556c76aSAlexandre Belloni speed = OCELOT_SPEED_100; 385a556c76aSAlexandre Belloni break; 386a556c76aSAlexandre Belloni case SPEED_1000: 387a556c76aSAlexandre Belloni speed = OCELOT_SPEED_1000; 388a556c76aSAlexandre Belloni mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 389a556c76aSAlexandre Belloni break; 390a556c76aSAlexandre Belloni case SPEED_2500: 391a556c76aSAlexandre Belloni speed = OCELOT_SPEED_2500; 392a556c76aSAlexandre Belloni mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 393a556c76aSAlexandre Belloni break; 394a556c76aSAlexandre Belloni default: 39526f4dbabSVladimir Oltean dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n", 39626f4dbabSVladimir Oltean port, phydev->speed); 397a556c76aSAlexandre Belloni return; 398a556c76aSAlexandre Belloni } 399a556c76aSAlexandre Belloni 40026f4dbabSVladimir Oltean phy_print_status(phydev); 401a556c76aSAlexandre Belloni 40226f4dbabSVladimir Oltean if (!phydev->link) 403a556c76aSAlexandre Belloni return; 404a556c76aSAlexandre Belloni 405a556c76aSAlexandre Belloni /* Only full duplex supported for now */ 406004d44f6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA | 407a556c76aSAlexandre Belloni mode, DEV_MAC_MODE_CFG); 408a556c76aSAlexandre Belloni 4091ba8f656SVladimir Oltean /* Disable HDX fast control */ 4101ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS, 4111ba8f656SVladimir Oltean DEV_PORT_MISC); 4121ba8f656SVladimir Oltean 4131ba8f656SVladimir Oltean /* SGMII only for now */ 4141ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA, 4151ba8f656SVladimir Oltean PCS1G_MODE_CFG); 4161ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG); 4171ba8f656SVladimir Oltean 4181ba8f656SVladimir Oltean /* Enable PCS */ 4191ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG); 4201ba8f656SVladimir Oltean 4211ba8f656SVladimir Oltean /* No aneg on SGMII */ 4221ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG); 4231ba8f656SVladimir Oltean 4241ba8f656SVladimir Oltean /* No loopback */ 4251ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG); 426a556c76aSAlexandre Belloni 427a556c76aSAlexandre Belloni /* Enable MAC module */ 428004d44f6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | 429a556c76aSAlexandre Belloni DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 430a556c76aSAlexandre Belloni 431a556c76aSAlexandre Belloni /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of 432a556c76aSAlexandre Belloni * reset */ 433004d44f6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed), 434a556c76aSAlexandre Belloni DEV_CLOCK_CFG); 435a556c76aSAlexandre Belloni 436a556c76aSAlexandre Belloni /* No PFC */ 437a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed), 438004d44f6SVladimir Oltean ANA_PFC_PFC_CFG, port); 439a556c76aSAlexandre Belloni 440a556c76aSAlexandre Belloni /* Core: Enable port for frame transfer */ 441886e1387SVladimir Oltean ocelot_fields_write(ocelot, port, 442886e1387SVladimir Oltean QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 443a556c76aSAlexandre Belloni 444a556c76aSAlexandre Belloni /* Flow control */ 445a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 446a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA | 447a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_ZERO_PAUSE_ENA | 448a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 449a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_FC_LINK_SPEED(speed), 450004d44f6SVladimir Oltean SYS_MAC_FC_CFG, port); 451004d44f6SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); 452a556c76aSAlexandre Belloni } 4535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link); 454a556c76aSAlexandre Belloni 4555e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port, 456889b8950SVladimir Oltean struct phy_device *phy) 457a556c76aSAlexandre Belloni { 458a556c76aSAlexandre Belloni /* Enable receiving frames on the port, and activate auto-learning of 459a556c76aSAlexandre Belloni * MAC addresses. 460a556c76aSAlexandre Belloni */ 461a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 462a556c76aSAlexandre Belloni ANA_PORT_PORT_CFG_RECV_ENA | 463004d44f6SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 464004d44f6SVladimir Oltean ANA_PORT_PORT_CFG, port); 465889b8950SVladimir Oltean } 4665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable); 467889b8950SVladimir Oltean 4685e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port) 469889b8950SVladimir Oltean { 470889b8950SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 471889b8950SVladimir Oltean 472889b8950SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG); 473886e1387SVladimir Oltean ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); 474889b8950SVladimir Oltean } 4755e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable); 476889b8950SVladimir Oltean 477e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, 478e2f9a8feSVladimir Oltean struct sk_buff *clone) 479400928bfSYangbo Lu { 480e2f9a8feSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 481400928bfSYangbo Lu 4826565243cSVladimir Oltean spin_lock(&ocelot_port->ts_id_lock); 4836565243cSVladimir Oltean 484e2f9a8feSVladimir Oltean skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; 485b049da13SYangbo Lu /* Store timestamp ID in cb[0] of sk_buff */ 486e2f9a8feSVladimir Oltean clone->cb[0] = ocelot_port->ts_id; 4876565243cSVladimir Oltean ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4; 488e2f9a8feSVladimir Oltean skb_queue_tail(&ocelot_port->tx_skbs, clone); 4896565243cSVladimir Oltean 4906565243cSVladimir Oltean spin_unlock(&ocelot_port->ts_id_lock); 491400928bfSYangbo Lu } 492400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb); 493400928bfSYangbo Lu 494e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot, 495e23a7b3eSYangbo Lu struct timespec64 *ts) 4964e3b0468SAntoine Tenart { 4974e3b0468SAntoine Tenart unsigned long flags; 4984e3b0468SAntoine Tenart u32 val; 4994e3b0468SAntoine Tenart 5004e3b0468SAntoine Tenart spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); 5014e3b0468SAntoine Tenart 5024e3b0468SAntoine Tenart /* Read current PTP time to get seconds */ 5034e3b0468SAntoine Tenart val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); 5044e3b0468SAntoine Tenart 5054e3b0468SAntoine Tenart val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); 5064e3b0468SAntoine Tenart val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); 5074e3b0468SAntoine Tenart ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); 5084e3b0468SAntoine Tenart ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); 5094e3b0468SAntoine Tenart 5104e3b0468SAntoine Tenart /* Read packet HW timestamp from FIFO */ 5114e3b0468SAntoine Tenart val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); 5124e3b0468SAntoine Tenart ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); 5134e3b0468SAntoine Tenart 5144e3b0468SAntoine Tenart /* Sec has incremented since the ts was registered */ 5154e3b0468SAntoine Tenart if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) 5164e3b0468SAntoine Tenart ts->tv_sec--; 5174e3b0468SAntoine Tenart 5184e3b0468SAntoine Tenart spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); 5194e3b0468SAntoine Tenart } 520e23a7b3eSYangbo Lu 521e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot) 522e23a7b3eSYangbo Lu { 523e23a7b3eSYangbo Lu int budget = OCELOT_PTP_QUEUE_SZ; 524e23a7b3eSYangbo Lu 525e23a7b3eSYangbo Lu while (budget--) { 526b049da13SYangbo Lu struct sk_buff *skb, *skb_tmp, *skb_match = NULL; 527e23a7b3eSYangbo Lu struct skb_shared_hwtstamps shhwtstamps; 528e23a7b3eSYangbo Lu struct ocelot_port *port; 529e23a7b3eSYangbo Lu struct timespec64 ts; 530b049da13SYangbo Lu unsigned long flags; 531e23a7b3eSYangbo Lu u32 val, id, txport; 532e23a7b3eSYangbo Lu 533e23a7b3eSYangbo Lu val = ocelot_read(ocelot, SYS_PTP_STATUS); 534e23a7b3eSYangbo Lu 535e23a7b3eSYangbo Lu /* Check if a timestamp can be retrieved */ 536e23a7b3eSYangbo Lu if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) 537e23a7b3eSYangbo Lu break; 538e23a7b3eSYangbo Lu 539e23a7b3eSYangbo Lu WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); 540e23a7b3eSYangbo Lu 541e23a7b3eSYangbo Lu /* Retrieve the ts ID and Tx port */ 542e23a7b3eSYangbo Lu id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); 543e23a7b3eSYangbo Lu txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); 544e23a7b3eSYangbo Lu 545e23a7b3eSYangbo Lu /* Retrieve its associated skb */ 546e23a7b3eSYangbo Lu port = ocelot->ports[txport]; 547e23a7b3eSYangbo Lu 548b049da13SYangbo Lu spin_lock_irqsave(&port->tx_skbs.lock, flags); 549b049da13SYangbo Lu 550b049da13SYangbo Lu skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { 551b049da13SYangbo Lu if (skb->cb[0] != id) 552e23a7b3eSYangbo Lu continue; 553b049da13SYangbo Lu __skb_unlink(skb, &port->tx_skbs); 554b049da13SYangbo Lu skb_match = skb; 555fc62c094SYangbo Lu break; 556e23a7b3eSYangbo Lu } 557e23a7b3eSYangbo Lu 558b049da13SYangbo Lu spin_unlock_irqrestore(&port->tx_skbs.lock, flags); 559b049da13SYangbo Lu 5605fd82200Slaurent brando /* Get the h/w timestamp */ 5615fd82200Slaurent brando ocelot_get_hwtimestamp(ocelot, &ts); 562e23a7b3eSYangbo Lu 563b049da13SYangbo Lu if (unlikely(!skb_match)) 564e23a7b3eSYangbo Lu continue; 565e23a7b3eSYangbo Lu 566e23a7b3eSYangbo Lu /* Set the timestamp into the skb */ 567e23a7b3eSYangbo Lu memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 568e23a7b3eSYangbo Lu shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 569e2f9a8feSVladimir Oltean skb_complete_tx_timestamp(skb_match, &shhwtstamps); 5705fd82200Slaurent brando 5715fd82200Slaurent brando /* Next ts */ 5725fd82200Slaurent brando ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); 573e23a7b3eSYangbo Lu } 574e23a7b3eSYangbo Lu } 575e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp); 5764e3b0468SAntoine Tenart 5775e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, 57887b0f983SVladimir Oltean const unsigned char *addr, u16 vid) 579a556c76aSAlexandre Belloni { 580471beb11SVladimir Oltean int pgid = port; 581471beb11SVladimir Oltean 582471beb11SVladimir Oltean if (port == ocelot->npi) 583471beb11SVladimir Oltean pgid = PGID_CPU; 584a556c76aSAlexandre Belloni 585471beb11SVladimir Oltean return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); 586a556c76aSAlexandre Belloni } 5875e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add); 588a556c76aSAlexandre Belloni 5895e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, 590531ee1a6SVladimir Oltean const unsigned char *addr, u16 vid) 591531ee1a6SVladimir Oltean { 592531ee1a6SVladimir Oltean return ocelot_mact_forget(ocelot, addr, vid); 593531ee1a6SVladimir Oltean } 5945e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del); 595531ee1a6SVladimir Oltean 5969c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, 597531ee1a6SVladimir Oltean bool is_static, void *data) 598a556c76aSAlexandre Belloni { 599531ee1a6SVladimir Oltean struct ocelot_dump_ctx *dump = data; 600a556c76aSAlexandre Belloni u32 portid = NETLINK_CB(dump->cb->skb).portid; 601a556c76aSAlexandre Belloni u32 seq = dump->cb->nlh->nlmsg_seq; 602a556c76aSAlexandre Belloni struct nlmsghdr *nlh; 603a556c76aSAlexandre Belloni struct ndmsg *ndm; 604a556c76aSAlexandre Belloni 605a556c76aSAlexandre Belloni if (dump->idx < dump->cb->args[2]) 606a556c76aSAlexandre Belloni goto skip; 607a556c76aSAlexandre Belloni 608a556c76aSAlexandre Belloni nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 609a556c76aSAlexandre Belloni sizeof(*ndm), NLM_F_MULTI); 610a556c76aSAlexandre Belloni if (!nlh) 611a556c76aSAlexandre Belloni return -EMSGSIZE; 612a556c76aSAlexandre Belloni 613a556c76aSAlexandre Belloni ndm = nlmsg_data(nlh); 614a556c76aSAlexandre Belloni ndm->ndm_family = AF_BRIDGE; 615a556c76aSAlexandre Belloni ndm->ndm_pad1 = 0; 616a556c76aSAlexandre Belloni ndm->ndm_pad2 = 0; 617a556c76aSAlexandre Belloni ndm->ndm_flags = NTF_SELF; 618a556c76aSAlexandre Belloni ndm->ndm_type = 0; 619a556c76aSAlexandre Belloni ndm->ndm_ifindex = dump->dev->ifindex; 620531ee1a6SVladimir Oltean ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 621a556c76aSAlexandre Belloni 622531ee1a6SVladimir Oltean if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 623a556c76aSAlexandre Belloni goto nla_put_failure; 624a556c76aSAlexandre Belloni 625531ee1a6SVladimir Oltean if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 626a556c76aSAlexandre Belloni goto nla_put_failure; 627a556c76aSAlexandre Belloni 628a556c76aSAlexandre Belloni nlmsg_end(dump->skb, nlh); 629a556c76aSAlexandre Belloni 630a556c76aSAlexandre Belloni skip: 631a556c76aSAlexandre Belloni dump->idx++; 632a556c76aSAlexandre Belloni return 0; 633a556c76aSAlexandre Belloni 634a556c76aSAlexandre Belloni nla_put_failure: 635a556c76aSAlexandre Belloni nlmsg_cancel(dump->skb, nlh); 636a556c76aSAlexandre Belloni return -EMSGSIZE; 637a556c76aSAlexandre Belloni } 6389c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump); 639a556c76aSAlexandre Belloni 640531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, 641a556c76aSAlexandre Belloni struct ocelot_mact_entry *entry) 642a556c76aSAlexandre Belloni { 643a556c76aSAlexandre Belloni u32 val, dst, macl, mach; 644531ee1a6SVladimir Oltean char mac[ETH_ALEN]; 645a556c76aSAlexandre Belloni 646a556c76aSAlexandre Belloni /* Set row and column to read from */ 647a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); 648a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); 649a556c76aSAlexandre Belloni 650a556c76aSAlexandre Belloni /* Issue a read command */ 651a556c76aSAlexandre Belloni ocelot_write(ocelot, 652a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 653a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 654a556c76aSAlexandre Belloni 655a556c76aSAlexandre Belloni if (ocelot_mact_wait_for_completion(ocelot)) 656a556c76aSAlexandre Belloni return -ETIMEDOUT; 657a556c76aSAlexandre Belloni 658a556c76aSAlexandre Belloni /* Read the entry flags */ 659a556c76aSAlexandre Belloni val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 660a556c76aSAlexandre Belloni if (!(val & ANA_TABLES_MACACCESS_VALID)) 661a556c76aSAlexandre Belloni return -EINVAL; 662a556c76aSAlexandre Belloni 663a556c76aSAlexandre Belloni /* If the entry read has another port configured as its destination, 664a556c76aSAlexandre Belloni * do not report it. 665a556c76aSAlexandre Belloni */ 666a556c76aSAlexandre Belloni dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; 667531ee1a6SVladimir Oltean if (dst != port) 668a556c76aSAlexandre Belloni return -EINVAL; 669a556c76aSAlexandre Belloni 670a556c76aSAlexandre Belloni /* Get the entry's MAC address and VLAN id */ 671a556c76aSAlexandre Belloni macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); 672a556c76aSAlexandre Belloni mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); 673a556c76aSAlexandre Belloni 674a556c76aSAlexandre Belloni mac[0] = (mach >> 8) & 0xff; 675a556c76aSAlexandre Belloni mac[1] = (mach >> 0) & 0xff; 676a556c76aSAlexandre Belloni mac[2] = (macl >> 24) & 0xff; 677a556c76aSAlexandre Belloni mac[3] = (macl >> 16) & 0xff; 678a556c76aSAlexandre Belloni mac[4] = (macl >> 8) & 0xff; 679a556c76aSAlexandre Belloni mac[5] = (macl >> 0) & 0xff; 680a556c76aSAlexandre Belloni 681a556c76aSAlexandre Belloni entry->vid = (mach >> 16) & 0xfff; 682a556c76aSAlexandre Belloni ether_addr_copy(entry->mac, mac); 683a556c76aSAlexandre Belloni 684a556c76aSAlexandre Belloni return 0; 685a556c76aSAlexandre Belloni } 686a556c76aSAlexandre Belloni 6875e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port, 688531ee1a6SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 689a556c76aSAlexandre Belloni { 690531ee1a6SVladimir Oltean int i, j; 691a556c76aSAlexandre Belloni 69221ce7f3eSVladimir Oltean /* Loop through all the mac tables entries. */ 69321ce7f3eSVladimir Oltean for (i = 0; i < ocelot->num_mact_rows; i++) { 694a556c76aSAlexandre Belloni for (j = 0; j < 4; j++) { 695531ee1a6SVladimir Oltean struct ocelot_mact_entry entry; 696531ee1a6SVladimir Oltean bool is_static; 697531ee1a6SVladimir Oltean int ret; 698531ee1a6SVladimir Oltean 699531ee1a6SVladimir Oltean ret = ocelot_mact_read(ocelot, port, i, j, &entry); 700a556c76aSAlexandre Belloni /* If the entry is invalid (wrong port, invalid...), 701a556c76aSAlexandre Belloni * skip it. 702a556c76aSAlexandre Belloni */ 703a556c76aSAlexandre Belloni if (ret == -EINVAL) 704a556c76aSAlexandre Belloni continue; 705a556c76aSAlexandre Belloni else if (ret) 706531ee1a6SVladimir Oltean return ret; 707a556c76aSAlexandre Belloni 708531ee1a6SVladimir Oltean is_static = (entry.type == ENTRYTYPE_LOCKED); 709531ee1a6SVladimir Oltean 710531ee1a6SVladimir Oltean ret = cb(entry.mac, entry.vid, is_static, data); 711a556c76aSAlexandre Belloni if (ret) 712531ee1a6SVladimir Oltean return ret; 713a556c76aSAlexandre Belloni } 714a556c76aSAlexandre Belloni } 715a556c76aSAlexandre Belloni 716531ee1a6SVladimir Oltean return 0; 717531ee1a6SVladimir Oltean } 7185e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump); 719531ee1a6SVladimir Oltean 720f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) 7214e3b0468SAntoine Tenart { 7224e3b0468SAntoine Tenart return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, 7234e3b0468SAntoine Tenart sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; 7244e3b0468SAntoine Tenart } 725f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get); 7264e3b0468SAntoine Tenart 727f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) 7284e3b0468SAntoine Tenart { 729306fd44bSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 7304e3b0468SAntoine Tenart struct hwtstamp_config cfg; 7314e3b0468SAntoine Tenart 7324e3b0468SAntoine Tenart if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 7334e3b0468SAntoine Tenart return -EFAULT; 7344e3b0468SAntoine Tenart 7354e3b0468SAntoine Tenart /* reserved for future extensions */ 7364e3b0468SAntoine Tenart if (cfg.flags) 7374e3b0468SAntoine Tenart return -EINVAL; 7384e3b0468SAntoine Tenart 7394e3b0468SAntoine Tenart /* Tx type sanity check */ 7404e3b0468SAntoine Tenart switch (cfg.tx_type) { 7414e3b0468SAntoine Tenart case HWTSTAMP_TX_ON: 742306fd44bSVladimir Oltean ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 7434e3b0468SAntoine Tenart break; 7444e3b0468SAntoine Tenart case HWTSTAMP_TX_ONESTEP_SYNC: 7454e3b0468SAntoine Tenart /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we 7464e3b0468SAntoine Tenart * need to update the origin time. 7474e3b0468SAntoine Tenart */ 748306fd44bSVladimir Oltean ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; 7494e3b0468SAntoine Tenart break; 7504e3b0468SAntoine Tenart case HWTSTAMP_TX_OFF: 751306fd44bSVladimir Oltean ocelot_port->ptp_cmd = 0; 7524e3b0468SAntoine Tenart break; 7534e3b0468SAntoine Tenart default: 7544e3b0468SAntoine Tenart return -ERANGE; 7554e3b0468SAntoine Tenart } 7564e3b0468SAntoine Tenart 7574e3b0468SAntoine Tenart mutex_lock(&ocelot->ptp_lock); 7584e3b0468SAntoine Tenart 7594e3b0468SAntoine Tenart switch (cfg.rx_filter) { 7604e3b0468SAntoine Tenart case HWTSTAMP_FILTER_NONE: 7614e3b0468SAntoine Tenart break; 7624e3b0468SAntoine Tenart case HWTSTAMP_FILTER_ALL: 7634e3b0468SAntoine Tenart case HWTSTAMP_FILTER_SOME: 7644e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 7654e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 7664e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 7674e3b0468SAntoine Tenart case HWTSTAMP_FILTER_NTP_ALL: 7684e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 7694e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 7704e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 7714e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 7724e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 7734e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 7744e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_EVENT: 7754e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_SYNC: 7764e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 7774e3b0468SAntoine Tenart cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 7784e3b0468SAntoine Tenart break; 7794e3b0468SAntoine Tenart default: 7804e3b0468SAntoine Tenart mutex_unlock(&ocelot->ptp_lock); 7814e3b0468SAntoine Tenart return -ERANGE; 7824e3b0468SAntoine Tenart } 7834e3b0468SAntoine Tenart 7844e3b0468SAntoine Tenart /* Commit back the result & save it */ 7854e3b0468SAntoine Tenart memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); 7864e3b0468SAntoine Tenart mutex_unlock(&ocelot->ptp_lock); 7874e3b0468SAntoine Tenart 7884e3b0468SAntoine Tenart return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 7894e3b0468SAntoine Tenart } 790f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set); 7914e3b0468SAntoine Tenart 7925e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) 793a556c76aSAlexandre Belloni { 794a556c76aSAlexandre Belloni int i; 795a556c76aSAlexandre Belloni 796a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 797a556c76aSAlexandre Belloni return; 798a556c76aSAlexandre Belloni 799a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 800a556c76aSAlexandre Belloni memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, 801a556c76aSAlexandre Belloni ETH_GSTRING_LEN); 802a556c76aSAlexandre Belloni } 8035e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings); 804a556c76aSAlexandre Belloni 8051e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot) 806a556c76aSAlexandre Belloni { 807a556c76aSAlexandre Belloni int i, j; 808a556c76aSAlexandre Belloni 809a556c76aSAlexandre Belloni mutex_lock(&ocelot->stats_lock); 810a556c76aSAlexandre Belloni 811a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_phys_ports; i++) { 812a556c76aSAlexandre Belloni /* Configure the port to read the stats from */ 813a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); 814a556c76aSAlexandre Belloni 815a556c76aSAlexandre Belloni for (j = 0; j < ocelot->num_stats; j++) { 816a556c76aSAlexandre Belloni u32 val; 817a556c76aSAlexandre Belloni unsigned int idx = i * ocelot->num_stats + j; 818a556c76aSAlexandre Belloni 819a556c76aSAlexandre Belloni val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, 820a556c76aSAlexandre Belloni ocelot->stats_layout[j].offset); 821a556c76aSAlexandre Belloni 822a556c76aSAlexandre Belloni if (val < (ocelot->stats[idx] & U32_MAX)) 823a556c76aSAlexandre Belloni ocelot->stats[idx] += (u64)1 << 32; 824a556c76aSAlexandre Belloni 825a556c76aSAlexandre Belloni ocelot->stats[idx] = (ocelot->stats[idx] & 826a556c76aSAlexandre Belloni ~(u64)U32_MAX) + val; 827a556c76aSAlexandre Belloni } 828a556c76aSAlexandre Belloni } 829a556c76aSAlexandre Belloni 8301e1caa97SClaudiu Manoil mutex_unlock(&ocelot->stats_lock); 8311e1caa97SClaudiu Manoil } 8321e1caa97SClaudiu Manoil 8331e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work) 8341e1caa97SClaudiu Manoil { 8351e1caa97SClaudiu Manoil struct delayed_work *del_work = to_delayed_work(work); 8361e1caa97SClaudiu Manoil struct ocelot *ocelot = container_of(del_work, struct ocelot, 8371e1caa97SClaudiu Manoil stats_work); 8381e1caa97SClaudiu Manoil 8391e1caa97SClaudiu Manoil ocelot_update_stats(ocelot); 8401e1caa97SClaudiu Manoil 841a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 842a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 843a556c76aSAlexandre Belloni } 844a556c76aSAlexandre Belloni 8455e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) 846a556c76aSAlexandre Belloni { 847a556c76aSAlexandre Belloni int i; 848a556c76aSAlexandre Belloni 849a556c76aSAlexandre Belloni /* check and update now */ 8501e1caa97SClaudiu Manoil ocelot_update_stats(ocelot); 851a556c76aSAlexandre Belloni 852a556c76aSAlexandre Belloni /* Copy all counters */ 853a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 854004d44f6SVladimir Oltean *data++ = ocelot->stats[port * ocelot->num_stats + i]; 855a556c76aSAlexandre Belloni } 8565e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats); 857a556c76aSAlexandre Belloni 8585e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) 859c7282d38SVladimir Oltean { 860a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 861a556c76aSAlexandre Belloni return -EOPNOTSUPP; 862c7282d38SVladimir Oltean 863a556c76aSAlexandre Belloni return ocelot->num_stats; 864a556c76aSAlexandre Belloni } 8655e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count); 866a556c76aSAlexandre Belloni 8675e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port, 868c7282d38SVladimir Oltean struct ethtool_ts_info *info) 869c7282d38SVladimir Oltean { 8704e3b0468SAntoine Tenart info->phc_index = ocelot->ptp_clock ? 8714e3b0468SAntoine Tenart ptp_clock_index(ocelot->ptp_clock) : -1; 872d2b09a8eSYangbo Lu if (info->phc_index == -1) { 873d2b09a8eSYangbo Lu info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 874d2b09a8eSYangbo Lu SOF_TIMESTAMPING_RX_SOFTWARE | 875d2b09a8eSYangbo Lu SOF_TIMESTAMPING_SOFTWARE; 876d2b09a8eSYangbo Lu return 0; 877d2b09a8eSYangbo Lu } 8784e3b0468SAntoine Tenart info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 8794e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_SOFTWARE | 8804e3b0468SAntoine Tenart SOF_TIMESTAMPING_SOFTWARE | 8814e3b0468SAntoine Tenart SOF_TIMESTAMPING_TX_HARDWARE | 8824e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_HARDWARE | 8834e3b0468SAntoine Tenart SOF_TIMESTAMPING_RAW_HARDWARE; 8844e3b0468SAntoine Tenart info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | 8854e3b0468SAntoine Tenart BIT(HWTSTAMP_TX_ONESTEP_SYNC); 8864e3b0468SAntoine Tenart info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); 8874e3b0468SAntoine Tenart 8884e3b0468SAntoine Tenart return 0; 8894e3b0468SAntoine Tenart } 8905e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info); 8914e3b0468SAntoine Tenart 892b80af659SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond) 893b80af659SVladimir Oltean { 894b80af659SVladimir Oltean u32 mask = 0; 895b80af659SVladimir Oltean int port; 896b80af659SVladimir Oltean 897b80af659SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 898b80af659SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 899b80af659SVladimir Oltean 900b80af659SVladimir Oltean if (!ocelot_port) 901b80af659SVladimir Oltean continue; 902b80af659SVladimir Oltean 903b80af659SVladimir Oltean if (ocelot_port->bond == bond) 904b80af659SVladimir Oltean mask |= BIT(port); 905b80af659SVladimir Oltean } 906b80af659SVladimir Oltean 907b80af659SVladimir Oltean return mask; 908b80af659SVladimir Oltean } 909b80af659SVladimir Oltean 910e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot) 9119b521250SVladimir Oltean { 912e21268efSVladimir Oltean u32 mask = 0; 9139b521250SVladimir Oltean int port; 9149b521250SVladimir Oltean 915e21268efSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 916e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 917e21268efSVladimir Oltean 918e21268efSVladimir Oltean if (!ocelot_port) 919e21268efSVladimir Oltean continue; 920e21268efSVladimir Oltean 921e21268efSVladimir Oltean if (ocelot_port->is_dsa_8021q_cpu) 922e21268efSVladimir Oltean mask |= BIT(port); 923e21268efSVladimir Oltean } 924e21268efSVladimir Oltean 925e21268efSVladimir Oltean return mask; 926e21268efSVladimir Oltean } 927e21268efSVladimir Oltean 928e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot) 929e21268efSVladimir Oltean { 930e21268efSVladimir Oltean unsigned long cpu_fwd_mask; 931e21268efSVladimir Oltean int port; 932e21268efSVladimir Oltean 933e21268efSVladimir Oltean /* If a DSA tag_8021q CPU exists, it needs to be included in the 934e21268efSVladimir Oltean * regular forwarding path of the front ports regardless of whether 935e21268efSVladimir Oltean * those are bridged or standalone. 936e21268efSVladimir Oltean * If DSA tag_8021q is not used, this returns 0, which is fine because 937e21268efSVladimir Oltean * the hardware-based CPU port module can be a destination for packets 938e21268efSVladimir Oltean * even if it isn't part of PGID_SRC. 939e21268efSVladimir Oltean */ 940e21268efSVladimir Oltean cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot); 941e21268efSVladimir Oltean 9429b521250SVladimir Oltean /* Apply FWD mask. The loop is needed to add/remove the current port as 9439b521250SVladimir Oltean * a source for the other ports. 9449b521250SVladimir Oltean */ 9459b521250SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 946e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 947e21268efSVladimir Oltean unsigned long mask; 948e21268efSVladimir Oltean 949e21268efSVladimir Oltean if (!ocelot_port) { 950e21268efSVladimir Oltean /* Unused ports can't send anywhere */ 951e21268efSVladimir Oltean mask = 0; 952e21268efSVladimir Oltean } else if (ocelot_port->is_dsa_8021q_cpu) { 953e21268efSVladimir Oltean /* The DSA tag_8021q CPU ports need to be able to 954e21268efSVladimir Oltean * forward packets to all other ports except for 955e21268efSVladimir Oltean * themselves 956e21268efSVladimir Oltean */ 957e21268efSVladimir Oltean mask = GENMASK(ocelot->num_phys_ports - 1, 0); 958e21268efSVladimir Oltean mask &= ~cpu_fwd_mask; 959e21268efSVladimir Oltean } else if (ocelot->bridge_fwd_mask & BIT(port)) { 9609b521250SVladimir Oltean int lag; 9619b521250SVladimir Oltean 962e21268efSVladimir Oltean mask = ocelot->bridge_fwd_mask & ~BIT(port); 963e21268efSVladimir Oltean 9649b521250SVladimir Oltean for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 9659b521250SVladimir Oltean unsigned long bond_mask = ocelot->lags[lag]; 9669b521250SVladimir Oltean 9679b521250SVladimir Oltean if (!bond_mask) 9689b521250SVladimir Oltean continue; 9699b521250SVladimir Oltean 9709b521250SVladimir Oltean if (bond_mask & BIT(port)) { 9719b521250SVladimir Oltean mask &= ~bond_mask; 9729b521250SVladimir Oltean break; 9739b521250SVladimir Oltean } 9749b521250SVladimir Oltean } 9759b521250SVladimir Oltean } else { 976e21268efSVladimir Oltean /* Standalone ports forward only to DSA tag_8021q CPU 977e21268efSVladimir Oltean * ports (if those exist), or to the hardware CPU port 978e21268efSVladimir Oltean * module otherwise. 979e21268efSVladimir Oltean */ 980e21268efSVladimir Oltean mask = cpu_fwd_mask; 981e21268efSVladimir Oltean } 982e21268efSVladimir Oltean 983e21268efSVladimir Oltean ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); 9849b521250SVladimir Oltean } 9859b521250SVladimir Oltean } 986e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask); 9879b521250SVladimir Oltean 9885e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) 989a556c76aSAlexandre Belloni { 990a556c76aSAlexandre Belloni u32 port_cfg; 991a556c76aSAlexandre Belloni 9924bda1415SVladimir Oltean if (!(BIT(port) & ocelot->bridge_mask)) 9934bda1415SVladimir Oltean return; 994a556c76aSAlexandre Belloni 9954bda1415SVladimir Oltean port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); 996a556c76aSAlexandre Belloni 997a556c76aSAlexandre Belloni switch (state) { 998a556c76aSAlexandre Belloni case BR_STATE_FORWARDING: 9994bda1415SVladimir Oltean ocelot->bridge_fwd_mask |= BIT(port); 1000df561f66SGustavo A. R. Silva fallthrough; 1001a556c76aSAlexandre Belloni case BR_STATE_LEARNING: 1002a556c76aSAlexandre Belloni port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA; 1003a556c76aSAlexandre Belloni break; 1004a556c76aSAlexandre Belloni 1005a556c76aSAlexandre Belloni default: 1006a556c76aSAlexandre Belloni port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA; 10074bda1415SVladimir Oltean ocelot->bridge_fwd_mask &= ~BIT(port); 1008a556c76aSAlexandre Belloni break; 1009a556c76aSAlexandre Belloni } 1010a556c76aSAlexandre Belloni 10114bda1415SVladimir Oltean ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port); 1012a556c76aSAlexandre Belloni 10139b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 1014a556c76aSAlexandre Belloni } 10155e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set); 1016a556c76aSAlexandre Belloni 10175e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) 10184bda1415SVladimir Oltean { 1019c0d7eccbSVladimir Oltean unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); 1020c0d7eccbSVladimir Oltean 1021c0d7eccbSVladimir Oltean /* Setting AGE_PERIOD to zero effectively disables automatic aging, 1022c0d7eccbSVladimir Oltean * which is clearly not what our intention is. So avoid that. 1023c0d7eccbSVladimir Oltean */ 1024c0d7eccbSVladimir Oltean if (!age_period) 1025c0d7eccbSVladimir Oltean age_period = 1; 1026c0d7eccbSVladimir Oltean 1027c0d7eccbSVladimir Oltean ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); 1028a556c76aSAlexandre Belloni } 10295e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time); 1030a556c76aSAlexandre Belloni 1031a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 1032a556c76aSAlexandre Belloni const unsigned char *addr, 1033a556c76aSAlexandre Belloni u16 vid) 1034a556c76aSAlexandre Belloni { 1035a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 1036a556c76aSAlexandre Belloni 1037a556c76aSAlexandre Belloni list_for_each_entry(mc, &ocelot->multicast, list) { 1038a556c76aSAlexandre Belloni if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 1039a556c76aSAlexandre Belloni return mc; 1040a556c76aSAlexandre Belloni } 1041a556c76aSAlexandre Belloni 1042a556c76aSAlexandre Belloni return NULL; 1043a556c76aSAlexandre Belloni } 1044a556c76aSAlexandre Belloni 10459403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) 10469403c158SVladimir Oltean { 10479403c158SVladimir Oltean if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) 10489403c158SVladimir Oltean return ENTRYTYPE_MACv4; 10499403c158SVladimir Oltean if (addr[0] == 0x33 && addr[1] == 0x33) 10509403c158SVladimir Oltean return ENTRYTYPE_MACv6; 10517c313143SVladimir Oltean return ENTRYTYPE_LOCKED; 10529403c158SVladimir Oltean } 10539403c158SVladimir Oltean 1054e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, 1055e5d1f896SVladimir Oltean unsigned long ports) 1056e5d1f896SVladimir Oltean { 1057e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1058e5d1f896SVladimir Oltean 1059e5d1f896SVladimir Oltean pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); 1060e5d1f896SVladimir Oltean if (!pgid) 1061e5d1f896SVladimir Oltean return ERR_PTR(-ENOMEM); 1062e5d1f896SVladimir Oltean 1063e5d1f896SVladimir Oltean pgid->ports = ports; 1064e5d1f896SVladimir Oltean pgid->index = index; 1065e5d1f896SVladimir Oltean refcount_set(&pgid->refcount, 1); 1066e5d1f896SVladimir Oltean list_add_tail(&pgid->list, &ocelot->pgids); 1067e5d1f896SVladimir Oltean 1068e5d1f896SVladimir Oltean return pgid; 1069e5d1f896SVladimir Oltean } 1070e5d1f896SVladimir Oltean 1071e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) 1072e5d1f896SVladimir Oltean { 1073e5d1f896SVladimir Oltean if (!refcount_dec_and_test(&pgid->refcount)) 1074e5d1f896SVladimir Oltean return; 1075e5d1f896SVladimir Oltean 1076e5d1f896SVladimir Oltean list_del(&pgid->list); 1077e5d1f896SVladimir Oltean kfree(pgid); 1078e5d1f896SVladimir Oltean } 1079e5d1f896SVladimir Oltean 1080e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, 1081bb8d53fdSVladimir Oltean const struct ocelot_multicast *mc) 10829403c158SVladimir Oltean { 1083e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1084e5d1f896SVladimir Oltean int index; 10859403c158SVladimir Oltean 10869403c158SVladimir Oltean /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and 10879403c158SVladimir Oltean * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the 10889403c158SVladimir Oltean * destination mask table (PGID), the destination set is programmed as 10899403c158SVladimir Oltean * part of the entry MAC address.", and the DEST_IDX is set to 0. 10909403c158SVladimir Oltean */ 1091bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4 || 1092bb8d53fdSVladimir Oltean mc->entry_type == ENTRYTYPE_MACv6) 1093e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, 0, mc->ports); 10949403c158SVladimir Oltean 1095e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 1096e5d1f896SVladimir Oltean /* When searching for a nonreserved multicast PGID, ignore the 1097e5d1f896SVladimir Oltean * dummy PGID of zero that we have for MACv4/MACv6 entries 1098e5d1f896SVladimir Oltean */ 1099e5d1f896SVladimir Oltean if (pgid->index && pgid->ports == mc->ports) { 1100e5d1f896SVladimir Oltean refcount_inc(&pgid->refcount); 1101e5d1f896SVladimir Oltean return pgid; 1102e5d1f896SVladimir Oltean } 1103e5d1f896SVladimir Oltean } 1104e5d1f896SVladimir Oltean 1105e5d1f896SVladimir Oltean /* Search for a free index in the nonreserved multicast PGID area */ 1106e5d1f896SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, index) { 11079403c158SVladimir Oltean bool used = false; 11089403c158SVladimir Oltean 1109e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 1110e5d1f896SVladimir Oltean if (pgid->index == index) { 11119403c158SVladimir Oltean used = true; 11129403c158SVladimir Oltean break; 11139403c158SVladimir Oltean } 11149403c158SVladimir Oltean } 11159403c158SVladimir Oltean 11169403c158SVladimir Oltean if (!used) 1117e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, index, mc->ports); 11189403c158SVladimir Oltean } 11199403c158SVladimir Oltean 1120e5d1f896SVladimir Oltean return ERR_PTR(-ENOSPC); 11219403c158SVladimir Oltean } 11229403c158SVladimir Oltean 11239403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr, 1124bb8d53fdSVladimir Oltean struct ocelot_multicast *mc) 11259403c158SVladimir Oltean { 1126ebbd860eSVladimir Oltean ether_addr_copy(addr, mc->addr); 11279403c158SVladimir Oltean 1128bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4) { 11299403c158SVladimir Oltean addr[0] = 0; 11309403c158SVladimir Oltean addr[1] = mc->ports >> 8; 11319403c158SVladimir Oltean addr[2] = mc->ports & 0xff; 1132bb8d53fdSVladimir Oltean } else if (mc->entry_type == ENTRYTYPE_MACv6) { 11339403c158SVladimir Oltean addr[0] = mc->ports >> 8; 11349403c158SVladimir Oltean addr[1] = mc->ports & 0xff; 11359403c158SVladimir Oltean } 11369403c158SVladimir Oltean } 11379403c158SVladimir Oltean 1138209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port, 1139209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1140a556c76aSAlexandre Belloni { 1141a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 1142004d44f6SVladimir Oltean struct ocelot_multicast *mc; 1143e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1144a556c76aSAlexandre Belloni u16 vid = mdb->vid; 1145a556c76aSAlexandre Belloni 1146471beb11SVladimir Oltean if (port == ocelot->npi) 1147471beb11SVladimir Oltean port = ocelot->num_phys_ports; 1148471beb11SVladimir Oltean 1149a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1150a556c76aSAlexandre Belloni if (!mc) { 1151728e69aeSVladimir Oltean /* New entry */ 1152bb8d53fdSVladimir Oltean mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 1153bb8d53fdSVladimir Oltean if (!mc) 1154bb8d53fdSVladimir Oltean return -ENOMEM; 1155bb8d53fdSVladimir Oltean 1156bb8d53fdSVladimir Oltean mc->entry_type = ocelot_classify_mdb(mdb->addr); 1157bb8d53fdSVladimir Oltean ether_addr_copy(mc->addr, mdb->addr); 1158bb8d53fdSVladimir Oltean mc->vid = vid; 1159bb8d53fdSVladimir Oltean 1160a556c76aSAlexandre Belloni list_add_tail(&mc->list, &ocelot->multicast); 1161728e69aeSVladimir Oltean } else { 1162e5d1f896SVladimir Oltean /* Existing entry. Clean up the current port mask from 1163e5d1f896SVladimir Oltean * hardware now, because we'll be modifying it. 1164e5d1f896SVladimir Oltean */ 1165e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 1166bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1167a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 1168a556c76aSAlexandre Belloni } 1169a556c76aSAlexandre Belloni 1170004d44f6SVladimir Oltean mc->ports |= BIT(port); 1171e5d1f896SVladimir Oltean 1172e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 1173e5d1f896SVladimir Oltean if (IS_ERR(pgid)) { 1174e5d1f896SVladimir Oltean dev_err(ocelot->dev, 1175e5d1f896SVladimir Oltean "Cannot allocate PGID for mdb %pM vid %d\n", 1176e5d1f896SVladimir Oltean mc->addr, mc->vid); 1177e5d1f896SVladimir Oltean devm_kfree(ocelot->dev, mc); 1178e5d1f896SVladimir Oltean return PTR_ERR(pgid); 1179e5d1f896SVladimir Oltean } 1180e5d1f896SVladimir Oltean mc->pgid = pgid; 1181e5d1f896SVladimir Oltean 1182bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1183a556c76aSAlexandre Belloni 1184e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 1185e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 1186e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1187e5d1f896SVladimir Oltean pgid->index); 1188e5d1f896SVladimir Oltean 1189e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1190bb8d53fdSVladimir Oltean mc->entry_type); 1191a556c76aSAlexandre Belloni } 1192209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add); 1193a556c76aSAlexandre Belloni 1194209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port, 1195a556c76aSAlexandre Belloni const struct switchdev_obj_port_mdb *mdb) 1196a556c76aSAlexandre Belloni { 1197a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 1198004d44f6SVladimir Oltean struct ocelot_multicast *mc; 1199e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1200a556c76aSAlexandre Belloni u16 vid = mdb->vid; 1201a556c76aSAlexandre Belloni 1202471beb11SVladimir Oltean if (port == ocelot->npi) 1203471beb11SVladimir Oltean port = ocelot->num_phys_ports; 1204471beb11SVladimir Oltean 1205a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1206a556c76aSAlexandre Belloni if (!mc) 1207a556c76aSAlexandre Belloni return -ENOENT; 1208a556c76aSAlexandre Belloni 1209bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1210a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 1211a556c76aSAlexandre Belloni 1212e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 1213004d44f6SVladimir Oltean mc->ports &= ~BIT(port); 1214a556c76aSAlexandre Belloni if (!mc->ports) { 1215a556c76aSAlexandre Belloni list_del(&mc->list); 1216a556c76aSAlexandre Belloni devm_kfree(ocelot->dev, mc); 1217a556c76aSAlexandre Belloni return 0; 1218a556c76aSAlexandre Belloni } 1219a556c76aSAlexandre Belloni 1220e5d1f896SVladimir Oltean /* We have a PGID with fewer ports now */ 1221e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 1222e5d1f896SVladimir Oltean if (IS_ERR(pgid)) 1223e5d1f896SVladimir Oltean return PTR_ERR(pgid); 1224e5d1f896SVladimir Oltean mc->pgid = pgid; 1225e5d1f896SVladimir Oltean 1226bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1227a556c76aSAlexandre Belloni 1228e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 1229e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 1230e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1231e5d1f896SVladimir Oltean pgid->index); 1232e5d1f896SVladimir Oltean 1233e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1234bb8d53fdSVladimir Oltean mc->entry_type); 1235a556c76aSAlexandre Belloni } 1236209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del); 1237a556c76aSAlexandre Belloni 12385e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port, 1239a556c76aSAlexandre Belloni struct net_device *bridge) 1240a556c76aSAlexandre Belloni { 1241a556c76aSAlexandre Belloni if (!ocelot->bridge_mask) { 1242a556c76aSAlexandre Belloni ocelot->hw_bridge_dev = bridge; 1243a556c76aSAlexandre Belloni } else { 1244a556c76aSAlexandre Belloni if (ocelot->hw_bridge_dev != bridge) 1245a556c76aSAlexandre Belloni /* This is adding the port to a second bridge, this is 1246a556c76aSAlexandre Belloni * unsupported */ 1247a556c76aSAlexandre Belloni return -ENODEV; 1248a556c76aSAlexandre Belloni } 1249a556c76aSAlexandre Belloni 1250f270dbfaSVladimir Oltean ocelot->bridge_mask |= BIT(port); 1251a556c76aSAlexandre Belloni 1252a556c76aSAlexandre Belloni return 0; 1253a556c76aSAlexandre Belloni } 12545e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join); 1255a556c76aSAlexandre Belloni 12565e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port, 1257a556c76aSAlexandre Belloni struct net_device *bridge) 1258a556c76aSAlexandre Belloni { 1259c3e58a75SVladimir Oltean struct ocelot_vlan pvid = {0}, native_vlan = {0}; 12602e554a7aSVladimir Oltean int ret; 12612e554a7aSVladimir Oltean 126297bb69e1SVladimir Oltean ocelot->bridge_mask &= ~BIT(port); 1263a556c76aSAlexandre Belloni 1264a556c76aSAlexandre Belloni if (!ocelot->bridge_mask) 1265a556c76aSAlexandre Belloni ocelot->hw_bridge_dev = NULL; 12667142529fSAntoine Tenart 1267bae33f2bSVladimir Oltean ret = ocelot_port_vlan_filtering(ocelot, port, false); 12682e554a7aSVladimir Oltean if (ret) 12692e554a7aSVladimir Oltean return ret; 12702e554a7aSVladimir Oltean 1271c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, pvid); 12722f0402feSVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, native_vlan); 12732f0402feSVladimir Oltean 12742f0402feSVladimir Oltean return 0; 1275a556c76aSAlexandre Belloni } 12765e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave); 1277a556c76aSAlexandre Belloni 1278dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 1279dc96ee37SAlexandre Belloni { 1280dc96ee37SAlexandre Belloni int i, port, lag; 1281dc96ee37SAlexandre Belloni 1282dc96ee37SAlexandre Belloni /* Reset destination and aggregation PGIDS */ 128396b029b0SVladimir Oltean for_each_unicast_dest_pgid(ocelot, port) 1284dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1285dc96ee37SAlexandre Belloni 128696b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) 1287dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 1288dc96ee37SAlexandre Belloni ANA_PGID_PGID, i); 1289dc96ee37SAlexandre Belloni 1290dc96ee37SAlexandre Belloni /* Now, set PGIDs for each LAG */ 1291dc96ee37SAlexandre Belloni for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 1292dc96ee37SAlexandre Belloni unsigned long bond_mask; 1293dc96ee37SAlexandre Belloni int aggr_count = 0; 1294dc96ee37SAlexandre Belloni u8 aggr_idx[16]; 1295dc96ee37SAlexandre Belloni 1296dc96ee37SAlexandre Belloni bond_mask = ocelot->lags[lag]; 1297dc96ee37SAlexandre Belloni if (!bond_mask) 1298dc96ee37SAlexandre Belloni continue; 1299dc96ee37SAlexandre Belloni 1300dc96ee37SAlexandre Belloni for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 1301dc96ee37SAlexandre Belloni // Destination mask 1302dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, bond_mask, 1303dc96ee37SAlexandre Belloni ANA_PGID_PGID, port); 1304dc96ee37SAlexandre Belloni aggr_idx[aggr_count] = port; 1305dc96ee37SAlexandre Belloni aggr_count++; 1306dc96ee37SAlexandre Belloni } 1307dc96ee37SAlexandre Belloni 130896b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) { 1309dc96ee37SAlexandre Belloni u32 ac; 1310dc96ee37SAlexandre Belloni 1311dc96ee37SAlexandre Belloni ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 1312dc96ee37SAlexandre Belloni ac &= ~bond_mask; 1313dc96ee37SAlexandre Belloni ac |= BIT(aggr_idx[i % aggr_count]); 1314dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 1315dc96ee37SAlexandre Belloni } 1316dc96ee37SAlexandre Belloni } 1317dc96ee37SAlexandre Belloni } 1318dc96ee37SAlexandre Belloni 1319*2527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the 1320*2527f2e8SVladimir Oltean * same bond must have the same logical port ID, equal to the physical port ID 1321*2527f2e8SVladimir Oltean * of the lowest numbered physical port in that bond. Otherwise, in standalone/ 1322*2527f2e8SVladimir Oltean * bridged mode, each port has a logical port ID equal to its physical port ID. 1323*2527f2e8SVladimir Oltean */ 1324*2527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) 1325dc96ee37SAlexandre Belloni { 1326*2527f2e8SVladimir Oltean int port; 1327dc96ee37SAlexandre Belloni 1328*2527f2e8SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1329*2527f2e8SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1330*2527f2e8SVladimir Oltean struct net_device *bond; 1331dc96ee37SAlexandre Belloni 1332*2527f2e8SVladimir Oltean if (!ocelot_port) 1333*2527f2e8SVladimir Oltean continue; 1334dc96ee37SAlexandre Belloni 1335*2527f2e8SVladimir Oltean bond = ocelot_port->bond; 1336*2527f2e8SVladimir Oltean if (bond) { 1337*2527f2e8SVladimir Oltean int lag = __ffs(ocelot_get_bond_mask(ocelot, bond)); 1338*2527f2e8SVladimir Oltean 1339*2527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 1340dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(lag), 1341*2527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 1342*2527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 1343*2527f2e8SVladimir Oltean } else { 1344*2527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 1345*2527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 1346*2527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 1347*2527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 1348*2527f2e8SVladimir Oltean } 1349dc96ee37SAlexandre Belloni } 1350dc96ee37SAlexandre Belloni } 1351dc96ee37SAlexandre Belloni 13529c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port, 1353583cbbe3SVladimir Oltean struct net_device *bond, 1354583cbbe3SVladimir Oltean struct netdev_lag_upper_info *info) 1355dc96ee37SAlexandre Belloni { 1356dc96ee37SAlexandre Belloni u32 bond_mask = 0; 13572e9f4afaSVladimir Oltean int lag; 1358dc96ee37SAlexandre Belloni 1359583cbbe3SVladimir Oltean if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) 1360583cbbe3SVladimir Oltean return -EOPNOTSUPP; 1361583cbbe3SVladimir Oltean 1362b80af659SVladimir Oltean ocelot->ports[port]->bond = bond; 1363dc96ee37SAlexandre Belloni 1364b80af659SVladimir Oltean bond_mask = ocelot_get_bond_mask(ocelot, bond); 1365dc96ee37SAlexandre Belloni 13662e9f4afaSVladimir Oltean lag = __ffs(bond_mask); 1367dc96ee37SAlexandre Belloni 1368dc96ee37SAlexandre Belloni /* If the new port is the lowest one, use it as the logical port from 1369dc96ee37SAlexandre Belloni * now on 1370dc96ee37SAlexandre Belloni */ 13712e9f4afaSVladimir Oltean if (port == lag) { 1372f270dbfaSVladimir Oltean ocelot->lags[port] = bond_mask; 1373f270dbfaSVladimir Oltean bond_mask &= ~BIT(port); 13742e9f4afaSVladimir Oltean if (bond_mask) 13752e9f4afaSVladimir Oltean ocelot->lags[__ffs(bond_mask)] = 0; 1376dc96ee37SAlexandre Belloni } else { 13772e9f4afaSVladimir Oltean ocelot->lags[lag] |= BIT(port); 1378dc96ee37SAlexandre Belloni } 1379dc96ee37SAlexandre Belloni 1380*2527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 13819b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 1382dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 1383dc96ee37SAlexandre Belloni 1384dc96ee37SAlexandre Belloni return 0; 1385dc96ee37SAlexandre Belloni } 13869c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join); 1387dc96ee37SAlexandre Belloni 13889c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port, 1389dc96ee37SAlexandre Belloni struct net_device *bond) 1390dc96ee37SAlexandre Belloni { 1391dc96ee37SAlexandre Belloni int i; 1392dc96ee37SAlexandre Belloni 1393b80af659SVladimir Oltean ocelot->ports[port]->bond = NULL; 1394b80af659SVladimir Oltean 1395dc96ee37SAlexandre Belloni /* Remove port from any lag */ 1396dc96ee37SAlexandre Belloni for (i = 0; i < ocelot->num_phys_ports; i++) 1397f270dbfaSVladimir Oltean ocelot->lags[i] &= ~BIT(port); 1398dc96ee37SAlexandre Belloni 1399dc96ee37SAlexandre Belloni /* if it was the logical port of the lag, move the lag config to the 1400dc96ee37SAlexandre Belloni * next port 1401dc96ee37SAlexandre Belloni */ 1402f270dbfaSVladimir Oltean if (ocelot->lags[port]) { 1403f270dbfaSVladimir Oltean int n = __ffs(ocelot->lags[port]); 1404dc96ee37SAlexandre Belloni 1405f270dbfaSVladimir Oltean ocelot->lags[n] = ocelot->lags[port]; 1406f270dbfaSVladimir Oltean ocelot->lags[port] = 0; 1407dc96ee37SAlexandre Belloni } 1408dc96ee37SAlexandre Belloni 1409*2527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 14109b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 1411dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 1412dc96ee37SAlexandre Belloni } 14139c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave); 14140e332c85SPetr Machata 1415a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. 1416a8015dedSVladimir Oltean * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. 14170b912fc9SVladimir Oltean * In the special case that it's the NPI port that we're configuring, the 14180b912fc9SVladimir Oltean * length of the tag and optional prefix needs to be accounted for privately, 14190b912fc9SVladimir Oltean * in order to be able to sustain communication at the requested @sdu. 1420a8015dedSVladimir Oltean */ 14210b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) 142231350d7fSVladimir Oltean { 142331350d7fSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1424a8015dedSVladimir Oltean int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; 1425e8e6e73dSVladimir Oltean int pause_start, pause_stop; 1426601e984fSVladimir Oltean int atop, atop_tot; 142731350d7fSVladimir Oltean 14280b912fc9SVladimir Oltean if (port == ocelot->npi) { 14290b912fc9SVladimir Oltean maxlen += OCELOT_TAG_LEN; 14300b912fc9SVladimir Oltean 1431cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 14320b912fc9SVladimir Oltean maxlen += OCELOT_SHORT_PREFIX_LEN; 1433cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 14340b912fc9SVladimir Oltean maxlen += OCELOT_LONG_PREFIX_LEN; 14350b912fc9SVladimir Oltean } 14360b912fc9SVladimir Oltean 1437a8015dedSVladimir Oltean ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); 1438fa914e9cSVladimir Oltean 1439e8e6e73dSVladimir Oltean /* Set Pause watermark hysteresis */ 1440e8e6e73dSVladimir Oltean pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; 1441e8e6e73dSVladimir Oltean pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; 1442541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, 1443541132f0SMaxim Kochetkov pause_start); 1444541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, 1445541132f0SMaxim Kochetkov pause_stop); 1446fa914e9cSVladimir Oltean 1447601e984fSVladimir Oltean /* Tail dropping watermarks */ 1448f6fe01d6SVladimir Oltean atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / 1449a8015dedSVladimir Oltean OCELOT_BUFFER_CELL_SZ; 1450601e984fSVladimir Oltean atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; 1451601e984fSVladimir Oltean ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); 1452601e984fSVladimir Oltean ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); 1453fa914e9cSVladimir Oltean } 14540b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen); 14550b912fc9SVladimir Oltean 14560b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port) 14570b912fc9SVladimir Oltean { 14580b912fc9SVladimir Oltean int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; 14590b912fc9SVladimir Oltean 14600b912fc9SVladimir Oltean if (port == ocelot->npi) { 14610b912fc9SVladimir Oltean max_mtu -= OCELOT_TAG_LEN; 14620b912fc9SVladimir Oltean 1463cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 14640b912fc9SVladimir Oltean max_mtu -= OCELOT_SHORT_PREFIX_LEN; 1465cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 14660b912fc9SVladimir Oltean max_mtu -= OCELOT_LONG_PREFIX_LEN; 14670b912fc9SVladimir Oltean } 14680b912fc9SVladimir Oltean 14690b912fc9SVladimir Oltean return max_mtu; 14700b912fc9SVladimir Oltean } 14710b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu); 1472fa914e9cSVladimir Oltean 14735e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port) 1474fa914e9cSVladimir Oltean { 1475fa914e9cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1476fa914e9cSVladimir Oltean 1477b049da13SYangbo Lu skb_queue_head_init(&ocelot_port->tx_skbs); 14786565243cSVladimir Oltean spin_lock_init(&ocelot_port->ts_id_lock); 147931350d7fSVladimir Oltean 148031350d7fSVladimir Oltean /* Basic L2 initialization */ 148131350d7fSVladimir Oltean 14825bc9d2e6SVladimir Oltean /* Set MAC IFG Gaps 14835bc9d2e6SVladimir Oltean * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 14845bc9d2e6SVladimir Oltean * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 14855bc9d2e6SVladimir Oltean */ 14865bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), 14875bc9d2e6SVladimir Oltean DEV_MAC_IFG_CFG); 14885bc9d2e6SVladimir Oltean 14895bc9d2e6SVladimir Oltean /* Load seed (0) and set MAC HDX late collision */ 14905bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 14915bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG_SEED_LOAD, 14925bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 14935bc9d2e6SVladimir Oltean mdelay(1); 14945bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 14955bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 14965bc9d2e6SVladimir Oltean 14975bc9d2e6SVladimir Oltean /* Set Max Length and maximum tags allowed */ 1498a8015dedSVladimir Oltean ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); 14995bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 15005bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 1501a8015dedSVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | 15025bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 15035bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG); 15045bc9d2e6SVladimir Oltean 15055bc9d2e6SVladimir Oltean /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 15065bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 15075bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); 15085bc9d2e6SVladimir Oltean 1509e8e6e73dSVladimir Oltean /* Enable transmission of pause frames */ 1510541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 1511e8e6e73dSVladimir Oltean 151231350d7fSVladimir Oltean /* Drop frames with multicast source address */ 151331350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 151431350d7fSVladimir Oltean ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 151531350d7fSVladimir Oltean ANA_PORT_DROP_CFG, port); 151631350d7fSVladimir Oltean 151731350d7fSVladimir Oltean /* Set default VLAN and tag type to 8021Q. */ 151831350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), 151931350d7fSVladimir Oltean REW_PORT_VLAN_CFG_PORT_TPID_M, 152031350d7fSVladimir Oltean REW_PORT_VLAN_CFG, port); 152131350d7fSVladimir Oltean 152231350d7fSVladimir Oltean /* Enable vcap lookups */ 152331350d7fSVladimir Oltean ocelot_vcap_enable(ocelot, port); 152431350d7fSVladimir Oltean } 15255e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port); 152631350d7fSVladimir Oltean 15272d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues 15282d44b097SVladimir Oltean * accessible through register MMIO, frame DMA or Ethernet (in case 15292d44b097SVladimir Oltean * NPI mode is used). 153069df578cSVladimir Oltean */ 15312d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot) 153221468199SVladimir Oltean { 153369df578cSVladimir Oltean int cpu = ocelot->num_phys_ports; 153469df578cSVladimir Oltean 153569df578cSVladimir Oltean /* The unicast destination PGID for the CPU port module is unused */ 153621468199SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 153769df578cSVladimir Oltean /* Instead set up a multicast destination PGID for traffic copied to 153869df578cSVladimir Oltean * the CPU. Whitelisted MAC addresses like the port netdevice MAC 153969df578cSVladimir Oltean * addresses will be copied to the CPU via this PGID. 154069df578cSVladimir Oltean */ 154121468199SVladimir Oltean ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 154221468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 154321468199SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 154421468199SVladimir Oltean ANA_PORT_PORT_CFG, cpu); 154521468199SVladimir Oltean 154669df578cSVladimir Oltean /* Enable CPU port module */ 1547886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 154869df578cSVladimir Oltean /* CPU port Injection/Extraction configuration */ 1549886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, 1550cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 1551886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, 1552cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 155321468199SVladimir Oltean 155421468199SVladimir Oltean /* Configure the CPU port to be VLAN aware */ 155521468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) | 155621468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 155721468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 155821468199SVladimir Oltean ANA_PORT_VLAN_CFG, cpu); 155921468199SVladimir Oltean } 156021468199SVladimir Oltean 1561f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot) 1562f6fe01d6SVladimir Oltean { 1563f6fe01d6SVladimir Oltean int mmgt, eq_ctrl; 1564f6fe01d6SVladimir Oltean 1565f6fe01d6SVladimir Oltean /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds 1566f6fe01d6SVladimir Oltean * the number of 240-byte free memory words (aka 4-cell chunks) and not 1567f6fe01d6SVladimir Oltean * 192 bytes as the documentation incorrectly says. 1568f6fe01d6SVladimir Oltean */ 1569f6fe01d6SVladimir Oltean mmgt = ocelot_read(ocelot, SYS_MMGT); 1570f6fe01d6SVladimir Oltean ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); 1571f6fe01d6SVladimir Oltean 1572f6fe01d6SVladimir Oltean eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); 1573f6fe01d6SVladimir Oltean ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); 1574f6fe01d6SVladimir Oltean } 1575f6fe01d6SVladimir Oltean 1576a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot) 1577a556c76aSAlexandre Belloni { 1578a556c76aSAlexandre Belloni char queue_name[32]; 157921468199SVladimir Oltean int i, ret; 158021468199SVladimir Oltean u32 port; 1581a556c76aSAlexandre Belloni 15823a77b593SVladimir Oltean if (ocelot->ops->reset) { 15833a77b593SVladimir Oltean ret = ocelot->ops->reset(ocelot); 15843a77b593SVladimir Oltean if (ret) { 15853a77b593SVladimir Oltean dev_err(ocelot->dev, "Switch reset failed\n"); 15863a77b593SVladimir Oltean return ret; 15873a77b593SVladimir Oltean } 15883a77b593SVladimir Oltean } 15893a77b593SVladimir Oltean 1590dc96ee37SAlexandre Belloni ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports, 1591dc96ee37SAlexandre Belloni sizeof(u32), GFP_KERNEL); 1592dc96ee37SAlexandre Belloni if (!ocelot->lags) 1593dc96ee37SAlexandre Belloni return -ENOMEM; 1594dc96ee37SAlexandre Belloni 1595a556c76aSAlexandre Belloni ocelot->stats = devm_kcalloc(ocelot->dev, 1596a556c76aSAlexandre Belloni ocelot->num_phys_ports * ocelot->num_stats, 1597a556c76aSAlexandre Belloni sizeof(u64), GFP_KERNEL); 1598a556c76aSAlexandre Belloni if (!ocelot->stats) 1599a556c76aSAlexandre Belloni return -ENOMEM; 1600a556c76aSAlexandre Belloni 1601a556c76aSAlexandre Belloni mutex_init(&ocelot->stats_lock); 16024e3b0468SAntoine Tenart mutex_init(&ocelot->ptp_lock); 16034e3b0468SAntoine Tenart spin_lock_init(&ocelot->ptp_clock_lock); 1604a556c76aSAlexandre Belloni snprintf(queue_name, sizeof(queue_name), "%s-stats", 1605a556c76aSAlexandre Belloni dev_name(ocelot->dev)); 1606a556c76aSAlexandre Belloni ocelot->stats_queue = create_singlethread_workqueue(queue_name); 1607a556c76aSAlexandre Belloni if (!ocelot->stats_queue) 1608a556c76aSAlexandre Belloni return -ENOMEM; 1609a556c76aSAlexandre Belloni 1610ca0b272bSVladimir Oltean ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); 1611ca0b272bSVladimir Oltean if (!ocelot->owq) { 1612ca0b272bSVladimir Oltean destroy_workqueue(ocelot->stats_queue); 1613ca0b272bSVladimir Oltean return -ENOMEM; 1614ca0b272bSVladimir Oltean } 1615ca0b272bSVladimir Oltean 16162b120ddeSClaudiu Manoil INIT_LIST_HEAD(&ocelot->multicast); 1617e5d1f896SVladimir Oltean INIT_LIST_HEAD(&ocelot->pgids); 1618f6fe01d6SVladimir Oltean ocelot_detect_features(ocelot); 1619a556c76aSAlexandre Belloni ocelot_mact_init(ocelot); 1620a556c76aSAlexandre Belloni ocelot_vlan_init(ocelot); 1621aae4e500SVladimir Oltean ocelot_vcap_init(ocelot); 16222d44b097SVladimir Oltean ocelot_cpu_port_init(ocelot); 1623a556c76aSAlexandre Belloni 1624a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 1625a556c76aSAlexandre Belloni /* Clear all counters (5 groups) */ 1626a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 1627a556c76aSAlexandre Belloni SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 1628a556c76aSAlexandre Belloni SYS_STAT_CFG); 1629a556c76aSAlexandre Belloni } 1630a556c76aSAlexandre Belloni 1631a556c76aSAlexandre Belloni /* Only use S-Tag */ 1632a556c76aSAlexandre Belloni ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 1633a556c76aSAlexandre Belloni 1634a556c76aSAlexandre Belloni /* Aggregation mode */ 1635a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 1636a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_DMAC_ENA | 1637a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 1638f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | 1639f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | 1640f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, 1641f79c20c8SVladimir Oltean ANA_AGGR_CFG); 1642a556c76aSAlexandre Belloni 1643a556c76aSAlexandre Belloni /* Set MAC age time to default value. The entry is aged after 1644a556c76aSAlexandre Belloni * 2*AGE_PERIOD 1645a556c76aSAlexandre Belloni */ 1646a556c76aSAlexandre Belloni ocelot_write(ocelot, 1647a556c76aSAlexandre Belloni ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 1648a556c76aSAlexandre Belloni ANA_AUTOAGE); 1649a556c76aSAlexandre Belloni 1650a556c76aSAlexandre Belloni /* Disable learning for frames discarded by VLAN ingress filtering */ 1651a556c76aSAlexandre Belloni regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 1652a556c76aSAlexandre Belloni 1653a556c76aSAlexandre Belloni /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 1654a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 1655a556c76aSAlexandre Belloni SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 1656a556c76aSAlexandre Belloni 1657a556c76aSAlexandre Belloni /* Setup flooding PGIDs */ 1658edd2410bSVladimir Oltean for (i = 0; i < ocelot->num_flooding_pgids; i++) 1659a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 1660a556c76aSAlexandre Belloni ANA_FLOODING_FLD_BROADCAST(PGID_MC) | 1661a556c76aSAlexandre Belloni ANA_FLOODING_FLD_UNICAST(PGID_UC), 1662edd2410bSVladimir Oltean ANA_FLOODING, i); 1663a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 1664a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 1665a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 1666a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 1667a556c76aSAlexandre Belloni ANA_FLOODING_IPMC); 1668a556c76aSAlexandre Belloni 1669a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 1670a556c76aSAlexandre Belloni /* Transmit the frame to the local port. */ 1671a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1672a556c76aSAlexandre Belloni /* Do not forward BPDU frames to the front ports. */ 1673a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, 1674a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 1675a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG, 1676a556c76aSAlexandre Belloni port); 1677a556c76aSAlexandre Belloni /* Ensure bridging is disabled */ 1678a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 1679a556c76aSAlexandre Belloni } 1680a556c76aSAlexandre Belloni 1681a556c76aSAlexandre Belloni /* Allow broadcast MAC frames. */ 168296b029b0SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, i) { 1683a556c76aSAlexandre Belloni u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 1684a556c76aSAlexandre Belloni 1685a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 1686a556c76aSAlexandre Belloni } 1687a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 1688a556c76aSAlexandre Belloni ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)), 1689a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_MC); 1690a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 1691a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 1692a556c76aSAlexandre Belloni 1693a556c76aSAlexandre Belloni /* Allow manual injection via DEVCPU_QS registers, and byte swap these 1694a556c76aSAlexandre Belloni * registers endianness. 1695a556c76aSAlexandre Belloni */ 1696a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 1697a556c76aSAlexandre Belloni QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 1698a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 1699a556c76aSAlexandre Belloni QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 1700a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 1701a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LRN(2) | 1702a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 1703a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 1704a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 1705a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 1706a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 1707a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IGMP(6) | 1708a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 1709a556c76aSAlexandre Belloni for (i = 0; i < 16; i++) 1710a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 1711a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 1712a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG, i); 1713a556c76aSAlexandre Belloni 17141e1caa97SClaudiu Manoil INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 1715a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1716a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 17174e3b0468SAntoine Tenart 1718a556c76aSAlexandre Belloni return 0; 1719a556c76aSAlexandre Belloni } 1720a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init); 1721a556c76aSAlexandre Belloni 1722a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot) 1723a556c76aSAlexandre Belloni { 1724c5d13969SClaudiu Manoil cancel_delayed_work(&ocelot->stats_work); 1725a556c76aSAlexandre Belloni destroy_workqueue(ocelot->stats_queue); 1726ca0b272bSVladimir Oltean destroy_workqueue(ocelot->owq); 1727a556c76aSAlexandre Belloni mutex_destroy(&ocelot->stats_lock); 1728a556c76aSAlexandre Belloni } 1729a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit); 1730a556c76aSAlexandre Belloni 1731e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port) 1732e5fb512dSVladimir Oltean { 1733e5fb512dSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1734e5fb512dSVladimir Oltean 1735e5fb512dSVladimir Oltean skb_queue_purge(&ocelot_port->tx_skbs); 1736e5fb512dSVladimir Oltean } 1737e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port); 1738e5fb512dSVladimir Oltean 1739a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL"); 1740