12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 291da11f8SLennert Buytenhek /* 391da11f8SLennert Buytenhek * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips 4e84665c9SLennert Buytenhek * Copyright (c) 2008-2009 Marvell Semiconductor 591da11f8SLennert Buytenhek */ 691da11f8SLennert Buytenhek 791da11f8SLennert Buytenhek #ifndef __LINUX_NET_DSA_H 891da11f8SLennert Buytenhek #define __LINUX_NET_DSA_H 991da11f8SLennert Buytenhek 104d56a29fSRussell King #include <linux/if.h> 11ea1f51beSAxel Lin #include <linux/if_ether.h> 12c8f0b869SBen Hutchings #include <linux/list.h> 13f515f192SVivien Didelot #include <linux/notifier.h> 14cf50dcc2SBen Hutchings #include <linux/timer.h> 15cf50dcc2SBen Hutchings #include <linux/workqueue.h> 16fa981d9aSFlorian Fainelli #include <linux/of.h> 17a2820543SFlorian Fainelli #include <linux/ethtool.h> 180336369dSBrandon Streiff #include <linux/net_tstamp.h> 1911d8f3ddSFlorian Fainelli #include <linux/phy.h> 20ecfc9372SFlorian Fainelli #include <linux/platform_data/dsa.h> 2144cc27e4SIoana Ciornei #include <linux/phylink.h> 2296567d5dSAndrew Lunn #include <net/devlink.h> 23f0c24ccfSVivien Didelot #include <net/switchdev.h> 24cf50dcc2SBen Hutchings 25f50f2127SFlorian Fainelli struct tc_action; 264d56a29fSRussell King struct phy_device; 274d56a29fSRussell King struct fixed_phy_status; 2811d8f3ddSFlorian Fainelli struct phylink_link_state; 29f50f2127SFlorian Fainelli 300b42f033SAndrew Lunn #define DSA_TAG_PROTO_NONE_VALUE 0 310b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_VALUE 1 320b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE 2 330b42f033SAndrew Lunn #define DSA_TAG_PROTO_DSA_VALUE 3 340b42f033SAndrew Lunn #define DSA_TAG_PROTO_EDSA_VALUE 4 350b42f033SAndrew Lunn #define DSA_TAG_PROTO_GSWIP_VALUE 5 360b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9477_VALUE 6 370b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9893_VALUE 7 380b42f033SAndrew Lunn #define DSA_TAG_PROTO_LAN9303_VALUE 8 390b42f033SAndrew Lunn #define DSA_TAG_PROTO_MTK_VALUE 9 400b42f033SAndrew Lunn #define DSA_TAG_PROTO_QCA_VALUE 10 410b42f033SAndrew Lunn #define DSA_TAG_PROTO_TRAILER_VALUE 11 42f9bbe447SVladimir Oltean #define DSA_TAG_PROTO_8021Q_VALUE 12 43227d07a0SVladimir Oltean #define DSA_TAG_PROTO_SJA1105_VALUE 13 44016e43a2STristram Ha #define DSA_TAG_PROTO_KSZ8795_VALUE 14 458dce89aaSVladimir Oltean #define DSA_TAG_PROTO_OCELOT_VALUE 15 4648fda74fSOleksij Rempel #define DSA_TAG_PROTO_AR9331_VALUE 16 47efd7fe68SLinus Walleij #define DSA_TAG_PROTO_RTL4_A_VALUE 17 4801ef09caSKurt Kanzenbach #define DSA_TAG_PROTO_HELLCREEK_VALUE 18 4954a52823SGeorge McCollister #define DSA_TAG_PROTO_XRS700X_VALUE 19 507c83a7c5SVladimir Oltean #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE 20 517c4bb540SVladimir Oltean #define DSA_TAG_PROTO_SEVILLE_VALUE 21 52964dbf18SÁlvaro Fernández Rojas #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE 22 534913b8ebSVladimir Oltean #define DSA_TAG_PROTO_SJA1110_VALUE 23 541521d5adSAlvin Šipraga #define DSA_TAG_PROTO_RTL8_4_VALUE 24 55cd87fecdSLuiz Angelo Daros de Luca #define DSA_TAG_PROTO_RTL8_4T_VALUE 25 560b42f033SAndrew Lunn 57ac7a04c3SFlorian Fainelli enum dsa_tag_protocol { 580b42f033SAndrew Lunn DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE, 590b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE, 60964dbf18SÁlvaro Fernández Rojas DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE, 610b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE, 620b42f033SAndrew Lunn DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE, 630b42f033SAndrew Lunn DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE, 640b42f033SAndrew Lunn DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE, 650b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE, 660b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE, 670b42f033SAndrew Lunn DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE, 680b42f033SAndrew Lunn DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE, 690b42f033SAndrew Lunn DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE, 700b42f033SAndrew Lunn DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE, 71f9bbe447SVladimir Oltean DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE, 72227d07a0SVladimir Oltean DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE, 73016e43a2STristram Ha DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE, 748dce89aaSVladimir Oltean DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE, 7548fda74fSOleksij Rempel DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE, 76efd7fe68SLinus Walleij DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE, 7701ef09caSKurt Kanzenbach DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE, 7854a52823SGeorge McCollister DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE, 797c83a7c5SVladimir Oltean DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE, 807c4bb540SVladimir Oltean DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE, 814913b8ebSVladimir Oltean DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE, 821521d5adSAlvin Šipraga DSA_TAG_PROTO_RTL8_4 = DSA_TAG_PROTO_RTL8_4_VALUE, 83cd87fecdSLuiz Angelo Daros de Luca DSA_TAG_PROTO_RTL8_4T = DSA_TAG_PROTO_RTL8_4T_VALUE, 84ac7a04c3SFlorian Fainelli }; 855037d532SFlorian Fainelli 8690af1059SBrandon Streiff struct dsa_switch; 873e8a72d1SFlorian Fainelli 8868277a2cSJohn Crispin struct dsa_device_ops { 8968277a2cSJohn Crispin struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev); 9029a097b7SVladimir Oltean struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev); 912e8cb1b3SVladimir Oltean void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto, 92598a9680SJohn Crispin int *offset); 937f297314SVladimir Oltean int (*connect)(struct dsa_switch *ds); 947f297314SVladimir Oltean void (*disconnect)(struct dsa_switch *ds); 954e500251SVladimir Oltean unsigned int needed_headroom; 964e500251SVladimir Oltean unsigned int needed_tailroom; 97875138f8SAndrew Lunn const char *name; 98056eed2fSAndrew Lunn enum dsa_tag_protocol proto; 99c3975400SVladimir Oltean /* Some tagging protocols either mangle or shift the destination MAC 100c3975400SVladimir Oltean * address, in which case the DSA master would drop packets on ingress 101c3975400SVladimir Oltean * if what it understands out of the destination MAC address is not in 102c3975400SVladimir Oltean * its RX filter. 103c3975400SVladimir Oltean */ 104c3975400SVladimir Oltean bool promisc_on_master; 10568277a2cSJohn Crispin }; 10668277a2cSJohn Crispin 1074cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the 1084cfab356SFlorian Fainelli * DSA layer on top of the DSA CPU/management net_device instance. This is 1094cfab356SFlorian Fainelli * used by the core net_device layer while calling various net_device_ops 1104cfab356SFlorian Fainelli * function pointers. 1114cfab356SFlorian Fainelli */ 1124cfab356SFlorian Fainelli struct dsa_netdevice_ops { 113a7605370SArnd Bergmann int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr, 1144cfab356SFlorian Fainelli int cmd); 1154cfab356SFlorian Fainelli }; 1164cfab356SFlorian Fainelli 1170b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-" 1180b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \ 1190b42f033SAndrew Lunn MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE)) 1200b42f033SAndrew Lunn 121dedd6a00SVladimir Oltean struct dsa_lag { 122dedd6a00SVladimir Oltean struct net_device *dev; 123dedd6a00SVladimir Oltean unsigned int id; 124e212fa7cSVladimir Oltean struct mutex fdb_lock; 125e212fa7cSVladimir Oltean struct list_head fdbs; 126dedd6a00SVladimir Oltean refcount_t refcount; 127dedd6a00SVladimir Oltean }; 128dedd6a00SVladimir Oltean 129cf50dcc2SBen Hutchings struct dsa_switch_tree { 13083c0afaeSAndrew Lunn struct list_head list; 13183c0afaeSAndrew Lunn 132b035c88cSVladimir Oltean /* List of switch ports */ 133b035c88cSVladimir Oltean struct list_head ports; 134b035c88cSVladimir Oltean 135f515f192SVivien Didelot /* Notifier chain for switch-wide events */ 136f515f192SVivien Didelot struct raw_notifier_head nh; 137f515f192SVivien Didelot 13883c0afaeSAndrew Lunn /* Tree identifier */ 13949463b7fSVivien Didelot unsigned int index; 14083c0afaeSAndrew Lunn 14183c0afaeSAndrew Lunn /* Number of switches attached to this tree */ 14283c0afaeSAndrew Lunn struct kref refcount; 14383c0afaeSAndrew Lunn 144b035c88cSVladimir Oltean /* Maps offloaded LAG netdevs to a zero-based linear ID for 145b035c88cSVladimir Oltean * drivers that need it. 146b035c88cSVladimir Oltean */ 147dedd6a00SVladimir Oltean struct dsa_lag **lags; 148b035c88cSVladimir Oltean 149357f203bSVladimir Oltean /* Tagging protocol operations */ 150357f203bSVladimir Oltean const struct dsa_device_ops *tag_ops; 151357f203bSVladimir Oltean 152deff7107STobias Waldekranz /* Default tagging protocol preferred by the switches in this 153deff7107STobias Waldekranz * tree. 154deff7107STobias Waldekranz */ 155deff7107STobias Waldekranz enum dsa_tag_protocol default_proto; 156deff7107STobias Waldekranz 1574b026e82SVladimir Oltean /* Has this tree been applied to the hardware? */ 1584b026e82SVladimir Oltean bool setup; 1594b026e82SVladimir Oltean 160cf50dcc2SBen Hutchings /* 161cf50dcc2SBen Hutchings * Configuration data for the platform device that owns 162cf50dcc2SBen Hutchings * this dsa switch tree instance. 163cf50dcc2SBen Hutchings */ 164cf50dcc2SBen Hutchings struct dsa_platform_data *pd; 165cf85d08fSLennert Buytenhek 166c5f51765SVivien Didelot /* List of DSA links composing the routing table */ 167c5f51765SVivien Didelot struct list_head rtable; 168058102a6STobias Waldekranz 169b035c88cSVladimir Oltean /* Length of "lags" array */ 170058102a6STobias Waldekranz unsigned int lags_len; 1715b22d366SVladimir Oltean 1725b22d366SVladimir Oltean /* Track the largest switch index within a tree */ 1735b22d366SVladimir Oltean unsigned int last_switch; 174cf50dcc2SBen Hutchings }; 175cf50dcc2SBen Hutchings 1763d4a0a2aSVladimir Oltean /* LAG IDs are one-based, the dst->lags array is zero-based */ 177058102a6STobias Waldekranz #define dsa_lags_foreach_id(_id, _dst) \ 1783d4a0a2aSVladimir Oltean for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \ 1793d4a0a2aSVladimir Oltean if ((_dst)->lags[(_id) - 1]) 180058102a6STobias Waldekranz 181058102a6STobias Waldekranz #define dsa_lag_foreach_port(_dp, _dst, _lag) \ 182058102a6STobias Waldekranz list_for_each_entry((_dp), &(_dst)->ports, list) \ 183dedd6a00SVladimir Oltean if (dsa_port_offloads_lag((_dp), (_lag))) 184058102a6STobias Waldekranz 18518596f50SGeorge McCollister #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \ 18618596f50SGeorge McCollister list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 18718596f50SGeorge McCollister if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr)) 18818596f50SGeorge McCollister 189dedd6a00SVladimir Oltean static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst, 190058102a6STobias Waldekranz unsigned int id) 191058102a6STobias Waldekranz { 1923d4a0a2aSVladimir Oltean /* DSA LAG IDs are one-based, dst->lags is zero-based */ 1933d4a0a2aSVladimir Oltean return dst->lags[id - 1]; 194058102a6STobias Waldekranz } 195058102a6STobias Waldekranz 196058102a6STobias Waldekranz static inline int dsa_lag_id(struct dsa_switch_tree *dst, 19746a76724SVladimir Oltean struct net_device *lag_dev) 198058102a6STobias Waldekranz { 199058102a6STobias Waldekranz unsigned int id; 200058102a6STobias Waldekranz 201058102a6STobias Waldekranz dsa_lags_foreach_id(id, dst) { 202dedd6a00SVladimir Oltean struct dsa_lag *lag = dsa_lag_by_id(dst, id); 203dedd6a00SVladimir Oltean 204dedd6a00SVladimir Oltean if (lag->dev == lag_dev) 205dedd6a00SVladimir Oltean return lag->id; 206058102a6STobias Waldekranz } 207058102a6STobias Waldekranz 208058102a6STobias Waldekranz return -ENODEV; 209058102a6STobias Waldekranz } 210058102a6STobias Waldekranz 21134297176SVladimir Oltean /* TC matchall action types */ 212f50f2127SFlorian Fainelli enum dsa_port_mall_action_type { 213f50f2127SFlorian Fainelli DSA_PORT_MALL_MIRROR, 21434297176SVladimir Oltean DSA_PORT_MALL_POLICER, 215f50f2127SFlorian Fainelli }; 216f50f2127SFlorian Fainelli 217f50f2127SFlorian Fainelli /* TC mirroring entry */ 218f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry { 219f50f2127SFlorian Fainelli u8 to_local_port; 220f50f2127SFlorian Fainelli bool ingress; 221f50f2127SFlorian Fainelli }; 222f50f2127SFlorian Fainelli 22334297176SVladimir Oltean /* TC port policer entry */ 22434297176SVladimir Oltean struct dsa_mall_policer_tc_entry { 2255f035af7SPo Liu u32 burst; 22634297176SVladimir Oltean u64 rate_bytes_per_sec; 22734297176SVladimir Oltean }; 22834297176SVladimir Oltean 229f50f2127SFlorian Fainelli /* TC matchall entry */ 230f50f2127SFlorian Fainelli struct dsa_mall_tc_entry { 231f50f2127SFlorian Fainelli struct list_head list; 232f50f2127SFlorian Fainelli unsigned long cookie; 233f50f2127SFlorian Fainelli enum dsa_port_mall_action_type type; 234f50f2127SFlorian Fainelli union { 235f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry mirror; 23634297176SVladimir Oltean struct dsa_mall_policer_tc_entry policer; 237f50f2127SFlorian Fainelli }; 238f50f2127SFlorian Fainelli }; 239f50f2127SFlorian Fainelli 240d3eed0e5SVladimir Oltean struct dsa_bridge { 241d3eed0e5SVladimir Oltean struct net_device *dev; 242d3eed0e5SVladimir Oltean unsigned int num; 243857fdd74SVladimir Oltean bool tx_fwd_offload; 244d3eed0e5SVladimir Oltean refcount_t refcount; 245d3eed0e5SVladimir Oltean }; 246f50f2127SFlorian Fainelli 247c8b09808SAndrew Lunn struct dsa_port { 248f8b8b1cdSVivien Didelot /* A CPU port is physically connected to a master device. 249f8b8b1cdSVivien Didelot * A user port exposed to userspace has a slave device. 250f8b8b1cdSVivien Didelot */ 251f8b8b1cdSVivien Didelot union { 252f8b8b1cdSVivien Didelot struct net_device *master; 253f8b8b1cdSVivien Didelot struct net_device *slave; 254f8b8b1cdSVivien Didelot }; 255f8b8b1cdSVivien Didelot 256357f203bSVladimir Oltean /* Copy of the tagging protocol operations, for quicker access 257357f203bSVladimir Oltean * in the data path. Valid only for the CPU ports. 258357f203bSVladimir Oltean */ 25915240248SVivien Didelot const struct dsa_device_ops *tag_ops; 26015240248SVivien Didelot 2613e41f93bSVivien Didelot /* Copies for faster access in master receive hot path */ 2623e41f93bSVivien Didelot struct dsa_switch_tree *dst; 26329a097b7SVladimir Oltean struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev); 2643e41f93bSVivien Didelot 26506251258SVladimir Oltean struct dsa_switch *ds; 26606251258SVladimir Oltean 26706251258SVladimir Oltean unsigned int index; 26806251258SVladimir Oltean 269057cad2cSVivien Didelot enum { 270057cad2cSVivien Didelot DSA_PORT_TYPE_UNUSED = 0, 271057cad2cSVivien Didelot DSA_PORT_TYPE_CPU, 272057cad2cSVivien Didelot DSA_PORT_TYPE_DSA, 273057cad2cSVivien Didelot DSA_PORT_TYPE_USER, 274057cad2cSVivien Didelot } type; 275057cad2cSVivien Didelot 27671e0bbdeSFlorian Fainelli const char *name; 27768b2d4a8SVivien Didelot struct dsa_port *cpu_dp; 27883216e39SMichael Walle u8 mac[ETH_ALEN]; 279b08db33dSVladimir Oltean 280b08db33dSVladimir Oltean u8 stp_state; 281b08db33dSVladimir Oltean 2821b26d364SVladimir Oltean /* Warning: the following bit fields are not atomic, and updating them 2831b26d364SVladimir Oltean * can only be done from code paths where concurrency is not possible 2841b26d364SVladimir Oltean * (probe time or under rtnl_lock). 2851b26d364SVladimir Oltean */ 28663cfc657SVladimir Oltean u8 vlan_filtering:1; 28763cfc657SVladimir Oltean 28863cfc657SVladimir Oltean /* Managed by DSA on user ports and by drivers on CPU and DSA ports */ 28963cfc657SVladimir Oltean u8 learning:1; 29063cfc657SVladimir Oltean 29163cfc657SVladimir Oltean u8 lag_tx_enabled:1; 29263cfc657SVladimir Oltean 29363cfc657SVladimir Oltean u8 devlink_port_setup:1; 29463cfc657SVladimir Oltean 295295ab96fSVladimir Oltean /* Master state bits, valid only on CPU ports */ 296295ab96fSVladimir Oltean u8 master_admin_up:1; 297295ab96fSVladimir Oltean u8 master_oper_up:1; 298295ab96fSVladimir Oltean 29963cfc657SVladimir Oltean u8 setup:1; 300bde82f38SVladimir Oltean 301189b0d93SAndrew Lunn struct device_node *dn; 30234a79f63SVivien Didelot unsigned int ageing_time; 303bde82f38SVladimir Oltean 304d3eed0e5SVladimir Oltean struct dsa_bridge *bridge; 30596567d5dSAndrew Lunn struct devlink_port devlink_port; 306aab9c406SFlorian Fainelli struct phylink *pl; 30744cc27e4SIoana Ciornei struct phylink_config pl_config; 308dedd6a00SVladimir Oltean struct dsa_lag *lag; 30918596f50SGeorge McCollister struct net_device *hsr_dev; 31097a69a0dSVladimir Oltean 311ab8ccae1SVivien Didelot struct list_head list; 312ab8ccae1SVivien Didelot 31367dbb9d4SFlorian Fainelli /* 31467dbb9d4SFlorian Fainelli * Original copy of the master netdev ethtool_ops 31567dbb9d4SFlorian Fainelli */ 31667dbb9d4SFlorian Fainelli const struct ethtool_ops *orig_ethtool_ops; 317da7b9e9bSFlorian Fainelli 318da7b9e9bSFlorian Fainelli /* 319da7b9e9bSFlorian Fainelli * Original copy of the master netdev net_device_ops 320da7b9e9bSFlorian Fainelli */ 3214cfab356SFlorian Fainelli const struct dsa_netdevice_ops *netdev_ops; 322fb35c60cSVivien Didelot 323161ca59dSVladimir Oltean /* List of MAC addresses that must be forwarded on this port. 324161ca59dSVladimir Oltean * These are only valid on CPU ports and DSA links. 325161ca59dSVladimir Oltean */ 326338a3a47SVladimir Oltean struct mutex addr_lists_lock; 3273f6e32f9SVladimir Oltean struct list_head fdbs; 328161ca59dSVladimir Oltean struct list_head mdbs; 329134ef238SVladimir Oltean 330134ef238SVladimir Oltean /* List of VLANs that CPU and DSA ports are members of. */ 331134ef238SVladimir Oltean struct mutex vlans_lock; 332134ef238SVladimir Oltean struct list_head vlans; 333c8b09808SAndrew Lunn }; 334c8b09808SAndrew Lunn 335c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member, 336c5f51765SVivien Didelot * and no dst->rtable nor this struct dsa_link would be needed, 337c5f51765SVivien Didelot * but this would require some more complex tree walking, 338c5f51765SVivien Didelot * so keep it stupid at the moment and list them all. 339c5f51765SVivien Didelot */ 340c5f51765SVivien Didelot struct dsa_link { 341c5f51765SVivien Didelot struct dsa_port *dp; 342c5f51765SVivien Didelot struct dsa_port *link_dp; 343c5f51765SVivien Didelot struct list_head list; 344c5f51765SVivien Didelot }; 345c5f51765SVivien Didelot 346c2693363SVladimir Oltean enum dsa_db_type { 347c2693363SVladimir Oltean DSA_DB_PORT, 348c2693363SVladimir Oltean DSA_DB_LAG, 349c2693363SVladimir Oltean DSA_DB_BRIDGE, 350c2693363SVladimir Oltean }; 351c2693363SVladimir Oltean 352c2693363SVladimir Oltean struct dsa_db { 353c2693363SVladimir Oltean enum dsa_db_type type; 354c2693363SVladimir Oltean 355c2693363SVladimir Oltean union { 356c2693363SVladimir Oltean const struct dsa_port *dp; 357c2693363SVladimir Oltean struct dsa_lag lag; 358c2693363SVladimir Oltean struct dsa_bridge bridge; 359c2693363SVladimir Oltean }; 360c2693363SVladimir Oltean }; 361c2693363SVladimir Oltean 362161ca59dSVladimir Oltean struct dsa_mac_addr { 363161ca59dSVladimir Oltean unsigned char addr[ETH_ALEN]; 364161ca59dSVladimir Oltean u16 vid; 365161ca59dSVladimir Oltean refcount_t refcount; 366161ca59dSVladimir Oltean struct list_head list; 367c2693363SVladimir Oltean struct dsa_db db; 368161ca59dSVladimir Oltean }; 369161ca59dSVladimir Oltean 370134ef238SVladimir Oltean struct dsa_vlan { 371134ef238SVladimir Oltean u16 vid; 372134ef238SVladimir Oltean refcount_t refcount; 373134ef238SVladimir Oltean struct list_head list; 374134ef238SVladimir Oltean }; 375134ef238SVladimir Oltean 376c8f0b869SBen Hutchings struct dsa_switch { 377c33063d6SAndrew Lunn struct device *dev; 378c33063d6SAndrew Lunn 379c8f0b869SBen Hutchings /* 380c8f0b869SBen Hutchings * Parent switch tree, and switch index. 381c8f0b869SBen Hutchings */ 382c8f0b869SBen Hutchings struct dsa_switch_tree *dst; 38399feaafcSVivien Didelot unsigned int index; 384c8f0b869SBen Hutchings 3851b26d364SVladimir Oltean /* Warning: the following bit fields are not atomic, and updating them 3861b26d364SVladimir Oltean * can only be done from code paths where concurrency is not possible 3871b26d364SVladimir Oltean * (probe time or under rtnl_lock). 3881b26d364SVladimir Oltean */ 38963cfc657SVladimir Oltean u32 setup:1; 39063cfc657SVladimir Oltean 39163cfc657SVladimir Oltean /* Disallow bridge core from requesting different VLAN awareness 39263cfc657SVladimir Oltean * settings on ports if not hardware-supported 3937787ff77SVladimir Oltean */ 39463cfc657SVladimir Oltean u32 vlan_filtering_is_global:1; 39563cfc657SVladimir Oltean 39663cfc657SVladimir Oltean /* Keep VLAN filtering enabled on ports not offloading any upper */ 39763cfc657SVladimir Oltean u32 needs_standalone_vlan_filtering:1; 39863cfc657SVladimir Oltean 39963cfc657SVladimir Oltean /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 40063cfc657SVladimir Oltean * that have vlan_filtering=0. All drivers should ideally set this (and 40163cfc657SVladimir Oltean * then the option would get removed), but it is unknown whether this 4027787ff77SVladimir Oltean * would break things or not. 4037787ff77SVladimir Oltean */ 40463cfc657SVladimir Oltean u32 configure_vlan_while_not_filtering:1; 40563cfc657SVladimir Oltean 40663cfc657SVladimir Oltean /* If the switch driver always programs the CPU port as egress tagged 40763cfc657SVladimir Oltean * despite the VLAN configuration indicating otherwise, then setting 40863cfc657SVladimir Oltean * @untag_bridge_pvid will force the DSA receive path to pop the 40963cfc657SVladimir Oltean * bridge's default_pvid VLAN tagged frames to offer a consistent 41063cfc657SVladimir Oltean * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge 4117787ff77SVladimir Oltean * device. 4127787ff77SVladimir Oltean */ 41363cfc657SVladimir Oltean u32 untag_bridge_pvid:1; 41463cfc657SVladimir Oltean 4157787ff77SVladimir Oltean /* Let DSA manage the FDB entries towards the 4167787ff77SVladimir Oltean * CPU, based on the software bridge database. 4177787ff77SVladimir Oltean */ 41863cfc657SVladimir Oltean u32 assisted_learning_on_cpu_port:1; 41963cfc657SVladimir Oltean 42063cfc657SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 42163cfc657SVladimir Oltean * should be retrieved from here and not from the per-port settings. 4227787ff77SVladimir Oltean */ 42363cfc657SVladimir Oltean u32 vlan_filtering:1; 42463cfc657SVladimir Oltean 42563cfc657SVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 42663cfc657SVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 42763cfc657SVladimir Oltean * interfaces is needed. 4287787ff77SVladimir Oltean */ 42963cfc657SVladimir Oltean u32 mtu_enforcement_ingress:1; 4307787ff77SVladimir Oltean 431c2693363SVladimir Oltean /* Drivers that isolate the FDBs of multiple bridges must set this 432c2693363SVladimir Oltean * to true to receive the bridge as an argument in .port_fdb_{add,del} 433c2693363SVladimir Oltean * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be 434c2693363SVladimir Oltean * passed as zero. 435c2693363SVladimir Oltean */ 436c2693363SVladimir Oltean u32 fdb_isolation:1; 437c2693363SVladimir Oltean 438f515f192SVivien Didelot /* Listener for switch fabric events */ 439f515f192SVivien Didelot struct notifier_block nb; 440f515f192SVivien Didelot 441c8f0b869SBen Hutchings /* 4427543a6d5SAndrew Lunn * Give the switch driver somewhere to hang its private data 4437543a6d5SAndrew Lunn * structure. 4447543a6d5SAndrew Lunn */ 4457543a6d5SAndrew Lunn void *priv; 4467543a6d5SAndrew Lunn 447dc452a47SVladimir Oltean void *tagger_data; 448dc452a47SVladimir Oltean 4497543a6d5SAndrew Lunn /* 450c8f0b869SBen Hutchings * Configuration data for this switch. 451c8f0b869SBen Hutchings */ 452ff04955cSAndrew Lunn struct dsa_chip_data *cd; 453c8f0b869SBen Hutchings 454c8f0b869SBen Hutchings /* 4559d490b4eSVivien Didelot * The switch operations. 456c8f0b869SBen Hutchings */ 457a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 458c8f0b869SBen Hutchings 45966472fc0SAndrew Lunn /* 460c8f0b869SBen Hutchings * Slave mii_bus and devices for the individual ports. 461c8f0b869SBen Hutchings */ 4620d8bcdd3SFlorian Fainelli u32 phys_mii_mask; 463c8f0b869SBen Hutchings struct mii_bus *slave_mii_bus; 464a0c02161SVivien Didelot 4650f3da6afSVivien Didelot /* Ageing Time limits in msecs */ 4660f3da6afSVivien Didelot unsigned int ageing_time_min; 4670f3da6afSVivien Didelot unsigned int ageing_time_max; 4680f3da6afSVivien Didelot 469d7b1fd52SVladimir Oltean /* Storage for drivers using tag_8021q */ 470d7b1fd52SVladimir Oltean struct dsa_8021q_context *tag_8021q_ctx; 471d7b1fd52SVladimir Oltean 47296567d5dSAndrew Lunn /* devlink used to represent this switch device */ 47396567d5dSAndrew Lunn struct devlink *devlink; 47496567d5dSAndrew Lunn 47555199df6SFlorian Fainelli /* Number of switch port queues */ 47655199df6SFlorian Fainelli unsigned int num_tx_queues; 47755199df6SFlorian Fainelli 478058102a6STobias Waldekranz /* Drivers that benefit from having an ID associated with each 479058102a6STobias Waldekranz * offloaded LAG should set this to the maximum number of 480058102a6STobias Waldekranz * supported IDs. DSA will then maintain a mapping of _at 481058102a6STobias Waldekranz * least_ these many IDs, accessible to drivers via 482058102a6STobias Waldekranz * dsa_lag_id(). 483058102a6STobias Waldekranz */ 484058102a6STobias Waldekranz unsigned int num_lag_ids; 485058102a6STobias Waldekranz 486947c8746SVladimir Oltean /* Drivers that support bridge forwarding offload or FDB isolation 487947c8746SVladimir Oltean * should set this to the maximum number of bridges spanning the same 488947c8746SVladimir Oltean * switch tree (or all trees, in the case of cross-tree bridging 489947c8746SVladimir Oltean * support) that can be offloaded. 490123abc06SVladimir Oltean */ 491947c8746SVladimir Oltean unsigned int max_num_bridges; 492123abc06SVladimir Oltean 493258030acSVladimir Oltean unsigned int num_ports; 494c8f0b869SBen Hutchings }; 495c8f0b869SBen Hutchings 49668bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 497c38c5a66SVivien Didelot { 498b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 499d607525bSVivien Didelot struct dsa_port *dp; 500b96ddf25SVivien Didelot 501b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 502b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 503b96ddf25SVivien Didelot return dp; 504d607525bSVivien Didelot 505d607525bSVivien Didelot return NULL; 506c38c5a66SVivien Didelot } 507c38c5a66SVivien Didelot 508a8986681SVladimir Oltean static inline bool dsa_port_is_dsa(struct dsa_port *port) 509a8986681SVladimir Oltean { 510a8986681SVladimir Oltean return port->type == DSA_PORT_TYPE_DSA; 511a8986681SVladimir Oltean } 512a8986681SVladimir Oltean 513a8986681SVladimir Oltean static inline bool dsa_port_is_cpu(struct dsa_port *port) 514a8986681SVladimir Oltean { 515a8986681SVladimir Oltean return port->type == DSA_PORT_TYPE_CPU; 516a8986681SVladimir Oltean } 517a8986681SVladimir Oltean 518a8986681SVladimir Oltean static inline bool dsa_port_is_user(struct dsa_port *dp) 519a8986681SVladimir Oltean { 520a8986681SVladimir Oltean return dp->type == DSA_PORT_TYPE_USER; 521a8986681SVladimir Oltean } 522a8986681SVladimir Oltean 523a57d8c21SVladimir Oltean static inline bool dsa_port_is_unused(struct dsa_port *dp) 524a57d8c21SVladimir Oltean { 525a57d8c21SVladimir Oltean return dp->type == DSA_PORT_TYPE_UNUSED; 526a57d8c21SVladimir Oltean } 527a57d8c21SVladimir Oltean 528295ab96fSVladimir Oltean static inline bool dsa_port_master_is_operational(struct dsa_port *dp) 529295ab96fSVladimir Oltean { 530295ab96fSVladimir Oltean return dsa_port_is_cpu(dp) && dp->master_admin_up && 531295ab96fSVladimir Oltean dp->master_oper_up; 532295ab96fSVladimir Oltean } 533295ab96fSVladimir Oltean 534bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 535bff7b688SVivien Didelot { 536c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 537bff7b688SVivien Didelot } 538bff7b688SVivien Didelot 539c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 540c8f0b869SBen Hutchings { 541c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 542c8f0b869SBen Hutchings } 543c8f0b869SBen Hutchings 54460045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 54560045cbfSAndrew Lunn { 546c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 54760045cbfSAndrew Lunn } 54860045cbfSAndrew Lunn 5492b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 5506cd456f3SVivien Didelot { 551c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 5526cd456f3SVivien Didelot } 5536cd456f3SVivien Didelot 55482b31898SVladimir Oltean #define dsa_tree_for_each_user_port(_dp, _dst) \ 55582b31898SVladimir Oltean list_for_each_entry((_dp), &(_dst)->ports, list) \ 55682b31898SVladimir Oltean if (dsa_port_is_user((_dp))) 55782b31898SVladimir Oltean 55882b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \ 55982b31898SVladimir Oltean list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ 56082b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 56182b31898SVladimir Oltean 56282b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \ 56382b31898SVladimir Oltean list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \ 56482b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 56582b31898SVladimir Oltean 56682b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \ 56782b31898SVladimir Oltean list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \ 56882b31898SVladimir Oltean if ((_dp)->ds == (_ds)) 56982b31898SVladimir Oltean 57082b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \ 57182b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 57282b31898SVladimir Oltean if (!dsa_port_is_unused((_dp))) 57382b31898SVladimir Oltean 57482b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \ 57582b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 57682b31898SVladimir Oltean if (dsa_port_is_user((_dp))) 57782b31898SVladimir Oltean 57882b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \ 57982b31898SVladimir Oltean dsa_switch_for_each_port((_dp), (_ds)) \ 58082b31898SVladimir Oltean if (dsa_port_is_cpu((_dp))) 58182b31898SVladimir Oltean 58202bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 58302bc6e54SVivien Didelot { 584d0004a02SVladimir Oltean struct dsa_port *dp; 585c38c5a66SVivien Didelot u32 mask = 0; 58602bc6e54SVivien Didelot 587d0004a02SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) 588d0004a02SVladimir Oltean mask |= BIT(dp->index); 589c38c5a66SVivien Didelot 590c38c5a66SVivien Didelot return mask; 591c8652c83SVivien Didelot } 592c8652c83SVivien Didelot 593c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 594c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 595c5f51765SVivien Didelot { 596c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 597c5f51765SVivien Didelot struct dsa_link *dl; 598c5f51765SVivien Didelot 599c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 600c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 601c5f51765SVivien Didelot return dl->dp->index; 602c5f51765SVivien Didelot 603c5f51765SVivien Didelot return ds->num_ports; 604c5f51765SVivien Didelot } 605c5f51765SVivien Didelot 6063b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 6073b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 6083b8fac5dSVivien Didelot int port) 6093b8fac5dSVivien Didelot { 6103b8fac5dSVivien Didelot if (device == ds->index) 6113b8fac5dSVivien Didelot return port; 6123b8fac5dSVivien Didelot else 613c5f51765SVivien Didelot return dsa_routing_port(ds, device); 6143b8fac5dSVivien Didelot } 6153b8fac5dSVivien Didelot 6163b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 61707073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 618c8f0b869SBen Hutchings { 61907073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 62007073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 62107073c79SVivien Didelot 62207073c79SVivien Didelot if (!cpu_dp) 62307073c79SVivien Didelot return port; 624c8f0b869SBen Hutchings 6253b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 626c8f0b869SBen Hutchings } 627c8f0b869SBen Hutchings 62863609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */ 62963609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port) 63063609c8fSVladimir Oltean { 63163609c8fSVladimir Oltean if (dsa_is_unused_port(ds, port)) 63263609c8fSVladimir Oltean return false; 63363609c8fSVladimir Oltean 63463609c8fSVladimir Oltean return port == dsa_upstream_port(ds, port); 63563609c8fSVladimir Oltean } 63663609c8fSVladimir Oltean 637d352b20fSTobias Waldekranz /* Return true if this is a DSA port leading away from the CPU */ 638d352b20fSTobias Waldekranz static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port) 639d352b20fSTobias Waldekranz { 640d352b20fSTobias Waldekranz return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port); 641d352b20fSTobias Waldekranz } 642d352b20fSTobias Waldekranz 6437af4a361STobias Waldekranz /* Return the local port used to reach the CPU port */ 6447af4a361STobias Waldekranz static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds) 6457af4a361STobias Waldekranz { 6467af4a361STobias Waldekranz struct dsa_port *dp; 6477af4a361STobias Waldekranz 6487af4a361STobias Waldekranz dsa_switch_for_each_available_port(dp, ds) { 6497af4a361STobias Waldekranz return dsa_upstream_port(ds, dp->index); 6507af4a361STobias Waldekranz } 6517af4a361STobias Waldekranz 6527af4a361STobias Waldekranz return ds->num_ports; 6537af4a361STobias Waldekranz } 6547af4a361STobias Waldekranz 65563609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning 65663609c8fSVladimir Oltean * that the routing port from @downstream_ds to @upstream_ds is also the port 65763609c8fSVladimir Oltean * which @downstream_ds uses to reach its dedicated CPU. 65863609c8fSVladimir Oltean */ 65963609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds, 66063609c8fSVladimir Oltean struct dsa_switch *downstream_ds) 66163609c8fSVladimir Oltean { 66263609c8fSVladimir Oltean int routing_port; 66363609c8fSVladimir Oltean 66463609c8fSVladimir Oltean if (upstream_ds == downstream_ds) 66563609c8fSVladimir Oltean return true; 66663609c8fSVladimir Oltean 66763609c8fSVladimir Oltean routing_port = dsa_routing_port(downstream_ds, upstream_ds->index); 66863609c8fSVladimir Oltean 66963609c8fSVladimir Oltean return dsa_is_upstream_port(downstream_ds, routing_port); 67063609c8fSVladimir Oltean } 67163609c8fSVladimir Oltean 672cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 673cf2d45f5SVladimir Oltean { 674cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 675cf2d45f5SVladimir Oltean 676cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 677cf2d45f5SVladimir Oltean return ds->vlan_filtering; 678cf2d45f5SVladimir Oltean else 679cf2d45f5SVladimir Oltean return dp->vlan_filtering; 680cf2d45f5SVladimir Oltean } 681cf2d45f5SVladimir Oltean 682dedd6a00SVladimir Oltean static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp) 683dedd6a00SVladimir Oltean { 684dedd6a00SVladimir Oltean return dp->lag ? dp->lag->id : 0; 685dedd6a00SVladimir Oltean } 686dedd6a00SVladimir Oltean 687dedd6a00SVladimir Oltean static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp) 688dedd6a00SVladimir Oltean { 689dedd6a00SVladimir Oltean return dp->lag ? dp->lag->dev : NULL; 690dedd6a00SVladimir Oltean } 691dedd6a00SVladimir Oltean 692dedd6a00SVladimir Oltean static inline bool dsa_port_offloads_lag(struct dsa_port *dp, 693dedd6a00SVladimir Oltean const struct dsa_lag *lag) 694dedd6a00SVladimir Oltean { 695dedd6a00SVladimir Oltean return dsa_port_lag_dev_get(dp) == lag->dev; 696dedd6a00SVladimir Oltean } 697dedd6a00SVladimir Oltean 698cc76ce9eSTobias Waldekranz static inline 699cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) 700cc76ce9eSTobias Waldekranz { 701d3eed0e5SVladimir Oltean if (!dp->bridge) 702cc76ce9eSTobias Waldekranz return NULL; 703cc76ce9eSTobias Waldekranz 704dedd6a00SVladimir Oltean if (dp->lag) 705dedd6a00SVladimir Oltean return dp->lag->dev; 706cc76ce9eSTobias Waldekranz else if (dp->hsr_dev) 707cc76ce9eSTobias Waldekranz return dp->hsr_dev; 708cc76ce9eSTobias Waldekranz 709cc76ce9eSTobias Waldekranz return dp->slave; 710cc76ce9eSTobias Waldekranz } 711cc76ce9eSTobias Waldekranz 71236cbf39bSVladimir Oltean static inline struct net_device * 71336cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp) 71436cbf39bSVladimir Oltean { 715d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->dev : NULL; 71636cbf39bSVladimir Oltean } 71736cbf39bSVladimir Oltean 71836cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp) 71936cbf39bSVladimir Oltean { 720d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->num : 0; 72136cbf39bSVladimir Oltean } 72236cbf39bSVladimir Oltean 72336cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a, 72436cbf39bSVladimir Oltean const struct dsa_port *b) 72536cbf39bSVladimir Oltean { 72636cbf39bSVladimir Oltean struct net_device *br_a = dsa_port_bridge_dev_get(a); 72736cbf39bSVladimir Oltean struct net_device *br_b = dsa_port_bridge_dev_get(b); 72836cbf39bSVladimir Oltean 72936cbf39bSVladimir Oltean /* Standalone ports are not in the same bridge with one another */ 73036cbf39bSVladimir Oltean return (!br_a || !br_b) ? false : (br_a == br_b); 73136cbf39bSVladimir Oltean } 73236cbf39bSVladimir Oltean 7336a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp, 7346a43cba3SVladimir Oltean const struct net_device *dev) 7356a43cba3SVladimir Oltean { 7366a43cba3SVladimir Oltean return dsa_port_to_bridge_port(dp) == dev; 7376a43cba3SVladimir Oltean } 7386a43cba3SVladimir Oltean 7396a43cba3SVladimir Oltean static inline bool 7406a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp, 7416a43cba3SVladimir Oltean const struct net_device *bridge_dev) 7426a43cba3SVladimir Oltean { 7436a43cba3SVladimir Oltean /* DSA ports connected to a bridge, and event was emitted 7446a43cba3SVladimir Oltean * for the bridge. 7456a43cba3SVladimir Oltean */ 7466a43cba3SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge_dev; 7476a43cba3SVladimir Oltean } 7486a43cba3SVladimir Oltean 749d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp, 750d3eed0e5SVladimir Oltean const struct dsa_bridge *bridge) 751d3eed0e5SVladimir Oltean { 752d3eed0e5SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge->dev; 753d3eed0e5SVladimir Oltean } 754d3eed0e5SVladimir Oltean 7556a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */ 7566a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst, 7576a43cba3SVladimir Oltean const struct net_device *dev) 7586a43cba3SVladimir Oltean { 7596a43cba3SVladimir Oltean struct dsa_port *dp; 7606a43cba3SVladimir Oltean 7616a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7626a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_port(dp, dev)) 7636a43cba3SVladimir Oltean return true; 7646a43cba3SVladimir Oltean 7656a43cba3SVladimir Oltean return false; 7666a43cba3SVladimir Oltean } 7676a43cba3SVladimir Oltean 7686a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */ 7696a43cba3SVladimir Oltean static inline bool 7706a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst, 7716a43cba3SVladimir Oltean const struct net_device *bridge_dev) 7726a43cba3SVladimir Oltean { 7736a43cba3SVladimir Oltean struct dsa_port *dp; 7746a43cba3SVladimir Oltean 7756a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7766a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_dev(dp, bridge_dev)) 7776a43cba3SVladimir Oltean return true; 7786a43cba3SVladimir Oltean 7796a43cba3SVladimir Oltean return false; 7806a43cba3SVladimir Oltean } 7816a43cba3SVladimir Oltean 7822bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 7832bedde1aSArkadi Sharshevsky bool is_static, void *data); 7849d490b4eSVivien Didelot struct dsa_switch_ops { 78553da0ebaSVladimir Oltean /* 78653da0ebaSVladimir Oltean * Tagging protocol helpers called for the CPU ports and DSA links. 78753da0ebaSVladimir Oltean * @get_tag_protocol retrieves the initial tagging protocol and is 78853da0ebaSVladimir Oltean * mandatory. Switches which can operate using multiple tagging 78953da0ebaSVladimir Oltean * protocols should implement @change_tag_protocol and report in 79053da0ebaSVladimir Oltean * @get_tag_protocol the tagger in current use. 79153da0ebaSVladimir Oltean */ 7925ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 7934d776482SFlorian Fainelli int port, 7944d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 79553da0ebaSVladimir Oltean int (*change_tag_protocol)(struct dsa_switch *ds, int port, 79653da0ebaSVladimir Oltean enum dsa_tag_protocol proto); 797dc452a47SVladimir Oltean /* 798dc452a47SVladimir Oltean * Method for switch drivers to connect to the tagging protocol driver 799dc452a47SVladimir Oltean * in current use. The switch driver can provide handlers for certain 800dc452a47SVladimir Oltean * types of packets for switch management. 801dc452a47SVladimir Oltean */ 802dc452a47SVladimir Oltean int (*connect_tag_protocol)(struct dsa_switch *ds, 803dc452a47SVladimir Oltean enum dsa_tag_protocol proto); 8047b314362SAndrew Lunn 805fd292c18SVladimir Oltean /* Optional switch-wide initialization and destruction methods */ 806c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 8075e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 808fd292c18SVladimir Oltean 809fd292c18SVladimir Oltean /* Per-port initialization and destruction methods. Mandatory if the 810fd292c18SVladimir Oltean * driver registers devlink port regions, optional otherwise. 811fd292c18SVladimir Oltean */ 812fd292c18SVladimir Oltean int (*port_setup)(struct dsa_switch *ds, int port); 813fd292c18SVladimir Oltean void (*port_teardown)(struct dsa_switch *ds, int port); 814fd292c18SVladimir Oltean 8156819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 816c8f0b869SBen Hutchings 817c8f0b869SBen Hutchings /* 818c8f0b869SBen Hutchings * Access to the switch's PHY registers. 819c8f0b869SBen Hutchings */ 820c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 821c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 822c8f0b869SBen Hutchings int regnum, u16 val); 823c8f0b869SBen Hutchings 824c8f0b869SBen Hutchings /* 825ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 826ec9436baSFlorian Fainelli */ 827ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 828ec9436baSFlorian Fainelli struct phy_device *phydev); 829ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 830ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 831ec9436baSFlorian Fainelli 832ec9436baSFlorian Fainelli /* 83311d8f3ddSFlorian Fainelli * PHYLINK integration 83411d8f3ddSFlorian Fainelli */ 835072eea6cSRussell King (Oracle) void (*phylink_get_caps)(struct dsa_switch *ds, int port, 836072eea6cSRussell King (Oracle) struct phylink_config *config); 83711d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 83811d8f3ddSFlorian Fainelli unsigned long *supported, 83911d8f3ddSFlorian Fainelli struct phylink_link_state *state); 840bde01822SRussell King (Oracle) struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds, 841bde01822SRussell King (Oracle) int port, 842bde01822SRussell King (Oracle) phy_interface_t iface); 84311d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 84411d8f3ddSFlorian Fainelli struct phylink_link_state *state); 84511d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 84611d8f3ddSFlorian Fainelli unsigned int mode, 84711d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 84811d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 84911d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 85011d8f3ddSFlorian Fainelli unsigned int mode, 85111d8f3ddSFlorian Fainelli phy_interface_t interface); 85211d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 85311d8f3ddSFlorian Fainelli unsigned int mode, 85411d8f3ddSFlorian Fainelli phy_interface_t interface, 8555b502a7bSRussell King struct phy_device *phydev, 8565b502a7bSRussell King int speed, int duplex, 8575b502a7bSRussell King bool tx_pause, bool rx_pause); 85811d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 85911d8f3ddSFlorian Fainelli struct phylink_link_state *state); 86011d8f3ddSFlorian Fainelli /* 861c2ec5f2eSOleksij Rempel * Port statistics counters. 862c8f0b869SBen Hutchings */ 86389f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 86489f09048SFlorian Fainelli u32 stringset, uint8_t *data); 865c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 866c8f0b869SBen Hutchings int port, uint64_t *data); 86789f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 868cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 869cf963573SFlorian Fainelli int port, uint64_t *data); 870487d3855SAlvin Šipraga void (*get_eth_phy_stats)(struct dsa_switch *ds, int port, 871487d3855SAlvin Šipraga struct ethtool_eth_phy_stats *phy_stats); 872487d3855SAlvin Šipraga void (*get_eth_mac_stats)(struct dsa_switch *ds, int port, 873487d3855SAlvin Šipraga struct ethtool_eth_mac_stats *mac_stats); 874487d3855SAlvin Šipraga void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port, 875487d3855SAlvin Šipraga struct ethtool_eth_ctrl_stats *ctrl_stats); 876c2ec5f2eSOleksij Rempel void (*get_stats64)(struct dsa_switch *ds, int port, 877c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s); 878a71acad9SOleksij Rempel void (*self_test)(struct dsa_switch *ds, int port, 879a71acad9SOleksij Rempel struct ethtool_test *etest, u64 *data); 88024462549SFlorian Fainelli 88124462549SFlorian Fainelli /* 88219e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 88319e57c4eSFlorian Fainelli */ 88419e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 88519e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 88619e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 88719e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 88819e57c4eSFlorian Fainelli 88919e57c4eSFlorian Fainelli /* 8900336369dSBrandon Streiff * ethtool timestamp info 8910336369dSBrandon Streiff */ 8920336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 8930336369dSBrandon Streiff struct ethtool_ts_info *ts); 8940336369dSBrandon Streiff 8950336369dSBrandon Streiff /* 896d538eca8SVladimir Oltean * DCB ops 897d538eca8SVladimir Oltean */ 898d538eca8SVladimir Oltean int (*port_get_default_prio)(struct dsa_switch *ds, int port); 899d538eca8SVladimir Oltean int (*port_set_default_prio)(struct dsa_switch *ds, int port, 900d538eca8SVladimir Oltean u8 prio); 90147d75f78SVladimir Oltean int (*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp); 90247d75f78SVladimir Oltean int (*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp, 90347d75f78SVladimir Oltean u8 prio); 90447d75f78SVladimir Oltean int (*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp, 90547d75f78SVladimir Oltean u8 prio); 906d538eca8SVladimir Oltean 907d538eca8SVladimir Oltean /* 90824462549SFlorian Fainelli * Suspend and resume 90924462549SFlorian Fainelli */ 91024462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 91124462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 912b2f2af21SFlorian Fainelli 913b2f2af21SFlorian Fainelli /* 914b2f2af21SFlorian Fainelli * Port enable/disable 915b2f2af21SFlorian Fainelli */ 916b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 917b2f2af21SFlorian Fainelli struct phy_device *phy); 91875104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 9197905288fSFlorian Fainelli 9207905288fSFlorian Fainelli /* 92108f50061SVivien Didelot * Port's MAC EEE settings 9227905288fSFlorian Fainelli */ 92308f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 9247905288fSFlorian Fainelli struct ethtool_eee *e); 92508f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 9267905288fSFlorian Fainelli struct ethtool_eee *e); 92751579c3fSGuenter Roeck 9286793abb4SGuenter Roeck /* EEPROM access */ 9296793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 9306793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 9316793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 9326793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 9336793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 9343d762a0fSGuenter Roeck 9353d762a0fSGuenter Roeck /* 9363d762a0fSGuenter Roeck * Register access. 9373d762a0fSGuenter Roeck */ 9383d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 9393d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 9403d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 941b73adef6SFlorian Fainelli 942b73adef6SFlorian Fainelli /* 943e358bef7SVladimir Oltean * Upper device tracking. 944e358bef7SVladimir Oltean */ 945e358bef7SVladimir Oltean int (*port_prechangeupper)(struct dsa_switch *ds, int port, 946e358bef7SVladimir Oltean struct netdev_notifier_changeupper_info *info); 947e358bef7SVladimir Oltean 948e358bef7SVladimir Oltean /* 949b73adef6SFlorian Fainelli * Bridge integration 950b73adef6SFlorian Fainelli */ 95134a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 95271327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 953b079922bSVladimir Oltean struct dsa_bridge bridge, 95406b9cce4SVladimir Oltean bool *tx_fwd_offload, 95506b9cce4SVladimir Oltean struct netlink_ext_ack *extack); 956f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 957d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 95843c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 959b73adef6SFlorian Fainelli u8 state); 9607414af30STobias Waldekranz int (*port_mst_state_set)(struct dsa_switch *ds, int port, 9617414af30STobias Waldekranz const struct switchdev_mst_state *state); 962732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 9637414af30STobias Waldekranz int (*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid); 964a8b659e7SVladimir Oltean int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 965a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 966a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 967a8b659e7SVladimir Oltean int (*port_bridge_flags)(struct dsa_switch *ds, int port, 968a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 969a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 9702a778e1bSVivien Didelot 9712a778e1bSVivien Didelot /* 97211149536SVivien Didelot * VLAN support 97311149536SVivien Didelot */ 974fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 97589153ed6SVladimir Oltean bool vlan_filtering, 97689153ed6SVladimir Oltean struct netlink_ext_ack *extack); 9771958d581SVladimir Oltean int (*port_vlan_add)(struct dsa_switch *ds, int port, 97831046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 97931046a5fSVladimir Oltean struct netlink_ext_ack *extack); 98076e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 98176e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 9828e6598a7STobias Waldekranz int (*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge, 9838e6598a7STobias Waldekranz const struct switchdev_vlan_msti *msti); 9848e6598a7STobias Waldekranz 98511149536SVivien Didelot /* 9862a778e1bSVivien Didelot * Forwarding database 9872a778e1bSVivien Didelot */ 9881b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 989c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 990c2693363SVladimir Oltean struct dsa_db db); 9912a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 992c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 993c2693363SVladimir Oltean struct dsa_db db); 994ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 9952bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 996e212fa7cSVladimir Oltean int (*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag, 997c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 998c2693363SVladimir Oltean struct dsa_db db); 999e212fa7cSVladimir Oltean int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag, 1000c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 1001c2693363SVladimir Oltean struct dsa_db db); 10028df30255SVivien Didelot 10038df30255SVivien Didelot /* 10048df30255SVivien Didelot * Multicast database 10058df30255SVivien Didelot */ 1006a52b2da7SVladimir Oltean int (*port_mdb_add)(struct dsa_switch *ds, int port, 1007c2693363SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 1008c2693363SVladimir Oltean struct dsa_db db); 10098df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 1010c2693363SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 1011c2693363SVladimir Oltean struct dsa_db db); 1012bf9f2648SFlorian Fainelli /* 1013bf9f2648SFlorian Fainelli * RXNFC 1014bf9f2648SFlorian Fainelli */ 1015bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 1016bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 1017bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 1018bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 1019f50f2127SFlorian Fainelli 1020f50f2127SFlorian Fainelli /* 1021f50f2127SFlorian Fainelli * TC integration 1022f50f2127SFlorian Fainelli */ 1023ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 1024ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 1025ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 1026ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 1027ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 1028ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 1029f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 1030f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 1031*0148bb50SVladimir Oltean bool ingress, struct netlink_ext_ack *extack); 1032f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 1033f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 103434297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 103534297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 103634297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 103747d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 103847d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 103940ef2c93SVivien Didelot 104040ef2c93SVivien Didelot /* 104140ef2c93SVivien Didelot * Cross-chip operations 104240ef2c93SVivien Didelot */ 1043f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 1044f66a6a69SVladimir Oltean int sw_index, int port, 104506b9cce4SVladimir Oltean struct dsa_bridge bridge, 104606b9cce4SVladimir Oltean struct netlink_ext_ack *extack); 1047f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 1048f66a6a69SVladimir Oltean int sw_index, int port, 1049d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 1050058102a6STobias Waldekranz int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 1051058102a6STobias Waldekranz int port); 1052058102a6STobias Waldekranz int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 1053dedd6a00SVladimir Oltean int port, struct dsa_lag lag, 1054058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 1055058102a6STobias Waldekranz int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 1056dedd6a00SVladimir Oltean int port, struct dsa_lag lag); 10570336369dSBrandon Streiff 10580336369dSBrandon Streiff /* 10590336369dSBrandon Streiff * PTP functionality 10600336369dSBrandon Streiff */ 10610336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 10620336369dSBrandon Streiff struct ifreq *ifr); 10630336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 10640336369dSBrandon Streiff struct ifreq *ifr); 10655c5416f5SYangbo Lu void (*port_txtstamp)(struct dsa_switch *ds, int port, 10665c5416f5SYangbo Lu struct sk_buff *skb); 106790af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 106890af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 106997a69a0dSVladimir Oltean 10700f06b855SAndrew Lunn /* Devlink parameters, etc */ 10716b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 10726b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10736b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 10746b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10750f06b855SAndrew Lunn int (*devlink_info_get)(struct dsa_switch *ds, 10760f06b855SAndrew Lunn struct devlink_info_req *req, 10770f06b855SAndrew Lunn struct netlink_ext_ack *extack); 10782a6ef763SVladimir Oltean int (*devlink_sb_pool_get)(struct dsa_switch *ds, 10792a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10802a6ef763SVladimir Oltean struct devlink_sb_pool_info *pool_info); 10812a6ef763SVladimir Oltean int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 10822a6ef763SVladimir Oltean u16 pool_index, u32 size, 10832a6ef763SVladimir Oltean enum devlink_sb_threshold_type threshold_type, 10842a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10852a6ef763SVladimir Oltean int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 10862a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10872a6ef763SVladimir Oltean u32 *p_threshold); 10882a6ef763SVladimir Oltean int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 10892a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 10902a6ef763SVladimir Oltean u32 threshold, 10912a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 10922a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 10932a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10942a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10952a6ef763SVladimir Oltean u16 *p_pool_index, u32 *p_threshold); 10962a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 10972a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 10982a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 10992a6ef763SVladimir Oltean u16 pool_index, u32 threshold, 11002a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 11012a6ef763SVladimir Oltean int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 11022a6ef763SVladimir Oltean unsigned int sb_index); 11032a6ef763SVladimir Oltean int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 11042a6ef763SVladimir Oltean unsigned int sb_index); 11052a6ef763SVladimir Oltean int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 11062a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 11072a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 11082a6ef763SVladimir Oltean int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 11092a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 11102a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 11112a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 1112bfcb8132SVladimir Oltean 1113bfcb8132SVladimir Oltean /* 1114bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 1115bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 1116bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 1117ab88d64aSRandy Dunlap * method needs to do so privately. 1118bfcb8132SVladimir Oltean */ 1119bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 1120bfcb8132SVladimir Oltean int new_mtu); 1121bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 1122058102a6STobias Waldekranz 1123058102a6STobias Waldekranz /* 1124058102a6STobias Waldekranz * LAG integration 1125058102a6STobias Waldekranz */ 1126058102a6STobias Waldekranz int (*port_lag_change)(struct dsa_switch *ds, int port); 1127058102a6STobias Waldekranz int (*port_lag_join)(struct dsa_switch *ds, int port, 1128dedd6a00SVladimir Oltean struct dsa_lag lag, 1129058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 1130058102a6STobias Waldekranz int (*port_lag_leave)(struct dsa_switch *ds, int port, 1131dedd6a00SVladimir Oltean struct dsa_lag lag); 113218596f50SGeorge McCollister 113318596f50SGeorge McCollister /* 113418596f50SGeorge McCollister * HSR integration 113518596f50SGeorge McCollister */ 113618596f50SGeorge McCollister int (*port_hsr_join)(struct dsa_switch *ds, int port, 113718596f50SGeorge McCollister struct net_device *hsr); 113818596f50SGeorge McCollister int (*port_hsr_leave)(struct dsa_switch *ds, int port, 113918596f50SGeorge McCollister struct net_device *hsr); 1140c595c433SHoratiu Vultur 1141c595c433SHoratiu Vultur /* 1142c595c433SHoratiu Vultur * MRP integration 1143c595c433SHoratiu Vultur */ 1144c595c433SHoratiu Vultur int (*port_mrp_add)(struct dsa_switch *ds, int port, 1145c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1146c595c433SHoratiu Vultur int (*port_mrp_del)(struct dsa_switch *ds, int port, 1147c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1148c595c433SHoratiu Vultur int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 1149c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 1150c595c433SHoratiu Vultur int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 1151c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 11525da11eb4SVladimir Oltean 11535da11eb4SVladimir Oltean /* 11545da11eb4SVladimir Oltean * tag_8021q operations 11555da11eb4SVladimir Oltean */ 11565da11eb4SVladimir Oltean int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 11575da11eb4SVladimir Oltean u16 flags); 11585da11eb4SVladimir Oltean int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 1159295ab96fSVladimir Oltean 1160295ab96fSVladimir Oltean /* 1161295ab96fSVladimir Oltean * DSA master tracking operations 1162295ab96fSVladimir Oltean */ 1163295ab96fSVladimir Oltean void (*master_state_change)(struct dsa_switch *ds, 1164295ab96fSVladimir Oltean const struct net_device *master, 1165295ab96fSVladimir Oltean bool operational); 11666b297524SAndrew Lunn }; 11676b297524SAndrew Lunn 11686b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 11696b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 11706b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 11716b297524SAndrew Lunn 11726b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 11736b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 11746b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 11756b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 11766b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 11776b297524SAndrew Lunn const struct devlink_param *params, 11786b297524SAndrew Lunn size_t params_count); 11796b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 11806b297524SAndrew Lunn const struct devlink_param *params, 11816b297524SAndrew Lunn size_t params_count); 11825cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 11835cd73fbdSAndrew Lunn const char *resource_name, 11845cd73fbdSAndrew Lunn u64 resource_size, 11855cd73fbdSAndrew Lunn u64 resource_id, 11865cd73fbdSAndrew Lunn u64 parent_resource_id, 11875cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 11885cd73fbdSAndrew Lunn 11895cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 11905cd73fbdSAndrew Lunn 11915cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 11925cd73fbdSAndrew Lunn u64 resource_id, 11935cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 11945cd73fbdSAndrew Lunn void *occ_get_priv); 11955cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 11965cd73fbdSAndrew Lunn u64 resource_id); 119797c82c23SAndrew Lunn struct devlink_region * 119897c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds, 119997c82c23SAndrew Lunn const struct devlink_region_ops *ops, 120097c82c23SAndrew Lunn u32 region_max_snapshots, u64 region_size); 120108156ba4SAndrew Lunn struct devlink_region * 120208156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds, 120308156ba4SAndrew Lunn int port, 120408156ba4SAndrew Lunn const struct devlink_port_region_ops *ops, 120508156ba4SAndrew Lunn u32 region_max_snapshots, u64 region_size); 120697c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region); 120797c82c23SAndrew Lunn 1208e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 12095cd73fbdSAndrew Lunn 12106b297524SAndrew Lunn struct dsa_devlink_priv { 12116b297524SAndrew Lunn struct dsa_switch *ds; 1212c8f0b869SBen Hutchings }; 1213c8f0b869SBen Hutchings 1214ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 1215ccc3e6b0SAndrew Lunn { 1216ccc3e6b0SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1217ccc3e6b0SAndrew Lunn 1218ccc3e6b0SAndrew Lunn return dl_priv->ds; 1219ccc3e6b0SAndrew Lunn } 1220ccc3e6b0SAndrew Lunn 12217d1e2a10SAndrew Lunn static inline 12227d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 12237d1e2a10SAndrew Lunn { 12247d1e2a10SAndrew Lunn struct devlink *dl = port->devlink; 12257d1e2a10SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 12267d1e2a10SAndrew Lunn 12277d1e2a10SAndrew Lunn return dl_priv->ds; 12287d1e2a10SAndrew Lunn } 12297d1e2a10SAndrew Lunn 12307d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port) 12317d1e2a10SAndrew Lunn { 12327d1e2a10SAndrew Lunn return port->index; 12337d1e2a10SAndrew Lunn } 12347d1e2a10SAndrew Lunn 1235ab3d408dSFlorian Fainelli struct dsa_switch_driver { 1236ab3d408dSFlorian Fainelli struct list_head list; 1237a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 1238ab3d408dSFlorian Fainelli }; 1239ab3d408dSFlorian Fainelli 124014b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 1241c8f0b869SBen Hutchings 1242f9cef64fSVladimir Oltean typedef int dsa_fdb_walk_cb_t(struct dsa_switch *ds, int port, 1243f9cef64fSVladimir Oltean const unsigned char *addr, u16 vid, 1244f9cef64fSVladimir Oltean struct dsa_db db); 1245f9cef64fSVladimir Oltean 1246f9cef64fSVladimir Oltean int dsa_port_walk_fdbs(struct dsa_switch *ds, int port, dsa_fdb_walk_cb_t cb); 1247f9cef64fSVladimir Oltean int dsa_port_walk_mdbs(struct dsa_switch *ds, int port, dsa_fdb_walk_cb_t cb); 12487e580490SVladimir Oltean bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port, 12497e580490SVladimir Oltean const unsigned char *addr, u16 vid, 12507e580490SVladimir Oltean struct dsa_db db); 12517e580490SVladimir Oltean bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port, 12527e580490SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 12537e580490SVladimir Oltean struct dsa_db db); 1254f9cef64fSVladimir Oltean 125573a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 12569eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 1257c6e970a0SAndrew Lunn { 1258c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 1259717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 1260c6e970a0SAndrew Lunn #endif 1261c6e970a0SAndrew Lunn return false; 1262c6e970a0SAndrew Lunn } 1263c6e970a0SAndrew Lunn 12649790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail 12659790cf20SVladimir Oltean * tags, which don't break dissection) can be treated the same from the 12669790cf20SVladimir Oltean * perspective of the flow dissector. 12679790cf20SVladimir Oltean * 12689790cf20SVladimir Oltean * We need to return: 12699790cf20SVladimir Oltean * - offset: the (B - A) difference between: 12709790cf20SVladimir Oltean * A. the position of the real EtherType and 12719790cf20SVladimir Oltean * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 12729790cf20SVladimir Oltean * after the normal EtherType was supposed to be) 12739790cf20SVladimir Oltean * The offset in bytes is exactly equal to the tagger overhead (and half of 12749790cf20SVladimir Oltean * that, in __be16 shorts). 12759790cf20SVladimir Oltean * 12769790cf20SVladimir Oltean * - proto: the value of the real EtherType. 12779790cf20SVladimir Oltean */ 12789790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 12799790cf20SVladimir Oltean __be16 *proto, int *offset) 12809790cf20SVladimir Oltean { 12819790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 12829790cf20SVladimir Oltean const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 12834e500251SVladimir Oltean int tag_len = ops->needed_headroom; 12849790cf20SVladimir Oltean 12859790cf20SVladimir Oltean *offset = tag_len; 12869790cf20SVladimir Oltean *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 12879790cf20SVladimir Oltean #endif 12889790cf20SVladimir Oltean } 12899790cf20SVladimir Oltean 12904cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 12914cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev) 12924cfab356SFlorian Fainelli { 12934cfab356SFlorian Fainelli int err = -EOPNOTSUPP; 12944cfab356SFlorian Fainelli 12954cfab356SFlorian Fainelli if (!dev->dsa_ptr) 12964cfab356SFlorian Fainelli return err; 12974cfab356SFlorian Fainelli 12984cfab356SFlorian Fainelli if (!dev->dsa_ptr->netdev_ops) 12994cfab356SFlorian Fainelli return err; 13004cfab356SFlorian Fainelli 13014cfab356SFlorian Fainelli return 0; 13024cfab356SFlorian Fainelli } 13034cfab356SFlorian Fainelli 1304a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 13054cfab356SFlorian Fainelli int cmd) 13064cfab356SFlorian Fainelli { 13074cfab356SFlorian Fainelli const struct dsa_netdevice_ops *ops; 13084cfab356SFlorian Fainelli int err; 13094cfab356SFlorian Fainelli 13104cfab356SFlorian Fainelli err = __dsa_netdevice_ops_check(dev); 13114cfab356SFlorian Fainelli if (err) 13124cfab356SFlorian Fainelli return err; 13134cfab356SFlorian Fainelli 13144cfab356SFlorian Fainelli ops = dev->dsa_ptr->netdev_ops; 13154cfab356SFlorian Fainelli 1316a7605370SArnd Bergmann return ops->ndo_eth_ioctl(dev, ifr, cmd); 13174cfab356SFlorian Fainelli } 13184cfab356SFlorian Fainelli #else 1319a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 13204cfab356SFlorian Fainelli int cmd) 13214cfab356SFlorian Fainelli { 13224cfab356SFlorian Fainelli return -EOPNOTSUPP; 13234cfab356SFlorian Fainelli } 13244cfab356SFlorian Fainelli #endif 13254cfab356SFlorian Fainelli 132683c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 132723c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 13280650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds); 13293b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1330a2614140SVladimir Oltean void dsa_flush_workqueue(void); 1331ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 1332ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 1333ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 1334ea825e70SFlorian Fainelli #else 1335ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 1336ea825e70SFlorian Fainelli { 1337ea825e70SFlorian Fainelli return 0; 1338ea825e70SFlorian Fainelli } 1339ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 1340ea825e70SFlorian Fainelli { 1341ea825e70SFlorian Fainelli return 0; 1342ea825e70SFlorian Fainelli } 1343ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 1344ea825e70SFlorian Fainelli 134560724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 1346a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev); 134760724d4bSFlorian Fainelli #else 1348a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev) 1349a5e3c9baSVladimir Oltean { 1350a5e3c9baSVladimir Oltean return false; 1351a5e3c9baSVladimir Oltean } 135260724d4bSFlorian Fainelli #endif 135360724d4bSFlorian Fainelli 135497a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 135511d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1356cf963573SFlorian Fainelli 1357d3b8c049SAndrew Lunn struct dsa_tag_driver { 1358d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 1359d3b8c049SAndrew Lunn struct list_head list; 1360d3b8c049SAndrew Lunn struct module *owner; 1361d3b8c049SAndrew Lunn }; 1362d3b8c049SAndrew Lunn 1363d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1364d3b8c049SAndrew Lunn unsigned int count, 1365d3b8c049SAndrew Lunn struct module *owner); 1366d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1367d3b8c049SAndrew Lunn unsigned int count); 1368d3b8c049SAndrew Lunn 1369d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1370d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 1371d3b8c049SAndrew Lunn { \ 1372d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1373d3b8c049SAndrew Lunn THIS_MODULE); \ 1374d3b8c049SAndrew Lunn return 0; \ 1375d3b8c049SAndrew Lunn } \ 1376d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 1377d3b8c049SAndrew Lunn \ 1378d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 1379d3b8c049SAndrew Lunn { \ 1380d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1381d3b8c049SAndrew Lunn } \ 1382d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 1383d3b8c049SAndrew Lunn 1384d3b8c049SAndrew Lunn /** 1385d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1386d3b8c049SAndrew Lunn * drivers 1387c7d9a675SLuiz Angelo Daros de Luca * @__ops_array: Array of tag driver structures 1388d3b8c049SAndrew Lunn * 1389d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1390d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1391d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1392d3b8c049SAndrew Lunn */ 1393d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 1394d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1395d3b8c049SAndrew Lunn 1396d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1397d3b8c049SAndrew Lunn 1398d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 1399d3b8c049SAndrew Lunn * drivers 1400d3b8c049SAndrew Lunn */ 1401d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 1402d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1403d3b8c049SAndrew Lunn .ops = &__ops, \ 1404d3b8c049SAndrew Lunn } 1405d3b8c049SAndrew Lunn 1406d3b8c049SAndrew Lunn /** 1407d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1408d3b8c049SAndrew Lunn * driver 1409d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 1410d3b8c049SAndrew Lunn * 1411d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1412d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1413d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1414d3b8c049SAndrew Lunn */ 1415d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 1416d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 1417d3b8c049SAndrew Lunn \ 1418d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1419d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 1420d3b8c049SAndrew Lunn }; \ 1421d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 142291da11f8SLennert Buytenhek #endif 1423d3b8c049SAndrew Lunn 1424