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
53 enum dsa_tag_protocol {
54 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
55 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
56 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
57 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
58 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
59 DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
60 DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
61 DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
62 DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
63 DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
64 DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
65 DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
66 DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
67 DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
68 DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE,
69 DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE,
70 DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE,
71 DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE,
72 DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE,
73 DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE,
74 DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
75 DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE,
81 struct dsa_device_ops {
82 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
83 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
84 struct packet_type *pt);
85 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
87 /* Used to determine which traffic should match the DSA filter in
88 * eth_type_trans, and which, if any, should bypass it and be processed
89 * as regular on the master net device.
91 bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
92 unsigned int overhead;
94 enum dsa_tag_protocol proto;
95 /* Some tagging protocols either mangle or shift the destination MAC
96 * address, in which case the DSA master would drop packets on ingress
97 * if what it understands out of the destination MAC address is not in
100 bool promisc_on_master;
104 /* This structure defines the control interfaces that are overlayed by the
105 * DSA layer on top of the DSA CPU/management net_device instance. This is
106 * used by the core net_device layer while calling various net_device_ops
109 struct dsa_netdevice_ops {
110 int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr,
114 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
115 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto) \
116 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
119 struct sk_buff *clone;
122 struct __dsa_skb_cb {
123 struct dsa_skb_cb cb;
124 u8 priv[48 - sizeof(struct dsa_skb_cb)];
127 #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
129 #define DSA_SKB_CB_PRIV(skb) \
130 ((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
132 struct dsa_switch_tree {
133 struct list_head list;
135 /* Notifier chain for switch-wide events */
136 struct raw_notifier_head nh;
138 /* Tree identifier */
141 /* Number of switches attached to this tree */
142 struct kref refcount;
144 /* Has this tree been applied to the hardware? */
147 /* Tagging protocol operations */
148 const struct dsa_device_ops *tag_ops;
151 * Configuration data for the platform device that owns
152 * this dsa switch tree instance.
154 struct dsa_platform_data *pd;
156 /* List of switch ports */
157 struct list_head ports;
159 /* List of DSA links composing the routing table */
160 struct list_head rtable;
162 /* Maps offloaded LAG netdevs to a zero-based linear ID for
163 * drivers that need it.
165 struct net_device **lags;
166 unsigned int lags_len;
169 #define dsa_lags_foreach_id(_id, _dst) \
170 for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++) \
171 if ((_dst)->lags[(_id)])
173 #define dsa_lag_foreach_port(_dp, _dst, _lag) \
174 list_for_each_entry((_dp), &(_dst)->ports, list) \
175 if ((_dp)->lag_dev == (_lag))
177 #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
178 list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
179 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
181 static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
184 return dst->lags[id];
187 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
188 struct net_device *lag)
192 dsa_lags_foreach_id(id, dst) {
193 if (dsa_lag_dev(dst, id) == lag)
200 /* TC matchall action types */
201 enum dsa_port_mall_action_type {
202 DSA_PORT_MALL_MIRROR,
203 DSA_PORT_MALL_POLICER,
206 /* TC mirroring entry */
207 struct dsa_mall_mirror_tc_entry {
212 /* TC port policer entry */
213 struct dsa_mall_policer_tc_entry {
215 u64 rate_bytes_per_sec;
218 /* TC matchall entry */
219 struct dsa_mall_tc_entry {
220 struct list_head list;
221 unsigned long cookie;
222 enum dsa_port_mall_action_type type;
224 struct dsa_mall_mirror_tc_entry mirror;
225 struct dsa_mall_policer_tc_entry policer;
231 /* A CPU port is physically connected to a master device.
232 * A user port exposed to userspace has a slave device.
235 struct net_device *master;
236 struct net_device *slave;
239 /* Copy of the tagging protocol operations, for quicker access
240 * in the data path. Valid only for the CPU ports.
242 const struct dsa_device_ops *tag_ops;
244 /* Copies for faster access in master receive hot path */
245 struct dsa_switch_tree *dst;
246 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
247 struct packet_type *pt);
248 bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
251 DSA_PORT_TYPE_UNUSED = 0,
257 struct dsa_switch *ds;
260 struct dsa_port *cpu_dp;
262 struct device_node *dn;
263 unsigned int ageing_time;
266 struct net_device *bridge_dev;
267 struct devlink_port devlink_port;
268 bool devlink_port_setup;
270 struct phylink_config pl_config;
271 struct net_device *lag_dev;
273 struct net_device *hsr_dev;
275 struct list_head list;
278 * Give the switch driver somewhere to hang its per-port private data
279 * structures (accessible from the tagger).
284 * Original copy of the master netdev ethtool_ops
286 const struct ethtool_ops *orig_ethtool_ops;
289 * Original copy of the master netdev net_device_ops
291 const struct dsa_netdevice_ops *netdev_ops;
296 /* TODO: ideally DSA ports would have a single dp->link_dp member,
297 * and no dst->rtable nor this struct dsa_link would be needed,
298 * but this would require some more complex tree walking,
299 * so keep it stupid at the moment and list them all.
303 struct dsa_port *link_dp;
304 struct list_head list;
313 * Parent switch tree, and switch index.
315 struct dsa_switch_tree *dst;
318 /* Listener for switch fabric events */
319 struct notifier_block nb;
322 * Give the switch driver somewhere to hang its private data
328 * Configuration data for this switch.
330 struct dsa_chip_data *cd;
333 * The switch operations.
335 const struct dsa_switch_ops *ops;
338 * Slave mii_bus and devices for the individual ports.
341 struct mii_bus *slave_mii_bus;
343 /* Ageing Time limits in msecs */
344 unsigned int ageing_time_min;
345 unsigned int ageing_time_max;
347 /* devlink used to represent this switch device */
348 struct devlink *devlink;
350 /* Number of switch port queues */
351 unsigned int num_tx_queues;
353 /* Disallow bridge core from requesting different VLAN awareness
354 * settings on ports if not hardware-supported
356 bool vlan_filtering_is_global;
358 /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
359 * that have vlan_filtering=0. All drivers should ideally set this (and
360 * then the option would get removed), but it is unknown whether this
361 * would break things or not.
363 bool configure_vlan_while_not_filtering;
365 /* If the switch driver always programs the CPU port as egress tagged
366 * despite the VLAN configuration indicating otherwise, then setting
367 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
368 * default_pvid VLAN tagged frames to offer a consistent behavior
369 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
371 bool untag_bridge_pvid;
373 /* Let DSA manage the FDB entries towards the CPU, based on the
374 * software bridge database.
376 bool assisted_learning_on_cpu_port;
378 /* In case vlan_filtering_is_global is set, the VLAN awareness state
379 * should be retrieved from here and not from the per-port settings.
383 /* MAC PCS does not provide link state change interrupt, and requires
384 * polling. Flag passed on to PHYLINK.
388 /* For switches that only have the MRU configurable. To ensure the
389 * configured MTU is not exceeded, normalization of MRU on all bridged
390 * interfaces is needed.
392 bool mtu_enforcement_ingress;
394 /* Drivers that benefit from having an ID associated with each
395 * offloaded LAG should set this to the maximum number of
396 * supported IDs. DSA will then maintain a mapping of _at
397 * least_ these many IDs, accessible to drivers via
400 unsigned int num_lag_ids;
405 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
407 struct dsa_switch_tree *dst = ds->dst;
410 list_for_each_entry(dp, &dst->ports, list)
411 if (dp->ds == ds && dp->index == p)
417 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
419 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
422 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
424 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
427 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
429 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
432 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
434 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
437 static inline u32 dsa_user_ports(struct dsa_switch *ds)
442 for (p = 0; p < ds->num_ports; p++)
443 if (dsa_is_user_port(ds, p))
449 /* Return the local port used to reach an arbitrary switch device */
450 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
452 struct dsa_switch_tree *dst = ds->dst;
455 list_for_each_entry(dl, &dst->rtable, list)
456 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
457 return dl->dp->index;
459 return ds->num_ports;
462 /* Return the local port used to reach an arbitrary switch port */
463 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
466 if (device == ds->index)
469 return dsa_routing_port(ds, device);
472 /* Return the local port used to reach the dedicated CPU port */
473 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
475 const struct dsa_port *dp = dsa_to_port(ds, port);
476 const struct dsa_port *cpu_dp = dp->cpu_dp;
481 return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
484 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
486 const struct dsa_switch *ds = dp->ds;
488 if (ds->vlan_filtering_is_global)
489 return ds->vlan_filtering;
491 return dp->vlan_filtering;
494 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
495 bool is_static, void *data);
496 struct dsa_switch_ops {
498 * Tagging protocol helpers called for the CPU ports and DSA links.
499 * @get_tag_protocol retrieves the initial tagging protocol and is
500 * mandatory. Switches which can operate using multiple tagging
501 * protocols should implement @change_tag_protocol and report in
502 * @get_tag_protocol the tagger in current use.
504 enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
506 enum dsa_tag_protocol mprot);
507 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
508 enum dsa_tag_protocol proto);
510 int (*setup)(struct dsa_switch *ds);
511 void (*teardown)(struct dsa_switch *ds);
512 u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
515 * Access to the switch's PHY registers.
517 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
518 int (*phy_write)(struct dsa_switch *ds, int port,
519 int regnum, u16 val);
522 * Link state adjustment (called from libphy)
524 void (*adjust_link)(struct dsa_switch *ds, int port,
525 struct phy_device *phydev);
526 void (*fixed_link_update)(struct dsa_switch *ds, int port,
527 struct fixed_phy_status *st);
530 * PHYLINK integration
532 void (*phylink_validate)(struct dsa_switch *ds, int port,
533 unsigned long *supported,
534 struct phylink_link_state *state);
535 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
536 struct phylink_link_state *state);
537 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
539 const struct phylink_link_state *state);
540 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
541 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
543 phy_interface_t interface);
544 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
546 phy_interface_t interface,
547 struct phy_device *phydev,
548 int speed, int duplex,
549 bool tx_pause, bool rx_pause);
550 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
551 struct phylink_link_state *state);
553 * Port statistics counters.
555 void (*get_strings)(struct dsa_switch *ds, int port,
556 u32 stringset, uint8_t *data);
557 void (*get_ethtool_stats)(struct dsa_switch *ds,
558 int port, uint64_t *data);
559 int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
560 void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
561 int port, uint64_t *data);
562 void (*get_stats64)(struct dsa_switch *ds, int port,
563 struct rtnl_link_stats64 *s);
566 * ethtool Wake-on-LAN
568 void (*get_wol)(struct dsa_switch *ds, int port,
569 struct ethtool_wolinfo *w);
570 int (*set_wol)(struct dsa_switch *ds, int port,
571 struct ethtool_wolinfo *w);
574 * ethtool timestamp info
576 int (*get_ts_info)(struct dsa_switch *ds, int port,
577 struct ethtool_ts_info *ts);
582 int (*suspend)(struct dsa_switch *ds);
583 int (*resume)(struct dsa_switch *ds);
586 * Port enable/disable
588 int (*port_enable)(struct dsa_switch *ds, int port,
589 struct phy_device *phy);
590 void (*port_disable)(struct dsa_switch *ds, int port);
593 * Port's MAC EEE settings
595 int (*set_mac_eee)(struct dsa_switch *ds, int port,
596 struct ethtool_eee *e);
597 int (*get_mac_eee)(struct dsa_switch *ds, int port,
598 struct ethtool_eee *e);
601 int (*get_eeprom_len)(struct dsa_switch *ds);
602 int (*get_eeprom)(struct dsa_switch *ds,
603 struct ethtool_eeprom *eeprom, u8 *data);
604 int (*set_eeprom)(struct dsa_switch *ds,
605 struct ethtool_eeprom *eeprom, u8 *data);
610 int (*get_regs_len)(struct dsa_switch *ds, int port);
611 void (*get_regs)(struct dsa_switch *ds, int port,
612 struct ethtool_regs *regs, void *p);
615 * Upper device tracking.
617 int (*port_prechangeupper)(struct dsa_switch *ds, int port,
618 struct netdev_notifier_changeupper_info *info);
623 int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
624 int (*port_bridge_join)(struct dsa_switch *ds, int port,
625 struct net_device *bridge);
626 void (*port_bridge_leave)(struct dsa_switch *ds, int port,
627 struct net_device *bridge);
628 void (*port_stp_state_set)(struct dsa_switch *ds, int port,
630 void (*port_fast_age)(struct dsa_switch *ds, int port);
631 int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
632 struct switchdev_brport_flags flags,
633 struct netlink_ext_ack *extack);
634 int (*port_bridge_flags)(struct dsa_switch *ds, int port,
635 struct switchdev_brport_flags flags,
636 struct netlink_ext_ack *extack);
637 int (*port_set_mrouter)(struct dsa_switch *ds, int port, bool mrouter,
638 struct netlink_ext_ack *extack);
643 int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
645 struct netlink_ext_ack *extack);
646 int (*port_vlan_add)(struct dsa_switch *ds, int port,
647 const struct switchdev_obj_port_vlan *vlan,
648 struct netlink_ext_ack *extack);
649 int (*port_vlan_del)(struct dsa_switch *ds, int port,
650 const struct switchdev_obj_port_vlan *vlan);
652 * Forwarding database
654 int (*port_fdb_add)(struct dsa_switch *ds, int port,
655 const unsigned char *addr, u16 vid);
656 int (*port_fdb_del)(struct dsa_switch *ds, int port,
657 const unsigned char *addr, u16 vid);
658 int (*port_fdb_dump)(struct dsa_switch *ds, int port,
659 dsa_fdb_dump_cb_t *cb, void *data);
664 int (*port_mdb_add)(struct dsa_switch *ds, int port,
665 const struct switchdev_obj_port_mdb *mdb);
666 int (*port_mdb_del)(struct dsa_switch *ds, int port,
667 const struct switchdev_obj_port_mdb *mdb);
671 int (*get_rxnfc)(struct dsa_switch *ds, int port,
672 struct ethtool_rxnfc *nfc, u32 *rule_locs);
673 int (*set_rxnfc)(struct dsa_switch *ds, int port,
674 struct ethtool_rxnfc *nfc);
679 int (*cls_flower_add)(struct dsa_switch *ds, int port,
680 struct flow_cls_offload *cls, bool ingress);
681 int (*cls_flower_del)(struct dsa_switch *ds, int port,
682 struct flow_cls_offload *cls, bool ingress);
683 int (*cls_flower_stats)(struct dsa_switch *ds, int port,
684 struct flow_cls_offload *cls, bool ingress);
685 int (*port_mirror_add)(struct dsa_switch *ds, int port,
686 struct dsa_mall_mirror_tc_entry *mirror,
688 void (*port_mirror_del)(struct dsa_switch *ds, int port,
689 struct dsa_mall_mirror_tc_entry *mirror);
690 int (*port_policer_add)(struct dsa_switch *ds, int port,
691 struct dsa_mall_policer_tc_entry *policer);
692 void (*port_policer_del)(struct dsa_switch *ds, int port);
693 int (*port_setup_tc)(struct dsa_switch *ds, int port,
694 enum tc_setup_type type, void *type_data);
697 * Cross-chip operations
699 int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
700 int sw_index, int port,
701 struct net_device *br);
702 void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
703 int sw_index, int port,
704 struct net_device *br);
705 int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
707 int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
708 int port, struct net_device *lag,
709 struct netdev_lag_upper_info *info);
710 int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
711 int port, struct net_device *lag);
716 int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
718 int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
720 bool (*port_txtstamp)(struct dsa_switch *ds, int port,
721 struct sk_buff *clone, unsigned int type);
722 bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
723 struct sk_buff *skb, unsigned int type);
725 /* Devlink parameters, etc */
726 int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
727 struct devlink_param_gset_ctx *ctx);
728 int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
729 struct devlink_param_gset_ctx *ctx);
730 int (*devlink_info_get)(struct dsa_switch *ds,
731 struct devlink_info_req *req,
732 struct netlink_ext_ack *extack);
733 int (*devlink_sb_pool_get)(struct dsa_switch *ds,
734 unsigned int sb_index, u16 pool_index,
735 struct devlink_sb_pool_info *pool_info);
736 int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
737 u16 pool_index, u32 size,
738 enum devlink_sb_threshold_type threshold_type,
739 struct netlink_ext_ack *extack);
740 int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
741 unsigned int sb_index, u16 pool_index,
743 int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
744 unsigned int sb_index, u16 pool_index,
746 struct netlink_ext_ack *extack);
747 int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
748 unsigned int sb_index, u16 tc_index,
749 enum devlink_sb_pool_type pool_type,
750 u16 *p_pool_index, u32 *p_threshold);
751 int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
752 unsigned int sb_index, u16 tc_index,
753 enum devlink_sb_pool_type pool_type,
754 u16 pool_index, u32 threshold,
755 struct netlink_ext_ack *extack);
756 int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
757 unsigned int sb_index);
758 int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
759 unsigned int sb_index);
760 int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
761 unsigned int sb_index, u16 pool_index,
762 u32 *p_cur, u32 *p_max);
763 int (*devlink_sb_occ_tc_port_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 u32 *p_cur, u32 *p_max);
769 * MTU change functionality. Switches can also adjust their MRU through
770 * this method. By MTU, one understands the SDU (L2 payload) length.
771 * If the switch needs to account for the DSA tag on the CPU port, this
772 * method needs to do so privately.
774 int (*port_change_mtu)(struct dsa_switch *ds, int port,
776 int (*port_max_mtu)(struct dsa_switch *ds, int port);
781 int (*port_lag_change)(struct dsa_switch *ds, int port);
782 int (*port_lag_join)(struct dsa_switch *ds, int port,
783 struct net_device *lag,
784 struct netdev_lag_upper_info *info);
785 int (*port_lag_leave)(struct dsa_switch *ds, int port,
786 struct net_device *lag);
791 int (*port_hsr_join)(struct dsa_switch *ds, int port,
792 struct net_device *hsr);
793 int (*port_hsr_leave)(struct dsa_switch *ds, int port,
794 struct net_device *hsr);
799 int (*port_mrp_add)(struct dsa_switch *ds, int port,
800 const struct switchdev_obj_mrp *mrp);
801 int (*port_mrp_del)(struct dsa_switch *ds, int port,
802 const struct switchdev_obj_mrp *mrp);
803 int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
804 const struct switchdev_obj_ring_role_mrp *mrp);
805 int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
806 const struct switchdev_obj_ring_role_mrp *mrp);
809 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
810 DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
811 dsa_devlink_param_get, dsa_devlink_param_set, NULL)
813 int dsa_devlink_param_get(struct devlink *dl, u32 id,
814 struct devlink_param_gset_ctx *ctx);
815 int dsa_devlink_param_set(struct devlink *dl, u32 id,
816 struct devlink_param_gset_ctx *ctx);
817 int dsa_devlink_params_register(struct dsa_switch *ds,
818 const struct devlink_param *params,
819 size_t params_count);
820 void dsa_devlink_params_unregister(struct dsa_switch *ds,
821 const struct devlink_param *params,
822 size_t params_count);
823 int dsa_devlink_resource_register(struct dsa_switch *ds,
824 const char *resource_name,
827 u64 parent_resource_id,
828 const struct devlink_resource_size_params *size_params);
830 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
832 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
834 devlink_resource_occ_get_t *occ_get,
836 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
838 struct devlink_region *
839 dsa_devlink_region_create(struct dsa_switch *ds,
840 const struct devlink_region_ops *ops,
841 u32 region_max_snapshots, u64 region_size);
842 struct devlink_region *
843 dsa_devlink_port_region_create(struct dsa_switch *ds,
845 const struct devlink_port_region_ops *ops,
846 u32 region_max_snapshots, u64 region_size);
847 void dsa_devlink_region_destroy(struct devlink_region *region);
849 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
851 struct dsa_devlink_priv {
852 struct dsa_switch *ds;
855 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
857 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
863 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
865 struct devlink *dl = port->devlink;
866 struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
871 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
876 struct dsa_switch_driver {
877 struct list_head list;
878 const struct dsa_switch_ops *ops;
881 struct net_device *dsa_dev_to_net_device(struct device *dev);
883 /* Keep inline for faster access in hot path */
884 static inline bool netdev_uses_dsa(const struct net_device *dev)
886 #if IS_ENABLED(CONFIG_NET_DSA)
887 return dev->dsa_ptr && dev->dsa_ptr->rcv;
892 static inline bool dsa_can_decode(const struct sk_buff *skb,
893 struct net_device *dev)
895 #if IS_ENABLED(CONFIG_NET_DSA)
896 return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
901 /* All DSA tags that push the EtherType to the right (basically all except tail
902 * tags, which don't break dissection) can be treated the same from the
903 * perspective of the flow dissector.
906 * - offset: the (B - A) difference between:
907 * A. the position of the real EtherType and
908 * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
909 * after the normal EtherType was supposed to be)
910 * The offset in bytes is exactly equal to the tagger overhead (and half of
911 * that, in __be16 shorts).
913 * - proto: the value of the real EtherType.
915 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
916 __be16 *proto, int *offset)
918 #if IS_ENABLED(CONFIG_NET_DSA)
919 const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
920 int tag_len = ops->overhead;
923 *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
927 #if IS_ENABLED(CONFIG_NET_DSA)
928 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
930 int err = -EOPNOTSUPP;
935 if (!dev->dsa_ptr->netdev_ops)
941 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
944 const struct dsa_netdevice_ops *ops;
947 err = __dsa_netdevice_ops_check(dev);
951 ops = dev->dsa_ptr->netdev_ops;
953 return ops->ndo_do_ioctl(dev, ifr, cmd);
956 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
963 void dsa_unregister_switch(struct dsa_switch *ds);
964 int dsa_register_switch(struct dsa_switch *ds);
965 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
966 #ifdef CONFIG_PM_SLEEP
967 int dsa_switch_suspend(struct dsa_switch *ds);
968 int dsa_switch_resume(struct dsa_switch *ds);
970 static inline int dsa_switch_suspend(struct dsa_switch *ds)
974 static inline int dsa_switch_resume(struct dsa_switch *ds)
978 #endif /* CONFIG_PM_SLEEP */
980 #if IS_ENABLED(CONFIG_NET_DSA)
981 bool dsa_slave_dev_check(const struct net_device *dev);
983 static inline bool dsa_slave_dev_check(const struct net_device *dev)
989 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
990 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
991 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
992 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
993 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
995 struct dsa_tag_driver {
996 const struct dsa_device_ops *ops;
997 struct list_head list;
998 struct module *owner;
1001 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1003 struct module *owner);
1004 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1005 unsigned int count);
1007 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1008 static int __init dsa_tag_driver_module_init(void) \
1010 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
1014 module_init(dsa_tag_driver_module_init); \
1016 static void __exit dsa_tag_driver_module_exit(void) \
1018 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
1020 module_exit(dsa_tag_driver_module_exit)
1023 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1025 * @__ops_array: Array of tag driver strucutres
1027 * Helper macro for DSA tag drivers which do not do anything special
1028 * in module init/exit. Each module may only use this macro once, and
1029 * calling it replaces module_init() and module_exit().
1031 #define module_dsa_tag_drivers(__ops_array) \
1032 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1034 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1036 /* Create a static structure we can build a linked list of dsa_tag
1039 #define DSA_TAG_DRIVER(__ops) \
1040 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
1045 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1047 * @__ops: Single tag driver structures
1049 * Helper macro for DSA tag drivers which do not do anything special
1050 * in module init/exit. Each module may only use this macro once, and
1051 * calling it replaces module_init() and module_exit().
1053 #define module_dsa_tag_driver(__ops) \
1054 DSA_TAG_DRIVER(__ops); \
1056 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
1057 &DSA_TAG_DRIVER_NAME(__ops) \
1059 module_dsa_tag_drivers(dsa_tag_driver_array)