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 119cf50dcc2SBen Hutchings struct dsa_switch_tree { 12083c0afaeSAndrew Lunn struct list_head list; 12183c0afaeSAndrew Lunn 122b035c88cSVladimir Oltean /* List of switch ports */ 123b035c88cSVladimir Oltean struct list_head ports; 124b035c88cSVladimir Oltean 125f515f192SVivien Didelot /* Notifier chain for switch-wide events */ 126f515f192SVivien Didelot struct raw_notifier_head nh; 127f515f192SVivien Didelot 12883c0afaeSAndrew Lunn /* Tree identifier */ 12949463b7fSVivien Didelot unsigned int index; 13083c0afaeSAndrew Lunn 13183c0afaeSAndrew Lunn /* Number of switches attached to this tree */ 13283c0afaeSAndrew Lunn struct kref refcount; 13383c0afaeSAndrew Lunn 134b035c88cSVladimir Oltean /* Maps offloaded LAG netdevs to a zero-based linear ID for 135b035c88cSVladimir Oltean * drivers that need it. 136b035c88cSVladimir Oltean */ 137b035c88cSVladimir Oltean struct net_device **lags; 138b035c88cSVladimir Oltean 139357f203bSVladimir Oltean /* Tagging protocol operations */ 140357f203bSVladimir Oltean const struct dsa_device_ops *tag_ops; 141357f203bSVladimir Oltean 142deff7107STobias Waldekranz /* Default tagging protocol preferred by the switches in this 143deff7107STobias Waldekranz * tree. 144deff7107STobias Waldekranz */ 145deff7107STobias Waldekranz enum dsa_tag_protocol default_proto; 146deff7107STobias Waldekranz 1474b026e82SVladimir Oltean /* Has this tree been applied to the hardware? */ 1484b026e82SVladimir Oltean bool setup; 1494b026e82SVladimir Oltean 150cf50dcc2SBen Hutchings /* 151cf50dcc2SBen Hutchings * Configuration data for the platform device that owns 152cf50dcc2SBen Hutchings * this dsa switch tree instance. 153cf50dcc2SBen Hutchings */ 154cf50dcc2SBen Hutchings struct dsa_platform_data *pd; 155cf85d08fSLennert Buytenhek 156c5f51765SVivien Didelot /* List of DSA links composing the routing table */ 157c5f51765SVivien Didelot struct list_head rtable; 158058102a6STobias Waldekranz 159b035c88cSVladimir Oltean /* Length of "lags" array */ 160058102a6STobias Waldekranz unsigned int lags_len; 1615b22d366SVladimir Oltean 1625b22d366SVladimir Oltean /* Track the largest switch index within a tree */ 1635b22d366SVladimir Oltean unsigned int last_switch; 164cf50dcc2SBen Hutchings }; 165cf50dcc2SBen Hutchings 166058102a6STobias Waldekranz #define dsa_lags_foreach_id(_id, _dst) \ 167058102a6STobias Waldekranz for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++) \ 168058102a6STobias Waldekranz if ((_dst)->lags[(_id)]) 169058102a6STobias Waldekranz 170058102a6STobias Waldekranz #define dsa_lag_foreach_port(_dp, _dst, _lag) \ 171058102a6STobias Waldekranz list_for_each_entry((_dp), &(_dst)->ports, list) \ 172058102a6STobias Waldekranz if ((_dp)->lag_dev == (_lag)) 173058102a6STobias Waldekranz 17418596f50SGeorge McCollister #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \ 17518596f50SGeorge McCollister list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 17618596f50SGeorge McCollister if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr)) 17718596f50SGeorge McCollister 178058102a6STobias Waldekranz static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst, 179058102a6STobias Waldekranz unsigned int id) 180058102a6STobias Waldekranz { 181058102a6STobias Waldekranz return dst->lags[id]; 182058102a6STobias Waldekranz } 183058102a6STobias Waldekranz 184058102a6STobias Waldekranz static inline int dsa_lag_id(struct dsa_switch_tree *dst, 185058102a6STobias Waldekranz struct net_device *lag) 186058102a6STobias Waldekranz { 187058102a6STobias Waldekranz unsigned int id; 188058102a6STobias Waldekranz 189058102a6STobias Waldekranz dsa_lags_foreach_id(id, dst) { 190058102a6STobias Waldekranz if (dsa_lag_dev(dst, id) == lag) 191058102a6STobias Waldekranz return id; 192058102a6STobias Waldekranz } 193058102a6STobias Waldekranz 194058102a6STobias Waldekranz return -ENODEV; 195058102a6STobias Waldekranz } 196058102a6STobias Waldekranz 19734297176SVladimir Oltean /* TC matchall action types */ 198f50f2127SFlorian Fainelli enum dsa_port_mall_action_type { 199f50f2127SFlorian Fainelli DSA_PORT_MALL_MIRROR, 20034297176SVladimir Oltean DSA_PORT_MALL_POLICER, 201f50f2127SFlorian Fainelli }; 202f50f2127SFlorian Fainelli 203f50f2127SFlorian Fainelli /* TC mirroring entry */ 204f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry { 205f50f2127SFlorian Fainelli u8 to_local_port; 206f50f2127SFlorian Fainelli bool ingress; 207f50f2127SFlorian Fainelli }; 208f50f2127SFlorian Fainelli 20934297176SVladimir Oltean /* TC port policer entry */ 21034297176SVladimir Oltean struct dsa_mall_policer_tc_entry { 2115f035af7SPo Liu u32 burst; 21234297176SVladimir Oltean u64 rate_bytes_per_sec; 21334297176SVladimir Oltean }; 21434297176SVladimir Oltean 215f50f2127SFlorian Fainelli /* TC matchall entry */ 216f50f2127SFlorian Fainelli struct dsa_mall_tc_entry { 217f50f2127SFlorian Fainelli struct list_head list; 218f50f2127SFlorian Fainelli unsigned long cookie; 219f50f2127SFlorian Fainelli enum dsa_port_mall_action_type type; 220f50f2127SFlorian Fainelli union { 221f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry mirror; 22234297176SVladimir Oltean struct dsa_mall_policer_tc_entry policer; 223f50f2127SFlorian Fainelli }; 224f50f2127SFlorian Fainelli }; 225f50f2127SFlorian Fainelli 226d3eed0e5SVladimir Oltean struct dsa_bridge { 227d3eed0e5SVladimir Oltean struct net_device *dev; 228d3eed0e5SVladimir Oltean unsigned int num; 229857fdd74SVladimir Oltean bool tx_fwd_offload; 230d3eed0e5SVladimir Oltean refcount_t refcount; 231d3eed0e5SVladimir Oltean }; 232f50f2127SFlorian Fainelli 233c8b09808SAndrew Lunn struct dsa_port { 234f8b8b1cdSVivien Didelot /* A CPU port is physically connected to a master device. 235f8b8b1cdSVivien Didelot * A user port exposed to userspace has a slave device. 236f8b8b1cdSVivien Didelot */ 237f8b8b1cdSVivien Didelot union { 238f8b8b1cdSVivien Didelot struct net_device *master; 239f8b8b1cdSVivien Didelot struct net_device *slave; 240f8b8b1cdSVivien Didelot }; 241f8b8b1cdSVivien Didelot 242357f203bSVladimir Oltean /* Copy of the tagging protocol operations, for quicker access 243357f203bSVladimir Oltean * in the data path. Valid only for the CPU ports. 244357f203bSVladimir Oltean */ 24515240248SVivien Didelot const struct dsa_device_ops *tag_ops; 24615240248SVivien Didelot 2473e41f93bSVivien Didelot /* Copies for faster access in master receive hot path */ 2483e41f93bSVivien Didelot struct dsa_switch_tree *dst; 24929a097b7SVladimir Oltean struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev); 2503e41f93bSVivien Didelot 25106251258SVladimir Oltean struct dsa_switch *ds; 25206251258SVladimir Oltean 25306251258SVladimir Oltean unsigned int index; 25406251258SVladimir Oltean 255057cad2cSVivien Didelot enum { 256057cad2cSVivien Didelot DSA_PORT_TYPE_UNUSED = 0, 257057cad2cSVivien Didelot DSA_PORT_TYPE_CPU, 258057cad2cSVivien Didelot DSA_PORT_TYPE_DSA, 259057cad2cSVivien Didelot DSA_PORT_TYPE_USER, 260057cad2cSVivien Didelot } type; 261057cad2cSVivien Didelot 26271e0bbdeSFlorian Fainelli const char *name; 26368b2d4a8SVivien Didelot struct dsa_port *cpu_dp; 26483216e39SMichael Walle u8 mac[ETH_ALEN]; 265b08db33dSVladimir Oltean 266b08db33dSVladimir Oltean u8 stp_state; 267b08db33dSVladimir Oltean 2681b26d364SVladimir Oltean /* Warning: the following bit fields are not atomic, and updating them 2691b26d364SVladimir Oltean * can only be done from code paths where concurrency is not possible 2701b26d364SVladimir Oltean * (probe time or under rtnl_lock). 2711b26d364SVladimir Oltean */ 27263cfc657SVladimir Oltean u8 vlan_filtering:1; 27363cfc657SVladimir Oltean 27463cfc657SVladimir Oltean /* Managed by DSA on user ports and by drivers on CPU and DSA ports */ 27563cfc657SVladimir Oltean u8 learning:1; 27663cfc657SVladimir Oltean 27763cfc657SVladimir Oltean u8 lag_tx_enabled:1; 27863cfc657SVladimir Oltean 27963cfc657SVladimir Oltean u8 devlink_port_setup:1; 28063cfc657SVladimir Oltean 281295ab96fSVladimir Oltean /* Master state bits, valid only on CPU ports */ 282295ab96fSVladimir Oltean u8 master_admin_up:1; 283295ab96fSVladimir Oltean u8 master_oper_up:1; 284295ab96fSVladimir Oltean 28563cfc657SVladimir Oltean u8 setup:1; 286bde82f38SVladimir Oltean 287189b0d93SAndrew Lunn struct device_node *dn; 28834a79f63SVivien Didelot unsigned int ageing_time; 289bde82f38SVladimir Oltean 290d3eed0e5SVladimir Oltean struct dsa_bridge *bridge; 29196567d5dSAndrew Lunn struct devlink_port devlink_port; 292aab9c406SFlorian Fainelli struct phylink *pl; 29344cc27e4SIoana Ciornei struct phylink_config pl_config; 294058102a6STobias Waldekranz struct net_device *lag_dev; 29518596f50SGeorge McCollister struct net_device *hsr_dev; 29697a69a0dSVladimir Oltean 297ab8ccae1SVivien Didelot struct list_head list; 298ab8ccae1SVivien Didelot 29967dbb9d4SFlorian Fainelli /* 30067dbb9d4SFlorian Fainelli * Original copy of the master netdev ethtool_ops 30167dbb9d4SFlorian Fainelli */ 30267dbb9d4SFlorian Fainelli const struct ethtool_ops *orig_ethtool_ops; 303da7b9e9bSFlorian Fainelli 304da7b9e9bSFlorian Fainelli /* 305da7b9e9bSFlorian Fainelli * Original copy of the master netdev net_device_ops 306da7b9e9bSFlorian Fainelli */ 3074cfab356SFlorian Fainelli const struct dsa_netdevice_ops *netdev_ops; 308fb35c60cSVivien Didelot 309161ca59dSVladimir Oltean /* List of MAC addresses that must be forwarded on this port. 310161ca59dSVladimir Oltean * These are only valid on CPU ports and DSA links. 311161ca59dSVladimir Oltean */ 312338a3a47SVladimir Oltean struct mutex addr_lists_lock; 3133f6e32f9SVladimir Oltean struct list_head fdbs; 314161ca59dSVladimir Oltean struct list_head mdbs; 315c8b09808SAndrew Lunn }; 316c8b09808SAndrew Lunn 317c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member, 318c5f51765SVivien Didelot * and no dst->rtable nor this struct dsa_link would be needed, 319c5f51765SVivien Didelot * but this would require some more complex tree walking, 320c5f51765SVivien Didelot * so keep it stupid at the moment and list them all. 321c5f51765SVivien Didelot */ 322c5f51765SVivien Didelot struct dsa_link { 323c5f51765SVivien Didelot struct dsa_port *dp; 324c5f51765SVivien Didelot struct dsa_port *link_dp; 325c5f51765SVivien Didelot struct list_head list; 326c5f51765SVivien Didelot }; 327c5f51765SVivien Didelot 328161ca59dSVladimir Oltean struct dsa_mac_addr { 329161ca59dSVladimir Oltean unsigned char addr[ETH_ALEN]; 330161ca59dSVladimir Oltean u16 vid; 331161ca59dSVladimir Oltean refcount_t refcount; 332161ca59dSVladimir Oltean struct list_head list; 333161ca59dSVladimir Oltean }; 334161ca59dSVladimir Oltean 335c8f0b869SBen Hutchings struct dsa_switch { 336c33063d6SAndrew Lunn struct device *dev; 337c33063d6SAndrew Lunn 338c8f0b869SBen Hutchings /* 339c8f0b869SBen Hutchings * Parent switch tree, and switch index. 340c8f0b869SBen Hutchings */ 341c8f0b869SBen Hutchings struct dsa_switch_tree *dst; 34299feaafcSVivien Didelot unsigned int index; 343c8f0b869SBen Hutchings 3441b26d364SVladimir Oltean /* Warning: the following bit fields are not atomic, and updating them 3451b26d364SVladimir Oltean * can only be done from code paths where concurrency is not possible 3461b26d364SVladimir Oltean * (probe time or under rtnl_lock). 3471b26d364SVladimir Oltean */ 34863cfc657SVladimir Oltean u32 setup:1; 34963cfc657SVladimir Oltean 35063cfc657SVladimir Oltean /* Disallow bridge core from requesting different VLAN awareness 35163cfc657SVladimir Oltean * settings on ports if not hardware-supported 3527787ff77SVladimir Oltean */ 35363cfc657SVladimir Oltean u32 vlan_filtering_is_global:1; 35463cfc657SVladimir Oltean 35563cfc657SVladimir Oltean /* Keep VLAN filtering enabled on ports not offloading any upper */ 35663cfc657SVladimir Oltean u32 needs_standalone_vlan_filtering:1; 35763cfc657SVladimir Oltean 35863cfc657SVladimir Oltean /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 35963cfc657SVladimir Oltean * that have vlan_filtering=0. All drivers should ideally set this (and 36063cfc657SVladimir Oltean * then the option would get removed), but it is unknown whether this 3617787ff77SVladimir Oltean * would break things or not. 3627787ff77SVladimir Oltean */ 36363cfc657SVladimir Oltean u32 configure_vlan_while_not_filtering:1; 36463cfc657SVladimir Oltean 36563cfc657SVladimir Oltean /* If the switch driver always programs the CPU port as egress tagged 36663cfc657SVladimir Oltean * despite the VLAN configuration indicating otherwise, then setting 36763cfc657SVladimir Oltean * @untag_bridge_pvid will force the DSA receive path to pop the 36863cfc657SVladimir Oltean * bridge's default_pvid VLAN tagged frames to offer a consistent 36963cfc657SVladimir Oltean * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge 3707787ff77SVladimir Oltean * device. 3717787ff77SVladimir Oltean */ 37263cfc657SVladimir Oltean u32 untag_bridge_pvid:1; 37363cfc657SVladimir Oltean 3747787ff77SVladimir Oltean /* Let DSA manage the FDB entries towards the 3757787ff77SVladimir Oltean * CPU, based on the software bridge database. 3767787ff77SVladimir Oltean */ 37763cfc657SVladimir Oltean u32 assisted_learning_on_cpu_port:1; 37863cfc657SVladimir Oltean 37963cfc657SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 38063cfc657SVladimir Oltean * should be retrieved from here and not from the per-port settings. 3817787ff77SVladimir Oltean */ 38263cfc657SVladimir Oltean u32 vlan_filtering:1; 38363cfc657SVladimir Oltean 38463cfc657SVladimir Oltean /* MAC PCS does not provide link state change interrupt, and requires 38563cfc657SVladimir Oltean * polling. Flag passed on to PHYLINK. 3867787ff77SVladimir Oltean */ 38763cfc657SVladimir Oltean u32 pcs_poll:1; 38863cfc657SVladimir Oltean 38963cfc657SVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 39063cfc657SVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 39163cfc657SVladimir Oltean * interfaces is needed. 3927787ff77SVladimir Oltean */ 39363cfc657SVladimir Oltean u32 mtu_enforcement_ingress:1; 3947787ff77SVladimir Oltean 395f515f192SVivien Didelot /* Listener for switch fabric events */ 396f515f192SVivien Didelot struct notifier_block nb; 397f515f192SVivien Didelot 398c8f0b869SBen Hutchings /* 3997543a6d5SAndrew Lunn * Give the switch driver somewhere to hang its private data 4007543a6d5SAndrew Lunn * structure. 4017543a6d5SAndrew Lunn */ 4027543a6d5SAndrew Lunn void *priv; 4037543a6d5SAndrew Lunn 404dc452a47SVladimir Oltean void *tagger_data; 405dc452a47SVladimir Oltean 4067543a6d5SAndrew Lunn /* 407c8f0b869SBen Hutchings * Configuration data for this switch. 408c8f0b869SBen Hutchings */ 409ff04955cSAndrew Lunn struct dsa_chip_data *cd; 410c8f0b869SBen Hutchings 411c8f0b869SBen Hutchings /* 4129d490b4eSVivien Didelot * The switch operations. 413c8f0b869SBen Hutchings */ 414a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 415c8f0b869SBen Hutchings 41666472fc0SAndrew Lunn /* 417c8f0b869SBen Hutchings * Slave mii_bus and devices for the individual ports. 418c8f0b869SBen Hutchings */ 4190d8bcdd3SFlorian Fainelli u32 phys_mii_mask; 420c8f0b869SBen Hutchings struct mii_bus *slave_mii_bus; 421a0c02161SVivien Didelot 4220f3da6afSVivien Didelot /* Ageing Time limits in msecs */ 4230f3da6afSVivien Didelot unsigned int ageing_time_min; 4240f3da6afSVivien Didelot unsigned int ageing_time_max; 4250f3da6afSVivien Didelot 426d7b1fd52SVladimir Oltean /* Storage for drivers using tag_8021q */ 427d7b1fd52SVladimir Oltean struct dsa_8021q_context *tag_8021q_ctx; 428d7b1fd52SVladimir Oltean 42996567d5dSAndrew Lunn /* devlink used to represent this switch device */ 43096567d5dSAndrew Lunn struct devlink *devlink; 43196567d5dSAndrew Lunn 43255199df6SFlorian Fainelli /* Number of switch port queues */ 43355199df6SFlorian Fainelli unsigned int num_tx_queues; 43455199df6SFlorian Fainelli 435058102a6STobias Waldekranz /* Drivers that benefit from having an ID associated with each 436058102a6STobias Waldekranz * offloaded LAG should set this to the maximum number of 437058102a6STobias Waldekranz * supported IDs. DSA will then maintain a mapping of _at 438058102a6STobias Waldekranz * least_ these many IDs, accessible to drivers via 439058102a6STobias Waldekranz * dsa_lag_id(). 440058102a6STobias Waldekranz */ 441058102a6STobias Waldekranz unsigned int num_lag_ids; 442058102a6STobias Waldekranz 443947c8746SVladimir Oltean /* Drivers that support bridge forwarding offload or FDB isolation 444947c8746SVladimir Oltean * should set this to the maximum number of bridges spanning the same 445947c8746SVladimir Oltean * switch tree (or all trees, in the case of cross-tree bridging 446947c8746SVladimir Oltean * support) that can be offloaded. 447123abc06SVladimir Oltean */ 448947c8746SVladimir Oltean unsigned int max_num_bridges; 449123abc06SVladimir Oltean 450258030acSVladimir Oltean unsigned int num_ports; 451c8f0b869SBen Hutchings }; 452c8f0b869SBen Hutchings 45368bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 454c38c5a66SVivien Didelot { 455b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 456d607525bSVivien Didelot struct dsa_port *dp; 457b96ddf25SVivien Didelot 458b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 459b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 460b96ddf25SVivien Didelot return dp; 461d607525bSVivien Didelot 462d607525bSVivien Didelot return NULL; 463c38c5a66SVivien Didelot } 464c38c5a66SVivien Didelot 465a8986681SVladimir Oltean static inline bool dsa_port_is_dsa(struct dsa_port *port) 466a8986681SVladimir Oltean { 467a8986681SVladimir Oltean return port->type == DSA_PORT_TYPE_DSA; 468a8986681SVladimir Oltean } 469a8986681SVladimir Oltean 470a8986681SVladimir Oltean static inline bool dsa_port_is_cpu(struct dsa_port *port) 471a8986681SVladimir Oltean { 472a8986681SVladimir Oltean return port->type == DSA_PORT_TYPE_CPU; 473a8986681SVladimir Oltean } 474a8986681SVladimir Oltean 475a8986681SVladimir Oltean static inline bool dsa_port_is_user(struct dsa_port *dp) 476a8986681SVladimir Oltean { 477a8986681SVladimir Oltean return dp->type == DSA_PORT_TYPE_USER; 478a8986681SVladimir Oltean } 479a8986681SVladimir Oltean 480a57d8c21SVladimir Oltean static inline bool dsa_port_is_unused(struct dsa_port *dp) 481a57d8c21SVladimir Oltean { 482a57d8c21SVladimir Oltean return dp->type == DSA_PORT_TYPE_UNUSED; 483a57d8c21SVladimir Oltean } 484a57d8c21SVladimir Oltean 485295ab96fSVladimir Oltean static inline bool dsa_port_master_is_operational(struct dsa_port *dp) 486295ab96fSVladimir Oltean { 487295ab96fSVladimir Oltean return dsa_port_is_cpu(dp) && dp->master_admin_up && 488295ab96fSVladimir Oltean dp->master_oper_up; 489295ab96fSVladimir Oltean } 490295ab96fSVladimir Oltean 491bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 492bff7b688SVivien Didelot { 493c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 494bff7b688SVivien Didelot } 495bff7b688SVivien Didelot 496c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 497c8f0b869SBen Hutchings { 498c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 499c8f0b869SBen Hutchings } 500c8f0b869SBen Hutchings 50160045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 50260045cbfSAndrew Lunn { 503c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 50460045cbfSAndrew Lunn } 50560045cbfSAndrew Lunn 5062b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 5076cd456f3SVivien Didelot { 508c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 5096cd456f3SVivien Didelot } 5106cd456f3SVivien Didelot 51182b31898SVladimir Oltean #define dsa_tree_for_each_user_port(_dp, _dst) \ 51282b31898SVladimir Oltean list_for_each_entry((_dp), &(_dst)->ports, list) \ 51382b31898SVladimir Oltean if (dsa_port_is_user((_dp))) 51482b31898SVladimir Oltean 51582b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \ 51682b31898SVladimir Oltean list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 51782b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 51882b31898SVladimir Oltean 51982b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \ 52082b31898SVladimir Oltean list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \ 52182b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 52282b31898SVladimir Oltean 52382b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \ 52482b31898SVladimir Oltean list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \ 52582b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 52682b31898SVladimir Oltean 52782b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \ 52882b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 52982b31898SVladimir Oltean if (!dsa_port_is_unused((_dp))) 53082b31898SVladimir Oltean 53182b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \ 53282b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 53382b31898SVladimir Oltean if (dsa_port_is_user((_dp))) 53482b31898SVladimir Oltean 53582b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \ 53682b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 53782b31898SVladimir Oltean if (dsa_port_is_cpu((_dp))) 53882b31898SVladimir Oltean 53902bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 54002bc6e54SVivien Didelot { 541d0004a02SVladimir Oltean struct dsa_port *dp; 542c38c5a66SVivien Didelot u32 mask = 0; 54302bc6e54SVivien Didelot 544d0004a02SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) 545d0004a02SVladimir Oltean mask |= BIT(dp->index); 546c38c5a66SVivien Didelot 547c38c5a66SVivien Didelot return mask; 548c8652c83SVivien Didelot } 549c8652c83SVivien Didelot 550c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 551c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 552c5f51765SVivien Didelot { 553c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 554c5f51765SVivien Didelot struct dsa_link *dl; 555c5f51765SVivien Didelot 556c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 557c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 558c5f51765SVivien Didelot return dl->dp->index; 559c5f51765SVivien Didelot 560c5f51765SVivien Didelot return ds->num_ports; 561c5f51765SVivien Didelot } 562c5f51765SVivien Didelot 5633b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 5643b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 5653b8fac5dSVivien Didelot int port) 5663b8fac5dSVivien Didelot { 5673b8fac5dSVivien Didelot if (device == ds->index) 5683b8fac5dSVivien Didelot return port; 5693b8fac5dSVivien Didelot else 570c5f51765SVivien Didelot return dsa_routing_port(ds, device); 5713b8fac5dSVivien Didelot } 5723b8fac5dSVivien Didelot 5733b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 57407073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 575c8f0b869SBen Hutchings { 57607073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 57707073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 57807073c79SVivien Didelot 57907073c79SVivien Didelot if (!cpu_dp) 58007073c79SVivien Didelot return port; 581c8f0b869SBen Hutchings 5823b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 583c8f0b869SBen Hutchings } 584c8f0b869SBen Hutchings 58563609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */ 58663609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port) 58763609c8fSVladimir Oltean { 58863609c8fSVladimir Oltean if (dsa_is_unused_port(ds, port)) 58963609c8fSVladimir Oltean return false; 59063609c8fSVladimir Oltean 59163609c8fSVladimir Oltean return port == dsa_upstream_port(ds, port); 59263609c8fSVladimir Oltean } 59363609c8fSVladimir Oltean 594d352b20fSTobias Waldekranz /* Return true if this is a DSA port leading away from the CPU */ 595d352b20fSTobias Waldekranz static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port) 596d352b20fSTobias Waldekranz { 597d352b20fSTobias Waldekranz return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port); 598d352b20fSTobias Waldekranz } 599d352b20fSTobias Waldekranz 6007af4a361STobias Waldekranz /* Return the local port used to reach the CPU port */ 6017af4a361STobias Waldekranz static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds) 6027af4a361STobias Waldekranz { 6037af4a361STobias Waldekranz struct dsa_port *dp; 6047af4a361STobias Waldekranz 6057af4a361STobias Waldekranz dsa_switch_for_each_available_port(dp, ds) { 6067af4a361STobias Waldekranz return dsa_upstream_port(ds, dp->index); 6077af4a361STobias Waldekranz } 6087af4a361STobias Waldekranz 6097af4a361STobias Waldekranz return ds->num_ports; 6107af4a361STobias Waldekranz } 6117af4a361STobias Waldekranz 61263609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning 61363609c8fSVladimir Oltean * that the routing port from @downstream_ds to @upstream_ds is also the port 61463609c8fSVladimir Oltean * which @downstream_ds uses to reach its dedicated CPU. 61563609c8fSVladimir Oltean */ 61663609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds, 61763609c8fSVladimir Oltean struct dsa_switch *downstream_ds) 61863609c8fSVladimir Oltean { 61963609c8fSVladimir Oltean int routing_port; 62063609c8fSVladimir Oltean 62163609c8fSVladimir Oltean if (upstream_ds == downstream_ds) 62263609c8fSVladimir Oltean return true; 62363609c8fSVladimir Oltean 62463609c8fSVladimir Oltean routing_port = dsa_routing_port(downstream_ds, upstream_ds->index); 62563609c8fSVladimir Oltean 62663609c8fSVladimir Oltean return dsa_is_upstream_port(downstream_ds, routing_port); 62763609c8fSVladimir Oltean } 62863609c8fSVladimir Oltean 629cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 630cf2d45f5SVladimir Oltean { 631cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 632cf2d45f5SVladimir Oltean 633cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 634cf2d45f5SVladimir Oltean return ds->vlan_filtering; 635cf2d45f5SVladimir Oltean else 636cf2d45f5SVladimir Oltean return dp->vlan_filtering; 637cf2d45f5SVladimir Oltean } 638cf2d45f5SVladimir Oltean 639cc76ce9eSTobias Waldekranz static inline 640cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) 641cc76ce9eSTobias Waldekranz { 642d3eed0e5SVladimir Oltean if (!dp->bridge) 643cc76ce9eSTobias Waldekranz return NULL; 644cc76ce9eSTobias Waldekranz 645cc76ce9eSTobias Waldekranz if (dp->lag_dev) 646cc76ce9eSTobias Waldekranz return dp->lag_dev; 647cc76ce9eSTobias Waldekranz else if (dp->hsr_dev) 648cc76ce9eSTobias Waldekranz return dp->hsr_dev; 649cc76ce9eSTobias Waldekranz 650cc76ce9eSTobias Waldekranz return dp->slave; 651cc76ce9eSTobias Waldekranz } 652cc76ce9eSTobias Waldekranz 65336cbf39bSVladimir Oltean static inline struct net_device * 65436cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp) 65536cbf39bSVladimir Oltean { 656d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->dev : NULL; 65736cbf39bSVladimir Oltean } 65836cbf39bSVladimir Oltean 65936cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp) 66036cbf39bSVladimir Oltean { 661d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->num : 0; 66236cbf39bSVladimir Oltean } 66336cbf39bSVladimir Oltean 66436cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a, 66536cbf39bSVladimir Oltean const struct dsa_port *b) 66636cbf39bSVladimir Oltean { 66736cbf39bSVladimir Oltean struct net_device *br_a = dsa_port_bridge_dev_get(a); 66836cbf39bSVladimir Oltean struct net_device *br_b = dsa_port_bridge_dev_get(b); 66936cbf39bSVladimir Oltean 67036cbf39bSVladimir Oltean /* Standalone ports are not in the same bridge with one another */ 67136cbf39bSVladimir Oltean return (!br_a || !br_b) ? false : (br_a == br_b); 67236cbf39bSVladimir Oltean } 67336cbf39bSVladimir Oltean 6746a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp, 6756a43cba3SVladimir Oltean const struct net_device *dev) 6766a43cba3SVladimir Oltean { 6776a43cba3SVladimir Oltean return dsa_port_to_bridge_port(dp) == dev; 6786a43cba3SVladimir Oltean } 6796a43cba3SVladimir Oltean 6806a43cba3SVladimir Oltean static inline bool 6816a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp, 6826a43cba3SVladimir Oltean const struct net_device *bridge_dev) 6836a43cba3SVladimir Oltean { 6846a43cba3SVladimir Oltean /* DSA ports connected to a bridge, and event was emitted 6856a43cba3SVladimir Oltean * for the bridge. 6866a43cba3SVladimir Oltean */ 6876a43cba3SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge_dev; 6886a43cba3SVladimir Oltean } 6896a43cba3SVladimir Oltean 690d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp, 691d3eed0e5SVladimir Oltean const struct dsa_bridge *bridge) 692d3eed0e5SVladimir Oltean { 693d3eed0e5SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge->dev; 694d3eed0e5SVladimir Oltean } 695d3eed0e5SVladimir Oltean 6966a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */ 6976a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst, 6986a43cba3SVladimir Oltean const struct net_device *dev) 6996a43cba3SVladimir Oltean { 7006a43cba3SVladimir Oltean struct dsa_port *dp; 7016a43cba3SVladimir Oltean 7026a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7036a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_port(dp, dev)) 7046a43cba3SVladimir Oltean return true; 7056a43cba3SVladimir Oltean 7066a43cba3SVladimir Oltean return false; 7076a43cba3SVladimir Oltean } 7086a43cba3SVladimir Oltean 7096a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */ 7106a43cba3SVladimir Oltean static inline bool 7116a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst, 7126a43cba3SVladimir Oltean const struct net_device *bridge_dev) 7136a43cba3SVladimir Oltean { 7146a43cba3SVladimir Oltean struct dsa_port *dp; 7156a43cba3SVladimir Oltean 7166a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7176a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_dev(dp, bridge_dev)) 7186a43cba3SVladimir Oltean return true; 7196a43cba3SVladimir Oltean 7206a43cba3SVladimir Oltean return false; 7216a43cba3SVladimir Oltean } 7226a43cba3SVladimir Oltean 7232bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 7242bedde1aSArkadi Sharshevsky bool is_static, void *data); 7259d490b4eSVivien Didelot struct dsa_switch_ops { 72653da0ebaSVladimir Oltean /* 72753da0ebaSVladimir Oltean * Tagging protocol helpers called for the CPU ports and DSA links. 72853da0ebaSVladimir Oltean * @get_tag_protocol retrieves the initial tagging protocol and is 72953da0ebaSVladimir Oltean * mandatory. Switches which can operate using multiple tagging 73053da0ebaSVladimir Oltean * protocols should implement @change_tag_protocol and report in 73153da0ebaSVladimir Oltean * @get_tag_protocol the tagger in current use. 73253da0ebaSVladimir Oltean */ 7335ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 7344d776482SFlorian Fainelli int port, 7354d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 73653da0ebaSVladimir Oltean int (*change_tag_protocol)(struct dsa_switch *ds, int port, 73753da0ebaSVladimir Oltean enum dsa_tag_protocol proto); 738dc452a47SVladimir Oltean /* 739dc452a47SVladimir Oltean * Method for switch drivers to connect to the tagging protocol driver 740dc452a47SVladimir Oltean * in current use. The switch driver can provide handlers for certain 741dc452a47SVladimir Oltean * types of packets for switch management. 742dc452a47SVladimir Oltean */ 743dc452a47SVladimir Oltean int (*connect_tag_protocol)(struct dsa_switch *ds, 744dc452a47SVladimir Oltean enum dsa_tag_protocol proto); 7457b314362SAndrew Lunn 746fd292c18SVladimir Oltean /* Optional switch-wide initialization and destruction methods */ 747c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 7485e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 749fd292c18SVladimir Oltean 750fd292c18SVladimir Oltean /* Per-port initialization and destruction methods. Mandatory if the 751fd292c18SVladimir Oltean * driver registers devlink port regions, optional otherwise. 752fd292c18SVladimir Oltean */ 753fd292c18SVladimir Oltean int (*port_setup)(struct dsa_switch *ds, int port); 754fd292c18SVladimir Oltean void (*port_teardown)(struct dsa_switch *ds, int port); 755fd292c18SVladimir Oltean 7566819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 757c8f0b869SBen Hutchings 758c8f0b869SBen Hutchings /* 759c8f0b869SBen Hutchings * Access to the switch's PHY registers. 760c8f0b869SBen Hutchings */ 761c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 762c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 763c8f0b869SBen Hutchings int regnum, u16 val); 764c8f0b869SBen Hutchings 765c8f0b869SBen Hutchings /* 766ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 767ec9436baSFlorian Fainelli */ 768ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 769ec9436baSFlorian Fainelli struct phy_device *phydev); 770ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 771ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 772ec9436baSFlorian Fainelli 773ec9436baSFlorian Fainelli /* 77411d8f3ddSFlorian Fainelli * PHYLINK integration 77511d8f3ddSFlorian Fainelli */ 776072eea6cSRussell King (Oracle) void (*phylink_get_caps)(struct dsa_switch *ds, int port, 777072eea6cSRussell King (Oracle) struct phylink_config *config); 77811d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 77911d8f3ddSFlorian Fainelli unsigned long *supported, 78011d8f3ddSFlorian Fainelli struct phylink_link_state *state); 78111d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 78211d8f3ddSFlorian Fainelli struct phylink_link_state *state); 78311d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 78411d8f3ddSFlorian Fainelli unsigned int mode, 78511d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 78611d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 78711d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 78811d8f3ddSFlorian Fainelli unsigned int mode, 78911d8f3ddSFlorian Fainelli phy_interface_t interface); 79011d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 79111d8f3ddSFlorian Fainelli unsigned int mode, 79211d8f3ddSFlorian Fainelli phy_interface_t interface, 7935b502a7bSRussell King struct phy_device *phydev, 7945b502a7bSRussell King int speed, int duplex, 7955b502a7bSRussell King bool tx_pause, bool rx_pause); 79611d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 79711d8f3ddSFlorian Fainelli struct phylink_link_state *state); 79811d8f3ddSFlorian Fainelli /* 799c2ec5f2eSOleksij Rempel * Port statistics counters. 800c8f0b869SBen Hutchings */ 80189f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 80289f09048SFlorian Fainelli u32 stringset, uint8_t *data); 803c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 804c8f0b869SBen Hutchings int port, uint64_t *data); 80589f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 806cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 807cf963573SFlorian Fainelli int port, uint64_t *data); 808487d3855SAlvin Šipraga void (*get_eth_phy_stats)(struct dsa_switch *ds, int port, 809487d3855SAlvin Šipraga struct ethtool_eth_phy_stats *phy_stats); 810487d3855SAlvin Šipraga void (*get_eth_mac_stats)(struct dsa_switch *ds, int port, 811487d3855SAlvin Šipraga struct ethtool_eth_mac_stats *mac_stats); 812487d3855SAlvin Šipraga void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port, 813487d3855SAlvin Šipraga struct ethtool_eth_ctrl_stats *ctrl_stats); 814c2ec5f2eSOleksij Rempel void (*get_stats64)(struct dsa_switch *ds, int port, 815c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s); 816a71acad9SOleksij Rempel void (*self_test)(struct dsa_switch *ds, int port, 817a71acad9SOleksij Rempel struct ethtool_test *etest, u64 *data); 81824462549SFlorian Fainelli 81924462549SFlorian Fainelli /* 82019e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 82119e57c4eSFlorian Fainelli */ 82219e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 82319e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 82419e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 82519e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 82619e57c4eSFlorian Fainelli 82719e57c4eSFlorian Fainelli /* 8280336369dSBrandon Streiff * ethtool timestamp info 8290336369dSBrandon Streiff */ 8300336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 8310336369dSBrandon Streiff struct ethtool_ts_info *ts); 8320336369dSBrandon Streiff 8330336369dSBrandon Streiff /* 83424462549SFlorian Fainelli * Suspend and resume 83524462549SFlorian Fainelli */ 83624462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 83724462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 838b2f2af21SFlorian Fainelli 839b2f2af21SFlorian Fainelli /* 840b2f2af21SFlorian Fainelli * Port enable/disable 841b2f2af21SFlorian Fainelli */ 842b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 843b2f2af21SFlorian Fainelli struct phy_device *phy); 84475104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 8457905288fSFlorian Fainelli 8467905288fSFlorian Fainelli /* 84708f50061SVivien Didelot * Port's MAC EEE settings 8487905288fSFlorian Fainelli */ 84908f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 8507905288fSFlorian Fainelli struct ethtool_eee *e); 85108f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 8527905288fSFlorian Fainelli struct ethtool_eee *e); 85351579c3fSGuenter Roeck 8546793abb4SGuenter Roeck /* EEPROM access */ 8556793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 8566793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 8576793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 8586793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 8596793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 8603d762a0fSGuenter Roeck 8613d762a0fSGuenter Roeck /* 8623d762a0fSGuenter Roeck * Register access. 8633d762a0fSGuenter Roeck */ 8643d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 8653d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 8663d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 867b73adef6SFlorian Fainelli 868b73adef6SFlorian Fainelli /* 869e358bef7SVladimir Oltean * Upper device tracking. 870e358bef7SVladimir Oltean */ 871e358bef7SVladimir Oltean int (*port_prechangeupper)(struct dsa_switch *ds, int port, 872e358bef7SVladimir Oltean struct netdev_notifier_changeupper_info *info); 873e358bef7SVladimir Oltean 874e358bef7SVladimir Oltean /* 875b73adef6SFlorian Fainelli * Bridge integration 876b73adef6SFlorian Fainelli */ 87734a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 87871327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 879b079922bSVladimir Oltean struct dsa_bridge bridge, 880b079922bSVladimir Oltean bool *tx_fwd_offload); 881f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 882d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 88343c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 884b73adef6SFlorian Fainelli u8 state); 885732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 886a8b659e7SVladimir Oltean int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 887a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 888a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 889a8b659e7SVladimir Oltean int (*port_bridge_flags)(struct dsa_switch *ds, int port, 890a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 891a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 8922a778e1bSVivien Didelot 8932a778e1bSVivien Didelot /* 89411149536SVivien Didelot * VLAN support 89511149536SVivien Didelot */ 896fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 89789153ed6SVladimir Oltean bool vlan_filtering, 89889153ed6SVladimir Oltean struct netlink_ext_ack *extack); 8991958d581SVladimir Oltean int (*port_vlan_add)(struct dsa_switch *ds, int port, 90031046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 90131046a5fSVladimir Oltean struct netlink_ext_ack *extack); 90276e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 90376e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 90411149536SVivien Didelot /* 9052a778e1bSVivien Didelot * Forwarding database 9062a778e1bSVivien Didelot */ 9071b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 9086c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 9092a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 9106c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 911ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 9122bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 9138df30255SVivien Didelot 9148df30255SVivien Didelot /* 9158df30255SVivien Didelot * Multicast database 9168df30255SVivien Didelot */ 917a52b2da7SVladimir Oltean int (*port_mdb_add)(struct dsa_switch *ds, int port, 9183709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 9198df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 9208df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb); 921bf9f2648SFlorian Fainelli /* 922bf9f2648SFlorian Fainelli * RXNFC 923bf9f2648SFlorian Fainelli */ 924bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 925bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 926bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 927bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 928f50f2127SFlorian Fainelli 929f50f2127SFlorian Fainelli /* 930f50f2127SFlorian Fainelli * TC integration 931f50f2127SFlorian Fainelli */ 932ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 933ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 934ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 935ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 936ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 937ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 938f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 939f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 940f50f2127SFlorian Fainelli bool ingress); 941f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 942f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 94334297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 94434297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 94534297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 94647d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 94747d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 94840ef2c93SVivien Didelot 94940ef2c93SVivien Didelot /* 95040ef2c93SVivien Didelot * Cross-chip operations 95140ef2c93SVivien Didelot */ 952f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 953f66a6a69SVladimir Oltean int sw_index, int port, 954d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 955f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 956f66a6a69SVladimir Oltean int sw_index, int port, 957d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 958058102a6STobias Waldekranz int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 959058102a6STobias Waldekranz int port); 960058102a6STobias Waldekranz int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 961058102a6STobias Waldekranz int port, struct net_device *lag, 962058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 963058102a6STobias Waldekranz int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 964058102a6STobias Waldekranz int port, struct net_device *lag); 9650336369dSBrandon Streiff 9660336369dSBrandon Streiff /* 9670336369dSBrandon Streiff * PTP functionality 9680336369dSBrandon Streiff */ 9690336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 9700336369dSBrandon Streiff struct ifreq *ifr); 9710336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 9720336369dSBrandon Streiff struct ifreq *ifr); 9735c5416f5SYangbo Lu void (*port_txtstamp)(struct dsa_switch *ds, int port, 9745c5416f5SYangbo Lu struct sk_buff *skb); 97590af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 97690af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 97797a69a0dSVladimir Oltean 9780f06b855SAndrew Lunn /* Devlink parameters, etc */ 9796b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 9806b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 9816b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 9826b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 9830f06b855SAndrew Lunn int (*devlink_info_get)(struct dsa_switch *ds, 9840f06b855SAndrew Lunn struct devlink_info_req *req, 9850f06b855SAndrew Lunn struct netlink_ext_ack *extack); 9862a6ef763SVladimir Oltean int (*devlink_sb_pool_get)(struct dsa_switch *ds, 9872a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 9882a6ef763SVladimir Oltean struct devlink_sb_pool_info *pool_info); 9892a6ef763SVladimir Oltean int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 9902a6ef763SVladimir Oltean u16 pool_index, u32 size, 9912a6ef763SVladimir Oltean enum devlink_sb_threshold_type threshold_type, 9922a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 9932a6ef763SVladimir Oltean int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 9942a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 9952a6ef763SVladimir Oltean u32 *p_threshold); 9962a6ef763SVladimir Oltean int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 9972a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 9982a6ef763SVladimir Oltean u32 threshold, 9992a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10002a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 10012a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10022a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10032a6ef763SVladimir Oltean u16 *p_pool_index, u32 *p_threshold); 10042a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 10052a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10062a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10072a6ef763SVladimir Oltean u16 pool_index, u32 threshold, 10082a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10092a6ef763SVladimir Oltean int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 10102a6ef763SVladimir Oltean unsigned int sb_index); 10112a6ef763SVladimir Oltean int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 10122a6ef763SVladimir Oltean unsigned int sb_index); 10132a6ef763SVladimir Oltean int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 10142a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10152a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 10162a6ef763SVladimir Oltean int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 10172a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10182a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10192a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 1020bfcb8132SVladimir Oltean 1021bfcb8132SVladimir Oltean /* 1022bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 1023bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 1024bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 1025ab88d64aSRandy Dunlap * method needs to do so privately. 1026bfcb8132SVladimir Oltean */ 1027bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 1028bfcb8132SVladimir Oltean int new_mtu); 1029bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 1030058102a6STobias Waldekranz 1031058102a6STobias Waldekranz /* 1032058102a6STobias Waldekranz * LAG integration 1033058102a6STobias Waldekranz */ 1034058102a6STobias Waldekranz int (*port_lag_change)(struct dsa_switch *ds, int port); 1035058102a6STobias Waldekranz int (*port_lag_join)(struct dsa_switch *ds, int port, 1036058102a6STobias Waldekranz struct net_device *lag, 1037058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 1038058102a6STobias Waldekranz int (*port_lag_leave)(struct dsa_switch *ds, int port, 1039058102a6STobias Waldekranz struct net_device *lag); 104018596f50SGeorge McCollister 104118596f50SGeorge McCollister /* 104218596f50SGeorge McCollister * HSR integration 104318596f50SGeorge McCollister */ 104418596f50SGeorge McCollister int (*port_hsr_join)(struct dsa_switch *ds, int port, 104518596f50SGeorge McCollister struct net_device *hsr); 104618596f50SGeorge McCollister int (*port_hsr_leave)(struct dsa_switch *ds, int port, 104718596f50SGeorge McCollister struct net_device *hsr); 1048c595c433SHoratiu Vultur 1049c595c433SHoratiu Vultur /* 1050c595c433SHoratiu Vultur * MRP integration 1051c595c433SHoratiu Vultur */ 1052c595c433SHoratiu Vultur int (*port_mrp_add)(struct dsa_switch *ds, int port, 1053c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1054c595c433SHoratiu Vultur int (*port_mrp_del)(struct dsa_switch *ds, int port, 1055c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1056c595c433SHoratiu Vultur int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 1057c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 1058c595c433SHoratiu Vultur int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 1059c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 10605da11eb4SVladimir Oltean 10615da11eb4SVladimir Oltean /* 10625da11eb4SVladimir Oltean * tag_8021q operations 10635da11eb4SVladimir Oltean */ 10645da11eb4SVladimir Oltean int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 10655da11eb4SVladimir Oltean u16 flags); 10665da11eb4SVladimir Oltean int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 1067295ab96fSVladimir Oltean 1068295ab96fSVladimir Oltean /* 1069295ab96fSVladimir Oltean * DSA master tracking operations 1070295ab96fSVladimir Oltean */ 1071295ab96fSVladimir Oltean void (*master_state_change)(struct dsa_switch *ds, 1072295ab96fSVladimir Oltean const struct net_device *master, 1073295ab96fSVladimir Oltean bool operational); 10746b297524SAndrew Lunn }; 10756b297524SAndrew Lunn 10766b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 10776b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 10786b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 10796b297524SAndrew Lunn 10806b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 10816b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10826b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 10836b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10846b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 10856b297524SAndrew Lunn const struct devlink_param *params, 10866b297524SAndrew Lunn size_t params_count); 10876b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 10886b297524SAndrew Lunn const struct devlink_param *params, 10896b297524SAndrew Lunn size_t params_count); 10905cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 10915cd73fbdSAndrew Lunn const char *resource_name, 10925cd73fbdSAndrew Lunn u64 resource_size, 10935cd73fbdSAndrew Lunn u64 resource_id, 10945cd73fbdSAndrew Lunn u64 parent_resource_id, 10955cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 10965cd73fbdSAndrew Lunn 10975cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 10985cd73fbdSAndrew Lunn 10995cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 11005cd73fbdSAndrew Lunn u64 resource_id, 11015cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 11025cd73fbdSAndrew Lunn void *occ_get_priv); 11035cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 11045cd73fbdSAndrew Lunn u64 resource_id); 110597c82c23SAndrew Lunn struct devlink_region * 110697c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds, 110797c82c23SAndrew Lunn const struct devlink_region_ops *ops, 110897c82c23SAndrew Lunn u32 region_max_snapshots, u64 region_size); 110908156ba4SAndrew Lunn struct devlink_region * 111008156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds, 111108156ba4SAndrew Lunn int port, 111208156ba4SAndrew Lunn const struct devlink_port_region_ops *ops, 111308156ba4SAndrew Lunn u32 region_max_snapshots, u64 region_size); 111497c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region); 111597c82c23SAndrew Lunn 1116e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 11175cd73fbdSAndrew Lunn 11186b297524SAndrew Lunn struct dsa_devlink_priv { 11196b297524SAndrew Lunn struct dsa_switch *ds; 1120c8f0b869SBen Hutchings }; 1121c8f0b869SBen Hutchings 1122ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 1123ccc3e6b0SAndrew Lunn { 1124ccc3e6b0SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1125ccc3e6b0SAndrew Lunn 1126ccc3e6b0SAndrew Lunn return dl_priv->ds; 1127ccc3e6b0SAndrew Lunn } 1128ccc3e6b0SAndrew Lunn 11297d1e2a10SAndrew Lunn static inline 11307d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 11317d1e2a10SAndrew Lunn { 11327d1e2a10SAndrew Lunn struct devlink *dl = port->devlink; 11337d1e2a10SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 11347d1e2a10SAndrew Lunn 11357d1e2a10SAndrew Lunn return dl_priv->ds; 11367d1e2a10SAndrew Lunn } 11377d1e2a10SAndrew Lunn 11387d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port) 11397d1e2a10SAndrew Lunn { 11407d1e2a10SAndrew Lunn return port->index; 11417d1e2a10SAndrew Lunn } 11427d1e2a10SAndrew Lunn 1143ab3d408dSFlorian Fainelli struct dsa_switch_driver { 1144ab3d408dSFlorian Fainelli struct list_head list; 1145a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 1146ab3d408dSFlorian Fainelli }; 1147ab3d408dSFlorian Fainelli 114814b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 1149c8f0b869SBen Hutchings 115073a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 11519eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 1152c6e970a0SAndrew Lunn { 1153c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 1154717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 1155c6e970a0SAndrew Lunn #endif 1156c6e970a0SAndrew Lunn return false; 1157c6e970a0SAndrew Lunn } 1158c6e970a0SAndrew Lunn 11599790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail 11609790cf20SVladimir Oltean * tags, which don't break dissection) can be treated the same from the 11619790cf20SVladimir Oltean * perspective of the flow dissector. 11629790cf20SVladimir Oltean * 11639790cf20SVladimir Oltean * We need to return: 11649790cf20SVladimir Oltean * - offset: the (B - A) difference between: 11659790cf20SVladimir Oltean * A. the position of the real EtherType and 11669790cf20SVladimir Oltean * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 11679790cf20SVladimir Oltean * after the normal EtherType was supposed to be) 11689790cf20SVladimir Oltean * The offset in bytes is exactly equal to the tagger overhead (and half of 11699790cf20SVladimir Oltean * that, in __be16 shorts). 11709790cf20SVladimir Oltean * 11719790cf20SVladimir Oltean * - proto: the value of the real EtherType. 11729790cf20SVladimir Oltean */ 11739790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 11749790cf20SVladimir Oltean __be16 *proto, int *offset) 11759790cf20SVladimir Oltean { 11769790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 11779790cf20SVladimir Oltean const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 11784e500251SVladimir Oltean int tag_len = ops->needed_headroom; 11799790cf20SVladimir Oltean 11809790cf20SVladimir Oltean *offset = tag_len; 11819790cf20SVladimir Oltean *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 11829790cf20SVladimir Oltean #endif 11839790cf20SVladimir Oltean } 11849790cf20SVladimir Oltean 11854cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 11864cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev) 11874cfab356SFlorian Fainelli { 11884cfab356SFlorian Fainelli int err = -EOPNOTSUPP; 11894cfab356SFlorian Fainelli 11904cfab356SFlorian Fainelli if (!dev->dsa_ptr) 11914cfab356SFlorian Fainelli return err; 11924cfab356SFlorian Fainelli 11934cfab356SFlorian Fainelli if (!dev->dsa_ptr->netdev_ops) 11944cfab356SFlorian Fainelli return err; 11954cfab356SFlorian Fainelli 11964cfab356SFlorian Fainelli return 0; 11974cfab356SFlorian Fainelli } 11984cfab356SFlorian Fainelli 1199a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 12004cfab356SFlorian Fainelli int cmd) 12014cfab356SFlorian Fainelli { 12024cfab356SFlorian Fainelli const struct dsa_netdevice_ops *ops; 12034cfab356SFlorian Fainelli int err; 12044cfab356SFlorian Fainelli 12054cfab356SFlorian Fainelli err = __dsa_netdevice_ops_check(dev); 12064cfab356SFlorian Fainelli if (err) 12074cfab356SFlorian Fainelli return err; 12084cfab356SFlorian Fainelli 12094cfab356SFlorian Fainelli ops = dev->dsa_ptr->netdev_ops; 12104cfab356SFlorian Fainelli 1211a7605370SArnd Bergmann return ops->ndo_eth_ioctl(dev, ifr, cmd); 12124cfab356SFlorian Fainelli } 12134cfab356SFlorian Fainelli #else 1214a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 12154cfab356SFlorian Fainelli int cmd) 12164cfab356SFlorian Fainelli { 12174cfab356SFlorian Fainelli return -EOPNOTSUPP; 12184cfab356SFlorian Fainelli } 12194cfab356SFlorian Fainelli #endif 12204cfab356SFlorian Fainelli 122183c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 122223c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 12230650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds); 12243b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1225ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 1226ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 1227ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 1228ea825e70SFlorian Fainelli #else 1229ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 1230ea825e70SFlorian Fainelli { 1231ea825e70SFlorian Fainelli return 0; 1232ea825e70SFlorian Fainelli } 1233ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 1234ea825e70SFlorian Fainelli { 1235ea825e70SFlorian Fainelli return 0; 1236ea825e70SFlorian Fainelli } 1237ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 1238ea825e70SFlorian Fainelli 123960724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 1240a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev); 124160724d4bSFlorian Fainelli #else 1242a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev) 1243a5e3c9baSVladimir Oltean { 1244a5e3c9baSVladimir Oltean return false; 1245a5e3c9baSVladimir Oltean } 124660724d4bSFlorian Fainelli #endif 124760724d4bSFlorian Fainelli 124897a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 1249cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 1250cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 1251cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp); 125211d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1253cf963573SFlorian Fainelli 1254d3b8c049SAndrew Lunn struct dsa_tag_driver { 1255d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 1256d3b8c049SAndrew Lunn struct list_head list; 1257d3b8c049SAndrew Lunn struct module *owner; 1258d3b8c049SAndrew Lunn }; 1259d3b8c049SAndrew Lunn 1260d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1261d3b8c049SAndrew Lunn unsigned int count, 1262d3b8c049SAndrew Lunn struct module *owner); 1263d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1264d3b8c049SAndrew Lunn unsigned int count); 1265d3b8c049SAndrew Lunn 1266d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1267d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 1268d3b8c049SAndrew Lunn { \ 1269d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1270d3b8c049SAndrew Lunn THIS_MODULE); \ 1271d3b8c049SAndrew Lunn return 0; \ 1272d3b8c049SAndrew Lunn } \ 1273d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 1274d3b8c049SAndrew Lunn \ 1275d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 1276d3b8c049SAndrew Lunn { \ 1277d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1278d3b8c049SAndrew Lunn } \ 1279d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 1280d3b8c049SAndrew Lunn 1281d3b8c049SAndrew Lunn /** 1282d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1283d3b8c049SAndrew Lunn * drivers 1284*c7d9a675SLuiz Angelo Daros de Luca * @__ops_array: Array of tag driver structures 1285d3b8c049SAndrew Lunn * 1286d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1287d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1288d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1289d3b8c049SAndrew Lunn */ 1290d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 1291d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1292d3b8c049SAndrew Lunn 1293d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1294d3b8c049SAndrew Lunn 1295d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 1296d3b8c049SAndrew Lunn * drivers 1297d3b8c049SAndrew Lunn */ 1298d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 1299d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1300d3b8c049SAndrew Lunn .ops = &__ops, \ 1301d3b8c049SAndrew Lunn } 1302d3b8c049SAndrew Lunn 1303d3b8c049SAndrew Lunn /** 1304d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1305d3b8c049SAndrew Lunn * driver 1306d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 1307d3b8c049SAndrew Lunn * 1308d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1309d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1310d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1311d3b8c049SAndrew Lunn */ 1312d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 1313d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 1314d3b8c049SAndrew Lunn \ 1315d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1316d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 1317d3b8c049SAndrew Lunn }; \ 1318d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 131991da11f8SLennert Buytenhek #endif 1320d3b8c049SAndrew Lunn 1321