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))
121 struct sk_buff *clone;
124 struct __dsa_skb_cb {
125 struct dsa_skb_cb cb;
126 u8 priv[48 - sizeof(struct dsa_skb_cb)];
129 #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
131 #define DSA_SKB_CB_PRIV(skb) \
132 ((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
134 struct dsa_switch_tree {
135 struct list_head list;
137 /* Notifier chain for switch-wide events */
138 struct raw_notifier_head nh;
140 /* Tree identifier */
143 /* Number of switches attached to this tree */
144 struct kref refcount;
146 /* Has this tree been applied to the hardware? */
149 /* Tagging protocol operations */
150 const struct dsa_device_ops *tag_ops;
153 * Configuration data for the platform device that owns
154 * this dsa switch tree instance.
156 struct dsa_platform_data *pd;
158 /* List of switch ports */
159 struct list_head ports;
161 /* List of DSA links composing the routing table */
162 struct list_head rtable;
164 /* Maps offloaded LAG netdevs to a zero-based linear ID for
165 * drivers that need it.
167 struct net_device **lags;
168 unsigned int lags_len;
171 #define dsa_lags_foreach_id(_id, _dst) \
172 for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++) \
173 if ((_dst)->lags[(_id)])
175 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
176 list_for_each_entry((_dp), &(_dst)->ports, list) \
177 if ((_dp)->lag_dev == (_lag))
179 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
180 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
181 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
183 static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
186 return dst->lags[id];
189 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
190 struct net_device *lag)
194 dsa_lags_foreach_id(id, dst) {
195 if (dsa_lag_dev(dst, id) == lag)
202 /* TC matchall action types */
203 enum dsa_port_mall_action_type {
204 DSA_PORT_MALL_MIRROR,
205 DSA_PORT_MALL_POLICER,
208 /* TC mirroring entry */
209 struct dsa_mall_mirror_tc_entry {
214 /* TC port policer entry */
215 struct dsa_mall_policer_tc_entry {
217 u64 rate_bytes_per_sec;
220 /* TC matchall entry */
221 struct dsa_mall_tc_entry {
222 struct list_head list;
223 unsigned long cookie;
224 enum dsa_port_mall_action_type type;
226 struct dsa_mall_mirror_tc_entry mirror;
227 struct dsa_mall_policer_tc_entry policer;
233 /* A CPU port is physically connected to a master device.
234 * A user port exposed to userspace has a slave device.
237 struct net_device *master;
238 struct net_device *slave;
241 /* Copy of the tagging protocol operations, for quicker access
242 * in the data path. Valid only for the CPU ports.
244 const struct dsa_device_ops *tag_ops;
246 /* Copies for faster access in master receive hot path */
247 struct dsa_switch_tree *dst;
248 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
249 struct packet_type *pt);
250 bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
253 DSA_PORT_TYPE_UNUSED = 0,
259 struct dsa_switch *ds;
262 struct dsa_port *cpu_dp;
264 struct device_node *dn;
265 unsigned int ageing_time;
268 struct net_device *bridge_dev;
269 struct devlink_port devlink_port;
270 bool devlink_port_setup;
272 struct phylink_config pl_config;
273 struct net_device *lag_dev;
275 struct net_device *hsr_dev;
277 struct list_head list;
280 * Give the switch driver somewhere to hang its per-port private data
281 * structures (accessible from the tagger).
286 * Original copy of the master netdev ethtool_ops
288 const struct ethtool_ops *orig_ethtool_ops;
291 * Original copy of the master netdev net_device_ops
293 const struct dsa_netdevice_ops *netdev_ops;
298 /* TODO: ideally DSA ports would have a single dp->link_dp member,
299 * and no dst->rtable nor this struct dsa_link would be needed,
300 * but this would require some more complex tree walking,
301 * so keep it stupid at the moment and list them all.
305 struct dsa_port *link_dp;
306 struct list_head list;
315 * Parent switch tree, and switch index.
317 struct dsa_switch_tree *dst;
320 /* Listener for switch fabric events */
321 struct notifier_block nb;
324 * Give the switch driver somewhere to hang its private data
330 * Configuration data for this switch.
332 struct dsa_chip_data *cd;
335 * The switch operations.
337 const struct dsa_switch_ops *ops;
340 * Slave mii_bus and devices for the individual ports.
343 struct mii_bus *slave_mii_bus;
345 /* Ageing Time limits in msecs */
346 unsigned int ageing_time_min;
347 unsigned int ageing_time_max;
349 /* devlink used to represent this switch device */
350 struct devlink *devlink;
352 /* Number of switch port queues */
353 unsigned int num_tx_queues;
355 /* Disallow bridge core from requesting different VLAN awareness
356 * settings on ports if not hardware-supported
358 bool vlan_filtering_is_global;
360 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
361 * that have vlan_filtering=0. All drivers should ideally set this (and
362 * then the option would get removed), but it is unknown whether this
363 * would break things or not.
365 bool configure_vlan_while_not_filtering;
367 /* If the switch driver always programs the CPU port as egress tagged
368 * despite the VLAN configuration indicating otherwise, then setting
369 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
370 * default_pvid VLAN tagged frames to offer a consistent behavior
371 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
373 bool untag_bridge_pvid;
375 /* Let DSA manage the FDB entries towards the CPU, based on the
376 * software bridge database.
378 bool assisted_learning_on_cpu_port;
380 /* In case vlan_filtering_is_global is set, the VLAN awareness state
381 * should be retrieved from here and not from the per-port settings.
385 /* MAC PCS does not provide link state change interrupt, and requires
386 * polling. Flag passed on to PHYLINK.
390 /* For switches that only have the MRU configurable. To ensure the
391 * configured MTU is not exceeded, normalization of MRU on all bridged
392 * interfaces is needed.
394 bool mtu_enforcement_ingress;
396 /* Drivers that benefit from having an ID associated with each
397 * offloaded LAG should set this to the maximum number of
398 * supported IDs. DSA will then maintain a mapping of _at
399 * least_ these many IDs, accessible to drivers via
402 unsigned int num_lag_ids;
407 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
409 struct dsa_switch_tree *dst = ds->dst;
412 list_for_each_entry(dp, &dst->ports, list)
413 if (dp->ds == ds && dp->index == p)
419 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
421 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
424 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
426 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
429 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
431 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
434 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
436 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
439 static inline u32 dsa_user_ports(struct dsa_switch *ds)
444 for (p = 0; p < ds->num_ports; p++)
445 if (dsa_is_user_port(ds, p))
451 /* Return the local port used to reach an arbitrary switch device */
452 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
454 struct dsa_switch_tree *dst = ds->dst;
457 list_for_each_entry(dl, &dst->rtable, list)
458 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
459 return dl->dp->index;
461 return ds->num_ports;
464 /* Return the local port used to reach an arbitrary switch port */
465 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
468 if (device == ds->index)
471 return dsa_routing_port(ds, device);
474 /* Return the local port used to reach the dedicated CPU port */
475 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
477 const struct dsa_port *dp = dsa_to_port(ds, port);
478 const struct dsa_port *cpu_dp = dp->cpu_dp;
483 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
486 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
488 const struct dsa_switch *ds = dp->ds;
490 if (ds->vlan_filtering_is_global)
491 return ds->vlan_filtering;
493 return dp->vlan_filtering;
497 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
504 else if (dp->hsr_dev)
510 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
511 bool is_static, void *data);
512 struct dsa_switch_ops {
514 * Tagging protocol helpers called for the CPU ports and DSA links.
515 * @get_tag_protocol retrieves the initial tagging protocol and is
516 * mandatory. Switches which can operate using multiple tagging
517 * protocols should implement @change_tag_protocol and report in
518 * @get_tag_protocol the tagger in current use.
520 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
522 enum dsa_tag_protocol mprot);
523 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
524 enum dsa_tag_protocol proto);
526 int (*setup)(struct dsa_switch *ds);
527 void (*teardown)(struct dsa_switch *ds);
528 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
531 * Access to the switch's PHY registers.
533 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
534 int (*phy_write)(struct dsa_switch *ds, int port,
535 int regnum, u16 val);
538 * Link state adjustment (called from libphy)
540 void (*adjust_link)(struct dsa_switch *ds, int port,
541 struct phy_device *phydev);
542 void (*fixed_link_update)(struct dsa_switch *ds, int port,
543 struct fixed_phy_status *st);
546 * PHYLINK integration
548 void (*phylink_validate)(struct dsa_switch *ds, int port,
549 unsigned long *supported,
550 struct phylink_link_state *state);
551 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
552 struct phylink_link_state *state);
553 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
555 const struct phylink_link_state *state);
556 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
557 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
559 phy_interface_t interface);
560 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
562 phy_interface_t interface,
563 struct phy_device *phydev,
564 int speed, int duplex,
565 bool tx_pause, bool rx_pause);
566 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
567 struct phylink_link_state *state);
569 * Port statistics counters.
571 void (*get_strings)(struct dsa_switch *ds, int port,
572 u32 stringset, uint8_t *data);
573 void (*get_ethtool_stats)(struct dsa_switch *ds,
574 int port, uint64_t *data);
575 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
576 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
577 int port, uint64_t *data);
578 void (*get_stats64)(struct dsa_switch *ds, int port,
579 struct rtnl_link_stats64 *s);
582 * ethtool Wake-on-LAN
584 void (*get_wol)(struct dsa_switch *ds, int port,
585 struct ethtool_wolinfo *w);
586 int (*set_wol)(struct dsa_switch *ds, int port,
587 struct ethtool_wolinfo *w);
590 * ethtool timestamp info
592 int (*get_ts_info)(struct dsa_switch *ds, int port,
593 struct ethtool_ts_info *ts);
598 int (*suspend)(struct dsa_switch *ds);
599 int (*resume)(struct dsa_switch *ds);
602 * Port enable/disable
604 int (*port_enable)(struct dsa_switch *ds, int port,
605 struct phy_device *phy);
606 void (*port_disable)(struct dsa_switch *ds, int port);
609 * Port's MAC EEE settings
611 int (*set_mac_eee)(struct dsa_switch *ds, int port,
612 struct ethtool_eee *e);
613 int (*get_mac_eee)(struct dsa_switch *ds, int port,
614 struct ethtool_eee *e);
617 int (*get_eeprom_len)(struct dsa_switch *ds);
618 int (*get_eeprom)(struct dsa_switch *ds,
619 struct ethtool_eeprom *eeprom, u8 *data);
620 int (*set_eeprom)(struct dsa_switch *ds,
621 struct ethtool_eeprom *eeprom, u8 *data);
626 int (*get_regs_len)(struct dsa_switch *ds, int port);
627 void (*get_regs)(struct dsa_switch *ds, int port,
628 struct ethtool_regs *regs, void *p);
631 * Upper device tracking.
633 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
634 struct netdev_notifier_changeupper_info *info);
639 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
640 int (*port_bridge_join)(struct dsa_switch *ds, int port,
641 struct net_device *bridge);
642 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
643 struct net_device *bridge);
644 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
646 void (*port_fast_age)(struct dsa_switch *ds, int port);
647 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
648 struct switchdev_brport_flags flags,
649 struct netlink_ext_ack *extack);
650 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
651 struct switchdev_brport_flags flags,
652 struct netlink_ext_ack *extack);
653 int (*port_set_mrouter)(struct dsa_switch *ds, int port, bool mrouter,
654 struct netlink_ext_ack *extack);
659 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
661 struct netlink_ext_ack *extack);
662 int (*port_vlan_add)(struct dsa_switch *ds, int port,
663 const struct switchdev_obj_port_vlan *vlan,
664 struct netlink_ext_ack *extack);
665 int (*port_vlan_del)(struct dsa_switch *ds, int port,
666 const struct switchdev_obj_port_vlan *vlan);
668 * Forwarding database
670 int (*port_fdb_add)(struct dsa_switch *ds, int port,
671 const unsigned char *addr, u16 vid);
672 int (*port_fdb_del)(struct dsa_switch *ds, int port,
673 const unsigned char *addr, u16 vid);
674 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
675 dsa_fdb_dump_cb_t *cb, void *data);
680 int (*port_mdb_add)(struct dsa_switch *ds, int port,
681 const struct switchdev_obj_port_mdb *mdb);
682 int (*port_mdb_del)(struct dsa_switch *ds, int port,
683 const struct switchdev_obj_port_mdb *mdb);
687 int (*get_rxnfc)(struct dsa_switch *ds, int port,
688 struct ethtool_rxnfc *nfc, u32 *rule_locs);
689 int (*set_rxnfc)(struct dsa_switch *ds, int port,
690 struct ethtool_rxnfc *nfc);
695 int (*cls_flower_add)(struct dsa_switch *ds, int port,
696 struct flow_cls_offload *cls, bool ingress);
697 int (*cls_flower_del)(struct dsa_switch *ds, int port,
698 struct flow_cls_offload *cls, bool ingress);
699 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
700 struct flow_cls_offload *cls, bool ingress);
701 int (*port_mirror_add)(struct dsa_switch *ds, int port,
702 struct dsa_mall_mirror_tc_entry *mirror,
704 void (*port_mirror_del)(struct dsa_switch *ds, int port,
705 struct dsa_mall_mirror_tc_entry *mirror);
706 int (*port_policer_add)(struct dsa_switch *ds, int port,
707 struct dsa_mall_policer_tc_entry *policer);
708 void (*port_policer_del)(struct dsa_switch *ds, int port);
709 int (*port_setup_tc)(struct dsa_switch *ds, int port,
710 enum tc_setup_type type, void *type_data);
713 * Cross-chip operations
715 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
716 int sw_index, int port,
717 struct net_device *br);
718 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
719 int sw_index, int port,
720 struct net_device *br);
721 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
723 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
724 int port, struct net_device *lag,
725 struct netdev_lag_upper_info *info);
726 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
727 int port, struct net_device *lag);
732 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
734 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
736 bool (*port_txtstamp)(struct dsa_switch *ds, int port,
737 struct sk_buff *clone, unsigned int type);
738 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
739 struct sk_buff *skb, unsigned int type);
741 /* Devlink parameters, etc */
742 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
743 struct devlink_param_gset_ctx *ctx);
744 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
745 struct devlink_param_gset_ctx *ctx);
746 int (*devlink_info_get)(struct dsa_switch *ds,
747 struct devlink_info_req *req,
748 struct netlink_ext_ack *extack);
749 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
750 unsigned int sb_index, u16 pool_index,
751 struct devlink_sb_pool_info *pool_info);
752 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
753 u16 pool_index, u32 size,
754 enum devlink_sb_threshold_type threshold_type,
755 struct netlink_ext_ack *extack);
756 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
757 unsigned int sb_index, u16 pool_index,
759 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
760 unsigned int sb_index, u16 pool_index,
762 struct netlink_ext_ack *extack);
763 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
764 unsigned int sb_index, u16 tc_index,
765 enum devlink_sb_pool_type pool_type,
766 u16 *p_pool_index, u32 *p_threshold);
767 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
768 unsigned int sb_index, u16 tc_index,
769 enum devlink_sb_pool_type pool_type,
770 u16 pool_index, u32 threshold,
771 struct netlink_ext_ack *extack);
772 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
773 unsigned int sb_index);
774 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
775 unsigned int sb_index);
776 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
777 unsigned int sb_index, u16 pool_index,
778 u32 *p_cur, u32 *p_max);
779 int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
780 unsigned int sb_index, u16 tc_index,
781 enum devlink_sb_pool_type pool_type,
782 u32 *p_cur, u32 *p_max);
785 * MTU change functionality. Switches can also adjust their MRU through
786 * this method. By MTU, one understands the SDU (L2 payload) length.
787 * If the switch needs to account for the DSA tag on the CPU port, this
788 * method needs to do so privately.
790 int (*port_change_mtu)(struct dsa_switch *ds, int port,
792 int (*port_max_mtu)(struct dsa_switch *ds, int port);
797 int (*port_lag_change)(struct dsa_switch *ds, int port);
798 int (*port_lag_join)(struct dsa_switch *ds, int port,
799 struct net_device *lag,
800 struct netdev_lag_upper_info *info);
801 int (*port_lag_leave)(struct dsa_switch *ds, int port,
802 struct net_device *lag);
807 int (*port_hsr_join)(struct dsa_switch *ds, int port,
808 struct net_device *hsr);
809 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
810 struct net_device *hsr);
815 int (*port_mrp_add)(struct dsa_switch *ds, int port,
816 const struct switchdev_obj_mrp *mrp);
817 int (*port_mrp_del)(struct dsa_switch *ds, int port,
818 const struct switchdev_obj_mrp *mrp);
819 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
820 const struct switchdev_obj_ring_role_mrp *mrp);
821 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
822 const struct switchdev_obj_ring_role_mrp *mrp);
825 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
826 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
827 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
829 int dsa_devlink_param_get(struct devlink *dl, u32 id,
830 struct devlink_param_gset_ctx *ctx);
831 int dsa_devlink_param_set(struct devlink *dl, u32 id,
832 struct devlink_param_gset_ctx *ctx);
833 int dsa_devlink_params_register(struct dsa_switch *ds,
834 const struct devlink_param *params,
835 size_t params_count);
836 void dsa_devlink_params_unregister(struct dsa_switch *ds,
837 const struct devlink_param *params,
838 size_t params_count);
839 int dsa_devlink_resource_register(struct dsa_switch *ds,
840 const char *resource_name,
843 u64 parent_resource_id,
844 const struct devlink_resource_size_params *size_params);
846 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
848 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
850 devlink_resource_occ_get_t *occ_get,
852 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
854 struct devlink_region *
855 dsa_devlink_region_create(struct dsa_switch *ds,
856 const struct devlink_region_ops *ops,
857 u32 region_max_snapshots, u64 region_size);
858 struct devlink_region *
859 dsa_devlink_port_region_create(struct dsa_switch *ds,
861 const struct devlink_port_region_ops *ops,
862 u32 region_max_snapshots, u64 region_size);
863 void dsa_devlink_region_destroy(struct devlink_region *region);
865 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
867 struct dsa_devlink_priv {
868 struct dsa_switch *ds;
871 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
873 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
879 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
881 struct devlink *dl = port->devlink;
882 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
887 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
892 struct dsa_switch_driver {
893 struct list_head list;
894 const struct dsa_switch_ops *ops;
897 struct net_device *dsa_dev_to_net_device(struct device *dev);
899 /* Keep inline for faster access in hot path */
900 static inline bool netdev_uses_dsa(const struct net_device *dev)
902 #if IS_ENABLED(CONFIG_NET_DSA)
903 return dev->dsa_ptr && dev->dsa_ptr->rcv;
908 static inline bool dsa_can_decode(const struct sk_buff *skb,
909 struct net_device *dev)
911 #if IS_ENABLED(CONFIG_NET_DSA)
912 return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
917 /* All DSA tags that push the EtherType to the right (basically all except tail
918 * tags, which don't break dissection) can be treated the same from the
919 * perspective of the flow dissector.
922 * - offset: the (B - A) difference between:
923 * A. the position of the real EtherType and
924 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
925 * after the normal EtherType was supposed to be)
926 * The offset in bytes is exactly equal to the tagger overhead (and half of
927 * that, in __be16 shorts).
929 * - proto: the value of the real EtherType.
931 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
932 __be16 *proto, int *offset)
934 #if IS_ENABLED(CONFIG_NET_DSA)
935 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
936 int tag_len = ops->overhead;
939 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
943 #if IS_ENABLED(CONFIG_NET_DSA)
944 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
946 int err = -EOPNOTSUPP;
951 if (!dev->dsa_ptr->netdev_ops)
957 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
960 const struct dsa_netdevice_ops *ops;
963 err = __dsa_netdevice_ops_check(dev);
967 ops = dev->dsa_ptr->netdev_ops;
969 return ops->ndo_do_ioctl(dev, ifr, cmd);
972 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
979 void dsa_unregister_switch(struct dsa_switch *ds);
980 int dsa_register_switch(struct dsa_switch *ds);
981 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
982 #ifdef CONFIG_PM_SLEEP
983 int dsa_switch_suspend(struct dsa_switch *ds);
984 int dsa_switch_resume(struct dsa_switch *ds);
986 static inline int dsa_switch_suspend(struct dsa_switch *ds)
990 static inline int dsa_switch_resume(struct dsa_switch *ds)
994 #endif /* CONFIG_PM_SLEEP */
996 #if IS_ENABLED(CONFIG_NET_DSA)
997 bool dsa_slave_dev_check(const struct net_device *dev);
999 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1005 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1006 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1007 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1008 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
1009 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1011 struct dsa_tag_driver {
1012 const struct dsa_device_ops *ops;
1013 struct list_head list;
1014 struct module *owner;
1017 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1019 struct module *owner);
1020 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1021 unsigned int count);
1023 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1024 static int __init dsa_tag_driver_module_init(void) \
1026 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1030 module_init(dsa_tag_driver_module_init); \
1032 static void __exit dsa_tag_driver_module_exit(void) \
1034 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1036 module_exit(dsa_tag_driver_module_exit)
1039 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1041 * @__ops_array: Array of tag driver strucutres
1043 * Helper macro for DSA tag drivers which do not do anything special
1044 * in module init/exit. Each module may only use this macro once, and
1045 * calling it replaces module_init() and module_exit().
1047 #define module_dsa_tag_drivers(__ops_array) \
1048 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1050 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1052 /* Create a static structure we can build a linked list of dsa_tag
1055 #define DSA_TAG_DRIVER(__ops) \
1056 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1061 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1063 * @__ops: Single tag driver structures
1065 * Helper macro for DSA tag drivers which do not do anything special
1066 * in module init/exit. Each module may only use this macro once, and
1067 * calling it replaces module_init() and module_exit().
1069 #define module_dsa_tag_driver(__ops) \
1070 DSA_TAG_DRIVER(__ops); \
1072 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1073 &DSA_TAG_DRIVER_NAME(__ops) \
1075 module_dsa_tag_drivers(dsa_tag_driver_array)