xref: /openbmc/linux/include/net/dsa.h (revision 3d410403)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
291da11f8SLennert Buytenhek /*
391da11f8SLennert Buytenhek  * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
4e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
591da11f8SLennert Buytenhek  */
691da11f8SLennert Buytenhek 
791da11f8SLennert Buytenhek #ifndef __LINUX_NET_DSA_H
891da11f8SLennert Buytenhek #define __LINUX_NET_DSA_H
991da11f8SLennert Buytenhek 
104d56a29fSRussell King #include <linux/if.h>
11ea1f51beSAxel Lin #include <linux/if_ether.h>
12c8f0b869SBen Hutchings #include <linux/list.h>
13f515f192SVivien Didelot #include <linux/notifier.h>
14cf50dcc2SBen Hutchings #include <linux/timer.h>
15cf50dcc2SBen Hutchings #include <linux/workqueue.h>
16fa981d9aSFlorian Fainelli #include <linux/of.h>
17a2820543SFlorian Fainelli #include <linux/ethtool.h>
180336369dSBrandon Streiff #include <linux/net_tstamp.h>
1911d8f3ddSFlorian Fainelli #include <linux/phy.h>
20ecfc9372SFlorian Fainelli #include <linux/platform_data/dsa.h>
2144cc27e4SIoana Ciornei #include <linux/phylink.h>
2296567d5dSAndrew Lunn #include <net/devlink.h>
23f0c24ccfSVivien Didelot #include <net/switchdev.h>
24cf50dcc2SBen Hutchings 
25f50f2127SFlorian Fainelli struct tc_action;
264d56a29fSRussell King struct phy_device;
274d56a29fSRussell King struct fixed_phy_status;
2811d8f3ddSFlorian Fainelli struct phylink_link_state;
29f50f2127SFlorian Fainelli 
300b42f033SAndrew Lunn #define DSA_TAG_PROTO_NONE_VALUE		0
310b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_VALUE		1
320b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE	2
330b42f033SAndrew Lunn #define DSA_TAG_PROTO_DSA_VALUE			3
340b42f033SAndrew Lunn #define DSA_TAG_PROTO_EDSA_VALUE		4
350b42f033SAndrew Lunn #define DSA_TAG_PROTO_GSWIP_VALUE		5
360b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9477_VALUE		6
370b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9893_VALUE		7
380b42f033SAndrew Lunn #define DSA_TAG_PROTO_LAN9303_VALUE		8
390b42f033SAndrew Lunn #define DSA_TAG_PROTO_MTK_VALUE			9
400b42f033SAndrew Lunn #define DSA_TAG_PROTO_QCA_VALUE			10
410b42f033SAndrew Lunn #define DSA_TAG_PROTO_TRAILER_VALUE		11
42f9bbe447SVladimir Oltean #define DSA_TAG_PROTO_8021Q_VALUE		12
43227d07a0SVladimir Oltean #define DSA_TAG_PROTO_SJA1105_VALUE		13
44016e43a2STristram Ha #define DSA_TAG_PROTO_KSZ8795_VALUE		14
458dce89aaSVladimir Oltean #define DSA_TAG_PROTO_OCELOT_VALUE		15
4648fda74fSOleksij Rempel #define DSA_TAG_PROTO_AR9331_VALUE		16
47efd7fe68SLinus Walleij #define DSA_TAG_PROTO_RTL4_A_VALUE		17
4801ef09caSKurt Kanzenbach #define DSA_TAG_PROTO_HELLCREEK_VALUE		18
4954a52823SGeorge McCollister #define DSA_TAG_PROTO_XRS700X_VALUE		19
507c83a7c5SVladimir Oltean #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE	20
517c4bb540SVladimir Oltean #define DSA_TAG_PROTO_SEVILLE_VALUE		21
52964dbf18SÁlvaro Fernández Rojas #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE		22
534913b8ebSVladimir Oltean #define DSA_TAG_PROTO_SJA1110_VALUE		23
541521d5adSAlvin Šipraga #define DSA_TAG_PROTO_RTL8_4_VALUE		24
55cd87fecdSLuiz Angelo Daros de Luca #define DSA_TAG_PROTO_RTL8_4T_VALUE		25
56a08d6a6dSClément Léger #define DSA_TAG_PROTO_RZN1_A5PSW_VALUE		26
570b42f033SAndrew Lunn 
58ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
590b42f033SAndrew Lunn 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
600b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
61964dbf18SÁlvaro Fernández Rojas 	DSA_TAG_PROTO_BRCM_LEGACY	= DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
620b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
630b42f033SAndrew Lunn 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
640b42f033SAndrew Lunn 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
650b42f033SAndrew Lunn 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
660b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
670b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
680b42f033SAndrew Lunn 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
690b42f033SAndrew Lunn 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
700b42f033SAndrew Lunn 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
710b42f033SAndrew Lunn 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
72f9bbe447SVladimir Oltean 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
73227d07a0SVladimir Oltean 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
74016e43a2STristram Ha 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
758dce89aaSVladimir Oltean 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
7648fda74fSOleksij Rempel 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
77efd7fe68SLinus Walleij 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
7801ef09caSKurt Kanzenbach 	DSA_TAG_PROTO_HELLCREEK		= DSA_TAG_PROTO_HELLCREEK_VALUE,
7954a52823SGeorge McCollister 	DSA_TAG_PROTO_XRS700X		= DSA_TAG_PROTO_XRS700X_VALUE,
807c83a7c5SVladimir Oltean 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
817c4bb540SVladimir Oltean 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
824913b8ebSVladimir Oltean 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
831521d5adSAlvin Šipraga 	DSA_TAG_PROTO_RTL8_4		= DSA_TAG_PROTO_RTL8_4_VALUE,
84cd87fecdSLuiz Angelo Daros de Luca 	DSA_TAG_PROTO_RTL8_4T		= DSA_TAG_PROTO_RTL8_4T_VALUE,
85a08d6a6dSClément Léger 	DSA_TAG_PROTO_RZN1_A5PSW	= DSA_TAG_PROTO_RZN1_A5PSW_VALUE,
86ac7a04c3SFlorian Fainelli };
875037d532SFlorian Fainelli 
8890af1059SBrandon Streiff struct dsa_switch;
893e8a72d1SFlorian Fainelli 
9068277a2cSJohn Crispin struct dsa_device_ops {
9168277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
9229a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
932e8cb1b3SVladimir Oltean 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
94598a9680SJohn Crispin 			     int *offset);
957f297314SVladimir Oltean 	int (*connect)(struct dsa_switch *ds);
967f297314SVladimir Oltean 	void (*disconnect)(struct dsa_switch *ds);
974e500251SVladimir Oltean 	unsigned int needed_headroom;
984e500251SVladimir Oltean 	unsigned int needed_tailroom;
99875138f8SAndrew Lunn 	const char *name;
100056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
101c3975400SVladimir Oltean 	/* Some tagging protocols either mangle or shift the destination MAC
102c3975400SVladimir Oltean 	 * address, in which case the DSA master would drop packets on ingress
103c3975400SVladimir Oltean 	 * if what it understands out of the destination MAC address is not in
104c3975400SVladimir Oltean 	 * its RX filter.
105c3975400SVladimir Oltean 	 */
106c3975400SVladimir Oltean 	bool promisc_on_master;
10768277a2cSJohn Crispin };
10868277a2cSJohn Crispin 
1094cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the
1104cfab356SFlorian Fainelli  * DSA layer on top of the DSA CPU/management net_device instance. This is
1114cfab356SFlorian Fainelli  * used by the core net_device layer while calling various net_device_ops
1124cfab356SFlorian Fainelli  * function pointers.
1134cfab356SFlorian Fainelli  */
1144cfab356SFlorian Fainelli struct dsa_netdevice_ops {
115a7605370SArnd Bergmann 	int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
1164cfab356SFlorian Fainelli 			     int cmd);
1174cfab356SFlorian Fainelli };
1184cfab356SFlorian Fainelli 
1190b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
1200b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
1210b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
1220b42f033SAndrew Lunn 
123dedd6a00SVladimir Oltean struct dsa_lag {
124dedd6a00SVladimir Oltean 	struct net_device *dev;
125dedd6a00SVladimir Oltean 	unsigned int id;
126e212fa7cSVladimir Oltean 	struct mutex fdb_lock;
127e212fa7cSVladimir Oltean 	struct list_head fdbs;
128dedd6a00SVladimir Oltean 	refcount_t refcount;
129dedd6a00SVladimir Oltean };
130dedd6a00SVladimir Oltean 
131cf50dcc2SBen Hutchings struct dsa_switch_tree {
13283c0afaeSAndrew Lunn 	struct list_head	list;
13383c0afaeSAndrew Lunn 
134b035c88cSVladimir Oltean 	/* List of switch ports */
135b035c88cSVladimir Oltean 	struct list_head ports;
136b035c88cSVladimir Oltean 
137f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
138f515f192SVivien Didelot 	struct raw_notifier_head	nh;
139f515f192SVivien Didelot 
14083c0afaeSAndrew Lunn 	/* Tree identifier */
14149463b7fSVivien Didelot 	unsigned int index;
14283c0afaeSAndrew Lunn 
14383c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
14483c0afaeSAndrew Lunn 	struct kref refcount;
14583c0afaeSAndrew Lunn 
146b035c88cSVladimir Oltean 	/* Maps offloaded LAG netdevs to a zero-based linear ID for
147b035c88cSVladimir Oltean 	 * drivers that need it.
148b035c88cSVladimir Oltean 	 */
149dedd6a00SVladimir Oltean 	struct dsa_lag **lags;
150b035c88cSVladimir Oltean 
151357f203bSVladimir Oltean 	/* Tagging protocol operations */
152357f203bSVladimir Oltean 	const struct dsa_device_ops *tag_ops;
153357f203bSVladimir Oltean 
154deff7107STobias Waldekranz 	/* Default tagging protocol preferred by the switches in this
155deff7107STobias Waldekranz 	 * tree.
156deff7107STobias Waldekranz 	 */
157deff7107STobias Waldekranz 	enum dsa_tag_protocol default_proto;
158deff7107STobias Waldekranz 
1594b026e82SVladimir Oltean 	/* Has this tree been applied to the hardware? */
1604b026e82SVladimir Oltean 	bool setup;
1614b026e82SVladimir Oltean 
162cf50dcc2SBen Hutchings 	/*
163cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
164cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
165cf50dcc2SBen Hutchings 	 */
166cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
167cf85d08fSLennert Buytenhek 
168c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
169c5f51765SVivien Didelot 	struct list_head rtable;
170058102a6STobias Waldekranz 
171b035c88cSVladimir Oltean 	/* Length of "lags" array */
172058102a6STobias Waldekranz 	unsigned int lags_len;
1735b22d366SVladimir Oltean 
1745b22d366SVladimir Oltean 	/* Track the largest switch index within a tree */
1755b22d366SVladimir Oltean 	unsigned int last_switch;
176cf50dcc2SBen Hutchings };
177cf50dcc2SBen Hutchings 
1783d4a0a2aSVladimir Oltean /* LAG IDs are one-based, the dst->lags array is zero-based */
179058102a6STobias Waldekranz #define dsa_lags_foreach_id(_id, _dst)				\
1803d4a0a2aSVladimir Oltean 	for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++)	\
1813d4a0a2aSVladimir Oltean 		if ((_dst)->lags[(_id) - 1])
182058102a6STobias Waldekranz 
183058102a6STobias Waldekranz #define dsa_lag_foreach_port(_dp, _dst, _lag)			\
184058102a6STobias Waldekranz 	list_for_each_entry((_dp), &(_dst)->ports, list)	\
185dedd6a00SVladimir Oltean 		if (dsa_port_offloads_lag((_dp), (_lag)))
186058102a6STobias Waldekranz 
18718596f50SGeorge McCollister #define dsa_hsr_foreach_port(_dp, _ds, _hsr)			\
18818596f50SGeorge McCollister 	list_for_each_entry((_dp), &(_ds)->dst->ports, list)	\
18918596f50SGeorge McCollister 		if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
19018596f50SGeorge McCollister 
191dedd6a00SVladimir Oltean static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
192058102a6STobias Waldekranz 					    unsigned int id)
193058102a6STobias Waldekranz {
1943d4a0a2aSVladimir Oltean 	/* DSA LAG IDs are one-based, dst->lags is zero-based */
1953d4a0a2aSVladimir Oltean 	return dst->lags[id - 1];
196058102a6STobias Waldekranz }
197058102a6STobias Waldekranz 
198058102a6STobias Waldekranz static inline int dsa_lag_id(struct dsa_switch_tree *dst,
19946a76724SVladimir Oltean 			     struct net_device *lag_dev)
200058102a6STobias Waldekranz {
201058102a6STobias Waldekranz 	unsigned int id;
202058102a6STobias Waldekranz 
203058102a6STobias Waldekranz 	dsa_lags_foreach_id(id, dst) {
204dedd6a00SVladimir Oltean 		struct dsa_lag *lag = dsa_lag_by_id(dst, id);
205dedd6a00SVladimir Oltean 
206dedd6a00SVladimir Oltean 		if (lag->dev == lag_dev)
207dedd6a00SVladimir Oltean 			return lag->id;
208058102a6STobias Waldekranz 	}
209058102a6STobias Waldekranz 
210058102a6STobias Waldekranz 	return -ENODEV;
211058102a6STobias Waldekranz }
212058102a6STobias Waldekranz 
21334297176SVladimir Oltean /* TC matchall action types */
214f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
215f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
21634297176SVladimir Oltean 	DSA_PORT_MALL_POLICER,
217f50f2127SFlorian Fainelli };
218f50f2127SFlorian Fainelli 
219f50f2127SFlorian Fainelli /* TC mirroring entry */
220f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
221f50f2127SFlorian Fainelli 	u8 to_local_port;
222f50f2127SFlorian Fainelli 	bool ingress;
223f50f2127SFlorian Fainelli };
224f50f2127SFlorian Fainelli 
22534297176SVladimir Oltean /* TC port policer entry */
22634297176SVladimir Oltean struct dsa_mall_policer_tc_entry {
2275f035af7SPo Liu 	u32 burst;
22834297176SVladimir Oltean 	u64 rate_bytes_per_sec;
22934297176SVladimir Oltean };
23034297176SVladimir Oltean 
231f50f2127SFlorian Fainelli /* TC matchall entry */
232f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
233f50f2127SFlorian Fainelli 	struct list_head list;
234f50f2127SFlorian Fainelli 	unsigned long cookie;
235f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
236f50f2127SFlorian Fainelli 	union {
237f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
23834297176SVladimir Oltean 		struct dsa_mall_policer_tc_entry policer;
239f50f2127SFlorian Fainelli 	};
240f50f2127SFlorian Fainelli };
241f50f2127SFlorian Fainelli 
242d3eed0e5SVladimir Oltean struct dsa_bridge {
243d3eed0e5SVladimir Oltean 	struct net_device *dev;
244d3eed0e5SVladimir Oltean 	unsigned int num;
245857fdd74SVladimir Oltean 	bool tx_fwd_offload;
246d3eed0e5SVladimir Oltean 	refcount_t refcount;
247d3eed0e5SVladimir Oltean };
248f50f2127SFlorian Fainelli 
249c8b09808SAndrew Lunn struct dsa_port {
250f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
251f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
252f8b8b1cdSVivien Didelot 	 */
253f8b8b1cdSVivien Didelot 	union {
254f8b8b1cdSVivien Didelot 		struct net_device *master;
255f8b8b1cdSVivien Didelot 		struct net_device *slave;
256f8b8b1cdSVivien Didelot 	};
257f8b8b1cdSVivien Didelot 
258357f203bSVladimir Oltean 	/* Copy of the tagging protocol operations, for quicker access
259357f203bSVladimir Oltean 	 * in the data path. Valid only for the CPU ports.
260357f203bSVladimir Oltean 	 */
26115240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
26215240248SVivien Didelot 
2633e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
2643e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
26529a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
2663e41f93bSVivien Didelot 
26706251258SVladimir Oltean 	struct dsa_switch	*ds;
26806251258SVladimir Oltean 
26906251258SVladimir Oltean 	unsigned int		index;
27006251258SVladimir Oltean 
271057cad2cSVivien Didelot 	enum {
272057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
273057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
274057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
275057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
276057cad2cSVivien Didelot 	} type;
277057cad2cSVivien Didelot 
27871e0bbdeSFlorian Fainelli 	const char		*name;
27968b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
28083216e39SMichael Walle 	u8			mac[ETH_ALEN];
281b08db33dSVladimir Oltean 
282b08db33dSVladimir Oltean 	u8			stp_state;
283b08db33dSVladimir Oltean 
2841b26d364SVladimir Oltean 	/* Warning: the following bit fields are not atomic, and updating them
2851b26d364SVladimir Oltean 	 * can only be done from code paths where concurrency is not possible
2861b26d364SVladimir Oltean 	 * (probe time or under rtnl_lock).
2871b26d364SVladimir Oltean 	 */
28863cfc657SVladimir Oltean 	u8			vlan_filtering:1;
28963cfc657SVladimir Oltean 
29063cfc657SVladimir Oltean 	/* Managed by DSA on user ports and by drivers on CPU and DSA ports */
29163cfc657SVladimir Oltean 	u8			learning:1;
29263cfc657SVladimir Oltean 
29363cfc657SVladimir Oltean 	u8			lag_tx_enabled:1;
29463cfc657SVladimir Oltean 
29563cfc657SVladimir Oltean 	u8			devlink_port_setup:1;
29663cfc657SVladimir Oltean 
297295ab96fSVladimir Oltean 	/* Master state bits, valid only on CPU ports */
298295ab96fSVladimir Oltean 	u8			master_admin_up:1;
299295ab96fSVladimir Oltean 	u8			master_oper_up:1;
300295ab96fSVladimir Oltean 
30163cfc657SVladimir Oltean 	u8			setup:1;
302bde82f38SVladimir Oltean 
303189b0d93SAndrew Lunn 	struct device_node	*dn;
30434a79f63SVivien Didelot 	unsigned int		ageing_time;
305bde82f38SVladimir Oltean 
306d3eed0e5SVladimir Oltean 	struct dsa_bridge	*bridge;
30796567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
308aab9c406SFlorian Fainelli 	struct phylink		*pl;
30944cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
310dedd6a00SVladimir Oltean 	struct dsa_lag		*lag;
31118596f50SGeorge McCollister 	struct net_device	*hsr_dev;
31297a69a0dSVladimir Oltean 
313ab8ccae1SVivien Didelot 	struct list_head list;
314ab8ccae1SVivien Didelot 
31567dbb9d4SFlorian Fainelli 	/*
31667dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
31767dbb9d4SFlorian Fainelli 	 */
31867dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
319da7b9e9bSFlorian Fainelli 
320da7b9e9bSFlorian Fainelli 	/*
321da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
322da7b9e9bSFlorian Fainelli 	 */
3234cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *netdev_ops;
324fb35c60cSVivien Didelot 
325161ca59dSVladimir Oltean 	/* List of MAC addresses that must be forwarded on this port.
326161ca59dSVladimir Oltean 	 * These are only valid on CPU ports and DSA links.
327161ca59dSVladimir Oltean 	 */
328338a3a47SVladimir Oltean 	struct mutex		addr_lists_lock;
3293f6e32f9SVladimir Oltean 	struct list_head	fdbs;
330161ca59dSVladimir Oltean 	struct list_head	mdbs;
331134ef238SVladimir Oltean 
332134ef238SVladimir Oltean 	/* List of VLANs that CPU and DSA ports are members of. */
333134ef238SVladimir Oltean 	struct mutex		vlans_lock;
334134ef238SVladimir Oltean 	struct list_head	vlans;
335c8b09808SAndrew Lunn };
336c8b09808SAndrew Lunn 
337c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
338c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
339c5f51765SVivien Didelot  * but this would require some more complex tree walking,
340c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
341c5f51765SVivien Didelot  */
342c5f51765SVivien Didelot struct dsa_link {
343c5f51765SVivien Didelot 	struct dsa_port *dp;
344c5f51765SVivien Didelot 	struct dsa_port *link_dp;
345c5f51765SVivien Didelot 	struct list_head list;
346c5f51765SVivien Didelot };
347c5f51765SVivien Didelot 
348c2693363SVladimir Oltean enum dsa_db_type {
349c2693363SVladimir Oltean 	DSA_DB_PORT,
350c2693363SVladimir Oltean 	DSA_DB_LAG,
351c2693363SVladimir Oltean 	DSA_DB_BRIDGE,
352c2693363SVladimir Oltean };
353c2693363SVladimir Oltean 
354c2693363SVladimir Oltean struct dsa_db {
355c2693363SVladimir Oltean 	enum dsa_db_type type;
356c2693363SVladimir Oltean 
357c2693363SVladimir Oltean 	union {
358c2693363SVladimir Oltean 		const struct dsa_port *dp;
359c2693363SVladimir Oltean 		struct dsa_lag lag;
360c2693363SVladimir Oltean 		struct dsa_bridge bridge;
361c2693363SVladimir Oltean 	};
362c2693363SVladimir Oltean };
363c2693363SVladimir Oltean 
364161ca59dSVladimir Oltean struct dsa_mac_addr {
365161ca59dSVladimir Oltean 	unsigned char addr[ETH_ALEN];
366161ca59dSVladimir Oltean 	u16 vid;
367161ca59dSVladimir Oltean 	refcount_t refcount;
368161ca59dSVladimir Oltean 	struct list_head list;
369c2693363SVladimir Oltean 	struct dsa_db db;
370161ca59dSVladimir Oltean };
371161ca59dSVladimir Oltean 
372134ef238SVladimir Oltean struct dsa_vlan {
373134ef238SVladimir Oltean 	u16 vid;
374134ef238SVladimir Oltean 	refcount_t refcount;
375134ef238SVladimir Oltean 	struct list_head list;
376134ef238SVladimir Oltean };
377134ef238SVladimir Oltean 
378c8f0b869SBen Hutchings struct dsa_switch {
379c33063d6SAndrew Lunn 	struct device *dev;
380c33063d6SAndrew Lunn 
381c8f0b869SBen Hutchings 	/*
382c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
383c8f0b869SBen Hutchings 	 */
384c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
38599feaafcSVivien Didelot 	unsigned int		index;
386c8f0b869SBen Hutchings 
3871b26d364SVladimir Oltean 	/* Warning: the following bit fields are not atomic, and updating them
3881b26d364SVladimir Oltean 	 * can only be done from code paths where concurrency is not possible
3891b26d364SVladimir Oltean 	 * (probe time or under rtnl_lock).
3901b26d364SVladimir Oltean 	 */
39163cfc657SVladimir Oltean 	u32			setup:1;
39263cfc657SVladimir Oltean 
39363cfc657SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
39463cfc657SVladimir Oltean 	 * settings on ports if not hardware-supported
3957787ff77SVladimir Oltean 	 */
39663cfc657SVladimir Oltean 	u32			vlan_filtering_is_global:1;
39763cfc657SVladimir Oltean 
39863cfc657SVladimir Oltean 	/* Keep VLAN filtering enabled on ports not offloading any upper */
39963cfc657SVladimir Oltean 	u32			needs_standalone_vlan_filtering:1;
40063cfc657SVladimir Oltean 
40163cfc657SVladimir Oltean 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
40263cfc657SVladimir Oltean 	 * that have vlan_filtering=0. All drivers should ideally set this (and
40363cfc657SVladimir Oltean 	 * then the option would get removed), but it is unknown whether this
4047787ff77SVladimir Oltean 	 * would break things or not.
4057787ff77SVladimir Oltean 	 */
40663cfc657SVladimir Oltean 	u32			configure_vlan_while_not_filtering:1;
40763cfc657SVladimir Oltean 
40863cfc657SVladimir Oltean 	/* If the switch driver always programs the CPU port as egress tagged
40963cfc657SVladimir Oltean 	 * despite the VLAN configuration indicating otherwise, then setting
41063cfc657SVladimir Oltean 	 * @untag_bridge_pvid will force the DSA receive path to pop the
41163cfc657SVladimir Oltean 	 * bridge's default_pvid VLAN tagged frames to offer a consistent
41263cfc657SVladimir Oltean 	 * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge
4137787ff77SVladimir Oltean 	 * device.
4147787ff77SVladimir Oltean 	 */
41563cfc657SVladimir Oltean 	u32			untag_bridge_pvid:1;
41663cfc657SVladimir Oltean 
4177787ff77SVladimir Oltean 	/* Let DSA manage the FDB entries towards the
4187787ff77SVladimir Oltean 	 * CPU, based on the software bridge database.
4197787ff77SVladimir Oltean 	 */
42063cfc657SVladimir Oltean 	u32			assisted_learning_on_cpu_port:1;
42163cfc657SVladimir Oltean 
42263cfc657SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
42363cfc657SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
4247787ff77SVladimir Oltean 	 */
42563cfc657SVladimir Oltean 	u32			vlan_filtering:1;
42663cfc657SVladimir Oltean 
42763cfc657SVladimir Oltean 	/* For switches that only have the MRU configurable. To ensure the
42863cfc657SVladimir Oltean 	 * configured MTU is not exceeded, normalization of MRU on all bridged
42963cfc657SVladimir Oltean 	 * interfaces is needed.
4307787ff77SVladimir Oltean 	 */
43163cfc657SVladimir Oltean 	u32			mtu_enforcement_ingress:1;
4327787ff77SVladimir Oltean 
433c2693363SVladimir Oltean 	/* Drivers that isolate the FDBs of multiple bridges must set this
434c2693363SVladimir Oltean 	 * to true to receive the bridge as an argument in .port_fdb_{add,del}
435c2693363SVladimir Oltean 	 * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
436c2693363SVladimir Oltean 	 * passed as zero.
437c2693363SVladimir Oltean 	 */
438c2693363SVladimir Oltean 	u32			fdb_isolation:1;
439c2693363SVladimir Oltean 
440f515f192SVivien Didelot 	/* Listener for switch fabric events */
441f515f192SVivien Didelot 	struct notifier_block	nb;
442f515f192SVivien Didelot 
443c8f0b869SBen Hutchings 	/*
4447543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
4457543a6d5SAndrew Lunn 	 * structure.
4467543a6d5SAndrew Lunn 	 */
4477543a6d5SAndrew Lunn 	void *priv;
4487543a6d5SAndrew Lunn 
449dc452a47SVladimir Oltean 	void *tagger_data;
450dc452a47SVladimir Oltean 
4517543a6d5SAndrew Lunn 	/*
452c8f0b869SBen Hutchings 	 * Configuration data for this switch.
453c8f0b869SBen Hutchings 	 */
454ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
455c8f0b869SBen Hutchings 
456c8f0b869SBen Hutchings 	/*
4579d490b4eSVivien Didelot 	 * The switch operations.
458c8f0b869SBen Hutchings 	 */
459a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
460c8f0b869SBen Hutchings 
46166472fc0SAndrew Lunn 	/*
462c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
463c8f0b869SBen Hutchings 	 */
4640d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
465c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
466a0c02161SVivien Didelot 
4670f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
4680f3da6afSVivien Didelot 	unsigned int ageing_time_min;
4690f3da6afSVivien Didelot 	unsigned int ageing_time_max;
4700f3da6afSVivien Didelot 
471d7b1fd52SVladimir Oltean 	/* Storage for drivers using tag_8021q */
472d7b1fd52SVladimir Oltean 	struct dsa_8021q_context *tag_8021q_ctx;
473d7b1fd52SVladimir Oltean 
47496567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
47596567d5dSAndrew Lunn 	struct devlink		*devlink;
47696567d5dSAndrew Lunn 
47755199df6SFlorian Fainelli 	/* Number of switch port queues */
47855199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
47955199df6SFlorian Fainelli 
480058102a6STobias Waldekranz 	/* Drivers that benefit from having an ID associated with each
481058102a6STobias Waldekranz 	 * offloaded LAG should set this to the maximum number of
482058102a6STobias Waldekranz 	 * supported IDs. DSA will then maintain a mapping of _at
483058102a6STobias Waldekranz 	 * least_ these many IDs, accessible to drivers via
484058102a6STobias Waldekranz 	 * dsa_lag_id().
485058102a6STobias Waldekranz 	 */
486058102a6STobias Waldekranz 	unsigned int		num_lag_ids;
487058102a6STobias Waldekranz 
488947c8746SVladimir Oltean 	/* Drivers that support bridge forwarding offload or FDB isolation
489947c8746SVladimir Oltean 	 * should set this to the maximum number of bridges spanning the same
490947c8746SVladimir Oltean 	 * switch tree (or all trees, in the case of cross-tree bridging
491947c8746SVladimir Oltean 	 * support) that can be offloaded.
492123abc06SVladimir Oltean 	 */
493947c8746SVladimir Oltean 	unsigned int		max_num_bridges;
494123abc06SVladimir Oltean 
495258030acSVladimir Oltean 	unsigned int		num_ports;
496c8f0b869SBen Hutchings };
497c8f0b869SBen Hutchings 
49868bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
499c38c5a66SVivien Didelot {
500b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
501d607525bSVivien Didelot 	struct dsa_port *dp;
502b96ddf25SVivien Didelot 
503b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
504b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
505b96ddf25SVivien Didelot 			return dp;
506d607525bSVivien Didelot 
507d607525bSVivien Didelot 	return NULL;
508c38c5a66SVivien Didelot }
509c38c5a66SVivien Didelot 
510a8986681SVladimir Oltean static inline bool dsa_port_is_dsa(struct dsa_port *port)
511a8986681SVladimir Oltean {
512a8986681SVladimir Oltean 	return port->type == DSA_PORT_TYPE_DSA;
513a8986681SVladimir Oltean }
514a8986681SVladimir Oltean 
515a8986681SVladimir Oltean static inline bool dsa_port_is_cpu(struct dsa_port *port)
516a8986681SVladimir Oltean {
517a8986681SVladimir Oltean 	return port->type == DSA_PORT_TYPE_CPU;
518a8986681SVladimir Oltean }
519a8986681SVladimir Oltean 
520a8986681SVladimir Oltean static inline bool dsa_port_is_user(struct dsa_port *dp)
521a8986681SVladimir Oltean {
522a8986681SVladimir Oltean 	return dp->type == DSA_PORT_TYPE_USER;
523a8986681SVladimir Oltean }
524a8986681SVladimir Oltean 
525a57d8c21SVladimir Oltean static inline bool dsa_port_is_unused(struct dsa_port *dp)
526a57d8c21SVladimir Oltean {
527a57d8c21SVladimir Oltean 	return dp->type == DSA_PORT_TYPE_UNUSED;
528a57d8c21SVladimir Oltean }
529a57d8c21SVladimir Oltean 
530295ab96fSVladimir Oltean static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
531295ab96fSVladimir Oltean {
532295ab96fSVladimir Oltean 	return dsa_port_is_cpu(dp) && dp->master_admin_up &&
533295ab96fSVladimir Oltean 	       dp->master_oper_up;
534295ab96fSVladimir Oltean }
535295ab96fSVladimir Oltean 
536bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
537bff7b688SVivien Didelot {
538c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
539bff7b688SVivien Didelot }
540bff7b688SVivien Didelot 
541c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
542c8f0b869SBen Hutchings {
543c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
544c8f0b869SBen Hutchings }
545c8f0b869SBen Hutchings 
54660045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
54760045cbfSAndrew Lunn {
548c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
54960045cbfSAndrew Lunn }
55060045cbfSAndrew Lunn 
5512b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
5526cd456f3SVivien Didelot {
553c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
5546cd456f3SVivien Didelot }
5556cd456f3SVivien Didelot 
55682b31898SVladimir Oltean #define dsa_tree_for_each_user_port(_dp, _dst) \
55782b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_dst)->ports, list) \
55882b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
55982b31898SVladimir Oltean 
56082b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \
56182b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
56282b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
56382b31898SVladimir Oltean 
56482b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
56582b31898SVladimir Oltean 	list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
56682b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
56782b31898SVladimir Oltean 
56882b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
56982b31898SVladimir Oltean 	list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
57082b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
57182b31898SVladimir Oltean 
57282b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \
57382b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
57482b31898SVladimir Oltean 		if (!dsa_port_is_unused((_dp)))
57582b31898SVladimir Oltean 
57682b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \
57782b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
57882b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
57982b31898SVladimir Oltean 
58082b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \
58182b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
58282b31898SVladimir Oltean 		if (dsa_port_is_cpu((_dp)))
58382b31898SVladimir Oltean 
584bacf93b0SVladimir Oltean #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \
585bacf93b0SVladimir Oltean 	dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
586bacf93b0SVladimir Oltean 		if (dsa_port_is_cpu((_dp)))
587bacf93b0SVladimir Oltean 
58802bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
58902bc6e54SVivien Didelot {
590d0004a02SVladimir Oltean 	struct dsa_port *dp;
591c38c5a66SVivien Didelot 	u32 mask = 0;
59202bc6e54SVivien Didelot 
593d0004a02SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
594d0004a02SVladimir Oltean 		mask |= BIT(dp->index);
595c38c5a66SVivien Didelot 
596c38c5a66SVivien Didelot 	return mask;
597c8652c83SVivien Didelot }
598c8652c83SVivien Didelot 
599465c3de4SVladimir Oltean static inline u32 dsa_cpu_ports(struct dsa_switch *ds)
600465c3de4SVladimir Oltean {
601465c3de4SVladimir Oltean 	struct dsa_port *cpu_dp;
602465c3de4SVladimir Oltean 	u32 mask = 0;
603465c3de4SVladimir Oltean 
604465c3de4SVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
605465c3de4SVladimir Oltean 		mask |= BIT(cpu_dp->index);
606465c3de4SVladimir Oltean 
607465c3de4SVladimir Oltean 	return mask;
608465c3de4SVladimir Oltean }
609465c3de4SVladimir Oltean 
610c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
611c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
612c5f51765SVivien Didelot {
613c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
614c5f51765SVivien Didelot 	struct dsa_link *dl;
615c5f51765SVivien Didelot 
616c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
617c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
618c5f51765SVivien Didelot 			return dl->dp->index;
619c5f51765SVivien Didelot 
620c5f51765SVivien Didelot 	return ds->num_ports;
621c5f51765SVivien Didelot }
622c5f51765SVivien Didelot 
6233b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
6243b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
6253b8fac5dSVivien Didelot 					    int port)
6263b8fac5dSVivien Didelot {
6273b8fac5dSVivien Didelot 	if (device == ds->index)
6283b8fac5dSVivien Didelot 		return port;
6293b8fac5dSVivien Didelot 	else
630c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
6313b8fac5dSVivien Didelot }
6323b8fac5dSVivien Didelot 
6333b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
63407073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
635c8f0b869SBen Hutchings {
63607073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
63707073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
63807073c79SVivien Didelot 
63907073c79SVivien Didelot 	if (!cpu_dp)
64007073c79SVivien Didelot 		return port;
641c8f0b869SBen Hutchings 
6423b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
643c8f0b869SBen Hutchings }
644c8f0b869SBen Hutchings 
64563609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */
64663609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
64763609c8fSVladimir Oltean {
64863609c8fSVladimir Oltean 	if (dsa_is_unused_port(ds, port))
64963609c8fSVladimir Oltean 		return false;
65063609c8fSVladimir Oltean 
65163609c8fSVladimir Oltean 	return port == dsa_upstream_port(ds, port);
65263609c8fSVladimir Oltean }
65363609c8fSVladimir Oltean 
654d352b20fSTobias Waldekranz /* Return true if this is a DSA port leading away from the CPU */
655d352b20fSTobias Waldekranz static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
656d352b20fSTobias Waldekranz {
657d352b20fSTobias Waldekranz 	return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
658d352b20fSTobias Waldekranz }
659d352b20fSTobias Waldekranz 
6607af4a361STobias Waldekranz /* Return the local port used to reach the CPU port */
6617af4a361STobias Waldekranz static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
6627af4a361STobias Waldekranz {
6637af4a361STobias Waldekranz 	struct dsa_port *dp;
6647af4a361STobias Waldekranz 
6657af4a361STobias Waldekranz 	dsa_switch_for_each_available_port(dp, ds) {
6667af4a361STobias Waldekranz 		return dsa_upstream_port(ds, dp->index);
6677af4a361STobias Waldekranz 	}
6687af4a361STobias Waldekranz 
6697af4a361STobias Waldekranz 	return ds->num_ports;
6707af4a361STobias Waldekranz }
6717af4a361STobias Waldekranz 
67263609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
67363609c8fSVladimir Oltean  * that the routing port from @downstream_ds to @upstream_ds is also the port
67463609c8fSVladimir Oltean  * which @downstream_ds uses to reach its dedicated CPU.
67563609c8fSVladimir Oltean  */
67663609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
67763609c8fSVladimir Oltean 					     struct dsa_switch *downstream_ds)
67863609c8fSVladimir Oltean {
67963609c8fSVladimir Oltean 	int routing_port;
68063609c8fSVladimir Oltean 
68163609c8fSVladimir Oltean 	if (upstream_ds == downstream_ds)
68263609c8fSVladimir Oltean 		return true;
68363609c8fSVladimir Oltean 
68463609c8fSVladimir Oltean 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
68563609c8fSVladimir Oltean 
68663609c8fSVladimir Oltean 	return dsa_is_upstream_port(downstream_ds, routing_port);
68763609c8fSVladimir Oltean }
68863609c8fSVladimir Oltean 
689cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
690cf2d45f5SVladimir Oltean {
691cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
692cf2d45f5SVladimir Oltean 
693cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
694cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
695cf2d45f5SVladimir Oltean 	else
696cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
697cf2d45f5SVladimir Oltean }
698cf2d45f5SVladimir Oltean 
699dedd6a00SVladimir Oltean static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
700dedd6a00SVladimir Oltean {
701dedd6a00SVladimir Oltean 	return dp->lag ? dp->lag->id : 0;
702dedd6a00SVladimir Oltean }
703dedd6a00SVladimir Oltean 
704dedd6a00SVladimir Oltean static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
705dedd6a00SVladimir Oltean {
706dedd6a00SVladimir Oltean 	return dp->lag ? dp->lag->dev : NULL;
707dedd6a00SVladimir Oltean }
708dedd6a00SVladimir Oltean 
709dedd6a00SVladimir Oltean static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
710dedd6a00SVladimir Oltean 					 const struct dsa_lag *lag)
711dedd6a00SVladimir Oltean {
712dedd6a00SVladimir Oltean 	return dsa_port_lag_dev_get(dp) == lag->dev;
713dedd6a00SVladimir Oltean }
714dedd6a00SVladimir Oltean 
715cc76ce9eSTobias Waldekranz static inline
716cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
717cc76ce9eSTobias Waldekranz {
718d3eed0e5SVladimir Oltean 	if (!dp->bridge)
719cc76ce9eSTobias Waldekranz 		return NULL;
720cc76ce9eSTobias Waldekranz 
721dedd6a00SVladimir Oltean 	if (dp->lag)
722dedd6a00SVladimir Oltean 		return dp->lag->dev;
723cc76ce9eSTobias Waldekranz 	else if (dp->hsr_dev)
724cc76ce9eSTobias Waldekranz 		return dp->hsr_dev;
725cc76ce9eSTobias Waldekranz 
726cc76ce9eSTobias Waldekranz 	return dp->slave;
727cc76ce9eSTobias Waldekranz }
728cc76ce9eSTobias Waldekranz 
72936cbf39bSVladimir Oltean static inline struct net_device *
73036cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp)
73136cbf39bSVladimir Oltean {
732d3eed0e5SVladimir Oltean 	return dp->bridge ? dp->bridge->dev : NULL;
73336cbf39bSVladimir Oltean }
73436cbf39bSVladimir Oltean 
73536cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
73636cbf39bSVladimir Oltean {
737d3eed0e5SVladimir Oltean 	return dp->bridge ? dp->bridge->num : 0;
73836cbf39bSVladimir Oltean }
73936cbf39bSVladimir Oltean 
74036cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a,
74136cbf39bSVladimir Oltean 					const struct dsa_port *b)
74236cbf39bSVladimir Oltean {
74336cbf39bSVladimir Oltean 	struct net_device *br_a = dsa_port_bridge_dev_get(a);
74436cbf39bSVladimir Oltean 	struct net_device *br_b = dsa_port_bridge_dev_get(b);
74536cbf39bSVladimir Oltean 
74636cbf39bSVladimir Oltean 	/* Standalone ports are not in the same bridge with one another */
74736cbf39bSVladimir Oltean 	return (!br_a || !br_b) ? false : (br_a == br_b);
74836cbf39bSVladimir Oltean }
74936cbf39bSVladimir Oltean 
7506a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
7516a43cba3SVladimir Oltean 						 const struct net_device *dev)
7526a43cba3SVladimir Oltean {
7536a43cba3SVladimir Oltean 	return dsa_port_to_bridge_port(dp) == dev;
7546a43cba3SVladimir Oltean }
7556a43cba3SVladimir Oltean 
7566a43cba3SVladimir Oltean static inline bool
7576a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp,
7586a43cba3SVladimir Oltean 			     const struct net_device *bridge_dev)
7596a43cba3SVladimir Oltean {
7606a43cba3SVladimir Oltean 	/* DSA ports connected to a bridge, and event was emitted
7616a43cba3SVladimir Oltean 	 * for the bridge.
7626a43cba3SVladimir Oltean 	 */
7636a43cba3SVladimir Oltean 	return dsa_port_bridge_dev_get(dp) == bridge_dev;
7646a43cba3SVladimir Oltean }
7656a43cba3SVladimir Oltean 
766d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
767d3eed0e5SVladimir Oltean 					    const struct dsa_bridge *bridge)
768d3eed0e5SVladimir Oltean {
769d3eed0e5SVladimir Oltean 	return dsa_port_bridge_dev_get(dp) == bridge->dev;
770d3eed0e5SVladimir Oltean }
771d3eed0e5SVladimir Oltean 
7726a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */
7736a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
7746a43cba3SVladimir Oltean 						 const struct net_device *dev)
7756a43cba3SVladimir Oltean {
7766a43cba3SVladimir Oltean 	struct dsa_port *dp;
7776a43cba3SVladimir Oltean 
7786a43cba3SVladimir Oltean 	list_for_each_entry(dp, &dst->ports, list)
7796a43cba3SVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, dev))
7806a43cba3SVladimir Oltean 			return true;
7816a43cba3SVladimir Oltean 
7826a43cba3SVladimir Oltean 	return false;
7836a43cba3SVladimir Oltean }
7846a43cba3SVladimir Oltean 
7856a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */
7866a43cba3SVladimir Oltean static inline bool
7876a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
7886a43cba3SVladimir Oltean 			     const struct net_device *bridge_dev)
7896a43cba3SVladimir Oltean {
7906a43cba3SVladimir Oltean 	struct dsa_port *dp;
7916a43cba3SVladimir Oltean 
7926a43cba3SVladimir Oltean 	list_for_each_entry(dp, &dst->ports, list)
7936a43cba3SVladimir Oltean 		if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
7946a43cba3SVladimir Oltean 			return true;
7956a43cba3SVladimir Oltean 
7966a43cba3SVladimir Oltean 	return false;
7976a43cba3SVladimir Oltean }
7986a43cba3SVladimir Oltean 
7992bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
8002bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
8019d490b4eSVivien Didelot struct dsa_switch_ops {
80253da0ebaSVladimir Oltean 	/*
80353da0ebaSVladimir Oltean 	 * Tagging protocol helpers called for the CPU ports and DSA links.
80453da0ebaSVladimir Oltean 	 * @get_tag_protocol retrieves the initial tagging protocol and is
80553da0ebaSVladimir Oltean 	 * mandatory. Switches which can operate using multiple tagging
80653da0ebaSVladimir Oltean 	 * protocols should implement @change_tag_protocol and report in
80753da0ebaSVladimir Oltean 	 * @get_tag_protocol the tagger in current use.
80853da0ebaSVladimir Oltean 	 */
8095ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
8104d776482SFlorian Fainelli 						  int port,
8114d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
812bacf93b0SVladimir Oltean 	int	(*change_tag_protocol)(struct dsa_switch *ds,
81353da0ebaSVladimir Oltean 				       enum dsa_tag_protocol proto);
814dc452a47SVladimir Oltean 	/*
815dc452a47SVladimir Oltean 	 * Method for switch drivers to connect to the tagging protocol driver
816dc452a47SVladimir Oltean 	 * in current use. The switch driver can provide handlers for certain
817dc452a47SVladimir Oltean 	 * types of packets for switch management.
818dc452a47SVladimir Oltean 	 */
819dc452a47SVladimir Oltean 	int	(*connect_tag_protocol)(struct dsa_switch *ds,
820dc452a47SVladimir Oltean 					enum dsa_tag_protocol proto);
8217b314362SAndrew Lunn 
822fd292c18SVladimir Oltean 	/* Optional switch-wide initialization and destruction methods */
823c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
8245e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
825fd292c18SVladimir Oltean 
826fd292c18SVladimir Oltean 	/* Per-port initialization and destruction methods. Mandatory if the
827fd292c18SVladimir Oltean 	 * driver registers devlink port regions, optional otherwise.
828fd292c18SVladimir Oltean 	 */
829fd292c18SVladimir Oltean 	int	(*port_setup)(struct dsa_switch *ds, int port);
830fd292c18SVladimir Oltean 	void	(*port_teardown)(struct dsa_switch *ds, int port);
831fd292c18SVladimir Oltean 
8326819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
833c8f0b869SBen Hutchings 
834c8f0b869SBen Hutchings 	/*
835c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
836c8f0b869SBen Hutchings 	 */
837c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
838c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
839c8f0b869SBen Hutchings 			     int regnum, u16 val);
840c8f0b869SBen Hutchings 
841c8f0b869SBen Hutchings 	/*
842ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
843ec9436baSFlorian Fainelli 	 */
844ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
845ec9436baSFlorian Fainelli 				struct phy_device *phydev);
846ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
847ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
848ec9436baSFlorian Fainelli 
849ec9436baSFlorian Fainelli 	/*
85011d8f3ddSFlorian Fainelli 	 * PHYLINK integration
85111d8f3ddSFlorian Fainelli 	 */
852072eea6cSRussell King (Oracle) 	void	(*phylink_get_caps)(struct dsa_switch *ds, int port,
853072eea6cSRussell King (Oracle) 				    struct phylink_config *config);
85411d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
85511d8f3ddSFlorian Fainelli 				    unsigned long *supported,
85611d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
857bde01822SRussell King (Oracle) 	struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
858bde01822SRussell King (Oracle) 						      int port,
859bde01822SRussell King (Oracle) 						      phy_interface_t iface);
86011d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
86111d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
86211d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
86311d8f3ddSFlorian Fainelli 				      unsigned int mode,
86411d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
86511d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
86611d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
86711d8f3ddSFlorian Fainelli 					 unsigned int mode,
86811d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
86911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
87011d8f3ddSFlorian Fainelli 				       unsigned int mode,
87111d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
8725b502a7bSRussell King 				       struct phy_device *phydev,
8735b502a7bSRussell King 				       int speed, int duplex,
8745b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
87511d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
87611d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
87711d8f3ddSFlorian Fainelli 	/*
878c2ec5f2eSOleksij Rempel 	 * Port statistics counters.
879c8f0b869SBen Hutchings 	 */
88089f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
88189f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
882c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
883c8f0b869SBen Hutchings 				     int port, uint64_t *data);
88489f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
885cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
886cf963573SFlorian Fainelli 					 int port, uint64_t *data);
887487d3855SAlvin Šipraga 	void	(*get_eth_phy_stats)(struct dsa_switch *ds, int port,
888487d3855SAlvin Šipraga 				     struct ethtool_eth_phy_stats *phy_stats);
889487d3855SAlvin Šipraga 	void	(*get_eth_mac_stats)(struct dsa_switch *ds, int port,
890487d3855SAlvin Šipraga 				     struct ethtool_eth_mac_stats *mac_stats);
891487d3855SAlvin Šipraga 	void	(*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
892487d3855SAlvin Šipraga 				      struct ethtool_eth_ctrl_stats *ctrl_stats);
89367f38b1cSClément Léger 	void	(*get_rmon_stats)(struct dsa_switch *ds, int port,
89467f38b1cSClément Léger 				  struct ethtool_rmon_stats *rmon_stats,
89567f38b1cSClément Léger 				  const struct ethtool_rmon_hist_range **ranges);
896c2ec5f2eSOleksij Rempel 	void	(*get_stats64)(struct dsa_switch *ds, int port,
897c2ec5f2eSOleksij Rempel 				   struct rtnl_link_stats64 *s);
898*3d410403SOleksij Rempel 	void	(*get_pause_stats)(struct dsa_switch *ds, int port,
899*3d410403SOleksij Rempel 				   struct ethtool_pause_stats *pause_stats);
900a71acad9SOleksij Rempel 	void	(*self_test)(struct dsa_switch *ds, int port,
901a71acad9SOleksij Rempel 			     struct ethtool_test *etest, u64 *data);
90224462549SFlorian Fainelli 
90324462549SFlorian Fainelli 	/*
90419e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
90519e57c4eSFlorian Fainelli 	 */
90619e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
90719e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
90819e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
90919e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
91019e57c4eSFlorian Fainelli 
91119e57c4eSFlorian Fainelli 	/*
9120336369dSBrandon Streiff 	 * ethtool timestamp info
9130336369dSBrandon Streiff 	 */
9140336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
9150336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
9160336369dSBrandon Streiff 
9170336369dSBrandon Streiff 	/*
918d538eca8SVladimir Oltean 	 * DCB ops
919d538eca8SVladimir Oltean 	 */
920d538eca8SVladimir Oltean 	int	(*port_get_default_prio)(struct dsa_switch *ds, int port);
921d538eca8SVladimir Oltean 	int	(*port_set_default_prio)(struct dsa_switch *ds, int port,
922d538eca8SVladimir Oltean 					 u8 prio);
92347d75f78SVladimir Oltean 	int	(*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
92447d75f78SVladimir Oltean 	int	(*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
92547d75f78SVladimir Oltean 				      u8 prio);
92647d75f78SVladimir Oltean 	int	(*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
92747d75f78SVladimir Oltean 				      u8 prio);
928d538eca8SVladimir Oltean 
929d538eca8SVladimir Oltean 	/*
93024462549SFlorian Fainelli 	 * Suspend and resume
93124462549SFlorian Fainelli 	 */
93224462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
93324462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
934b2f2af21SFlorian Fainelli 
935b2f2af21SFlorian Fainelli 	/*
936b2f2af21SFlorian Fainelli 	 * Port enable/disable
937b2f2af21SFlorian Fainelli 	 */
938b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
939b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
94075104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
9417905288fSFlorian Fainelli 
9427905288fSFlorian Fainelli 	/*
94308f50061SVivien Didelot 	 * Port's MAC EEE settings
9447905288fSFlorian Fainelli 	 */
94508f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
9467905288fSFlorian Fainelli 			       struct ethtool_eee *e);
94708f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
9487905288fSFlorian Fainelli 			       struct ethtool_eee *e);
94951579c3fSGuenter Roeck 
9506793abb4SGuenter Roeck 	/* EEPROM access */
9516793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
9526793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
9536793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
9546793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
9556793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
9563d762a0fSGuenter Roeck 
9573d762a0fSGuenter Roeck 	/*
9583d762a0fSGuenter Roeck 	 * Register access.
9593d762a0fSGuenter Roeck 	 */
9603d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
9613d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
9623d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
963b73adef6SFlorian Fainelli 
964b73adef6SFlorian Fainelli 	/*
965e358bef7SVladimir Oltean 	 * Upper device tracking.
966e358bef7SVladimir Oltean 	 */
967e358bef7SVladimir Oltean 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
968e358bef7SVladimir Oltean 				       struct netdev_notifier_changeupper_info *info);
969e358bef7SVladimir Oltean 
970e358bef7SVladimir Oltean 	/*
971b73adef6SFlorian Fainelli 	 * Bridge integration
972b73adef6SFlorian Fainelli 	 */
97334a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
97471327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
975b079922bSVladimir Oltean 				    struct dsa_bridge bridge,
97606b9cce4SVladimir Oltean 				    bool *tx_fwd_offload,
97706b9cce4SVladimir Oltean 				    struct netlink_ext_ack *extack);
978f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
979d3eed0e5SVladimir Oltean 				     struct dsa_bridge bridge);
98043c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
981b73adef6SFlorian Fainelli 				      u8 state);
9827414af30STobias Waldekranz 	int	(*port_mst_state_set)(struct dsa_switch *ds, int port,
9837414af30STobias Waldekranz 				      const struct switchdev_mst_state *state);
984732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
9857414af30STobias Waldekranz 	int	(*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
986a8b659e7SVladimir Oltean 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
987a8b659e7SVladimir Oltean 					 struct switchdev_brport_flags flags,
988a8b659e7SVladimir Oltean 					 struct netlink_ext_ack *extack);
989a8b659e7SVladimir Oltean 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
990a8b659e7SVladimir Oltean 				     struct switchdev_brport_flags flags,
991a8b659e7SVladimir Oltean 				     struct netlink_ext_ack *extack);
99272c3b0c7SVladimir Oltean 	void	(*port_set_host_flood)(struct dsa_switch *ds, int port,
99372c3b0c7SVladimir Oltean 				       bool uc, bool mc);
9942a778e1bSVivien Didelot 
9952a778e1bSVivien Didelot 	/*
99611149536SVivien Didelot 	 * VLAN support
99711149536SVivien Didelot 	 */
998fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
99989153ed6SVladimir Oltean 				       bool vlan_filtering,
100089153ed6SVladimir Oltean 				       struct netlink_ext_ack *extack);
10011958d581SVladimir Oltean 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
100231046a5fSVladimir Oltean 				 const struct switchdev_obj_port_vlan *vlan,
100331046a5fSVladimir Oltean 				 struct netlink_ext_ack *extack);
100476e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
100576e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
10068e6598a7STobias Waldekranz 	int	(*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge,
10078e6598a7STobias Waldekranz 				 const struct switchdev_vlan_msti *msti);
10088e6598a7STobias Waldekranz 
100911149536SVivien Didelot 	/*
10102a778e1bSVivien Didelot 	 * Forwarding database
10112a778e1bSVivien Didelot 	 */
10121b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
1013c2693363SVladimir Oltean 				const unsigned char *addr, u16 vid,
1014c2693363SVladimir Oltean 				struct dsa_db db);
10152a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
1016c2693363SVladimir Oltean 				const unsigned char *addr, u16 vid,
1017c2693363SVladimir Oltean 				struct dsa_db db);
1018ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
10192bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
1020e212fa7cSVladimir Oltean 	int	(*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
1021c2693363SVladimir Oltean 			       const unsigned char *addr, u16 vid,
1022c2693363SVladimir Oltean 			       struct dsa_db db);
1023e212fa7cSVladimir Oltean 	int	(*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
1024c2693363SVladimir Oltean 			       const unsigned char *addr, u16 vid,
1025c2693363SVladimir Oltean 			       struct dsa_db db);
10268df30255SVivien Didelot 
10278df30255SVivien Didelot 	/*
10288df30255SVivien Didelot 	 * Multicast database
10298df30255SVivien Didelot 	 */
1030a52b2da7SVladimir Oltean 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
1031c2693363SVladimir Oltean 				const struct switchdev_obj_port_mdb *mdb,
1032c2693363SVladimir Oltean 				struct dsa_db db);
10338df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
1034c2693363SVladimir Oltean 				const struct switchdev_obj_port_mdb *mdb,
1035c2693363SVladimir Oltean 				struct dsa_db db);
1036bf9f2648SFlorian Fainelli 	/*
1037bf9f2648SFlorian Fainelli 	 * RXNFC
1038bf9f2648SFlorian Fainelli 	 */
1039bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
1040bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
1041bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
1042bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
1043f50f2127SFlorian Fainelli 
1044f50f2127SFlorian Fainelli 	/*
1045f50f2127SFlorian Fainelli 	 * TC integration
1046f50f2127SFlorian Fainelli 	 */
1047ed11bb1fSVladimir Oltean 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
1048ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
1049ed11bb1fSVladimir Oltean 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
1050ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
1051ed11bb1fSVladimir Oltean 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
1052ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls, bool ingress);
1053f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
1054f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
10550148bb50SVladimir Oltean 				   bool ingress, struct netlink_ext_ack *extack);
1056f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
1057f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
105834297176SVladimir Oltean 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
105934297176SVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer);
106034297176SVladimir Oltean 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
106147d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
106247d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
106340ef2c93SVivien Didelot 
106440ef2c93SVivien Didelot 	/*
106540ef2c93SVivien Didelot 	 * Cross-chip operations
106640ef2c93SVivien Didelot 	 */
1067f66a6a69SVladimir Oltean 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
1068f66a6a69SVladimir Oltean 					 int sw_index, int port,
106906b9cce4SVladimir Oltean 					 struct dsa_bridge bridge,
107006b9cce4SVladimir Oltean 					 struct netlink_ext_ack *extack);
1071f66a6a69SVladimir Oltean 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
1072f66a6a69SVladimir Oltean 					  int sw_index, int port,
1073d3eed0e5SVladimir Oltean 					  struct dsa_bridge bridge);
1074058102a6STobias Waldekranz 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
1075058102a6STobias Waldekranz 					int port);
1076058102a6STobias Waldekranz 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
1077dedd6a00SVladimir Oltean 				      int port, struct dsa_lag lag,
1078058102a6STobias Waldekranz 				      struct netdev_lag_upper_info *info);
1079058102a6STobias Waldekranz 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
1080dedd6a00SVladimir Oltean 				       int port, struct dsa_lag lag);
10810336369dSBrandon Streiff 
10820336369dSBrandon Streiff 	/*
10830336369dSBrandon Streiff 	 * PTP functionality
10840336369dSBrandon Streiff 	 */
10850336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
10860336369dSBrandon Streiff 				     struct ifreq *ifr);
10870336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
10880336369dSBrandon Streiff 				     struct ifreq *ifr);
10895c5416f5SYangbo Lu 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
10905c5416f5SYangbo Lu 				 struct sk_buff *skb);
109190af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
109290af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
109397a69a0dSVladimir Oltean 
10940f06b855SAndrew Lunn 	/* Devlink parameters, etc */
10956b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
10966b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
10976b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
10986b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
10990f06b855SAndrew Lunn 	int	(*devlink_info_get)(struct dsa_switch *ds,
11000f06b855SAndrew Lunn 				    struct devlink_info_req *req,
11010f06b855SAndrew Lunn 				    struct netlink_ext_ack *extack);
11022a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
11032a6ef763SVladimir Oltean 				       unsigned int sb_index, u16 pool_index,
11042a6ef763SVladimir Oltean 				       struct devlink_sb_pool_info *pool_info);
11052a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
11062a6ef763SVladimir Oltean 				       u16 pool_index, u32 size,
11072a6ef763SVladimir Oltean 				       enum devlink_sb_threshold_type threshold_type,
11082a6ef763SVladimir Oltean 				       struct netlink_ext_ack *extack);
11092a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
11102a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
11112a6ef763SVladimir Oltean 					    u32 *p_threshold);
11122a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
11132a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
11142a6ef763SVladimir Oltean 					    u32 threshold,
11152a6ef763SVladimir Oltean 					    struct netlink_ext_ack *extack);
11162a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
11172a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
11182a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
11192a6ef763SVladimir Oltean 					       u16 *p_pool_index, u32 *p_threshold);
11202a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
11212a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
11222a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
11232a6ef763SVladimir Oltean 					       u16 pool_index, u32 threshold,
11242a6ef763SVladimir Oltean 					       struct netlink_ext_ack *extack);
11252a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
11262a6ef763SVladimir Oltean 					   unsigned int sb_index);
11272a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
11282a6ef763SVladimir Oltean 					    unsigned int sb_index);
11292a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
11302a6ef763SVladimir Oltean 						unsigned int sb_index, u16 pool_index,
11312a6ef763SVladimir Oltean 						u32 *p_cur, u32 *p_max);
11322a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
11332a6ef763SVladimir Oltean 						   unsigned int sb_index, u16 tc_index,
11342a6ef763SVladimir Oltean 						   enum devlink_sb_pool_type pool_type,
11352a6ef763SVladimir Oltean 						   u32 *p_cur, u32 *p_max);
1136bfcb8132SVladimir Oltean 
1137bfcb8132SVladimir Oltean 	/*
1138bfcb8132SVladimir Oltean 	 * MTU change functionality. Switches can also adjust their MRU through
1139bfcb8132SVladimir Oltean 	 * this method. By MTU, one understands the SDU (L2 payload) length.
1140bfcb8132SVladimir Oltean 	 * If the switch needs to account for the DSA tag on the CPU port, this
1141ab88d64aSRandy Dunlap 	 * method needs to do so privately.
1142bfcb8132SVladimir Oltean 	 */
1143bfcb8132SVladimir Oltean 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
1144bfcb8132SVladimir Oltean 				   int new_mtu);
1145bfcb8132SVladimir Oltean 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
1146058102a6STobias Waldekranz 
1147058102a6STobias Waldekranz 	/*
1148058102a6STobias Waldekranz 	 * LAG integration
1149058102a6STobias Waldekranz 	 */
1150058102a6STobias Waldekranz 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
1151058102a6STobias Waldekranz 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
1152dedd6a00SVladimir Oltean 				 struct dsa_lag lag,
1153058102a6STobias Waldekranz 				 struct netdev_lag_upper_info *info);
1154058102a6STobias Waldekranz 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
1155dedd6a00SVladimir Oltean 				  struct dsa_lag lag);
115618596f50SGeorge McCollister 
115718596f50SGeorge McCollister 	/*
115818596f50SGeorge McCollister 	 * HSR integration
115918596f50SGeorge McCollister 	 */
116018596f50SGeorge McCollister 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
116118596f50SGeorge McCollister 				 struct net_device *hsr);
116218596f50SGeorge McCollister 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
116318596f50SGeorge McCollister 				  struct net_device *hsr);
1164c595c433SHoratiu Vultur 
1165c595c433SHoratiu Vultur 	/*
1166c595c433SHoratiu Vultur 	 * MRP integration
1167c595c433SHoratiu Vultur 	 */
1168c595c433SHoratiu Vultur 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
1169c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
1170c595c433SHoratiu Vultur 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
1171c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
1172c595c433SHoratiu Vultur 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1173c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
1174c595c433SHoratiu Vultur 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1175c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
11765da11eb4SVladimir Oltean 
11775da11eb4SVladimir Oltean 	/*
11785da11eb4SVladimir Oltean 	 * tag_8021q operations
11795da11eb4SVladimir Oltean 	 */
11805da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
11815da11eb4SVladimir Oltean 				      u16 flags);
11825da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1183295ab96fSVladimir Oltean 
1184295ab96fSVladimir Oltean 	/*
1185295ab96fSVladimir Oltean 	 * DSA master tracking operations
1186295ab96fSVladimir Oltean 	 */
1187295ab96fSVladimir Oltean 	void	(*master_state_change)(struct dsa_switch *ds,
1188295ab96fSVladimir Oltean 				       const struct net_device *master,
1189295ab96fSVladimir Oltean 				       bool operational);
11906b297524SAndrew Lunn };
11916b297524SAndrew Lunn 
11926b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
11936b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
11946b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
11956b297524SAndrew Lunn 
11966b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
11976b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
11986b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
11996b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
12006b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
12016b297524SAndrew Lunn 				const struct devlink_param *params,
12026b297524SAndrew Lunn 				size_t params_count);
12036b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
12046b297524SAndrew Lunn 				   const struct devlink_param *params,
12056b297524SAndrew Lunn 				   size_t params_count);
12065cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
12075cd73fbdSAndrew Lunn 				  const char *resource_name,
12085cd73fbdSAndrew Lunn 				  u64 resource_size,
12095cd73fbdSAndrew Lunn 				  u64 resource_id,
12105cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
12115cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
12125cd73fbdSAndrew Lunn 
12135cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
12145cd73fbdSAndrew Lunn 
12155cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
12165cd73fbdSAndrew Lunn 					   u64 resource_id,
12175cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
12185cd73fbdSAndrew Lunn 					   void *occ_get_priv);
12195cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
12205cd73fbdSAndrew Lunn 					     u64 resource_id);
122197c82c23SAndrew Lunn struct devlink_region *
122297c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds,
122397c82c23SAndrew Lunn 			  const struct devlink_region_ops *ops,
122497c82c23SAndrew Lunn 			  u32 region_max_snapshots, u64 region_size);
122508156ba4SAndrew Lunn struct devlink_region *
122608156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds,
122708156ba4SAndrew Lunn 			       int port,
122808156ba4SAndrew Lunn 			       const struct devlink_port_region_ops *ops,
122908156ba4SAndrew Lunn 			       u32 region_max_snapshots, u64 region_size);
123097c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region);
123197c82c23SAndrew Lunn 
1232e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
12335cd73fbdSAndrew Lunn 
12346b297524SAndrew Lunn struct dsa_devlink_priv {
12356b297524SAndrew Lunn 	struct dsa_switch *ds;
1236c8f0b869SBen Hutchings };
1237c8f0b869SBen Hutchings 
1238ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1239ccc3e6b0SAndrew Lunn {
1240ccc3e6b0SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1241ccc3e6b0SAndrew Lunn 
1242ccc3e6b0SAndrew Lunn 	return dl_priv->ds;
1243ccc3e6b0SAndrew Lunn }
1244ccc3e6b0SAndrew Lunn 
12457d1e2a10SAndrew Lunn static inline
12467d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
12477d1e2a10SAndrew Lunn {
12487d1e2a10SAndrew Lunn 	struct devlink *dl = port->devlink;
12497d1e2a10SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
12507d1e2a10SAndrew Lunn 
12517d1e2a10SAndrew Lunn 	return dl_priv->ds;
12527d1e2a10SAndrew Lunn }
12537d1e2a10SAndrew Lunn 
12547d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port)
12557d1e2a10SAndrew Lunn {
12567d1e2a10SAndrew Lunn 	return port->index;
12577d1e2a10SAndrew Lunn }
12587d1e2a10SAndrew Lunn 
1259ab3d408dSFlorian Fainelli struct dsa_switch_driver {
1260ab3d408dSFlorian Fainelli 	struct list_head	list;
1261a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
1262ab3d408dSFlorian Fainelli };
1263ab3d408dSFlorian Fainelli 
126414b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
1265c8f0b869SBen Hutchings 
12667e580490SVladimir Oltean bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
12677e580490SVladimir Oltean 				 const unsigned char *addr, u16 vid,
12687e580490SVladimir Oltean 				 struct dsa_db db);
12697e580490SVladimir Oltean bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
12707e580490SVladimir Oltean 				 const struct switchdev_obj_port_mdb *mdb,
12717e580490SVladimir Oltean 				 struct dsa_db db);
1272f9cef64fSVladimir Oltean 
127373a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
12749eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev)
1275c6e970a0SAndrew Lunn {
1276c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
1277717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
1278c6e970a0SAndrew Lunn #endif
1279c6e970a0SAndrew Lunn 	return false;
1280c6e970a0SAndrew Lunn }
1281c6e970a0SAndrew Lunn 
12829790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail
12839790cf20SVladimir Oltean  * tags, which don't break dissection) can be treated the same from the
12849790cf20SVladimir Oltean  * perspective of the flow dissector.
12859790cf20SVladimir Oltean  *
12869790cf20SVladimir Oltean  * We need to return:
12879790cf20SVladimir Oltean  *  - offset: the (B - A) difference between:
12889790cf20SVladimir Oltean  *    A. the position of the real EtherType and
12899790cf20SVladimir Oltean  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
12909790cf20SVladimir Oltean  *       after the normal EtherType was supposed to be)
12919790cf20SVladimir Oltean  *    The offset in bytes is exactly equal to the tagger overhead (and half of
12929790cf20SVladimir Oltean  *    that, in __be16 shorts).
12939790cf20SVladimir Oltean  *
12949790cf20SVladimir Oltean  *  - proto: the value of the real EtherType.
12959790cf20SVladimir Oltean  */
12969790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
12979790cf20SVladimir Oltean 						__be16 *proto, int *offset)
12989790cf20SVladimir Oltean {
12999790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
13009790cf20SVladimir Oltean 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
13014e500251SVladimir Oltean 	int tag_len = ops->needed_headroom;
13029790cf20SVladimir Oltean 
13039790cf20SVladimir Oltean 	*offset = tag_len;
13049790cf20SVladimir Oltean 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
13059790cf20SVladimir Oltean #endif
13069790cf20SVladimir Oltean }
13079790cf20SVladimir Oltean 
13084cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
13094cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev)
13104cfab356SFlorian Fainelli {
13114cfab356SFlorian Fainelli 	int err = -EOPNOTSUPP;
13124cfab356SFlorian Fainelli 
13134cfab356SFlorian Fainelli 	if (!dev->dsa_ptr)
13144cfab356SFlorian Fainelli 		return err;
13154cfab356SFlorian Fainelli 
13164cfab356SFlorian Fainelli 	if (!dev->dsa_ptr->netdev_ops)
13174cfab356SFlorian Fainelli 		return err;
13184cfab356SFlorian Fainelli 
13194cfab356SFlorian Fainelli 	return 0;
13204cfab356SFlorian Fainelli }
13214cfab356SFlorian Fainelli 
1322a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
13234cfab356SFlorian Fainelli 				    int cmd)
13244cfab356SFlorian Fainelli {
13254cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *ops;
13264cfab356SFlorian Fainelli 	int err;
13274cfab356SFlorian Fainelli 
13284cfab356SFlorian Fainelli 	err = __dsa_netdevice_ops_check(dev);
13294cfab356SFlorian Fainelli 	if (err)
13304cfab356SFlorian Fainelli 		return err;
13314cfab356SFlorian Fainelli 
13324cfab356SFlorian Fainelli 	ops = dev->dsa_ptr->netdev_ops;
13334cfab356SFlorian Fainelli 
1334a7605370SArnd Bergmann 	return ops->ndo_eth_ioctl(dev, ifr, cmd);
13354cfab356SFlorian Fainelli }
13364cfab356SFlorian Fainelli #else
1337a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
13384cfab356SFlorian Fainelli 				    int cmd)
13394cfab356SFlorian Fainelli {
13404cfab356SFlorian Fainelli 	return -EOPNOTSUPP;
13414cfab356SFlorian Fainelli }
13424cfab356SFlorian Fainelli #endif
13434cfab356SFlorian Fainelli 
134483c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
134523c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
13460650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds);
13473b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1348a2614140SVladimir Oltean void dsa_flush_workqueue(void);
1349ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
1350ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
1351ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
1352ea825e70SFlorian Fainelli #else
1353ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
1354ea825e70SFlorian Fainelli {
1355ea825e70SFlorian Fainelli 	return 0;
1356ea825e70SFlorian Fainelli }
1357ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
1358ea825e70SFlorian Fainelli {
1359ea825e70SFlorian Fainelli 	return 0;
1360ea825e70SFlorian Fainelli }
1361ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
1362ea825e70SFlorian Fainelli 
136360724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
1364a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev);
136560724d4bSFlorian Fainelli #else
1366a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev)
1367a5e3c9baSVladimir Oltean {
1368a5e3c9baSVladimir Oltean 	return false;
1369a5e3c9baSVladimir Oltean }
137060724d4bSFlorian Fainelli #endif
137160724d4bSFlorian Fainelli 
137297a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
137311d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1374cf963573SFlorian Fainelli 
1375d3b8c049SAndrew Lunn struct dsa_tag_driver {
1376d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
1377d3b8c049SAndrew Lunn 	struct list_head list;
1378d3b8c049SAndrew Lunn 	struct module *owner;
1379d3b8c049SAndrew Lunn };
1380d3b8c049SAndrew Lunn 
1381d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1382d3b8c049SAndrew Lunn 			      unsigned int count,
1383d3b8c049SAndrew Lunn 			      struct module *owner);
1384d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1385d3b8c049SAndrew Lunn 				unsigned int count);
1386d3b8c049SAndrew Lunn 
1387d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
1388d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
1389d3b8c049SAndrew Lunn {									\
1390d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
1391d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
1392d3b8c049SAndrew Lunn 	return 0;							\
1393d3b8c049SAndrew Lunn }									\
1394d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
1395d3b8c049SAndrew Lunn 									\
1396d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
1397d3b8c049SAndrew Lunn {									\
1398d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
1399d3b8c049SAndrew Lunn }									\
1400d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
1401d3b8c049SAndrew Lunn 
1402d3b8c049SAndrew Lunn /**
1403d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1404d3b8c049SAndrew Lunn  * drivers
1405c7d9a675SLuiz Angelo Daros de Luca  * @__ops_array: Array of tag driver structures
1406d3b8c049SAndrew Lunn  *
1407d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
1408d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
1409d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
1410d3b8c049SAndrew Lunn  */
1411d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
1412d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1413d3b8c049SAndrew Lunn 
1414d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1415d3b8c049SAndrew Lunn 
1416d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
1417d3b8c049SAndrew Lunn  * drivers
1418d3b8c049SAndrew Lunn  */
1419d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
1420d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1421d3b8c049SAndrew Lunn 	.ops = &__ops,							\
1422d3b8c049SAndrew Lunn }
1423d3b8c049SAndrew Lunn 
1424d3b8c049SAndrew Lunn /**
1425d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1426d3b8c049SAndrew Lunn  * driver
1427d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
1428d3b8c049SAndrew Lunn  *
1429d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
1430d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
1431d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
1432d3b8c049SAndrew Lunn  */
1433d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
1434d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
1435d3b8c049SAndrew Lunn 									\
1436d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1437d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
1438d3b8c049SAndrew Lunn };									\
1439d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
144091da11f8SLennert Buytenhek #endif
1441d3b8c049SAndrew Lunn 
1442