xref: /openbmc/linux/include/net/dsa.h (revision 72c3b0c7)
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
560b42f033SAndrew Lunn 
57ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
580b42f033SAndrew Lunn 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
590b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
60964dbf18SÁlvaro Fernández Rojas 	DSA_TAG_PROTO_BRCM_LEGACY	= DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
610b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
620b42f033SAndrew Lunn 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
630b42f033SAndrew Lunn 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
640b42f033SAndrew Lunn 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
650b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
660b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
670b42f033SAndrew Lunn 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
680b42f033SAndrew Lunn 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
690b42f033SAndrew Lunn 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
700b42f033SAndrew Lunn 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
71f9bbe447SVladimir Oltean 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
72227d07a0SVladimir Oltean 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
73016e43a2STristram Ha 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
748dce89aaSVladimir Oltean 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
7548fda74fSOleksij Rempel 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
76efd7fe68SLinus Walleij 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
7701ef09caSKurt Kanzenbach 	DSA_TAG_PROTO_HELLCREEK		= DSA_TAG_PROTO_HELLCREEK_VALUE,
7854a52823SGeorge McCollister 	DSA_TAG_PROTO_XRS700X		= DSA_TAG_PROTO_XRS700X_VALUE,
797c83a7c5SVladimir Oltean 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
807c4bb540SVladimir Oltean 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
814913b8ebSVladimir Oltean 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
821521d5adSAlvin Šipraga 	DSA_TAG_PROTO_RTL8_4		= DSA_TAG_PROTO_RTL8_4_VALUE,
83cd87fecdSLuiz Angelo Daros de Luca 	DSA_TAG_PROTO_RTL8_4T		= DSA_TAG_PROTO_RTL8_4T_VALUE,
84ac7a04c3SFlorian Fainelli };
855037d532SFlorian Fainelli 
8690af1059SBrandon Streiff struct dsa_switch;
873e8a72d1SFlorian Fainelli 
8868277a2cSJohn Crispin struct dsa_device_ops {
8968277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
9029a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
912e8cb1b3SVladimir Oltean 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
92598a9680SJohn Crispin 			     int *offset);
937f297314SVladimir Oltean 	int (*connect)(struct dsa_switch *ds);
947f297314SVladimir Oltean 	void (*disconnect)(struct dsa_switch *ds);
954e500251SVladimir Oltean 	unsigned int needed_headroom;
964e500251SVladimir Oltean 	unsigned int needed_tailroom;
97875138f8SAndrew Lunn 	const char *name;
98056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
99c3975400SVladimir Oltean 	/* Some tagging protocols either mangle or shift the destination MAC
100c3975400SVladimir Oltean 	 * address, in which case the DSA master would drop packets on ingress
101c3975400SVladimir Oltean 	 * if what it understands out of the destination MAC address is not in
102c3975400SVladimir Oltean 	 * its RX filter.
103c3975400SVladimir Oltean 	 */
104c3975400SVladimir Oltean 	bool promisc_on_master;
10568277a2cSJohn Crispin };
10668277a2cSJohn Crispin 
1074cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the
1084cfab356SFlorian Fainelli  * DSA layer on top of the DSA CPU/management net_device instance. This is
1094cfab356SFlorian Fainelli  * used by the core net_device layer while calling various net_device_ops
1104cfab356SFlorian Fainelli  * function pointers.
1114cfab356SFlorian Fainelli  */
1124cfab356SFlorian Fainelli struct dsa_netdevice_ops {
113a7605370SArnd Bergmann 	int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
1144cfab356SFlorian Fainelli 			     int cmd);
1154cfab356SFlorian Fainelli };
1164cfab356SFlorian Fainelli 
1170b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
1180b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
1190b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
1200b42f033SAndrew Lunn 
121dedd6a00SVladimir Oltean struct dsa_lag {
122dedd6a00SVladimir Oltean 	struct net_device *dev;
123dedd6a00SVladimir Oltean 	unsigned int id;
124e212fa7cSVladimir Oltean 	struct mutex fdb_lock;
125e212fa7cSVladimir Oltean 	struct list_head fdbs;
126dedd6a00SVladimir Oltean 	refcount_t refcount;
127dedd6a00SVladimir Oltean };
128dedd6a00SVladimir Oltean 
129cf50dcc2SBen Hutchings struct dsa_switch_tree {
13083c0afaeSAndrew Lunn 	struct list_head	list;
13183c0afaeSAndrew Lunn 
132b035c88cSVladimir Oltean 	/* List of switch ports */
133b035c88cSVladimir Oltean 	struct list_head ports;
134b035c88cSVladimir Oltean 
135f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
136f515f192SVivien Didelot 	struct raw_notifier_head	nh;
137f515f192SVivien Didelot 
13883c0afaeSAndrew Lunn 	/* Tree identifier */
13949463b7fSVivien Didelot 	unsigned int index;
14083c0afaeSAndrew Lunn 
14183c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
14283c0afaeSAndrew Lunn 	struct kref refcount;
14383c0afaeSAndrew Lunn 
144b035c88cSVladimir Oltean 	/* Maps offloaded LAG netdevs to a zero-based linear ID for
145b035c88cSVladimir Oltean 	 * drivers that need it.
146b035c88cSVladimir Oltean 	 */
147dedd6a00SVladimir Oltean 	struct dsa_lag **lags;
148b035c88cSVladimir Oltean 
149357f203bSVladimir Oltean 	/* Tagging protocol operations */
150357f203bSVladimir Oltean 	const struct dsa_device_ops *tag_ops;
151357f203bSVladimir Oltean 
152deff7107STobias Waldekranz 	/* Default tagging protocol preferred by the switches in this
153deff7107STobias Waldekranz 	 * tree.
154deff7107STobias Waldekranz 	 */
155deff7107STobias Waldekranz 	enum dsa_tag_protocol default_proto;
156deff7107STobias Waldekranz 
1574b026e82SVladimir Oltean 	/* Has this tree been applied to the hardware? */
1584b026e82SVladimir Oltean 	bool setup;
1594b026e82SVladimir Oltean 
160cf50dcc2SBen Hutchings 	/*
161cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
162cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
163cf50dcc2SBen Hutchings 	 */
164cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
165cf85d08fSLennert Buytenhek 
166c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
167c5f51765SVivien Didelot 	struct list_head rtable;
168058102a6STobias Waldekranz 
169b035c88cSVladimir Oltean 	/* Length of "lags" array */
170058102a6STobias Waldekranz 	unsigned int lags_len;
1715b22d366SVladimir Oltean 
1725b22d366SVladimir Oltean 	/* Track the largest switch index within a tree */
1735b22d366SVladimir Oltean 	unsigned int last_switch;
174cf50dcc2SBen Hutchings };
175cf50dcc2SBen Hutchings 
1763d4a0a2aSVladimir Oltean /* LAG IDs are one-based, the dst->lags array is zero-based */
177058102a6STobias Waldekranz #define dsa_lags_foreach_id(_id, _dst)				\
1783d4a0a2aSVladimir Oltean 	for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++)	\
1793d4a0a2aSVladimir Oltean 		if ((_dst)->lags[(_id) - 1])
180058102a6STobias Waldekranz 
181058102a6STobias Waldekranz #define dsa_lag_foreach_port(_dp, _dst, _lag)			\
182058102a6STobias Waldekranz 	list_for_each_entry((_dp), &(_dst)->ports, list)	\
183dedd6a00SVladimir Oltean 		if (dsa_port_offloads_lag((_dp), (_lag)))
184058102a6STobias Waldekranz 
18518596f50SGeorge McCollister #define dsa_hsr_foreach_port(_dp, _ds, _hsr)			\
18618596f50SGeorge McCollister 	list_for_each_entry((_dp), &(_ds)->dst->ports, list)	\
18718596f50SGeorge McCollister 		if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
18818596f50SGeorge McCollister 
189dedd6a00SVladimir Oltean static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
190058102a6STobias Waldekranz 					    unsigned int id)
191058102a6STobias Waldekranz {
1923d4a0a2aSVladimir Oltean 	/* DSA LAG IDs are one-based, dst->lags is zero-based */
1933d4a0a2aSVladimir Oltean 	return dst->lags[id - 1];
194058102a6STobias Waldekranz }
195058102a6STobias Waldekranz 
196058102a6STobias Waldekranz static inline int dsa_lag_id(struct dsa_switch_tree *dst,
19746a76724SVladimir Oltean 			     struct net_device *lag_dev)
198058102a6STobias Waldekranz {
199058102a6STobias Waldekranz 	unsigned int id;
200058102a6STobias Waldekranz 
201058102a6STobias Waldekranz 	dsa_lags_foreach_id(id, dst) {
202dedd6a00SVladimir Oltean 		struct dsa_lag *lag = dsa_lag_by_id(dst, id);
203dedd6a00SVladimir Oltean 
204dedd6a00SVladimir Oltean 		if (lag->dev == lag_dev)
205dedd6a00SVladimir Oltean 			return lag->id;
206058102a6STobias Waldekranz 	}
207058102a6STobias Waldekranz 
208058102a6STobias Waldekranz 	return -ENODEV;
209058102a6STobias Waldekranz }
210058102a6STobias Waldekranz 
21134297176SVladimir Oltean /* TC matchall action types */
212f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
213f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
21434297176SVladimir Oltean 	DSA_PORT_MALL_POLICER,
215f50f2127SFlorian Fainelli };
216f50f2127SFlorian Fainelli 
217f50f2127SFlorian Fainelli /* TC mirroring entry */
218f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
219f50f2127SFlorian Fainelli 	u8 to_local_port;
220f50f2127SFlorian Fainelli 	bool ingress;
221f50f2127SFlorian Fainelli };
222f50f2127SFlorian Fainelli 
22334297176SVladimir Oltean /* TC port policer entry */
22434297176SVladimir Oltean struct dsa_mall_policer_tc_entry {
2255f035af7SPo Liu 	u32 burst;
22634297176SVladimir Oltean 	u64 rate_bytes_per_sec;
22734297176SVladimir Oltean };
22834297176SVladimir Oltean 
229f50f2127SFlorian Fainelli /* TC matchall entry */
230f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
231f50f2127SFlorian Fainelli 	struct list_head list;
232f50f2127SFlorian Fainelli 	unsigned long cookie;
233f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
234f50f2127SFlorian Fainelli 	union {
235f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
23634297176SVladimir Oltean 		struct dsa_mall_policer_tc_entry policer;
237f50f2127SFlorian Fainelli 	};
238f50f2127SFlorian Fainelli };
239f50f2127SFlorian Fainelli 
240d3eed0e5SVladimir Oltean struct dsa_bridge {
241d3eed0e5SVladimir Oltean 	struct net_device *dev;
242d3eed0e5SVladimir Oltean 	unsigned int num;
243857fdd74SVladimir Oltean 	bool tx_fwd_offload;
244d3eed0e5SVladimir Oltean 	refcount_t refcount;
245d3eed0e5SVladimir Oltean };
246f50f2127SFlorian Fainelli 
247c8b09808SAndrew Lunn struct dsa_port {
248f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
249f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
250f8b8b1cdSVivien Didelot 	 */
251f8b8b1cdSVivien Didelot 	union {
252f8b8b1cdSVivien Didelot 		struct net_device *master;
253f8b8b1cdSVivien Didelot 		struct net_device *slave;
254f8b8b1cdSVivien Didelot 	};
255f8b8b1cdSVivien Didelot 
256357f203bSVladimir Oltean 	/* Copy of the tagging protocol operations, for quicker access
257357f203bSVladimir Oltean 	 * in the data path. Valid only for the CPU ports.
258357f203bSVladimir Oltean 	 */
25915240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
26015240248SVivien Didelot 
2613e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
2623e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
26329a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
2643e41f93bSVivien Didelot 
26506251258SVladimir Oltean 	struct dsa_switch	*ds;
26606251258SVladimir Oltean 
26706251258SVladimir Oltean 	unsigned int		index;
26806251258SVladimir Oltean 
269057cad2cSVivien Didelot 	enum {
270057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
271057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
272057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
273057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
274057cad2cSVivien Didelot 	} type;
275057cad2cSVivien Didelot 
27671e0bbdeSFlorian Fainelli 	const char		*name;
27768b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
27883216e39SMichael Walle 	u8			mac[ETH_ALEN];
279b08db33dSVladimir Oltean 
280b08db33dSVladimir Oltean 	u8			stp_state;
281b08db33dSVladimir Oltean 
2821b26d364SVladimir Oltean 	/* Warning: the following bit fields are not atomic, and updating them
2831b26d364SVladimir Oltean 	 * can only be done from code paths where concurrency is not possible
2841b26d364SVladimir Oltean 	 * (probe time or under rtnl_lock).
2851b26d364SVladimir Oltean 	 */
28663cfc657SVladimir Oltean 	u8			vlan_filtering:1;
28763cfc657SVladimir Oltean 
28863cfc657SVladimir Oltean 	/* Managed by DSA on user ports and by drivers on CPU and DSA ports */
28963cfc657SVladimir Oltean 	u8			learning:1;
29063cfc657SVladimir Oltean 
29163cfc657SVladimir Oltean 	u8			lag_tx_enabled:1;
29263cfc657SVladimir Oltean 
29363cfc657SVladimir Oltean 	u8			devlink_port_setup:1;
29463cfc657SVladimir Oltean 
295295ab96fSVladimir Oltean 	/* Master state bits, valid only on CPU ports */
296295ab96fSVladimir Oltean 	u8			master_admin_up:1;
297295ab96fSVladimir Oltean 	u8			master_oper_up:1;
298295ab96fSVladimir Oltean 
29963cfc657SVladimir Oltean 	u8			setup:1;
300bde82f38SVladimir Oltean 
301189b0d93SAndrew Lunn 	struct device_node	*dn;
30234a79f63SVivien Didelot 	unsigned int		ageing_time;
303bde82f38SVladimir Oltean 
304d3eed0e5SVladimir Oltean 	struct dsa_bridge	*bridge;
30596567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
306aab9c406SFlorian Fainelli 	struct phylink		*pl;
30744cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
308dedd6a00SVladimir Oltean 	struct dsa_lag		*lag;
30918596f50SGeorge McCollister 	struct net_device	*hsr_dev;
31097a69a0dSVladimir Oltean 
311ab8ccae1SVivien Didelot 	struct list_head list;
312ab8ccae1SVivien Didelot 
31367dbb9d4SFlorian Fainelli 	/*
31467dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
31567dbb9d4SFlorian Fainelli 	 */
31667dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
317da7b9e9bSFlorian Fainelli 
318da7b9e9bSFlorian Fainelli 	/*
319da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
320da7b9e9bSFlorian Fainelli 	 */
3214cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *netdev_ops;
322fb35c60cSVivien Didelot 
323161ca59dSVladimir Oltean 	/* List of MAC addresses that must be forwarded on this port.
324161ca59dSVladimir Oltean 	 * These are only valid on CPU ports and DSA links.
325161ca59dSVladimir Oltean 	 */
326338a3a47SVladimir Oltean 	struct mutex		addr_lists_lock;
3273f6e32f9SVladimir Oltean 	struct list_head	fdbs;
328161ca59dSVladimir Oltean 	struct list_head	mdbs;
329134ef238SVladimir Oltean 
330134ef238SVladimir Oltean 	/* List of VLANs that CPU and DSA ports are members of. */
331134ef238SVladimir Oltean 	struct mutex		vlans_lock;
332134ef238SVladimir Oltean 	struct list_head	vlans;
333c8b09808SAndrew Lunn };
334c8b09808SAndrew Lunn 
335c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
336c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
337c5f51765SVivien Didelot  * but this would require some more complex tree walking,
338c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
339c5f51765SVivien Didelot  */
340c5f51765SVivien Didelot struct dsa_link {
341c5f51765SVivien Didelot 	struct dsa_port *dp;
342c5f51765SVivien Didelot 	struct dsa_port *link_dp;
343c5f51765SVivien Didelot 	struct list_head list;
344c5f51765SVivien Didelot };
345c5f51765SVivien Didelot 
346c2693363SVladimir Oltean enum dsa_db_type {
347c2693363SVladimir Oltean 	DSA_DB_PORT,
348c2693363SVladimir Oltean 	DSA_DB_LAG,
349c2693363SVladimir Oltean 	DSA_DB_BRIDGE,
350c2693363SVladimir Oltean };
351c2693363SVladimir Oltean 
352c2693363SVladimir Oltean struct dsa_db {
353c2693363SVladimir Oltean 	enum dsa_db_type type;
354c2693363SVladimir Oltean 
355c2693363SVladimir Oltean 	union {
356c2693363SVladimir Oltean 		const struct dsa_port *dp;
357c2693363SVladimir Oltean 		struct dsa_lag lag;
358c2693363SVladimir Oltean 		struct dsa_bridge bridge;
359c2693363SVladimir Oltean 	};
360c2693363SVladimir Oltean };
361c2693363SVladimir Oltean 
362161ca59dSVladimir Oltean struct dsa_mac_addr {
363161ca59dSVladimir Oltean 	unsigned char addr[ETH_ALEN];
364161ca59dSVladimir Oltean 	u16 vid;
365161ca59dSVladimir Oltean 	refcount_t refcount;
366161ca59dSVladimir Oltean 	struct list_head list;
367c2693363SVladimir Oltean 	struct dsa_db db;
368161ca59dSVladimir Oltean };
369161ca59dSVladimir Oltean 
370134ef238SVladimir Oltean struct dsa_vlan {
371134ef238SVladimir Oltean 	u16 vid;
372134ef238SVladimir Oltean 	refcount_t refcount;
373134ef238SVladimir Oltean 	struct list_head list;
374134ef238SVladimir Oltean };
375134ef238SVladimir Oltean 
376c8f0b869SBen Hutchings struct dsa_switch {
377c33063d6SAndrew Lunn 	struct device *dev;
378c33063d6SAndrew Lunn 
379c8f0b869SBen Hutchings 	/*
380c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
381c8f0b869SBen Hutchings 	 */
382c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
38399feaafcSVivien Didelot 	unsigned int		index;
384c8f0b869SBen Hutchings 
3851b26d364SVladimir Oltean 	/* Warning: the following bit fields are not atomic, and updating them
3861b26d364SVladimir Oltean 	 * can only be done from code paths where concurrency is not possible
3871b26d364SVladimir Oltean 	 * (probe time or under rtnl_lock).
3881b26d364SVladimir Oltean 	 */
38963cfc657SVladimir Oltean 	u32			setup:1;
39063cfc657SVladimir Oltean 
39163cfc657SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
39263cfc657SVladimir Oltean 	 * settings on ports if not hardware-supported
3937787ff77SVladimir Oltean 	 */
39463cfc657SVladimir Oltean 	u32			vlan_filtering_is_global:1;
39563cfc657SVladimir Oltean 
39663cfc657SVladimir Oltean 	/* Keep VLAN filtering enabled on ports not offloading any upper */
39763cfc657SVladimir Oltean 	u32			needs_standalone_vlan_filtering:1;
39863cfc657SVladimir Oltean 
39963cfc657SVladimir Oltean 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
40063cfc657SVladimir Oltean 	 * that have vlan_filtering=0. All drivers should ideally set this (and
40163cfc657SVladimir Oltean 	 * then the option would get removed), but it is unknown whether this
4027787ff77SVladimir Oltean 	 * would break things or not.
4037787ff77SVladimir Oltean 	 */
40463cfc657SVladimir Oltean 	u32			configure_vlan_while_not_filtering:1;
40563cfc657SVladimir Oltean 
40663cfc657SVladimir Oltean 	/* If the switch driver always programs the CPU port as egress tagged
40763cfc657SVladimir Oltean 	 * despite the VLAN configuration indicating otherwise, then setting
40863cfc657SVladimir Oltean 	 * @untag_bridge_pvid will force the DSA receive path to pop the
40963cfc657SVladimir Oltean 	 * bridge's default_pvid VLAN tagged frames to offer a consistent
41063cfc657SVladimir Oltean 	 * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge
4117787ff77SVladimir Oltean 	 * device.
4127787ff77SVladimir Oltean 	 */
41363cfc657SVladimir Oltean 	u32			untag_bridge_pvid:1;
41463cfc657SVladimir Oltean 
4157787ff77SVladimir Oltean 	/* Let DSA manage the FDB entries towards the
4167787ff77SVladimir Oltean 	 * CPU, based on the software bridge database.
4177787ff77SVladimir Oltean 	 */
41863cfc657SVladimir Oltean 	u32			assisted_learning_on_cpu_port:1;
41963cfc657SVladimir Oltean 
42063cfc657SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
42163cfc657SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
4227787ff77SVladimir Oltean 	 */
42363cfc657SVladimir Oltean 	u32			vlan_filtering:1;
42463cfc657SVladimir Oltean 
42563cfc657SVladimir Oltean 	/* For switches that only have the MRU configurable. To ensure the
42663cfc657SVladimir Oltean 	 * configured MTU is not exceeded, normalization of MRU on all bridged
42763cfc657SVladimir Oltean 	 * interfaces is needed.
4287787ff77SVladimir Oltean 	 */
42963cfc657SVladimir Oltean 	u32			mtu_enforcement_ingress:1;
4307787ff77SVladimir Oltean 
431c2693363SVladimir Oltean 	/* Drivers that isolate the FDBs of multiple bridges must set this
432c2693363SVladimir Oltean 	 * to true to receive the bridge as an argument in .port_fdb_{add,del}
433c2693363SVladimir Oltean 	 * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
434c2693363SVladimir Oltean 	 * passed as zero.
435c2693363SVladimir Oltean 	 */
436c2693363SVladimir Oltean 	u32			fdb_isolation:1;
437c2693363SVladimir Oltean 
438f515f192SVivien Didelot 	/* Listener for switch fabric events */
439f515f192SVivien Didelot 	struct notifier_block	nb;
440f515f192SVivien Didelot 
441c8f0b869SBen Hutchings 	/*
4427543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
4437543a6d5SAndrew Lunn 	 * structure.
4447543a6d5SAndrew Lunn 	 */
4457543a6d5SAndrew Lunn 	void *priv;
4467543a6d5SAndrew Lunn 
447dc452a47SVladimir Oltean 	void *tagger_data;
448dc452a47SVladimir Oltean 
4497543a6d5SAndrew Lunn 	/*
450c8f0b869SBen Hutchings 	 * Configuration data for this switch.
451c8f0b869SBen Hutchings 	 */
452ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
453c8f0b869SBen Hutchings 
454c8f0b869SBen Hutchings 	/*
4559d490b4eSVivien Didelot 	 * The switch operations.
456c8f0b869SBen Hutchings 	 */
457a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
458c8f0b869SBen Hutchings 
45966472fc0SAndrew Lunn 	/*
460c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
461c8f0b869SBen Hutchings 	 */
4620d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
463c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
464a0c02161SVivien Didelot 
4650f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
4660f3da6afSVivien Didelot 	unsigned int ageing_time_min;
4670f3da6afSVivien Didelot 	unsigned int ageing_time_max;
4680f3da6afSVivien Didelot 
469d7b1fd52SVladimir Oltean 	/* Storage for drivers using tag_8021q */
470d7b1fd52SVladimir Oltean 	struct dsa_8021q_context *tag_8021q_ctx;
471d7b1fd52SVladimir Oltean 
47296567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
47396567d5dSAndrew Lunn 	struct devlink		*devlink;
47496567d5dSAndrew Lunn 
47555199df6SFlorian Fainelli 	/* Number of switch port queues */
47655199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
47755199df6SFlorian Fainelli 
478058102a6STobias Waldekranz 	/* Drivers that benefit from having an ID associated with each
479058102a6STobias Waldekranz 	 * offloaded LAG should set this to the maximum number of
480058102a6STobias Waldekranz 	 * supported IDs. DSA will then maintain a mapping of _at
481058102a6STobias Waldekranz 	 * least_ these many IDs, accessible to drivers via
482058102a6STobias Waldekranz 	 * dsa_lag_id().
483058102a6STobias Waldekranz 	 */
484058102a6STobias Waldekranz 	unsigned int		num_lag_ids;
485058102a6STobias Waldekranz 
486947c8746SVladimir Oltean 	/* Drivers that support bridge forwarding offload or FDB isolation
487947c8746SVladimir Oltean 	 * should set this to the maximum number of bridges spanning the same
488947c8746SVladimir Oltean 	 * switch tree (or all trees, in the case of cross-tree bridging
489947c8746SVladimir Oltean 	 * support) that can be offloaded.
490123abc06SVladimir Oltean 	 */
491947c8746SVladimir Oltean 	unsigned int		max_num_bridges;
492123abc06SVladimir Oltean 
493258030acSVladimir Oltean 	unsigned int		num_ports;
494c8f0b869SBen Hutchings };
495c8f0b869SBen Hutchings 
49668bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
497c38c5a66SVivien Didelot {
498b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
499d607525bSVivien Didelot 	struct dsa_port *dp;
500b96ddf25SVivien Didelot 
501b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
502b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
503b96ddf25SVivien Didelot 			return dp;
504d607525bSVivien Didelot 
505d607525bSVivien Didelot 	return NULL;
506c38c5a66SVivien Didelot }
507c38c5a66SVivien Didelot 
508a8986681SVladimir Oltean static inline bool dsa_port_is_dsa(struct dsa_port *port)
509a8986681SVladimir Oltean {
510a8986681SVladimir Oltean 	return port->type == DSA_PORT_TYPE_DSA;
511a8986681SVladimir Oltean }
512a8986681SVladimir Oltean 
513a8986681SVladimir Oltean static inline bool dsa_port_is_cpu(struct dsa_port *port)
514a8986681SVladimir Oltean {
515a8986681SVladimir Oltean 	return port->type == DSA_PORT_TYPE_CPU;
516a8986681SVladimir Oltean }
517a8986681SVladimir Oltean 
518a8986681SVladimir Oltean static inline bool dsa_port_is_user(struct dsa_port *dp)
519a8986681SVladimir Oltean {
520a8986681SVladimir Oltean 	return dp->type == DSA_PORT_TYPE_USER;
521a8986681SVladimir Oltean }
522a8986681SVladimir Oltean 
523a57d8c21SVladimir Oltean static inline bool dsa_port_is_unused(struct dsa_port *dp)
524a57d8c21SVladimir Oltean {
525a57d8c21SVladimir Oltean 	return dp->type == DSA_PORT_TYPE_UNUSED;
526a57d8c21SVladimir Oltean }
527a57d8c21SVladimir Oltean 
528295ab96fSVladimir Oltean static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
529295ab96fSVladimir Oltean {
530295ab96fSVladimir Oltean 	return dsa_port_is_cpu(dp) && dp->master_admin_up &&
531295ab96fSVladimir Oltean 	       dp->master_oper_up;
532295ab96fSVladimir Oltean }
533295ab96fSVladimir Oltean 
534bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
535bff7b688SVivien Didelot {
536c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
537bff7b688SVivien Didelot }
538bff7b688SVivien Didelot 
539c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
540c8f0b869SBen Hutchings {
541c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
542c8f0b869SBen Hutchings }
543c8f0b869SBen Hutchings 
54460045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
54560045cbfSAndrew Lunn {
546c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
54760045cbfSAndrew Lunn }
54860045cbfSAndrew Lunn 
5492b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
5506cd456f3SVivien Didelot {
551c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
5526cd456f3SVivien Didelot }
5536cd456f3SVivien Didelot 
55482b31898SVladimir Oltean #define dsa_tree_for_each_user_port(_dp, _dst) \
55582b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_dst)->ports, list) \
55682b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
55782b31898SVladimir Oltean 
55882b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \
55982b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
56082b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
56182b31898SVladimir Oltean 
56282b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
56382b31898SVladimir Oltean 	list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
56482b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
56582b31898SVladimir Oltean 
56682b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
56782b31898SVladimir Oltean 	list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
56882b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
56982b31898SVladimir Oltean 
57082b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \
57182b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
57282b31898SVladimir Oltean 		if (!dsa_port_is_unused((_dp)))
57382b31898SVladimir Oltean 
57482b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \
57582b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
57682b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
57782b31898SVladimir Oltean 
57882b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \
57982b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
58082b31898SVladimir Oltean 		if (dsa_port_is_cpu((_dp)))
58182b31898SVladimir Oltean 
58202bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
58302bc6e54SVivien Didelot {
584d0004a02SVladimir Oltean 	struct dsa_port *dp;
585c38c5a66SVivien Didelot 	u32 mask = 0;
58602bc6e54SVivien Didelot 
587d0004a02SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
588d0004a02SVladimir Oltean 		mask |= BIT(dp->index);
589c38c5a66SVivien Didelot 
590c38c5a66SVivien Didelot 	return mask;
591c8652c83SVivien Didelot }
592c8652c83SVivien Didelot 
593465c3de4SVladimir Oltean static inline u32 dsa_cpu_ports(struct dsa_switch *ds)
594465c3de4SVladimir Oltean {
595465c3de4SVladimir Oltean 	struct dsa_port *cpu_dp;
596465c3de4SVladimir Oltean 	u32 mask = 0;
597465c3de4SVladimir Oltean 
598465c3de4SVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
599465c3de4SVladimir Oltean 		mask |= BIT(cpu_dp->index);
600465c3de4SVladimir Oltean 
601465c3de4SVladimir Oltean 	return mask;
602465c3de4SVladimir Oltean }
603465c3de4SVladimir Oltean 
604c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
605c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
606c5f51765SVivien Didelot {
607c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
608c5f51765SVivien Didelot 	struct dsa_link *dl;
609c5f51765SVivien Didelot 
610c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
611c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
612c5f51765SVivien Didelot 			return dl->dp->index;
613c5f51765SVivien Didelot 
614c5f51765SVivien Didelot 	return ds->num_ports;
615c5f51765SVivien Didelot }
616c5f51765SVivien Didelot 
6173b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
6183b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
6193b8fac5dSVivien Didelot 					    int port)
6203b8fac5dSVivien Didelot {
6213b8fac5dSVivien Didelot 	if (device == ds->index)
6223b8fac5dSVivien Didelot 		return port;
6233b8fac5dSVivien Didelot 	else
624c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
6253b8fac5dSVivien Didelot }
6263b8fac5dSVivien Didelot 
6273b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
62807073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
629c8f0b869SBen Hutchings {
63007073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
63107073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
63207073c79SVivien Didelot 
63307073c79SVivien Didelot 	if (!cpu_dp)
63407073c79SVivien Didelot 		return port;
635c8f0b869SBen Hutchings 
6363b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
637c8f0b869SBen Hutchings }
638c8f0b869SBen Hutchings 
63963609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */
64063609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
64163609c8fSVladimir Oltean {
64263609c8fSVladimir Oltean 	if (dsa_is_unused_port(ds, port))
64363609c8fSVladimir Oltean 		return false;
64463609c8fSVladimir Oltean 
64563609c8fSVladimir Oltean 	return port == dsa_upstream_port(ds, port);
64663609c8fSVladimir Oltean }
64763609c8fSVladimir Oltean 
648d352b20fSTobias Waldekranz /* Return true if this is a DSA port leading away from the CPU */
649d352b20fSTobias Waldekranz static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
650d352b20fSTobias Waldekranz {
651d352b20fSTobias Waldekranz 	return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
652d352b20fSTobias Waldekranz }
653d352b20fSTobias Waldekranz 
6547af4a361STobias Waldekranz /* Return the local port used to reach the CPU port */
6557af4a361STobias Waldekranz static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
6567af4a361STobias Waldekranz {
6577af4a361STobias Waldekranz 	struct dsa_port *dp;
6587af4a361STobias Waldekranz 
6597af4a361STobias Waldekranz 	dsa_switch_for_each_available_port(dp, ds) {
6607af4a361STobias Waldekranz 		return dsa_upstream_port(ds, dp->index);
6617af4a361STobias Waldekranz 	}
6627af4a361STobias Waldekranz 
6637af4a361STobias Waldekranz 	return ds->num_ports;
6647af4a361STobias Waldekranz }
6657af4a361STobias Waldekranz 
66663609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
66763609c8fSVladimir Oltean  * that the routing port from @downstream_ds to @upstream_ds is also the port
66863609c8fSVladimir Oltean  * which @downstream_ds uses to reach its dedicated CPU.
66963609c8fSVladimir Oltean  */
67063609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
67163609c8fSVladimir Oltean 					     struct dsa_switch *downstream_ds)
67263609c8fSVladimir Oltean {
67363609c8fSVladimir Oltean 	int routing_port;
67463609c8fSVladimir Oltean 
67563609c8fSVladimir Oltean 	if (upstream_ds == downstream_ds)
67663609c8fSVladimir Oltean 		return true;
67763609c8fSVladimir Oltean 
67863609c8fSVladimir Oltean 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
67963609c8fSVladimir Oltean 
68063609c8fSVladimir Oltean 	return dsa_is_upstream_port(downstream_ds, routing_port);
68163609c8fSVladimir Oltean }
68263609c8fSVladimir Oltean 
683cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
684cf2d45f5SVladimir Oltean {
685cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
686cf2d45f5SVladimir Oltean 
687cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
688cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
689cf2d45f5SVladimir Oltean 	else
690cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
691cf2d45f5SVladimir Oltean }
692cf2d45f5SVladimir Oltean 
693dedd6a00SVladimir Oltean static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
694dedd6a00SVladimir Oltean {
695dedd6a00SVladimir Oltean 	return dp->lag ? dp->lag->id : 0;
696dedd6a00SVladimir Oltean }
697dedd6a00SVladimir Oltean 
698dedd6a00SVladimir Oltean static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
699dedd6a00SVladimir Oltean {
700dedd6a00SVladimir Oltean 	return dp->lag ? dp->lag->dev : NULL;
701dedd6a00SVladimir Oltean }
702dedd6a00SVladimir Oltean 
703dedd6a00SVladimir Oltean static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
704dedd6a00SVladimir Oltean 					 const struct dsa_lag *lag)
705dedd6a00SVladimir Oltean {
706dedd6a00SVladimir Oltean 	return dsa_port_lag_dev_get(dp) == lag->dev;
707dedd6a00SVladimir Oltean }
708dedd6a00SVladimir Oltean 
709cc76ce9eSTobias Waldekranz static inline
710cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
711cc76ce9eSTobias Waldekranz {
712d3eed0e5SVladimir Oltean 	if (!dp->bridge)
713cc76ce9eSTobias Waldekranz 		return NULL;
714cc76ce9eSTobias Waldekranz 
715dedd6a00SVladimir Oltean 	if (dp->lag)
716dedd6a00SVladimir Oltean 		return dp->lag->dev;
717cc76ce9eSTobias Waldekranz 	else if (dp->hsr_dev)
718cc76ce9eSTobias Waldekranz 		return dp->hsr_dev;
719cc76ce9eSTobias Waldekranz 
720cc76ce9eSTobias Waldekranz 	return dp->slave;
721cc76ce9eSTobias Waldekranz }
722cc76ce9eSTobias Waldekranz 
72336cbf39bSVladimir Oltean static inline struct net_device *
72436cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp)
72536cbf39bSVladimir Oltean {
726d3eed0e5SVladimir Oltean 	return dp->bridge ? dp->bridge->dev : NULL;
72736cbf39bSVladimir Oltean }
72836cbf39bSVladimir Oltean 
72936cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
73036cbf39bSVladimir Oltean {
731d3eed0e5SVladimir Oltean 	return dp->bridge ? dp->bridge->num : 0;
73236cbf39bSVladimir Oltean }
73336cbf39bSVladimir Oltean 
73436cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a,
73536cbf39bSVladimir Oltean 					const struct dsa_port *b)
73636cbf39bSVladimir Oltean {
73736cbf39bSVladimir Oltean 	struct net_device *br_a = dsa_port_bridge_dev_get(a);
73836cbf39bSVladimir Oltean 	struct net_device *br_b = dsa_port_bridge_dev_get(b);
73936cbf39bSVladimir Oltean 
74036cbf39bSVladimir Oltean 	/* Standalone ports are not in the same bridge with one another */
74136cbf39bSVladimir Oltean 	return (!br_a || !br_b) ? false : (br_a == br_b);
74236cbf39bSVladimir Oltean }
74336cbf39bSVladimir Oltean 
7446a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
7456a43cba3SVladimir Oltean 						 const struct net_device *dev)
7466a43cba3SVladimir Oltean {
7476a43cba3SVladimir Oltean 	return dsa_port_to_bridge_port(dp) == dev;
7486a43cba3SVladimir Oltean }
7496a43cba3SVladimir Oltean 
7506a43cba3SVladimir Oltean static inline bool
7516a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp,
7526a43cba3SVladimir Oltean 			     const struct net_device *bridge_dev)
7536a43cba3SVladimir Oltean {
7546a43cba3SVladimir Oltean 	/* DSA ports connected to a bridge, and event was emitted
7556a43cba3SVladimir Oltean 	 * for the bridge.
7566a43cba3SVladimir Oltean 	 */
7576a43cba3SVladimir Oltean 	return dsa_port_bridge_dev_get(dp) == bridge_dev;
7586a43cba3SVladimir Oltean }
7596a43cba3SVladimir Oltean 
760d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
761d3eed0e5SVladimir Oltean 					    const struct dsa_bridge *bridge)
762d3eed0e5SVladimir Oltean {
763d3eed0e5SVladimir Oltean 	return dsa_port_bridge_dev_get(dp) == bridge->dev;
764d3eed0e5SVladimir Oltean }
765d3eed0e5SVladimir Oltean 
7666a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */
7676a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
7686a43cba3SVladimir Oltean 						 const struct net_device *dev)
7696a43cba3SVladimir Oltean {
7706a43cba3SVladimir Oltean 	struct dsa_port *dp;
7716a43cba3SVladimir Oltean 
7726a43cba3SVladimir Oltean 	list_for_each_entry(dp, &dst->ports, list)
7736a43cba3SVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, dev))
7746a43cba3SVladimir Oltean 			return true;
7756a43cba3SVladimir Oltean 
7766a43cba3SVladimir Oltean 	return false;
7776a43cba3SVladimir Oltean }
7786a43cba3SVladimir Oltean 
7796a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */
7806a43cba3SVladimir Oltean static inline bool
7816a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
7826a43cba3SVladimir Oltean 			     const struct net_device *bridge_dev)
7836a43cba3SVladimir Oltean {
7846a43cba3SVladimir Oltean 	struct dsa_port *dp;
7856a43cba3SVladimir Oltean 
7866a43cba3SVladimir Oltean 	list_for_each_entry(dp, &dst->ports, list)
7876a43cba3SVladimir Oltean 		if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
7886a43cba3SVladimir Oltean 			return true;
7896a43cba3SVladimir Oltean 
7906a43cba3SVladimir Oltean 	return false;
7916a43cba3SVladimir Oltean }
7926a43cba3SVladimir Oltean 
7932bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
7942bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
7959d490b4eSVivien Didelot struct dsa_switch_ops {
79653da0ebaSVladimir Oltean 	/*
79753da0ebaSVladimir Oltean 	 * Tagging protocol helpers called for the CPU ports and DSA links.
79853da0ebaSVladimir Oltean 	 * @get_tag_protocol retrieves the initial tagging protocol and is
79953da0ebaSVladimir Oltean 	 * mandatory. Switches which can operate using multiple tagging
80053da0ebaSVladimir Oltean 	 * protocols should implement @change_tag_protocol and report in
80153da0ebaSVladimir Oltean 	 * @get_tag_protocol the tagger in current use.
80253da0ebaSVladimir Oltean 	 */
8035ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
8044d776482SFlorian Fainelli 						  int port,
8054d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
80653da0ebaSVladimir Oltean 	int	(*change_tag_protocol)(struct dsa_switch *ds, int port,
80753da0ebaSVladimir Oltean 				       enum dsa_tag_protocol proto);
808dc452a47SVladimir Oltean 	/*
809dc452a47SVladimir Oltean 	 * Method for switch drivers to connect to the tagging protocol driver
810dc452a47SVladimir Oltean 	 * in current use. The switch driver can provide handlers for certain
811dc452a47SVladimir Oltean 	 * types of packets for switch management.
812dc452a47SVladimir Oltean 	 */
813dc452a47SVladimir Oltean 	int	(*connect_tag_protocol)(struct dsa_switch *ds,
814dc452a47SVladimir Oltean 					enum dsa_tag_protocol proto);
8157b314362SAndrew Lunn 
816fd292c18SVladimir Oltean 	/* Optional switch-wide initialization and destruction methods */
817c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
8185e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
819fd292c18SVladimir Oltean 
820fd292c18SVladimir Oltean 	/* Per-port initialization and destruction methods. Mandatory if the
821fd292c18SVladimir Oltean 	 * driver registers devlink port regions, optional otherwise.
822fd292c18SVladimir Oltean 	 */
823fd292c18SVladimir Oltean 	int	(*port_setup)(struct dsa_switch *ds, int port);
824fd292c18SVladimir Oltean 	void	(*port_teardown)(struct dsa_switch *ds, int port);
825fd292c18SVladimir Oltean 
8266819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
827c8f0b869SBen Hutchings 
828c8f0b869SBen Hutchings 	/*
829c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
830c8f0b869SBen Hutchings 	 */
831c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
832c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
833c8f0b869SBen Hutchings 			     int regnum, u16 val);
834c8f0b869SBen Hutchings 
835c8f0b869SBen Hutchings 	/*
836ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
837ec9436baSFlorian Fainelli 	 */
838ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
839ec9436baSFlorian Fainelli 				struct phy_device *phydev);
840ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
841ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
842ec9436baSFlorian Fainelli 
843ec9436baSFlorian Fainelli 	/*
84411d8f3ddSFlorian Fainelli 	 * PHYLINK integration
84511d8f3ddSFlorian Fainelli 	 */
846072eea6cSRussell King (Oracle) 	void	(*phylink_get_caps)(struct dsa_switch *ds, int port,
847072eea6cSRussell King (Oracle) 				    struct phylink_config *config);
84811d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
84911d8f3ddSFlorian Fainelli 				    unsigned long *supported,
85011d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
851bde01822SRussell King (Oracle) 	struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
852bde01822SRussell King (Oracle) 						      int port,
853bde01822SRussell King (Oracle) 						      phy_interface_t iface);
85411d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
85511d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
85611d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
85711d8f3ddSFlorian Fainelli 				      unsigned int mode,
85811d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
85911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
86011d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
86111d8f3ddSFlorian Fainelli 					 unsigned int mode,
86211d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
86311d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
86411d8f3ddSFlorian Fainelli 				       unsigned int mode,
86511d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
8665b502a7bSRussell King 				       struct phy_device *phydev,
8675b502a7bSRussell King 				       int speed, int duplex,
8685b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
86911d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
87011d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
87111d8f3ddSFlorian Fainelli 	/*
872c2ec5f2eSOleksij Rempel 	 * Port statistics counters.
873c8f0b869SBen Hutchings 	 */
87489f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
87589f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
876c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
877c8f0b869SBen Hutchings 				     int port, uint64_t *data);
87889f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
879cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
880cf963573SFlorian Fainelli 					 int port, uint64_t *data);
881487d3855SAlvin Šipraga 	void	(*get_eth_phy_stats)(struct dsa_switch *ds, int port,
882487d3855SAlvin Šipraga 				     struct ethtool_eth_phy_stats *phy_stats);
883487d3855SAlvin Šipraga 	void	(*get_eth_mac_stats)(struct dsa_switch *ds, int port,
884487d3855SAlvin Šipraga 				     struct ethtool_eth_mac_stats *mac_stats);
885487d3855SAlvin Šipraga 	void	(*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
886487d3855SAlvin Šipraga 				      struct ethtool_eth_ctrl_stats *ctrl_stats);
887c2ec5f2eSOleksij Rempel 	void	(*get_stats64)(struct dsa_switch *ds, int port,
888c2ec5f2eSOleksij Rempel 				   struct rtnl_link_stats64 *s);
889a71acad9SOleksij Rempel 	void	(*self_test)(struct dsa_switch *ds, int port,
890a71acad9SOleksij Rempel 			     struct ethtool_test *etest, u64 *data);
89124462549SFlorian Fainelli 
89224462549SFlorian Fainelli 	/*
89319e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
89419e57c4eSFlorian Fainelli 	 */
89519e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
89619e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
89719e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
89819e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
89919e57c4eSFlorian Fainelli 
90019e57c4eSFlorian Fainelli 	/*
9010336369dSBrandon Streiff 	 * ethtool timestamp info
9020336369dSBrandon Streiff 	 */
9030336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
9040336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
9050336369dSBrandon Streiff 
9060336369dSBrandon Streiff 	/*
907d538eca8SVladimir Oltean 	 * DCB ops
908d538eca8SVladimir Oltean 	 */
909d538eca8SVladimir Oltean 	int	(*port_get_default_prio)(struct dsa_switch *ds, int port);
910d538eca8SVladimir Oltean 	int	(*port_set_default_prio)(struct dsa_switch *ds, int port,
911d538eca8SVladimir Oltean 					 u8 prio);
91247d75f78SVladimir Oltean 	int	(*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
91347d75f78SVladimir Oltean 	int	(*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
91447d75f78SVladimir Oltean 				      u8 prio);
91547d75f78SVladimir Oltean 	int	(*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
91647d75f78SVladimir Oltean 				      u8 prio);
917d538eca8SVladimir Oltean 
918d538eca8SVladimir Oltean 	/*
91924462549SFlorian Fainelli 	 * Suspend and resume
92024462549SFlorian Fainelli 	 */
92124462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
92224462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
923b2f2af21SFlorian Fainelli 
924b2f2af21SFlorian Fainelli 	/*
925b2f2af21SFlorian Fainelli 	 * Port enable/disable
926b2f2af21SFlorian Fainelli 	 */
927b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
928b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
92975104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
9307905288fSFlorian Fainelli 
9317905288fSFlorian Fainelli 	/*
93208f50061SVivien Didelot 	 * Port's MAC EEE settings
9337905288fSFlorian Fainelli 	 */
93408f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
9357905288fSFlorian Fainelli 			       struct ethtool_eee *e);
93608f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
9377905288fSFlorian Fainelli 			       struct ethtool_eee *e);
93851579c3fSGuenter Roeck 
9396793abb4SGuenter Roeck 	/* EEPROM access */
9406793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
9416793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
9426793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
9436793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
9446793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
9453d762a0fSGuenter Roeck 
9463d762a0fSGuenter Roeck 	/*
9473d762a0fSGuenter Roeck 	 * Register access.
9483d762a0fSGuenter Roeck 	 */
9493d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
9503d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
9513d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
952b73adef6SFlorian Fainelli 
953b73adef6SFlorian Fainelli 	/*
954e358bef7SVladimir Oltean 	 * Upper device tracking.
955e358bef7SVladimir Oltean 	 */
956e358bef7SVladimir Oltean 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
957e358bef7SVladimir Oltean 				       struct netdev_notifier_changeupper_info *info);
958e358bef7SVladimir Oltean 
959e358bef7SVladimir Oltean 	/*
960b73adef6SFlorian Fainelli 	 * Bridge integration
961b73adef6SFlorian Fainelli 	 */
96234a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
96371327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
964b079922bSVladimir Oltean 				    struct dsa_bridge bridge,
96506b9cce4SVladimir Oltean 				    bool *tx_fwd_offload,
96606b9cce4SVladimir Oltean 				    struct netlink_ext_ack *extack);
967f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
968d3eed0e5SVladimir Oltean 				     struct dsa_bridge bridge);
96943c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
970b73adef6SFlorian Fainelli 				      u8 state);
9717414af30STobias Waldekranz 	int	(*port_mst_state_set)(struct dsa_switch *ds, int port,
9727414af30STobias Waldekranz 				      const struct switchdev_mst_state *state);
973732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
9747414af30STobias Waldekranz 	int	(*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
975a8b659e7SVladimir Oltean 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
976a8b659e7SVladimir Oltean 					 struct switchdev_brport_flags flags,
977a8b659e7SVladimir Oltean 					 struct netlink_ext_ack *extack);
978a8b659e7SVladimir Oltean 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
979a8b659e7SVladimir Oltean 				     struct switchdev_brport_flags flags,
980a8b659e7SVladimir Oltean 				     struct netlink_ext_ack *extack);
981*72c3b0c7SVladimir Oltean 	void	(*port_set_host_flood)(struct dsa_switch *ds, int port,
982*72c3b0c7SVladimir Oltean 				       bool uc, bool mc);
9832a778e1bSVivien Didelot 
9842a778e1bSVivien Didelot 	/*
98511149536SVivien Didelot 	 * VLAN support
98611149536SVivien Didelot 	 */
987fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
98889153ed6SVladimir Oltean 				       bool vlan_filtering,
98989153ed6SVladimir Oltean 				       struct netlink_ext_ack *extack);
9901958d581SVladimir Oltean 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
99131046a5fSVladimir Oltean 				 const struct switchdev_obj_port_vlan *vlan,
99231046a5fSVladimir Oltean 				 struct netlink_ext_ack *extack);
99376e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
99476e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
9958e6598a7STobias Waldekranz 	int	(*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge,
9968e6598a7STobias Waldekranz 				 const struct switchdev_vlan_msti *msti);
9978e6598a7STobias Waldekranz 
99811149536SVivien Didelot 	/*
9992a778e1bSVivien Didelot 	 * Forwarding database
10002a778e1bSVivien Didelot 	 */
10011b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
1002c2693363SVladimir Oltean 				const unsigned char *addr, u16 vid,
1003c2693363SVladimir Oltean 				struct dsa_db db);
10042a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
1005c2693363SVladimir Oltean 				const unsigned char *addr, u16 vid,
1006c2693363SVladimir Oltean 				struct dsa_db db);
1007ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
10082bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
1009e212fa7cSVladimir Oltean 	int	(*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
1010c2693363SVladimir Oltean 			       const unsigned char *addr, u16 vid,
1011c2693363SVladimir Oltean 			       struct dsa_db db);
1012e212fa7cSVladimir Oltean 	int	(*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
1013c2693363SVladimir Oltean 			       const unsigned char *addr, u16 vid,
1014c2693363SVladimir Oltean 			       struct dsa_db db);
10158df30255SVivien Didelot 
10168df30255SVivien Didelot 	/*
10178df30255SVivien Didelot 	 * Multicast database
10188df30255SVivien Didelot 	 */
1019a52b2da7SVladimir Oltean 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
1020c2693363SVladimir Oltean 				const struct switchdev_obj_port_mdb *mdb,
1021c2693363SVladimir Oltean 				struct dsa_db db);
10228df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
1023c2693363SVladimir Oltean 				const struct switchdev_obj_port_mdb *mdb,
1024c2693363SVladimir Oltean 				struct dsa_db db);
1025bf9f2648SFlorian Fainelli 	/*
1026bf9f2648SFlorian Fainelli 	 * RXNFC
1027bf9f2648SFlorian Fainelli 	 */
1028bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
1029bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
1030bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
1031bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
1032f50f2127SFlorian Fainelli 
1033f50f2127SFlorian Fainelli 	/*
1034f50f2127SFlorian Fainelli 	 * TC integration
1035f50f2127SFlorian Fainelli 	 */
1036ed11bb1fSVladimir Oltean 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
1037ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
1038ed11bb1fSVladimir Oltean 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
1039ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
1040ed11bb1fSVladimir Oltean 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
1041ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls, bool ingress);
1042f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
1043f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
10440148bb50SVladimir Oltean 				   bool ingress, struct netlink_ext_ack *extack);
1045f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
1046f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
104734297176SVladimir Oltean 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
104834297176SVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer);
104934297176SVladimir Oltean 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
105047d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
105147d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
105240ef2c93SVivien Didelot 
105340ef2c93SVivien Didelot 	/*
105440ef2c93SVivien Didelot 	 * Cross-chip operations
105540ef2c93SVivien Didelot 	 */
1056f66a6a69SVladimir Oltean 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
1057f66a6a69SVladimir Oltean 					 int sw_index, int port,
105806b9cce4SVladimir Oltean 					 struct dsa_bridge bridge,
105906b9cce4SVladimir Oltean 					 struct netlink_ext_ack *extack);
1060f66a6a69SVladimir Oltean 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
1061f66a6a69SVladimir Oltean 					  int sw_index, int port,
1062d3eed0e5SVladimir Oltean 					  struct dsa_bridge bridge);
1063058102a6STobias Waldekranz 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
1064058102a6STobias Waldekranz 					int port);
1065058102a6STobias Waldekranz 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
1066dedd6a00SVladimir Oltean 				      int port, struct dsa_lag lag,
1067058102a6STobias Waldekranz 				      struct netdev_lag_upper_info *info);
1068058102a6STobias Waldekranz 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
1069dedd6a00SVladimir Oltean 				       int port, struct dsa_lag lag);
10700336369dSBrandon Streiff 
10710336369dSBrandon Streiff 	/*
10720336369dSBrandon Streiff 	 * PTP functionality
10730336369dSBrandon Streiff 	 */
10740336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
10750336369dSBrandon Streiff 				     struct ifreq *ifr);
10760336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
10770336369dSBrandon Streiff 				     struct ifreq *ifr);
10785c5416f5SYangbo Lu 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
10795c5416f5SYangbo Lu 				 struct sk_buff *skb);
108090af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
108190af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
108297a69a0dSVladimir Oltean 
10830f06b855SAndrew Lunn 	/* Devlink parameters, etc */
10846b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
10856b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
10866b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
10876b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
10880f06b855SAndrew Lunn 	int	(*devlink_info_get)(struct dsa_switch *ds,
10890f06b855SAndrew Lunn 				    struct devlink_info_req *req,
10900f06b855SAndrew Lunn 				    struct netlink_ext_ack *extack);
10912a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
10922a6ef763SVladimir Oltean 				       unsigned int sb_index, u16 pool_index,
10932a6ef763SVladimir Oltean 				       struct devlink_sb_pool_info *pool_info);
10942a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
10952a6ef763SVladimir Oltean 				       u16 pool_index, u32 size,
10962a6ef763SVladimir Oltean 				       enum devlink_sb_threshold_type threshold_type,
10972a6ef763SVladimir Oltean 				       struct netlink_ext_ack *extack);
10982a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
10992a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
11002a6ef763SVladimir Oltean 					    u32 *p_threshold);
11012a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
11022a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
11032a6ef763SVladimir Oltean 					    u32 threshold,
11042a6ef763SVladimir Oltean 					    struct netlink_ext_ack *extack);
11052a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
11062a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
11072a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
11082a6ef763SVladimir Oltean 					       u16 *p_pool_index, u32 *p_threshold);
11092a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
11102a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
11112a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
11122a6ef763SVladimir Oltean 					       u16 pool_index, u32 threshold,
11132a6ef763SVladimir Oltean 					       struct netlink_ext_ack *extack);
11142a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
11152a6ef763SVladimir Oltean 					   unsigned int sb_index);
11162a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
11172a6ef763SVladimir Oltean 					    unsigned int sb_index);
11182a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
11192a6ef763SVladimir Oltean 						unsigned int sb_index, u16 pool_index,
11202a6ef763SVladimir Oltean 						u32 *p_cur, u32 *p_max);
11212a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
11222a6ef763SVladimir Oltean 						   unsigned int sb_index, u16 tc_index,
11232a6ef763SVladimir Oltean 						   enum devlink_sb_pool_type pool_type,
11242a6ef763SVladimir Oltean 						   u32 *p_cur, u32 *p_max);
1125bfcb8132SVladimir Oltean 
1126bfcb8132SVladimir Oltean 	/*
1127bfcb8132SVladimir Oltean 	 * MTU change functionality. Switches can also adjust their MRU through
1128bfcb8132SVladimir Oltean 	 * this method. By MTU, one understands the SDU (L2 payload) length.
1129bfcb8132SVladimir Oltean 	 * If the switch needs to account for the DSA tag on the CPU port, this
1130ab88d64aSRandy Dunlap 	 * method needs to do so privately.
1131bfcb8132SVladimir Oltean 	 */
1132bfcb8132SVladimir Oltean 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
1133bfcb8132SVladimir Oltean 				   int new_mtu);
1134bfcb8132SVladimir Oltean 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
1135058102a6STobias Waldekranz 
1136058102a6STobias Waldekranz 	/*
1137058102a6STobias Waldekranz 	 * LAG integration
1138058102a6STobias Waldekranz 	 */
1139058102a6STobias Waldekranz 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
1140058102a6STobias Waldekranz 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
1141dedd6a00SVladimir Oltean 				 struct dsa_lag lag,
1142058102a6STobias Waldekranz 				 struct netdev_lag_upper_info *info);
1143058102a6STobias Waldekranz 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
1144dedd6a00SVladimir Oltean 				  struct dsa_lag lag);
114518596f50SGeorge McCollister 
114618596f50SGeorge McCollister 	/*
114718596f50SGeorge McCollister 	 * HSR integration
114818596f50SGeorge McCollister 	 */
114918596f50SGeorge McCollister 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
115018596f50SGeorge McCollister 				 struct net_device *hsr);
115118596f50SGeorge McCollister 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
115218596f50SGeorge McCollister 				  struct net_device *hsr);
1153c595c433SHoratiu Vultur 
1154c595c433SHoratiu Vultur 	/*
1155c595c433SHoratiu Vultur 	 * MRP integration
1156c595c433SHoratiu Vultur 	 */
1157c595c433SHoratiu Vultur 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
1158c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
1159c595c433SHoratiu Vultur 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
1160c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
1161c595c433SHoratiu Vultur 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1162c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
1163c595c433SHoratiu Vultur 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1164c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
11655da11eb4SVladimir Oltean 
11665da11eb4SVladimir Oltean 	/*
11675da11eb4SVladimir Oltean 	 * tag_8021q operations
11685da11eb4SVladimir Oltean 	 */
11695da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
11705da11eb4SVladimir Oltean 				      u16 flags);
11715da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1172295ab96fSVladimir Oltean 
1173295ab96fSVladimir Oltean 	/*
1174295ab96fSVladimir Oltean 	 * DSA master tracking operations
1175295ab96fSVladimir Oltean 	 */
1176295ab96fSVladimir Oltean 	void	(*master_state_change)(struct dsa_switch *ds,
1177295ab96fSVladimir Oltean 				       const struct net_device *master,
1178295ab96fSVladimir Oltean 				       bool operational);
11796b297524SAndrew Lunn };
11806b297524SAndrew Lunn 
11816b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
11826b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
11836b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
11846b297524SAndrew Lunn 
11856b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
11866b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
11876b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
11886b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
11896b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
11906b297524SAndrew Lunn 				const struct devlink_param *params,
11916b297524SAndrew Lunn 				size_t params_count);
11926b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
11936b297524SAndrew Lunn 				   const struct devlink_param *params,
11946b297524SAndrew Lunn 				   size_t params_count);
11955cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
11965cd73fbdSAndrew Lunn 				  const char *resource_name,
11975cd73fbdSAndrew Lunn 				  u64 resource_size,
11985cd73fbdSAndrew Lunn 				  u64 resource_id,
11995cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
12005cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
12015cd73fbdSAndrew Lunn 
12025cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
12035cd73fbdSAndrew Lunn 
12045cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
12055cd73fbdSAndrew Lunn 					   u64 resource_id,
12065cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
12075cd73fbdSAndrew Lunn 					   void *occ_get_priv);
12085cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
12095cd73fbdSAndrew Lunn 					     u64 resource_id);
121097c82c23SAndrew Lunn struct devlink_region *
121197c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds,
121297c82c23SAndrew Lunn 			  const struct devlink_region_ops *ops,
121397c82c23SAndrew Lunn 			  u32 region_max_snapshots, u64 region_size);
121408156ba4SAndrew Lunn struct devlink_region *
121508156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds,
121608156ba4SAndrew Lunn 			       int port,
121708156ba4SAndrew Lunn 			       const struct devlink_port_region_ops *ops,
121808156ba4SAndrew Lunn 			       u32 region_max_snapshots, u64 region_size);
121997c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region);
122097c82c23SAndrew Lunn 
1221e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
12225cd73fbdSAndrew Lunn 
12236b297524SAndrew Lunn struct dsa_devlink_priv {
12246b297524SAndrew Lunn 	struct dsa_switch *ds;
1225c8f0b869SBen Hutchings };
1226c8f0b869SBen Hutchings 
1227ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1228ccc3e6b0SAndrew Lunn {
1229ccc3e6b0SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1230ccc3e6b0SAndrew Lunn 
1231ccc3e6b0SAndrew Lunn 	return dl_priv->ds;
1232ccc3e6b0SAndrew Lunn }
1233ccc3e6b0SAndrew Lunn 
12347d1e2a10SAndrew Lunn static inline
12357d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
12367d1e2a10SAndrew Lunn {
12377d1e2a10SAndrew Lunn 	struct devlink *dl = port->devlink;
12387d1e2a10SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
12397d1e2a10SAndrew Lunn 
12407d1e2a10SAndrew Lunn 	return dl_priv->ds;
12417d1e2a10SAndrew Lunn }
12427d1e2a10SAndrew Lunn 
12437d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port)
12447d1e2a10SAndrew Lunn {
12457d1e2a10SAndrew Lunn 	return port->index;
12467d1e2a10SAndrew Lunn }
12477d1e2a10SAndrew Lunn 
1248ab3d408dSFlorian Fainelli struct dsa_switch_driver {
1249ab3d408dSFlorian Fainelli 	struct list_head	list;
1250a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
1251ab3d408dSFlorian Fainelli };
1252ab3d408dSFlorian Fainelli 
125314b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
1254c8f0b869SBen Hutchings 
12557e580490SVladimir Oltean bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
12567e580490SVladimir Oltean 				 const unsigned char *addr, u16 vid,
12577e580490SVladimir Oltean 				 struct dsa_db db);
12587e580490SVladimir Oltean bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
12597e580490SVladimir Oltean 				 const struct switchdev_obj_port_mdb *mdb,
12607e580490SVladimir Oltean 				 struct dsa_db db);
1261f9cef64fSVladimir Oltean 
126273a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
12639eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev)
1264c6e970a0SAndrew Lunn {
1265c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
1266717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
1267c6e970a0SAndrew Lunn #endif
1268c6e970a0SAndrew Lunn 	return false;
1269c6e970a0SAndrew Lunn }
1270c6e970a0SAndrew Lunn 
12719790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail
12729790cf20SVladimir Oltean  * tags, which don't break dissection) can be treated the same from the
12739790cf20SVladimir Oltean  * perspective of the flow dissector.
12749790cf20SVladimir Oltean  *
12759790cf20SVladimir Oltean  * We need to return:
12769790cf20SVladimir Oltean  *  - offset: the (B - A) difference between:
12779790cf20SVladimir Oltean  *    A. the position of the real EtherType and
12789790cf20SVladimir Oltean  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
12799790cf20SVladimir Oltean  *       after the normal EtherType was supposed to be)
12809790cf20SVladimir Oltean  *    The offset in bytes is exactly equal to the tagger overhead (and half of
12819790cf20SVladimir Oltean  *    that, in __be16 shorts).
12829790cf20SVladimir Oltean  *
12839790cf20SVladimir Oltean  *  - proto: the value of the real EtherType.
12849790cf20SVladimir Oltean  */
12859790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
12869790cf20SVladimir Oltean 						__be16 *proto, int *offset)
12879790cf20SVladimir Oltean {
12889790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
12899790cf20SVladimir Oltean 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
12904e500251SVladimir Oltean 	int tag_len = ops->needed_headroom;
12919790cf20SVladimir Oltean 
12929790cf20SVladimir Oltean 	*offset = tag_len;
12939790cf20SVladimir Oltean 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
12949790cf20SVladimir Oltean #endif
12959790cf20SVladimir Oltean }
12969790cf20SVladimir Oltean 
12974cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
12984cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev)
12994cfab356SFlorian Fainelli {
13004cfab356SFlorian Fainelli 	int err = -EOPNOTSUPP;
13014cfab356SFlorian Fainelli 
13024cfab356SFlorian Fainelli 	if (!dev->dsa_ptr)
13034cfab356SFlorian Fainelli 		return err;
13044cfab356SFlorian Fainelli 
13054cfab356SFlorian Fainelli 	if (!dev->dsa_ptr->netdev_ops)
13064cfab356SFlorian Fainelli 		return err;
13074cfab356SFlorian Fainelli 
13084cfab356SFlorian Fainelli 	return 0;
13094cfab356SFlorian Fainelli }
13104cfab356SFlorian Fainelli 
1311a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
13124cfab356SFlorian Fainelli 				    int cmd)
13134cfab356SFlorian Fainelli {
13144cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *ops;
13154cfab356SFlorian Fainelli 	int err;
13164cfab356SFlorian Fainelli 
13174cfab356SFlorian Fainelli 	err = __dsa_netdevice_ops_check(dev);
13184cfab356SFlorian Fainelli 	if (err)
13194cfab356SFlorian Fainelli 		return err;
13204cfab356SFlorian Fainelli 
13214cfab356SFlorian Fainelli 	ops = dev->dsa_ptr->netdev_ops;
13224cfab356SFlorian Fainelli 
1323a7605370SArnd Bergmann 	return ops->ndo_eth_ioctl(dev, ifr, cmd);
13244cfab356SFlorian Fainelli }
13254cfab356SFlorian Fainelli #else
1326a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
13274cfab356SFlorian Fainelli 				    int cmd)
13284cfab356SFlorian Fainelli {
13294cfab356SFlorian Fainelli 	return -EOPNOTSUPP;
13304cfab356SFlorian Fainelli }
13314cfab356SFlorian Fainelli #endif
13324cfab356SFlorian Fainelli 
133383c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
133423c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
13350650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds);
13363b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1337a2614140SVladimir Oltean void dsa_flush_workqueue(void);
1338ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
1339ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
1340ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
1341ea825e70SFlorian Fainelli #else
1342ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
1343ea825e70SFlorian Fainelli {
1344ea825e70SFlorian Fainelli 	return 0;
1345ea825e70SFlorian Fainelli }
1346ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
1347ea825e70SFlorian Fainelli {
1348ea825e70SFlorian Fainelli 	return 0;
1349ea825e70SFlorian Fainelli }
1350ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
1351ea825e70SFlorian Fainelli 
135260724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
1353a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev);
135460724d4bSFlorian Fainelli #else
1355a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev)
1356a5e3c9baSVladimir Oltean {
1357a5e3c9baSVladimir Oltean 	return false;
1358a5e3c9baSVladimir Oltean }
135960724d4bSFlorian Fainelli #endif
136060724d4bSFlorian Fainelli 
136197a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
136211d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1363cf963573SFlorian Fainelli 
1364d3b8c049SAndrew Lunn struct dsa_tag_driver {
1365d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
1366d3b8c049SAndrew Lunn 	struct list_head list;
1367d3b8c049SAndrew Lunn 	struct module *owner;
1368d3b8c049SAndrew Lunn };
1369d3b8c049SAndrew Lunn 
1370d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1371d3b8c049SAndrew Lunn 			      unsigned int count,
1372d3b8c049SAndrew Lunn 			      struct module *owner);
1373d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1374d3b8c049SAndrew Lunn 				unsigned int count);
1375d3b8c049SAndrew Lunn 
1376d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
1377d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
1378d3b8c049SAndrew Lunn {									\
1379d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
1380d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
1381d3b8c049SAndrew Lunn 	return 0;							\
1382d3b8c049SAndrew Lunn }									\
1383d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
1384d3b8c049SAndrew Lunn 									\
1385d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
1386d3b8c049SAndrew Lunn {									\
1387d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
1388d3b8c049SAndrew Lunn }									\
1389d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
1390d3b8c049SAndrew Lunn 
1391d3b8c049SAndrew Lunn /**
1392d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1393d3b8c049SAndrew Lunn  * drivers
1394c7d9a675SLuiz Angelo Daros de Luca  * @__ops_array: Array of tag driver structures
1395d3b8c049SAndrew Lunn  *
1396d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
1397d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
1398d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
1399d3b8c049SAndrew Lunn  */
1400d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
1401d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1402d3b8c049SAndrew Lunn 
1403d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1404d3b8c049SAndrew Lunn 
1405d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
1406d3b8c049SAndrew Lunn  * drivers
1407d3b8c049SAndrew Lunn  */
1408d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
1409d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1410d3b8c049SAndrew Lunn 	.ops = &__ops,							\
1411d3b8c049SAndrew Lunn }
1412d3b8c049SAndrew Lunn 
1413d3b8c049SAndrew Lunn /**
1414d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1415d3b8c049SAndrew Lunn  * driver
1416d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
1417d3b8c049SAndrew Lunn  *
1418d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
1419d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
1420d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
1421d3b8c049SAndrew Lunn  */
1422d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
1423d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
1424d3b8c049SAndrew Lunn 									\
1425d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1426d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
1427d3b8c049SAndrew Lunn };									\
1428d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
142991da11f8SLennert Buytenhek #endif
1430d3b8c049SAndrew Lunn 
1431