1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2a556c76aSAlexandre Belloni /* 3a556c76aSAlexandre Belloni * Microsemi Ocelot Switch driver 4a556c76aSAlexandre Belloni * 5a556c76aSAlexandre Belloni * Copyright (c) 2017 Microsemi Corporation 6a556c76aSAlexandre Belloni */ 7a556c76aSAlexandre Belloni #include <linux/etherdevice.h> 8a556c76aSAlexandre Belloni #include <linux/ethtool.h> 9a556c76aSAlexandre Belloni #include <linux/if_bridge.h> 10a556c76aSAlexandre Belloni #include <linux/if_ether.h> 11a556c76aSAlexandre Belloni #include <linux/if_vlan.h> 12a556c76aSAlexandre Belloni #include <linux/interrupt.h> 13a556c76aSAlexandre Belloni #include <linux/kernel.h> 14a556c76aSAlexandre Belloni #include <linux/module.h> 15a556c76aSAlexandre Belloni #include <linux/netdevice.h> 16a556c76aSAlexandre Belloni #include <linux/phy.h> 17a556c76aSAlexandre Belloni #include <linux/skbuff.h> 18639c1b26SSteen Hegelund #include <linux/iopoll.h> 19a556c76aSAlexandre Belloni #include <net/arp.h> 20a556c76aSAlexandre Belloni #include <net/netevent.h> 21a556c76aSAlexandre Belloni #include <net/rtnetlink.h> 22a556c76aSAlexandre Belloni #include <net/switchdev.h> 23a556c76aSAlexandre Belloni 24a556c76aSAlexandre Belloni #include "ocelot.h" 25a556c76aSAlexandre Belloni 26639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10 27639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000 28639c1b26SSteen Hegelund 29a556c76aSAlexandre Belloni /* MAC table entry types. 30a556c76aSAlexandre Belloni * ENTRYTYPE_NORMAL is subject to aging. 31a556c76aSAlexandre Belloni * ENTRYTYPE_LOCKED is not subject to aging. 32a556c76aSAlexandre Belloni * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast. 33a556c76aSAlexandre Belloni * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast. 34a556c76aSAlexandre Belloni */ 35a556c76aSAlexandre Belloni enum macaccess_entry_type { 36a556c76aSAlexandre Belloni ENTRYTYPE_NORMAL = 0, 37a556c76aSAlexandre Belloni ENTRYTYPE_LOCKED, 38a556c76aSAlexandre Belloni ENTRYTYPE_MACv4, 39a556c76aSAlexandre Belloni ENTRYTYPE_MACv6, 40a556c76aSAlexandre Belloni }; 41a556c76aSAlexandre Belloni 42a556c76aSAlexandre Belloni struct ocelot_mact_entry { 43a556c76aSAlexandre Belloni u8 mac[ETH_ALEN]; 44a556c76aSAlexandre Belloni u16 vid; 45a556c76aSAlexandre Belloni enum macaccess_entry_type type; 46a556c76aSAlexandre Belloni }; 47a556c76aSAlexandre Belloni 48639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) 49639c1b26SSteen Hegelund { 50639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_MACACCESS); 51639c1b26SSteen Hegelund } 52639c1b26SSteen Hegelund 53a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) 54a556c76aSAlexandre Belloni { 55639c1b26SSteen Hegelund u32 val; 56a556c76aSAlexandre Belloni 57639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_mact_read_macaccess, 58639c1b26SSteen Hegelund ocelot, val, 59639c1b26SSteen Hegelund (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == 60639c1b26SSteen Hegelund MACACCESS_CMD_IDLE, 61639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 62a556c76aSAlexandre Belloni } 63a556c76aSAlexandre Belloni 64a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot, 65a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 66a556c76aSAlexandre Belloni unsigned int vid) 67a556c76aSAlexandre Belloni { 68a556c76aSAlexandre Belloni u32 macl = 0, mach = 0; 69a556c76aSAlexandre Belloni 70a556c76aSAlexandre Belloni /* Set the MAC address to handle and the vlan associated in a format 71a556c76aSAlexandre Belloni * understood by the hardware. 72a556c76aSAlexandre Belloni */ 73a556c76aSAlexandre Belloni mach |= vid << 16; 74a556c76aSAlexandre Belloni mach |= mac[0] << 8; 75a556c76aSAlexandre Belloni mach |= mac[1] << 0; 76a556c76aSAlexandre Belloni macl |= mac[2] << 24; 77a556c76aSAlexandre Belloni macl |= mac[3] << 16; 78a556c76aSAlexandre Belloni macl |= mac[4] << 8; 79a556c76aSAlexandre Belloni macl |= mac[5] << 0; 80a556c76aSAlexandre Belloni 81a556c76aSAlexandre Belloni ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); 82a556c76aSAlexandre Belloni ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); 83a556c76aSAlexandre Belloni 84a556c76aSAlexandre Belloni } 85a556c76aSAlexandre Belloni 86a556c76aSAlexandre Belloni static int ocelot_mact_learn(struct ocelot *ocelot, int port, 87a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 88a556c76aSAlexandre Belloni unsigned int vid, 89a556c76aSAlexandre Belloni enum macaccess_entry_type type) 90a556c76aSAlexandre Belloni { 91a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 92a556c76aSAlexandre Belloni 93a556c76aSAlexandre Belloni /* Issue a write command */ 94a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID | 95a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_DEST_IDX(port) | 96a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_ENTRYTYPE(type) | 97a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN), 98a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 99a556c76aSAlexandre Belloni 100a556c76aSAlexandre Belloni return ocelot_mact_wait_for_completion(ocelot); 101a556c76aSAlexandre Belloni } 102a556c76aSAlexandre Belloni 103a556c76aSAlexandre Belloni static int ocelot_mact_forget(struct ocelot *ocelot, 104a556c76aSAlexandre Belloni const unsigned char mac[ETH_ALEN], 105a556c76aSAlexandre Belloni unsigned int vid) 106a556c76aSAlexandre Belloni { 107a556c76aSAlexandre Belloni ocelot_mact_select(ocelot, mac, vid); 108a556c76aSAlexandre Belloni 109a556c76aSAlexandre Belloni /* Issue a forget command */ 110a556c76aSAlexandre Belloni ocelot_write(ocelot, 111a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), 112a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 113a556c76aSAlexandre Belloni 114a556c76aSAlexandre Belloni return ocelot_mact_wait_for_completion(ocelot); 115a556c76aSAlexandre Belloni } 116a556c76aSAlexandre Belloni 117a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot) 118a556c76aSAlexandre Belloni { 119a556c76aSAlexandre Belloni /* Configure the learning mode entries attributes: 120a556c76aSAlexandre Belloni * - Do not copy the frame to the CPU extraction queues. 121a556c76aSAlexandre Belloni * - Use the vlan and mac_cpoy for dmac lookup. 122a556c76aSAlexandre Belloni */ 123a556c76aSAlexandre Belloni ocelot_rmw(ocelot, 0, 124a556c76aSAlexandre Belloni ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS 125a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_FWD_KILL 126a556c76aSAlexandre Belloni | ANA_AGENCTRL_LEARN_IGNORE_VLAN, 127a556c76aSAlexandre Belloni ANA_AGENCTRL); 128a556c76aSAlexandre Belloni 129a556c76aSAlexandre Belloni /* Clear the MAC table */ 130a556c76aSAlexandre Belloni ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); 131a556c76aSAlexandre Belloni } 132a556c76aSAlexandre Belloni 133639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) 134639c1b26SSteen Hegelund { 135639c1b26SSteen Hegelund return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); 136639c1b26SSteen Hegelund } 137639c1b26SSteen Hegelund 138a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) 139a556c76aSAlexandre Belloni { 140639c1b26SSteen Hegelund u32 val; 141a556c76aSAlexandre Belloni 142639c1b26SSteen Hegelund return readx_poll_timeout(ocelot_vlant_read_vlanaccess, 143639c1b26SSteen Hegelund ocelot, 144639c1b26SSteen Hegelund val, 145639c1b26SSteen Hegelund (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == 146639c1b26SSteen Hegelund ANA_TABLES_VLANACCESS_CMD_IDLE, 147639c1b26SSteen Hegelund TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 148a556c76aSAlexandre Belloni } 149a556c76aSAlexandre Belloni 1507142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) 1517142529fSAntoine Tenart { 1527142529fSAntoine Tenart /* Select the VID to configure */ 1537142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), 1547142529fSAntoine Tenart ANA_TABLES_VLANTIDX); 1557142529fSAntoine Tenart /* Set the vlan port members mask and issue a write command */ 1567142529fSAntoine Tenart ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | 1577142529fSAntoine Tenart ANA_TABLES_VLANACCESS_CMD_WRITE, 1587142529fSAntoine Tenart ANA_TABLES_VLANACCESS); 1597142529fSAntoine Tenart 1607142529fSAntoine Tenart return ocelot_vlant_wait_for_completion(ocelot); 1617142529fSAntoine Tenart } 1627142529fSAntoine Tenart 1637142529fSAntoine Tenart static void ocelot_vlan_mode(struct ocelot_port *port, 1647142529fSAntoine Tenart netdev_features_t features) 1657142529fSAntoine Tenart { 1667142529fSAntoine Tenart struct ocelot *ocelot = port->ocelot; 1677142529fSAntoine Tenart u8 p = port->chip_port; 1687142529fSAntoine Tenart u32 val; 1697142529fSAntoine Tenart 1707142529fSAntoine Tenart /* Filtering */ 1717142529fSAntoine Tenart val = ocelot_read(ocelot, ANA_VLANMASK); 1727142529fSAntoine Tenart if (features & NETIF_F_HW_VLAN_CTAG_FILTER) 1737142529fSAntoine Tenart val |= BIT(p); 1747142529fSAntoine Tenart else 1757142529fSAntoine Tenart val &= ~BIT(p); 1767142529fSAntoine Tenart ocelot_write(ocelot, val, ANA_VLANMASK); 1777142529fSAntoine Tenart } 1787142529fSAntoine Tenart 1797142529fSAntoine Tenart static void ocelot_vlan_port_apply(struct ocelot *ocelot, 1807142529fSAntoine Tenart struct ocelot_port *port) 1817142529fSAntoine Tenart { 1827142529fSAntoine Tenart u32 val; 1837142529fSAntoine Tenart 1847142529fSAntoine Tenart /* Ingress clasification (ANA_PORT_VLAN_CFG) */ 1857142529fSAntoine Tenart /* Default vlan to clasify for untagged frames (may be zero) */ 1867142529fSAntoine Tenart val = ANA_PORT_VLAN_CFG_VLAN_VID(port->pvid); 1877142529fSAntoine Tenart if (port->vlan_aware) 1887142529fSAntoine Tenart val |= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 1897142529fSAntoine Tenart ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); 1907142529fSAntoine Tenart 1917142529fSAntoine Tenart ocelot_rmw_gix(ocelot, val, 1927142529fSAntoine Tenart ANA_PORT_VLAN_CFG_VLAN_VID_M | 1937142529fSAntoine Tenart ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 1947142529fSAntoine Tenart ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, 1957142529fSAntoine Tenart ANA_PORT_VLAN_CFG, port->chip_port); 1967142529fSAntoine Tenart 1977142529fSAntoine Tenart /* Drop frames with multicast source address */ 1987142529fSAntoine Tenart val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA; 1997142529fSAntoine Tenart if (port->vlan_aware && !port->vid) 2007142529fSAntoine Tenart /* If port is vlan-aware and tagged, drop untagged and priority 2017142529fSAntoine Tenart * tagged frames. 2027142529fSAntoine Tenart */ 2037142529fSAntoine Tenart val |= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA | 2047142529fSAntoine Tenart ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 2057142529fSAntoine Tenart ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; 2067142529fSAntoine Tenart ocelot_write_gix(ocelot, val, ANA_PORT_DROP_CFG, port->chip_port); 2077142529fSAntoine Tenart 2087142529fSAntoine Tenart /* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q. */ 2097142529fSAntoine Tenart val = REW_TAG_CFG_TAG_TPID_CFG(0); 2107142529fSAntoine Tenart 2117142529fSAntoine Tenart if (port->vlan_aware) { 2127142529fSAntoine Tenart if (port->vid) 2137142529fSAntoine Tenart /* Tag all frames except when VID == DEFAULT_VLAN */ 2147142529fSAntoine Tenart val |= REW_TAG_CFG_TAG_CFG(1); 2157142529fSAntoine Tenart else 2167142529fSAntoine Tenart /* Tag all frames */ 2177142529fSAntoine Tenart val |= REW_TAG_CFG_TAG_CFG(3); 2187142529fSAntoine Tenart } 2197142529fSAntoine Tenart ocelot_rmw_gix(ocelot, val, 2207142529fSAntoine Tenart REW_TAG_CFG_TAG_TPID_CFG_M | 2217142529fSAntoine Tenart REW_TAG_CFG_TAG_CFG_M, 2227142529fSAntoine Tenart REW_TAG_CFG, port->chip_port); 2237142529fSAntoine Tenart 2247142529fSAntoine Tenart /* Set default VLAN and tag type to 8021Q. */ 2257142529fSAntoine Tenart val = REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q) | 2267142529fSAntoine Tenart REW_PORT_VLAN_CFG_PORT_VID(port->vid); 2277142529fSAntoine Tenart ocelot_rmw_gix(ocelot, val, 2287142529fSAntoine Tenart REW_PORT_VLAN_CFG_PORT_TPID_M | 2297142529fSAntoine Tenart REW_PORT_VLAN_CFG_PORT_VID_M, 2307142529fSAntoine Tenart REW_PORT_VLAN_CFG, port->chip_port); 2317142529fSAntoine Tenart } 2327142529fSAntoine Tenart 2337142529fSAntoine Tenart static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid, 2347142529fSAntoine Tenart bool untagged) 2357142529fSAntoine Tenart { 2367142529fSAntoine Tenart struct ocelot_port *port = netdev_priv(dev); 2377142529fSAntoine Tenart struct ocelot *ocelot = port->ocelot; 2387142529fSAntoine Tenart int ret; 2397142529fSAntoine Tenart 2407142529fSAntoine Tenart /* Add the port MAC address to with the right VLAN information */ 2417142529fSAntoine Tenart ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid, 2427142529fSAntoine Tenart ENTRYTYPE_LOCKED); 2437142529fSAntoine Tenart 2447142529fSAntoine Tenart /* Make the port a member of the VLAN */ 2457142529fSAntoine Tenart ocelot->vlan_mask[vid] |= BIT(port->chip_port); 2467142529fSAntoine Tenart ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 2477142529fSAntoine Tenart if (ret) 2487142529fSAntoine Tenart return ret; 2497142529fSAntoine Tenart 2507142529fSAntoine Tenart /* Default ingress vlan classification */ 2517142529fSAntoine Tenart if (pvid) 2527142529fSAntoine Tenart port->pvid = vid; 2537142529fSAntoine Tenart 2547142529fSAntoine Tenart /* Untagged egress vlan clasification */ 2557142529fSAntoine Tenart if (untagged) 2567142529fSAntoine Tenart port->vid = vid; 2577142529fSAntoine Tenart 2587142529fSAntoine Tenart ocelot_vlan_port_apply(ocelot, port); 2597142529fSAntoine Tenart 2607142529fSAntoine Tenart return 0; 2617142529fSAntoine Tenart } 2627142529fSAntoine Tenart 2637142529fSAntoine Tenart static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid) 2647142529fSAntoine Tenart { 2657142529fSAntoine Tenart struct ocelot_port *port = netdev_priv(dev); 2667142529fSAntoine Tenart struct ocelot *ocelot = port->ocelot; 2677142529fSAntoine Tenart int ret; 2687142529fSAntoine Tenart 2697142529fSAntoine Tenart /* 8021q removes VID 0 on module unload for all interfaces 2707142529fSAntoine Tenart * with VLAN filtering feature. We need to keep it to receive 2717142529fSAntoine Tenart * untagged traffic. 2727142529fSAntoine Tenart */ 2737142529fSAntoine Tenart if (vid == 0) 2747142529fSAntoine Tenart return 0; 2757142529fSAntoine Tenart 2767142529fSAntoine Tenart /* Del the port MAC address to with the right VLAN information */ 2777142529fSAntoine Tenart ocelot_mact_forget(ocelot, dev->dev_addr, vid); 2787142529fSAntoine Tenart 2797142529fSAntoine Tenart /* Stop the port from being a member of the vlan */ 2807142529fSAntoine Tenart ocelot->vlan_mask[vid] &= ~BIT(port->chip_port); 2817142529fSAntoine Tenart ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 2827142529fSAntoine Tenart if (ret) 2837142529fSAntoine Tenart return ret; 2847142529fSAntoine Tenart 2857142529fSAntoine Tenart /* Ingress */ 2867142529fSAntoine Tenart if (port->pvid == vid) 2877142529fSAntoine Tenart port->pvid = 0; 2887142529fSAntoine Tenart 2897142529fSAntoine Tenart /* Egress */ 2907142529fSAntoine Tenart if (port->vid == vid) 2917142529fSAntoine Tenart port->vid = 0; 2927142529fSAntoine Tenart 2937142529fSAntoine Tenart ocelot_vlan_port_apply(ocelot, port); 2947142529fSAntoine Tenart 2957142529fSAntoine Tenart return 0; 2967142529fSAntoine Tenart } 2977142529fSAntoine Tenart 298a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot) 299a556c76aSAlexandre Belloni { 3007142529fSAntoine Tenart u16 port, vid; 3017142529fSAntoine Tenart 302a556c76aSAlexandre Belloni /* Clear VLAN table, by default all ports are members of all VLANs */ 303a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, 304a556c76aSAlexandre Belloni ANA_TABLES_VLANACCESS); 305a556c76aSAlexandre Belloni ocelot_vlant_wait_for_completion(ocelot); 3067142529fSAntoine Tenart 3077142529fSAntoine Tenart /* Configure the port VLAN memberships */ 3087142529fSAntoine Tenart for (vid = 1; vid < VLAN_N_VID; vid++) { 3097142529fSAntoine Tenart ocelot->vlan_mask[vid] = 0; 3107142529fSAntoine Tenart ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 3117142529fSAntoine Tenart } 3127142529fSAntoine Tenart 3137142529fSAntoine Tenart /* Because VLAN filtering is enabled, we need VID 0 to get untagged 3147142529fSAntoine Tenart * traffic. It is added automatically if 8021q module is loaded, but 3157142529fSAntoine Tenart * we can't rely on it since module may be not loaded. 3167142529fSAntoine Tenart */ 3177142529fSAntoine Tenart ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0); 3187142529fSAntoine Tenart ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]); 3197142529fSAntoine Tenart 3207142529fSAntoine Tenart /* Configure the CPU port to be VLAN aware */ 3217142529fSAntoine Tenart ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) | 3227142529fSAntoine Tenart ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 3237142529fSAntoine Tenart ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 3247142529fSAntoine Tenart ANA_PORT_VLAN_CFG, ocelot->num_phys_ports); 3257142529fSAntoine Tenart 3267142529fSAntoine Tenart /* Set vlan ingress filter mask to all ports but the CPU port by 3277142529fSAntoine Tenart * default. 3287142529fSAntoine Tenart */ 3297142529fSAntoine Tenart ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK); 3307142529fSAntoine Tenart 3317142529fSAntoine Tenart for (port = 0; port < ocelot->num_phys_ports; port++) { 3327142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); 3337142529fSAntoine Tenart ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); 3347142529fSAntoine Tenart } 335a556c76aSAlexandre Belloni } 336a556c76aSAlexandre Belloni 337a556c76aSAlexandre Belloni /* Watermark encode 338a556c76aSAlexandre Belloni * Bit 8: Unit; 0:1, 1:16 339a556c76aSAlexandre Belloni * Bit 7-0: Value to be multiplied with unit 340a556c76aSAlexandre Belloni */ 341a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value) 342a556c76aSAlexandre Belloni { 343a556c76aSAlexandre Belloni if (value >= BIT(8)) 344a556c76aSAlexandre Belloni return BIT(8) | (value / 16); 345a556c76aSAlexandre Belloni 346a556c76aSAlexandre Belloni return value; 347a556c76aSAlexandre Belloni } 348a556c76aSAlexandre Belloni 349a556c76aSAlexandre Belloni static void ocelot_port_adjust_link(struct net_device *dev) 350a556c76aSAlexandre Belloni { 351a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 352a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 353a556c76aSAlexandre Belloni u8 p = port->chip_port; 354a556c76aSAlexandre Belloni int speed, atop_wm, mode = 0; 355a556c76aSAlexandre Belloni 356a556c76aSAlexandre Belloni switch (dev->phydev->speed) { 357a556c76aSAlexandre Belloni case SPEED_10: 358a556c76aSAlexandre Belloni speed = OCELOT_SPEED_10; 359a556c76aSAlexandre Belloni break; 360a556c76aSAlexandre Belloni case SPEED_100: 361a556c76aSAlexandre Belloni speed = OCELOT_SPEED_100; 362a556c76aSAlexandre Belloni break; 363a556c76aSAlexandre Belloni case SPEED_1000: 364a556c76aSAlexandre Belloni speed = OCELOT_SPEED_1000; 365a556c76aSAlexandre Belloni mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 366a556c76aSAlexandre Belloni break; 367a556c76aSAlexandre Belloni case SPEED_2500: 368a556c76aSAlexandre Belloni speed = OCELOT_SPEED_2500; 369a556c76aSAlexandre Belloni mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 370a556c76aSAlexandre Belloni break; 371a556c76aSAlexandre Belloni default: 372a556c76aSAlexandre Belloni netdev_err(dev, "Unsupported PHY speed: %d\n", 373a556c76aSAlexandre Belloni dev->phydev->speed); 374a556c76aSAlexandre Belloni return; 375a556c76aSAlexandre Belloni } 376a556c76aSAlexandre Belloni 377a556c76aSAlexandre Belloni phy_print_status(dev->phydev); 378a556c76aSAlexandre Belloni 379a556c76aSAlexandre Belloni if (!dev->phydev->link) 380a556c76aSAlexandre Belloni return; 381a556c76aSAlexandre Belloni 382a556c76aSAlexandre Belloni /* Only full duplex supported for now */ 383a556c76aSAlexandre Belloni ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA | 384a556c76aSAlexandre Belloni mode, DEV_MAC_MODE_CFG); 385a556c76aSAlexandre Belloni 386a556c76aSAlexandre Belloni /* Set MAC IFG Gaps 387a556c76aSAlexandre Belloni * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 388a556c76aSAlexandre Belloni * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 389a556c76aSAlexandre Belloni */ 390a556c76aSAlexandre Belloni ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG); 391a556c76aSAlexandre Belloni 392a556c76aSAlexandre Belloni /* Load seed (0) and set MAC HDX late collision */ 393a556c76aSAlexandre Belloni ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 394a556c76aSAlexandre Belloni DEV_MAC_HDX_CFG_SEED_LOAD, 395a556c76aSAlexandre Belloni DEV_MAC_HDX_CFG); 396a556c76aSAlexandre Belloni mdelay(1); 397a556c76aSAlexandre Belloni ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 398a556c76aSAlexandre Belloni DEV_MAC_HDX_CFG); 399a556c76aSAlexandre Belloni 400a556c76aSAlexandre Belloni /* Disable HDX fast control */ 401a556c76aSAlexandre Belloni ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC); 402a556c76aSAlexandre Belloni 403a556c76aSAlexandre Belloni /* SGMII only for now */ 404a556c76aSAlexandre Belloni ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG); 405a556c76aSAlexandre Belloni ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG); 406a556c76aSAlexandre Belloni 407a556c76aSAlexandre Belloni /* Enable PCS */ 408a556c76aSAlexandre Belloni ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG); 409a556c76aSAlexandre Belloni 410a556c76aSAlexandre Belloni /* No aneg on SGMII */ 411a556c76aSAlexandre Belloni ocelot_port_writel(port, 0, PCS1G_ANEG_CFG); 412a556c76aSAlexandre Belloni 413a556c76aSAlexandre Belloni /* No loopback */ 414a556c76aSAlexandre Belloni ocelot_port_writel(port, 0, PCS1G_LB_CFG); 415a556c76aSAlexandre Belloni 416a556c76aSAlexandre Belloni /* Set Max Length and maximum tags allowed */ 417a556c76aSAlexandre Belloni ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG); 418a556c76aSAlexandre Belloni ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 419a556c76aSAlexandre Belloni DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 420a556c76aSAlexandre Belloni DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 421a556c76aSAlexandre Belloni DEV_MAC_TAGS_CFG); 422a556c76aSAlexandre Belloni 423a556c76aSAlexandre Belloni /* Enable MAC module */ 424a556c76aSAlexandre Belloni ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA | 425a556c76aSAlexandre Belloni DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 426a556c76aSAlexandre Belloni 427a556c76aSAlexandre Belloni /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of 428a556c76aSAlexandre Belloni * reset */ 429a556c76aSAlexandre Belloni ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed), 430a556c76aSAlexandre Belloni DEV_CLOCK_CFG); 431a556c76aSAlexandre Belloni 432a556c76aSAlexandre Belloni /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 433a556c76aSAlexandre Belloni ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 434a556c76aSAlexandre Belloni ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG); 435a556c76aSAlexandre Belloni 436a556c76aSAlexandre Belloni /* No PFC */ 437a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed), 438a556c76aSAlexandre Belloni ANA_PFC_PFC_CFG, p); 439a556c76aSAlexandre Belloni 440a556c76aSAlexandre Belloni /* Set Pause WM hysteresis 441a556c76aSAlexandre Belloni * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ 442a556c76aSAlexandre Belloni * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ 443a556c76aSAlexandre Belloni */ 444a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA | 445a556c76aSAlexandre Belloni SYS_PAUSE_CFG_PAUSE_STOP(101) | 446a556c76aSAlexandre Belloni SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p); 447a556c76aSAlexandre Belloni 448a556c76aSAlexandre Belloni /* Core: Enable port for frame transfer */ 449a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE | 450a556c76aSAlexandre Belloni QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) | 451a556c76aSAlexandre Belloni QSYS_SWITCH_PORT_MODE_PORT_ENA, 452a556c76aSAlexandre Belloni QSYS_SWITCH_PORT_MODE, p); 453a556c76aSAlexandre Belloni 454a556c76aSAlexandre Belloni /* Flow control */ 455a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 456a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA | 457a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_ZERO_PAUSE_ENA | 458a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 459a556c76aSAlexandre Belloni SYS_MAC_FC_CFG_FC_LINK_SPEED(speed), 460a556c76aSAlexandre Belloni SYS_MAC_FC_CFG, p); 461a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p); 462a556c76aSAlexandre Belloni 463a556c76aSAlexandre Belloni /* Tail dropping watermark */ 464a556c76aSAlexandre Belloni atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ; 465a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN), 466a556c76aSAlexandre Belloni SYS_ATOP, p); 467a556c76aSAlexandre Belloni ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG); 468a556c76aSAlexandre Belloni } 469a556c76aSAlexandre Belloni 470a556c76aSAlexandre Belloni static int ocelot_port_open(struct net_device *dev) 471a556c76aSAlexandre Belloni { 472a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 473a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 474a556c76aSAlexandre Belloni int err; 475a556c76aSAlexandre Belloni 476a556c76aSAlexandre Belloni /* Enable receiving frames on the port, and activate auto-learning of 477a556c76aSAlexandre Belloni * MAC addresses. 478a556c76aSAlexandre Belloni */ 479a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 480a556c76aSAlexandre Belloni ANA_PORT_PORT_CFG_RECV_ENA | 481a556c76aSAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port), 482a556c76aSAlexandre Belloni ANA_PORT_PORT_CFG, port->chip_port); 483a556c76aSAlexandre Belloni 48471e32a20SQuentin Schulz if (port->serdes) { 485c8fe6d7fSGrygorii Strashko err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET, 486c8fe6d7fSGrygorii Strashko port->phy_mode); 48771e32a20SQuentin Schulz if (err) { 48871e32a20SQuentin Schulz netdev_err(dev, "Could not set mode of SerDes\n"); 48971e32a20SQuentin Schulz return err; 49071e32a20SQuentin Schulz } 49171e32a20SQuentin Schulz } 49271e32a20SQuentin Schulz 493a556c76aSAlexandre Belloni err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link, 49471e32a20SQuentin Schulz port->phy_mode); 495a556c76aSAlexandre Belloni if (err) { 496a556c76aSAlexandre Belloni netdev_err(dev, "Could not attach to PHY\n"); 497a556c76aSAlexandre Belloni return err; 498a556c76aSAlexandre Belloni } 499a556c76aSAlexandre Belloni 500a556c76aSAlexandre Belloni dev->phydev = port->phy; 501a556c76aSAlexandre Belloni 502a556c76aSAlexandre Belloni phy_attached_info(port->phy); 503a556c76aSAlexandre Belloni phy_start(port->phy); 504a556c76aSAlexandre Belloni return 0; 505a556c76aSAlexandre Belloni } 506a556c76aSAlexandre Belloni 507a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev) 508a556c76aSAlexandre Belloni { 509a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 510a556c76aSAlexandre Belloni 511a556c76aSAlexandre Belloni phy_disconnect(port->phy); 512a556c76aSAlexandre Belloni 513a556c76aSAlexandre Belloni dev->phydev = NULL; 514a556c76aSAlexandre Belloni 515a556c76aSAlexandre Belloni ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG); 516a556c76aSAlexandre Belloni ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA, 517a556c76aSAlexandre Belloni QSYS_SWITCH_PORT_MODE, port->chip_port); 518a556c76aSAlexandre Belloni return 0; 519a556c76aSAlexandre Belloni } 520a556c76aSAlexandre Belloni 521a556c76aSAlexandre Belloni /* Generate the IFH for frame injection 522a556c76aSAlexandre Belloni * 523a556c76aSAlexandre Belloni * The IFH is a 128bit-value 524a556c76aSAlexandre Belloni * bit 127: bypass the analyzer processing 525a556c76aSAlexandre Belloni * bit 56-67: destination mask 526a556c76aSAlexandre Belloni * bit 28-29: pop_cnt: 3 disables all rewriting of the frame 527a556c76aSAlexandre Belloni * bit 20-27: cpu extraction queue mask 528a556c76aSAlexandre Belloni * bit 16: tag type 0: C-tag, 1: S-tag 529a556c76aSAlexandre Belloni * bit 0-11: VID 530a556c76aSAlexandre Belloni */ 531a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info) 532a556c76aSAlexandre Belloni { 533a556c76aSAlexandre Belloni ifh[0] = IFH_INJ_BYPASS; 53408d02364SAntoine Tenart ifh[1] = (0xf00 & info->port) >> 8; 535a556c76aSAlexandre Belloni ifh[2] = (0xff & info->port) << 24; 53608d02364SAntoine Tenart ifh[3] = (info->tag_type << 16) | info->vid; 537a556c76aSAlexandre Belloni 538a556c76aSAlexandre Belloni return 0; 539a556c76aSAlexandre Belloni } 540a556c76aSAlexandre Belloni 541a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev) 542a556c76aSAlexandre Belloni { 543a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 544a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 545a556c76aSAlexandre Belloni u32 val, ifh[IFH_LEN]; 546a556c76aSAlexandre Belloni struct frame_info info = {}; 547a556c76aSAlexandre Belloni u8 grp = 0; /* Send everything on CPU group 0 */ 548a556c76aSAlexandre Belloni unsigned int i, count, last; 549a556c76aSAlexandre Belloni 550a556c76aSAlexandre Belloni val = ocelot_read(ocelot, QS_INJ_STATUS); 551a556c76aSAlexandre Belloni if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) || 552a556c76aSAlexandre Belloni (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))) 553a556c76aSAlexandre Belloni return NETDEV_TX_BUSY; 554a556c76aSAlexandre Belloni 555a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 556a556c76aSAlexandre Belloni QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp); 557a556c76aSAlexandre Belloni 558a556c76aSAlexandre Belloni info.port = BIT(port->chip_port); 55908d02364SAntoine Tenart info.tag_type = IFH_TAG_TYPE_C; 56008d02364SAntoine Tenart info.vid = skb_vlan_tag_get(skb); 561a556c76aSAlexandre Belloni ocelot_gen_ifh(ifh, &info); 562a556c76aSAlexandre Belloni 563a556c76aSAlexandre Belloni for (i = 0; i < IFH_LEN; i++) 564c2cd650bSAntoine Tenart ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]), 565c2cd650bSAntoine Tenart QS_INJ_WR, grp); 566a556c76aSAlexandre Belloni 567a556c76aSAlexandre Belloni count = (skb->len + 3) / 4; 568a556c76aSAlexandre Belloni last = skb->len % 4; 569a556c76aSAlexandre Belloni for (i = 0; i < count; i++) { 570a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp); 571a556c76aSAlexandre Belloni } 572a556c76aSAlexandre Belloni 573a556c76aSAlexandre Belloni /* Add padding */ 574a556c76aSAlexandre Belloni while (i < (OCELOT_BUFFER_CELL_SZ / 4)) { 575a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 576a556c76aSAlexandre Belloni i++; 577a556c76aSAlexandre Belloni } 578a556c76aSAlexandre Belloni 579a556c76aSAlexandre Belloni /* Indicate EOF and valid bytes in last word */ 580a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 581a556c76aSAlexandre Belloni QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) | 582a556c76aSAlexandre Belloni QS_INJ_CTRL_EOF, 583a556c76aSAlexandre Belloni QS_INJ_CTRL, grp); 584a556c76aSAlexandre Belloni 585a556c76aSAlexandre Belloni /* Add dummy CRC */ 586a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 587a556c76aSAlexandre Belloni skb_tx_timestamp(skb); 588a556c76aSAlexandre Belloni 589a556c76aSAlexandre Belloni dev->stats.tx_packets++; 590a556c76aSAlexandre Belloni dev->stats.tx_bytes += skb->len; 591a556c76aSAlexandre Belloni dev_kfree_skb_any(skb); 592a556c76aSAlexandre Belloni 593a556c76aSAlexandre Belloni return NETDEV_TX_OK; 594a556c76aSAlexandre Belloni } 595a556c76aSAlexandre Belloni 596*40a1578dSClaudiu Manoil static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr) 597a556c76aSAlexandre Belloni { 598*40a1578dSClaudiu Manoil struct ocelot_port *port = netdev_priv(dev); 599a556c76aSAlexandre Belloni 600*40a1578dSClaudiu Manoil return ocelot_mact_forget(port->ocelot, addr, port->pvid); 601a556c76aSAlexandre Belloni } 602a556c76aSAlexandre Belloni 603*40a1578dSClaudiu Manoil static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr) 604a556c76aSAlexandre Belloni { 605*40a1578dSClaudiu Manoil struct ocelot_port *port = netdev_priv(dev); 606a556c76aSAlexandre Belloni 607*40a1578dSClaudiu Manoil return ocelot_mact_learn(port->ocelot, PGID_CPU, addr, port->pvid, 608a556c76aSAlexandre Belloni ENTRYTYPE_LOCKED); 609a556c76aSAlexandre Belloni } 610a556c76aSAlexandre Belloni 611a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev) 612a556c76aSAlexandre Belloni { 613a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 614a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 615a556c76aSAlexandre Belloni int i; 616a556c76aSAlexandre Belloni u32 val; 617a556c76aSAlexandre Belloni 618a556c76aSAlexandre Belloni /* This doesn't handle promiscuous mode because the bridge core is 619a556c76aSAlexandre Belloni * setting IFF_PROMISC on all slave interfaces and all frames would be 620a556c76aSAlexandre Belloni * forwarded to the CPU port. 621a556c76aSAlexandre Belloni */ 622a556c76aSAlexandre Belloni val = GENMASK(ocelot->num_phys_ports - 1, 0); 623a556c76aSAlexandre Belloni for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) 624a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 625a556c76aSAlexandre Belloni 626*40a1578dSClaudiu Manoil __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync); 627a556c76aSAlexandre Belloni } 628a556c76aSAlexandre Belloni 629a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev, 630a556c76aSAlexandre Belloni char *buf, size_t len) 631a556c76aSAlexandre Belloni { 632a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 633a556c76aSAlexandre Belloni int ret; 634a556c76aSAlexandre Belloni 635a556c76aSAlexandre Belloni ret = snprintf(buf, len, "p%d", port->chip_port); 636a556c76aSAlexandre Belloni if (ret >= len) 637a556c76aSAlexandre Belloni return -EINVAL; 638a556c76aSAlexandre Belloni 639a556c76aSAlexandre Belloni return 0; 640a556c76aSAlexandre Belloni } 641a556c76aSAlexandre Belloni 642a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p) 643a556c76aSAlexandre Belloni { 644a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 645a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 646a556c76aSAlexandre Belloni const struct sockaddr *addr = p; 647a556c76aSAlexandre Belloni 648a556c76aSAlexandre Belloni /* Learn the new net device MAC address in the mac table. */ 649a556c76aSAlexandre Belloni ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid, 650a556c76aSAlexandre Belloni ENTRYTYPE_LOCKED); 651a556c76aSAlexandre Belloni /* Then forget the previous one. */ 652a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid); 653a556c76aSAlexandre Belloni 654a556c76aSAlexandre Belloni ether_addr_copy(dev->dev_addr, addr->sa_data); 655a556c76aSAlexandre Belloni return 0; 656a556c76aSAlexandre Belloni } 657a556c76aSAlexandre Belloni 658a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev, 659a556c76aSAlexandre Belloni struct rtnl_link_stats64 *stats) 660a556c76aSAlexandre Belloni { 661a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 662a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 663a556c76aSAlexandre Belloni 664a556c76aSAlexandre Belloni /* Configure the port to read the stats from */ 665a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port), 666a556c76aSAlexandre Belloni SYS_STAT_CFG); 667a556c76aSAlexandre Belloni 668a556c76aSAlexandre Belloni /* Get Rx stats */ 669a556c76aSAlexandre Belloni stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS); 670a556c76aSAlexandre Belloni stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) + 671a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) + 672a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) + 673a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_LONGS) + 674a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_64) + 675a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_65_127) + 676a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_128_255) + 677a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_256_1023) + 678a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) + 679a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX); 680a556c76aSAlexandre Belloni stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST); 681a556c76aSAlexandre Belloni stats->rx_dropped = dev->stats.rx_dropped; 682a556c76aSAlexandre Belloni 683a556c76aSAlexandre Belloni /* Get Tx stats */ 684a556c76aSAlexandre Belloni stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS); 685a556c76aSAlexandre Belloni stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) + 686a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_TX_65_127) + 687a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_TX_128_511) + 688a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_TX_512_1023) + 689a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) + 690a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX); 691a556c76aSAlexandre Belloni stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) + 692a556c76aSAlexandre Belloni ocelot_read(ocelot, SYS_COUNT_TX_AGING); 693a556c76aSAlexandre Belloni stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION); 694a556c76aSAlexandre Belloni } 695a556c76aSAlexandre Belloni 696a556c76aSAlexandre Belloni static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 697a556c76aSAlexandre Belloni struct net_device *dev, const unsigned char *addr, 69887b0984eSPetr Machata u16 vid, u16 flags, 69987b0984eSPetr Machata struct netlink_ext_ack *extack) 700a556c76aSAlexandre Belloni { 701a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 702a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 703a556c76aSAlexandre Belloni 7047142529fSAntoine Tenart if (!vid) { 7057142529fSAntoine Tenart if (!port->vlan_aware) 7067142529fSAntoine Tenart /* If the bridge is not VLAN aware and no VID was 7077142529fSAntoine Tenart * provided, set it to pvid to ensure the MAC entry 7087142529fSAntoine Tenart * matches incoming untagged packets 7097142529fSAntoine Tenart */ 7107142529fSAntoine Tenart vid = port->pvid; 7117142529fSAntoine Tenart else 7127142529fSAntoine Tenart /* If the bridge is VLAN aware a VID must be provided as 7137142529fSAntoine Tenart * otherwise the learnt entry wouldn't match any frame. 7147142529fSAntoine Tenart */ 7157142529fSAntoine Tenart return -EINVAL; 7167142529fSAntoine Tenart } 7177142529fSAntoine Tenart 718a556c76aSAlexandre Belloni return ocelot_mact_learn(ocelot, port->chip_port, addr, vid, 7198fd1a4afSAllan W. Nielsen ENTRYTYPE_LOCKED); 720a556c76aSAlexandre Belloni } 721a556c76aSAlexandre Belloni 722a556c76aSAlexandre Belloni static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 723a556c76aSAlexandre Belloni struct net_device *dev, 724a556c76aSAlexandre Belloni const unsigned char *addr, u16 vid) 725a556c76aSAlexandre Belloni { 726a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 727a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 728a556c76aSAlexandre Belloni 729a556c76aSAlexandre Belloni return ocelot_mact_forget(ocelot, addr, vid); 730a556c76aSAlexandre Belloni } 731a556c76aSAlexandre Belloni 732a556c76aSAlexandre Belloni struct ocelot_dump_ctx { 733a556c76aSAlexandre Belloni struct net_device *dev; 734a556c76aSAlexandre Belloni struct sk_buff *skb; 735a556c76aSAlexandre Belloni struct netlink_callback *cb; 736a556c76aSAlexandre Belloni int idx; 737a556c76aSAlexandre Belloni }; 738a556c76aSAlexandre Belloni 739a556c76aSAlexandre Belloni static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry, 740a556c76aSAlexandre Belloni struct ocelot_dump_ctx *dump) 741a556c76aSAlexandre Belloni { 742a556c76aSAlexandre Belloni u32 portid = NETLINK_CB(dump->cb->skb).portid; 743a556c76aSAlexandre Belloni u32 seq = dump->cb->nlh->nlmsg_seq; 744a556c76aSAlexandre Belloni struct nlmsghdr *nlh; 745a556c76aSAlexandre Belloni struct ndmsg *ndm; 746a556c76aSAlexandre Belloni 747a556c76aSAlexandre Belloni if (dump->idx < dump->cb->args[2]) 748a556c76aSAlexandre Belloni goto skip; 749a556c76aSAlexandre Belloni 750a556c76aSAlexandre Belloni nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 751a556c76aSAlexandre Belloni sizeof(*ndm), NLM_F_MULTI); 752a556c76aSAlexandre Belloni if (!nlh) 753a556c76aSAlexandre Belloni return -EMSGSIZE; 754a556c76aSAlexandre Belloni 755a556c76aSAlexandre Belloni ndm = nlmsg_data(nlh); 756a556c76aSAlexandre Belloni ndm->ndm_family = AF_BRIDGE; 757a556c76aSAlexandre Belloni ndm->ndm_pad1 = 0; 758a556c76aSAlexandre Belloni ndm->ndm_pad2 = 0; 759a556c76aSAlexandre Belloni ndm->ndm_flags = NTF_SELF; 760a556c76aSAlexandre Belloni ndm->ndm_type = 0; 761a556c76aSAlexandre Belloni ndm->ndm_ifindex = dump->dev->ifindex; 762a556c76aSAlexandre Belloni ndm->ndm_state = NUD_REACHABLE; 763a556c76aSAlexandre Belloni 764a556c76aSAlexandre Belloni if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac)) 765a556c76aSAlexandre Belloni goto nla_put_failure; 766a556c76aSAlexandre Belloni 767a556c76aSAlexandre Belloni if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid)) 768a556c76aSAlexandre Belloni goto nla_put_failure; 769a556c76aSAlexandre Belloni 770a556c76aSAlexandre Belloni nlmsg_end(dump->skb, nlh); 771a556c76aSAlexandre Belloni 772a556c76aSAlexandre Belloni skip: 773a556c76aSAlexandre Belloni dump->idx++; 774a556c76aSAlexandre Belloni return 0; 775a556c76aSAlexandre Belloni 776a556c76aSAlexandre Belloni nla_put_failure: 777a556c76aSAlexandre Belloni nlmsg_cancel(dump->skb, nlh); 778a556c76aSAlexandre Belloni return -EMSGSIZE; 779a556c76aSAlexandre Belloni } 780a556c76aSAlexandre Belloni 781a556c76aSAlexandre Belloni static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col, 782a556c76aSAlexandre Belloni struct ocelot_mact_entry *entry) 783a556c76aSAlexandre Belloni { 784a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 785a556c76aSAlexandre Belloni char mac[ETH_ALEN]; 786a556c76aSAlexandre Belloni u32 val, dst, macl, mach; 787a556c76aSAlexandre Belloni 788a556c76aSAlexandre Belloni /* Set row and column to read from */ 789a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); 790a556c76aSAlexandre Belloni ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); 791a556c76aSAlexandre Belloni 792a556c76aSAlexandre Belloni /* Issue a read command */ 793a556c76aSAlexandre Belloni ocelot_write(ocelot, 794a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 795a556c76aSAlexandre Belloni ANA_TABLES_MACACCESS); 796a556c76aSAlexandre Belloni 797a556c76aSAlexandre Belloni if (ocelot_mact_wait_for_completion(ocelot)) 798a556c76aSAlexandre Belloni return -ETIMEDOUT; 799a556c76aSAlexandre Belloni 800a556c76aSAlexandre Belloni /* Read the entry flags */ 801a556c76aSAlexandre Belloni val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 802a556c76aSAlexandre Belloni if (!(val & ANA_TABLES_MACACCESS_VALID)) 803a556c76aSAlexandre Belloni return -EINVAL; 804a556c76aSAlexandre Belloni 805a556c76aSAlexandre Belloni /* If the entry read has another port configured as its destination, 806a556c76aSAlexandre Belloni * do not report it. 807a556c76aSAlexandre Belloni */ 808a556c76aSAlexandre Belloni dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; 809a556c76aSAlexandre Belloni if (dst != port->chip_port) 810a556c76aSAlexandre Belloni return -EINVAL; 811a556c76aSAlexandre Belloni 812a556c76aSAlexandre Belloni /* Get the entry's MAC address and VLAN id */ 813a556c76aSAlexandre Belloni macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); 814a556c76aSAlexandre Belloni mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); 815a556c76aSAlexandre Belloni 816a556c76aSAlexandre Belloni mac[0] = (mach >> 8) & 0xff; 817a556c76aSAlexandre Belloni mac[1] = (mach >> 0) & 0xff; 818a556c76aSAlexandre Belloni mac[2] = (macl >> 24) & 0xff; 819a556c76aSAlexandre Belloni mac[3] = (macl >> 16) & 0xff; 820a556c76aSAlexandre Belloni mac[4] = (macl >> 8) & 0xff; 821a556c76aSAlexandre Belloni mac[5] = (macl >> 0) & 0xff; 822a556c76aSAlexandre Belloni 823a556c76aSAlexandre Belloni entry->vid = (mach >> 16) & 0xfff; 824a556c76aSAlexandre Belloni ether_addr_copy(entry->mac, mac); 825a556c76aSAlexandre Belloni 826a556c76aSAlexandre Belloni return 0; 827a556c76aSAlexandre Belloni } 828a556c76aSAlexandre Belloni 829a556c76aSAlexandre Belloni static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 830a556c76aSAlexandre Belloni struct net_device *dev, 831a556c76aSAlexandre Belloni struct net_device *filter_dev, int *idx) 832a556c76aSAlexandre Belloni { 833a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 834a556c76aSAlexandre Belloni int i, j, ret = 0; 835a556c76aSAlexandre Belloni struct ocelot_dump_ctx dump = { 836a556c76aSAlexandre Belloni .dev = dev, 837a556c76aSAlexandre Belloni .skb = skb, 838a556c76aSAlexandre Belloni .cb = cb, 839a556c76aSAlexandre Belloni .idx = *idx, 840a556c76aSAlexandre Belloni }; 841a556c76aSAlexandre Belloni 842a556c76aSAlexandre Belloni struct ocelot_mact_entry entry; 843a556c76aSAlexandre Belloni 844a556c76aSAlexandre Belloni /* Loop through all the mac tables entries. There are 1024 rows of 4 845a556c76aSAlexandre Belloni * entries. 846a556c76aSAlexandre Belloni */ 847a556c76aSAlexandre Belloni for (i = 0; i < 1024; i++) { 848a556c76aSAlexandre Belloni for (j = 0; j < 4; j++) { 849a556c76aSAlexandre Belloni ret = ocelot_mact_read(port, i, j, &entry); 850a556c76aSAlexandre Belloni /* If the entry is invalid (wrong port, invalid...), 851a556c76aSAlexandre Belloni * skip it. 852a556c76aSAlexandre Belloni */ 853a556c76aSAlexandre Belloni if (ret == -EINVAL) 854a556c76aSAlexandre Belloni continue; 855a556c76aSAlexandre Belloni else if (ret) 856a556c76aSAlexandre Belloni goto end; 857a556c76aSAlexandre Belloni 858a556c76aSAlexandre Belloni ret = ocelot_fdb_do_dump(&entry, &dump); 859a556c76aSAlexandre Belloni if (ret) 860a556c76aSAlexandre Belloni goto end; 861a556c76aSAlexandre Belloni } 862a556c76aSAlexandre Belloni } 863a556c76aSAlexandre Belloni 864a556c76aSAlexandre Belloni end: 865a556c76aSAlexandre Belloni *idx = dump.idx; 866a556c76aSAlexandre Belloni return ret; 867a556c76aSAlexandre Belloni } 868a556c76aSAlexandre Belloni 8697142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 8707142529fSAntoine Tenart u16 vid) 8717142529fSAntoine Tenart { 8727142529fSAntoine Tenart return ocelot_vlan_vid_add(dev, vid, false, true); 8737142529fSAntoine Tenart } 8747142529fSAntoine Tenart 8757142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 8767142529fSAntoine Tenart u16 vid) 8777142529fSAntoine Tenart { 8787142529fSAntoine Tenart return ocelot_vlan_vid_del(dev, vid); 8797142529fSAntoine Tenart } 8807142529fSAntoine Tenart 8817142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev, 8827142529fSAntoine Tenart netdev_features_t features) 8837142529fSAntoine Tenart { 8847142529fSAntoine Tenart struct ocelot_port *port = netdev_priv(dev); 8857142529fSAntoine Tenart netdev_features_t changed = dev->features ^ features; 8867142529fSAntoine Tenart 8877142529fSAntoine Tenart if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) 8887142529fSAntoine Tenart ocelot_vlan_mode(port, features); 8897142529fSAntoine Tenart 8907142529fSAntoine Tenart return 0; 8917142529fSAntoine Tenart } 8927142529fSAntoine Tenart 893751302c3SFlorian Fainelli static int ocelot_get_port_parent_id(struct net_device *dev, 894751302c3SFlorian Fainelli struct netdev_phys_item_id *ppid) 895751302c3SFlorian Fainelli { 896751302c3SFlorian Fainelli struct ocelot_port *ocelot_port = netdev_priv(dev); 897751302c3SFlorian Fainelli struct ocelot *ocelot = ocelot_port->ocelot; 898751302c3SFlorian Fainelli 899751302c3SFlorian Fainelli ppid->id_len = sizeof(ocelot->base_mac); 900751302c3SFlorian Fainelli memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len); 901751302c3SFlorian Fainelli 902751302c3SFlorian Fainelli return 0; 903751302c3SFlorian Fainelli } 904751302c3SFlorian Fainelli 905a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = { 906a556c76aSAlexandre Belloni .ndo_open = ocelot_port_open, 907a556c76aSAlexandre Belloni .ndo_stop = ocelot_port_stop, 908a556c76aSAlexandre Belloni .ndo_start_xmit = ocelot_port_xmit, 909a556c76aSAlexandre Belloni .ndo_set_rx_mode = ocelot_set_rx_mode, 910a556c76aSAlexandre Belloni .ndo_get_phys_port_name = ocelot_port_get_phys_port_name, 911a556c76aSAlexandre Belloni .ndo_set_mac_address = ocelot_port_set_mac_address, 912a556c76aSAlexandre Belloni .ndo_get_stats64 = ocelot_get_stats64, 913a556c76aSAlexandre Belloni .ndo_fdb_add = ocelot_fdb_add, 914a556c76aSAlexandre Belloni .ndo_fdb_del = ocelot_fdb_del, 915a556c76aSAlexandre Belloni .ndo_fdb_dump = ocelot_fdb_dump, 9167142529fSAntoine Tenart .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid, 9177142529fSAntoine Tenart .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid, 9187142529fSAntoine Tenart .ndo_set_features = ocelot_set_features, 919751302c3SFlorian Fainelli .ndo_get_port_parent_id = ocelot_get_port_parent_id, 920a556c76aSAlexandre Belloni }; 921a556c76aSAlexandre Belloni 922a556c76aSAlexandre Belloni static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data) 923a556c76aSAlexandre Belloni { 924a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(netdev); 925a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 926a556c76aSAlexandre Belloni int i; 927a556c76aSAlexandre Belloni 928a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 929a556c76aSAlexandre Belloni return; 930a556c76aSAlexandre Belloni 931a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 932a556c76aSAlexandre Belloni memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, 933a556c76aSAlexandre Belloni ETH_GSTRING_LEN); 934a556c76aSAlexandre Belloni } 935a556c76aSAlexandre Belloni 9361e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot) 937a556c76aSAlexandre Belloni { 938a556c76aSAlexandre Belloni int i, j; 939a556c76aSAlexandre Belloni 940a556c76aSAlexandre Belloni mutex_lock(&ocelot->stats_lock); 941a556c76aSAlexandre Belloni 942a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_phys_ports; i++) { 943a556c76aSAlexandre Belloni /* Configure the port to read the stats from */ 944a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); 945a556c76aSAlexandre Belloni 946a556c76aSAlexandre Belloni for (j = 0; j < ocelot->num_stats; j++) { 947a556c76aSAlexandre Belloni u32 val; 948a556c76aSAlexandre Belloni unsigned int idx = i * ocelot->num_stats + j; 949a556c76aSAlexandre Belloni 950a556c76aSAlexandre Belloni val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, 951a556c76aSAlexandre Belloni ocelot->stats_layout[j].offset); 952a556c76aSAlexandre Belloni 953a556c76aSAlexandre Belloni if (val < (ocelot->stats[idx] & U32_MAX)) 954a556c76aSAlexandre Belloni ocelot->stats[idx] += (u64)1 << 32; 955a556c76aSAlexandre Belloni 956a556c76aSAlexandre Belloni ocelot->stats[idx] = (ocelot->stats[idx] & 957a556c76aSAlexandre Belloni ~(u64)U32_MAX) + val; 958a556c76aSAlexandre Belloni } 959a556c76aSAlexandre Belloni } 960a556c76aSAlexandre Belloni 9611e1caa97SClaudiu Manoil mutex_unlock(&ocelot->stats_lock); 9621e1caa97SClaudiu Manoil } 9631e1caa97SClaudiu Manoil 9641e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work) 9651e1caa97SClaudiu Manoil { 9661e1caa97SClaudiu Manoil struct delayed_work *del_work = to_delayed_work(work); 9671e1caa97SClaudiu Manoil struct ocelot *ocelot = container_of(del_work, struct ocelot, 9681e1caa97SClaudiu Manoil stats_work); 9691e1caa97SClaudiu Manoil 9701e1caa97SClaudiu Manoil ocelot_update_stats(ocelot); 9711e1caa97SClaudiu Manoil 972a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 973a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 974a556c76aSAlexandre Belloni } 975a556c76aSAlexandre Belloni 976a556c76aSAlexandre Belloni static void ocelot_get_ethtool_stats(struct net_device *dev, 977a556c76aSAlexandre Belloni struct ethtool_stats *stats, u64 *data) 978a556c76aSAlexandre Belloni { 979a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 980a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 981a556c76aSAlexandre Belloni int i; 982a556c76aSAlexandre Belloni 983a556c76aSAlexandre Belloni /* check and update now */ 9841e1caa97SClaudiu Manoil ocelot_update_stats(ocelot); 985a556c76aSAlexandre Belloni 986a556c76aSAlexandre Belloni /* Copy all counters */ 987a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_stats; i++) 988a556c76aSAlexandre Belloni *data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i]; 989a556c76aSAlexandre Belloni } 990a556c76aSAlexandre Belloni 991a556c76aSAlexandre Belloni static int ocelot_get_sset_count(struct net_device *dev, int sset) 992a556c76aSAlexandre Belloni { 993a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 994a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 995a556c76aSAlexandre Belloni 996a556c76aSAlexandre Belloni if (sset != ETH_SS_STATS) 997a556c76aSAlexandre Belloni return -EOPNOTSUPP; 998a556c76aSAlexandre Belloni return ocelot->num_stats; 999a556c76aSAlexandre Belloni } 1000a556c76aSAlexandre Belloni 1001a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = { 1002a556c76aSAlexandre Belloni .get_strings = ocelot_get_strings, 1003a556c76aSAlexandre Belloni .get_ethtool_stats = ocelot_get_ethtool_stats, 1004a556c76aSAlexandre Belloni .get_sset_count = ocelot_get_sset_count, 1005dc96ee37SAlexandre Belloni .get_link_ksettings = phy_ethtool_get_link_ksettings, 1006dc96ee37SAlexandre Belloni .set_link_ksettings = phy_ethtool_set_link_ksettings, 1007a556c76aSAlexandre Belloni }; 1008a556c76aSAlexandre Belloni 1009a556c76aSAlexandre Belloni static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port, 1010a556c76aSAlexandre Belloni struct switchdev_trans *trans, 1011a556c76aSAlexandre Belloni u8 state) 1012a556c76aSAlexandre Belloni { 1013a556c76aSAlexandre Belloni struct ocelot *ocelot = ocelot_port->ocelot; 1014a556c76aSAlexandre Belloni u32 port_cfg; 1015a556c76aSAlexandre Belloni int port, i; 1016a556c76aSAlexandre Belloni 1017a556c76aSAlexandre Belloni if (switchdev_trans_ph_prepare(trans)) 1018a556c76aSAlexandre Belloni return 0; 1019a556c76aSAlexandre Belloni 1020a556c76aSAlexandre Belloni if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask)) 1021a556c76aSAlexandre Belloni return 0; 1022a556c76aSAlexandre Belloni 1023a556c76aSAlexandre Belloni port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, 1024a556c76aSAlexandre Belloni ocelot_port->chip_port); 1025a556c76aSAlexandre Belloni 1026a556c76aSAlexandre Belloni switch (state) { 1027a556c76aSAlexandre Belloni case BR_STATE_FORWARDING: 1028a556c76aSAlexandre Belloni ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port); 1029a556c76aSAlexandre Belloni /* Fallthrough */ 1030a556c76aSAlexandre Belloni case BR_STATE_LEARNING: 1031a556c76aSAlexandre Belloni port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA; 1032a556c76aSAlexandre Belloni break; 1033a556c76aSAlexandre Belloni 1034a556c76aSAlexandre Belloni default: 1035a556c76aSAlexandre Belloni port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA; 1036a556c76aSAlexandre Belloni ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port); 1037a556c76aSAlexandre Belloni break; 1038a556c76aSAlexandre Belloni } 1039a556c76aSAlexandre Belloni 1040a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, 1041a556c76aSAlexandre Belloni ocelot_port->chip_port); 1042a556c76aSAlexandre Belloni 1043a556c76aSAlexandre Belloni /* Apply FWD mask. The loop is needed to add/remove the current port as 1044a556c76aSAlexandre Belloni * a source for the other ports. 1045a556c76aSAlexandre Belloni */ 1046a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 1047a556c76aSAlexandre Belloni if (ocelot->bridge_fwd_mask & BIT(port)) { 1048a556c76aSAlexandre Belloni unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port); 1049a556c76aSAlexandre Belloni 1050a556c76aSAlexandre Belloni for (i = 0; i < ocelot->num_phys_ports; i++) { 1051a556c76aSAlexandre Belloni unsigned long bond_mask = ocelot->lags[i]; 1052a556c76aSAlexandre Belloni 1053a556c76aSAlexandre Belloni if (!bond_mask) 1054a556c76aSAlexandre Belloni continue; 1055a556c76aSAlexandre Belloni 1056a556c76aSAlexandre Belloni if (bond_mask & BIT(port)) { 1057a556c76aSAlexandre Belloni mask &= ~bond_mask; 1058a556c76aSAlexandre Belloni break; 1059a556c76aSAlexandre Belloni } 1060a556c76aSAlexandre Belloni } 1061a556c76aSAlexandre Belloni 1062a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 1063a556c76aSAlexandre Belloni BIT(ocelot->num_phys_ports) | mask, 1064a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_SRC + port); 1065a556c76aSAlexandre Belloni } else { 1066a556c76aSAlexandre Belloni /* Only the CPU port, this is compatible with link 1067a556c76aSAlexandre Belloni * aggregation. 1068a556c76aSAlexandre Belloni */ 1069a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 1070a556c76aSAlexandre Belloni BIT(ocelot->num_phys_ports), 1071a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_SRC + port); 1072a556c76aSAlexandre Belloni } 1073a556c76aSAlexandre Belloni } 1074a556c76aSAlexandre Belloni 1075a556c76aSAlexandre Belloni return 0; 1076a556c76aSAlexandre Belloni } 1077a556c76aSAlexandre Belloni 1078a556c76aSAlexandre Belloni static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port, 1079a556c76aSAlexandre Belloni unsigned long ageing_clock_t) 1080a556c76aSAlexandre Belloni { 1081a556c76aSAlexandre Belloni struct ocelot *ocelot = ocelot_port->ocelot; 1082a556c76aSAlexandre Belloni unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t); 1083a556c76aSAlexandre Belloni u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000; 1084a556c76aSAlexandre Belloni 1085a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2), 1086a556c76aSAlexandre Belloni ANA_AUTOAGE); 1087a556c76aSAlexandre Belloni } 1088a556c76aSAlexandre Belloni 1089a556c76aSAlexandre Belloni static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc) 1090a556c76aSAlexandre Belloni { 1091a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 1092a556c76aSAlexandre Belloni u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG, 1093a556c76aSAlexandre Belloni port->chip_port); 1094a556c76aSAlexandre Belloni 1095a556c76aSAlexandre Belloni if (mc) 1096a556c76aSAlexandre Belloni val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA | 1097a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA | 1098a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA; 1099a556c76aSAlexandre Belloni else 1100a556c76aSAlexandre Belloni val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA | 1101a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA | 1102a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA); 1103a556c76aSAlexandre Belloni 1104a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port); 1105a556c76aSAlexandre Belloni } 1106a556c76aSAlexandre Belloni 1107a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev, 1108a556c76aSAlexandre Belloni const struct switchdev_attr *attr, 1109a556c76aSAlexandre Belloni struct switchdev_trans *trans) 1110a556c76aSAlexandre Belloni { 1111a556c76aSAlexandre Belloni struct ocelot_port *ocelot_port = netdev_priv(dev); 1112a556c76aSAlexandre Belloni int err = 0; 1113a556c76aSAlexandre Belloni 1114a556c76aSAlexandre Belloni switch (attr->id) { 1115a556c76aSAlexandre Belloni case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 1116a556c76aSAlexandre Belloni ocelot_port_attr_stp_state_set(ocelot_port, trans, 1117a556c76aSAlexandre Belloni attr->u.stp_state); 1118a556c76aSAlexandre Belloni break; 1119a556c76aSAlexandre Belloni case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 1120a556c76aSAlexandre Belloni ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time); 1121a556c76aSAlexandre Belloni break; 11227142529fSAntoine Tenart case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 11237142529fSAntoine Tenart ocelot_port->vlan_aware = attr->u.vlan_filtering; 11247142529fSAntoine Tenart ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port); 11257142529fSAntoine Tenart break; 1126a556c76aSAlexandre Belloni case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED: 1127a556c76aSAlexandre Belloni ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled); 1128a556c76aSAlexandre Belloni break; 1129a556c76aSAlexandre Belloni default: 1130a556c76aSAlexandre Belloni err = -EOPNOTSUPP; 1131a556c76aSAlexandre Belloni break; 1132a556c76aSAlexandre Belloni } 1133a556c76aSAlexandre Belloni 1134a556c76aSAlexandre Belloni return err; 1135a556c76aSAlexandre Belloni } 1136a556c76aSAlexandre Belloni 11377142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev, 11387142529fSAntoine Tenart const struct switchdev_obj_port_vlan *vlan, 11397142529fSAntoine Tenart struct switchdev_trans *trans) 11407142529fSAntoine Tenart { 11417142529fSAntoine Tenart int ret; 11427142529fSAntoine Tenart u16 vid; 11437142529fSAntoine Tenart 11447142529fSAntoine Tenart for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 11457142529fSAntoine Tenart ret = ocelot_vlan_vid_add(dev, vid, 11467142529fSAntoine Tenart vlan->flags & BRIDGE_VLAN_INFO_PVID, 11477142529fSAntoine Tenart vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED); 11487142529fSAntoine Tenart if (ret) 11497142529fSAntoine Tenart return ret; 11507142529fSAntoine Tenart } 11517142529fSAntoine Tenart 11527142529fSAntoine Tenart return 0; 11537142529fSAntoine Tenart } 11547142529fSAntoine Tenart 11557142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev, 11567142529fSAntoine Tenart const struct switchdev_obj_port_vlan *vlan) 11577142529fSAntoine Tenart { 11587142529fSAntoine Tenart int ret; 11597142529fSAntoine Tenart u16 vid; 11607142529fSAntoine Tenart 11617142529fSAntoine Tenart for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 11627142529fSAntoine Tenart ret = ocelot_vlan_vid_del(dev, vid); 11637142529fSAntoine Tenart 11647142529fSAntoine Tenart if (ret) 11657142529fSAntoine Tenart return ret; 11667142529fSAntoine Tenart } 11677142529fSAntoine Tenart 11687142529fSAntoine Tenart return 0; 11697142529fSAntoine Tenart } 11707142529fSAntoine Tenart 1171a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 1172a556c76aSAlexandre Belloni const unsigned char *addr, 1173a556c76aSAlexandre Belloni u16 vid) 1174a556c76aSAlexandre Belloni { 1175a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 1176a556c76aSAlexandre Belloni 1177a556c76aSAlexandre Belloni list_for_each_entry(mc, &ocelot->multicast, list) { 1178a556c76aSAlexandre Belloni if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 1179a556c76aSAlexandre Belloni return mc; 1180a556c76aSAlexandre Belloni } 1181a556c76aSAlexandre Belloni 1182a556c76aSAlexandre Belloni return NULL; 1183a556c76aSAlexandre Belloni } 1184a556c76aSAlexandre Belloni 1185a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev, 1186a556c76aSAlexandre Belloni const struct switchdev_obj_port_mdb *mdb, 1187a556c76aSAlexandre Belloni struct switchdev_trans *trans) 1188a556c76aSAlexandre Belloni { 1189a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 1190a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 1191a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 1192a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 1193a556c76aSAlexandre Belloni u16 vid = mdb->vid; 1194a556c76aSAlexandre Belloni bool new = false; 1195a556c76aSAlexandre Belloni 1196a556c76aSAlexandre Belloni if (!vid) 11977142529fSAntoine Tenart vid = port->pvid; 1198a556c76aSAlexandre Belloni 1199a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1200a556c76aSAlexandre Belloni if (!mc) { 1201a556c76aSAlexandre Belloni mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 1202a556c76aSAlexandre Belloni if (!mc) 1203a556c76aSAlexandre Belloni return -ENOMEM; 1204a556c76aSAlexandre Belloni 1205a556c76aSAlexandre Belloni memcpy(mc->addr, mdb->addr, ETH_ALEN); 1206a556c76aSAlexandre Belloni mc->vid = vid; 1207a556c76aSAlexandre Belloni 1208a556c76aSAlexandre Belloni list_add_tail(&mc->list, &ocelot->multicast); 1209a556c76aSAlexandre Belloni new = true; 1210a556c76aSAlexandre Belloni } 1211a556c76aSAlexandre Belloni 1212a556c76aSAlexandre Belloni memcpy(addr, mc->addr, ETH_ALEN); 1213a556c76aSAlexandre Belloni addr[0] = 0; 1214a556c76aSAlexandre Belloni 1215a556c76aSAlexandre Belloni if (!new) { 1216a556c76aSAlexandre Belloni addr[2] = mc->ports << 0; 1217a556c76aSAlexandre Belloni addr[1] = mc->ports << 8; 1218a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 1219a556c76aSAlexandre Belloni } 1220a556c76aSAlexandre Belloni 1221a556c76aSAlexandre Belloni mc->ports |= BIT(port->chip_port); 1222a556c76aSAlexandre Belloni addr[2] = mc->ports << 0; 1223a556c76aSAlexandre Belloni addr[1] = mc->ports << 8; 1224a556c76aSAlexandre Belloni 1225a556c76aSAlexandre Belloni return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4); 1226a556c76aSAlexandre Belloni } 1227a556c76aSAlexandre Belloni 1228a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev, 1229a556c76aSAlexandre Belloni const struct switchdev_obj_port_mdb *mdb) 1230a556c76aSAlexandre Belloni { 1231a556c76aSAlexandre Belloni struct ocelot_port *port = netdev_priv(dev); 1232a556c76aSAlexandre Belloni struct ocelot *ocelot = port->ocelot; 1233a556c76aSAlexandre Belloni struct ocelot_multicast *mc; 1234a556c76aSAlexandre Belloni unsigned char addr[ETH_ALEN]; 1235a556c76aSAlexandre Belloni u16 vid = mdb->vid; 1236a556c76aSAlexandre Belloni 1237a556c76aSAlexandre Belloni if (!vid) 12387142529fSAntoine Tenart vid = port->pvid; 1239a556c76aSAlexandre Belloni 1240a556c76aSAlexandre Belloni mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1241a556c76aSAlexandre Belloni if (!mc) 1242a556c76aSAlexandre Belloni return -ENOENT; 1243a556c76aSAlexandre Belloni 1244a556c76aSAlexandre Belloni memcpy(addr, mc->addr, ETH_ALEN); 1245a556c76aSAlexandre Belloni addr[2] = mc->ports << 0; 1246a556c76aSAlexandre Belloni addr[1] = mc->ports << 8; 1247a556c76aSAlexandre Belloni addr[0] = 0; 1248a556c76aSAlexandre Belloni ocelot_mact_forget(ocelot, addr, vid); 1249a556c76aSAlexandre Belloni 1250a556c76aSAlexandre Belloni mc->ports &= ~BIT(port->chip_port); 1251a556c76aSAlexandre Belloni if (!mc->ports) { 1252a556c76aSAlexandre Belloni list_del(&mc->list); 1253a556c76aSAlexandre Belloni devm_kfree(ocelot->dev, mc); 1254a556c76aSAlexandre Belloni return 0; 1255a556c76aSAlexandre Belloni } 1256a556c76aSAlexandre Belloni 1257a556c76aSAlexandre Belloni addr[2] = mc->ports << 0; 1258a556c76aSAlexandre Belloni addr[1] = mc->ports << 8; 1259a556c76aSAlexandre Belloni 1260a556c76aSAlexandre Belloni return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4); 1261a556c76aSAlexandre Belloni } 1262a556c76aSAlexandre Belloni 1263a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev, 1264a556c76aSAlexandre Belloni const struct switchdev_obj *obj, 126569213513SPetr Machata struct switchdev_trans *trans, 126669213513SPetr Machata struct netlink_ext_ack *extack) 1267a556c76aSAlexandre Belloni { 1268a556c76aSAlexandre Belloni int ret = 0; 1269a556c76aSAlexandre Belloni 1270a556c76aSAlexandre Belloni switch (obj->id) { 12717142529fSAntoine Tenart case SWITCHDEV_OBJ_ID_PORT_VLAN: 12727142529fSAntoine Tenart ret = ocelot_port_obj_add_vlan(dev, 12737142529fSAntoine Tenart SWITCHDEV_OBJ_PORT_VLAN(obj), 12747142529fSAntoine Tenart trans); 12757142529fSAntoine Tenart break; 1276a556c76aSAlexandre Belloni case SWITCHDEV_OBJ_ID_PORT_MDB: 1277a556c76aSAlexandre Belloni ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj), 1278a556c76aSAlexandre Belloni trans); 1279a556c76aSAlexandre Belloni break; 1280a556c76aSAlexandre Belloni default: 1281a556c76aSAlexandre Belloni return -EOPNOTSUPP; 1282a556c76aSAlexandre Belloni } 1283a556c76aSAlexandre Belloni 1284a556c76aSAlexandre Belloni return ret; 1285a556c76aSAlexandre Belloni } 1286a556c76aSAlexandre Belloni 1287a556c76aSAlexandre Belloni static int ocelot_port_obj_del(struct net_device *dev, 1288a556c76aSAlexandre Belloni const struct switchdev_obj *obj) 1289a556c76aSAlexandre Belloni { 1290a556c76aSAlexandre Belloni int ret = 0; 1291a556c76aSAlexandre Belloni 1292a556c76aSAlexandre Belloni switch (obj->id) { 12937142529fSAntoine Tenart case SWITCHDEV_OBJ_ID_PORT_VLAN: 12947142529fSAntoine Tenart ret = ocelot_port_vlan_del_vlan(dev, 12957142529fSAntoine Tenart SWITCHDEV_OBJ_PORT_VLAN(obj)); 12967142529fSAntoine Tenart break; 1297a556c76aSAlexandre Belloni case SWITCHDEV_OBJ_ID_PORT_MDB: 1298a556c76aSAlexandre Belloni ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj)); 1299a556c76aSAlexandre Belloni break; 1300a556c76aSAlexandre Belloni default: 1301a556c76aSAlexandre Belloni return -EOPNOTSUPP; 1302a556c76aSAlexandre Belloni } 1303a556c76aSAlexandre Belloni 1304a556c76aSAlexandre Belloni return ret; 1305a556c76aSAlexandre Belloni } 1306a556c76aSAlexandre Belloni 1307a556c76aSAlexandre Belloni static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port, 1308a556c76aSAlexandre Belloni struct net_device *bridge) 1309a556c76aSAlexandre Belloni { 1310a556c76aSAlexandre Belloni struct ocelot *ocelot = ocelot_port->ocelot; 1311a556c76aSAlexandre Belloni 1312a556c76aSAlexandre Belloni if (!ocelot->bridge_mask) { 1313a556c76aSAlexandre Belloni ocelot->hw_bridge_dev = bridge; 1314a556c76aSAlexandre Belloni } else { 1315a556c76aSAlexandre Belloni if (ocelot->hw_bridge_dev != bridge) 1316a556c76aSAlexandre Belloni /* This is adding the port to a second bridge, this is 1317a556c76aSAlexandre Belloni * unsupported */ 1318a556c76aSAlexandre Belloni return -ENODEV; 1319a556c76aSAlexandre Belloni } 1320a556c76aSAlexandre Belloni 1321a556c76aSAlexandre Belloni ocelot->bridge_mask |= BIT(ocelot_port->chip_port); 1322a556c76aSAlexandre Belloni 1323a556c76aSAlexandre Belloni return 0; 1324a556c76aSAlexandre Belloni } 1325a556c76aSAlexandre Belloni 1326a556c76aSAlexandre Belloni static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port, 1327a556c76aSAlexandre Belloni struct net_device *bridge) 1328a556c76aSAlexandre Belloni { 1329a556c76aSAlexandre Belloni struct ocelot *ocelot = ocelot_port->ocelot; 1330a556c76aSAlexandre Belloni 1331a556c76aSAlexandre Belloni ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port); 1332a556c76aSAlexandre Belloni 1333a556c76aSAlexandre Belloni if (!ocelot->bridge_mask) 1334a556c76aSAlexandre Belloni ocelot->hw_bridge_dev = NULL; 13357142529fSAntoine Tenart 13367142529fSAntoine Tenart /* Clear bridge vlan settings before calling ocelot_vlan_port_apply */ 13377142529fSAntoine Tenart ocelot_port->vlan_aware = 0; 13387142529fSAntoine Tenart ocelot_port->pvid = 0; 13397142529fSAntoine Tenart ocelot_port->vid = 0; 1340a556c76aSAlexandre Belloni } 1341a556c76aSAlexandre Belloni 1342dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 1343dc96ee37SAlexandre Belloni { 1344dc96ee37SAlexandre Belloni int i, port, lag; 1345dc96ee37SAlexandre Belloni 1346dc96ee37SAlexandre Belloni /* Reset destination and aggregation PGIDS */ 1347dc96ee37SAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) 1348dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1349dc96ee37SAlexandre Belloni 1350dc96ee37SAlexandre Belloni for (i = PGID_AGGR; i < PGID_SRC; i++) 1351dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 1352dc96ee37SAlexandre Belloni ANA_PGID_PGID, i); 1353dc96ee37SAlexandre Belloni 1354dc96ee37SAlexandre Belloni /* Now, set PGIDs for each LAG */ 1355dc96ee37SAlexandre Belloni for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 1356dc96ee37SAlexandre Belloni unsigned long bond_mask; 1357dc96ee37SAlexandre Belloni int aggr_count = 0; 1358dc96ee37SAlexandre Belloni u8 aggr_idx[16]; 1359dc96ee37SAlexandre Belloni 1360dc96ee37SAlexandre Belloni bond_mask = ocelot->lags[lag]; 1361dc96ee37SAlexandre Belloni if (!bond_mask) 1362dc96ee37SAlexandre Belloni continue; 1363dc96ee37SAlexandre Belloni 1364dc96ee37SAlexandre Belloni for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 1365dc96ee37SAlexandre Belloni // Destination mask 1366dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, bond_mask, 1367dc96ee37SAlexandre Belloni ANA_PGID_PGID, port); 1368dc96ee37SAlexandre Belloni aggr_idx[aggr_count] = port; 1369dc96ee37SAlexandre Belloni aggr_count++; 1370dc96ee37SAlexandre Belloni } 1371dc96ee37SAlexandre Belloni 1372dc96ee37SAlexandre Belloni for (i = PGID_AGGR; i < PGID_SRC; i++) { 1373dc96ee37SAlexandre Belloni u32 ac; 1374dc96ee37SAlexandre Belloni 1375dc96ee37SAlexandre Belloni ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 1376dc96ee37SAlexandre Belloni ac &= ~bond_mask; 1377dc96ee37SAlexandre Belloni ac |= BIT(aggr_idx[i % aggr_count]); 1378dc96ee37SAlexandre Belloni ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 1379dc96ee37SAlexandre Belloni } 1380dc96ee37SAlexandre Belloni } 1381dc96ee37SAlexandre Belloni } 1382dc96ee37SAlexandre Belloni 1383dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag) 1384dc96ee37SAlexandre Belloni { 1385dc96ee37SAlexandre Belloni unsigned long bond_mask = ocelot->lags[lag]; 1386dc96ee37SAlexandre Belloni unsigned int p; 1387dc96ee37SAlexandre Belloni 1388dc96ee37SAlexandre Belloni for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) { 1389dc96ee37SAlexandre Belloni u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p); 1390dc96ee37SAlexandre Belloni 1391dc96ee37SAlexandre Belloni port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M; 1392dc96ee37SAlexandre Belloni 1393dc96ee37SAlexandre Belloni /* Use lag port as logical port for port i */ 1394dc96ee37SAlexandre Belloni ocelot_write_gix(ocelot, port_cfg | 1395dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(lag), 1396dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG, p); 1397dc96ee37SAlexandre Belloni } 1398dc96ee37SAlexandre Belloni } 1399dc96ee37SAlexandre Belloni 1400dc96ee37SAlexandre Belloni static int ocelot_port_lag_join(struct ocelot_port *ocelot_port, 1401dc96ee37SAlexandre Belloni struct net_device *bond) 1402dc96ee37SAlexandre Belloni { 1403dc96ee37SAlexandre Belloni struct ocelot *ocelot = ocelot_port->ocelot; 1404dc96ee37SAlexandre Belloni int p = ocelot_port->chip_port; 1405dc96ee37SAlexandre Belloni int lag, lp; 1406dc96ee37SAlexandre Belloni struct net_device *ndev; 1407dc96ee37SAlexandre Belloni u32 bond_mask = 0; 1408dc96ee37SAlexandre Belloni 1409dc96ee37SAlexandre Belloni rcu_read_lock(); 1410dc96ee37SAlexandre Belloni for_each_netdev_in_bond_rcu(bond, ndev) { 1411dc96ee37SAlexandre Belloni struct ocelot_port *port = netdev_priv(ndev); 1412dc96ee37SAlexandre Belloni 1413dc96ee37SAlexandre Belloni bond_mask |= BIT(port->chip_port); 1414dc96ee37SAlexandre Belloni } 1415dc96ee37SAlexandre Belloni rcu_read_unlock(); 1416dc96ee37SAlexandre Belloni 1417dc96ee37SAlexandre Belloni lp = __ffs(bond_mask); 1418dc96ee37SAlexandre Belloni 1419dc96ee37SAlexandre Belloni /* If the new port is the lowest one, use it as the logical port from 1420dc96ee37SAlexandre Belloni * now on 1421dc96ee37SAlexandre Belloni */ 1422dc96ee37SAlexandre Belloni if (p == lp) { 1423dc96ee37SAlexandre Belloni lag = p; 1424dc96ee37SAlexandre Belloni ocelot->lags[p] = bond_mask; 1425dc96ee37SAlexandre Belloni bond_mask &= ~BIT(p); 1426dc96ee37SAlexandre Belloni if (bond_mask) { 1427dc96ee37SAlexandre Belloni lp = __ffs(bond_mask); 1428dc96ee37SAlexandre Belloni ocelot->lags[lp] = 0; 1429dc96ee37SAlexandre Belloni } 1430dc96ee37SAlexandre Belloni } else { 1431dc96ee37SAlexandre Belloni lag = lp; 1432dc96ee37SAlexandre Belloni ocelot->lags[lp] |= BIT(p); 1433dc96ee37SAlexandre Belloni } 1434dc96ee37SAlexandre Belloni 1435dc96ee37SAlexandre Belloni ocelot_setup_lag(ocelot, lag); 1436dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 1437dc96ee37SAlexandre Belloni 1438dc96ee37SAlexandre Belloni return 0; 1439dc96ee37SAlexandre Belloni } 1440dc96ee37SAlexandre Belloni 1441dc96ee37SAlexandre Belloni static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port, 1442dc96ee37SAlexandre Belloni struct net_device *bond) 1443dc96ee37SAlexandre Belloni { 1444dc96ee37SAlexandre Belloni struct ocelot *ocelot = ocelot_port->ocelot; 1445dc96ee37SAlexandre Belloni int p = ocelot_port->chip_port; 1446dc96ee37SAlexandre Belloni u32 port_cfg; 1447dc96ee37SAlexandre Belloni int i; 1448dc96ee37SAlexandre Belloni 1449dc96ee37SAlexandre Belloni /* Remove port from any lag */ 1450dc96ee37SAlexandre Belloni for (i = 0; i < ocelot->num_phys_ports; i++) 1451dc96ee37SAlexandre Belloni ocelot->lags[i] &= ~BIT(ocelot_port->chip_port); 1452dc96ee37SAlexandre Belloni 1453dc96ee37SAlexandre Belloni /* if it was the logical port of the lag, move the lag config to the 1454dc96ee37SAlexandre Belloni * next port 1455dc96ee37SAlexandre Belloni */ 1456dc96ee37SAlexandre Belloni if (ocelot->lags[p]) { 1457dc96ee37SAlexandre Belloni int n = __ffs(ocelot->lags[p]); 1458dc96ee37SAlexandre Belloni 1459dc96ee37SAlexandre Belloni ocelot->lags[n] = ocelot->lags[p]; 1460dc96ee37SAlexandre Belloni ocelot->lags[p] = 0; 1461dc96ee37SAlexandre Belloni 1462dc96ee37SAlexandre Belloni ocelot_setup_lag(ocelot, n); 1463dc96ee37SAlexandre Belloni } 1464dc96ee37SAlexandre Belloni 1465dc96ee37SAlexandre Belloni port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p); 1466dc96ee37SAlexandre Belloni port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M; 1467dc96ee37SAlexandre Belloni ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p), 1468dc96ee37SAlexandre Belloni ANA_PORT_PORT_CFG, p); 1469dc96ee37SAlexandre Belloni 1470dc96ee37SAlexandre Belloni ocelot_set_aggr_pgids(ocelot); 1471dc96ee37SAlexandre Belloni } 1472dc96ee37SAlexandre Belloni 1473a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */ 1474a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev) 1475a556c76aSAlexandre Belloni { 1476a556c76aSAlexandre Belloni return dev->netdev_ops == &ocelot_port_netdev_ops; 1477a556c76aSAlexandre Belloni } 1478a556c76aSAlexandre Belloni 1479a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev, 1480a556c76aSAlexandre Belloni unsigned long event, 1481a556c76aSAlexandre Belloni struct netdev_notifier_changeupper_info *info) 1482a556c76aSAlexandre Belloni { 1483a556c76aSAlexandre Belloni struct ocelot_port *ocelot_port = netdev_priv(dev); 1484a556c76aSAlexandre Belloni int err = 0; 1485a556c76aSAlexandre Belloni 1486a556c76aSAlexandre Belloni if (!ocelot_netdevice_dev_check(dev)) 1487a556c76aSAlexandre Belloni return 0; 1488a556c76aSAlexandre Belloni 1489a556c76aSAlexandre Belloni switch (event) { 1490a556c76aSAlexandre Belloni case NETDEV_CHANGEUPPER: 1491a556c76aSAlexandre Belloni if (netif_is_bridge_master(info->upper_dev)) { 1492a556c76aSAlexandre Belloni if (info->linking) 1493a556c76aSAlexandre Belloni err = ocelot_port_bridge_join(ocelot_port, 1494a556c76aSAlexandre Belloni info->upper_dev); 1495a556c76aSAlexandre Belloni else 1496a556c76aSAlexandre Belloni ocelot_port_bridge_leave(ocelot_port, 1497a556c76aSAlexandre Belloni info->upper_dev); 14987142529fSAntoine Tenart 14997142529fSAntoine Tenart ocelot_vlan_port_apply(ocelot_port->ocelot, 15007142529fSAntoine Tenart ocelot_port); 1501a556c76aSAlexandre Belloni } 1502dc96ee37SAlexandre Belloni if (netif_is_lag_master(info->upper_dev)) { 1503dc96ee37SAlexandre Belloni if (info->linking) 1504dc96ee37SAlexandre Belloni err = ocelot_port_lag_join(ocelot_port, 1505dc96ee37SAlexandre Belloni info->upper_dev); 1506dc96ee37SAlexandre Belloni else 1507dc96ee37SAlexandre Belloni ocelot_port_lag_leave(ocelot_port, 1508dc96ee37SAlexandre Belloni info->upper_dev); 1509dc96ee37SAlexandre Belloni } 1510a556c76aSAlexandre Belloni break; 1511a556c76aSAlexandre Belloni default: 1512a556c76aSAlexandre Belloni break; 1513a556c76aSAlexandre Belloni } 1514a556c76aSAlexandre Belloni 1515a556c76aSAlexandre Belloni return err; 1516a556c76aSAlexandre Belloni } 1517a556c76aSAlexandre Belloni 1518a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused, 1519a556c76aSAlexandre Belloni unsigned long event, void *ptr) 1520a556c76aSAlexandre Belloni { 1521a556c76aSAlexandre Belloni struct netdev_notifier_changeupper_info *info = ptr; 1522a556c76aSAlexandre Belloni struct net_device *dev = netdev_notifier_info_to_dev(ptr); 15232ac0e152SGeert Uytterhoeven int ret = 0; 1524a556c76aSAlexandre Belloni 1525dc96ee37SAlexandre Belloni if (event == NETDEV_PRECHANGEUPPER && 1526dc96ee37SAlexandre Belloni netif_is_lag_master(info->upper_dev)) { 1527dc96ee37SAlexandre Belloni struct netdev_lag_upper_info *lag_upper_info = info->upper_info; 1528dc96ee37SAlexandre Belloni struct netlink_ext_ack *extack; 1529dc96ee37SAlexandre Belloni 1530dc96ee37SAlexandre Belloni if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) { 1531dc96ee37SAlexandre Belloni extack = netdev_notifier_info_to_extack(&info->info); 1532dc96ee37SAlexandre Belloni NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type"); 1533dc96ee37SAlexandre Belloni 1534dc96ee37SAlexandre Belloni ret = -EINVAL; 1535dc96ee37SAlexandre Belloni goto notify; 1536dc96ee37SAlexandre Belloni } 1537dc96ee37SAlexandre Belloni } 1538dc96ee37SAlexandre Belloni 1539a556c76aSAlexandre Belloni if (netif_is_lag_master(dev)) { 1540a556c76aSAlexandre Belloni struct net_device *slave; 1541a556c76aSAlexandre Belloni struct list_head *iter; 1542a556c76aSAlexandre Belloni 1543a556c76aSAlexandre Belloni netdev_for_each_lower_dev(dev, slave, iter) { 1544a556c76aSAlexandre Belloni ret = ocelot_netdevice_port_event(slave, event, info); 1545a556c76aSAlexandre Belloni if (ret) 1546a556c76aSAlexandre Belloni goto notify; 1547a556c76aSAlexandre Belloni } 1548a556c76aSAlexandre Belloni } else { 1549a556c76aSAlexandre Belloni ret = ocelot_netdevice_port_event(dev, event, info); 1550a556c76aSAlexandre Belloni } 1551a556c76aSAlexandre Belloni 1552a556c76aSAlexandre Belloni notify: 1553a556c76aSAlexandre Belloni return notifier_from_errno(ret); 1554a556c76aSAlexandre Belloni } 1555a556c76aSAlexandre Belloni 1556a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = { 1557a556c76aSAlexandre Belloni .notifier_call = ocelot_netdevice_event, 1558a556c76aSAlexandre Belloni }; 1559a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb); 1560a556c76aSAlexandre Belloni 156156da64bcSFlorian Fainelli static int ocelot_switchdev_event(struct notifier_block *unused, 156256da64bcSFlorian Fainelli unsigned long event, void *ptr) 156356da64bcSFlorian Fainelli { 156456da64bcSFlorian Fainelli struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 156556da64bcSFlorian Fainelli int err; 156656da64bcSFlorian Fainelli 156756da64bcSFlorian Fainelli switch (event) { 156856da64bcSFlorian Fainelli case SWITCHDEV_PORT_ATTR_SET: 156956da64bcSFlorian Fainelli err = switchdev_handle_port_attr_set(dev, ptr, 157056da64bcSFlorian Fainelli ocelot_netdevice_dev_check, 157156da64bcSFlorian Fainelli ocelot_port_attr_set); 157256da64bcSFlorian Fainelli return notifier_from_errno(err); 157356da64bcSFlorian Fainelli } 157456da64bcSFlorian Fainelli 157556da64bcSFlorian Fainelli return NOTIFY_DONE; 157656da64bcSFlorian Fainelli } 157756da64bcSFlorian Fainelli 157856da64bcSFlorian Fainelli struct notifier_block ocelot_switchdev_nb __read_mostly = { 157956da64bcSFlorian Fainelli .notifier_call = ocelot_switchdev_event, 158056da64bcSFlorian Fainelli }; 158156da64bcSFlorian Fainelli EXPORT_SYMBOL(ocelot_switchdev_nb); 158256da64bcSFlorian Fainelli 15830e332c85SPetr Machata static int ocelot_switchdev_blocking_event(struct notifier_block *unused, 15840e332c85SPetr Machata unsigned long event, void *ptr) 15850e332c85SPetr Machata { 15860e332c85SPetr Machata struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 15870e332c85SPetr Machata int err; 15880e332c85SPetr Machata 15890e332c85SPetr Machata switch (event) { 15900e332c85SPetr Machata /* Blocking events. */ 15910e332c85SPetr Machata case SWITCHDEV_PORT_OBJ_ADD: 15920e332c85SPetr Machata err = switchdev_handle_port_obj_add(dev, ptr, 15930e332c85SPetr Machata ocelot_netdevice_dev_check, 15940e332c85SPetr Machata ocelot_port_obj_add); 15950e332c85SPetr Machata return notifier_from_errno(err); 15960e332c85SPetr Machata case SWITCHDEV_PORT_OBJ_DEL: 15970e332c85SPetr Machata err = switchdev_handle_port_obj_del(dev, ptr, 15980e332c85SPetr Machata ocelot_netdevice_dev_check, 15990e332c85SPetr Machata ocelot_port_obj_del); 16000e332c85SPetr Machata return notifier_from_errno(err); 160156da64bcSFlorian Fainelli case SWITCHDEV_PORT_ATTR_SET: 160256da64bcSFlorian Fainelli err = switchdev_handle_port_attr_set(dev, ptr, 160356da64bcSFlorian Fainelli ocelot_netdevice_dev_check, 160456da64bcSFlorian Fainelli ocelot_port_attr_set); 160556da64bcSFlorian Fainelli return notifier_from_errno(err); 16060e332c85SPetr Machata } 16070e332c85SPetr Machata 16080e332c85SPetr Machata return NOTIFY_DONE; 16090e332c85SPetr Machata } 16100e332c85SPetr Machata 16110e332c85SPetr Machata struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = { 16120e332c85SPetr Machata .notifier_call = ocelot_switchdev_blocking_event, 16130e332c85SPetr Machata }; 16140e332c85SPetr Machata EXPORT_SYMBOL(ocelot_switchdev_blocking_nb); 16150e332c85SPetr Machata 1616a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port, 1617a556c76aSAlexandre Belloni void __iomem *regs, 1618a556c76aSAlexandre Belloni struct phy_device *phy) 1619a556c76aSAlexandre Belloni { 1620a556c76aSAlexandre Belloni struct ocelot_port *ocelot_port; 1621a556c76aSAlexandre Belloni struct net_device *dev; 1622a556c76aSAlexandre Belloni int err; 1623a556c76aSAlexandre Belloni 1624a556c76aSAlexandre Belloni dev = alloc_etherdev(sizeof(struct ocelot_port)); 1625a556c76aSAlexandre Belloni if (!dev) 1626a556c76aSAlexandre Belloni return -ENOMEM; 1627a556c76aSAlexandre Belloni SET_NETDEV_DEV(dev, ocelot->dev); 1628a556c76aSAlexandre Belloni ocelot_port = netdev_priv(dev); 1629a556c76aSAlexandre Belloni ocelot_port->dev = dev; 1630a556c76aSAlexandre Belloni ocelot_port->ocelot = ocelot; 1631a556c76aSAlexandre Belloni ocelot_port->regs = regs; 1632a556c76aSAlexandre Belloni ocelot_port->chip_port = port; 1633a556c76aSAlexandre Belloni ocelot_port->phy = phy; 1634a556c76aSAlexandre Belloni ocelot->ports[port] = ocelot_port; 1635a556c76aSAlexandre Belloni 1636a556c76aSAlexandre Belloni dev->netdev_ops = &ocelot_port_netdev_ops; 1637a556c76aSAlexandre Belloni dev->ethtool_ops = &ocelot_ethtool_ops; 1638a556c76aSAlexandre Belloni 163960f8e67dSAntoine Tenart dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS; 16407142529fSAntoine Tenart dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 16417142529fSAntoine Tenart 1642a556c76aSAlexandre Belloni memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN); 1643a556c76aSAlexandre Belloni dev->dev_addr[ETH_ALEN - 1] += port; 1644a556c76aSAlexandre Belloni ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid, 1645a556c76aSAlexandre Belloni ENTRYTYPE_LOCKED); 1646a556c76aSAlexandre Belloni 1647a556c76aSAlexandre Belloni err = register_netdev(dev); 1648a556c76aSAlexandre Belloni if (err) { 1649a556c76aSAlexandre Belloni dev_err(ocelot->dev, "register_netdev failed\n"); 1650a556c76aSAlexandre Belloni goto err_register_netdev; 1651a556c76aSAlexandre Belloni } 1652a556c76aSAlexandre Belloni 16537142529fSAntoine Tenart /* Basic L2 initialization */ 16547142529fSAntoine Tenart ocelot_vlan_port_apply(ocelot, ocelot_port); 16557142529fSAntoine Tenart 1656a556c76aSAlexandre Belloni return 0; 1657a556c76aSAlexandre Belloni 1658a556c76aSAlexandre Belloni err_register_netdev: 1659a556c76aSAlexandre Belloni free_netdev(dev); 1660a556c76aSAlexandre Belloni return err; 1661a556c76aSAlexandre Belloni } 1662a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port); 1663a556c76aSAlexandre Belloni 1664a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot) 1665a556c76aSAlexandre Belloni { 1666a556c76aSAlexandre Belloni u32 port; 1667a556c76aSAlexandre Belloni int i, cpu = ocelot->num_phys_ports; 1668a556c76aSAlexandre Belloni char queue_name[32]; 1669a556c76aSAlexandre Belloni 1670dc96ee37SAlexandre Belloni ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports, 1671dc96ee37SAlexandre Belloni sizeof(u32), GFP_KERNEL); 1672dc96ee37SAlexandre Belloni if (!ocelot->lags) 1673dc96ee37SAlexandre Belloni return -ENOMEM; 1674dc96ee37SAlexandre Belloni 1675a556c76aSAlexandre Belloni ocelot->stats = devm_kcalloc(ocelot->dev, 1676a556c76aSAlexandre Belloni ocelot->num_phys_ports * ocelot->num_stats, 1677a556c76aSAlexandre Belloni sizeof(u64), GFP_KERNEL); 1678a556c76aSAlexandre Belloni if (!ocelot->stats) 1679a556c76aSAlexandre Belloni return -ENOMEM; 1680a556c76aSAlexandre Belloni 1681a556c76aSAlexandre Belloni mutex_init(&ocelot->stats_lock); 1682a556c76aSAlexandre Belloni snprintf(queue_name, sizeof(queue_name), "%s-stats", 1683a556c76aSAlexandre Belloni dev_name(ocelot->dev)); 1684a556c76aSAlexandre Belloni ocelot->stats_queue = create_singlethread_workqueue(queue_name); 1685a556c76aSAlexandre Belloni if (!ocelot->stats_queue) 1686a556c76aSAlexandre Belloni return -ENOMEM; 1687a556c76aSAlexandre Belloni 1688a556c76aSAlexandre Belloni ocelot_mact_init(ocelot); 1689a556c76aSAlexandre Belloni ocelot_vlan_init(ocelot); 1690a556c76aSAlexandre Belloni 1691a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 1692a556c76aSAlexandre Belloni /* Clear all counters (5 groups) */ 1693a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 1694a556c76aSAlexandre Belloni SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 1695a556c76aSAlexandre Belloni SYS_STAT_CFG); 1696a556c76aSAlexandre Belloni } 1697a556c76aSAlexandre Belloni 1698a556c76aSAlexandre Belloni /* Only use S-Tag */ 1699a556c76aSAlexandre Belloni ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 1700a556c76aSAlexandre Belloni 1701a556c76aSAlexandre Belloni /* Aggregation mode */ 1702a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 1703a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_DMAC_ENA | 1704a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 1705a556c76aSAlexandre Belloni ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG); 1706a556c76aSAlexandre Belloni 1707a556c76aSAlexandre Belloni /* Set MAC age time to default value. The entry is aged after 1708a556c76aSAlexandre Belloni * 2*AGE_PERIOD 1709a556c76aSAlexandre Belloni */ 1710a556c76aSAlexandre Belloni ocelot_write(ocelot, 1711a556c76aSAlexandre Belloni ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 1712a556c76aSAlexandre Belloni ANA_AUTOAGE); 1713a556c76aSAlexandre Belloni 1714a556c76aSAlexandre Belloni /* Disable learning for frames discarded by VLAN ingress filtering */ 1715a556c76aSAlexandre Belloni regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 1716a556c76aSAlexandre Belloni 1717a556c76aSAlexandre Belloni /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 1718a556c76aSAlexandre Belloni ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 1719a556c76aSAlexandre Belloni SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 1720a556c76aSAlexandre Belloni 1721a556c76aSAlexandre Belloni /* Setup flooding PGIDs */ 1722a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 1723a556c76aSAlexandre Belloni ANA_FLOODING_FLD_BROADCAST(PGID_MC) | 1724a556c76aSAlexandre Belloni ANA_FLOODING_FLD_UNICAST(PGID_UC), 1725a556c76aSAlexandre Belloni ANA_FLOODING, 0); 1726a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 1727a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 1728a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 1729a556c76aSAlexandre Belloni ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 1730a556c76aSAlexandre Belloni ANA_FLOODING_IPMC); 1731a556c76aSAlexandre Belloni 1732a556c76aSAlexandre Belloni for (port = 0; port < ocelot->num_phys_ports; port++) { 1733a556c76aSAlexandre Belloni /* Transmit the frame to the local port. */ 1734a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1735a556c76aSAlexandre Belloni /* Do not forward BPDU frames to the front ports. */ 1736a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, 1737a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 1738a556c76aSAlexandre Belloni ANA_PORT_CPU_FWD_BPDU_CFG, 1739a556c76aSAlexandre Belloni port); 1740a556c76aSAlexandre Belloni /* Ensure bridging is disabled */ 1741a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 1742a556c76aSAlexandre Belloni } 1743a556c76aSAlexandre Belloni 1744a556c76aSAlexandre Belloni /* Configure and enable the CPU port. */ 1745a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 1746a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 1747a556c76aSAlexandre Belloni ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 1748a556c76aSAlexandre Belloni ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 1749a556c76aSAlexandre Belloni ANA_PORT_PORT_CFG, cpu); 1750a556c76aSAlexandre Belloni 1751a556c76aSAlexandre Belloni /* Allow broadcast MAC frames. */ 1752a556c76aSAlexandre Belloni for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) { 1753a556c76aSAlexandre Belloni u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 1754a556c76aSAlexandre Belloni 1755a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 1756a556c76aSAlexandre Belloni } 1757a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 1758a556c76aSAlexandre Belloni ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)), 1759a556c76aSAlexandre Belloni ANA_PGID_PGID, PGID_MC); 1760a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 1761a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 1762a556c76aSAlexandre Belloni 1763a556c76aSAlexandre Belloni /* CPU port Injection/Extraction configuration */ 1764a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE | 1765a556c76aSAlexandre Belloni QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) | 1766a556c76aSAlexandre Belloni QSYS_SWITCH_PORT_MODE_PORT_ENA, 1767a556c76aSAlexandre Belloni QSYS_SWITCH_PORT_MODE, cpu); 1768a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) | 1769a556c76aSAlexandre Belloni SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu); 1770a556c76aSAlexandre Belloni /* Allow manual injection via DEVCPU_QS registers, and byte swap these 1771a556c76aSAlexandre Belloni * registers endianness. 1772a556c76aSAlexandre Belloni */ 1773a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 1774a556c76aSAlexandre Belloni QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 1775a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 1776a556c76aSAlexandre Belloni QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 1777a556c76aSAlexandre Belloni ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 1778a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LRN(2) | 1779a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 1780a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 1781a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 1782a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 1783a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 1784a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_IGMP(6) | 1785a556c76aSAlexandre Belloni ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 1786a556c76aSAlexandre Belloni for (i = 0; i < 16; i++) 1787a556c76aSAlexandre Belloni ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 1788a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 1789a556c76aSAlexandre Belloni ANA_CPUQ_8021_CFG, i); 1790a556c76aSAlexandre Belloni 17911e1caa97SClaudiu Manoil INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 1792a556c76aSAlexandre Belloni queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1793a556c76aSAlexandre Belloni OCELOT_STATS_CHECK_DELAY); 1794a556c76aSAlexandre Belloni return 0; 1795a556c76aSAlexandre Belloni } 1796a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init); 1797a556c76aSAlexandre Belloni 1798a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot) 1799a556c76aSAlexandre Belloni { 1800a556c76aSAlexandre Belloni destroy_workqueue(ocelot->stats_queue); 1801a556c76aSAlexandre Belloni mutex_destroy(&ocelot->stats_lock); 1802a556c76aSAlexandre Belloni } 1803a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit); 1804a556c76aSAlexandre Belloni 1805a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL"); 1806