1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2a556c76aSAlexandre Belloni /* 3a556c76aSAlexandre Belloni * Microsemi Ocelot Switch driver 4a556c76aSAlexandre Belloni * 5a556c76aSAlexandre Belloni * Copyright (c) 2017 Microsemi Corporation 6a556c76aSAlexandre Belloni */ 740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h> 8a556c76aSAlexandre Belloni #include <linux/if_bridge.h> 939e5308bSYangbo Lu #include <linux/ptp_classify.h> 1020968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h> 11a556c76aSAlexandre Belloni #include "ocelot.h" 123c83654fSVladimir Oltean #include "ocelot_vcap.h" 13a556c76aSAlexandre Belloni 14639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10 15639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000 16639c1b26SSteen Hegelund 17a556c76aSAlexandre Belloni struct ocelot_mact_entry { 18a556c76aSAlexandre Belloni u8 mac[ETH_ALEN]; 19a556c76aSAlexandre Belloni u16 vid; 20a556c76aSAlexandre Belloni enum macaccess_entry_type type; 21a556c76aSAlexandre Belloni }; 22a556c76aSAlexandre Belloni 232468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */ 24639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) 25639c1b26SSteen Hegelund { 26639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_MACACCESS); 27639c1b26SSteen Hegelund } 28639c1b26SSteen Hegelund 292468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */ 30a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) 31a556c76aSAlexandre Belloni { 32639c1b26SSteen Hegelund u32 val; 33a556c76aSAlexandre Belloni 34639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_mact_read_macaccess, 35639c1b26SSteen Hegelund ocelot, val, 36639c1b26SSteen Hegelund (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == 37639c1b26SSteen Hegelund MACACCESS_CMD_IDLE, 38639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 39a556c76aSAlexandre Belloni } 40a556c76aSAlexandre Belloni 412468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */ 42a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot, 43a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 44a556c76aSAlexandre Belloni unsigned int vid) 45a556c76aSAlexandre Belloni { 46a556c76aSAlexandre Belloni u32 macl = 0, mach = 0; 47a556c76aSAlexandre Belloni 48a556c76aSAlexandre Belloni /* Set the MAC address to handle and the vlan associated in a format 49a556c76aSAlexandre Belloni * understood by the hardware. 50a556c76aSAlexandre Belloni */ 51a556c76aSAlexandre Belloni mach |= vid << 16; 52a556c76aSAlexandre Belloni mach |= mac[0] << 8; 53a556c76aSAlexandre Belloni mach |= mac[1] << 0; 54a556c76aSAlexandre Belloni macl |= mac[2] << 24; 55a556c76aSAlexandre Belloni macl |= mac[3] << 16; 56a556c76aSAlexandre Belloni macl |= mac[4] << 8; 57a556c76aSAlexandre Belloni macl |= mac[5] << 0; 58a556c76aSAlexandre Belloni 59a556c76aSAlexandre Belloni ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); 60a556c76aSAlexandre Belloni ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); 61a556c76aSAlexandre Belloni 62a556c76aSAlexandre Belloni } 63a556c76aSAlexandre Belloni 640568c3bfSXiaoliang Yang static int __ocelot_mact_learn(struct ocelot *ocelot, int port, 65a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 669c90eea3SVladimir Oltean unsigned int vid, enum macaccess_entry_type type) 67a556c76aSAlexandre Belloni { 68584b7cfcSAlban Bedel u32 cmd = ANA_TABLES_MACACCESS_VALID | 69584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_DEST_IDX(port) | 70584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_ENTRYTYPE(type) | 71584b7cfcSAlban Bedel ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); 72584b7cfcSAlban Bedel unsigned int mc_ports; 732468346cSVladimir Oltean int err; 74584b7cfcSAlban Bedel 75584b7cfcSAlban Bedel /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ 76584b7cfcSAlban Bedel if (type == ENTRYTYPE_MACv4) 77584b7cfcSAlban Bedel mc_ports = (mac[1] << 8) | mac[2]; 78584b7cfcSAlban Bedel else if (type == ENTRYTYPE_MACv6) 79584b7cfcSAlban Bedel mc_ports = (mac[0] << 8) | mac[1]; 80584b7cfcSAlban Bedel else 81584b7cfcSAlban Bedel mc_ports = 0; 82584b7cfcSAlban Bedel 83584b7cfcSAlban Bedel if (mc_ports & BIT(ocelot->num_phys_ports)) 84584b7cfcSAlban Bedel cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; 85584b7cfcSAlban Bedel 86a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 87a556c76aSAlexandre Belloni 88a556c76aSAlexandre Belloni /* Issue a write command */ 89584b7cfcSAlban Bedel ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); 90a556c76aSAlexandre Belloni 912468346cSVladimir Oltean err = ocelot_mact_wait_for_completion(ocelot); 922468346cSVladimir Oltean 930568c3bfSXiaoliang Yang return err; 940568c3bfSXiaoliang Yang } 950568c3bfSXiaoliang Yang 960568c3bfSXiaoliang Yang int ocelot_mact_learn(struct ocelot *ocelot, int port, 970568c3bfSXiaoliang Yang const unsigned char mac[ETH_ALEN], 980568c3bfSXiaoliang Yang unsigned int vid, enum macaccess_entry_type type) 990568c3bfSXiaoliang Yang { 1000568c3bfSXiaoliang Yang int ret; 1010568c3bfSXiaoliang Yang 1020568c3bfSXiaoliang Yang mutex_lock(&ocelot->mact_lock); 1030568c3bfSXiaoliang Yang ret = __ocelot_mact_learn(ocelot, port, mac, vid, type); 1042468346cSVladimir Oltean mutex_unlock(&ocelot->mact_lock); 1052468346cSVladimir Oltean 1060568c3bfSXiaoliang Yang return ret; 107a556c76aSAlexandre Belloni } 1089c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn); 109a556c76aSAlexandre Belloni 1109c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot, 1119c90eea3SVladimir Oltean const unsigned char mac[ETH_ALEN], unsigned int vid) 112a556c76aSAlexandre Belloni { 1132468346cSVladimir Oltean int err; 1142468346cSVladimir Oltean 1152468346cSVladimir Oltean mutex_lock(&ocelot->mact_lock); 1162468346cSVladimir Oltean 117a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 118a556c76aSAlexandre Belloni 119a556c76aSAlexandre Belloni /* Issue a forget command */ 120a556c76aSAlexandre Belloni ocelot_write(ocelot, 121a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), 122a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 123a556c76aSAlexandre Belloni 1242468346cSVladimir Oltean err = ocelot_mact_wait_for_completion(ocelot); 1252468346cSVladimir Oltean 1262468346cSVladimir Oltean mutex_unlock(&ocelot->mact_lock); 1272468346cSVladimir Oltean 1282468346cSVladimir Oltean return err; 129a556c76aSAlexandre Belloni } 1309c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget); 131a556c76aSAlexandre Belloni 1320568c3bfSXiaoliang Yang int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx, 1330568c3bfSXiaoliang Yang const unsigned char mac[ETH_ALEN], 1340568c3bfSXiaoliang Yang unsigned int vid, enum macaccess_entry_type *type) 1350568c3bfSXiaoliang Yang { 1360568c3bfSXiaoliang Yang int val; 1370568c3bfSXiaoliang Yang 1380568c3bfSXiaoliang Yang mutex_lock(&ocelot->mact_lock); 1390568c3bfSXiaoliang Yang 1400568c3bfSXiaoliang Yang ocelot_mact_select(ocelot, mac, vid); 1410568c3bfSXiaoliang Yang 1420568c3bfSXiaoliang Yang /* Issue a read command with MACACCESS_VALID=1. */ 1430568c3bfSXiaoliang Yang ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID | 1440568c3bfSXiaoliang Yang ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 1450568c3bfSXiaoliang Yang ANA_TABLES_MACACCESS); 1460568c3bfSXiaoliang Yang 1470568c3bfSXiaoliang Yang if (ocelot_mact_wait_for_completion(ocelot)) { 1480568c3bfSXiaoliang Yang mutex_unlock(&ocelot->mact_lock); 1490568c3bfSXiaoliang Yang return -ETIMEDOUT; 1500568c3bfSXiaoliang Yang } 1510568c3bfSXiaoliang Yang 1520568c3bfSXiaoliang Yang /* Read back the entry flags */ 1530568c3bfSXiaoliang Yang val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 1540568c3bfSXiaoliang Yang 1550568c3bfSXiaoliang Yang mutex_unlock(&ocelot->mact_lock); 1560568c3bfSXiaoliang Yang 1570568c3bfSXiaoliang Yang if (!(val & ANA_TABLES_MACACCESS_VALID)) 1580568c3bfSXiaoliang Yang return -ENOENT; 1590568c3bfSXiaoliang Yang 1600568c3bfSXiaoliang Yang *dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val); 1610568c3bfSXiaoliang Yang *type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val); 1620568c3bfSXiaoliang Yang 1630568c3bfSXiaoliang Yang return 0; 1640568c3bfSXiaoliang Yang } 1650568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_lookup); 1660568c3bfSXiaoliang Yang 1670568c3bfSXiaoliang Yang int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx, 1680568c3bfSXiaoliang Yang const unsigned char mac[ETH_ALEN], 1690568c3bfSXiaoliang Yang unsigned int vid, 1700568c3bfSXiaoliang Yang enum macaccess_entry_type type, 1710568c3bfSXiaoliang Yang int sfid, int ssid) 1720568c3bfSXiaoliang Yang { 1730568c3bfSXiaoliang Yang int ret; 1740568c3bfSXiaoliang Yang 1750568c3bfSXiaoliang Yang mutex_lock(&ocelot->mact_lock); 1760568c3bfSXiaoliang Yang 1770568c3bfSXiaoliang Yang ocelot_write(ocelot, 1780568c3bfSXiaoliang Yang (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) | 1790568c3bfSXiaoliang Yang ANA_TABLES_STREAMDATA_SFID(sfid) | 1800568c3bfSXiaoliang Yang (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) | 1810568c3bfSXiaoliang Yang ANA_TABLES_STREAMDATA_SSID(ssid), 1820568c3bfSXiaoliang Yang ANA_TABLES_STREAMDATA); 1830568c3bfSXiaoliang Yang 1840568c3bfSXiaoliang Yang ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type); 1850568c3bfSXiaoliang Yang 1860568c3bfSXiaoliang Yang mutex_unlock(&ocelot->mact_lock); 1870568c3bfSXiaoliang Yang 1880568c3bfSXiaoliang Yang return ret; 1890568c3bfSXiaoliang Yang } 1900568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_learn_streamdata); 1910568c3bfSXiaoliang Yang 192a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot) 193a556c76aSAlexandre Belloni { 194a556c76aSAlexandre Belloni /* Configure the learning mode entries attributes: 195a556c76aSAlexandre Belloni * - Do not copy the frame to the CPU extraction queues. 196a556c76aSAlexandre Belloni * - Use the vlan and mac_cpoy for dmac lookup. 197a556c76aSAlexandre Belloni */ 198a556c76aSAlexandre Belloni ocelot_rmw(ocelot, 0, 199a556c76aSAlexandre Belloni ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS 200a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_FWD_KILL 201a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_IGNORE_VLAN, 202a556c76aSAlexandre Belloni ANA_AGENCTRL); 203a556c76aSAlexandre Belloni 2042468346cSVladimir Oltean /* Clear the MAC table. We are not concurrent with anyone, so 2052468346cSVladimir Oltean * holding &ocelot->mact_lock is pointless. 2062468346cSVladimir Oltean */ 207a556c76aSAlexandre Belloni ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); 208a556c76aSAlexandre Belloni } 209a556c76aSAlexandre Belloni 210f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port) 211b5962294SHoratiu Vultur { 212b5962294SHoratiu Vultur ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | 213b5962294SHoratiu Vultur ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), 214f270dbfaSVladimir Oltean ANA_PORT_VCAP_S2_CFG, port); 21575944fdaSXiaoliang Yang 21675944fdaSXiaoliang Yang ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, 21775944fdaSXiaoliang Yang ANA_PORT_VCAP_CFG, port); 2182f17c050SXiaoliang Yang 2192f17c050SXiaoliang Yang ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, 2202f17c050SXiaoliang Yang REW_PORT_CFG_ES0_EN, 2212f17c050SXiaoliang Yang REW_PORT_CFG, port); 222b5962294SHoratiu Vultur } 223b5962294SHoratiu Vultur 224639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) 225639c1b26SSteen Hegelund { 226639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); 227639c1b26SSteen Hegelund } 228639c1b26SSteen Hegelund 229a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) 230a556c76aSAlexandre Belloni { 231639c1b26SSteen Hegelund u32 val; 232a556c76aSAlexandre Belloni 233639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_vlant_read_vlanaccess, 234639c1b26SSteen Hegelund ocelot, 235639c1b26SSteen Hegelund val, 236639c1b26SSteen Hegelund (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == 237639c1b26SSteen Hegelund ANA_TABLES_VLANACCESS_CMD_IDLE, 238639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 239a556c76aSAlexandre Belloni } 240a556c76aSAlexandre Belloni 2417142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) 2427142529fSAntoine Tenart { 2437142529fSAntoine Tenart /* Select the VID to configure */ 2447142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), 2457142529fSAntoine Tenart ANA_TABLES_VLANTIDX); 2467142529fSAntoine Tenart /* Set the vlan port members mask and issue a write command */ 2477142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | 2487142529fSAntoine Tenart ANA_TABLES_VLANACCESS_CMD_WRITE, 2497142529fSAntoine Tenart ANA_TABLES_VLANACCESS); 2507142529fSAntoine Tenart 2517142529fSAntoine Tenart return ocelot_vlant_wait_for_completion(ocelot); 2527142529fSAntoine Tenart } 2537142529fSAntoine Tenart 2540da1a1c4SVladimir Oltean static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port) 2550da1a1c4SVladimir Oltean { 2560da1a1c4SVladimir Oltean struct ocelot_bridge_vlan *vlan; 2570da1a1c4SVladimir Oltean int num_untagged = 0; 2580da1a1c4SVladimir Oltean 2590da1a1c4SVladimir Oltean list_for_each_entry(vlan, &ocelot->vlans, list) { 2600da1a1c4SVladimir Oltean if (!(vlan->portmask & BIT(port))) 2610da1a1c4SVladimir Oltean continue; 2620da1a1c4SVladimir Oltean 2630da1a1c4SVladimir Oltean if (vlan->untagged & BIT(port)) 2640da1a1c4SVladimir Oltean num_untagged++; 2650da1a1c4SVladimir Oltean } 2660da1a1c4SVladimir Oltean 2670da1a1c4SVladimir Oltean return num_untagged; 2680da1a1c4SVladimir Oltean } 2690da1a1c4SVladimir Oltean 2700da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port) 2710da1a1c4SVladimir Oltean { 2720da1a1c4SVladimir Oltean struct ocelot_bridge_vlan *vlan; 2730da1a1c4SVladimir Oltean int num_tagged = 0; 2740da1a1c4SVladimir Oltean 2750da1a1c4SVladimir Oltean list_for_each_entry(vlan, &ocelot->vlans, list) { 2760da1a1c4SVladimir Oltean if (!(vlan->portmask & BIT(port))) 2770da1a1c4SVladimir Oltean continue; 2780da1a1c4SVladimir Oltean 2790da1a1c4SVladimir Oltean if (!(vlan->untagged & BIT(port))) 2800da1a1c4SVladimir Oltean num_tagged++; 2810da1a1c4SVladimir Oltean } 2820da1a1c4SVladimir Oltean 2830da1a1c4SVladimir Oltean return num_tagged; 2840da1a1c4SVladimir Oltean } 2850da1a1c4SVladimir Oltean 2860da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly 2870da1a1c4SVladimir Oltean * _one_ egress-untagged VLAN (_the_ native VLAN) 2880da1a1c4SVladimir Oltean */ 2890da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port) 2900da1a1c4SVladimir Oltean { 2910da1a1c4SVladimir Oltean return ocelot_port_num_tagged_vlans(ocelot, port) && 2920da1a1c4SVladimir Oltean ocelot_port_num_untagged_vlans(ocelot, port) == 1; 2930da1a1c4SVladimir Oltean } 2940da1a1c4SVladimir Oltean 2950da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan * 2960da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port) 2970da1a1c4SVladimir Oltean { 2980da1a1c4SVladimir Oltean struct ocelot_bridge_vlan *vlan; 2990da1a1c4SVladimir Oltean 3000da1a1c4SVladimir Oltean list_for_each_entry(vlan, &ocelot->vlans, list) 3010da1a1c4SVladimir Oltean if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port)) 3020da1a1c4SVladimir Oltean return vlan; 3030da1a1c4SVladimir Oltean 3040da1a1c4SVladimir Oltean return NULL; 3050da1a1c4SVladimir Oltean } 3060da1a1c4SVladimir Oltean 3070da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable, 3080da1a1c4SVladimir Oltean * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness 3090da1a1c4SVladimir Oltean * state of the port. 3100da1a1c4SVladimir Oltean */ 3110da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port) 31297bb69e1SVladimir Oltean { 31397bb69e1SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 31462a22bcbSVladimir Oltean enum ocelot_port_tag_config tag_cfg; 3150da1a1c4SVladimir Oltean bool uses_native_vlan = false; 31697bb69e1SVladimir Oltean 31787b0f983SVladimir Oltean if (ocelot_port->vlan_aware) { 3180da1a1c4SVladimir Oltean uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port); 3190da1a1c4SVladimir Oltean 3200da1a1c4SVladimir Oltean if (uses_native_vlan) 32162a22bcbSVladimir Oltean tag_cfg = OCELOT_PORT_TAG_NATIVE; 3220da1a1c4SVladimir Oltean else if (ocelot_port_num_untagged_vlans(ocelot, port)) 3230da1a1c4SVladimir Oltean tag_cfg = OCELOT_PORT_TAG_DISABLED; 32487b0f983SVladimir Oltean else 32562a22bcbSVladimir Oltean tag_cfg = OCELOT_PORT_TAG_TRUNK; 32687b0f983SVladimir Oltean } else { 32762a22bcbSVladimir Oltean tag_cfg = OCELOT_PORT_TAG_DISABLED; 32887b0f983SVladimir Oltean } 3290da1a1c4SVladimir Oltean 33062a22bcbSVladimir Oltean ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg), 33187b0f983SVladimir Oltean REW_TAG_CFG_TAG_CFG_M, 33287b0f983SVladimir Oltean REW_TAG_CFG, port); 3330da1a1c4SVladimir Oltean 3340da1a1c4SVladimir Oltean if (uses_native_vlan) { 3350da1a1c4SVladimir Oltean struct ocelot_bridge_vlan *native_vlan; 3360da1a1c4SVladimir Oltean 3370da1a1c4SVladimir Oltean /* Not having a native VLAN is impossible, because 3380da1a1c4SVladimir Oltean * ocelot_port_num_untagged_vlans has returned 1. 3390da1a1c4SVladimir Oltean * So there is no use in checking for NULL here. 3400da1a1c4SVladimir Oltean */ 3410da1a1c4SVladimir Oltean native_vlan = ocelot_port_find_native_vlan(ocelot, port); 3420da1a1c4SVladimir Oltean 3430da1a1c4SVladimir Oltean ocelot_rmw_gix(ocelot, 3440da1a1c4SVladimir Oltean REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid), 3450da1a1c4SVladimir Oltean REW_PORT_VLAN_CFG_PORT_VID_M, 3460da1a1c4SVladimir Oltean REW_PORT_VLAN_CFG, port); 3470da1a1c4SVladimir Oltean } 34897bb69e1SVladimir Oltean } 34997bb69e1SVladimir Oltean 35075e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */ 351c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, 352d4004422SVladimir Oltean const struct ocelot_bridge_vlan *pvid_vlan) 35375e5a554SVladimir Oltean { 35475e5a554SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 355d4004422SVladimir Oltean u16 pvid = OCELOT_VLAN_UNAWARE_PVID; 356be0576feSVladimir Oltean u32 val = 0; 35775e5a554SVladimir Oltean 358c3e58a75SVladimir Oltean ocelot_port->pvid_vlan = pvid_vlan; 35975e5a554SVladimir Oltean 360d4004422SVladimir Oltean if (ocelot_port->vlan_aware && pvid_vlan) 361d4004422SVladimir Oltean pvid = pvid_vlan->vid; 36275e5a554SVladimir Oltean 36375e5a554SVladimir Oltean ocelot_rmw_gix(ocelot, 364d4004422SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID(pvid), 36575e5a554SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID_M, 36675e5a554SVladimir Oltean ANA_PORT_VLAN_CFG, port); 367be0576feSVladimir Oltean 368be0576feSVladimir Oltean /* If there's no pvid, we should drop not only untagged traffic (which 369be0576feSVladimir Oltean * happens automatically), but also 802.1p traffic which gets 370be0576feSVladimir Oltean * classified to VLAN 0, but that is always in our RX filter, so it 371be0576feSVladimir Oltean * would get accepted were it not for this setting. 372be0576feSVladimir Oltean */ 373d4004422SVladimir Oltean if (!pvid_vlan && ocelot_port->vlan_aware) 374be0576feSVladimir Oltean val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 375be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; 376be0576feSVladimir Oltean 377be0576feSVladimir Oltean ocelot_rmw_gix(ocelot, val, 378be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 379be0576feSVladimir Oltean ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, 380be0576feSVladimir Oltean ANA_PORT_DROP_CFG, port); 38175e5a554SVladimir Oltean } 38275e5a554SVladimir Oltean 38390e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot, 38490e0aa8dSVladimir Oltean u16 vid) 385bbf6a2d9SVladimir Oltean { 38690e0aa8dSVladimir Oltean struct ocelot_bridge_vlan *vlan; 387bbf6a2d9SVladimir Oltean 38890e0aa8dSVladimir Oltean list_for_each_entry(vlan, &ocelot->vlans, list) 38990e0aa8dSVladimir Oltean if (vlan->vid == vid) 39090e0aa8dSVladimir Oltean return vlan; 391bbf6a2d9SVladimir Oltean 39290e0aa8dSVladimir Oltean return NULL; 393bbf6a2d9SVladimir Oltean } 394bbf6a2d9SVladimir Oltean 3950da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid, 3960da1a1c4SVladimir Oltean bool untagged) 397bbf6a2d9SVladimir Oltean { 39890e0aa8dSVladimir Oltean struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid); 39990e0aa8dSVladimir Oltean unsigned long portmask; 40090e0aa8dSVladimir Oltean int err; 40190e0aa8dSVladimir Oltean 40290e0aa8dSVladimir Oltean if (vlan) { 40390e0aa8dSVladimir Oltean portmask = vlan->portmask | BIT(port); 40490e0aa8dSVladimir Oltean 40590e0aa8dSVladimir Oltean err = ocelot_vlant_set_mask(ocelot, vid, portmask); 40690e0aa8dSVladimir Oltean if (err) 40790e0aa8dSVladimir Oltean return err; 40890e0aa8dSVladimir Oltean 40990e0aa8dSVladimir Oltean vlan->portmask = portmask; 4100da1a1c4SVladimir Oltean /* Bridge VLANs can be overwritten with a different 4110da1a1c4SVladimir Oltean * egress-tagging setting, so make sure to override an untagged 4120da1a1c4SVladimir Oltean * with a tagged VID if that's going on. 4130da1a1c4SVladimir Oltean */ 4140da1a1c4SVladimir Oltean if (untagged) 4150da1a1c4SVladimir Oltean vlan->untagged |= BIT(port); 4160da1a1c4SVladimir Oltean else 4170da1a1c4SVladimir Oltean vlan->untagged &= ~BIT(port); 41890e0aa8dSVladimir Oltean 41990e0aa8dSVladimir Oltean return 0; 42090e0aa8dSVladimir Oltean } 42190e0aa8dSVladimir Oltean 42290e0aa8dSVladimir Oltean vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); 42390e0aa8dSVladimir Oltean if (!vlan) 42490e0aa8dSVladimir Oltean return -ENOMEM; 42590e0aa8dSVladimir Oltean 42690e0aa8dSVladimir Oltean portmask = BIT(port); 42790e0aa8dSVladimir Oltean 42890e0aa8dSVladimir Oltean err = ocelot_vlant_set_mask(ocelot, vid, portmask); 42990e0aa8dSVladimir Oltean if (err) { 43090e0aa8dSVladimir Oltean kfree(vlan); 43190e0aa8dSVladimir Oltean return err; 43290e0aa8dSVladimir Oltean } 43390e0aa8dSVladimir Oltean 43490e0aa8dSVladimir Oltean vlan->vid = vid; 43590e0aa8dSVladimir Oltean vlan->portmask = portmask; 4360da1a1c4SVladimir Oltean if (untagged) 4370da1a1c4SVladimir Oltean vlan->untagged = BIT(port); 43890e0aa8dSVladimir Oltean INIT_LIST_HEAD(&vlan->list); 43990e0aa8dSVladimir Oltean list_add_tail(&vlan->list, &ocelot->vlans); 44090e0aa8dSVladimir Oltean 44190e0aa8dSVladimir Oltean return 0; 442bbf6a2d9SVladimir Oltean } 443bbf6a2d9SVladimir Oltean 444bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid) 445bbf6a2d9SVladimir Oltean { 44690e0aa8dSVladimir Oltean struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid); 44790e0aa8dSVladimir Oltean unsigned long portmask; 44890e0aa8dSVladimir Oltean int err; 44990e0aa8dSVladimir Oltean 45090e0aa8dSVladimir Oltean if (!vlan) 45190e0aa8dSVladimir Oltean return 0; 45290e0aa8dSVladimir Oltean 45390e0aa8dSVladimir Oltean portmask = vlan->portmask & ~BIT(port); 45490e0aa8dSVladimir Oltean 45590e0aa8dSVladimir Oltean err = ocelot_vlant_set_mask(ocelot, vid, portmask); 45690e0aa8dSVladimir Oltean if (err) 45790e0aa8dSVladimir Oltean return err; 45890e0aa8dSVladimir Oltean 45990e0aa8dSVladimir Oltean vlan->portmask = portmask; 46090e0aa8dSVladimir Oltean if (vlan->portmask) 46190e0aa8dSVladimir Oltean return 0; 46290e0aa8dSVladimir Oltean 46390e0aa8dSVladimir Oltean list_del(&vlan->list); 46490e0aa8dSVladimir Oltean kfree(vlan); 46590e0aa8dSVladimir Oltean 46690e0aa8dSVladimir Oltean return 0; 467bbf6a2d9SVladimir Oltean } 468bbf6a2d9SVladimir Oltean 4692e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, 4703b95d1b2SVladimir Oltean bool vlan_aware, struct netlink_ext_ack *extack) 47187b0f983SVladimir Oltean { 47270edfae1SVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; 473bae33f2bSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 47470edfae1SVladimir Oltean struct ocelot_vcap_filter *filter; 475bae33f2bSVladimir Oltean u32 val; 47670edfae1SVladimir Oltean 47770edfae1SVladimir Oltean list_for_each_entry(filter, &block->rules, list) { 47870edfae1SVladimir Oltean if (filter->ingress_port_mask & BIT(port) && 47970edfae1SVladimir Oltean filter->action.vid_replace_ena) { 4803b95d1b2SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 4813b95d1b2SVladimir Oltean "Cannot change VLAN state with vlan modify rules active"); 48270edfae1SVladimir Oltean return -EBUSY; 48370edfae1SVladimir Oltean } 48470edfae1SVladimir Oltean } 48570edfae1SVladimir Oltean 48687b0f983SVladimir Oltean ocelot_port->vlan_aware = vlan_aware; 48787b0f983SVladimir Oltean 48887b0f983SVladimir Oltean if (vlan_aware) 48987b0f983SVladimir Oltean val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 49087b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); 49187b0f983SVladimir Oltean else 49287b0f983SVladimir Oltean val = 0; 49387b0f983SVladimir Oltean ocelot_rmw_gix(ocelot, val, 49487b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 49587b0f983SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, 49687b0f983SVladimir Oltean ANA_PORT_VLAN_CFG, port); 49787b0f983SVladimir Oltean 498c3e58a75SVladimir Oltean ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); 4990da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 5002e554a7aSVladimir Oltean 5012e554a7aSVladimir Oltean return 0; 50287b0f983SVladimir Oltean } 50387b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering); 50487b0f983SVladimir Oltean 5052f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, 50601af940eSVladimir Oltean bool untagged, struct netlink_ext_ack *extack) 5072f0402feSVladimir Oltean { 5080da1a1c4SVladimir Oltean if (untagged) { 5090da1a1c4SVladimir Oltean /* We are adding an egress-tagged VLAN */ 5100da1a1c4SVladimir Oltean if (ocelot_port_uses_native_vlan(ocelot, port)) { 51101af940eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 5120da1a1c4SVladimir Oltean "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN"); 5132f0402feSVladimir Oltean return -EBUSY; 5142f0402feSVladimir Oltean } 5150da1a1c4SVladimir Oltean } else { 5160da1a1c4SVladimir Oltean /* We are adding an egress-tagged VLAN */ 5170da1a1c4SVladimir Oltean if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) { 5180da1a1c4SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 5190da1a1c4SVladimir Oltean "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs"); 5200da1a1c4SVladimir Oltean return -EBUSY; 5210da1a1c4SVladimir Oltean } 5220da1a1c4SVladimir Oltean } 5232f0402feSVladimir Oltean 5242f0402feSVladimir Oltean return 0; 5252f0402feSVladimir Oltean } 5262f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare); 5272f0402feSVladimir Oltean 5285e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, 5297142529fSAntoine Tenart bool untagged) 5307142529fSAntoine Tenart { 531bbf6a2d9SVladimir Oltean int err; 5327142529fSAntoine Tenart 5330da1a1c4SVladimir Oltean err = ocelot_vlan_member_add(ocelot, port, vid, untagged); 534bbf6a2d9SVladimir Oltean if (err) 535bbf6a2d9SVladimir Oltean return err; 5367142529fSAntoine Tenart 5377142529fSAntoine Tenart /* Default ingress vlan classification */ 538d4004422SVladimir Oltean if (pvid) 539d4004422SVladimir Oltean ocelot_port_set_pvid(ocelot, port, 540d4004422SVladimir Oltean ocelot_bridge_vlan_find(ocelot, vid)); 5417142529fSAntoine Tenart 5427142529fSAntoine Tenart /* Untagged egress vlan clasification */ 5430da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 5447142529fSAntoine Tenart 5457142529fSAntoine Tenart return 0; 5467142529fSAntoine Tenart } 5475e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add); 5487142529fSAntoine Tenart 5495e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) 5509855934cSVladimir Oltean { 5519855934cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 552bbf6a2d9SVladimir Oltean int err; 5537142529fSAntoine Tenart 554bbf6a2d9SVladimir Oltean err = ocelot_vlan_member_del(ocelot, port, vid); 555bbf6a2d9SVladimir Oltean if (err) 556bbf6a2d9SVladimir Oltean return err; 5577142529fSAntoine Tenart 558be0576feSVladimir Oltean /* Ingress */ 559d4004422SVladimir Oltean if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid) 560d4004422SVladimir Oltean ocelot_port_set_pvid(ocelot, port, NULL); 561be0576feSVladimir Oltean 5627142529fSAntoine Tenart /* Egress */ 5630da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 5647142529fSAntoine Tenart 5657142529fSAntoine Tenart return 0; 5667142529fSAntoine Tenart } 5675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del); 5687142529fSAntoine Tenart 569a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot) 570a556c76aSAlexandre Belloni { 571bbf6a2d9SVladimir Oltean unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0); 5727142529fSAntoine Tenart u16 port, vid; 5737142529fSAntoine Tenart 574a556c76aSAlexandre Belloni /* Clear VLAN table, by default all ports are members of all VLANs */ 575a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, 576a556c76aSAlexandre Belloni ANA_TABLES_VLANACCESS); 577a556c76aSAlexandre Belloni ocelot_vlant_wait_for_completion(ocelot); 5787142529fSAntoine Tenart 5797142529fSAntoine Tenart /* Configure the port VLAN memberships */ 580bbf6a2d9SVladimir Oltean for (vid = 1; vid < VLAN_N_VID; vid++) 58190e0aa8dSVladimir Oltean ocelot_vlant_set_mask(ocelot, vid, 0); 5827142529fSAntoine Tenart 5837142529fSAntoine Tenart /* Because VLAN filtering is enabled, we need VID 0 to get untagged 5847142529fSAntoine Tenart * traffic. It is added automatically if 8021q module is loaded, but 5857142529fSAntoine Tenart * we can't rely on it since module may be not loaded. 5867142529fSAntoine Tenart */ 587bfbab310SVladimir Oltean ocelot_vlant_set_mask(ocelot, OCELOT_VLAN_UNAWARE_PVID, all_ports); 5887142529fSAntoine Tenart 5897142529fSAntoine Tenart /* Set vlan ingress filter mask to all ports but the CPU port by 5907142529fSAntoine Tenart * default. 5917142529fSAntoine Tenart */ 592bbf6a2d9SVladimir Oltean ocelot_write(ocelot, all_ports, ANA_VLANMASK); 5937142529fSAntoine Tenart 5947142529fSAntoine Tenart for (port = 0; port < ocelot->num_phys_ports; port++) { 5957142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); 5967142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); 5977142529fSAntoine Tenart } 598a556c76aSAlexandre Belloni } 599a556c76aSAlexandre Belloni 600eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port) 601eb4733d7SVladimir Oltean { 602eb4733d7SVladimir Oltean return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port); 603eb4733d7SVladimir Oltean } 604eb4733d7SVladimir Oltean 605e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port) 606eb4733d7SVladimir Oltean { 6071650bdb1SVladimir Oltean unsigned int pause_ena; 608eb4733d7SVladimir Oltean int err, val; 609eb4733d7SVladimir Oltean 610eb4733d7SVladimir Oltean /* Disable dequeuing from the egress queues */ 611eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS, 612eb4733d7SVladimir Oltean QSYS_PORT_MODE_DEQUEUE_DIS, 613eb4733d7SVladimir Oltean QSYS_PORT_MODE, port); 614eb4733d7SVladimir Oltean 615eb4733d7SVladimir Oltean /* Disable flow control */ 6161650bdb1SVladimir Oltean ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena); 617eb4733d7SVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 618eb4733d7SVladimir Oltean 619eb4733d7SVladimir Oltean /* Disable priority flow control */ 620eb4733d7SVladimir Oltean ocelot_fields_write(ocelot, port, 621eb4733d7SVladimir Oltean QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0); 622eb4733d7SVladimir Oltean 623eb4733d7SVladimir Oltean /* Wait at least the time it takes to receive a frame of maximum length 624eb4733d7SVladimir Oltean * at the port. 625eb4733d7SVladimir Oltean * Worst-case delays for 10 kilobyte jumbo frames are: 626eb4733d7SVladimir Oltean * 8 ms on a 10M port 627eb4733d7SVladimir Oltean * 800 μs on a 100M port 628eb4733d7SVladimir Oltean * 80 μs on a 1G port 629eb4733d7SVladimir Oltean * 32 μs on a 2.5G port 630eb4733d7SVladimir Oltean */ 631eb4733d7SVladimir Oltean usleep_range(8000, 10000); 632eb4733d7SVladimir Oltean 633eb4733d7SVladimir Oltean /* Disable half duplex backpressure. */ 634eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE, 635eb4733d7SVladimir Oltean SYS_FRONT_PORT_MODE, port); 636eb4733d7SVladimir Oltean 637eb4733d7SVladimir Oltean /* Flush the queues associated with the port. */ 638eb4733d7SVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA, 639eb4733d7SVladimir Oltean REW_PORT_CFG, port); 640eb4733d7SVladimir Oltean 641eb4733d7SVladimir Oltean /* Enable dequeuing from the egress queues. */ 642eb4733d7SVladimir Oltean ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE, 643eb4733d7SVladimir Oltean port); 644eb4733d7SVladimir Oltean 645eb4733d7SVladimir Oltean /* Wait until flushing is complete. */ 646eb4733d7SVladimir Oltean err = read_poll_timeout(ocelot_read_eq_avail, val, !val, 647eb4733d7SVladimir Oltean 100, 2000000, false, ocelot, port); 648eb4733d7SVladimir Oltean 649eb4733d7SVladimir Oltean /* Clear flushing again. */ 650eb4733d7SVladimir Oltean ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port); 651eb4733d7SVladimir Oltean 6521650bdb1SVladimir Oltean /* Re-enable flow control */ 6531650bdb1SVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena); 6541650bdb1SVladimir Oltean 655eb4733d7SVladimir Oltean return err; 656eb4733d7SVladimir Oltean } 657eb4733d7SVladimir Oltean 658e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port, 659e6e12df6SVladimir Oltean unsigned int link_an_mode, 660e6e12df6SVladimir Oltean phy_interface_t interface, 661e6e12df6SVladimir Oltean unsigned long quirks) 662a556c76aSAlexandre Belloni { 66326f4dbabSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 664e6e12df6SVladimir Oltean int err; 665a556c76aSAlexandre Belloni 6668abe1970SVladimir Oltean ocelot_port->speed = SPEED_UNKNOWN; 6678abe1970SVladimir Oltean 668e6e12df6SVladimir Oltean ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA, 669e6e12df6SVladimir Oltean DEV_MAC_ENA_CFG); 670e6e12df6SVladimir Oltean 6718abe1970SVladimir Oltean if (ocelot->ops->cut_through_fwd) { 6728abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 6738abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 6748abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 6758abe1970SVladimir Oltean } 6768abe1970SVladimir Oltean 677e6e12df6SVladimir Oltean ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); 678e6e12df6SVladimir Oltean 679e6e12df6SVladimir Oltean err = ocelot_port_flush(ocelot, port); 680e6e12df6SVladimir Oltean if (err) 681e6e12df6SVladimir Oltean dev_err(ocelot->dev, "failed to flush port %d: %d\n", 682e6e12df6SVladimir Oltean port, err); 683e6e12df6SVladimir Oltean 684e6e12df6SVladimir Oltean /* Put the port in reset. */ 685e6e12df6SVladimir Oltean if (interface != PHY_INTERFACE_MODE_QSGMII || 686e6e12df6SVladimir Oltean !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP)) 687e6e12df6SVladimir Oltean ocelot_port_rmwl(ocelot_port, 688e6e12df6SVladimir Oltean DEV_CLOCK_CFG_MAC_TX_RST | 68974a3bc42SWan Jiabing DEV_CLOCK_CFG_MAC_RX_RST, 690e6e12df6SVladimir Oltean DEV_CLOCK_CFG_MAC_TX_RST | 69174a3bc42SWan Jiabing DEV_CLOCK_CFG_MAC_RX_RST, 692e6e12df6SVladimir Oltean DEV_CLOCK_CFG); 693e6e12df6SVladimir Oltean } 694e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down); 695e6e12df6SVladimir Oltean 696e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port, 697e6e12df6SVladimir Oltean struct phy_device *phydev, 698e6e12df6SVladimir Oltean unsigned int link_an_mode, 699e6e12df6SVladimir Oltean phy_interface_t interface, 700e6e12df6SVladimir Oltean int speed, int duplex, 701e6e12df6SVladimir Oltean bool tx_pause, bool rx_pause, 702e6e12df6SVladimir Oltean unsigned long quirks) 703e6e12df6SVladimir Oltean { 704e6e12df6SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 705e6e12df6SVladimir Oltean int mac_speed, mode = 0; 706e6e12df6SVladimir Oltean u32 mac_fc_cfg; 707e6e12df6SVladimir Oltean 7088abe1970SVladimir Oltean ocelot_port->speed = speed; 7098abe1970SVladimir Oltean 710e6e12df6SVladimir Oltean /* The MAC might be integrated in systems where the MAC speed is fixed 711e6e12df6SVladimir Oltean * and it's the PCS who is performing the rate adaptation, so we have 712e6e12df6SVladimir Oltean * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG 713e6e12df6SVladimir Oltean * (which is also its default value). 714e6e12df6SVladimir Oltean */ 715e6e12df6SVladimir Oltean if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) || 716e6e12df6SVladimir Oltean speed == SPEED_1000) { 717e6e12df6SVladimir Oltean mac_speed = OCELOT_SPEED_1000; 718e6e12df6SVladimir Oltean mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 719e6e12df6SVladimir Oltean } else if (speed == SPEED_2500) { 720e6e12df6SVladimir Oltean mac_speed = OCELOT_SPEED_2500; 721e6e12df6SVladimir Oltean mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 722e6e12df6SVladimir Oltean } else if (speed == SPEED_100) { 723e6e12df6SVladimir Oltean mac_speed = OCELOT_SPEED_100; 724e6e12df6SVladimir Oltean } else { 725e6e12df6SVladimir Oltean mac_speed = OCELOT_SPEED_10; 726e6e12df6SVladimir Oltean } 727e6e12df6SVladimir Oltean 728e6e12df6SVladimir Oltean if (duplex == DUPLEX_FULL) 729e6e12df6SVladimir Oltean mode |= DEV_MAC_MODE_CFG_FDX_ENA; 730e6e12df6SVladimir Oltean 731e6e12df6SVladimir Oltean ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG); 732e6e12df6SVladimir Oltean 733e6e12df6SVladimir Oltean /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and 734e6e12df6SVladimir Oltean * PORT_RST bits in DEV_CLOCK_CFG. 735e6e12df6SVladimir Oltean */ 736e6e12df6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed), 737e6e12df6SVladimir Oltean DEV_CLOCK_CFG); 738e6e12df6SVladimir Oltean 739e6e12df6SVladimir Oltean switch (speed) { 740a556c76aSAlexandre Belloni case SPEED_10: 741e6e12df6SVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10); 742a556c76aSAlexandre Belloni break; 743a556c76aSAlexandre Belloni case SPEED_100: 744e6e12df6SVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100); 745a556c76aSAlexandre Belloni break; 746a556c76aSAlexandre Belloni case SPEED_1000: 747a556c76aSAlexandre Belloni case SPEED_2500: 748e6e12df6SVladimir Oltean mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000); 749a556c76aSAlexandre Belloni break; 750a556c76aSAlexandre Belloni default: 751e6e12df6SVladimir Oltean dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n", 752e6e12df6SVladimir Oltean port, speed); 753a556c76aSAlexandre Belloni return; 754a556c76aSAlexandre Belloni } 755a556c76aSAlexandre Belloni 756e6e12df6SVladimir Oltean /* Handle RX pause in all cases, with 2500base-X this is used for rate 757e6e12df6SVladimir Oltean * adaptation. 758e6e12df6SVladimir Oltean */ 759e6e12df6SVladimir Oltean mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA; 760a556c76aSAlexandre Belloni 761e6e12df6SVladimir Oltean if (tx_pause) 762e6e12df6SVladimir Oltean mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA | 763e6e12df6SVladimir Oltean SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 764e6e12df6SVladimir Oltean SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 765e6e12df6SVladimir Oltean SYS_MAC_FC_CFG_ZERO_PAUSE_ENA; 766a556c76aSAlexandre Belloni 767e6e12df6SVladimir Oltean /* Flow control. Link speed is only used here to evaluate the time 768e6e12df6SVladimir Oltean * specification in incoming pause frames. 769e6e12df6SVladimir Oltean */ 770e6e12df6SVladimir Oltean ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port); 771a556c76aSAlexandre Belloni 772e6e12df6SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); 7731ba8f656SVladimir Oltean 774e6e12df6SVladimir Oltean ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause); 7751ba8f656SVladimir Oltean 776e6e12df6SVladimir Oltean /* Undo the effects of ocelot_phylink_mac_link_down: 777e6e12df6SVladimir Oltean * enable MAC module 778e6e12df6SVladimir Oltean */ 779004d44f6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | 780a556c76aSAlexandre Belloni DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 781a556c76aSAlexandre Belloni 7828abe1970SVladimir Oltean /* If the port supports cut-through forwarding, update the masks before 7838abe1970SVladimir Oltean * enabling forwarding on the port. 7848abe1970SVladimir Oltean */ 7858abe1970SVladimir Oltean if (ocelot->ops->cut_through_fwd) { 7868abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 7878abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 7888abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 7898abe1970SVladimir Oltean } 7908abe1970SVladimir Oltean 791a556c76aSAlexandre Belloni /* Core: Enable port for frame transfer */ 792886e1387SVladimir Oltean ocelot_fields_write(ocelot, port, 793886e1387SVladimir Oltean QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 794a556c76aSAlexandre Belloni } 795e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up); 796889b8950SVladimir Oltean 79752849bcfSVladimir Oltean static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, 798e2f9a8feSVladimir Oltean struct sk_buff *clone) 799400928bfSYangbo Lu { 800e2f9a8feSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 80152849bcfSVladimir Oltean unsigned long flags; 802400928bfSYangbo Lu 80352849bcfSVladimir Oltean spin_lock_irqsave(&ocelot->ts_id_lock, flags); 80452849bcfSVladimir Oltean 80552849bcfSVladimir Oltean if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID || 80652849bcfSVladimir Oltean ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) { 80752849bcfSVladimir Oltean spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); 80852849bcfSVladimir Oltean return -EBUSY; 80952849bcfSVladimir Oltean } 8106565243cSVladimir Oltean 811e2f9a8feSVladimir Oltean skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; 812c4b364ceSYangbo Lu /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */ 813c4b364ceSYangbo Lu OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id; 81452849bcfSVladimir Oltean 815c57fe003SVladimir Oltean ocelot_port->ts_id++; 816c57fe003SVladimir Oltean if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID) 817c57fe003SVladimir Oltean ocelot_port->ts_id = 0; 81852849bcfSVladimir Oltean 81952849bcfSVladimir Oltean ocelot_port->ptp_skbs_in_flight++; 82052849bcfSVladimir Oltean ocelot->ptp_skbs_in_flight++; 82152849bcfSVladimir Oltean 822e2f9a8feSVladimir Oltean skb_queue_tail(&ocelot_port->tx_skbs, clone); 8236565243cSVladimir Oltean 82452849bcfSVladimir Oltean spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); 82552849bcfSVladimir Oltean 82652849bcfSVladimir Oltean return 0; 827400928bfSYangbo Lu } 828682eaad9SYangbo Lu 829fba01283SVladimir Oltean static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb, 830fba01283SVladimir Oltean unsigned int ptp_class) 83139e5308bSYangbo Lu { 83239e5308bSYangbo Lu struct ptp_header *hdr; 83339e5308bSYangbo Lu u8 msgtype, twostep; 83439e5308bSYangbo Lu 83539e5308bSYangbo Lu hdr = ptp_parse_header(skb, ptp_class); 83639e5308bSYangbo Lu if (!hdr) 83739e5308bSYangbo Lu return false; 83839e5308bSYangbo Lu 83939e5308bSYangbo Lu msgtype = ptp_get_msgtype(hdr, ptp_class); 84039e5308bSYangbo Lu twostep = hdr->flag_field[0] & 0x2; 84139e5308bSYangbo Lu 84239e5308bSYangbo Lu if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) 84339e5308bSYangbo Lu return true; 84439e5308bSYangbo Lu 84539e5308bSYangbo Lu return false; 84639e5308bSYangbo Lu } 84739e5308bSYangbo Lu 848682eaad9SYangbo Lu int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port, 849682eaad9SYangbo Lu struct sk_buff *skb, 850682eaad9SYangbo Lu struct sk_buff **clone) 851682eaad9SYangbo Lu { 852682eaad9SYangbo Lu struct ocelot_port *ocelot_port = ocelot->ports[port]; 853682eaad9SYangbo Lu u8 ptp_cmd = ocelot_port->ptp_cmd; 854fba01283SVladimir Oltean unsigned int ptp_class; 85552849bcfSVladimir Oltean int err; 856682eaad9SYangbo Lu 857fba01283SVladimir Oltean /* Don't do anything if PTP timestamping not enabled */ 858fba01283SVladimir Oltean if (!ptp_cmd) 859fba01283SVladimir Oltean return 0; 860fba01283SVladimir Oltean 861fba01283SVladimir Oltean ptp_class = ptp_classify_raw(skb); 862fba01283SVladimir Oltean if (ptp_class == PTP_CLASS_NONE) 863fba01283SVladimir Oltean return -EINVAL; 864682eaad9SYangbo Lu 86539e5308bSYangbo Lu /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */ 86639e5308bSYangbo Lu if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) { 867fba01283SVladimir Oltean if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) { 86839e5308bSYangbo Lu OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; 86939e5308bSYangbo Lu return 0; 87039e5308bSYangbo Lu } 87139e5308bSYangbo Lu 87239e5308bSYangbo Lu /* Fall back to two-step timestamping */ 87339e5308bSYangbo Lu ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 87439e5308bSYangbo Lu } 87539e5308bSYangbo Lu 876682eaad9SYangbo Lu if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) { 877682eaad9SYangbo Lu *clone = skb_clone_sk(skb); 878682eaad9SYangbo Lu if (!(*clone)) 879682eaad9SYangbo Lu return -ENOMEM; 880682eaad9SYangbo Lu 88152849bcfSVladimir Oltean err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone); 88252849bcfSVladimir Oltean if (err) 88352849bcfSVladimir Oltean return err; 88452849bcfSVladimir Oltean 88539e5308bSYangbo Lu OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; 886ebb4c6a9SVladimir Oltean OCELOT_SKB_CB(*clone)->ptp_class = ptp_class; 887682eaad9SYangbo Lu } 888682eaad9SYangbo Lu 889682eaad9SYangbo Lu return 0; 890682eaad9SYangbo Lu } 891682eaad9SYangbo Lu EXPORT_SYMBOL(ocelot_port_txtstamp_request); 892400928bfSYangbo Lu 893e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot, 894e23a7b3eSYangbo Lu struct timespec64 *ts) 8954e3b0468SAntoine Tenart { 8964e3b0468SAntoine Tenart unsigned long flags; 8974e3b0468SAntoine Tenart u32 val; 8984e3b0468SAntoine Tenart 8994e3b0468SAntoine Tenart spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); 9004e3b0468SAntoine Tenart 9014e3b0468SAntoine Tenart /* Read current PTP time to get seconds */ 9024e3b0468SAntoine Tenart val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); 9034e3b0468SAntoine Tenart 9044e3b0468SAntoine Tenart val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); 9054e3b0468SAntoine Tenart val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); 9064e3b0468SAntoine Tenart ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); 9074e3b0468SAntoine Tenart ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); 9084e3b0468SAntoine Tenart 9094e3b0468SAntoine Tenart /* Read packet HW timestamp from FIFO */ 9104e3b0468SAntoine Tenart val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); 9114e3b0468SAntoine Tenart ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); 9124e3b0468SAntoine Tenart 9134e3b0468SAntoine Tenart /* Sec has incremented since the ts was registered */ 9144e3b0468SAntoine Tenart if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) 9154e3b0468SAntoine Tenart ts->tv_sec--; 9164e3b0468SAntoine Tenart 9174e3b0468SAntoine Tenart spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); 9184e3b0468SAntoine Tenart } 919e23a7b3eSYangbo Lu 920ebb4c6a9SVladimir Oltean static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid) 921ebb4c6a9SVladimir Oltean { 922ebb4c6a9SVladimir Oltean struct ptp_header *hdr; 923ebb4c6a9SVladimir Oltean 924ebb4c6a9SVladimir Oltean hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class); 925ebb4c6a9SVladimir Oltean if (WARN_ON(!hdr)) 926ebb4c6a9SVladimir Oltean return false; 927ebb4c6a9SVladimir Oltean 928ebb4c6a9SVladimir Oltean return seqid == ntohs(hdr->sequence_id); 929ebb4c6a9SVladimir Oltean } 930ebb4c6a9SVladimir Oltean 931e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot) 932e23a7b3eSYangbo Lu { 933e23a7b3eSYangbo Lu int budget = OCELOT_PTP_QUEUE_SZ; 934e23a7b3eSYangbo Lu 935e23a7b3eSYangbo Lu while (budget--) { 936b049da13SYangbo Lu struct sk_buff *skb, *skb_tmp, *skb_match = NULL; 937e23a7b3eSYangbo Lu struct skb_shared_hwtstamps shhwtstamps; 938ebb4c6a9SVladimir Oltean u32 val, id, seqid, txport; 939e23a7b3eSYangbo Lu struct ocelot_port *port; 940e23a7b3eSYangbo Lu struct timespec64 ts; 941b049da13SYangbo Lu unsigned long flags; 942e23a7b3eSYangbo Lu 943e23a7b3eSYangbo Lu val = ocelot_read(ocelot, SYS_PTP_STATUS); 944e23a7b3eSYangbo Lu 945e23a7b3eSYangbo Lu /* Check if a timestamp can be retrieved */ 946e23a7b3eSYangbo Lu if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) 947e23a7b3eSYangbo Lu break; 948e23a7b3eSYangbo Lu 949e23a7b3eSYangbo Lu WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); 950e23a7b3eSYangbo Lu 951e23a7b3eSYangbo Lu /* Retrieve the ts ID and Tx port */ 952e23a7b3eSYangbo Lu id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); 953e23a7b3eSYangbo Lu txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); 954ebb4c6a9SVladimir Oltean seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val); 955e23a7b3eSYangbo Lu 956e23a7b3eSYangbo Lu port = ocelot->ports[txport]; 957e23a7b3eSYangbo Lu 95852849bcfSVladimir Oltean spin_lock(&ocelot->ts_id_lock); 95952849bcfSVladimir Oltean port->ptp_skbs_in_flight--; 96052849bcfSVladimir Oltean ocelot->ptp_skbs_in_flight--; 96152849bcfSVladimir Oltean spin_unlock(&ocelot->ts_id_lock); 96252849bcfSVladimir Oltean 96352849bcfSVladimir Oltean /* Retrieve its associated skb */ 964ebb4c6a9SVladimir Oltean try_again: 965b049da13SYangbo Lu spin_lock_irqsave(&port->tx_skbs.lock, flags); 966b049da13SYangbo Lu 967b049da13SYangbo Lu skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { 968c4b364ceSYangbo Lu if (OCELOT_SKB_CB(skb)->ts_id != id) 969e23a7b3eSYangbo Lu continue; 970b049da13SYangbo Lu __skb_unlink(skb, &port->tx_skbs); 971b049da13SYangbo Lu skb_match = skb; 972fc62c094SYangbo Lu break; 973e23a7b3eSYangbo Lu } 974e23a7b3eSYangbo Lu 975b049da13SYangbo Lu spin_unlock_irqrestore(&port->tx_skbs.lock, flags); 976b049da13SYangbo Lu 9779fde506eSVladimir Oltean if (WARN_ON(!skb_match)) 9789fde506eSVladimir Oltean continue; 9799fde506eSVladimir Oltean 980ebb4c6a9SVladimir Oltean if (!ocelot_validate_ptp_skb(skb_match, seqid)) { 981ebb4c6a9SVladimir Oltean dev_err_ratelimited(ocelot->dev, 982ebb4c6a9SVladimir Oltean "port %d received stale TX timestamp for seqid %d, discarding\n", 983ebb4c6a9SVladimir Oltean txport, seqid); 984ebb4c6a9SVladimir Oltean dev_kfree_skb_any(skb); 985ebb4c6a9SVladimir Oltean goto try_again; 986ebb4c6a9SVladimir Oltean } 987ebb4c6a9SVladimir Oltean 9885fd82200Slaurent brando /* Get the h/w timestamp */ 9895fd82200Slaurent brando ocelot_get_hwtimestamp(ocelot, &ts); 990e23a7b3eSYangbo Lu 991e23a7b3eSYangbo Lu /* Set the timestamp into the skb */ 992e23a7b3eSYangbo Lu memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 993e23a7b3eSYangbo Lu shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 994e2f9a8feSVladimir Oltean skb_complete_tx_timestamp(skb_match, &shhwtstamps); 9955fd82200Slaurent brando 9965fd82200Slaurent brando /* Next ts */ 9975fd82200Slaurent brando ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); 998e23a7b3eSYangbo Lu } 999e23a7b3eSYangbo Lu } 1000e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp); 10014e3b0468SAntoine Tenart 1002924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh, 1003924ee317SVladimir Oltean u32 *rval) 1004924ee317SVladimir Oltean { 1005924ee317SVladimir Oltean u32 bytes_valid, val; 1006924ee317SVladimir Oltean 1007924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1008924ee317SVladimir Oltean if (val == XTR_NOT_READY) { 1009924ee317SVladimir Oltean if (ifh) 1010924ee317SVladimir Oltean return -EIO; 1011924ee317SVladimir Oltean 1012924ee317SVladimir Oltean do { 1013924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1014924ee317SVladimir Oltean } while (val == XTR_NOT_READY); 1015924ee317SVladimir Oltean } 1016924ee317SVladimir Oltean 1017924ee317SVladimir Oltean switch (val) { 1018924ee317SVladimir Oltean case XTR_ABORT: 1019924ee317SVladimir Oltean return -EIO; 1020924ee317SVladimir Oltean case XTR_EOF_0: 1021924ee317SVladimir Oltean case XTR_EOF_1: 1022924ee317SVladimir Oltean case XTR_EOF_2: 1023924ee317SVladimir Oltean case XTR_EOF_3: 1024924ee317SVladimir Oltean case XTR_PRUNED: 1025924ee317SVladimir Oltean bytes_valid = XTR_VALID_BYTES(val); 1026924ee317SVladimir Oltean val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1027924ee317SVladimir Oltean if (val == XTR_ESCAPE) 1028924ee317SVladimir Oltean *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1029924ee317SVladimir Oltean else 1030924ee317SVladimir Oltean *rval = val; 1031924ee317SVladimir Oltean 1032924ee317SVladimir Oltean return bytes_valid; 1033924ee317SVladimir Oltean case XTR_ESCAPE: 1034924ee317SVladimir Oltean *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 1035924ee317SVladimir Oltean 1036924ee317SVladimir Oltean return 4; 1037924ee317SVladimir Oltean default: 1038924ee317SVladimir Oltean *rval = val; 1039924ee317SVladimir Oltean 1040924ee317SVladimir Oltean return 4; 1041924ee317SVladimir Oltean } 1042924ee317SVladimir Oltean } 1043924ee317SVladimir Oltean 1044924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh) 1045924ee317SVladimir Oltean { 1046924ee317SVladimir Oltean int i, err = 0; 1047924ee317SVladimir Oltean 1048924ee317SVladimir Oltean for (i = 0; i < OCELOT_TAG_LEN / 4; i++) { 1049924ee317SVladimir Oltean err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]); 1050924ee317SVladimir Oltean if (err != 4) 1051924ee317SVladimir Oltean return (err < 0) ? err : -EIO; 1052924ee317SVladimir Oltean } 1053924ee317SVladimir Oltean 1054924ee317SVladimir Oltean return 0; 1055924ee317SVladimir Oltean } 1056924ee317SVladimir Oltean 1057924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb) 1058924ee317SVladimir Oltean { 1059924ee317SVladimir Oltean struct skb_shared_hwtstamps *shhwtstamps; 10602ed2c5f0SHoratiu Vultur u64 tod_in_ns, full_ts_in_ns; 1061924ee317SVladimir Oltean u64 timestamp, src_port, len; 1062924ee317SVladimir Oltean u32 xfh[OCELOT_TAG_LEN / 4]; 1063924ee317SVladimir Oltean struct net_device *dev; 1064924ee317SVladimir Oltean struct timespec64 ts; 1065924ee317SVladimir Oltean struct sk_buff *skb; 1066924ee317SVladimir Oltean int sz, buf_len; 1067924ee317SVladimir Oltean u32 val, *buf; 1068924ee317SVladimir Oltean int err; 1069924ee317SVladimir Oltean 1070924ee317SVladimir Oltean err = ocelot_xtr_poll_xfh(ocelot, grp, xfh); 1071924ee317SVladimir Oltean if (err) 1072924ee317SVladimir Oltean return err; 1073924ee317SVladimir Oltean 1074924ee317SVladimir Oltean ocelot_xfh_get_src_port(xfh, &src_port); 1075924ee317SVladimir Oltean ocelot_xfh_get_len(xfh, &len); 1076924ee317SVladimir Oltean ocelot_xfh_get_rew_val(xfh, ×tamp); 1077924ee317SVladimir Oltean 1078924ee317SVladimir Oltean if (WARN_ON(src_port >= ocelot->num_phys_ports)) 1079924ee317SVladimir Oltean return -EINVAL; 1080924ee317SVladimir Oltean 1081924ee317SVladimir Oltean dev = ocelot->ops->port_to_netdev(ocelot, src_port); 1082924ee317SVladimir Oltean if (!dev) 1083924ee317SVladimir Oltean return -EINVAL; 1084924ee317SVladimir Oltean 1085924ee317SVladimir Oltean skb = netdev_alloc_skb(dev, len); 1086924ee317SVladimir Oltean if (unlikely(!skb)) { 1087924ee317SVladimir Oltean netdev_err(dev, "Unable to allocate sk_buff\n"); 1088924ee317SVladimir Oltean return -ENOMEM; 1089924ee317SVladimir Oltean } 1090924ee317SVladimir Oltean 1091924ee317SVladimir Oltean buf_len = len - ETH_FCS_LEN; 1092924ee317SVladimir Oltean buf = (u32 *)skb_put(skb, buf_len); 1093924ee317SVladimir Oltean 1094924ee317SVladimir Oltean len = 0; 1095924ee317SVladimir Oltean do { 1096924ee317SVladimir Oltean sz = ocelot_rx_frame_word(ocelot, grp, false, &val); 1097924ee317SVladimir Oltean if (sz < 0) { 1098924ee317SVladimir Oltean err = sz; 1099924ee317SVladimir Oltean goto out_free_skb; 1100924ee317SVladimir Oltean } 1101924ee317SVladimir Oltean *buf++ = val; 1102924ee317SVladimir Oltean len += sz; 1103924ee317SVladimir Oltean } while (len < buf_len); 1104924ee317SVladimir Oltean 1105924ee317SVladimir Oltean /* Read the FCS */ 1106924ee317SVladimir Oltean sz = ocelot_rx_frame_word(ocelot, grp, false, &val); 1107924ee317SVladimir Oltean if (sz < 0) { 1108924ee317SVladimir Oltean err = sz; 1109924ee317SVladimir Oltean goto out_free_skb; 1110924ee317SVladimir Oltean } 1111924ee317SVladimir Oltean 1112924ee317SVladimir Oltean /* Update the statistics if part of the FCS was read before */ 1113924ee317SVladimir Oltean len -= ETH_FCS_LEN - sz; 1114924ee317SVladimir Oltean 1115924ee317SVladimir Oltean if (unlikely(dev->features & NETIF_F_RXFCS)) { 1116924ee317SVladimir Oltean buf = (u32 *)skb_put(skb, ETH_FCS_LEN); 1117924ee317SVladimir Oltean *buf = val; 1118924ee317SVladimir Oltean } 1119924ee317SVladimir Oltean 1120924ee317SVladimir Oltean if (ocelot->ptp) { 1121924ee317SVladimir Oltean ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1122924ee317SVladimir Oltean 1123924ee317SVladimir Oltean tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec); 1124924ee317SVladimir Oltean if ((tod_in_ns & 0xffffffff) < timestamp) 1125924ee317SVladimir Oltean full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) | 1126924ee317SVladimir Oltean timestamp; 1127924ee317SVladimir Oltean else 1128924ee317SVladimir Oltean full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) | 1129924ee317SVladimir Oltean timestamp; 1130924ee317SVladimir Oltean 1131924ee317SVladimir Oltean shhwtstamps = skb_hwtstamps(skb); 1132924ee317SVladimir Oltean memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1133924ee317SVladimir Oltean shhwtstamps->hwtstamp = full_ts_in_ns; 1134924ee317SVladimir Oltean } 1135924ee317SVladimir Oltean 1136924ee317SVladimir Oltean /* Everything we see on an interface that is in the HW bridge 1137924ee317SVladimir Oltean * has already been forwarded. 1138924ee317SVladimir Oltean */ 1139df291e54SVladimir Oltean if (ocelot->ports[src_port]->bridge) 1140924ee317SVladimir Oltean skb->offload_fwd_mark = 1; 1141924ee317SVladimir Oltean 1142924ee317SVladimir Oltean skb->protocol = eth_type_trans(skb, dev); 1143d8ea7ff3SHoratiu Vultur 1144924ee317SVladimir Oltean *nskb = skb; 1145924ee317SVladimir Oltean 1146924ee317SVladimir Oltean return 0; 1147924ee317SVladimir Oltean 1148924ee317SVladimir Oltean out_free_skb: 1149924ee317SVladimir Oltean kfree_skb(skb); 1150924ee317SVladimir Oltean return err; 1151924ee317SVladimir Oltean } 1152924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame); 1153924ee317SVladimir Oltean 1154137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp) 1155137ffbc4SVladimir Oltean { 1156137ffbc4SVladimir Oltean u32 val = ocelot_read(ocelot, QS_INJ_STATUS); 1157137ffbc4SVladimir Oltean 1158137ffbc4SVladimir Oltean if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp)))) 1159137ffbc4SVladimir Oltean return false; 1160137ffbc4SVladimir Oltean if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))) 1161137ffbc4SVladimir Oltean return false; 1162137ffbc4SVladimir Oltean 1163137ffbc4SVladimir Oltean return true; 1164137ffbc4SVladimir Oltean } 1165137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject); 1166137ffbc4SVladimir Oltean 1167137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp, 1168137ffbc4SVladimir Oltean u32 rew_op, struct sk_buff *skb) 1169137ffbc4SVladimir Oltean { 117040d3f295SVladimir Oltean u32 ifh[OCELOT_TAG_LEN / 4] = {0}; 1171137ffbc4SVladimir Oltean unsigned int i, count, last; 1172137ffbc4SVladimir Oltean 1173137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 1174137ffbc4SVladimir Oltean QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp); 1175137ffbc4SVladimir Oltean 117640d3f295SVladimir Oltean ocelot_ifh_set_bypass(ifh, 1); 11771f778d50SVladimir Oltean ocelot_ifh_set_dest(ifh, BIT_ULL(port)); 117840d3f295SVladimir Oltean ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C); 1179e8c07229SVladimir Oltean ocelot_ifh_set_vlan_tci(ifh, skb_vlan_tag_get(skb)); 118040d3f295SVladimir Oltean ocelot_ifh_set_rew_op(ifh, rew_op); 1181137ffbc4SVladimir Oltean 1182137ffbc4SVladimir Oltean for (i = 0; i < OCELOT_TAG_LEN / 4; i++) 118340d3f295SVladimir Oltean ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp); 1184137ffbc4SVladimir Oltean 1185137ffbc4SVladimir Oltean count = DIV_ROUND_UP(skb->len, 4); 1186137ffbc4SVladimir Oltean last = skb->len % 4; 1187137ffbc4SVladimir Oltean for (i = 0; i < count; i++) 1188137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp); 1189137ffbc4SVladimir Oltean 1190137ffbc4SVladimir Oltean /* Add padding */ 1191137ffbc4SVladimir Oltean while (i < (OCELOT_BUFFER_CELL_SZ / 4)) { 1192137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 1193137ffbc4SVladimir Oltean i++; 1194137ffbc4SVladimir Oltean } 1195137ffbc4SVladimir Oltean 1196137ffbc4SVladimir Oltean /* Indicate EOF and valid bytes in last word */ 1197137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 1198137ffbc4SVladimir Oltean QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) | 1199137ffbc4SVladimir Oltean QS_INJ_CTRL_EOF, 1200137ffbc4SVladimir Oltean QS_INJ_CTRL, grp); 1201137ffbc4SVladimir Oltean 1202137ffbc4SVladimir Oltean /* Add dummy CRC */ 1203137ffbc4SVladimir Oltean ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 1204137ffbc4SVladimir Oltean skb_tx_timestamp(skb); 1205137ffbc4SVladimir Oltean 1206137ffbc4SVladimir Oltean skb->dev->stats.tx_packets++; 1207137ffbc4SVladimir Oltean skb->dev->stats.tx_bytes += skb->len; 1208137ffbc4SVladimir Oltean } 1209137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame); 1210137ffbc4SVladimir Oltean 12110a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp) 12120a6f17c6SVladimir Oltean { 12130a6f17c6SVladimir Oltean while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) 12140a6f17c6SVladimir Oltean ocelot_read_rix(ocelot, QS_XTR_RD, grp); 12150a6f17c6SVladimir Oltean } 12160a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue); 12170a6f17c6SVladimir Oltean 12185e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, 121987b0f983SVladimir Oltean const unsigned char *addr, u16 vid) 1220a556c76aSAlexandre Belloni { 1221471beb11SVladimir Oltean int pgid = port; 1222471beb11SVladimir Oltean 1223471beb11SVladimir Oltean if (port == ocelot->npi) 1224471beb11SVladimir Oltean pgid = PGID_CPU; 1225a556c76aSAlexandre Belloni 1226471beb11SVladimir Oltean return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); 1227a556c76aSAlexandre Belloni } 12285e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add); 1229a556c76aSAlexandre Belloni 12305e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, 1231531ee1a6SVladimir Oltean const unsigned char *addr, u16 vid) 1232531ee1a6SVladimir Oltean { 1233531ee1a6SVladimir Oltean return ocelot_mact_forget(ocelot, addr, vid); 1234531ee1a6SVladimir Oltean } 12355e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del); 1236531ee1a6SVladimir Oltean 12379c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, 1238531ee1a6SVladimir Oltean bool is_static, void *data) 1239a556c76aSAlexandre Belloni { 1240531ee1a6SVladimir Oltean struct ocelot_dump_ctx *dump = data; 1241a556c76aSAlexandre Belloni u32 portid = NETLINK_CB(dump->cb->skb).portid; 1242a556c76aSAlexandre Belloni u32 seq = dump->cb->nlh->nlmsg_seq; 1243a556c76aSAlexandre Belloni struct nlmsghdr *nlh; 1244a556c76aSAlexandre Belloni struct ndmsg *ndm; 1245a556c76aSAlexandre Belloni 1246a556c76aSAlexandre Belloni if (dump->idx < dump->cb->args[2]) 1247a556c76aSAlexandre Belloni goto skip; 1248a556c76aSAlexandre Belloni 1249a556c76aSAlexandre Belloni nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 1250a556c76aSAlexandre Belloni sizeof(*ndm), NLM_F_MULTI); 1251a556c76aSAlexandre Belloni if (!nlh) 1252a556c76aSAlexandre Belloni return -EMSGSIZE; 1253a556c76aSAlexandre Belloni 1254a556c76aSAlexandre Belloni ndm = nlmsg_data(nlh); 1255a556c76aSAlexandre Belloni ndm->ndm_family = AF_BRIDGE; 1256a556c76aSAlexandre Belloni ndm->ndm_pad1 = 0; 1257a556c76aSAlexandre Belloni ndm->ndm_pad2 = 0; 1258a556c76aSAlexandre Belloni ndm->ndm_flags = NTF_SELF; 1259a556c76aSAlexandre Belloni ndm->ndm_type = 0; 1260a556c76aSAlexandre Belloni ndm->ndm_ifindex = dump->dev->ifindex; 1261531ee1a6SVladimir Oltean ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 1262a556c76aSAlexandre Belloni 1263531ee1a6SVladimir Oltean if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 1264a556c76aSAlexandre Belloni goto nla_put_failure; 1265a556c76aSAlexandre Belloni 1266531ee1a6SVladimir Oltean if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 1267a556c76aSAlexandre Belloni goto nla_put_failure; 1268a556c76aSAlexandre Belloni 1269a556c76aSAlexandre Belloni nlmsg_end(dump->skb, nlh); 1270a556c76aSAlexandre Belloni 1271a556c76aSAlexandre Belloni skip: 1272a556c76aSAlexandre Belloni dump->idx++; 1273a556c76aSAlexandre Belloni return 0; 1274a556c76aSAlexandre Belloni 1275a556c76aSAlexandre Belloni nla_put_failure: 1276a556c76aSAlexandre Belloni nlmsg_cancel(dump->skb, nlh); 1277a556c76aSAlexandre Belloni return -EMSGSIZE; 1278a556c76aSAlexandre Belloni } 12799c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump); 1280a556c76aSAlexandre Belloni 12812468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */ 1282531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, 1283a556c76aSAlexandre Belloni struct ocelot_mact_entry *entry) 1284a556c76aSAlexandre Belloni { 1285a556c76aSAlexandre Belloni u32 val, dst, macl, mach; 1286531ee1a6SVladimir Oltean char mac[ETH_ALEN]; 1287a556c76aSAlexandre Belloni 1288a556c76aSAlexandre Belloni /* Set row and column to read from */ 1289a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); 1290a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); 1291a556c76aSAlexandre Belloni 1292a556c76aSAlexandre Belloni /* Issue a read command */ 1293a556c76aSAlexandre Belloni ocelot_write(ocelot, 1294a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 1295a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 1296a556c76aSAlexandre Belloni 1297a556c76aSAlexandre Belloni if (ocelot_mact_wait_for_completion(ocelot)) 1298a556c76aSAlexandre Belloni return -ETIMEDOUT; 1299a556c76aSAlexandre Belloni 1300a556c76aSAlexandre Belloni /* Read the entry flags */ 1301a556c76aSAlexandre Belloni val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 1302a556c76aSAlexandre Belloni if (!(val & ANA_TABLES_MACACCESS_VALID)) 1303a556c76aSAlexandre Belloni return -EINVAL; 1304a556c76aSAlexandre Belloni 1305a556c76aSAlexandre Belloni /* If the entry read has another port configured as its destination, 1306a556c76aSAlexandre Belloni * do not report it. 1307a556c76aSAlexandre Belloni */ 1308a556c76aSAlexandre Belloni dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; 1309531ee1a6SVladimir Oltean if (dst != port) 1310a556c76aSAlexandre Belloni return -EINVAL; 1311a556c76aSAlexandre Belloni 1312a556c76aSAlexandre Belloni /* Get the entry's MAC address and VLAN id */ 1313a556c76aSAlexandre Belloni macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); 1314a556c76aSAlexandre Belloni mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); 1315a556c76aSAlexandre Belloni 1316a556c76aSAlexandre Belloni mac[0] = (mach >> 8) & 0xff; 1317a556c76aSAlexandre Belloni mac[1] = (mach >> 0) & 0xff; 1318a556c76aSAlexandre Belloni mac[2] = (macl >> 24) & 0xff; 1319a556c76aSAlexandre Belloni mac[3] = (macl >> 16) & 0xff; 1320a556c76aSAlexandre Belloni mac[4] = (macl >> 8) & 0xff; 1321a556c76aSAlexandre Belloni mac[5] = (macl >> 0) & 0xff; 1322a556c76aSAlexandre Belloni 1323a556c76aSAlexandre Belloni entry->vid = (mach >> 16) & 0xfff; 1324a556c76aSAlexandre Belloni ether_addr_copy(entry->mac, mac); 1325a556c76aSAlexandre Belloni 1326a556c76aSAlexandre Belloni return 0; 1327a556c76aSAlexandre Belloni } 1328a556c76aSAlexandre Belloni 13295e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port, 1330531ee1a6SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1331a556c76aSAlexandre Belloni { 13322468346cSVladimir Oltean int err = 0; 1333531ee1a6SVladimir Oltean int i, j; 1334a556c76aSAlexandre Belloni 13352468346cSVladimir Oltean /* We could take the lock just around ocelot_mact_read, but doing so 13362468346cSVladimir Oltean * thousands of times in a row seems rather pointless and inefficient. 13372468346cSVladimir Oltean */ 13382468346cSVladimir Oltean mutex_lock(&ocelot->mact_lock); 13392468346cSVladimir Oltean 134021ce7f3eSVladimir Oltean /* Loop through all the mac tables entries. */ 134121ce7f3eSVladimir Oltean for (i = 0; i < ocelot->num_mact_rows; i++) { 1342a556c76aSAlexandre Belloni for (j = 0; j < 4; j++) { 1343531ee1a6SVladimir Oltean struct ocelot_mact_entry entry; 1344531ee1a6SVladimir Oltean bool is_static; 1345531ee1a6SVladimir Oltean 13462468346cSVladimir Oltean err = ocelot_mact_read(ocelot, port, i, j, &entry); 1347a556c76aSAlexandre Belloni /* If the entry is invalid (wrong port, invalid...), 1348a556c76aSAlexandre Belloni * skip it. 1349a556c76aSAlexandre Belloni */ 13502468346cSVladimir Oltean if (err == -EINVAL) 1351a556c76aSAlexandre Belloni continue; 13522468346cSVladimir Oltean else if (err) 13532468346cSVladimir Oltean break; 1354a556c76aSAlexandre Belloni 1355531ee1a6SVladimir Oltean is_static = (entry.type == ENTRYTYPE_LOCKED); 1356531ee1a6SVladimir Oltean 13572468346cSVladimir Oltean err = cb(entry.mac, entry.vid, is_static, data); 13582468346cSVladimir Oltean if (err) 13592468346cSVladimir Oltean break; 1360a556c76aSAlexandre Belloni } 1361a556c76aSAlexandre Belloni } 1362a556c76aSAlexandre Belloni 13632468346cSVladimir Oltean mutex_unlock(&ocelot->mact_lock); 13642468346cSVladimir Oltean 13652468346cSVladimir Oltean return err; 1366531ee1a6SVladimir Oltean } 13675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump); 1368531ee1a6SVladimir Oltean 136996ca08c0SVladimir Oltean static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap) 137096ca08c0SVladimir Oltean { 137196ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_ETYPE; 137296ca08c0SVladimir Oltean *(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588); 137396ca08c0SVladimir Oltean *(__be16 *)trap->key.etype.etype.mask = htons(0xffff); 137496ca08c0SVladimir Oltean } 137596ca08c0SVladimir Oltean 137696ca08c0SVladimir Oltean static void 137796ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap) 137896ca08c0SVladimir Oltean { 137996ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_IPV4; 138096ca08c0SVladimir Oltean trap->key.ipv4.dport.value = PTP_EV_PORT; 138196ca08c0SVladimir Oltean trap->key.ipv4.dport.mask = 0xffff; 138296ca08c0SVladimir Oltean } 138396ca08c0SVladimir Oltean 138496ca08c0SVladimir Oltean static void 138596ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap) 138696ca08c0SVladimir Oltean { 138796ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_IPV6; 138896ca08c0SVladimir Oltean trap->key.ipv6.dport.value = PTP_EV_PORT; 138996ca08c0SVladimir Oltean trap->key.ipv6.dport.mask = 0xffff; 139096ca08c0SVladimir Oltean } 139196ca08c0SVladimir Oltean 139296ca08c0SVladimir Oltean static void 139396ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap) 139496ca08c0SVladimir Oltean { 139596ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_IPV4; 139696ca08c0SVladimir Oltean trap->key.ipv4.dport.value = PTP_GEN_PORT; 139796ca08c0SVladimir Oltean trap->key.ipv4.dport.mask = 0xffff; 139896ca08c0SVladimir Oltean } 139996ca08c0SVladimir Oltean 140096ca08c0SVladimir Oltean static void 140196ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap) 140296ca08c0SVladimir Oltean { 140396ca08c0SVladimir Oltean trap->key_type = OCELOT_VCAP_KEY_IPV6; 140496ca08c0SVladimir Oltean trap->key.ipv6.dport.value = PTP_GEN_PORT; 140596ca08c0SVladimir Oltean trap->key.ipv6.dport.mask = 0xffff; 140696ca08c0SVladimir Oltean } 140796ca08c0SVladimir Oltean 140896ca08c0SVladimir Oltean static int ocelot_trap_add(struct ocelot *ocelot, int port, 140996ca08c0SVladimir Oltean unsigned long cookie, 141096ca08c0SVladimir Oltean void (*populate)(struct ocelot_vcap_filter *f)) 141196ca08c0SVladimir Oltean { 141296ca08c0SVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 141396ca08c0SVladimir Oltean struct ocelot_vcap_filter *trap; 141496ca08c0SVladimir Oltean bool new = false; 141596ca08c0SVladimir Oltean int err; 141696ca08c0SVladimir Oltean 141796ca08c0SVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 141896ca08c0SVladimir Oltean 141996ca08c0SVladimir Oltean trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie, 142096ca08c0SVladimir Oltean false); 142196ca08c0SVladimir Oltean if (!trap) { 142296ca08c0SVladimir Oltean trap = kzalloc(sizeof(*trap), GFP_KERNEL); 142396ca08c0SVladimir Oltean if (!trap) 142496ca08c0SVladimir Oltean return -ENOMEM; 142596ca08c0SVladimir Oltean 142696ca08c0SVladimir Oltean populate(trap); 142796ca08c0SVladimir Oltean trap->prio = 1; 142896ca08c0SVladimir Oltean trap->id.cookie = cookie; 142996ca08c0SVladimir Oltean trap->id.tc_offload = false; 143096ca08c0SVladimir Oltean trap->block_id = VCAP_IS2; 143196ca08c0SVladimir Oltean trap->type = OCELOT_VCAP_FILTER_OFFLOAD; 143296ca08c0SVladimir Oltean trap->lookup = 0; 143396ca08c0SVladimir Oltean trap->action.cpu_copy_ena = true; 143496ca08c0SVladimir Oltean trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 143596ca08c0SVladimir Oltean trap->action.port_mask = 0; 143696ca08c0SVladimir Oltean new = true; 143796ca08c0SVladimir Oltean } 143896ca08c0SVladimir Oltean 143996ca08c0SVladimir Oltean trap->ingress_port_mask |= BIT(port); 144096ca08c0SVladimir Oltean 144196ca08c0SVladimir Oltean if (new) 144296ca08c0SVladimir Oltean err = ocelot_vcap_filter_add(ocelot, trap, NULL); 144396ca08c0SVladimir Oltean else 144496ca08c0SVladimir Oltean err = ocelot_vcap_filter_replace(ocelot, trap); 144596ca08c0SVladimir Oltean if (err) { 144696ca08c0SVladimir Oltean trap->ingress_port_mask &= ~BIT(port); 144796ca08c0SVladimir Oltean if (!trap->ingress_port_mask) 144896ca08c0SVladimir Oltean kfree(trap); 144996ca08c0SVladimir Oltean return err; 145096ca08c0SVladimir Oltean } 145196ca08c0SVladimir Oltean 145296ca08c0SVladimir Oltean return 0; 145396ca08c0SVladimir Oltean } 145496ca08c0SVladimir Oltean 145596ca08c0SVladimir Oltean static int ocelot_trap_del(struct ocelot *ocelot, int port, 145696ca08c0SVladimir Oltean unsigned long cookie) 145796ca08c0SVladimir Oltean { 145896ca08c0SVladimir Oltean struct ocelot_vcap_block *block_vcap_is2; 145996ca08c0SVladimir Oltean struct ocelot_vcap_filter *trap; 146096ca08c0SVladimir Oltean 146196ca08c0SVladimir Oltean block_vcap_is2 = &ocelot->block[VCAP_IS2]; 146296ca08c0SVladimir Oltean 146396ca08c0SVladimir Oltean trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie, 146496ca08c0SVladimir Oltean false); 146596ca08c0SVladimir Oltean if (!trap) 146696ca08c0SVladimir Oltean return 0; 146796ca08c0SVladimir Oltean 146896ca08c0SVladimir Oltean trap->ingress_port_mask &= ~BIT(port); 146996ca08c0SVladimir Oltean if (!trap->ingress_port_mask) 147096ca08c0SVladimir Oltean return ocelot_vcap_filter_del(ocelot, trap); 147196ca08c0SVladimir Oltean 147296ca08c0SVladimir Oltean return ocelot_vcap_filter_replace(ocelot, trap); 147396ca08c0SVladimir Oltean } 147496ca08c0SVladimir Oltean 147596ca08c0SVladimir Oltean static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port) 147696ca08c0SVladimir Oltean { 147796ca08c0SVladimir Oltean unsigned long l2_cookie = ocelot->num_phys_ports + 1; 147896ca08c0SVladimir Oltean 147996ca08c0SVladimir Oltean return ocelot_trap_add(ocelot, port, l2_cookie, 148096ca08c0SVladimir Oltean ocelot_populate_l2_ptp_trap_key); 148196ca08c0SVladimir Oltean } 148296ca08c0SVladimir Oltean 148396ca08c0SVladimir Oltean static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port) 148496ca08c0SVladimir Oltean { 148596ca08c0SVladimir Oltean unsigned long l2_cookie = ocelot->num_phys_ports + 1; 148696ca08c0SVladimir Oltean 148796ca08c0SVladimir Oltean return ocelot_trap_del(ocelot, port, l2_cookie); 148896ca08c0SVladimir Oltean } 148996ca08c0SVladimir Oltean 149096ca08c0SVladimir Oltean static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port) 149196ca08c0SVladimir Oltean { 149296ca08c0SVladimir Oltean unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2; 149396ca08c0SVladimir Oltean unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3; 149496ca08c0SVladimir Oltean int err; 149596ca08c0SVladimir Oltean 149696ca08c0SVladimir Oltean err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, 149796ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_event_trap_key); 149896ca08c0SVladimir Oltean if (err) 149996ca08c0SVladimir Oltean return err; 150096ca08c0SVladimir Oltean 150196ca08c0SVladimir Oltean err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, 150296ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_general_trap_key); 150396ca08c0SVladimir Oltean if (err) 150496ca08c0SVladimir Oltean ocelot_trap_del(ocelot, port, ipv4_ev_cookie); 150596ca08c0SVladimir Oltean 150696ca08c0SVladimir Oltean return err; 150796ca08c0SVladimir Oltean } 150896ca08c0SVladimir Oltean 150996ca08c0SVladimir Oltean static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port) 151096ca08c0SVladimir Oltean { 151196ca08c0SVladimir Oltean unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2; 151296ca08c0SVladimir Oltean unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3; 151396ca08c0SVladimir Oltean int err; 151496ca08c0SVladimir Oltean 151596ca08c0SVladimir Oltean err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie); 151696ca08c0SVladimir Oltean err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie); 151796ca08c0SVladimir Oltean return err; 151896ca08c0SVladimir Oltean } 151996ca08c0SVladimir Oltean 152096ca08c0SVladimir Oltean static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port) 152196ca08c0SVladimir Oltean { 152296ca08c0SVladimir Oltean unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4; 152396ca08c0SVladimir Oltean unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5; 152496ca08c0SVladimir Oltean int err; 152596ca08c0SVladimir Oltean 152696ca08c0SVladimir Oltean err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, 152796ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_event_trap_key); 152896ca08c0SVladimir Oltean if (err) 152996ca08c0SVladimir Oltean return err; 153096ca08c0SVladimir Oltean 153196ca08c0SVladimir Oltean err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, 153296ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_general_trap_key); 153396ca08c0SVladimir Oltean if (err) 153496ca08c0SVladimir Oltean ocelot_trap_del(ocelot, port, ipv6_ev_cookie); 153596ca08c0SVladimir Oltean 153696ca08c0SVladimir Oltean return err; 153796ca08c0SVladimir Oltean } 153896ca08c0SVladimir Oltean 153996ca08c0SVladimir Oltean static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port) 154096ca08c0SVladimir Oltean { 154196ca08c0SVladimir Oltean unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4; 154296ca08c0SVladimir Oltean unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5; 154396ca08c0SVladimir Oltean int err; 154496ca08c0SVladimir Oltean 154596ca08c0SVladimir Oltean err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie); 154696ca08c0SVladimir Oltean err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie); 154796ca08c0SVladimir Oltean return err; 154896ca08c0SVladimir Oltean } 154996ca08c0SVladimir Oltean 155096ca08c0SVladimir Oltean static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port, 155196ca08c0SVladimir Oltean bool l2, bool l4) 155296ca08c0SVladimir Oltean { 155396ca08c0SVladimir Oltean int err; 155496ca08c0SVladimir Oltean 155596ca08c0SVladimir Oltean if (l2) 155696ca08c0SVladimir Oltean err = ocelot_l2_ptp_trap_add(ocelot, port); 155796ca08c0SVladimir Oltean else 155896ca08c0SVladimir Oltean err = ocelot_l2_ptp_trap_del(ocelot, port); 155996ca08c0SVladimir Oltean if (err) 156096ca08c0SVladimir Oltean return err; 156196ca08c0SVladimir Oltean 156296ca08c0SVladimir Oltean if (l4) { 156396ca08c0SVladimir Oltean err = ocelot_ipv4_ptp_trap_add(ocelot, port); 156496ca08c0SVladimir Oltean if (err) 156596ca08c0SVladimir Oltean goto err_ipv4; 156696ca08c0SVladimir Oltean 156796ca08c0SVladimir Oltean err = ocelot_ipv6_ptp_trap_add(ocelot, port); 156896ca08c0SVladimir Oltean if (err) 156996ca08c0SVladimir Oltean goto err_ipv6; 157096ca08c0SVladimir Oltean } else { 157196ca08c0SVladimir Oltean err = ocelot_ipv4_ptp_trap_del(ocelot, port); 157296ca08c0SVladimir Oltean 157396ca08c0SVladimir Oltean err |= ocelot_ipv6_ptp_trap_del(ocelot, port); 157496ca08c0SVladimir Oltean } 157596ca08c0SVladimir Oltean if (err) 157696ca08c0SVladimir Oltean return err; 157796ca08c0SVladimir Oltean 157896ca08c0SVladimir Oltean return 0; 157996ca08c0SVladimir Oltean 158096ca08c0SVladimir Oltean err_ipv6: 158196ca08c0SVladimir Oltean ocelot_ipv4_ptp_trap_del(ocelot, port); 158296ca08c0SVladimir Oltean err_ipv4: 158396ca08c0SVladimir Oltean if (l2) 158496ca08c0SVladimir Oltean ocelot_l2_ptp_trap_del(ocelot, port); 158596ca08c0SVladimir Oltean return err; 158696ca08c0SVladimir Oltean } 158796ca08c0SVladimir Oltean 1588f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) 15894e3b0468SAntoine Tenart { 15904e3b0468SAntoine Tenart return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, 15914e3b0468SAntoine Tenart sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; 15924e3b0468SAntoine Tenart } 1593f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get); 15944e3b0468SAntoine Tenart 1595f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) 15964e3b0468SAntoine Tenart { 1597306fd44bSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 159896ca08c0SVladimir Oltean bool l2 = false, l4 = false; 15994e3b0468SAntoine Tenart struct hwtstamp_config cfg; 160096ca08c0SVladimir Oltean int err; 16014e3b0468SAntoine Tenart 16024e3b0468SAntoine Tenart if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 16034e3b0468SAntoine Tenart return -EFAULT; 16044e3b0468SAntoine Tenart 16054e3b0468SAntoine Tenart /* reserved for future extensions */ 16064e3b0468SAntoine Tenart if (cfg.flags) 16074e3b0468SAntoine Tenart return -EINVAL; 16084e3b0468SAntoine Tenart 16094e3b0468SAntoine Tenart /* Tx type sanity check */ 16104e3b0468SAntoine Tenart switch (cfg.tx_type) { 16114e3b0468SAntoine Tenart case HWTSTAMP_TX_ON: 1612306fd44bSVladimir Oltean ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 16134e3b0468SAntoine Tenart break; 16144e3b0468SAntoine Tenart case HWTSTAMP_TX_ONESTEP_SYNC: 16154e3b0468SAntoine Tenart /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we 16164e3b0468SAntoine Tenart * need to update the origin time. 16174e3b0468SAntoine Tenart */ 1618306fd44bSVladimir Oltean ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; 16194e3b0468SAntoine Tenart break; 16204e3b0468SAntoine Tenart case HWTSTAMP_TX_OFF: 1621306fd44bSVladimir Oltean ocelot_port->ptp_cmd = 0; 16224e3b0468SAntoine Tenart break; 16234e3b0468SAntoine Tenart default: 16244e3b0468SAntoine Tenart return -ERANGE; 16254e3b0468SAntoine Tenart } 16264e3b0468SAntoine Tenart 16274e3b0468SAntoine Tenart mutex_lock(&ocelot->ptp_lock); 16284e3b0468SAntoine Tenart 16294e3b0468SAntoine Tenart switch (cfg.rx_filter) { 16304e3b0468SAntoine Tenart case HWTSTAMP_FILTER_NONE: 16314e3b0468SAntoine Tenart break; 16324e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 16334e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 16344e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 163596ca08c0SVladimir Oltean l4 = true; 163696ca08c0SVladimir Oltean break; 16374e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 16384e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 16394e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 164096ca08c0SVladimir Oltean l2 = true; 164196ca08c0SVladimir Oltean break; 16424e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_EVENT: 16434e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_SYNC: 16444e3b0468SAntoine Tenart case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 164596ca08c0SVladimir Oltean l2 = true; 164696ca08c0SVladimir Oltean l4 = true; 16474e3b0468SAntoine Tenart break; 16484e3b0468SAntoine Tenart default: 16494e3b0468SAntoine Tenart mutex_unlock(&ocelot->ptp_lock); 16504e3b0468SAntoine Tenart return -ERANGE; 16514e3b0468SAntoine Tenart } 16524e3b0468SAntoine Tenart 165396ca08c0SVladimir Oltean err = ocelot_setup_ptp_traps(ocelot, port, l2, l4); 1654*9c32950fSLv Ruyi if (err) { 1655*9c32950fSLv Ruyi mutex_unlock(&ocelot->ptp_lock); 165696ca08c0SVladimir Oltean return err; 1657*9c32950fSLv Ruyi } 165896ca08c0SVladimir Oltean 165996ca08c0SVladimir Oltean if (l2 && l4) 166096ca08c0SVladimir Oltean cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 166196ca08c0SVladimir Oltean else if (l2) 166296ca08c0SVladimir Oltean cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; 166396ca08c0SVladimir Oltean else if (l4) 166496ca08c0SVladimir Oltean cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; 166596ca08c0SVladimir Oltean else 166696ca08c0SVladimir Oltean cfg.rx_filter = HWTSTAMP_FILTER_NONE; 166796ca08c0SVladimir Oltean 16684e3b0468SAntoine Tenart /* Commit back the result & save it */ 16694e3b0468SAntoine Tenart memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); 16704e3b0468SAntoine Tenart mutex_unlock(&ocelot->ptp_lock); 16714e3b0468SAntoine Tenart 16724e3b0468SAntoine Tenart return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 16734e3b0468SAntoine Tenart } 1674f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set); 16754e3b0468SAntoine Tenart 16765e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) 1677a556c76aSAlexandre Belloni { 1678a556c76aSAlexandre Belloni int i; 1679a556c76aSAlexandre Belloni 1680a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 1681a556c76aSAlexandre Belloni return; 1682a556c76aSAlexandre Belloni 1683a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 1684a556c76aSAlexandre Belloni memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, 1685a556c76aSAlexandre Belloni ETH_GSTRING_LEN); 1686a556c76aSAlexandre Belloni } 16875e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings); 1688a556c76aSAlexandre Belloni 16891e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot) 1690a556c76aSAlexandre Belloni { 1691a556c76aSAlexandre Belloni int i, j; 1692a556c76aSAlexandre Belloni 1693a556c76aSAlexandre Belloni mutex_lock(&ocelot->stats_lock); 1694a556c76aSAlexandre Belloni 1695a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_phys_ports; i++) { 1696a556c76aSAlexandre Belloni /* Configure the port to read the stats from */ 1697a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); 1698a556c76aSAlexandre Belloni 1699a556c76aSAlexandre Belloni for (j = 0; j < ocelot->num_stats; j++) { 1700a556c76aSAlexandre Belloni u32 val; 1701a556c76aSAlexandre Belloni unsigned int idx = i * ocelot->num_stats + j; 1702a556c76aSAlexandre Belloni 1703a556c76aSAlexandre Belloni val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, 1704a556c76aSAlexandre Belloni ocelot->stats_layout[j].offset); 1705a556c76aSAlexandre Belloni 1706a556c76aSAlexandre Belloni if (val < (ocelot->stats[idx] & U32_MAX)) 1707a556c76aSAlexandre Belloni ocelot->stats[idx] += (u64)1 << 32; 1708a556c76aSAlexandre Belloni 1709a556c76aSAlexandre Belloni ocelot->stats[idx] = (ocelot->stats[idx] & 1710a556c76aSAlexandre Belloni ~(u64)U32_MAX) + val; 1711a556c76aSAlexandre Belloni } 1712a556c76aSAlexandre Belloni } 1713a556c76aSAlexandre Belloni 17141e1caa97SClaudiu Manoil mutex_unlock(&ocelot->stats_lock); 17151e1caa97SClaudiu Manoil } 17161e1caa97SClaudiu Manoil 17171e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work) 17181e1caa97SClaudiu Manoil { 17191e1caa97SClaudiu Manoil struct delayed_work *del_work = to_delayed_work(work); 17201e1caa97SClaudiu Manoil struct ocelot *ocelot = container_of(del_work, struct ocelot, 17211e1caa97SClaudiu Manoil stats_work); 17221e1caa97SClaudiu Manoil 17231e1caa97SClaudiu Manoil ocelot_update_stats(ocelot); 17241e1caa97SClaudiu Manoil 1725a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1726a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 1727a556c76aSAlexandre Belloni } 1728a556c76aSAlexandre Belloni 17295e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) 1730a556c76aSAlexandre Belloni { 1731a556c76aSAlexandre Belloni int i; 1732a556c76aSAlexandre Belloni 1733a556c76aSAlexandre Belloni /* check and update now */ 17341e1caa97SClaudiu Manoil ocelot_update_stats(ocelot); 1735a556c76aSAlexandre Belloni 1736a556c76aSAlexandre Belloni /* Copy all counters */ 1737a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 1738004d44f6SVladimir Oltean *data++ = ocelot->stats[port * ocelot->num_stats + i]; 1739a556c76aSAlexandre Belloni } 17405e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats); 1741a556c76aSAlexandre Belloni 17425e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) 1743c7282d38SVladimir Oltean { 1744a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 1745a556c76aSAlexandre Belloni return -EOPNOTSUPP; 1746c7282d38SVladimir Oltean 1747a556c76aSAlexandre Belloni return ocelot->num_stats; 1748a556c76aSAlexandre Belloni } 17495e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count); 1750a556c76aSAlexandre Belloni 17515e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port, 1752c7282d38SVladimir Oltean struct ethtool_ts_info *info) 1753c7282d38SVladimir Oltean { 17544e3b0468SAntoine Tenart info->phc_index = ocelot->ptp_clock ? 17554e3b0468SAntoine Tenart ptp_clock_index(ocelot->ptp_clock) : -1; 1756d2b09a8eSYangbo Lu if (info->phc_index == -1) { 1757d2b09a8eSYangbo Lu info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 1758d2b09a8eSYangbo Lu SOF_TIMESTAMPING_RX_SOFTWARE | 1759d2b09a8eSYangbo Lu SOF_TIMESTAMPING_SOFTWARE; 1760d2b09a8eSYangbo Lu return 0; 1761d2b09a8eSYangbo Lu } 17624e3b0468SAntoine Tenart info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 17634e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_SOFTWARE | 17644e3b0468SAntoine Tenart SOF_TIMESTAMPING_SOFTWARE | 17654e3b0468SAntoine Tenart SOF_TIMESTAMPING_TX_HARDWARE | 17664e3b0468SAntoine Tenart SOF_TIMESTAMPING_RX_HARDWARE | 17674e3b0468SAntoine Tenart SOF_TIMESTAMPING_RAW_HARDWARE; 17684e3b0468SAntoine Tenart info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | 17694e3b0468SAntoine Tenart BIT(HWTSTAMP_TX_ONESTEP_SYNC); 1770c49a35eeSVladimir Oltean info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | 1771c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) | 1772c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 1773c49a35eeSVladimir Oltean BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT); 17744e3b0468SAntoine Tenart 17754e3b0468SAntoine Tenart return 0; 17764e3b0468SAntoine Tenart } 17775e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info); 17784e3b0468SAntoine Tenart 177923ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond, 178023ca3b72SVladimir Oltean bool only_active_ports) 1781b80af659SVladimir Oltean { 1782b80af659SVladimir Oltean u32 mask = 0; 1783b80af659SVladimir Oltean int port; 1784b80af659SVladimir Oltean 1785b80af659SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1786b80af659SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1787b80af659SVladimir Oltean 1788b80af659SVladimir Oltean if (!ocelot_port) 1789b80af659SVladimir Oltean continue; 1790b80af659SVladimir Oltean 179123ca3b72SVladimir Oltean if (ocelot_port->bond == bond) { 179223ca3b72SVladimir Oltean if (only_active_ports && !ocelot_port->lag_tx_active) 179323ca3b72SVladimir Oltean continue; 179423ca3b72SVladimir Oltean 1795b80af659SVladimir Oltean mask |= BIT(port); 1796b80af659SVladimir Oltean } 179723ca3b72SVladimir Oltean } 1798b80af659SVladimir Oltean 1799b80af659SVladimir Oltean return mask; 1800b80af659SVladimir Oltean } 1801b80af659SVladimir Oltean 18028abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port) 1803df291e54SVladimir Oltean { 1804acc64f52SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[src_port]; 1805a8bd9fa5SVladimir Oltean const struct net_device *bridge; 1806df291e54SVladimir Oltean u32 mask = 0; 1807df291e54SVladimir Oltean int port; 1808df291e54SVladimir Oltean 1809a8bd9fa5SVladimir Oltean if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING) 1810a8bd9fa5SVladimir Oltean return 0; 1811a8bd9fa5SVladimir Oltean 1812a8bd9fa5SVladimir Oltean bridge = ocelot_port->bridge; 1813a8bd9fa5SVladimir Oltean if (!bridge) 1814acc64f52SVladimir Oltean return 0; 1815acc64f52SVladimir Oltean 1816df291e54SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1817acc64f52SVladimir Oltean ocelot_port = ocelot->ports[port]; 1818df291e54SVladimir Oltean 1819df291e54SVladimir Oltean if (!ocelot_port) 1820df291e54SVladimir Oltean continue; 1821df291e54SVladimir Oltean 1822df291e54SVladimir Oltean if (ocelot_port->stp_state == BR_STATE_FORWARDING && 1823df291e54SVladimir Oltean ocelot_port->bridge == bridge) 1824df291e54SVladimir Oltean mask |= BIT(port); 1825df291e54SVladimir Oltean } 1826df291e54SVladimir Oltean 1827df291e54SVladimir Oltean return mask; 1828df291e54SVladimir Oltean } 18298abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask); 1830df291e54SVladimir Oltean 18318abe1970SVladimir Oltean u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot) 18329b521250SVladimir Oltean { 1833e21268efSVladimir Oltean u32 mask = 0; 18349b521250SVladimir Oltean int port; 18359b521250SVladimir Oltean 1836e21268efSVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1837e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1838e21268efSVladimir Oltean 1839e21268efSVladimir Oltean if (!ocelot_port) 1840e21268efSVladimir Oltean continue; 1841e21268efSVladimir Oltean 1842e21268efSVladimir Oltean if (ocelot_port->is_dsa_8021q_cpu) 1843e21268efSVladimir Oltean mask |= BIT(port); 1844e21268efSVladimir Oltean } 1845e21268efSVladimir Oltean 1846e21268efSVladimir Oltean return mask; 1847e21268efSVladimir Oltean } 18488abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_dsa_8021q_cpu_mask); 1849e21268efSVladimir Oltean 18508abe1970SVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining) 1851e21268efSVladimir Oltean { 1852e21268efSVladimir Oltean unsigned long cpu_fwd_mask; 1853e21268efSVladimir Oltean int port; 1854e21268efSVladimir Oltean 18558abe1970SVladimir Oltean lockdep_assert_held(&ocelot->fwd_domain_lock); 18568abe1970SVladimir Oltean 18578abe1970SVladimir Oltean /* If cut-through forwarding is supported, update the masks before a 18588abe1970SVladimir Oltean * port joins the forwarding domain, to avoid potential underruns if it 18598abe1970SVladimir Oltean * has the highest speed from the new domain. 18608abe1970SVladimir Oltean */ 18618abe1970SVladimir Oltean if (joining && ocelot->ops->cut_through_fwd) 18628abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 18638abe1970SVladimir Oltean 1864e21268efSVladimir Oltean /* If a DSA tag_8021q CPU exists, it needs to be included in the 1865e21268efSVladimir Oltean * regular forwarding path of the front ports regardless of whether 1866e21268efSVladimir Oltean * those are bridged or standalone. 1867e21268efSVladimir Oltean * If DSA tag_8021q is not used, this returns 0, which is fine because 1868e21268efSVladimir Oltean * the hardware-based CPU port module can be a destination for packets 1869e21268efSVladimir Oltean * even if it isn't part of PGID_SRC. 1870e21268efSVladimir Oltean */ 1871e21268efSVladimir Oltean cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot); 1872e21268efSVladimir Oltean 18739b521250SVladimir Oltean /* Apply FWD mask. The loop is needed to add/remove the current port as 18749b521250SVladimir Oltean * a source for the other ports. 18759b521250SVladimir Oltean */ 18769b521250SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 1877e21268efSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1878e21268efSVladimir Oltean unsigned long mask; 1879e21268efSVladimir Oltean 1880e21268efSVladimir Oltean if (!ocelot_port) { 1881e21268efSVladimir Oltean /* Unused ports can't send anywhere */ 1882e21268efSVladimir Oltean mask = 0; 1883e21268efSVladimir Oltean } else if (ocelot_port->is_dsa_8021q_cpu) { 1884e21268efSVladimir Oltean /* The DSA tag_8021q CPU ports need to be able to 1885e21268efSVladimir Oltean * forward packets to all other ports except for 1886e21268efSVladimir Oltean * themselves 1887e21268efSVladimir Oltean */ 1888e21268efSVladimir Oltean mask = GENMASK(ocelot->num_phys_ports - 1, 0); 1889e21268efSVladimir Oltean mask &= ~cpu_fwd_mask; 1890df291e54SVladimir Oltean } else if (ocelot_port->bridge) { 1891528d3f19SVladimir Oltean struct net_device *bond = ocelot_port->bond; 18929b521250SVladimir Oltean 1893a8bd9fa5SVladimir Oltean mask = ocelot_get_bridge_fwd_mask(ocelot, port); 1894c1930148SVladimir Oltean mask |= cpu_fwd_mask; 1895df291e54SVladimir Oltean mask &= ~BIT(port); 189623ca3b72SVladimir Oltean if (bond) { 189723ca3b72SVladimir Oltean mask &= ~ocelot_get_bond_mask(ocelot, bond, 189823ca3b72SVladimir Oltean false); 189923ca3b72SVladimir Oltean } 19009b521250SVladimir Oltean } else { 1901e21268efSVladimir Oltean /* Standalone ports forward only to DSA tag_8021q CPU 1902e21268efSVladimir Oltean * ports (if those exist), or to the hardware CPU port 1903e21268efSVladimir Oltean * module otherwise. 1904e21268efSVladimir Oltean */ 1905e21268efSVladimir Oltean mask = cpu_fwd_mask; 1906e21268efSVladimir Oltean } 1907e21268efSVladimir Oltean 1908e21268efSVladimir Oltean ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); 19099b521250SVladimir Oltean } 19108abe1970SVladimir Oltean 19118abe1970SVladimir Oltean /* If cut-through forwarding is supported and a port is leaving, there 19128abe1970SVladimir Oltean * is a chance that cut-through was disabled on the other ports due to 19138abe1970SVladimir Oltean * the port which is leaving (it has a higher link speed). We need to 19148abe1970SVladimir Oltean * update the cut-through masks of the remaining ports no earlier than 19158abe1970SVladimir Oltean * after the port has left, to prevent underruns from happening between 19168abe1970SVladimir Oltean * the cut-through update and the forwarding domain update. 19178abe1970SVladimir Oltean */ 19188abe1970SVladimir Oltean if (!joining && ocelot->ops->cut_through_fwd) 19198abe1970SVladimir Oltean ocelot->ops->cut_through_fwd(ocelot); 19209b521250SVladimir Oltean } 1921e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask); 19229b521250SVladimir Oltean 19235e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) 1924a556c76aSAlexandre Belloni { 1925421741eaSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 1926df291e54SVladimir Oltean u32 learn_ena = 0; 1927a556c76aSAlexandre Belloni 19288abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 19298abe1970SVladimir Oltean 1930df291e54SVladimir Oltean ocelot_port->stp_state = state; 1931a556c76aSAlexandre Belloni 1932df291e54SVladimir Oltean if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) && 1933df291e54SVladimir Oltean ocelot_port->learn_ena) 1934df291e54SVladimir Oltean learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA; 1935a556c76aSAlexandre Belloni 1936df291e54SVladimir Oltean ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA, 1937df291e54SVladimir Oltean ANA_PORT_PORT_CFG, port); 1938a556c76aSAlexandre Belloni 19398abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING); 19408abe1970SVladimir Oltean 19418abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 1942a556c76aSAlexandre Belloni } 19435e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set); 1944a556c76aSAlexandre Belloni 19455e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) 19464bda1415SVladimir Oltean { 1947c0d7eccbSVladimir Oltean unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); 1948c0d7eccbSVladimir Oltean 1949c0d7eccbSVladimir Oltean /* Setting AGE_PERIOD to zero effectively disables automatic aging, 1950c0d7eccbSVladimir Oltean * which is clearly not what our intention is. So avoid that. 1951c0d7eccbSVladimir Oltean */ 1952c0d7eccbSVladimir Oltean if (!age_period) 1953c0d7eccbSVladimir Oltean age_period = 1; 1954c0d7eccbSVladimir Oltean 1955c0d7eccbSVladimir Oltean ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); 1956a556c76aSAlexandre Belloni } 19575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time); 1958a556c76aSAlexandre Belloni 1959a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 1960a556c76aSAlexandre Belloni const unsigned char *addr, 1961a556c76aSAlexandre Belloni u16 vid) 1962a556c76aSAlexandre Belloni { 1963a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 1964a556c76aSAlexandre Belloni 1965a556c76aSAlexandre Belloni list_for_each_entry(mc, &ocelot->multicast, list) { 1966a556c76aSAlexandre Belloni if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 1967a556c76aSAlexandre Belloni return mc; 1968a556c76aSAlexandre Belloni } 1969a556c76aSAlexandre Belloni 1970a556c76aSAlexandre Belloni return NULL; 1971a556c76aSAlexandre Belloni } 1972a556c76aSAlexandre Belloni 19739403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) 19749403c158SVladimir Oltean { 19759403c158SVladimir Oltean if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) 19769403c158SVladimir Oltean return ENTRYTYPE_MACv4; 19779403c158SVladimir Oltean if (addr[0] == 0x33 && addr[1] == 0x33) 19789403c158SVladimir Oltean return ENTRYTYPE_MACv6; 19797c313143SVladimir Oltean return ENTRYTYPE_LOCKED; 19809403c158SVladimir Oltean } 19819403c158SVladimir Oltean 1982e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, 1983e5d1f896SVladimir Oltean unsigned long ports) 1984e5d1f896SVladimir Oltean { 1985e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 1986e5d1f896SVladimir Oltean 1987e5d1f896SVladimir Oltean pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); 1988e5d1f896SVladimir Oltean if (!pgid) 1989e5d1f896SVladimir Oltean return ERR_PTR(-ENOMEM); 1990e5d1f896SVladimir Oltean 1991e5d1f896SVladimir Oltean pgid->ports = ports; 1992e5d1f896SVladimir Oltean pgid->index = index; 1993e5d1f896SVladimir Oltean refcount_set(&pgid->refcount, 1); 1994e5d1f896SVladimir Oltean list_add_tail(&pgid->list, &ocelot->pgids); 1995e5d1f896SVladimir Oltean 1996e5d1f896SVladimir Oltean return pgid; 1997e5d1f896SVladimir Oltean } 1998e5d1f896SVladimir Oltean 1999e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) 2000e5d1f896SVladimir Oltean { 2001e5d1f896SVladimir Oltean if (!refcount_dec_and_test(&pgid->refcount)) 2002e5d1f896SVladimir Oltean return; 2003e5d1f896SVladimir Oltean 2004e5d1f896SVladimir Oltean list_del(&pgid->list); 2005e5d1f896SVladimir Oltean kfree(pgid); 2006e5d1f896SVladimir Oltean } 2007e5d1f896SVladimir Oltean 2008e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, 2009bb8d53fdSVladimir Oltean const struct ocelot_multicast *mc) 20109403c158SVladimir Oltean { 2011e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2012e5d1f896SVladimir Oltean int index; 20139403c158SVladimir Oltean 20149403c158SVladimir Oltean /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and 20159403c158SVladimir Oltean * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the 20169403c158SVladimir Oltean * destination mask table (PGID), the destination set is programmed as 20179403c158SVladimir Oltean * part of the entry MAC address.", and the DEST_IDX is set to 0. 20189403c158SVladimir Oltean */ 2019bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4 || 2020bb8d53fdSVladimir Oltean mc->entry_type == ENTRYTYPE_MACv6) 2021e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, 0, mc->ports); 20229403c158SVladimir Oltean 2023e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 2024e5d1f896SVladimir Oltean /* When searching for a nonreserved multicast PGID, ignore the 2025e5d1f896SVladimir Oltean * dummy PGID of zero that we have for MACv4/MACv6 entries 2026e5d1f896SVladimir Oltean */ 2027e5d1f896SVladimir Oltean if (pgid->index && pgid->ports == mc->ports) { 2028e5d1f896SVladimir Oltean refcount_inc(&pgid->refcount); 2029e5d1f896SVladimir Oltean return pgid; 2030e5d1f896SVladimir Oltean } 2031e5d1f896SVladimir Oltean } 2032e5d1f896SVladimir Oltean 2033e5d1f896SVladimir Oltean /* Search for a free index in the nonreserved multicast PGID area */ 2034e5d1f896SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, index) { 20359403c158SVladimir Oltean bool used = false; 20369403c158SVladimir Oltean 2037e5d1f896SVladimir Oltean list_for_each_entry(pgid, &ocelot->pgids, list) { 2038e5d1f896SVladimir Oltean if (pgid->index == index) { 20399403c158SVladimir Oltean used = true; 20409403c158SVladimir Oltean break; 20419403c158SVladimir Oltean } 20429403c158SVladimir Oltean } 20439403c158SVladimir Oltean 20449403c158SVladimir Oltean if (!used) 2045e5d1f896SVladimir Oltean return ocelot_pgid_alloc(ocelot, index, mc->ports); 20469403c158SVladimir Oltean } 20479403c158SVladimir Oltean 2048e5d1f896SVladimir Oltean return ERR_PTR(-ENOSPC); 20499403c158SVladimir Oltean } 20509403c158SVladimir Oltean 20519403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr, 2052bb8d53fdSVladimir Oltean struct ocelot_multicast *mc) 20539403c158SVladimir Oltean { 2054ebbd860eSVladimir Oltean ether_addr_copy(addr, mc->addr); 20559403c158SVladimir Oltean 2056bb8d53fdSVladimir Oltean if (mc->entry_type == ENTRYTYPE_MACv4) { 20579403c158SVladimir Oltean addr[0] = 0; 20589403c158SVladimir Oltean addr[1] = mc->ports >> 8; 20599403c158SVladimir Oltean addr[2] = mc->ports & 0xff; 2060bb8d53fdSVladimir Oltean } else if (mc->entry_type == ENTRYTYPE_MACv6) { 20619403c158SVladimir Oltean addr[0] = mc->ports >> 8; 20629403c158SVladimir Oltean addr[1] = mc->ports & 0xff; 20639403c158SVladimir Oltean } 20649403c158SVladimir Oltean } 20659403c158SVladimir Oltean 2066209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port, 2067209edf95SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 2068a556c76aSAlexandre Belloni { 2069a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 2070004d44f6SVladimir Oltean struct ocelot_multicast *mc; 2071e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2072a556c76aSAlexandre Belloni u16 vid = mdb->vid; 2073a556c76aSAlexandre Belloni 2074471beb11SVladimir Oltean if (port == ocelot->npi) 2075471beb11SVladimir Oltean port = ocelot->num_phys_ports; 2076471beb11SVladimir Oltean 2077a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 2078a556c76aSAlexandre Belloni if (!mc) { 2079728e69aeSVladimir Oltean /* New entry */ 2080bb8d53fdSVladimir Oltean mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 2081bb8d53fdSVladimir Oltean if (!mc) 2082bb8d53fdSVladimir Oltean return -ENOMEM; 2083bb8d53fdSVladimir Oltean 2084bb8d53fdSVladimir Oltean mc->entry_type = ocelot_classify_mdb(mdb->addr); 2085bb8d53fdSVladimir Oltean ether_addr_copy(mc->addr, mdb->addr); 2086bb8d53fdSVladimir Oltean mc->vid = vid; 2087bb8d53fdSVladimir Oltean 2088a556c76aSAlexandre Belloni list_add_tail(&mc->list, &ocelot->multicast); 2089728e69aeSVladimir Oltean } else { 2090e5d1f896SVladimir Oltean /* Existing entry. Clean up the current port mask from 2091e5d1f896SVladimir Oltean * hardware now, because we'll be modifying it. 2092e5d1f896SVladimir Oltean */ 2093e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 2094bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2095a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 2096a556c76aSAlexandre Belloni } 2097a556c76aSAlexandre Belloni 2098004d44f6SVladimir Oltean mc->ports |= BIT(port); 2099e5d1f896SVladimir Oltean 2100e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 2101e5d1f896SVladimir Oltean if (IS_ERR(pgid)) { 2102e5d1f896SVladimir Oltean dev_err(ocelot->dev, 2103e5d1f896SVladimir Oltean "Cannot allocate PGID for mdb %pM vid %d\n", 2104e5d1f896SVladimir Oltean mc->addr, mc->vid); 2105e5d1f896SVladimir Oltean devm_kfree(ocelot->dev, mc); 2106e5d1f896SVladimir Oltean return PTR_ERR(pgid); 2107e5d1f896SVladimir Oltean } 2108e5d1f896SVladimir Oltean mc->pgid = pgid; 2109e5d1f896SVladimir Oltean 2110bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2111a556c76aSAlexandre Belloni 2112e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 2113e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 2114e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 2115e5d1f896SVladimir Oltean pgid->index); 2116e5d1f896SVladimir Oltean 2117e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 2118bb8d53fdSVladimir Oltean mc->entry_type); 2119a556c76aSAlexandre Belloni } 2120209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add); 2121a556c76aSAlexandre Belloni 2122209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port, 2123a556c76aSAlexandre Belloni const struct switchdev_obj_port_mdb *mdb) 2124a556c76aSAlexandre Belloni { 2125a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 2126004d44f6SVladimir Oltean struct ocelot_multicast *mc; 2127e5d1f896SVladimir Oltean struct ocelot_pgid *pgid; 2128a556c76aSAlexandre Belloni u16 vid = mdb->vid; 2129a556c76aSAlexandre Belloni 2130471beb11SVladimir Oltean if (port == ocelot->npi) 2131471beb11SVladimir Oltean port = ocelot->num_phys_ports; 2132471beb11SVladimir Oltean 2133a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 2134a556c76aSAlexandre Belloni if (!mc) 2135a556c76aSAlexandre Belloni return -ENOENT; 2136a556c76aSAlexandre Belloni 2137bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2138a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 2139a556c76aSAlexandre Belloni 2140e5d1f896SVladimir Oltean ocelot_pgid_free(ocelot, mc->pgid); 2141004d44f6SVladimir Oltean mc->ports &= ~BIT(port); 2142a556c76aSAlexandre Belloni if (!mc->ports) { 2143a556c76aSAlexandre Belloni list_del(&mc->list); 2144a556c76aSAlexandre Belloni devm_kfree(ocelot->dev, mc); 2145a556c76aSAlexandre Belloni return 0; 2146a556c76aSAlexandre Belloni } 2147a556c76aSAlexandre Belloni 2148e5d1f896SVladimir Oltean /* We have a PGID with fewer ports now */ 2149e5d1f896SVladimir Oltean pgid = ocelot_mdb_get_pgid(ocelot, mc); 2150e5d1f896SVladimir Oltean if (IS_ERR(pgid)) 2151e5d1f896SVladimir Oltean return PTR_ERR(pgid); 2152e5d1f896SVladimir Oltean mc->pgid = pgid; 2153e5d1f896SVladimir Oltean 2154bb8d53fdSVladimir Oltean ocelot_encode_ports_to_mdb(addr, mc); 2155a556c76aSAlexandre Belloni 2156e5d1f896SVladimir Oltean if (mc->entry_type != ENTRYTYPE_MACv4 && 2157e5d1f896SVladimir Oltean mc->entry_type != ENTRYTYPE_MACv6) 2158e5d1f896SVladimir Oltean ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 2159e5d1f896SVladimir Oltean pgid->index); 2160e5d1f896SVladimir Oltean 2161e5d1f896SVladimir Oltean return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 2162bb8d53fdSVladimir Oltean mc->entry_type); 2163a556c76aSAlexandre Belloni } 2164209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del); 2165a556c76aSAlexandre Belloni 2166e4bd44e8SVladimir Oltean void ocelot_port_bridge_join(struct ocelot *ocelot, int port, 2167a556c76aSAlexandre Belloni struct net_device *bridge) 2168a556c76aSAlexandre Belloni { 2169df291e54SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2170a556c76aSAlexandre Belloni 21718abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 21728abe1970SVladimir Oltean 2173df291e54SVladimir Oltean ocelot_port->bridge = bridge; 2174a556c76aSAlexandre Belloni 21758abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 21768abe1970SVladimir Oltean 21778abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2178a556c76aSAlexandre Belloni } 21795e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join); 2180a556c76aSAlexandre Belloni 2181e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port, 2182a556c76aSAlexandre Belloni struct net_device *bridge) 2183a556c76aSAlexandre Belloni { 2184df291e54SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 21852e554a7aSVladimir Oltean 21868abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 21878abe1970SVladimir Oltean 2188df291e54SVladimir Oltean ocelot_port->bridge = NULL; 21897142529fSAntoine Tenart 2190d4004422SVladimir Oltean ocelot_port_set_pvid(ocelot, port, NULL); 21910da1a1c4SVladimir Oltean ocelot_port_manage_port_tag(ocelot, port); 21928abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, false); 21938abe1970SVladimir Oltean 21948abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2195a556c76aSAlexandre Belloni } 21965e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave); 2197a556c76aSAlexandre Belloni 2198dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 2199dc96ee37SAlexandre Belloni { 2200528d3f19SVladimir Oltean unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0); 2201dc96ee37SAlexandre Belloni int i, port, lag; 2202dc96ee37SAlexandre Belloni 2203dc96ee37SAlexandre Belloni /* Reset destination and aggregation PGIDS */ 220496b029b0SVladimir Oltean for_each_unicast_dest_pgid(ocelot, port) 2205dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 2206dc96ee37SAlexandre Belloni 220796b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) 2208dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 2209dc96ee37SAlexandre Belloni ANA_PGID_PGID, i); 2210dc96ee37SAlexandre Belloni 2211528d3f19SVladimir Oltean /* The visited ports bitmask holds the list of ports offloading any 2212528d3f19SVladimir Oltean * bonding interface. Initially we mark all these ports as unvisited, 2213528d3f19SVladimir Oltean * then every time we visit a port in this bitmask, we know that it is 2214528d3f19SVladimir Oltean * the lowest numbered port, i.e. the one whose logical ID == physical 2215528d3f19SVladimir Oltean * port ID == LAG ID. So we mark as visited all further ports in the 2216528d3f19SVladimir Oltean * bitmask that are offloading the same bonding interface. This way, 2217528d3f19SVladimir Oltean * we set up the aggregation PGIDs only once per bonding interface. 2218528d3f19SVladimir Oltean */ 2219528d3f19SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 2220528d3f19SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2221528d3f19SVladimir Oltean 2222528d3f19SVladimir Oltean if (!ocelot_port || !ocelot_port->bond) 2223528d3f19SVladimir Oltean continue; 2224528d3f19SVladimir Oltean 2225528d3f19SVladimir Oltean visited &= ~BIT(port); 2226528d3f19SVladimir Oltean } 2227528d3f19SVladimir Oltean 2228528d3f19SVladimir Oltean /* Now, set PGIDs for each active LAG */ 2229dc96ee37SAlexandre Belloni for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 2230528d3f19SVladimir Oltean struct net_device *bond = ocelot->ports[lag]->bond; 223123ca3b72SVladimir Oltean int num_active_ports = 0; 2232dc96ee37SAlexandre Belloni unsigned long bond_mask; 2233dc96ee37SAlexandre Belloni u8 aggr_idx[16]; 2234dc96ee37SAlexandre Belloni 2235528d3f19SVladimir Oltean if (!bond || (visited & BIT(lag))) 2236dc96ee37SAlexandre Belloni continue; 2237dc96ee37SAlexandre Belloni 223823ca3b72SVladimir Oltean bond_mask = ocelot_get_bond_mask(ocelot, bond, true); 2239528d3f19SVladimir Oltean 2240dc96ee37SAlexandre Belloni for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 2241dc96ee37SAlexandre Belloni // Destination mask 2242dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, bond_mask, 2243dc96ee37SAlexandre Belloni ANA_PGID_PGID, port); 224423ca3b72SVladimir Oltean aggr_idx[num_active_ports++] = port; 2245dc96ee37SAlexandre Belloni } 2246dc96ee37SAlexandre Belloni 224796b029b0SVladimir Oltean for_each_aggr_pgid(ocelot, i) { 2248dc96ee37SAlexandre Belloni u32 ac; 2249dc96ee37SAlexandre Belloni 2250dc96ee37SAlexandre Belloni ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 2251dc96ee37SAlexandre Belloni ac &= ~bond_mask; 225223ca3b72SVladimir Oltean /* Don't do division by zero if there was no active 225323ca3b72SVladimir Oltean * port. Just make all aggregation codes zero. 225423ca3b72SVladimir Oltean */ 225523ca3b72SVladimir Oltean if (num_active_ports) 225623ca3b72SVladimir Oltean ac |= BIT(aggr_idx[i % num_active_ports]); 2257dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 2258dc96ee37SAlexandre Belloni } 2259528d3f19SVladimir Oltean 2260528d3f19SVladimir Oltean /* Mark all ports in the same LAG as visited to avoid applying 2261528d3f19SVladimir Oltean * the same config again. 2262528d3f19SVladimir Oltean */ 2263528d3f19SVladimir Oltean for (port = lag; port < ocelot->num_phys_ports; port++) { 2264528d3f19SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2265528d3f19SVladimir Oltean 2266528d3f19SVladimir Oltean if (!ocelot_port) 2267528d3f19SVladimir Oltean continue; 2268528d3f19SVladimir Oltean 2269528d3f19SVladimir Oltean if (ocelot_port->bond == bond) 2270528d3f19SVladimir Oltean visited |= BIT(port); 2271528d3f19SVladimir Oltean } 2272dc96ee37SAlexandre Belloni } 2273dc96ee37SAlexandre Belloni } 2274dc96ee37SAlexandre Belloni 22752527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the 22762527f2e8SVladimir Oltean * same bond must have the same logical port ID, equal to the physical port ID 22772527f2e8SVladimir Oltean * of the lowest numbered physical port in that bond. Otherwise, in standalone/ 22782527f2e8SVladimir Oltean * bridged mode, each port has a logical port ID equal to its physical port ID. 22792527f2e8SVladimir Oltean */ 22802527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) 2281dc96ee37SAlexandre Belloni { 22822527f2e8SVladimir Oltean int port; 2283dc96ee37SAlexandre Belloni 22842527f2e8SVladimir Oltean for (port = 0; port < ocelot->num_phys_ports; port++) { 22852527f2e8SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 22862527f2e8SVladimir Oltean struct net_device *bond; 2287dc96ee37SAlexandre Belloni 22882527f2e8SVladimir Oltean if (!ocelot_port) 22892527f2e8SVladimir Oltean continue; 2290dc96ee37SAlexandre Belloni 22912527f2e8SVladimir Oltean bond = ocelot_port->bond; 22922527f2e8SVladimir Oltean if (bond) { 229323ca3b72SVladimir Oltean int lag = __ffs(ocelot_get_bond_mask(ocelot, bond, 229423ca3b72SVladimir Oltean false)); 22952527f2e8SVladimir Oltean 22962527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 2297dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(lag), 22982527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 22992527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 23002527f2e8SVladimir Oltean } else { 23012527f2e8SVladimir Oltean ocelot_rmw_gix(ocelot, 23022527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 23032527f2e8SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL_M, 23042527f2e8SVladimir Oltean ANA_PORT_PORT_CFG, port); 23052527f2e8SVladimir Oltean } 2306dc96ee37SAlexandre Belloni } 2307dc96ee37SAlexandre Belloni } 2308dc96ee37SAlexandre Belloni 23099c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port, 2310583cbbe3SVladimir Oltean struct net_device *bond, 2311583cbbe3SVladimir Oltean struct netdev_lag_upper_info *info) 2312dc96ee37SAlexandre Belloni { 2313583cbbe3SVladimir Oltean if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) 2314583cbbe3SVladimir Oltean return -EOPNOTSUPP; 2315583cbbe3SVladimir Oltean 23168abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 23178abe1970SVladimir Oltean 2318b80af659SVladimir Oltean ocelot->ports[port]->bond = bond; 2319dc96ee37SAlexandre Belloni 23202527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 23218abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, true); 2322dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 2323dc96ee37SAlexandre Belloni 23248abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 23258abe1970SVladimir Oltean 2326dc96ee37SAlexandre Belloni return 0; 2327dc96ee37SAlexandre Belloni } 23289c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join); 2329dc96ee37SAlexandre Belloni 23309c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port, 2331dc96ee37SAlexandre Belloni struct net_device *bond) 2332dc96ee37SAlexandre Belloni { 23338abe1970SVladimir Oltean mutex_lock(&ocelot->fwd_domain_lock); 23348abe1970SVladimir Oltean 2335b80af659SVladimir Oltean ocelot->ports[port]->bond = NULL; 2336b80af659SVladimir Oltean 23372527f2e8SVladimir Oltean ocelot_setup_logical_port_ids(ocelot); 23388abe1970SVladimir Oltean ocelot_apply_bridge_fwd_mask(ocelot, false); 2339dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 23408abe1970SVladimir Oltean 23418abe1970SVladimir Oltean mutex_unlock(&ocelot->fwd_domain_lock); 2342dc96ee37SAlexandre Belloni } 23439c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave); 23440e332c85SPetr Machata 234523ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active) 234623ca3b72SVladimir Oltean { 234723ca3b72SVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 234823ca3b72SVladimir Oltean 234923ca3b72SVladimir Oltean ocelot_port->lag_tx_active = lag_tx_active; 235023ca3b72SVladimir Oltean 235123ca3b72SVladimir Oltean /* Rebalance the LAGs */ 235223ca3b72SVladimir Oltean ocelot_set_aggr_pgids(ocelot); 235323ca3b72SVladimir Oltean } 235423ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change); 235523ca3b72SVladimir Oltean 2356a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. 2357a8015dedSVladimir Oltean * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. 23580b912fc9SVladimir Oltean * In the special case that it's the NPI port that we're configuring, the 23590b912fc9SVladimir Oltean * length of the tag and optional prefix needs to be accounted for privately, 23600b912fc9SVladimir Oltean * in order to be able to sustain communication at the requested @sdu. 2361a8015dedSVladimir Oltean */ 23620b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) 236331350d7fSVladimir Oltean { 236431350d7fSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2365a8015dedSVladimir Oltean int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; 2366e8e6e73dSVladimir Oltean int pause_start, pause_stop; 2367601e984fSVladimir Oltean int atop, atop_tot; 236831350d7fSVladimir Oltean 23690b912fc9SVladimir Oltean if (port == ocelot->npi) { 23700b912fc9SVladimir Oltean maxlen += OCELOT_TAG_LEN; 23710b912fc9SVladimir Oltean 2372cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 23730b912fc9SVladimir Oltean maxlen += OCELOT_SHORT_PREFIX_LEN; 2374cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 23750b912fc9SVladimir Oltean maxlen += OCELOT_LONG_PREFIX_LEN; 23760b912fc9SVladimir Oltean } 23770b912fc9SVladimir Oltean 2378a8015dedSVladimir Oltean ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); 2379fa914e9cSVladimir Oltean 2380e8e6e73dSVladimir Oltean /* Set Pause watermark hysteresis */ 2381e8e6e73dSVladimir Oltean pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; 2382e8e6e73dSVladimir Oltean pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; 2383541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, 2384541132f0SMaxim Kochetkov pause_start); 2385541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, 2386541132f0SMaxim Kochetkov pause_stop); 2387fa914e9cSVladimir Oltean 2388601e984fSVladimir Oltean /* Tail dropping watermarks */ 2389f6fe01d6SVladimir Oltean atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / 2390a8015dedSVladimir Oltean OCELOT_BUFFER_CELL_SZ; 2391601e984fSVladimir Oltean atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; 2392601e984fSVladimir Oltean ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); 2393601e984fSVladimir Oltean ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); 2394fa914e9cSVladimir Oltean } 23950b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen); 23960b912fc9SVladimir Oltean 23970b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port) 23980b912fc9SVladimir Oltean { 23990b912fc9SVladimir Oltean int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; 24000b912fc9SVladimir Oltean 24010b912fc9SVladimir Oltean if (port == ocelot->npi) { 24020b912fc9SVladimir Oltean max_mtu -= OCELOT_TAG_LEN; 24030b912fc9SVladimir Oltean 2404cacea62fSVladimir Oltean if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 24050b912fc9SVladimir Oltean max_mtu -= OCELOT_SHORT_PREFIX_LEN; 2406cacea62fSVladimir Oltean else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 24070b912fc9SVladimir Oltean max_mtu -= OCELOT_LONG_PREFIX_LEN; 24080b912fc9SVladimir Oltean } 24090b912fc9SVladimir Oltean 24100b912fc9SVladimir Oltean return max_mtu; 24110b912fc9SVladimir Oltean } 24120b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu); 2413fa914e9cSVladimir Oltean 2414421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port, 2415421741eaSVladimir Oltean bool enabled) 2416421741eaSVladimir Oltean { 2417421741eaSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2418421741eaSVladimir Oltean u32 val = 0; 2419421741eaSVladimir Oltean 2420421741eaSVladimir Oltean if (enabled) 2421421741eaSVladimir Oltean val = ANA_PORT_PORT_CFG_LEARN_ENA; 2422421741eaSVladimir Oltean 2423421741eaSVladimir Oltean ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA, 2424421741eaSVladimir Oltean ANA_PORT_PORT_CFG, port); 2425421741eaSVladimir Oltean 2426421741eaSVladimir Oltean ocelot_port->learn_ena = enabled; 2427421741eaSVladimir Oltean } 2428421741eaSVladimir Oltean 2429421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port, 2430421741eaSVladimir Oltean bool enabled) 2431421741eaSVladimir Oltean { 2432421741eaSVladimir Oltean u32 val = 0; 2433421741eaSVladimir Oltean 2434421741eaSVladimir Oltean if (enabled) 2435421741eaSVladimir Oltean val = BIT(port); 2436421741eaSVladimir Oltean 2437421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC); 2438421741eaSVladimir Oltean } 2439421741eaSVladimir Oltean 2440421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port, 2441421741eaSVladimir Oltean bool enabled) 2442421741eaSVladimir Oltean { 2443421741eaSVladimir Oltean u32 val = 0; 2444421741eaSVladimir Oltean 2445421741eaSVladimir Oltean if (enabled) 2446421741eaSVladimir Oltean val = BIT(port); 2447421741eaSVladimir Oltean 2448421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC); 2449421741eaSVladimir Oltean } 2450421741eaSVladimir Oltean 2451421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port, 2452421741eaSVladimir Oltean bool enabled) 2453421741eaSVladimir Oltean { 2454421741eaSVladimir Oltean u32 val = 0; 2455421741eaSVladimir Oltean 2456421741eaSVladimir Oltean if (enabled) 2457421741eaSVladimir Oltean val = BIT(port); 2458421741eaSVladimir Oltean 2459421741eaSVladimir Oltean ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC); 2460421741eaSVladimir Oltean } 2461421741eaSVladimir Oltean 2462421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port, 2463421741eaSVladimir Oltean struct switchdev_brport_flags flags) 2464421741eaSVladimir Oltean { 2465421741eaSVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 2466421741eaSVladimir Oltean BR_BCAST_FLOOD)) 2467421741eaSVladimir Oltean return -EINVAL; 2468421741eaSVladimir Oltean 2469421741eaSVladimir Oltean return 0; 2470421741eaSVladimir Oltean } 2471421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags); 2472421741eaSVladimir Oltean 2473421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port, 2474421741eaSVladimir Oltean struct switchdev_brport_flags flags) 2475421741eaSVladimir Oltean { 2476421741eaSVladimir Oltean if (flags.mask & BR_LEARNING) 2477421741eaSVladimir Oltean ocelot_port_set_learning(ocelot, port, 2478421741eaSVladimir Oltean !!(flags.val & BR_LEARNING)); 2479421741eaSVladimir Oltean 2480421741eaSVladimir Oltean if (flags.mask & BR_FLOOD) 2481421741eaSVladimir Oltean ocelot_port_set_ucast_flood(ocelot, port, 2482421741eaSVladimir Oltean !!(flags.val & BR_FLOOD)); 2483421741eaSVladimir Oltean 2484421741eaSVladimir Oltean if (flags.mask & BR_MCAST_FLOOD) 2485421741eaSVladimir Oltean ocelot_port_set_mcast_flood(ocelot, port, 2486421741eaSVladimir Oltean !!(flags.val & BR_MCAST_FLOOD)); 2487421741eaSVladimir Oltean 2488421741eaSVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) 2489421741eaSVladimir Oltean ocelot_port_set_bcast_flood(ocelot, port, 2490421741eaSVladimir Oltean !!(flags.val & BR_BCAST_FLOOD)); 2491421741eaSVladimir Oltean } 2492421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags); 2493421741eaSVladimir Oltean 24945e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port) 2495fa914e9cSVladimir Oltean { 2496fa914e9cSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2497fa914e9cSVladimir Oltean 2498b049da13SYangbo Lu skb_queue_head_init(&ocelot_port->tx_skbs); 249931350d7fSVladimir Oltean 250031350d7fSVladimir Oltean /* Basic L2 initialization */ 250131350d7fSVladimir Oltean 25025bc9d2e6SVladimir Oltean /* Set MAC IFG Gaps 25035bc9d2e6SVladimir Oltean * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 25045bc9d2e6SVladimir Oltean * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 25055bc9d2e6SVladimir Oltean */ 25065bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), 25075bc9d2e6SVladimir Oltean DEV_MAC_IFG_CFG); 25085bc9d2e6SVladimir Oltean 25095bc9d2e6SVladimir Oltean /* Load seed (0) and set MAC HDX late collision */ 25105bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 25115bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG_SEED_LOAD, 25125bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 25135bc9d2e6SVladimir Oltean mdelay(1); 25145bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 25155bc9d2e6SVladimir Oltean DEV_MAC_HDX_CFG); 25165bc9d2e6SVladimir Oltean 25175bc9d2e6SVladimir Oltean /* Set Max Length and maximum tags allowed */ 2518a8015dedSVladimir Oltean ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); 25195bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 25205bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 2521a8015dedSVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | 25225bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 25235bc9d2e6SVladimir Oltean DEV_MAC_TAGS_CFG); 25245bc9d2e6SVladimir Oltean 25255bc9d2e6SVladimir Oltean /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 25265bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 25275bc9d2e6SVladimir Oltean ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); 25285bc9d2e6SVladimir Oltean 2529e8e6e73dSVladimir Oltean /* Enable transmission of pause frames */ 2530541132f0SMaxim Kochetkov ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 2531e8e6e73dSVladimir Oltean 253231350d7fSVladimir Oltean /* Drop frames with multicast source address */ 253331350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 253431350d7fSVladimir Oltean ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 253531350d7fSVladimir Oltean ANA_PORT_DROP_CFG, port); 253631350d7fSVladimir Oltean 253731350d7fSVladimir Oltean /* Set default VLAN and tag type to 8021Q. */ 253831350d7fSVladimir Oltean ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), 253931350d7fSVladimir Oltean REW_PORT_VLAN_CFG_PORT_TPID_M, 254031350d7fSVladimir Oltean REW_PORT_VLAN_CFG, port); 254131350d7fSVladimir Oltean 2542421741eaSVladimir Oltean /* Disable source address learning for standalone mode */ 2543421741eaSVladimir Oltean ocelot_port_set_learning(ocelot, port, false); 2544421741eaSVladimir Oltean 254546efe4efSVladimir Oltean /* Set the port's initial logical port ID value, enable receiving 254646efe4efSVladimir Oltean * frames on it, and configure the MAC address learning type to 254746efe4efSVladimir Oltean * automatic. 254846efe4efSVladimir Oltean */ 254946efe4efSVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 255046efe4efSVladimir Oltean ANA_PORT_PORT_CFG_RECV_ENA | 255146efe4efSVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(port), 255246efe4efSVladimir Oltean ANA_PORT_PORT_CFG, port); 255346efe4efSVladimir Oltean 255431350d7fSVladimir Oltean /* Enable vcap lookups */ 255531350d7fSVladimir Oltean ocelot_vcap_enable(ocelot, port); 255631350d7fSVladimir Oltean } 25575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port); 255831350d7fSVladimir Oltean 25592d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues 25602d44b097SVladimir Oltean * accessible through register MMIO, frame DMA or Ethernet (in case 25612d44b097SVladimir Oltean * NPI mode is used). 256269df578cSVladimir Oltean */ 25632d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot) 256421468199SVladimir Oltean { 256569df578cSVladimir Oltean int cpu = ocelot->num_phys_ports; 256669df578cSVladimir Oltean 256769df578cSVladimir Oltean /* The unicast destination PGID for the CPU port module is unused */ 256821468199SVladimir Oltean ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 256969df578cSVladimir Oltean /* Instead set up a multicast destination PGID for traffic copied to 257069df578cSVladimir Oltean * the CPU. Whitelisted MAC addresses like the port netdevice MAC 257169df578cSVladimir Oltean * addresses will be copied to the CPU via this PGID. 257269df578cSVladimir Oltean */ 257321468199SVladimir Oltean ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 257421468199SVladimir Oltean ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 257521468199SVladimir Oltean ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 257621468199SVladimir Oltean ANA_PORT_PORT_CFG, cpu); 257721468199SVladimir Oltean 257869df578cSVladimir Oltean /* Enable CPU port module */ 2579886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 258069df578cSVladimir Oltean /* CPU port Injection/Extraction configuration */ 2581886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, 2582cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 2583886e1387SVladimir Oltean ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, 2584cacea62fSVladimir Oltean OCELOT_TAG_PREFIX_NONE); 258521468199SVladimir Oltean 258621468199SVladimir Oltean /* Configure the CPU port to be VLAN aware */ 2587bfbab310SVladimir Oltean ocelot_write_gix(ocelot, 2588bfbab310SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_VLAN_UNAWARE_PVID) | 258921468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 259021468199SVladimir Oltean ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 259121468199SVladimir Oltean ANA_PORT_VLAN_CFG, cpu); 259221468199SVladimir Oltean } 259321468199SVladimir Oltean 2594f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot) 2595f6fe01d6SVladimir Oltean { 2596f6fe01d6SVladimir Oltean int mmgt, eq_ctrl; 2597f6fe01d6SVladimir Oltean 2598f6fe01d6SVladimir Oltean /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds 2599f6fe01d6SVladimir Oltean * the number of 240-byte free memory words (aka 4-cell chunks) and not 2600f6fe01d6SVladimir Oltean * 192 bytes as the documentation incorrectly says. 2601f6fe01d6SVladimir Oltean */ 2602f6fe01d6SVladimir Oltean mmgt = ocelot_read(ocelot, SYS_MMGT); 2603f6fe01d6SVladimir Oltean ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); 2604f6fe01d6SVladimir Oltean 2605f6fe01d6SVladimir Oltean eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); 2606f6fe01d6SVladimir Oltean ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); 2607f6fe01d6SVladimir Oltean } 2608f6fe01d6SVladimir Oltean 2609a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot) 2610a556c76aSAlexandre Belloni { 2611a556c76aSAlexandre Belloni char queue_name[32]; 261221468199SVladimir Oltean int i, ret; 261321468199SVladimir Oltean u32 port; 2614a556c76aSAlexandre Belloni 26153a77b593SVladimir Oltean if (ocelot->ops->reset) { 26163a77b593SVladimir Oltean ret = ocelot->ops->reset(ocelot); 26173a77b593SVladimir Oltean if (ret) { 26183a77b593SVladimir Oltean dev_err(ocelot->dev, "Switch reset failed\n"); 26193a77b593SVladimir Oltean return ret; 26203a77b593SVladimir Oltean } 26213a77b593SVladimir Oltean } 26223a77b593SVladimir Oltean 2623a556c76aSAlexandre Belloni ocelot->stats = devm_kcalloc(ocelot->dev, 2624a556c76aSAlexandre Belloni ocelot->num_phys_ports * ocelot->num_stats, 2625a556c76aSAlexandre Belloni sizeof(u64), GFP_KERNEL); 2626a556c76aSAlexandre Belloni if (!ocelot->stats) 2627a556c76aSAlexandre Belloni return -ENOMEM; 2628a556c76aSAlexandre Belloni 2629a556c76aSAlexandre Belloni mutex_init(&ocelot->stats_lock); 26304e3b0468SAntoine Tenart mutex_init(&ocelot->ptp_lock); 26312468346cSVladimir Oltean mutex_init(&ocelot->mact_lock); 26328abe1970SVladimir Oltean mutex_init(&ocelot->fwd_domain_lock); 26334e3b0468SAntoine Tenart spin_lock_init(&ocelot->ptp_clock_lock); 263452849bcfSVladimir Oltean spin_lock_init(&ocelot->ts_id_lock); 2635a556c76aSAlexandre Belloni snprintf(queue_name, sizeof(queue_name), "%s-stats", 2636a556c76aSAlexandre Belloni dev_name(ocelot->dev)); 2637a556c76aSAlexandre Belloni ocelot->stats_queue = create_singlethread_workqueue(queue_name); 2638a556c76aSAlexandre Belloni if (!ocelot->stats_queue) 2639a556c76aSAlexandre Belloni return -ENOMEM; 2640a556c76aSAlexandre Belloni 2641ca0b272bSVladimir Oltean ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); 2642ca0b272bSVladimir Oltean if (!ocelot->owq) { 2643ca0b272bSVladimir Oltean destroy_workqueue(ocelot->stats_queue); 2644ca0b272bSVladimir Oltean return -ENOMEM; 2645ca0b272bSVladimir Oltean } 2646ca0b272bSVladimir Oltean 26472b120ddeSClaudiu Manoil INIT_LIST_HEAD(&ocelot->multicast); 2648e5d1f896SVladimir Oltean INIT_LIST_HEAD(&ocelot->pgids); 264990e0aa8dSVladimir Oltean INIT_LIST_HEAD(&ocelot->vlans); 2650f6fe01d6SVladimir Oltean ocelot_detect_features(ocelot); 2651a556c76aSAlexandre Belloni ocelot_mact_init(ocelot); 2652a556c76aSAlexandre Belloni ocelot_vlan_init(ocelot); 2653aae4e500SVladimir Oltean ocelot_vcap_init(ocelot); 26542d44b097SVladimir Oltean ocelot_cpu_port_init(ocelot); 2655a556c76aSAlexandre Belloni 265623e2c506SXiaoliang Yang if (ocelot->ops->psfp_init) 265723e2c506SXiaoliang Yang ocelot->ops->psfp_init(ocelot); 265823e2c506SXiaoliang Yang 2659a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 2660a556c76aSAlexandre Belloni /* Clear all counters (5 groups) */ 2661a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 2662a556c76aSAlexandre Belloni SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 2663a556c76aSAlexandre Belloni SYS_STAT_CFG); 2664a556c76aSAlexandre Belloni } 2665a556c76aSAlexandre Belloni 2666a556c76aSAlexandre Belloni /* Only use S-Tag */ 2667a556c76aSAlexandre Belloni ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 2668a556c76aSAlexandre Belloni 2669a556c76aSAlexandre Belloni /* Aggregation mode */ 2670a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 2671a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_DMAC_ENA | 2672a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 2673f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | 2674f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | 2675f79c20c8SVladimir Oltean ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, 2676f79c20c8SVladimir Oltean ANA_AGGR_CFG); 2677a556c76aSAlexandre Belloni 2678a556c76aSAlexandre Belloni /* Set MAC age time to default value. The entry is aged after 2679a556c76aSAlexandre Belloni * 2*AGE_PERIOD 2680a556c76aSAlexandre Belloni */ 2681a556c76aSAlexandre Belloni ocelot_write(ocelot, 2682a556c76aSAlexandre Belloni ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 2683a556c76aSAlexandre Belloni ANA_AUTOAGE); 2684a556c76aSAlexandre Belloni 2685a556c76aSAlexandre Belloni /* Disable learning for frames discarded by VLAN ingress filtering */ 2686a556c76aSAlexandre Belloni regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 2687a556c76aSAlexandre Belloni 2688a556c76aSAlexandre Belloni /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 2689a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 2690a556c76aSAlexandre Belloni SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 2691a556c76aSAlexandre Belloni 2692a556c76aSAlexandre Belloni /* Setup flooding PGIDs */ 2693edd2410bSVladimir Oltean for (i = 0; i < ocelot->num_flooding_pgids; i++) 2694a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 2695b360d94fSVladimir Oltean ANA_FLOODING_FLD_BROADCAST(PGID_BC) | 2696a556c76aSAlexandre Belloni ANA_FLOODING_FLD_UNICAST(PGID_UC), 2697edd2410bSVladimir Oltean ANA_FLOODING, i); 2698a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 2699a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 2700a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 2701a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 2702a556c76aSAlexandre Belloni ANA_FLOODING_IPMC); 2703a556c76aSAlexandre Belloni 2704a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 2705a556c76aSAlexandre Belloni /* Transmit the frame to the local port. */ 2706a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 2707a556c76aSAlexandre Belloni /* Do not forward BPDU frames to the front ports. */ 2708a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, 2709a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 2710a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG, 2711a556c76aSAlexandre Belloni port); 2712a556c76aSAlexandre Belloni /* Ensure bridging is disabled */ 2713a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 2714a556c76aSAlexandre Belloni } 2715a556c76aSAlexandre Belloni 271696b029b0SVladimir Oltean for_each_nonreserved_multicast_dest_pgid(ocelot, i) { 2717a556c76aSAlexandre Belloni u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 2718a556c76aSAlexandre Belloni 2719a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 2720a556c76aSAlexandre Belloni } 2721ebb1bb40SHoratiu Vultur 2722ebb1bb40SHoratiu Vultur ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE); 2723ebb1bb40SHoratiu Vultur 2724b360d94fSVladimir Oltean /* Allow broadcast and unknown L2 multicast to the CPU. */ 2725b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2726b360d94fSVladimir Oltean ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2727a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_MC); 2728b360d94fSVladimir Oltean ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2729b360d94fSVladimir Oltean ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2730b360d94fSVladimir Oltean ANA_PGID_PGID, PGID_BC); 2731a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 2732a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 2733a556c76aSAlexandre Belloni 2734a556c76aSAlexandre Belloni /* Allow manual injection via DEVCPU_QS registers, and byte swap these 2735a556c76aSAlexandre Belloni * registers endianness. 2736a556c76aSAlexandre Belloni */ 2737a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 2738a556c76aSAlexandre Belloni QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 2739a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 2740a556c76aSAlexandre Belloni QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 2741a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 2742a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LRN(2) | 2743a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 2744a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 2745a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 2746a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 2747a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 2748a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IGMP(6) | 2749a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 2750a556c76aSAlexandre Belloni for (i = 0; i < 16; i++) 2751a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 2752a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 2753a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG, i); 2754a556c76aSAlexandre Belloni 27551e1caa97SClaudiu Manoil INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 2756a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 2757a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 27584e3b0468SAntoine Tenart 2759a556c76aSAlexandre Belloni return 0; 2760a556c76aSAlexandre Belloni } 2761a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init); 2762a556c76aSAlexandre Belloni 2763a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot) 2764a556c76aSAlexandre Belloni { 2765c5d13969SClaudiu Manoil cancel_delayed_work(&ocelot->stats_work); 2766a556c76aSAlexandre Belloni destroy_workqueue(ocelot->stats_queue); 2767ca0b272bSVladimir Oltean destroy_workqueue(ocelot->owq); 2768a556c76aSAlexandre Belloni mutex_destroy(&ocelot->stats_lock); 2769a556c76aSAlexandre Belloni } 2770a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit); 2771a556c76aSAlexandre Belloni 2772e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port) 2773e5fb512dSVladimir Oltean { 2774e5fb512dSVladimir Oltean struct ocelot_port *ocelot_port = ocelot->ports[port]; 2775e5fb512dSVladimir Oltean 2776e5fb512dSVladimir Oltean skb_queue_purge(&ocelot_port->tx_skbs); 2777e5fb512dSVladimir Oltean } 2778e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port); 2779e5fb512dSVladimir Oltean 2780a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL"); 2781