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, 185*46a76724SVladimir Oltean struct net_device *lag_dev) 186058102a6STobias Waldekranz { 187058102a6STobias Waldekranz unsigned int id; 188058102a6STobias Waldekranz 189058102a6STobias Waldekranz dsa_lags_foreach_id(id, dst) { 190*46a76724SVladimir Oltean if (dsa_lag_dev(dst, id) == lag_dev) 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; 315134ef238SVladimir Oltean 316134ef238SVladimir Oltean /* List of VLANs that CPU and DSA ports are members of. */ 317134ef238SVladimir Oltean struct mutex vlans_lock; 318134ef238SVladimir Oltean struct list_head vlans; 319c8b09808SAndrew Lunn }; 320c8b09808SAndrew Lunn 321c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member, 322c5f51765SVivien Didelot * and no dst->rtable nor this struct dsa_link would be needed, 323c5f51765SVivien Didelot * but this would require some more complex tree walking, 324c5f51765SVivien Didelot * so keep it stupid at the moment and list them all. 325c5f51765SVivien Didelot */ 326c5f51765SVivien Didelot struct dsa_link { 327c5f51765SVivien Didelot struct dsa_port *dp; 328c5f51765SVivien Didelot struct dsa_port *link_dp; 329c5f51765SVivien Didelot struct list_head list; 330c5f51765SVivien Didelot }; 331c5f51765SVivien Didelot 332161ca59dSVladimir Oltean struct dsa_mac_addr { 333161ca59dSVladimir Oltean unsigned char addr[ETH_ALEN]; 334161ca59dSVladimir Oltean u16 vid; 335161ca59dSVladimir Oltean refcount_t refcount; 336161ca59dSVladimir Oltean struct list_head list; 337161ca59dSVladimir Oltean }; 338161ca59dSVladimir Oltean 339134ef238SVladimir Oltean struct dsa_vlan { 340134ef238SVladimir Oltean u16 vid; 341134ef238SVladimir Oltean refcount_t refcount; 342134ef238SVladimir Oltean struct list_head list; 343134ef238SVladimir Oltean }; 344134ef238SVladimir Oltean 345c8f0b869SBen Hutchings struct dsa_switch { 346c33063d6SAndrew Lunn struct device *dev; 347c33063d6SAndrew Lunn 348c8f0b869SBen Hutchings /* 349c8f0b869SBen Hutchings * Parent switch tree, and switch index. 350c8f0b869SBen Hutchings */ 351c8f0b869SBen Hutchings struct dsa_switch_tree *dst; 35299feaafcSVivien Didelot unsigned int index; 353c8f0b869SBen Hutchings 3541b26d364SVladimir Oltean /* Warning: the following bit fields are not atomic, and updating them 3551b26d364SVladimir Oltean * can only be done from code paths where concurrency is not possible 3561b26d364SVladimir Oltean * (probe time or under rtnl_lock). 3571b26d364SVladimir Oltean */ 35863cfc657SVladimir Oltean u32 setup:1; 35963cfc657SVladimir Oltean 36063cfc657SVladimir Oltean /* Disallow bridge core from requesting different VLAN awareness 36163cfc657SVladimir Oltean * settings on ports if not hardware-supported 3627787ff77SVladimir Oltean */ 36363cfc657SVladimir Oltean u32 vlan_filtering_is_global:1; 36463cfc657SVladimir Oltean 36563cfc657SVladimir Oltean /* Keep VLAN filtering enabled on ports not offloading any upper */ 36663cfc657SVladimir Oltean u32 needs_standalone_vlan_filtering:1; 36763cfc657SVladimir Oltean 36863cfc657SVladimir Oltean /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 36963cfc657SVladimir Oltean * that have vlan_filtering=0. All drivers should ideally set this (and 37063cfc657SVladimir Oltean * then the option would get removed), but it is unknown whether this 3717787ff77SVladimir Oltean * would break things or not. 3727787ff77SVladimir Oltean */ 37363cfc657SVladimir Oltean u32 configure_vlan_while_not_filtering:1; 37463cfc657SVladimir Oltean 37563cfc657SVladimir Oltean /* If the switch driver always programs the CPU port as egress tagged 37663cfc657SVladimir Oltean * despite the VLAN configuration indicating otherwise, then setting 37763cfc657SVladimir Oltean * @untag_bridge_pvid will force the DSA receive path to pop the 37863cfc657SVladimir Oltean * bridge's default_pvid VLAN tagged frames to offer a consistent 37963cfc657SVladimir Oltean * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge 3807787ff77SVladimir Oltean * device. 3817787ff77SVladimir Oltean */ 38263cfc657SVladimir Oltean u32 untag_bridge_pvid:1; 38363cfc657SVladimir Oltean 3847787ff77SVladimir Oltean /* Let DSA manage the FDB entries towards the 3857787ff77SVladimir Oltean * CPU, based on the software bridge database. 3867787ff77SVladimir Oltean */ 38763cfc657SVladimir Oltean u32 assisted_learning_on_cpu_port:1; 38863cfc657SVladimir Oltean 38963cfc657SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 39063cfc657SVladimir Oltean * should be retrieved from here and not from the per-port settings. 3917787ff77SVladimir Oltean */ 39263cfc657SVladimir Oltean u32 vlan_filtering:1; 39363cfc657SVladimir Oltean 39463cfc657SVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 39563cfc657SVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 39663cfc657SVladimir Oltean * interfaces is needed. 3977787ff77SVladimir Oltean */ 39863cfc657SVladimir Oltean u32 mtu_enforcement_ingress:1; 3997787ff77SVladimir Oltean 400f515f192SVivien Didelot /* Listener for switch fabric events */ 401f515f192SVivien Didelot struct notifier_block nb; 402f515f192SVivien Didelot 403c8f0b869SBen Hutchings /* 4047543a6d5SAndrew Lunn * Give the switch driver somewhere to hang its private data 4057543a6d5SAndrew Lunn * structure. 4067543a6d5SAndrew Lunn */ 4077543a6d5SAndrew Lunn void *priv; 4087543a6d5SAndrew Lunn 409dc452a47SVladimir Oltean void *tagger_data; 410dc452a47SVladimir Oltean 4117543a6d5SAndrew Lunn /* 412c8f0b869SBen Hutchings * Configuration data for this switch. 413c8f0b869SBen Hutchings */ 414ff04955cSAndrew Lunn struct dsa_chip_data *cd; 415c8f0b869SBen Hutchings 416c8f0b869SBen Hutchings /* 4179d490b4eSVivien Didelot * The switch operations. 418c8f0b869SBen Hutchings */ 419a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 420c8f0b869SBen Hutchings 42166472fc0SAndrew Lunn /* 422c8f0b869SBen Hutchings * Slave mii_bus and devices for the individual ports. 423c8f0b869SBen Hutchings */ 4240d8bcdd3SFlorian Fainelli u32 phys_mii_mask; 425c8f0b869SBen Hutchings struct mii_bus *slave_mii_bus; 426a0c02161SVivien Didelot 4270f3da6afSVivien Didelot /* Ageing Time limits in msecs */ 4280f3da6afSVivien Didelot unsigned int ageing_time_min; 4290f3da6afSVivien Didelot unsigned int ageing_time_max; 4300f3da6afSVivien Didelot 431d7b1fd52SVladimir Oltean /* Storage for drivers using tag_8021q */ 432d7b1fd52SVladimir Oltean struct dsa_8021q_context *tag_8021q_ctx; 433d7b1fd52SVladimir Oltean 43496567d5dSAndrew Lunn /* devlink used to represent this switch device */ 43596567d5dSAndrew Lunn struct devlink *devlink; 43696567d5dSAndrew Lunn 43755199df6SFlorian Fainelli /* Number of switch port queues */ 43855199df6SFlorian Fainelli unsigned int num_tx_queues; 43955199df6SFlorian Fainelli 440058102a6STobias Waldekranz /* Drivers that benefit from having an ID associated with each 441058102a6STobias Waldekranz * offloaded LAG should set this to the maximum number of 442058102a6STobias Waldekranz * supported IDs. DSA will then maintain a mapping of _at 443058102a6STobias Waldekranz * least_ these many IDs, accessible to drivers via 444058102a6STobias Waldekranz * dsa_lag_id(). 445058102a6STobias Waldekranz */ 446058102a6STobias Waldekranz unsigned int num_lag_ids; 447058102a6STobias Waldekranz 448947c8746SVladimir Oltean /* Drivers that support bridge forwarding offload or FDB isolation 449947c8746SVladimir Oltean * should set this to the maximum number of bridges spanning the same 450947c8746SVladimir Oltean * switch tree (or all trees, in the case of cross-tree bridging 451947c8746SVladimir Oltean * support) that can be offloaded. 452123abc06SVladimir Oltean */ 453947c8746SVladimir Oltean unsigned int max_num_bridges; 454123abc06SVladimir Oltean 455258030acSVladimir Oltean unsigned int num_ports; 456c8f0b869SBen Hutchings }; 457c8f0b869SBen Hutchings 45868bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 459c38c5a66SVivien Didelot { 460b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 461d607525bSVivien Didelot struct dsa_port *dp; 462b96ddf25SVivien Didelot 463b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 464b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 465b96ddf25SVivien Didelot return dp; 466d607525bSVivien Didelot 467d607525bSVivien Didelot return NULL; 468c38c5a66SVivien Didelot } 469c38c5a66SVivien Didelot 470a8986681SVladimir Oltean static inline bool dsa_port_is_dsa(struct dsa_port *port) 471a8986681SVladimir Oltean { 472a8986681SVladimir Oltean return port->type == DSA_PORT_TYPE_DSA; 473a8986681SVladimir Oltean } 474a8986681SVladimir Oltean 475a8986681SVladimir Oltean static inline bool dsa_port_is_cpu(struct dsa_port *port) 476a8986681SVladimir Oltean { 477a8986681SVladimir Oltean return port->type == DSA_PORT_TYPE_CPU; 478a8986681SVladimir Oltean } 479a8986681SVladimir Oltean 480a8986681SVladimir Oltean static inline bool dsa_port_is_user(struct dsa_port *dp) 481a8986681SVladimir Oltean { 482a8986681SVladimir Oltean return dp->type == DSA_PORT_TYPE_USER; 483a8986681SVladimir Oltean } 484a8986681SVladimir Oltean 485a57d8c21SVladimir Oltean static inline bool dsa_port_is_unused(struct dsa_port *dp) 486a57d8c21SVladimir Oltean { 487a57d8c21SVladimir Oltean return dp->type == DSA_PORT_TYPE_UNUSED; 488a57d8c21SVladimir Oltean } 489a57d8c21SVladimir Oltean 490295ab96fSVladimir Oltean static inline bool dsa_port_master_is_operational(struct dsa_port *dp) 491295ab96fSVladimir Oltean { 492295ab96fSVladimir Oltean return dsa_port_is_cpu(dp) && dp->master_admin_up && 493295ab96fSVladimir Oltean dp->master_oper_up; 494295ab96fSVladimir Oltean } 495295ab96fSVladimir Oltean 496bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 497bff7b688SVivien Didelot { 498c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 499bff7b688SVivien Didelot } 500bff7b688SVivien Didelot 501c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 502c8f0b869SBen Hutchings { 503c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 504c8f0b869SBen Hutchings } 505c8f0b869SBen Hutchings 50660045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 50760045cbfSAndrew Lunn { 508c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 50960045cbfSAndrew Lunn } 51060045cbfSAndrew Lunn 5112b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 5126cd456f3SVivien Didelot { 513c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 5146cd456f3SVivien Didelot } 5156cd456f3SVivien Didelot 51682b31898SVladimir Oltean #define dsa_tree_for_each_user_port(_dp, _dst) \ 51782b31898SVladimir Oltean list_for_each_entry((_dp), &(_dst)->ports, list) \ 51882b31898SVladimir Oltean if (dsa_port_is_user((_dp))) 51982b31898SVladimir Oltean 52082b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \ 52182b31898SVladimir Oltean list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 52282b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 52382b31898SVladimir Oltean 52482b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \ 52582b31898SVladimir Oltean list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \ 52682b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 52782b31898SVladimir Oltean 52882b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \ 52982b31898SVladimir Oltean list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \ 53082b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 53182b31898SVladimir Oltean 53282b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \ 53382b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 53482b31898SVladimir Oltean if (!dsa_port_is_unused((_dp))) 53582b31898SVladimir Oltean 53682b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \ 53782b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 53882b31898SVladimir Oltean if (dsa_port_is_user((_dp))) 53982b31898SVladimir Oltean 54082b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \ 54182b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 54282b31898SVladimir Oltean if (dsa_port_is_cpu((_dp))) 54382b31898SVladimir Oltean 54402bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 54502bc6e54SVivien Didelot { 546d0004a02SVladimir Oltean struct dsa_port *dp; 547c38c5a66SVivien Didelot u32 mask = 0; 54802bc6e54SVivien Didelot 549d0004a02SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) 550d0004a02SVladimir Oltean mask |= BIT(dp->index); 551c38c5a66SVivien Didelot 552c38c5a66SVivien Didelot return mask; 553c8652c83SVivien Didelot } 554c8652c83SVivien Didelot 555c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 556c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 557c5f51765SVivien Didelot { 558c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 559c5f51765SVivien Didelot struct dsa_link *dl; 560c5f51765SVivien Didelot 561c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 562c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 563c5f51765SVivien Didelot return dl->dp->index; 564c5f51765SVivien Didelot 565c5f51765SVivien Didelot return ds->num_ports; 566c5f51765SVivien Didelot } 567c5f51765SVivien Didelot 5683b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 5693b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 5703b8fac5dSVivien Didelot int port) 5713b8fac5dSVivien Didelot { 5723b8fac5dSVivien Didelot if (device == ds->index) 5733b8fac5dSVivien Didelot return port; 5743b8fac5dSVivien Didelot else 575c5f51765SVivien Didelot return dsa_routing_port(ds, device); 5763b8fac5dSVivien Didelot } 5773b8fac5dSVivien Didelot 5783b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 57907073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 580c8f0b869SBen Hutchings { 58107073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 58207073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 58307073c79SVivien Didelot 58407073c79SVivien Didelot if (!cpu_dp) 58507073c79SVivien Didelot return port; 586c8f0b869SBen Hutchings 5873b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 588c8f0b869SBen Hutchings } 589c8f0b869SBen Hutchings 59063609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */ 59163609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port) 59263609c8fSVladimir Oltean { 59363609c8fSVladimir Oltean if (dsa_is_unused_port(ds, port)) 59463609c8fSVladimir Oltean return false; 59563609c8fSVladimir Oltean 59663609c8fSVladimir Oltean return port == dsa_upstream_port(ds, port); 59763609c8fSVladimir Oltean } 59863609c8fSVladimir Oltean 599d352b20fSTobias Waldekranz /* Return true if this is a DSA port leading away from the CPU */ 600d352b20fSTobias Waldekranz static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port) 601d352b20fSTobias Waldekranz { 602d352b20fSTobias Waldekranz return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port); 603d352b20fSTobias Waldekranz } 604d352b20fSTobias Waldekranz 6057af4a361STobias Waldekranz /* Return the local port used to reach the CPU port */ 6067af4a361STobias Waldekranz static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds) 6077af4a361STobias Waldekranz { 6087af4a361STobias Waldekranz struct dsa_port *dp; 6097af4a361STobias Waldekranz 6107af4a361STobias Waldekranz dsa_switch_for_each_available_port(dp, ds) { 6117af4a361STobias Waldekranz return dsa_upstream_port(ds, dp->index); 6127af4a361STobias Waldekranz } 6137af4a361STobias Waldekranz 6147af4a361STobias Waldekranz return ds->num_ports; 6157af4a361STobias Waldekranz } 6167af4a361STobias Waldekranz 61763609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning 61863609c8fSVladimir Oltean * that the routing port from @downstream_ds to @upstream_ds is also the port 61963609c8fSVladimir Oltean * which @downstream_ds uses to reach its dedicated CPU. 62063609c8fSVladimir Oltean */ 62163609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds, 62263609c8fSVladimir Oltean struct dsa_switch *downstream_ds) 62363609c8fSVladimir Oltean { 62463609c8fSVladimir Oltean int routing_port; 62563609c8fSVladimir Oltean 62663609c8fSVladimir Oltean if (upstream_ds == downstream_ds) 62763609c8fSVladimir Oltean return true; 62863609c8fSVladimir Oltean 62963609c8fSVladimir Oltean routing_port = dsa_routing_port(downstream_ds, upstream_ds->index); 63063609c8fSVladimir Oltean 63163609c8fSVladimir Oltean return dsa_is_upstream_port(downstream_ds, routing_port); 63263609c8fSVladimir Oltean } 63363609c8fSVladimir Oltean 634cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 635cf2d45f5SVladimir Oltean { 636cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 637cf2d45f5SVladimir Oltean 638cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 639cf2d45f5SVladimir Oltean return ds->vlan_filtering; 640cf2d45f5SVladimir Oltean else 641cf2d45f5SVladimir Oltean return dp->vlan_filtering; 642cf2d45f5SVladimir Oltean } 643cf2d45f5SVladimir Oltean 644cc76ce9eSTobias Waldekranz static inline 645cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) 646cc76ce9eSTobias Waldekranz { 647d3eed0e5SVladimir Oltean if (!dp->bridge) 648cc76ce9eSTobias Waldekranz return NULL; 649cc76ce9eSTobias Waldekranz 650cc76ce9eSTobias Waldekranz if (dp->lag_dev) 651cc76ce9eSTobias Waldekranz return dp->lag_dev; 652cc76ce9eSTobias Waldekranz else if (dp->hsr_dev) 653cc76ce9eSTobias Waldekranz return dp->hsr_dev; 654cc76ce9eSTobias Waldekranz 655cc76ce9eSTobias Waldekranz return dp->slave; 656cc76ce9eSTobias Waldekranz } 657cc76ce9eSTobias Waldekranz 65836cbf39bSVladimir Oltean static inline struct net_device * 65936cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp) 66036cbf39bSVladimir Oltean { 661d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->dev : NULL; 66236cbf39bSVladimir Oltean } 66336cbf39bSVladimir Oltean 66436cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp) 66536cbf39bSVladimir Oltean { 666d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->num : 0; 66736cbf39bSVladimir Oltean } 66836cbf39bSVladimir Oltean 66936cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a, 67036cbf39bSVladimir Oltean const struct dsa_port *b) 67136cbf39bSVladimir Oltean { 67236cbf39bSVladimir Oltean struct net_device *br_a = dsa_port_bridge_dev_get(a); 67336cbf39bSVladimir Oltean struct net_device *br_b = dsa_port_bridge_dev_get(b); 67436cbf39bSVladimir Oltean 67536cbf39bSVladimir Oltean /* Standalone ports are not in the same bridge with one another */ 67636cbf39bSVladimir Oltean return (!br_a || !br_b) ? false : (br_a == br_b); 67736cbf39bSVladimir Oltean } 67836cbf39bSVladimir Oltean 6796a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp, 6806a43cba3SVladimir Oltean const struct net_device *dev) 6816a43cba3SVladimir Oltean { 6826a43cba3SVladimir Oltean return dsa_port_to_bridge_port(dp) == dev; 6836a43cba3SVladimir Oltean } 6846a43cba3SVladimir Oltean 6856a43cba3SVladimir Oltean static inline bool 6866a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp, 6876a43cba3SVladimir Oltean const struct net_device *bridge_dev) 6886a43cba3SVladimir Oltean { 6896a43cba3SVladimir Oltean /* DSA ports connected to a bridge, and event was emitted 6906a43cba3SVladimir Oltean * for the bridge. 6916a43cba3SVladimir Oltean */ 6926a43cba3SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge_dev; 6936a43cba3SVladimir Oltean } 6946a43cba3SVladimir Oltean 695d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp, 696d3eed0e5SVladimir Oltean const struct dsa_bridge *bridge) 697d3eed0e5SVladimir Oltean { 698d3eed0e5SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge->dev; 699d3eed0e5SVladimir Oltean } 700d3eed0e5SVladimir Oltean 7016a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */ 7026a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst, 7036a43cba3SVladimir Oltean const struct net_device *dev) 7046a43cba3SVladimir Oltean { 7056a43cba3SVladimir Oltean struct dsa_port *dp; 7066a43cba3SVladimir Oltean 7076a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7086a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_port(dp, dev)) 7096a43cba3SVladimir Oltean return true; 7106a43cba3SVladimir Oltean 7116a43cba3SVladimir Oltean return false; 7126a43cba3SVladimir Oltean } 7136a43cba3SVladimir Oltean 7146a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */ 7156a43cba3SVladimir Oltean static inline bool 7166a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst, 7176a43cba3SVladimir Oltean const struct net_device *bridge_dev) 7186a43cba3SVladimir Oltean { 7196a43cba3SVladimir Oltean struct dsa_port *dp; 7206a43cba3SVladimir Oltean 7216a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7226a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_dev(dp, bridge_dev)) 7236a43cba3SVladimir Oltean return true; 7246a43cba3SVladimir Oltean 7256a43cba3SVladimir Oltean return false; 7266a43cba3SVladimir Oltean } 7276a43cba3SVladimir Oltean 7282bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 7292bedde1aSArkadi Sharshevsky bool is_static, void *data); 7309d490b4eSVivien Didelot struct dsa_switch_ops { 73153da0ebaSVladimir Oltean /* 73253da0ebaSVladimir Oltean * Tagging protocol helpers called for the CPU ports and DSA links. 73353da0ebaSVladimir Oltean * @get_tag_protocol retrieves the initial tagging protocol and is 73453da0ebaSVladimir Oltean * mandatory. Switches which can operate using multiple tagging 73553da0ebaSVladimir Oltean * protocols should implement @change_tag_protocol and report in 73653da0ebaSVladimir Oltean * @get_tag_protocol the tagger in current use. 73753da0ebaSVladimir Oltean */ 7385ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 7394d776482SFlorian Fainelli int port, 7404d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 74153da0ebaSVladimir Oltean int (*change_tag_protocol)(struct dsa_switch *ds, int port, 74253da0ebaSVladimir Oltean enum dsa_tag_protocol proto); 743dc452a47SVladimir Oltean /* 744dc452a47SVladimir Oltean * Method for switch drivers to connect to the tagging protocol driver 745dc452a47SVladimir Oltean * in current use. The switch driver can provide handlers for certain 746dc452a47SVladimir Oltean * types of packets for switch management. 747dc452a47SVladimir Oltean */ 748dc452a47SVladimir Oltean int (*connect_tag_protocol)(struct dsa_switch *ds, 749dc452a47SVladimir Oltean enum dsa_tag_protocol proto); 7507b314362SAndrew Lunn 751fd292c18SVladimir Oltean /* Optional switch-wide initialization and destruction methods */ 752c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 7535e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 754fd292c18SVladimir Oltean 755fd292c18SVladimir Oltean /* Per-port initialization and destruction methods. Mandatory if the 756fd292c18SVladimir Oltean * driver registers devlink port regions, optional otherwise. 757fd292c18SVladimir Oltean */ 758fd292c18SVladimir Oltean int (*port_setup)(struct dsa_switch *ds, int port); 759fd292c18SVladimir Oltean void (*port_teardown)(struct dsa_switch *ds, int port); 760fd292c18SVladimir Oltean 7616819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 762c8f0b869SBen Hutchings 763c8f0b869SBen Hutchings /* 764c8f0b869SBen Hutchings * Access to the switch's PHY registers. 765c8f0b869SBen Hutchings */ 766c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 767c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 768c8f0b869SBen Hutchings int regnum, u16 val); 769c8f0b869SBen Hutchings 770c8f0b869SBen Hutchings /* 771ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 772ec9436baSFlorian Fainelli */ 773ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 774ec9436baSFlorian Fainelli struct phy_device *phydev); 775ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 776ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 777ec9436baSFlorian Fainelli 778ec9436baSFlorian Fainelli /* 77911d8f3ddSFlorian Fainelli * PHYLINK integration 78011d8f3ddSFlorian Fainelli */ 781072eea6cSRussell King (Oracle) void (*phylink_get_caps)(struct dsa_switch *ds, int port, 782072eea6cSRussell King (Oracle) struct phylink_config *config); 78311d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 78411d8f3ddSFlorian Fainelli unsigned long *supported, 78511d8f3ddSFlorian Fainelli struct phylink_link_state *state); 786bde01822SRussell King (Oracle) struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds, 787bde01822SRussell King (Oracle) int port, 788bde01822SRussell King (Oracle) phy_interface_t iface); 78911d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 79011d8f3ddSFlorian Fainelli struct phylink_link_state *state); 79111d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 79211d8f3ddSFlorian Fainelli unsigned int mode, 79311d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 79411d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 79511d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 79611d8f3ddSFlorian Fainelli unsigned int mode, 79711d8f3ddSFlorian Fainelli phy_interface_t interface); 79811d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 79911d8f3ddSFlorian Fainelli unsigned int mode, 80011d8f3ddSFlorian Fainelli phy_interface_t interface, 8015b502a7bSRussell King struct phy_device *phydev, 8025b502a7bSRussell King int speed, int duplex, 8035b502a7bSRussell King bool tx_pause, bool rx_pause); 80411d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 80511d8f3ddSFlorian Fainelli struct phylink_link_state *state); 80611d8f3ddSFlorian Fainelli /* 807c2ec5f2eSOleksij Rempel * Port statistics counters. 808c8f0b869SBen Hutchings */ 80989f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 81089f09048SFlorian Fainelli u32 stringset, uint8_t *data); 811c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 812c8f0b869SBen Hutchings int port, uint64_t *data); 81389f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 814cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 815cf963573SFlorian Fainelli int port, uint64_t *data); 816487d3855SAlvin Šipraga void (*get_eth_phy_stats)(struct dsa_switch *ds, int port, 817487d3855SAlvin Šipraga struct ethtool_eth_phy_stats *phy_stats); 818487d3855SAlvin Šipraga void (*get_eth_mac_stats)(struct dsa_switch *ds, int port, 819487d3855SAlvin Šipraga struct ethtool_eth_mac_stats *mac_stats); 820487d3855SAlvin Šipraga void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port, 821487d3855SAlvin Šipraga struct ethtool_eth_ctrl_stats *ctrl_stats); 822c2ec5f2eSOleksij Rempel void (*get_stats64)(struct dsa_switch *ds, int port, 823c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s); 824a71acad9SOleksij Rempel void (*self_test)(struct dsa_switch *ds, int port, 825a71acad9SOleksij Rempel struct ethtool_test *etest, u64 *data); 82624462549SFlorian Fainelli 82724462549SFlorian Fainelli /* 82819e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 82919e57c4eSFlorian Fainelli */ 83019e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 83119e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 83219e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 83319e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 83419e57c4eSFlorian Fainelli 83519e57c4eSFlorian Fainelli /* 8360336369dSBrandon Streiff * ethtool timestamp info 8370336369dSBrandon Streiff */ 8380336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 8390336369dSBrandon Streiff struct ethtool_ts_info *ts); 8400336369dSBrandon Streiff 8410336369dSBrandon Streiff /* 84224462549SFlorian Fainelli * Suspend and resume 84324462549SFlorian Fainelli */ 84424462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 84524462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 846b2f2af21SFlorian Fainelli 847b2f2af21SFlorian Fainelli /* 848b2f2af21SFlorian Fainelli * Port enable/disable 849b2f2af21SFlorian Fainelli */ 850b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 851b2f2af21SFlorian Fainelli struct phy_device *phy); 85275104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 8537905288fSFlorian Fainelli 8547905288fSFlorian Fainelli /* 85508f50061SVivien Didelot * Port's MAC EEE settings 8567905288fSFlorian Fainelli */ 85708f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 8587905288fSFlorian Fainelli struct ethtool_eee *e); 85908f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 8607905288fSFlorian Fainelli struct ethtool_eee *e); 86151579c3fSGuenter Roeck 8626793abb4SGuenter Roeck /* EEPROM access */ 8636793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 8646793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 8656793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 8666793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 8676793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 8683d762a0fSGuenter Roeck 8693d762a0fSGuenter Roeck /* 8703d762a0fSGuenter Roeck * Register access. 8713d762a0fSGuenter Roeck */ 8723d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 8733d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 8743d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 875b73adef6SFlorian Fainelli 876b73adef6SFlorian Fainelli /* 877e358bef7SVladimir Oltean * Upper device tracking. 878e358bef7SVladimir Oltean */ 879e358bef7SVladimir Oltean int (*port_prechangeupper)(struct dsa_switch *ds, int port, 880e358bef7SVladimir Oltean struct netdev_notifier_changeupper_info *info); 881e358bef7SVladimir Oltean 882e358bef7SVladimir Oltean /* 883b73adef6SFlorian Fainelli * Bridge integration 884b73adef6SFlorian Fainelli */ 88534a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 88671327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 887b079922bSVladimir Oltean struct dsa_bridge bridge, 888b079922bSVladimir Oltean bool *tx_fwd_offload); 889f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 890d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 89143c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 892b73adef6SFlorian Fainelli u8 state); 893732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 894a8b659e7SVladimir Oltean int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 895a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 896a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 897a8b659e7SVladimir Oltean int (*port_bridge_flags)(struct dsa_switch *ds, int port, 898a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 899a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 9002a778e1bSVivien Didelot 9012a778e1bSVivien Didelot /* 90211149536SVivien Didelot * VLAN support 90311149536SVivien Didelot */ 904fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 90589153ed6SVladimir Oltean bool vlan_filtering, 90689153ed6SVladimir Oltean struct netlink_ext_ack *extack); 9071958d581SVladimir Oltean int (*port_vlan_add)(struct dsa_switch *ds, int port, 90831046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 90931046a5fSVladimir Oltean struct netlink_ext_ack *extack); 91076e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 91176e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 91211149536SVivien Didelot /* 9132a778e1bSVivien Didelot * Forwarding database 9142a778e1bSVivien Didelot */ 9151b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 9166c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 9172a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 9186c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 919ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 9202bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 9218df30255SVivien Didelot 9228df30255SVivien Didelot /* 9238df30255SVivien Didelot * Multicast database 9248df30255SVivien Didelot */ 925a52b2da7SVladimir Oltean int (*port_mdb_add)(struct dsa_switch *ds, int port, 9263709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 9278df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 9288df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb); 929bf9f2648SFlorian Fainelli /* 930bf9f2648SFlorian Fainelli * RXNFC 931bf9f2648SFlorian Fainelli */ 932bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 933bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 934bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 935bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 936f50f2127SFlorian Fainelli 937f50f2127SFlorian Fainelli /* 938f50f2127SFlorian Fainelli * TC integration 939f50f2127SFlorian Fainelli */ 940ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 941ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 942ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 943ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 944ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 945ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 946f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 947f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 948f50f2127SFlorian Fainelli bool ingress); 949f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 950f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 95134297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 95234297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 95334297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 95447d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 95547d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 95640ef2c93SVivien Didelot 95740ef2c93SVivien Didelot /* 95840ef2c93SVivien Didelot * Cross-chip operations 95940ef2c93SVivien Didelot */ 960f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 961f66a6a69SVladimir Oltean int sw_index, int port, 962d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 963f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 964f66a6a69SVladimir Oltean int sw_index, int port, 965d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 966058102a6STobias Waldekranz int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 967058102a6STobias Waldekranz int port); 968058102a6STobias Waldekranz int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 969*46a76724SVladimir Oltean int port, struct net_device *lag_dev, 970058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 971058102a6STobias Waldekranz int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 972*46a76724SVladimir Oltean int port, struct net_device *lag_dev); 9730336369dSBrandon Streiff 9740336369dSBrandon Streiff /* 9750336369dSBrandon Streiff * PTP functionality 9760336369dSBrandon Streiff */ 9770336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 9780336369dSBrandon Streiff struct ifreq *ifr); 9790336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 9800336369dSBrandon Streiff struct ifreq *ifr); 9815c5416f5SYangbo Lu void (*port_txtstamp)(struct dsa_switch *ds, int port, 9825c5416f5SYangbo Lu struct sk_buff *skb); 98390af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 98490af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 98597a69a0dSVladimir Oltean 9860f06b855SAndrew Lunn /* Devlink parameters, etc */ 9876b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 9886b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 9896b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 9906b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 9910f06b855SAndrew Lunn int (*devlink_info_get)(struct dsa_switch *ds, 9920f06b855SAndrew Lunn struct devlink_info_req *req, 9930f06b855SAndrew Lunn struct netlink_ext_ack *extack); 9942a6ef763SVladimir Oltean int (*devlink_sb_pool_get)(struct dsa_switch *ds, 9952a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 9962a6ef763SVladimir Oltean struct devlink_sb_pool_info *pool_info); 9972a6ef763SVladimir Oltean int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 9982a6ef763SVladimir Oltean u16 pool_index, u32 size, 9992a6ef763SVladimir Oltean enum devlink_sb_threshold_type threshold_type, 10002a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10012a6ef763SVladimir Oltean int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 10022a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10032a6ef763SVladimir Oltean u32 *p_threshold); 10042a6ef763SVladimir Oltean int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 10052a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10062a6ef763SVladimir Oltean u32 threshold, 10072a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10082a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 10092a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10102a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10112a6ef763SVladimir Oltean u16 *p_pool_index, u32 *p_threshold); 10122a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 10132a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10142a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10152a6ef763SVladimir Oltean u16 pool_index, u32 threshold, 10162a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10172a6ef763SVladimir Oltean int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 10182a6ef763SVladimir Oltean unsigned int sb_index); 10192a6ef763SVladimir Oltean int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 10202a6ef763SVladimir Oltean unsigned int sb_index); 10212a6ef763SVladimir Oltean int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 10222a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10232a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 10242a6ef763SVladimir Oltean int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 10252a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10262a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10272a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 1028bfcb8132SVladimir Oltean 1029bfcb8132SVladimir Oltean /* 1030bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 1031bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 1032bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 1033ab88d64aSRandy Dunlap * method needs to do so privately. 1034bfcb8132SVladimir Oltean */ 1035bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 1036bfcb8132SVladimir Oltean int new_mtu); 1037bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 1038058102a6STobias Waldekranz 1039058102a6STobias Waldekranz /* 1040058102a6STobias Waldekranz * LAG integration 1041058102a6STobias Waldekranz */ 1042058102a6STobias Waldekranz int (*port_lag_change)(struct dsa_switch *ds, int port); 1043058102a6STobias Waldekranz int (*port_lag_join)(struct dsa_switch *ds, int port, 1044*46a76724SVladimir Oltean struct net_device *lag_dev, 1045058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 1046058102a6STobias Waldekranz int (*port_lag_leave)(struct dsa_switch *ds, int port, 1047*46a76724SVladimir Oltean struct net_device *lag_dev); 104818596f50SGeorge McCollister 104918596f50SGeorge McCollister /* 105018596f50SGeorge McCollister * HSR integration 105118596f50SGeorge McCollister */ 105218596f50SGeorge McCollister int (*port_hsr_join)(struct dsa_switch *ds, int port, 105318596f50SGeorge McCollister struct net_device *hsr); 105418596f50SGeorge McCollister int (*port_hsr_leave)(struct dsa_switch *ds, int port, 105518596f50SGeorge McCollister struct net_device *hsr); 1056c595c433SHoratiu Vultur 1057c595c433SHoratiu Vultur /* 1058c595c433SHoratiu Vultur * MRP integration 1059c595c433SHoratiu Vultur */ 1060c595c433SHoratiu Vultur int (*port_mrp_add)(struct dsa_switch *ds, int port, 1061c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1062c595c433SHoratiu Vultur int (*port_mrp_del)(struct dsa_switch *ds, int port, 1063c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1064c595c433SHoratiu Vultur int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 1065c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 1066c595c433SHoratiu Vultur int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 1067c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 10685da11eb4SVladimir Oltean 10695da11eb4SVladimir Oltean /* 10705da11eb4SVladimir Oltean * tag_8021q operations 10715da11eb4SVladimir Oltean */ 10725da11eb4SVladimir Oltean int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 10735da11eb4SVladimir Oltean u16 flags); 10745da11eb4SVladimir Oltean int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 1075295ab96fSVladimir Oltean 1076295ab96fSVladimir Oltean /* 1077295ab96fSVladimir Oltean * DSA master tracking operations 1078295ab96fSVladimir Oltean */ 1079295ab96fSVladimir Oltean void (*master_state_change)(struct dsa_switch *ds, 1080295ab96fSVladimir Oltean const struct net_device *master, 1081295ab96fSVladimir Oltean bool operational); 10826b297524SAndrew Lunn }; 10836b297524SAndrew Lunn 10846b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 10856b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 10866b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 10876b297524SAndrew Lunn 10886b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 10896b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10906b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 10916b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10926b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 10936b297524SAndrew Lunn const struct devlink_param *params, 10946b297524SAndrew Lunn size_t params_count); 10956b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 10966b297524SAndrew Lunn const struct devlink_param *params, 10976b297524SAndrew Lunn size_t params_count); 10985cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 10995cd73fbdSAndrew Lunn const char *resource_name, 11005cd73fbdSAndrew Lunn u64 resource_size, 11015cd73fbdSAndrew Lunn u64 resource_id, 11025cd73fbdSAndrew Lunn u64 parent_resource_id, 11035cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 11045cd73fbdSAndrew Lunn 11055cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 11065cd73fbdSAndrew Lunn 11075cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 11085cd73fbdSAndrew Lunn u64 resource_id, 11095cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 11105cd73fbdSAndrew Lunn void *occ_get_priv); 11115cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 11125cd73fbdSAndrew Lunn u64 resource_id); 111397c82c23SAndrew Lunn struct devlink_region * 111497c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds, 111597c82c23SAndrew Lunn const struct devlink_region_ops *ops, 111697c82c23SAndrew Lunn u32 region_max_snapshots, u64 region_size); 111708156ba4SAndrew Lunn struct devlink_region * 111808156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds, 111908156ba4SAndrew Lunn int port, 112008156ba4SAndrew Lunn const struct devlink_port_region_ops *ops, 112108156ba4SAndrew Lunn u32 region_max_snapshots, u64 region_size); 112297c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region); 112397c82c23SAndrew Lunn 1124e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 11255cd73fbdSAndrew Lunn 11266b297524SAndrew Lunn struct dsa_devlink_priv { 11276b297524SAndrew Lunn struct dsa_switch *ds; 1128c8f0b869SBen Hutchings }; 1129c8f0b869SBen Hutchings 1130ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 1131ccc3e6b0SAndrew Lunn { 1132ccc3e6b0SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1133ccc3e6b0SAndrew Lunn 1134ccc3e6b0SAndrew Lunn return dl_priv->ds; 1135ccc3e6b0SAndrew Lunn } 1136ccc3e6b0SAndrew Lunn 11377d1e2a10SAndrew Lunn static inline 11387d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 11397d1e2a10SAndrew Lunn { 11407d1e2a10SAndrew Lunn struct devlink *dl = port->devlink; 11417d1e2a10SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 11427d1e2a10SAndrew Lunn 11437d1e2a10SAndrew Lunn return dl_priv->ds; 11447d1e2a10SAndrew Lunn } 11457d1e2a10SAndrew Lunn 11467d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port) 11477d1e2a10SAndrew Lunn { 11487d1e2a10SAndrew Lunn return port->index; 11497d1e2a10SAndrew Lunn } 11507d1e2a10SAndrew Lunn 1151ab3d408dSFlorian Fainelli struct dsa_switch_driver { 1152ab3d408dSFlorian Fainelli struct list_head list; 1153a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 1154ab3d408dSFlorian Fainelli }; 1155ab3d408dSFlorian Fainelli 115614b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 1157c8f0b869SBen Hutchings 115873a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 11599eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 1160c6e970a0SAndrew Lunn { 1161c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 1162717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 1163c6e970a0SAndrew Lunn #endif 1164c6e970a0SAndrew Lunn return false; 1165c6e970a0SAndrew Lunn } 1166c6e970a0SAndrew Lunn 11679790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail 11689790cf20SVladimir Oltean * tags, which don't break dissection) can be treated the same from the 11699790cf20SVladimir Oltean * perspective of the flow dissector. 11709790cf20SVladimir Oltean * 11719790cf20SVladimir Oltean * We need to return: 11729790cf20SVladimir Oltean * - offset: the (B - A) difference between: 11739790cf20SVladimir Oltean * A. the position of the real EtherType and 11749790cf20SVladimir Oltean * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 11759790cf20SVladimir Oltean * after the normal EtherType was supposed to be) 11769790cf20SVladimir Oltean * The offset in bytes is exactly equal to the tagger overhead (and half of 11779790cf20SVladimir Oltean * that, in __be16 shorts). 11789790cf20SVladimir Oltean * 11799790cf20SVladimir Oltean * - proto: the value of the real EtherType. 11809790cf20SVladimir Oltean */ 11819790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 11829790cf20SVladimir Oltean __be16 *proto, int *offset) 11839790cf20SVladimir Oltean { 11849790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 11859790cf20SVladimir Oltean const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 11864e500251SVladimir Oltean int tag_len = ops->needed_headroom; 11879790cf20SVladimir Oltean 11889790cf20SVladimir Oltean *offset = tag_len; 11899790cf20SVladimir Oltean *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 11909790cf20SVladimir Oltean #endif 11919790cf20SVladimir Oltean } 11929790cf20SVladimir Oltean 11934cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 11944cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev) 11954cfab356SFlorian Fainelli { 11964cfab356SFlorian Fainelli int err = -EOPNOTSUPP; 11974cfab356SFlorian Fainelli 11984cfab356SFlorian Fainelli if (!dev->dsa_ptr) 11994cfab356SFlorian Fainelli return err; 12004cfab356SFlorian Fainelli 12014cfab356SFlorian Fainelli if (!dev->dsa_ptr->netdev_ops) 12024cfab356SFlorian Fainelli return err; 12034cfab356SFlorian Fainelli 12044cfab356SFlorian Fainelli return 0; 12054cfab356SFlorian Fainelli } 12064cfab356SFlorian Fainelli 1207a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 12084cfab356SFlorian Fainelli int cmd) 12094cfab356SFlorian Fainelli { 12104cfab356SFlorian Fainelli const struct dsa_netdevice_ops *ops; 12114cfab356SFlorian Fainelli int err; 12124cfab356SFlorian Fainelli 12134cfab356SFlorian Fainelli err = __dsa_netdevice_ops_check(dev); 12144cfab356SFlorian Fainelli if (err) 12154cfab356SFlorian Fainelli return err; 12164cfab356SFlorian Fainelli 12174cfab356SFlorian Fainelli ops = dev->dsa_ptr->netdev_ops; 12184cfab356SFlorian Fainelli 1219a7605370SArnd Bergmann return ops->ndo_eth_ioctl(dev, ifr, cmd); 12204cfab356SFlorian Fainelli } 12214cfab356SFlorian Fainelli #else 1222a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 12234cfab356SFlorian Fainelli int cmd) 12244cfab356SFlorian Fainelli { 12254cfab356SFlorian Fainelli return -EOPNOTSUPP; 12264cfab356SFlorian Fainelli } 12274cfab356SFlorian Fainelli #endif 12284cfab356SFlorian Fainelli 122983c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 123023c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 12310650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds); 12323b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1233a2614140SVladimir Oltean void dsa_flush_workqueue(void); 1234ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 1235ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 1236ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 1237ea825e70SFlorian Fainelli #else 1238ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 1239ea825e70SFlorian Fainelli { 1240ea825e70SFlorian Fainelli return 0; 1241ea825e70SFlorian Fainelli } 1242ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 1243ea825e70SFlorian Fainelli { 1244ea825e70SFlorian Fainelli return 0; 1245ea825e70SFlorian Fainelli } 1246ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 1247ea825e70SFlorian Fainelli 124860724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 1249a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev); 125060724d4bSFlorian Fainelli #else 1251a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev) 1252a5e3c9baSVladimir Oltean { 1253a5e3c9baSVladimir Oltean return false; 1254a5e3c9baSVladimir Oltean } 125560724d4bSFlorian Fainelli #endif 125660724d4bSFlorian Fainelli 125797a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 125811d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1259cf963573SFlorian Fainelli 1260d3b8c049SAndrew Lunn struct dsa_tag_driver { 1261d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 1262d3b8c049SAndrew Lunn struct list_head list; 1263d3b8c049SAndrew Lunn struct module *owner; 1264d3b8c049SAndrew Lunn }; 1265d3b8c049SAndrew Lunn 1266d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1267d3b8c049SAndrew Lunn unsigned int count, 1268d3b8c049SAndrew Lunn struct module *owner); 1269d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1270d3b8c049SAndrew Lunn unsigned int count); 1271d3b8c049SAndrew Lunn 1272d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1273d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 1274d3b8c049SAndrew Lunn { \ 1275d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1276d3b8c049SAndrew Lunn THIS_MODULE); \ 1277d3b8c049SAndrew Lunn return 0; \ 1278d3b8c049SAndrew Lunn } \ 1279d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 1280d3b8c049SAndrew Lunn \ 1281d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 1282d3b8c049SAndrew Lunn { \ 1283d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1284d3b8c049SAndrew Lunn } \ 1285d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 1286d3b8c049SAndrew Lunn 1287d3b8c049SAndrew Lunn /** 1288d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1289d3b8c049SAndrew Lunn * drivers 1290c7d9a675SLuiz Angelo Daros de Luca * @__ops_array: Array of tag driver structures 1291d3b8c049SAndrew Lunn * 1292d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1293d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1294d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1295d3b8c049SAndrew Lunn */ 1296d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 1297d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1298d3b8c049SAndrew Lunn 1299d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1300d3b8c049SAndrew Lunn 1301d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 1302d3b8c049SAndrew Lunn * drivers 1303d3b8c049SAndrew Lunn */ 1304d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 1305d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1306d3b8c049SAndrew Lunn .ops = &__ops, \ 1307d3b8c049SAndrew Lunn } 1308d3b8c049SAndrew Lunn 1309d3b8c049SAndrew Lunn /** 1310d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1311d3b8c049SAndrew Lunn * driver 1312d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 1313d3b8c049SAndrew Lunn * 1314d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1315d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1316d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1317d3b8c049SAndrew Lunn */ 1318d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 1319d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 1320d3b8c049SAndrew Lunn \ 1321d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1322d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 1323d3b8c049SAndrew Lunn }; \ 1324d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 132591da11f8SLennert Buytenhek #endif 1326d3b8c049SAndrew Lunn 1327