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 470b42f033SAndrew Lunn 48ac7a04c3SFlorian Fainelli enum dsa_tag_protocol { 490b42f033SAndrew Lunn DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE, 500b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE, 510b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE, 520b42f033SAndrew Lunn DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE, 530b42f033SAndrew Lunn DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE, 540b42f033SAndrew Lunn DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE, 550b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE, 560b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE, 570b42f033SAndrew Lunn DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE, 580b42f033SAndrew Lunn DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE, 590b42f033SAndrew Lunn DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE, 600b42f033SAndrew Lunn DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE, 61f9bbe447SVladimir Oltean DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE, 62227d07a0SVladimir Oltean DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE, 63016e43a2STristram Ha DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE, 648dce89aaSVladimir Oltean DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE, 6548fda74fSOleksij Rempel DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE, 66ac7a04c3SFlorian Fainelli }; 675037d532SFlorian Fainelli 685075314eSAlexander Duyck struct packet_type; 6990af1059SBrandon Streiff struct dsa_switch; 703e8a72d1SFlorian Fainelli 7168277a2cSJohn Crispin struct dsa_device_ops { 7268277a2cSJohn Crispin struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev); 7368277a2cSJohn Crispin struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev, 7489e49506SFlorian Westphal struct packet_type *pt); 75598a9680SJohn Crispin int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto, 76598a9680SJohn Crispin int *offset); 77cc1939e4SVladimir Oltean /* Used to determine which traffic should match the DSA filter in 78cc1939e4SVladimir Oltean * eth_type_trans, and which, if any, should bypass it and be processed 79cc1939e4SVladimir Oltean * as regular on the master net device. 80cc1939e4SVladimir Oltean */ 81cc1939e4SVladimir Oltean bool (*filter)(const struct sk_buff *skb, struct net_device *dev); 82a5dd3087SAndrew Lunn unsigned int overhead; 83875138f8SAndrew Lunn const char *name; 84056eed2fSAndrew Lunn enum dsa_tag_protocol proto; 8568277a2cSJohn Crispin }; 8668277a2cSJohn Crispin 870b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-" 880b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \ 890b42f033SAndrew Lunn MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE)) 900b42f033SAndrew Lunn 91b68b0dd0SVladimir Oltean struct dsa_skb_cb { 92b68b0dd0SVladimir Oltean struct sk_buff *clone; 93b68b0dd0SVladimir Oltean }; 94b68b0dd0SVladimir Oltean 95b68b0dd0SVladimir Oltean struct __dsa_skb_cb { 96b68b0dd0SVladimir Oltean struct dsa_skb_cb cb; 97b68b0dd0SVladimir Oltean u8 priv[48 - sizeof(struct dsa_skb_cb)]; 98b68b0dd0SVladimir Oltean }; 99b68b0dd0SVladimir Oltean 100b68b0dd0SVladimir Oltean #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb)) 101b68b0dd0SVladimir Oltean 102b68b0dd0SVladimir Oltean #define DSA_SKB_CB_PRIV(skb) \ 103b68b0dd0SVladimir Oltean ((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv)) 104b68b0dd0SVladimir Oltean 105cf50dcc2SBen Hutchings struct dsa_switch_tree { 10683c0afaeSAndrew Lunn struct list_head list; 10783c0afaeSAndrew Lunn 108f515f192SVivien Didelot /* Notifier chain for switch-wide events */ 109f515f192SVivien Didelot struct raw_notifier_head nh; 110f515f192SVivien Didelot 11183c0afaeSAndrew Lunn /* Tree identifier */ 11249463b7fSVivien Didelot unsigned int index; 11383c0afaeSAndrew Lunn 11483c0afaeSAndrew Lunn /* Number of switches attached to this tree */ 11583c0afaeSAndrew Lunn struct kref refcount; 11683c0afaeSAndrew Lunn 11783c0afaeSAndrew Lunn /* Has this tree been applied to the hardware? */ 118ec15dd42SVivien Didelot bool setup; 11983c0afaeSAndrew Lunn 120cf50dcc2SBen Hutchings /* 121cf50dcc2SBen Hutchings * Configuration data for the platform device that owns 122cf50dcc2SBen Hutchings * this dsa switch tree instance. 123cf50dcc2SBen Hutchings */ 124cf50dcc2SBen Hutchings struct dsa_platform_data *pd; 125cf85d08fSLennert Buytenhek 126ab8ccae1SVivien Didelot /* List of switch ports */ 127ab8ccae1SVivien Didelot struct list_head ports; 128ab8ccae1SVivien Didelot 129c5f51765SVivien Didelot /* List of DSA links composing the routing table */ 130c5f51765SVivien Didelot struct list_head rtable; 131cf50dcc2SBen Hutchings }; 132cf50dcc2SBen Hutchings 13334297176SVladimir Oltean /* TC matchall action types */ 134f50f2127SFlorian Fainelli enum dsa_port_mall_action_type { 135f50f2127SFlorian Fainelli DSA_PORT_MALL_MIRROR, 13634297176SVladimir Oltean DSA_PORT_MALL_POLICER, 137f50f2127SFlorian Fainelli }; 138f50f2127SFlorian Fainelli 139f50f2127SFlorian Fainelli /* TC mirroring entry */ 140f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry { 141f50f2127SFlorian Fainelli u8 to_local_port; 142f50f2127SFlorian Fainelli bool ingress; 143f50f2127SFlorian Fainelli }; 144f50f2127SFlorian Fainelli 14534297176SVladimir Oltean /* TC port policer entry */ 14634297176SVladimir Oltean struct dsa_mall_policer_tc_entry { 14734297176SVladimir Oltean s64 burst; 14834297176SVladimir Oltean u64 rate_bytes_per_sec; 14934297176SVladimir Oltean }; 15034297176SVladimir Oltean 151f50f2127SFlorian Fainelli /* TC matchall entry */ 152f50f2127SFlorian Fainelli struct dsa_mall_tc_entry { 153f50f2127SFlorian Fainelli struct list_head list; 154f50f2127SFlorian Fainelli unsigned long cookie; 155f50f2127SFlorian Fainelli enum dsa_port_mall_action_type type; 156f50f2127SFlorian Fainelli union { 157f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry mirror; 15834297176SVladimir Oltean struct dsa_mall_policer_tc_entry policer; 159f50f2127SFlorian Fainelli }; 160f50f2127SFlorian Fainelli }; 161f50f2127SFlorian Fainelli 162f50f2127SFlorian Fainelli 163c8b09808SAndrew Lunn struct dsa_port { 164f8b8b1cdSVivien Didelot /* A CPU port is physically connected to a master device. 165f8b8b1cdSVivien Didelot * A user port exposed to userspace has a slave device. 166f8b8b1cdSVivien Didelot */ 167f8b8b1cdSVivien Didelot union { 168f8b8b1cdSVivien Didelot struct net_device *master; 169f8b8b1cdSVivien Didelot struct net_device *slave; 170f8b8b1cdSVivien Didelot }; 171f8b8b1cdSVivien Didelot 17215240248SVivien Didelot /* CPU port tagging operations used by master or slave devices */ 17315240248SVivien Didelot const struct dsa_device_ops *tag_ops; 17415240248SVivien Didelot 1753e41f93bSVivien Didelot /* Copies for faster access in master receive hot path */ 1763e41f93bSVivien Didelot struct dsa_switch_tree *dst; 1773e41f93bSVivien Didelot struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev, 1783e41f93bSVivien Didelot struct packet_type *pt); 179cc1939e4SVladimir Oltean bool (*filter)(const struct sk_buff *skb, struct net_device *dev); 1803e41f93bSVivien Didelot 181057cad2cSVivien Didelot enum { 182057cad2cSVivien Didelot DSA_PORT_TYPE_UNUSED = 0, 183057cad2cSVivien Didelot DSA_PORT_TYPE_CPU, 184057cad2cSVivien Didelot DSA_PORT_TYPE_DSA, 185057cad2cSVivien Didelot DSA_PORT_TYPE_USER, 186057cad2cSVivien Didelot } type; 187057cad2cSVivien Didelot 188818be848SVivien Didelot struct dsa_switch *ds; 189818be848SVivien Didelot unsigned int index; 19071e0bbdeSFlorian Fainelli const char *name; 19168b2d4a8SVivien Didelot struct dsa_port *cpu_dp; 192a2c7023fSXiaofei Shen const char *mac; 193189b0d93SAndrew Lunn struct device_node *dn; 19434a79f63SVivien Didelot unsigned int ageing_time; 19533162e9aSVladimir Oltean bool vlan_filtering; 196732f794cSVivien Didelot u8 stp_state; 197a5e9a02eSVivien Didelot struct net_device *bridge_dev; 19896567d5dSAndrew Lunn struct devlink_port devlink_port; 199aab9c406SFlorian Fainelli struct phylink *pl; 20044cc27e4SIoana Ciornei struct phylink_config pl_config; 20197a69a0dSVladimir Oltean 202ab8ccae1SVivien Didelot struct list_head list; 203ab8ccae1SVivien Didelot 20467dbb9d4SFlorian Fainelli /* 205c362beb0SVladimir Oltean * Give the switch driver somewhere to hang its per-port private data 206c362beb0SVladimir Oltean * structures (accessible from the tagger). 207c362beb0SVladimir Oltean */ 208c362beb0SVladimir Oltean void *priv; 209c362beb0SVladimir Oltean 210c362beb0SVladimir Oltean /* 21167dbb9d4SFlorian Fainelli * Original copy of the master netdev ethtool_ops 21267dbb9d4SFlorian Fainelli */ 21367dbb9d4SFlorian Fainelli const struct ethtool_ops *orig_ethtool_ops; 214da7b9e9bSFlorian Fainelli 215da7b9e9bSFlorian Fainelli /* 216da7b9e9bSFlorian Fainelli * Original copy of the master netdev net_device_ops 217da7b9e9bSFlorian Fainelli */ 218da7b9e9bSFlorian Fainelli const struct net_device_ops *orig_ndo_ops; 219fb35c60cSVivien Didelot 220fb35c60cSVivien Didelot bool setup; 221c8b09808SAndrew Lunn }; 222c8b09808SAndrew Lunn 223c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member, 224c5f51765SVivien Didelot * and no dst->rtable nor this struct dsa_link would be needed, 225c5f51765SVivien Didelot * but this would require some more complex tree walking, 226c5f51765SVivien Didelot * so keep it stupid at the moment and list them all. 227c5f51765SVivien Didelot */ 228c5f51765SVivien Didelot struct dsa_link { 229c5f51765SVivien Didelot struct dsa_port *dp; 230c5f51765SVivien Didelot struct dsa_port *link_dp; 231c5f51765SVivien Didelot struct list_head list; 232c5f51765SVivien Didelot }; 233c5f51765SVivien Didelot 234c8f0b869SBen Hutchings struct dsa_switch { 235fb35c60cSVivien Didelot bool setup; 236fb35c60cSVivien Didelot 237c33063d6SAndrew Lunn struct device *dev; 238c33063d6SAndrew Lunn 239c8f0b869SBen Hutchings /* 240c8f0b869SBen Hutchings * Parent switch tree, and switch index. 241c8f0b869SBen Hutchings */ 242c8f0b869SBen Hutchings struct dsa_switch_tree *dst; 24399feaafcSVivien Didelot unsigned int index; 244c8f0b869SBen Hutchings 245f515f192SVivien Didelot /* Listener for switch fabric events */ 246f515f192SVivien Didelot struct notifier_block nb; 247f515f192SVivien Didelot 248c8f0b869SBen Hutchings /* 2497543a6d5SAndrew Lunn * Give the switch driver somewhere to hang its private data 2507543a6d5SAndrew Lunn * structure. 2517543a6d5SAndrew Lunn */ 2527543a6d5SAndrew Lunn void *priv; 2537543a6d5SAndrew Lunn 2547543a6d5SAndrew Lunn /* 255c8f0b869SBen Hutchings * Configuration data for this switch. 256c8f0b869SBen Hutchings */ 257ff04955cSAndrew Lunn struct dsa_chip_data *cd; 258c8f0b869SBen Hutchings 259c8f0b869SBen Hutchings /* 2609d490b4eSVivien Didelot * The switch operations. 261c8f0b869SBen Hutchings */ 262a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 263c8f0b869SBen Hutchings 26466472fc0SAndrew Lunn /* 265c8f0b869SBen Hutchings * Slave mii_bus and devices for the individual ports. 266c8f0b869SBen Hutchings */ 2670d8bcdd3SFlorian Fainelli u32 phys_mii_mask; 268c8f0b869SBen Hutchings struct mii_bus *slave_mii_bus; 269a0c02161SVivien Didelot 2700f3da6afSVivien Didelot /* Ageing Time limits in msecs */ 2710f3da6afSVivien Didelot unsigned int ageing_time_min; 2720f3da6afSVivien Didelot unsigned int ageing_time_max; 2730f3da6afSVivien Didelot 27496567d5dSAndrew Lunn /* devlink used to represent this switch device */ 27596567d5dSAndrew Lunn struct devlink *devlink; 27696567d5dSAndrew Lunn 27755199df6SFlorian Fainelli /* Number of switch port queues */ 27855199df6SFlorian Fainelli unsigned int num_tx_queues; 27955199df6SFlorian Fainelli 2808f5d16f6SVladimir Oltean /* Disallow bridge core from requesting different VLAN awareness 2818f5d16f6SVladimir Oltean * settings on ports if not hardware-supported 2828f5d16f6SVladimir Oltean */ 2838f5d16f6SVladimir Oltean bool vlan_filtering_is_global; 2848f5d16f6SVladimir Oltean 28554a0ed0dSRussell King /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 28654a0ed0dSRussell King * that have vlan_filtering=0. All drivers should ideally set this (and 28754a0ed0dSRussell King * then the option would get removed), but it is unknown whether this 28854a0ed0dSRussell King * would break things or not. 28954a0ed0dSRussell King */ 29054a0ed0dSRussell King bool configure_vlan_while_not_filtering; 29154a0ed0dSRussell King 29214574676SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 29314574676SVladimir Oltean * should be retrieved from here and not from the per-port settings. 29414574676SVladimir Oltean */ 29514574676SVladimir Oltean bool vlan_filtering; 29614574676SVladimir Oltean 297787cac3fSVladimir Oltean /* MAC PCS does not provide link state change interrupt, and requires 298787cac3fSVladimir Oltean * polling. Flag passed on to PHYLINK. 299787cac3fSVladimir Oltean */ 300787cac3fSVladimir Oltean bool pcs_poll; 301787cac3fSVladimir Oltean 302bff33f7eSVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 303bff33f7eSVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 304bff33f7eSVladimir Oltean * interfaces is needed. 305bff33f7eSVladimir Oltean */ 306bff33f7eSVladimir Oltean bool mtu_enforcement_ingress; 307bff33f7eSVladimir Oltean 308a0c02161SVivien Didelot size_t num_ports; 309c8f0b869SBen Hutchings }; 310c8f0b869SBen Hutchings 31168bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 312c38c5a66SVivien Didelot { 313b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 314d607525bSVivien Didelot struct dsa_port *dp; 315b96ddf25SVivien Didelot 316b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 317b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 318b96ddf25SVivien Didelot return dp; 319d607525bSVivien Didelot 320d607525bSVivien Didelot return NULL; 321c38c5a66SVivien Didelot } 322c38c5a66SVivien Didelot 323bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 324bff7b688SVivien Didelot { 325c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 326bff7b688SVivien Didelot } 327bff7b688SVivien Didelot 328c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 329c8f0b869SBen Hutchings { 330c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 331c8f0b869SBen Hutchings } 332c8f0b869SBen Hutchings 33360045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 33460045cbfSAndrew Lunn { 335c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 33660045cbfSAndrew Lunn } 33760045cbfSAndrew Lunn 3382b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 3396cd456f3SVivien Didelot { 340c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 3416cd456f3SVivien Didelot } 3426cd456f3SVivien Didelot 34302bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 34402bc6e54SVivien Didelot { 345c38c5a66SVivien Didelot u32 mask = 0; 346c38c5a66SVivien Didelot int p; 34702bc6e54SVivien Didelot 348c38c5a66SVivien Didelot for (p = 0; p < ds->num_ports; p++) 349c38c5a66SVivien Didelot if (dsa_is_user_port(ds, p)) 350c38c5a66SVivien Didelot mask |= BIT(p); 351c38c5a66SVivien Didelot 352c38c5a66SVivien Didelot return mask; 353c8652c83SVivien Didelot } 354c8652c83SVivien Didelot 355c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 356c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 357c5f51765SVivien Didelot { 358c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 359c5f51765SVivien Didelot struct dsa_link *dl; 360c5f51765SVivien Didelot 361c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 362c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 363c5f51765SVivien Didelot return dl->dp->index; 364c5f51765SVivien Didelot 365c5f51765SVivien Didelot return ds->num_ports; 366c5f51765SVivien Didelot } 367c5f51765SVivien Didelot 3683b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 3693b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 3703b8fac5dSVivien Didelot int port) 3713b8fac5dSVivien Didelot { 3723b8fac5dSVivien Didelot if (device == ds->index) 3733b8fac5dSVivien Didelot return port; 3743b8fac5dSVivien Didelot else 375c5f51765SVivien Didelot return dsa_routing_port(ds, device); 3763b8fac5dSVivien Didelot } 3773b8fac5dSVivien Didelot 3783b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 37907073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 380c8f0b869SBen Hutchings { 38107073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 38207073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 38307073c79SVivien Didelot 38407073c79SVivien Didelot if (!cpu_dp) 38507073c79SVivien Didelot return port; 386c8f0b869SBen Hutchings 3873b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 388c8f0b869SBen Hutchings } 389c8f0b869SBen Hutchings 390cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 391cf2d45f5SVladimir Oltean { 392cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 393cf2d45f5SVladimir Oltean 394cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 395cf2d45f5SVladimir Oltean return ds->vlan_filtering; 396cf2d45f5SVladimir Oltean else 397cf2d45f5SVladimir Oltean return dp->vlan_filtering; 398cf2d45f5SVladimir Oltean } 399cf2d45f5SVladimir Oltean 4002bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 4012bedde1aSArkadi Sharshevsky bool is_static, void *data); 4029d490b4eSVivien Didelot struct dsa_switch_ops { 4035ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 4044d776482SFlorian Fainelli int port, 4054d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 4067b314362SAndrew Lunn 407c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 4085e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 4096819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 410c8f0b869SBen Hutchings 411c8f0b869SBen Hutchings /* 412c8f0b869SBen Hutchings * Access to the switch's PHY registers. 413c8f0b869SBen Hutchings */ 414c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 415c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 416c8f0b869SBen Hutchings int regnum, u16 val); 417c8f0b869SBen Hutchings 418c8f0b869SBen Hutchings /* 419ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 420ec9436baSFlorian Fainelli */ 421ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 422ec9436baSFlorian Fainelli struct phy_device *phydev); 423ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 424ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 425ec9436baSFlorian Fainelli 426ec9436baSFlorian Fainelli /* 42711d8f3ddSFlorian Fainelli * PHYLINK integration 42811d8f3ddSFlorian Fainelli */ 42911d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 43011d8f3ddSFlorian Fainelli unsigned long *supported, 43111d8f3ddSFlorian Fainelli struct phylink_link_state *state); 43211d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 43311d8f3ddSFlorian Fainelli struct phylink_link_state *state); 43411d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 43511d8f3ddSFlorian Fainelli unsigned int mode, 43611d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 43711d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 43811d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 43911d8f3ddSFlorian Fainelli unsigned int mode, 44011d8f3ddSFlorian Fainelli phy_interface_t interface); 44111d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 44211d8f3ddSFlorian Fainelli unsigned int mode, 44311d8f3ddSFlorian Fainelli phy_interface_t interface, 4445b502a7bSRussell King struct phy_device *phydev, 4455b502a7bSRussell King int speed, int duplex, 4465b502a7bSRussell King bool tx_pause, bool rx_pause); 44711d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 44811d8f3ddSFlorian Fainelli struct phylink_link_state *state); 44911d8f3ddSFlorian Fainelli /* 450c8f0b869SBen Hutchings * ethtool hardware statistics. 451c8f0b869SBen Hutchings */ 45289f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 45389f09048SFlorian Fainelli u32 stringset, uint8_t *data); 454c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 455c8f0b869SBen Hutchings int port, uint64_t *data); 45689f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 457cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 458cf963573SFlorian Fainelli int port, uint64_t *data); 45924462549SFlorian Fainelli 46024462549SFlorian Fainelli /* 46119e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 46219e57c4eSFlorian Fainelli */ 46319e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 46419e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 46519e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 46619e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 46719e57c4eSFlorian Fainelli 46819e57c4eSFlorian Fainelli /* 4690336369dSBrandon Streiff * ethtool timestamp info 4700336369dSBrandon Streiff */ 4710336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 4720336369dSBrandon Streiff struct ethtool_ts_info *ts); 4730336369dSBrandon Streiff 4740336369dSBrandon Streiff /* 47524462549SFlorian Fainelli * Suspend and resume 47624462549SFlorian Fainelli */ 47724462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 47824462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 479b2f2af21SFlorian Fainelli 480b2f2af21SFlorian Fainelli /* 481b2f2af21SFlorian Fainelli * Port enable/disable 482b2f2af21SFlorian Fainelli */ 483b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 484b2f2af21SFlorian Fainelli struct phy_device *phy); 48575104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 4867905288fSFlorian Fainelli 4877905288fSFlorian Fainelli /* 48808f50061SVivien Didelot * Port's MAC EEE settings 4897905288fSFlorian Fainelli */ 49008f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 4917905288fSFlorian Fainelli struct ethtool_eee *e); 49208f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 4937905288fSFlorian Fainelli struct ethtool_eee *e); 49451579c3fSGuenter Roeck 4956793abb4SGuenter Roeck /* EEPROM access */ 4966793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 4976793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 4986793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 4996793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 5006793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 5013d762a0fSGuenter Roeck 5023d762a0fSGuenter Roeck /* 5033d762a0fSGuenter Roeck * Register access. 5043d762a0fSGuenter Roeck */ 5053d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 5063d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 5073d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 508b73adef6SFlorian Fainelli 509b73adef6SFlorian Fainelli /* 510b73adef6SFlorian Fainelli * Bridge integration 511b73adef6SFlorian Fainelli */ 51234a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 51371327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 514a6692754SVivien Didelot struct net_device *bridge); 515f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 516f123f2fbSVivien Didelot struct net_device *bridge); 51743c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 518b73adef6SFlorian Fainelli u8 state); 519732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 52057652796SRussell King int (*port_egress_floods)(struct dsa_switch *ds, int port, 52157652796SRussell King bool unicast, bool multicast); 5222a778e1bSVivien Didelot 5232a778e1bSVivien Didelot /* 52411149536SVivien Didelot * VLAN support 52511149536SVivien Didelot */ 526fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 527fb2dabadSVivien Didelot bool vlan_filtering); 52876e398a6SVivien Didelot int (*port_vlan_prepare)(struct dsa_switch *ds, int port, 52980e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 5304d5770b3SVivien Didelot void (*port_vlan_add)(struct dsa_switch *ds, int port, 53180e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 53276e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 53376e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 53411149536SVivien Didelot /* 5352a778e1bSVivien Didelot * Forwarding database 5362a778e1bSVivien Didelot */ 5371b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 5386c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 5392a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 5406c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 541ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 5422bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 5438df30255SVivien Didelot 5448df30255SVivien Didelot /* 5458df30255SVivien Didelot * Multicast database 5468df30255SVivien Didelot */ 5478df30255SVivien Didelot int (*port_mdb_prepare)(struct dsa_switch *ds, int port, 5483709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 5498df30255SVivien Didelot void (*port_mdb_add)(struct dsa_switch *ds, int port, 5503709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 5518df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 5528df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb); 553bf9f2648SFlorian Fainelli /* 554bf9f2648SFlorian Fainelli * RXNFC 555bf9f2648SFlorian Fainelli */ 556bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 557bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 558bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 559bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 560f50f2127SFlorian Fainelli 561f50f2127SFlorian Fainelli /* 562f50f2127SFlorian Fainelli * TC integration 563f50f2127SFlorian Fainelli */ 564ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 565ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 566ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 567ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 568ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 569ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 570f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 571f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 572f50f2127SFlorian Fainelli bool ingress); 573f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 574f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 57534297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 57634297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 57734297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 57847d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 57947d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 58040ef2c93SVivien Didelot 58140ef2c93SVivien Didelot /* 58240ef2c93SVivien Didelot * Cross-chip operations 58340ef2c93SVivien Didelot */ 584f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 585f66a6a69SVladimir Oltean int sw_index, int port, 586f66a6a69SVladimir Oltean struct net_device *br); 587f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 588f66a6a69SVladimir Oltean int sw_index, int port, 589f66a6a69SVladimir Oltean struct net_device *br); 5900336369dSBrandon Streiff 5910336369dSBrandon Streiff /* 5920336369dSBrandon Streiff * PTP functionality 5930336369dSBrandon Streiff */ 5940336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 5950336369dSBrandon Streiff struct ifreq *ifr); 5960336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 5970336369dSBrandon Streiff struct ifreq *ifr); 59890af1059SBrandon Streiff bool (*port_txtstamp)(struct dsa_switch *ds, int port, 59990af1059SBrandon Streiff struct sk_buff *clone, unsigned int type); 60090af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 60190af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 60297a69a0dSVladimir Oltean 6036b297524SAndrew Lunn /* Devlink parameters */ 6046b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 6056b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6066b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 6076b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 608bfcb8132SVladimir Oltean 609bfcb8132SVladimir Oltean /* 610bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 611bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 612bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 613bfcb8132SVladimir Oltean * method needs to to do so privately. 614bfcb8132SVladimir Oltean */ 615bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 616bfcb8132SVladimir Oltean int new_mtu); 617bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 6186b297524SAndrew Lunn }; 6196b297524SAndrew Lunn 6206b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 6216b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 6226b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 6236b297524SAndrew Lunn 6246b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 6256b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6266b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 6276b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6286b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 6296b297524SAndrew Lunn const struct devlink_param *params, 6306b297524SAndrew Lunn size_t params_count); 6316b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 6326b297524SAndrew Lunn const struct devlink_param *params, 6336b297524SAndrew Lunn size_t params_count); 6345cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 6355cd73fbdSAndrew Lunn const char *resource_name, 6365cd73fbdSAndrew Lunn u64 resource_size, 6375cd73fbdSAndrew Lunn u64 resource_id, 6385cd73fbdSAndrew Lunn u64 parent_resource_id, 6395cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 6405cd73fbdSAndrew Lunn 6415cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 6425cd73fbdSAndrew Lunn 6435cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 6445cd73fbdSAndrew Lunn u64 resource_id, 6455cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 6465cd73fbdSAndrew Lunn void *occ_get_priv); 6475cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 6485cd73fbdSAndrew Lunn u64 resource_id); 649e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 6505cd73fbdSAndrew Lunn 6516b297524SAndrew Lunn struct dsa_devlink_priv { 6526b297524SAndrew Lunn struct dsa_switch *ds; 653c8f0b869SBen Hutchings }; 654c8f0b869SBen Hutchings 655ab3d408dSFlorian Fainelli struct dsa_switch_driver { 656ab3d408dSFlorian Fainelli struct list_head list; 657a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 658ab3d408dSFlorian Fainelli }; 659ab3d408dSFlorian Fainelli 66014b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 661c8f0b869SBen Hutchings 66273a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 6639eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 664c6e970a0SAndrew Lunn { 665c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 666717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 667c6e970a0SAndrew Lunn #endif 668c6e970a0SAndrew Lunn return false; 669c6e970a0SAndrew Lunn } 670c6e970a0SAndrew Lunn 671cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb, 672cc1939e4SVladimir Oltean struct net_device *dev) 673cc1939e4SVladimir Oltean { 674cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 675cc1939e4SVladimir Oltean return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev); 676cc1939e4SVladimir Oltean #endif 677cc1939e4SVladimir Oltean return false; 678cc1939e4SVladimir Oltean } 679cc1939e4SVladimir Oltean 68083c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 68123c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 6823b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 683ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 684ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 685ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 686ea825e70SFlorian Fainelli #else 687ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 688ea825e70SFlorian Fainelli { 689ea825e70SFlorian Fainelli return 0; 690ea825e70SFlorian Fainelli } 691ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 692ea825e70SFlorian Fainelli { 693ea825e70SFlorian Fainelli return 0; 694ea825e70SFlorian Fainelli } 695ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 696ea825e70SFlorian Fainelli 69760724d4bSFlorian Fainelli enum dsa_notifier_type { 69860724d4bSFlorian Fainelli DSA_PORT_REGISTER, 69960724d4bSFlorian Fainelli DSA_PORT_UNREGISTER, 70060724d4bSFlorian Fainelli }; 70160724d4bSFlorian Fainelli 70260724d4bSFlorian Fainelli struct dsa_notifier_info { 70360724d4bSFlorian Fainelli struct net_device *dev; 70460724d4bSFlorian Fainelli }; 70560724d4bSFlorian Fainelli 70660724d4bSFlorian Fainelli struct dsa_notifier_register_info { 70760724d4bSFlorian Fainelli struct dsa_notifier_info info; /* must be first */ 70860724d4bSFlorian Fainelli struct net_device *master; 70960724d4bSFlorian Fainelli unsigned int port_number; 71060724d4bSFlorian Fainelli unsigned int switch_number; 71160724d4bSFlorian Fainelli }; 71260724d4bSFlorian Fainelli 71360724d4bSFlorian Fainelli static inline struct net_device * 71460724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info) 71560724d4bSFlorian Fainelli { 71660724d4bSFlorian Fainelli return info->dev; 71760724d4bSFlorian Fainelli } 71860724d4bSFlorian Fainelli 71960724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 72060724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb); 72160724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb); 72260724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev, 72360724d4bSFlorian Fainelli struct dsa_notifier_info *info); 72460724d4bSFlorian Fainelli #else 72560724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb) 72660724d4bSFlorian Fainelli { 72760724d4bSFlorian Fainelli return 0; 72860724d4bSFlorian Fainelli } 72960724d4bSFlorian Fainelli 73060724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb) 73160724d4bSFlorian Fainelli { 73260724d4bSFlorian Fainelli return 0; 73360724d4bSFlorian Fainelli } 73460724d4bSFlorian Fainelli 73560724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev, 73660724d4bSFlorian Fainelli struct dsa_notifier_info *info) 73760724d4bSFlorian Fainelli { 73860724d4bSFlorian Fainelli return NOTIFY_DONE; 73960724d4bSFlorian Fainelli } 74060724d4bSFlorian Fainelli #endif 74160724d4bSFlorian Fainelli 7420a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */ 7430a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q) 7440a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v) ((v) >> 8) 7450a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff) 7460a5f14ceSFlorian Fainelli 747cf963573SFlorian Fainelli 74897a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 749cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 750cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 751cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp); 75211d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 753cf963573SFlorian Fainelli 754d3b8c049SAndrew Lunn struct dsa_tag_driver { 755d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 756d3b8c049SAndrew Lunn struct list_head list; 757d3b8c049SAndrew Lunn struct module *owner; 758d3b8c049SAndrew Lunn }; 759d3b8c049SAndrew Lunn 760d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 761d3b8c049SAndrew Lunn unsigned int count, 762d3b8c049SAndrew Lunn struct module *owner); 763d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 764d3b8c049SAndrew Lunn unsigned int count); 765d3b8c049SAndrew Lunn 766d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 767d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 768d3b8c049SAndrew Lunn { \ 769d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 770d3b8c049SAndrew Lunn THIS_MODULE); \ 771d3b8c049SAndrew Lunn return 0; \ 772d3b8c049SAndrew Lunn } \ 773d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 774d3b8c049SAndrew Lunn \ 775d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 776d3b8c049SAndrew Lunn { \ 777d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 778d3b8c049SAndrew Lunn } \ 779d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 780d3b8c049SAndrew Lunn 781d3b8c049SAndrew Lunn /** 782d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 783d3b8c049SAndrew Lunn * drivers 784d3b8c049SAndrew Lunn * @__ops_array: Array of tag driver strucutres 785d3b8c049SAndrew Lunn * 786d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 787d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 788d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 789d3b8c049SAndrew Lunn */ 790d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 791d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 792d3b8c049SAndrew Lunn 793d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 794d3b8c049SAndrew Lunn 795d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 796d3b8c049SAndrew Lunn * drivers 797d3b8c049SAndrew Lunn */ 798d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 799d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 800d3b8c049SAndrew Lunn .ops = &__ops, \ 801d3b8c049SAndrew Lunn } 802d3b8c049SAndrew Lunn 803d3b8c049SAndrew Lunn /** 804d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 805d3b8c049SAndrew Lunn * driver 806d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 807d3b8c049SAndrew Lunn * 808d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 809d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 810d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 811d3b8c049SAndrew Lunn */ 812d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 813d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 814d3b8c049SAndrew Lunn \ 815d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 816d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 817d3b8c049SAndrew Lunn }; \ 818d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 81991da11f8SLennert Buytenhek #endif 820d3b8c049SAndrew Lunn 821