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 582bacf93b0SVladimir Oltean #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \ 583bacf93b0SVladimir Oltean dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \ 584bacf93b0SVladimir Oltean if (dsa_port_is_cpu((_dp))) 585bacf93b0SVladimir Oltean 58602bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 58702bc6e54SVivien Didelot { 588d0004a02SVladimir Oltean struct dsa_port *dp; 589c38c5a66SVivien Didelot u32 mask = 0; 59002bc6e54SVivien Didelot 591d0004a02SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) 592d0004a02SVladimir Oltean mask |= BIT(dp->index); 593c38c5a66SVivien Didelot 594c38c5a66SVivien Didelot return mask; 595c8652c83SVivien Didelot } 596c8652c83SVivien Didelot 597465c3de4SVladimir Oltean static inline u32 dsa_cpu_ports(struct dsa_switch *ds) 598465c3de4SVladimir Oltean { 599465c3de4SVladimir Oltean struct dsa_port *cpu_dp; 600465c3de4SVladimir Oltean u32 mask = 0; 601465c3de4SVladimir Oltean 602465c3de4SVladimir Oltean dsa_switch_for_each_cpu_port(cpu_dp, ds) 603465c3de4SVladimir Oltean mask |= BIT(cpu_dp->index); 604465c3de4SVladimir Oltean 605465c3de4SVladimir Oltean return mask; 606465c3de4SVladimir Oltean } 607465c3de4SVladimir Oltean 608c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 609c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 610c5f51765SVivien Didelot { 611c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 612c5f51765SVivien Didelot struct dsa_link *dl; 613c5f51765SVivien Didelot 614c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 615c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 616c5f51765SVivien Didelot return dl->dp->index; 617c5f51765SVivien Didelot 618c5f51765SVivien Didelot return ds->num_ports; 619c5f51765SVivien Didelot } 620c5f51765SVivien Didelot 6213b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 6223b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 6233b8fac5dSVivien Didelot int port) 6243b8fac5dSVivien Didelot { 6253b8fac5dSVivien Didelot if (device == ds->index) 6263b8fac5dSVivien Didelot return port; 6273b8fac5dSVivien Didelot else 628c5f51765SVivien Didelot return dsa_routing_port(ds, device); 6293b8fac5dSVivien Didelot } 6303b8fac5dSVivien Didelot 6313b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 63207073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 633c8f0b869SBen Hutchings { 63407073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 63507073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 63607073c79SVivien Didelot 63707073c79SVivien Didelot if (!cpu_dp) 63807073c79SVivien Didelot return port; 639c8f0b869SBen Hutchings 6403b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 641c8f0b869SBen Hutchings } 642c8f0b869SBen Hutchings 64363609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */ 64463609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port) 64563609c8fSVladimir Oltean { 64663609c8fSVladimir Oltean if (dsa_is_unused_port(ds, port)) 64763609c8fSVladimir Oltean return false; 64863609c8fSVladimir Oltean 64963609c8fSVladimir Oltean return port == dsa_upstream_port(ds, port); 65063609c8fSVladimir Oltean } 65163609c8fSVladimir Oltean 652d352b20fSTobias Waldekranz /* Return true if this is a DSA port leading away from the CPU */ 653d352b20fSTobias Waldekranz static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port) 654d352b20fSTobias Waldekranz { 655d352b20fSTobias Waldekranz return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port); 656d352b20fSTobias Waldekranz } 657d352b20fSTobias Waldekranz 6587af4a361STobias Waldekranz /* Return the local port used to reach the CPU port */ 6597af4a361STobias Waldekranz static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds) 6607af4a361STobias Waldekranz { 6617af4a361STobias Waldekranz struct dsa_port *dp; 6627af4a361STobias Waldekranz 6637af4a361STobias Waldekranz dsa_switch_for_each_available_port(dp, ds) { 6647af4a361STobias Waldekranz return dsa_upstream_port(ds, dp->index); 6657af4a361STobias Waldekranz } 6667af4a361STobias Waldekranz 6677af4a361STobias Waldekranz return ds->num_ports; 6687af4a361STobias Waldekranz } 6697af4a361STobias Waldekranz 67063609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning 67163609c8fSVladimir Oltean * that the routing port from @downstream_ds to @upstream_ds is also the port 67263609c8fSVladimir Oltean * which @downstream_ds uses to reach its dedicated CPU. 67363609c8fSVladimir Oltean */ 67463609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds, 67563609c8fSVladimir Oltean struct dsa_switch *downstream_ds) 67663609c8fSVladimir Oltean { 67763609c8fSVladimir Oltean int routing_port; 67863609c8fSVladimir Oltean 67963609c8fSVladimir Oltean if (upstream_ds == downstream_ds) 68063609c8fSVladimir Oltean return true; 68163609c8fSVladimir Oltean 68263609c8fSVladimir Oltean routing_port = dsa_routing_port(downstream_ds, upstream_ds->index); 68363609c8fSVladimir Oltean 68463609c8fSVladimir Oltean return dsa_is_upstream_port(downstream_ds, routing_port); 68563609c8fSVladimir Oltean } 68663609c8fSVladimir Oltean 687cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 688cf2d45f5SVladimir Oltean { 689cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 690cf2d45f5SVladimir Oltean 691cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 692cf2d45f5SVladimir Oltean return ds->vlan_filtering; 693cf2d45f5SVladimir Oltean else 694cf2d45f5SVladimir Oltean return dp->vlan_filtering; 695cf2d45f5SVladimir Oltean } 696cf2d45f5SVladimir Oltean 697dedd6a00SVladimir Oltean static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp) 698dedd6a00SVladimir Oltean { 699dedd6a00SVladimir Oltean return dp->lag ? dp->lag->id : 0; 700dedd6a00SVladimir Oltean } 701dedd6a00SVladimir Oltean 702dedd6a00SVladimir Oltean static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp) 703dedd6a00SVladimir Oltean { 704dedd6a00SVladimir Oltean return dp->lag ? dp->lag->dev : NULL; 705dedd6a00SVladimir Oltean } 706dedd6a00SVladimir Oltean 707dedd6a00SVladimir Oltean static inline bool dsa_port_offloads_lag(struct dsa_port *dp, 708dedd6a00SVladimir Oltean const struct dsa_lag *lag) 709dedd6a00SVladimir Oltean { 710dedd6a00SVladimir Oltean return dsa_port_lag_dev_get(dp) == lag->dev; 711dedd6a00SVladimir Oltean } 712dedd6a00SVladimir Oltean 713cc76ce9eSTobias Waldekranz static inline 714cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) 715cc76ce9eSTobias Waldekranz { 716d3eed0e5SVladimir Oltean if (!dp->bridge) 717cc76ce9eSTobias Waldekranz return NULL; 718cc76ce9eSTobias Waldekranz 719dedd6a00SVladimir Oltean if (dp->lag) 720dedd6a00SVladimir Oltean return dp->lag->dev; 721cc76ce9eSTobias Waldekranz else if (dp->hsr_dev) 722cc76ce9eSTobias Waldekranz return dp->hsr_dev; 723cc76ce9eSTobias Waldekranz 724cc76ce9eSTobias Waldekranz return dp->slave; 725cc76ce9eSTobias Waldekranz } 726cc76ce9eSTobias Waldekranz 72736cbf39bSVladimir Oltean static inline struct net_device * 72836cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp) 72936cbf39bSVladimir Oltean { 730d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->dev : NULL; 73136cbf39bSVladimir Oltean } 73236cbf39bSVladimir Oltean 73336cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp) 73436cbf39bSVladimir Oltean { 735d3eed0e5SVladimir Oltean return dp->bridge ? dp->bridge->num : 0; 73636cbf39bSVladimir Oltean } 73736cbf39bSVladimir Oltean 73836cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a, 73936cbf39bSVladimir Oltean const struct dsa_port *b) 74036cbf39bSVladimir Oltean { 74136cbf39bSVladimir Oltean struct net_device *br_a = dsa_port_bridge_dev_get(a); 74236cbf39bSVladimir Oltean struct net_device *br_b = dsa_port_bridge_dev_get(b); 74336cbf39bSVladimir Oltean 74436cbf39bSVladimir Oltean /* Standalone ports are not in the same bridge with one another */ 74536cbf39bSVladimir Oltean return (!br_a || !br_b) ? false : (br_a == br_b); 74636cbf39bSVladimir Oltean } 74736cbf39bSVladimir Oltean 7486a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp, 7496a43cba3SVladimir Oltean const struct net_device *dev) 7506a43cba3SVladimir Oltean { 7516a43cba3SVladimir Oltean return dsa_port_to_bridge_port(dp) == dev; 7526a43cba3SVladimir Oltean } 7536a43cba3SVladimir Oltean 7546a43cba3SVladimir Oltean static inline bool 7556a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp, 7566a43cba3SVladimir Oltean const struct net_device *bridge_dev) 7576a43cba3SVladimir Oltean { 7586a43cba3SVladimir Oltean /* DSA ports connected to a bridge, and event was emitted 7596a43cba3SVladimir Oltean * for the bridge. 7606a43cba3SVladimir Oltean */ 7616a43cba3SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge_dev; 7626a43cba3SVladimir Oltean } 7636a43cba3SVladimir Oltean 764d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp, 765d3eed0e5SVladimir Oltean const struct dsa_bridge *bridge) 766d3eed0e5SVladimir Oltean { 767d3eed0e5SVladimir Oltean return dsa_port_bridge_dev_get(dp) == bridge->dev; 768d3eed0e5SVladimir Oltean } 769d3eed0e5SVladimir Oltean 7706a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */ 7716a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst, 7726a43cba3SVladimir Oltean const struct net_device *dev) 7736a43cba3SVladimir Oltean { 7746a43cba3SVladimir Oltean struct dsa_port *dp; 7756a43cba3SVladimir Oltean 7766a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7776a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_port(dp, dev)) 7786a43cba3SVladimir Oltean return true; 7796a43cba3SVladimir Oltean 7806a43cba3SVladimir Oltean return false; 7816a43cba3SVladimir Oltean } 7826a43cba3SVladimir Oltean 7836a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */ 7846a43cba3SVladimir Oltean static inline bool 7856a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst, 7866a43cba3SVladimir Oltean const struct net_device *bridge_dev) 7876a43cba3SVladimir Oltean { 7886a43cba3SVladimir Oltean struct dsa_port *dp; 7896a43cba3SVladimir Oltean 7906a43cba3SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) 7916a43cba3SVladimir Oltean if (dsa_port_offloads_bridge_dev(dp, bridge_dev)) 7926a43cba3SVladimir Oltean return true; 7936a43cba3SVladimir Oltean 7946a43cba3SVladimir Oltean return false; 7956a43cba3SVladimir Oltean } 7966a43cba3SVladimir Oltean 7972bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 7982bedde1aSArkadi Sharshevsky bool is_static, void *data); 7999d490b4eSVivien Didelot struct dsa_switch_ops { 80053da0ebaSVladimir Oltean /* 80153da0ebaSVladimir Oltean * Tagging protocol helpers called for the CPU ports and DSA links. 80253da0ebaSVladimir Oltean * @get_tag_protocol retrieves the initial tagging protocol and is 80353da0ebaSVladimir Oltean * mandatory. Switches which can operate using multiple tagging 80453da0ebaSVladimir Oltean * protocols should implement @change_tag_protocol and report in 80553da0ebaSVladimir Oltean * @get_tag_protocol the tagger in current use. 80653da0ebaSVladimir Oltean */ 8075ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 8084d776482SFlorian Fainelli int port, 8094d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 810bacf93b0SVladimir Oltean int (*change_tag_protocol)(struct dsa_switch *ds, 81153da0ebaSVladimir Oltean enum dsa_tag_protocol proto); 812dc452a47SVladimir Oltean /* 813dc452a47SVladimir Oltean * Method for switch drivers to connect to the tagging protocol driver 814dc452a47SVladimir Oltean * in current use. The switch driver can provide handlers for certain 815dc452a47SVladimir Oltean * types of packets for switch management. 816dc452a47SVladimir Oltean */ 817dc452a47SVladimir Oltean int (*connect_tag_protocol)(struct dsa_switch *ds, 818dc452a47SVladimir Oltean enum dsa_tag_protocol proto); 8197b314362SAndrew Lunn 820fd292c18SVladimir Oltean /* Optional switch-wide initialization and destruction methods */ 821c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 8225e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 823fd292c18SVladimir Oltean 824fd292c18SVladimir Oltean /* Per-port initialization and destruction methods. Mandatory if the 825fd292c18SVladimir Oltean * driver registers devlink port regions, optional otherwise. 826fd292c18SVladimir Oltean */ 827fd292c18SVladimir Oltean int (*port_setup)(struct dsa_switch *ds, int port); 828fd292c18SVladimir Oltean void (*port_teardown)(struct dsa_switch *ds, int port); 829fd292c18SVladimir Oltean 8306819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 831c8f0b869SBen Hutchings 832c8f0b869SBen Hutchings /* 833c8f0b869SBen Hutchings * Access to the switch's PHY registers. 834c8f0b869SBen Hutchings */ 835c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 836c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 837c8f0b869SBen Hutchings int regnum, u16 val); 838c8f0b869SBen Hutchings 839c8f0b869SBen Hutchings /* 840ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 841ec9436baSFlorian Fainelli */ 842ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 843ec9436baSFlorian Fainelli struct phy_device *phydev); 844ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 845ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 846ec9436baSFlorian Fainelli 847ec9436baSFlorian Fainelli /* 84811d8f3ddSFlorian Fainelli * PHYLINK integration 84911d8f3ddSFlorian Fainelli */ 850072eea6cSRussell King (Oracle) void (*phylink_get_caps)(struct dsa_switch *ds, int port, 851072eea6cSRussell King (Oracle) struct phylink_config *config); 85211d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 85311d8f3ddSFlorian Fainelli unsigned long *supported, 85411d8f3ddSFlorian Fainelli struct phylink_link_state *state); 855bde01822SRussell King (Oracle) struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds, 856bde01822SRussell King (Oracle) int port, 857bde01822SRussell King (Oracle) phy_interface_t iface); 85811d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 85911d8f3ddSFlorian Fainelli struct phylink_link_state *state); 86011d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 86111d8f3ddSFlorian Fainelli unsigned int mode, 86211d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 86311d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 86411d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 86511d8f3ddSFlorian Fainelli unsigned int mode, 86611d8f3ddSFlorian Fainelli phy_interface_t interface); 86711d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 86811d8f3ddSFlorian Fainelli unsigned int mode, 86911d8f3ddSFlorian Fainelli phy_interface_t interface, 8705b502a7bSRussell King struct phy_device *phydev, 8715b502a7bSRussell King int speed, int duplex, 8725b502a7bSRussell King bool tx_pause, bool rx_pause); 87311d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 87411d8f3ddSFlorian Fainelli struct phylink_link_state *state); 87511d8f3ddSFlorian Fainelli /* 876c2ec5f2eSOleksij Rempel * Port statistics counters. 877c8f0b869SBen Hutchings */ 87889f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 87989f09048SFlorian Fainelli u32 stringset, uint8_t *data); 880c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 881c8f0b869SBen Hutchings int port, uint64_t *data); 88289f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 883cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 884cf963573SFlorian Fainelli int port, uint64_t *data); 885487d3855SAlvin Šipraga void (*get_eth_phy_stats)(struct dsa_switch *ds, int port, 886487d3855SAlvin Šipraga struct ethtool_eth_phy_stats *phy_stats); 887487d3855SAlvin Šipraga void (*get_eth_mac_stats)(struct dsa_switch *ds, int port, 888487d3855SAlvin Šipraga struct ethtool_eth_mac_stats *mac_stats); 889487d3855SAlvin Šipraga void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port, 890487d3855SAlvin Šipraga struct ethtool_eth_ctrl_stats *ctrl_stats); 891*67f38b1cSClément Léger void (*get_rmon_stats)(struct dsa_switch *ds, int port, 892*67f38b1cSClément Léger struct ethtool_rmon_stats *rmon_stats, 893*67f38b1cSClément Léger const struct ethtool_rmon_hist_range **ranges); 894c2ec5f2eSOleksij Rempel void (*get_stats64)(struct dsa_switch *ds, int port, 895c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s); 896a71acad9SOleksij Rempel void (*self_test)(struct dsa_switch *ds, int port, 897a71acad9SOleksij Rempel struct ethtool_test *etest, u64 *data); 89824462549SFlorian Fainelli 89924462549SFlorian Fainelli /* 90019e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 90119e57c4eSFlorian Fainelli */ 90219e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 90319e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 90419e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 90519e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 90619e57c4eSFlorian Fainelli 90719e57c4eSFlorian Fainelli /* 9080336369dSBrandon Streiff * ethtool timestamp info 9090336369dSBrandon Streiff */ 9100336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 9110336369dSBrandon Streiff struct ethtool_ts_info *ts); 9120336369dSBrandon Streiff 9130336369dSBrandon Streiff /* 914d538eca8SVladimir Oltean * DCB ops 915d538eca8SVladimir Oltean */ 916d538eca8SVladimir Oltean int (*port_get_default_prio)(struct dsa_switch *ds, int port); 917d538eca8SVladimir Oltean int (*port_set_default_prio)(struct dsa_switch *ds, int port, 918d538eca8SVladimir Oltean u8 prio); 91947d75f78SVladimir Oltean int (*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp); 92047d75f78SVladimir Oltean int (*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp, 92147d75f78SVladimir Oltean u8 prio); 92247d75f78SVladimir Oltean int (*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp, 92347d75f78SVladimir Oltean u8 prio); 924d538eca8SVladimir Oltean 925d538eca8SVladimir Oltean /* 92624462549SFlorian Fainelli * Suspend and resume 92724462549SFlorian Fainelli */ 92824462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 92924462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 930b2f2af21SFlorian Fainelli 931b2f2af21SFlorian Fainelli /* 932b2f2af21SFlorian Fainelli * Port enable/disable 933b2f2af21SFlorian Fainelli */ 934b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 935b2f2af21SFlorian Fainelli struct phy_device *phy); 93675104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 9377905288fSFlorian Fainelli 9387905288fSFlorian Fainelli /* 93908f50061SVivien Didelot * Port's MAC EEE settings 9407905288fSFlorian Fainelli */ 94108f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 9427905288fSFlorian Fainelli struct ethtool_eee *e); 94308f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 9447905288fSFlorian Fainelli struct ethtool_eee *e); 94551579c3fSGuenter Roeck 9466793abb4SGuenter Roeck /* EEPROM access */ 9476793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 9486793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 9496793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 9506793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 9516793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 9523d762a0fSGuenter Roeck 9533d762a0fSGuenter Roeck /* 9543d762a0fSGuenter Roeck * Register access. 9553d762a0fSGuenter Roeck */ 9563d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 9573d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 9583d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 959b73adef6SFlorian Fainelli 960b73adef6SFlorian Fainelli /* 961e358bef7SVladimir Oltean * Upper device tracking. 962e358bef7SVladimir Oltean */ 963e358bef7SVladimir Oltean int (*port_prechangeupper)(struct dsa_switch *ds, int port, 964e358bef7SVladimir Oltean struct netdev_notifier_changeupper_info *info); 965e358bef7SVladimir Oltean 966e358bef7SVladimir Oltean /* 967b73adef6SFlorian Fainelli * Bridge integration 968b73adef6SFlorian Fainelli */ 96934a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 97071327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 971b079922bSVladimir Oltean struct dsa_bridge bridge, 97206b9cce4SVladimir Oltean bool *tx_fwd_offload, 97306b9cce4SVladimir Oltean struct netlink_ext_ack *extack); 974f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 975d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 97643c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 977b73adef6SFlorian Fainelli u8 state); 9787414af30STobias Waldekranz int (*port_mst_state_set)(struct dsa_switch *ds, int port, 9797414af30STobias Waldekranz const struct switchdev_mst_state *state); 980732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 9817414af30STobias Waldekranz int (*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid); 982a8b659e7SVladimir Oltean int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port, 983a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 984a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 985a8b659e7SVladimir Oltean int (*port_bridge_flags)(struct dsa_switch *ds, int port, 986a8b659e7SVladimir Oltean struct switchdev_brport_flags flags, 987a8b659e7SVladimir Oltean struct netlink_ext_ack *extack); 98872c3b0c7SVladimir Oltean void (*port_set_host_flood)(struct dsa_switch *ds, int port, 98972c3b0c7SVladimir Oltean bool uc, bool mc); 9902a778e1bSVivien Didelot 9912a778e1bSVivien Didelot /* 99211149536SVivien Didelot * VLAN support 99311149536SVivien Didelot */ 994fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 99589153ed6SVladimir Oltean bool vlan_filtering, 99689153ed6SVladimir Oltean struct netlink_ext_ack *extack); 9971958d581SVladimir Oltean int (*port_vlan_add)(struct dsa_switch *ds, int port, 99831046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 99931046a5fSVladimir Oltean struct netlink_ext_ack *extack); 100076e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 100176e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 10028e6598a7STobias Waldekranz int (*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge, 10038e6598a7STobias Waldekranz const struct switchdev_vlan_msti *msti); 10048e6598a7STobias Waldekranz 100511149536SVivien Didelot /* 10062a778e1bSVivien Didelot * Forwarding database 10072a778e1bSVivien Didelot */ 10081b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 1009c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 1010c2693363SVladimir Oltean struct dsa_db db); 10112a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 1012c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 1013c2693363SVladimir Oltean struct dsa_db db); 1014ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 10152bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 1016e212fa7cSVladimir Oltean int (*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag, 1017c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 1018c2693363SVladimir Oltean struct dsa_db db); 1019e212fa7cSVladimir Oltean int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag, 1020c2693363SVladimir Oltean const unsigned char *addr, u16 vid, 1021c2693363SVladimir Oltean struct dsa_db db); 10228df30255SVivien Didelot 10238df30255SVivien Didelot /* 10248df30255SVivien Didelot * Multicast database 10258df30255SVivien Didelot */ 1026a52b2da7SVladimir Oltean int (*port_mdb_add)(struct dsa_switch *ds, int port, 1027c2693363SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 1028c2693363SVladimir Oltean struct dsa_db db); 10298df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 1030c2693363SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 1031c2693363SVladimir Oltean struct dsa_db db); 1032bf9f2648SFlorian Fainelli /* 1033bf9f2648SFlorian Fainelli * RXNFC 1034bf9f2648SFlorian Fainelli */ 1035bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 1036bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 1037bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 1038bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 1039f50f2127SFlorian Fainelli 1040f50f2127SFlorian Fainelli /* 1041f50f2127SFlorian Fainelli * TC integration 1042f50f2127SFlorian Fainelli */ 1043ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 1044ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 1045ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 1046ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 1047ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 1048ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 1049f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 1050f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 10510148bb50SVladimir Oltean bool ingress, struct netlink_ext_ack *extack); 1052f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 1053f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 105434297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 105534297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 105634297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 105747d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 105847d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 105940ef2c93SVivien Didelot 106040ef2c93SVivien Didelot /* 106140ef2c93SVivien Didelot * Cross-chip operations 106240ef2c93SVivien Didelot */ 1063f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 1064f66a6a69SVladimir Oltean int sw_index, int port, 106506b9cce4SVladimir Oltean struct dsa_bridge bridge, 106606b9cce4SVladimir Oltean struct netlink_ext_ack *extack); 1067f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 1068f66a6a69SVladimir Oltean int sw_index, int port, 1069d3eed0e5SVladimir Oltean struct dsa_bridge bridge); 1070058102a6STobias Waldekranz int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 1071058102a6STobias Waldekranz int port); 1072058102a6STobias Waldekranz int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 1073dedd6a00SVladimir Oltean int port, struct dsa_lag lag, 1074058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 1075058102a6STobias Waldekranz int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 1076dedd6a00SVladimir Oltean int port, struct dsa_lag lag); 10770336369dSBrandon Streiff 10780336369dSBrandon Streiff /* 10790336369dSBrandon Streiff * PTP functionality 10800336369dSBrandon Streiff */ 10810336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 10820336369dSBrandon Streiff struct ifreq *ifr); 10830336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 10840336369dSBrandon Streiff struct ifreq *ifr); 10855c5416f5SYangbo Lu void (*port_txtstamp)(struct dsa_switch *ds, int port, 10865c5416f5SYangbo Lu struct sk_buff *skb); 108790af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 108890af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 108997a69a0dSVladimir Oltean 10900f06b855SAndrew Lunn /* Devlink parameters, etc */ 10916b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 10926b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10936b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 10946b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 10950f06b855SAndrew Lunn int (*devlink_info_get)(struct dsa_switch *ds, 10960f06b855SAndrew Lunn struct devlink_info_req *req, 10970f06b855SAndrew Lunn struct netlink_ext_ack *extack); 10982a6ef763SVladimir Oltean int (*devlink_sb_pool_get)(struct dsa_switch *ds, 10992a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 11002a6ef763SVladimir Oltean struct devlink_sb_pool_info *pool_info); 11012a6ef763SVladimir Oltean int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 11022a6ef763SVladimir Oltean u16 pool_index, u32 size, 11032a6ef763SVladimir Oltean enum devlink_sb_threshold_type threshold_type, 11042a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 11052a6ef763SVladimir Oltean int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 11062a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 11072a6ef763SVladimir Oltean u32 *p_threshold); 11082a6ef763SVladimir Oltean int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 11092a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 11102a6ef763SVladimir Oltean u32 threshold, 11112a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 11122a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 11132a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 11142a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 11152a6ef763SVladimir Oltean u16 *p_pool_index, u32 *p_threshold); 11162a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 11172a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 11182a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 11192a6ef763SVladimir Oltean u16 pool_index, u32 threshold, 11202a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 11212a6ef763SVladimir Oltean int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 11222a6ef763SVladimir Oltean unsigned int sb_index); 11232a6ef763SVladimir Oltean int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 11242a6ef763SVladimir Oltean unsigned int sb_index); 11252a6ef763SVladimir Oltean int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 11262a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 11272a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 11282a6ef763SVladimir Oltean int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 11292a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 11302a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 11312a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 1132bfcb8132SVladimir Oltean 1133bfcb8132SVladimir Oltean /* 1134bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 1135bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 1136bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 1137ab88d64aSRandy Dunlap * method needs to do so privately. 1138bfcb8132SVladimir Oltean */ 1139bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 1140bfcb8132SVladimir Oltean int new_mtu); 1141bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 1142058102a6STobias Waldekranz 1143058102a6STobias Waldekranz /* 1144058102a6STobias Waldekranz * LAG integration 1145058102a6STobias Waldekranz */ 1146058102a6STobias Waldekranz int (*port_lag_change)(struct dsa_switch *ds, int port); 1147058102a6STobias Waldekranz int (*port_lag_join)(struct dsa_switch *ds, int port, 1148dedd6a00SVladimir Oltean struct dsa_lag lag, 1149058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 1150058102a6STobias Waldekranz int (*port_lag_leave)(struct dsa_switch *ds, int port, 1151dedd6a00SVladimir Oltean struct dsa_lag lag); 115218596f50SGeorge McCollister 115318596f50SGeorge McCollister /* 115418596f50SGeorge McCollister * HSR integration 115518596f50SGeorge McCollister */ 115618596f50SGeorge McCollister int (*port_hsr_join)(struct dsa_switch *ds, int port, 115718596f50SGeorge McCollister struct net_device *hsr); 115818596f50SGeorge McCollister int (*port_hsr_leave)(struct dsa_switch *ds, int port, 115918596f50SGeorge McCollister struct net_device *hsr); 1160c595c433SHoratiu Vultur 1161c595c433SHoratiu Vultur /* 1162c595c433SHoratiu Vultur * MRP integration 1163c595c433SHoratiu Vultur */ 1164c595c433SHoratiu Vultur int (*port_mrp_add)(struct dsa_switch *ds, int port, 1165c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1166c595c433SHoratiu Vultur int (*port_mrp_del)(struct dsa_switch *ds, int port, 1167c595c433SHoratiu Vultur const struct switchdev_obj_mrp *mrp); 1168c595c433SHoratiu Vultur int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port, 1169c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 1170c595c433SHoratiu Vultur int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port, 1171c595c433SHoratiu Vultur const struct switchdev_obj_ring_role_mrp *mrp); 11725da11eb4SVladimir Oltean 11735da11eb4SVladimir Oltean /* 11745da11eb4SVladimir Oltean * tag_8021q operations 11755da11eb4SVladimir Oltean */ 11765da11eb4SVladimir Oltean int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid, 11775da11eb4SVladimir Oltean u16 flags); 11785da11eb4SVladimir Oltean int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid); 1179295ab96fSVladimir Oltean 1180295ab96fSVladimir Oltean /* 1181295ab96fSVladimir Oltean * DSA master tracking operations 1182295ab96fSVladimir Oltean */ 1183295ab96fSVladimir Oltean void (*master_state_change)(struct dsa_switch *ds, 1184295ab96fSVladimir Oltean const struct net_device *master, 1185295ab96fSVladimir Oltean bool operational); 11866b297524SAndrew Lunn }; 11876b297524SAndrew Lunn 11886b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 11896b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 11906b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 11916b297524SAndrew Lunn 11926b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 11936b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 11946b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 11956b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 11966b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 11976b297524SAndrew Lunn const struct devlink_param *params, 11986b297524SAndrew Lunn size_t params_count); 11996b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 12006b297524SAndrew Lunn const struct devlink_param *params, 12016b297524SAndrew Lunn size_t params_count); 12025cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 12035cd73fbdSAndrew Lunn const char *resource_name, 12045cd73fbdSAndrew Lunn u64 resource_size, 12055cd73fbdSAndrew Lunn u64 resource_id, 12065cd73fbdSAndrew Lunn u64 parent_resource_id, 12075cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 12085cd73fbdSAndrew Lunn 12095cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 12105cd73fbdSAndrew Lunn 12115cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 12125cd73fbdSAndrew Lunn u64 resource_id, 12135cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 12145cd73fbdSAndrew Lunn void *occ_get_priv); 12155cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 12165cd73fbdSAndrew Lunn u64 resource_id); 121797c82c23SAndrew Lunn struct devlink_region * 121897c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds, 121997c82c23SAndrew Lunn const struct devlink_region_ops *ops, 122097c82c23SAndrew Lunn u32 region_max_snapshots, u64 region_size); 122108156ba4SAndrew Lunn struct devlink_region * 122208156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds, 122308156ba4SAndrew Lunn int port, 122408156ba4SAndrew Lunn const struct devlink_port_region_ops *ops, 122508156ba4SAndrew Lunn u32 region_max_snapshots, u64 region_size); 122697c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region); 122797c82c23SAndrew Lunn 1228e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 12295cd73fbdSAndrew Lunn 12306b297524SAndrew Lunn struct dsa_devlink_priv { 12316b297524SAndrew Lunn struct dsa_switch *ds; 1232c8f0b869SBen Hutchings }; 1233c8f0b869SBen Hutchings 1234ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 1235ccc3e6b0SAndrew Lunn { 1236ccc3e6b0SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 1237ccc3e6b0SAndrew Lunn 1238ccc3e6b0SAndrew Lunn return dl_priv->ds; 1239ccc3e6b0SAndrew Lunn } 1240ccc3e6b0SAndrew Lunn 12417d1e2a10SAndrew Lunn static inline 12427d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 12437d1e2a10SAndrew Lunn { 12447d1e2a10SAndrew Lunn struct devlink *dl = port->devlink; 12457d1e2a10SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 12467d1e2a10SAndrew Lunn 12477d1e2a10SAndrew Lunn return dl_priv->ds; 12487d1e2a10SAndrew Lunn } 12497d1e2a10SAndrew Lunn 12507d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port) 12517d1e2a10SAndrew Lunn { 12527d1e2a10SAndrew Lunn return port->index; 12537d1e2a10SAndrew Lunn } 12547d1e2a10SAndrew Lunn 1255ab3d408dSFlorian Fainelli struct dsa_switch_driver { 1256ab3d408dSFlorian Fainelli struct list_head list; 1257a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 1258ab3d408dSFlorian Fainelli }; 1259ab3d408dSFlorian Fainelli 126014b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 1261c8f0b869SBen Hutchings 12627e580490SVladimir Oltean bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port, 12637e580490SVladimir Oltean const unsigned char *addr, u16 vid, 12647e580490SVladimir Oltean struct dsa_db db); 12657e580490SVladimir Oltean bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port, 12667e580490SVladimir Oltean const struct switchdev_obj_port_mdb *mdb, 12677e580490SVladimir Oltean struct dsa_db db); 1268f9cef64fSVladimir Oltean 126973a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 12709eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 1271c6e970a0SAndrew Lunn { 1272c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 1273717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 1274c6e970a0SAndrew Lunn #endif 1275c6e970a0SAndrew Lunn return false; 1276c6e970a0SAndrew Lunn } 1277c6e970a0SAndrew Lunn 12789790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail 12799790cf20SVladimir Oltean * tags, which don't break dissection) can be treated the same from the 12809790cf20SVladimir Oltean * perspective of the flow dissector. 12819790cf20SVladimir Oltean * 12829790cf20SVladimir Oltean * We need to return: 12839790cf20SVladimir Oltean * - offset: the (B - A) difference between: 12849790cf20SVladimir Oltean * A. the position of the real EtherType and 12859790cf20SVladimir Oltean * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 12869790cf20SVladimir Oltean * after the normal EtherType was supposed to be) 12879790cf20SVladimir Oltean * The offset in bytes is exactly equal to the tagger overhead (and half of 12889790cf20SVladimir Oltean * that, in __be16 shorts). 12899790cf20SVladimir Oltean * 12909790cf20SVladimir Oltean * - proto: the value of the real EtherType. 12919790cf20SVladimir Oltean */ 12929790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 12939790cf20SVladimir Oltean __be16 *proto, int *offset) 12949790cf20SVladimir Oltean { 12959790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 12969790cf20SVladimir Oltean const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 12974e500251SVladimir Oltean int tag_len = ops->needed_headroom; 12989790cf20SVladimir Oltean 12999790cf20SVladimir Oltean *offset = tag_len; 13009790cf20SVladimir Oltean *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 13019790cf20SVladimir Oltean #endif 13029790cf20SVladimir Oltean } 13039790cf20SVladimir Oltean 13044cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 13054cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev) 13064cfab356SFlorian Fainelli { 13074cfab356SFlorian Fainelli int err = -EOPNOTSUPP; 13084cfab356SFlorian Fainelli 13094cfab356SFlorian Fainelli if (!dev->dsa_ptr) 13104cfab356SFlorian Fainelli return err; 13114cfab356SFlorian Fainelli 13124cfab356SFlorian Fainelli if (!dev->dsa_ptr->netdev_ops) 13134cfab356SFlorian Fainelli return err; 13144cfab356SFlorian Fainelli 13154cfab356SFlorian Fainelli return 0; 13164cfab356SFlorian Fainelli } 13174cfab356SFlorian Fainelli 1318a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 13194cfab356SFlorian Fainelli int cmd) 13204cfab356SFlorian Fainelli { 13214cfab356SFlorian Fainelli const struct dsa_netdevice_ops *ops; 13224cfab356SFlorian Fainelli int err; 13234cfab356SFlorian Fainelli 13244cfab356SFlorian Fainelli err = __dsa_netdevice_ops_check(dev); 13254cfab356SFlorian Fainelli if (err) 13264cfab356SFlorian Fainelli return err; 13274cfab356SFlorian Fainelli 13284cfab356SFlorian Fainelli ops = dev->dsa_ptr->netdev_ops; 13294cfab356SFlorian Fainelli 1330a7605370SArnd Bergmann return ops->ndo_eth_ioctl(dev, ifr, cmd); 13314cfab356SFlorian Fainelli } 13324cfab356SFlorian Fainelli #else 1333a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr, 13344cfab356SFlorian Fainelli int cmd) 13354cfab356SFlorian Fainelli { 13364cfab356SFlorian Fainelli return -EOPNOTSUPP; 13374cfab356SFlorian Fainelli } 13384cfab356SFlorian Fainelli #endif 13394cfab356SFlorian Fainelli 134083c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 134123c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 13420650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds); 13433b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 1344a2614140SVladimir Oltean void dsa_flush_workqueue(void); 1345ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 1346ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 1347ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 1348ea825e70SFlorian Fainelli #else 1349ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 1350ea825e70SFlorian Fainelli { 1351ea825e70SFlorian Fainelli return 0; 1352ea825e70SFlorian Fainelli } 1353ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 1354ea825e70SFlorian Fainelli { 1355ea825e70SFlorian Fainelli return 0; 1356ea825e70SFlorian Fainelli } 1357ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 1358ea825e70SFlorian Fainelli 135960724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 1360a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev); 136160724d4bSFlorian Fainelli #else 1362a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev) 1363a5e3c9baSVladimir Oltean { 1364a5e3c9baSVladimir Oltean return false; 1365a5e3c9baSVladimir Oltean } 136660724d4bSFlorian Fainelli #endif 136760724d4bSFlorian Fainelli 136897a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 136911d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 1370cf963573SFlorian Fainelli 1371d3b8c049SAndrew Lunn struct dsa_tag_driver { 1372d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 1373d3b8c049SAndrew Lunn struct list_head list; 1374d3b8c049SAndrew Lunn struct module *owner; 1375d3b8c049SAndrew Lunn }; 1376d3b8c049SAndrew Lunn 1377d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 1378d3b8c049SAndrew Lunn unsigned int count, 1379d3b8c049SAndrew Lunn struct module *owner); 1380d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 1381d3b8c049SAndrew Lunn unsigned int count); 1382d3b8c049SAndrew Lunn 1383d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 1384d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 1385d3b8c049SAndrew Lunn { \ 1386d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 1387d3b8c049SAndrew Lunn THIS_MODULE); \ 1388d3b8c049SAndrew Lunn return 0; \ 1389d3b8c049SAndrew Lunn } \ 1390d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 1391d3b8c049SAndrew Lunn \ 1392d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 1393d3b8c049SAndrew Lunn { \ 1394d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 1395d3b8c049SAndrew Lunn } \ 1396d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 1397d3b8c049SAndrew Lunn 1398d3b8c049SAndrew Lunn /** 1399d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 1400d3b8c049SAndrew Lunn * drivers 1401c7d9a675SLuiz Angelo Daros de Luca * @__ops_array: Array of tag driver structures 1402d3b8c049SAndrew Lunn * 1403d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1404d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1405d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1406d3b8c049SAndrew Lunn */ 1407d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 1408d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 1409d3b8c049SAndrew Lunn 1410d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1411d3b8c049SAndrew Lunn 1412d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 1413d3b8c049SAndrew Lunn * drivers 1414d3b8c049SAndrew Lunn */ 1415d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 1416d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1417d3b8c049SAndrew Lunn .ops = &__ops, \ 1418d3b8c049SAndrew Lunn } 1419d3b8c049SAndrew Lunn 1420d3b8c049SAndrew Lunn /** 1421d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1422d3b8c049SAndrew Lunn * driver 1423d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 1424d3b8c049SAndrew Lunn * 1425d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1426d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1427d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1428d3b8c049SAndrew Lunn */ 1429d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 1430d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 1431d3b8c049SAndrew Lunn \ 1432d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1433d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 1434d3b8c049SAndrew Lunn }; \ 1435d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 143691da11f8SLennert Buytenhek #endif 1437d3b8c049SAndrew Lunn 1438