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
57 enum dsa_tag_protocol {
58 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
59 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
60 DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
61 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
62 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
63 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
64 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
65 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
66 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
67 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
68 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
69 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
70 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
71 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
72 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
73 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE,
74 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE,
75 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE,
76 DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE,
77 DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE,
78 DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE,
79 DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
80 DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE,
81 DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE,
82 DSA_TAG_PROTO_RTL8_4 = DSA_TAG_PROTO_RTL8_4_VALUE,
83 DSA_TAG_PROTO_RTL8_4T = DSA_TAG_PROTO_RTL8_4T_VALUE,
88 struct dsa_device_ops {
89 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
90 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
91 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
93 int (*connect)(struct dsa_switch *ds);
94 void (*disconnect)(struct dsa_switch *ds);
95 unsigned int needed_headroom;
96 unsigned int needed_tailroom;
98 enum dsa_tag_protocol proto;
99 /* Some tagging protocols either mangle or shift the destination MAC
100 * address, in which case the DSA master would drop packets on ingress
101 * if what it understands out of the destination MAC address is not in
104 bool promisc_on_master;
107 /* This structure defines the control interfaces that are overlayed by the
108 * DSA layer on top of the DSA CPU/management net_device instance. This is
109 * used by the core net_device layer while calling various net_device_ops
112 struct dsa_netdevice_ops {
113 int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
117 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
118 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \
119 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
122 struct net_device *dev;
124 struct mutex fdb_lock;
125 struct list_head fdbs;
129 struct dsa_switch_tree {
130 struct list_head list;
132 /* List of switch ports */
133 struct list_head ports;
135 /* Notifier chain for switch-wide events */
136 struct raw_notifier_head nh;
138 /* Tree identifier */
141 /* Number of switches attached to this tree */
142 struct kref refcount;
144 /* Maps offloaded LAG netdevs to a zero-based linear ID for
145 * drivers that need it.
147 struct dsa_lag **lags;
149 /* Tagging protocol operations */
150 const struct dsa_device_ops *tag_ops;
152 /* Default tagging protocol preferred by the switches in this
155 enum dsa_tag_protocol default_proto;
157 /* Has this tree been applied to the hardware? */
161 * Configuration data for the platform device that owns
162 * this dsa switch tree instance.
164 struct dsa_platform_data *pd;
166 /* List of DSA links composing the routing table */
167 struct list_head rtable;
169 /* Length of "lags" array */
170 unsigned int lags_len;
172 /* Track the largest switch index within a tree */
173 unsigned int last_switch;
176 /* LAG IDs are one-based, the dst->lags array is zero-based */
177 #define dsa_lags_foreach_id(_id, _dst) \
178 for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \
179 if ((_dst)->lags[(_id) - 1])
181 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
182 list_for_each_entry((_dp), &(_dst)->ports, list) \
183 if (dsa_port_offloads_lag((_dp), (_lag)))
185 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
186 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
187 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
189 static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
192 /* DSA LAG IDs are one-based, dst->lags is zero-based */
193 return dst->lags[id - 1];
196 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
197 struct net_device *lag_dev)
201 dsa_lags_foreach_id(id, dst) {
202 struct dsa_lag *lag = dsa_lag_by_id(dst, id);
204 if (lag->dev == lag_dev)
211 /* TC matchall action types */
212 enum dsa_port_mall_action_type {
213 DSA_PORT_MALL_MIRROR,
214 DSA_PORT_MALL_POLICER,
217 /* TC mirroring entry */
218 struct dsa_mall_mirror_tc_entry {
223 /* TC port policer entry */
224 struct dsa_mall_policer_tc_entry {
226 u64 rate_bytes_per_sec;
229 /* TC matchall entry */
230 struct dsa_mall_tc_entry {
231 struct list_head list;
232 unsigned long cookie;
233 enum dsa_port_mall_action_type type;
235 struct dsa_mall_mirror_tc_entry mirror;
236 struct dsa_mall_policer_tc_entry policer;
241 struct net_device *dev;
248 /* A CPU port is physically connected to a master device.
249 * A user port exposed to userspace has a slave device.
252 struct net_device *master;
253 struct net_device *slave;
256 /* Copy of the tagging protocol operations, for quicker access
257 * in the data path. Valid only for the CPU ports.
259 const struct dsa_device_ops *tag_ops;
261 /* Copies for faster access in master receive hot path */
262 struct dsa_switch_tree *dst;
263 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
265 struct dsa_switch *ds;
270 DSA_PORT_TYPE_UNUSED = 0,
277 struct dsa_port *cpu_dp;
282 /* Warning: the following bit fields are not atomic, and updating them
283 * can only be done from code paths where concurrency is not possible
284 * (probe time or under rtnl_lock).
288 /* Managed by DSA on user ports and by drivers on CPU and DSA ports */
293 u8 devlink_port_setup:1;
295 /* Master state bits, valid only on CPU ports */
296 u8 master_admin_up:1;
301 struct device_node *dn;
302 unsigned int ageing_time;
304 struct dsa_bridge *bridge;
305 struct devlink_port devlink_port;
307 struct phylink_config pl_config;
309 struct net_device *hsr_dev;
311 struct list_head list;
314 * Original copy of the master netdev ethtool_ops
316 const struct ethtool_ops *orig_ethtool_ops;
319 * Original copy of the master netdev net_device_ops
321 const struct dsa_netdevice_ops *netdev_ops;
323 /* List of MAC addresses that must be forwarded on this port.
324 * These are only valid on CPU ports and DSA links.
326 struct mutex addr_lists_lock;
327 struct list_head fdbs;
328 struct list_head mdbs;
330 /* List of VLANs that CPU and DSA ports are members of. */
331 struct mutex vlans_lock;
332 struct list_head vlans;
335 /* TODO: ideally DSA ports would have a single dp->link_dp member,
336 * and no dst->rtable nor this struct dsa_link would be needed,
337 * but this would require some more complex tree walking,
338 * so keep it stupid at the moment and list them all.
342 struct dsa_port *link_dp;
343 struct list_head list;
353 enum dsa_db_type type;
356 const struct dsa_port *dp;
358 struct dsa_bridge bridge;
362 struct dsa_mac_addr {
363 unsigned char addr[ETH_ALEN];
366 struct list_head list;
373 struct list_head list;
380 * Parent switch tree, and switch index.
382 struct dsa_switch_tree *dst;
385 /* Warning: the following bit fields are not atomic, and updating them
386 * can only be done from code paths where concurrency is not possible
387 * (probe time or under rtnl_lock).
391 /* Disallow bridge core from requesting different VLAN awareness
392 * settings on ports if not hardware-supported
394 u32 vlan_filtering_is_global:1;
396 /* Keep VLAN filtering enabled on ports not offloading any upper */
397 u32 needs_standalone_vlan_filtering:1;
399 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
400 * that have vlan_filtering=0. All drivers should ideally set this (and
401 * then the option would get removed), but it is unknown whether this
402 * would break things or not.
404 u32 configure_vlan_while_not_filtering:1;
406 /* If the switch driver always programs the CPU port as egress tagged
407 * despite the VLAN configuration indicating otherwise, then setting
408 * @untag_bridge_pvid will force the DSA receive path to pop the
409 * bridge's default_pvid VLAN tagged frames to offer a consistent
410 * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge
413 u32 untag_bridge_pvid:1;
415 /* Let DSA manage the FDB entries towards the
416 * CPU, based on the software bridge database.
418 u32 assisted_learning_on_cpu_port:1;
420 /* In case vlan_filtering_is_global is set, the VLAN awareness state
421 * should be retrieved from here and not from the per-port settings.
423 u32 vlan_filtering:1;
425 /* For switches that only have the MRU configurable. To ensure the
426 * configured MTU is not exceeded, normalization of MRU on all bridged
427 * interfaces is needed.
429 u32 mtu_enforcement_ingress:1;
431 /* Drivers that isolate the FDBs of multiple bridges must set this
432 * to true to receive the bridge as an argument in .port_fdb_{add,del}
433 * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
438 /* Listener for switch fabric events */
439 struct notifier_block nb;
442 * Give the switch driver somewhere to hang its private data
450 * Configuration data for this switch.
452 struct dsa_chip_data *cd;
455 * The switch operations.
457 const struct dsa_switch_ops *ops;
460 * Slave mii_bus and devices for the individual ports.
463 struct mii_bus *slave_mii_bus;
465 /* Ageing Time limits in msecs */
466 unsigned int ageing_time_min;
467 unsigned int ageing_time_max;
469 /* Storage for drivers using tag_8021q */
470 struct dsa_8021q_context *tag_8021q_ctx;
472 /* devlink used to represent this switch device */
473 struct devlink *devlink;
475 /* Number of switch port queues */
476 unsigned int num_tx_queues;
478 /* Drivers that benefit from having an ID associated with each
479 * offloaded LAG should set this to the maximum number of
480 * supported IDs. DSA will then maintain a mapping of _at
481 * least_ these many IDs, accessible to drivers via
484 unsigned int num_lag_ids;
486 /* Drivers that support bridge forwarding offload or FDB isolation
487 * should set this to the maximum number of bridges spanning the same
488 * switch tree (or all trees, in the case of cross-tree bridging
489 * support) that can be offloaded.
491 unsigned int max_num_bridges;
493 unsigned int num_ports;
496 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
498 struct dsa_switch_tree *dst = ds->dst;
501 list_for_each_entry(dp, &dst->ports, list)
502 if (dp->ds == ds && dp->index == p)
508 static inline bool dsa_port_is_dsa(struct dsa_port *port)
510 return port->type == DSA_PORT_TYPE_DSA;
513 static inline bool dsa_port_is_cpu(struct dsa_port *port)
515 return port->type == DSA_PORT_TYPE_CPU;
518 static inline bool dsa_port_is_user(struct dsa_port *dp)
520 return dp->type == DSA_PORT_TYPE_USER;
523 static inline bool dsa_port_is_unused(struct dsa_port *dp)
525 return dp->type == DSA_PORT_TYPE_UNUSED;
528 static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
530 return dsa_port_is_cpu(dp) && dp->master_admin_up &&
534 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
536 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
539 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
541 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
544 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
546 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
549 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
551 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
554 #define dsa_tree_for_each_user_port(_dp, _dst) \
555 list_for_each_entry((_dp), &(_dst)->ports, list) \
556 if (dsa_port_is_user((_dp)))
558 #define dsa_switch_for_each_port(_dp, _ds) \
559 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
560 if ((_dp)->ds == (_ds))
562 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
563 list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
564 if ((_dp)->ds == (_ds))
566 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
567 list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
568 if ((_dp)->ds == (_ds))
570 #define dsa_switch_for_each_available_port(_dp, _ds) \
571 dsa_switch_for_each_port((_dp), (_ds)) \
572 if (!dsa_port_is_unused((_dp)))
574 #define dsa_switch_for_each_user_port(_dp, _ds) \
575 dsa_switch_for_each_port((_dp), (_ds)) \
576 if (dsa_port_is_user((_dp)))
578 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
579 dsa_switch_for_each_port((_dp), (_ds)) \
580 if (dsa_port_is_cpu((_dp)))
582 static inline u32 dsa_user_ports(struct dsa_switch *ds)
587 dsa_switch_for_each_user_port(dp, ds)
588 mask |= BIT(dp->index);
593 /* Return the local port used to reach an arbitrary switch device */
594 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
596 struct dsa_switch_tree *dst = ds->dst;
599 list_for_each_entry(dl, &dst->rtable, list)
600 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
601 return dl->dp->index;
603 return ds->num_ports;
606 /* Return the local port used to reach an arbitrary switch port */
607 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
610 if (device == ds->index)
613 return dsa_routing_port(ds, device);
616 /* Return the local port used to reach the dedicated CPU port */
617 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
619 const struct dsa_port *dp = dsa_to_port(ds, port);
620 const struct dsa_port *cpu_dp = dp->cpu_dp;
625 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
628 /* Return true if this is the local port used to reach the CPU port */
629 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
631 if (dsa_is_unused_port(ds, port))
634 return port == dsa_upstream_port(ds, port);
637 /* Return true if this is a DSA port leading away from the CPU */
638 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
640 return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
643 /* Return the local port used to reach the CPU port */
644 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
648 dsa_switch_for_each_available_port(dp, ds) {
649 return dsa_upstream_port(ds, dp->index);
652 return ds->num_ports;
655 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
656 * that the routing port from @downstream_ds to @upstream_ds is also the port
657 * which @downstream_ds uses to reach its dedicated CPU.
659 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
660 struct dsa_switch *downstream_ds)
664 if (upstream_ds == downstream_ds)
667 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
669 return dsa_is_upstream_port(downstream_ds, routing_port);
672 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
674 const struct dsa_switch *ds = dp->ds;
676 if (ds->vlan_filtering_is_global)
677 return ds->vlan_filtering;
679 return dp->vlan_filtering;
682 static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
684 return dp->lag ? dp->lag->id : 0;
687 static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
689 return dp->lag ? dp->lag->dev : NULL;
692 static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
693 const struct dsa_lag *lag)
695 return dsa_port_lag_dev_get(dp) == lag->dev;
699 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
706 else if (dp->hsr_dev)
712 static inline struct net_device *
713 dsa_port_bridge_dev_get(const struct dsa_port *dp)
715 return dp->bridge ? dp->bridge->dev : NULL;
718 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
720 return dp->bridge ? dp->bridge->num : 0;
723 static inline bool dsa_port_bridge_same(const struct dsa_port *a,
724 const struct dsa_port *b)
726 struct net_device *br_a = dsa_port_bridge_dev_get(a);
727 struct net_device *br_b = dsa_port_bridge_dev_get(b);
729 /* Standalone ports are not in the same bridge with one another */
730 return (!br_a || !br_b) ? false : (br_a == br_b);
733 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
734 const struct net_device *dev)
736 return dsa_port_to_bridge_port(dp) == dev;
740 dsa_port_offloads_bridge_dev(struct dsa_port *dp,
741 const struct net_device *bridge_dev)
743 /* DSA ports connected to a bridge, and event was emitted
746 return dsa_port_bridge_dev_get(dp) == bridge_dev;
749 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
750 const struct dsa_bridge *bridge)
752 return dsa_port_bridge_dev_get(dp) == bridge->dev;
755 /* Returns true if any port of this tree offloads the given net_device */
756 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
757 const struct net_device *dev)
761 list_for_each_entry(dp, &dst->ports, list)
762 if (dsa_port_offloads_bridge_port(dp, dev))
768 /* Returns true if any port of this tree offloads the given bridge */
770 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
771 const struct net_device *bridge_dev)
775 list_for_each_entry(dp, &dst->ports, list)
776 if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
782 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
783 bool is_static, void *data);
784 struct dsa_switch_ops {
786 * Tagging protocol helpers called for the CPU ports and DSA links.
787 * @get_tag_protocol retrieves the initial tagging protocol and is
788 * mandatory. Switches which can operate using multiple tagging
789 * protocols should implement @change_tag_protocol and report in
790 * @get_tag_protocol the tagger in current use.
792 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
794 enum dsa_tag_protocol mprot);
795 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
796 enum dsa_tag_protocol proto);
798 * Method for switch drivers to connect to the tagging protocol driver
799 * in current use. The switch driver can provide handlers for certain
800 * types of packets for switch management.
802 int (*connect_tag_protocol)(struct dsa_switch *ds,
803 enum dsa_tag_protocol proto);
805 /* Optional switch-wide initialization and destruction methods */
806 int (*setup)(struct dsa_switch *ds);
807 void (*teardown)(struct dsa_switch *ds);
809 /* Per-port initialization and destruction methods. Mandatory if the
810 * driver registers devlink port regions, optional otherwise.
812 int (*port_setup)(struct dsa_switch *ds, int port);
813 void (*port_teardown)(struct dsa_switch *ds, int port);
815 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
818 * Access to the switch's PHY registers.
820 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
821 int (*phy_write)(struct dsa_switch *ds, int port,
822 int regnum, u16 val);
825 * Link state adjustment (called from libphy)
827 void (*adjust_link)(struct dsa_switch *ds, int port,
828 struct phy_device *phydev);
829 void (*fixed_link_update)(struct dsa_switch *ds, int port,
830 struct fixed_phy_status *st);
833 * PHYLINK integration
835 void (*phylink_get_caps)(struct dsa_switch *ds, int port,
836 struct phylink_config *config);
837 void (*phylink_validate)(struct dsa_switch *ds, int port,
838 unsigned long *supported,
839 struct phylink_link_state *state);
840 struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
842 phy_interface_t iface);
843 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
844 struct phylink_link_state *state);
845 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
847 const struct phylink_link_state *state);
848 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
849 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
851 phy_interface_t interface);
852 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
854 phy_interface_t interface,
855 struct phy_device *phydev,
856 int speed, int duplex,
857 bool tx_pause, bool rx_pause);
858 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
859 struct phylink_link_state *state);
861 * Port statistics counters.
863 void (*get_strings)(struct dsa_switch *ds, int port,
864 u32 stringset, uint8_t *data);
865 void (*get_ethtool_stats)(struct dsa_switch *ds,
866 int port, uint64_t *data);
867 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
868 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
869 int port, uint64_t *data);
870 void (*get_eth_phy_stats)(struct dsa_switch *ds, int port,
871 struct ethtool_eth_phy_stats *phy_stats);
872 void (*get_eth_mac_stats)(struct dsa_switch *ds, int port,
873 struct ethtool_eth_mac_stats *mac_stats);
874 void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
875 struct ethtool_eth_ctrl_stats *ctrl_stats);
876 void (*get_stats64)(struct dsa_switch *ds, int port,
877 struct rtnl_link_stats64 *s);
878 void (*self_test)(struct dsa_switch *ds, int port,
879 struct ethtool_test *etest, u64 *data);
882 * ethtool Wake-on-LAN
884 void (*get_wol)(struct dsa_switch *ds, int port,
885 struct ethtool_wolinfo *w);
886 int (*set_wol)(struct dsa_switch *ds, int port,
887 struct ethtool_wolinfo *w);
890 * ethtool timestamp info
892 int (*get_ts_info)(struct dsa_switch *ds, int port,
893 struct ethtool_ts_info *ts);
898 int (*port_get_default_prio)(struct dsa_switch *ds, int port);
899 int (*port_set_default_prio)(struct dsa_switch *ds, int port,
901 int (*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
902 int (*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
904 int (*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
910 int (*suspend)(struct dsa_switch *ds);
911 int (*resume)(struct dsa_switch *ds);
914 * Port enable/disable
916 int (*port_enable)(struct dsa_switch *ds, int port,
917 struct phy_device *phy);
918 void (*port_disable)(struct dsa_switch *ds, int port);
921 * Port's MAC EEE settings
923 int (*set_mac_eee)(struct dsa_switch *ds, int port,
924 struct ethtool_eee *e);
925 int (*get_mac_eee)(struct dsa_switch *ds, int port,
926 struct ethtool_eee *e);
929 int (*get_eeprom_len)(struct dsa_switch *ds);
930 int (*get_eeprom)(struct dsa_switch *ds,
931 struct ethtool_eeprom *eeprom, u8 *data);
932 int (*set_eeprom)(struct dsa_switch *ds,
933 struct ethtool_eeprom *eeprom, u8 *data);
938 int (*get_regs_len)(struct dsa_switch *ds, int port);
939 void (*get_regs)(struct dsa_switch *ds, int port,
940 struct ethtool_regs *regs, void *p);
943 * Upper device tracking.
945 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
946 struct netdev_notifier_changeupper_info *info);
951 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
952 int (*port_bridge_join)(struct dsa_switch *ds, int port,
953 struct dsa_bridge bridge,
954 bool *tx_fwd_offload,
955 struct netlink_ext_ack *extack);
956 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
957 struct dsa_bridge bridge);
958 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
960 int (*port_mst_state_set)(struct dsa_switch *ds, int port,
961 const struct switchdev_mst_state *state);
962 void (*port_fast_age)(struct dsa_switch *ds, int port);
963 int (*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
964 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
965 struct switchdev_brport_flags flags,
966 struct netlink_ext_ack *extack);
967 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
968 struct switchdev_brport_flags flags,
969 struct netlink_ext_ack *extack);
974 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
976 struct netlink_ext_ack *extack);
977 int (*port_vlan_add)(struct dsa_switch *ds, int port,
978 const struct switchdev_obj_port_vlan *vlan,
979 struct netlink_ext_ack *extack);
980 int (*port_vlan_del)(struct dsa_switch *ds, int port,
981 const struct switchdev_obj_port_vlan *vlan);
982 int (*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge,
983 const struct switchdev_vlan_msti *msti);
986 * Forwarding database
988 int (*port_fdb_add)(struct dsa_switch *ds, int port,
989 const unsigned char *addr, u16 vid,
991 int (*port_fdb_del)(struct dsa_switch *ds, int port,
992 const unsigned char *addr, u16 vid,
994 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
995 dsa_fdb_dump_cb_t *cb, void *data);
996 int (*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
997 const unsigned char *addr, u16 vid,
999 int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
1000 const unsigned char *addr, u16 vid,
1004 * Multicast database
1006 int (*port_mdb_add)(struct dsa_switch *ds, int port,
1007 const struct switchdev_obj_port_mdb *mdb,
1009 int (*port_mdb_del)(struct dsa_switch *ds, int port,
1010 const struct switchdev_obj_port_mdb *mdb,
1015 int (*get_rxnfc)(struct dsa_switch *ds, int port,
1016 struct ethtool_rxnfc *nfc, u32 *rule_locs);
1017 int (*set_rxnfc)(struct dsa_switch *ds, int port,
1018 struct ethtool_rxnfc *nfc);
1023 int (*cls_flower_add)(struct dsa_switch *ds, int port,
1024 struct flow_cls_offload *cls, bool ingress);
1025 int (*cls_flower_del)(struct dsa_switch *ds, int port,
1026 struct flow_cls_offload *cls, bool ingress);
1027 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
1028 struct flow_cls_offload *cls, bool ingress);
1029 int (*port_mirror_add)(struct dsa_switch *ds, int port,
1030 struct dsa_mall_mirror_tc_entry *mirror,
1031 bool ingress, struct netlink_ext_ack *extack);
1032 void (*port_mirror_del)(struct dsa_switch *ds, int port,
1033 struct dsa_mall_mirror_tc_entry *mirror);
1034 int (*port_policer_add)(struct dsa_switch *ds, int port,
1035 struct dsa_mall_policer_tc_entry *policer);
1036 void (*port_policer_del)(struct dsa_switch *ds, int port);
1037 int (*port_setup_tc)(struct dsa_switch *ds, int port,
1038 enum tc_setup_type type, void *type_data);
1041 * Cross-chip operations
1043 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
1044 int sw_index, int port,
1045 struct dsa_bridge bridge,
1046 struct netlink_ext_ack *extack);
1047 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
1048 int sw_index, int port,
1049 struct dsa_bridge bridge);
1050 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
1052 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
1053 int port, struct dsa_lag lag,
1054 struct netdev_lag_upper_info *info);
1055 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
1056 int port, struct dsa_lag lag);
1061 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
1063 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
1065 void (*port_txtstamp)(struct dsa_switch *ds, int port,
1066 struct sk_buff *skb);
1067 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
1068 struct sk_buff *skb, unsigned int type);
1070 /* Devlink parameters, etc */
1071 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
1072 struct devlink_param_gset_ctx *ctx);
1073 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
1074 struct devlink_param_gset_ctx *ctx);
1075 int (*devlink_info_get)(struct dsa_switch *ds,
1076 struct devlink_info_req *req,
1077 struct netlink_ext_ack *extack);
1078 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
1079 unsigned int sb_index, u16 pool_index,
1080 struct devlink_sb_pool_info *pool_info);
1081 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
1082 u16 pool_index, u32 size,
1083 enum devlink_sb_threshold_type threshold_type,
1084 struct netlink_ext_ack *extack);
1085 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
1086 unsigned int sb_index, u16 pool_index,
1088 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
1089 unsigned int sb_index, u16 pool_index,
1091 struct netlink_ext_ack *extack);
1092 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
1093 unsigned int sb_index, u16 tc_index,
1094 enum devlink_sb_pool_type pool_type,
1095 u16 *p_pool_index, u32 *p_threshold);
1096 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
1097 unsigned int sb_index, u16 tc_index,
1098 enum devlink_sb_pool_type pool_type,
1099 u16 pool_index, u32 threshold,
1100 struct netlink_ext_ack *extack);
1101 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
1102 unsigned int sb_index);
1103 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
1104 unsigned int sb_index);
1105 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
1106 unsigned int sb_index, u16 pool_index,
1107 u32 *p_cur, u32 *p_max);
1108 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
1109 unsigned int sb_index, u16 tc_index,
1110 enum devlink_sb_pool_type pool_type,
1111 u32 *p_cur, u32 *p_max);
1114 * MTU change functionality. Switches can also adjust their MRU through
1115 * this method. By MTU, one understands the SDU (L2 payload) length.
1116 * If the switch needs to account for the DSA tag on the CPU port, this
1117 * method needs to do so privately.
1119 int (*port_change_mtu)(struct dsa_switch *ds, int port,
1121 int (*port_max_mtu)(struct dsa_switch *ds, int port);
1126 int (*port_lag_change)(struct dsa_switch *ds, int port);
1127 int (*port_lag_join)(struct dsa_switch *ds, int port,
1129 struct netdev_lag_upper_info *info);
1130 int (*port_lag_leave)(struct dsa_switch *ds, int port,
1131 struct dsa_lag lag);
1136 int (*port_hsr_join)(struct dsa_switch *ds, int port,
1137 struct net_device *hsr);
1138 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
1139 struct net_device *hsr);
1144 int (*port_mrp_add)(struct dsa_switch *ds, int port,
1145 const struct switchdev_obj_mrp *mrp);
1146 int (*port_mrp_del)(struct dsa_switch *ds, int port,
1147 const struct switchdev_obj_mrp *mrp);
1148 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1149 const struct switchdev_obj_ring_role_mrp *mrp);
1150 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1151 const struct switchdev_obj_ring_role_mrp *mrp);
1154 * tag_8021q operations
1156 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
1158 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1161 * DSA master tracking operations
1163 void (*master_state_change)(struct dsa_switch *ds,
1164 const struct net_device *master,
1168 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
1169 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
1170 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
1172 int dsa_devlink_param_get(struct devlink *dl, u32 id,
1173 struct devlink_param_gset_ctx *ctx);
1174 int dsa_devlink_param_set(struct devlink *dl, u32 id,
1175 struct devlink_param_gset_ctx *ctx);
1176 int dsa_devlink_params_register(struct dsa_switch *ds,
1177 const struct devlink_param *params,
1178 size_t params_count);
1179 void dsa_devlink_params_unregister(struct dsa_switch *ds,
1180 const struct devlink_param *params,
1181 size_t params_count);
1182 int dsa_devlink_resource_register(struct dsa_switch *ds,
1183 const char *resource_name,
1186 u64 parent_resource_id,
1187 const struct devlink_resource_size_params *size_params);
1189 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
1191 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
1193 devlink_resource_occ_get_t *occ_get,
1194 void *occ_get_priv);
1195 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
1197 struct devlink_region *
1198 dsa_devlink_region_create(struct dsa_switch *ds,
1199 const struct devlink_region_ops *ops,
1200 u32 region_max_snapshots, u64 region_size);
1201 struct devlink_region *
1202 dsa_devlink_port_region_create(struct dsa_switch *ds,
1204 const struct devlink_port_region_ops *ops,
1205 u32 region_max_snapshots, u64 region_size);
1206 void dsa_devlink_region_destroy(struct devlink_region *region);
1208 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
1210 struct dsa_devlink_priv {
1211 struct dsa_switch *ds;
1214 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1216 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1222 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
1224 struct devlink *dl = port->devlink;
1225 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1230 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1235 struct dsa_switch_driver {
1236 struct list_head list;
1237 const struct dsa_switch_ops *ops;
1240 struct net_device *dsa_dev_to_net_device(struct device *dev);
1242 typedef int dsa_fdb_walk_cb_t(struct dsa_switch *ds, int port,
1243 const unsigned char *addr, u16 vid,
1246 int dsa_port_walk_fdbs(struct dsa_switch *ds, int port, dsa_fdb_walk_cb_t cb);
1247 int dsa_port_walk_mdbs(struct dsa_switch *ds, int port, dsa_fdb_walk_cb_t cb);
1248 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
1249 const unsigned char *addr, u16 vid,
1251 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
1252 const struct switchdev_obj_port_mdb *mdb,
1255 /* Keep inline for faster access in hot path */
1256 static inline bool netdev_uses_dsa(const struct net_device *dev)
1258 #if IS_ENABLED(CONFIG_NET_DSA)
1259 return dev->dsa_ptr && dev->dsa_ptr->rcv;
1264 /* All DSA tags that push the EtherType to the right (basically all except tail
1265 * tags, which don't break dissection) can be treated the same from the
1266 * perspective of the flow dissector.
1268 * We need to return:
1269 * - offset: the (B - A) difference between:
1270 * A. the position of the real EtherType and
1271 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1272 * after the normal EtherType was supposed to be)
1273 * The offset in bytes is exactly equal to the tagger overhead (and half of
1274 * that, in __be16 shorts).
1276 * - proto: the value of the real EtherType.
1278 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1279 __be16 *proto, int *offset)
1281 #if IS_ENABLED(CONFIG_NET_DSA)
1282 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1283 int tag_len = ops->needed_headroom;
1286 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1290 #if IS_ENABLED(CONFIG_NET_DSA)
1291 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1293 int err = -EOPNOTSUPP;
1298 if (!dev->dsa_ptr->netdev_ops)
1304 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1307 const struct dsa_netdevice_ops *ops;
1310 err = __dsa_netdevice_ops_check(dev);
1314 ops = dev->dsa_ptr->netdev_ops;
1316 return ops->ndo_eth_ioctl(dev, ifr, cmd);
1319 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1326 void dsa_unregister_switch(struct dsa_switch *ds);
1327 int dsa_register_switch(struct dsa_switch *ds);
1328 void dsa_switch_shutdown(struct dsa_switch *ds);
1329 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1330 void dsa_flush_workqueue(void);
1331 #ifdef CONFIG_PM_SLEEP
1332 int dsa_switch_suspend(struct dsa_switch *ds);
1333 int dsa_switch_resume(struct dsa_switch *ds);
1335 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1339 static inline int dsa_switch_resume(struct dsa_switch *ds)
1343 #endif /* CONFIG_PM_SLEEP */
1345 #if IS_ENABLED(CONFIG_NET_DSA)
1346 bool dsa_slave_dev_check(const struct net_device *dev);
1348 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1354 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1355 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1357 struct dsa_tag_driver {
1358 const struct dsa_device_ops *ops;
1359 struct list_head list;
1360 struct module *owner;
1363 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1365 struct module *owner);
1366 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1367 unsigned int count);
1369 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1370 static int __init dsa_tag_driver_module_init(void) \
1372 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1376 module_init(dsa_tag_driver_module_init); \
1378 static void __exit dsa_tag_driver_module_exit(void) \
1380 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1382 module_exit(dsa_tag_driver_module_exit)
1385 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1387 * @__ops_array: Array of tag driver structures
1389 * Helper macro for DSA tag drivers which do not do anything special
1390 * in module init/exit. Each module may only use this macro once, and
1391 * calling it replaces module_init() and module_exit().
1393 #define module_dsa_tag_drivers(__ops_array) \
1394 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1396 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1398 /* Create a static structure we can build a linked list of dsa_tag
1401 #define DSA_TAG_DRIVER(__ops) \
1402 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1407 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1409 * @__ops: Single tag driver structures
1411 * Helper macro for DSA tag drivers which do not do anything special
1412 * in module init/exit. Each module may only use this macro once, and
1413 * calling it replaces module_init() and module_exit().
1415 #define module_dsa_tag_driver(__ops) \
1416 DSA_TAG_DRIVER(__ops); \
1418 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1419 &DSA_TAG_DRIVER_NAME(__ops) \
1421 module_dsa_tag_drivers(dsa_tag_driver_array)