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 322*d5f19486SVladimir Oltean /* Let DSA manage the FDB entries towards the CPU, based on the 323*d5f19486SVladimir Oltean * software bridge database. 324*d5f19486SVladimir Oltean */ 325*d5f19486SVladimir Oltean bool assisted_learning_on_cpu_port; 326*d5f19486SVladimir Oltean 32714574676SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 32814574676SVladimir Oltean * should be retrieved from here and not from the per-port settings. 32914574676SVladimir Oltean */ 33014574676SVladimir Oltean bool vlan_filtering; 33114574676SVladimir Oltean 332787cac3fSVladimir Oltean /* MAC PCS does not provide link state change interrupt, and requires 333787cac3fSVladimir Oltean * polling. Flag passed on to PHYLINK. 334787cac3fSVladimir Oltean */ 335787cac3fSVladimir Oltean bool pcs_poll; 336787cac3fSVladimir Oltean 337bff33f7eSVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 338bff33f7eSVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 339bff33f7eSVladimir Oltean * interfaces is needed. 340bff33f7eSVladimir Oltean */ 341bff33f7eSVladimir Oltean bool mtu_enforcement_ingress; 342bff33f7eSVladimir Oltean 343a0c02161SVivien Didelot size_t num_ports; 344c8f0b869SBen Hutchings }; 345c8f0b869SBen Hutchings 34668bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 347c38c5a66SVivien Didelot { 348b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 349d607525bSVivien Didelot struct dsa_port *dp; 350b96ddf25SVivien Didelot 351b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 352b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 353b96ddf25SVivien Didelot return dp; 354d607525bSVivien Didelot 355d607525bSVivien Didelot return NULL; 356c38c5a66SVivien Didelot } 357c38c5a66SVivien Didelot 358bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 359bff7b688SVivien Didelot { 360c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 361bff7b688SVivien Didelot } 362bff7b688SVivien Didelot 363c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 364c8f0b869SBen Hutchings { 365c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 366c8f0b869SBen Hutchings } 367c8f0b869SBen Hutchings 36860045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 36960045cbfSAndrew Lunn { 370c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 37160045cbfSAndrew Lunn } 37260045cbfSAndrew Lunn 3732b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 3746cd456f3SVivien Didelot { 375c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 3766cd456f3SVivien Didelot } 3776cd456f3SVivien Didelot 37802bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 37902bc6e54SVivien Didelot { 380c38c5a66SVivien Didelot u32 mask = 0; 381c38c5a66SVivien Didelot int p; 38202bc6e54SVivien Didelot 383c38c5a66SVivien Didelot for (p = 0; p < ds->num_ports; p++) 384c38c5a66SVivien Didelot if (dsa_is_user_port(ds, p)) 385c38c5a66SVivien Didelot mask |= BIT(p); 386c38c5a66SVivien Didelot 387c38c5a66SVivien Didelot return mask; 388c8652c83SVivien Didelot } 389c8652c83SVivien Didelot 390c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 391c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 392c5f51765SVivien Didelot { 393c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 394c5f51765SVivien Didelot struct dsa_link *dl; 395c5f51765SVivien Didelot 396c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 397c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 398c5f51765SVivien Didelot return dl->dp->index; 399c5f51765SVivien Didelot 400c5f51765SVivien Didelot return ds->num_ports; 401c5f51765SVivien Didelot } 402c5f51765SVivien Didelot 4033b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 4043b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 4053b8fac5dSVivien Didelot int port) 4063b8fac5dSVivien Didelot { 4073b8fac5dSVivien Didelot if (device == ds->index) 4083b8fac5dSVivien Didelot return port; 4093b8fac5dSVivien Didelot else 410c5f51765SVivien Didelot return dsa_routing_port(ds, device); 4113b8fac5dSVivien Didelot } 4123b8fac5dSVivien Didelot 4133b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 41407073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 415c8f0b869SBen Hutchings { 41607073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 41707073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 41807073c79SVivien Didelot 41907073c79SVivien Didelot if (!cpu_dp) 42007073c79SVivien Didelot return port; 421c8f0b869SBen Hutchings 4223b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 423c8f0b869SBen Hutchings } 424c8f0b869SBen Hutchings 425cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 426cf2d45f5SVladimir Oltean { 427cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 428cf2d45f5SVladimir Oltean 429cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 430cf2d45f5SVladimir Oltean return ds->vlan_filtering; 431cf2d45f5SVladimir Oltean else 432cf2d45f5SVladimir Oltean return dp->vlan_filtering; 433cf2d45f5SVladimir Oltean } 434cf2d45f5SVladimir Oltean 4352bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 4362bedde1aSArkadi Sharshevsky bool is_static, void *data); 4379d490b4eSVivien Didelot struct dsa_switch_ops { 4385ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 4394d776482SFlorian Fainelli int port, 4404d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 4417b314362SAndrew Lunn 442c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 4435e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 4446819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 445c8f0b869SBen Hutchings 446c8f0b869SBen Hutchings /* 447c8f0b869SBen Hutchings * Access to the switch's PHY registers. 448c8f0b869SBen Hutchings */ 449c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 450c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 451c8f0b869SBen Hutchings int regnum, u16 val); 452c8f0b869SBen Hutchings 453c8f0b869SBen Hutchings /* 454ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 455ec9436baSFlorian Fainelli */ 456ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 457ec9436baSFlorian Fainelli struct phy_device *phydev); 458ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 459ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 460ec9436baSFlorian Fainelli 461ec9436baSFlorian Fainelli /* 46211d8f3ddSFlorian Fainelli * PHYLINK integration 46311d8f3ddSFlorian Fainelli */ 46411d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 46511d8f3ddSFlorian Fainelli unsigned long *supported, 46611d8f3ddSFlorian Fainelli struct phylink_link_state *state); 46711d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 46811d8f3ddSFlorian Fainelli struct phylink_link_state *state); 46911d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 47011d8f3ddSFlorian Fainelli unsigned int mode, 47111d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 47211d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 47311d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 47411d8f3ddSFlorian Fainelli unsigned int mode, 47511d8f3ddSFlorian Fainelli phy_interface_t interface); 47611d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 47711d8f3ddSFlorian Fainelli unsigned int mode, 47811d8f3ddSFlorian Fainelli phy_interface_t interface, 4795b502a7bSRussell King struct phy_device *phydev, 4805b502a7bSRussell King int speed, int duplex, 4815b502a7bSRussell King bool tx_pause, bool rx_pause); 48211d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 48311d8f3ddSFlorian Fainelli struct phylink_link_state *state); 48411d8f3ddSFlorian Fainelli /* 485c8f0b869SBen Hutchings * ethtool hardware statistics. 486c8f0b869SBen Hutchings */ 48789f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 48889f09048SFlorian Fainelli u32 stringset, uint8_t *data); 489c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 490c8f0b869SBen Hutchings int port, uint64_t *data); 49189f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 492cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 493cf963573SFlorian Fainelli int port, uint64_t *data); 49424462549SFlorian Fainelli 49524462549SFlorian Fainelli /* 49619e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 49719e57c4eSFlorian Fainelli */ 49819e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 49919e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 50019e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 50119e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 50219e57c4eSFlorian Fainelli 50319e57c4eSFlorian Fainelli /* 5040336369dSBrandon Streiff * ethtool timestamp info 5050336369dSBrandon Streiff */ 5060336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 5070336369dSBrandon Streiff struct ethtool_ts_info *ts); 5080336369dSBrandon Streiff 5090336369dSBrandon Streiff /* 51024462549SFlorian Fainelli * Suspend and resume 51124462549SFlorian Fainelli */ 51224462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 51324462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 514b2f2af21SFlorian Fainelli 515b2f2af21SFlorian Fainelli /* 516b2f2af21SFlorian Fainelli * Port enable/disable 517b2f2af21SFlorian Fainelli */ 518b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 519b2f2af21SFlorian Fainelli struct phy_device *phy); 52075104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 5217905288fSFlorian Fainelli 5227905288fSFlorian Fainelli /* 52308f50061SVivien Didelot * Port's MAC EEE settings 5247905288fSFlorian Fainelli */ 52508f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 5267905288fSFlorian Fainelli struct ethtool_eee *e); 52708f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 5287905288fSFlorian Fainelli struct ethtool_eee *e); 52951579c3fSGuenter Roeck 5306793abb4SGuenter Roeck /* EEPROM access */ 5316793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 5326793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 5336793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 5346793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 5356793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 5363d762a0fSGuenter Roeck 5373d762a0fSGuenter Roeck /* 5383d762a0fSGuenter Roeck * Register access. 5393d762a0fSGuenter Roeck */ 5403d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 5413d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 5423d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 543b73adef6SFlorian Fainelli 544b73adef6SFlorian Fainelli /* 545e358bef7SVladimir Oltean * Upper device tracking. 546e358bef7SVladimir Oltean */ 547e358bef7SVladimir Oltean int (*port_prechangeupper)(struct dsa_switch *ds, int port, 548e358bef7SVladimir Oltean struct netdev_notifier_changeupper_info *info); 549e358bef7SVladimir Oltean 550e358bef7SVladimir Oltean /* 551b73adef6SFlorian Fainelli * Bridge integration 552b73adef6SFlorian Fainelli */ 55334a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 55471327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 555a6692754SVivien Didelot struct net_device *bridge); 556f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 557f123f2fbSVivien Didelot struct net_device *bridge); 55843c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 559b73adef6SFlorian Fainelli u8 state); 560732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 56157652796SRussell King int (*port_egress_floods)(struct dsa_switch *ds, int port, 56257652796SRussell King bool unicast, bool multicast); 5632a778e1bSVivien Didelot 5642a778e1bSVivien Didelot /* 56511149536SVivien Didelot * VLAN support 56611149536SVivien Didelot */ 567fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 5682e554a7aSVladimir Oltean bool vlan_filtering, 5692e554a7aSVladimir Oltean struct switchdev_trans *trans); 57076e398a6SVivien Didelot int (*port_vlan_prepare)(struct dsa_switch *ds, int port, 57180e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 5724d5770b3SVivien Didelot void (*port_vlan_add)(struct dsa_switch *ds, int port, 57380e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 57476e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 57576e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 57611149536SVivien Didelot /* 5772a778e1bSVivien Didelot * Forwarding database 5782a778e1bSVivien Didelot */ 5791b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 5806c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 5812a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 5826c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 583ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 5842bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 5858df30255SVivien Didelot 5868df30255SVivien Didelot /* 5878df30255SVivien Didelot * Multicast database 5888df30255SVivien Didelot */ 5898df30255SVivien Didelot int (*port_mdb_prepare)(struct dsa_switch *ds, int port, 5903709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 5918df30255SVivien Didelot void (*port_mdb_add)(struct dsa_switch *ds, int port, 5923709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 5938df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 5948df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb); 595bf9f2648SFlorian Fainelli /* 596bf9f2648SFlorian Fainelli * RXNFC 597bf9f2648SFlorian Fainelli */ 598bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 599bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 600bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 601bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 602f50f2127SFlorian Fainelli 603f50f2127SFlorian Fainelli /* 604f50f2127SFlorian Fainelli * TC integration 605f50f2127SFlorian Fainelli */ 606ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 607ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 608ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 609ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 610ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 611ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 612f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 613f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 614f50f2127SFlorian Fainelli bool ingress); 615f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 616f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 61734297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 61834297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 61934297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 62047d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 62147d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 62240ef2c93SVivien Didelot 62340ef2c93SVivien Didelot /* 62440ef2c93SVivien Didelot * Cross-chip operations 62540ef2c93SVivien Didelot */ 626f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 627f66a6a69SVladimir Oltean int sw_index, int port, 628f66a6a69SVladimir Oltean struct net_device *br); 629f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 630f66a6a69SVladimir Oltean int sw_index, int port, 631f66a6a69SVladimir Oltean struct net_device *br); 6320336369dSBrandon Streiff 6330336369dSBrandon Streiff /* 6340336369dSBrandon Streiff * PTP functionality 6350336369dSBrandon Streiff */ 6360336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 6370336369dSBrandon Streiff struct ifreq *ifr); 6380336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 6390336369dSBrandon Streiff struct ifreq *ifr); 64090af1059SBrandon Streiff bool (*port_txtstamp)(struct dsa_switch *ds, int port, 64190af1059SBrandon Streiff struct sk_buff *clone, unsigned int type); 64290af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 64390af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 64497a69a0dSVladimir Oltean 6450f06b855SAndrew Lunn /* Devlink parameters, etc */ 6466b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 6476b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6486b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 6496b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6500f06b855SAndrew Lunn int (*devlink_info_get)(struct dsa_switch *ds, 6510f06b855SAndrew Lunn struct devlink_info_req *req, 6520f06b855SAndrew Lunn struct netlink_ext_ack *extack); 653bfcb8132SVladimir Oltean 654bfcb8132SVladimir Oltean /* 655bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 656bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 657bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 658ab88d64aSRandy Dunlap * method needs to do so privately. 659bfcb8132SVladimir Oltean */ 660bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 661bfcb8132SVladimir Oltean int new_mtu); 662bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 6636b297524SAndrew Lunn }; 6646b297524SAndrew Lunn 6656b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 6666b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 6676b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 6686b297524SAndrew Lunn 6696b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 6706b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6716b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 6726b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6736b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 6746b297524SAndrew Lunn const struct devlink_param *params, 6756b297524SAndrew Lunn size_t params_count); 6766b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 6776b297524SAndrew Lunn const struct devlink_param *params, 6786b297524SAndrew Lunn size_t params_count); 6795cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 6805cd73fbdSAndrew Lunn const char *resource_name, 6815cd73fbdSAndrew Lunn u64 resource_size, 6825cd73fbdSAndrew Lunn u64 resource_id, 6835cd73fbdSAndrew Lunn u64 parent_resource_id, 6845cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 6855cd73fbdSAndrew Lunn 6865cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 6875cd73fbdSAndrew Lunn 6885cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 6895cd73fbdSAndrew Lunn u64 resource_id, 6905cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 6915cd73fbdSAndrew Lunn void *occ_get_priv); 6925cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 6935cd73fbdSAndrew Lunn u64 resource_id); 69497c82c23SAndrew Lunn struct devlink_region * 69597c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds, 69697c82c23SAndrew Lunn const struct devlink_region_ops *ops, 69797c82c23SAndrew Lunn u32 region_max_snapshots, u64 region_size); 69808156ba4SAndrew Lunn struct devlink_region * 69908156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds, 70008156ba4SAndrew Lunn int port, 70108156ba4SAndrew Lunn const struct devlink_port_region_ops *ops, 70208156ba4SAndrew Lunn u32 region_max_snapshots, u64 region_size); 70397c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region); 70497c82c23SAndrew Lunn 705e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 7065cd73fbdSAndrew Lunn 7076b297524SAndrew Lunn struct dsa_devlink_priv { 7086b297524SAndrew Lunn struct dsa_switch *ds; 709c8f0b869SBen Hutchings }; 710c8f0b869SBen Hutchings 711ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 712ccc3e6b0SAndrew Lunn { 713ccc3e6b0SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 714ccc3e6b0SAndrew Lunn 715ccc3e6b0SAndrew Lunn return dl_priv->ds; 716ccc3e6b0SAndrew Lunn } 717ccc3e6b0SAndrew Lunn 7187d1e2a10SAndrew Lunn static inline 7197d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 7207d1e2a10SAndrew Lunn { 7217d1e2a10SAndrew Lunn struct devlink *dl = port->devlink; 7227d1e2a10SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 7237d1e2a10SAndrew Lunn 7247d1e2a10SAndrew Lunn return dl_priv->ds; 7257d1e2a10SAndrew Lunn } 7267d1e2a10SAndrew Lunn 7277d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port) 7287d1e2a10SAndrew Lunn { 7297d1e2a10SAndrew Lunn return port->index; 7307d1e2a10SAndrew Lunn } 7317d1e2a10SAndrew Lunn 732ab3d408dSFlorian Fainelli struct dsa_switch_driver { 733ab3d408dSFlorian Fainelli struct list_head list; 734a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 735ab3d408dSFlorian Fainelli }; 736ab3d408dSFlorian Fainelli 73714b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 738c8f0b869SBen Hutchings 73973a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 7409eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 741c6e970a0SAndrew Lunn { 742c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 743717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 744c6e970a0SAndrew Lunn #endif 745c6e970a0SAndrew Lunn return false; 746c6e970a0SAndrew Lunn } 747c6e970a0SAndrew Lunn 748cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb, 749cc1939e4SVladimir Oltean struct net_device *dev) 750cc1939e4SVladimir Oltean { 751cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 752cc1939e4SVladimir Oltean return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev); 753cc1939e4SVladimir Oltean #endif 754cc1939e4SVladimir Oltean return false; 755cc1939e4SVladimir Oltean } 756cc1939e4SVladimir Oltean 7579790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail 7589790cf20SVladimir Oltean * tags, which don't break dissection) can be treated the same from the 7599790cf20SVladimir Oltean * perspective of the flow dissector. 7609790cf20SVladimir Oltean * 7619790cf20SVladimir Oltean * We need to return: 7629790cf20SVladimir Oltean * - offset: the (B - A) difference between: 7639790cf20SVladimir Oltean * A. the position of the real EtherType and 7649790cf20SVladimir Oltean * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 7659790cf20SVladimir Oltean * after the normal EtherType was supposed to be) 7669790cf20SVladimir Oltean * The offset in bytes is exactly equal to the tagger overhead (and half of 7679790cf20SVladimir Oltean * that, in __be16 shorts). 7689790cf20SVladimir Oltean * 7699790cf20SVladimir Oltean * - proto: the value of the real EtherType. 7709790cf20SVladimir Oltean */ 7719790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 7729790cf20SVladimir Oltean __be16 *proto, int *offset) 7739790cf20SVladimir Oltean { 7749790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 7759790cf20SVladimir Oltean const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 7769790cf20SVladimir Oltean int tag_len = ops->overhead; 7779790cf20SVladimir Oltean 7789790cf20SVladimir Oltean *offset = tag_len; 7799790cf20SVladimir Oltean *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 7809790cf20SVladimir Oltean #endif 7819790cf20SVladimir Oltean } 7829790cf20SVladimir Oltean 7834cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 7844cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev) 7854cfab356SFlorian Fainelli { 7864cfab356SFlorian Fainelli int err = -EOPNOTSUPP; 7874cfab356SFlorian Fainelli 7884cfab356SFlorian Fainelli if (!dev->dsa_ptr) 7894cfab356SFlorian Fainelli return err; 7904cfab356SFlorian Fainelli 7914cfab356SFlorian Fainelli if (!dev->dsa_ptr->netdev_ops) 7924cfab356SFlorian Fainelli return err; 7934cfab356SFlorian Fainelli 7944cfab356SFlorian Fainelli return 0; 7954cfab356SFlorian Fainelli } 7964cfab356SFlorian Fainelli 7974cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr, 7984cfab356SFlorian Fainelli int cmd) 7994cfab356SFlorian Fainelli { 8004cfab356SFlorian Fainelli const struct dsa_netdevice_ops *ops; 8014cfab356SFlorian Fainelli int err; 8024cfab356SFlorian Fainelli 8034cfab356SFlorian Fainelli err = __dsa_netdevice_ops_check(dev); 8044cfab356SFlorian Fainelli if (err) 8054cfab356SFlorian Fainelli return err; 8064cfab356SFlorian Fainelli 8074cfab356SFlorian Fainelli ops = dev->dsa_ptr->netdev_ops; 8084cfab356SFlorian Fainelli 8094cfab356SFlorian Fainelli return ops->ndo_do_ioctl(dev, ifr, cmd); 8104cfab356SFlorian Fainelli } 8114cfab356SFlorian Fainelli #else 8124cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr, 8134cfab356SFlorian Fainelli int cmd) 8144cfab356SFlorian Fainelli { 8154cfab356SFlorian Fainelli return -EOPNOTSUPP; 8164cfab356SFlorian Fainelli } 8174cfab356SFlorian Fainelli #endif 8184cfab356SFlorian Fainelli 81983c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 82023c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 8213b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 822ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 823ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 824ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 825ea825e70SFlorian Fainelli #else 826ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 827ea825e70SFlorian Fainelli { 828ea825e70SFlorian Fainelli return 0; 829ea825e70SFlorian Fainelli } 830ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 831ea825e70SFlorian Fainelli { 832ea825e70SFlorian Fainelli return 0; 833ea825e70SFlorian Fainelli } 834ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 835ea825e70SFlorian Fainelli 83660724d4bSFlorian Fainelli enum dsa_notifier_type { 83760724d4bSFlorian Fainelli DSA_PORT_REGISTER, 83860724d4bSFlorian Fainelli DSA_PORT_UNREGISTER, 83960724d4bSFlorian Fainelli }; 84060724d4bSFlorian Fainelli 84160724d4bSFlorian Fainelli struct dsa_notifier_info { 84260724d4bSFlorian Fainelli struct net_device *dev; 84360724d4bSFlorian Fainelli }; 84460724d4bSFlorian Fainelli 84560724d4bSFlorian Fainelli struct dsa_notifier_register_info { 84660724d4bSFlorian Fainelli struct dsa_notifier_info info; /* must be first */ 84760724d4bSFlorian Fainelli struct net_device *master; 84860724d4bSFlorian Fainelli unsigned int port_number; 84960724d4bSFlorian Fainelli unsigned int switch_number; 85060724d4bSFlorian Fainelli }; 85160724d4bSFlorian Fainelli 85260724d4bSFlorian Fainelli static inline struct net_device * 85360724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info) 85460724d4bSFlorian Fainelli { 85560724d4bSFlorian Fainelli return info->dev; 85660724d4bSFlorian Fainelli } 85760724d4bSFlorian Fainelli 85860724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 85960724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb); 86060724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb); 86160724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev, 86260724d4bSFlorian Fainelli struct dsa_notifier_info *info); 86360724d4bSFlorian Fainelli #else 86460724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb) 86560724d4bSFlorian Fainelli { 86660724d4bSFlorian Fainelli return 0; 86760724d4bSFlorian Fainelli } 86860724d4bSFlorian Fainelli 86960724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb) 87060724d4bSFlorian Fainelli { 87160724d4bSFlorian Fainelli return 0; 87260724d4bSFlorian Fainelli } 87360724d4bSFlorian Fainelli 87460724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev, 87560724d4bSFlorian Fainelli struct dsa_notifier_info *info) 87660724d4bSFlorian Fainelli { 87760724d4bSFlorian Fainelli return NOTIFY_DONE; 87860724d4bSFlorian Fainelli } 87960724d4bSFlorian Fainelli #endif 88060724d4bSFlorian Fainelli 8810a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */ 8820a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q) 8830a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v) ((v) >> 8) 8840a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff) 8850a5f14ceSFlorian Fainelli 886cf963573SFlorian Fainelli 88797a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 888cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 889cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 890cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp); 89111d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 892cf963573SFlorian Fainelli 893d3b8c049SAndrew Lunn struct dsa_tag_driver { 894d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 895d3b8c049SAndrew Lunn struct list_head list; 896d3b8c049SAndrew Lunn struct module *owner; 897d3b8c049SAndrew Lunn }; 898d3b8c049SAndrew Lunn 899d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 900d3b8c049SAndrew Lunn unsigned int count, 901d3b8c049SAndrew Lunn struct module *owner); 902d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 903d3b8c049SAndrew Lunn unsigned int count); 904d3b8c049SAndrew Lunn 905d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 906d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 907d3b8c049SAndrew Lunn { \ 908d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 909d3b8c049SAndrew Lunn THIS_MODULE); \ 910d3b8c049SAndrew Lunn return 0; \ 911d3b8c049SAndrew Lunn } \ 912d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 913d3b8c049SAndrew Lunn \ 914d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 915d3b8c049SAndrew Lunn { \ 916d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 917d3b8c049SAndrew Lunn } \ 918d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 919d3b8c049SAndrew Lunn 920d3b8c049SAndrew Lunn /** 921d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 922d3b8c049SAndrew Lunn * drivers 923d3b8c049SAndrew Lunn * @__ops_array: Array of tag driver strucutres 924d3b8c049SAndrew Lunn * 925d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 926d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 927d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 928d3b8c049SAndrew Lunn */ 929d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 930d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 931d3b8c049SAndrew Lunn 932d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 933d3b8c049SAndrew Lunn 934d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 935d3b8c049SAndrew Lunn * drivers 936d3b8c049SAndrew Lunn */ 937d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 938d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 939d3b8c049SAndrew Lunn .ops = &__ops, \ 940d3b8c049SAndrew Lunn } 941d3b8c049SAndrew Lunn 942d3b8c049SAndrew Lunn /** 943d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 944d3b8c049SAndrew Lunn * driver 945d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 946d3b8c049SAndrew Lunn * 947d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 948d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 949d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 950d3b8c049SAndrew Lunn */ 951d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 952d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 953d3b8c049SAndrew Lunn \ 954d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 955d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 956d3b8c049SAndrew Lunn }; \ 957d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 95891da11f8SLennert Buytenhek #endif 959d3b8c049SAndrew Lunn 960