12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 291da11f8SLennert Buytenhek /* 391da11f8SLennert Buytenhek * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips 4e84665c9SLennert Buytenhek * Copyright (c) 2008-2009 Marvell Semiconductor 591da11f8SLennert Buytenhek */ 691da11f8SLennert Buytenhek 791da11f8SLennert Buytenhek #ifndef __LINUX_NET_DSA_H 891da11f8SLennert Buytenhek #define __LINUX_NET_DSA_H 991da11f8SLennert Buytenhek 104d56a29fSRussell King #include <linux/if.h> 11ea1f51beSAxel Lin #include <linux/if_ether.h> 12c8f0b869SBen Hutchings #include <linux/list.h> 13f515f192SVivien Didelot #include <linux/notifier.h> 14cf50dcc2SBen Hutchings #include <linux/timer.h> 15cf50dcc2SBen Hutchings #include <linux/workqueue.h> 16fa981d9aSFlorian Fainelli #include <linux/of.h> 17a2820543SFlorian Fainelli #include <linux/ethtool.h> 180336369dSBrandon Streiff #include <linux/net_tstamp.h> 1911d8f3ddSFlorian Fainelli #include <linux/phy.h> 20ecfc9372SFlorian Fainelli #include <linux/platform_data/dsa.h> 2144cc27e4SIoana Ciornei #include <linux/phylink.h> 2296567d5dSAndrew Lunn #include <net/devlink.h> 23f0c24ccfSVivien Didelot #include <net/switchdev.h> 24cf50dcc2SBen Hutchings 25f50f2127SFlorian Fainelli struct tc_action; 264d56a29fSRussell King struct phy_device; 274d56a29fSRussell King struct fixed_phy_status; 2811d8f3ddSFlorian Fainelli struct phylink_link_state; 29f50f2127SFlorian Fainelli 300b42f033SAndrew Lunn #define DSA_TAG_PROTO_NONE_VALUE 0 310b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_VALUE 1 320b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE 2 330b42f033SAndrew Lunn #define DSA_TAG_PROTO_DSA_VALUE 3 340b42f033SAndrew Lunn #define DSA_TAG_PROTO_EDSA_VALUE 4 350b42f033SAndrew Lunn #define DSA_TAG_PROTO_GSWIP_VALUE 5 360b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9477_VALUE 6 370b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9893_VALUE 7 380b42f033SAndrew Lunn #define DSA_TAG_PROTO_LAN9303_VALUE 8 390b42f033SAndrew Lunn #define DSA_TAG_PROTO_MTK_VALUE 9 400b42f033SAndrew Lunn #define DSA_TAG_PROTO_QCA_VALUE 10 410b42f033SAndrew Lunn #define DSA_TAG_PROTO_TRAILER_VALUE 11 42f9bbe447SVladimir Oltean #define DSA_TAG_PROTO_8021Q_VALUE 12 43227d07a0SVladimir Oltean #define DSA_TAG_PROTO_SJA1105_VALUE 13 44016e43a2STristram Ha #define DSA_TAG_PROTO_KSZ8795_VALUE 14 458dce89aaSVladimir Oltean #define DSA_TAG_PROTO_OCELOT_VALUE 15 4648fda74fSOleksij Rempel #define DSA_TAG_PROTO_AR9331_VALUE 16 47efd7fe68SLinus Walleij #define DSA_TAG_PROTO_RTL4_A_VALUE 17 4801ef09caSKurt Kanzenbach #define DSA_TAG_PROTO_HELLCREEK_VALUE 18 4954a52823SGeorge McCollister #define DSA_TAG_PROTO_XRS700X_VALUE 19 507c83a7c5SVladimir Oltean #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE 20 517c4bb540SVladimir Oltean #define DSA_TAG_PROTO_SEVILLE_VALUE 21 52964dbf18SÁlvaro Fernández Rojas #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE 22 534913b8ebSVladimir Oltean #define DSA_TAG_PROTO_SJA1110_VALUE 23 541521d5adSAlvin Šipraga #define DSA_TAG_PROTO_RTL8_4_VALUE 24 550b42f033SAndrew Lunn 56ac7a04c3SFlorian Fainelli enum dsa_tag_protocol { 570b42f033SAndrew Lunn DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE, 580b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE, 59964dbf18SÁlvaro Fernández Rojas DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE, 600b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE, 610b42f033SAndrew Lunn DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE, 620b42f033SAndrew Lunn DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE, 630b42f033SAndrew Lunn DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE, 640b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE, 650b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE, 660b42f033SAndrew Lunn DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE, 670b42f033SAndrew Lunn DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE, 680b42f033SAndrew Lunn DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE, 690b42f033SAndrew Lunn DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE, 70f9bbe447SVladimir Oltean DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE, 71227d07a0SVladimir Oltean DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE, 72016e43a2STristram Ha DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE, 738dce89aaSVladimir Oltean DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE, 7448fda74fSOleksij Rempel DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE, 75efd7fe68SLinus Walleij DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE, 7601ef09caSKurt Kanzenbach DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE, 7754a52823SGeorge McCollister DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE, 787c83a7c5SVladimir Oltean DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE, 797c4bb540SVladimir Oltean DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE, 804913b8ebSVladimir Oltean DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE, 811521d5adSAlvin Šipraga DSA_TAG_PROTO_RTL8_4 = DSA_TAG_PROTO_RTL8_4_VALUE, 82ac7a04c3SFlorian Fainelli }; 835037d532SFlorian Fainelli 8490af1059SBrandon Streiff struct dsa_switch; 853e8a72d1SFlorian Fainelli 8668277a2cSJohn Crispin struct dsa_device_ops { 8768277a2cSJohn Crispin struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev); 8829a097b7SVladimir Oltean struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev); 892e8cb1b3SVladimir Oltean void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto, 90598a9680SJohn Crispin int *offset); 917f297314SVladimir Oltean int (*connect)(struct dsa_switch *ds); 927f297314SVladimir Oltean void (*disconnect)(struct dsa_switch *ds); 934e500251SVladimir Oltean unsigned int needed_headroom; 944e500251SVladimir Oltean unsigned int needed_tailroom; 95875138f8SAndrew Lunn const char *name; 96056eed2fSAndrew Lunn enum dsa_tag_protocol proto; 97c3975400SVladimir Oltean /* Some tagging protocols either mangle or shift the destination MAC 98c3975400SVladimir Oltean * address, in which case the DSA master would drop packets on ingress 99c3975400SVladimir Oltean * if what it understands out of the destination MAC address is not in 100c3975400SVladimir Oltean * its RX filter. 101c3975400SVladimir Oltean */ 102c3975400SVladimir Oltean bool promisc_on_master; 10368277a2cSJohn Crispin }; 10468277a2cSJohn Crispin 1054cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the 1064cfab356SFlorian Fainelli * DSA layer on top of the DSA CPU/management net_device instance. This is 1074cfab356SFlorian Fainelli * used by the core net_device layer while calling various net_device_ops 1084cfab356SFlorian Fainelli * function pointers. 1094cfab356SFlorian Fainelli */ 1104cfab356SFlorian Fainelli struct dsa_netdevice_ops { 111a7605370SArnd Bergmann int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr, 1124cfab356SFlorian Fainelli int cmd); 1134cfab356SFlorian Fainelli }; 1144cfab356SFlorian Fainelli 1150b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-" 1160b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \ 1170b42f033SAndrew Lunn MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE)) 1180b42f033SAndrew Lunn 119*dedd6a00SVladimir Oltean struct dsa_lag { 120*dedd6a00SVladimir Oltean struct net_device *dev; 121*dedd6a00SVladimir Oltean unsigned int id; 122*dedd6a00SVladimir Oltean refcount_t refcount; 123*dedd6a00SVladimir Oltean }; 124*dedd6a00SVladimir Oltean 125cf50dcc2SBen Hutchings struct dsa_switch_tree { 12683c0afaeSAndrew Lunn struct list_head list; 12783c0afaeSAndrew Lunn 128b035c88cSVladimir Oltean /* List of switch ports */ 129b035c88cSVladimir Oltean struct list_head ports; 130b035c88cSVladimir Oltean 131f515f192SVivien Didelot /* Notifier chain for switch-wide events */ 132f515f192SVivien Didelot struct raw_notifier_head nh; 133f515f192SVivien Didelot 13483c0afaeSAndrew Lunn /* Tree identifier */ 13549463b7fSVivien Didelot unsigned int index; 13683c0afaeSAndrew Lunn 13783c0afaeSAndrew Lunn /* Number of switches attached to this tree */ 13883c0afaeSAndrew Lunn struct kref refcount; 13983c0afaeSAndrew Lunn 140b035c88cSVladimir Oltean /* Maps offloaded LAG netdevs to a zero-based linear ID for 141b035c88cSVladimir Oltean * drivers that need it. 142b035c88cSVladimir Oltean */ 143*dedd6a00SVladimir Oltean struct dsa_lag **lags; 144b035c88cSVladimir Oltean 145357f203bSVladimir Oltean /* Tagging protocol operations */ 146357f203bSVladimir Oltean const struct dsa_device_ops *tag_ops; 147357f203bSVladimir Oltean 148deff7107STobias Waldekranz /* Default tagging protocol preferred by the switches in this 149deff7107STobias Waldekranz * tree. 150deff7107STobias Waldekranz */ 151deff7107STobias Waldekranz enum dsa_tag_protocol default_proto; 152deff7107STobias Waldekranz 1534b026e82SVladimir Oltean /* Has this tree been applied to the hardware? */ 1544b026e82SVladimir Oltean bool setup; 1554b026e82SVladimir Oltean 156cf50dcc2SBen Hutchings /* 157cf50dcc2SBen Hutchings * Configuration data for the platform device that owns 158cf50dcc2SBen Hutchings * this dsa switch tree instance. 159cf50dcc2SBen Hutchings */ 160cf50dcc2SBen Hutchings struct dsa_platform_data *pd; 161cf85d08fSLennert Buytenhek 162c5f51765SVivien Didelot /* List of DSA links composing the routing table */ 163c5f51765SVivien Didelot struct list_head rtable; 164058102a6STobias Waldekranz 165b035c88cSVladimir Oltean /* Length of "lags" array */ 166058102a6STobias Waldekranz unsigned int lags_len; 1675b22d366SVladimir Oltean 1685b22d366SVladimir Oltean /* Track the largest switch index within a tree */ 1695b22d366SVladimir Oltean unsigned int last_switch; 170cf50dcc2SBen Hutchings }; 171cf50dcc2SBen Hutchings 1723d4a0a2aSVladimir Oltean /* LAG IDs are one-based, the dst->lags array is zero-based */ 173058102a6STobias Waldekranz #define dsa_lags_foreach_id(_id, _dst) \ 1743d4a0a2aSVladimir Oltean for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \ 1753d4a0a2aSVladimir Oltean if ((_dst)->lags[(_id) - 1]) 176058102a6STobias Waldekranz 177058102a6STobias Waldekranz #define dsa_lag_foreach_port(_dp, _dst, _lag) \ 178058102a6STobias Waldekranz list_for_each_entry((_dp), &(_dst)->ports, list) \ 179*dedd6a00SVladimir Oltean if (dsa_port_offloads_lag((_dp), (_lag))) 180058102a6STobias Waldekranz 18118596f50SGeorge McCollister #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \ 18218596f50SGeorge McCollister list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 18318596f50SGeorge McCollister if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr)) 18418596f50SGeorge McCollister 185*dedd6a00SVladimir Oltean static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst, 186058102a6STobias Waldekranz unsigned int id) 187058102a6STobias Waldekranz { 1883d4a0a2aSVladimir Oltean /* DSA LAG IDs are one-based, dst->lags is zero-based */ 1893d4a0a2aSVladimir Oltean return dst->lags[id - 1]; 190058102a6STobias Waldekranz } 191058102a6STobias Waldekranz 192058102a6STobias Waldekranz static inline int dsa_lag_id(struct dsa_switch_tree *dst, 19346a76724SVladimir Oltean struct net_device *lag_dev) 194058102a6STobias Waldekranz { 195058102a6STobias Waldekranz unsigned int id; 196058102a6STobias Waldekranz 197058102a6STobias Waldekranz dsa_lags_foreach_id(id, dst) { 198*dedd6a00SVladimir Oltean struct dsa_lag *lag = dsa_lag_by_id(dst, id); 199*dedd6a00SVladimir Oltean 200*dedd6a00SVladimir Oltean if (lag->dev == lag_dev) 201*dedd6a00SVladimir Oltean return lag->id; 202058102a6STobias Waldekranz } 203058102a6STobias Waldekranz 204058102a6STobias Waldekranz return -ENODEV; 205058102a6STobias Waldekranz } 206058102a6STobias Waldekranz 20734297176SVladimir Oltean /* TC matchall action types */ 208f50f2127SFlorian Fainelli enum dsa_port_mall_action_type { 209f50f2127SFlorian Fainelli DSA_PORT_MALL_MIRROR, 21034297176SVladimir Oltean DSA_PORT_MALL_POLICER, 211f50f2127SFlorian Fainelli }; 212f50f2127SFlorian Fainelli 213f50f2127SFlorian Fainelli /* TC mirroring entry */ 214f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry { 215f50f2127SFlorian Fainelli u8 to_local_port; 216f50f2127SFlorian Fainelli bool ingress; 217f50f2127SFlorian Fainelli }; 218f50f2127SFlorian Fainelli 21934297176SVladimir Oltean /* TC port policer entry */ 22034297176SVladimir Oltean struct dsa_mall_policer_tc_entry { 2215f035af7SPo Liu u32 burst; 22234297176SVladimir Oltean u64 rate_bytes_per_sec; 22334297176SVladimir Oltean }; 22434297176SVladimir Oltean 225f50f2127SFlorian Fainelli /* TC matchall entry */ 226f50f2127SFlorian Fainelli struct dsa_mall_tc_entry { 227f50f2127SFlorian Fainelli struct list_head list; 228f50f2127SFlorian Fainelli unsigned long cookie; 229f50f2127SFlorian Fainelli enum dsa_port_mall_action_type type; 230f50f2127SFlorian Fainelli union { 231f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry mirror; 23234297176SVladimir Oltean struct dsa_mall_policer_tc_entry policer; 233f50f2127SFlorian Fainelli }; 234f50f2127SFlorian Fainelli }; 235f50f2127SFlorian Fainelli 236d3eed0e5SVladimir Oltean struct dsa_bridge { 237d3eed0e5SVladimir Oltean struct net_device *dev; 238d3eed0e5SVladimir Oltean unsigned int num; 239857fdd74SVladimir Oltean bool tx_fwd_offload; 240d3eed0e5SVladimir Oltean refcount_t refcount; 241d3eed0e5SVladimir Oltean }; 242f50f2127SFlorian Fainelli 243c8b09808SAndrew Lunn struct dsa_port { 244f8b8b1cdSVivien Didelot /* A CPU port is physically connected to a master device. 245f8b8b1cdSVivien Didelot * A user port exposed to userspace has a slave device. 246f8b8b1cdSVivien Didelot */ 247f8b8b1cdSVivien Didelot union { 248f8b8b1cdSVivien Didelot struct net_device *master; 249f8b8b1cdSVivien Didelot struct net_device *slave; 250f8b8b1cdSVivien Didelot }; 251f8b8b1cdSVivien Didelot 252357f203bSVladimir Oltean /* Copy of the tagging protocol operations, for quicker access 253357f203bSVladimir Oltean * in the data path. Valid only for the CPU ports. 254357f203bSVladimir Oltean */ 25515240248SVivien Didelot const struct dsa_device_ops *tag_ops; 25615240248SVivien Didelot 2573e41f93bSVivien Didelot /* Copies for faster access in master receive hot path */ 2583e41f93bSVivien Didelot struct dsa_switch_tree *dst; 25929a097b7SVladimir Oltean struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev); 2603e41f93bSVivien Didelot 26106251258SVladimir Oltean struct dsa_switch *ds; 26206251258SVladimir Oltean 26306251258SVladimir Oltean unsigned int index; 26406251258SVladimir Oltean 265057cad2cSVivien Didelot enum { 266057cad2cSVivien Didelot DSA_PORT_TYPE_UNUSED = 0, 267057cad2cSVivien Didelot DSA_PORT_TYPE_CPU, 268057cad2cSVivien Didelot DSA_PORT_TYPE_DSA, 269057cad2cSVivien Didelot DSA_PORT_TYPE_USER, 270057cad2cSVivien Didelot } type; 271057cad2cSVivien Didelot 27271e0bbdeSFlorian Fainelli const char *name; 27368b2d4a8SVivien Didelot struct dsa_port *cpu_dp; 27483216e39SMichael Walle u8 mac[ETH_ALEN]; 275b08db33dSVladimir Oltean 276b08db33dSVladimir Oltean u8 stp_state; 277b08db33dSVladimir Oltean 2781b26d364SVladimir Oltean /* Warning: the following bit fields are not atomic, and updating them 2791b26d364SVladimir Oltean * can only be done from code paths where concurrency is not possible 2801b26d364SVladimir Oltean * (probe time or under rtnl_lock). 2811b26d364SVladimir Oltean */ 28263cfc657SVladimir Oltean u8 vlan_filtering:1; 28363cfc657SVladimir Oltean 28463cfc657SVladimir Oltean /* Managed by DSA on user ports and by drivers on CPU and DSA ports */ 28563cfc657SVladimir Oltean u8 learning:1; 28663cfc657SVladimir Oltean 28763cfc657SVladimir Oltean u8 lag_tx_enabled:1; 28863cfc657SVladimir Oltean 28963cfc657SVladimir Oltean u8 devlink_port_setup:1; 29063cfc657SVladimir Oltean 291295ab96fSVladimir Oltean /* Master state bits, valid only on CPU ports */ 292295ab96fSVladimir Oltean u8 master_admin_up:1; 293295ab96fSVladimir Oltean u8 master_oper_up:1; 294295ab96fSVladimir Oltean 29563cfc657SVladimir Oltean u8 setup:1; 296bde82f38SVladimir Oltean 297189b0d93SAndrew Lunn struct device_node *dn; 29834a79f63SVivien Didelot unsigned int ageing_time; 299bde82f38SVladimir Oltean 300d3eed0e5SVladimir Oltean struct dsa_bridge *bridge; 30196567d5dSAndrew Lunn struct devlink_port devlink_port; 302aab9c406SFlorian Fainelli struct phylink *pl; 30344cc27e4SIoana Ciornei struct phylink_config pl_config; 304*dedd6a00SVladimir Oltean struct dsa_lag *lag; 30518596f50SGeorge McCollister struct net_device *hsr_dev; 30697a69a0dSVladimir Oltean 307ab8ccae1SVivien Didelot struct list_head list; 308ab8ccae1SVivien Didelot 30967dbb9d4SFlorian Fainelli /* 31067dbb9d4SFlorian Fainelli * Original copy of the master netdev ethtool_ops 31167dbb9d4SFlorian Fainelli */ 31267dbb9d4SFlorian Fainelli const struct ethtool_ops *orig_ethtool_ops; 313da7b9e9bSFlorian Fainelli 314da7b9e9bSFlorian Fainelli /* 315da7b9e9bSFlorian Fainelli * Original copy of the master netdev net_device_ops 316da7b9e9bSFlorian Fainelli */ 3174cfab356SFlorian Fainelli const struct dsa_netdevice_ops *netdev_ops; 318fb35c60cSVivien Didelot 319161ca59dSVladimir Oltean /* List of MAC addresses that must be forwarded on this port. 320161ca59dSVladimir Oltean * These are only valid on CPU ports and DSA links. 321161ca59dSVladimir Oltean */ 322338a3a47SVladimir Oltean struct mutex addr_lists_lock; 3233f6e32f9SVladimir Oltean struct list_head fdbs; 324161ca59dSVladimir Oltean struct list_head mdbs; 325134ef238SVladimir Oltean 326134ef238SVladimir Oltean /* List of VLANs that CPU and DSA ports are members of. */ 327134ef238SVladimir Oltean struct mutex vlans_lock; 328134ef238SVladimir Oltean struct list_head vlans; 329c8b09808SAndrew Lunn }; 330c8b09808SAndrew Lunn 331c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member, 332c5f51765SVivien Didelot * and no dst->rtable nor this struct dsa_link would be needed, 333c5f51765SVivien Didelot * but this would require some more complex tree walking, 334c5f51765SVivien Didelot * so keep it stupid at the moment and list them all. 335c5f51765SVivien Didelot */ 336c5f51765SVivien Didelot struct dsa_link { 337c5f51765SVivien Didelot struct dsa_port *dp; 338c5f51765SVivien Didelot struct dsa_port *link_dp; 339c5f51765SVivien Didelot struct list_head list; 340c5f51765SVivien Didelot }; 341c5f51765SVivien Didelot 342161ca59dSVladimir Oltean struct dsa_mac_addr { 343161ca59dSVladimir Oltean unsigned char addr[ETH_ALEN]; 344161ca59dSVladimir Oltean u16 vid; 345161ca59dSVladimir Oltean refcount_t refcount; 346161ca59dSVladimir Oltean struct list_head list; 347161ca59dSVladimir Oltean }; 348161ca59dSVladimir Oltean 349134ef238SVladimir Oltean struct dsa_vlan { 350134ef238SVladimir Oltean u16 vid; 351134ef238SVladimir Oltean refcount_t refcount; 352134ef238SVladimir Oltean struct list_head list; 353134ef238SVladimir Oltean }; 354134ef238SVladimir Oltean 355c8f0b869SBen Hutchings struct dsa_switch { 356c33063d6SAndrew Lunn struct device *dev; 357c33063d6SAndrew Lunn 358c8f0b869SBen Hutchings /* 359c8f0b869SBen Hutchings * Parent switch tree, and switch index. 360c8f0b869SBen Hutchings */ 361c8f0b869SBen Hutchings struct dsa_switch_tree *dst; 36299feaafcSVivien Didelot unsigned int index; 363c8f0b869SBen Hutchings 3641b26d364SVladimir Oltean /* Warning: the following bit fields are not atomic, and updating them 3651b26d364SVladimir Oltean * can only be done from code paths where concurrency is not possible 3661b26d364SVladimir Oltean * (probe time or under rtnl_lock). 3671b26d364SVladimir Oltean */ 36863cfc657SVladimir Oltean u32 setup:1; 36963cfc657SVladimir Oltean 37063cfc657SVladimir Oltean /* Disallow bridge core from requesting different VLAN awareness 37163cfc657SVladimir Oltean * settings on ports if not hardware-supported 3727787ff77SVladimir Oltean */ 37363cfc657SVladimir Oltean u32 vlan_filtering_is_global:1; 37463cfc657SVladimir Oltean 37563cfc657SVladimir Oltean /* Keep VLAN filtering enabled on ports not offloading any upper */ 37663cfc657SVladimir Oltean u32 needs_standalone_vlan_filtering:1; 37763cfc657SVladimir Oltean 37863cfc657SVladimir Oltean /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 37963cfc657SVladimir Oltean * that have vlan_filtering=0. All drivers should ideally set this (and 38063cfc657SVladimir Oltean * then the option would get removed), but it is unknown whether this 3817787ff77SVladimir Oltean * would break things or not. 3827787ff77SVladimir Oltean */ 38363cfc657SVladimir Oltean u32 configure_vlan_while_not_filtering:1; 38463cfc657SVladimir Oltean 38563cfc657SVladimir Oltean /* If the switch driver always programs the CPU port as egress tagged 38663cfc657SVladimir Oltean * despite the VLAN configuration indicating otherwise, then setting 38763cfc657SVladimir Oltean * @untag_bridge_pvid will force the DSA receive path to pop the 38863cfc657SVladimir Oltean * bridge's default_pvid VLAN tagged frames to offer a consistent 38963cfc657SVladimir Oltean * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge 3907787ff77SVladimir Oltean * device. 3917787ff77SVladimir Oltean */ 39263cfc657SVladimir Oltean u32 untag_bridge_pvid:1; 39363cfc657SVladimir Oltean 3947787ff77SVladimir Oltean /* Let DSA manage the FDB entries towards the 3957787ff77SVladimir Oltean * CPU, based on the software bridge database. 3967787ff77SVladimir Oltean */ 39763cfc657SVladimir Oltean u32 assisted_learning_on_cpu_port:1; 39863cfc657SVladimir Oltean 39963cfc657SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 40063cfc657SVladimir Oltean * should be retrieved from here and not from the per-port settings. 4017787ff77SVladimir Oltean */ 40263cfc657SVladimir Oltean u32 vlan_filtering:1; 40363cfc657SVladimir Oltean 40463cfc657SVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 40563cfc657SVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 40663cfc657SVladimir Oltean * interfaces is needed. 4077787ff77SVladimir Oltean */ 40863cfc657SVladimir Oltean u32 mtu_enforcement_ingress:1; 4097787ff77SVladimir Oltean 410f515f192SVivien Didelot /* Listener for switch fabric events */ 411f515f192SVivien Didelot struct notifier_block nb; 412f515f192SVivien Didelot 413c8f0b869SBen Hutchings /* 4147543a6d5SAndrew Lunn * Give the switch driver somewhere to hang its private data 4157543a6d5SAndrew Lunn * structure. 4167543a6d5SAndrew Lunn */ 4177543a6d5SAndrew Lunn void *priv; 4187543a6d5SAndrew Lunn 419dc452a47SVladimir Oltean void *tagger_data; 420dc452a47SVladimir Oltean 4217543a6d5SAndrew Lunn /* 422c8f0b869SBen Hutchings * Configuration data for this switch. 423c8f0b869SBen Hutchings */ 424ff04955cSAndrew Lunn struct dsa_chip_data *cd; 425c8f0b869SBen Hutchings 426c8f0b869SBen Hutchings /* 4279d490b4eSVivien Didelot * The switch operations. 428c8f0b869SBen Hutchings */ 429a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 430c8f0b869SBen Hutchings 43166472fc0SAndrew Lunn /* 432c8f0b869SBen Hutchings * Slave mii_bus and devices for the individual ports. 433c8f0b869SBen Hutchings */ 4340d8bcdd3SFlorian Fainelli u32 phys_mii_mask; 435c8f0b869SBen Hutchings struct mii_bus *slave_mii_bus; 436a0c02161SVivien Didelot 4370f3da6afSVivien Didelot /* Ageing Time limits in msecs */ 4380f3da6afSVivien Didelot unsigned int ageing_time_min; 4390f3da6afSVivien Didelot unsigned int ageing_time_max; 4400f3da6afSVivien Didelot 441d7b1fd52SVladimir Oltean /* Storage for drivers using tag_8021q */ 442d7b1fd52SVladimir Oltean struct dsa_8021q_context *tag_8021q_ctx; 443d7b1fd52SVladimir Oltean 44496567d5dSAndrew Lunn /* devlink used to represent this switch device */ 44596567d5dSAndrew Lunn struct devlink *devlink; 44696567d5dSAndrew Lunn 44755199df6SFlorian Fainelli /* Number of switch port queues */ 44855199df6SFlorian Fainelli unsigned int num_tx_queues; 44955199df6SFlorian Fainelli 450058102a6STobias Waldekranz /* Drivers that benefit from having an ID associated with each 451058102a6STobias Waldekranz * offloaded LAG should set this to the maximum number of 452058102a6STobias Waldekranz * supported IDs. DSA will then maintain a mapping of _at 453058102a6STobias Waldekranz * least_ these many IDs, accessible to drivers via 454058102a6STobias Waldekranz * dsa_lag_id(). 455058102a6STobias Waldekranz */ 456058102a6STobias Waldekranz unsigned int num_lag_ids; 457058102a6STobias Waldekranz 458947c8746SVladimir Oltean /* Drivers that support bridge forwarding offload or FDB isolation 459947c8746SVladimir Oltean * should set this to the maximum number of bridges spanning the same 460947c8746SVladimir Oltean * switch tree (or all trees, in the case of cross-tree bridging 461947c8746SVladimir Oltean * support) that can be offloaded. 462123abc06SVladimir Oltean */ 463947c8746SVladimir Oltean unsigned int max_num_bridges; 464123abc06SVladimir Oltean 465258030acSVladimir Oltean unsigned int num_ports; 466c8f0b869SBen Hutchings }; 467c8f0b869SBen Hutchings 46868bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 469c38c5a66SVivien Didelot { 470b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 471d607525bSVivien Didelot struct dsa_port *dp; 472b96ddf25SVivien Didelot 473b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 474b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 475b96ddf25SVivien Didelot return dp; 476d607525bSVivien Didelot 477d607525bSVivien Didelot return NULL; 478c38c5a66SVivien Didelot } 479c38c5a66SVivien Didelot 480a8986681SVladimir Oltean static inline bool dsa_port_is_dsa(struct dsa_port *port) 481a8986681SVladimir Oltean { 482a8986681SVladimir Oltean return port->type == DSA_PORT_TYPE_DSA; 483a8986681SVladimir Oltean } 484a8986681SVladimir Oltean 485a8986681SVladimir Oltean static inline bool dsa_port_is_cpu(struct dsa_port *port) 486a8986681SVladimir Oltean { 487a8986681SVladimir Oltean return port->type == DSA_PORT_TYPE_CPU; 488a8986681SVladimir Oltean } 489a8986681SVladimir Oltean 490a8986681SVladimir Oltean static inline bool dsa_port_is_user(struct dsa_port *dp) 491a8986681SVladimir Oltean { 492a8986681SVladimir Oltean return dp->type == DSA_PORT_TYPE_USER; 493a8986681SVladimir Oltean } 494a8986681SVladimir Oltean 495a57d8c21SVladimir Oltean static inline bool dsa_port_is_unused(struct dsa_port *dp) 496a57d8c21SVladimir Oltean { 497a57d8c21SVladimir Oltean return dp->type == DSA_PORT_TYPE_UNUSED; 498a57d8c21SVladimir Oltean } 499a57d8c21SVladimir Oltean 500295ab96fSVladimir Oltean static inline bool dsa_port_master_is_operational(struct dsa_port *dp) 501295ab96fSVladimir Oltean { 502295ab96fSVladimir Oltean return dsa_port_is_cpu(dp) && dp->master_admin_up && 503295ab96fSVladimir Oltean dp->master_oper_up; 504295ab96fSVladimir Oltean } 505295ab96fSVladimir Oltean 506bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 507bff7b688SVivien Didelot { 508c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 509bff7b688SVivien Didelot } 510bff7b688SVivien Didelot 511c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 512c8f0b869SBen Hutchings { 513c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 514c8f0b869SBen Hutchings } 515c8f0b869SBen Hutchings 51660045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 51760045cbfSAndrew Lunn { 518c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 51960045cbfSAndrew Lunn } 52060045cbfSAndrew Lunn 5212b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 5226cd456f3SVivien Didelot { 523c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 5246cd456f3SVivien Didelot } 5256cd456f3SVivien Didelot 52682b31898SVladimir Oltean #define dsa_tree_for_each_user_port(_dp, _dst) \ 52782b31898SVladimir Oltean list_for_each_entry((_dp), &(_dst)->ports, list) \ 52882b31898SVladimir Oltean if (dsa_port_is_user((_dp))) 52982b31898SVladimir Oltean 53082b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \ 53182b31898SVladimir Oltean list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 53282b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 53382b31898SVladimir Oltean 53482b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \ 53582b31898SVladimir Oltean list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \ 53682b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 53782b31898SVladimir Oltean 53882b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \ 53982b31898SVladimir Oltean list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \ 54082b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 54182b31898SVladimir Oltean 54282b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \ 54382b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 54482b31898SVladimir Oltean if (!dsa_port_is_unused((_dp))) 54582b31898SVladimir Oltean 54682b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \ 54782b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 54882b31898SVladimir Oltean if (dsa_port_is_user((_dp))) 54982b31898SVladimir Oltean 55082b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \ 55182b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 55282b31898SVladimir Oltean if (dsa_port_is_cpu((_dp))) 55382b31898SVladimir Oltean 55402bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 55502bc6e54SVivien Didelot { 556d0004a02SVladimir Oltean struct dsa_port *dp; 557c38c5a66SVivien Didelot u32 mask = 0; 55802bc6e54SVivien Didelot 559d0004a02SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) 560d0004a02SVladimir Oltean mask |= BIT(dp->index); 561c38c5a66SVivien Didelot 562c38c5a66SVivien Didelot return mask; 563c8652c83SVivien Didelot } 564c8652c83SVivien Didelot 565c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 566c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 567c5f51765SVivien Didelot { 568c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 569c5f51765SVivien Didelot struct dsa_link *dl; 570c5f51765SVivien Didelot 571c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 572c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 573c5f51765SVivien Didelot return dl->dp->index; 574c5f51765SVivien Didelot 575c5f51765SVivien Didelot return ds->num_ports; 576c5f51765SVivien Didelot } 577c5f51765SVivien Didelot 5783b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 5793b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 5803b8fac5dSVivien Didelot int port) 5813b8fac5dSVivien Didelot { 5823b8fac5dSVivien Didelot if (device == ds->index) 5833b8fac5dSVivien Didelot return port; 5843b8fac5dSVivien Didelot else 585c5f51765SVivien Didelot return dsa_routing_port(ds, device); 5863b8fac5dSVivien Didelot } 5873b8fac5dSVivien Didelot 5883b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 58907073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 590c8f0b869SBen Hutchings { 59107073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 59207073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 59307073c79SVivien Didelot 59407073c79SVivien Didelot if (!cpu_dp) 59507073c79SVivien Didelot return port; 596c8f0b869SBen Hutchings 5973b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 598c8f0b869SBen Hutchings } 599c8f0b869SBen Hutchings 60063609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */ 60163609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port) 60263609c8fSVladimir Oltean { 60363609c8fSVladimir Oltean if (dsa_is_unused_port(ds, port)) 60463609c8fSVladimir Oltean return false; 60563609c8fSVladimir Oltean 60663609c8fSVladimir Oltean return port == dsa_upstream_port(ds, port); 60763609c8fSVladimir Oltean } 60863609c8fSVladimir Oltean 609d352b20fSTobias Waldekranz /* Return true if this is a DSA port leading away from the CPU */ 610d352b20fSTobias Waldekranz static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port) 611d352b20fSTobias Waldekranz { 612d352b20fSTobias Waldekranz return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port); 613d352b20fSTobias Waldekranz } 614d352b20fSTobias Waldekranz 6157af4a361STobias Waldekranz /* Return the local port used to reach the CPU port */ 6167af4a361STobias Waldekranz static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds) 6177af4a361STobias Waldekranz { 6187af4a361STobias Waldekranz struct dsa_port *dp; 6197af4a361STobias Waldekranz 6207af4a361STobias Waldekranz dsa_switch_for_each_available_port(dp, ds) { 6217af4a361STobias Waldekranz return dsa_upstream_port(ds, dp->index); 6227af4a361STobias Waldekranz } 6237af4a361STobias Waldekranz 6247af4a361STobias Waldekranz return ds->num_ports; 6257af4a361STobias Waldekranz } 6267af4a361STobias Waldekranz 62763609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning 62863609c8fSVladimir Oltean * that the routing port from @downstream_ds to @upstream_ds is also the port 62963609c8fSVladimir Oltean * which @downstream_ds uses to reach its dedicated CPU. 63063609c8fSVladimir Oltean */ 63163609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds, 63263609c8fSVladimir Oltean struct dsa_switch *downstream_ds) 63363609c8fSVladimir Oltean { 63463609c8fSVladimir Oltean int routing_port; 63563609c8fSVladimir Oltean 63663609c8fSVladimir Oltean if (upstream_ds == downstream_ds) 63763609c8fSVladimir Oltean return true; 63863609c8fSVladimir Oltean 63963609c8fSVladimir Oltean routing_port = dsa_routing_port(downstream_ds, upstream_ds->index); 64063609c8fSVladimir Oltean 64163609c8fSVladimir Oltean return dsa_is_upstream_port(downstream_ds, routing_port); 64263609c8fSVladimir Oltean } 64363609c8fSVladimir Oltean 644cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 645cf2d45f5SVladimir Oltean { 646cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 647cf2d45f5SVladimir Oltean 648cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 649cf2d45f5SVladimir Oltean return ds->vlan_filtering; 650cf2d45f5SVladimir Oltean else 651cf2d45f5SVladimir Oltean return dp->vlan_filtering; 652cf2d45f5SVladimir Oltean } 653cf2d45f5SVladimir Oltean 654*dedd6a00SVladimir Oltean static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp) 655*dedd6a00SVladimir Oltean { 656*dedd6a00SVladimir Oltean return dp->lag ? dp->lag->id : 0; 657*dedd6a00SVladimir Oltean } 658*dedd6a00SVladimir Oltean 659*dedd6a00SVladimir Oltean static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp) 660*dedd6a00SVladimir Oltean { 661*dedd6a00SVladimir Oltean return dp->lag ? dp->lag->dev : NULL; 662*dedd6a00SVladimir Oltean } 663*dedd6a00SVladimir Oltean 664*dedd6a00SVladimir Oltean static inline bool dsa_port_offloads_lag(struct dsa_port *dp, 665*dedd6a00SVladimir Oltean const struct dsa_lag *lag) 666*dedd6a00SVladimir Oltean { 667*dedd6a00SVladimir Oltean return dsa_port_lag_dev_get(dp) == lag->dev; 668*dedd6a00SVladimir Oltean } 669*dedd6a00SVladimir Oltean 670cc76ce9eSTobias Waldekranz static inline 671cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) 672cc76ce9eSTobias Waldekranz { 673d3eed0e5SVladimir Oltean if (!dp->bridge) 674cc76ce9eSTobias Waldekranz return NULL; 675cc76ce9eSTobias Waldekranz 676*dedd6a00SVladimir Oltean if (dp->lag) 677*dedd6a00SVladimir Oltean return dp->lag->dev; 678cc76ce9eSTobias Waldekranz else if (dp->hsr_dev) 679cc76ce9eSTobias Waldekranz return dp->hsr_dev; 680cc76ce9eSTobias Waldekranz 681cc76ce9eSTobias Waldekranz return dp->slave; 682cc76ce9eSTobias Waldekranz } 683cc76ce9eSTobias Waldekranz 68436cbf39bSVladimir Oltean static inline struct net_device * 68536cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp) 68636cbf39bSVladimir Oltean { 687d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->dev : NULL; 68836cbf39bSVladimir Oltean } 68936cbf39bSVladimir Oltean 69036cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp) 69136cbf39bSVladimir Oltean { 692d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->num : 0; 69336cbf39bSVladimir Oltean } 69436cbf39bSVladimir Oltean 69536cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a, 69636cbf39bSVladimir Oltean const struct dsa_port *b) 69736cbf39bSVladimir Oltean { 69836cbf39bSVladimir Oltean struct net_device *br_a = dsa_port_bridge_dev_get(a); 69936cbf39bSVladimir Oltean struct net_device *br_b = dsa_port_bridge_dev_get(b); 70036cbf39bSVladimir Oltean 70136cbf39bSVladimir Oltean /* Standalone ports are not in the same bridge with one another */ 70236cbf39bSVladimir Oltean return (!br_a || !br_b) ? false : (br_a == br_b); 70336cbf39bSVladimir Oltean } 70436cbf39bSVladimir Oltean 7056a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp, 7066a43cba3SVladimir Oltean const struct net_device *dev) 7076a43cba3SVladimir Oltean { 7086a43cba3SVladimir Oltean return dsa_port_to_bridge_port(dp) == dev; 7096a43cba3SVladimir Oltean } 7106a43cba3SVladimir Oltean 7116a43cba3SVladimir Oltean static inline bool 7126a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp, 7136a43cba3SVladimir Oltean const struct net_device *bridge_dev) 7146a43cba3SVladimir Oltean { 7156a43cba3SVladimir Oltean /* DSA ports connected to a bridge, and event was emitted 7166a43cba3SVladimir Oltean * for the bridge. 7176a43cba3SVladimir Oltean */ 7186a43cba3SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge_dev; 7196a43cba3SVladimir Oltean } 7206a43cba3SVladimir Oltean 721d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp, 722d3eed0e5SVladimir Oltean const struct dsa_bridge *bridge) 723d3eed0e5SVladimir Oltean { 724d3eed0e5SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge->dev; 725d3eed0e5SVladimir Oltean } 726d3eed0e5SVladimir Oltean 7276a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */ 7286a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst, 7296a43cba3SVladimir Oltean const struct net_device *dev) 7306a43cba3SVladimir Oltean { 7316a43cba3SVladimir Oltean struct dsa_port *dp; 7326a43cba3SVladimir Oltean 7336a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7346a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_port(dp, dev)) 7356a43cba3SVladimir Oltean return true; 7366a43cba3SVladimir Oltean 7376a43cba3SVladimir Oltean return false; 7386a43cba3SVladimir Oltean } 7396a43cba3SVladimir Oltean 7406a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */ 7416a43cba3SVladimir Oltean static inline bool 7426a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst, 7436a43cba3SVladimir Oltean const struct net_device *bridge_dev) 7446a43cba3SVladimir Oltean { 7456a43cba3SVladimir Oltean struct dsa_port *dp; 7466a43cba3SVladimir Oltean 7476a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7486a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_dev(dp, bridge_dev)) 7496a43cba3SVladimir Oltean return true; 7506a43cba3SVladimir Oltean 7516a43cba3SVladimir Oltean return false; 7526a43cba3SVladimir Oltean } 7536a43cba3SVladimir Oltean 7542bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 7552bedde1aSArkadi Sharshevsky bool is_static, void *data); 7569d490b4eSVivien Didelot struct dsa_switch_ops { 75753da0ebaSVladimir Oltean /* 75853da0ebaSVladimir Oltean * Tagging protocol helpers called for the CPU ports and DSA links. 75953da0ebaSVladimir Oltean * @get_tag_protocol retrieves the initial tagging protocol and is 76053da0ebaSVladimir Oltean * mandatory. Switches which can operate using multiple tagging 76153da0ebaSVladimir Oltean * protocols should implement @change_tag_protocol and report in 76253da0ebaSVladimir Oltean * @get_tag_protocol the tagger in current use. 76353da0ebaSVladimir Oltean */ 7645ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 7654d776482SFlorian Fainelli int port, 7664d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 76753da0ebaSVladimir Oltean int (*change_tag_protocol)(struct dsa_switch *ds, int port, 76853da0ebaSVladimir Oltean enum dsa_tag_protocol proto); 769dc452a47SVladimir Oltean /* 770dc452a47SVladimir Oltean * Method for switch drivers to connect to the tagging protocol driver 771dc452a47SVladimir Oltean * in current use. The switch driver can provide handlers for certain 772dc452a47SVladimir Oltean * types of packets for switch management. 773dc452a47SVladimir Oltean */ 774dc452a47SVladimir Oltean int (*connect_tag_protocol)(struct dsa_switch *ds, 775dc452a47SVladimir Oltean enum dsa_tag_protocol proto); 7767b314362SAndrew Lunn 777fd292c18SVladimir Oltean /* Optional switch-wide initialization and destruction methods */ 778c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 7795e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 780fd292c18SVladimir Oltean 781fd292c18SVladimir Oltean /* Per-port initialization and destruction methods. Mandatory if the 782fd292c18SVladimir Oltean * driver registers devlink port regions, optional otherwise. 783fd292c18SVladimir Oltean */ 784fd292c18SVladimir Oltean int (*port_setup)(struct dsa_switch *ds, int port); 785fd292c18SVladimir Oltean void (*port_teardown)(struct dsa_switch *ds, int port); 786fd292c18SVladimir Oltean 7876819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 788c8f0b869SBen Hutchings 789c8f0b869SBen Hutchings /* 790c8f0b869SBen Hutchings * Access to the switch's PHY registers. 791c8f0b869SBen Hutchings */ 792c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 793c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 794c8f0b869SBen Hutchings int regnum, u16 val); 795c8f0b869SBen Hutchings 796c8f0b869SBen Hutchings /* 797ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 798ec9436baSFlorian Fainelli */ 799ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 800ec9436baSFlorian Fainelli struct phy_device *phydev); 801ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 802ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 803ec9436baSFlorian Fainelli 804ec9436baSFlorian Fainelli /* 80511d8f3ddSFlorian Fainelli * PHYLINK integration 80611d8f3ddSFlorian Fainelli */ 807072eea6cSRussell King (Oracle) void (*phylink_get_caps)(struct dsa_switch *ds, int port, 808072eea6cSRussell King (Oracle) struct phylink_config *config); 80911d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 81011d8f3ddSFlorian Fainelli unsigned long *supported, 81111d8f3ddSFlorian Fainelli struct phylink_link_state *state); 812bde01822SRussell King (Oracle) struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds, 813bde01822SRussell King (Oracle) int port, 814bde01822SRussell King (Oracle) phy_interface_t iface); 81511d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 81611d8f3ddSFlorian Fainelli struct phylink_link_state *state); 81711d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 81811d8f3ddSFlorian Fainelli unsigned int mode, 81911d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 82011d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 82111d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 82211d8f3ddSFlorian Fainelli unsigned int mode, 82311d8f3ddSFlorian Fainelli phy_interface_t interface); 82411d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 82511d8f3ddSFlorian Fainelli unsigned int mode, 82611d8f3ddSFlorian Fainelli phy_interface_t interface, 8275b502a7bSRussell King struct phy_device *phydev, 8285b502a7bSRussell King int speed, int duplex, 8295b502a7bSRussell King bool tx_pause, bool rx_pause); 83011d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 83111d8f3ddSFlorian Fainelli struct phylink_link_state *state); 83211d8f3ddSFlorian Fainelli /* 833c2ec5f2eSOleksij Rempel * Port statistics counters. 834c8f0b869SBen Hutchings */ 83589f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 83689f09048SFlorian Fainelli u32 stringset, uint8_t *data); 837c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 838c8f0b869SBen Hutchings int port, uint64_t *data); 83989f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 840cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 841cf963573SFlorian Fainelli int port, uint64_t *data); 842487d3855SAlvin Šipraga void (*get_eth_phy_stats)(struct dsa_switch *ds, int port, 843487d3855SAlvin Šipraga struct ethtool_eth_phy_stats *phy_stats); 844487d3855SAlvin Šipraga void (*get_eth_mac_stats)(struct dsa_switch *ds, int port, 845487d3855SAlvin Šipraga struct ethtool_eth_mac_stats *mac_stats); 846487d3855SAlvin Šipraga void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port, 847487d3855SAlvin Šipraga struct ethtool_eth_ctrl_stats *ctrl_stats); 848c2ec5f2eSOleksij Rempel void (*get_stats64)(struct dsa_switch *ds, int port, 849c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s); 850a71acad9SOleksij Rempel void (*self_test)(struct dsa_switch *ds, int port, 851a71acad9SOleksij Rempel struct ethtool_test *etest, u64 *data); 85224462549SFlorian Fainelli 85324462549SFlorian Fainelli /* 85419e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 85519e57c4eSFlorian Fainelli */ 85619e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 85719e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 85819e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 85919e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 86019e57c4eSFlorian Fainelli 86119e57c4eSFlorian Fainelli /* 8620336369dSBrandon Streiff * ethtool timestamp info 8630336369dSBrandon Streiff */ 8640336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 8650336369dSBrandon Streiff struct ethtool_ts_info *ts); 8660336369dSBrandon Streiff 8670336369dSBrandon Streiff /* 86824462549SFlorian Fainelli * Suspend and resume 86924462549SFlorian Fainelli */ 87024462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 87124462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 872b2f2af21SFlorian Fainelli 873b2f2af21SFlorian Fainelli /* 874b2f2af21SFlorian Fainelli * Port enable/disable 875b2f2af21SFlorian Fainelli */ 876b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 877b2f2af21SFlorian Fainelli struct phy_device *phy); 87875104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 8797905288fSFlorian Fainelli 8807905288fSFlorian Fainelli /* 88108f50061SVivien Didelot * Port's MAC EEE settings 8827905288fSFlorian Fainelli */ 88308f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 8847905288fSFlorian Fainelli struct ethtool_eee *e); 88508f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 8867905288fSFlorian Fainelli struct ethtool_eee *e); 88751579c3fSGuenter Roeck 8886793abb4SGuenter Roeck /* EEPROM access */ 8896793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 8906793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 8916793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 8926793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 8936793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 8943d762a0fSGuenter Roeck 8953d762a0fSGuenter Roeck /* 8963d762a0fSGuenter Roeck * Register access. 8973d762a0fSGuenter Roeck */ 8983d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 8993d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 9003d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 901b73adef6SFlorian Fainelli 902b73adef6SFlorian Fainelli /* 903e358bef7SVladimir Oltean * Upper device tracking. 904e358bef7SVladimir Oltean */ 905e358bef7SVladimir Oltean int (*port_prechangeupper)(struct dsa_switch *ds, int port, 906e358bef7SVladimir Oltean struct netdev_notifier_changeupper_info *info); 907e358bef7SVladimir Oltean 908e358bef7SVladimir Oltean /* 909b73adef6SFlorian Fainelli * Bridge integration 910b73adef6SFlorian Fainelli */ 91134a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 91271327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 913b079922bSVladimir Oltean struct dsa_bridge bridge, 914b079922bSVladimir Oltean bool *tx_fwd_offload); 915f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 916d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 91743c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 918b73adef6SFlorian Fainelli u8 state); 919732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 920a8b659e7SVladimir Oltean int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 921a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 922a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 923a8b659e7SVladimir Oltean int (*port_bridge_flags)(struct dsa_switch *ds, int port, 924a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 925a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 9262a778e1bSVivien Didelot 9272a778e1bSVivien Didelot /* 92811149536SVivien Didelot * VLAN support 92911149536SVivien Didelot */ 930fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 93189153ed6SVladimir Oltean bool vlan_filtering, 93289153ed6SVladimir Oltean struct netlink_ext_ack *extack); 9331958d581SVladimir Oltean int (*port_vlan_add)(struct dsa_switch *ds, int port, 93431046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 93531046a5fSVladimir Oltean struct netlink_ext_ack *extack); 93676e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 93776e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 93811149536SVivien Didelot /* 9392a778e1bSVivien Didelot * Forwarding database 9402a778e1bSVivien Didelot */ 9411b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 9426c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 9432a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 9446c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 945ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 9462bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 9478df30255SVivien Didelot 9488df30255SVivien Didelot /* 9498df30255SVivien Didelot * Multicast database 9508df30255SVivien Didelot */ 951a52b2da7SVladimir Oltean int (*port_mdb_add)(struct dsa_switch *ds, int port, 9523709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 9538df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 9548df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb); 955bf9f2648SFlorian Fainelli /* 956bf9f2648SFlorian Fainelli * RXNFC 957bf9f2648SFlorian Fainelli */ 958bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 959bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 960bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 961bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 962f50f2127SFlorian Fainelli 963f50f2127SFlorian Fainelli /* 964f50f2127SFlorian Fainelli * TC integration 965f50f2127SFlorian Fainelli */ 966ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 967ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 968ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 969ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 970ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 971ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 972f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 973f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 974f50f2127SFlorian Fainelli bool ingress); 975f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 976f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 97734297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 97834297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 97934297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 98047d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 98147d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 98240ef2c93SVivien Didelot 98340ef2c93SVivien Didelot /* 98440ef2c93SVivien Didelot * Cross-chip operations 98540ef2c93SVivien Didelot */ 986f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 987f66a6a69SVladimir Oltean int sw_index, int port, 988d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 989f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 990f66a6a69SVladimir Oltean int sw_index, int port, 991d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 992058102a6STobias Waldekranz int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 993058102a6STobias Waldekranz int port); 994058102a6STobias Waldekranz int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 995*dedd6a00SVladimir Oltean int port, struct dsa_lag lag, 996058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 997058102a6STobias Waldekranz int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 998*dedd6a00SVladimir Oltean int port, struct dsa_lag lag); 9990336369dSBrandon Streiff 10000336369dSBrandon Streiff /* 10010336369dSBrandon Streiff * PTP functionality 10020336369dSBrandon Streiff */ 10030336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 10040336369dSBrandon Streiff struct ifreq *ifr); 10050336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 10060336369dSBrandon Streiff struct ifreq *ifr); 10075c5416f5SYangbo Lu void (*port_txtstamp)(struct dsa_switch *ds, int port, 10085c5416f5SYangbo Lu struct sk_buff *skb); 100990af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 101090af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 101197a69a0dSVladimir Oltean 10120f06b855SAndrew Lunn /* Devlink parameters, etc */ 10136b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 10146b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10156b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 10166b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10170f06b855SAndrew Lunn int (*devlink_info_get)(struct dsa_switch *ds, 10180f06b855SAndrew Lunn struct devlink_info_req *req, 10190f06b855SAndrew Lunn struct netlink_ext_ack *extack); 10202a6ef763SVladimir Oltean int (*devlink_sb_pool_get)(struct dsa_switch *ds, 10212a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10222a6ef763SVladimir Oltean struct devlink_sb_pool_info *pool_info); 10232a6ef763SVladimir Oltean int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 10242a6ef763SVladimir Oltean u16 pool_index, u32 size, 10252a6ef763SVladimir Oltean enum devlink_sb_threshold_type threshold_type, 10262a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10272a6ef763SVladimir Oltean int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 10282a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10292a6ef763SVladimir Oltean u32 *p_threshold); 10302a6ef763SVladimir Oltean int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 10312a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10322a6ef763SVladimir Oltean u32 threshold, 10332a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10342a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 10352a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10362a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10372a6ef763SVladimir Oltean u16 *p_pool_index, u32 *p_threshold); 10382a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 10392a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10402a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10412a6ef763SVladimir Oltean u16 pool_index, u32 threshold, 10422a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10432a6ef763SVladimir Oltean int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 10442a6ef763SVladimir Oltean unsigned int sb_index); 10452a6ef763SVladimir Oltean int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 10462a6ef763SVladimir Oltean unsigned int sb_index); 10472a6ef763SVladimir Oltean int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 10482a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10492a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 10502a6ef763SVladimir Oltean int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 10512a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10522a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10532a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 1054bfcb8132SVladimir Oltean 1055bfcb8132SVladimir Oltean /* 1056bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 1057bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 1058bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 1059ab88d64aSRandy Dunlap * method needs to do so privately. 1060bfcb8132SVladimir Oltean */ 1061bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 1062bfcb8132SVladimir Oltean int new_mtu); 1063bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 1064058102a6STobias Waldekranz 1065058102a6STobias Waldekranz /* 1066058102a6STobias Waldekranz * LAG integration 1067058102a6STobias Waldekranz */ 1068058102a6STobias Waldekranz int (*port_lag_change)(struct dsa_switch *ds, int port); 1069058102a6STobias Waldekranz int (*port_lag_join)(struct dsa_switch *ds, int port, 1070*dedd6a00SVladimir Oltean struct dsa_lag lag, 1071058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 1072058102a6STobias Waldekranz int (*port_lag_leave)(struct dsa_switch *ds, int port, 1073*dedd6a00SVladimir Oltean struct dsa_lag lag); 107418596f50SGeorge McCollister 107518596f50SGeorge McCollister /* 107618596f50SGeorge McCollister * HSR integration 107718596f50SGeorge McCollister */ 107818596f50SGeorge McCollister int (*port_hsr_join)(struct dsa_switch *ds, int port, 107918596f50SGeorge McCollister struct net_device *hsr); 108018596f50SGeorge McCollister int (*port_hsr_leave)(struct dsa_switch *ds, int port, 108118596f50SGeorge McCollister struct net_device *hsr); 1082c595c433SHoratiu Vultur 1083c595c433SHoratiu Vultur /* 1084c595c433SHoratiu Vultur * MRP integration 1085c595c433SHoratiu Vultur */ 1086c595c433SHoratiu Vultur int (*port_mrp_add)(struct dsa_switch *ds, int port, 1087c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1088c595c433SHoratiu Vultur int (*port_mrp_del)(struct dsa_switch *ds, int port, 1089c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1090c595c433SHoratiu Vultur int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 1091c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 1092c595c433SHoratiu Vultur int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 1093c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 10945da11eb4SVladimir Oltean 10955da11eb4SVladimir Oltean /* 10965da11eb4SVladimir Oltean * tag_8021q operations 10975da11eb4SVladimir Oltean */ 10985da11eb4SVladimir Oltean int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 10995da11eb4SVladimir Oltean u16 flags); 11005da11eb4SVladimir Oltean int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 1101295ab96fSVladimir Oltean 1102295ab96fSVladimir Oltean /* 1103295ab96fSVladimir Oltean * DSA master tracking operations 1104295ab96fSVladimir Oltean */ 1105295ab96fSVladimir Oltean void (*master_state_change)(struct dsa_switch *ds, 1106295ab96fSVladimir Oltean const struct net_device *master, 1107295ab96fSVladimir Oltean bool operational); 11086b297524SAndrew Lunn }; 11096b297524SAndrew Lunn 11106b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 11116b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 11126b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 11136b297524SAndrew Lunn 11146b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 11156b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 11166b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 11176b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 11186b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 11196b297524SAndrew Lunn const struct devlink_param *params, 11206b297524SAndrew Lunn size_t params_count); 11216b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 11226b297524SAndrew Lunn const struct devlink_param *params, 11236b297524SAndrew Lunn size_t params_count); 11245cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 11255cd73fbdSAndrew Lunn const char *resource_name, 11265cd73fbdSAndrew Lunn u64 resource_size, 11275cd73fbdSAndrew Lunn u64 resource_id, 11285cd73fbdSAndrew Lunn u64 parent_resource_id, 11295cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 11305cd73fbdSAndrew Lunn 11315cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 11325cd73fbdSAndrew Lunn 11335cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 11345cd73fbdSAndrew Lunn u64 resource_id, 11355cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 11365cd73fbdSAndrew Lunn void *occ_get_priv); 11375cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 11385cd73fbdSAndrew Lunn u64 resource_id); 113997c82c23SAndrew Lunn struct devlink_region * 114097c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds, 114197c82c23SAndrew Lunn const struct devlink_region_ops *ops, 114297c82c23SAndrew Lunn u32 region_max_snapshots, u64 region_size); 114308156ba4SAndrew Lunn struct devlink_region * 114408156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds, 114508156ba4SAndrew Lunn int port, 114608156ba4SAndrew Lunn const struct devlink_port_region_ops *ops, 114708156ba4SAndrew Lunn u32 region_max_snapshots, u64 region_size); 114897c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region); 114997c82c23SAndrew Lunn 1150e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 11515cd73fbdSAndrew Lunn 11526b297524SAndrew Lunn struct dsa_devlink_priv { 11536b297524SAndrew Lunn struct dsa_switch *ds; 1154c8f0b869SBen Hutchings }; 1155c8f0b869SBen Hutchings 1156ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 1157ccc3e6b0SAndrew Lunn { 1158ccc3e6b0SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1159ccc3e6b0SAndrew Lunn 1160ccc3e6b0SAndrew Lunn return dl_priv->ds; 1161ccc3e6b0SAndrew Lunn } 1162ccc3e6b0SAndrew Lunn 11637d1e2a10SAndrew Lunn static inline 11647d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 11657d1e2a10SAndrew Lunn { 11667d1e2a10SAndrew Lunn struct devlink *dl = port->devlink; 11677d1e2a10SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 11687d1e2a10SAndrew Lunn 11697d1e2a10SAndrew Lunn return dl_priv->ds; 11707d1e2a10SAndrew Lunn } 11717d1e2a10SAndrew Lunn 11727d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port) 11737d1e2a10SAndrew Lunn { 11747d1e2a10SAndrew Lunn return port->index; 11757d1e2a10SAndrew Lunn } 11767d1e2a10SAndrew Lunn 1177ab3d408dSFlorian Fainelli struct dsa_switch_driver { 1178ab3d408dSFlorian Fainelli struct list_head list; 1179a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 1180ab3d408dSFlorian Fainelli }; 1181ab3d408dSFlorian Fainelli 118214b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 1183c8f0b869SBen Hutchings 118473a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 11859eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 1186c6e970a0SAndrew Lunn { 1187c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 1188717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 1189c6e970a0SAndrew Lunn #endif 1190c6e970a0SAndrew Lunn return false; 1191c6e970a0SAndrew Lunn } 1192c6e970a0SAndrew Lunn 11939790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail 11949790cf20SVladimir Oltean * tags, which don't break dissection) can be treated the same from the 11959790cf20SVladimir Oltean * perspective of the flow dissector. 11969790cf20SVladimir Oltean * 11979790cf20SVladimir Oltean * We need to return: 11989790cf20SVladimir Oltean * - offset: the (B - A) difference between: 11999790cf20SVladimir Oltean * A. the position of the real EtherType and 12009790cf20SVladimir Oltean * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 12019790cf20SVladimir Oltean * after the normal EtherType was supposed to be) 12029790cf20SVladimir Oltean * The offset in bytes is exactly equal to the tagger overhead (and half of 12039790cf20SVladimir Oltean * that, in __be16 shorts). 12049790cf20SVladimir Oltean * 12059790cf20SVladimir Oltean * - proto: the value of the real EtherType. 12069790cf20SVladimir Oltean */ 12079790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 12089790cf20SVladimir Oltean __be16 *proto, int *offset) 12099790cf20SVladimir Oltean { 12109790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 12119790cf20SVladimir Oltean const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 12124e500251SVladimir Oltean int tag_len = ops->needed_headroom; 12139790cf20SVladimir Oltean 12149790cf20SVladimir Oltean *offset = tag_len; 12159790cf20SVladimir Oltean *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 12169790cf20SVladimir Oltean #endif 12179790cf20SVladimir Oltean } 12189790cf20SVladimir Oltean 12194cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 12204cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev) 12214cfab356SFlorian Fainelli { 12224cfab356SFlorian Fainelli int err = -EOPNOTSUPP; 12234cfab356SFlorian Fainelli 12244cfab356SFlorian Fainelli if (!dev->dsa_ptr) 12254cfab356SFlorian Fainelli return err; 12264cfab356SFlorian Fainelli 12274cfab356SFlorian Fainelli if (!dev->dsa_ptr->netdev_ops) 12284cfab356SFlorian Fainelli return err; 12294cfab356SFlorian Fainelli 12304cfab356SFlorian Fainelli return 0; 12314cfab356SFlorian Fainelli } 12324cfab356SFlorian Fainelli 1233a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 12344cfab356SFlorian Fainelli int cmd) 12354cfab356SFlorian Fainelli { 12364cfab356SFlorian Fainelli const struct dsa_netdevice_ops *ops; 12374cfab356SFlorian Fainelli int err; 12384cfab356SFlorian Fainelli 12394cfab356SFlorian Fainelli err = __dsa_netdevice_ops_check(dev); 12404cfab356SFlorian Fainelli if (err) 12414cfab356SFlorian Fainelli return err; 12424cfab356SFlorian Fainelli 12434cfab356SFlorian Fainelli ops = dev->dsa_ptr->netdev_ops; 12444cfab356SFlorian Fainelli 1245a7605370SArnd Bergmann return ops->ndo_eth_ioctl(dev, ifr, cmd); 12464cfab356SFlorian Fainelli } 12474cfab356SFlorian Fainelli #else 1248a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 12494cfab356SFlorian Fainelli int cmd) 12504cfab356SFlorian Fainelli { 12514cfab356SFlorian Fainelli return -EOPNOTSUPP; 12524cfab356SFlorian Fainelli } 12534cfab356SFlorian Fainelli #endif 12544cfab356SFlorian Fainelli 125583c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 125623c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 12570650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds); 12583b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1259a2614140SVladimir Oltean void dsa_flush_workqueue(void); 1260ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 1261ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 1262ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 1263ea825e70SFlorian Fainelli #else 1264ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 1265ea825e70SFlorian Fainelli { 1266ea825e70SFlorian Fainelli return 0; 1267ea825e70SFlorian Fainelli } 1268ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 1269ea825e70SFlorian Fainelli { 1270ea825e70SFlorian Fainelli return 0; 1271ea825e70SFlorian Fainelli } 1272ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 1273ea825e70SFlorian Fainelli 127460724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 1275a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev); 127660724d4bSFlorian Fainelli #else 1277a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev) 1278a5e3c9baSVladimir Oltean { 1279a5e3c9baSVladimir Oltean return false; 1280a5e3c9baSVladimir Oltean } 128160724d4bSFlorian Fainelli #endif 128260724d4bSFlorian Fainelli 128397a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 128411d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1285cf963573SFlorian Fainelli 1286d3b8c049SAndrew Lunn struct dsa_tag_driver { 1287d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 1288d3b8c049SAndrew Lunn struct list_head list; 1289d3b8c049SAndrew Lunn struct module *owner; 1290d3b8c049SAndrew Lunn }; 1291d3b8c049SAndrew Lunn 1292d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1293d3b8c049SAndrew Lunn unsigned int count, 1294d3b8c049SAndrew Lunn struct module *owner); 1295d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1296d3b8c049SAndrew Lunn unsigned int count); 1297d3b8c049SAndrew Lunn 1298d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1299d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 1300d3b8c049SAndrew Lunn { \ 1301d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1302d3b8c049SAndrew Lunn THIS_MODULE); \ 1303d3b8c049SAndrew Lunn return 0; \ 1304d3b8c049SAndrew Lunn } \ 1305d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 1306d3b8c049SAndrew Lunn \ 1307d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 1308d3b8c049SAndrew Lunn { \ 1309d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1310d3b8c049SAndrew Lunn } \ 1311d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 1312d3b8c049SAndrew Lunn 1313d3b8c049SAndrew Lunn /** 1314d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1315d3b8c049SAndrew Lunn * drivers 1316c7d9a675SLuiz Angelo Daros de Luca * @__ops_array: Array of tag driver structures 1317d3b8c049SAndrew Lunn * 1318d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1319d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1320d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1321d3b8c049SAndrew Lunn */ 1322d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 1323d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1324d3b8c049SAndrew Lunn 1325d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1326d3b8c049SAndrew Lunn 1327d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 1328d3b8c049SAndrew Lunn * drivers 1329d3b8c049SAndrew Lunn */ 1330d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 1331d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1332d3b8c049SAndrew Lunn .ops = &__ops, \ 1333d3b8c049SAndrew Lunn } 1334d3b8c049SAndrew Lunn 1335d3b8c049SAndrew Lunn /** 1336d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1337d3b8c049SAndrew Lunn * driver 1338d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 1339d3b8c049SAndrew Lunn * 1340d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1341d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1342d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1343d3b8c049SAndrew Lunn */ 1344d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 1345d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 1346d3b8c049SAndrew Lunn \ 1347d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1348d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 1349d3b8c049SAndrew Lunn }; \ 1350d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 135191da11f8SLennert Buytenhek #endif 1352d3b8c049SAndrew Lunn 1353