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
56 enum dsa_tag_protocol {
57 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
58 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
59 DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
60 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
61 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
62 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
63 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
64 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
65 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
66 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
67 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
68 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
69 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
70 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
71 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
72 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE,
73 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE,
74 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE,
75 DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE,
76 DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE,
77 DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE,
78 DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
79 DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE,
80 DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE,
81 DSA_TAG_PROTO_RTL8_4 = DSA_TAG_PROTO_RTL8_4_VALUE,
86 struct dsa_device_ops {
87 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
88 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
89 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
91 int (*connect)(struct dsa_switch *ds);
92 void (*disconnect)(struct dsa_switch *ds);
93 unsigned int needed_headroom;
94 unsigned int needed_tailroom;
96 enum dsa_tag_protocol proto;
97 /* Some tagging protocols either mangle or shift the destination MAC
98 * address, in which case the DSA master would drop packets on ingress
99 * if what it understands out of the destination MAC address is not in
102 bool promisc_on_master;
105 /* This structure defines the control interfaces that are overlayed by the
106 * DSA layer on top of the DSA CPU/management net_device instance. This is
107 * used by the core net_device layer while calling various net_device_ops
110 struct dsa_netdevice_ops {
111 int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
115 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
116 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \
117 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
120 struct net_device *dev;
122 struct mutex fdb_lock;
123 struct list_head fdbs;
127 struct dsa_switch_tree {
128 struct list_head list;
130 /* List of switch ports */
131 struct list_head ports;
133 /* Notifier chain for switch-wide events */
134 struct raw_notifier_head nh;
136 /* Tree identifier */
139 /* Number of switches attached to this tree */
140 struct kref refcount;
142 /* Maps offloaded LAG netdevs to a zero-based linear ID for
143 * drivers that need it.
145 struct dsa_lag **lags;
147 /* Tagging protocol operations */
148 const struct dsa_device_ops *tag_ops;
150 /* Default tagging protocol preferred by the switches in this
153 enum dsa_tag_protocol default_proto;
155 /* Has this tree been applied to the hardware? */
159 * Configuration data for the platform device that owns
160 * this dsa switch tree instance.
162 struct dsa_platform_data *pd;
164 /* List of DSA links composing the routing table */
165 struct list_head rtable;
167 /* Length of "lags" array */
168 unsigned int lags_len;
170 /* Track the largest switch index within a tree */
171 unsigned int last_switch;
174 /* LAG IDs are one-based, the dst->lags array is zero-based */
175 #define dsa_lags_foreach_id(_id, _dst) \
176 for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \
177 if ((_dst)->lags[(_id) - 1])
179 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
180 list_for_each_entry((_dp), &(_dst)->ports, list) \
181 if (dsa_port_offloads_lag((_dp), (_lag)))
183 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
184 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
185 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
187 static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
190 /* DSA LAG IDs are one-based, dst->lags is zero-based */
191 return dst->lags[id - 1];
194 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
195 struct net_device *lag_dev)
199 dsa_lags_foreach_id(id, dst) {
200 struct dsa_lag *lag = dsa_lag_by_id(dst, id);
202 if (lag->dev == lag_dev)
209 /* TC matchall action types */
210 enum dsa_port_mall_action_type {
211 DSA_PORT_MALL_MIRROR,
212 DSA_PORT_MALL_POLICER,
215 /* TC mirroring entry */
216 struct dsa_mall_mirror_tc_entry {
221 /* TC port policer entry */
222 struct dsa_mall_policer_tc_entry {
224 u64 rate_bytes_per_sec;
227 /* TC matchall entry */
228 struct dsa_mall_tc_entry {
229 struct list_head list;
230 unsigned long cookie;
231 enum dsa_port_mall_action_type type;
233 struct dsa_mall_mirror_tc_entry mirror;
234 struct dsa_mall_policer_tc_entry policer;
239 struct net_device *dev;
246 /* A CPU port is physically connected to a master device.
247 * A user port exposed to userspace has a slave device.
250 struct net_device *master;
251 struct net_device *slave;
254 /* Copy of the tagging protocol operations, for quicker access
255 * in the data path. Valid only for the CPU ports.
257 const struct dsa_device_ops *tag_ops;
259 /* Copies for faster access in master receive hot path */
260 struct dsa_switch_tree *dst;
261 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
263 struct dsa_switch *ds;
268 DSA_PORT_TYPE_UNUSED = 0,
275 struct dsa_port *cpu_dp;
280 /* Warning: the following bit fields are not atomic, and updating them
281 * can only be done from code paths where concurrency is not possible
282 * (probe time or under rtnl_lock).
286 /* Managed by DSA on user ports and by drivers on CPU and DSA ports */
291 u8 devlink_port_setup:1;
293 /* Master state bits, valid only on CPU ports */
294 u8 master_admin_up:1;
299 struct device_node *dn;
300 unsigned int ageing_time;
302 struct dsa_bridge *bridge;
303 struct devlink_port devlink_port;
305 struct phylink_config pl_config;
307 struct net_device *hsr_dev;
309 struct list_head list;
312 * Original copy of the master netdev ethtool_ops
314 const struct ethtool_ops *orig_ethtool_ops;
317 * Original copy of the master netdev net_device_ops
319 const struct dsa_netdevice_ops *netdev_ops;
321 /* List of MAC addresses that must be forwarded on this port.
322 * These are only valid on CPU ports and DSA links.
324 struct mutex addr_lists_lock;
325 struct list_head fdbs;
326 struct list_head mdbs;
328 /* List of VLANs that CPU and DSA ports are members of. */
329 struct mutex vlans_lock;
330 struct list_head vlans;
333 /* TODO: ideally DSA ports would have a single dp->link_dp member,
334 * and no dst->rtable nor this struct dsa_link would be needed,
335 * but this would require some more complex tree walking,
336 * so keep it stupid at the moment and list them all.
340 struct dsa_port *link_dp;
341 struct list_head list;
351 enum dsa_db_type type;
354 const struct dsa_port *dp;
356 struct dsa_bridge bridge;
360 struct dsa_mac_addr {
361 unsigned char addr[ETH_ALEN];
364 struct list_head list;
371 struct list_head list;
378 * Parent switch tree, and switch index.
380 struct dsa_switch_tree *dst;
383 /* Warning: the following bit fields are not atomic, and updating them
384 * can only be done from code paths where concurrency is not possible
385 * (probe time or under rtnl_lock).
389 /* Disallow bridge core from requesting different VLAN awareness
390 * settings on ports if not hardware-supported
392 u32 vlan_filtering_is_global:1;
394 /* Keep VLAN filtering enabled on ports not offloading any upper */
395 u32 needs_standalone_vlan_filtering:1;
397 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
398 * that have vlan_filtering=0. All drivers should ideally set this (and
399 * then the option would get removed), but it is unknown whether this
400 * would break things or not.
402 u32 configure_vlan_while_not_filtering:1;
404 /* If the switch driver always programs the CPU port as egress tagged
405 * despite the VLAN configuration indicating otherwise, then setting
406 * @untag_bridge_pvid will force the DSA receive path to pop the
407 * bridge's default_pvid VLAN tagged frames to offer a consistent
408 * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge
411 u32 untag_bridge_pvid:1;
413 /* Let DSA manage the FDB entries towards the
414 * CPU, based on the software bridge database.
416 u32 assisted_learning_on_cpu_port:1;
418 /* In case vlan_filtering_is_global is set, the VLAN awareness state
419 * should be retrieved from here and not from the per-port settings.
421 u32 vlan_filtering:1;
423 /* For switches that only have the MRU configurable. To ensure the
424 * configured MTU is not exceeded, normalization of MRU on all bridged
425 * interfaces is needed.
427 u32 mtu_enforcement_ingress:1;
429 /* Drivers that isolate the FDBs of multiple bridges must set this
430 * to true to receive the bridge as an argument in .port_fdb_{add,del}
431 * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
436 /* Listener for switch fabric events */
437 struct notifier_block nb;
440 * Give the switch driver somewhere to hang its private data
448 * Configuration data for this switch.
450 struct dsa_chip_data *cd;
453 * The switch operations.
455 const struct dsa_switch_ops *ops;
458 * Slave mii_bus and devices for the individual ports.
461 struct mii_bus *slave_mii_bus;
463 /* Ageing Time limits in msecs */
464 unsigned int ageing_time_min;
465 unsigned int ageing_time_max;
467 /* Storage for drivers using tag_8021q */
468 struct dsa_8021q_context *tag_8021q_ctx;
470 /* devlink used to represent this switch device */
471 struct devlink *devlink;
473 /* Number of switch port queues */
474 unsigned int num_tx_queues;
476 /* Drivers that benefit from having an ID associated with each
477 * offloaded LAG should set this to the maximum number of
478 * supported IDs. DSA will then maintain a mapping of _at
479 * least_ these many IDs, accessible to drivers via
482 unsigned int num_lag_ids;
484 /* Drivers that support bridge forwarding offload or FDB isolation
485 * should set this to the maximum number of bridges spanning the same
486 * switch tree (or all trees, in the case of cross-tree bridging
487 * support) that can be offloaded.
489 unsigned int max_num_bridges;
491 unsigned int num_ports;
494 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
496 struct dsa_switch_tree *dst = ds->dst;
499 list_for_each_entry(dp, &dst->ports, list)
500 if (dp->ds == ds && dp->index == p)
506 static inline bool dsa_port_is_dsa(struct dsa_port *port)
508 return port->type == DSA_PORT_TYPE_DSA;
511 static inline bool dsa_port_is_cpu(struct dsa_port *port)
513 return port->type == DSA_PORT_TYPE_CPU;
516 static inline bool dsa_port_is_user(struct dsa_port *dp)
518 return dp->type == DSA_PORT_TYPE_USER;
521 static inline bool dsa_port_is_unused(struct dsa_port *dp)
523 return dp->type == DSA_PORT_TYPE_UNUSED;
526 static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
528 return dsa_port_is_cpu(dp) && dp->master_admin_up &&
532 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
534 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
537 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
539 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
542 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
544 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
547 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
549 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
552 #define dsa_tree_for_each_user_port(_dp, _dst) \
553 list_for_each_entry((_dp), &(_dst)->ports, list) \
554 if (dsa_port_is_user((_dp)))
556 #define dsa_switch_for_each_port(_dp, _ds) \
557 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
558 if ((_dp)->ds == (_ds))
560 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
561 list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
562 if ((_dp)->ds == (_ds))
564 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
565 list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
566 if ((_dp)->ds == (_ds))
568 #define dsa_switch_for_each_available_port(_dp, _ds) \
569 dsa_switch_for_each_port((_dp), (_ds)) \
570 if (!dsa_port_is_unused((_dp)))
572 #define dsa_switch_for_each_user_port(_dp, _ds) \
573 dsa_switch_for_each_port((_dp), (_ds)) \
574 if (dsa_port_is_user((_dp)))
576 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
577 dsa_switch_for_each_port((_dp), (_ds)) \
578 if (dsa_port_is_cpu((_dp)))
580 static inline u32 dsa_user_ports(struct dsa_switch *ds)
585 dsa_switch_for_each_user_port(dp, ds)
586 mask |= BIT(dp->index);
591 /* Return the local port used to reach an arbitrary switch device */
592 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
594 struct dsa_switch_tree *dst = ds->dst;
597 list_for_each_entry(dl, &dst->rtable, list)
598 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
599 return dl->dp->index;
601 return ds->num_ports;
604 /* Return the local port used to reach an arbitrary switch port */
605 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
608 if (device == ds->index)
611 return dsa_routing_port(ds, device);
614 /* Return the local port used to reach the dedicated CPU port */
615 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
617 const struct dsa_port *dp = dsa_to_port(ds, port);
618 const struct dsa_port *cpu_dp = dp->cpu_dp;
623 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
626 /* Return true if this is the local port used to reach the CPU port */
627 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
629 if (dsa_is_unused_port(ds, port))
632 return port == dsa_upstream_port(ds, port);
635 /* Return true if this is a DSA port leading away from the CPU */
636 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
638 return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
641 /* Return the local port used to reach the CPU port */
642 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
646 dsa_switch_for_each_available_port(dp, ds) {
647 return dsa_upstream_port(ds, dp->index);
650 return ds->num_ports;
653 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
654 * that the routing port from @downstream_ds to @upstream_ds is also the port
655 * which @downstream_ds uses to reach its dedicated CPU.
657 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
658 struct dsa_switch *downstream_ds)
662 if (upstream_ds == downstream_ds)
665 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
667 return dsa_is_upstream_port(downstream_ds, routing_port);
670 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
672 const struct dsa_switch *ds = dp->ds;
674 if (ds->vlan_filtering_is_global)
675 return ds->vlan_filtering;
677 return dp->vlan_filtering;
680 static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
682 return dp->lag ? dp->lag->id : 0;
685 static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
687 return dp->lag ? dp->lag->dev : NULL;
690 static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
691 const struct dsa_lag *lag)
693 return dsa_port_lag_dev_get(dp) == lag->dev;
697 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
704 else if (dp->hsr_dev)
710 static inline struct net_device *
711 dsa_port_bridge_dev_get(const struct dsa_port *dp)
713 return dp->bridge ? dp->bridge->dev : NULL;
716 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
718 return dp->bridge ? dp->bridge->num : 0;
721 static inline bool dsa_port_bridge_same(const struct dsa_port *a,
722 const struct dsa_port *b)
724 struct net_device *br_a = dsa_port_bridge_dev_get(a);
725 struct net_device *br_b = dsa_port_bridge_dev_get(b);
727 /* Standalone ports are not in the same bridge with one another */
728 return (!br_a || !br_b) ? false : (br_a == br_b);
731 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
732 const struct net_device *dev)
734 return dsa_port_to_bridge_port(dp) == dev;
738 dsa_port_offloads_bridge_dev(struct dsa_port *dp,
739 const struct net_device *bridge_dev)
741 /* DSA ports connected to a bridge, and event was emitted
744 return dsa_port_bridge_dev_get(dp) == bridge_dev;
747 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
748 const struct dsa_bridge *bridge)
750 return dsa_port_bridge_dev_get(dp) == bridge->dev;
753 /* Returns true if any port of this tree offloads the given net_device */
754 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
755 const struct net_device *dev)
759 list_for_each_entry(dp, &dst->ports, list)
760 if (dsa_port_offloads_bridge_port(dp, dev))
766 /* Returns true if any port of this tree offloads the given bridge */
768 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
769 const struct net_device *bridge_dev)
773 list_for_each_entry(dp, &dst->ports, list)
774 if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
780 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
781 bool is_static, void *data);
782 struct dsa_switch_ops {
784 * Tagging protocol helpers called for the CPU ports and DSA links.
785 * @get_tag_protocol retrieves the initial tagging protocol and is
786 * mandatory. Switches which can operate using multiple tagging
787 * protocols should implement @change_tag_protocol and report in
788 * @get_tag_protocol the tagger in current use.
790 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
792 enum dsa_tag_protocol mprot);
793 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
794 enum dsa_tag_protocol proto);
796 * Method for switch drivers to connect to the tagging protocol driver
797 * in current use. The switch driver can provide handlers for certain
798 * types of packets for switch management.
800 int (*connect_tag_protocol)(struct dsa_switch *ds,
801 enum dsa_tag_protocol proto);
803 /* Optional switch-wide initialization and destruction methods */
804 int (*setup)(struct dsa_switch *ds);
805 void (*teardown)(struct dsa_switch *ds);
807 /* Per-port initialization and destruction methods. Mandatory if the
808 * driver registers devlink port regions, optional otherwise.
810 int (*port_setup)(struct dsa_switch *ds, int port);
811 void (*port_teardown)(struct dsa_switch *ds, int port);
813 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
816 * Access to the switch's PHY registers.
818 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
819 int (*phy_write)(struct dsa_switch *ds, int port,
820 int regnum, u16 val);
823 * Link state adjustment (called from libphy)
825 void (*adjust_link)(struct dsa_switch *ds, int port,
826 struct phy_device *phydev);
827 void (*fixed_link_update)(struct dsa_switch *ds, int port,
828 struct fixed_phy_status *st);
831 * PHYLINK integration
833 void (*phylink_get_caps)(struct dsa_switch *ds, int port,
834 struct phylink_config *config);
835 void (*phylink_validate)(struct dsa_switch *ds, int port,
836 unsigned long *supported,
837 struct phylink_link_state *state);
838 struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
840 phy_interface_t iface);
841 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
842 struct phylink_link_state *state);
843 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
845 const struct phylink_link_state *state);
846 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
847 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
849 phy_interface_t interface);
850 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
852 phy_interface_t interface,
853 struct phy_device *phydev,
854 int speed, int duplex,
855 bool tx_pause, bool rx_pause);
856 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
857 struct phylink_link_state *state);
859 * Port statistics counters.
861 void (*get_strings)(struct dsa_switch *ds, int port,
862 u32 stringset, uint8_t *data);
863 void (*get_ethtool_stats)(struct dsa_switch *ds,
864 int port, uint64_t *data);
865 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
866 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
867 int port, uint64_t *data);
868 void (*get_eth_phy_stats)(struct dsa_switch *ds, int port,
869 struct ethtool_eth_phy_stats *phy_stats);
870 void (*get_eth_mac_stats)(struct dsa_switch *ds, int port,
871 struct ethtool_eth_mac_stats *mac_stats);
872 void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
873 struct ethtool_eth_ctrl_stats *ctrl_stats);
874 void (*get_stats64)(struct dsa_switch *ds, int port,
875 struct rtnl_link_stats64 *s);
876 void (*self_test)(struct dsa_switch *ds, int port,
877 struct ethtool_test *etest, u64 *data);
880 * ethtool Wake-on-LAN
882 void (*get_wol)(struct dsa_switch *ds, int port,
883 struct ethtool_wolinfo *w);
884 int (*set_wol)(struct dsa_switch *ds, int port,
885 struct ethtool_wolinfo *w);
888 * ethtool timestamp info
890 int (*get_ts_info)(struct dsa_switch *ds, int port,
891 struct ethtool_ts_info *ts);
896 int (*suspend)(struct dsa_switch *ds);
897 int (*resume)(struct dsa_switch *ds);
900 * Port enable/disable
902 int (*port_enable)(struct dsa_switch *ds, int port,
903 struct phy_device *phy);
904 void (*port_disable)(struct dsa_switch *ds, int port);
907 * Port's MAC EEE settings
909 int (*set_mac_eee)(struct dsa_switch *ds, int port,
910 struct ethtool_eee *e);
911 int (*get_mac_eee)(struct dsa_switch *ds, int port,
912 struct ethtool_eee *e);
915 int (*get_eeprom_len)(struct dsa_switch *ds);
916 int (*get_eeprom)(struct dsa_switch *ds,
917 struct ethtool_eeprom *eeprom, u8 *data);
918 int (*set_eeprom)(struct dsa_switch *ds,
919 struct ethtool_eeprom *eeprom, u8 *data);
924 int (*get_regs_len)(struct dsa_switch *ds, int port);
925 void (*get_regs)(struct dsa_switch *ds, int port,
926 struct ethtool_regs *regs, void *p);
929 * Upper device tracking.
931 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
932 struct netdev_notifier_changeupper_info *info);
937 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
938 int (*port_bridge_join)(struct dsa_switch *ds, int port,
939 struct dsa_bridge bridge,
940 bool *tx_fwd_offload);
941 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
942 struct dsa_bridge bridge);
943 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
945 void (*port_fast_age)(struct dsa_switch *ds, int port);
946 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
947 struct switchdev_brport_flags flags,
948 struct netlink_ext_ack *extack);
949 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
950 struct switchdev_brport_flags flags,
951 struct netlink_ext_ack *extack);
956 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
958 struct netlink_ext_ack *extack);
959 int (*port_vlan_add)(struct dsa_switch *ds, int port,
960 const struct switchdev_obj_port_vlan *vlan,
961 struct netlink_ext_ack *extack);
962 int (*port_vlan_del)(struct dsa_switch *ds, int port,
963 const struct switchdev_obj_port_vlan *vlan);
965 * Forwarding database
967 int (*port_fdb_add)(struct dsa_switch *ds, int port,
968 const unsigned char *addr, u16 vid,
970 int (*port_fdb_del)(struct dsa_switch *ds, int port,
971 const unsigned char *addr, u16 vid,
973 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
974 dsa_fdb_dump_cb_t *cb, void *data);
975 int (*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
976 const unsigned char *addr, u16 vid,
978 int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
979 const unsigned char *addr, u16 vid,
985 int (*port_mdb_add)(struct dsa_switch *ds, int port,
986 const struct switchdev_obj_port_mdb *mdb,
988 int (*port_mdb_del)(struct dsa_switch *ds, int port,
989 const struct switchdev_obj_port_mdb *mdb,
994 int (*get_rxnfc)(struct dsa_switch *ds, int port,
995 struct ethtool_rxnfc *nfc, u32 *rule_locs);
996 int (*set_rxnfc)(struct dsa_switch *ds, int port,
997 struct ethtool_rxnfc *nfc);
1002 int (*cls_flower_add)(struct dsa_switch *ds, int port,
1003 struct flow_cls_offload *cls, bool ingress);
1004 int (*cls_flower_del)(struct dsa_switch *ds, int port,
1005 struct flow_cls_offload *cls, bool ingress);
1006 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
1007 struct flow_cls_offload *cls, bool ingress);
1008 int (*port_mirror_add)(struct dsa_switch *ds, int port,
1009 struct dsa_mall_mirror_tc_entry *mirror,
1011 void (*port_mirror_del)(struct dsa_switch *ds, int port,
1012 struct dsa_mall_mirror_tc_entry *mirror);
1013 int (*port_policer_add)(struct dsa_switch *ds, int port,
1014 struct dsa_mall_policer_tc_entry *policer);
1015 void (*port_policer_del)(struct dsa_switch *ds, int port);
1016 int (*port_setup_tc)(struct dsa_switch *ds, int port,
1017 enum tc_setup_type type, void *type_data);
1020 * Cross-chip operations
1022 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
1023 int sw_index, int port,
1024 struct dsa_bridge bridge);
1025 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
1026 int sw_index, int port,
1027 struct dsa_bridge bridge);
1028 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
1030 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
1031 int port, struct dsa_lag lag,
1032 struct netdev_lag_upper_info *info);
1033 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
1034 int port, struct dsa_lag lag);
1039 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
1041 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
1043 void (*port_txtstamp)(struct dsa_switch *ds, int port,
1044 struct sk_buff *skb);
1045 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
1046 struct sk_buff *skb, unsigned int type);
1048 /* Devlink parameters, etc */
1049 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
1050 struct devlink_param_gset_ctx *ctx);
1051 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
1052 struct devlink_param_gset_ctx *ctx);
1053 int (*devlink_info_get)(struct dsa_switch *ds,
1054 struct devlink_info_req *req,
1055 struct netlink_ext_ack *extack);
1056 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
1057 unsigned int sb_index, u16 pool_index,
1058 struct devlink_sb_pool_info *pool_info);
1059 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
1060 u16 pool_index, u32 size,
1061 enum devlink_sb_threshold_type threshold_type,
1062 struct netlink_ext_ack *extack);
1063 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
1064 unsigned int sb_index, u16 pool_index,
1066 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
1067 unsigned int sb_index, u16 pool_index,
1069 struct netlink_ext_ack *extack);
1070 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
1071 unsigned int sb_index, u16 tc_index,
1072 enum devlink_sb_pool_type pool_type,
1073 u16 *p_pool_index, u32 *p_threshold);
1074 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
1075 unsigned int sb_index, u16 tc_index,
1076 enum devlink_sb_pool_type pool_type,
1077 u16 pool_index, u32 threshold,
1078 struct netlink_ext_ack *extack);
1079 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
1080 unsigned int sb_index);
1081 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
1082 unsigned int sb_index);
1083 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
1084 unsigned int sb_index, u16 pool_index,
1085 u32 *p_cur, u32 *p_max);
1086 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
1087 unsigned int sb_index, u16 tc_index,
1088 enum devlink_sb_pool_type pool_type,
1089 u32 *p_cur, u32 *p_max);
1092 * MTU change functionality. Switches can also adjust their MRU through
1093 * this method. By MTU, one understands the SDU (L2 payload) length.
1094 * If the switch needs to account for the DSA tag on the CPU port, this
1095 * method needs to do so privately.
1097 int (*port_change_mtu)(struct dsa_switch *ds, int port,
1099 int (*port_max_mtu)(struct dsa_switch *ds, int port);
1104 int (*port_lag_change)(struct dsa_switch *ds, int port);
1105 int (*port_lag_join)(struct dsa_switch *ds, int port,
1107 struct netdev_lag_upper_info *info);
1108 int (*port_lag_leave)(struct dsa_switch *ds, int port,
1109 struct dsa_lag lag);
1114 int (*port_hsr_join)(struct dsa_switch *ds, int port,
1115 struct net_device *hsr);
1116 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
1117 struct net_device *hsr);
1122 int (*port_mrp_add)(struct dsa_switch *ds, int port,
1123 const struct switchdev_obj_mrp *mrp);
1124 int (*port_mrp_del)(struct dsa_switch *ds, int port,
1125 const struct switchdev_obj_mrp *mrp);
1126 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1127 const struct switchdev_obj_ring_role_mrp *mrp);
1128 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1129 const struct switchdev_obj_ring_role_mrp *mrp);
1132 * tag_8021q operations
1134 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
1136 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1139 * DSA master tracking operations
1141 void (*master_state_change)(struct dsa_switch *ds,
1142 const struct net_device *master,
1146 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
1147 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
1148 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
1150 int dsa_devlink_param_get(struct devlink *dl, u32 id,
1151 struct devlink_param_gset_ctx *ctx);
1152 int dsa_devlink_param_set(struct devlink *dl, u32 id,
1153 struct devlink_param_gset_ctx *ctx);
1154 int dsa_devlink_params_register(struct dsa_switch *ds,
1155 const struct devlink_param *params,
1156 size_t params_count);
1157 void dsa_devlink_params_unregister(struct dsa_switch *ds,
1158 const struct devlink_param *params,
1159 size_t params_count);
1160 int dsa_devlink_resource_register(struct dsa_switch *ds,
1161 const char *resource_name,
1164 u64 parent_resource_id,
1165 const struct devlink_resource_size_params *size_params);
1167 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
1169 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
1171 devlink_resource_occ_get_t *occ_get,
1172 void *occ_get_priv);
1173 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
1175 struct devlink_region *
1176 dsa_devlink_region_create(struct dsa_switch *ds,
1177 const struct devlink_region_ops *ops,
1178 u32 region_max_snapshots, u64 region_size);
1179 struct devlink_region *
1180 dsa_devlink_port_region_create(struct dsa_switch *ds,
1182 const struct devlink_port_region_ops *ops,
1183 u32 region_max_snapshots, u64 region_size);
1184 void dsa_devlink_region_destroy(struct devlink_region *region);
1186 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
1188 struct dsa_devlink_priv {
1189 struct dsa_switch *ds;
1192 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1194 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1200 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
1202 struct devlink *dl = port->devlink;
1203 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1208 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1213 struct dsa_switch_driver {
1214 struct list_head list;
1215 const struct dsa_switch_ops *ops;
1218 struct net_device *dsa_dev_to_net_device(struct device *dev);
1220 /* Keep inline for faster access in hot path */
1221 static inline bool netdev_uses_dsa(const struct net_device *dev)
1223 #if IS_ENABLED(CONFIG_NET_DSA)
1224 return dev->dsa_ptr && dev->dsa_ptr->rcv;
1229 /* All DSA tags that push the EtherType to the right (basically all except tail
1230 * tags, which don't break dissection) can be treated the same from the
1231 * perspective of the flow dissector.
1233 * We need to return:
1234 * - offset: the (B - A) difference between:
1235 * A. the position of the real EtherType and
1236 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1237 * after the normal EtherType was supposed to be)
1238 * The offset in bytes is exactly equal to the tagger overhead (and half of
1239 * that, in __be16 shorts).
1241 * - proto: the value of the real EtherType.
1243 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1244 __be16 *proto, int *offset)
1246 #if IS_ENABLED(CONFIG_NET_DSA)
1247 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1248 int tag_len = ops->needed_headroom;
1251 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1255 #if IS_ENABLED(CONFIG_NET_DSA)
1256 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1258 int err = -EOPNOTSUPP;
1263 if (!dev->dsa_ptr->netdev_ops)
1269 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1272 const struct dsa_netdevice_ops *ops;
1275 err = __dsa_netdevice_ops_check(dev);
1279 ops = dev->dsa_ptr->netdev_ops;
1281 return ops->ndo_eth_ioctl(dev, ifr, cmd);
1284 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1291 void dsa_unregister_switch(struct dsa_switch *ds);
1292 int dsa_register_switch(struct dsa_switch *ds);
1293 void dsa_switch_shutdown(struct dsa_switch *ds);
1294 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1295 void dsa_flush_workqueue(void);
1296 #ifdef CONFIG_PM_SLEEP
1297 int dsa_switch_suspend(struct dsa_switch *ds);
1298 int dsa_switch_resume(struct dsa_switch *ds);
1300 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1304 static inline int dsa_switch_resume(struct dsa_switch *ds)
1308 #endif /* CONFIG_PM_SLEEP */
1310 #if IS_ENABLED(CONFIG_NET_DSA)
1311 bool dsa_slave_dev_check(const struct net_device *dev);
1313 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1319 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1320 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1322 struct dsa_tag_driver {
1323 const struct dsa_device_ops *ops;
1324 struct list_head list;
1325 struct module *owner;
1328 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1330 struct module *owner);
1331 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1332 unsigned int count);
1334 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1335 static int __init dsa_tag_driver_module_init(void) \
1337 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1341 module_init(dsa_tag_driver_module_init); \
1343 static void __exit dsa_tag_driver_module_exit(void) \
1345 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1347 module_exit(dsa_tag_driver_module_exit)
1350 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1352 * @__ops_array: Array of tag driver structures
1354 * Helper macro for DSA tag drivers which do not do anything special
1355 * in module init/exit. Each module may only use this macro once, and
1356 * calling it replaces module_init() and module_exit().
1358 #define module_dsa_tag_drivers(__ops_array) \
1359 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1361 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1363 /* Create a static structure we can build a linked list of dsa_tag
1366 #define DSA_TAG_DRIVER(__ops) \
1367 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1372 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1374 * @__ops: Single tag driver structures
1376 * Helper macro for DSA tag drivers which do not do anything special
1377 * in module init/exit. Each module may only use this macro once, and
1378 * calling it replaces module_init() and module_exit().
1380 #define module_dsa_tag_driver(__ops) \
1381 DSA_TAG_DRIVER(__ops); \
1383 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1384 &DSA_TAG_DRIVER_NAME(__ops) \
1386 module_dsa_tag_drivers(dsa_tag_driver_array)