1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2a556c76aSAlexandre Belloni /* 3a556c76aSAlexandre Belloni * Microsemi Ocelot Switch driver 4a556c76aSAlexandre Belloni * 5a556c76aSAlexandre Belloni * Copyright (c) 2017 Microsemi Corporation 6a556c76aSAlexandre Belloni */ 740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h> 8a556c76aSAlexandre Belloni #include <linux/if_bridge.h> 920968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h> 10a556c76aSAlexandre Belloni #include "ocelot.h" 113c83654fSVladimir Oltean #include "ocelot_vcap.h" 12a556c76aSAlexandre Belloni 13639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10 14639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000 15639c1b26SSteen Hegelund 16a556c76aSAlexandre Belloni struct ocelot_mact_entry { 17a556c76aSAlexandre Belloni u8 mac[ETH_ALEN]; 18a556c76aSAlexandre Belloni u16 vid; 19a556c76aSAlexandre Belloni enum macaccess_entry_type type; 20a556c76aSAlexandre Belloni }; 21a556c76aSAlexandre Belloni 22639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) 23639c1b26SSteen Hegelund { 24639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_MACACCESS); 25639c1b26SSteen Hegelund } 26639c1b26SSteen Hegelund 27a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) 28a556c76aSAlexandre Belloni { 29639c1b26SSteen Hegelund u32 val; 30a556c76aSAlexandre Belloni 31639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_mact_read_macaccess, 32639c1b26SSteen Hegelund ocelot, val, 33639c1b26SSteen Hegelund (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == 34639c1b26SSteen Hegelund MACACCESS_CMD_IDLE, 35639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 36a556c76aSAlexandre Belloni } 37a556c76aSAlexandre Belloni 38a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot, 39a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 40a556c76aSAlexandre Belloni unsigned int vid) 41a556c76aSAlexandre Belloni { 42a556c76aSAlexandre Belloni u32 macl = 0, mach = 0; 43a556c76aSAlexandre Belloni 44a556c76aSAlexandre Belloni /* Set the MAC address to handle and the vlan associated in a format 45a556c76aSAlexandre Belloni * understood by the hardware. 46a556c76aSAlexandre Belloni */ 47a556c76aSAlexandre Belloni mach |= vid << 16; 48a556c76aSAlexandre Belloni mach |= mac[0] << 8; 49a556c76aSAlexandre Belloni mach |= mac[1] << 0; 50a556c76aSAlexandre Belloni macl |= mac[2] << 24; 51a556c76aSAlexandre Belloni macl |= mac[3] << 16; 52a556c76aSAlexandre Belloni macl |= mac[4] << 8; 53a556c76aSAlexandre Belloni macl |= mac[5] << 0; 54a556c76aSAlexandre Belloni 55a556c76aSAlexandre Belloni ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); 56a556c76aSAlexandre Belloni ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); 57a556c76aSAlexandre Belloni 58a556c76aSAlexandre Belloni } 59a556c76aSAlexandre Belloni 609c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port, 61a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 629c90eea3SVladimir Oltean unsigned int vid, enum macaccess_entry_type type) 63a556c76aSAlexandre Belloni { 64584b7cfcSAlban Bedel u32 cmd = ANA_TABLES_MACACCESS_VALID | 65584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_DEST_IDX(port) | 66584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_ENTRYTYPE(type) | 67584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); 68584b7cfcSAlban Bedel unsigned int mc_ports; 69584b7cfcSAlban Bedel 70584b7cfcSAlban Bedel /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ 71584b7cfcSAlban Bedel if (type == ENTRYTYPE_MACv4) 72584b7cfcSAlban Bedel mc_ports = (mac[1] << 8) | mac[2]; 73584b7cfcSAlban Bedel else if (type == ENTRYTYPE_MACv6) 74584b7cfcSAlban Bedel mc_ports = (mac[0] << 8) | mac[1]; 75584b7cfcSAlban Bedel else 76584b7cfcSAlban Bedel mc_ports = 0; 77584b7cfcSAlban Bedel 78584b7cfcSAlban Bedel if (mc_ports & BIT(ocelot->num_phys_ports)) 79584b7cfcSAlban Bedel cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; 80584b7cfcSAlban Bedel 81a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 82a556c76aSAlexandre Belloni 83a556c76aSAlexandre Belloni /* Issue a write command */ 84584b7cfcSAlban Bedel ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); 85a556c76aSAlexandre Belloni 86a556c76aSAlexandre Belloni return ocelot_mact_wait_for_completion(ocelot); 87a556c76aSAlexandre Belloni } 889c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn); 89a556c76aSAlexandre Belloni 909c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot, 919c90eea3SVladimir Oltean const unsigned char mac[ETH_ALEN], unsigned int vid) 92a556c76aSAlexandre Belloni { 93a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 94a556c76aSAlexandre Belloni 95a556c76aSAlexandre Belloni /* Issue a forget command */ 96a556c76aSAlexandre Belloni ocelot_write(ocelot, 97a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), 98a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 99a556c76aSAlexandre Belloni 100a556c76aSAlexandre Belloni return ocelot_mact_wait_for_completion(ocelot); 101a556c76aSAlexandre Belloni } 1029c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget); 103a556c76aSAlexandre Belloni 104a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot) 105a556c76aSAlexandre Belloni { 106a556c76aSAlexandre Belloni /* Configure the learning mode entries attributes: 107a556c76aSAlexandre Belloni * - Do not copy the frame to the CPU extraction queues. 108a556c76aSAlexandre Belloni * - Use the vlan and mac_cpoy for dmac lookup. 109a556c76aSAlexandre Belloni */ 110a556c76aSAlexandre Belloni ocelot_rmw(ocelot, 0, 111a556c76aSAlexandre Belloni ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS 112a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_FWD_KILL 113a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_IGNORE_VLAN, 114a556c76aSAlexandre Belloni ANA_AGENCTRL); 115a556c76aSAlexandre Belloni 116a556c76aSAlexandre Belloni /* Clear the MAC table */ 117a556c76aSAlexandre Belloni ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); 118a556c76aSAlexandre Belloni } 119a556c76aSAlexandre Belloni 120f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port) 121b5962294SHoratiu Vultur { 122b5962294SHoratiu Vultur ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | 123b5962294SHoratiu Vultur ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), 124f270dbfaSVladimir Oltean ANA_PORT_VCAP_S2_CFG, port); 12575944fdaSXiaoliang Yang 12675944fdaSXiaoliang Yang ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, 12775944fdaSXiaoliang Yang ANA_PORT_VCAP_CFG, port); 1282f17c050SXiaoliang Yang 1292f17c050SXiaoliang Yang ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, 1302f17c050SXiaoliang Yang REW_PORT_CFG_ES0_EN, 1312f17c050SXiaoliang Yang REW_PORT_CFG, port); 132b5962294SHoratiu Vultur } 133b5962294SHoratiu Vultur 134639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) 135639c1b26SSteen Hegelund { 136639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); 137639c1b26SSteen Hegelund } 138639c1b26SSteen Hegelund 139a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) 140a556c76aSAlexandre Belloni { 141639c1b26SSteen Hegelund u32 val; 142a556c76aSAlexandre Belloni 143639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_vlant_read_vlanaccess, 144639c1b26SSteen Hegelund ocelot, 145639c1b26SSteen Hegelund val, 146639c1b26SSteen Hegelund (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == 147639c1b26SSteen Hegelund ANA_TABLES_VLANACCESS_CMD_IDLE, 148639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 149a556c76aSAlexandre Belloni } 150a556c76aSAlexandre Belloni 1517142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) 1527142529fSAntoine Tenart { 1537142529fSAntoine Tenart /* Select the VID to configure */ 1547142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), 1557142529fSAntoine Tenart ANA_TABLES_VLANTIDX); 1567142529fSAntoine Tenart /* Set the vlan port members mask and issue a write command */ 1577142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | 1587142529fSAntoine Tenart ANA_TABLES_VLANACCESS_CMD_WRITE, 1597142529fSAntoine Tenart ANA_TABLES_VLANACCESS); 1607142529fSAntoine Tenart 1617142529fSAntoine Tenart return ocelot_vlant_wait_for_completion(ocelot); 1627142529fSAntoine Tenart } 1637142529fSAntoine Tenart 1642f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port, 165c3e58a75SVladimir Oltean struct ocelot_vlan native_vlan) 16697bb69e1SVladimir Oltean { 16797bb69e1SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 16887b0f983SVladimir Oltean u32 val = 0; 16997bb69e1SVladimir Oltean 170c3e58a75SVladimir Oltean ocelot_port->native_vlan = native_vlan; 17197bb69e1SVladimir Oltean 172c3e58a75SVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid), 1737142529fSAntoine Tenart REW_PORT_VLAN_CFG_PORT_VID_M, 17497bb69e1SVladimir Oltean REW_PORT_VLAN_CFG, port); 17597bb69e1SVladimir Oltean 17687b0f983SVladimir Oltean if (ocelot_port->vlan_aware) { 177e2b2e83eSVladimir Oltean if (native_vlan.valid) 17887b0f983SVladimir Oltean /* Tag all frames except when VID == DEFAULT_VLAN */ 17987b0f983SVladimir Oltean val = REW_TAG_CFG_TAG_CFG(1); 18087b0f983SVladimir Oltean else 18187b0f983SVladimir Oltean /* Tag all frames */ 18287b0f983SVladimir Oltean val = REW_TAG_CFG_TAG_CFG(3); 18387b0f983SVladimir Oltean } else { 18487b0f983SVladimir Oltean /* Port tagging disabled. */ 18587b0f983SVladimir Oltean val = REW_TAG_CFG_TAG_CFG(0); 18687b0f983SVladimir Oltean } 18787b0f983SVladimir Oltean ocelot_rmw_gix(ocelot, val, 18887b0f983SVladimir Oltean REW_TAG_CFG_TAG_CFG_M, 18987b0f983SVladimir Oltean REW_TAG_CFG, port); 19097bb69e1SVladimir Oltean } 19197bb69e1SVladimir Oltean 19275e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */ 193c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, 194c3e58a75SVladimir Oltean struct ocelot_vlan pvid_vlan) 19575e5a554SVladimir Oltean { 19675e5a554SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 197be0576feSVladimir Oltean u32 val = 0; 19875e5a554SVladimir Oltean 199c3e58a75SVladimir Oltean ocelot_port->pvid_vlan = pvid_vlan; 20075e5a554SVladimir Oltean 20175e5a554SVladimir Oltean if (!ocelot_port->vlan_aware) 202c3e58a75SVladimir Oltean pvid_vlan.vid = 0; 20375e5a554SVladimir Oltean 20475e5a554SVladimir Oltean ocelot_rmw_gix(ocelot, 205c3e58a75SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid), 20675e5a554SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID_M, 20775e5a554SVladimir Oltean ANA_PORT_VLAN_CFG, port); 208be0576feSVladimir Oltean 209be0576feSVladimir Oltean /* If there's no pvid, we should drop not only untagged traffic (which 210be0576feSVladimir Oltean * happens automatically), but also 802.1p traffic which gets 211be0576feSVladimir Oltean * classified to VLAN 0, but that is always in our RX filter, so it 212be0576feSVladimir Oltean * would get accepted were it not for this setting. 213be0576feSVladimir Oltean */ 214be0576feSVladimir Oltean if (!pvid_vlan.valid && ocelot_port->vlan_aware) 215be0576feSVladimir Oltean val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 216be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; 217be0576feSVladimir Oltean 218be0576feSVladimir Oltean ocelot_rmw_gix(ocelot, val, 219be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 220be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, 221be0576feSVladimir Oltean ANA_PORT_DROP_CFG, port); 22275e5a554SVladimir Oltean } 22375e5a554SVladimir Oltean 2242e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, 225bae33f2bSVladimir Oltean bool vlan_aware) 22687b0f983SVladimir Oltean { 22770edfae1SVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; 228bae33f2bSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 22970edfae1SVladimir Oltean struct ocelot_vcap_filter *filter; 230bae33f2bSVladimir Oltean u32 val; 23170edfae1SVladimir Oltean 23270edfae1SVladimir Oltean list_for_each_entry(filter, &block->rules, list) { 23370edfae1SVladimir Oltean if (filter->ingress_port_mask & BIT(port) && 23470edfae1SVladimir Oltean filter->action.vid_replace_ena) { 23570edfae1SVladimir Oltean dev_err(ocelot->dev, 23670edfae1SVladimir Oltean "Cannot change VLAN state with vlan modify rules active\n"); 23770edfae1SVladimir Oltean return -EBUSY; 23870edfae1SVladimir Oltean } 23970edfae1SVladimir Oltean } 24070edfae1SVladimir Oltean 24187b0f983SVladimir Oltean ocelot_port->vlan_aware = vlan_aware; 24287b0f983SVladimir Oltean 24387b0f983SVladimir Oltean if (vlan_aware) 24487b0f983SVladimir Oltean val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 24587b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); 24687b0f983SVladimir Oltean else 24787b0f983SVladimir Oltean val = 0; 24887b0f983SVladimir Oltean ocelot_rmw_gix(ocelot, val, 24987b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 25087b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, 25187b0f983SVladimir Oltean ANA_PORT_VLAN_CFG, port); 25287b0f983SVladimir Oltean 253c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); 254c3e58a75SVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan); 2552e554a7aSVladimir Oltean 2562e554a7aSVladimir Oltean return 0; 25787b0f983SVladimir Oltean } 25887b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering); 25987b0f983SVladimir Oltean 2602f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, 2612f0402feSVladimir Oltean bool untagged) 2622f0402feSVladimir Oltean { 2632f0402feSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2642f0402feSVladimir Oltean 2652f0402feSVladimir Oltean /* Deny changing the native VLAN, but always permit deleting it */ 2662f0402feSVladimir Oltean if (untagged && ocelot_port->native_vlan.vid != vid && 2672f0402feSVladimir Oltean ocelot_port->native_vlan.valid) { 2682f0402feSVladimir Oltean dev_err(ocelot->dev, 2692f0402feSVladimir Oltean "Port already has a native VLAN: %d\n", 2702f0402feSVladimir Oltean ocelot_port->native_vlan.vid); 2712f0402feSVladimir Oltean return -EBUSY; 2722f0402feSVladimir Oltean } 2732f0402feSVladimir Oltean 2742f0402feSVladimir Oltean return 0; 2752f0402feSVladimir Oltean } 2762f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare); 2772f0402feSVladimir Oltean 2785e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, 2797142529fSAntoine Tenart bool untagged) 2807142529fSAntoine Tenart { 2817142529fSAntoine Tenart int ret; 2827142529fSAntoine Tenart 2837142529fSAntoine Tenart /* Make the port a member of the VLAN */ 28497bb69e1SVladimir Oltean ocelot->vlan_mask[vid] |= BIT(port); 2857142529fSAntoine Tenart ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 2867142529fSAntoine Tenart if (ret) 2877142529fSAntoine Tenart return ret; 2887142529fSAntoine Tenart 2897142529fSAntoine Tenart /* Default ingress vlan classification */ 290c3e58a75SVladimir Oltean if (pvid) { 291c3e58a75SVladimir Oltean struct ocelot_vlan pvid_vlan; 292c3e58a75SVladimir Oltean 293c3e58a75SVladimir Oltean pvid_vlan.vid = vid; 294e2b2e83eSVladimir Oltean pvid_vlan.valid = true; 295c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, pvid_vlan); 296c3e58a75SVladimir Oltean } 2977142529fSAntoine Tenart 2987142529fSAntoine Tenart /* Untagged egress vlan clasification */ 29997bb69e1SVladimir Oltean if (untagged) { 300c3e58a75SVladimir Oltean struct ocelot_vlan native_vlan; 301c3e58a75SVladimir Oltean 302c3e58a75SVladimir Oltean native_vlan.vid = vid; 303e2b2e83eSVladimir Oltean native_vlan.valid = true; 3042f0402feSVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, native_vlan); 305b9cd75e6SVladimir Oltean } 3067142529fSAntoine Tenart 3077142529fSAntoine Tenart return 0; 3087142529fSAntoine Tenart } 3095e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add); 3107142529fSAntoine Tenart 3115e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) 3129855934cSVladimir Oltean { 3139855934cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 3149855934cSVladimir Oltean int ret; 3157142529fSAntoine Tenart 3167142529fSAntoine Tenart /* Stop the port from being a member of the vlan */ 31797bb69e1SVladimir Oltean ocelot->vlan_mask[vid] &= ~BIT(port); 3187142529fSAntoine Tenart ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 3197142529fSAntoine Tenart if (ret) 3207142529fSAntoine Tenart return ret; 3217142529fSAntoine Tenart 322be0576feSVladimir Oltean /* Ingress */ 323be0576feSVladimir Oltean if (ocelot_port->pvid_vlan.vid == vid) { 324be0576feSVladimir Oltean struct ocelot_vlan pvid_vlan = {0}; 325be0576feSVladimir Oltean 326be0576feSVladimir Oltean ocelot_port_set_pvid(ocelot, port, pvid_vlan); 327be0576feSVladimir Oltean } 328be0576feSVladimir Oltean 3297142529fSAntoine Tenart /* Egress */ 330c3e58a75SVladimir Oltean if (ocelot_port->native_vlan.vid == vid) { 331e2b2e83eSVladimir Oltean struct ocelot_vlan native_vlan = {0}; 332c3e58a75SVladimir Oltean 333c3e58a75SVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, native_vlan); 334c3e58a75SVladimir Oltean } 3357142529fSAntoine Tenart 3367142529fSAntoine Tenart return 0; 3377142529fSAntoine Tenart } 3385e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del); 3397142529fSAntoine Tenart 340a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot) 341a556c76aSAlexandre Belloni { 3427142529fSAntoine Tenart u16 port, vid; 3437142529fSAntoine Tenart 344a556c76aSAlexandre Belloni /* Clear VLAN table, by default all ports are members of all VLANs */ 345a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, 346a556c76aSAlexandre Belloni ANA_TABLES_VLANACCESS); 347a556c76aSAlexandre Belloni ocelot_vlant_wait_for_completion(ocelot); 3487142529fSAntoine Tenart 3497142529fSAntoine Tenart /* Configure the port VLAN memberships */ 3507142529fSAntoine Tenart for (vid = 1; vid < VLAN_N_VID; vid++) { 3517142529fSAntoine Tenart ocelot->vlan_mask[vid] = 0; 3527142529fSAntoine Tenart ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 3537142529fSAntoine Tenart } 3547142529fSAntoine Tenart 3557142529fSAntoine Tenart /* Because VLAN filtering is enabled, we need VID 0 to get untagged 3567142529fSAntoine Tenart * traffic. It is added automatically if 8021q module is loaded, but 3577142529fSAntoine Tenart * we can't rely on it since module may be not loaded. 3587142529fSAntoine Tenart */ 3597142529fSAntoine Tenart ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0); 3607142529fSAntoine Tenart ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]); 3617142529fSAntoine Tenart 3627142529fSAntoine Tenart /* Set vlan ingress filter mask to all ports but the CPU port by 3637142529fSAntoine Tenart * default. 3647142529fSAntoine Tenart */ 365714d0ffaSVladimir Oltean ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 366714d0ffaSVladimir Oltean ANA_VLANMASK); 3677142529fSAntoine Tenart 3687142529fSAntoine Tenart for (port = 0; port < ocelot->num_phys_ports; port++) { 3697142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); 3707142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); 3717142529fSAntoine Tenart } 372a556c76aSAlexandre Belloni } 373a556c76aSAlexandre Belloni 374eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port) 375eb4733d7SVladimir Oltean { 376eb4733d7SVladimir Oltean return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port); 377eb4733d7SVladimir Oltean } 378eb4733d7SVladimir Oltean 379eb4733d7SVladimir Oltean int ocelot_port_flush(struct ocelot *ocelot, int port) 380eb4733d7SVladimir Oltean { 381eb4733d7SVladimir Oltean int err, val; 382eb4733d7SVladimir Oltean 383eb4733d7SVladimir Oltean /* Disable dequeuing from the egress queues */ 384eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS, 385eb4733d7SVladimir Oltean QSYS_PORT_MODE_DEQUEUE_DIS, 386eb4733d7SVladimir Oltean QSYS_PORT_MODE, port); 387eb4733d7SVladimir Oltean 388eb4733d7SVladimir Oltean /* Disable flow control */ 389eb4733d7SVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 390eb4733d7SVladimir Oltean 391eb4733d7SVladimir Oltean /* Disable priority flow control */ 392eb4733d7SVladimir Oltean ocelot_fields_write(ocelot, port, 393eb4733d7SVladimir Oltean QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0); 394eb4733d7SVladimir Oltean 395eb4733d7SVladimir Oltean /* Wait at least the time it takes to receive a frame of maximum length 396eb4733d7SVladimir Oltean * at the port. 397eb4733d7SVladimir Oltean * Worst-case delays for 10 kilobyte jumbo frames are: 398eb4733d7SVladimir Oltean * 8 ms on a 10M port 399eb4733d7SVladimir Oltean * 800 μs on a 100M port 400eb4733d7SVladimir Oltean * 80 μs on a 1G port 401eb4733d7SVladimir Oltean * 32 μs on a 2.5G port 402eb4733d7SVladimir Oltean */ 403eb4733d7SVladimir Oltean usleep_range(8000, 10000); 404eb4733d7SVladimir Oltean 405eb4733d7SVladimir Oltean /* Disable half duplex backpressure. */ 406eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE, 407eb4733d7SVladimir Oltean SYS_FRONT_PORT_MODE, port); 408eb4733d7SVladimir Oltean 409eb4733d7SVladimir Oltean /* Flush the queues associated with the port. */ 410eb4733d7SVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA, 411eb4733d7SVladimir Oltean REW_PORT_CFG, port); 412eb4733d7SVladimir Oltean 413eb4733d7SVladimir Oltean /* Enable dequeuing from the egress queues. */ 414eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE, 415eb4733d7SVladimir Oltean port); 416eb4733d7SVladimir Oltean 417eb4733d7SVladimir Oltean /* Wait until flushing is complete. */ 418eb4733d7SVladimir Oltean err = read_poll_timeout(ocelot_read_eq_avail, val, !val, 419eb4733d7SVladimir Oltean 100, 2000000, false, ocelot, port); 420eb4733d7SVladimir Oltean 421eb4733d7SVladimir Oltean /* Clear flushing again. */ 422eb4733d7SVladimir Oltean ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port); 423eb4733d7SVladimir Oltean 424eb4733d7SVladimir Oltean return err; 425eb4733d7SVladimir Oltean } 426eb4733d7SVladimir Oltean EXPORT_SYMBOL(ocelot_port_flush); 427eb4733d7SVladimir Oltean 4285e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port, 42926f4dbabSVladimir Oltean struct phy_device *phydev) 430a556c76aSAlexandre Belloni { 43126f4dbabSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 4325bc9d2e6SVladimir Oltean int speed, mode = 0; 433a556c76aSAlexandre Belloni 43426f4dbabSVladimir Oltean switch (phydev->speed) { 435a556c76aSAlexandre Belloni case SPEED_10: 436a556c76aSAlexandre Belloni speed = OCELOT_SPEED_10; 437a556c76aSAlexandre Belloni break; 438a556c76aSAlexandre Belloni case SPEED_100: 439a556c76aSAlexandre Belloni speed = OCELOT_SPEED_100; 440a556c76aSAlexandre Belloni break; 441a556c76aSAlexandre Belloni case SPEED_1000: 442a556c76aSAlexandre Belloni speed = OCELOT_SPEED_1000; 443a556c76aSAlexandre Belloni mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 444a556c76aSAlexandre Belloni break; 445a556c76aSAlexandre Belloni case SPEED_2500: 446a556c76aSAlexandre Belloni speed = OCELOT_SPEED_2500; 447a556c76aSAlexandre Belloni mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 448a556c76aSAlexandre Belloni break; 449a556c76aSAlexandre Belloni default: 45026f4dbabSVladimir Oltean dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n", 45126f4dbabSVladimir Oltean port, phydev->speed); 452a556c76aSAlexandre Belloni return; 453a556c76aSAlexandre Belloni } 454a556c76aSAlexandre Belloni 45526f4dbabSVladimir Oltean phy_print_status(phydev); 456a556c76aSAlexandre Belloni 45726f4dbabSVladimir Oltean if (!phydev->link) 458a556c76aSAlexandre Belloni return; 459a556c76aSAlexandre Belloni 460a556c76aSAlexandre Belloni /* Only full duplex supported for now */ 461004d44f6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA | 462a556c76aSAlexandre Belloni mode, DEV_MAC_MODE_CFG); 463a556c76aSAlexandre Belloni 4641ba8f656SVladimir Oltean /* Disable HDX fast control */ 4651ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS, 4661ba8f656SVladimir Oltean DEV_PORT_MISC); 4671ba8f656SVladimir Oltean 4681ba8f656SVladimir Oltean /* SGMII only for now */ 4691ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA, 4701ba8f656SVladimir Oltean PCS1G_MODE_CFG); 4711ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG); 4721ba8f656SVladimir Oltean 4731ba8f656SVladimir Oltean /* Enable PCS */ 4741ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG); 4751ba8f656SVladimir Oltean 4761ba8f656SVladimir Oltean /* No aneg on SGMII */ 4771ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG); 4781ba8f656SVladimir Oltean 4791ba8f656SVladimir Oltean /* No loopback */ 4801ba8f656SVladimir Oltean ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG); 481a556c76aSAlexandre Belloni 482a556c76aSAlexandre Belloni /* Enable MAC module */ 483004d44f6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | 484a556c76aSAlexandre Belloni DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 485a556c76aSAlexandre Belloni 486a556c76aSAlexandre Belloni /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of 487a556c76aSAlexandre Belloni * reset */ 488004d44f6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed), 489a556c76aSAlexandre Belloni DEV_CLOCK_CFG); 490a556c76aSAlexandre Belloni 491a556c76aSAlexandre Belloni /* No PFC */ 492a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed), 493004d44f6SVladimir Oltean ANA_PFC_PFC_CFG, port); 494a556c76aSAlexandre Belloni 495a556c76aSAlexandre Belloni /* Core: Enable port for frame transfer */ 496886e1387SVladimir Oltean ocelot_fields_write(ocelot, port, 497886e1387SVladimir Oltean QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 498a556c76aSAlexandre Belloni 499a556c76aSAlexandre Belloni /* Flow control */ 500a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 501a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA | 502a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_ZERO_PAUSE_ENA | 503a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 504a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_FC_LINK_SPEED(speed), 505004d44f6SVladimir Oltean SYS_MAC_FC_CFG, port); 506004d44f6SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); 507a556c76aSAlexandre Belloni } 5085e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link); 509a556c76aSAlexandre Belloni 5105e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port, 511889b8950SVladimir Oltean struct phy_device *phy) 512a556c76aSAlexandre Belloni { 513a556c76aSAlexandre Belloni /* Enable receiving frames on the port, and activate auto-learning of 514a556c76aSAlexandre Belloni * MAC addresses. 515a556c76aSAlexandre Belloni */ 516a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 517a556c76aSAlexandre Belloni ANA_PORT_PORT_CFG_RECV_ENA | 518004d44f6SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 519004d44f6SVladimir Oltean ANA_PORT_PORT_CFG, port); 520889b8950SVladimir Oltean } 5215e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable); 522889b8950SVladimir Oltean 5235e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port) 524889b8950SVladimir Oltean { 525889b8950SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 526889b8950SVladimir Oltean 527889b8950SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG); 528886e1387SVladimir Oltean ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); 529889b8950SVladimir Oltean } 5305e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable); 531889b8950SVladimir Oltean 532e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, 533e2f9a8feSVladimir Oltean struct sk_buff *clone) 534400928bfSYangbo Lu { 535e2f9a8feSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 536400928bfSYangbo Lu 5376565243cSVladimir Oltean spin_lock(&ocelot_port->ts_id_lock); 5386565243cSVladimir Oltean 539e2f9a8feSVladimir Oltean skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; 540b049da13SYangbo Lu /* Store timestamp ID in cb[0] of sk_buff */ 541e2f9a8feSVladimir Oltean clone->cb[0] = ocelot_port->ts_id; 5426565243cSVladimir Oltean ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4; 543e2f9a8feSVladimir Oltean skb_queue_tail(&ocelot_port->tx_skbs, clone); 5446565243cSVladimir Oltean 5456565243cSVladimir Oltean spin_unlock(&ocelot_port->ts_id_lock); 546400928bfSYangbo Lu } 547400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb); 548400928bfSYangbo Lu 549e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot, 550e23a7b3eSYangbo Lu struct timespec64 *ts) 5514e3b0468SAntoine Tenart { 5524e3b0468SAntoine Tenart unsigned long flags; 5534e3b0468SAntoine Tenart u32 val; 5544e3b0468SAntoine Tenart 5554e3b0468SAntoine Tenart spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); 5564e3b0468SAntoine Tenart 5574e3b0468SAntoine Tenart /* Read current PTP time to get seconds */ 5584e3b0468SAntoine Tenart val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); 5594e3b0468SAntoine Tenart 5604e3b0468SAntoine Tenart val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); 5614e3b0468SAntoine Tenart val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); 5624e3b0468SAntoine Tenart ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); 5634e3b0468SAntoine Tenart ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); 5644e3b0468SAntoine Tenart 5654e3b0468SAntoine Tenart /* Read packet HW timestamp from FIFO */ 5664e3b0468SAntoine Tenart val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); 5674e3b0468SAntoine Tenart ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); 5684e3b0468SAntoine Tenart 5694e3b0468SAntoine Tenart /* Sec has incremented since the ts was registered */ 5704e3b0468SAntoine Tenart if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) 5714e3b0468SAntoine Tenart ts->tv_sec--; 5724e3b0468SAntoine Tenart 5734e3b0468SAntoine Tenart spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); 5744e3b0468SAntoine Tenart } 575e23a7b3eSYangbo Lu 576e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot) 577e23a7b3eSYangbo Lu { 578e23a7b3eSYangbo Lu int budget = OCELOT_PTP_QUEUE_SZ; 579e23a7b3eSYangbo Lu 580e23a7b3eSYangbo Lu while (budget--) { 581b049da13SYangbo Lu struct sk_buff *skb, *skb_tmp, *skb_match = NULL; 582e23a7b3eSYangbo Lu struct skb_shared_hwtstamps shhwtstamps; 583e23a7b3eSYangbo Lu struct ocelot_port *port; 584e23a7b3eSYangbo Lu struct timespec64 ts; 585b049da13SYangbo Lu unsigned long flags; 586e23a7b3eSYangbo Lu u32 val, id, txport; 587e23a7b3eSYangbo Lu 588e23a7b3eSYangbo Lu val = ocelot_read(ocelot, SYS_PTP_STATUS); 589e23a7b3eSYangbo Lu 590e23a7b3eSYangbo Lu /* Check if a timestamp can be retrieved */ 591e23a7b3eSYangbo Lu if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) 592e23a7b3eSYangbo Lu break; 593e23a7b3eSYangbo Lu 594e23a7b3eSYangbo Lu WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); 595e23a7b3eSYangbo Lu 596e23a7b3eSYangbo Lu /* Retrieve the ts ID and Tx port */ 597e23a7b3eSYangbo Lu id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); 598e23a7b3eSYangbo Lu txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); 599e23a7b3eSYangbo Lu 600e23a7b3eSYangbo Lu /* Retrieve its associated skb */ 601e23a7b3eSYangbo Lu port = ocelot->ports[txport]; 602e23a7b3eSYangbo Lu 603b049da13SYangbo Lu spin_lock_irqsave(&port->tx_skbs.lock, flags); 604b049da13SYangbo Lu 605b049da13SYangbo Lu skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { 606b049da13SYangbo Lu if (skb->cb[0] != id) 607e23a7b3eSYangbo Lu continue; 608b049da13SYangbo Lu __skb_unlink(skb, &port->tx_skbs); 609b049da13SYangbo Lu skb_match = skb; 610fc62c094SYangbo Lu break; 611e23a7b3eSYangbo Lu } 612e23a7b3eSYangbo Lu 613b049da13SYangbo Lu spin_unlock_irqrestore(&port->tx_skbs.lock, flags); 614b049da13SYangbo Lu 6155fd82200Slaurent brando /* Get the h/w timestamp */ 6165fd82200Slaurent brando ocelot_get_hwtimestamp(ocelot, &ts); 617e23a7b3eSYangbo Lu 618b049da13SYangbo Lu if (unlikely(!skb_match)) 619e23a7b3eSYangbo Lu continue; 620e23a7b3eSYangbo Lu 621e23a7b3eSYangbo Lu /* Set the timestamp into the skb */ 622e23a7b3eSYangbo Lu memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 623e23a7b3eSYangbo Lu shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 624e2f9a8feSVladimir Oltean skb_complete_tx_timestamp(skb_match, &shhwtstamps); 6255fd82200Slaurent brando 6265fd82200Slaurent brando /* Next ts */ 6275fd82200Slaurent brando ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); 628e23a7b3eSYangbo Lu } 629e23a7b3eSYangbo Lu } 630e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp); 6314e3b0468SAntoine Tenart 632924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh, 633924ee317SVladimir Oltean u32 *rval) 634924ee317SVladimir Oltean { 635924ee317SVladimir Oltean u32 bytes_valid, val; 636924ee317SVladimir Oltean 637924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 638924ee317SVladimir Oltean if (val == XTR_NOT_READY) { 639924ee317SVladimir Oltean if (ifh) 640924ee317SVladimir Oltean return -EIO; 641924ee317SVladimir Oltean 642924ee317SVladimir Oltean do { 643924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 644924ee317SVladimir Oltean } while (val == XTR_NOT_READY); 645924ee317SVladimir Oltean } 646924ee317SVladimir Oltean 647924ee317SVladimir Oltean switch (val) { 648924ee317SVladimir Oltean case XTR_ABORT: 649924ee317SVladimir Oltean return -EIO; 650924ee317SVladimir Oltean case XTR_EOF_0: 651924ee317SVladimir Oltean case XTR_EOF_1: 652924ee317SVladimir Oltean case XTR_EOF_2: 653924ee317SVladimir Oltean case XTR_EOF_3: 654924ee317SVladimir Oltean case XTR_PRUNED: 655924ee317SVladimir Oltean bytes_valid = XTR_VALID_BYTES(val); 656924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 657924ee317SVladimir Oltean if (val == XTR_ESCAPE) 658924ee317SVladimir Oltean *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 659924ee317SVladimir Oltean else 660924ee317SVladimir Oltean *rval = val; 661924ee317SVladimir Oltean 662924ee317SVladimir Oltean return bytes_valid; 663924ee317SVladimir Oltean case XTR_ESCAPE: 664924ee317SVladimir Oltean *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 665924ee317SVladimir Oltean 666924ee317SVladimir Oltean return 4; 667924ee317SVladimir Oltean default: 668924ee317SVladimir Oltean *rval = val; 669924ee317SVladimir Oltean 670924ee317SVladimir Oltean return 4; 671924ee317SVladimir Oltean } 672924ee317SVladimir Oltean } 673924ee317SVladimir Oltean 674924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh) 675924ee317SVladimir Oltean { 676924ee317SVladimir Oltean int i, err = 0; 677924ee317SVladimir Oltean 678924ee317SVladimir Oltean for (i = 0; i < OCELOT_TAG_LEN / 4; i++) { 679924ee317SVladimir Oltean err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]); 680924ee317SVladimir Oltean if (err != 4) 681924ee317SVladimir Oltean return (err < 0) ? err : -EIO; 682924ee317SVladimir Oltean } 683924ee317SVladimir Oltean 684924ee317SVladimir Oltean return 0; 685924ee317SVladimir Oltean } 686924ee317SVladimir Oltean 687924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb) 688924ee317SVladimir Oltean { 689924ee317SVladimir Oltean struct skb_shared_hwtstamps *shhwtstamps; 690924ee317SVladimir Oltean u64 tod_in_ns, full_ts_in_ns; 691924ee317SVladimir Oltean u64 timestamp, src_port, len; 692924ee317SVladimir Oltean u32 xfh[OCELOT_TAG_LEN / 4]; 693924ee317SVladimir Oltean struct net_device *dev; 694924ee317SVladimir Oltean struct timespec64 ts; 695924ee317SVladimir Oltean struct sk_buff *skb; 696924ee317SVladimir Oltean int sz, buf_len; 697924ee317SVladimir Oltean u32 val, *buf; 698924ee317SVladimir Oltean int err; 699924ee317SVladimir Oltean 700924ee317SVladimir Oltean err = ocelot_xtr_poll_xfh(ocelot, grp, xfh); 701924ee317SVladimir Oltean if (err) 702924ee317SVladimir Oltean return err; 703924ee317SVladimir Oltean 704924ee317SVladimir Oltean ocelot_xfh_get_src_port(xfh, &src_port); 705924ee317SVladimir Oltean ocelot_xfh_get_len(xfh, &len); 706924ee317SVladimir Oltean ocelot_xfh_get_rew_val(xfh, ×tamp); 707924ee317SVladimir Oltean 708924ee317SVladimir Oltean if (WARN_ON(src_port >= ocelot->num_phys_ports)) 709924ee317SVladimir Oltean return -EINVAL; 710924ee317SVladimir Oltean 711924ee317SVladimir Oltean dev = ocelot->ops->port_to_netdev(ocelot, src_port); 712924ee317SVladimir Oltean if (!dev) 713924ee317SVladimir Oltean return -EINVAL; 714924ee317SVladimir Oltean 715924ee317SVladimir Oltean skb = netdev_alloc_skb(dev, len); 716924ee317SVladimir Oltean if (unlikely(!skb)) { 717924ee317SVladimir Oltean netdev_err(dev, "Unable to allocate sk_buff\n"); 718924ee317SVladimir Oltean return -ENOMEM; 719924ee317SVladimir Oltean } 720924ee317SVladimir Oltean 721924ee317SVladimir Oltean buf_len = len - ETH_FCS_LEN; 722924ee317SVladimir Oltean buf = (u32 *)skb_put(skb, buf_len); 723924ee317SVladimir Oltean 724924ee317SVladimir Oltean len = 0; 725924ee317SVladimir Oltean do { 726924ee317SVladimir Oltean sz = ocelot_rx_frame_word(ocelot, grp, false, &val); 727924ee317SVladimir Oltean if (sz < 0) { 728924ee317SVladimir Oltean err = sz; 729924ee317SVladimir Oltean goto out_free_skb; 730924ee317SVladimir Oltean } 731924ee317SVladimir Oltean *buf++ = val; 732924ee317SVladimir Oltean len += sz; 733924ee317SVladimir Oltean } while (len < buf_len); 734924ee317SVladimir Oltean 735924ee317SVladimir Oltean /* Read the FCS */ 736924ee317SVladimir Oltean sz = ocelot_rx_frame_word(ocelot, grp, false, &val); 737924ee317SVladimir Oltean if (sz < 0) { 738924ee317SVladimir Oltean err = sz; 739924ee317SVladimir Oltean goto out_free_skb; 740924ee317SVladimir Oltean } 741924ee317SVladimir Oltean 742924ee317SVladimir Oltean /* Update the statistics if part of the FCS was read before */ 743924ee317SVladimir Oltean len -= ETH_FCS_LEN - sz; 744924ee317SVladimir Oltean 745924ee317SVladimir Oltean if (unlikely(dev->features & NETIF_F_RXFCS)) { 746924ee317SVladimir Oltean buf = (u32 *)skb_put(skb, ETH_FCS_LEN); 747924ee317SVladimir Oltean *buf = val; 748924ee317SVladimir Oltean } 749924ee317SVladimir Oltean 750924ee317SVladimir Oltean if (ocelot->ptp) { 751924ee317SVladimir Oltean ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 752924ee317SVladimir Oltean 753924ee317SVladimir Oltean tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec); 754924ee317SVladimir Oltean if ((tod_in_ns & 0xffffffff) < timestamp) 755924ee317SVladimir Oltean full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) | 756924ee317SVladimir Oltean timestamp; 757924ee317SVladimir Oltean else 758924ee317SVladimir Oltean full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) | 759924ee317SVladimir Oltean timestamp; 760924ee317SVladimir Oltean 761924ee317SVladimir Oltean shhwtstamps = skb_hwtstamps(skb); 762924ee317SVladimir Oltean memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 763924ee317SVladimir Oltean shhwtstamps->hwtstamp = full_ts_in_ns; 764924ee317SVladimir Oltean } 765924ee317SVladimir Oltean 766924ee317SVladimir Oltean /* Everything we see on an interface that is in the HW bridge 767924ee317SVladimir Oltean * has already been forwarded. 768924ee317SVladimir Oltean */ 769924ee317SVladimir Oltean if (ocelot->bridge_mask & BIT(src_port)) 770924ee317SVladimir Oltean skb->offload_fwd_mark = 1; 771924ee317SVladimir Oltean 772924ee317SVladimir Oltean skb->protocol = eth_type_trans(skb, dev); 773924ee317SVladimir Oltean *nskb = skb; 774924ee317SVladimir Oltean 775924ee317SVladimir Oltean return 0; 776924ee317SVladimir Oltean 777924ee317SVladimir Oltean out_free_skb: 778924ee317SVladimir Oltean kfree_skb(skb); 779924ee317SVladimir Oltean return err; 780924ee317SVladimir Oltean } 781924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame); 782924ee317SVladimir Oltean 783137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp) 784137ffbc4SVladimir Oltean { 785137ffbc4SVladimir Oltean u32 val = ocelot_read(ocelot, QS_INJ_STATUS); 786137ffbc4SVladimir Oltean 787137ffbc4SVladimir Oltean if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp)))) 788137ffbc4SVladimir Oltean return false; 789137ffbc4SVladimir Oltean if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))) 790137ffbc4SVladimir Oltean return false; 791137ffbc4SVladimir Oltean 792137ffbc4SVladimir Oltean return true; 793137ffbc4SVladimir Oltean } 794137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject); 795137ffbc4SVladimir Oltean 796137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp, 797137ffbc4SVladimir Oltean u32 rew_op, struct sk_buff *skb) 798137ffbc4SVladimir Oltean { 79940d3f295SVladimir Oltean u32 ifh[OCELOT_TAG_LEN / 4] = {0}; 800137ffbc4SVladimir Oltean unsigned int i, count, last; 801137ffbc4SVladimir Oltean 802137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 803137ffbc4SVladimir Oltean QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp); 804137ffbc4SVladimir Oltean 80540d3f295SVladimir Oltean ocelot_ifh_set_bypass(ifh, 1); 806*1f778d50SVladimir Oltean ocelot_ifh_set_dest(ifh, BIT_ULL(port)); 80740d3f295SVladimir Oltean ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C); 80840d3f295SVladimir Oltean ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb)); 80940d3f295SVladimir Oltean ocelot_ifh_set_rew_op(ifh, rew_op); 810137ffbc4SVladimir Oltean 811137ffbc4SVladimir Oltean for (i = 0; i < OCELOT_TAG_LEN / 4; i++) 81240d3f295SVladimir Oltean ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp); 813137ffbc4SVladimir Oltean 814137ffbc4SVladimir Oltean count = DIV_ROUND_UP(skb->len, 4); 815137ffbc4SVladimir Oltean last = skb->len % 4; 816137ffbc4SVladimir Oltean for (i = 0; i < count; i++) 817137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp); 818137ffbc4SVladimir Oltean 819137ffbc4SVladimir Oltean /* Add padding */ 820137ffbc4SVladimir Oltean while (i < (OCELOT_BUFFER_CELL_SZ / 4)) { 821137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 822137ffbc4SVladimir Oltean i++; 823137ffbc4SVladimir Oltean } 824137ffbc4SVladimir Oltean 825137ffbc4SVladimir Oltean /* Indicate EOF and valid bytes in last word */ 826137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 827137ffbc4SVladimir Oltean QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) | 828137ffbc4SVladimir Oltean QS_INJ_CTRL_EOF, 829137ffbc4SVladimir Oltean QS_INJ_CTRL, grp); 830137ffbc4SVladimir Oltean 831137ffbc4SVladimir Oltean /* Add dummy CRC */ 832137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 833137ffbc4SVladimir Oltean skb_tx_timestamp(skb); 834137ffbc4SVladimir Oltean 835137ffbc4SVladimir Oltean skb->dev->stats.tx_packets++; 836137ffbc4SVladimir Oltean skb->dev->stats.tx_bytes += skb->len; 837137ffbc4SVladimir Oltean } 838137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame); 839137ffbc4SVladimir Oltean 8400a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp) 8410a6f17c6SVladimir Oltean { 8420a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) 8430a6f17c6SVladimir Oltean ocelot_read_rix(ocelot, QS_XTR_RD, grp); 8440a6f17c6SVladimir Oltean } 8450a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue); 8460a6f17c6SVladimir Oltean 8475e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, 84887b0f983SVladimir Oltean const unsigned char *addr, u16 vid) 849a556c76aSAlexandre Belloni { 850471beb11SVladimir Oltean int pgid = port; 851471beb11SVladimir Oltean 852471beb11SVladimir Oltean if (port == ocelot->npi) 853471beb11SVladimir Oltean pgid = PGID_CPU; 854a556c76aSAlexandre Belloni 855471beb11SVladimir Oltean return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); 856a556c76aSAlexandre Belloni } 8575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add); 858a556c76aSAlexandre Belloni 8595e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, 860531ee1a6SVladimir Oltean const unsigned char *addr, u16 vid) 861531ee1a6SVladimir Oltean { 862531ee1a6SVladimir Oltean return ocelot_mact_forget(ocelot, addr, vid); 863531ee1a6SVladimir Oltean } 8645e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del); 865531ee1a6SVladimir Oltean 8669c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, 867531ee1a6SVladimir Oltean bool is_static, void *data) 868a556c76aSAlexandre Belloni { 869531ee1a6SVladimir Oltean struct ocelot_dump_ctx *dump = data; 870a556c76aSAlexandre Belloni u32 portid = NETLINK_CB(dump->cb->skb).portid; 871a556c76aSAlexandre Belloni u32 seq = dump->cb->nlh->nlmsg_seq; 872a556c76aSAlexandre Belloni struct nlmsghdr *nlh; 873a556c76aSAlexandre Belloni struct ndmsg *ndm; 874a556c76aSAlexandre Belloni 875a556c76aSAlexandre Belloni if (dump->idx < dump->cb->args[2]) 876a556c76aSAlexandre Belloni goto skip; 877a556c76aSAlexandre Belloni 878a556c76aSAlexandre Belloni nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 879a556c76aSAlexandre Belloni sizeof(*ndm), NLM_F_MULTI); 880a556c76aSAlexandre Belloni if (!nlh) 881a556c76aSAlexandre Belloni return -EMSGSIZE; 882a556c76aSAlexandre Belloni 883a556c76aSAlexandre Belloni ndm = nlmsg_data(nlh); 884a556c76aSAlexandre Belloni ndm->ndm_family = AF_BRIDGE; 885a556c76aSAlexandre Belloni ndm->ndm_pad1 = 0; 886a556c76aSAlexandre Belloni ndm->ndm_pad2 = 0; 887a556c76aSAlexandre Belloni ndm->ndm_flags = NTF_SELF; 888a556c76aSAlexandre Belloni ndm->ndm_type = 0; 889a556c76aSAlexandre Belloni ndm->ndm_ifindex = dump->dev->ifindex; 890531ee1a6SVladimir Oltean ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 891a556c76aSAlexandre Belloni 892531ee1a6SVladimir Oltean if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 893a556c76aSAlexandre Belloni goto nla_put_failure; 894a556c76aSAlexandre Belloni 895531ee1a6SVladimir Oltean if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 896a556c76aSAlexandre Belloni goto nla_put_failure; 897a556c76aSAlexandre Belloni 898a556c76aSAlexandre Belloni nlmsg_end(dump->skb, nlh); 899a556c76aSAlexandre Belloni 900a556c76aSAlexandre Belloni skip: 901a556c76aSAlexandre Belloni dump->idx++; 902a556c76aSAlexandre Belloni return 0; 903a556c76aSAlexandre Belloni 904a556c76aSAlexandre Belloni nla_put_failure: 905a556c76aSAlexandre Belloni nlmsg_cancel(dump->skb, nlh); 906a556c76aSAlexandre Belloni return -EMSGSIZE; 907a556c76aSAlexandre Belloni } 9089c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump); 909a556c76aSAlexandre Belloni 910531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, 911a556c76aSAlexandre Belloni struct ocelot_mact_entry *entry) 912a556c76aSAlexandre Belloni { 913a556c76aSAlexandre Belloni u32 val, dst, macl, mach; 914531ee1a6SVladimir Oltean char mac[ETH_ALEN]; 915a556c76aSAlexandre Belloni 916a556c76aSAlexandre Belloni /* Set row and column to read from */ 917a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); 918a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); 919a556c76aSAlexandre Belloni 920a556c76aSAlexandre Belloni /* Issue a read command */ 921a556c76aSAlexandre Belloni ocelot_write(ocelot, 922a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 923a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 924a556c76aSAlexandre Belloni 925a556c76aSAlexandre Belloni if (ocelot_mact_wait_for_completion(ocelot)) 926a556c76aSAlexandre Belloni return -ETIMEDOUT; 927a556c76aSAlexandre Belloni 928a556c76aSAlexandre Belloni /* Read the entry flags */ 929a556c76aSAlexandre Belloni val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 930a556c76aSAlexandre Belloni if (!(val & ANA_TABLES_MACACCESS_VALID)) 931a556c76aSAlexandre Belloni return -EINVAL; 932a556c76aSAlexandre Belloni 933a556c76aSAlexandre Belloni /* If the entry read has another port configured as its destination, 934a556c76aSAlexandre Belloni * do not report it. 935a556c76aSAlexandre Belloni */ 936a556c76aSAlexandre Belloni dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; 937531ee1a6SVladimir Oltean if (dst != port) 938a556c76aSAlexandre Belloni return -EINVAL; 939a556c76aSAlexandre Belloni 940a556c76aSAlexandre Belloni /* Get the entry's MAC address and VLAN id */ 941a556c76aSAlexandre Belloni macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); 942a556c76aSAlexandre Belloni mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); 943a556c76aSAlexandre Belloni 944a556c76aSAlexandre Belloni mac[0] = (mach >> 8) & 0xff; 945a556c76aSAlexandre Belloni mac[1] = (mach >> 0) & 0xff; 946a556c76aSAlexandre Belloni mac[2] = (macl >> 24) & 0xff; 947a556c76aSAlexandre Belloni mac[3] = (macl >> 16) & 0xff; 948a556c76aSAlexandre Belloni mac[4] = (macl >> 8) & 0xff; 949a556c76aSAlexandre Belloni mac[5] = (macl >> 0) & 0xff; 950a556c76aSAlexandre Belloni 951a556c76aSAlexandre Belloni entry->vid = (mach >> 16) & 0xfff; 952a556c76aSAlexandre Belloni ether_addr_copy(entry->mac, mac); 953a556c76aSAlexandre Belloni 954a556c76aSAlexandre Belloni return 0; 955a556c76aSAlexandre Belloni } 956a556c76aSAlexandre Belloni 9575e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port, 958531ee1a6SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 959a556c76aSAlexandre Belloni { 960531ee1a6SVladimir Oltean int i, j; 961a556c76aSAlexandre Belloni 96221ce7f3eSVladimir Oltean /* Loop through all the mac tables entries. */ 96321ce7f3eSVladimir Oltean for (i = 0; i < ocelot->num_mact_rows; i++) { 964a556c76aSAlexandre Belloni for (j = 0; j < 4; j++) { 965531ee1a6SVladimir Oltean struct ocelot_mact_entry entry; 966531ee1a6SVladimir Oltean bool is_static; 967531ee1a6SVladimir Oltean int ret; 968531ee1a6SVladimir Oltean 969531ee1a6SVladimir Oltean ret = ocelot_mact_read(ocelot, port, i, j, &entry); 970a556c76aSAlexandre Belloni /* If the entry is invalid (wrong port, invalid...), 971a556c76aSAlexandre Belloni * skip it. 972a556c76aSAlexandre Belloni */ 973a556c76aSAlexandre Belloni if (ret == -EINVAL) 974a556c76aSAlexandre Belloni continue; 975a556c76aSAlexandre Belloni else if (ret) 976531ee1a6SVladimir Oltean return ret; 977a556c76aSAlexandre Belloni 978531ee1a6SVladimir Oltean is_static = (entry.type == ENTRYTYPE_LOCKED); 979531ee1a6SVladimir Oltean 980531ee1a6SVladimir Oltean ret = cb(entry.mac, entry.vid, is_static, data); 981a556c76aSAlexandre Belloni if (ret) 982531ee1a6SVladimir Oltean return ret; 983a556c76aSAlexandre Belloni } 984a556c76aSAlexandre Belloni } 985a556c76aSAlexandre Belloni 986531ee1a6SVladimir Oltean return 0; 987531ee1a6SVladimir Oltean } 9885e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump); 989531ee1a6SVladimir Oltean 990f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) 9914e3b0468SAntoine Tenart { 9924e3b0468SAntoine Tenart return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, 9934e3b0468SAntoine Tenart sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; 9944e3b0468SAntoine Tenart } 995f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get); 9964e3b0468SAntoine Tenart 997f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) 9984e3b0468SAntoine Tenart { 999306fd44bSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 10004e3b0468SAntoine Tenart struct hwtstamp_config cfg; 10014e3b0468SAntoine Tenart 10024e3b0468SAntoine Tenart if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 10034e3b0468SAntoine Tenart return -EFAULT; 10044e3b0468SAntoine Tenart 10054e3b0468SAntoine Tenart /* reserved for future extensions */ 10064e3b0468SAntoine Tenart if (cfg.flags) 10074e3b0468SAntoine Tenart return -EINVAL; 10084e3b0468SAntoine Tenart 10094e3b0468SAntoine Tenart /* Tx type sanity check */ 10104e3b0468SAntoine Tenart switch (cfg.tx_type) { 10114e3b0468SAntoine Tenart case HWTSTAMP_TX_ON: 1012306fd44bSVladimir Oltean ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 10134e3b0468SAntoine Tenart break; 10144e3b0468SAntoine Tenart case HWTSTAMP_TX_ONESTEP_SYNC: 10154e3b0468SAntoine Tenart /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we 10164e3b0468SAntoine Tenart * need to update the origin time. 10174e3b0468SAntoine Tenart */ 1018306fd44bSVladimir Oltean ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; 10194e3b0468SAntoine Tenart break; 10204e3b0468SAntoine Tenart case HWTSTAMP_TX_OFF: 1021306fd44bSVladimir Oltean ocelot_port->ptp_cmd = 0; 10224e3b0468SAntoine Tenart break; 10234e3b0468SAntoine Tenart default: 10244e3b0468SAntoine Tenart return -ERANGE; 10254e3b0468SAntoine Tenart } 10264e3b0468SAntoine Tenart 10274e3b0468SAntoine Tenart mutex_lock(&ocelot->ptp_lock); 10284e3b0468SAntoine Tenart 10294e3b0468SAntoine Tenart switch (cfg.rx_filter) { 10304e3b0468SAntoine Tenart case HWTSTAMP_FILTER_NONE: 10314e3b0468SAntoine Tenart break; 10324e3b0468SAntoine Tenart case HWTSTAMP_FILTER_ALL: 10334e3b0468SAntoine Tenart case HWTSTAMP_FILTER_SOME: 10344e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 10354e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 10364e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 10374e3b0468SAntoine Tenart case HWTSTAMP_FILTER_NTP_ALL: 10384e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 10394e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 10404e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 10414e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 10424e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 10434e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 10444e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_EVENT: 10454e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_SYNC: 10464e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 10474e3b0468SAntoine Tenart cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 10484e3b0468SAntoine Tenart break; 10494e3b0468SAntoine Tenart default: 10504e3b0468SAntoine Tenart mutex_unlock(&ocelot->ptp_lock); 10514e3b0468SAntoine Tenart return -ERANGE; 10524e3b0468SAntoine Tenart } 10534e3b0468SAntoine Tenart 10544e3b0468SAntoine Tenart /* Commit back the result & save it */ 10554e3b0468SAntoine Tenart memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); 10564e3b0468SAntoine Tenart mutex_unlock(&ocelot->ptp_lock); 10574e3b0468SAntoine Tenart 10584e3b0468SAntoine Tenart return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 10594e3b0468SAntoine Tenart } 1060f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set); 10614e3b0468SAntoine Tenart 10625e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) 1063a556c76aSAlexandre Belloni { 1064a556c76aSAlexandre Belloni int i; 1065a556c76aSAlexandre Belloni 1066a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 1067a556c76aSAlexandre Belloni return; 1068a556c76aSAlexandre Belloni 1069a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 1070a556c76aSAlexandre Belloni memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, 1071a556c76aSAlexandre Belloni ETH_GSTRING_LEN); 1072a556c76aSAlexandre Belloni } 10735e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings); 1074a556c76aSAlexandre Belloni 10751e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot) 1076a556c76aSAlexandre Belloni { 1077a556c76aSAlexandre Belloni int i, j; 1078a556c76aSAlexandre Belloni 1079a556c76aSAlexandre Belloni mutex_lock(&ocelot->stats_lock); 1080a556c76aSAlexandre Belloni 1081a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_phys_ports; i++) { 1082a556c76aSAlexandre Belloni /* Configure the port to read the stats from */ 1083a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); 1084a556c76aSAlexandre Belloni 1085a556c76aSAlexandre Belloni for (j = 0; j < ocelot->num_stats; j++) { 1086a556c76aSAlexandre Belloni u32 val; 1087a556c76aSAlexandre Belloni unsigned int idx = i * ocelot->num_stats + j; 1088a556c76aSAlexandre Belloni 1089a556c76aSAlexandre Belloni val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, 1090a556c76aSAlexandre Belloni ocelot->stats_layout[j].offset); 1091a556c76aSAlexandre Belloni 1092a556c76aSAlexandre Belloni if (val < (ocelot->stats[idx] & U32_MAX)) 1093a556c76aSAlexandre Belloni ocelot->stats[idx] += (u64)1 << 32; 1094a556c76aSAlexandre Belloni 1095a556c76aSAlexandre Belloni ocelot->stats[idx] = (ocelot->stats[idx] & 1096a556c76aSAlexandre Belloni ~(u64)U32_MAX) + val; 1097a556c76aSAlexandre Belloni } 1098a556c76aSAlexandre Belloni } 1099a556c76aSAlexandre Belloni 11001e1caa97SClaudiu Manoil mutex_unlock(&ocelot->stats_lock); 11011e1caa97SClaudiu Manoil } 11021e1caa97SClaudiu Manoil 11031e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work) 11041e1caa97SClaudiu Manoil { 11051e1caa97SClaudiu Manoil struct delayed_work *del_work = to_delayed_work(work); 11061e1caa97SClaudiu Manoil struct ocelot *ocelot = container_of(del_work, struct ocelot, 11071e1caa97SClaudiu Manoil stats_work); 11081e1caa97SClaudiu Manoil 11091e1caa97SClaudiu Manoil ocelot_update_stats(ocelot); 11101e1caa97SClaudiu Manoil 1111a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1112a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 1113a556c76aSAlexandre Belloni } 1114a556c76aSAlexandre Belloni 11155e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) 1116a556c76aSAlexandre Belloni { 1117a556c76aSAlexandre Belloni int i; 1118a556c76aSAlexandre Belloni 1119a556c76aSAlexandre Belloni /* check and update now */ 11201e1caa97SClaudiu Manoil ocelot_update_stats(ocelot); 1121a556c76aSAlexandre Belloni 1122a556c76aSAlexandre Belloni /* Copy all counters */ 1123a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 1124004d44f6SVladimir Oltean *data++ = ocelot->stats[port * ocelot->num_stats + i]; 1125a556c76aSAlexandre Belloni } 11265e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats); 1127a556c76aSAlexandre Belloni 11285e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) 1129c7282d38SVladimir Oltean { 1130a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 1131a556c76aSAlexandre Belloni return -EOPNOTSUPP; 1132c7282d38SVladimir Oltean 1133a556c76aSAlexandre Belloni return ocelot->num_stats; 1134a556c76aSAlexandre Belloni } 11355e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count); 1136a556c76aSAlexandre Belloni 11375e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port, 1138c7282d38SVladimir Oltean struct ethtool_ts_info *info) 1139c7282d38SVladimir Oltean { 11404e3b0468SAntoine Tenart info->phc_index = ocelot->ptp_clock ? 11414e3b0468SAntoine Tenart ptp_clock_index(ocelot->ptp_clock) : -1; 1142d2b09a8eSYangbo Lu if (info->phc_index == -1) { 1143d2b09a8eSYangbo Lu info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 1144d2b09a8eSYangbo Lu SOF_TIMESTAMPING_RX_SOFTWARE | 1145d2b09a8eSYangbo Lu SOF_TIMESTAMPING_SOFTWARE; 1146d2b09a8eSYangbo Lu return 0; 1147d2b09a8eSYangbo Lu } 11484e3b0468SAntoine Tenart info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 11494e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_SOFTWARE | 11504e3b0468SAntoine Tenart SOF_TIMESTAMPING_SOFTWARE | 11514e3b0468SAntoine Tenart SOF_TIMESTAMPING_TX_HARDWARE | 11524e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_HARDWARE | 11534e3b0468SAntoine Tenart SOF_TIMESTAMPING_RAW_HARDWARE; 11544e3b0468SAntoine Tenart info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | 11554e3b0468SAntoine Tenart BIT(HWTSTAMP_TX_ONESTEP_SYNC); 11564e3b0468SAntoine Tenart info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); 11574e3b0468SAntoine Tenart 11584e3b0468SAntoine Tenart return 0; 11594e3b0468SAntoine Tenart } 11605e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info); 11614e3b0468SAntoine Tenart 116223ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond, 116323ca3b72SVladimir Oltean bool only_active_ports) 1164b80af659SVladimir Oltean { 1165b80af659SVladimir Oltean u32 mask = 0; 1166b80af659SVladimir Oltean int port; 1167b80af659SVladimir Oltean 1168b80af659SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1169b80af659SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1170b80af659SVladimir Oltean 1171b80af659SVladimir Oltean if (!ocelot_port) 1172b80af659SVladimir Oltean continue; 1173b80af659SVladimir Oltean 117423ca3b72SVladimir Oltean if (ocelot_port->bond == bond) { 117523ca3b72SVladimir Oltean if (only_active_ports && !ocelot_port->lag_tx_active) 117623ca3b72SVladimir Oltean continue; 117723ca3b72SVladimir Oltean 1178b80af659SVladimir Oltean mask |= BIT(port); 1179b80af659SVladimir Oltean } 118023ca3b72SVladimir Oltean } 1181b80af659SVladimir Oltean 1182b80af659SVladimir Oltean return mask; 1183b80af659SVladimir Oltean } 1184b80af659SVladimir Oltean 1185e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot) 11869b521250SVladimir Oltean { 1187e21268efSVladimir Oltean u32 mask = 0; 11889b521250SVladimir Oltean int port; 11899b521250SVladimir Oltean 1190e21268efSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1191e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1192e21268efSVladimir Oltean 1193e21268efSVladimir Oltean if (!ocelot_port) 1194e21268efSVladimir Oltean continue; 1195e21268efSVladimir Oltean 1196e21268efSVladimir Oltean if (ocelot_port->is_dsa_8021q_cpu) 1197e21268efSVladimir Oltean mask |= BIT(port); 1198e21268efSVladimir Oltean } 1199e21268efSVladimir Oltean 1200e21268efSVladimir Oltean return mask; 1201e21268efSVladimir Oltean } 1202e21268efSVladimir Oltean 1203e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot) 1204e21268efSVladimir Oltean { 1205e21268efSVladimir Oltean unsigned long cpu_fwd_mask; 1206e21268efSVladimir Oltean int port; 1207e21268efSVladimir Oltean 1208e21268efSVladimir Oltean /* If a DSA tag_8021q CPU exists, it needs to be included in the 1209e21268efSVladimir Oltean * regular forwarding path of the front ports regardless of whether 1210e21268efSVladimir Oltean * those are bridged or standalone. 1211e21268efSVladimir Oltean * If DSA tag_8021q is not used, this returns 0, which is fine because 1212e21268efSVladimir Oltean * the hardware-based CPU port module can be a destination for packets 1213e21268efSVladimir Oltean * even if it isn't part of PGID_SRC. 1214e21268efSVladimir Oltean */ 1215e21268efSVladimir Oltean cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot); 1216e21268efSVladimir Oltean 12179b521250SVladimir Oltean /* Apply FWD mask. The loop is needed to add/remove the current port as 12189b521250SVladimir Oltean * a source for the other ports. 12199b521250SVladimir Oltean */ 12209b521250SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1221e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1222e21268efSVladimir Oltean unsigned long mask; 1223e21268efSVladimir Oltean 1224e21268efSVladimir Oltean if (!ocelot_port) { 1225e21268efSVladimir Oltean /* Unused ports can't send anywhere */ 1226e21268efSVladimir Oltean mask = 0; 1227e21268efSVladimir Oltean } else if (ocelot_port->is_dsa_8021q_cpu) { 1228e21268efSVladimir Oltean /* The DSA tag_8021q CPU ports need to be able to 1229e21268efSVladimir Oltean * forward packets to all other ports except for 1230e21268efSVladimir Oltean * themselves 1231e21268efSVladimir Oltean */ 1232e21268efSVladimir Oltean mask = GENMASK(ocelot->num_phys_ports - 1, 0); 1233e21268efSVladimir Oltean mask &= ~cpu_fwd_mask; 1234e21268efSVladimir Oltean } else if (ocelot->bridge_fwd_mask & BIT(port)) { 1235528d3f19SVladimir Oltean struct net_device *bond = ocelot_port->bond; 12369b521250SVladimir Oltean 1237e21268efSVladimir Oltean mask = ocelot->bridge_fwd_mask & ~BIT(port); 123823ca3b72SVladimir Oltean if (bond) { 123923ca3b72SVladimir Oltean mask &= ~ocelot_get_bond_mask(ocelot, bond, 124023ca3b72SVladimir Oltean false); 124123ca3b72SVladimir Oltean } 12429b521250SVladimir Oltean } else { 1243e21268efSVladimir Oltean /* Standalone ports forward only to DSA tag_8021q CPU 1244e21268efSVladimir Oltean * ports (if those exist), or to the hardware CPU port 1245e21268efSVladimir Oltean * module otherwise. 1246e21268efSVladimir Oltean */ 1247e21268efSVladimir Oltean mask = cpu_fwd_mask; 1248e21268efSVladimir Oltean } 1249e21268efSVladimir Oltean 1250e21268efSVladimir Oltean ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); 12519b521250SVladimir Oltean } 12529b521250SVladimir Oltean } 1253e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask); 12549b521250SVladimir Oltean 12555e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) 1256a556c76aSAlexandre Belloni { 1257421741eaSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1258a556c76aSAlexandre Belloni u32 port_cfg; 1259a556c76aSAlexandre Belloni 12604bda1415SVladimir Oltean if (!(BIT(port) & ocelot->bridge_mask)) 12614bda1415SVladimir Oltean return; 1262a556c76aSAlexandre Belloni 12634bda1415SVladimir Oltean port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); 1264a556c76aSAlexandre Belloni 1265a556c76aSAlexandre Belloni switch (state) { 1266a556c76aSAlexandre Belloni case BR_STATE_FORWARDING: 12674bda1415SVladimir Oltean ocelot->bridge_fwd_mask |= BIT(port); 1268df561f66SGustavo A. R. Silva fallthrough; 1269a556c76aSAlexandre Belloni case BR_STATE_LEARNING: 1270421741eaSVladimir Oltean if (ocelot_port->learn_ena) 1271a556c76aSAlexandre Belloni port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA; 1272a556c76aSAlexandre Belloni break; 1273a556c76aSAlexandre Belloni 1274a556c76aSAlexandre Belloni default: 1275a556c76aSAlexandre Belloni port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA; 12764bda1415SVladimir Oltean ocelot->bridge_fwd_mask &= ~BIT(port); 1277a556c76aSAlexandre Belloni break; 1278a556c76aSAlexandre Belloni } 1279a556c76aSAlexandre Belloni 12804bda1415SVladimir Oltean ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port); 1281a556c76aSAlexandre Belloni 12829b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 1283a556c76aSAlexandre Belloni } 12845e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set); 1285a556c76aSAlexandre Belloni 12865e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) 12874bda1415SVladimir Oltean { 1288c0d7eccbSVladimir Oltean unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); 1289c0d7eccbSVladimir Oltean 1290c0d7eccbSVladimir Oltean /* Setting AGE_PERIOD to zero effectively disables automatic aging, 1291c0d7eccbSVladimir Oltean * which is clearly not what our intention is. So avoid that. 1292c0d7eccbSVladimir Oltean */ 1293c0d7eccbSVladimir Oltean if (!age_period) 1294c0d7eccbSVladimir Oltean age_period = 1; 1295c0d7eccbSVladimir Oltean 1296c0d7eccbSVladimir Oltean ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); 1297a556c76aSAlexandre Belloni } 12985e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time); 1299a556c76aSAlexandre Belloni 1300a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 1301a556c76aSAlexandre Belloni const unsigned char *addr, 1302a556c76aSAlexandre Belloni u16 vid) 1303a556c76aSAlexandre Belloni { 1304a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 1305a556c76aSAlexandre Belloni 1306a556c76aSAlexandre Belloni list_for_each_entry(mc, &ocelot->multicast, list) { 1307a556c76aSAlexandre Belloni if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 1308a556c76aSAlexandre Belloni return mc; 1309a556c76aSAlexandre Belloni } 1310a556c76aSAlexandre Belloni 1311a556c76aSAlexandre Belloni return NULL; 1312a556c76aSAlexandre Belloni } 1313a556c76aSAlexandre Belloni 13149403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) 13159403c158SVladimir Oltean { 13169403c158SVladimir Oltean if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) 13179403c158SVladimir Oltean return ENTRYTYPE_MACv4; 13189403c158SVladimir Oltean if (addr[0] == 0x33 && addr[1] == 0x33) 13199403c158SVladimir Oltean return ENTRYTYPE_MACv6; 13207c313143SVladimir Oltean return ENTRYTYPE_LOCKED; 13219403c158SVladimir Oltean } 13229403c158SVladimir Oltean 1323e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, 1324e5d1f896SVladimir Oltean unsigned long ports) 1325e5d1f896SVladimir Oltean { 1326e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1327e5d1f896SVladimir Oltean 1328e5d1f896SVladimir Oltean pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); 1329e5d1f896SVladimir Oltean if (!pgid) 1330e5d1f896SVladimir Oltean return ERR_PTR(-ENOMEM); 1331e5d1f896SVladimir Oltean 1332e5d1f896SVladimir Oltean pgid->ports = ports; 1333e5d1f896SVladimir Oltean pgid->index = index; 1334e5d1f896SVladimir Oltean refcount_set(&pgid->refcount, 1); 1335e5d1f896SVladimir Oltean list_add_tail(&pgid->list, &ocelot->pgids); 1336e5d1f896SVladimir Oltean 1337e5d1f896SVladimir Oltean return pgid; 1338e5d1f896SVladimir Oltean } 1339e5d1f896SVladimir Oltean 1340e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) 1341e5d1f896SVladimir Oltean { 1342e5d1f896SVladimir Oltean if (!refcount_dec_and_test(&pgid->refcount)) 1343e5d1f896SVladimir Oltean return; 1344e5d1f896SVladimir Oltean 1345e5d1f896SVladimir Oltean list_del(&pgid->list); 1346e5d1f896SVladimir Oltean kfree(pgid); 1347e5d1f896SVladimir Oltean } 1348e5d1f896SVladimir Oltean 1349e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, 1350bb8d53fdSVladimir Oltean const struct ocelot_multicast *mc) 13519403c158SVladimir Oltean { 1352e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1353e5d1f896SVladimir Oltean int index; 13549403c158SVladimir Oltean 13559403c158SVladimir Oltean /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and 13569403c158SVladimir Oltean * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the 13579403c158SVladimir Oltean * destination mask table (PGID), the destination set is programmed as 13589403c158SVladimir Oltean * part of the entry MAC address.", and the DEST_IDX is set to 0. 13599403c158SVladimir Oltean */ 1360bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4 || 1361bb8d53fdSVladimir Oltean mc->entry_type == ENTRYTYPE_MACv6) 1362e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, 0, mc->ports); 13639403c158SVladimir Oltean 1364e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 1365e5d1f896SVladimir Oltean /* When searching for a nonreserved multicast PGID, ignore the 1366e5d1f896SVladimir Oltean * dummy PGID of zero that we have for MACv4/MACv6 entries 1367e5d1f896SVladimir Oltean */ 1368e5d1f896SVladimir Oltean if (pgid->index && pgid->ports == mc->ports) { 1369e5d1f896SVladimir Oltean refcount_inc(&pgid->refcount); 1370e5d1f896SVladimir Oltean return pgid; 1371e5d1f896SVladimir Oltean } 1372e5d1f896SVladimir Oltean } 1373e5d1f896SVladimir Oltean 1374e5d1f896SVladimir Oltean /* Search for a free index in the nonreserved multicast PGID area */ 1375e5d1f896SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, index) { 13769403c158SVladimir Oltean bool used = false; 13779403c158SVladimir Oltean 1378e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 1379e5d1f896SVladimir Oltean if (pgid->index == index) { 13809403c158SVladimir Oltean used = true; 13819403c158SVladimir Oltean break; 13829403c158SVladimir Oltean } 13839403c158SVladimir Oltean } 13849403c158SVladimir Oltean 13859403c158SVladimir Oltean if (!used) 1386e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, index, mc->ports); 13879403c158SVladimir Oltean } 13889403c158SVladimir Oltean 1389e5d1f896SVladimir Oltean return ERR_PTR(-ENOSPC); 13909403c158SVladimir Oltean } 13919403c158SVladimir Oltean 13929403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr, 1393bb8d53fdSVladimir Oltean struct ocelot_multicast *mc) 13949403c158SVladimir Oltean { 1395ebbd860eSVladimir Oltean ether_addr_copy(addr, mc->addr); 13969403c158SVladimir Oltean 1397bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4) { 13989403c158SVladimir Oltean addr[0] = 0; 13999403c158SVladimir Oltean addr[1] = mc->ports >> 8; 14009403c158SVladimir Oltean addr[2] = mc->ports & 0xff; 1401bb8d53fdSVladimir Oltean } else if (mc->entry_type == ENTRYTYPE_MACv6) { 14029403c158SVladimir Oltean addr[0] = mc->ports >> 8; 14039403c158SVladimir Oltean addr[1] = mc->ports & 0xff; 14049403c158SVladimir Oltean } 14059403c158SVladimir Oltean } 14069403c158SVladimir Oltean 1407209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port, 1408209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1409a556c76aSAlexandre Belloni { 1410a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 1411004d44f6SVladimir Oltean struct ocelot_multicast *mc; 1412e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1413a556c76aSAlexandre Belloni u16 vid = mdb->vid; 1414a556c76aSAlexandre Belloni 1415471beb11SVladimir Oltean if (port == ocelot->npi) 1416471beb11SVladimir Oltean port = ocelot->num_phys_ports; 1417471beb11SVladimir Oltean 1418a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1419a556c76aSAlexandre Belloni if (!mc) { 1420728e69aeSVladimir Oltean /* New entry */ 1421bb8d53fdSVladimir Oltean mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 1422bb8d53fdSVladimir Oltean if (!mc) 1423bb8d53fdSVladimir Oltean return -ENOMEM; 1424bb8d53fdSVladimir Oltean 1425bb8d53fdSVladimir Oltean mc->entry_type = ocelot_classify_mdb(mdb->addr); 1426bb8d53fdSVladimir Oltean ether_addr_copy(mc->addr, mdb->addr); 1427bb8d53fdSVladimir Oltean mc->vid = vid; 1428bb8d53fdSVladimir Oltean 1429a556c76aSAlexandre Belloni list_add_tail(&mc->list, &ocelot->multicast); 1430728e69aeSVladimir Oltean } else { 1431e5d1f896SVladimir Oltean /* Existing entry. Clean up the current port mask from 1432e5d1f896SVladimir Oltean * hardware now, because we'll be modifying it. 1433e5d1f896SVladimir Oltean */ 1434e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 1435bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1436a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 1437a556c76aSAlexandre Belloni } 1438a556c76aSAlexandre Belloni 1439004d44f6SVladimir Oltean mc->ports |= BIT(port); 1440e5d1f896SVladimir Oltean 1441e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 1442e5d1f896SVladimir Oltean if (IS_ERR(pgid)) { 1443e5d1f896SVladimir Oltean dev_err(ocelot->dev, 1444e5d1f896SVladimir Oltean "Cannot allocate PGID for mdb %pM vid %d\n", 1445e5d1f896SVladimir Oltean mc->addr, mc->vid); 1446e5d1f896SVladimir Oltean devm_kfree(ocelot->dev, mc); 1447e5d1f896SVladimir Oltean return PTR_ERR(pgid); 1448e5d1f896SVladimir Oltean } 1449e5d1f896SVladimir Oltean mc->pgid = pgid; 1450e5d1f896SVladimir Oltean 1451bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1452a556c76aSAlexandre Belloni 1453e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 1454e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 1455e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1456e5d1f896SVladimir Oltean pgid->index); 1457e5d1f896SVladimir Oltean 1458e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1459bb8d53fdSVladimir Oltean mc->entry_type); 1460a556c76aSAlexandre Belloni } 1461209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add); 1462a556c76aSAlexandre Belloni 1463209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port, 1464a556c76aSAlexandre Belloni const struct switchdev_obj_port_mdb *mdb) 1465a556c76aSAlexandre Belloni { 1466a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 1467004d44f6SVladimir Oltean struct ocelot_multicast *mc; 1468e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1469a556c76aSAlexandre Belloni u16 vid = mdb->vid; 1470a556c76aSAlexandre Belloni 1471471beb11SVladimir Oltean if (port == ocelot->npi) 1472471beb11SVladimir Oltean port = ocelot->num_phys_ports; 1473471beb11SVladimir Oltean 1474a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1475a556c76aSAlexandre Belloni if (!mc) 1476a556c76aSAlexandre Belloni return -ENOENT; 1477a556c76aSAlexandre Belloni 1478bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1479a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 1480a556c76aSAlexandre Belloni 1481e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 1482004d44f6SVladimir Oltean mc->ports &= ~BIT(port); 1483a556c76aSAlexandre Belloni if (!mc->ports) { 1484a556c76aSAlexandre Belloni list_del(&mc->list); 1485a556c76aSAlexandre Belloni devm_kfree(ocelot->dev, mc); 1486a556c76aSAlexandre Belloni return 0; 1487a556c76aSAlexandre Belloni } 1488a556c76aSAlexandre Belloni 1489e5d1f896SVladimir Oltean /* We have a PGID with fewer ports now */ 1490e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 1491e5d1f896SVladimir Oltean if (IS_ERR(pgid)) 1492e5d1f896SVladimir Oltean return PTR_ERR(pgid); 1493e5d1f896SVladimir Oltean mc->pgid = pgid; 1494e5d1f896SVladimir Oltean 1495bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 1496a556c76aSAlexandre Belloni 1497e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 1498e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 1499e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1500e5d1f896SVladimir Oltean pgid->index); 1501e5d1f896SVladimir Oltean 1502e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1503bb8d53fdSVladimir Oltean mc->entry_type); 1504a556c76aSAlexandre Belloni } 1505209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del); 1506a556c76aSAlexandre Belloni 15075e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port, 1508a556c76aSAlexandre Belloni struct net_device *bridge) 1509a556c76aSAlexandre Belloni { 1510a556c76aSAlexandre Belloni if (!ocelot->bridge_mask) { 1511a556c76aSAlexandre Belloni ocelot->hw_bridge_dev = bridge; 1512a556c76aSAlexandre Belloni } else { 1513a556c76aSAlexandre Belloni if (ocelot->hw_bridge_dev != bridge) 1514a556c76aSAlexandre Belloni /* This is adding the port to a second bridge, this is 1515a556c76aSAlexandre Belloni * unsupported */ 1516a556c76aSAlexandre Belloni return -ENODEV; 1517a556c76aSAlexandre Belloni } 1518a556c76aSAlexandre Belloni 1519f270dbfaSVladimir Oltean ocelot->bridge_mask |= BIT(port); 1520a556c76aSAlexandre Belloni 1521a556c76aSAlexandre Belloni return 0; 1522a556c76aSAlexandre Belloni } 15235e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join); 1524a556c76aSAlexandre Belloni 15255e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port, 1526a556c76aSAlexandre Belloni struct net_device *bridge) 1527a556c76aSAlexandre Belloni { 1528c3e58a75SVladimir Oltean struct ocelot_vlan pvid = {0}, native_vlan = {0}; 15292e554a7aSVladimir Oltean int ret; 15302e554a7aSVladimir Oltean 153197bb69e1SVladimir Oltean ocelot->bridge_mask &= ~BIT(port); 1532a556c76aSAlexandre Belloni 1533a556c76aSAlexandre Belloni if (!ocelot->bridge_mask) 1534a556c76aSAlexandre Belloni ocelot->hw_bridge_dev = NULL; 15357142529fSAntoine Tenart 1536bae33f2bSVladimir Oltean ret = ocelot_port_vlan_filtering(ocelot, port, false); 15372e554a7aSVladimir Oltean if (ret) 15382e554a7aSVladimir Oltean return ret; 15392e554a7aSVladimir Oltean 1540c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, pvid); 15412f0402feSVladimir Oltean ocelot_port_set_native_vlan(ocelot, port, native_vlan); 15422f0402feSVladimir Oltean 15432f0402feSVladimir Oltean return 0; 1544a556c76aSAlexandre Belloni } 15455e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave); 1546a556c76aSAlexandre Belloni 1547dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 1548dc96ee37SAlexandre Belloni { 1549528d3f19SVladimir Oltean unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0); 1550dc96ee37SAlexandre Belloni int i, port, lag; 1551dc96ee37SAlexandre Belloni 1552dc96ee37SAlexandre Belloni /* Reset destination and aggregation PGIDS */ 155396b029b0SVladimir Oltean for_each_unicast_dest_pgid(ocelot, port) 1554dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1555dc96ee37SAlexandre Belloni 155696b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) 1557dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 1558dc96ee37SAlexandre Belloni ANA_PGID_PGID, i); 1559dc96ee37SAlexandre Belloni 1560528d3f19SVladimir Oltean /* The visited ports bitmask holds the list of ports offloading any 1561528d3f19SVladimir Oltean * bonding interface. Initially we mark all these ports as unvisited, 1562528d3f19SVladimir Oltean * then every time we visit a port in this bitmask, we know that it is 1563528d3f19SVladimir Oltean * the lowest numbered port, i.e. the one whose logical ID == physical 1564528d3f19SVladimir Oltean * port ID == LAG ID. So we mark as visited all further ports in the 1565528d3f19SVladimir Oltean * bitmask that are offloading the same bonding interface. This way, 1566528d3f19SVladimir Oltean * we set up the aggregation PGIDs only once per bonding interface. 1567528d3f19SVladimir Oltean */ 1568528d3f19SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1569528d3f19SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1570528d3f19SVladimir Oltean 1571528d3f19SVladimir Oltean if (!ocelot_port || !ocelot_port->bond) 1572528d3f19SVladimir Oltean continue; 1573528d3f19SVladimir Oltean 1574528d3f19SVladimir Oltean visited &= ~BIT(port); 1575528d3f19SVladimir Oltean } 1576528d3f19SVladimir Oltean 1577528d3f19SVladimir Oltean /* Now, set PGIDs for each active LAG */ 1578dc96ee37SAlexandre Belloni for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 1579528d3f19SVladimir Oltean struct net_device *bond = ocelot->ports[lag]->bond; 158023ca3b72SVladimir Oltean int num_active_ports = 0; 1581dc96ee37SAlexandre Belloni unsigned long bond_mask; 1582dc96ee37SAlexandre Belloni u8 aggr_idx[16]; 1583dc96ee37SAlexandre Belloni 1584528d3f19SVladimir Oltean if (!bond || (visited & BIT(lag))) 1585dc96ee37SAlexandre Belloni continue; 1586dc96ee37SAlexandre Belloni 158723ca3b72SVladimir Oltean bond_mask = ocelot_get_bond_mask(ocelot, bond, true); 1588528d3f19SVladimir Oltean 1589dc96ee37SAlexandre Belloni for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 1590dc96ee37SAlexandre Belloni // Destination mask 1591dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, bond_mask, 1592dc96ee37SAlexandre Belloni ANA_PGID_PGID, port); 159323ca3b72SVladimir Oltean aggr_idx[num_active_ports++] = port; 1594dc96ee37SAlexandre Belloni } 1595dc96ee37SAlexandre Belloni 159696b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) { 1597dc96ee37SAlexandre Belloni u32 ac; 1598dc96ee37SAlexandre Belloni 1599dc96ee37SAlexandre Belloni ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 1600dc96ee37SAlexandre Belloni ac &= ~bond_mask; 160123ca3b72SVladimir Oltean /* Don't do division by zero if there was no active 160223ca3b72SVladimir Oltean * port. Just make all aggregation codes zero. 160323ca3b72SVladimir Oltean */ 160423ca3b72SVladimir Oltean if (num_active_ports) 160523ca3b72SVladimir Oltean ac |= BIT(aggr_idx[i % num_active_ports]); 1606dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 1607dc96ee37SAlexandre Belloni } 1608528d3f19SVladimir Oltean 1609528d3f19SVladimir Oltean /* Mark all ports in the same LAG as visited to avoid applying 1610528d3f19SVladimir Oltean * the same config again. 1611528d3f19SVladimir Oltean */ 1612528d3f19SVladimir Oltean for (port = lag; port < ocelot->num_phys_ports; port++) { 1613528d3f19SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1614528d3f19SVladimir Oltean 1615528d3f19SVladimir Oltean if (!ocelot_port) 1616528d3f19SVladimir Oltean continue; 1617528d3f19SVladimir Oltean 1618528d3f19SVladimir Oltean if (ocelot_port->bond == bond) 1619528d3f19SVladimir Oltean visited |= BIT(port); 1620528d3f19SVladimir Oltean } 1621dc96ee37SAlexandre Belloni } 1622dc96ee37SAlexandre Belloni } 1623dc96ee37SAlexandre Belloni 16242527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the 16252527f2e8SVladimir Oltean * same bond must have the same logical port ID, equal to the physical port ID 16262527f2e8SVladimir Oltean * of the lowest numbered physical port in that bond. Otherwise, in standalone/ 16272527f2e8SVladimir Oltean * bridged mode, each port has a logical port ID equal to its physical port ID. 16282527f2e8SVladimir Oltean */ 16292527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) 1630dc96ee37SAlexandre Belloni { 16312527f2e8SVladimir Oltean int port; 1632dc96ee37SAlexandre Belloni 16332527f2e8SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 16342527f2e8SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 16352527f2e8SVladimir Oltean struct net_device *bond; 1636dc96ee37SAlexandre Belloni 16372527f2e8SVladimir Oltean if (!ocelot_port) 16382527f2e8SVladimir Oltean continue; 1639dc96ee37SAlexandre Belloni 16402527f2e8SVladimir Oltean bond = ocelot_port->bond; 16412527f2e8SVladimir Oltean if (bond) { 164223ca3b72SVladimir Oltean int lag = __ffs(ocelot_get_bond_mask(ocelot, bond, 164323ca3b72SVladimir Oltean false)); 16442527f2e8SVladimir Oltean 16452527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 1646dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(lag), 16472527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 16482527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 16492527f2e8SVladimir Oltean } else { 16502527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 16512527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 16522527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 16532527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 16542527f2e8SVladimir Oltean } 1655dc96ee37SAlexandre Belloni } 1656dc96ee37SAlexandre Belloni } 1657dc96ee37SAlexandre Belloni 16589c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port, 1659583cbbe3SVladimir Oltean struct net_device *bond, 1660583cbbe3SVladimir Oltean struct netdev_lag_upper_info *info) 1661dc96ee37SAlexandre Belloni { 1662583cbbe3SVladimir Oltean if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) 1663583cbbe3SVladimir Oltean return -EOPNOTSUPP; 1664583cbbe3SVladimir Oltean 1665b80af659SVladimir Oltean ocelot->ports[port]->bond = bond; 1666dc96ee37SAlexandre Belloni 16672527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 16689b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 1669dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 1670dc96ee37SAlexandre Belloni 1671dc96ee37SAlexandre Belloni return 0; 1672dc96ee37SAlexandre Belloni } 16739c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join); 1674dc96ee37SAlexandre Belloni 16759c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port, 1676dc96ee37SAlexandre Belloni struct net_device *bond) 1677dc96ee37SAlexandre Belloni { 1678b80af659SVladimir Oltean ocelot->ports[port]->bond = NULL; 1679b80af659SVladimir Oltean 16802527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 16819b521250SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot); 1682dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 1683dc96ee37SAlexandre Belloni } 16849c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave); 16850e332c85SPetr Machata 168623ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active) 168723ca3b72SVladimir Oltean { 168823ca3b72SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 168923ca3b72SVladimir Oltean 169023ca3b72SVladimir Oltean ocelot_port->lag_tx_active = lag_tx_active; 169123ca3b72SVladimir Oltean 169223ca3b72SVladimir Oltean /* Rebalance the LAGs */ 169323ca3b72SVladimir Oltean ocelot_set_aggr_pgids(ocelot); 169423ca3b72SVladimir Oltean } 169523ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change); 169623ca3b72SVladimir Oltean 1697a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. 1698a8015dedSVladimir Oltean * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. 16990b912fc9SVladimir Oltean * In the special case that it's the NPI port that we're configuring, the 17000b912fc9SVladimir Oltean * length of the tag and optional prefix needs to be accounted for privately, 17010b912fc9SVladimir Oltean * in order to be able to sustain communication at the requested @sdu. 1702a8015dedSVladimir Oltean */ 17030b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) 170431350d7fSVladimir Oltean { 170531350d7fSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1706a8015dedSVladimir Oltean int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; 1707e8e6e73dSVladimir Oltean int pause_start, pause_stop; 1708601e984fSVladimir Oltean int atop, atop_tot; 170931350d7fSVladimir Oltean 17100b912fc9SVladimir Oltean if (port == ocelot->npi) { 17110b912fc9SVladimir Oltean maxlen += OCELOT_TAG_LEN; 17120b912fc9SVladimir Oltean 1713cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 17140b912fc9SVladimir Oltean maxlen += OCELOT_SHORT_PREFIX_LEN; 1715cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 17160b912fc9SVladimir Oltean maxlen += OCELOT_LONG_PREFIX_LEN; 17170b912fc9SVladimir Oltean } 17180b912fc9SVladimir Oltean 1719a8015dedSVladimir Oltean ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); 1720fa914e9cSVladimir Oltean 1721e8e6e73dSVladimir Oltean /* Set Pause watermark hysteresis */ 1722e8e6e73dSVladimir Oltean pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; 1723e8e6e73dSVladimir Oltean pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; 1724541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, 1725541132f0SMaxim Kochetkov pause_start); 1726541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, 1727541132f0SMaxim Kochetkov pause_stop); 1728fa914e9cSVladimir Oltean 1729601e984fSVladimir Oltean /* Tail dropping watermarks */ 1730f6fe01d6SVladimir Oltean atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / 1731a8015dedSVladimir Oltean OCELOT_BUFFER_CELL_SZ; 1732601e984fSVladimir Oltean atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; 1733601e984fSVladimir Oltean ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); 1734601e984fSVladimir Oltean ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); 1735fa914e9cSVladimir Oltean } 17360b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen); 17370b912fc9SVladimir Oltean 17380b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port) 17390b912fc9SVladimir Oltean { 17400b912fc9SVladimir Oltean int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; 17410b912fc9SVladimir Oltean 17420b912fc9SVladimir Oltean if (port == ocelot->npi) { 17430b912fc9SVladimir Oltean max_mtu -= OCELOT_TAG_LEN; 17440b912fc9SVladimir Oltean 1745cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 17460b912fc9SVladimir Oltean max_mtu -= OCELOT_SHORT_PREFIX_LEN; 1747cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 17480b912fc9SVladimir Oltean max_mtu -= OCELOT_LONG_PREFIX_LEN; 17490b912fc9SVladimir Oltean } 17500b912fc9SVladimir Oltean 17510b912fc9SVladimir Oltean return max_mtu; 17520b912fc9SVladimir Oltean } 17530b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu); 1754fa914e9cSVladimir Oltean 1755421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port, 1756421741eaSVladimir Oltean bool enabled) 1757421741eaSVladimir Oltean { 1758421741eaSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1759421741eaSVladimir Oltean u32 val = 0; 1760421741eaSVladimir Oltean 1761421741eaSVladimir Oltean if (enabled) 1762421741eaSVladimir Oltean val = ANA_PORT_PORT_CFG_LEARN_ENA; 1763421741eaSVladimir Oltean 1764421741eaSVladimir Oltean ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA, 1765421741eaSVladimir Oltean ANA_PORT_PORT_CFG, port); 1766421741eaSVladimir Oltean 1767421741eaSVladimir Oltean ocelot_port->learn_ena = enabled; 1768421741eaSVladimir Oltean } 1769421741eaSVladimir Oltean 1770421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port, 1771421741eaSVladimir Oltean bool enabled) 1772421741eaSVladimir Oltean { 1773421741eaSVladimir Oltean u32 val = 0; 1774421741eaSVladimir Oltean 1775421741eaSVladimir Oltean if (enabled) 1776421741eaSVladimir Oltean val = BIT(port); 1777421741eaSVladimir Oltean 1778421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC); 1779421741eaSVladimir Oltean } 1780421741eaSVladimir Oltean 1781421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port, 1782421741eaSVladimir Oltean bool enabled) 1783421741eaSVladimir Oltean { 1784421741eaSVladimir Oltean u32 val = 0; 1785421741eaSVladimir Oltean 1786421741eaSVladimir Oltean if (enabled) 1787421741eaSVladimir Oltean val = BIT(port); 1788421741eaSVladimir Oltean 1789421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC); 1790421741eaSVladimir Oltean } 1791421741eaSVladimir Oltean 1792421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port, 1793421741eaSVladimir Oltean bool enabled) 1794421741eaSVladimir Oltean { 1795421741eaSVladimir Oltean u32 val = 0; 1796421741eaSVladimir Oltean 1797421741eaSVladimir Oltean if (enabled) 1798421741eaSVladimir Oltean val = BIT(port); 1799421741eaSVladimir Oltean 1800421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC); 1801421741eaSVladimir Oltean } 1802421741eaSVladimir Oltean 1803421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port, 1804421741eaSVladimir Oltean struct switchdev_brport_flags flags) 1805421741eaSVladimir Oltean { 1806421741eaSVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 1807421741eaSVladimir Oltean BR_BCAST_FLOOD)) 1808421741eaSVladimir Oltean return -EINVAL; 1809421741eaSVladimir Oltean 1810421741eaSVladimir Oltean return 0; 1811421741eaSVladimir Oltean } 1812421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags); 1813421741eaSVladimir Oltean 1814421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port, 1815421741eaSVladimir Oltean struct switchdev_brport_flags flags) 1816421741eaSVladimir Oltean { 1817421741eaSVladimir Oltean if (flags.mask & BR_LEARNING) 1818421741eaSVladimir Oltean ocelot_port_set_learning(ocelot, port, 1819421741eaSVladimir Oltean !!(flags.val & BR_LEARNING)); 1820421741eaSVladimir Oltean 1821421741eaSVladimir Oltean if (flags.mask & BR_FLOOD) 1822421741eaSVladimir Oltean ocelot_port_set_ucast_flood(ocelot, port, 1823421741eaSVladimir Oltean !!(flags.val & BR_FLOOD)); 1824421741eaSVladimir Oltean 1825421741eaSVladimir Oltean if (flags.mask & BR_MCAST_FLOOD) 1826421741eaSVladimir Oltean ocelot_port_set_mcast_flood(ocelot, port, 1827421741eaSVladimir Oltean !!(flags.val & BR_MCAST_FLOOD)); 1828421741eaSVladimir Oltean 1829421741eaSVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) 1830421741eaSVladimir Oltean ocelot_port_set_bcast_flood(ocelot, port, 1831421741eaSVladimir Oltean !!(flags.val & BR_BCAST_FLOOD)); 1832421741eaSVladimir Oltean } 1833421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags); 1834421741eaSVladimir Oltean 18355e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port) 1836fa914e9cSVladimir Oltean { 1837fa914e9cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1838fa914e9cSVladimir Oltean 1839b049da13SYangbo Lu skb_queue_head_init(&ocelot_port->tx_skbs); 18406565243cSVladimir Oltean spin_lock_init(&ocelot_port->ts_id_lock); 184131350d7fSVladimir Oltean 184231350d7fSVladimir Oltean /* Basic L2 initialization */ 184331350d7fSVladimir Oltean 18445bc9d2e6SVladimir Oltean /* Set MAC IFG Gaps 18455bc9d2e6SVladimir Oltean * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 18465bc9d2e6SVladimir Oltean * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 18475bc9d2e6SVladimir Oltean */ 18485bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), 18495bc9d2e6SVladimir Oltean DEV_MAC_IFG_CFG); 18505bc9d2e6SVladimir Oltean 18515bc9d2e6SVladimir Oltean /* Load seed (0) and set MAC HDX late collision */ 18525bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 18535bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG_SEED_LOAD, 18545bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 18555bc9d2e6SVladimir Oltean mdelay(1); 18565bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 18575bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 18585bc9d2e6SVladimir Oltean 18595bc9d2e6SVladimir Oltean /* Set Max Length and maximum tags allowed */ 1860a8015dedSVladimir Oltean ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); 18615bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 18625bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 1863a8015dedSVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | 18645bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 18655bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG); 18665bc9d2e6SVladimir Oltean 18675bc9d2e6SVladimir Oltean /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 18685bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 18695bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); 18705bc9d2e6SVladimir Oltean 1871e8e6e73dSVladimir Oltean /* Enable transmission of pause frames */ 1872541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 1873e8e6e73dSVladimir Oltean 187431350d7fSVladimir Oltean /* Drop frames with multicast source address */ 187531350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 187631350d7fSVladimir Oltean ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 187731350d7fSVladimir Oltean ANA_PORT_DROP_CFG, port); 187831350d7fSVladimir Oltean 187931350d7fSVladimir Oltean /* Set default VLAN and tag type to 8021Q. */ 188031350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), 188131350d7fSVladimir Oltean REW_PORT_VLAN_CFG_PORT_TPID_M, 188231350d7fSVladimir Oltean REW_PORT_VLAN_CFG, port); 188331350d7fSVladimir Oltean 1884421741eaSVladimir Oltean /* Disable source address learning for standalone mode */ 1885421741eaSVladimir Oltean ocelot_port_set_learning(ocelot, port, false); 1886421741eaSVladimir Oltean 188731350d7fSVladimir Oltean /* Enable vcap lookups */ 188831350d7fSVladimir Oltean ocelot_vcap_enable(ocelot, port); 188931350d7fSVladimir Oltean } 18905e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port); 189131350d7fSVladimir Oltean 18922d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues 18932d44b097SVladimir Oltean * accessible through register MMIO, frame DMA or Ethernet (in case 18942d44b097SVladimir Oltean * NPI mode is used). 189569df578cSVladimir Oltean */ 18962d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot) 189721468199SVladimir Oltean { 189869df578cSVladimir Oltean int cpu = ocelot->num_phys_ports; 189969df578cSVladimir Oltean 190069df578cSVladimir Oltean /* The unicast destination PGID for the CPU port module is unused */ 190121468199SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 190269df578cSVladimir Oltean /* Instead set up a multicast destination PGID for traffic copied to 190369df578cSVladimir Oltean * the CPU. Whitelisted MAC addresses like the port netdevice MAC 190469df578cSVladimir Oltean * addresses will be copied to the CPU via this PGID. 190569df578cSVladimir Oltean */ 190621468199SVladimir Oltean ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 190721468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 190821468199SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 190921468199SVladimir Oltean ANA_PORT_PORT_CFG, cpu); 191021468199SVladimir Oltean 191169df578cSVladimir Oltean /* Enable CPU port module */ 1912886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 191369df578cSVladimir Oltean /* CPU port Injection/Extraction configuration */ 1914886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, 1915cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 1916886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, 1917cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 191821468199SVladimir Oltean 191921468199SVladimir Oltean /* Configure the CPU port to be VLAN aware */ 192021468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) | 192121468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 192221468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 192321468199SVladimir Oltean ANA_PORT_VLAN_CFG, cpu); 192421468199SVladimir Oltean } 192521468199SVladimir Oltean 1926f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot) 1927f6fe01d6SVladimir Oltean { 1928f6fe01d6SVladimir Oltean int mmgt, eq_ctrl; 1929f6fe01d6SVladimir Oltean 1930f6fe01d6SVladimir Oltean /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds 1931f6fe01d6SVladimir Oltean * the number of 240-byte free memory words (aka 4-cell chunks) and not 1932f6fe01d6SVladimir Oltean * 192 bytes as the documentation incorrectly says. 1933f6fe01d6SVladimir Oltean */ 1934f6fe01d6SVladimir Oltean mmgt = ocelot_read(ocelot, SYS_MMGT); 1935f6fe01d6SVladimir Oltean ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); 1936f6fe01d6SVladimir Oltean 1937f6fe01d6SVladimir Oltean eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); 1938f6fe01d6SVladimir Oltean ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); 1939f6fe01d6SVladimir Oltean } 1940f6fe01d6SVladimir Oltean 1941a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot) 1942a556c76aSAlexandre Belloni { 1943a556c76aSAlexandre Belloni char queue_name[32]; 194421468199SVladimir Oltean int i, ret; 194521468199SVladimir Oltean u32 port; 1946a556c76aSAlexandre Belloni 19473a77b593SVladimir Oltean if (ocelot->ops->reset) { 19483a77b593SVladimir Oltean ret = ocelot->ops->reset(ocelot); 19493a77b593SVladimir Oltean if (ret) { 19503a77b593SVladimir Oltean dev_err(ocelot->dev, "Switch reset failed\n"); 19513a77b593SVladimir Oltean return ret; 19523a77b593SVladimir Oltean } 19533a77b593SVladimir Oltean } 19543a77b593SVladimir Oltean 1955a556c76aSAlexandre Belloni ocelot->stats = devm_kcalloc(ocelot->dev, 1956a556c76aSAlexandre Belloni ocelot->num_phys_ports * ocelot->num_stats, 1957a556c76aSAlexandre Belloni sizeof(u64), GFP_KERNEL); 1958a556c76aSAlexandre Belloni if (!ocelot->stats) 1959a556c76aSAlexandre Belloni return -ENOMEM; 1960a556c76aSAlexandre Belloni 1961a556c76aSAlexandre Belloni mutex_init(&ocelot->stats_lock); 19624e3b0468SAntoine Tenart mutex_init(&ocelot->ptp_lock); 19634e3b0468SAntoine Tenart spin_lock_init(&ocelot->ptp_clock_lock); 1964a556c76aSAlexandre Belloni snprintf(queue_name, sizeof(queue_name), "%s-stats", 1965a556c76aSAlexandre Belloni dev_name(ocelot->dev)); 1966a556c76aSAlexandre Belloni ocelot->stats_queue = create_singlethread_workqueue(queue_name); 1967a556c76aSAlexandre Belloni if (!ocelot->stats_queue) 1968a556c76aSAlexandre Belloni return -ENOMEM; 1969a556c76aSAlexandre Belloni 1970ca0b272bSVladimir Oltean ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); 1971ca0b272bSVladimir Oltean if (!ocelot->owq) { 1972ca0b272bSVladimir Oltean destroy_workqueue(ocelot->stats_queue); 1973ca0b272bSVladimir Oltean return -ENOMEM; 1974ca0b272bSVladimir Oltean } 1975ca0b272bSVladimir Oltean 19762b120ddeSClaudiu Manoil INIT_LIST_HEAD(&ocelot->multicast); 1977e5d1f896SVladimir Oltean INIT_LIST_HEAD(&ocelot->pgids); 1978f6fe01d6SVladimir Oltean ocelot_detect_features(ocelot); 1979a556c76aSAlexandre Belloni ocelot_mact_init(ocelot); 1980a556c76aSAlexandre Belloni ocelot_vlan_init(ocelot); 1981aae4e500SVladimir Oltean ocelot_vcap_init(ocelot); 19822d44b097SVladimir Oltean ocelot_cpu_port_init(ocelot); 1983a556c76aSAlexandre Belloni 1984a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 1985a556c76aSAlexandre Belloni /* Clear all counters (5 groups) */ 1986a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 1987a556c76aSAlexandre Belloni SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 1988a556c76aSAlexandre Belloni SYS_STAT_CFG); 1989a556c76aSAlexandre Belloni } 1990a556c76aSAlexandre Belloni 1991a556c76aSAlexandre Belloni /* Only use S-Tag */ 1992a556c76aSAlexandre Belloni ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 1993a556c76aSAlexandre Belloni 1994a556c76aSAlexandre Belloni /* Aggregation mode */ 1995a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 1996a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_DMAC_ENA | 1997a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 1998f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | 1999f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | 2000f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, 2001f79c20c8SVladimir Oltean ANA_AGGR_CFG); 2002a556c76aSAlexandre Belloni 2003a556c76aSAlexandre Belloni /* Set MAC age time to default value. The entry is aged after 2004a556c76aSAlexandre Belloni * 2*AGE_PERIOD 2005a556c76aSAlexandre Belloni */ 2006a556c76aSAlexandre Belloni ocelot_write(ocelot, 2007a556c76aSAlexandre Belloni ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 2008a556c76aSAlexandre Belloni ANA_AUTOAGE); 2009a556c76aSAlexandre Belloni 2010a556c76aSAlexandre Belloni /* Disable learning for frames discarded by VLAN ingress filtering */ 2011a556c76aSAlexandre Belloni regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 2012a556c76aSAlexandre Belloni 2013a556c76aSAlexandre Belloni /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 2014a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 2015a556c76aSAlexandre Belloni SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 2016a556c76aSAlexandre Belloni 2017a556c76aSAlexandre Belloni /* Setup flooding PGIDs */ 2018edd2410bSVladimir Oltean for (i = 0; i < ocelot->num_flooding_pgids; i++) 2019a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 2020b360d94fSVladimir Oltean ANA_FLOODING_FLD_BROADCAST(PGID_BC) | 2021a556c76aSAlexandre Belloni ANA_FLOODING_FLD_UNICAST(PGID_UC), 2022edd2410bSVladimir Oltean ANA_FLOODING, i); 2023a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 2024a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 2025a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 2026a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 2027a556c76aSAlexandre Belloni ANA_FLOODING_IPMC); 2028a556c76aSAlexandre Belloni 2029a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 2030a556c76aSAlexandre Belloni /* Transmit the frame to the local port. */ 2031a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 2032a556c76aSAlexandre Belloni /* Do not forward BPDU frames to the front ports. */ 2033a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, 2034a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 2035a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG, 2036a556c76aSAlexandre Belloni port); 2037a556c76aSAlexandre Belloni /* Ensure bridging is disabled */ 2038a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 2039a556c76aSAlexandre Belloni } 2040a556c76aSAlexandre Belloni 204196b029b0SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, i) { 2042a556c76aSAlexandre Belloni u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 2043a556c76aSAlexandre Belloni 2044a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 2045a556c76aSAlexandre Belloni } 2046b360d94fSVladimir Oltean /* Allow broadcast and unknown L2 multicast to the CPU. */ 2047b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2048b360d94fSVladimir Oltean ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2049a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_MC); 2050b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2051b360d94fSVladimir Oltean ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2052b360d94fSVladimir Oltean ANA_PGID_PGID, PGID_BC); 2053a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 2054a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 2055a556c76aSAlexandre Belloni 2056a556c76aSAlexandre Belloni /* Allow manual injection via DEVCPU_QS registers, and byte swap these 2057a556c76aSAlexandre Belloni * registers endianness. 2058a556c76aSAlexandre Belloni */ 2059a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 2060a556c76aSAlexandre Belloni QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 2061a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 2062a556c76aSAlexandre Belloni QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 2063a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 2064a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LRN(2) | 2065a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 2066a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 2067a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 2068a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 2069a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 2070a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IGMP(6) | 2071a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 2072a556c76aSAlexandre Belloni for (i = 0; i < 16; i++) 2073a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 2074a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 2075a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG, i); 2076a556c76aSAlexandre Belloni 20771e1caa97SClaudiu Manoil INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 2078a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 2079a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 20804e3b0468SAntoine Tenart 2081a556c76aSAlexandre Belloni return 0; 2082a556c76aSAlexandre Belloni } 2083a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init); 2084a556c76aSAlexandre Belloni 2085a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot) 2086a556c76aSAlexandre Belloni { 2087c5d13969SClaudiu Manoil cancel_delayed_work(&ocelot->stats_work); 2088a556c76aSAlexandre Belloni destroy_workqueue(ocelot->stats_queue); 2089ca0b272bSVladimir Oltean destroy_workqueue(ocelot->owq); 2090a556c76aSAlexandre Belloni mutex_destroy(&ocelot->stats_lock); 2091a556c76aSAlexandre Belloni } 2092a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit); 2093a556c76aSAlexandre Belloni 2094e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port) 2095e5fb512dSVladimir Oltean { 2096e5fb512dSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2097e5fb512dSVladimir Oltean 2098e5fb512dSVladimir Oltean skb_queue_purge(&ocelot_port->tx_skbs); 2099e5fb512dSVladimir Oltean } 2100e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port); 2101e5fb512dSVladimir Oltean 2102a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL"); 2103