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 28514574676SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 28614574676SVladimir Oltean * should be retrieved from here and not from the per-port settings. 28714574676SVladimir Oltean */ 28814574676SVladimir Oltean bool vlan_filtering; 28914574676SVladimir Oltean 290787cac3fSVladimir Oltean /* MAC PCS does not provide link state change interrupt, and requires 291787cac3fSVladimir Oltean * polling. Flag passed on to PHYLINK. 292787cac3fSVladimir Oltean */ 293787cac3fSVladimir Oltean bool pcs_poll; 294787cac3fSVladimir Oltean 295bff33f7eSVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 296bff33f7eSVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 297bff33f7eSVladimir Oltean * interfaces is needed. 298bff33f7eSVladimir Oltean */ 299bff33f7eSVladimir Oltean bool mtu_enforcement_ingress; 300bff33f7eSVladimir Oltean 301a0c02161SVivien Didelot size_t num_ports; 302c8f0b869SBen Hutchings }; 303c8f0b869SBen Hutchings 30468bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 305c38c5a66SVivien Didelot { 306b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 307d607525bSVivien Didelot struct dsa_port *dp; 308b96ddf25SVivien Didelot 309b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 310b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 311b96ddf25SVivien Didelot return dp; 312d607525bSVivien Didelot 313d607525bSVivien Didelot return NULL; 314c38c5a66SVivien Didelot } 315c38c5a66SVivien Didelot 316bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 317bff7b688SVivien Didelot { 318c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 319bff7b688SVivien Didelot } 320bff7b688SVivien Didelot 321c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 322c8f0b869SBen Hutchings { 323c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 324c8f0b869SBen Hutchings } 325c8f0b869SBen Hutchings 32660045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 32760045cbfSAndrew Lunn { 328c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 32960045cbfSAndrew Lunn } 33060045cbfSAndrew Lunn 3312b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 3326cd456f3SVivien Didelot { 333c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 3346cd456f3SVivien Didelot } 3356cd456f3SVivien Didelot 33602bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 33702bc6e54SVivien Didelot { 338c38c5a66SVivien Didelot u32 mask = 0; 339c38c5a66SVivien Didelot int p; 34002bc6e54SVivien Didelot 341c38c5a66SVivien Didelot for (p = 0; p < ds->num_ports; p++) 342c38c5a66SVivien Didelot if (dsa_is_user_port(ds, p)) 343c38c5a66SVivien Didelot mask |= BIT(p); 344c38c5a66SVivien Didelot 345c38c5a66SVivien Didelot return mask; 346c8652c83SVivien Didelot } 347c8652c83SVivien Didelot 348c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 349c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 350c5f51765SVivien Didelot { 351c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 352c5f51765SVivien Didelot struct dsa_link *dl; 353c5f51765SVivien Didelot 354c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 355c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 356c5f51765SVivien Didelot return dl->dp->index; 357c5f51765SVivien Didelot 358c5f51765SVivien Didelot return ds->num_ports; 359c5f51765SVivien Didelot } 360c5f51765SVivien Didelot 3613b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 3623b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 3633b8fac5dSVivien Didelot int port) 3643b8fac5dSVivien Didelot { 3653b8fac5dSVivien Didelot if (device == ds->index) 3663b8fac5dSVivien Didelot return port; 3673b8fac5dSVivien Didelot else 368c5f51765SVivien Didelot return dsa_routing_port(ds, device); 3693b8fac5dSVivien Didelot } 3703b8fac5dSVivien Didelot 3713b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 37207073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 373c8f0b869SBen Hutchings { 37407073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 37507073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 37607073c79SVivien Didelot 37707073c79SVivien Didelot if (!cpu_dp) 37807073c79SVivien Didelot return port; 379c8f0b869SBen Hutchings 3803b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 381c8f0b869SBen Hutchings } 382c8f0b869SBen Hutchings 383cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 384cf2d45f5SVladimir Oltean { 385cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 386cf2d45f5SVladimir Oltean 387cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 388cf2d45f5SVladimir Oltean return ds->vlan_filtering; 389cf2d45f5SVladimir Oltean else 390cf2d45f5SVladimir Oltean return dp->vlan_filtering; 391cf2d45f5SVladimir Oltean } 392cf2d45f5SVladimir Oltean 3932bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 3942bedde1aSArkadi Sharshevsky bool is_static, void *data); 3959d490b4eSVivien Didelot struct dsa_switch_ops { 3965ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 3974d776482SFlorian Fainelli int port, 3984d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 3997b314362SAndrew Lunn 400c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 4015e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 4026819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 403c8f0b869SBen Hutchings 404c8f0b869SBen Hutchings /* 405c8f0b869SBen Hutchings * Access to the switch's PHY registers. 406c8f0b869SBen Hutchings */ 407c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 408c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 409c8f0b869SBen Hutchings int regnum, u16 val); 410c8f0b869SBen Hutchings 411c8f0b869SBen Hutchings /* 412ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 413ec9436baSFlorian Fainelli */ 414ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 415ec9436baSFlorian Fainelli struct phy_device *phydev); 416ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 417ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 418ec9436baSFlorian Fainelli 419ec9436baSFlorian Fainelli /* 42011d8f3ddSFlorian Fainelli * PHYLINK integration 42111d8f3ddSFlorian Fainelli */ 42211d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 42311d8f3ddSFlorian Fainelli unsigned long *supported, 42411d8f3ddSFlorian Fainelli struct phylink_link_state *state); 42511d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 42611d8f3ddSFlorian Fainelli struct phylink_link_state *state); 42711d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 42811d8f3ddSFlorian Fainelli unsigned int mode, 42911d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 43011d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 43111d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 43211d8f3ddSFlorian Fainelli unsigned int mode, 43311d8f3ddSFlorian Fainelli phy_interface_t interface); 43411d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 43511d8f3ddSFlorian Fainelli unsigned int mode, 43611d8f3ddSFlorian Fainelli phy_interface_t interface, 4375b502a7bSRussell King struct phy_device *phydev, 4385b502a7bSRussell King int speed, int duplex, 4395b502a7bSRussell King bool tx_pause, bool rx_pause); 44011d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 44111d8f3ddSFlorian Fainelli struct phylink_link_state *state); 44211d8f3ddSFlorian Fainelli /* 443c8f0b869SBen Hutchings * ethtool hardware statistics. 444c8f0b869SBen Hutchings */ 44589f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 44689f09048SFlorian Fainelli u32 stringset, uint8_t *data); 447c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 448c8f0b869SBen Hutchings int port, uint64_t *data); 44989f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 450cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 451cf963573SFlorian Fainelli int port, uint64_t *data); 45224462549SFlorian Fainelli 45324462549SFlorian Fainelli /* 45419e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 45519e57c4eSFlorian Fainelli */ 45619e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 45719e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 45819e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 45919e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 46019e57c4eSFlorian Fainelli 46119e57c4eSFlorian Fainelli /* 4620336369dSBrandon Streiff * ethtool timestamp info 4630336369dSBrandon Streiff */ 4640336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 4650336369dSBrandon Streiff struct ethtool_ts_info *ts); 4660336369dSBrandon Streiff 4670336369dSBrandon Streiff /* 46824462549SFlorian Fainelli * Suspend and resume 46924462549SFlorian Fainelli */ 47024462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 47124462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 472b2f2af21SFlorian Fainelli 473b2f2af21SFlorian Fainelli /* 474b2f2af21SFlorian Fainelli * Port enable/disable 475b2f2af21SFlorian Fainelli */ 476b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 477b2f2af21SFlorian Fainelli struct phy_device *phy); 47875104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 4797905288fSFlorian Fainelli 4807905288fSFlorian Fainelli /* 48108f50061SVivien Didelot * Port's MAC EEE settings 4827905288fSFlorian Fainelli */ 48308f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 4847905288fSFlorian Fainelli struct ethtool_eee *e); 48508f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 4867905288fSFlorian Fainelli struct ethtool_eee *e); 48751579c3fSGuenter Roeck 4886793abb4SGuenter Roeck /* EEPROM access */ 4896793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 4906793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 4916793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 4926793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 4936793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 4943d762a0fSGuenter Roeck 4953d762a0fSGuenter Roeck /* 4963d762a0fSGuenter Roeck * Register access. 4973d762a0fSGuenter Roeck */ 4983d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 4993d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 5003d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 501b73adef6SFlorian Fainelli 502b73adef6SFlorian Fainelli /* 503b73adef6SFlorian Fainelli * Bridge integration 504b73adef6SFlorian Fainelli */ 50534a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 50671327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 507a6692754SVivien Didelot struct net_device *bridge); 508f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 509f123f2fbSVivien Didelot struct net_device *bridge); 51043c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 511b73adef6SFlorian Fainelli u8 state); 512732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 51357652796SRussell King int (*port_egress_floods)(struct dsa_switch *ds, int port, 51457652796SRussell King bool unicast, bool multicast); 5152a778e1bSVivien Didelot 5162a778e1bSVivien Didelot /* 51711149536SVivien Didelot * VLAN support 51811149536SVivien Didelot */ 519fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 520fb2dabadSVivien Didelot bool vlan_filtering); 52176e398a6SVivien Didelot int (*port_vlan_prepare)(struct dsa_switch *ds, int port, 52280e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 5234d5770b3SVivien Didelot void (*port_vlan_add)(struct dsa_switch *ds, int port, 52480e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 52576e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 52676e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 52711149536SVivien Didelot /* 5282a778e1bSVivien Didelot * Forwarding database 5292a778e1bSVivien Didelot */ 5301b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 5316c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 5322a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 5336c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 534ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 5352bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 5368df30255SVivien Didelot 5378df30255SVivien Didelot /* 5388df30255SVivien Didelot * Multicast database 5398df30255SVivien Didelot */ 5408df30255SVivien Didelot int (*port_mdb_prepare)(struct dsa_switch *ds, int port, 5413709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 5428df30255SVivien Didelot void (*port_mdb_add)(struct dsa_switch *ds, int port, 5433709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 5448df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 5458df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb); 546bf9f2648SFlorian Fainelli /* 547bf9f2648SFlorian Fainelli * RXNFC 548bf9f2648SFlorian Fainelli */ 549bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 550bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 551bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 552bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 553f50f2127SFlorian Fainelli 554f50f2127SFlorian Fainelli /* 555f50f2127SFlorian Fainelli * TC integration 556f50f2127SFlorian Fainelli */ 557ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 558ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 559ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 560ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 561ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 562ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 563f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 564f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 565f50f2127SFlorian Fainelli bool ingress); 566f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 567f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 56834297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 56934297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 57034297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 57147d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 57247d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 57340ef2c93SVivien Didelot 57440ef2c93SVivien Didelot /* 57540ef2c93SVivien Didelot * Cross-chip operations 57640ef2c93SVivien Didelot */ 577*f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 578*f66a6a69SVladimir Oltean int sw_index, int port, 579*f66a6a69SVladimir Oltean struct net_device *br); 580*f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 581*f66a6a69SVladimir Oltean int sw_index, int port, 582*f66a6a69SVladimir Oltean struct net_device *br); 5830336369dSBrandon Streiff 5840336369dSBrandon Streiff /* 5850336369dSBrandon Streiff * PTP functionality 5860336369dSBrandon Streiff */ 5870336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 5880336369dSBrandon Streiff struct ifreq *ifr); 5890336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 5900336369dSBrandon Streiff struct ifreq *ifr); 59190af1059SBrandon Streiff bool (*port_txtstamp)(struct dsa_switch *ds, int port, 59290af1059SBrandon Streiff struct sk_buff *clone, unsigned int type); 59390af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 59490af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 59597a69a0dSVladimir Oltean 5966b297524SAndrew Lunn /* Devlink parameters */ 5976b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 5986b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 5996b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 6006b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 601bfcb8132SVladimir Oltean 602bfcb8132SVladimir Oltean /* 603bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 604bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 605bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 606bfcb8132SVladimir Oltean * method needs to to do so privately. 607bfcb8132SVladimir Oltean */ 608bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 609bfcb8132SVladimir Oltean int new_mtu); 610bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 6116b297524SAndrew Lunn }; 6126b297524SAndrew Lunn 6136b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 6146b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 6156b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 6166b297524SAndrew Lunn 6176b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 6186b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6196b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 6206b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 6216b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 6226b297524SAndrew Lunn const struct devlink_param *params, 6236b297524SAndrew Lunn size_t params_count); 6246b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 6256b297524SAndrew Lunn const struct devlink_param *params, 6266b297524SAndrew Lunn size_t params_count); 6275cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 6285cd73fbdSAndrew Lunn const char *resource_name, 6295cd73fbdSAndrew Lunn u64 resource_size, 6305cd73fbdSAndrew Lunn u64 resource_id, 6315cd73fbdSAndrew Lunn u64 parent_resource_id, 6325cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 6335cd73fbdSAndrew Lunn 6345cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 6355cd73fbdSAndrew Lunn 6365cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 6375cd73fbdSAndrew Lunn u64 resource_id, 6385cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 6395cd73fbdSAndrew Lunn void *occ_get_priv); 6405cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 6415cd73fbdSAndrew Lunn u64 resource_id); 642e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 6435cd73fbdSAndrew Lunn 6446b297524SAndrew Lunn struct dsa_devlink_priv { 6456b297524SAndrew Lunn struct dsa_switch *ds; 646c8f0b869SBen Hutchings }; 647c8f0b869SBen Hutchings 648ab3d408dSFlorian Fainelli struct dsa_switch_driver { 649ab3d408dSFlorian Fainelli struct list_head list; 650a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 651ab3d408dSFlorian Fainelli }; 652ab3d408dSFlorian Fainelli 65314b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 654c8f0b869SBen Hutchings 65573a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 6569eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 657c6e970a0SAndrew Lunn { 658c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 659717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 660c6e970a0SAndrew Lunn #endif 661c6e970a0SAndrew Lunn return false; 662c6e970a0SAndrew Lunn } 663c6e970a0SAndrew Lunn 664cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb, 665cc1939e4SVladimir Oltean struct net_device *dev) 666cc1939e4SVladimir Oltean { 667cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 668cc1939e4SVladimir Oltean return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev); 669cc1939e4SVladimir Oltean #endif 670cc1939e4SVladimir Oltean return false; 671cc1939e4SVladimir Oltean } 672cc1939e4SVladimir Oltean 67383c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 67423c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 675ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 676ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 677ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 678ea825e70SFlorian Fainelli #else 679ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 680ea825e70SFlorian Fainelli { 681ea825e70SFlorian Fainelli return 0; 682ea825e70SFlorian Fainelli } 683ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 684ea825e70SFlorian Fainelli { 685ea825e70SFlorian Fainelli return 0; 686ea825e70SFlorian Fainelli } 687ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 688ea825e70SFlorian Fainelli 68960724d4bSFlorian Fainelli enum dsa_notifier_type { 69060724d4bSFlorian Fainelli DSA_PORT_REGISTER, 69160724d4bSFlorian Fainelli DSA_PORT_UNREGISTER, 69260724d4bSFlorian Fainelli }; 69360724d4bSFlorian Fainelli 69460724d4bSFlorian Fainelli struct dsa_notifier_info { 69560724d4bSFlorian Fainelli struct net_device *dev; 69660724d4bSFlorian Fainelli }; 69760724d4bSFlorian Fainelli 69860724d4bSFlorian Fainelli struct dsa_notifier_register_info { 69960724d4bSFlorian Fainelli struct dsa_notifier_info info; /* must be first */ 70060724d4bSFlorian Fainelli struct net_device *master; 70160724d4bSFlorian Fainelli unsigned int port_number; 70260724d4bSFlorian Fainelli unsigned int switch_number; 70360724d4bSFlorian Fainelli }; 70460724d4bSFlorian Fainelli 70560724d4bSFlorian Fainelli static inline struct net_device * 70660724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info) 70760724d4bSFlorian Fainelli { 70860724d4bSFlorian Fainelli return info->dev; 70960724d4bSFlorian Fainelli } 71060724d4bSFlorian Fainelli 71160724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 71260724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb); 71360724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb); 71460724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev, 71560724d4bSFlorian Fainelli struct dsa_notifier_info *info); 71660724d4bSFlorian Fainelli #else 71760724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb) 71860724d4bSFlorian Fainelli { 71960724d4bSFlorian Fainelli return 0; 72060724d4bSFlorian Fainelli } 72160724d4bSFlorian Fainelli 72260724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb) 72360724d4bSFlorian Fainelli { 72460724d4bSFlorian Fainelli return 0; 72560724d4bSFlorian Fainelli } 72660724d4bSFlorian Fainelli 72760724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev, 72860724d4bSFlorian Fainelli struct dsa_notifier_info *info) 72960724d4bSFlorian Fainelli { 73060724d4bSFlorian Fainelli return NOTIFY_DONE; 73160724d4bSFlorian Fainelli } 73260724d4bSFlorian Fainelli #endif 73360724d4bSFlorian Fainelli 7340a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */ 7350a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q) 7360a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v) ((v) >> 8) 7370a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff) 7380a5f14ceSFlorian Fainelli 739cf963573SFlorian Fainelli 74097a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 741cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 742cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 743cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp); 74411d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 745cf963573SFlorian Fainelli 746d3b8c049SAndrew Lunn struct dsa_tag_driver { 747d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 748d3b8c049SAndrew Lunn struct list_head list; 749d3b8c049SAndrew Lunn struct module *owner; 750d3b8c049SAndrew Lunn }; 751d3b8c049SAndrew Lunn 752d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 753d3b8c049SAndrew Lunn unsigned int count, 754d3b8c049SAndrew Lunn struct module *owner); 755d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 756d3b8c049SAndrew Lunn unsigned int count); 757d3b8c049SAndrew Lunn 758d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 759d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 760d3b8c049SAndrew Lunn { \ 761d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 762d3b8c049SAndrew Lunn THIS_MODULE); \ 763d3b8c049SAndrew Lunn return 0; \ 764d3b8c049SAndrew Lunn } \ 765d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 766d3b8c049SAndrew Lunn \ 767d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 768d3b8c049SAndrew Lunn { \ 769d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 770d3b8c049SAndrew Lunn } \ 771d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 772d3b8c049SAndrew Lunn 773d3b8c049SAndrew Lunn /** 774d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 775d3b8c049SAndrew Lunn * drivers 776d3b8c049SAndrew Lunn * @__ops_array: Array of tag driver strucutres 777d3b8c049SAndrew Lunn * 778d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 779d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 780d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 781d3b8c049SAndrew Lunn */ 782d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 783d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 784d3b8c049SAndrew Lunn 785d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 786d3b8c049SAndrew Lunn 787d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 788d3b8c049SAndrew Lunn * drivers 789d3b8c049SAndrew Lunn */ 790d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 791d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 792d3b8c049SAndrew Lunn .ops = &__ops, \ 793d3b8c049SAndrew Lunn } 794d3b8c049SAndrew Lunn 795d3b8c049SAndrew Lunn /** 796d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 797d3b8c049SAndrew Lunn * driver 798d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 799d3b8c049SAndrew Lunn * 800d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 801d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 802d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 803d3b8c049SAndrew Lunn */ 804d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 805d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 806d3b8c049SAndrew Lunn \ 807d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 808d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 809d3b8c049SAndrew Lunn }; \ 810d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 81191da11f8SLennert Buytenhek #endif 812d3b8c049SAndrew Lunn 813