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
54 enum dsa_tag_protocol {
55 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
56 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
57 DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
58 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
59 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
60 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
61 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
62 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
63 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
64 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
65 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
66 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
67 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
68 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
69 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
70 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE,
71 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE,
72 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE,
73 DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE,
74 DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE,
75 DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE,
76 DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
77 DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE,
83 struct dsa_device_ops {
84 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
85 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
86 struct packet_type *pt);
87 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
89 /* Used to determine which traffic should match the DSA filter in
90 * eth_type_trans, and which, if any, should bypass it and be processed
91 * as regular on the master net device.
93 bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
94 unsigned int overhead;
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;
106 /* This structure defines the control interfaces that are overlayed by the
107 * DSA layer on top of the DSA CPU/management net_device instance. This is
108 * used by the core net_device layer while calling various net_device_ops
111 struct dsa_netdevice_ops {
112 int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr,
116 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
117 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \
118 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
120 struct dsa_switch_tree {
121 struct list_head list;
123 /* Notifier chain for switch-wide events */
124 struct raw_notifier_head nh;
126 /* Tree identifier */
129 /* Number of switches attached to this tree */
130 struct kref refcount;
132 /* Has this tree been applied to the hardware? */
135 /* Tagging protocol operations */
136 const struct dsa_device_ops *tag_ops;
138 /* Default tagging protocol preferred by the switches in this
141 enum dsa_tag_protocol default_proto;
144 * Configuration data for the platform device that owns
145 * this dsa switch tree instance.
147 struct dsa_platform_data *pd;
149 /* List of switch ports */
150 struct list_head ports;
152 /* List of DSA links composing the routing table */
153 struct list_head rtable;
155 /* Maps offloaded LAG netdevs to a zero-based linear ID for
156 * drivers that need it.
158 struct net_device **lags;
159 unsigned int lags_len;
162 #define dsa_lags_foreach_id(_id, _dst) \
163 for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++) \
164 if ((_dst)->lags[(_id)])
166 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
167 list_for_each_entry((_dp), &(_dst)->ports, list) \
168 if ((_dp)->lag_dev == (_lag))
170 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
171 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
172 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
174 static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
177 return dst->lags[id];
180 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
181 struct net_device *lag)
185 dsa_lags_foreach_id(id, dst) {
186 if (dsa_lag_dev(dst, id) == lag)
193 /* TC matchall action types */
194 enum dsa_port_mall_action_type {
195 DSA_PORT_MALL_MIRROR,
196 DSA_PORT_MALL_POLICER,
199 /* TC mirroring entry */
200 struct dsa_mall_mirror_tc_entry {
205 /* TC port policer entry */
206 struct dsa_mall_policer_tc_entry {
208 u64 rate_bytes_per_sec;
211 /* TC matchall entry */
212 struct dsa_mall_tc_entry {
213 struct list_head list;
214 unsigned long cookie;
215 enum dsa_port_mall_action_type type;
217 struct dsa_mall_mirror_tc_entry mirror;
218 struct dsa_mall_policer_tc_entry policer;
224 /* A CPU port is physically connected to a master device.
225 * A user port exposed to userspace has a slave device.
228 struct net_device *master;
229 struct net_device *slave;
232 /* Copy of the tagging protocol operations, for quicker access
233 * in the data path. Valid only for the CPU ports.
235 const struct dsa_device_ops *tag_ops;
237 /* Copies for faster access in master receive hot path */
238 struct dsa_switch_tree *dst;
239 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
240 struct packet_type *pt);
241 bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
244 DSA_PORT_TYPE_UNUSED = 0,
250 struct dsa_switch *ds;
253 struct dsa_port *cpu_dp;
255 struct device_node *dn;
256 unsigned int ageing_time;
259 struct net_device *bridge_dev;
260 struct devlink_port devlink_port;
261 bool devlink_port_setup;
263 struct phylink_config pl_config;
264 struct net_device *lag_dev;
266 struct net_device *hsr_dev;
268 struct list_head list;
271 * Give the switch driver somewhere to hang its per-port private data
272 * structures (accessible from the tagger).
277 * Original copy of the master netdev ethtool_ops
279 const struct ethtool_ops *orig_ethtool_ops;
282 * Original copy of the master netdev net_device_ops
284 const struct dsa_netdevice_ops *netdev_ops;
289 /* TODO: ideally DSA ports would have a single dp->link_dp member,
290 * and no dst->rtable nor this struct dsa_link would be needed,
291 * but this would require some more complex tree walking,
292 * so keep it stupid at the moment and list them all.
296 struct dsa_port *link_dp;
297 struct list_head list;
306 * Parent switch tree, and switch index.
308 struct dsa_switch_tree *dst;
311 /* Listener for switch fabric events */
312 struct notifier_block nb;
315 * Give the switch driver somewhere to hang its private data
321 * Configuration data for this switch.
323 struct dsa_chip_data *cd;
326 * The switch operations.
328 const struct dsa_switch_ops *ops;
331 * Slave mii_bus and devices for the individual ports.
334 struct mii_bus *slave_mii_bus;
336 /* Ageing Time limits in msecs */
337 unsigned int ageing_time_min;
338 unsigned int ageing_time_max;
340 /* devlink used to represent this switch device */
341 struct devlink *devlink;
343 /* Number of switch port queues */
344 unsigned int num_tx_queues;
346 /* Disallow bridge core from requesting different VLAN awareness
347 * settings on ports if not hardware-supported
349 bool vlan_filtering_is_global;
351 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
352 * that have vlan_filtering=0. All drivers should ideally set this (and
353 * then the option would get removed), but it is unknown whether this
354 * would break things or not.
356 bool configure_vlan_while_not_filtering;
358 /* If the switch driver always programs the CPU port as egress tagged
359 * despite the VLAN configuration indicating otherwise, then setting
360 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
361 * default_pvid VLAN tagged frames to offer a consistent behavior
362 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
364 bool untag_bridge_pvid;
366 /* Let DSA manage the FDB entries towards the CPU, based on the
367 * software bridge database.
369 bool assisted_learning_on_cpu_port;
371 /* In case vlan_filtering_is_global is set, the VLAN awareness state
372 * should be retrieved from here and not from the per-port settings.
376 /* MAC PCS does not provide link state change interrupt, and requires
377 * polling. Flag passed on to PHYLINK.
381 /* For switches that only have the MRU configurable. To ensure the
382 * configured MTU is not exceeded, normalization of MRU on all bridged
383 * interfaces is needed.
385 bool mtu_enforcement_ingress;
387 /* Drivers that benefit from having an ID associated with each
388 * offloaded LAG should set this to the maximum number of
389 * supported IDs. DSA will then maintain a mapping of _at
390 * least_ these many IDs, accessible to drivers via
393 unsigned int num_lag_ids;
398 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
400 struct dsa_switch_tree *dst = ds->dst;
403 list_for_each_entry(dp, &dst->ports, list)
404 if (dp->ds == ds && dp->index == p)
410 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
412 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
415 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
417 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
420 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
422 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
425 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
427 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
430 static inline u32 dsa_user_ports(struct dsa_switch *ds)
435 for (p = 0; p < ds->num_ports; p++)
436 if (dsa_is_user_port(ds, p))
442 /* Return the local port used to reach an arbitrary switch device */
443 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
445 struct dsa_switch_tree *dst = ds->dst;
448 list_for_each_entry(dl, &dst->rtable, list)
449 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
450 return dl->dp->index;
452 return ds->num_ports;
455 /* Return the local port used to reach an arbitrary switch port */
456 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
459 if (device == ds->index)
462 return dsa_routing_port(ds, device);
465 /* Return the local port used to reach the dedicated CPU port */
466 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
468 const struct dsa_port *dp = dsa_to_port(ds, port);
469 const struct dsa_port *cpu_dp = dp->cpu_dp;
474 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
477 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
479 const struct dsa_switch *ds = dp->ds;
481 if (ds->vlan_filtering_is_global)
482 return ds->vlan_filtering;
484 return dp->vlan_filtering;
488 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
495 else if (dp->hsr_dev)
501 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
502 bool is_static, void *data);
503 struct dsa_switch_ops {
505 * Tagging protocol helpers called for the CPU ports and DSA links.
506 * @get_tag_protocol retrieves the initial tagging protocol and is
507 * mandatory. Switches which can operate using multiple tagging
508 * protocols should implement @change_tag_protocol and report in
509 * @get_tag_protocol the tagger in current use.
511 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
513 enum dsa_tag_protocol mprot);
514 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
515 enum dsa_tag_protocol proto);
517 int (*setup)(struct dsa_switch *ds);
518 void (*teardown)(struct dsa_switch *ds);
519 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
522 * Access to the switch's PHY registers.
524 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
525 int (*phy_write)(struct dsa_switch *ds, int port,
526 int regnum, u16 val);
529 * Link state adjustment (called from libphy)
531 void (*adjust_link)(struct dsa_switch *ds, int port,
532 struct phy_device *phydev);
533 void (*fixed_link_update)(struct dsa_switch *ds, int port,
534 struct fixed_phy_status *st);
537 * PHYLINK integration
539 void (*phylink_validate)(struct dsa_switch *ds, int port,
540 unsigned long *supported,
541 struct phylink_link_state *state);
542 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
543 struct phylink_link_state *state);
544 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
546 const struct phylink_link_state *state);
547 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
548 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
550 phy_interface_t interface);
551 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
553 phy_interface_t interface,
554 struct phy_device *phydev,
555 int speed, int duplex,
556 bool tx_pause, bool rx_pause);
557 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
558 struct phylink_link_state *state);
560 * Port statistics counters.
562 void (*get_strings)(struct dsa_switch *ds, int port,
563 u32 stringset, uint8_t *data);
564 void (*get_ethtool_stats)(struct dsa_switch *ds,
565 int port, uint64_t *data);
566 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
567 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
568 int port, uint64_t *data);
569 void (*get_stats64)(struct dsa_switch *ds, int port,
570 struct rtnl_link_stats64 *s);
571 void (*self_test)(struct dsa_switch *ds, int port,
572 struct ethtool_test *etest, u64 *data);
575 * ethtool Wake-on-LAN
577 void (*get_wol)(struct dsa_switch *ds, int port,
578 struct ethtool_wolinfo *w);
579 int (*set_wol)(struct dsa_switch *ds, int port,
580 struct ethtool_wolinfo *w);
583 * ethtool timestamp info
585 int (*get_ts_info)(struct dsa_switch *ds, int port,
586 struct ethtool_ts_info *ts);
591 int (*suspend)(struct dsa_switch *ds);
592 int (*resume)(struct dsa_switch *ds);
595 * Port enable/disable
597 int (*port_enable)(struct dsa_switch *ds, int port,
598 struct phy_device *phy);
599 void (*port_disable)(struct dsa_switch *ds, int port);
602 * Port's MAC EEE settings
604 int (*set_mac_eee)(struct dsa_switch *ds, int port,
605 struct ethtool_eee *e);
606 int (*get_mac_eee)(struct dsa_switch *ds, int port,
607 struct ethtool_eee *e);
610 int (*get_eeprom_len)(struct dsa_switch *ds);
611 int (*get_eeprom)(struct dsa_switch *ds,
612 struct ethtool_eeprom *eeprom, u8 *data);
613 int (*set_eeprom)(struct dsa_switch *ds,
614 struct ethtool_eeprom *eeprom, u8 *data);
619 int (*get_regs_len)(struct dsa_switch *ds, int port);
620 void (*get_regs)(struct dsa_switch *ds, int port,
621 struct ethtool_regs *regs, void *p);
624 * Upper device tracking.
626 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
627 struct netdev_notifier_changeupper_info *info);
632 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
633 int (*port_bridge_join)(struct dsa_switch *ds, int port,
634 struct net_device *bridge);
635 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
636 struct net_device *bridge);
637 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
639 void (*port_fast_age)(struct dsa_switch *ds, int port);
640 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
641 struct switchdev_brport_flags flags,
642 struct netlink_ext_ack *extack);
643 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
644 struct switchdev_brport_flags flags,
645 struct netlink_ext_ack *extack);
646 int (*port_set_mrouter)(struct dsa_switch *ds, int port, bool mrouter,
647 struct netlink_ext_ack *extack);
652 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
654 struct netlink_ext_ack *extack);
655 int (*port_vlan_add)(struct dsa_switch *ds, int port,
656 const struct switchdev_obj_port_vlan *vlan,
657 struct netlink_ext_ack *extack);
658 int (*port_vlan_del)(struct dsa_switch *ds, int port,
659 const struct switchdev_obj_port_vlan *vlan);
661 * Forwarding database
663 int (*port_fdb_add)(struct dsa_switch *ds, int port,
664 const unsigned char *addr, u16 vid);
665 int (*port_fdb_del)(struct dsa_switch *ds, int port,
666 const unsigned char *addr, u16 vid);
667 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
668 dsa_fdb_dump_cb_t *cb, void *data);
673 int (*port_mdb_add)(struct dsa_switch *ds, int port,
674 const struct switchdev_obj_port_mdb *mdb);
675 int (*port_mdb_del)(struct dsa_switch *ds, int port,
676 const struct switchdev_obj_port_mdb *mdb);
680 int (*get_rxnfc)(struct dsa_switch *ds, int port,
681 struct ethtool_rxnfc *nfc, u32 *rule_locs);
682 int (*set_rxnfc)(struct dsa_switch *ds, int port,
683 struct ethtool_rxnfc *nfc);
688 int (*cls_flower_add)(struct dsa_switch *ds, int port,
689 struct flow_cls_offload *cls, bool ingress);
690 int (*cls_flower_del)(struct dsa_switch *ds, int port,
691 struct flow_cls_offload *cls, bool ingress);
692 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
693 struct flow_cls_offload *cls, bool ingress);
694 int (*port_mirror_add)(struct dsa_switch *ds, int port,
695 struct dsa_mall_mirror_tc_entry *mirror,
697 void (*port_mirror_del)(struct dsa_switch *ds, int port,
698 struct dsa_mall_mirror_tc_entry *mirror);
699 int (*port_policer_add)(struct dsa_switch *ds, int port,
700 struct dsa_mall_policer_tc_entry *policer);
701 void (*port_policer_del)(struct dsa_switch *ds, int port);
702 int (*port_setup_tc)(struct dsa_switch *ds, int port,
703 enum tc_setup_type type, void *type_data);
706 * Cross-chip operations
708 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
709 int sw_index, int port,
710 struct net_device *br);
711 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
712 int sw_index, int port,
713 struct net_device *br);
714 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
716 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
717 int port, struct net_device *lag,
718 struct netdev_lag_upper_info *info);
719 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
720 int port, struct net_device *lag);
725 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
727 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
729 void (*port_txtstamp)(struct dsa_switch *ds, int port,
730 struct sk_buff *skb);
731 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
732 struct sk_buff *skb, unsigned int type);
734 /* Devlink parameters, etc */
735 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
736 struct devlink_param_gset_ctx *ctx);
737 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
738 struct devlink_param_gset_ctx *ctx);
739 int (*devlink_info_get)(struct dsa_switch *ds,
740 struct devlink_info_req *req,
741 struct netlink_ext_ack *extack);
742 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
743 unsigned int sb_index, u16 pool_index,
744 struct devlink_sb_pool_info *pool_info);
745 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
746 u16 pool_index, u32 size,
747 enum devlink_sb_threshold_type threshold_type,
748 struct netlink_ext_ack *extack);
749 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
750 unsigned int sb_index, u16 pool_index,
752 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
753 unsigned int sb_index, u16 pool_index,
755 struct netlink_ext_ack *extack);
756 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
757 unsigned int sb_index, u16 tc_index,
758 enum devlink_sb_pool_type pool_type,
759 u16 *p_pool_index, u32 *p_threshold);
760 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
761 unsigned int sb_index, u16 tc_index,
762 enum devlink_sb_pool_type pool_type,
763 u16 pool_index, u32 threshold,
764 struct netlink_ext_ack *extack);
765 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
766 unsigned int sb_index);
767 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
768 unsigned int sb_index);
769 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
770 unsigned int sb_index, u16 pool_index,
771 u32 *p_cur, u32 *p_max);
772 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
773 unsigned int sb_index, u16 tc_index,
774 enum devlink_sb_pool_type pool_type,
775 u32 *p_cur, u32 *p_max);
778 * MTU change functionality. Switches can also adjust their MRU through
779 * this method. By MTU, one understands the SDU (L2 payload) length.
780 * If the switch needs to account for the DSA tag on the CPU port, this
781 * method needs to do so privately.
783 int (*port_change_mtu)(struct dsa_switch *ds, int port,
785 int (*port_max_mtu)(struct dsa_switch *ds, int port);
790 int (*port_lag_change)(struct dsa_switch *ds, int port);
791 int (*port_lag_join)(struct dsa_switch *ds, int port,
792 struct net_device *lag,
793 struct netdev_lag_upper_info *info);
794 int (*port_lag_leave)(struct dsa_switch *ds, int port,
795 struct net_device *lag);
800 int (*port_hsr_join)(struct dsa_switch *ds, int port,
801 struct net_device *hsr);
802 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
803 struct net_device *hsr);
808 int (*port_mrp_add)(struct dsa_switch *ds, int port,
809 const struct switchdev_obj_mrp *mrp);
810 int (*port_mrp_del)(struct dsa_switch *ds, int port,
811 const struct switchdev_obj_mrp *mrp);
812 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
813 const struct switchdev_obj_ring_role_mrp *mrp);
814 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
815 const struct switchdev_obj_ring_role_mrp *mrp);
818 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
819 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
820 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
822 int dsa_devlink_param_get(struct devlink *dl, u32 id,
823 struct devlink_param_gset_ctx *ctx);
824 int dsa_devlink_param_set(struct devlink *dl, u32 id,
825 struct devlink_param_gset_ctx *ctx);
826 int dsa_devlink_params_register(struct dsa_switch *ds,
827 const struct devlink_param *params,
828 size_t params_count);
829 void dsa_devlink_params_unregister(struct dsa_switch *ds,
830 const struct devlink_param *params,
831 size_t params_count);
832 int dsa_devlink_resource_register(struct dsa_switch *ds,
833 const char *resource_name,
836 u64 parent_resource_id,
837 const struct devlink_resource_size_params *size_params);
839 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
841 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
843 devlink_resource_occ_get_t *occ_get,
845 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
847 struct devlink_region *
848 dsa_devlink_region_create(struct dsa_switch *ds,
849 const struct devlink_region_ops *ops,
850 u32 region_max_snapshots, u64 region_size);
851 struct devlink_region *
852 dsa_devlink_port_region_create(struct dsa_switch *ds,
854 const struct devlink_port_region_ops *ops,
855 u32 region_max_snapshots, u64 region_size);
856 void dsa_devlink_region_destroy(struct devlink_region *region);
858 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
860 struct dsa_devlink_priv {
861 struct dsa_switch *ds;
864 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
866 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
872 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
874 struct devlink *dl = port->devlink;
875 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
880 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
885 struct dsa_switch_driver {
886 struct list_head list;
887 const struct dsa_switch_ops *ops;
890 struct net_device *dsa_dev_to_net_device(struct device *dev);
892 /* Keep inline for faster access in hot path */
893 static inline bool netdev_uses_dsa(const struct net_device *dev)
895 #if IS_ENABLED(CONFIG_NET_DSA)
896 return dev->dsa_ptr && dev->dsa_ptr->rcv;
901 static inline bool dsa_can_decode(const struct sk_buff *skb,
902 struct net_device *dev)
904 #if IS_ENABLED(CONFIG_NET_DSA)
905 return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
910 /* All DSA tags that push the EtherType to the right (basically all except tail
911 * tags, which don't break dissection) can be treated the same from the
912 * perspective of the flow dissector.
915 * - offset: the (B - A) difference between:
916 * A. the position of the real EtherType and
917 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
918 * after the normal EtherType was supposed to be)
919 * The offset in bytes is exactly equal to the tagger overhead (and half of
920 * that, in __be16 shorts).
922 * - proto: the value of the real EtherType.
924 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
925 __be16 *proto, int *offset)
927 #if IS_ENABLED(CONFIG_NET_DSA)
928 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
929 int tag_len = ops->overhead;
932 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
936 #if IS_ENABLED(CONFIG_NET_DSA)
937 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
939 int err = -EOPNOTSUPP;
944 if (!dev->dsa_ptr->netdev_ops)
950 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
953 const struct dsa_netdevice_ops *ops;
956 err = __dsa_netdevice_ops_check(dev);
960 ops = dev->dsa_ptr->netdev_ops;
962 return ops->ndo_do_ioctl(dev, ifr, cmd);
965 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
972 void dsa_unregister_switch(struct dsa_switch *ds);
973 int dsa_register_switch(struct dsa_switch *ds);
974 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
975 #ifdef CONFIG_PM_SLEEP
976 int dsa_switch_suspend(struct dsa_switch *ds);
977 int dsa_switch_resume(struct dsa_switch *ds);
979 static inline int dsa_switch_suspend(struct dsa_switch *ds)
983 static inline int dsa_switch_resume(struct dsa_switch *ds)
987 #endif /* CONFIG_PM_SLEEP */
989 #if IS_ENABLED(CONFIG_NET_DSA)
990 bool dsa_slave_dev_check(const struct net_device *dev);
992 static inline bool dsa_slave_dev_check(const struct net_device *dev)
998 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
999 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1000 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1001 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
1002 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1004 struct dsa_tag_driver {
1005 const struct dsa_device_ops *ops;
1006 struct list_head list;
1007 struct module *owner;
1010 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1012 struct module *owner);
1013 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1014 unsigned int count);
1016 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1017 static int __init dsa_tag_driver_module_init(void) \
1019 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1023 module_init(dsa_tag_driver_module_init); \
1025 static void __exit dsa_tag_driver_module_exit(void) \
1027 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1029 module_exit(dsa_tag_driver_module_exit)
1032 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1034 * @__ops_array: Array of tag driver strucutres
1036 * Helper macro for DSA tag drivers which do not do anything special
1037 * in module init/exit. Each module may only use this macro once, and
1038 * calling it replaces module_init() and module_exit().
1040 #define module_dsa_tag_drivers(__ops_array) \
1041 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1043 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1045 /* Create a static structure we can build a linked list of dsa_tag
1048 #define DSA_TAG_DRIVER(__ops) \
1049 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1054 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1056 * @__ops: Single tag driver structures
1058 * Helper macro for DSA tag drivers which do not do anything special
1059 * in module init/exit. Each module may only use this macro once, and
1060 * calling it replaces module_init() and module_exit().
1062 #define module_dsa_tag_driver(__ops) \
1063 DSA_TAG_DRIVER(__ops); \
1065 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1066 &DSA_TAG_DRIVER_NAME(__ops) \
1068 module_dsa_tag_drivers(dsa_tag_driver_array)