Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
0004  * Copyright (c) 2008-2009 Marvell Semiconductor
0005  */
0006 
0007 #ifndef __LINUX_NET_DSA_H
0008 #define __LINUX_NET_DSA_H
0009 
0010 #include <linux/if.h>
0011 #include <linux/if_ether.h>
0012 #include <linux/list.h>
0013 #include <linux/notifier.h>
0014 #include <linux/timer.h>
0015 #include <linux/workqueue.h>
0016 #include <linux/of.h>
0017 #include <linux/ethtool.h>
0018 #include <linux/net_tstamp.h>
0019 #include <linux/phy.h>
0020 #include <linux/platform_data/dsa.h>
0021 #include <linux/phylink.h>
0022 #include <net/devlink.h>
0023 #include <net/switchdev.h>
0024 
0025 struct tc_action;
0026 struct phy_device;
0027 struct fixed_phy_status;
0028 struct phylink_link_state;
0029 
0030 #define DSA_TAG_PROTO_NONE_VALUE        0
0031 #define DSA_TAG_PROTO_BRCM_VALUE        1
0032 #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE    2
0033 #define DSA_TAG_PROTO_DSA_VALUE         3
0034 #define DSA_TAG_PROTO_EDSA_VALUE        4
0035 #define DSA_TAG_PROTO_GSWIP_VALUE       5
0036 #define DSA_TAG_PROTO_KSZ9477_VALUE     6
0037 #define DSA_TAG_PROTO_KSZ9893_VALUE     7
0038 #define DSA_TAG_PROTO_LAN9303_VALUE     8
0039 #define DSA_TAG_PROTO_MTK_VALUE         9
0040 #define DSA_TAG_PROTO_QCA_VALUE         10
0041 #define DSA_TAG_PROTO_TRAILER_VALUE     11
0042 #define DSA_TAG_PROTO_8021Q_VALUE       12
0043 #define DSA_TAG_PROTO_SJA1105_VALUE     13
0044 #define DSA_TAG_PROTO_KSZ8795_VALUE     14
0045 #define DSA_TAG_PROTO_OCELOT_VALUE      15
0046 #define DSA_TAG_PROTO_AR9331_VALUE      16
0047 #define DSA_TAG_PROTO_RTL4_A_VALUE      17
0048 #define DSA_TAG_PROTO_HELLCREEK_VALUE       18
0049 #define DSA_TAG_PROTO_XRS700X_VALUE     19
0050 #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE    20
0051 #define DSA_TAG_PROTO_SEVILLE_VALUE     21
0052 #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE     22
0053 #define DSA_TAG_PROTO_SJA1110_VALUE     23
0054 #define DSA_TAG_PROTO_RTL8_4_VALUE      24
0055 #define DSA_TAG_PROTO_RTL8_4T_VALUE     25
0056 #define DSA_TAG_PROTO_RZN1_A5PSW_VALUE      26
0057 #define DSA_TAG_PROTO_LAN937X_VALUE     27
0058 
0059 enum dsa_tag_protocol {
0060     DSA_TAG_PROTO_NONE      = DSA_TAG_PROTO_NONE_VALUE,
0061     DSA_TAG_PROTO_BRCM      = DSA_TAG_PROTO_BRCM_VALUE,
0062     DSA_TAG_PROTO_BRCM_LEGACY   = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
0063     DSA_TAG_PROTO_BRCM_PREPEND  = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
0064     DSA_TAG_PROTO_DSA       = DSA_TAG_PROTO_DSA_VALUE,
0065     DSA_TAG_PROTO_EDSA      = DSA_TAG_PROTO_EDSA_VALUE,
0066     DSA_TAG_PROTO_GSWIP     = DSA_TAG_PROTO_GSWIP_VALUE,
0067     DSA_TAG_PROTO_KSZ9477       = DSA_TAG_PROTO_KSZ9477_VALUE,
0068     DSA_TAG_PROTO_KSZ9893       = DSA_TAG_PROTO_KSZ9893_VALUE,
0069     DSA_TAG_PROTO_LAN9303       = DSA_TAG_PROTO_LAN9303_VALUE,
0070     DSA_TAG_PROTO_MTK       = DSA_TAG_PROTO_MTK_VALUE,
0071     DSA_TAG_PROTO_QCA       = DSA_TAG_PROTO_QCA_VALUE,
0072     DSA_TAG_PROTO_TRAILER       = DSA_TAG_PROTO_TRAILER_VALUE,
0073     DSA_TAG_PROTO_8021Q     = DSA_TAG_PROTO_8021Q_VALUE,
0074     DSA_TAG_PROTO_SJA1105       = DSA_TAG_PROTO_SJA1105_VALUE,
0075     DSA_TAG_PROTO_KSZ8795       = DSA_TAG_PROTO_KSZ8795_VALUE,
0076     DSA_TAG_PROTO_OCELOT        = DSA_TAG_PROTO_OCELOT_VALUE,
0077     DSA_TAG_PROTO_AR9331        = DSA_TAG_PROTO_AR9331_VALUE,
0078     DSA_TAG_PROTO_RTL4_A        = DSA_TAG_PROTO_RTL4_A_VALUE,
0079     DSA_TAG_PROTO_HELLCREEK     = DSA_TAG_PROTO_HELLCREEK_VALUE,
0080     DSA_TAG_PROTO_XRS700X       = DSA_TAG_PROTO_XRS700X_VALUE,
0081     DSA_TAG_PROTO_OCELOT_8021Q  = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
0082     DSA_TAG_PROTO_SEVILLE       = DSA_TAG_PROTO_SEVILLE_VALUE,
0083     DSA_TAG_PROTO_SJA1110       = DSA_TAG_PROTO_SJA1110_VALUE,
0084     DSA_TAG_PROTO_RTL8_4        = DSA_TAG_PROTO_RTL8_4_VALUE,
0085     DSA_TAG_PROTO_RTL8_4T       = DSA_TAG_PROTO_RTL8_4T_VALUE,
0086     DSA_TAG_PROTO_RZN1_A5PSW    = DSA_TAG_PROTO_RZN1_A5PSW_VALUE,
0087     DSA_TAG_PROTO_LAN937X       = DSA_TAG_PROTO_LAN937X_VALUE,
0088 };
0089 
0090 struct dsa_switch;
0091 
0092 struct dsa_device_ops {
0093     struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
0094     struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
0095     void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
0096                  int *offset);
0097     int (*connect)(struct dsa_switch *ds);
0098     void (*disconnect)(struct dsa_switch *ds);
0099     unsigned int needed_headroom;
0100     unsigned int needed_tailroom;
0101     const char *name;
0102     enum dsa_tag_protocol proto;
0103     /* Some tagging protocols either mangle or shift the destination MAC
0104      * address, in which case the DSA master would drop packets on ingress
0105      * if what it understands out of the destination MAC address is not in
0106      * its RX filter.
0107      */
0108     bool promisc_on_master;
0109 };
0110 
0111 /* This structure defines the control interfaces that are overlayed by the
0112  * DSA layer on top of the DSA CPU/management net_device instance. This is
0113  * used by the core net_device layer while calling various net_device_ops
0114  * function pointers.
0115  */
0116 struct dsa_netdevice_ops {
0117     int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
0118                  int cmd);
0119 };
0120 
0121 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
0122 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)                \
0123     MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
0124 
0125 struct dsa_lag {
0126     struct net_device *dev;
0127     unsigned int id;
0128     struct mutex fdb_lock;
0129     struct list_head fdbs;
0130     refcount_t refcount;
0131 };
0132 
0133 struct dsa_switch_tree {
0134     struct list_head    list;
0135 
0136     /* List of switch ports */
0137     struct list_head ports;
0138 
0139     /* Notifier chain for switch-wide events */
0140     struct raw_notifier_head    nh;
0141 
0142     /* Tree identifier */
0143     unsigned int index;
0144 
0145     /* Number of switches attached to this tree */
0146     struct kref refcount;
0147 
0148     /* Maps offloaded LAG netdevs to a zero-based linear ID for
0149      * drivers that need it.
0150      */
0151     struct dsa_lag **lags;
0152 
0153     /* Tagging protocol operations */
0154     const struct dsa_device_ops *tag_ops;
0155 
0156     /* Default tagging protocol preferred by the switches in this
0157      * tree.
0158      */
0159     enum dsa_tag_protocol default_proto;
0160 
0161     /* Has this tree been applied to the hardware? */
0162     bool setup;
0163 
0164     /*
0165      * Configuration data for the platform device that owns
0166      * this dsa switch tree instance.
0167      */
0168     struct dsa_platform_data    *pd;
0169 
0170     /* List of DSA links composing the routing table */
0171     struct list_head rtable;
0172 
0173     /* Length of "lags" array */
0174     unsigned int lags_len;
0175 
0176     /* Track the largest switch index within a tree */
0177     unsigned int last_switch;
0178 };
0179 
0180 /* LAG IDs are one-based, the dst->lags array is zero-based */
0181 #define dsa_lags_foreach_id(_id, _dst)              \
0182     for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \
0183         if ((_dst)->lags[(_id) - 1])
0184 
0185 #define dsa_lag_foreach_port(_dp, _dst, _lag)           \
0186     list_for_each_entry((_dp), &(_dst)->ports, list)    \
0187         if (dsa_port_offloads_lag((_dp), (_lag)))
0188 
0189 #define dsa_hsr_foreach_port(_dp, _ds, _hsr)            \
0190     list_for_each_entry((_dp), &(_ds)->dst->ports, list)    \
0191         if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
0192 
0193 static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
0194                         unsigned int id)
0195 {
0196     /* DSA LAG IDs are one-based, dst->lags is zero-based */
0197     return dst->lags[id - 1];
0198 }
0199 
0200 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
0201                  struct net_device *lag_dev)
0202 {
0203     unsigned int id;
0204 
0205     dsa_lags_foreach_id(id, dst) {
0206         struct dsa_lag *lag = dsa_lag_by_id(dst, id);
0207 
0208         if (lag->dev == lag_dev)
0209             return lag->id;
0210     }
0211 
0212     return -ENODEV;
0213 }
0214 
0215 /* TC matchall action types */
0216 enum dsa_port_mall_action_type {
0217     DSA_PORT_MALL_MIRROR,
0218     DSA_PORT_MALL_POLICER,
0219 };
0220 
0221 /* TC mirroring entry */
0222 struct dsa_mall_mirror_tc_entry {
0223     u8 to_local_port;
0224     bool ingress;
0225 };
0226 
0227 /* TC port policer entry */
0228 struct dsa_mall_policer_tc_entry {
0229     u32 burst;
0230     u64 rate_bytes_per_sec;
0231 };
0232 
0233 /* TC matchall entry */
0234 struct dsa_mall_tc_entry {
0235     struct list_head list;
0236     unsigned long cookie;
0237     enum dsa_port_mall_action_type type;
0238     union {
0239         struct dsa_mall_mirror_tc_entry mirror;
0240         struct dsa_mall_policer_tc_entry policer;
0241     };
0242 };
0243 
0244 struct dsa_bridge {
0245     struct net_device *dev;
0246     unsigned int num;
0247     bool tx_fwd_offload;
0248     refcount_t refcount;
0249 };
0250 
0251 struct dsa_port {
0252     /* A CPU port is physically connected to a master device.
0253      * A user port exposed to userspace has a slave device.
0254      */
0255     union {
0256         struct net_device *master;
0257         struct net_device *slave;
0258     };
0259 
0260     /* Copy of the tagging protocol operations, for quicker access
0261      * in the data path. Valid only for the CPU ports.
0262      */
0263     const struct dsa_device_ops *tag_ops;
0264 
0265     /* Copies for faster access in master receive hot path */
0266     struct dsa_switch_tree *dst;
0267     struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
0268 
0269     struct dsa_switch   *ds;
0270 
0271     unsigned int        index;
0272 
0273     enum {
0274         DSA_PORT_TYPE_UNUSED = 0,
0275         DSA_PORT_TYPE_CPU,
0276         DSA_PORT_TYPE_DSA,
0277         DSA_PORT_TYPE_USER,
0278     } type;
0279 
0280     const char      *name;
0281     struct dsa_port     *cpu_dp;
0282     u8          mac[ETH_ALEN];
0283 
0284     u8          stp_state;
0285 
0286     /* Warning: the following bit fields are not atomic, and updating them
0287      * can only be done from code paths where concurrency is not possible
0288      * (probe time or under rtnl_lock).
0289      */
0290     u8          vlan_filtering:1;
0291 
0292     /* Managed by DSA on user ports and by drivers on CPU and DSA ports */
0293     u8          learning:1;
0294 
0295     u8          lag_tx_enabled:1;
0296 
0297     u8          devlink_port_setup:1;
0298 
0299     /* Master state bits, valid only on CPU ports */
0300     u8          master_admin_up:1;
0301     u8          master_oper_up:1;
0302 
0303     u8          setup:1;
0304 
0305     struct device_node  *dn;
0306     unsigned int        ageing_time;
0307 
0308     struct dsa_bridge   *bridge;
0309     struct devlink_port devlink_port;
0310     struct phylink      *pl;
0311     struct phylink_config   pl_config;
0312     struct dsa_lag      *lag;
0313     struct net_device   *hsr_dev;
0314 
0315     struct list_head list;
0316 
0317     /*
0318      * Original copy of the master netdev ethtool_ops
0319      */
0320     const struct ethtool_ops *orig_ethtool_ops;
0321 
0322     /*
0323      * Original copy of the master netdev net_device_ops
0324      */
0325     const struct dsa_netdevice_ops *netdev_ops;
0326 
0327     /* List of MAC addresses that must be forwarded on this port.
0328      * These are only valid on CPU ports and DSA links.
0329      */
0330     struct mutex        addr_lists_lock;
0331     struct list_head    fdbs;
0332     struct list_head    mdbs;
0333 
0334     /* List of VLANs that CPU and DSA ports are members of. */
0335     struct mutex        vlans_lock;
0336     struct list_head    vlans;
0337 };
0338 
0339 /* TODO: ideally DSA ports would have a single dp->link_dp member,
0340  * and no dst->rtable nor this struct dsa_link would be needed,
0341  * but this would require some more complex tree walking,
0342  * so keep it stupid at the moment and list them all.
0343  */
0344 struct dsa_link {
0345     struct dsa_port *dp;
0346     struct dsa_port *link_dp;
0347     struct list_head list;
0348 };
0349 
0350 enum dsa_db_type {
0351     DSA_DB_PORT,
0352     DSA_DB_LAG,
0353     DSA_DB_BRIDGE,
0354 };
0355 
0356 struct dsa_db {
0357     enum dsa_db_type type;
0358 
0359     union {
0360         const struct dsa_port *dp;
0361         struct dsa_lag lag;
0362         struct dsa_bridge bridge;
0363     };
0364 };
0365 
0366 struct dsa_mac_addr {
0367     unsigned char addr[ETH_ALEN];
0368     u16 vid;
0369     refcount_t refcount;
0370     struct list_head list;
0371     struct dsa_db db;
0372 };
0373 
0374 struct dsa_vlan {
0375     u16 vid;
0376     refcount_t refcount;
0377     struct list_head list;
0378 };
0379 
0380 struct dsa_switch {
0381     struct device *dev;
0382 
0383     /*
0384      * Parent switch tree, and switch index.
0385      */
0386     struct dsa_switch_tree  *dst;
0387     unsigned int        index;
0388 
0389     /* Warning: the following bit fields are not atomic, and updating them
0390      * can only be done from code paths where concurrency is not possible
0391      * (probe time or under rtnl_lock).
0392      */
0393     u32         setup:1;
0394 
0395     /* Disallow bridge core from requesting different VLAN awareness
0396      * settings on ports if not hardware-supported
0397      */
0398     u32         vlan_filtering_is_global:1;
0399 
0400     /* Keep VLAN filtering enabled on ports not offloading any upper */
0401     u32         needs_standalone_vlan_filtering:1;
0402 
0403     /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
0404      * that have vlan_filtering=0. All drivers should ideally set this (and
0405      * then the option would get removed), but it is unknown whether this
0406      * would break things or not.
0407      */
0408     u32         configure_vlan_while_not_filtering:1;
0409 
0410     /* If the switch driver always programs the CPU port as egress tagged
0411      * despite the VLAN configuration indicating otherwise, then setting
0412      * @untag_bridge_pvid will force the DSA receive path to pop the
0413      * bridge's default_pvid VLAN tagged frames to offer a consistent
0414      * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge
0415      * device.
0416      */
0417     u32         untag_bridge_pvid:1;
0418 
0419     /* Let DSA manage the FDB entries towards the
0420      * CPU, based on the software bridge database.
0421      */
0422     u32         assisted_learning_on_cpu_port:1;
0423 
0424     /* In case vlan_filtering_is_global is set, the VLAN awareness state
0425      * should be retrieved from here and not from the per-port settings.
0426      */
0427     u32         vlan_filtering:1;
0428 
0429     /* For switches that only have the MRU configurable. To ensure the
0430      * configured MTU is not exceeded, normalization of MRU on all bridged
0431      * interfaces is needed.
0432      */
0433     u32         mtu_enforcement_ingress:1;
0434 
0435     /* Drivers that isolate the FDBs of multiple bridges must set this
0436      * to true to receive the bridge as an argument in .port_fdb_{add,del}
0437      * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
0438      * passed as zero.
0439      */
0440     u32         fdb_isolation:1;
0441 
0442     /* Listener for switch fabric events */
0443     struct notifier_block   nb;
0444 
0445     /*
0446      * Give the switch driver somewhere to hang its private data
0447      * structure.
0448      */
0449     void *priv;
0450 
0451     void *tagger_data;
0452 
0453     /*
0454      * Configuration data for this switch.
0455      */
0456     struct dsa_chip_data    *cd;
0457 
0458     /*
0459      * The switch operations.
0460      */
0461     const struct dsa_switch_ops *ops;
0462 
0463     /*
0464      * Slave mii_bus and devices for the individual ports.
0465      */
0466     u32         phys_mii_mask;
0467     struct mii_bus      *slave_mii_bus;
0468 
0469     /* Ageing Time limits in msecs */
0470     unsigned int ageing_time_min;
0471     unsigned int ageing_time_max;
0472 
0473     /* Storage for drivers using tag_8021q */
0474     struct dsa_8021q_context *tag_8021q_ctx;
0475 
0476     /* devlink used to represent this switch device */
0477     struct devlink      *devlink;
0478 
0479     /* Number of switch port queues */
0480     unsigned int        num_tx_queues;
0481 
0482     /* Drivers that benefit from having an ID associated with each
0483      * offloaded LAG should set this to the maximum number of
0484      * supported IDs. DSA will then maintain a mapping of _at
0485      * least_ these many IDs, accessible to drivers via
0486      * dsa_lag_id().
0487      */
0488     unsigned int        num_lag_ids;
0489 
0490     /* Drivers that support bridge forwarding offload or FDB isolation
0491      * should set this to the maximum number of bridges spanning the same
0492      * switch tree (or all trees, in the case of cross-tree bridging
0493      * support) that can be offloaded.
0494      */
0495     unsigned int        max_num_bridges;
0496 
0497     unsigned int        num_ports;
0498 };
0499 
0500 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
0501 {
0502     struct dsa_switch_tree *dst = ds->dst;
0503     struct dsa_port *dp;
0504 
0505     list_for_each_entry(dp, &dst->ports, list)
0506         if (dp->ds == ds && dp->index == p)
0507             return dp;
0508 
0509     return NULL;
0510 }
0511 
0512 static inline bool dsa_port_is_dsa(struct dsa_port *port)
0513 {
0514     return port->type == DSA_PORT_TYPE_DSA;
0515 }
0516 
0517 static inline bool dsa_port_is_cpu(struct dsa_port *port)
0518 {
0519     return port->type == DSA_PORT_TYPE_CPU;
0520 }
0521 
0522 static inline bool dsa_port_is_user(struct dsa_port *dp)
0523 {
0524     return dp->type == DSA_PORT_TYPE_USER;
0525 }
0526 
0527 static inline bool dsa_port_is_unused(struct dsa_port *dp)
0528 {
0529     return dp->type == DSA_PORT_TYPE_UNUSED;
0530 }
0531 
0532 static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
0533 {
0534     return dsa_port_is_cpu(dp) && dp->master_admin_up &&
0535            dp->master_oper_up;
0536 }
0537 
0538 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
0539 {
0540     return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
0541 }
0542 
0543 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
0544 {
0545     return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
0546 }
0547 
0548 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
0549 {
0550     return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
0551 }
0552 
0553 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
0554 {
0555     return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
0556 }
0557 
0558 #define dsa_tree_for_each_user_port(_dp, _dst) \
0559     list_for_each_entry((_dp), &(_dst)->ports, list) \
0560         if (dsa_port_is_user((_dp)))
0561 
0562 #define dsa_switch_for_each_port(_dp, _ds) \
0563     list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
0564         if ((_dp)->ds == (_ds))
0565 
0566 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
0567     list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
0568         if ((_dp)->ds == (_ds))
0569 
0570 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
0571     list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
0572         if ((_dp)->ds == (_ds))
0573 
0574 #define dsa_switch_for_each_available_port(_dp, _ds) \
0575     dsa_switch_for_each_port((_dp), (_ds)) \
0576         if (!dsa_port_is_unused((_dp)))
0577 
0578 #define dsa_switch_for_each_user_port(_dp, _ds) \
0579     dsa_switch_for_each_port((_dp), (_ds)) \
0580         if (dsa_port_is_user((_dp)))
0581 
0582 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
0583     dsa_switch_for_each_port((_dp), (_ds)) \
0584         if (dsa_port_is_cpu((_dp)))
0585 
0586 #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \
0587     dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
0588         if (dsa_port_is_cpu((_dp)))
0589 
0590 static inline u32 dsa_user_ports(struct dsa_switch *ds)
0591 {
0592     struct dsa_port *dp;
0593     u32 mask = 0;
0594 
0595     dsa_switch_for_each_user_port(dp, ds)
0596         mask |= BIT(dp->index);
0597 
0598     return mask;
0599 }
0600 
0601 static inline u32 dsa_cpu_ports(struct dsa_switch *ds)
0602 {
0603     struct dsa_port *cpu_dp;
0604     u32 mask = 0;
0605 
0606     dsa_switch_for_each_cpu_port(cpu_dp, ds)
0607         mask |= BIT(cpu_dp->index);
0608 
0609     return mask;
0610 }
0611 
0612 /* Return the local port used to reach an arbitrary switch device */
0613 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
0614 {
0615     struct dsa_switch_tree *dst = ds->dst;
0616     struct dsa_link *dl;
0617 
0618     list_for_each_entry(dl, &dst->rtable, list)
0619         if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
0620             return dl->dp->index;
0621 
0622     return ds->num_ports;
0623 }
0624 
0625 /* Return the local port used to reach an arbitrary switch port */
0626 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
0627                         int port)
0628 {
0629     if (device == ds->index)
0630         return port;
0631     else
0632         return dsa_routing_port(ds, device);
0633 }
0634 
0635 /* Return the local port used to reach the dedicated CPU port */
0636 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
0637 {
0638     const struct dsa_port *dp = dsa_to_port(ds, port);
0639     const struct dsa_port *cpu_dp = dp->cpu_dp;
0640 
0641     if (!cpu_dp)
0642         return port;
0643 
0644     return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
0645 }
0646 
0647 /* Return true if this is the local port used to reach the CPU port */
0648 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
0649 {
0650     if (dsa_is_unused_port(ds, port))
0651         return false;
0652 
0653     return port == dsa_upstream_port(ds, port);
0654 }
0655 
0656 /* Return true if this is a DSA port leading away from the CPU */
0657 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
0658 {
0659     return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
0660 }
0661 
0662 /* Return the local port used to reach the CPU port */
0663 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
0664 {
0665     struct dsa_port *dp;
0666 
0667     dsa_switch_for_each_available_port(dp, ds) {
0668         return dsa_upstream_port(ds, dp->index);
0669     }
0670 
0671     return ds->num_ports;
0672 }
0673 
0674 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
0675  * that the routing port from @downstream_ds to @upstream_ds is also the port
0676  * which @downstream_ds uses to reach its dedicated CPU.
0677  */
0678 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
0679                          struct dsa_switch *downstream_ds)
0680 {
0681     int routing_port;
0682 
0683     if (upstream_ds == downstream_ds)
0684         return true;
0685 
0686     routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
0687 
0688     return dsa_is_upstream_port(downstream_ds, routing_port);
0689 }
0690 
0691 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
0692 {
0693     const struct dsa_switch *ds = dp->ds;
0694 
0695     if (ds->vlan_filtering_is_global)
0696         return ds->vlan_filtering;
0697     else
0698         return dp->vlan_filtering;
0699 }
0700 
0701 static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
0702 {
0703     return dp->lag ? dp->lag->id : 0;
0704 }
0705 
0706 static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
0707 {
0708     return dp->lag ? dp->lag->dev : NULL;
0709 }
0710 
0711 static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
0712                      const struct dsa_lag *lag)
0713 {
0714     return dsa_port_lag_dev_get(dp) == lag->dev;
0715 }
0716 
0717 static inline
0718 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
0719 {
0720     if (!dp->bridge)
0721         return NULL;
0722 
0723     if (dp->lag)
0724         return dp->lag->dev;
0725     else if (dp->hsr_dev)
0726         return dp->hsr_dev;
0727 
0728     return dp->slave;
0729 }
0730 
0731 static inline struct net_device *
0732 dsa_port_bridge_dev_get(const struct dsa_port *dp)
0733 {
0734     return dp->bridge ? dp->bridge->dev : NULL;
0735 }
0736 
0737 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
0738 {
0739     return dp->bridge ? dp->bridge->num : 0;
0740 }
0741 
0742 static inline bool dsa_port_bridge_same(const struct dsa_port *a,
0743                     const struct dsa_port *b)
0744 {
0745     struct net_device *br_a = dsa_port_bridge_dev_get(a);
0746     struct net_device *br_b = dsa_port_bridge_dev_get(b);
0747 
0748     /* Standalone ports are not in the same bridge with one another */
0749     return (!br_a || !br_b) ? false : (br_a == br_b);
0750 }
0751 
0752 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
0753                          const struct net_device *dev)
0754 {
0755     return dsa_port_to_bridge_port(dp) == dev;
0756 }
0757 
0758 static inline bool
0759 dsa_port_offloads_bridge_dev(struct dsa_port *dp,
0760                  const struct net_device *bridge_dev)
0761 {
0762     /* DSA ports connected to a bridge, and event was emitted
0763      * for the bridge.
0764      */
0765     return dsa_port_bridge_dev_get(dp) == bridge_dev;
0766 }
0767 
0768 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
0769                         const struct dsa_bridge *bridge)
0770 {
0771     return dsa_port_bridge_dev_get(dp) == bridge->dev;
0772 }
0773 
0774 /* Returns true if any port of this tree offloads the given net_device */
0775 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
0776                          const struct net_device *dev)
0777 {
0778     struct dsa_port *dp;
0779 
0780     list_for_each_entry(dp, &dst->ports, list)
0781         if (dsa_port_offloads_bridge_port(dp, dev))
0782             return true;
0783 
0784     return false;
0785 }
0786 
0787 /* Returns true if any port of this tree offloads the given bridge */
0788 static inline bool
0789 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
0790                  const struct net_device *bridge_dev)
0791 {
0792     struct dsa_port *dp;
0793 
0794     list_for_each_entry(dp, &dst->ports, list)
0795         if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
0796             return true;
0797 
0798     return false;
0799 }
0800 
0801 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
0802                   bool is_static, void *data);
0803 struct dsa_switch_ops {
0804     /*
0805      * Tagging protocol helpers called for the CPU ports and DSA links.
0806      * @get_tag_protocol retrieves the initial tagging protocol and is
0807      * mandatory. Switches which can operate using multiple tagging
0808      * protocols should implement @change_tag_protocol and report in
0809      * @get_tag_protocol the tagger in current use.
0810      */
0811     enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
0812                           int port,
0813                           enum dsa_tag_protocol mprot);
0814     int (*change_tag_protocol)(struct dsa_switch *ds,
0815                        enum dsa_tag_protocol proto);
0816     /*
0817      * Method for switch drivers to connect to the tagging protocol driver
0818      * in current use. The switch driver can provide handlers for certain
0819      * types of packets for switch management.
0820      */
0821     int (*connect_tag_protocol)(struct dsa_switch *ds,
0822                     enum dsa_tag_protocol proto);
0823 
0824     /* Optional switch-wide initialization and destruction methods */
0825     int (*setup)(struct dsa_switch *ds);
0826     void    (*teardown)(struct dsa_switch *ds);
0827 
0828     /* Per-port initialization and destruction methods. Mandatory if the
0829      * driver registers devlink port regions, optional otherwise.
0830      */
0831     int (*port_setup)(struct dsa_switch *ds, int port);
0832     void    (*port_teardown)(struct dsa_switch *ds, int port);
0833 
0834     u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
0835 
0836     /*
0837      * Access to the switch's PHY registers.
0838      */
0839     int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
0840     int (*phy_write)(struct dsa_switch *ds, int port,
0841                  int regnum, u16 val);
0842 
0843     /*
0844      * Link state adjustment (called from libphy)
0845      */
0846     void    (*adjust_link)(struct dsa_switch *ds, int port,
0847                 struct phy_device *phydev);
0848     void    (*fixed_link_update)(struct dsa_switch *ds, int port,
0849                 struct fixed_phy_status *st);
0850 
0851     /*
0852      * PHYLINK integration
0853      */
0854     void    (*phylink_get_caps)(struct dsa_switch *ds, int port,
0855                     struct phylink_config *config);
0856     void    (*phylink_validate)(struct dsa_switch *ds, int port,
0857                     unsigned long *supported,
0858                     struct phylink_link_state *state);
0859     struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
0860                               int port,
0861                               phy_interface_t iface);
0862     int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
0863                       struct phylink_link_state *state);
0864     void    (*phylink_mac_config)(struct dsa_switch *ds, int port,
0865                       unsigned int mode,
0866                       const struct phylink_link_state *state);
0867     void    (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
0868     void    (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
0869                      unsigned int mode,
0870                      phy_interface_t interface);
0871     void    (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
0872                        unsigned int mode,
0873                        phy_interface_t interface,
0874                        struct phy_device *phydev,
0875                        int speed, int duplex,
0876                        bool tx_pause, bool rx_pause);
0877     void    (*phylink_fixed_state)(struct dsa_switch *ds, int port,
0878                        struct phylink_link_state *state);
0879     /*
0880      * Port statistics counters.
0881      */
0882     void    (*get_strings)(struct dsa_switch *ds, int port,
0883                    u32 stringset, uint8_t *data);
0884     void    (*get_ethtool_stats)(struct dsa_switch *ds,
0885                      int port, uint64_t *data);
0886     int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
0887     void    (*get_ethtool_phy_stats)(struct dsa_switch *ds,
0888                      int port, uint64_t *data);
0889     void    (*get_eth_phy_stats)(struct dsa_switch *ds, int port,
0890                      struct ethtool_eth_phy_stats *phy_stats);
0891     void    (*get_eth_mac_stats)(struct dsa_switch *ds, int port,
0892                      struct ethtool_eth_mac_stats *mac_stats);
0893     void    (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
0894                       struct ethtool_eth_ctrl_stats *ctrl_stats);
0895     void    (*get_rmon_stats)(struct dsa_switch *ds, int port,
0896                   struct ethtool_rmon_stats *rmon_stats,
0897                   const struct ethtool_rmon_hist_range **ranges);
0898     void    (*get_stats64)(struct dsa_switch *ds, int port,
0899                    struct rtnl_link_stats64 *s);
0900     void    (*get_pause_stats)(struct dsa_switch *ds, int port,
0901                    struct ethtool_pause_stats *pause_stats);
0902     void    (*self_test)(struct dsa_switch *ds, int port,
0903                  struct ethtool_test *etest, u64 *data);
0904 
0905     /*
0906      * ethtool Wake-on-LAN
0907      */
0908     void    (*get_wol)(struct dsa_switch *ds, int port,
0909                struct ethtool_wolinfo *w);
0910     int (*set_wol)(struct dsa_switch *ds, int port,
0911                struct ethtool_wolinfo *w);
0912 
0913     /*
0914      * ethtool timestamp info
0915      */
0916     int (*get_ts_info)(struct dsa_switch *ds, int port,
0917                    struct ethtool_ts_info *ts);
0918 
0919     /*
0920      * DCB ops
0921      */
0922     int (*port_get_default_prio)(struct dsa_switch *ds, int port);
0923     int (*port_set_default_prio)(struct dsa_switch *ds, int port,
0924                      u8 prio);
0925     int (*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
0926     int (*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
0927                       u8 prio);
0928     int (*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
0929                       u8 prio);
0930 
0931     /*
0932      * Suspend and resume
0933      */
0934     int (*suspend)(struct dsa_switch *ds);
0935     int (*resume)(struct dsa_switch *ds);
0936 
0937     /*
0938      * Port enable/disable
0939      */
0940     int (*port_enable)(struct dsa_switch *ds, int port,
0941                    struct phy_device *phy);
0942     void    (*port_disable)(struct dsa_switch *ds, int port);
0943 
0944     /*
0945      * Port's MAC EEE settings
0946      */
0947     int (*set_mac_eee)(struct dsa_switch *ds, int port,
0948                    struct ethtool_eee *e);
0949     int (*get_mac_eee)(struct dsa_switch *ds, int port,
0950                    struct ethtool_eee *e);
0951 
0952     /* EEPROM access */
0953     int (*get_eeprom_len)(struct dsa_switch *ds);
0954     int (*get_eeprom)(struct dsa_switch *ds,
0955                   struct ethtool_eeprom *eeprom, u8 *data);
0956     int (*set_eeprom)(struct dsa_switch *ds,
0957                   struct ethtool_eeprom *eeprom, u8 *data);
0958 
0959     /*
0960      * Register access.
0961      */
0962     int (*get_regs_len)(struct dsa_switch *ds, int port);
0963     void    (*get_regs)(struct dsa_switch *ds, int port,
0964                 struct ethtool_regs *regs, void *p);
0965 
0966     /*
0967      * Upper device tracking.
0968      */
0969     int (*port_prechangeupper)(struct dsa_switch *ds, int port,
0970                        struct netdev_notifier_changeupper_info *info);
0971 
0972     /*
0973      * Bridge integration
0974      */
0975     int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
0976     int (*port_bridge_join)(struct dsa_switch *ds, int port,
0977                     struct dsa_bridge bridge,
0978                     bool *tx_fwd_offload,
0979                     struct netlink_ext_ack *extack);
0980     void    (*port_bridge_leave)(struct dsa_switch *ds, int port,
0981                      struct dsa_bridge bridge);
0982     void    (*port_stp_state_set)(struct dsa_switch *ds, int port,
0983                       u8 state);
0984     int (*port_mst_state_set)(struct dsa_switch *ds, int port,
0985                       const struct switchdev_mst_state *state);
0986     void    (*port_fast_age)(struct dsa_switch *ds, int port);
0987     int (*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
0988     int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
0989                      struct switchdev_brport_flags flags,
0990                      struct netlink_ext_ack *extack);
0991     int (*port_bridge_flags)(struct dsa_switch *ds, int port,
0992                      struct switchdev_brport_flags flags,
0993                      struct netlink_ext_ack *extack);
0994     void    (*port_set_host_flood)(struct dsa_switch *ds, int port,
0995                        bool uc, bool mc);
0996 
0997     /*
0998      * VLAN support
0999      */
1000     int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
1001                        bool vlan_filtering,
1002                        struct netlink_ext_ack *extack);
1003     int (*port_vlan_add)(struct dsa_switch *ds, int port,
1004                  const struct switchdev_obj_port_vlan *vlan,
1005                  struct netlink_ext_ack *extack);
1006     int (*port_vlan_del)(struct dsa_switch *ds, int port,
1007                  const struct switchdev_obj_port_vlan *vlan);
1008     int (*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge,
1009                  const struct switchdev_vlan_msti *msti);
1010 
1011     /*
1012      * Forwarding database
1013      */
1014     int (*port_fdb_add)(struct dsa_switch *ds, int port,
1015                 const unsigned char *addr, u16 vid,
1016                 struct dsa_db db);
1017     int (*port_fdb_del)(struct dsa_switch *ds, int port,
1018                 const unsigned char *addr, u16 vid,
1019                 struct dsa_db db);
1020     int (*port_fdb_dump)(struct dsa_switch *ds, int port,
1021                  dsa_fdb_dump_cb_t *cb, void *data);
1022     int (*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
1023                    const unsigned char *addr, u16 vid,
1024                    struct dsa_db db);
1025     int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
1026                    const unsigned char *addr, u16 vid,
1027                    struct dsa_db db);
1028 
1029     /*
1030      * Multicast database
1031      */
1032     int (*port_mdb_add)(struct dsa_switch *ds, int port,
1033                 const struct switchdev_obj_port_mdb *mdb,
1034                 struct dsa_db db);
1035     int (*port_mdb_del)(struct dsa_switch *ds, int port,
1036                 const struct switchdev_obj_port_mdb *mdb,
1037                 struct dsa_db db);
1038     /*
1039      * RXNFC
1040      */
1041     int (*get_rxnfc)(struct dsa_switch *ds, int port,
1042                  struct ethtool_rxnfc *nfc, u32 *rule_locs);
1043     int (*set_rxnfc)(struct dsa_switch *ds, int port,
1044                  struct ethtool_rxnfc *nfc);
1045 
1046     /*
1047      * TC integration
1048      */
1049     int (*cls_flower_add)(struct dsa_switch *ds, int port,
1050                   struct flow_cls_offload *cls, bool ingress);
1051     int (*cls_flower_del)(struct dsa_switch *ds, int port,
1052                   struct flow_cls_offload *cls, bool ingress);
1053     int (*cls_flower_stats)(struct dsa_switch *ds, int port,
1054                     struct flow_cls_offload *cls, bool ingress);
1055     int (*port_mirror_add)(struct dsa_switch *ds, int port,
1056                    struct dsa_mall_mirror_tc_entry *mirror,
1057                    bool ingress, struct netlink_ext_ack *extack);
1058     void    (*port_mirror_del)(struct dsa_switch *ds, int port,
1059                    struct dsa_mall_mirror_tc_entry *mirror);
1060     int (*port_policer_add)(struct dsa_switch *ds, int port,
1061                     struct dsa_mall_policer_tc_entry *policer);
1062     void    (*port_policer_del)(struct dsa_switch *ds, int port);
1063     int (*port_setup_tc)(struct dsa_switch *ds, int port,
1064                  enum tc_setup_type type, void *type_data);
1065 
1066     /*
1067      * Cross-chip operations
1068      */
1069     int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
1070                      int sw_index, int port,
1071                      struct dsa_bridge bridge,
1072                      struct netlink_ext_ack *extack);
1073     void    (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
1074                       int sw_index, int port,
1075                       struct dsa_bridge bridge);
1076     int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
1077                     int port);
1078     int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
1079                       int port, struct dsa_lag lag,
1080                       struct netdev_lag_upper_info *info);
1081     int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
1082                        int port, struct dsa_lag lag);
1083 
1084     /*
1085      * PTP functionality
1086      */
1087     int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
1088                      struct ifreq *ifr);
1089     int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
1090                      struct ifreq *ifr);
1091     void    (*port_txtstamp)(struct dsa_switch *ds, int port,
1092                  struct sk_buff *skb);
1093     bool    (*port_rxtstamp)(struct dsa_switch *ds, int port,
1094                  struct sk_buff *skb, unsigned int type);
1095 
1096     /* Devlink parameters, etc */
1097     int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
1098                      struct devlink_param_gset_ctx *ctx);
1099     int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
1100                      struct devlink_param_gset_ctx *ctx);
1101     int (*devlink_info_get)(struct dsa_switch *ds,
1102                     struct devlink_info_req *req,
1103                     struct netlink_ext_ack *extack);
1104     int (*devlink_sb_pool_get)(struct dsa_switch *ds,
1105                        unsigned int sb_index, u16 pool_index,
1106                        struct devlink_sb_pool_info *pool_info);
1107     int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
1108                        u16 pool_index, u32 size,
1109                        enum devlink_sb_threshold_type threshold_type,
1110                        struct netlink_ext_ack *extack);
1111     int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
1112                         unsigned int sb_index, u16 pool_index,
1113                         u32 *p_threshold);
1114     int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
1115                         unsigned int sb_index, u16 pool_index,
1116                         u32 threshold,
1117                         struct netlink_ext_ack *extack);
1118     int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
1119                            unsigned int sb_index, u16 tc_index,
1120                            enum devlink_sb_pool_type pool_type,
1121                            u16 *p_pool_index, u32 *p_threshold);
1122     int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
1123                            unsigned int sb_index, u16 tc_index,
1124                            enum devlink_sb_pool_type pool_type,
1125                            u16 pool_index, u32 threshold,
1126                            struct netlink_ext_ack *extack);
1127     int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
1128                        unsigned int sb_index);
1129     int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
1130                         unsigned int sb_index);
1131     int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
1132                         unsigned int sb_index, u16 pool_index,
1133                         u32 *p_cur, u32 *p_max);
1134     int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
1135                            unsigned int sb_index, u16 tc_index,
1136                            enum devlink_sb_pool_type pool_type,
1137                            u32 *p_cur, u32 *p_max);
1138 
1139     /*
1140      * MTU change functionality. Switches can also adjust their MRU through
1141      * this method. By MTU, one understands the SDU (L2 payload) length.
1142      * If the switch needs to account for the DSA tag on the CPU port, this
1143      * method needs to do so privately.
1144      */
1145     int (*port_change_mtu)(struct dsa_switch *ds, int port,
1146                    int new_mtu);
1147     int (*port_max_mtu)(struct dsa_switch *ds, int port);
1148 
1149     /*
1150      * LAG integration
1151      */
1152     int (*port_lag_change)(struct dsa_switch *ds, int port);
1153     int (*port_lag_join)(struct dsa_switch *ds, int port,
1154                  struct dsa_lag lag,
1155                  struct netdev_lag_upper_info *info);
1156     int (*port_lag_leave)(struct dsa_switch *ds, int port,
1157                   struct dsa_lag lag);
1158 
1159     /*
1160      * HSR integration
1161      */
1162     int (*port_hsr_join)(struct dsa_switch *ds, int port,
1163                  struct net_device *hsr);
1164     int (*port_hsr_leave)(struct dsa_switch *ds, int port,
1165                   struct net_device *hsr);
1166 
1167     /*
1168      * MRP integration
1169      */
1170     int (*port_mrp_add)(struct dsa_switch *ds, int port,
1171                 const struct switchdev_obj_mrp *mrp);
1172     int (*port_mrp_del)(struct dsa_switch *ds, int port,
1173                 const struct switchdev_obj_mrp *mrp);
1174     int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1175                       const struct switchdev_obj_ring_role_mrp *mrp);
1176     int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1177                       const struct switchdev_obj_ring_role_mrp *mrp);
1178 
1179     /*
1180      * tag_8021q operations
1181      */
1182     int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
1183                       u16 flags);
1184     int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1185 
1186     /*
1187      * DSA master tracking operations
1188      */
1189     void    (*master_state_change)(struct dsa_switch *ds,
1190                        const struct net_device *master,
1191                        bool operational);
1192 };
1193 
1194 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)        \
1195     DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,        \
1196                  dsa_devlink_param_get, dsa_devlink_param_set, NULL)
1197 
1198 int dsa_devlink_param_get(struct devlink *dl, u32 id,
1199               struct devlink_param_gset_ctx *ctx);
1200 int dsa_devlink_param_set(struct devlink *dl, u32 id,
1201               struct devlink_param_gset_ctx *ctx);
1202 int dsa_devlink_params_register(struct dsa_switch *ds,
1203                 const struct devlink_param *params,
1204                 size_t params_count);
1205 void dsa_devlink_params_unregister(struct dsa_switch *ds,
1206                    const struct devlink_param *params,
1207                    size_t params_count);
1208 int dsa_devlink_resource_register(struct dsa_switch *ds,
1209                   const char *resource_name,
1210                   u64 resource_size,
1211                   u64 resource_id,
1212                   u64 parent_resource_id,
1213                   const struct devlink_resource_size_params *size_params);
1214 
1215 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
1216 
1217 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
1218                        u64 resource_id,
1219                        devlink_resource_occ_get_t *occ_get,
1220                        void *occ_get_priv);
1221 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
1222                          u64 resource_id);
1223 struct devlink_region *
1224 dsa_devlink_region_create(struct dsa_switch *ds,
1225               const struct devlink_region_ops *ops,
1226               u32 region_max_snapshots, u64 region_size);
1227 struct devlink_region *
1228 dsa_devlink_port_region_create(struct dsa_switch *ds,
1229                    int port,
1230                    const struct devlink_port_region_ops *ops,
1231                    u32 region_max_snapshots, u64 region_size);
1232 void dsa_devlink_region_destroy(struct devlink_region *region);
1233 
1234 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
1235 
1236 struct dsa_devlink_priv {
1237     struct dsa_switch *ds;
1238 };
1239 
1240 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1241 {
1242     struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1243 
1244     return dl_priv->ds;
1245 }
1246 
1247 static inline
1248 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
1249 {
1250     struct devlink *dl = port->devlink;
1251     struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1252 
1253     return dl_priv->ds;
1254 }
1255 
1256 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1257 {
1258     return port->index;
1259 }
1260 
1261 struct dsa_switch_driver {
1262     struct list_head    list;
1263     const struct dsa_switch_ops *ops;
1264 };
1265 
1266 struct net_device *dsa_dev_to_net_device(struct device *dev);
1267 
1268 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
1269                  const unsigned char *addr, u16 vid,
1270                  struct dsa_db db);
1271 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
1272                  const struct switchdev_obj_port_mdb *mdb,
1273                  struct dsa_db db);
1274 
1275 /* Keep inline for faster access in hot path */
1276 static inline bool netdev_uses_dsa(const struct net_device *dev)
1277 {
1278 #if IS_ENABLED(CONFIG_NET_DSA)
1279     return dev->dsa_ptr && dev->dsa_ptr->rcv;
1280 #endif
1281     return false;
1282 }
1283 
1284 /* All DSA tags that push the EtherType to the right (basically all except tail
1285  * tags, which don't break dissection) can be treated the same from the
1286  * perspective of the flow dissector.
1287  *
1288  * We need to return:
1289  *  - offset: the (B - A) difference between:
1290  *    A. the position of the real EtherType and
1291  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1292  *       after the normal EtherType was supposed to be)
1293  *    The offset in bytes is exactly equal to the tagger overhead (and half of
1294  *    that, in __be16 shorts).
1295  *
1296  *  - proto: the value of the real EtherType.
1297  */
1298 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1299                         __be16 *proto, int *offset)
1300 {
1301 #if IS_ENABLED(CONFIG_NET_DSA)
1302     const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1303     int tag_len = ops->needed_headroom;
1304 
1305     *offset = tag_len;
1306     *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1307 #endif
1308 }
1309 
1310 #if IS_ENABLED(CONFIG_NET_DSA)
1311 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1312 {
1313     int err = -EOPNOTSUPP;
1314 
1315     if (!dev->dsa_ptr)
1316         return err;
1317 
1318     if (!dev->dsa_ptr->netdev_ops)
1319         return err;
1320 
1321     return 0;
1322 }
1323 
1324 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1325                     int cmd)
1326 {
1327     const struct dsa_netdevice_ops *ops;
1328     int err;
1329 
1330     err = __dsa_netdevice_ops_check(dev);
1331     if (err)
1332         return err;
1333 
1334     ops = dev->dsa_ptr->netdev_ops;
1335 
1336     return ops->ndo_eth_ioctl(dev, ifr, cmd);
1337 }
1338 #else
1339 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1340                     int cmd)
1341 {
1342     return -EOPNOTSUPP;
1343 }
1344 #endif
1345 
1346 void dsa_unregister_switch(struct dsa_switch *ds);
1347 int dsa_register_switch(struct dsa_switch *ds);
1348 void dsa_switch_shutdown(struct dsa_switch *ds);
1349 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1350 void dsa_flush_workqueue(void);
1351 #ifdef CONFIG_PM_SLEEP
1352 int dsa_switch_suspend(struct dsa_switch *ds);
1353 int dsa_switch_resume(struct dsa_switch *ds);
1354 #else
1355 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1356 {
1357     return 0;
1358 }
1359 static inline int dsa_switch_resume(struct dsa_switch *ds)
1360 {
1361     return 0;
1362 }
1363 #endif /* CONFIG_PM_SLEEP */
1364 
1365 #if IS_ENABLED(CONFIG_NET_DSA)
1366 bool dsa_slave_dev_check(const struct net_device *dev);
1367 #else
1368 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1369 {
1370     return false;
1371 }
1372 #endif
1373 
1374 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1375 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1376 
1377 struct dsa_tag_driver {
1378     const struct dsa_device_ops *ops;
1379     struct list_head list;
1380     struct module *owner;
1381 };
1382 
1383 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1384                   unsigned int count,
1385                   struct module *owner);
1386 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1387                 unsigned int count);
1388 
1389 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1390 static int __init dsa_tag_driver_module_init(void)          \
1391 {                                   \
1392     dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,  \
1393                  THIS_MODULE);              \
1394     return 0;                           \
1395 }                                   \
1396 module_init(dsa_tag_driver_module_init);                \
1397                                     \
1398 static void __exit dsa_tag_driver_module_exit(void)         \
1399 {                                   \
1400     dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);   \
1401 }                                   \
1402 module_exit(dsa_tag_driver_module_exit)
1403 
1404 /**
1405  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1406  * drivers
1407  * @__ops_array: Array of tag driver structures
1408  *
1409  * Helper macro for DSA tag drivers which do not do anything special
1410  * in module init/exit. Each module may only use this macro once, and
1411  * calling it replaces module_init() and module_exit().
1412  */
1413 #define module_dsa_tag_drivers(__ops_array)             \
1414 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1415 
1416 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1417 
1418 /* Create a static structure we can build a linked list of dsa_tag
1419  * drivers
1420  */
1421 #define DSA_TAG_DRIVER(__ops)                       \
1422 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {     \
1423     .ops = &__ops,                          \
1424 }
1425 
1426 /**
1427  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1428  * driver
1429  * @__ops: Single tag driver structures
1430  *
1431  * Helper macro for DSA tag drivers which do not do anything special
1432  * in module init/exit. Each module may only use this macro once, and
1433  * calling it replaces module_init() and module_exit().
1434  */
1435 #define module_dsa_tag_driver(__ops)                    \
1436 DSA_TAG_DRIVER(__ops);                          \
1437                                     \
1438 static struct dsa_tag_driver *dsa_tag_driver_array[] =  {       \
1439     &DSA_TAG_DRIVER_NAME(__ops)                 \
1440 };                                  \
1441 module_dsa_tag_drivers(dsa_tag_driver_array)
1442 #endif
1443