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 892e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot) 8939b521250SVladimir Oltean { 894e21268efSVladimir Oltean u32 mask = 0; 8959b521250SVladimir Oltean int port; 8969b521250SVladimir Oltean 897e21268efSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 898e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 899e21268efSVladimir Oltean 900e21268efSVladimir Oltean if (!ocelot_port) 901e21268efSVladimir Oltean continue; 902e21268efSVladimir Oltean 903e21268efSVladimir Oltean if (ocelot_port->is_dsa_8021q_cpu) 904e21268efSVladimir Oltean mask |= BIT(port); 905e21268efSVladimir Oltean } 906e21268efSVladimir Oltean 907e21268efSVladimir Oltean return mask; 908e21268efSVladimir Oltean } 909e21268efSVladimir Oltean 910e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot) 911e21268efSVladimir Oltean { 912e21268efSVladimir Oltean unsigned long cpu_fwd_mask; 913e21268efSVladimir Oltean int port; 914e21268efSVladimir Oltean 915e21268efSVladimir Oltean /* If a DSA tag_8021q CPU exists, it needs to be included in the 916e21268efSVladimir Oltean * regular forwarding path of the front ports regardless of whether 917e21268efSVladimir Oltean * those are bridged or standalone. 918e21268efSVladimir Oltean * If DSA tag_8021q is not used, this returns 0, which is fine because 919e21268efSVladimir Oltean * the hardware-based CPU port module can be a destination for packets 920e21268efSVladimir Oltean * even if it isn't part of PGID_SRC. 921e21268efSVladimir Oltean */ 922e21268efSVladimir Oltean cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot); 923e21268efSVladimir Oltean 9249b521250SVladimir Oltean /* Apply FWD mask. The loop is needed to add/remove the current port as 9259b521250SVladimir Oltean * a source for the other ports. 9269b521250SVladimir Oltean */ 9279b521250SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 928e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 929e21268efSVladimir Oltean unsigned long mask; 930e21268efSVladimir Oltean 931e21268efSVladimir Oltean if (!ocelot_port) { 932e21268efSVladimir Oltean /* Unused ports can't send anywhere */ 933e21268efSVladimir Oltean mask = 0; 934e21268efSVladimir Oltean } else if (ocelot_port->is_dsa_8021q_cpu) { 935e21268efSVladimir Oltean /* The DSA tag_8021q CPU ports need to be able to 936e21268efSVladimir Oltean * forward packets to all other ports except for 937e21268efSVladimir Oltean * themselves 938e21268efSVladimir Oltean */ 939e21268efSVladimir Oltean mask = GENMASK(ocelot->num_phys_ports - 1, 0); 940e21268efSVladimir Oltean mask &= ~cpu_fwd_mask; 941e21268efSVladimir Oltean } else if (ocelot->bridge_fwd_mask & BIT(port)) { 9429b521250SVladimir Oltean int lag; 9439b521250SVladimir Oltean 944e21268efSVladimir Oltean mask = ocelot->bridge_fwd_mask & ~BIT(port); 945e21268efSVladimir Oltean 9469b521250SVladimir Oltean for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 9479b521250SVladimir Oltean unsigned long bond_mask = ocelot->lags[lag]; 9489b521250SVladimir Oltean 9499b521250SVladimir Oltean if (!bond_mask) 9509b521250SVladimir Oltean continue; 9519b521250SVladimir Oltean 9529b521250SVladimir Oltean if (bond_mask & BIT(port)) { 9539b521250SVladimir Oltean mask &= ~bond_mask; 9549b521250SVladimir Oltean break; 9559b521250SVladimir Oltean } 9569b521250SVladimir Oltean } 9579b521250SVladimir Oltean } else { 958e21268efSVladimir Oltean /* Standalone ports forward only to DSA tag_8021q CPU 959e21268efSVladimir Oltean * ports (if those exist), or to the hardware CPU port 960e21268efSVladimir Oltean * module otherwise. 961e21268efSVladimir Oltean */ 962e21268efSVladimir Oltean mask = cpu_fwd_mask; 963e21268efSVladimir Oltean } 964e21268efSVladimir Oltean 965e21268efSVladimir Oltean ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); 9669b521250SVladimir Oltean } 9679b521250SVladimir Oltean } 968e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask); 9699b521250SVladimir Oltean 9705e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) 971a556c76aSAlexandre Belloni { 972a556c76aSAlexandre Belloni u32 port_cfg; 973a556c76aSAlexandre Belloni 9744bda1415SVladimir Oltean if (!(BIT(port) & ocelot->bridge_mask)) 9754bda1415SVladimir Oltean return; 976a556c76aSAlexandre Belloni 9774bda1415SVladimir Oltean port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); 978a556c76aSAlexandre Belloni 979a556c76aSAlexandre Belloni switch (state) { 980a556c76aSAlexandre Belloni case BR_STATE_FORWARDING: 9814bda1415SVladimir Oltean ocelot->bridge_fwd_mask |= BIT(port); 982df561f66SGustavo A. R. Silva fallthrough; 983a556c76aSAlexandre Belloni case BR_STATE_LEARNING: 984a556c76aSAlexandre Belloni port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA; 985a556c76aSAlexandre Belloni break; 986a556c76aSAlexandre Belloni 987a556c76aSAlexandre Belloni default: 988a556c76aSAlexandre Belloni port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA; 9894bda1415SVladimir Oltean ocelot->bridge_fwd_mask &= ~BIT(port); 990a556c76aSAlexandre Belloni break; 991a556c76aSAlexandre Belloni } 992a556c76aSAlexandre Belloni 9934bda1415SVladimir Oltean ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port); 994a556c76aSAlexandre Belloni 9959b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 996a556c76aSAlexandre Belloni } 9975e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set); 998a556c76aSAlexandre Belloni 9995e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) 10004bda1415SVladimir Oltean { 1001c0d7eccbSVladimir Oltean unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); 1002c0d7eccbSVladimir Oltean 1003c0d7eccbSVladimir Oltean /* Setting AGE_PERIOD to zero effectively disables automatic aging, 1004c0d7eccbSVladimir Oltean * which is clearly not what our intention is. So avoid that. 1005c0d7eccbSVladimir Oltean */ 1006c0d7eccbSVladimir Oltean if (!age_period) 1007c0d7eccbSVladimir Oltean age_period = 1; 1008c0d7eccbSVladimir Oltean 1009c0d7eccbSVladimir Oltean ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); 1010a556c76aSAlexandre Belloni } 10115e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time); 1012a556c76aSAlexandre Belloni 1013a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 1014a556c76aSAlexandre Belloni const unsigned char *addr, 1015a556c76aSAlexandre Belloni u16 vid) 1016a556c76aSAlexandre Belloni { 1017a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 1018a556c76aSAlexandre Belloni 1019a556c76aSAlexandre Belloni list_for_each_entry(mc, &ocelot->multicast, list) { 1020a556c76aSAlexandre Belloni if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 1021a556c76aSAlexandre Belloni return mc; 1022a556c76aSAlexandre Belloni } 1023a556c76aSAlexandre Belloni 1024a556c76aSAlexandre Belloni return NULL; 1025a556c76aSAlexandre Belloni } 1026a556c76aSAlexandre Belloni 10279403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) 10289403c158SVladimir Oltean { 10299403c158SVladimir Oltean if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) 10309403c158SVladimir Oltean return ENTRYTYPE_MACv4; 10319403c158SVladimir Oltean if (addr[0] == 0x33 && addr[1] == 0x33) 10329403c158SVladimir Oltean return ENTRYTYPE_MACv6; 10337c313143SVladimir Oltean return ENTRYTYPE_LOCKED; 10349403c158SVladimir Oltean } 10359403c158SVladimir Oltean 1036e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, 1037e5d1f896SVladimir Oltean unsigned long ports) 1038e5d1f896SVladimir Oltean { 1039e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1040e5d1f896SVladimir Oltean 1041e5d1f896SVladimir Oltean pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); 1042e5d1f896SVladimir Oltean if (!pgid) 1043e5d1f896SVladimir Oltean return ERR_PTR(-ENOMEM); 1044e5d1f896SVladimir Oltean 1045e5d1f896SVladimir Oltean pgid->ports = ports; 1046e5d1f896SVladimir Oltean pgid->index = index; 1047e5d1f896SVladimir Oltean refcount_set(&pgid->refcount, 1); 1048e5d1f896SVladimir Oltean list_add_tail(&pgid->list, &ocelot->pgids); 1049e5d1f896SVladimir Oltean 1050e5d1f896SVladimir Oltean return pgid; 1051e5d1f896SVladimir Oltean } 1052e5d1f896SVladimir Oltean 1053e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) 1054e5d1f896SVladimir Oltean { 1055e5d1f896SVladimir Oltean if (!refcount_dec_and_test(&pgid->refcount)) 1056e5d1f896SVladimir Oltean return; 1057e5d1f896SVladimir Oltean 1058e5d1f896SVladimir Oltean list_del(&pgid->list); 1059e5d1f896SVladimir Oltean kfree(pgid); 1060e5d1f896SVladimir Oltean } 1061e5d1f896SVladimir Oltean 1062e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, 1063bb8d53fdSVladimir Oltean const struct ocelot_multicast *mc) 10649403c158SVladimir Oltean { 1065e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1066e5d1f896SVladimir Oltean int index; 10679403c158SVladimir Oltean 10689403c158SVladimir Oltean /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and 10699403c158SVladimir Oltean * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the 10709403c158SVladimir Oltean * destination mask table (PGID), the destination set is programmed as 10719403c158SVladimir Oltean * part of the entry MAC address.", and the DEST_IDX is set to 0. 10729403c158SVladimir Oltean */ 1073bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4 || 1074bb8d53fdSVladimir Oltean mc->entry_type == ENTRYTYPE_MACv6) 1075e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, 0, mc->ports); 10769403c158SVladimir Oltean 1077e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 1078e5d1f896SVladimir Oltean /* When searching for a nonreserved multicast PGID, ignore the 1079e5d1f896SVladimir Oltean * dummy PGID of zero that we have for MACv4/MACv6 entries 1080e5d1f896SVladimir Oltean */ 1081e5d1f896SVladimir Oltean if (pgid->index && pgid->ports == mc->ports) { 1082e5d1f896SVladimir Oltean refcount_inc(&pgid->refcount); 1083e5d1f896SVladimir Oltean return pgid; 1084e5d1f896SVladimir Oltean } 1085e5d1f896SVladimir Oltean } 1086e5d1f896SVladimir Oltean 1087e5d1f896SVladimir Oltean /* Search for a free index in the nonreserved multicast PGID area */ 1088e5d1f896SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, index) { 10899403c158SVladimir Oltean bool used = false; 10909403c158SVladimir Oltean 1091e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 1092e5d1f896SVladimir Oltean if (pgid->index == index) { 10939403c158SVladimir Oltean used = true; 10949403c158SVladimir Oltean break; 10959403c158SVladimir Oltean } 10969403c158SVladimir Oltean } 10979403c158SVladimir Oltean 10989403c158SVladimir Oltean if (!used) 1099e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, index, mc->ports); 11009403c158SVladimir Oltean } 11019403c158SVladimir Oltean 1102e5d1f896SVladimir Oltean return ERR_PTR(-ENOSPC); 11039403c158SVladimir Oltean } 11049403c158SVladimir Oltean 11059403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr, 1106bb8d53fdSVladimir Oltean struct ocelot_multicast *mc) 11079403c158SVladimir Oltean { 1108ebbd860eSVladimir Oltean ether_addr_copy(addr, mc->addr); 11099403c158SVladimir Oltean 1110bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4) { 11119403c158SVladimir Oltean addr[0] = 0; 11129403c158SVladimir Oltean addr[1] = mc->ports >> 8; 11139403c158SVladimir Oltean addr[2] = mc->ports & 0xff; 1114bb8d53fdSVladimir Oltean } else if (mc->entry_type == ENTRYTYPE_MACv6) { 11159403c158SVladimir Oltean addr[0] = mc->ports >> 8; 11169403c158SVladimir Oltean addr[1] = mc->ports & 0xff; 11179403c158SVladimir Oltean } 11189403c158SVladimir Oltean } 11199403c158SVladimir Oltean 1120209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port, 1121209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1122a556c76aSAlexandre Belloni { 1123a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 1124004d44f6SVladimir Oltean struct ocelot_multicast *mc; 1125e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1126a556c76aSAlexandre Belloni u16 vid = mdb->vid; 1127a556c76aSAlexandre Belloni 1128471beb11SVladimir Oltean if (port == ocelot->npi) 1129471beb11SVladimir Oltean port = ocelot->num_phys_ports; 1130471beb11SVladimir Oltean 1131a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1132a556c76aSAlexandre Belloni if (!mc) { 1133728e69aeSVladimir Oltean /* New entry */ 1134bb8d53fdSVladimir Oltean mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 1135bb8d53fdSVladimir Oltean if (!mc) 1136bb8d53fdSVladimir Oltean return -ENOMEM; 1137bb8d53fdSVladimir Oltean 1138bb8d53fdSVladimir Oltean mc->entry_type = ocelot_classify_mdb(mdb->addr); 1139bb8d53fdSVladimir Oltean ether_addr_copy(mc->addr, mdb->addr); 1140bb8d53fdSVladimir Oltean mc->vid = vid; 1141bb8d53fdSVladimir Oltean 1142a556c76aSAlexandre Belloni list_add_tail(&mc->list, &ocelot->multicast); 1143728e69aeSVladimir Oltean } else { 1144e5d1f896SVladimir Oltean /* Existing entry. Clean up the current port mask from 1145e5d1f896SVladimir Oltean * hardware now, because we'll be modifying it. 1146e5d1f896SVladimir Oltean */ 1147e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 1148bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1149a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 1150a556c76aSAlexandre Belloni } 1151a556c76aSAlexandre Belloni 1152004d44f6SVladimir Oltean mc->ports |= BIT(port); 1153e5d1f896SVladimir Oltean 1154e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 1155e5d1f896SVladimir Oltean if (IS_ERR(pgid)) { 1156e5d1f896SVladimir Oltean dev_err(ocelot->dev, 1157e5d1f896SVladimir Oltean "Cannot allocate PGID for mdb %pM vid %d\n", 1158e5d1f896SVladimir Oltean mc->addr, mc->vid); 1159e5d1f896SVladimir Oltean devm_kfree(ocelot->dev, mc); 1160e5d1f896SVladimir Oltean return PTR_ERR(pgid); 1161e5d1f896SVladimir Oltean } 1162e5d1f896SVladimir Oltean mc->pgid = pgid; 1163e5d1f896SVladimir Oltean 1164bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1165a556c76aSAlexandre Belloni 1166e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 1167e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 1168e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1169e5d1f896SVladimir Oltean pgid->index); 1170e5d1f896SVladimir Oltean 1171e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1172bb8d53fdSVladimir Oltean mc->entry_type); 1173a556c76aSAlexandre Belloni } 1174209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add); 1175a556c76aSAlexandre Belloni 1176209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port, 1177a556c76aSAlexandre Belloni const struct switchdev_obj_port_mdb *mdb) 1178a556c76aSAlexandre Belloni { 1179a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 1180004d44f6SVladimir Oltean struct ocelot_multicast *mc; 1181e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1182a556c76aSAlexandre Belloni u16 vid = mdb->vid; 1183a556c76aSAlexandre Belloni 1184471beb11SVladimir Oltean if (port == ocelot->npi) 1185471beb11SVladimir Oltean port = ocelot->num_phys_ports; 1186471beb11SVladimir Oltean 1187a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1188a556c76aSAlexandre Belloni if (!mc) 1189a556c76aSAlexandre Belloni return -ENOENT; 1190a556c76aSAlexandre Belloni 1191bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1192a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 1193a556c76aSAlexandre Belloni 1194e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 1195004d44f6SVladimir Oltean mc->ports &= ~BIT(port); 1196a556c76aSAlexandre Belloni if (!mc->ports) { 1197a556c76aSAlexandre Belloni list_del(&mc->list); 1198a556c76aSAlexandre Belloni devm_kfree(ocelot->dev, mc); 1199a556c76aSAlexandre Belloni return 0; 1200a556c76aSAlexandre Belloni } 1201a556c76aSAlexandre Belloni 1202e5d1f896SVladimir Oltean /* We have a PGID with fewer ports now */ 1203e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 1204e5d1f896SVladimir Oltean if (IS_ERR(pgid)) 1205e5d1f896SVladimir Oltean return PTR_ERR(pgid); 1206e5d1f896SVladimir Oltean mc->pgid = pgid; 1207e5d1f896SVladimir Oltean 1208bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1209a556c76aSAlexandre Belloni 1210e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 1211e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 1212e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1213e5d1f896SVladimir Oltean pgid->index); 1214e5d1f896SVladimir Oltean 1215e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1216bb8d53fdSVladimir Oltean mc->entry_type); 1217a556c76aSAlexandre Belloni } 1218209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del); 1219a556c76aSAlexandre Belloni 12205e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port, 1221a556c76aSAlexandre Belloni struct net_device *bridge) 1222a556c76aSAlexandre Belloni { 1223a556c76aSAlexandre Belloni if (!ocelot->bridge_mask) { 1224a556c76aSAlexandre Belloni ocelot->hw_bridge_dev = bridge; 1225a556c76aSAlexandre Belloni } else { 1226a556c76aSAlexandre Belloni if (ocelot->hw_bridge_dev != bridge) 1227a556c76aSAlexandre Belloni /* This is adding the port to a second bridge, this is 1228a556c76aSAlexandre Belloni * unsupported */ 1229a556c76aSAlexandre Belloni return -ENODEV; 1230a556c76aSAlexandre Belloni } 1231a556c76aSAlexandre Belloni 1232f270dbfaSVladimir Oltean ocelot->bridge_mask |= BIT(port); 1233a556c76aSAlexandre Belloni 1234a556c76aSAlexandre Belloni return 0; 1235a556c76aSAlexandre Belloni } 12365e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join); 1237a556c76aSAlexandre Belloni 12385e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port, 1239a556c76aSAlexandre Belloni struct net_device *bridge) 1240a556c76aSAlexandre Belloni { 1241c3e58a75SVladimir Oltean struct ocelot_vlan pvid = {0}, native_vlan = {0}; 12422e554a7aSVladimir Oltean int ret; 12432e554a7aSVladimir Oltean 124497bb69e1SVladimir Oltean ocelot->bridge_mask &= ~BIT(port); 1245a556c76aSAlexandre Belloni 1246a556c76aSAlexandre Belloni if (!ocelot->bridge_mask) 1247a556c76aSAlexandre Belloni ocelot->hw_bridge_dev = NULL; 12487142529fSAntoine Tenart 1249bae33f2bSVladimir Oltean ret = ocelot_port_vlan_filtering(ocelot, port, false); 12502e554a7aSVladimir Oltean if (ret) 12512e554a7aSVladimir Oltean return ret; 12522e554a7aSVladimir Oltean 1253c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, pvid); 12542f0402feSVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, native_vlan); 12552f0402feSVladimir Oltean 12562f0402feSVladimir Oltean return 0; 1257a556c76aSAlexandre Belloni } 12585e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave); 1259a556c76aSAlexandre Belloni 1260dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 1261dc96ee37SAlexandre Belloni { 1262dc96ee37SAlexandre Belloni int i, port, lag; 1263dc96ee37SAlexandre Belloni 1264dc96ee37SAlexandre Belloni /* Reset destination and aggregation PGIDS */ 126596b029b0SVladimir Oltean for_each_unicast_dest_pgid(ocelot, port) 1266dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1267dc96ee37SAlexandre Belloni 126896b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) 1269dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 1270dc96ee37SAlexandre Belloni ANA_PGID_PGID, i); 1271dc96ee37SAlexandre Belloni 1272dc96ee37SAlexandre Belloni /* Now, set PGIDs for each LAG */ 1273dc96ee37SAlexandre Belloni for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 1274dc96ee37SAlexandre Belloni unsigned long bond_mask; 1275dc96ee37SAlexandre Belloni int aggr_count = 0; 1276dc96ee37SAlexandre Belloni u8 aggr_idx[16]; 1277dc96ee37SAlexandre Belloni 1278dc96ee37SAlexandre Belloni bond_mask = ocelot->lags[lag]; 1279dc96ee37SAlexandre Belloni if (!bond_mask) 1280dc96ee37SAlexandre Belloni continue; 1281dc96ee37SAlexandre Belloni 1282dc96ee37SAlexandre Belloni for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 1283dc96ee37SAlexandre Belloni // Destination mask 1284dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, bond_mask, 1285dc96ee37SAlexandre Belloni ANA_PGID_PGID, port); 1286dc96ee37SAlexandre Belloni aggr_idx[aggr_count] = port; 1287dc96ee37SAlexandre Belloni aggr_count++; 1288dc96ee37SAlexandre Belloni } 1289dc96ee37SAlexandre Belloni 129096b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) { 1291dc96ee37SAlexandre Belloni u32 ac; 1292dc96ee37SAlexandre Belloni 1293dc96ee37SAlexandre Belloni ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 1294dc96ee37SAlexandre Belloni ac &= ~bond_mask; 1295dc96ee37SAlexandre Belloni ac |= BIT(aggr_idx[i % aggr_count]); 1296dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 1297dc96ee37SAlexandre Belloni } 1298dc96ee37SAlexandre Belloni } 1299dc96ee37SAlexandre Belloni } 1300dc96ee37SAlexandre Belloni 1301dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag) 1302dc96ee37SAlexandre Belloni { 1303dc96ee37SAlexandre Belloni unsigned long bond_mask = ocelot->lags[lag]; 1304dc96ee37SAlexandre Belloni unsigned int p; 1305dc96ee37SAlexandre Belloni 1306dc96ee37SAlexandre Belloni for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) { 1307dc96ee37SAlexandre Belloni u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p); 1308dc96ee37SAlexandre Belloni 1309dc96ee37SAlexandre Belloni port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M; 1310dc96ee37SAlexandre Belloni 1311dc96ee37SAlexandre Belloni /* Use lag port as logical port for port i */ 1312dc96ee37SAlexandre Belloni ocelot_write_gix(ocelot, port_cfg | 1313dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(lag), 1314dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG, p); 1315dc96ee37SAlexandre Belloni } 1316dc96ee37SAlexandre Belloni } 1317dc96ee37SAlexandre Belloni 13189c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port, 1319*583cbbe3SVladimir Oltean struct net_device *bond, 1320*583cbbe3SVladimir Oltean struct netdev_lag_upper_info *info) 1321dc96ee37SAlexandre Belloni { 1322dc96ee37SAlexandre Belloni struct net_device *ndev; 1323dc96ee37SAlexandre Belloni u32 bond_mask = 0; 1324f270dbfaSVladimir Oltean int lag, lp; 1325dc96ee37SAlexandre Belloni 1326*583cbbe3SVladimir Oltean if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) 1327*583cbbe3SVladimir Oltean return -EOPNOTSUPP; 1328*583cbbe3SVladimir Oltean 1329dc96ee37SAlexandre Belloni rcu_read_lock(); 1330dc96ee37SAlexandre Belloni for_each_netdev_in_bond_rcu(bond, ndev) { 1331004d44f6SVladimir Oltean struct ocelot_port_private *priv = netdev_priv(ndev); 1332dc96ee37SAlexandre Belloni 1333004d44f6SVladimir Oltean bond_mask |= BIT(priv->chip_port); 1334dc96ee37SAlexandre Belloni } 1335dc96ee37SAlexandre Belloni rcu_read_unlock(); 1336dc96ee37SAlexandre Belloni 1337dc96ee37SAlexandre Belloni lp = __ffs(bond_mask); 1338dc96ee37SAlexandre Belloni 1339dc96ee37SAlexandre Belloni /* If the new port is the lowest one, use it as the logical port from 1340dc96ee37SAlexandre Belloni * now on 1341dc96ee37SAlexandre Belloni */ 1342f270dbfaSVladimir Oltean if (port == lp) { 1343f270dbfaSVladimir Oltean lag = port; 1344f270dbfaSVladimir Oltean ocelot->lags[port] = bond_mask; 1345f270dbfaSVladimir Oltean bond_mask &= ~BIT(port); 1346dc96ee37SAlexandre Belloni if (bond_mask) { 1347dc96ee37SAlexandre Belloni lp = __ffs(bond_mask); 1348dc96ee37SAlexandre Belloni ocelot->lags[lp] = 0; 1349dc96ee37SAlexandre Belloni } 1350dc96ee37SAlexandre Belloni } else { 1351dc96ee37SAlexandre Belloni lag = lp; 1352f270dbfaSVladimir Oltean ocelot->lags[lp] |= BIT(port); 1353dc96ee37SAlexandre Belloni } 1354dc96ee37SAlexandre Belloni 1355dc96ee37SAlexandre Belloni ocelot_setup_lag(ocelot, lag); 13569b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 1357dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 1358dc96ee37SAlexandre Belloni 1359dc96ee37SAlexandre Belloni return 0; 1360dc96ee37SAlexandre Belloni } 13619c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join); 1362dc96ee37SAlexandre Belloni 13639c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port, 1364dc96ee37SAlexandre Belloni struct net_device *bond) 1365dc96ee37SAlexandre Belloni { 1366dc96ee37SAlexandre Belloni u32 port_cfg; 1367dc96ee37SAlexandre Belloni int i; 1368dc96ee37SAlexandre Belloni 1369dc96ee37SAlexandre Belloni /* Remove port from any lag */ 1370dc96ee37SAlexandre Belloni for (i = 0; i < ocelot->num_phys_ports; i++) 1371f270dbfaSVladimir Oltean ocelot->lags[i] &= ~BIT(port); 1372dc96ee37SAlexandre Belloni 1373dc96ee37SAlexandre Belloni /* if it was the logical port of the lag, move the lag config to the 1374dc96ee37SAlexandre Belloni * next port 1375dc96ee37SAlexandre Belloni */ 1376f270dbfaSVladimir Oltean if (ocelot->lags[port]) { 1377f270dbfaSVladimir Oltean int n = __ffs(ocelot->lags[port]); 1378dc96ee37SAlexandre Belloni 1379f270dbfaSVladimir Oltean ocelot->lags[n] = ocelot->lags[port]; 1380f270dbfaSVladimir Oltean ocelot->lags[port] = 0; 1381dc96ee37SAlexandre Belloni 1382dc96ee37SAlexandre Belloni ocelot_setup_lag(ocelot, n); 1383dc96ee37SAlexandre Belloni } 1384dc96ee37SAlexandre Belloni 1385f270dbfaSVladimir Oltean port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); 1386dc96ee37SAlexandre Belloni port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M; 1387f270dbfaSVladimir Oltean ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port), 1388f270dbfaSVladimir Oltean ANA_PORT_PORT_CFG, port); 1389dc96ee37SAlexandre Belloni 13909b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 1391dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 1392dc96ee37SAlexandre Belloni } 13939c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave); 13940e332c85SPetr Machata 1395a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. 1396a8015dedSVladimir Oltean * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. 13970b912fc9SVladimir Oltean * In the special case that it's the NPI port that we're configuring, the 13980b912fc9SVladimir Oltean * length of the tag and optional prefix needs to be accounted for privately, 13990b912fc9SVladimir Oltean * in order to be able to sustain communication at the requested @sdu. 1400a8015dedSVladimir Oltean */ 14010b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) 140231350d7fSVladimir Oltean { 140331350d7fSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1404a8015dedSVladimir Oltean int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; 1405e8e6e73dSVladimir Oltean int pause_start, pause_stop; 1406601e984fSVladimir Oltean int atop, atop_tot; 140731350d7fSVladimir Oltean 14080b912fc9SVladimir Oltean if (port == ocelot->npi) { 14090b912fc9SVladimir Oltean maxlen += OCELOT_TAG_LEN; 14100b912fc9SVladimir Oltean 1411cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 14120b912fc9SVladimir Oltean maxlen += OCELOT_SHORT_PREFIX_LEN; 1413cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 14140b912fc9SVladimir Oltean maxlen += OCELOT_LONG_PREFIX_LEN; 14150b912fc9SVladimir Oltean } 14160b912fc9SVladimir Oltean 1417a8015dedSVladimir Oltean ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); 1418fa914e9cSVladimir Oltean 1419e8e6e73dSVladimir Oltean /* Set Pause watermark hysteresis */ 1420e8e6e73dSVladimir Oltean pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; 1421e8e6e73dSVladimir Oltean pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; 1422541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, 1423541132f0SMaxim Kochetkov pause_start); 1424541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, 1425541132f0SMaxim Kochetkov pause_stop); 1426fa914e9cSVladimir Oltean 1427601e984fSVladimir Oltean /* Tail dropping watermarks */ 1428f6fe01d6SVladimir Oltean atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / 1429a8015dedSVladimir Oltean OCELOT_BUFFER_CELL_SZ; 1430601e984fSVladimir Oltean atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; 1431601e984fSVladimir Oltean ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); 1432601e984fSVladimir Oltean ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); 1433fa914e9cSVladimir Oltean } 14340b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen); 14350b912fc9SVladimir Oltean 14360b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port) 14370b912fc9SVladimir Oltean { 14380b912fc9SVladimir Oltean int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; 14390b912fc9SVladimir Oltean 14400b912fc9SVladimir Oltean if (port == ocelot->npi) { 14410b912fc9SVladimir Oltean max_mtu -= OCELOT_TAG_LEN; 14420b912fc9SVladimir Oltean 1443cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 14440b912fc9SVladimir Oltean max_mtu -= OCELOT_SHORT_PREFIX_LEN; 1445cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 14460b912fc9SVladimir Oltean max_mtu -= OCELOT_LONG_PREFIX_LEN; 14470b912fc9SVladimir Oltean } 14480b912fc9SVladimir Oltean 14490b912fc9SVladimir Oltean return max_mtu; 14500b912fc9SVladimir Oltean } 14510b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu); 1452fa914e9cSVladimir Oltean 14535e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port) 1454fa914e9cSVladimir Oltean { 1455fa914e9cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1456fa914e9cSVladimir Oltean 1457b049da13SYangbo Lu skb_queue_head_init(&ocelot_port->tx_skbs); 14586565243cSVladimir Oltean spin_lock_init(&ocelot_port->ts_id_lock); 145931350d7fSVladimir Oltean 146031350d7fSVladimir Oltean /* Basic L2 initialization */ 146131350d7fSVladimir Oltean 14625bc9d2e6SVladimir Oltean /* Set MAC IFG Gaps 14635bc9d2e6SVladimir Oltean * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 14645bc9d2e6SVladimir Oltean * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 14655bc9d2e6SVladimir Oltean */ 14665bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), 14675bc9d2e6SVladimir Oltean DEV_MAC_IFG_CFG); 14685bc9d2e6SVladimir Oltean 14695bc9d2e6SVladimir Oltean /* Load seed (0) and set MAC HDX late collision */ 14705bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 14715bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG_SEED_LOAD, 14725bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 14735bc9d2e6SVladimir Oltean mdelay(1); 14745bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 14755bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 14765bc9d2e6SVladimir Oltean 14775bc9d2e6SVladimir Oltean /* Set Max Length and maximum tags allowed */ 1478a8015dedSVladimir Oltean ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); 14795bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 14805bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 1481a8015dedSVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | 14825bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 14835bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG); 14845bc9d2e6SVladimir Oltean 14855bc9d2e6SVladimir Oltean /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 14865bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 14875bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); 14885bc9d2e6SVladimir Oltean 1489e8e6e73dSVladimir Oltean /* Enable transmission of pause frames */ 1490541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 1491e8e6e73dSVladimir Oltean 149231350d7fSVladimir Oltean /* Drop frames with multicast source address */ 149331350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 149431350d7fSVladimir Oltean ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 149531350d7fSVladimir Oltean ANA_PORT_DROP_CFG, port); 149631350d7fSVladimir Oltean 149731350d7fSVladimir Oltean /* Set default VLAN and tag type to 8021Q. */ 149831350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), 149931350d7fSVladimir Oltean REW_PORT_VLAN_CFG_PORT_TPID_M, 150031350d7fSVladimir Oltean REW_PORT_VLAN_CFG, port); 150131350d7fSVladimir Oltean 150231350d7fSVladimir Oltean /* Enable vcap lookups */ 150331350d7fSVladimir Oltean ocelot_vcap_enable(ocelot, port); 150431350d7fSVladimir Oltean } 15055e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port); 150631350d7fSVladimir Oltean 15072d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues 15082d44b097SVladimir Oltean * accessible through register MMIO, frame DMA or Ethernet (in case 15092d44b097SVladimir Oltean * NPI mode is used). 151069df578cSVladimir Oltean */ 15112d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot) 151221468199SVladimir Oltean { 151369df578cSVladimir Oltean int cpu = ocelot->num_phys_ports; 151469df578cSVladimir Oltean 151569df578cSVladimir Oltean /* The unicast destination PGID for the CPU port module is unused */ 151621468199SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 151769df578cSVladimir Oltean /* Instead set up a multicast destination PGID for traffic copied to 151869df578cSVladimir Oltean * the CPU. Whitelisted MAC addresses like the port netdevice MAC 151969df578cSVladimir Oltean * addresses will be copied to the CPU via this PGID. 152069df578cSVladimir Oltean */ 152121468199SVladimir Oltean ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 152221468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 152321468199SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 152421468199SVladimir Oltean ANA_PORT_PORT_CFG, cpu); 152521468199SVladimir Oltean 152669df578cSVladimir Oltean /* Enable CPU port module */ 1527886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 152869df578cSVladimir Oltean /* CPU port Injection/Extraction configuration */ 1529886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, 1530cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 1531886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, 1532cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 153321468199SVladimir Oltean 153421468199SVladimir Oltean /* Configure the CPU port to be VLAN aware */ 153521468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) | 153621468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 153721468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 153821468199SVladimir Oltean ANA_PORT_VLAN_CFG, cpu); 153921468199SVladimir Oltean } 154021468199SVladimir Oltean 1541f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot) 1542f6fe01d6SVladimir Oltean { 1543f6fe01d6SVladimir Oltean int mmgt, eq_ctrl; 1544f6fe01d6SVladimir Oltean 1545f6fe01d6SVladimir Oltean /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds 1546f6fe01d6SVladimir Oltean * the number of 240-byte free memory words (aka 4-cell chunks) and not 1547f6fe01d6SVladimir Oltean * 192 bytes as the documentation incorrectly says. 1548f6fe01d6SVladimir Oltean */ 1549f6fe01d6SVladimir Oltean mmgt = ocelot_read(ocelot, SYS_MMGT); 1550f6fe01d6SVladimir Oltean ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); 1551f6fe01d6SVladimir Oltean 1552f6fe01d6SVladimir Oltean eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); 1553f6fe01d6SVladimir Oltean ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); 1554f6fe01d6SVladimir Oltean } 1555f6fe01d6SVladimir Oltean 1556a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot) 1557a556c76aSAlexandre Belloni { 1558a556c76aSAlexandre Belloni char queue_name[32]; 155921468199SVladimir Oltean int i, ret; 156021468199SVladimir Oltean u32 port; 1561a556c76aSAlexandre Belloni 15623a77b593SVladimir Oltean if (ocelot->ops->reset) { 15633a77b593SVladimir Oltean ret = ocelot->ops->reset(ocelot); 15643a77b593SVladimir Oltean if (ret) { 15653a77b593SVladimir Oltean dev_err(ocelot->dev, "Switch reset failed\n"); 15663a77b593SVladimir Oltean return ret; 15673a77b593SVladimir Oltean } 15683a77b593SVladimir Oltean } 15693a77b593SVladimir Oltean 1570dc96ee37SAlexandre Belloni ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports, 1571dc96ee37SAlexandre Belloni sizeof(u32), GFP_KERNEL); 1572dc96ee37SAlexandre Belloni if (!ocelot->lags) 1573dc96ee37SAlexandre Belloni return -ENOMEM; 1574dc96ee37SAlexandre Belloni 1575a556c76aSAlexandre Belloni ocelot->stats = devm_kcalloc(ocelot->dev, 1576a556c76aSAlexandre Belloni ocelot->num_phys_ports * ocelot->num_stats, 1577a556c76aSAlexandre Belloni sizeof(u64), GFP_KERNEL); 1578a556c76aSAlexandre Belloni if (!ocelot->stats) 1579a556c76aSAlexandre Belloni return -ENOMEM; 1580a556c76aSAlexandre Belloni 1581a556c76aSAlexandre Belloni mutex_init(&ocelot->stats_lock); 15824e3b0468SAntoine Tenart mutex_init(&ocelot->ptp_lock); 15834e3b0468SAntoine Tenart spin_lock_init(&ocelot->ptp_clock_lock); 1584a556c76aSAlexandre Belloni snprintf(queue_name, sizeof(queue_name), "%s-stats", 1585a556c76aSAlexandre Belloni dev_name(ocelot->dev)); 1586a556c76aSAlexandre Belloni ocelot->stats_queue = create_singlethread_workqueue(queue_name); 1587a556c76aSAlexandre Belloni if (!ocelot->stats_queue) 1588a556c76aSAlexandre Belloni return -ENOMEM; 1589a556c76aSAlexandre Belloni 1590ca0b272bSVladimir Oltean ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); 1591ca0b272bSVladimir Oltean if (!ocelot->owq) { 1592ca0b272bSVladimir Oltean destroy_workqueue(ocelot->stats_queue); 1593ca0b272bSVladimir Oltean return -ENOMEM; 1594ca0b272bSVladimir Oltean } 1595ca0b272bSVladimir Oltean 15962b120ddeSClaudiu Manoil INIT_LIST_HEAD(&ocelot->multicast); 1597e5d1f896SVladimir Oltean INIT_LIST_HEAD(&ocelot->pgids); 1598f6fe01d6SVladimir Oltean ocelot_detect_features(ocelot); 1599a556c76aSAlexandre Belloni ocelot_mact_init(ocelot); 1600a556c76aSAlexandre Belloni ocelot_vlan_init(ocelot); 1601aae4e500SVladimir Oltean ocelot_vcap_init(ocelot); 16022d44b097SVladimir Oltean ocelot_cpu_port_init(ocelot); 1603a556c76aSAlexandre Belloni 1604a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 1605a556c76aSAlexandre Belloni /* Clear all counters (5 groups) */ 1606a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 1607a556c76aSAlexandre Belloni SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 1608a556c76aSAlexandre Belloni SYS_STAT_CFG); 1609a556c76aSAlexandre Belloni } 1610a556c76aSAlexandre Belloni 1611a556c76aSAlexandre Belloni /* Only use S-Tag */ 1612a556c76aSAlexandre Belloni ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 1613a556c76aSAlexandre Belloni 1614a556c76aSAlexandre Belloni /* Aggregation mode */ 1615a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 1616a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_DMAC_ENA | 1617a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 1618a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG); 1619a556c76aSAlexandre Belloni 1620a556c76aSAlexandre Belloni /* Set MAC age time to default value. The entry is aged after 1621a556c76aSAlexandre Belloni * 2*AGE_PERIOD 1622a556c76aSAlexandre Belloni */ 1623a556c76aSAlexandre Belloni ocelot_write(ocelot, 1624a556c76aSAlexandre Belloni ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 1625a556c76aSAlexandre Belloni ANA_AUTOAGE); 1626a556c76aSAlexandre Belloni 1627a556c76aSAlexandre Belloni /* Disable learning for frames discarded by VLAN ingress filtering */ 1628a556c76aSAlexandre Belloni regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 1629a556c76aSAlexandre Belloni 1630a556c76aSAlexandre Belloni /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 1631a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 1632a556c76aSAlexandre Belloni SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 1633a556c76aSAlexandre Belloni 1634a556c76aSAlexandre Belloni /* Setup flooding PGIDs */ 1635edd2410bSVladimir Oltean for (i = 0; i < ocelot->num_flooding_pgids; i++) 1636a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 1637a556c76aSAlexandre Belloni ANA_FLOODING_FLD_BROADCAST(PGID_MC) | 1638a556c76aSAlexandre Belloni ANA_FLOODING_FLD_UNICAST(PGID_UC), 1639edd2410bSVladimir Oltean ANA_FLOODING, i); 1640a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 1641a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 1642a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 1643a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 1644a556c76aSAlexandre Belloni ANA_FLOODING_IPMC); 1645a556c76aSAlexandre Belloni 1646a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 1647a556c76aSAlexandre Belloni /* Transmit the frame to the local port. */ 1648a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1649a556c76aSAlexandre Belloni /* Do not forward BPDU frames to the front ports. */ 1650a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, 1651a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 1652a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG, 1653a556c76aSAlexandre Belloni port); 1654a556c76aSAlexandre Belloni /* Ensure bridging is disabled */ 1655a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 1656a556c76aSAlexandre Belloni } 1657a556c76aSAlexandre Belloni 1658a556c76aSAlexandre Belloni /* Allow broadcast MAC frames. */ 165996b029b0SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, i) { 1660a556c76aSAlexandre Belloni u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 1661a556c76aSAlexandre Belloni 1662a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 1663a556c76aSAlexandre Belloni } 1664a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 1665a556c76aSAlexandre Belloni ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)), 1666a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_MC); 1667a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 1668a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 1669a556c76aSAlexandre Belloni 1670a556c76aSAlexandre Belloni /* Allow manual injection via DEVCPU_QS registers, and byte swap these 1671a556c76aSAlexandre Belloni * registers endianness. 1672a556c76aSAlexandre Belloni */ 1673a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 1674a556c76aSAlexandre Belloni QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 1675a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 1676a556c76aSAlexandre Belloni QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 1677a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 1678a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LRN(2) | 1679a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 1680a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 1681a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 1682a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 1683a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 1684a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IGMP(6) | 1685a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 1686a556c76aSAlexandre Belloni for (i = 0; i < 16; i++) 1687a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 1688a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 1689a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG, i); 1690a556c76aSAlexandre Belloni 16911e1caa97SClaudiu Manoil INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 1692a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1693a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 16944e3b0468SAntoine Tenart 1695a556c76aSAlexandre Belloni return 0; 1696a556c76aSAlexandre Belloni } 1697a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init); 1698a556c76aSAlexandre Belloni 1699a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot) 1700a556c76aSAlexandre Belloni { 1701c5d13969SClaudiu Manoil cancel_delayed_work(&ocelot->stats_work); 1702a556c76aSAlexandre Belloni destroy_workqueue(ocelot->stats_queue); 1703ca0b272bSVladimir Oltean destroy_workqueue(ocelot->owq); 1704a556c76aSAlexandre Belloni mutex_destroy(&ocelot->stats_lock); 1705a556c76aSAlexandre Belloni } 1706a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit); 1707a556c76aSAlexandre Belloni 1708e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port) 1709e5fb512dSVladimir Oltean { 1710e5fb512dSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1711e5fb512dSVladimir Oltean 1712e5fb512dSVladimir Oltean skb_queue_purge(&ocelot_port->tx_skbs); 1713e5fb512dSVladimir Oltean } 1714e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port); 1715e5fb512dSVladimir Oltean 1716a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL"); 1717