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
55 enum dsa_tag_protocol {
56 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
57 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
58 DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
59 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
60 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
61 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
62 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
63 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
64 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
65 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
66 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
67 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
68 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
69 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
70 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
71 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE,
72 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE,
73 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE,
74 DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE,
75 DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE,
76 DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE,
77 DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
78 DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE,
79 DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE,
84 struct dsa_device_ops {
85 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
86 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
87 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
89 unsigned int needed_headroom;
90 unsigned int needed_tailroom;
92 enum dsa_tag_protocol proto;
93 /* Some tagging protocols either mangle or shift the destination MAC
94 * address, in which case the DSA master would drop packets on ingress
95 * if what it understands out of the destination MAC address is not in
98 bool promisc_on_master;
101 /* This structure defines the control interfaces that are overlayed by the
102 * DSA layer on top of the DSA CPU/management net_device instance. This is
103 * used by the core net_device layer while calling various net_device_ops
106 struct dsa_netdevice_ops {
107 int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
111 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
112 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \
113 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
115 struct dsa_switch_tree {
116 struct list_head list;
118 /* Notifier chain for switch-wide events */
119 struct raw_notifier_head nh;
121 /* Tree identifier */
124 /* Number of switches attached to this tree */
125 struct kref refcount;
127 /* Has this tree been applied to the hardware? */
130 /* Tagging protocol operations */
131 const struct dsa_device_ops *tag_ops;
133 /* Default tagging protocol preferred by the switches in this
136 enum dsa_tag_protocol default_proto;
139 * Configuration data for the platform device that owns
140 * this dsa switch tree instance.
142 struct dsa_platform_data *pd;
144 /* List of switch ports */
145 struct list_head ports;
147 /* List of DSA links composing the routing table */
148 struct list_head rtable;
150 /* Maps offloaded LAG netdevs to a zero-based linear ID for
151 * drivers that need it.
153 struct net_device **lags;
154 unsigned int lags_len;
156 /* Track the largest switch index within a tree */
157 unsigned int last_switch;
160 #define dsa_lags_foreach_id(_id, _dst) \
161 for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++) \
162 if ((_dst)->lags[(_id)])
164 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
165 list_for_each_entry((_dp), &(_dst)->ports, list) \
166 if ((_dp)->lag_dev == (_lag))
168 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
169 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
170 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
172 static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
175 return dst->lags[id];
178 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
179 struct net_device *lag)
183 dsa_lags_foreach_id(id, dst) {
184 if (dsa_lag_dev(dst, id) == lag)
191 /* TC matchall action types */
192 enum dsa_port_mall_action_type {
193 DSA_PORT_MALL_MIRROR,
194 DSA_PORT_MALL_POLICER,
197 /* TC mirroring entry */
198 struct dsa_mall_mirror_tc_entry {
203 /* TC port policer entry */
204 struct dsa_mall_policer_tc_entry {
206 u64 rate_bytes_per_sec;
209 /* TC matchall entry */
210 struct dsa_mall_tc_entry {
211 struct list_head list;
212 unsigned long cookie;
213 enum dsa_port_mall_action_type type;
215 struct dsa_mall_mirror_tc_entry mirror;
216 struct dsa_mall_policer_tc_entry policer;
222 /* A CPU port is physically connected to a master device.
223 * A user port exposed to userspace has a slave device.
226 struct net_device *master;
227 struct net_device *slave;
230 /* Copy of the tagging protocol operations, for quicker access
231 * in the data path. Valid only for the CPU ports.
233 const struct dsa_device_ops *tag_ops;
235 /* Copies for faster access in master receive hot path */
236 struct dsa_switch_tree *dst;
237 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
240 DSA_PORT_TYPE_UNUSED = 0,
246 struct dsa_switch *ds;
249 struct dsa_port *cpu_dp;
251 struct device_node *dn;
252 unsigned int ageing_time;
254 /* Managed by DSA on user ports and by drivers on CPU and DSA ports */
257 struct net_device *bridge_dev;
259 struct devlink_port devlink_port;
260 bool devlink_port_setup;
262 struct phylink_config pl_config;
263 struct net_device *lag_dev;
265 struct net_device *hsr_dev;
267 struct list_head list;
270 * Give the switch driver somewhere to hang its per-port private data
271 * structures (accessible from the tagger).
276 * Original copy of the master netdev ethtool_ops
278 const struct ethtool_ops *orig_ethtool_ops;
281 * Original copy of the master netdev net_device_ops
283 const struct dsa_netdevice_ops *netdev_ops;
285 /* List of MAC addresses that must be forwarded on this port.
286 * These are only valid on CPU ports and DSA links.
288 struct list_head fdbs;
289 struct list_head mdbs;
294 /* TODO: ideally DSA ports would have a single dp->link_dp member,
295 * and no dst->rtable nor this struct dsa_link would be needed,
296 * but this would require some more complex tree walking,
297 * so keep it stupid at the moment and list them all.
301 struct dsa_port *link_dp;
302 struct list_head list;
305 struct dsa_mac_addr {
306 unsigned char addr[ETH_ALEN];
309 struct list_head list;
318 * Parent switch tree, and switch index.
320 struct dsa_switch_tree *dst;
323 /* Listener for switch fabric events */
324 struct notifier_block nb;
327 * Give the switch driver somewhere to hang its private data
333 * Configuration data for this switch.
335 struct dsa_chip_data *cd;
338 * The switch operations.
340 const struct dsa_switch_ops *ops;
343 * Slave mii_bus and devices for the individual ports.
346 struct mii_bus *slave_mii_bus;
348 /* Ageing Time limits in msecs */
349 unsigned int ageing_time_min;
350 unsigned int ageing_time_max;
352 /* Storage for drivers using tag_8021q */
353 struct dsa_8021q_context *tag_8021q_ctx;
355 /* devlink used to represent this switch device */
356 struct devlink *devlink;
358 /* Number of switch port queues */
359 unsigned int num_tx_queues;
361 /* Disallow bridge core from requesting different VLAN awareness
362 * settings on ports if not hardware-supported
364 bool vlan_filtering_is_global;
366 /* Keep VLAN filtering enabled on ports not offloading any upper. */
367 bool needs_standalone_vlan_filtering;
369 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
370 * that have vlan_filtering=0. All drivers should ideally set this (and
371 * then the option would get removed), but it is unknown whether this
372 * would break things or not.
374 bool configure_vlan_while_not_filtering;
376 /* If the switch driver always programs the CPU port as egress tagged
377 * despite the VLAN configuration indicating otherwise, then setting
378 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
379 * default_pvid VLAN tagged frames to offer a consistent behavior
380 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
382 bool untag_bridge_pvid;
384 /* Let DSA manage the FDB entries towards the CPU, based on the
385 * software bridge database.
387 bool assisted_learning_on_cpu_port;
389 /* In case vlan_filtering_is_global is set, the VLAN awareness state
390 * should be retrieved from here and not from the per-port settings.
394 /* MAC PCS does not provide link state change interrupt, and requires
395 * polling. Flag passed on to PHYLINK.
399 /* For switches that only have the MRU configurable. To ensure the
400 * configured MTU is not exceeded, normalization of MRU on all bridged
401 * interfaces is needed.
403 bool mtu_enforcement_ingress;
405 /* Drivers that benefit from having an ID associated with each
406 * offloaded LAG should set this to the maximum number of
407 * supported IDs. DSA will then maintain a mapping of _at
408 * least_ these many IDs, accessible to drivers via
411 unsigned int num_lag_ids;
413 /* Drivers that support bridge forwarding offload should set this to
414 * the maximum number of bridges spanning the same switch tree (or all
415 * trees, in the case of cross-tree bridging support) that can be
418 unsigned int num_fwd_offloading_bridges;
423 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
425 struct dsa_switch_tree *dst = ds->dst;
428 list_for_each_entry(dp, &dst->ports, list)
429 if (dp->ds == ds && dp->index == p)
435 static inline bool dsa_port_is_dsa(struct dsa_port *port)
437 return port->type == DSA_PORT_TYPE_DSA;
440 static inline bool dsa_port_is_cpu(struct dsa_port *port)
442 return port->type == DSA_PORT_TYPE_CPU;
445 static inline bool dsa_port_is_user(struct dsa_port *dp)
447 return dp->type == DSA_PORT_TYPE_USER;
450 static inline bool dsa_port_is_unused(struct dsa_port *dp)
452 return dp->type == DSA_PORT_TYPE_UNUSED;
455 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
457 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
460 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
462 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
465 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
467 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
470 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
472 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
475 static inline u32 dsa_user_ports(struct dsa_switch *ds)
480 for (p = 0; p < ds->num_ports; p++)
481 if (dsa_is_user_port(ds, p))
487 /* Return the local port used to reach an arbitrary switch device */
488 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
490 struct dsa_switch_tree *dst = ds->dst;
493 list_for_each_entry(dl, &dst->rtable, list)
494 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
495 return dl->dp->index;
497 return ds->num_ports;
500 /* Return the local port used to reach an arbitrary switch port */
501 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
504 if (device == ds->index)
507 return dsa_routing_port(ds, device);
510 /* Return the local port used to reach the dedicated CPU port */
511 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
513 const struct dsa_port *dp = dsa_to_port(ds, port);
514 const struct dsa_port *cpu_dp = dp->cpu_dp;
519 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
522 /* Return true if this is the local port used to reach the CPU port */
523 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
525 if (dsa_is_unused_port(ds, port))
528 return port == dsa_upstream_port(ds, port);
531 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
532 * that the routing port from @downstream_ds to @upstream_ds is also the port
533 * which @downstream_ds uses to reach its dedicated CPU.
535 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
536 struct dsa_switch *downstream_ds)
540 if (upstream_ds == downstream_ds)
543 routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
545 return dsa_is_upstream_port(downstream_ds, routing_port);
548 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
550 const struct dsa_switch *ds = dp->ds;
552 if (ds->vlan_filtering_is_global)
553 return ds->vlan_filtering;
555 return dp->vlan_filtering;
559 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
566 else if (dp->hsr_dev)
572 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
573 bool is_static, void *data);
574 struct dsa_switch_ops {
576 * Tagging protocol helpers called for the CPU ports and DSA links.
577 * @get_tag_protocol retrieves the initial tagging protocol and is
578 * mandatory. Switches which can operate using multiple tagging
579 * protocols should implement @change_tag_protocol and report in
580 * @get_tag_protocol the tagger in current use.
582 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
584 enum dsa_tag_protocol mprot);
585 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
586 enum dsa_tag_protocol proto);
588 /* Optional switch-wide initialization and destruction methods */
589 int (*setup)(struct dsa_switch *ds);
590 void (*teardown)(struct dsa_switch *ds);
592 /* Per-port initialization and destruction methods. Mandatory if the
593 * driver registers devlink port regions, optional otherwise.
595 int (*port_setup)(struct dsa_switch *ds, int port);
596 void (*port_teardown)(struct dsa_switch *ds, int port);
598 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
601 * Access to the switch's PHY registers.
603 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
604 int (*phy_write)(struct dsa_switch *ds, int port,
605 int regnum, u16 val);
608 * Link state adjustment (called from libphy)
610 void (*adjust_link)(struct dsa_switch *ds, int port,
611 struct phy_device *phydev);
612 void (*fixed_link_update)(struct dsa_switch *ds, int port,
613 struct fixed_phy_status *st);
616 * PHYLINK integration
618 void (*phylink_validate)(struct dsa_switch *ds, int port,
619 unsigned long *supported,
620 struct phylink_link_state *state);
621 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
622 struct phylink_link_state *state);
623 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
625 const struct phylink_link_state *state);
626 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
627 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
629 phy_interface_t interface);
630 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
632 phy_interface_t interface,
633 struct phy_device *phydev,
634 int speed, int duplex,
635 bool tx_pause, bool rx_pause);
636 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
637 struct phylink_link_state *state);
639 * Port statistics counters.
641 void (*get_strings)(struct dsa_switch *ds, int port,
642 u32 stringset, uint8_t *data);
643 void (*get_ethtool_stats)(struct dsa_switch *ds,
644 int port, uint64_t *data);
645 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
646 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
647 int port, uint64_t *data);
648 void (*get_stats64)(struct dsa_switch *ds, int port,
649 struct rtnl_link_stats64 *s);
650 void (*self_test)(struct dsa_switch *ds, int port,
651 struct ethtool_test *etest, u64 *data);
654 * ethtool Wake-on-LAN
656 void (*get_wol)(struct dsa_switch *ds, int port,
657 struct ethtool_wolinfo *w);
658 int (*set_wol)(struct dsa_switch *ds, int port,
659 struct ethtool_wolinfo *w);
662 * ethtool timestamp info
664 int (*get_ts_info)(struct dsa_switch *ds, int port,
665 struct ethtool_ts_info *ts);
670 int (*suspend)(struct dsa_switch *ds);
671 int (*resume)(struct dsa_switch *ds);
674 * Port enable/disable
676 int (*port_enable)(struct dsa_switch *ds, int port,
677 struct phy_device *phy);
678 void (*port_disable)(struct dsa_switch *ds, int port);
681 * Port's MAC EEE settings
683 int (*set_mac_eee)(struct dsa_switch *ds, int port,
684 struct ethtool_eee *e);
685 int (*get_mac_eee)(struct dsa_switch *ds, int port,
686 struct ethtool_eee *e);
689 int (*get_eeprom_len)(struct dsa_switch *ds);
690 int (*get_eeprom)(struct dsa_switch *ds,
691 struct ethtool_eeprom *eeprom, u8 *data);
692 int (*set_eeprom)(struct dsa_switch *ds,
693 struct ethtool_eeprom *eeprom, u8 *data);
698 int (*get_regs_len)(struct dsa_switch *ds, int port);
699 void (*get_regs)(struct dsa_switch *ds, int port,
700 struct ethtool_regs *regs, void *p);
703 * Upper device tracking.
705 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
706 struct netdev_notifier_changeupper_info *info);
711 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
712 int (*port_bridge_join)(struct dsa_switch *ds, int port,
713 struct net_device *bridge);
714 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
715 struct net_device *bridge);
716 /* Called right after .port_bridge_join() */
717 int (*port_bridge_tx_fwd_offload)(struct dsa_switch *ds, int port,
718 struct net_device *bridge,
720 /* Called right before .port_bridge_leave() */
721 void (*port_bridge_tx_fwd_unoffload)(struct dsa_switch *ds, int port,
722 struct net_device *bridge,
724 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
726 void (*port_fast_age)(struct dsa_switch *ds, int port);
727 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
728 struct switchdev_brport_flags flags,
729 struct netlink_ext_ack *extack);
730 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
731 struct switchdev_brport_flags flags,
732 struct netlink_ext_ack *extack);
737 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
739 struct netlink_ext_ack *extack);
740 int (*port_vlan_add)(struct dsa_switch *ds, int port,
741 const struct switchdev_obj_port_vlan *vlan,
742 struct netlink_ext_ack *extack);
743 int (*port_vlan_del)(struct dsa_switch *ds, int port,
744 const struct switchdev_obj_port_vlan *vlan);
746 * Forwarding database
748 int (*port_fdb_add)(struct dsa_switch *ds, int port,
749 const unsigned char *addr, u16 vid);
750 int (*port_fdb_del)(struct dsa_switch *ds, int port,
751 const unsigned char *addr, u16 vid);
752 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
753 dsa_fdb_dump_cb_t *cb, void *data);
758 int (*port_mdb_add)(struct dsa_switch *ds, int port,
759 const struct switchdev_obj_port_mdb *mdb);
760 int (*port_mdb_del)(struct dsa_switch *ds, int port,
761 const struct switchdev_obj_port_mdb *mdb);
765 int (*get_rxnfc)(struct dsa_switch *ds, int port,
766 struct ethtool_rxnfc *nfc, u32 *rule_locs);
767 int (*set_rxnfc)(struct dsa_switch *ds, int port,
768 struct ethtool_rxnfc *nfc);
773 int (*cls_flower_add)(struct dsa_switch *ds, int port,
774 struct flow_cls_offload *cls, bool ingress);
775 int (*cls_flower_del)(struct dsa_switch *ds, int port,
776 struct flow_cls_offload *cls, bool ingress);
777 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
778 struct flow_cls_offload *cls, bool ingress);
779 int (*port_mirror_add)(struct dsa_switch *ds, int port,
780 struct dsa_mall_mirror_tc_entry *mirror,
782 void (*port_mirror_del)(struct dsa_switch *ds, int port,
783 struct dsa_mall_mirror_tc_entry *mirror);
784 int (*port_policer_add)(struct dsa_switch *ds, int port,
785 struct dsa_mall_policer_tc_entry *policer);
786 void (*port_policer_del)(struct dsa_switch *ds, int port);
787 int (*port_setup_tc)(struct dsa_switch *ds, int port,
788 enum tc_setup_type type, void *type_data);
791 * Cross-chip operations
793 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
794 int sw_index, int port,
795 struct net_device *br);
796 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
797 int sw_index, int port,
798 struct net_device *br);
799 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
801 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
802 int port, struct net_device *lag,
803 struct netdev_lag_upper_info *info);
804 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
805 int port, struct net_device *lag);
810 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
812 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
814 void (*port_txtstamp)(struct dsa_switch *ds, int port,
815 struct sk_buff *skb);
816 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
817 struct sk_buff *skb, unsigned int type);
819 /* Devlink parameters, etc */
820 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
821 struct devlink_param_gset_ctx *ctx);
822 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
823 struct devlink_param_gset_ctx *ctx);
824 int (*devlink_info_get)(struct dsa_switch *ds,
825 struct devlink_info_req *req,
826 struct netlink_ext_ack *extack);
827 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
828 unsigned int sb_index, u16 pool_index,
829 struct devlink_sb_pool_info *pool_info);
830 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
831 u16 pool_index, u32 size,
832 enum devlink_sb_threshold_type threshold_type,
833 struct netlink_ext_ack *extack);
834 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
835 unsigned int sb_index, u16 pool_index,
837 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
838 unsigned int sb_index, u16 pool_index,
840 struct netlink_ext_ack *extack);
841 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
842 unsigned int sb_index, u16 tc_index,
843 enum devlink_sb_pool_type pool_type,
844 u16 *p_pool_index, u32 *p_threshold);
845 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
846 unsigned int sb_index, u16 tc_index,
847 enum devlink_sb_pool_type pool_type,
848 u16 pool_index, u32 threshold,
849 struct netlink_ext_ack *extack);
850 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
851 unsigned int sb_index);
852 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
853 unsigned int sb_index);
854 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
855 unsigned int sb_index, u16 pool_index,
856 u32 *p_cur, u32 *p_max);
857 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
858 unsigned int sb_index, u16 tc_index,
859 enum devlink_sb_pool_type pool_type,
860 u32 *p_cur, u32 *p_max);
863 * MTU change functionality. Switches can also adjust their MRU through
864 * this method. By MTU, one understands the SDU (L2 payload) length.
865 * If the switch needs to account for the DSA tag on the CPU port, this
866 * method needs to do so privately.
868 int (*port_change_mtu)(struct dsa_switch *ds, int port,
870 int (*port_max_mtu)(struct dsa_switch *ds, int port);
875 int (*port_lag_change)(struct dsa_switch *ds, int port);
876 int (*port_lag_join)(struct dsa_switch *ds, int port,
877 struct net_device *lag,
878 struct netdev_lag_upper_info *info);
879 int (*port_lag_leave)(struct dsa_switch *ds, int port,
880 struct net_device *lag);
885 int (*port_hsr_join)(struct dsa_switch *ds, int port,
886 struct net_device *hsr);
887 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
888 struct net_device *hsr);
893 int (*port_mrp_add)(struct dsa_switch *ds, int port,
894 const struct switchdev_obj_mrp *mrp);
895 int (*port_mrp_del)(struct dsa_switch *ds, int port,
896 const struct switchdev_obj_mrp *mrp);
897 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
898 const struct switchdev_obj_ring_role_mrp *mrp);
899 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
900 const struct switchdev_obj_ring_role_mrp *mrp);
903 * tag_8021q operations
905 int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
907 int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
910 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
911 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
912 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
914 int dsa_devlink_param_get(struct devlink *dl, u32 id,
915 struct devlink_param_gset_ctx *ctx);
916 int dsa_devlink_param_set(struct devlink *dl, u32 id,
917 struct devlink_param_gset_ctx *ctx);
918 int dsa_devlink_params_register(struct dsa_switch *ds,
919 const struct devlink_param *params,
920 size_t params_count);
921 void dsa_devlink_params_unregister(struct dsa_switch *ds,
922 const struct devlink_param *params,
923 size_t params_count);
924 int dsa_devlink_resource_register(struct dsa_switch *ds,
925 const char *resource_name,
928 u64 parent_resource_id,
929 const struct devlink_resource_size_params *size_params);
931 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
933 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
935 devlink_resource_occ_get_t *occ_get,
937 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
939 struct devlink_region *
940 dsa_devlink_region_create(struct dsa_switch *ds,
941 const struct devlink_region_ops *ops,
942 u32 region_max_snapshots, u64 region_size);
943 struct devlink_region *
944 dsa_devlink_port_region_create(struct dsa_switch *ds,
946 const struct devlink_port_region_ops *ops,
947 u32 region_max_snapshots, u64 region_size);
948 void dsa_devlink_region_destroy(struct devlink_region *region);
950 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
952 struct dsa_devlink_priv {
953 struct dsa_switch *ds;
956 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
958 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
964 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
966 struct devlink *dl = port->devlink;
967 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
972 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
977 struct dsa_switch_driver {
978 struct list_head list;
979 const struct dsa_switch_ops *ops;
982 struct net_device *dsa_dev_to_net_device(struct device *dev);
984 /* Keep inline for faster access in hot path */
985 static inline bool netdev_uses_dsa(const struct net_device *dev)
987 #if IS_ENABLED(CONFIG_NET_DSA)
988 return dev->dsa_ptr && dev->dsa_ptr->rcv;
993 /* All DSA tags that push the EtherType to the right (basically all except tail
994 * tags, which don't break dissection) can be treated the same from the
995 * perspective of the flow dissector.
998 * - offset: the (B - A) difference between:
999 * A. the position of the real EtherType and
1000 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1001 * after the normal EtherType was supposed to be)
1002 * The offset in bytes is exactly equal to the tagger overhead (and half of
1003 * that, in __be16 shorts).
1005 * - proto: the value of the real EtherType.
1007 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1008 __be16 *proto, int *offset)
1010 #if IS_ENABLED(CONFIG_NET_DSA)
1011 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1012 int tag_len = ops->needed_headroom;
1015 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1019 #if IS_ENABLED(CONFIG_NET_DSA)
1020 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1022 int err = -EOPNOTSUPP;
1027 if (!dev->dsa_ptr->netdev_ops)
1033 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1036 const struct dsa_netdevice_ops *ops;
1039 err = __dsa_netdevice_ops_check(dev);
1043 ops = dev->dsa_ptr->netdev_ops;
1045 return ops->ndo_eth_ioctl(dev, ifr, cmd);
1048 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1055 void dsa_unregister_switch(struct dsa_switch *ds);
1056 int dsa_register_switch(struct dsa_switch *ds);
1057 void dsa_switch_shutdown(struct dsa_switch *ds);
1058 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1059 #ifdef CONFIG_PM_SLEEP
1060 int dsa_switch_suspend(struct dsa_switch *ds);
1061 int dsa_switch_resume(struct dsa_switch *ds);
1063 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1067 static inline int dsa_switch_resume(struct dsa_switch *ds)
1071 #endif /* CONFIG_PM_SLEEP */
1073 #if IS_ENABLED(CONFIG_NET_DSA)
1074 bool dsa_slave_dev_check(const struct net_device *dev);
1076 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1082 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1083 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1084 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1085 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
1086 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1088 struct dsa_tag_driver {
1089 const struct dsa_device_ops *ops;
1090 struct list_head list;
1091 struct module *owner;
1094 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1096 struct module *owner);
1097 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1098 unsigned int count);
1100 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1101 static int __init dsa_tag_driver_module_init(void) \
1103 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1107 module_init(dsa_tag_driver_module_init); \
1109 static void __exit dsa_tag_driver_module_exit(void) \
1111 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1113 module_exit(dsa_tag_driver_module_exit)
1116 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1118 * @__ops_array: Array of tag driver strucutres
1120 * Helper macro for DSA tag drivers which do not do anything special
1121 * in module init/exit. Each module may only use this macro once, and
1122 * calling it replaces module_init() and module_exit().
1124 #define module_dsa_tag_drivers(__ops_array) \
1125 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1127 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1129 /* Create a static structure we can build a linked list of dsa_tag
1132 #define DSA_TAG_DRIVER(__ops) \
1133 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1138 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1140 * @__ops: Single tag driver structures
1142 * Helper macro for DSA tag drivers which do not do anything special
1143 * in module init/exit. Each module may only use this macro once, and
1144 * calling it replaces module_init() and module_exit().
1146 #define module_dsa_tag_driver(__ops) \
1147 DSA_TAG_DRIVER(__ops); \
1149 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1150 &DSA_TAG_DRIVER_NAME(__ops) \
1152 module_dsa_tag_drivers(dsa_tag_driver_array)