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 490b42f033SAndrew Lunn 50ac7a04c3SFlorian Fainelli enum dsa_tag_protocol { 510b42f033SAndrew Lunn DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE, 520b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE, 530b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE, 540b42f033SAndrew Lunn DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE, 550b42f033SAndrew Lunn DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE, 560b42f033SAndrew Lunn DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE, 570b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE, 580b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE, 590b42f033SAndrew Lunn DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE, 600b42f033SAndrew Lunn DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE, 610b42f033SAndrew Lunn DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE, 620b42f033SAndrew Lunn DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE, 63f9bbe447SVladimir Oltean DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE, 64227d07a0SVladimir Oltean DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE, 65016e43a2STristram Ha DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE, 668dce89aaSVladimir Oltean DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE, 6748fda74fSOleksij Rempel DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE, 68efd7fe68SLinus Walleij DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE, 6901ef09caSKurt Kanzenbach DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE, 70ac7a04c3SFlorian Fainelli }; 715037d532SFlorian Fainelli 725075314eSAlexander Duyck struct packet_type; 7390af1059SBrandon Streiff struct dsa_switch; 743e8a72d1SFlorian Fainelli 7568277a2cSJohn Crispin struct dsa_device_ops { 7668277a2cSJohn Crispin struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev); 7768277a2cSJohn Crispin struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev, 7889e49506SFlorian Westphal struct packet_type *pt); 792e8cb1b3SVladimir Oltean void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto, 80598a9680SJohn Crispin int *offset); 81cc1939e4SVladimir Oltean /* Used to determine which traffic should match the DSA filter in 82cc1939e4SVladimir Oltean * eth_type_trans, and which, if any, should bypass it and be processed 83cc1939e4SVladimir Oltean * as regular on the master net device. 84cc1939e4SVladimir Oltean */ 85cc1939e4SVladimir Oltean bool (*filter)(const struct sk_buff *skb, struct net_device *dev); 86a5dd3087SAndrew Lunn unsigned int overhead; 87875138f8SAndrew Lunn const char *name; 88056eed2fSAndrew Lunn enum dsa_tag_protocol proto; 89c3975400SVladimir Oltean /* Some tagging protocols either mangle or shift the destination MAC 90c3975400SVladimir Oltean * address, in which case the DSA master would drop packets on ingress 91c3975400SVladimir Oltean * if what it understands out of the destination MAC address is not in 92c3975400SVladimir Oltean * its RX filter. 93c3975400SVladimir Oltean */ 94c3975400SVladimir Oltean bool promisc_on_master; 957a6ffe76SVladimir Oltean bool tail_tag; 9668277a2cSJohn Crispin }; 9768277a2cSJohn Crispin 984cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the 994cfab356SFlorian Fainelli * DSA layer on top of the DSA CPU/management net_device instance. This is 1004cfab356SFlorian Fainelli * used by the core net_device layer while calling various net_device_ops 1014cfab356SFlorian Fainelli * function pointers. 1024cfab356SFlorian Fainelli */ 1034cfab356SFlorian Fainelli struct dsa_netdevice_ops { 1044cfab356SFlorian Fainelli int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr, 1054cfab356SFlorian Fainelli int cmd); 1064cfab356SFlorian Fainelli }; 1074cfab356SFlorian Fainelli 1080b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-" 1090b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \ 1100b42f033SAndrew Lunn MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE)) 1110b42f033SAndrew Lunn 112b68b0dd0SVladimir Oltean struct dsa_skb_cb { 113b68b0dd0SVladimir Oltean struct sk_buff *clone; 114b68b0dd0SVladimir Oltean }; 115b68b0dd0SVladimir Oltean 116b68b0dd0SVladimir Oltean struct __dsa_skb_cb { 117b68b0dd0SVladimir Oltean struct dsa_skb_cb cb; 118b68b0dd0SVladimir Oltean u8 priv[48 - sizeof(struct dsa_skb_cb)]; 119b68b0dd0SVladimir Oltean }; 120b68b0dd0SVladimir Oltean 121b68b0dd0SVladimir Oltean #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb)) 122b68b0dd0SVladimir Oltean 123b68b0dd0SVladimir Oltean #define DSA_SKB_CB_PRIV(skb) \ 124b68b0dd0SVladimir Oltean ((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv)) 125b68b0dd0SVladimir Oltean 126cf50dcc2SBen Hutchings struct dsa_switch_tree { 12783c0afaeSAndrew Lunn struct list_head list; 12883c0afaeSAndrew Lunn 129f515f192SVivien Didelot /* Notifier chain for switch-wide events */ 130f515f192SVivien Didelot struct raw_notifier_head nh; 131f515f192SVivien Didelot 13283c0afaeSAndrew Lunn /* Tree identifier */ 13349463b7fSVivien Didelot unsigned int index; 13483c0afaeSAndrew Lunn 13583c0afaeSAndrew Lunn /* Number of switches attached to this tree */ 13683c0afaeSAndrew Lunn struct kref refcount; 13783c0afaeSAndrew Lunn 13883c0afaeSAndrew Lunn /* Has this tree been applied to the hardware? */ 139ec15dd42SVivien Didelot bool setup; 14083c0afaeSAndrew Lunn 141cf50dcc2SBen Hutchings /* 142cf50dcc2SBen Hutchings * Configuration data for the platform device that owns 143cf50dcc2SBen Hutchings * this dsa switch tree instance. 144cf50dcc2SBen Hutchings */ 145cf50dcc2SBen Hutchings struct dsa_platform_data *pd; 146cf85d08fSLennert Buytenhek 147ab8ccae1SVivien Didelot /* List of switch ports */ 148ab8ccae1SVivien Didelot struct list_head ports; 149ab8ccae1SVivien Didelot 150c5f51765SVivien Didelot /* List of DSA links composing the routing table */ 151c5f51765SVivien Didelot struct list_head rtable; 152cf50dcc2SBen Hutchings }; 153cf50dcc2SBen Hutchings 15434297176SVladimir Oltean /* TC matchall action types */ 155f50f2127SFlorian Fainelli enum dsa_port_mall_action_type { 156f50f2127SFlorian Fainelli DSA_PORT_MALL_MIRROR, 15734297176SVladimir Oltean DSA_PORT_MALL_POLICER, 158f50f2127SFlorian Fainelli }; 159f50f2127SFlorian Fainelli 160f50f2127SFlorian Fainelli /* TC mirroring entry */ 161f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry { 162f50f2127SFlorian Fainelli u8 to_local_port; 163f50f2127SFlorian Fainelli bool ingress; 164f50f2127SFlorian Fainelli }; 165f50f2127SFlorian Fainelli 16634297176SVladimir Oltean /* TC port policer entry */ 16734297176SVladimir Oltean struct dsa_mall_policer_tc_entry { 1685f035af7SPo Liu u32 burst; 16934297176SVladimir Oltean u64 rate_bytes_per_sec; 17034297176SVladimir Oltean }; 17134297176SVladimir Oltean 172f50f2127SFlorian Fainelli /* TC matchall entry */ 173f50f2127SFlorian Fainelli struct dsa_mall_tc_entry { 174f50f2127SFlorian Fainelli struct list_head list; 175f50f2127SFlorian Fainelli unsigned long cookie; 176f50f2127SFlorian Fainelli enum dsa_port_mall_action_type type; 177f50f2127SFlorian Fainelli union { 178f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry mirror; 17934297176SVladimir Oltean struct dsa_mall_policer_tc_entry policer; 180f50f2127SFlorian Fainelli }; 181f50f2127SFlorian Fainelli }; 182f50f2127SFlorian Fainelli 183f50f2127SFlorian Fainelli 184c8b09808SAndrew Lunn struct dsa_port { 185f8b8b1cdSVivien Didelot /* A CPU port is physically connected to a master device. 186f8b8b1cdSVivien Didelot * A user port exposed to userspace has a slave device. 187f8b8b1cdSVivien Didelot */ 188f8b8b1cdSVivien Didelot union { 189f8b8b1cdSVivien Didelot struct net_device *master; 190f8b8b1cdSVivien Didelot struct net_device *slave; 191f8b8b1cdSVivien Didelot }; 192f8b8b1cdSVivien Didelot 19315240248SVivien Didelot /* CPU port tagging operations used by master or slave devices */ 19415240248SVivien Didelot const struct dsa_device_ops *tag_ops; 19515240248SVivien Didelot 1963e41f93bSVivien Didelot /* Copies for faster access in master receive hot path */ 1973e41f93bSVivien Didelot struct dsa_switch_tree *dst; 1983e41f93bSVivien Didelot struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev, 1993e41f93bSVivien Didelot struct packet_type *pt); 200cc1939e4SVladimir Oltean bool (*filter)(const struct sk_buff *skb, struct net_device *dev); 2013e41f93bSVivien Didelot 202057cad2cSVivien Didelot enum { 203057cad2cSVivien Didelot DSA_PORT_TYPE_UNUSED = 0, 204057cad2cSVivien Didelot DSA_PORT_TYPE_CPU, 205057cad2cSVivien Didelot DSA_PORT_TYPE_DSA, 206057cad2cSVivien Didelot DSA_PORT_TYPE_USER, 207057cad2cSVivien Didelot } type; 208057cad2cSVivien Didelot 209818be848SVivien Didelot struct dsa_switch *ds; 210818be848SVivien Didelot unsigned int index; 21171e0bbdeSFlorian Fainelli const char *name; 21268b2d4a8SVivien Didelot struct dsa_port *cpu_dp; 213a2c7023fSXiaofei Shen const char *mac; 214189b0d93SAndrew Lunn struct device_node *dn; 21534a79f63SVivien Didelot unsigned int ageing_time; 21633162e9aSVladimir Oltean bool vlan_filtering; 217732f794cSVivien Didelot u8 stp_state; 218a5e9a02eSVivien Didelot struct net_device *bridge_dev; 21996567d5dSAndrew Lunn struct devlink_port devlink_port; 2203122433eSAndrew Lunn bool devlink_port_setup; 221aab9c406SFlorian Fainelli struct phylink *pl; 22244cc27e4SIoana Ciornei struct phylink_config pl_config; 22397a69a0dSVladimir Oltean 224ab8ccae1SVivien Didelot struct list_head list; 225ab8ccae1SVivien Didelot 22667dbb9d4SFlorian Fainelli /* 227c362beb0SVladimir Oltean * Give the switch driver somewhere to hang its per-port private data 228c362beb0SVladimir Oltean * structures (accessible from the tagger). 229c362beb0SVladimir Oltean */ 230c362beb0SVladimir Oltean void *priv; 231c362beb0SVladimir Oltean 232c362beb0SVladimir Oltean /* 23367dbb9d4SFlorian Fainelli * Original copy of the master netdev ethtool_ops 23467dbb9d4SFlorian Fainelli */ 23567dbb9d4SFlorian Fainelli const struct ethtool_ops *orig_ethtool_ops; 236da7b9e9bSFlorian Fainelli 237da7b9e9bSFlorian Fainelli /* 238da7b9e9bSFlorian Fainelli * Original copy of the master netdev net_device_ops 239da7b9e9bSFlorian Fainelli */ 2404cfab356SFlorian Fainelli const struct dsa_netdevice_ops *netdev_ops; 241fb35c60cSVivien Didelot 242fb35c60cSVivien Didelot bool setup; 243c8b09808SAndrew Lunn }; 244c8b09808SAndrew Lunn 245c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member, 246c5f51765SVivien Didelot * and no dst->rtable nor this struct dsa_link would be needed, 247c5f51765SVivien Didelot * but this would require some more complex tree walking, 248c5f51765SVivien Didelot * so keep it stupid at the moment and list them all. 249c5f51765SVivien Didelot */ 250c5f51765SVivien Didelot struct dsa_link { 251c5f51765SVivien Didelot struct dsa_port *dp; 252c5f51765SVivien Didelot struct dsa_port *link_dp; 253c5f51765SVivien Didelot struct list_head list; 254c5f51765SVivien Didelot }; 255c5f51765SVivien Didelot 256c8f0b869SBen Hutchings struct dsa_switch { 257fb35c60cSVivien Didelot bool setup; 258fb35c60cSVivien Didelot 259c33063d6SAndrew Lunn struct device *dev; 260c33063d6SAndrew Lunn 261c8f0b869SBen Hutchings /* 262c8f0b869SBen Hutchings * Parent switch tree, and switch index. 263c8f0b869SBen Hutchings */ 264c8f0b869SBen Hutchings struct dsa_switch_tree *dst; 26599feaafcSVivien Didelot unsigned int index; 266c8f0b869SBen Hutchings 267f515f192SVivien Didelot /* Listener for switch fabric events */ 268f515f192SVivien Didelot struct notifier_block nb; 269f515f192SVivien Didelot 270c8f0b869SBen Hutchings /* 2717543a6d5SAndrew Lunn * Give the switch driver somewhere to hang its private data 2727543a6d5SAndrew Lunn * structure. 2737543a6d5SAndrew Lunn */ 2747543a6d5SAndrew Lunn void *priv; 2757543a6d5SAndrew Lunn 2767543a6d5SAndrew Lunn /* 277c8f0b869SBen Hutchings * Configuration data for this switch. 278c8f0b869SBen Hutchings */ 279ff04955cSAndrew Lunn struct dsa_chip_data *cd; 280c8f0b869SBen Hutchings 281c8f0b869SBen Hutchings /* 2829d490b4eSVivien Didelot * The switch operations. 283c8f0b869SBen Hutchings */ 284a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 285c8f0b869SBen Hutchings 28666472fc0SAndrew Lunn /* 287c8f0b869SBen Hutchings * Slave mii_bus and devices for the individual ports. 288c8f0b869SBen Hutchings */ 2890d8bcdd3SFlorian Fainelli u32 phys_mii_mask; 290c8f0b869SBen Hutchings struct mii_bus *slave_mii_bus; 291a0c02161SVivien Didelot 2920f3da6afSVivien Didelot /* Ageing Time limits in msecs */ 2930f3da6afSVivien Didelot unsigned int ageing_time_min; 2940f3da6afSVivien Didelot unsigned int ageing_time_max; 2950f3da6afSVivien Didelot 29696567d5dSAndrew Lunn /* devlink used to represent this switch device */ 29796567d5dSAndrew Lunn struct devlink *devlink; 29896567d5dSAndrew Lunn 29955199df6SFlorian Fainelli /* Number of switch port queues */ 30055199df6SFlorian Fainelli unsigned int num_tx_queues; 30155199df6SFlorian Fainelli 3028f5d16f6SVladimir Oltean /* Disallow bridge core from requesting different VLAN awareness 3038f5d16f6SVladimir Oltean * settings on ports if not hardware-supported 3048f5d16f6SVladimir Oltean */ 3058f5d16f6SVladimir Oltean bool vlan_filtering_is_global; 3068f5d16f6SVladimir Oltean 30754a0ed0dSRussell King /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 30854a0ed0dSRussell King * that have vlan_filtering=0. All drivers should ideally set this (and 30954a0ed0dSRussell King * then the option would get removed), but it is unknown whether this 31054a0ed0dSRussell King * would break things or not. 31154a0ed0dSRussell King */ 31254a0ed0dSRussell King bool configure_vlan_while_not_filtering; 31354a0ed0dSRussell King 3141dc0408cSFlorian Fainelli /* If the switch driver always programs the CPU port as egress tagged 3151dc0408cSFlorian Fainelli * despite the VLAN configuration indicating otherwise, then setting 3161dc0408cSFlorian Fainelli * @untag_bridge_pvid will force the DSA receive path to pop the bridge's 3171dc0408cSFlorian Fainelli * default_pvid VLAN tagged frames to offer a consistent behavior 3181dc0408cSFlorian Fainelli * between a vlan_filtering=0 and vlan_filtering=1 bridge device. 3191dc0408cSFlorian Fainelli */ 3201dc0408cSFlorian Fainelli bool untag_bridge_pvid; 3211dc0408cSFlorian Fainelli 32214574676SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 32314574676SVladimir Oltean * should be retrieved from here and not from the per-port settings. 32414574676SVladimir Oltean */ 32514574676SVladimir Oltean bool vlan_filtering; 32614574676SVladimir Oltean 327787cac3fSVladimir Oltean /* MAC PCS does not provide link state change interrupt, and requires 328787cac3fSVladimir Oltean * polling. Flag passed on to PHYLINK. 329787cac3fSVladimir Oltean */ 330787cac3fSVladimir Oltean bool pcs_poll; 331787cac3fSVladimir Oltean 332bff33f7eSVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 333bff33f7eSVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 334bff33f7eSVladimir Oltean * interfaces is needed. 335bff33f7eSVladimir Oltean */ 336bff33f7eSVladimir Oltean bool mtu_enforcement_ingress; 337bff33f7eSVladimir Oltean 338a0c02161SVivien Didelot size_t num_ports; 339c8f0b869SBen Hutchings }; 340c8f0b869SBen Hutchings 34168bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 342c38c5a66SVivien Didelot { 343b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 344d607525bSVivien Didelot struct dsa_port *dp; 345b96ddf25SVivien Didelot 346b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 347b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 348b96ddf25SVivien Didelot return dp; 349d607525bSVivien Didelot 350d607525bSVivien Didelot return NULL; 351c38c5a66SVivien Didelot } 352c38c5a66SVivien Didelot 353bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 354bff7b688SVivien Didelot { 355c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 356bff7b688SVivien Didelot } 357bff7b688SVivien Didelot 358c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 359c8f0b869SBen Hutchings { 360c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 361c8f0b869SBen Hutchings } 362c8f0b869SBen Hutchings 36360045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 36460045cbfSAndrew Lunn { 365c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 36660045cbfSAndrew Lunn } 36760045cbfSAndrew Lunn 3682b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 3696cd456f3SVivien Didelot { 370c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 3716cd456f3SVivien Didelot } 3726cd456f3SVivien Didelot 37302bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 37402bc6e54SVivien Didelot { 375c38c5a66SVivien Didelot u32 mask = 0; 376c38c5a66SVivien Didelot int p; 37702bc6e54SVivien Didelot 378c38c5a66SVivien Didelot for (p = 0; p < ds->num_ports; p++) 379c38c5a66SVivien Didelot if (dsa_is_user_port(ds, p)) 380c38c5a66SVivien Didelot mask |= BIT(p); 381c38c5a66SVivien Didelot 382c38c5a66SVivien Didelot return mask; 383c8652c83SVivien Didelot } 384c8652c83SVivien Didelot 385c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 386c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 387c5f51765SVivien Didelot { 388c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 389c5f51765SVivien Didelot struct dsa_link *dl; 390c5f51765SVivien Didelot 391c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 392c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 393c5f51765SVivien Didelot return dl->dp->index; 394c5f51765SVivien Didelot 395c5f51765SVivien Didelot return ds->num_ports; 396c5f51765SVivien Didelot } 397c5f51765SVivien Didelot 3983b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 3993b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 4003b8fac5dSVivien Didelot int port) 4013b8fac5dSVivien Didelot { 4023b8fac5dSVivien Didelot if (device == ds->index) 4033b8fac5dSVivien Didelot return port; 4043b8fac5dSVivien Didelot else 405c5f51765SVivien Didelot return dsa_routing_port(ds, device); 4063b8fac5dSVivien Didelot } 4073b8fac5dSVivien Didelot 4083b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 40907073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 410c8f0b869SBen Hutchings { 41107073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 41207073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 41307073c79SVivien Didelot 41407073c79SVivien Didelot if (!cpu_dp) 41507073c79SVivien Didelot return port; 416c8f0b869SBen Hutchings 4173b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 418c8f0b869SBen Hutchings } 419c8f0b869SBen Hutchings 420cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 421cf2d45f5SVladimir Oltean { 422cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 423cf2d45f5SVladimir Oltean 424cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 425cf2d45f5SVladimir Oltean return ds->vlan_filtering; 426cf2d45f5SVladimir Oltean else 427cf2d45f5SVladimir Oltean return dp->vlan_filtering; 428cf2d45f5SVladimir Oltean } 429cf2d45f5SVladimir Oltean 4302bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 4312bedde1aSArkadi Sharshevsky bool is_static, void *data); 4329d490b4eSVivien Didelot struct dsa_switch_ops { 4335ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 4344d776482SFlorian Fainelli int port, 4354d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 4367b314362SAndrew Lunn 437c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 4385e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 4396819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 440c8f0b869SBen Hutchings 441c8f0b869SBen Hutchings /* 442c8f0b869SBen Hutchings * Access to the switch's PHY registers. 443c8f0b869SBen Hutchings */ 444c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 445c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 446c8f0b869SBen Hutchings int regnum, u16 val); 447c8f0b869SBen Hutchings 448c8f0b869SBen Hutchings /* 449ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 450ec9436baSFlorian Fainelli */ 451ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 452ec9436baSFlorian Fainelli struct phy_device *phydev); 453ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 454ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 455ec9436baSFlorian Fainelli 456ec9436baSFlorian Fainelli /* 45711d8f3ddSFlorian Fainelli * PHYLINK integration 45811d8f3ddSFlorian Fainelli */ 45911d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 46011d8f3ddSFlorian Fainelli unsigned long *supported, 46111d8f3ddSFlorian Fainelli struct phylink_link_state *state); 46211d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 46311d8f3ddSFlorian Fainelli struct phylink_link_state *state); 46411d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 46511d8f3ddSFlorian Fainelli unsigned int mode, 46611d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 46711d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 46811d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 46911d8f3ddSFlorian Fainelli unsigned int mode, 47011d8f3ddSFlorian Fainelli phy_interface_t interface); 47111d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 47211d8f3ddSFlorian Fainelli unsigned int mode, 47311d8f3ddSFlorian Fainelli phy_interface_t interface, 4745b502a7bSRussell King struct phy_device *phydev, 4755b502a7bSRussell King int speed, int duplex, 4765b502a7bSRussell King bool tx_pause, bool rx_pause); 47711d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 47811d8f3ddSFlorian Fainelli struct phylink_link_state *state); 47911d8f3ddSFlorian Fainelli /* 480c8f0b869SBen Hutchings * ethtool hardware statistics. 481c8f0b869SBen Hutchings */ 48289f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 48389f09048SFlorian Fainelli u32 stringset, uint8_t *data); 484c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 485c8f0b869SBen Hutchings int port, uint64_t *data); 48689f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 487cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 488cf963573SFlorian Fainelli int port, uint64_t *data); 48924462549SFlorian Fainelli 49024462549SFlorian Fainelli /* 49119e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 49219e57c4eSFlorian Fainelli */ 49319e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 49419e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 49519e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 49619e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 49719e57c4eSFlorian Fainelli 49819e57c4eSFlorian Fainelli /* 4990336369dSBrandon Streiff * ethtool timestamp info 5000336369dSBrandon Streiff */ 5010336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 5020336369dSBrandon Streiff struct ethtool_ts_info *ts); 5030336369dSBrandon Streiff 5040336369dSBrandon Streiff /* 50524462549SFlorian Fainelli * Suspend and resume 50624462549SFlorian Fainelli */ 50724462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 50824462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 509b2f2af21SFlorian Fainelli 510b2f2af21SFlorian Fainelli /* 511b2f2af21SFlorian Fainelli * Port enable/disable 512b2f2af21SFlorian Fainelli */ 513b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 514b2f2af21SFlorian Fainelli struct phy_device *phy); 51575104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 5167905288fSFlorian Fainelli 5177905288fSFlorian Fainelli /* 51808f50061SVivien Didelot * Port's MAC EEE settings 5197905288fSFlorian Fainelli */ 52008f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 5217905288fSFlorian Fainelli struct ethtool_eee *e); 52208f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 5237905288fSFlorian Fainelli struct ethtool_eee *e); 52451579c3fSGuenter Roeck 5256793abb4SGuenter Roeck /* EEPROM access */ 5266793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 5276793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 5286793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 5296793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 5306793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 5313d762a0fSGuenter Roeck 5323d762a0fSGuenter Roeck /* 5333d762a0fSGuenter Roeck * Register access. 5343d762a0fSGuenter Roeck */ 5353d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 5363d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 5373d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 538b73adef6SFlorian Fainelli 539b73adef6SFlorian Fainelli /* 540*e358bef7SVladimir Oltean * Upper device tracking. 541*e358bef7SVladimir Oltean */ 542*e358bef7SVladimir Oltean int (*port_prechangeupper)(struct dsa_switch *ds, int port, 543*e358bef7SVladimir Oltean struct netdev_notifier_changeupper_info *info); 544*e358bef7SVladimir Oltean 545*e358bef7SVladimir Oltean /* 546b73adef6SFlorian Fainelli * Bridge integration 547b73adef6SFlorian Fainelli */ 54834a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 54971327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 550a6692754SVivien Didelot struct net_device *bridge); 551f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 552f123f2fbSVivien Didelot struct net_device *bridge); 55343c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 554b73adef6SFlorian Fainelli u8 state); 555732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 55657652796SRussell King int (*port_egress_floods)(struct dsa_switch *ds, int port, 55757652796SRussell King bool unicast, bool multicast); 5582a778e1bSVivien Didelot 5592a778e1bSVivien Didelot /* 56011149536SVivien Didelot * VLAN support 56111149536SVivien Didelot */ 562fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 5632e554a7aSVladimir Oltean bool vlan_filtering, 5642e554a7aSVladimir Oltean struct switchdev_trans *trans); 56576e398a6SVivien Didelot int (*port_vlan_prepare)(struct dsa_switch *ds, int port, 56680e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 5674d5770b3SVivien Didelot void (*port_vlan_add)(struct dsa_switch *ds, int port, 56880e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 56976e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 57076e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 57111149536SVivien Didelot /* 5722a778e1bSVivien Didelot * Forwarding database 5732a778e1bSVivien Didelot */ 5741b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 5756c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 5762a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 5776c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 578ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 5792bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 5808df30255SVivien Didelot 5818df30255SVivien Didelot /* 5828df30255SVivien Didelot * Multicast database 5838df30255SVivien Didelot */ 5848df30255SVivien Didelot int (*port_mdb_prepare)(struct dsa_switch *ds, int port, 5853709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 5868df30255SVivien Didelot void (*port_mdb_add)(struct dsa_switch *ds, int port, 5873709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 5888df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 5898df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb); 590bf9f2648SFlorian Fainelli /* 591bf9f2648SFlorian Fainelli * RXNFC 592bf9f2648SFlorian Fainelli */ 593bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 594bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 595bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 596bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 597f50f2127SFlorian Fainelli 598f50f2127SFlorian Fainelli /* 599f50f2127SFlorian Fainelli * TC integration 600f50f2127SFlorian Fainelli */ 601ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 602ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 603ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 604ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 605ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 606ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 607f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 608f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 609f50f2127SFlorian Fainelli bool ingress); 610f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 611f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 61234297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 61334297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 61434297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 61547d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 61647d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 61740ef2c93SVivien Didelot 61840ef2c93SVivien Didelot /* 61940ef2c93SVivien Didelot * Cross-chip operations 62040ef2c93SVivien Didelot */ 621f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 622f66a6a69SVladimir Oltean int sw_index, int port, 623f66a6a69SVladimir Oltean struct net_device *br); 624f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 625f66a6a69SVladimir Oltean int sw_index, int port, 626f66a6a69SVladimir Oltean struct net_device *br); 6270336369dSBrandon Streiff 6280336369dSBrandon Streiff /* 6290336369dSBrandon Streiff * PTP functionality 6300336369dSBrandon Streiff */ 6310336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 6320336369dSBrandon Streiff struct ifreq *ifr); 6330336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 6340336369dSBrandon Streiff struct ifreq *ifr); 63590af1059SBrandon Streiff bool (*port_txtstamp)(struct dsa_switch *ds, int port, 63690af1059SBrandon Streiff struct sk_buff *clone, unsigned int type); 63790af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 63890af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 63997a69a0dSVladimir Oltean 6400f06b855SAndrew Lunn /* Devlink parameters, etc */ 6416b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 6426b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6436b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 6446b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6450f06b855SAndrew Lunn int (*devlink_info_get)(struct dsa_switch *ds, 6460f06b855SAndrew Lunn struct devlink_info_req *req, 6470f06b855SAndrew Lunn struct netlink_ext_ack *extack); 648bfcb8132SVladimir Oltean 649bfcb8132SVladimir Oltean /* 650bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 651bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 652bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 653ab88d64aSRandy Dunlap * method needs to do so privately. 654bfcb8132SVladimir Oltean */ 655bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 656bfcb8132SVladimir Oltean int new_mtu); 657bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 6586b297524SAndrew Lunn }; 6596b297524SAndrew Lunn 6606b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 6616b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 6626b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 6636b297524SAndrew Lunn 6646b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 6656b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6666b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 6676b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6686b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 6696b297524SAndrew Lunn const struct devlink_param *params, 6706b297524SAndrew Lunn size_t params_count); 6716b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 6726b297524SAndrew Lunn const struct devlink_param *params, 6736b297524SAndrew Lunn size_t params_count); 6745cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 6755cd73fbdSAndrew Lunn const char *resource_name, 6765cd73fbdSAndrew Lunn u64 resource_size, 6775cd73fbdSAndrew Lunn u64 resource_id, 6785cd73fbdSAndrew Lunn u64 parent_resource_id, 6795cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 6805cd73fbdSAndrew Lunn 6815cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 6825cd73fbdSAndrew Lunn 6835cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 6845cd73fbdSAndrew Lunn u64 resource_id, 6855cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 6865cd73fbdSAndrew Lunn void *occ_get_priv); 6875cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 6885cd73fbdSAndrew Lunn u64 resource_id); 68997c82c23SAndrew Lunn struct devlink_region * 69097c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds, 69197c82c23SAndrew Lunn const struct devlink_region_ops *ops, 69297c82c23SAndrew Lunn u32 region_max_snapshots, u64 region_size); 69308156ba4SAndrew Lunn struct devlink_region * 69408156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds, 69508156ba4SAndrew Lunn int port, 69608156ba4SAndrew Lunn const struct devlink_port_region_ops *ops, 69708156ba4SAndrew Lunn u32 region_max_snapshots, u64 region_size); 69897c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region); 69997c82c23SAndrew Lunn 700e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 7015cd73fbdSAndrew Lunn 7026b297524SAndrew Lunn struct dsa_devlink_priv { 7036b297524SAndrew Lunn struct dsa_switch *ds; 704c8f0b869SBen Hutchings }; 705c8f0b869SBen Hutchings 706ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 707ccc3e6b0SAndrew Lunn { 708ccc3e6b0SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 709ccc3e6b0SAndrew Lunn 710ccc3e6b0SAndrew Lunn return dl_priv->ds; 711ccc3e6b0SAndrew Lunn } 712ccc3e6b0SAndrew Lunn 7137d1e2a10SAndrew Lunn static inline 7147d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 7157d1e2a10SAndrew Lunn { 7167d1e2a10SAndrew Lunn struct devlink *dl = port->devlink; 7177d1e2a10SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 7187d1e2a10SAndrew Lunn 7197d1e2a10SAndrew Lunn return dl_priv->ds; 7207d1e2a10SAndrew Lunn } 7217d1e2a10SAndrew Lunn 7227d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port) 7237d1e2a10SAndrew Lunn { 7247d1e2a10SAndrew Lunn return port->index; 7257d1e2a10SAndrew Lunn } 7267d1e2a10SAndrew Lunn 727ab3d408dSFlorian Fainelli struct dsa_switch_driver { 728ab3d408dSFlorian Fainelli struct list_head list; 729a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 730ab3d408dSFlorian Fainelli }; 731ab3d408dSFlorian Fainelli 73214b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 733c8f0b869SBen Hutchings 73473a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 7359eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 736c6e970a0SAndrew Lunn { 737c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 738717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 739c6e970a0SAndrew Lunn #endif 740c6e970a0SAndrew Lunn return false; 741c6e970a0SAndrew Lunn } 742c6e970a0SAndrew Lunn 743cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb, 744cc1939e4SVladimir Oltean struct net_device *dev) 745cc1939e4SVladimir Oltean { 746cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 747cc1939e4SVladimir Oltean return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev); 748cc1939e4SVladimir Oltean #endif 749cc1939e4SVladimir Oltean return false; 750cc1939e4SVladimir Oltean } 751cc1939e4SVladimir Oltean 7529790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail 7539790cf20SVladimir Oltean * tags, which don't break dissection) can be treated the same from the 7549790cf20SVladimir Oltean * perspective of the flow dissector. 7559790cf20SVladimir Oltean * 7569790cf20SVladimir Oltean * We need to return: 7579790cf20SVladimir Oltean * - offset: the (B - A) difference between: 7589790cf20SVladimir Oltean * A. the position of the real EtherType and 7599790cf20SVladimir Oltean * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 7609790cf20SVladimir Oltean * after the normal EtherType was supposed to be) 7619790cf20SVladimir Oltean * The offset in bytes is exactly equal to the tagger overhead (and half of 7629790cf20SVladimir Oltean * that, in __be16 shorts). 7639790cf20SVladimir Oltean * 7649790cf20SVladimir Oltean * - proto: the value of the real EtherType. 7659790cf20SVladimir Oltean */ 7669790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 7679790cf20SVladimir Oltean __be16 *proto, int *offset) 7689790cf20SVladimir Oltean { 7699790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 7709790cf20SVladimir Oltean const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 7719790cf20SVladimir Oltean int tag_len = ops->overhead; 7729790cf20SVladimir Oltean 7739790cf20SVladimir Oltean *offset = tag_len; 7749790cf20SVladimir Oltean *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 7759790cf20SVladimir Oltean #endif 7769790cf20SVladimir Oltean } 7779790cf20SVladimir Oltean 7784cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 7794cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev) 7804cfab356SFlorian Fainelli { 7814cfab356SFlorian Fainelli int err = -EOPNOTSUPP; 7824cfab356SFlorian Fainelli 7834cfab356SFlorian Fainelli if (!dev->dsa_ptr) 7844cfab356SFlorian Fainelli return err; 7854cfab356SFlorian Fainelli 7864cfab356SFlorian Fainelli if (!dev->dsa_ptr->netdev_ops) 7874cfab356SFlorian Fainelli return err; 7884cfab356SFlorian Fainelli 7894cfab356SFlorian Fainelli return 0; 7904cfab356SFlorian Fainelli } 7914cfab356SFlorian Fainelli 7924cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr, 7934cfab356SFlorian Fainelli int cmd) 7944cfab356SFlorian Fainelli { 7954cfab356SFlorian Fainelli const struct dsa_netdevice_ops *ops; 7964cfab356SFlorian Fainelli int err; 7974cfab356SFlorian Fainelli 7984cfab356SFlorian Fainelli err = __dsa_netdevice_ops_check(dev); 7994cfab356SFlorian Fainelli if (err) 8004cfab356SFlorian Fainelli return err; 8014cfab356SFlorian Fainelli 8024cfab356SFlorian Fainelli ops = dev->dsa_ptr->netdev_ops; 8034cfab356SFlorian Fainelli 8044cfab356SFlorian Fainelli return ops->ndo_do_ioctl(dev, ifr, cmd); 8054cfab356SFlorian Fainelli } 8064cfab356SFlorian Fainelli #else 8074cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr, 8084cfab356SFlorian Fainelli int cmd) 8094cfab356SFlorian Fainelli { 8104cfab356SFlorian Fainelli return -EOPNOTSUPP; 8114cfab356SFlorian Fainelli } 8124cfab356SFlorian Fainelli #endif 8134cfab356SFlorian Fainelli 81483c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 81523c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 8163b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 817ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 818ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 819ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 820ea825e70SFlorian Fainelli #else 821ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 822ea825e70SFlorian Fainelli { 823ea825e70SFlorian Fainelli return 0; 824ea825e70SFlorian Fainelli } 825ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 826ea825e70SFlorian Fainelli { 827ea825e70SFlorian Fainelli return 0; 828ea825e70SFlorian Fainelli } 829ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 830ea825e70SFlorian Fainelli 83160724d4bSFlorian Fainelli enum dsa_notifier_type { 83260724d4bSFlorian Fainelli DSA_PORT_REGISTER, 83360724d4bSFlorian Fainelli DSA_PORT_UNREGISTER, 83460724d4bSFlorian Fainelli }; 83560724d4bSFlorian Fainelli 83660724d4bSFlorian Fainelli struct dsa_notifier_info { 83760724d4bSFlorian Fainelli struct net_device *dev; 83860724d4bSFlorian Fainelli }; 83960724d4bSFlorian Fainelli 84060724d4bSFlorian Fainelli struct dsa_notifier_register_info { 84160724d4bSFlorian Fainelli struct dsa_notifier_info info; /* must be first */ 84260724d4bSFlorian Fainelli struct net_device *master; 84360724d4bSFlorian Fainelli unsigned int port_number; 84460724d4bSFlorian Fainelli unsigned int switch_number; 84560724d4bSFlorian Fainelli }; 84660724d4bSFlorian Fainelli 84760724d4bSFlorian Fainelli static inline struct net_device * 84860724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info) 84960724d4bSFlorian Fainelli { 85060724d4bSFlorian Fainelli return info->dev; 85160724d4bSFlorian Fainelli } 85260724d4bSFlorian Fainelli 85360724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 85460724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb); 85560724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb); 85660724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev, 85760724d4bSFlorian Fainelli struct dsa_notifier_info *info); 85860724d4bSFlorian Fainelli #else 85960724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb) 86060724d4bSFlorian Fainelli { 86160724d4bSFlorian Fainelli return 0; 86260724d4bSFlorian Fainelli } 86360724d4bSFlorian Fainelli 86460724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb) 86560724d4bSFlorian Fainelli { 86660724d4bSFlorian Fainelli return 0; 86760724d4bSFlorian Fainelli } 86860724d4bSFlorian Fainelli 86960724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev, 87060724d4bSFlorian Fainelli struct dsa_notifier_info *info) 87160724d4bSFlorian Fainelli { 87260724d4bSFlorian Fainelli return NOTIFY_DONE; 87360724d4bSFlorian Fainelli } 87460724d4bSFlorian Fainelli #endif 87560724d4bSFlorian Fainelli 8760a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */ 8770a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q) 8780a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v) ((v) >> 8) 8790a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff) 8800a5f14ceSFlorian Fainelli 881cf963573SFlorian Fainelli 88297a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 883cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 884cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 885cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp); 88611d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 887cf963573SFlorian Fainelli 888d3b8c049SAndrew Lunn struct dsa_tag_driver { 889d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 890d3b8c049SAndrew Lunn struct list_head list; 891d3b8c049SAndrew Lunn struct module *owner; 892d3b8c049SAndrew Lunn }; 893d3b8c049SAndrew Lunn 894d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 895d3b8c049SAndrew Lunn unsigned int count, 896d3b8c049SAndrew Lunn struct module *owner); 897d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 898d3b8c049SAndrew Lunn unsigned int count); 899d3b8c049SAndrew Lunn 900d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 901d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 902d3b8c049SAndrew Lunn { \ 903d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 904d3b8c049SAndrew Lunn THIS_MODULE); \ 905d3b8c049SAndrew Lunn return 0; \ 906d3b8c049SAndrew Lunn } \ 907d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 908d3b8c049SAndrew Lunn \ 909d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 910d3b8c049SAndrew Lunn { \ 911d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 912d3b8c049SAndrew Lunn } \ 913d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 914d3b8c049SAndrew Lunn 915d3b8c049SAndrew Lunn /** 916d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 917d3b8c049SAndrew Lunn * drivers 918d3b8c049SAndrew Lunn * @__ops_array: Array of tag driver strucutres 919d3b8c049SAndrew Lunn * 920d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 921d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 922d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 923d3b8c049SAndrew Lunn */ 924d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 925d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 926d3b8c049SAndrew Lunn 927d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 928d3b8c049SAndrew Lunn 929d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 930d3b8c049SAndrew Lunn * drivers 931d3b8c049SAndrew Lunn */ 932d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 933d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 934d3b8c049SAndrew Lunn .ops = &__ops, \ 935d3b8c049SAndrew Lunn } 936d3b8c049SAndrew Lunn 937d3b8c049SAndrew Lunn /** 938d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 939d3b8c049SAndrew Lunn * driver 940d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 941d3b8c049SAndrew Lunn * 942d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 943d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 944d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 945d3b8c049SAndrew Lunn */ 946d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 947d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 948d3b8c049SAndrew Lunn \ 949d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 950d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 951d3b8c049SAndrew Lunn }; \ 952d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 95391da11f8SLennert Buytenhek #endif 954d3b8c049SAndrew Lunn 955