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 50*7c83a7c5SVladimir Oltean #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE 20 510b42f033SAndrew Lunn 52ac7a04c3SFlorian Fainelli enum dsa_tag_protocol { 530b42f033SAndrew Lunn DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE, 540b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE, 550b42f033SAndrew Lunn DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE, 560b42f033SAndrew Lunn DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE, 570b42f033SAndrew Lunn DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE, 580b42f033SAndrew Lunn DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE, 590b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE, 600b42f033SAndrew Lunn DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE, 610b42f033SAndrew Lunn DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE, 620b42f033SAndrew Lunn DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE, 630b42f033SAndrew Lunn DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE, 640b42f033SAndrew Lunn DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE, 65f9bbe447SVladimir Oltean DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE, 66227d07a0SVladimir Oltean DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE, 67016e43a2STristram Ha DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE, 688dce89aaSVladimir Oltean DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE, 6948fda74fSOleksij Rempel DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE, 70efd7fe68SLinus Walleij DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE, 7101ef09caSKurt Kanzenbach DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE, 7254a52823SGeorge McCollister DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE, 73*7c83a7c5SVladimir Oltean DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE, 74ac7a04c3SFlorian Fainelli }; 755037d532SFlorian Fainelli 765075314eSAlexander Duyck struct packet_type; 7790af1059SBrandon Streiff struct dsa_switch; 783e8a72d1SFlorian Fainelli 7968277a2cSJohn Crispin struct dsa_device_ops { 8068277a2cSJohn Crispin struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev); 8168277a2cSJohn Crispin struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev, 8289e49506SFlorian Westphal struct packet_type *pt); 832e8cb1b3SVladimir Oltean void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto, 84598a9680SJohn Crispin int *offset); 85cc1939e4SVladimir Oltean /* Used to determine which traffic should match the DSA filter in 86cc1939e4SVladimir Oltean * eth_type_trans, and which, if any, should bypass it and be processed 87cc1939e4SVladimir Oltean * as regular on the master net device. 88cc1939e4SVladimir Oltean */ 89cc1939e4SVladimir Oltean bool (*filter)(const struct sk_buff *skb, struct net_device *dev); 90a5dd3087SAndrew Lunn unsigned int overhead; 91875138f8SAndrew Lunn const char *name; 92056eed2fSAndrew Lunn enum dsa_tag_protocol proto; 93c3975400SVladimir Oltean /* Some tagging protocols either mangle or shift the destination MAC 94c3975400SVladimir Oltean * address, in which case the DSA master would drop packets on ingress 95c3975400SVladimir Oltean * if what it understands out of the destination MAC address is not in 96c3975400SVladimir Oltean * its RX filter. 97c3975400SVladimir Oltean */ 98c3975400SVladimir Oltean bool promisc_on_master; 997a6ffe76SVladimir Oltean bool tail_tag; 10068277a2cSJohn Crispin }; 10168277a2cSJohn Crispin 1024cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the 1034cfab356SFlorian Fainelli * DSA layer on top of the DSA CPU/management net_device instance. This is 1044cfab356SFlorian Fainelli * used by the core net_device layer while calling various net_device_ops 1054cfab356SFlorian Fainelli * function pointers. 1064cfab356SFlorian Fainelli */ 1074cfab356SFlorian Fainelli struct dsa_netdevice_ops { 1084cfab356SFlorian Fainelli int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr, 1094cfab356SFlorian Fainelli int cmd); 1104cfab356SFlorian Fainelli }; 1114cfab356SFlorian Fainelli 1120b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-" 1130b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \ 1140b42f033SAndrew Lunn MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE)) 1150b42f033SAndrew Lunn 116b68b0dd0SVladimir Oltean struct dsa_skb_cb { 117b68b0dd0SVladimir Oltean struct sk_buff *clone; 118b68b0dd0SVladimir Oltean }; 119b68b0dd0SVladimir Oltean 120b68b0dd0SVladimir Oltean struct __dsa_skb_cb { 121b68b0dd0SVladimir Oltean struct dsa_skb_cb cb; 122b68b0dd0SVladimir Oltean u8 priv[48 - sizeof(struct dsa_skb_cb)]; 123b68b0dd0SVladimir Oltean }; 124b68b0dd0SVladimir Oltean 125b68b0dd0SVladimir Oltean #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb)) 126b68b0dd0SVladimir Oltean 127b68b0dd0SVladimir Oltean #define DSA_SKB_CB_PRIV(skb) \ 128b68b0dd0SVladimir Oltean ((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv)) 129b68b0dd0SVladimir Oltean 130cf50dcc2SBen Hutchings struct dsa_switch_tree { 13183c0afaeSAndrew Lunn struct list_head list; 13283c0afaeSAndrew Lunn 133f515f192SVivien Didelot /* Notifier chain for switch-wide events */ 134f515f192SVivien Didelot struct raw_notifier_head nh; 135f515f192SVivien Didelot 13683c0afaeSAndrew Lunn /* Tree identifier */ 13749463b7fSVivien Didelot unsigned int index; 13883c0afaeSAndrew Lunn 13983c0afaeSAndrew Lunn /* Number of switches attached to this tree */ 14083c0afaeSAndrew Lunn struct kref refcount; 14183c0afaeSAndrew Lunn 14283c0afaeSAndrew Lunn /* Has this tree been applied to the hardware? */ 143ec15dd42SVivien Didelot bool setup; 14483c0afaeSAndrew Lunn 145357f203bSVladimir Oltean /* Tagging protocol operations */ 146357f203bSVladimir Oltean const struct dsa_device_ops *tag_ops; 147357f203bSVladimir Oltean 148cf50dcc2SBen Hutchings /* 149cf50dcc2SBen Hutchings * Configuration data for the platform device that owns 150cf50dcc2SBen Hutchings * this dsa switch tree instance. 151cf50dcc2SBen Hutchings */ 152cf50dcc2SBen Hutchings struct dsa_platform_data *pd; 153cf85d08fSLennert Buytenhek 154ab8ccae1SVivien Didelot /* List of switch ports */ 155ab8ccae1SVivien Didelot struct list_head ports; 156ab8ccae1SVivien Didelot 157c5f51765SVivien Didelot /* List of DSA links composing the routing table */ 158c5f51765SVivien Didelot struct list_head rtable; 159058102a6STobias Waldekranz 160058102a6STobias Waldekranz /* Maps offloaded LAG netdevs to a zero-based linear ID for 161058102a6STobias Waldekranz * drivers that need it. 162058102a6STobias Waldekranz */ 163058102a6STobias Waldekranz struct net_device **lags; 164058102a6STobias Waldekranz unsigned int lags_len; 165cf50dcc2SBen Hutchings }; 166cf50dcc2SBen Hutchings 167058102a6STobias Waldekranz #define dsa_lags_foreach_id(_id, _dst) \ 168058102a6STobias Waldekranz for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++) \ 169058102a6STobias Waldekranz if ((_dst)->lags[(_id)]) 170058102a6STobias Waldekranz 171058102a6STobias Waldekranz #define dsa_lag_foreach_port(_dp, _dst, _lag) \ 172058102a6STobias Waldekranz list_for_each_entry((_dp), &(_dst)->ports, list) \ 173058102a6STobias Waldekranz if ((_dp)->lag_dev == (_lag)) 174058102a6STobias Waldekranz 175058102a6STobias Waldekranz static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst, 176058102a6STobias Waldekranz unsigned int id) 177058102a6STobias Waldekranz { 178058102a6STobias Waldekranz return dst->lags[id]; 179058102a6STobias Waldekranz } 180058102a6STobias Waldekranz 181058102a6STobias Waldekranz static inline int dsa_lag_id(struct dsa_switch_tree *dst, 182058102a6STobias Waldekranz struct net_device *lag) 183058102a6STobias Waldekranz { 184058102a6STobias Waldekranz unsigned int id; 185058102a6STobias Waldekranz 186058102a6STobias Waldekranz dsa_lags_foreach_id(id, dst) { 187058102a6STobias Waldekranz if (dsa_lag_dev(dst, id) == lag) 188058102a6STobias Waldekranz return id; 189058102a6STobias Waldekranz } 190058102a6STobias Waldekranz 191058102a6STobias Waldekranz return -ENODEV; 192058102a6STobias Waldekranz } 193058102a6STobias Waldekranz 19434297176SVladimir Oltean /* TC matchall action types */ 195f50f2127SFlorian Fainelli enum dsa_port_mall_action_type { 196f50f2127SFlorian Fainelli DSA_PORT_MALL_MIRROR, 19734297176SVladimir Oltean DSA_PORT_MALL_POLICER, 198f50f2127SFlorian Fainelli }; 199f50f2127SFlorian Fainelli 200f50f2127SFlorian Fainelli /* TC mirroring entry */ 201f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry { 202f50f2127SFlorian Fainelli u8 to_local_port; 203f50f2127SFlorian Fainelli bool ingress; 204f50f2127SFlorian Fainelli }; 205f50f2127SFlorian Fainelli 20634297176SVladimir Oltean /* TC port policer entry */ 20734297176SVladimir Oltean struct dsa_mall_policer_tc_entry { 2085f035af7SPo Liu u32 burst; 20934297176SVladimir Oltean u64 rate_bytes_per_sec; 21034297176SVladimir Oltean }; 21134297176SVladimir Oltean 212f50f2127SFlorian Fainelli /* TC matchall entry */ 213f50f2127SFlorian Fainelli struct dsa_mall_tc_entry { 214f50f2127SFlorian Fainelli struct list_head list; 215f50f2127SFlorian Fainelli unsigned long cookie; 216f50f2127SFlorian Fainelli enum dsa_port_mall_action_type type; 217f50f2127SFlorian Fainelli union { 218f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry mirror; 21934297176SVladimir Oltean struct dsa_mall_policer_tc_entry policer; 220f50f2127SFlorian Fainelli }; 221f50f2127SFlorian Fainelli }; 222f50f2127SFlorian Fainelli 223f50f2127SFlorian Fainelli 224c8b09808SAndrew Lunn struct dsa_port { 225f8b8b1cdSVivien Didelot /* A CPU port is physically connected to a master device. 226f8b8b1cdSVivien Didelot * A user port exposed to userspace has a slave device. 227f8b8b1cdSVivien Didelot */ 228f8b8b1cdSVivien Didelot union { 229f8b8b1cdSVivien Didelot struct net_device *master; 230f8b8b1cdSVivien Didelot struct net_device *slave; 231f8b8b1cdSVivien Didelot }; 232f8b8b1cdSVivien Didelot 233357f203bSVladimir Oltean /* Copy of the tagging protocol operations, for quicker access 234357f203bSVladimir Oltean * in the data path. Valid only for the CPU ports. 235357f203bSVladimir Oltean */ 23615240248SVivien Didelot const struct dsa_device_ops *tag_ops; 23715240248SVivien Didelot 2383e41f93bSVivien Didelot /* Copies for faster access in master receive hot path */ 2393e41f93bSVivien Didelot struct dsa_switch_tree *dst; 2403e41f93bSVivien Didelot struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev, 2413e41f93bSVivien Didelot struct packet_type *pt); 242cc1939e4SVladimir Oltean bool (*filter)(const struct sk_buff *skb, struct net_device *dev); 2433e41f93bSVivien Didelot 244057cad2cSVivien Didelot enum { 245057cad2cSVivien Didelot DSA_PORT_TYPE_UNUSED = 0, 246057cad2cSVivien Didelot DSA_PORT_TYPE_CPU, 247057cad2cSVivien Didelot DSA_PORT_TYPE_DSA, 248057cad2cSVivien Didelot DSA_PORT_TYPE_USER, 249057cad2cSVivien Didelot } type; 250057cad2cSVivien Didelot 251818be848SVivien Didelot struct dsa_switch *ds; 252818be848SVivien Didelot unsigned int index; 25371e0bbdeSFlorian Fainelli const char *name; 25468b2d4a8SVivien Didelot struct dsa_port *cpu_dp; 255a2c7023fSXiaofei Shen const char *mac; 256189b0d93SAndrew Lunn struct device_node *dn; 25734a79f63SVivien Didelot unsigned int ageing_time; 25833162e9aSVladimir Oltean bool vlan_filtering; 259732f794cSVivien Didelot u8 stp_state; 260a5e9a02eSVivien Didelot struct net_device *bridge_dev; 26196567d5dSAndrew Lunn struct devlink_port devlink_port; 2623122433eSAndrew Lunn bool devlink_port_setup; 263aab9c406SFlorian Fainelli struct phylink *pl; 26444cc27e4SIoana Ciornei struct phylink_config pl_config; 265058102a6STobias Waldekranz struct net_device *lag_dev; 266058102a6STobias Waldekranz bool lag_tx_enabled; 26797a69a0dSVladimir Oltean 268ab8ccae1SVivien Didelot struct list_head list; 269ab8ccae1SVivien Didelot 27067dbb9d4SFlorian Fainelli /* 271c362beb0SVladimir Oltean * Give the switch driver somewhere to hang its per-port private data 272c362beb0SVladimir Oltean * structures (accessible from the tagger). 273c362beb0SVladimir Oltean */ 274c362beb0SVladimir Oltean void *priv; 275c362beb0SVladimir Oltean 276c362beb0SVladimir Oltean /* 27767dbb9d4SFlorian Fainelli * Original copy of the master netdev ethtool_ops 27867dbb9d4SFlorian Fainelli */ 27967dbb9d4SFlorian Fainelli const struct ethtool_ops *orig_ethtool_ops; 280da7b9e9bSFlorian Fainelli 281da7b9e9bSFlorian Fainelli /* 282da7b9e9bSFlorian Fainelli * Original copy of the master netdev net_device_ops 283da7b9e9bSFlorian Fainelli */ 2844cfab356SFlorian Fainelli const struct dsa_netdevice_ops *netdev_ops; 285fb35c60cSVivien Didelot 286fb35c60cSVivien Didelot bool setup; 287c8b09808SAndrew Lunn }; 288c8b09808SAndrew Lunn 289c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member, 290c5f51765SVivien Didelot * and no dst->rtable nor this struct dsa_link would be needed, 291c5f51765SVivien Didelot * but this would require some more complex tree walking, 292c5f51765SVivien Didelot * so keep it stupid at the moment and list them all. 293c5f51765SVivien Didelot */ 294c5f51765SVivien Didelot struct dsa_link { 295c5f51765SVivien Didelot struct dsa_port *dp; 296c5f51765SVivien Didelot struct dsa_port *link_dp; 297c5f51765SVivien Didelot struct list_head list; 298c5f51765SVivien Didelot }; 299c5f51765SVivien Didelot 300c8f0b869SBen Hutchings struct dsa_switch { 301fb35c60cSVivien Didelot bool setup; 302fb35c60cSVivien Didelot 303c33063d6SAndrew Lunn struct device *dev; 304c33063d6SAndrew Lunn 305c8f0b869SBen Hutchings /* 306c8f0b869SBen Hutchings * Parent switch tree, and switch index. 307c8f0b869SBen Hutchings */ 308c8f0b869SBen Hutchings struct dsa_switch_tree *dst; 30999feaafcSVivien Didelot unsigned int index; 310c8f0b869SBen Hutchings 311f515f192SVivien Didelot /* Listener for switch fabric events */ 312f515f192SVivien Didelot struct notifier_block nb; 313f515f192SVivien Didelot 314c8f0b869SBen Hutchings /* 3157543a6d5SAndrew Lunn * Give the switch driver somewhere to hang its private data 3167543a6d5SAndrew Lunn * structure. 3177543a6d5SAndrew Lunn */ 3187543a6d5SAndrew Lunn void *priv; 3197543a6d5SAndrew Lunn 3207543a6d5SAndrew Lunn /* 321c8f0b869SBen Hutchings * Configuration data for this switch. 322c8f0b869SBen Hutchings */ 323ff04955cSAndrew Lunn struct dsa_chip_data *cd; 324c8f0b869SBen Hutchings 325c8f0b869SBen Hutchings /* 3269d490b4eSVivien Didelot * The switch operations. 327c8f0b869SBen Hutchings */ 328a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 329c8f0b869SBen Hutchings 33066472fc0SAndrew Lunn /* 331c8f0b869SBen Hutchings * Slave mii_bus and devices for the individual ports. 332c8f0b869SBen Hutchings */ 3330d8bcdd3SFlorian Fainelli u32 phys_mii_mask; 334c8f0b869SBen Hutchings struct mii_bus *slave_mii_bus; 335a0c02161SVivien Didelot 3360f3da6afSVivien Didelot /* Ageing Time limits in msecs */ 3370f3da6afSVivien Didelot unsigned int ageing_time_min; 3380f3da6afSVivien Didelot unsigned int ageing_time_max; 3390f3da6afSVivien Didelot 34096567d5dSAndrew Lunn /* devlink used to represent this switch device */ 34196567d5dSAndrew Lunn struct devlink *devlink; 34296567d5dSAndrew Lunn 34355199df6SFlorian Fainelli /* Number of switch port queues */ 34455199df6SFlorian Fainelli unsigned int num_tx_queues; 34555199df6SFlorian Fainelli 3468f5d16f6SVladimir Oltean /* Disallow bridge core from requesting different VLAN awareness 3478f5d16f6SVladimir Oltean * settings on ports if not hardware-supported 3488f5d16f6SVladimir Oltean */ 3498f5d16f6SVladimir Oltean bool vlan_filtering_is_global; 3508f5d16f6SVladimir Oltean 35154a0ed0dSRussell King /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges 35254a0ed0dSRussell King * that have vlan_filtering=0. All drivers should ideally set this (and 35354a0ed0dSRussell King * then the option would get removed), but it is unknown whether this 35454a0ed0dSRussell King * would break things or not. 35554a0ed0dSRussell King */ 35654a0ed0dSRussell King bool configure_vlan_while_not_filtering; 35754a0ed0dSRussell King 3581dc0408cSFlorian Fainelli /* If the switch driver always programs the CPU port as egress tagged 3591dc0408cSFlorian Fainelli * despite the VLAN configuration indicating otherwise, then setting 3601dc0408cSFlorian Fainelli * @untag_bridge_pvid will force the DSA receive path to pop the bridge's 3611dc0408cSFlorian Fainelli * default_pvid VLAN tagged frames to offer a consistent behavior 3621dc0408cSFlorian Fainelli * between a vlan_filtering=0 and vlan_filtering=1 bridge device. 3631dc0408cSFlorian Fainelli */ 3641dc0408cSFlorian Fainelli bool untag_bridge_pvid; 3651dc0408cSFlorian Fainelli 366d5f19486SVladimir Oltean /* Let DSA manage the FDB entries towards the CPU, based on the 367d5f19486SVladimir Oltean * software bridge database. 368d5f19486SVladimir Oltean */ 369d5f19486SVladimir Oltean bool assisted_learning_on_cpu_port; 370d5f19486SVladimir Oltean 37114574676SVladimir Oltean /* In case vlan_filtering_is_global is set, the VLAN awareness state 37214574676SVladimir Oltean * should be retrieved from here and not from the per-port settings. 37314574676SVladimir Oltean */ 37414574676SVladimir Oltean bool vlan_filtering; 37514574676SVladimir Oltean 376787cac3fSVladimir Oltean /* MAC PCS does not provide link state change interrupt, and requires 377787cac3fSVladimir Oltean * polling. Flag passed on to PHYLINK. 378787cac3fSVladimir Oltean */ 379787cac3fSVladimir Oltean bool pcs_poll; 380787cac3fSVladimir Oltean 381bff33f7eSVladimir Oltean /* For switches that only have the MRU configurable. To ensure the 382bff33f7eSVladimir Oltean * configured MTU is not exceeded, normalization of MRU on all bridged 383bff33f7eSVladimir Oltean * interfaces is needed. 384bff33f7eSVladimir Oltean */ 385bff33f7eSVladimir Oltean bool mtu_enforcement_ingress; 386bff33f7eSVladimir Oltean 387058102a6STobias Waldekranz /* Drivers that benefit from having an ID associated with each 388058102a6STobias Waldekranz * offloaded LAG should set this to the maximum number of 389058102a6STobias Waldekranz * supported IDs. DSA will then maintain a mapping of _at 390058102a6STobias Waldekranz * least_ these many IDs, accessible to drivers via 391058102a6STobias Waldekranz * dsa_lag_id(). 392058102a6STobias Waldekranz */ 393058102a6STobias Waldekranz unsigned int num_lag_ids; 394058102a6STobias Waldekranz 395a0c02161SVivien Didelot size_t num_ports; 396c8f0b869SBen Hutchings }; 397c8f0b869SBen Hutchings 39868bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p) 399c38c5a66SVivien Didelot { 400b96ddf25SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 401d607525bSVivien Didelot struct dsa_port *dp; 402b96ddf25SVivien Didelot 403b96ddf25SVivien Didelot list_for_each_entry(dp, &dst->ports, list) 404b96ddf25SVivien Didelot if (dp->ds == ds && dp->index == p) 405b96ddf25SVivien Didelot return dp; 406d607525bSVivien Didelot 407d607525bSVivien Didelot return NULL; 408c38c5a66SVivien Didelot } 409c38c5a66SVivien Didelot 410bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p) 411bff7b688SVivien Didelot { 412c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED; 413bff7b688SVivien Didelot } 414bff7b688SVivien Didelot 415c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p) 416c8f0b869SBen Hutchings { 417c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU; 418c8f0b869SBen Hutchings } 419c8f0b869SBen Hutchings 42060045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p) 42160045cbfSAndrew Lunn { 422c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA; 42360045cbfSAndrew Lunn } 42460045cbfSAndrew Lunn 4252b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p) 4266cd456f3SVivien Didelot { 427c38c5a66SVivien Didelot return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER; 4286cd456f3SVivien Didelot } 4296cd456f3SVivien Didelot 43002bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds) 43102bc6e54SVivien Didelot { 432c38c5a66SVivien Didelot u32 mask = 0; 433c38c5a66SVivien Didelot int p; 43402bc6e54SVivien Didelot 435c38c5a66SVivien Didelot for (p = 0; p < ds->num_ports; p++) 436c38c5a66SVivien Didelot if (dsa_is_user_port(ds, p)) 437c38c5a66SVivien Didelot mask |= BIT(p); 438c38c5a66SVivien Didelot 439c38c5a66SVivien Didelot return mask; 440c8652c83SVivien Didelot } 441c8652c83SVivien Didelot 442c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */ 443c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device) 444c5f51765SVivien Didelot { 445c5f51765SVivien Didelot struct dsa_switch_tree *dst = ds->dst; 446c5f51765SVivien Didelot struct dsa_link *dl; 447c5f51765SVivien Didelot 448c5f51765SVivien Didelot list_for_each_entry(dl, &dst->rtable, list) 449c5f51765SVivien Didelot if (dl->dp->ds == ds && dl->link_dp->ds->index == device) 450c5f51765SVivien Didelot return dl->dp->index; 451c5f51765SVivien Didelot 452c5f51765SVivien Didelot return ds->num_ports; 453c5f51765SVivien Didelot } 454c5f51765SVivien Didelot 4553b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */ 4563b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device, 4573b8fac5dSVivien Didelot int port) 4583b8fac5dSVivien Didelot { 4593b8fac5dSVivien Didelot if (device == ds->index) 4603b8fac5dSVivien Didelot return port; 4613b8fac5dSVivien Didelot else 462c5f51765SVivien Didelot return dsa_routing_port(ds, device); 4633b8fac5dSVivien Didelot } 4643b8fac5dSVivien Didelot 4653b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */ 46607073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port) 467c8f0b869SBen Hutchings { 46807073c79SVivien Didelot const struct dsa_port *dp = dsa_to_port(ds, port); 46907073c79SVivien Didelot const struct dsa_port *cpu_dp = dp->cpu_dp; 47007073c79SVivien Didelot 47107073c79SVivien Didelot if (!cpu_dp) 47207073c79SVivien Didelot return port; 473c8f0b869SBen Hutchings 4743b8fac5dSVivien Didelot return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index); 475c8f0b869SBen Hutchings } 476c8f0b869SBen Hutchings 477cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp) 478cf2d45f5SVladimir Oltean { 479cf2d45f5SVladimir Oltean const struct dsa_switch *ds = dp->ds; 480cf2d45f5SVladimir Oltean 481cf2d45f5SVladimir Oltean if (ds->vlan_filtering_is_global) 482cf2d45f5SVladimir Oltean return ds->vlan_filtering; 483cf2d45f5SVladimir Oltean else 484cf2d45f5SVladimir Oltean return dp->vlan_filtering; 485cf2d45f5SVladimir Oltean } 486cf2d45f5SVladimir Oltean 4872bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid, 4882bedde1aSArkadi Sharshevsky bool is_static, void *data); 4899d490b4eSVivien Didelot struct dsa_switch_ops { 49053da0ebaSVladimir Oltean /* 49153da0ebaSVladimir Oltean * Tagging protocol helpers called for the CPU ports and DSA links. 49253da0ebaSVladimir Oltean * @get_tag_protocol retrieves the initial tagging protocol and is 49353da0ebaSVladimir Oltean * mandatory. Switches which can operate using multiple tagging 49453da0ebaSVladimir Oltean * protocols should implement @change_tag_protocol and report in 49553da0ebaSVladimir Oltean * @get_tag_protocol the tagger in current use. 49653da0ebaSVladimir Oltean */ 4975ed4e3ebSFlorian Fainelli enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds, 4984d776482SFlorian Fainelli int port, 4994d776482SFlorian Fainelli enum dsa_tag_protocol mprot); 50053da0ebaSVladimir Oltean int (*change_tag_protocol)(struct dsa_switch *ds, int port, 50153da0ebaSVladimir Oltean enum dsa_tag_protocol proto); 5027b314362SAndrew Lunn 503c8f0b869SBen Hutchings int (*setup)(struct dsa_switch *ds); 5045e3f847aSVladimir Oltean void (*teardown)(struct dsa_switch *ds); 5056819563eSFlorian Fainelli u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 506c8f0b869SBen Hutchings 507c8f0b869SBen Hutchings /* 508c8f0b869SBen Hutchings * Access to the switch's PHY registers. 509c8f0b869SBen Hutchings */ 510c8f0b869SBen Hutchings int (*phy_read)(struct dsa_switch *ds, int port, int regnum); 511c8f0b869SBen Hutchings int (*phy_write)(struct dsa_switch *ds, int port, 512c8f0b869SBen Hutchings int regnum, u16 val); 513c8f0b869SBen Hutchings 514c8f0b869SBen Hutchings /* 515ec9436baSFlorian Fainelli * Link state adjustment (called from libphy) 516ec9436baSFlorian Fainelli */ 517ec9436baSFlorian Fainelli void (*adjust_link)(struct dsa_switch *ds, int port, 518ec9436baSFlorian Fainelli struct phy_device *phydev); 519ce31b31cSFlorian Fainelli void (*fixed_link_update)(struct dsa_switch *ds, int port, 520ce31b31cSFlorian Fainelli struct fixed_phy_status *st); 521ec9436baSFlorian Fainelli 522ec9436baSFlorian Fainelli /* 52311d8f3ddSFlorian Fainelli * PHYLINK integration 52411d8f3ddSFlorian Fainelli */ 52511d8f3ddSFlorian Fainelli void (*phylink_validate)(struct dsa_switch *ds, int port, 52611d8f3ddSFlorian Fainelli unsigned long *supported, 52711d8f3ddSFlorian Fainelli struct phylink_link_state *state); 52811d8f3ddSFlorian Fainelli int (*phylink_mac_link_state)(struct dsa_switch *ds, int port, 52911d8f3ddSFlorian Fainelli struct phylink_link_state *state); 53011d8f3ddSFlorian Fainelli void (*phylink_mac_config)(struct dsa_switch *ds, int port, 53111d8f3ddSFlorian Fainelli unsigned int mode, 53211d8f3ddSFlorian Fainelli const struct phylink_link_state *state); 53311d8f3ddSFlorian Fainelli void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port); 53411d8f3ddSFlorian Fainelli void (*phylink_mac_link_down)(struct dsa_switch *ds, int port, 53511d8f3ddSFlorian Fainelli unsigned int mode, 53611d8f3ddSFlorian Fainelli phy_interface_t interface); 53711d8f3ddSFlorian Fainelli void (*phylink_mac_link_up)(struct dsa_switch *ds, int port, 53811d8f3ddSFlorian Fainelli unsigned int mode, 53911d8f3ddSFlorian Fainelli phy_interface_t interface, 5405b502a7bSRussell King struct phy_device *phydev, 5415b502a7bSRussell King int speed, int duplex, 5425b502a7bSRussell King bool tx_pause, bool rx_pause); 54311d8f3ddSFlorian Fainelli void (*phylink_fixed_state)(struct dsa_switch *ds, int port, 54411d8f3ddSFlorian Fainelli struct phylink_link_state *state); 54511d8f3ddSFlorian Fainelli /* 546c2ec5f2eSOleksij Rempel * Port statistics counters. 547c8f0b869SBen Hutchings */ 54889f09048SFlorian Fainelli void (*get_strings)(struct dsa_switch *ds, int port, 54989f09048SFlorian Fainelli u32 stringset, uint8_t *data); 550c8f0b869SBen Hutchings void (*get_ethtool_stats)(struct dsa_switch *ds, 551c8f0b869SBen Hutchings int port, uint64_t *data); 55289f09048SFlorian Fainelli int (*get_sset_count)(struct dsa_switch *ds, int port, int sset); 553cf963573SFlorian Fainelli void (*get_ethtool_phy_stats)(struct dsa_switch *ds, 554cf963573SFlorian Fainelli int port, uint64_t *data); 555c2ec5f2eSOleksij Rempel void (*get_stats64)(struct dsa_switch *ds, int port, 556c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s); 55724462549SFlorian Fainelli 55824462549SFlorian Fainelli /* 55919e57c4eSFlorian Fainelli * ethtool Wake-on-LAN 56019e57c4eSFlorian Fainelli */ 56119e57c4eSFlorian Fainelli void (*get_wol)(struct dsa_switch *ds, int port, 56219e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 56319e57c4eSFlorian Fainelli int (*set_wol)(struct dsa_switch *ds, int port, 56419e57c4eSFlorian Fainelli struct ethtool_wolinfo *w); 56519e57c4eSFlorian Fainelli 56619e57c4eSFlorian Fainelli /* 5670336369dSBrandon Streiff * ethtool timestamp info 5680336369dSBrandon Streiff */ 5690336369dSBrandon Streiff int (*get_ts_info)(struct dsa_switch *ds, int port, 5700336369dSBrandon Streiff struct ethtool_ts_info *ts); 5710336369dSBrandon Streiff 5720336369dSBrandon Streiff /* 57324462549SFlorian Fainelli * Suspend and resume 57424462549SFlorian Fainelli */ 57524462549SFlorian Fainelli int (*suspend)(struct dsa_switch *ds); 57624462549SFlorian Fainelli int (*resume)(struct dsa_switch *ds); 577b2f2af21SFlorian Fainelli 578b2f2af21SFlorian Fainelli /* 579b2f2af21SFlorian Fainelli * Port enable/disable 580b2f2af21SFlorian Fainelli */ 581b2f2af21SFlorian Fainelli int (*port_enable)(struct dsa_switch *ds, int port, 582b2f2af21SFlorian Fainelli struct phy_device *phy); 58375104db0SAndrew Lunn void (*port_disable)(struct dsa_switch *ds, int port); 5847905288fSFlorian Fainelli 5857905288fSFlorian Fainelli /* 58608f50061SVivien Didelot * Port's MAC EEE settings 5877905288fSFlorian Fainelli */ 58808f50061SVivien Didelot int (*set_mac_eee)(struct dsa_switch *ds, int port, 5897905288fSFlorian Fainelli struct ethtool_eee *e); 59008f50061SVivien Didelot int (*get_mac_eee)(struct dsa_switch *ds, int port, 5917905288fSFlorian Fainelli struct ethtool_eee *e); 59251579c3fSGuenter Roeck 5936793abb4SGuenter Roeck /* EEPROM access */ 5946793abb4SGuenter Roeck int (*get_eeprom_len)(struct dsa_switch *ds); 5956793abb4SGuenter Roeck int (*get_eeprom)(struct dsa_switch *ds, 5966793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 5976793abb4SGuenter Roeck int (*set_eeprom)(struct dsa_switch *ds, 5986793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data); 5993d762a0fSGuenter Roeck 6003d762a0fSGuenter Roeck /* 6013d762a0fSGuenter Roeck * Register access. 6023d762a0fSGuenter Roeck */ 6033d762a0fSGuenter Roeck int (*get_regs_len)(struct dsa_switch *ds, int port); 6043d762a0fSGuenter Roeck void (*get_regs)(struct dsa_switch *ds, int port, 6053d762a0fSGuenter Roeck struct ethtool_regs *regs, void *p); 606b73adef6SFlorian Fainelli 607b73adef6SFlorian Fainelli /* 608e358bef7SVladimir Oltean * Upper device tracking. 609e358bef7SVladimir Oltean */ 610e358bef7SVladimir Oltean int (*port_prechangeupper)(struct dsa_switch *ds, int port, 611e358bef7SVladimir Oltean struct netdev_notifier_changeupper_info *info); 612e358bef7SVladimir Oltean 613e358bef7SVladimir Oltean /* 614b73adef6SFlorian Fainelli * Bridge integration 615b73adef6SFlorian Fainelli */ 61634a79f63SVivien Didelot int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs); 61771327a4eSVivien Didelot int (*port_bridge_join)(struct dsa_switch *ds, int port, 618a6692754SVivien Didelot struct net_device *bridge); 619f123f2fbSVivien Didelot void (*port_bridge_leave)(struct dsa_switch *ds, int port, 620f123f2fbSVivien Didelot struct net_device *bridge); 62143c44a9fSVivien Didelot void (*port_stp_state_set)(struct dsa_switch *ds, int port, 622b73adef6SFlorian Fainelli u8 state); 623732f794cSVivien Didelot void (*port_fast_age)(struct dsa_switch *ds, int port); 62457652796SRussell King int (*port_egress_floods)(struct dsa_switch *ds, int port, 62557652796SRussell King bool unicast, bool multicast); 6262a778e1bSVivien Didelot 6272a778e1bSVivien Didelot /* 62811149536SVivien Didelot * VLAN support 62911149536SVivien Didelot */ 630fb2dabadSVivien Didelot int (*port_vlan_filtering)(struct dsa_switch *ds, int port, 631bae33f2bSVladimir Oltean bool vlan_filtering); 6321958d581SVladimir Oltean int (*port_vlan_add)(struct dsa_switch *ds, int port, 63380e02360SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 63476e398a6SVivien Didelot int (*port_vlan_del)(struct dsa_switch *ds, int port, 63576e398a6SVivien Didelot const struct switchdev_obj_port_vlan *vlan); 63611149536SVivien Didelot /* 6372a778e1bSVivien Didelot * Forwarding database 6382a778e1bSVivien Didelot */ 6391b6dd556SArkadi Sharshevsky int (*port_fdb_add)(struct dsa_switch *ds, int port, 6406c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 6412a778e1bSVivien Didelot int (*port_fdb_del)(struct dsa_switch *ds, int port, 6426c2c1dcbSArkadi Sharshevsky const unsigned char *addr, u16 vid); 643ea70ba98SVivien Didelot int (*port_fdb_dump)(struct dsa_switch *ds, int port, 6442bedde1aSArkadi Sharshevsky dsa_fdb_dump_cb_t *cb, void *data); 6458df30255SVivien Didelot 6468df30255SVivien Didelot /* 6478df30255SVivien Didelot * Multicast database 6488df30255SVivien Didelot */ 649a52b2da7SVladimir Oltean int (*port_mdb_add)(struct dsa_switch *ds, int port, 6503709aadcSVivien Didelot const struct switchdev_obj_port_mdb *mdb); 6518df30255SVivien Didelot int (*port_mdb_del)(struct dsa_switch *ds, int port, 6528df30255SVivien Didelot const struct switchdev_obj_port_mdb *mdb); 653bf9f2648SFlorian Fainelli /* 654bf9f2648SFlorian Fainelli * RXNFC 655bf9f2648SFlorian Fainelli */ 656bf9f2648SFlorian Fainelli int (*get_rxnfc)(struct dsa_switch *ds, int port, 657bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs); 658bf9f2648SFlorian Fainelli int (*set_rxnfc)(struct dsa_switch *ds, int port, 659bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc); 660f50f2127SFlorian Fainelli 661f50f2127SFlorian Fainelli /* 662f50f2127SFlorian Fainelli * TC integration 663f50f2127SFlorian Fainelli */ 664ed11bb1fSVladimir Oltean int (*cls_flower_add)(struct dsa_switch *ds, int port, 665ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 666ed11bb1fSVladimir Oltean int (*cls_flower_del)(struct dsa_switch *ds, int port, 667ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 668ed11bb1fSVladimir Oltean int (*cls_flower_stats)(struct dsa_switch *ds, int port, 669ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, bool ingress); 670f50f2127SFlorian Fainelli int (*port_mirror_add)(struct dsa_switch *ds, int port, 671f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror, 672f50f2127SFlorian Fainelli bool ingress); 673f50f2127SFlorian Fainelli void (*port_mirror_del)(struct dsa_switch *ds, int port, 674f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror); 67534297176SVladimir Oltean int (*port_policer_add)(struct dsa_switch *ds, int port, 67634297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer); 67734297176SVladimir Oltean void (*port_policer_del)(struct dsa_switch *ds, int port); 67847d23af2SVladimir Oltean int (*port_setup_tc)(struct dsa_switch *ds, int port, 67947d23af2SVladimir Oltean enum tc_setup_type type, void *type_data); 68040ef2c93SVivien Didelot 68140ef2c93SVivien Didelot /* 68240ef2c93SVivien Didelot * Cross-chip operations 68340ef2c93SVivien Didelot */ 684f66a6a69SVladimir Oltean int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index, 685f66a6a69SVladimir Oltean int sw_index, int port, 686f66a6a69SVladimir Oltean struct net_device *br); 687f66a6a69SVladimir Oltean void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index, 688f66a6a69SVladimir Oltean int sw_index, int port, 689f66a6a69SVladimir Oltean struct net_device *br); 690058102a6STobias Waldekranz int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index, 691058102a6STobias Waldekranz int port); 692058102a6STobias Waldekranz int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index, 693058102a6STobias Waldekranz int port, struct net_device *lag, 694058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 695058102a6STobias Waldekranz int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index, 696058102a6STobias Waldekranz int port, struct net_device *lag); 6970336369dSBrandon Streiff 6980336369dSBrandon Streiff /* 6990336369dSBrandon Streiff * PTP functionality 7000336369dSBrandon Streiff */ 7010336369dSBrandon Streiff int (*port_hwtstamp_get)(struct dsa_switch *ds, int port, 7020336369dSBrandon Streiff struct ifreq *ifr); 7030336369dSBrandon Streiff int (*port_hwtstamp_set)(struct dsa_switch *ds, int port, 7040336369dSBrandon Streiff struct ifreq *ifr); 70590af1059SBrandon Streiff bool (*port_txtstamp)(struct dsa_switch *ds, int port, 70690af1059SBrandon Streiff struct sk_buff *clone, unsigned int type); 70790af1059SBrandon Streiff bool (*port_rxtstamp)(struct dsa_switch *ds, int port, 70890af1059SBrandon Streiff struct sk_buff *skb, unsigned int type); 70997a69a0dSVladimir Oltean 7100f06b855SAndrew Lunn /* Devlink parameters, etc */ 7116b297524SAndrew Lunn int (*devlink_param_get)(struct dsa_switch *ds, u32 id, 7126b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 7136b297524SAndrew Lunn int (*devlink_param_set)(struct dsa_switch *ds, u32 id, 7146b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 7150f06b855SAndrew Lunn int (*devlink_info_get)(struct dsa_switch *ds, 7160f06b855SAndrew Lunn struct devlink_info_req *req, 7170f06b855SAndrew Lunn struct netlink_ext_ack *extack); 7182a6ef763SVladimir Oltean int (*devlink_sb_pool_get)(struct dsa_switch *ds, 7192a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 7202a6ef763SVladimir Oltean struct devlink_sb_pool_info *pool_info); 7212a6ef763SVladimir Oltean int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index, 7222a6ef763SVladimir Oltean u16 pool_index, u32 size, 7232a6ef763SVladimir Oltean enum devlink_sb_threshold_type threshold_type, 7242a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 7252a6ef763SVladimir Oltean int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port, 7262a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 7272a6ef763SVladimir Oltean u32 *p_threshold); 7282a6ef763SVladimir Oltean int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port, 7292a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 7302a6ef763SVladimir Oltean u32 threshold, 7312a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 7322a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port, 7332a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 7342a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 7352a6ef763SVladimir Oltean u16 *p_pool_index, u32 *p_threshold); 7362a6ef763SVladimir Oltean int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port, 7372a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 7382a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 7392a6ef763SVladimir Oltean u16 pool_index, u32 threshold, 7402a6ef763SVladimir Oltean struct netlink_ext_ack *extack); 7412a6ef763SVladimir Oltean int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds, 7422a6ef763SVladimir Oltean unsigned int sb_index); 7432a6ef763SVladimir Oltean int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds, 7442a6ef763SVladimir Oltean unsigned int sb_index); 7452a6ef763SVladimir Oltean int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port, 7462a6ef763SVladimir Oltean unsigned int sb_index, u16 pool_index, 7472a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 7482a6ef763SVladimir Oltean int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port, 7492a6ef763SVladimir Oltean unsigned int sb_index, u16 tc_index, 7502a6ef763SVladimir Oltean enum devlink_sb_pool_type pool_type, 7512a6ef763SVladimir Oltean u32 *p_cur, u32 *p_max); 752bfcb8132SVladimir Oltean 753bfcb8132SVladimir Oltean /* 754bfcb8132SVladimir Oltean * MTU change functionality. Switches can also adjust their MRU through 755bfcb8132SVladimir Oltean * this method. By MTU, one understands the SDU (L2 payload) length. 756bfcb8132SVladimir Oltean * If the switch needs to account for the DSA tag on the CPU port, this 757ab88d64aSRandy Dunlap * method needs to do so privately. 758bfcb8132SVladimir Oltean */ 759bfcb8132SVladimir Oltean int (*port_change_mtu)(struct dsa_switch *ds, int port, 760bfcb8132SVladimir Oltean int new_mtu); 761bfcb8132SVladimir Oltean int (*port_max_mtu)(struct dsa_switch *ds, int port); 762058102a6STobias Waldekranz 763058102a6STobias Waldekranz /* 764058102a6STobias Waldekranz * LAG integration 765058102a6STobias Waldekranz */ 766058102a6STobias Waldekranz int (*port_lag_change)(struct dsa_switch *ds, int port); 767058102a6STobias Waldekranz int (*port_lag_join)(struct dsa_switch *ds, int port, 768058102a6STobias Waldekranz struct net_device *lag, 769058102a6STobias Waldekranz struct netdev_lag_upper_info *info); 770058102a6STobias Waldekranz int (*port_lag_leave)(struct dsa_switch *ds, int port, 771058102a6STobias Waldekranz struct net_device *lag); 7726b297524SAndrew Lunn }; 7736b297524SAndrew Lunn 7746b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \ 7756b297524SAndrew Lunn DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \ 7766b297524SAndrew Lunn dsa_devlink_param_get, dsa_devlink_param_set, NULL) 7776b297524SAndrew Lunn 7786b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id, 7796b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 7806b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id, 7816b297524SAndrew Lunn struct devlink_param_gset_ctx *ctx); 7826b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds, 7836b297524SAndrew Lunn const struct devlink_param *params, 7846b297524SAndrew Lunn size_t params_count); 7856b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds, 7866b297524SAndrew Lunn const struct devlink_param *params, 7876b297524SAndrew Lunn size_t params_count); 7885cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds, 7895cd73fbdSAndrew Lunn const char *resource_name, 7905cd73fbdSAndrew Lunn u64 resource_size, 7915cd73fbdSAndrew Lunn u64 resource_id, 7925cd73fbdSAndrew Lunn u64 parent_resource_id, 7935cd73fbdSAndrew Lunn const struct devlink_resource_size_params *size_params); 7945cd73fbdSAndrew Lunn 7955cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds); 7965cd73fbdSAndrew Lunn 7975cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 7985cd73fbdSAndrew Lunn u64 resource_id, 7995cd73fbdSAndrew Lunn devlink_resource_occ_get_t *occ_get, 8005cd73fbdSAndrew Lunn void *occ_get_priv); 8015cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 8025cd73fbdSAndrew Lunn u64 resource_id); 80397c82c23SAndrew Lunn struct devlink_region * 80497c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds, 80597c82c23SAndrew Lunn const struct devlink_region_ops *ops, 80697c82c23SAndrew Lunn u32 region_max_snapshots, u64 region_size); 80708156ba4SAndrew Lunn struct devlink_region * 80808156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds, 80908156ba4SAndrew Lunn int port, 81008156ba4SAndrew Lunn const struct devlink_port_region_ops *ops, 81108156ba4SAndrew Lunn u32 region_max_snapshots, u64 region_size); 81297c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region); 81397c82c23SAndrew Lunn 814e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev); 8155cd73fbdSAndrew Lunn 8166b297524SAndrew Lunn struct dsa_devlink_priv { 8176b297524SAndrew Lunn struct dsa_switch *ds; 818c8f0b869SBen Hutchings }; 819c8f0b869SBen Hutchings 820ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl) 821ccc3e6b0SAndrew Lunn { 822ccc3e6b0SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 823ccc3e6b0SAndrew Lunn 824ccc3e6b0SAndrew Lunn return dl_priv->ds; 825ccc3e6b0SAndrew Lunn } 826ccc3e6b0SAndrew Lunn 8277d1e2a10SAndrew Lunn static inline 8287d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port) 8297d1e2a10SAndrew Lunn { 8307d1e2a10SAndrew Lunn struct devlink *dl = port->devlink; 8317d1e2a10SAndrew Lunn struct dsa_devlink_priv *dl_priv = devlink_priv(dl); 8327d1e2a10SAndrew Lunn 8337d1e2a10SAndrew Lunn return dl_priv->ds; 8347d1e2a10SAndrew Lunn } 8357d1e2a10SAndrew Lunn 8367d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port) 8377d1e2a10SAndrew Lunn { 8387d1e2a10SAndrew Lunn return port->index; 8397d1e2a10SAndrew Lunn } 8407d1e2a10SAndrew Lunn 841ab3d408dSFlorian Fainelli struct dsa_switch_driver { 842ab3d408dSFlorian Fainelli struct list_head list; 843a82f67afSFlorian Fainelli const struct dsa_switch_ops *ops; 844ab3d408dSFlorian Fainelli }; 845ab3d408dSFlorian Fainelli 84614b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev); 847c8f0b869SBen Hutchings 84873a7ece8SVivien Didelot /* Keep inline for faster access in hot path */ 8499eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev) 850c6e970a0SAndrew Lunn { 851c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA) 852717ffbfbSVivien Didelot return dev->dsa_ptr && dev->dsa_ptr->rcv; 853c6e970a0SAndrew Lunn #endif 854c6e970a0SAndrew Lunn return false; 855c6e970a0SAndrew Lunn } 856c6e970a0SAndrew Lunn 857cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb, 858cc1939e4SVladimir Oltean struct net_device *dev) 859cc1939e4SVladimir Oltean { 860cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 861cc1939e4SVladimir Oltean return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev); 862cc1939e4SVladimir Oltean #endif 863cc1939e4SVladimir Oltean return false; 864cc1939e4SVladimir Oltean } 865cc1939e4SVladimir Oltean 8669790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail 8679790cf20SVladimir Oltean * tags, which don't break dissection) can be treated the same from the 8689790cf20SVladimir Oltean * perspective of the flow dissector. 8699790cf20SVladimir Oltean * 8709790cf20SVladimir Oltean * We need to return: 8719790cf20SVladimir Oltean * - offset: the (B - A) difference between: 8729790cf20SVladimir Oltean * A. the position of the real EtherType and 8739790cf20SVladimir Oltean * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes 8749790cf20SVladimir Oltean * after the normal EtherType was supposed to be) 8759790cf20SVladimir Oltean * The offset in bytes is exactly equal to the tagger overhead (and half of 8769790cf20SVladimir Oltean * that, in __be16 shorts). 8779790cf20SVladimir Oltean * 8789790cf20SVladimir Oltean * - proto: the value of the real EtherType. 8799790cf20SVladimir Oltean */ 8809790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb, 8819790cf20SVladimir Oltean __be16 *proto, int *offset) 8829790cf20SVladimir Oltean { 8839790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA) 8849790cf20SVladimir Oltean const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops; 8859790cf20SVladimir Oltean int tag_len = ops->overhead; 8869790cf20SVladimir Oltean 8879790cf20SVladimir Oltean *offset = tag_len; 8889790cf20SVladimir Oltean *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1]; 8899790cf20SVladimir Oltean #endif 8909790cf20SVladimir Oltean } 8919790cf20SVladimir Oltean 8924cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 8934cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev) 8944cfab356SFlorian Fainelli { 8954cfab356SFlorian Fainelli int err = -EOPNOTSUPP; 8964cfab356SFlorian Fainelli 8974cfab356SFlorian Fainelli if (!dev->dsa_ptr) 8984cfab356SFlorian Fainelli return err; 8994cfab356SFlorian Fainelli 9004cfab356SFlorian Fainelli if (!dev->dsa_ptr->netdev_ops) 9014cfab356SFlorian Fainelli return err; 9024cfab356SFlorian Fainelli 9034cfab356SFlorian Fainelli return 0; 9044cfab356SFlorian Fainelli } 9054cfab356SFlorian Fainelli 9064cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr, 9074cfab356SFlorian Fainelli int cmd) 9084cfab356SFlorian Fainelli { 9094cfab356SFlorian Fainelli const struct dsa_netdevice_ops *ops; 9104cfab356SFlorian Fainelli int err; 9114cfab356SFlorian Fainelli 9124cfab356SFlorian Fainelli err = __dsa_netdevice_ops_check(dev); 9134cfab356SFlorian Fainelli if (err) 9144cfab356SFlorian Fainelli return err; 9154cfab356SFlorian Fainelli 9164cfab356SFlorian Fainelli ops = dev->dsa_ptr->netdev_ops; 9174cfab356SFlorian Fainelli 9184cfab356SFlorian Fainelli return ops->ndo_do_ioctl(dev, ifr, cmd); 9194cfab356SFlorian Fainelli } 9204cfab356SFlorian Fainelli #else 9214cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr, 9224cfab356SFlorian Fainelli int cmd) 9234cfab356SFlorian Fainelli { 9244cfab356SFlorian Fainelli return -EOPNOTSUPP; 9254cfab356SFlorian Fainelli } 9264cfab356SFlorian Fainelli #endif 9274cfab356SFlorian Fainelli 92883c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds); 92923c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds); 9303b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index); 931ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP 932ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds); 933ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds); 934ea825e70SFlorian Fainelli #else 935ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds) 936ea825e70SFlorian Fainelli { 937ea825e70SFlorian Fainelli return 0; 938ea825e70SFlorian Fainelli } 939ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds) 940ea825e70SFlorian Fainelli { 941ea825e70SFlorian Fainelli return 0; 942ea825e70SFlorian Fainelli } 943ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */ 944ea825e70SFlorian Fainelli 94560724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA) 946a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev); 94760724d4bSFlorian Fainelli #else 948a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev) 949a5e3c9baSVladimir Oltean { 950a5e3c9baSVladimir Oltean return false; 951a5e3c9baSVladimir Oltean } 95260724d4bSFlorian Fainelli #endif 95360724d4bSFlorian Fainelli 95497a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev); 955cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 956cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 957cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp); 95811d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up); 959cf963573SFlorian Fainelli 960d3b8c049SAndrew Lunn struct dsa_tag_driver { 961d3b8c049SAndrew Lunn const struct dsa_device_ops *ops; 962d3b8c049SAndrew Lunn struct list_head list; 963d3b8c049SAndrew Lunn struct module *owner; 964d3b8c049SAndrew Lunn }; 965d3b8c049SAndrew Lunn 966d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[], 967d3b8c049SAndrew Lunn unsigned int count, 968d3b8c049SAndrew Lunn struct module *owner); 969d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[], 970d3b8c049SAndrew Lunn unsigned int count); 971d3b8c049SAndrew Lunn 972d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \ 973d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void) \ 974d3b8c049SAndrew Lunn { \ 975d3b8c049SAndrew Lunn dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \ 976d3b8c049SAndrew Lunn THIS_MODULE); \ 977d3b8c049SAndrew Lunn return 0; \ 978d3b8c049SAndrew Lunn } \ 979d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init); \ 980d3b8c049SAndrew Lunn \ 981d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void) \ 982d3b8c049SAndrew Lunn { \ 983d3b8c049SAndrew Lunn dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \ 984d3b8c049SAndrew Lunn } \ 985d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit) 986d3b8c049SAndrew Lunn 987d3b8c049SAndrew Lunn /** 988d3b8c049SAndrew Lunn * module_dsa_tag_drivers() - Helper macro for registering DSA tag 989d3b8c049SAndrew Lunn * drivers 990d3b8c049SAndrew Lunn * @__ops_array: Array of tag driver strucutres 991d3b8c049SAndrew Lunn * 992d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 993d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 994d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 995d3b8c049SAndrew Lunn */ 996d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array) \ 997d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array)) 998d3b8c049SAndrew Lunn 999d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops 1000d3b8c049SAndrew Lunn 1001d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag 1002d3b8c049SAndrew Lunn * drivers 1003d3b8c049SAndrew Lunn */ 1004d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops) \ 1005d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \ 1006d3b8c049SAndrew Lunn .ops = &__ops, \ 1007d3b8c049SAndrew Lunn } 1008d3b8c049SAndrew Lunn 1009d3b8c049SAndrew Lunn /** 1010d3b8c049SAndrew Lunn * module_dsa_tag_driver() - Helper macro for registering a single DSA tag 1011d3b8c049SAndrew Lunn * driver 1012d3b8c049SAndrew Lunn * @__ops: Single tag driver structures 1013d3b8c049SAndrew Lunn * 1014d3b8c049SAndrew Lunn * Helper macro for DSA tag drivers which do not do anything special 1015d3b8c049SAndrew Lunn * in module init/exit. Each module may only use this macro once, and 1016d3b8c049SAndrew Lunn * calling it replaces module_init() and module_exit(). 1017d3b8c049SAndrew Lunn */ 1018d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops) \ 1019d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops); \ 1020d3b8c049SAndrew Lunn \ 1021d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] = { \ 1022d3b8c049SAndrew Lunn &DSA_TAG_DRIVER_NAME(__ops) \ 1023d3b8c049SAndrew Lunn }; \ 1024d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array) 102591da11f8SLennert Buytenhek #endif 1026d3b8c049SAndrew Lunn 1027