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;
125 struct dsa_switch_tree {
126 struct list_head list;
128 /* List of switch ports */
129 struct list_head ports;
131 /* Notifier chain for switch-wide events */
132 struct raw_notifier_head nh;
134 /* Tree identifier */
137 /* Number of switches attached to this tree */
138 struct kref refcount;
140 /* Maps offloaded LAG netdevs to a zero-based linear ID for
141 * drivers that need it.
143 struct dsa_lag **lags;
145 /* Tagging protocol operations */
146 const struct dsa_device_ops *tag_ops;
148 /* Default tagging protocol preferred by the switches in this
151 enum dsa_tag_protocol default_proto;
153 /* Has this tree been applied to the hardware? */
157 * Configuration data for the platform device that owns
158 * this dsa switch tree instance.
160 struct dsa_platform_data *pd;
162 /* List of DSA links composing the routing table */
163 struct list_head rtable;
165 /* Length of "lags" array */
166 unsigned int lags_len;
168 /* Track the largest switch index within a tree */
169 unsigned int last_switch;
172 /* LAG IDs are one-based, the dst->lags array is zero-based */
173 #define dsa_lags_foreach_id(_id, _dst) \
174 for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \
175 if ((_dst)->lags[(_id) - 1])
177 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
178 list_for_each_entry((_dp), &(_dst)->ports, list) \
179 if (dsa_port_offloads_lag((_dp), (_lag)))
181 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
182 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
183 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
185 static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
188 /* DSA LAG IDs are one-based, dst->lags is zero-based */
189 return dst->lags[id - 1];
192 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
193 struct net_device *lag_dev)
197 dsa_lags_foreach_id(id, dst) {
198 struct dsa_lag *lag = dsa_lag_by_id(dst, id);
200 if (lag->dev == lag_dev)
207 /* TC matchall action types */
208 enum dsa_port_mall_action_type {
209 DSA_PORT_MALL_MIRROR,
210 DSA_PORT_MALL_POLICER,
213 /* TC mirroring entry */
214 struct dsa_mall_mirror_tc_entry {
219 /* TC port policer entry */
220 struct dsa_mall_policer_tc_entry {
222 u64 rate_bytes_per_sec;
225 /* TC matchall entry */
226 struct dsa_mall_tc_entry {
227 struct list_head list;
228 unsigned long cookie;
229 enum dsa_port_mall_action_type type;
231 struct dsa_mall_mirror_tc_entry mirror;
232 struct dsa_mall_policer_tc_entry policer;
237 struct net_device *dev;
244 /* A CPU port is physically connected to a master device.
245 * A user port exposed to userspace has a slave device.
248 struct net_device *master;
249 struct net_device *slave;
252 /* Copy of the tagging protocol operations, for quicker access
253 * in the data path. Valid only for the CPU ports.
255 const struct dsa_device_ops *tag_ops;
257 /* Copies for faster access in master receive hot path */
258 struct dsa_switch_tree *dst;
259 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
261 struct dsa_switch *ds;
266 DSA_PORT_TYPE_UNUSED = 0,
273 struct dsa_port *cpu_dp;
278 /* Warning: the following bit fields are not atomic, and updating them
279 * can only be done from code paths where concurrency is not possible
280 * (probe time or under rtnl_lock).
284 /* Managed by DSA on user ports and by drivers on CPU and DSA ports */
289 u8 devlink_port_setup:1;
291 /* Master state bits, valid only on CPU ports */
292 u8 master_admin_up:1;
297 struct device_node *dn;
298 unsigned int ageing_time;
300 struct dsa_bridge *bridge;
301 struct devlink_port devlink_port;
303 struct phylink_config pl_config;
305 struct net_device *hsr_dev;
307 struct list_head list;
310 * Original copy of the master netdev ethtool_ops
312 const struct ethtool_ops *orig_ethtool_ops;
315 * Original copy of the master netdev net_device_ops
317 const struct dsa_netdevice_ops *netdev_ops;
319 /* List of MAC addresses that must be forwarded on this port.
320 * These are only valid on CPU ports and DSA links.
322 struct mutex addr_lists_lock;
323 struct list_head fdbs;
324 struct list_head mdbs;
326 /* List of VLANs that CPU and DSA ports are members of. */
327 struct mutex vlans_lock;
328 struct list_head vlans;
331 /* TODO: ideally DSA ports would have a single dp->link_dp member,
332 * and no dst->rtable nor this struct dsa_link would be needed,
333 * but this would require some more complex tree walking,
334 * so keep it stupid at the moment and list them all.
338 struct dsa_port *link_dp;
339 struct list_head list;
342 struct dsa_mac_addr {
343 unsigned char addr[ETH_ALEN];
346 struct list_head list;
352 struct list_head list;
359 * Parent switch tree, and switch index.
361 struct dsa_switch_tree *dst;
364 /* Warning: the following bit fields are not atomic, and updating them
365 * can only be done from code paths where concurrency is not possible
366 * (probe time or under rtnl_lock).
370 /* Disallow bridge core from requesting different VLAN awareness
371 * settings on ports if not hardware-supported
373 u32 vlan_filtering_is_global:1;
375 /* Keep VLAN filtering enabled on ports not offloading any upper */
376 u32 needs_standalone_vlan_filtering:1;
378 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
379 * that have vlan_filtering=0. All drivers should ideally set this (and
380 * then the option would get removed), but it is unknown whether this
381 * would break things or not.
383 u32 configure_vlan_while_not_filtering:1;
385 /* If the switch driver always programs the CPU port as egress tagged
386 * despite the VLAN configuration indicating otherwise, then setting
387 * @untag_bridge_pvid will force the DSA receive path to pop the
388 * bridge's default_pvid VLAN tagged frames to offer a consistent
389 * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge
392 u32 untag_bridge_pvid:1;
394 /* Let DSA manage the FDB entries towards the
395 * CPU, based on the software bridge database.
397 u32 assisted_learning_on_cpu_port:1;
399 /* In case vlan_filtering_is_global is set, the VLAN awareness state
400 * should be retrieved from here and not from the per-port settings.
402 u32 vlan_filtering:1;
404 /* For switches that only have the MRU configurable. To ensure the
405 * configured MTU is not exceeded, normalization of MRU on all bridged
406 * interfaces is needed.
408 u32 mtu_enforcement_ingress:1;
410 /* Listener for switch fabric events */
411 struct notifier_block nb;
414 * Give the switch driver somewhere to hang its private data
422 * Configuration data for this switch.
424 struct dsa_chip_data *cd;
427 * The switch operations.
429 const struct dsa_switch_ops *ops;
432 * Slave mii_bus and devices for the individual ports.
435 struct mii_bus *slave_mii_bus;
437 /* Ageing Time limits in msecs */
438 unsigned int ageing_time_min;
439 unsigned int ageing_time_max;
441 /* Storage for drivers using tag_8021q */
442 struct dsa_8021q_context *tag_8021q_ctx;
444 /* devlink used to represent this switch device */
445 struct devlink *devlink;
447 /* Number of switch port queues */
448 unsigned int num_tx_queues;
450 /* Drivers that benefit from having an ID associated with each
451 * offloaded LAG should set this to the maximum number of
452 * supported IDs. DSA will then maintain a mapping of _at
453 * least_ these many IDs, accessible to drivers via
456 unsigned int num_lag_ids;
458 /* Drivers that support bridge forwarding offload or FDB isolation
459 * should set this to the maximum number of bridges spanning the same
460 * switch tree (or all trees, in the case of cross-tree bridging
461 * support) that can be offloaded.
463 unsigned int max_num_bridges;
465 unsigned int num_ports;
468 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
470 struct dsa_switch_tree *dst = ds->dst;
473 list_for_each_entry(dp, &dst->ports, list)
474 if (dp->ds == ds && dp->index == p)
480 static inline bool dsa_port_is_dsa(struct dsa_port *port)
482 return port->type == DSA_PORT_TYPE_DSA;
485 static inline bool dsa_port_is_cpu(struct dsa_port *port)
487 return port->type == DSA_PORT_TYPE_CPU;
490 static inline bool dsa_port_is_user(struct dsa_port *dp)
492 return dp->type == DSA_PORT_TYPE_USER;
495 static inline bool dsa_port_is_unused(struct dsa_port *dp)
497 return dp->type == DSA_PORT_TYPE_UNUSED;
500 static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
502 return dsa_port_is_cpu(dp) && dp->master_admin_up &&
506 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
508 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
511 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
513 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
516 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
518 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
521 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
523 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
526 #define dsa_tree_for_each_user_port(_dp, _dst) \
527 list_for_each_entry((_dp), &(_dst)->ports, list) \
528 if (dsa_port_is_user((_dp)))
530 #define dsa_switch_for_each_port(_dp, _ds) \
531 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
532 if ((_dp)->ds == (_ds))
534 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
535 list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
536 if ((_dp)->ds == (_ds))
538 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
539 list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
540 if ((_dp)->ds == (_ds))
542 #define dsa_switch_for_each_available_port(_dp, _ds) \
543 dsa_switch_for_each_port((_dp), (_ds)) \
544 if (!dsa_port_is_unused((_dp)))
546 #define dsa_switch_for_each_user_port(_dp, _ds) \
547 dsa_switch_for_each_port((_dp), (_ds)) \
548 if (dsa_port_is_user((_dp)))
550 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
551 dsa_switch_for_each_port((_dp), (_ds)) \
552 if (dsa_port_is_cpu((_dp)))
554 static inline u32 dsa_user_ports(struct dsa_switch *ds)
559 dsa_switch_for_each_user_port(dp, ds)
560 mask |= BIT(dp->index);
565 /* Return the local port used to reach an arbitrary switch device */
566 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
568 struct dsa_switch_tree *dst = ds->dst;
571 list_for_each_entry(dl, &dst->rtable, list)
572 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
573 return dl->dp->index;
575 return ds->num_ports;
578 /* Return the local port used to reach an arbitrary switch port */
579 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
582 if (device == ds->index)
585 return dsa_routing_port(ds, device);
588 /* Return the local port used to reach the dedicated CPU port */
589 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
591 const struct dsa_port *dp = dsa_to_port(ds, port);
592 const struct dsa_port *cpu_dp = dp->cpu_dp;
597 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
600 /* Return true if this is the local port used to reach the CPU port */
601 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
603 if (dsa_is_unused_port(ds, port))
606 return port == dsa_upstream_port(ds, port);
609 /* Return true if this is a DSA port leading away from the CPU */
610 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
612 return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
615 /* Return the local port used to reach the CPU port */
616 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
620 dsa_switch_for_each_available_port(dp, ds) {
621 return dsa_upstream_port(ds, dp->index);
624 return ds->num_ports;
627 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
628 * that the routing port from @downstream_ds to @upstream_ds is also the port
629 * which @downstream_ds uses to reach its dedicated CPU.
631 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
632 struct dsa_switch *downstream_ds)
636 if (upstream_ds == downstream_ds)
639 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
641 return dsa_is_upstream_port(downstream_ds, routing_port);
644 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
646 const struct dsa_switch *ds = dp->ds;
648 if (ds->vlan_filtering_is_global)
649 return ds->vlan_filtering;
651 return dp->vlan_filtering;
654 static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
656 return dp->lag ? dp->lag->id : 0;
659 static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
661 return dp->lag ? dp->lag->dev : NULL;
664 static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
665 const struct dsa_lag *lag)
667 return dsa_port_lag_dev_get(dp) == lag->dev;
671 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
678 else if (dp->hsr_dev)
684 static inline struct net_device *
685 dsa_port_bridge_dev_get(const struct dsa_port *dp)
687 return dp->bridge ? dp->bridge->dev : NULL;
690 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
692 return dp->bridge ? dp->bridge->num : 0;
695 static inline bool dsa_port_bridge_same(const struct dsa_port *a,
696 const struct dsa_port *b)
698 struct net_device *br_a = dsa_port_bridge_dev_get(a);
699 struct net_device *br_b = dsa_port_bridge_dev_get(b);
701 /* Standalone ports are not in the same bridge with one another */
702 return (!br_a || !br_b) ? false : (br_a == br_b);
705 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
706 const struct net_device *dev)
708 return dsa_port_to_bridge_port(dp) == dev;
712 dsa_port_offloads_bridge_dev(struct dsa_port *dp,
713 const struct net_device *bridge_dev)
715 /* DSA ports connected to a bridge, and event was emitted
718 return dsa_port_bridge_dev_get(dp) == bridge_dev;
721 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
722 const struct dsa_bridge *bridge)
724 return dsa_port_bridge_dev_get(dp) == bridge->dev;
727 /* Returns true if any port of this tree offloads the given net_device */
728 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
729 const struct net_device *dev)
733 list_for_each_entry(dp, &dst->ports, list)
734 if (dsa_port_offloads_bridge_port(dp, dev))
740 /* Returns true if any port of this tree offloads the given bridge */
742 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
743 const struct net_device *bridge_dev)
747 list_for_each_entry(dp, &dst->ports, list)
748 if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
754 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
755 bool is_static, void *data);
756 struct dsa_switch_ops {
758 * Tagging protocol helpers called for the CPU ports and DSA links.
759 * @get_tag_protocol retrieves the initial tagging protocol and is
760 * mandatory. Switches which can operate using multiple tagging
761 * protocols should implement @change_tag_protocol and report in
762 * @get_tag_protocol the tagger in current use.
764 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
766 enum dsa_tag_protocol mprot);
767 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
768 enum dsa_tag_protocol proto);
770 * Method for switch drivers to connect to the tagging protocol driver
771 * in current use. The switch driver can provide handlers for certain
772 * types of packets for switch management.
774 int (*connect_tag_protocol)(struct dsa_switch *ds,
775 enum dsa_tag_protocol proto);
777 /* Optional switch-wide initialization and destruction methods */
778 int (*setup)(struct dsa_switch *ds);
779 void (*teardown)(struct dsa_switch *ds);
781 /* Per-port initialization and destruction methods. Mandatory if the
782 * driver registers devlink port regions, optional otherwise.
784 int (*port_setup)(struct dsa_switch *ds, int port);
785 void (*port_teardown)(struct dsa_switch *ds, int port);
787 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
790 * Access to the switch's PHY registers.
792 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
793 int (*phy_write)(struct dsa_switch *ds, int port,
794 int regnum, u16 val);
797 * Link state adjustment (called from libphy)
799 void (*adjust_link)(struct dsa_switch *ds, int port,
800 struct phy_device *phydev);
801 void (*fixed_link_update)(struct dsa_switch *ds, int port,
802 struct fixed_phy_status *st);
805 * PHYLINK integration
807 void (*phylink_get_caps)(struct dsa_switch *ds, int port,
808 struct phylink_config *config);
809 void (*phylink_validate)(struct dsa_switch *ds, int port,
810 unsigned long *supported,
811 struct phylink_link_state *state);
812 struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
814 phy_interface_t iface);
815 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
816 struct phylink_link_state *state);
817 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
819 const struct phylink_link_state *state);
820 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
821 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
823 phy_interface_t interface);
824 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
826 phy_interface_t interface,
827 struct phy_device *phydev,
828 int speed, int duplex,
829 bool tx_pause, bool rx_pause);
830 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
831 struct phylink_link_state *state);
833 * Port statistics counters.
835 void (*get_strings)(struct dsa_switch *ds, int port,
836 u32 stringset, uint8_t *data);
837 void (*get_ethtool_stats)(struct dsa_switch *ds,
838 int port, uint64_t *data);
839 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
840 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
841 int port, uint64_t *data);
842 void (*get_eth_phy_stats)(struct dsa_switch *ds, int port,
843 struct ethtool_eth_phy_stats *phy_stats);
844 void (*get_eth_mac_stats)(struct dsa_switch *ds, int port,
845 struct ethtool_eth_mac_stats *mac_stats);
846 void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
847 struct ethtool_eth_ctrl_stats *ctrl_stats);
848 void (*get_stats64)(struct dsa_switch *ds, int port,
849 struct rtnl_link_stats64 *s);
850 void (*self_test)(struct dsa_switch *ds, int port,
851 struct ethtool_test *etest, u64 *data);
854 * ethtool Wake-on-LAN
856 void (*get_wol)(struct dsa_switch *ds, int port,
857 struct ethtool_wolinfo *w);
858 int (*set_wol)(struct dsa_switch *ds, int port,
859 struct ethtool_wolinfo *w);
862 * ethtool timestamp info
864 int (*get_ts_info)(struct dsa_switch *ds, int port,
865 struct ethtool_ts_info *ts);
870 int (*suspend)(struct dsa_switch *ds);
871 int (*resume)(struct dsa_switch *ds);
874 * Port enable/disable
876 int (*port_enable)(struct dsa_switch *ds, int port,
877 struct phy_device *phy);
878 void (*port_disable)(struct dsa_switch *ds, int port);
881 * Port's MAC EEE settings
883 int (*set_mac_eee)(struct dsa_switch *ds, int port,
884 struct ethtool_eee *e);
885 int (*get_mac_eee)(struct dsa_switch *ds, int port,
886 struct ethtool_eee *e);
889 int (*get_eeprom_len)(struct dsa_switch *ds);
890 int (*get_eeprom)(struct dsa_switch *ds,
891 struct ethtool_eeprom *eeprom, u8 *data);
892 int (*set_eeprom)(struct dsa_switch *ds,
893 struct ethtool_eeprom *eeprom, u8 *data);
898 int (*get_regs_len)(struct dsa_switch *ds, int port);
899 void (*get_regs)(struct dsa_switch *ds, int port,
900 struct ethtool_regs *regs, void *p);
903 * Upper device tracking.
905 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
906 struct netdev_notifier_changeupper_info *info);
911 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
912 int (*port_bridge_join)(struct dsa_switch *ds, int port,
913 struct dsa_bridge bridge,
914 bool *tx_fwd_offload);
915 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
916 struct dsa_bridge bridge);
917 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
919 void (*port_fast_age)(struct dsa_switch *ds, int port);
920 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
921 struct switchdev_brport_flags flags,
922 struct netlink_ext_ack *extack);
923 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
924 struct switchdev_brport_flags flags,
925 struct netlink_ext_ack *extack);
930 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
932 struct netlink_ext_ack *extack);
933 int (*port_vlan_add)(struct dsa_switch *ds, int port,
934 const struct switchdev_obj_port_vlan *vlan,
935 struct netlink_ext_ack *extack);
936 int (*port_vlan_del)(struct dsa_switch *ds, int port,
937 const struct switchdev_obj_port_vlan *vlan);
939 * Forwarding database
941 int (*port_fdb_add)(struct dsa_switch *ds, int port,
942 const unsigned char *addr, u16 vid);
943 int (*port_fdb_del)(struct dsa_switch *ds, int port,
944 const unsigned char *addr, u16 vid);
945 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
946 dsa_fdb_dump_cb_t *cb, void *data);
951 int (*port_mdb_add)(struct dsa_switch *ds, int port,
952 const struct switchdev_obj_port_mdb *mdb);
953 int (*port_mdb_del)(struct dsa_switch *ds, int port,
954 const struct switchdev_obj_port_mdb *mdb);
958 int (*get_rxnfc)(struct dsa_switch *ds, int port,
959 struct ethtool_rxnfc *nfc, u32 *rule_locs);
960 int (*set_rxnfc)(struct dsa_switch *ds, int port,
961 struct ethtool_rxnfc *nfc);
966 int (*cls_flower_add)(struct dsa_switch *ds, int port,
967 struct flow_cls_offload *cls, bool ingress);
968 int (*cls_flower_del)(struct dsa_switch *ds, int port,
969 struct flow_cls_offload *cls, bool ingress);
970 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
971 struct flow_cls_offload *cls, bool ingress);
972 int (*port_mirror_add)(struct dsa_switch *ds, int port,
973 struct dsa_mall_mirror_tc_entry *mirror,
975 void (*port_mirror_del)(struct dsa_switch *ds, int port,
976 struct dsa_mall_mirror_tc_entry *mirror);
977 int (*port_policer_add)(struct dsa_switch *ds, int port,
978 struct dsa_mall_policer_tc_entry *policer);
979 void (*port_policer_del)(struct dsa_switch *ds, int port);
980 int (*port_setup_tc)(struct dsa_switch *ds, int port,
981 enum tc_setup_type type, void *type_data);
984 * Cross-chip operations
986 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
987 int sw_index, int port,
988 struct dsa_bridge bridge);
989 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
990 int sw_index, int port,
991 struct dsa_bridge bridge);
992 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
994 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
995 int port, struct dsa_lag lag,
996 struct netdev_lag_upper_info *info);
997 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
998 int port, struct dsa_lag lag);
1003 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
1005 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
1007 void (*port_txtstamp)(struct dsa_switch *ds, int port,
1008 struct sk_buff *skb);
1009 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
1010 struct sk_buff *skb, unsigned int type);
1012 /* Devlink parameters, etc */
1013 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
1014 struct devlink_param_gset_ctx *ctx);
1015 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
1016 struct devlink_param_gset_ctx *ctx);
1017 int (*devlink_info_get)(struct dsa_switch *ds,
1018 struct devlink_info_req *req,
1019 struct netlink_ext_ack *extack);
1020 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
1021 unsigned int sb_index, u16 pool_index,
1022 struct devlink_sb_pool_info *pool_info);
1023 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
1024 u16 pool_index, u32 size,
1025 enum devlink_sb_threshold_type threshold_type,
1026 struct netlink_ext_ack *extack);
1027 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
1028 unsigned int sb_index, u16 pool_index,
1030 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
1031 unsigned int sb_index, u16 pool_index,
1033 struct netlink_ext_ack *extack);
1034 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
1035 unsigned int sb_index, u16 tc_index,
1036 enum devlink_sb_pool_type pool_type,
1037 u16 *p_pool_index, u32 *p_threshold);
1038 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
1039 unsigned int sb_index, u16 tc_index,
1040 enum devlink_sb_pool_type pool_type,
1041 u16 pool_index, u32 threshold,
1042 struct netlink_ext_ack *extack);
1043 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
1044 unsigned int sb_index);
1045 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
1046 unsigned int sb_index);
1047 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
1048 unsigned int sb_index, u16 pool_index,
1049 u32 *p_cur, u32 *p_max);
1050 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
1051 unsigned int sb_index, u16 tc_index,
1052 enum devlink_sb_pool_type pool_type,
1053 u32 *p_cur, u32 *p_max);
1056 * MTU change functionality. Switches can also adjust their MRU through
1057 * this method. By MTU, one understands the SDU (L2 payload) length.
1058 * If the switch needs to account for the DSA tag on the CPU port, this
1059 * method needs to do so privately.
1061 int (*port_change_mtu)(struct dsa_switch *ds, int port,
1063 int (*port_max_mtu)(struct dsa_switch *ds, int port);
1068 int (*port_lag_change)(struct dsa_switch *ds, int port);
1069 int (*port_lag_join)(struct dsa_switch *ds, int port,
1071 struct netdev_lag_upper_info *info);
1072 int (*port_lag_leave)(struct dsa_switch *ds, int port,
1073 struct dsa_lag lag);
1078 int (*port_hsr_join)(struct dsa_switch *ds, int port,
1079 struct net_device *hsr);
1080 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
1081 struct net_device *hsr);
1086 int (*port_mrp_add)(struct dsa_switch *ds, int port,
1087 const struct switchdev_obj_mrp *mrp);
1088 int (*port_mrp_del)(struct dsa_switch *ds, int port,
1089 const struct switchdev_obj_mrp *mrp);
1090 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1091 const struct switchdev_obj_ring_role_mrp *mrp);
1092 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1093 const struct switchdev_obj_ring_role_mrp *mrp);
1096 * tag_8021q operations
1098 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
1100 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1103 * DSA master tracking operations
1105 void (*master_state_change)(struct dsa_switch *ds,
1106 const struct net_device *master,
1110 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
1111 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
1112 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
1114 int dsa_devlink_param_get(struct devlink *dl, u32 id,
1115 struct devlink_param_gset_ctx *ctx);
1116 int dsa_devlink_param_set(struct devlink *dl, u32 id,
1117 struct devlink_param_gset_ctx *ctx);
1118 int dsa_devlink_params_register(struct dsa_switch *ds,
1119 const struct devlink_param *params,
1120 size_t params_count);
1121 void dsa_devlink_params_unregister(struct dsa_switch *ds,
1122 const struct devlink_param *params,
1123 size_t params_count);
1124 int dsa_devlink_resource_register(struct dsa_switch *ds,
1125 const char *resource_name,
1128 u64 parent_resource_id,
1129 const struct devlink_resource_size_params *size_params);
1131 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
1133 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
1135 devlink_resource_occ_get_t *occ_get,
1136 void *occ_get_priv);
1137 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
1139 struct devlink_region *
1140 dsa_devlink_region_create(struct dsa_switch *ds,
1141 const struct devlink_region_ops *ops,
1142 u32 region_max_snapshots, u64 region_size);
1143 struct devlink_region *
1144 dsa_devlink_port_region_create(struct dsa_switch *ds,
1146 const struct devlink_port_region_ops *ops,
1147 u32 region_max_snapshots, u64 region_size);
1148 void dsa_devlink_region_destroy(struct devlink_region *region);
1150 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
1152 struct dsa_devlink_priv {
1153 struct dsa_switch *ds;
1156 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1158 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1164 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
1166 struct devlink *dl = port->devlink;
1167 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1172 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1177 struct dsa_switch_driver {
1178 struct list_head list;
1179 const struct dsa_switch_ops *ops;
1182 struct net_device *dsa_dev_to_net_device(struct device *dev);
1184 /* Keep inline for faster access in hot path */
1185 static inline bool netdev_uses_dsa(const struct net_device *dev)
1187 #if IS_ENABLED(CONFIG_NET_DSA)
1188 return dev->dsa_ptr && dev->dsa_ptr->rcv;
1193 /* All DSA tags that push the EtherType to the right (basically all except tail
1194 * tags, which don't break dissection) can be treated the same from the
1195 * perspective of the flow dissector.
1197 * We need to return:
1198 * - offset: the (B - A) difference between:
1199 * A. the position of the real EtherType and
1200 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1201 * after the normal EtherType was supposed to be)
1202 * The offset in bytes is exactly equal to the tagger overhead (and half of
1203 * that, in __be16 shorts).
1205 * - proto: the value of the real EtherType.
1207 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1208 __be16 *proto, int *offset)
1210 #if IS_ENABLED(CONFIG_NET_DSA)
1211 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1212 int tag_len = ops->needed_headroom;
1215 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1219 #if IS_ENABLED(CONFIG_NET_DSA)
1220 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1222 int err = -EOPNOTSUPP;
1227 if (!dev->dsa_ptr->netdev_ops)
1233 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1236 const struct dsa_netdevice_ops *ops;
1239 err = __dsa_netdevice_ops_check(dev);
1243 ops = dev->dsa_ptr->netdev_ops;
1245 return ops->ndo_eth_ioctl(dev, ifr, cmd);
1248 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1255 void dsa_unregister_switch(struct dsa_switch *ds);
1256 int dsa_register_switch(struct dsa_switch *ds);
1257 void dsa_switch_shutdown(struct dsa_switch *ds);
1258 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1259 void dsa_flush_workqueue(void);
1260 #ifdef CONFIG_PM_SLEEP
1261 int dsa_switch_suspend(struct dsa_switch *ds);
1262 int dsa_switch_resume(struct dsa_switch *ds);
1264 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1268 static inline int dsa_switch_resume(struct dsa_switch *ds)
1272 #endif /* CONFIG_PM_SLEEP */
1274 #if IS_ENABLED(CONFIG_NET_DSA)
1275 bool dsa_slave_dev_check(const struct net_device *dev);
1277 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1283 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1284 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1286 struct dsa_tag_driver {
1287 const struct dsa_device_ops *ops;
1288 struct list_head list;
1289 struct module *owner;
1292 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1294 struct module *owner);
1295 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1296 unsigned int count);
1298 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1299 static int __init dsa_tag_driver_module_init(void) \
1301 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1305 module_init(dsa_tag_driver_module_init); \
1307 static void __exit dsa_tag_driver_module_exit(void) \
1309 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1311 module_exit(dsa_tag_driver_module_exit)
1314 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1316 * @__ops_array: Array of tag driver structures
1318 * Helper macro for DSA tag drivers which do not do anything special
1319 * in module init/exit. Each module may only use this macro once, and
1320 * calling it replaces module_init() and module_exit().
1322 #define module_dsa_tag_drivers(__ops_array) \
1323 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1325 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1327 /* Create a static structure we can build a linked list of dsa_tag
1330 #define DSA_TAG_DRIVER(__ops) \
1331 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1336 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1338 * @__ops: Single tag driver structures
1340 * Helper macro for DSA tag drivers which do not do anything special
1341 * in module init/exit. Each module may only use this macro once, and
1342 * calling it replaces module_init() and module_exit().
1344 #define module_dsa_tag_driver(__ops) \
1345 DSA_TAG_DRIVER(__ops); \
1347 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1348 &DSA_TAG_DRIVER_NAME(__ops) \
1350 module_dsa_tag_drivers(dsa_tag_driver_array)