xref: /openbmc/linux/include/net/dsa.h (revision 5f6c2d49)
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 
2519d05ea7SVladimir Oltean struct dsa_8021q_context;
26f50f2127SFlorian Fainelli struct tc_action;
274d56a29fSRussell King struct phy_device;
284d56a29fSRussell King struct fixed_phy_status;
2911d8f3ddSFlorian Fainelli struct phylink_link_state;
30f50f2127SFlorian Fainelli 
310b42f033SAndrew Lunn #define DSA_TAG_PROTO_NONE_VALUE		0
320b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_VALUE		1
330b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE	2
340b42f033SAndrew Lunn #define DSA_TAG_PROTO_DSA_VALUE			3
350b42f033SAndrew Lunn #define DSA_TAG_PROTO_EDSA_VALUE		4
360b42f033SAndrew Lunn #define DSA_TAG_PROTO_GSWIP_VALUE		5
370b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9477_VALUE		6
380b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9893_VALUE		7
390b42f033SAndrew Lunn #define DSA_TAG_PROTO_LAN9303_VALUE		8
400b42f033SAndrew Lunn #define DSA_TAG_PROTO_MTK_VALUE			9
410b42f033SAndrew Lunn #define DSA_TAG_PROTO_QCA_VALUE			10
420b42f033SAndrew Lunn #define DSA_TAG_PROTO_TRAILER_VALUE		11
43f9bbe447SVladimir Oltean #define DSA_TAG_PROTO_8021Q_VALUE		12
44227d07a0SVladimir Oltean #define DSA_TAG_PROTO_SJA1105_VALUE		13
45016e43a2STristram Ha #define DSA_TAG_PROTO_KSZ8795_VALUE		14
468dce89aaSVladimir Oltean #define DSA_TAG_PROTO_OCELOT_VALUE		15
4748fda74fSOleksij Rempel #define DSA_TAG_PROTO_AR9331_VALUE		16
48efd7fe68SLinus Walleij #define DSA_TAG_PROTO_RTL4_A_VALUE		17
4901ef09caSKurt Kanzenbach #define DSA_TAG_PROTO_HELLCREEK_VALUE		18
5054a52823SGeorge McCollister #define DSA_TAG_PROTO_XRS700X_VALUE		19
517c83a7c5SVladimir Oltean #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE	20
527c4bb540SVladimir Oltean #define DSA_TAG_PROTO_SEVILLE_VALUE		21
53964dbf18SÁlvaro Fernández Rojas #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE		22
544913b8ebSVladimir Oltean #define DSA_TAG_PROTO_SJA1110_VALUE		23
551521d5adSAlvin Šipraga #define DSA_TAG_PROTO_RTL8_4_VALUE		24
56cd87fecdSLuiz Angelo Daros de Luca #define DSA_TAG_PROTO_RTL8_4T_VALUE		25
57a08d6a6dSClément Léger #define DSA_TAG_PROTO_RZN1_A5PSW_VALUE		26
58092f8751SPrasanna Vengateshan #define DSA_TAG_PROTO_LAN937X_VALUE		27
590b42f033SAndrew Lunn 
60ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
610b42f033SAndrew Lunn 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
620b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
63964dbf18SÁlvaro Fernández Rojas 	DSA_TAG_PROTO_BRCM_LEGACY	= DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
640b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
650b42f033SAndrew Lunn 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
660b42f033SAndrew Lunn 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
670b42f033SAndrew Lunn 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
680b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
690b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
700b42f033SAndrew Lunn 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
710b42f033SAndrew Lunn 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
720b42f033SAndrew Lunn 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
730b42f033SAndrew Lunn 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
74f9bbe447SVladimir Oltean 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
75227d07a0SVladimir Oltean 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
76016e43a2STristram Ha 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
778dce89aaSVladimir Oltean 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
7848fda74fSOleksij Rempel 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
79efd7fe68SLinus Walleij 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
8001ef09caSKurt Kanzenbach 	DSA_TAG_PROTO_HELLCREEK		= DSA_TAG_PROTO_HELLCREEK_VALUE,
8154a52823SGeorge McCollister 	DSA_TAG_PROTO_XRS700X		= DSA_TAG_PROTO_XRS700X_VALUE,
827c83a7c5SVladimir Oltean 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
837c4bb540SVladimir Oltean 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
844913b8ebSVladimir Oltean 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
851521d5adSAlvin Šipraga 	DSA_TAG_PROTO_RTL8_4		= DSA_TAG_PROTO_RTL8_4_VALUE,
86cd87fecdSLuiz Angelo Daros de Luca 	DSA_TAG_PROTO_RTL8_4T		= DSA_TAG_PROTO_RTL8_4T_VALUE,
87a08d6a6dSClément Léger 	DSA_TAG_PROTO_RZN1_A5PSW	= DSA_TAG_PROTO_RZN1_A5PSW_VALUE,
88092f8751SPrasanna Vengateshan 	DSA_TAG_PROTO_LAN937X		= DSA_TAG_PROTO_LAN937X_VALUE,
89ac7a04c3SFlorian Fainelli };
905037d532SFlorian Fainelli 
9190af1059SBrandon Streiff struct dsa_switch;
923e8a72d1SFlorian Fainelli 
9368277a2cSJohn Crispin struct dsa_device_ops {
9468277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
9529a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
962e8cb1b3SVladimir Oltean 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
97598a9680SJohn Crispin 			     int *offset);
987f297314SVladimir Oltean 	int (*connect)(struct dsa_switch *ds);
997f297314SVladimir Oltean 	void (*disconnect)(struct dsa_switch *ds);
1004e500251SVladimir Oltean 	unsigned int needed_headroom;
1014e500251SVladimir Oltean 	unsigned int needed_tailroom;
102875138f8SAndrew Lunn 	const char *name;
103056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
104c3975400SVladimir Oltean 	/* Some tagging protocols either mangle or shift the destination MAC
105c3975400SVladimir Oltean 	 * address, in which case the DSA master would drop packets on ingress
106c3975400SVladimir Oltean 	 * if what it understands out of the destination MAC address is not in
107c3975400SVladimir Oltean 	 * its RX filter.
108c3975400SVladimir Oltean 	 */
109c3975400SVladimir Oltean 	bool promisc_on_master;
11068277a2cSJohn Crispin };
11168277a2cSJohn Crispin 
1124cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the
1134cfab356SFlorian Fainelli  * DSA layer on top of the DSA CPU/management net_device instance. This is
1144cfab356SFlorian Fainelli  * used by the core net_device layer while calling various net_device_ops
1154cfab356SFlorian Fainelli  * function pointers.
1164cfab356SFlorian Fainelli  */
1174cfab356SFlorian Fainelli struct dsa_netdevice_ops {
118a7605370SArnd Bergmann 	int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
1194cfab356SFlorian Fainelli 			     int cmd);
1204cfab356SFlorian Fainelli };
1214cfab356SFlorian Fainelli 
122dedd6a00SVladimir Oltean struct dsa_lag {
123dedd6a00SVladimir Oltean 	struct net_device *dev;
124dedd6a00SVladimir Oltean 	unsigned int id;
125e212fa7cSVladimir Oltean 	struct mutex fdb_lock;
126e212fa7cSVladimir Oltean 	struct list_head fdbs;
127dedd6a00SVladimir Oltean 	refcount_t refcount;
128dedd6a00SVladimir Oltean };
129dedd6a00SVladimir Oltean 
130cf50dcc2SBen Hutchings struct dsa_switch_tree {
13183c0afaeSAndrew Lunn 	struct list_head	list;
13283c0afaeSAndrew Lunn 
133b035c88cSVladimir Oltean 	/* List of switch ports */
134b035c88cSVladimir Oltean 	struct list_head ports;
135b035c88cSVladimir Oltean 
136f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
137f515f192SVivien Didelot 	struct raw_notifier_head	nh;
138f515f192SVivien Didelot 
13983c0afaeSAndrew Lunn 	/* Tree identifier */
14049463b7fSVivien Didelot 	unsigned int index;
14183c0afaeSAndrew Lunn 
14283c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
14383c0afaeSAndrew Lunn 	struct kref refcount;
14483c0afaeSAndrew Lunn 
145b035c88cSVladimir Oltean 	/* Maps offloaded LAG netdevs to a zero-based linear ID for
146b035c88cSVladimir Oltean 	 * drivers that need it.
147b035c88cSVladimir Oltean 	 */
148dedd6a00SVladimir Oltean 	struct dsa_lag **lags;
149b035c88cSVladimir Oltean 
150357f203bSVladimir Oltean 	/* Tagging protocol operations */
151357f203bSVladimir Oltean 	const struct dsa_device_ops *tag_ops;
152357f203bSVladimir Oltean 
153deff7107STobias Waldekranz 	/* Default tagging protocol preferred by the switches in this
154deff7107STobias Waldekranz 	 * tree.
155deff7107STobias Waldekranz 	 */
156deff7107STobias Waldekranz 	enum dsa_tag_protocol default_proto;
157deff7107STobias Waldekranz 
1584b026e82SVladimir Oltean 	/* Has this tree been applied to the hardware? */
1594b026e82SVladimir Oltean 	bool setup;
1604b026e82SVladimir Oltean 
161cf50dcc2SBen Hutchings 	/*
162cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
163cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
164cf50dcc2SBen Hutchings 	 */
165cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
166cf85d08fSLennert Buytenhek 
167c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
168c5f51765SVivien Didelot 	struct list_head rtable;
169058102a6STobias Waldekranz 
170b035c88cSVladimir Oltean 	/* Length of "lags" array */
171058102a6STobias Waldekranz 	unsigned int lags_len;
1725b22d366SVladimir Oltean 
1735b22d366SVladimir Oltean 	/* Track the largest switch index within a tree */
1745b22d366SVladimir Oltean 	unsigned int last_switch;
175cf50dcc2SBen Hutchings };
176cf50dcc2SBen Hutchings 
1773d4a0a2aSVladimir Oltean /* LAG IDs are one-based, the dst->lags array is zero-based */
178058102a6STobias Waldekranz #define dsa_lags_foreach_id(_id, _dst)				\
1793d4a0a2aSVladimir Oltean 	for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++)	\
1803d4a0a2aSVladimir Oltean 		if ((_dst)->lags[(_id) - 1])
181058102a6STobias Waldekranz 
182058102a6STobias Waldekranz #define dsa_lag_foreach_port(_dp, _dst, _lag)			\
183058102a6STobias Waldekranz 	list_for_each_entry((_dp), &(_dst)->ports, list)	\
184dedd6a00SVladimir Oltean 		if (dsa_port_offloads_lag((_dp), (_lag)))
185058102a6STobias Waldekranz 
18618596f50SGeorge McCollister #define dsa_hsr_foreach_port(_dp, _ds, _hsr)			\
18718596f50SGeorge McCollister 	list_for_each_entry((_dp), &(_ds)->dst->ports, list)	\
18818596f50SGeorge McCollister 		if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
18918596f50SGeorge McCollister 
190dedd6a00SVladimir Oltean static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
191058102a6STobias Waldekranz 					    unsigned int id)
192058102a6STobias Waldekranz {
1933d4a0a2aSVladimir Oltean 	/* DSA LAG IDs are one-based, dst->lags is zero-based */
1943d4a0a2aSVladimir Oltean 	return dst->lags[id - 1];
195058102a6STobias Waldekranz }
196058102a6STobias Waldekranz 
197058102a6STobias Waldekranz static inline int dsa_lag_id(struct dsa_switch_tree *dst,
19846a76724SVladimir Oltean 			     struct net_device *lag_dev)
199058102a6STobias Waldekranz {
200058102a6STobias Waldekranz 	unsigned int id;
201058102a6STobias Waldekranz 
202058102a6STobias Waldekranz 	dsa_lags_foreach_id(id, dst) {
203dedd6a00SVladimir Oltean 		struct dsa_lag *lag = dsa_lag_by_id(dst, id);
204dedd6a00SVladimir Oltean 
205dedd6a00SVladimir Oltean 		if (lag->dev == lag_dev)
206dedd6a00SVladimir Oltean 			return lag->id;
207058102a6STobias Waldekranz 	}
208058102a6STobias Waldekranz 
209058102a6STobias Waldekranz 	return -ENODEV;
210058102a6STobias Waldekranz }
211058102a6STobias Waldekranz 
21234297176SVladimir Oltean /* TC matchall action types */
213f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
214f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
21534297176SVladimir Oltean 	DSA_PORT_MALL_POLICER,
216f50f2127SFlorian Fainelli };
217f50f2127SFlorian Fainelli 
218f50f2127SFlorian Fainelli /* TC mirroring entry */
219f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
220f50f2127SFlorian Fainelli 	u8 to_local_port;
221f50f2127SFlorian Fainelli 	bool ingress;
222f50f2127SFlorian Fainelli };
223f50f2127SFlorian Fainelli 
22434297176SVladimir Oltean /* TC port policer entry */
22534297176SVladimir Oltean struct dsa_mall_policer_tc_entry {
2265f035af7SPo Liu 	u32 burst;
22734297176SVladimir Oltean 	u64 rate_bytes_per_sec;
22834297176SVladimir Oltean };
22934297176SVladimir Oltean 
230f50f2127SFlorian Fainelli /* TC matchall entry */
231f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
232f50f2127SFlorian Fainelli 	struct list_head list;
233f50f2127SFlorian Fainelli 	unsigned long cookie;
234f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
235f50f2127SFlorian Fainelli 	union {
236f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
23734297176SVladimir Oltean 		struct dsa_mall_policer_tc_entry policer;
238f50f2127SFlorian Fainelli 	};
239f50f2127SFlorian Fainelli };
240f50f2127SFlorian Fainelli 
241d3eed0e5SVladimir Oltean struct dsa_bridge {
242d3eed0e5SVladimir Oltean 	struct net_device *dev;
243d3eed0e5SVladimir Oltean 	unsigned int num;
244857fdd74SVladimir Oltean 	bool tx_fwd_offload;
245d3eed0e5SVladimir Oltean 	refcount_t refcount;
246d3eed0e5SVladimir Oltean };
247f50f2127SFlorian Fainelli 
248c8b09808SAndrew Lunn struct dsa_port {
249f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
250f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
251f8b8b1cdSVivien Didelot 	 */
252f8b8b1cdSVivien Didelot 	union {
253f8b8b1cdSVivien Didelot 		struct net_device *master;
254f8b8b1cdSVivien Didelot 		struct net_device *slave;
255f8b8b1cdSVivien Didelot 	};
256f8b8b1cdSVivien Didelot 
257357f203bSVladimir Oltean 	/* Copy of the tagging protocol operations, for quicker access
258357f203bSVladimir Oltean 	 * in the data path. Valid only for the CPU ports.
259357f203bSVladimir Oltean 	 */
26015240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
26115240248SVivien Didelot 
2623e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
2633e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
26429a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
2653e41f93bSVivien Didelot 
26606251258SVladimir Oltean 	struct dsa_switch	*ds;
26706251258SVladimir Oltean 
26806251258SVladimir Oltean 	unsigned int		index;
26906251258SVladimir Oltean 
270057cad2cSVivien Didelot 	enum {
271057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
272057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
273057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
274057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
275057cad2cSVivien Didelot 	} type;
276057cad2cSVivien Didelot 
27771e0bbdeSFlorian Fainelli 	const char		*name;
27868b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
27983216e39SMichael Walle 	u8			mac[ETH_ALEN];
280b08db33dSVladimir Oltean 
281b08db33dSVladimir Oltean 	u8			stp_state;
282b08db33dSVladimir Oltean 
2831b26d364SVladimir Oltean 	/* Warning: the following bit fields are not atomic, and updating them
2841b26d364SVladimir Oltean 	 * can only be done from code paths where concurrency is not possible
2851b26d364SVladimir Oltean 	 * (probe time or under rtnl_lock).
2861b26d364SVladimir Oltean 	 */
28763cfc657SVladimir Oltean 	u8			vlan_filtering:1;
28863cfc657SVladimir Oltean 
28963cfc657SVladimir Oltean 	/* Managed by DSA on user ports and by drivers on CPU and DSA ports */
29063cfc657SVladimir Oltean 	u8			learning:1;
29163cfc657SVladimir Oltean 
29263cfc657SVladimir Oltean 	u8			lag_tx_enabled:1;
29363cfc657SVladimir Oltean 
294295ab96fSVladimir Oltean 	/* Master state bits, valid only on CPU ports */
295295ab96fSVladimir Oltean 	u8			master_admin_up:1;
296295ab96fSVladimir Oltean 	u8			master_oper_up:1;
297295ab96fSVladimir Oltean 
298acc43b7bSVladimir Oltean 	/* Valid only on user ports */
299acc43b7bSVladimir Oltean 	u8			cpu_port_in_lag:1;
300acc43b7bSVladimir 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 
56095f510d0SVladimir Oltean #define dsa_tree_for_each_user_port_continue_reverse(_dp, _dst) \
56195f510d0SVladimir Oltean 	list_for_each_entry_continue_reverse((_dp), &(_dst)->ports, list) \
56295f510d0SVladimir Oltean 		if (dsa_port_is_user((_dp)))
56395f510d0SVladimir Oltean 
5645dc760d1SVladimir Oltean #define dsa_tree_for_each_cpu_port(_dp, _dst) \
5655dc760d1SVladimir Oltean 	list_for_each_entry((_dp), &(_dst)->ports, list) \
5665dc760d1SVladimir Oltean 		if (dsa_port_is_cpu((_dp)))
5675dc760d1SVladimir Oltean 
56882b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \
56982b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
57082b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
57182b31898SVladimir Oltean 
57282b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
57382b31898SVladimir Oltean 	list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
57482b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
57582b31898SVladimir Oltean 
57682b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
57782b31898SVladimir Oltean 	list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
57882b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
57982b31898SVladimir Oltean 
58082b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \
58182b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
58282b31898SVladimir Oltean 		if (!dsa_port_is_unused((_dp)))
58382b31898SVladimir Oltean 
58482b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \
58582b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
58682b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
58782b31898SVladimir Oltean 
58882b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \
58982b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
59082b31898SVladimir Oltean 		if (dsa_port_is_cpu((_dp)))
59182b31898SVladimir Oltean 
592bacf93b0SVladimir Oltean #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \
593bacf93b0SVladimir Oltean 	dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
594bacf93b0SVladimir Oltean 		if (dsa_port_is_cpu((_dp)))
595bacf93b0SVladimir Oltean 
59602bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
59702bc6e54SVivien Didelot {
598d0004a02SVladimir Oltean 	struct dsa_port *dp;
599c38c5a66SVivien Didelot 	u32 mask = 0;
60002bc6e54SVivien Didelot 
601d0004a02SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
602d0004a02SVladimir Oltean 		mask |= BIT(dp->index);
603c38c5a66SVivien Didelot 
604c38c5a66SVivien Didelot 	return mask;
605c8652c83SVivien Didelot }
606c8652c83SVivien Didelot 
607465c3de4SVladimir Oltean static inline u32 dsa_cpu_ports(struct dsa_switch *ds)
608465c3de4SVladimir Oltean {
609465c3de4SVladimir Oltean 	struct dsa_port *cpu_dp;
610465c3de4SVladimir Oltean 	u32 mask = 0;
611465c3de4SVladimir Oltean 
612465c3de4SVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
613465c3de4SVladimir Oltean 		mask |= BIT(cpu_dp->index);
614465c3de4SVladimir Oltean 
615465c3de4SVladimir Oltean 	return mask;
616465c3de4SVladimir Oltean }
617465c3de4SVladimir Oltean 
618c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
619c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
620c5f51765SVivien Didelot {
621c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
622c5f51765SVivien Didelot 	struct dsa_link *dl;
623c5f51765SVivien Didelot 
624c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
625c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
626c5f51765SVivien Didelot 			return dl->dp->index;
627c5f51765SVivien Didelot 
628c5f51765SVivien Didelot 	return ds->num_ports;
629c5f51765SVivien Didelot }
630c5f51765SVivien Didelot 
6313b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
6323b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
6333b8fac5dSVivien Didelot 					    int port)
6343b8fac5dSVivien Didelot {
6353b8fac5dSVivien Didelot 	if (device == ds->index)
6363b8fac5dSVivien Didelot 		return port;
6373b8fac5dSVivien Didelot 	else
638c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
6393b8fac5dSVivien Didelot }
6403b8fac5dSVivien Didelot 
6413b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
64207073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
643c8f0b869SBen Hutchings {
64407073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
64507073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
64607073c79SVivien Didelot 
64707073c79SVivien Didelot 	if (!cpu_dp)
64807073c79SVivien Didelot 		return port;
649c8f0b869SBen Hutchings 
6503b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
651c8f0b869SBen Hutchings }
652c8f0b869SBen Hutchings 
65363609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */
65463609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
65563609c8fSVladimir Oltean {
65663609c8fSVladimir Oltean 	if (dsa_is_unused_port(ds, port))
65763609c8fSVladimir Oltean 		return false;
65863609c8fSVladimir Oltean 
65963609c8fSVladimir Oltean 	return port == dsa_upstream_port(ds, port);
66063609c8fSVladimir Oltean }
66163609c8fSVladimir Oltean 
662d352b20fSTobias Waldekranz /* Return true if this is a DSA port leading away from the CPU */
663d352b20fSTobias Waldekranz static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
664d352b20fSTobias Waldekranz {
665d352b20fSTobias Waldekranz 	return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
666d352b20fSTobias Waldekranz }
667d352b20fSTobias Waldekranz 
6687af4a361STobias Waldekranz /* Return the local port used to reach the CPU port */
6697af4a361STobias Waldekranz static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
6707af4a361STobias Waldekranz {
6717af4a361STobias Waldekranz 	struct dsa_port *dp;
6727af4a361STobias Waldekranz 
6737af4a361STobias Waldekranz 	dsa_switch_for_each_available_port(dp, ds) {
6747af4a361STobias Waldekranz 		return dsa_upstream_port(ds, dp->index);
6757af4a361STobias Waldekranz 	}
6767af4a361STobias Waldekranz 
6777af4a361STobias Waldekranz 	return ds->num_ports;
6787af4a361STobias Waldekranz }
6797af4a361STobias Waldekranz 
68063609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
68163609c8fSVladimir Oltean  * that the routing port from @downstream_ds to @upstream_ds is also the port
68263609c8fSVladimir Oltean  * which @downstream_ds uses to reach its dedicated CPU.
68363609c8fSVladimir Oltean  */
68463609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
68563609c8fSVladimir Oltean 					     struct dsa_switch *downstream_ds)
68663609c8fSVladimir Oltean {
68763609c8fSVladimir Oltean 	int routing_port;
68863609c8fSVladimir Oltean 
68963609c8fSVladimir Oltean 	if (upstream_ds == downstream_ds)
69063609c8fSVladimir Oltean 		return true;
69163609c8fSVladimir Oltean 
69263609c8fSVladimir Oltean 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
69363609c8fSVladimir Oltean 
69463609c8fSVladimir Oltean 	return dsa_is_upstream_port(downstream_ds, routing_port);
69563609c8fSVladimir Oltean }
69663609c8fSVladimir Oltean 
697cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
698cf2d45f5SVladimir Oltean {
699cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
700cf2d45f5SVladimir Oltean 
701cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
702cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
703cf2d45f5SVladimir Oltean 	else
704cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
705cf2d45f5SVladimir Oltean }
706cf2d45f5SVladimir Oltean 
707dedd6a00SVladimir Oltean static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
708dedd6a00SVladimir Oltean {
709dedd6a00SVladimir Oltean 	return dp->lag ? dp->lag->id : 0;
710dedd6a00SVladimir Oltean }
711dedd6a00SVladimir Oltean 
712dedd6a00SVladimir Oltean static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
713dedd6a00SVladimir Oltean {
714dedd6a00SVladimir Oltean 	return dp->lag ? dp->lag->dev : NULL;
715dedd6a00SVladimir Oltean }
716dedd6a00SVladimir Oltean 
717dedd6a00SVladimir Oltean static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
718dedd6a00SVladimir Oltean 					 const struct dsa_lag *lag)
719dedd6a00SVladimir Oltean {
720dedd6a00SVladimir Oltean 	return dsa_port_lag_dev_get(dp) == lag->dev;
721dedd6a00SVladimir Oltean }
722dedd6a00SVladimir Oltean 
7238f6a19c0SVladimir Oltean static inline struct net_device *dsa_port_to_master(const struct dsa_port *dp)
7248f6a19c0SVladimir Oltean {
725acc43b7bSVladimir Oltean 	if (dp->cpu_port_in_lag)
726acc43b7bSVladimir Oltean 		return dsa_port_lag_dev_get(dp->cpu_dp);
727acc43b7bSVladimir Oltean 
7288f6a19c0SVladimir Oltean 	return dp->cpu_dp->master;
7298f6a19c0SVladimir Oltean }
7308f6a19c0SVladimir Oltean 
731cc76ce9eSTobias Waldekranz static inline
732cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
733cc76ce9eSTobias Waldekranz {
734d3eed0e5SVladimir Oltean 	if (!dp->bridge)
735cc76ce9eSTobias Waldekranz 		return NULL;
736cc76ce9eSTobias Waldekranz 
737dedd6a00SVladimir Oltean 	if (dp->lag)
738dedd6a00SVladimir Oltean 		return dp->lag->dev;
739cc76ce9eSTobias Waldekranz 	else if (dp->hsr_dev)
740cc76ce9eSTobias Waldekranz 		return dp->hsr_dev;
741cc76ce9eSTobias Waldekranz 
742cc76ce9eSTobias Waldekranz 	return dp->slave;
743cc76ce9eSTobias Waldekranz }
744cc76ce9eSTobias Waldekranz 
74536cbf39bSVladimir Oltean static inline struct net_device *
74636cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp)
74736cbf39bSVladimir Oltean {
748d3eed0e5SVladimir Oltean 	return dp->bridge ? dp->bridge->dev : NULL;
74936cbf39bSVladimir Oltean }
75036cbf39bSVladimir Oltean 
75136cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
75236cbf39bSVladimir Oltean {
753d3eed0e5SVladimir Oltean 	return dp->bridge ? dp->bridge->num : 0;
75436cbf39bSVladimir Oltean }
75536cbf39bSVladimir Oltean 
75636cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a,
75736cbf39bSVladimir Oltean 					const struct dsa_port *b)
75836cbf39bSVladimir Oltean {
75936cbf39bSVladimir Oltean 	struct net_device *br_a = dsa_port_bridge_dev_get(a);
76036cbf39bSVladimir Oltean 	struct net_device *br_b = dsa_port_bridge_dev_get(b);
76136cbf39bSVladimir Oltean 
76236cbf39bSVladimir Oltean 	/* Standalone ports are not in the same bridge with one another */
76336cbf39bSVladimir Oltean 	return (!br_a || !br_b) ? false : (br_a == br_b);
76436cbf39bSVladimir Oltean }
76536cbf39bSVladimir Oltean 
7666a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
7676a43cba3SVladimir Oltean 						 const struct net_device *dev)
7686a43cba3SVladimir Oltean {
7696a43cba3SVladimir Oltean 	return dsa_port_to_bridge_port(dp) == dev;
7706a43cba3SVladimir Oltean }
7716a43cba3SVladimir Oltean 
7726a43cba3SVladimir Oltean static inline bool
7736a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp,
7746a43cba3SVladimir Oltean 			     const struct net_device *bridge_dev)
7756a43cba3SVladimir Oltean {
7766a43cba3SVladimir Oltean 	/* DSA ports connected to a bridge, and event was emitted
7776a43cba3SVladimir Oltean 	 * for the bridge.
7786a43cba3SVladimir Oltean 	 */
7796a43cba3SVladimir Oltean 	return dsa_port_bridge_dev_get(dp) == bridge_dev;
7806a43cba3SVladimir Oltean }
7816a43cba3SVladimir Oltean 
782d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
783d3eed0e5SVladimir Oltean 					    const struct dsa_bridge *bridge)
784d3eed0e5SVladimir Oltean {
785d3eed0e5SVladimir Oltean 	return dsa_port_bridge_dev_get(dp) == bridge->dev;
786d3eed0e5SVladimir Oltean }
787d3eed0e5SVladimir Oltean 
7886a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */
7896a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
7906a43cba3SVladimir Oltean 						 const struct net_device *dev)
7916a43cba3SVladimir Oltean {
7926a43cba3SVladimir Oltean 	struct dsa_port *dp;
7936a43cba3SVladimir Oltean 
7946a43cba3SVladimir Oltean 	list_for_each_entry(dp, &dst->ports, list)
7956a43cba3SVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, dev))
7966a43cba3SVladimir Oltean 			return true;
7976a43cba3SVladimir Oltean 
7986a43cba3SVladimir Oltean 	return false;
7996a43cba3SVladimir Oltean }
8006a43cba3SVladimir Oltean 
8016a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */
8026a43cba3SVladimir Oltean static inline bool
8036a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
8046a43cba3SVladimir Oltean 			     const struct net_device *bridge_dev)
8056a43cba3SVladimir Oltean {
8066a43cba3SVladimir Oltean 	struct dsa_port *dp;
8076a43cba3SVladimir Oltean 
8086a43cba3SVladimir Oltean 	list_for_each_entry(dp, &dst->ports, list)
8096a43cba3SVladimir Oltean 		if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
8106a43cba3SVladimir Oltean 			return true;
8116a43cba3SVladimir Oltean 
8126a43cba3SVladimir Oltean 	return false;
8136a43cba3SVladimir Oltean }
8146a43cba3SVladimir Oltean 
815acc43b7bSVladimir Oltean static inline bool dsa_port_tree_same(const struct dsa_port *a,
816acc43b7bSVladimir Oltean 				      const struct dsa_port *b)
817acc43b7bSVladimir Oltean {
818acc43b7bSVladimir Oltean 	return a->ds->dst == b->ds->dst;
819acc43b7bSVladimir Oltean }
820acc43b7bSVladimir Oltean 
8212bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
8222bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
8239d490b4eSVivien Didelot struct dsa_switch_ops {
82453da0ebaSVladimir Oltean 	/*
82553da0ebaSVladimir Oltean 	 * Tagging protocol helpers called for the CPU ports and DSA links.
82653da0ebaSVladimir Oltean 	 * @get_tag_protocol retrieves the initial tagging protocol and is
82753da0ebaSVladimir Oltean 	 * mandatory. Switches which can operate using multiple tagging
82853da0ebaSVladimir Oltean 	 * protocols should implement @change_tag_protocol and report in
82953da0ebaSVladimir Oltean 	 * @get_tag_protocol the tagger in current use.
83053da0ebaSVladimir Oltean 	 */
8315ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
8324d776482SFlorian Fainelli 						  int port,
8334d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
834bacf93b0SVladimir Oltean 	int	(*change_tag_protocol)(struct dsa_switch *ds,
83553da0ebaSVladimir Oltean 				       enum dsa_tag_protocol proto);
836dc452a47SVladimir Oltean 	/*
837dc452a47SVladimir Oltean 	 * Method for switch drivers to connect to the tagging protocol driver
838dc452a47SVladimir Oltean 	 * in current use. The switch driver can provide handlers for certain
839dc452a47SVladimir Oltean 	 * types of packets for switch management.
840dc452a47SVladimir Oltean 	 */
841dc452a47SVladimir Oltean 	int	(*connect_tag_protocol)(struct dsa_switch *ds,
842dc452a47SVladimir Oltean 					enum dsa_tag_protocol proto);
8437b314362SAndrew Lunn 
84495f510d0SVladimir Oltean 	int	(*port_change_master)(struct dsa_switch *ds, int port,
84595f510d0SVladimir Oltean 				      struct net_device *master,
84695f510d0SVladimir Oltean 				      struct netlink_ext_ack *extack);
84795f510d0SVladimir Oltean 
848fd292c18SVladimir Oltean 	/* Optional switch-wide initialization and destruction methods */
849c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
8505e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
851fd292c18SVladimir Oltean 
852fd292c18SVladimir Oltean 	/* Per-port initialization and destruction methods. Mandatory if the
853fd292c18SVladimir Oltean 	 * driver registers devlink port regions, optional otherwise.
854fd292c18SVladimir Oltean 	 */
855fd292c18SVladimir Oltean 	int	(*port_setup)(struct dsa_switch *ds, int port);
856fd292c18SVladimir Oltean 	void	(*port_teardown)(struct dsa_switch *ds, int port);
857fd292c18SVladimir Oltean 
8586819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
859c8f0b869SBen Hutchings 
860c8f0b869SBen Hutchings 	/*
861c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
862c8f0b869SBen Hutchings 	 */
863c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
864c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
865c8f0b869SBen Hutchings 			     int regnum, u16 val);
866c8f0b869SBen Hutchings 
867c8f0b869SBen Hutchings 	/*
868ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
869ec9436baSFlorian Fainelli 	 */
870ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
871ec9436baSFlorian Fainelli 				struct phy_device *phydev);
872ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
873ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
874ec9436baSFlorian Fainelli 
875ec9436baSFlorian Fainelli 	/*
87611d8f3ddSFlorian Fainelli 	 * PHYLINK integration
87711d8f3ddSFlorian Fainelli 	 */
878072eea6cSRussell King (Oracle) 	void	(*phylink_get_caps)(struct dsa_switch *ds, int port,
879072eea6cSRussell King (Oracle) 				    struct phylink_config *config);
880bde01822SRussell King (Oracle) 	struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
881bde01822SRussell King (Oracle) 						      int port,
882bde01822SRussell King (Oracle) 						      phy_interface_t iface);
88311d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
88411d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
88511d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
88611d8f3ddSFlorian Fainelli 				      unsigned int mode,
88711d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
88811d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
88911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
89011d8f3ddSFlorian Fainelli 					 unsigned int mode,
89111d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
89211d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
89311d8f3ddSFlorian Fainelli 				       unsigned int mode,
89411d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
8955b502a7bSRussell King 				       struct phy_device *phydev,
8965b502a7bSRussell King 				       int speed, int duplex,
8975b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
89811d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
89911d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
90011d8f3ddSFlorian Fainelli 	/*
901c2ec5f2eSOleksij Rempel 	 * Port statistics counters.
902c8f0b869SBen Hutchings 	 */
90389f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
90489f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
905c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
906c8f0b869SBen Hutchings 				     int port, uint64_t *data);
90789f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
908cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
909cf963573SFlorian Fainelli 					 int port, uint64_t *data);
910487d3855SAlvin Šipraga 	void	(*get_eth_phy_stats)(struct dsa_switch *ds, int port,
911487d3855SAlvin Šipraga 				     struct ethtool_eth_phy_stats *phy_stats);
912487d3855SAlvin Šipraga 	void	(*get_eth_mac_stats)(struct dsa_switch *ds, int port,
913487d3855SAlvin Šipraga 				     struct ethtool_eth_mac_stats *mac_stats);
914487d3855SAlvin Šipraga 	void	(*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
915487d3855SAlvin Šipraga 				      struct ethtool_eth_ctrl_stats *ctrl_stats);
91667f38b1cSClément Léger 	void	(*get_rmon_stats)(struct dsa_switch *ds, int port,
91767f38b1cSClément Léger 				  struct ethtool_rmon_stats *rmon_stats,
91867f38b1cSClément Léger 				  const struct ethtool_rmon_hist_range **ranges);
919c2ec5f2eSOleksij Rempel 	void	(*get_stats64)(struct dsa_switch *ds, int port,
920c2ec5f2eSOleksij Rempel 				   struct rtnl_link_stats64 *s);
9213d410403SOleksij Rempel 	void	(*get_pause_stats)(struct dsa_switch *ds, int port,
9223d410403SOleksij Rempel 				   struct ethtool_pause_stats *pause_stats);
923a71acad9SOleksij Rempel 	void	(*self_test)(struct dsa_switch *ds, int port,
924a71acad9SOleksij Rempel 			     struct ethtool_test *etest, u64 *data);
92524462549SFlorian Fainelli 
92624462549SFlorian Fainelli 	/*
92719e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
92819e57c4eSFlorian Fainelli 	 */
92919e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
93019e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
93119e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
93219e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
93319e57c4eSFlorian Fainelli 
93419e57c4eSFlorian Fainelli 	/*
9350336369dSBrandon Streiff 	 * ethtool timestamp info
9360336369dSBrandon Streiff 	 */
9370336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
9380336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
9390336369dSBrandon Streiff 
9400336369dSBrandon Streiff 	/*
941*5f6c2d49SVladimir Oltean 	 * ethtool MAC merge layer
942*5f6c2d49SVladimir Oltean 	 */
943*5f6c2d49SVladimir Oltean 	int	(*get_mm)(struct dsa_switch *ds, int port,
944*5f6c2d49SVladimir Oltean 			  struct ethtool_mm_state *state);
945*5f6c2d49SVladimir Oltean 	int	(*set_mm)(struct dsa_switch *ds, int port,
946*5f6c2d49SVladimir Oltean 			  struct ethtool_mm_cfg *cfg,
947*5f6c2d49SVladimir Oltean 			  struct netlink_ext_ack *extack);
948*5f6c2d49SVladimir Oltean 	void	(*get_mm_stats)(struct dsa_switch *ds, int port,
949*5f6c2d49SVladimir Oltean 				struct ethtool_mm_stats *stats);
950*5f6c2d49SVladimir Oltean 
951*5f6c2d49SVladimir Oltean 	/*
952d538eca8SVladimir Oltean 	 * DCB ops
953d538eca8SVladimir Oltean 	 */
954d538eca8SVladimir Oltean 	int	(*port_get_default_prio)(struct dsa_switch *ds, int port);
955d538eca8SVladimir Oltean 	int	(*port_set_default_prio)(struct dsa_switch *ds, int port,
956d538eca8SVladimir Oltean 					 u8 prio);
95747d75f78SVladimir Oltean 	int	(*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
95847d75f78SVladimir Oltean 	int	(*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
95947d75f78SVladimir Oltean 				      u8 prio);
96047d75f78SVladimir Oltean 	int	(*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
96147d75f78SVladimir Oltean 				      u8 prio);
962d538eca8SVladimir Oltean 
963d538eca8SVladimir Oltean 	/*
96424462549SFlorian Fainelli 	 * Suspend and resume
96524462549SFlorian Fainelli 	 */
96624462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
96724462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
968b2f2af21SFlorian Fainelli 
969b2f2af21SFlorian Fainelli 	/*
970b2f2af21SFlorian Fainelli 	 * Port enable/disable
971b2f2af21SFlorian Fainelli 	 */
972b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
973b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
97475104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
9757905288fSFlorian Fainelli 
9767905288fSFlorian Fainelli 	/*
97708f50061SVivien Didelot 	 * Port's MAC EEE settings
9787905288fSFlorian Fainelli 	 */
97908f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
9807905288fSFlorian Fainelli 			       struct ethtool_eee *e);
98108f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
9827905288fSFlorian Fainelli 			       struct ethtool_eee *e);
98351579c3fSGuenter Roeck 
9846793abb4SGuenter Roeck 	/* EEPROM access */
9856793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
9866793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
9876793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
9886793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
9896793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
9903d762a0fSGuenter Roeck 
9913d762a0fSGuenter Roeck 	/*
9923d762a0fSGuenter Roeck 	 * Register access.
9933d762a0fSGuenter Roeck 	 */
9943d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
9953d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
9963d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
997b73adef6SFlorian Fainelli 
998b73adef6SFlorian Fainelli 	/*
999e358bef7SVladimir Oltean 	 * Upper device tracking.
1000e358bef7SVladimir Oltean 	 */
1001e358bef7SVladimir Oltean 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
1002e358bef7SVladimir Oltean 				       struct netdev_notifier_changeupper_info *info);
1003e358bef7SVladimir Oltean 
1004e358bef7SVladimir Oltean 	/*
1005b73adef6SFlorian Fainelli 	 * Bridge integration
1006b73adef6SFlorian Fainelli 	 */
100734a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
100871327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
1009b079922bSVladimir Oltean 				    struct dsa_bridge bridge,
101006b9cce4SVladimir Oltean 				    bool *tx_fwd_offload,
101106b9cce4SVladimir Oltean 				    struct netlink_ext_ack *extack);
1012f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
1013d3eed0e5SVladimir Oltean 				     struct dsa_bridge bridge);
101443c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
1015b73adef6SFlorian Fainelli 				      u8 state);
10167414af30STobias Waldekranz 	int	(*port_mst_state_set)(struct dsa_switch *ds, int port,
10177414af30STobias Waldekranz 				      const struct switchdev_mst_state *state);
1018732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
10197414af30STobias Waldekranz 	int	(*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
1020a8b659e7SVladimir Oltean 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
1021a8b659e7SVladimir Oltean 					 struct switchdev_brport_flags flags,
1022a8b659e7SVladimir Oltean 					 struct netlink_ext_ack *extack);
1023a8b659e7SVladimir Oltean 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
1024a8b659e7SVladimir Oltean 				     struct switchdev_brport_flags flags,
1025a8b659e7SVladimir Oltean 				     struct netlink_ext_ack *extack);
102672c3b0c7SVladimir Oltean 	void	(*port_set_host_flood)(struct dsa_switch *ds, int port,
102772c3b0c7SVladimir Oltean 				       bool uc, bool mc);
10282a778e1bSVivien Didelot 
10292a778e1bSVivien Didelot 	/*
103011149536SVivien Didelot 	 * VLAN support
103111149536SVivien Didelot 	 */
1032fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
103389153ed6SVladimir Oltean 				       bool vlan_filtering,
103489153ed6SVladimir Oltean 				       struct netlink_ext_ack *extack);
10351958d581SVladimir Oltean 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
103631046a5fSVladimir Oltean 				 const struct switchdev_obj_port_vlan *vlan,
103731046a5fSVladimir Oltean 				 struct netlink_ext_ack *extack);
103876e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
103976e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
10408e6598a7STobias Waldekranz 	int	(*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge,
10418e6598a7STobias Waldekranz 				 const struct switchdev_vlan_msti *msti);
10428e6598a7STobias Waldekranz 
104311149536SVivien Didelot 	/*
10442a778e1bSVivien Didelot 	 * Forwarding database
10452a778e1bSVivien Didelot 	 */
10461b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
1047c2693363SVladimir Oltean 				const unsigned char *addr, u16 vid,
1048c2693363SVladimir Oltean 				struct dsa_db db);
10492a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
1050c2693363SVladimir Oltean 				const unsigned char *addr, u16 vid,
1051c2693363SVladimir Oltean 				struct dsa_db db);
1052ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
10532bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
1054e212fa7cSVladimir Oltean 	int	(*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
1055c2693363SVladimir Oltean 			       const unsigned char *addr, u16 vid,
1056c2693363SVladimir Oltean 			       struct dsa_db db);
1057e212fa7cSVladimir Oltean 	int	(*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
1058c2693363SVladimir Oltean 			       const unsigned char *addr, u16 vid,
1059c2693363SVladimir Oltean 			       struct dsa_db db);
10608df30255SVivien Didelot 
10618df30255SVivien Didelot 	/*
10628df30255SVivien Didelot 	 * Multicast database
10638df30255SVivien Didelot 	 */
1064a52b2da7SVladimir Oltean 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
1065c2693363SVladimir Oltean 				const struct switchdev_obj_port_mdb *mdb,
1066c2693363SVladimir Oltean 				struct dsa_db db);
10678df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
1068c2693363SVladimir Oltean 				const struct switchdev_obj_port_mdb *mdb,
1069c2693363SVladimir Oltean 				struct dsa_db db);
1070bf9f2648SFlorian Fainelli 	/*
1071bf9f2648SFlorian Fainelli 	 * RXNFC
1072bf9f2648SFlorian Fainelli 	 */
1073bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
1074bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
1075bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
1076bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
1077f50f2127SFlorian Fainelli 
1078f50f2127SFlorian Fainelli 	/*
1079f50f2127SFlorian Fainelli 	 * TC integration
1080f50f2127SFlorian Fainelli 	 */
1081ed11bb1fSVladimir Oltean 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
1082ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
1083ed11bb1fSVladimir Oltean 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
1084ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
1085ed11bb1fSVladimir Oltean 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
1086ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls, bool ingress);
1087f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
1088f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
10890148bb50SVladimir Oltean 				   bool ingress, struct netlink_ext_ack *extack);
1090f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
1091f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
109234297176SVladimir Oltean 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
109334297176SVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer);
109434297176SVladimir Oltean 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
109547d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
109647d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
109740ef2c93SVivien Didelot 
109840ef2c93SVivien Didelot 	/*
109940ef2c93SVivien Didelot 	 * Cross-chip operations
110040ef2c93SVivien Didelot 	 */
1101f66a6a69SVladimir Oltean 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
1102f66a6a69SVladimir Oltean 					 int sw_index, int port,
110306b9cce4SVladimir Oltean 					 struct dsa_bridge bridge,
110406b9cce4SVladimir Oltean 					 struct netlink_ext_ack *extack);
1105f66a6a69SVladimir Oltean 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
1106f66a6a69SVladimir Oltean 					  int sw_index, int port,
1107d3eed0e5SVladimir Oltean 					  struct dsa_bridge bridge);
1108058102a6STobias Waldekranz 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
1109058102a6STobias Waldekranz 					int port);
1110058102a6STobias Waldekranz 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
1111dedd6a00SVladimir Oltean 				      int port, struct dsa_lag lag,
11122e359b00SVladimir Oltean 				      struct netdev_lag_upper_info *info,
11132e359b00SVladimir Oltean 				      struct netlink_ext_ack *extack);
1114058102a6STobias Waldekranz 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
1115dedd6a00SVladimir Oltean 				       int port, struct dsa_lag lag);
11160336369dSBrandon Streiff 
11170336369dSBrandon Streiff 	/*
11180336369dSBrandon Streiff 	 * PTP functionality
11190336369dSBrandon Streiff 	 */
11200336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
11210336369dSBrandon Streiff 				     struct ifreq *ifr);
11220336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
11230336369dSBrandon Streiff 				     struct ifreq *ifr);
11245c5416f5SYangbo Lu 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
11255c5416f5SYangbo Lu 				 struct sk_buff *skb);
112690af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
112790af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
112897a69a0dSVladimir Oltean 
11290f06b855SAndrew Lunn 	/* Devlink parameters, etc */
11306b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
11316b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
11326b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
11336b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
11340f06b855SAndrew Lunn 	int	(*devlink_info_get)(struct dsa_switch *ds,
11350f06b855SAndrew Lunn 				    struct devlink_info_req *req,
11360f06b855SAndrew Lunn 				    struct netlink_ext_ack *extack);
11372a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
11382a6ef763SVladimir Oltean 				       unsigned int sb_index, u16 pool_index,
11392a6ef763SVladimir Oltean 				       struct devlink_sb_pool_info *pool_info);
11402a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
11412a6ef763SVladimir Oltean 				       u16 pool_index, u32 size,
11422a6ef763SVladimir Oltean 				       enum devlink_sb_threshold_type threshold_type,
11432a6ef763SVladimir Oltean 				       struct netlink_ext_ack *extack);
11442a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
11452a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
11462a6ef763SVladimir Oltean 					    u32 *p_threshold);
11472a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
11482a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
11492a6ef763SVladimir Oltean 					    u32 threshold,
11502a6ef763SVladimir Oltean 					    struct netlink_ext_ack *extack);
11512a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
11522a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
11532a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
11542a6ef763SVladimir Oltean 					       u16 *p_pool_index, u32 *p_threshold);
11552a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
11562a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
11572a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
11582a6ef763SVladimir Oltean 					       u16 pool_index, u32 threshold,
11592a6ef763SVladimir Oltean 					       struct netlink_ext_ack *extack);
11602a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
11612a6ef763SVladimir Oltean 					   unsigned int sb_index);
11622a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
11632a6ef763SVladimir Oltean 					    unsigned int sb_index);
11642a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
11652a6ef763SVladimir Oltean 						unsigned int sb_index, u16 pool_index,
11662a6ef763SVladimir Oltean 						u32 *p_cur, u32 *p_max);
11672a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
11682a6ef763SVladimir Oltean 						   unsigned int sb_index, u16 tc_index,
11692a6ef763SVladimir Oltean 						   enum devlink_sb_pool_type pool_type,
11702a6ef763SVladimir Oltean 						   u32 *p_cur, u32 *p_max);
1171bfcb8132SVladimir Oltean 
1172bfcb8132SVladimir Oltean 	/*
1173bfcb8132SVladimir Oltean 	 * MTU change functionality. Switches can also adjust their MRU through
1174bfcb8132SVladimir Oltean 	 * this method. By MTU, one understands the SDU (L2 payload) length.
1175bfcb8132SVladimir Oltean 	 * If the switch needs to account for the DSA tag on the CPU port, this
1176ab88d64aSRandy Dunlap 	 * method needs to do so privately.
1177bfcb8132SVladimir Oltean 	 */
1178bfcb8132SVladimir Oltean 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
1179bfcb8132SVladimir Oltean 				   int new_mtu);
1180bfcb8132SVladimir Oltean 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
1181058102a6STobias Waldekranz 
1182058102a6STobias Waldekranz 	/*
1183058102a6STobias Waldekranz 	 * LAG integration
1184058102a6STobias Waldekranz 	 */
1185058102a6STobias Waldekranz 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
1186058102a6STobias Waldekranz 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
1187dedd6a00SVladimir Oltean 				 struct dsa_lag lag,
11882e359b00SVladimir Oltean 				 struct netdev_lag_upper_info *info,
11892e359b00SVladimir Oltean 				 struct netlink_ext_ack *extack);
1190058102a6STobias Waldekranz 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
1191dedd6a00SVladimir Oltean 				  struct dsa_lag lag);
119218596f50SGeorge McCollister 
119318596f50SGeorge McCollister 	/*
119418596f50SGeorge McCollister 	 * HSR integration
119518596f50SGeorge McCollister 	 */
119618596f50SGeorge McCollister 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
119718596f50SGeorge McCollister 				 struct net_device *hsr);
119818596f50SGeorge McCollister 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
119918596f50SGeorge McCollister 				  struct net_device *hsr);
1200c595c433SHoratiu Vultur 
1201c595c433SHoratiu Vultur 	/*
1202c595c433SHoratiu Vultur 	 * MRP integration
1203c595c433SHoratiu Vultur 	 */
1204c595c433SHoratiu Vultur 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
1205c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
1206c595c433SHoratiu Vultur 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
1207c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
1208c595c433SHoratiu Vultur 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1209c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
1210c595c433SHoratiu Vultur 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1211c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
12125da11eb4SVladimir Oltean 
12135da11eb4SVladimir Oltean 	/*
12145da11eb4SVladimir Oltean 	 * tag_8021q operations
12155da11eb4SVladimir Oltean 	 */
12165da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
12175da11eb4SVladimir Oltean 				      u16 flags);
12185da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1219295ab96fSVladimir Oltean 
1220295ab96fSVladimir Oltean 	/*
1221295ab96fSVladimir Oltean 	 * DSA master tracking operations
1222295ab96fSVladimir Oltean 	 */
1223295ab96fSVladimir Oltean 	void	(*master_state_change)(struct dsa_switch *ds,
1224295ab96fSVladimir Oltean 				       const struct net_device *master,
1225295ab96fSVladimir Oltean 				       bool operational);
12266b297524SAndrew Lunn };
12276b297524SAndrew Lunn 
12286b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
12296b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
12306b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
12316b297524SAndrew Lunn 
12326b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
12336b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
12346b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
12356b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
12366b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
12376b297524SAndrew Lunn 				const struct devlink_param *params,
12386b297524SAndrew Lunn 				size_t params_count);
12396b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
12406b297524SAndrew Lunn 				   const struct devlink_param *params,
12416b297524SAndrew Lunn 				   size_t params_count);
12425cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
12435cd73fbdSAndrew Lunn 				  const char *resource_name,
12445cd73fbdSAndrew Lunn 				  u64 resource_size,
12455cd73fbdSAndrew Lunn 				  u64 resource_id,
12465cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
12475cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
12485cd73fbdSAndrew Lunn 
12495cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
12505cd73fbdSAndrew Lunn 
12515cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
12525cd73fbdSAndrew Lunn 					   u64 resource_id,
12535cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
12545cd73fbdSAndrew Lunn 					   void *occ_get_priv);
12555cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
12565cd73fbdSAndrew Lunn 					     u64 resource_id);
125797c82c23SAndrew Lunn struct devlink_region *
125897c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds,
125997c82c23SAndrew Lunn 			  const struct devlink_region_ops *ops,
126097c82c23SAndrew Lunn 			  u32 region_max_snapshots, u64 region_size);
126108156ba4SAndrew Lunn struct devlink_region *
126208156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds,
126308156ba4SAndrew Lunn 			       int port,
126408156ba4SAndrew Lunn 			       const struct devlink_port_region_ops *ops,
126508156ba4SAndrew Lunn 			       u32 region_max_snapshots, u64 region_size);
126697c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region);
126797c82c23SAndrew Lunn 
1268e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
12695cd73fbdSAndrew Lunn 
12706b297524SAndrew Lunn struct dsa_devlink_priv {
12716b297524SAndrew Lunn 	struct dsa_switch *ds;
1272c8f0b869SBen Hutchings };
1273c8f0b869SBen Hutchings 
1274ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1275ccc3e6b0SAndrew Lunn {
1276ccc3e6b0SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1277ccc3e6b0SAndrew Lunn 
1278ccc3e6b0SAndrew Lunn 	return dl_priv->ds;
1279ccc3e6b0SAndrew Lunn }
1280ccc3e6b0SAndrew Lunn 
12817d1e2a10SAndrew Lunn static inline
12827d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
12837d1e2a10SAndrew Lunn {
12847d1e2a10SAndrew Lunn 	struct devlink *dl = port->devlink;
12857d1e2a10SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
12867d1e2a10SAndrew Lunn 
12877d1e2a10SAndrew Lunn 	return dl_priv->ds;
12887d1e2a10SAndrew Lunn }
12897d1e2a10SAndrew Lunn 
12907d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port)
12917d1e2a10SAndrew Lunn {
12927d1e2a10SAndrew Lunn 	return port->index;
12937d1e2a10SAndrew Lunn }
12947d1e2a10SAndrew Lunn 
1295ab3d408dSFlorian Fainelli struct dsa_switch_driver {
1296ab3d408dSFlorian Fainelli 	struct list_head	list;
1297a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
1298ab3d408dSFlorian Fainelli };
1299ab3d408dSFlorian Fainelli 
13007e580490SVladimir Oltean bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
13017e580490SVladimir Oltean 				 const unsigned char *addr, u16 vid,
13027e580490SVladimir Oltean 				 struct dsa_db db);
13037e580490SVladimir Oltean bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
13047e580490SVladimir Oltean 				 const struct switchdev_obj_port_mdb *mdb,
13057e580490SVladimir Oltean 				 struct dsa_db db);
1306f9cef64fSVladimir Oltean 
130773a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
13089eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev)
1309c6e970a0SAndrew Lunn {
1310c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
1311717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
1312c6e970a0SAndrew Lunn #endif
1313c6e970a0SAndrew Lunn 	return false;
1314c6e970a0SAndrew Lunn }
1315c6e970a0SAndrew Lunn 
13169790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail
13179790cf20SVladimir Oltean  * tags, which don't break dissection) can be treated the same from the
13189790cf20SVladimir Oltean  * perspective of the flow dissector.
13199790cf20SVladimir Oltean  *
13209790cf20SVladimir Oltean  * We need to return:
13219790cf20SVladimir Oltean  *  - offset: the (B - A) difference between:
13229790cf20SVladimir Oltean  *    A. the position of the real EtherType and
13239790cf20SVladimir Oltean  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
13249790cf20SVladimir Oltean  *       after the normal EtherType was supposed to be)
13259790cf20SVladimir Oltean  *    The offset in bytes is exactly equal to the tagger overhead (and half of
13269790cf20SVladimir Oltean  *    that, in __be16 shorts).
13279790cf20SVladimir Oltean  *
13289790cf20SVladimir Oltean  *  - proto: the value of the real EtherType.
13299790cf20SVladimir Oltean  */
13309790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
13319790cf20SVladimir Oltean 						__be16 *proto, int *offset)
13329790cf20SVladimir Oltean {
13339790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
13349790cf20SVladimir Oltean 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
13354e500251SVladimir Oltean 	int tag_len = ops->needed_headroom;
13369790cf20SVladimir Oltean 
13379790cf20SVladimir Oltean 	*offset = tag_len;
13389790cf20SVladimir Oltean 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
13399790cf20SVladimir Oltean #endif
13409790cf20SVladimir Oltean }
13419790cf20SVladimir Oltean 
13424cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
13434cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev)
13444cfab356SFlorian Fainelli {
13454cfab356SFlorian Fainelli 	int err = -EOPNOTSUPP;
13464cfab356SFlorian Fainelli 
13474cfab356SFlorian Fainelli 	if (!dev->dsa_ptr)
13484cfab356SFlorian Fainelli 		return err;
13494cfab356SFlorian Fainelli 
13504cfab356SFlorian Fainelli 	if (!dev->dsa_ptr->netdev_ops)
13514cfab356SFlorian Fainelli 		return err;
13524cfab356SFlorian Fainelli 
13534cfab356SFlorian Fainelli 	return 0;
13544cfab356SFlorian Fainelli }
13554cfab356SFlorian Fainelli 
1356a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
13574cfab356SFlorian Fainelli 				    int cmd)
13584cfab356SFlorian Fainelli {
13594cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *ops;
13604cfab356SFlorian Fainelli 	int err;
13614cfab356SFlorian Fainelli 
13624cfab356SFlorian Fainelli 	err = __dsa_netdevice_ops_check(dev);
13634cfab356SFlorian Fainelli 	if (err)
13644cfab356SFlorian Fainelli 		return err;
13654cfab356SFlorian Fainelli 
13664cfab356SFlorian Fainelli 	ops = dev->dsa_ptr->netdev_ops;
13674cfab356SFlorian Fainelli 
1368a7605370SArnd Bergmann 	return ops->ndo_eth_ioctl(dev, ifr, cmd);
13694cfab356SFlorian Fainelli }
13704cfab356SFlorian Fainelli #else
1371a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
13724cfab356SFlorian Fainelli 				    int cmd)
13734cfab356SFlorian Fainelli {
13744cfab356SFlorian Fainelli 	return -EOPNOTSUPP;
13754cfab356SFlorian Fainelli }
13764cfab356SFlorian Fainelli #endif
13774cfab356SFlorian Fainelli 
137883c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
137923c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
13800650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds);
13813b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1382a2614140SVladimir Oltean void dsa_flush_workqueue(void);
1383ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
1384ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
1385ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
1386ea825e70SFlorian Fainelli #else
1387ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
1388ea825e70SFlorian Fainelli {
1389ea825e70SFlorian Fainelli 	return 0;
1390ea825e70SFlorian Fainelli }
1391ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
1392ea825e70SFlorian Fainelli {
1393ea825e70SFlorian Fainelli 	return 0;
1394ea825e70SFlorian Fainelli }
1395ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
1396ea825e70SFlorian Fainelli 
139760724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
1398a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev);
139960724d4bSFlorian Fainelli #else
1400a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev)
1401a5e3c9baSVladimir Oltean {
1402a5e3c9baSVladimir Oltean 	return false;
1403a5e3c9baSVladimir Oltean }
140460724d4bSFlorian Fainelli #endif
140560724d4bSFlorian Fainelli 
140697a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
140711d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1408cf963573SFlorian Fainelli 
140991da11f8SLennert Buytenhek #endif
1410