1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
4 * Copyright (c) 2008-2009 Marvell Semiconductor
7 #ifndef __LINUX_NET_DSA_H
8 #define __LINUX_NET_DSA_H
11 #include <linux/if_ether.h>
12 #include <linux/list.h>
13 #include <linux/notifier.h>
14 #include <linux/timer.h>
15 #include <linux/workqueue.h>
17 #include <linux/ethtool.h>
18 #include <linux/net_tstamp.h>
19 #include <linux/phy.h>
20 #include <linux/platform_data/dsa.h>
21 #include <linux/phylink.h>
22 #include <net/devlink.h>
23 #include <net/switchdev.h>
27 struct fixed_phy_status;
28 struct phylink_link_state;
30 #define DSA_TAG_PROTO_NONE_VALUE 0
31 #define DSA_TAG_PROTO_BRCM_VALUE 1
32 #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE 2
33 #define DSA_TAG_PROTO_DSA_VALUE 3
34 #define DSA_TAG_PROTO_EDSA_VALUE 4
35 #define DSA_TAG_PROTO_GSWIP_VALUE 5
36 #define DSA_TAG_PROTO_KSZ9477_VALUE 6
37 #define DSA_TAG_PROTO_KSZ9893_VALUE 7
38 #define DSA_TAG_PROTO_LAN9303_VALUE 8
39 #define DSA_TAG_PROTO_MTK_VALUE 9
40 #define DSA_TAG_PROTO_QCA_VALUE 10
41 #define DSA_TAG_PROTO_TRAILER_VALUE 11
42 #define DSA_TAG_PROTO_8021Q_VALUE 12
43 #define DSA_TAG_PROTO_SJA1105_VALUE 13
44 #define DSA_TAG_PROTO_KSZ8795_VALUE 14
45 #define DSA_TAG_PROTO_OCELOT_VALUE 15
46 #define DSA_TAG_PROTO_AR9331_VALUE 16
47 #define DSA_TAG_PROTO_RTL4_A_VALUE 17
48 #define DSA_TAG_PROTO_HELLCREEK_VALUE 18
49 #define DSA_TAG_PROTO_XRS700X_VALUE 19
50 #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE 20
51 #define DSA_TAG_PROTO_SEVILLE_VALUE 21
52 #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE 22
53 #define DSA_TAG_PROTO_SJA1110_VALUE 23
54 #define DSA_TAG_PROTO_RTL8_4_VALUE 24
55 #define DSA_TAG_PROTO_RTL8_4T_VALUE 25
56 #define DSA_TAG_PROTO_RZN1_A5PSW_VALUE 26
57 #define DSA_TAG_PROTO_LAN937X_VALUE 27
59 enum dsa_tag_protocol {
60 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
61 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
62 DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
63 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
64 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
65 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
66 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
67 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
68 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
69 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
70 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
71 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
72 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
73 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
74 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
75 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE,
76 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE,
77 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE,
78 DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE,
79 DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE,
80 DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE,
81 DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
82 DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE,
83 DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE,
84 DSA_TAG_PROTO_RTL8_4 = DSA_TAG_PROTO_RTL8_4_VALUE,
85 DSA_TAG_PROTO_RTL8_4T = DSA_TAG_PROTO_RTL8_4T_VALUE,
86 DSA_TAG_PROTO_RZN1_A5PSW = DSA_TAG_PROTO_RZN1_A5PSW_VALUE,
87 DSA_TAG_PROTO_LAN937X = DSA_TAG_PROTO_LAN937X_VALUE,
92 struct dsa_device_ops {
93 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
94 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
95 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
97 int (*connect)(struct dsa_switch *ds);
98 void (*disconnect)(struct dsa_switch *ds);
99 unsigned int needed_headroom;
100 unsigned int needed_tailroom;
102 enum dsa_tag_protocol proto;
103 /* Some tagging protocols either mangle or shift the destination MAC
104 * address, in which case the DSA master would drop packets on ingress
105 * if what it understands out of the destination MAC address is not in
108 bool promisc_on_master;
111 /* This structure defines the control interfaces that are overlayed by the
112 * DSA layer on top of the DSA CPU/management net_device instance. This is
113 * used by the core net_device layer while calling various net_device_ops
116 struct dsa_netdevice_ops {
117 int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
121 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
122 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \
123 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
126 struct net_device *dev;
128 struct mutex fdb_lock;
129 struct list_head fdbs;
133 struct dsa_switch_tree {
134 struct list_head list;
136 /* List of switch ports */
137 struct list_head ports;
139 /* Notifier chain for switch-wide events */
140 struct raw_notifier_head nh;
142 /* Tree identifier */
145 /* Number of switches attached to this tree */
146 struct kref refcount;
148 /* Maps offloaded LAG netdevs to a zero-based linear ID for
149 * drivers that need it.
151 struct dsa_lag **lags;
153 /* Tagging protocol operations */
154 const struct dsa_device_ops *tag_ops;
156 /* Default tagging protocol preferred by the switches in this
159 enum dsa_tag_protocol default_proto;
161 /* Has this tree been applied to the hardware? */
165 * Configuration data for the platform device that owns
166 * this dsa switch tree instance.
168 struct dsa_platform_data *pd;
170 /* List of DSA links composing the routing table */
171 struct list_head rtable;
173 /* Length of "lags" array */
174 unsigned int lags_len;
176 /* Track the largest switch index within a tree */
177 unsigned int last_switch;
180 /* LAG IDs are one-based, the dst->lags array is zero-based */
181 #define dsa_lags_foreach_id(_id, _dst) \
182 for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \
183 if ((_dst)->lags[(_id) - 1])
185 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
186 list_for_each_entry((_dp), &(_dst)->ports, list) \
187 if (dsa_port_offloads_lag((_dp), (_lag)))
189 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
190 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
191 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
193 static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
196 /* DSA LAG IDs are one-based, dst->lags is zero-based */
197 return dst->lags[id - 1];
200 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
201 struct net_device *lag_dev)
205 dsa_lags_foreach_id(id, dst) {
206 struct dsa_lag *lag = dsa_lag_by_id(dst, id);
208 if (lag->dev == lag_dev)
215 /* TC matchall action types */
216 enum dsa_port_mall_action_type {
217 DSA_PORT_MALL_MIRROR,
218 DSA_PORT_MALL_POLICER,
221 /* TC mirroring entry */
222 struct dsa_mall_mirror_tc_entry {
227 /* TC port policer entry */
228 struct dsa_mall_policer_tc_entry {
230 u64 rate_bytes_per_sec;
233 /* TC matchall entry */
234 struct dsa_mall_tc_entry {
235 struct list_head list;
236 unsigned long cookie;
237 enum dsa_port_mall_action_type type;
239 struct dsa_mall_mirror_tc_entry mirror;
240 struct dsa_mall_policer_tc_entry policer;
245 struct net_device *dev;
252 /* A CPU port is physically connected to a master device.
253 * A user port exposed to userspace has a slave device.
256 struct net_device *master;
257 struct net_device *slave;
260 /* Copy of the tagging protocol operations, for quicker access
261 * in the data path. Valid only for the CPU ports.
263 const struct dsa_device_ops *tag_ops;
265 /* Copies for faster access in master receive hot path */
266 struct dsa_switch_tree *dst;
267 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
269 struct dsa_switch *ds;
274 DSA_PORT_TYPE_UNUSED = 0,
281 struct dsa_port *cpu_dp;
286 /* Warning: the following bit fields are not atomic, and updating them
287 * can only be done from code paths where concurrency is not possible
288 * (probe time or under rtnl_lock).
292 /* Managed by DSA on user ports and by drivers on CPU and DSA ports */
297 u8 devlink_port_setup:1;
299 /* Master state bits, valid only on CPU ports */
300 u8 master_admin_up:1;
305 struct device_node *dn;
306 unsigned int ageing_time;
308 struct dsa_bridge *bridge;
309 struct devlink_port devlink_port;
311 struct phylink_config pl_config;
313 struct net_device *hsr_dev;
315 struct list_head list;
318 * Original copy of the master netdev ethtool_ops
320 const struct ethtool_ops *orig_ethtool_ops;
323 * Original copy of the master netdev net_device_ops
325 const struct dsa_netdevice_ops *netdev_ops;
327 /* List of MAC addresses that must be forwarded on this port.
328 * These are only valid on CPU ports and DSA links.
330 struct mutex addr_lists_lock;
331 struct list_head fdbs;
332 struct list_head mdbs;
334 /* List of VLANs that CPU and DSA ports are members of. */
335 struct mutex vlans_lock;
336 struct list_head vlans;
339 /* TODO: ideally DSA ports would have a single dp->link_dp member,
340 * and no dst->rtable nor this struct dsa_link would be needed,
341 * but this would require some more complex tree walking,
342 * so keep it stupid at the moment and list them all.
346 struct dsa_port *link_dp;
347 struct list_head list;
357 enum dsa_db_type type;
360 const struct dsa_port *dp;
362 struct dsa_bridge bridge;
366 struct dsa_mac_addr {
367 unsigned char addr[ETH_ALEN];
370 struct list_head list;
377 struct list_head list;
384 * Parent switch tree, and switch index.
386 struct dsa_switch_tree *dst;
389 /* Warning: the following bit fields are not atomic, and updating them
390 * can only be done from code paths where concurrency is not possible
391 * (probe time or under rtnl_lock).
395 /* Disallow bridge core from requesting different VLAN awareness
396 * settings on ports if not hardware-supported
398 u32 vlan_filtering_is_global:1;
400 /* Keep VLAN filtering enabled on ports not offloading any upper */
401 u32 needs_standalone_vlan_filtering:1;
403 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
404 * that have vlan_filtering=0. All drivers should ideally set this (and
405 * then the option would get removed), but it is unknown whether this
406 * would break things or not.
408 u32 configure_vlan_while_not_filtering:1;
410 /* If the switch driver always programs the CPU port as egress tagged
411 * despite the VLAN configuration indicating otherwise, then setting
412 * @untag_bridge_pvid will force the DSA receive path to pop the
413 * bridge's default_pvid VLAN tagged frames to offer a consistent
414 * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge
417 u32 untag_bridge_pvid:1;
419 /* Let DSA manage the FDB entries towards the
420 * CPU, based on the software bridge database.
422 u32 assisted_learning_on_cpu_port:1;
424 /* In case vlan_filtering_is_global is set, the VLAN awareness state
425 * should be retrieved from here and not from the per-port settings.
427 u32 vlan_filtering:1;
429 /* For switches that only have the MRU configurable. To ensure the
430 * configured MTU is not exceeded, normalization of MRU on all bridged
431 * interfaces is needed.
433 u32 mtu_enforcement_ingress:1;
435 /* Drivers that isolate the FDBs of multiple bridges must set this
436 * to true to receive the bridge as an argument in .port_fdb_{add,del}
437 * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
442 /* Listener for switch fabric events */
443 struct notifier_block nb;
446 * Give the switch driver somewhere to hang its private data
454 * Configuration data for this switch.
456 struct dsa_chip_data *cd;
459 * The switch operations.
461 const struct dsa_switch_ops *ops;
464 * Slave mii_bus and devices for the individual ports.
467 struct mii_bus *slave_mii_bus;
469 /* Ageing Time limits in msecs */
470 unsigned int ageing_time_min;
471 unsigned int ageing_time_max;
473 /* Storage for drivers using tag_8021q */
474 struct dsa_8021q_context *tag_8021q_ctx;
476 /* devlink used to represent this switch device */
477 struct devlink *devlink;
479 /* Number of switch port queues */
480 unsigned int num_tx_queues;
482 /* Drivers that benefit from having an ID associated with each
483 * offloaded LAG should set this to the maximum number of
484 * supported IDs. DSA will then maintain a mapping of _at
485 * least_ these many IDs, accessible to drivers via
488 unsigned int num_lag_ids;
490 /* Drivers that support bridge forwarding offload or FDB isolation
491 * should set this to the maximum number of bridges spanning the same
492 * switch tree (or all trees, in the case of cross-tree bridging
493 * support) that can be offloaded.
495 unsigned int max_num_bridges;
497 unsigned int num_ports;
500 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
502 struct dsa_switch_tree *dst = ds->dst;
505 list_for_each_entry(dp, &dst->ports, list)
506 if (dp->ds == ds && dp->index == p)
512 static inline bool dsa_port_is_dsa(struct dsa_port *port)
514 return port->type == DSA_PORT_TYPE_DSA;
517 static inline bool dsa_port_is_cpu(struct dsa_port *port)
519 return port->type == DSA_PORT_TYPE_CPU;
522 static inline bool dsa_port_is_user(struct dsa_port *dp)
524 return dp->type == DSA_PORT_TYPE_USER;
527 static inline bool dsa_port_is_unused(struct dsa_port *dp)
529 return dp->type == DSA_PORT_TYPE_UNUSED;
532 static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
534 return dsa_port_is_cpu(dp) && dp->master_admin_up &&
538 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
540 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
543 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
545 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
548 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
550 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
553 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
555 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
558 #define dsa_tree_for_each_user_port(_dp, _dst) \
559 list_for_each_entry((_dp), &(_dst)->ports, list) \
560 if (dsa_port_is_user((_dp)))
562 #define dsa_switch_for_each_port(_dp, _ds) \
563 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
564 if ((_dp)->ds == (_ds))
566 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
567 list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
568 if ((_dp)->ds == (_ds))
570 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
571 list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
572 if ((_dp)->ds == (_ds))
574 #define dsa_switch_for_each_available_port(_dp, _ds) \
575 dsa_switch_for_each_port((_dp), (_ds)) \
576 if (!dsa_port_is_unused((_dp)))
578 #define dsa_switch_for_each_user_port(_dp, _ds) \
579 dsa_switch_for_each_port((_dp), (_ds)) \
580 if (dsa_port_is_user((_dp)))
582 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
583 dsa_switch_for_each_port((_dp), (_ds)) \
584 if (dsa_port_is_cpu((_dp)))
586 #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \
587 dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
588 if (dsa_port_is_cpu((_dp)))
590 static inline u32 dsa_user_ports(struct dsa_switch *ds)
595 dsa_switch_for_each_user_port(dp, ds)
596 mask |= BIT(dp->index);
601 static inline u32 dsa_cpu_ports(struct dsa_switch *ds)
603 struct dsa_port *cpu_dp;
606 dsa_switch_for_each_cpu_port(cpu_dp, ds)
607 mask |= BIT(cpu_dp->index);
612 /* Return the local port used to reach an arbitrary switch device */
613 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
615 struct dsa_switch_tree *dst = ds->dst;
618 list_for_each_entry(dl, &dst->rtable, list)
619 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
620 return dl->dp->index;
622 return ds->num_ports;
625 /* Return the local port used to reach an arbitrary switch port */
626 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
629 if (device == ds->index)
632 return dsa_routing_port(ds, device);
635 /* Return the local port used to reach the dedicated CPU port */
636 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
638 const struct dsa_port *dp = dsa_to_port(ds, port);
639 const struct dsa_port *cpu_dp = dp->cpu_dp;
644 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
647 /* Return true if this is the local port used to reach the CPU port */
648 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
650 if (dsa_is_unused_port(ds, port))
653 return port == dsa_upstream_port(ds, port);
656 /* Return true if this is a DSA port leading away from the CPU */
657 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
659 return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
662 /* Return the local port used to reach the CPU port */
663 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
667 dsa_switch_for_each_available_port(dp, ds) {
668 return dsa_upstream_port(ds, dp->index);
671 return ds->num_ports;
674 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
675 * that the routing port from @downstream_ds to @upstream_ds is also the port
676 * which @downstream_ds uses to reach its dedicated CPU.
678 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
679 struct dsa_switch *downstream_ds)
683 if (upstream_ds == downstream_ds)
686 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
688 return dsa_is_upstream_port(downstream_ds, routing_port);
691 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
693 const struct dsa_switch *ds = dp->ds;
695 if (ds->vlan_filtering_is_global)
696 return ds->vlan_filtering;
698 return dp->vlan_filtering;
701 static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
703 return dp->lag ? dp->lag->id : 0;
706 static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
708 return dp->lag ? dp->lag->dev : NULL;
711 static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
712 const struct dsa_lag *lag)
714 return dsa_port_lag_dev_get(dp) == lag->dev;
718 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
725 else if (dp->hsr_dev)
731 static inline struct net_device *
732 dsa_port_bridge_dev_get(const struct dsa_port *dp)
734 return dp->bridge ? dp->bridge->dev : NULL;
737 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
739 return dp->bridge ? dp->bridge->num : 0;
742 static inline bool dsa_port_bridge_same(const struct dsa_port *a,
743 const struct dsa_port *b)
745 struct net_device *br_a = dsa_port_bridge_dev_get(a);
746 struct net_device *br_b = dsa_port_bridge_dev_get(b);
748 /* Standalone ports are not in the same bridge with one another */
749 return (!br_a || !br_b) ? false : (br_a == br_b);
752 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
753 const struct net_device *dev)
755 return dsa_port_to_bridge_port(dp) == dev;
759 dsa_port_offloads_bridge_dev(struct dsa_port *dp,
760 const struct net_device *bridge_dev)
762 /* DSA ports connected to a bridge, and event was emitted
765 return dsa_port_bridge_dev_get(dp) == bridge_dev;
768 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
769 const struct dsa_bridge *bridge)
771 return dsa_port_bridge_dev_get(dp) == bridge->dev;
774 /* Returns true if any port of this tree offloads the given net_device */
775 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
776 const struct net_device *dev)
780 list_for_each_entry(dp, &dst->ports, list)
781 if (dsa_port_offloads_bridge_port(dp, dev))
787 /* Returns true if any port of this tree offloads the given bridge */
789 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
790 const struct net_device *bridge_dev)
794 list_for_each_entry(dp, &dst->ports, list)
795 if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
801 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
802 bool is_static, void *data);
803 struct dsa_switch_ops {
805 * Tagging protocol helpers called for the CPU ports and DSA links.
806 * @get_tag_protocol retrieves the initial tagging protocol and is
807 * mandatory. Switches which can operate using multiple tagging
808 * protocols should implement @change_tag_protocol and report in
809 * @get_tag_protocol the tagger in current use.
811 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
813 enum dsa_tag_protocol mprot);
814 int (*change_tag_protocol)(struct dsa_switch *ds,
815 enum dsa_tag_protocol proto);
817 * Method for switch drivers to connect to the tagging protocol driver
818 * in current use. The switch driver can provide handlers for certain
819 * types of packets for switch management.
821 int (*connect_tag_protocol)(struct dsa_switch *ds,
822 enum dsa_tag_protocol proto);
824 /* Optional switch-wide initialization and destruction methods */
825 int (*setup)(struct dsa_switch *ds);
826 void (*teardown)(struct dsa_switch *ds);
828 /* Per-port initialization and destruction methods. Mandatory if the
829 * driver registers devlink port regions, optional otherwise.
831 int (*port_setup)(struct dsa_switch *ds, int port);
832 void (*port_teardown)(struct dsa_switch *ds, int port);
834 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
837 * Access to the switch's PHY registers.
839 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
840 int (*phy_write)(struct dsa_switch *ds, int port,
841 int regnum, u16 val);
844 * Link state adjustment (called from libphy)
846 void (*adjust_link)(struct dsa_switch *ds, int port,
847 struct phy_device *phydev);
848 void (*fixed_link_update)(struct dsa_switch *ds, int port,
849 struct fixed_phy_status *st);
852 * PHYLINK integration
854 void (*phylink_get_caps)(struct dsa_switch *ds, int port,
855 struct phylink_config *config);
856 void (*phylink_validate)(struct dsa_switch *ds, int port,
857 unsigned long *supported,
858 struct phylink_link_state *state);
859 struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
861 phy_interface_t iface);
862 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
863 struct phylink_link_state *state);
864 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
866 const struct phylink_link_state *state);
867 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
868 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
870 phy_interface_t interface);
871 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
873 phy_interface_t interface,
874 struct phy_device *phydev,
875 int speed, int duplex,
876 bool tx_pause, bool rx_pause);
877 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
878 struct phylink_link_state *state);
880 * Port statistics counters.
882 void (*get_strings)(struct dsa_switch *ds, int port,
883 u32 stringset, uint8_t *data);
884 void (*get_ethtool_stats)(struct dsa_switch *ds,
885 int port, uint64_t *data);
886 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
887 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
888 int port, uint64_t *data);
889 void (*get_eth_phy_stats)(struct dsa_switch *ds, int port,
890 struct ethtool_eth_phy_stats *phy_stats);
891 void (*get_eth_mac_stats)(struct dsa_switch *ds, int port,
892 struct ethtool_eth_mac_stats *mac_stats);
893 void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
894 struct ethtool_eth_ctrl_stats *ctrl_stats);
895 void (*get_rmon_stats)(struct dsa_switch *ds, int port,
896 struct ethtool_rmon_stats *rmon_stats,
897 const struct ethtool_rmon_hist_range **ranges);
898 void (*get_stats64)(struct dsa_switch *ds, int port,
899 struct rtnl_link_stats64 *s);
900 void (*get_pause_stats)(struct dsa_switch *ds, int port,
901 struct ethtool_pause_stats *pause_stats);
902 void (*self_test)(struct dsa_switch *ds, int port,
903 struct ethtool_test *etest, u64 *data);
906 * ethtool Wake-on-LAN
908 void (*get_wol)(struct dsa_switch *ds, int port,
909 struct ethtool_wolinfo *w);
910 int (*set_wol)(struct dsa_switch *ds, int port,
911 struct ethtool_wolinfo *w);
914 * ethtool timestamp info
916 int (*get_ts_info)(struct dsa_switch *ds, int port,
917 struct ethtool_ts_info *ts);
922 int (*port_get_default_prio)(struct dsa_switch *ds, int port);
923 int (*port_set_default_prio)(struct dsa_switch *ds, int port,
925 int (*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
926 int (*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
928 int (*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
934 int (*suspend)(struct dsa_switch *ds);
935 int (*resume)(struct dsa_switch *ds);
938 * Port enable/disable
940 int (*port_enable)(struct dsa_switch *ds, int port,
941 struct phy_device *phy);
942 void (*port_disable)(struct dsa_switch *ds, int port);
945 * Port's MAC EEE settings
947 int (*set_mac_eee)(struct dsa_switch *ds, int port,
948 struct ethtool_eee *e);
949 int (*get_mac_eee)(struct dsa_switch *ds, int port,
950 struct ethtool_eee *e);
953 int (*get_eeprom_len)(struct dsa_switch *ds);
954 int (*get_eeprom)(struct dsa_switch *ds,
955 struct ethtool_eeprom *eeprom, u8 *data);
956 int (*set_eeprom)(struct dsa_switch *ds,
957 struct ethtool_eeprom *eeprom, u8 *data);
962 int (*get_regs_len)(struct dsa_switch *ds, int port);
963 void (*get_regs)(struct dsa_switch *ds, int port,
964 struct ethtool_regs *regs, void *p);
967 * Upper device tracking.
969 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
970 struct netdev_notifier_changeupper_info *info);
975 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
976 int (*port_bridge_join)(struct dsa_switch *ds, int port,
977 struct dsa_bridge bridge,
978 bool *tx_fwd_offload,
979 struct netlink_ext_ack *extack);
980 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
981 struct dsa_bridge bridge);
982 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
984 int (*port_mst_state_set)(struct dsa_switch *ds, int port,
985 const struct switchdev_mst_state *state);
986 void (*port_fast_age)(struct dsa_switch *ds, int port);
987 int (*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
988 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
989 struct switchdev_brport_flags flags,
990 struct netlink_ext_ack *extack);
991 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
992 struct switchdev_brport_flags flags,
993 struct netlink_ext_ack *extack);
994 void (*port_set_host_flood)(struct dsa_switch *ds, int port,
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);
1012 * Forwarding database
1014 int (*port_fdb_add)(struct dsa_switch *ds, int port,
1015 const unsigned char *addr, u16 vid,
1017 int (*port_fdb_del)(struct dsa_switch *ds, int port,
1018 const unsigned char *addr, u16 vid,
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,
1025 int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
1026 const unsigned char *addr, u16 vid,
1030 * Multicast database
1032 int (*port_mdb_add)(struct dsa_switch *ds, int port,
1033 const struct switchdev_obj_port_mdb *mdb,
1035 int (*port_mdb_del)(struct dsa_switch *ds, int port,
1036 const struct switchdev_obj_port_mdb *mdb,
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);
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);
1067 * Cross-chip operations
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,
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);
1087 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
1089 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
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);
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,
1114 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
1115 unsigned int sb_index, u16 pool_index,
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);
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.
1145 int (*port_change_mtu)(struct dsa_switch *ds, int port,
1147 int (*port_max_mtu)(struct dsa_switch *ds, int port);
1152 int (*port_lag_change)(struct dsa_switch *ds, int port);
1153 int (*port_lag_join)(struct dsa_switch *ds, int port,
1155 struct netdev_lag_upper_info *info);
1156 int (*port_lag_leave)(struct dsa_switch *ds, int port,
1157 struct dsa_lag lag);
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);
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);
1180 * tag_8021q operations
1182 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
1184 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1187 * DSA master tracking operations
1189 void (*master_state_change)(struct dsa_switch *ds,
1190 const struct net_device *master,
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)
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,
1212 u64 parent_resource_id,
1213 const struct devlink_resource_size_params *size_params);
1215 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
1217 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
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,
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,
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);
1234 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
1236 struct dsa_devlink_priv {
1237 struct dsa_switch *ds;
1240 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1242 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1248 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
1250 struct devlink *dl = port->devlink;
1251 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1256 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1261 struct dsa_switch_driver {
1262 struct list_head list;
1263 const struct dsa_switch_ops *ops;
1266 struct net_device *dsa_dev_to_net_device(struct device *dev);
1268 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
1269 const unsigned char *addr, u16 vid,
1271 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
1272 const struct switchdev_obj_port_mdb *mdb,
1275 /* Keep inline for faster access in hot path */
1276 static inline bool netdev_uses_dsa(const struct net_device *dev)
1278 #if IS_ENABLED(CONFIG_NET_DSA)
1279 return dev->dsa_ptr && dev->dsa_ptr->rcv;
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.
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).
1296 * - proto: the value of the real EtherType.
1298 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1299 __be16 *proto, int *offset)
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;
1306 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1310 #if IS_ENABLED(CONFIG_NET_DSA)
1311 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1313 int err = -EOPNOTSUPP;
1318 if (!dev->dsa_ptr->netdev_ops)
1324 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1327 const struct dsa_netdevice_ops *ops;
1330 err = __dsa_netdevice_ops_check(dev);
1334 ops = dev->dsa_ptr->netdev_ops;
1336 return ops->ndo_eth_ioctl(dev, ifr, cmd);
1339 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
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);
1355 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1359 static inline int dsa_switch_resume(struct dsa_switch *ds)
1363 #endif /* CONFIG_PM_SLEEP */
1365 #if IS_ENABLED(CONFIG_NET_DSA)
1366 bool dsa_slave_dev_check(const struct net_device *dev);
1368 static inline bool dsa_slave_dev_check(const struct net_device *dev)
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);
1377 struct dsa_tag_driver {
1378 const struct dsa_device_ops *ops;
1379 struct list_head list;
1380 struct module *owner;
1383 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1385 struct module *owner);
1386 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1387 unsigned int count);
1389 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1390 static int __init dsa_tag_driver_module_init(void) \
1392 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1396 module_init(dsa_tag_driver_module_init); \
1398 static void __exit dsa_tag_driver_module_exit(void) \
1400 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1402 module_exit(dsa_tag_driver_module_exit)
1405 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1407 * @__ops_array: Array of tag driver structures
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().
1413 #define module_dsa_tag_drivers(__ops_array) \
1414 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1416 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1418 /* Create a static structure we can build a linked list of dsa_tag
1421 #define DSA_TAG_DRIVER(__ops) \
1422 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1427 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1429 * @__ops: Single tag driver structures
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().
1435 #define module_dsa_tag_driver(__ops) \
1436 DSA_TAG_DRIVER(__ops); \
1438 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1439 &DSA_TAG_DRIVER_NAME(__ops) \
1441 module_dsa_tag_drivers(dsa_tag_driver_array)