xref: /linux/include/net/dsa.h (revision 947c8746e2c31bb469ba9ee02dc739be6a98ee38)
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
550b42f033SAndrew Lunn 
56ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
570b42f033SAndrew Lunn 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
580b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
59964dbf18SÁlvaro Fernández Rojas 	DSA_TAG_PROTO_BRCM_LEGACY	= DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
600b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
610b42f033SAndrew Lunn 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
620b42f033SAndrew Lunn 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
630b42f033SAndrew Lunn 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
640b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
650b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
660b42f033SAndrew Lunn 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
670b42f033SAndrew Lunn 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
680b42f033SAndrew Lunn 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
690b42f033SAndrew Lunn 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
70f9bbe447SVladimir Oltean 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
71227d07a0SVladimir Oltean 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
72016e43a2STristram Ha 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
738dce89aaSVladimir Oltean 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
7448fda74fSOleksij Rempel 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
75efd7fe68SLinus Walleij 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
7601ef09caSKurt Kanzenbach 	DSA_TAG_PROTO_HELLCREEK		= DSA_TAG_PROTO_HELLCREEK_VALUE,
7754a52823SGeorge McCollister 	DSA_TAG_PROTO_XRS700X		= DSA_TAG_PROTO_XRS700X_VALUE,
787c83a7c5SVladimir Oltean 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
797c4bb540SVladimir Oltean 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
804913b8ebSVladimir Oltean 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
811521d5adSAlvin Šipraga 	DSA_TAG_PROTO_RTL8_4		= DSA_TAG_PROTO_RTL8_4_VALUE,
82ac7a04c3SFlorian Fainelli };
835037d532SFlorian Fainelli 
8490af1059SBrandon Streiff struct dsa_switch;
853e8a72d1SFlorian Fainelli 
8668277a2cSJohn Crispin struct dsa_device_ops {
8768277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
8829a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
892e8cb1b3SVladimir Oltean 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
90598a9680SJohn Crispin 			     int *offset);
914e500251SVladimir Oltean 	unsigned int needed_headroom;
924e500251SVladimir Oltean 	unsigned int needed_tailroom;
93875138f8SAndrew Lunn 	const char *name;
94056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
95c3975400SVladimir Oltean 	/* Some tagging protocols either mangle or shift the destination MAC
96c3975400SVladimir Oltean 	 * address, in which case the DSA master would drop packets on ingress
97c3975400SVladimir Oltean 	 * if what it understands out of the destination MAC address is not in
98c3975400SVladimir Oltean 	 * its RX filter.
99c3975400SVladimir Oltean 	 */
100c3975400SVladimir Oltean 	bool promisc_on_master;
10168277a2cSJohn Crispin };
10268277a2cSJohn Crispin 
1034cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the
1044cfab356SFlorian Fainelli  * DSA layer on top of the DSA CPU/management net_device instance. This is
1054cfab356SFlorian Fainelli  * used by the core net_device layer while calling various net_device_ops
1064cfab356SFlorian Fainelli  * function pointers.
1074cfab356SFlorian Fainelli  */
1084cfab356SFlorian Fainelli struct dsa_netdevice_ops {
109a7605370SArnd Bergmann 	int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
1104cfab356SFlorian Fainelli 			     int cmd);
1114cfab356SFlorian Fainelli };
1124cfab356SFlorian Fainelli 
1130b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
1140b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
1150b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
1160b42f033SAndrew Lunn 
117cf50dcc2SBen Hutchings struct dsa_switch_tree {
11883c0afaeSAndrew Lunn 	struct list_head	list;
11983c0afaeSAndrew Lunn 
120f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
121f515f192SVivien Didelot 	struct raw_notifier_head	nh;
122f515f192SVivien Didelot 
12383c0afaeSAndrew Lunn 	/* Tree identifier */
12449463b7fSVivien Didelot 	unsigned int index;
12583c0afaeSAndrew Lunn 
12683c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
12783c0afaeSAndrew Lunn 	struct kref refcount;
12883c0afaeSAndrew Lunn 
12983c0afaeSAndrew Lunn 	/* Has this tree been applied to the hardware? */
130ec15dd42SVivien Didelot 	bool setup;
13183c0afaeSAndrew Lunn 
132357f203bSVladimir Oltean 	/* Tagging protocol operations */
133357f203bSVladimir Oltean 	const struct dsa_device_ops *tag_ops;
134357f203bSVladimir Oltean 
135deff7107STobias Waldekranz 	/* Default tagging protocol preferred by the switches in this
136deff7107STobias Waldekranz 	 * tree.
137deff7107STobias Waldekranz 	 */
138deff7107STobias Waldekranz 	enum dsa_tag_protocol default_proto;
139deff7107STobias Waldekranz 
140cf50dcc2SBen Hutchings 	/*
141cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
142cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
143cf50dcc2SBen Hutchings 	 */
144cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
145cf85d08fSLennert Buytenhek 
146ab8ccae1SVivien Didelot 	/* List of switch ports */
147ab8ccae1SVivien Didelot 	struct list_head ports;
148ab8ccae1SVivien Didelot 
149c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
150c5f51765SVivien Didelot 	struct list_head rtable;
151058102a6STobias Waldekranz 
152058102a6STobias Waldekranz 	/* Maps offloaded LAG netdevs to a zero-based linear ID for
153058102a6STobias Waldekranz 	 * drivers that need it.
154058102a6STobias Waldekranz 	 */
155058102a6STobias Waldekranz 	struct net_device **lags;
156058102a6STobias Waldekranz 	unsigned int lags_len;
1575b22d366SVladimir Oltean 
1585b22d366SVladimir Oltean 	/* Track the largest switch index within a tree */
1595b22d366SVladimir Oltean 	unsigned int last_switch;
160cf50dcc2SBen Hutchings };
161cf50dcc2SBen Hutchings 
162058102a6STobias Waldekranz #define dsa_lags_foreach_id(_id, _dst)				\
163058102a6STobias Waldekranz 	for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++)	\
164058102a6STobias Waldekranz 		if ((_dst)->lags[(_id)])
165058102a6STobias Waldekranz 
166058102a6STobias Waldekranz #define dsa_lag_foreach_port(_dp, _dst, _lag)			\
167058102a6STobias Waldekranz 	list_for_each_entry((_dp), &(_dst)->ports, list)	\
168058102a6STobias Waldekranz 		if ((_dp)->lag_dev == (_lag))
169058102a6STobias Waldekranz 
17018596f50SGeorge McCollister #define dsa_hsr_foreach_port(_dp, _ds, _hsr)			\
17118596f50SGeorge McCollister 	list_for_each_entry((_dp), &(_ds)->dst->ports, list)	\
17218596f50SGeorge McCollister 		if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
17318596f50SGeorge McCollister 
174058102a6STobias Waldekranz static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
175058102a6STobias Waldekranz 					     unsigned int id)
176058102a6STobias Waldekranz {
177058102a6STobias Waldekranz 	return dst->lags[id];
178058102a6STobias Waldekranz }
179058102a6STobias Waldekranz 
180058102a6STobias Waldekranz static inline int dsa_lag_id(struct dsa_switch_tree *dst,
181058102a6STobias Waldekranz 			     struct net_device *lag)
182058102a6STobias Waldekranz {
183058102a6STobias Waldekranz 	unsigned int id;
184058102a6STobias Waldekranz 
185058102a6STobias Waldekranz 	dsa_lags_foreach_id(id, dst) {
186058102a6STobias Waldekranz 		if (dsa_lag_dev(dst, id) == lag)
187058102a6STobias Waldekranz 			return id;
188058102a6STobias Waldekranz 	}
189058102a6STobias Waldekranz 
190058102a6STobias Waldekranz 	return -ENODEV;
191058102a6STobias Waldekranz }
192058102a6STobias Waldekranz 
19334297176SVladimir Oltean /* TC matchall action types */
194f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
195f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
19634297176SVladimir Oltean 	DSA_PORT_MALL_POLICER,
197f50f2127SFlorian Fainelli };
198f50f2127SFlorian Fainelli 
199f50f2127SFlorian Fainelli /* TC mirroring entry */
200f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
201f50f2127SFlorian Fainelli 	u8 to_local_port;
202f50f2127SFlorian Fainelli 	bool ingress;
203f50f2127SFlorian Fainelli };
204f50f2127SFlorian Fainelli 
20534297176SVladimir Oltean /* TC port policer entry */
20634297176SVladimir Oltean struct dsa_mall_policer_tc_entry {
2075f035af7SPo Liu 	u32 burst;
20834297176SVladimir Oltean 	u64 rate_bytes_per_sec;
20934297176SVladimir Oltean };
21034297176SVladimir Oltean 
211f50f2127SFlorian Fainelli /* TC matchall entry */
212f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
213f50f2127SFlorian Fainelli 	struct list_head list;
214f50f2127SFlorian Fainelli 	unsigned long cookie;
215f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
216f50f2127SFlorian Fainelli 	union {
217f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
21834297176SVladimir Oltean 		struct dsa_mall_policer_tc_entry policer;
219f50f2127SFlorian Fainelli 	};
220f50f2127SFlorian Fainelli };
221f50f2127SFlorian Fainelli 
222f50f2127SFlorian Fainelli 
223c8b09808SAndrew Lunn struct dsa_port {
224f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
225f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
226f8b8b1cdSVivien Didelot 	 */
227f8b8b1cdSVivien Didelot 	union {
228f8b8b1cdSVivien Didelot 		struct net_device *master;
229f8b8b1cdSVivien Didelot 		struct net_device *slave;
230f8b8b1cdSVivien Didelot 	};
231f8b8b1cdSVivien Didelot 
232357f203bSVladimir Oltean 	/* Copy of the tagging protocol operations, for quicker access
233357f203bSVladimir Oltean 	 * in the data path. Valid only for the CPU ports.
234357f203bSVladimir Oltean 	 */
23515240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
23615240248SVivien Didelot 
2373e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
2383e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
23929a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
2403e41f93bSVivien Didelot 
241057cad2cSVivien Didelot 	enum {
242057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
243057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
244057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
245057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
246057cad2cSVivien Didelot 	} type;
247057cad2cSVivien Didelot 
248818be848SVivien Didelot 	struct dsa_switch	*ds;
249818be848SVivien Didelot 	unsigned int		index;
25071e0bbdeSFlorian Fainelli 	const char		*name;
25168b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
25283216e39SMichael Walle 	u8			mac[ETH_ALEN];
253189b0d93SAndrew Lunn 	struct device_node	*dn;
25434a79f63SVivien Didelot 	unsigned int		ageing_time;
25533162e9aSVladimir Oltean 	bool			vlan_filtering;
2565313a37bSVladimir Oltean 	/* Managed by DSA on user ports and by drivers on CPU and DSA ports */
257045c45d1SVladimir Oltean 	bool			learning;
258732f794cSVivien Didelot 	u8			stp_state;
259a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
2603f9bb030SVladimir Oltean 	unsigned int		bridge_num;
26196567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
2623122433eSAndrew Lunn 	bool			devlink_port_setup;
263aab9c406SFlorian Fainelli 	struct phylink		*pl;
26444cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
265058102a6STobias Waldekranz 	struct net_device	*lag_dev;
266058102a6STobias Waldekranz 	bool			lag_tx_enabled;
26718596f50SGeorge McCollister 	struct net_device	*hsr_dev;
26897a69a0dSVladimir Oltean 
269ab8ccae1SVivien Didelot 	struct list_head list;
270ab8ccae1SVivien Didelot 
27167dbb9d4SFlorian Fainelli 	/*
272c362beb0SVladimir Oltean 	 * Give the switch driver somewhere to hang its per-port private data
273c362beb0SVladimir Oltean 	 * structures (accessible from the tagger).
274c362beb0SVladimir Oltean 	 */
275c362beb0SVladimir Oltean 	void *priv;
276c362beb0SVladimir Oltean 
277c362beb0SVladimir Oltean 	/*
27867dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
27967dbb9d4SFlorian Fainelli 	 */
28067dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
281da7b9e9bSFlorian Fainelli 
282da7b9e9bSFlorian Fainelli 	/*
283da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
284da7b9e9bSFlorian Fainelli 	 */
2854cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *netdev_ops;
286fb35c60cSVivien Didelot 
287161ca59dSVladimir Oltean 	/* List of MAC addresses that must be forwarded on this port.
288161ca59dSVladimir Oltean 	 * These are only valid on CPU ports and DSA links.
289161ca59dSVladimir Oltean 	 */
290338a3a47SVladimir Oltean 	struct mutex		addr_lists_lock;
2913f6e32f9SVladimir Oltean 	struct list_head	fdbs;
292161ca59dSVladimir Oltean 	struct list_head	mdbs;
293161ca59dSVladimir Oltean 
294fb35c60cSVivien Didelot 	bool setup;
295c8b09808SAndrew Lunn };
296c8b09808SAndrew Lunn 
297c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
298c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
299c5f51765SVivien Didelot  * but this would require some more complex tree walking,
300c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
301c5f51765SVivien Didelot  */
302c5f51765SVivien Didelot struct dsa_link {
303c5f51765SVivien Didelot 	struct dsa_port *dp;
304c5f51765SVivien Didelot 	struct dsa_port *link_dp;
305c5f51765SVivien Didelot 	struct list_head list;
306c5f51765SVivien Didelot };
307c5f51765SVivien Didelot 
308161ca59dSVladimir Oltean struct dsa_mac_addr {
309161ca59dSVladimir Oltean 	unsigned char addr[ETH_ALEN];
310161ca59dSVladimir Oltean 	u16 vid;
311161ca59dSVladimir Oltean 	refcount_t refcount;
312161ca59dSVladimir Oltean 	struct list_head list;
313161ca59dSVladimir Oltean };
314161ca59dSVladimir Oltean 
315c8f0b869SBen Hutchings struct dsa_switch {
316fb35c60cSVivien Didelot 	bool setup;
317fb35c60cSVivien Didelot 
318c33063d6SAndrew Lunn 	struct device *dev;
319c33063d6SAndrew Lunn 
320c8f0b869SBen Hutchings 	/*
321c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
322c8f0b869SBen Hutchings 	 */
323c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
32499feaafcSVivien Didelot 	unsigned int		index;
325c8f0b869SBen Hutchings 
326f515f192SVivien Didelot 	/* Listener for switch fabric events */
327f515f192SVivien Didelot 	struct notifier_block	nb;
328f515f192SVivien Didelot 
329c8f0b869SBen Hutchings 	/*
3307543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
3317543a6d5SAndrew Lunn 	 * structure.
3327543a6d5SAndrew Lunn 	 */
3337543a6d5SAndrew Lunn 	void *priv;
3347543a6d5SAndrew Lunn 
3357543a6d5SAndrew Lunn 	/*
336c8f0b869SBen Hutchings 	 * Configuration data for this switch.
337c8f0b869SBen Hutchings 	 */
338ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
339c8f0b869SBen Hutchings 
340c8f0b869SBen Hutchings 	/*
3419d490b4eSVivien Didelot 	 * The switch operations.
342c8f0b869SBen Hutchings 	 */
343a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
344c8f0b869SBen Hutchings 
34566472fc0SAndrew Lunn 	/*
346c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
347c8f0b869SBen Hutchings 	 */
3480d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
349c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
350a0c02161SVivien Didelot 
3510f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
3520f3da6afSVivien Didelot 	unsigned int ageing_time_min;
3530f3da6afSVivien Didelot 	unsigned int ageing_time_max;
3540f3da6afSVivien Didelot 
355d7b1fd52SVladimir Oltean 	/* Storage for drivers using tag_8021q */
356d7b1fd52SVladimir Oltean 	struct dsa_8021q_context *tag_8021q_ctx;
357d7b1fd52SVladimir Oltean 
35896567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
35996567d5dSAndrew Lunn 	struct devlink		*devlink;
36096567d5dSAndrew Lunn 
36155199df6SFlorian Fainelli 	/* Number of switch port queues */
36255199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
36355199df6SFlorian Fainelli 
3648f5d16f6SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
3658f5d16f6SVladimir Oltean 	 * settings on ports if not hardware-supported
3668f5d16f6SVladimir Oltean 	 */
3678f5d16f6SVladimir Oltean 	bool			vlan_filtering_is_global;
3688f5d16f6SVladimir Oltean 
36958adf9dcSVladimir Oltean 	/* Keep VLAN filtering enabled on ports not offloading any upper. */
37058adf9dcSVladimir Oltean 	bool			needs_standalone_vlan_filtering;
37158adf9dcSVladimir Oltean 
37254a0ed0dSRussell King 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
37354a0ed0dSRussell King 	 * that have vlan_filtering=0. All drivers should ideally set this (and
37454a0ed0dSRussell King 	 * then the option would get removed), but it is unknown whether this
37554a0ed0dSRussell King 	 * would break things or not.
37654a0ed0dSRussell King 	 */
37754a0ed0dSRussell King 	bool			configure_vlan_while_not_filtering;
37854a0ed0dSRussell King 
3791dc0408cSFlorian Fainelli 	/* If the switch driver always programs the CPU port as egress tagged
3801dc0408cSFlorian Fainelli 	 * despite the VLAN configuration indicating otherwise, then setting
3811dc0408cSFlorian Fainelli 	 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
3821dc0408cSFlorian Fainelli 	 * default_pvid VLAN tagged frames to offer a consistent behavior
3831dc0408cSFlorian Fainelli 	 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
3841dc0408cSFlorian Fainelli 	 */
3851dc0408cSFlorian Fainelli 	bool			untag_bridge_pvid;
3861dc0408cSFlorian Fainelli 
387d5f19486SVladimir Oltean 	/* Let DSA manage the FDB entries towards the CPU, based on the
388d5f19486SVladimir Oltean 	 * software bridge database.
389d5f19486SVladimir Oltean 	 */
390d5f19486SVladimir Oltean 	bool			assisted_learning_on_cpu_port;
391d5f19486SVladimir Oltean 
39214574676SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
39314574676SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
39414574676SVladimir Oltean 	 */
39514574676SVladimir Oltean 	bool			vlan_filtering;
39614574676SVladimir Oltean 
397787cac3fSVladimir Oltean 	/* MAC PCS does not provide link state change interrupt, and requires
398787cac3fSVladimir Oltean 	 * polling. Flag passed on to PHYLINK.
399787cac3fSVladimir Oltean 	 */
400787cac3fSVladimir Oltean 	bool			pcs_poll;
401787cac3fSVladimir Oltean 
402bff33f7eSVladimir Oltean 	/* For switches that only have the MRU configurable. To ensure the
403bff33f7eSVladimir Oltean 	 * configured MTU is not exceeded, normalization of MRU on all bridged
404bff33f7eSVladimir Oltean 	 * interfaces is needed.
405bff33f7eSVladimir Oltean 	 */
406bff33f7eSVladimir Oltean 	bool			mtu_enforcement_ingress;
407bff33f7eSVladimir Oltean 
408058102a6STobias Waldekranz 	/* Drivers that benefit from having an ID associated with each
409058102a6STobias Waldekranz 	 * offloaded LAG should set this to the maximum number of
410058102a6STobias Waldekranz 	 * supported IDs. DSA will then maintain a mapping of _at
411058102a6STobias Waldekranz 	 * least_ these many IDs, accessible to drivers via
412058102a6STobias Waldekranz 	 * dsa_lag_id().
413058102a6STobias Waldekranz 	 */
414058102a6STobias Waldekranz 	unsigned int		num_lag_ids;
415058102a6STobias Waldekranz 
416*947c8746SVladimir Oltean 	/* Drivers that support bridge forwarding offload or FDB isolation
417*947c8746SVladimir Oltean 	 * should set this to the maximum number of bridges spanning the same
418*947c8746SVladimir Oltean 	 * switch tree (or all trees, in the case of cross-tree bridging
419*947c8746SVladimir Oltean 	 * support) that can be offloaded.
420123abc06SVladimir Oltean 	 */
421*947c8746SVladimir Oltean 	unsigned int		max_num_bridges;
422123abc06SVladimir Oltean 
423a0c02161SVivien Didelot 	size_t num_ports;
424c8f0b869SBen Hutchings };
425c8f0b869SBen Hutchings 
42668bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
427c38c5a66SVivien Didelot {
428b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
429d607525bSVivien Didelot 	struct dsa_port *dp;
430b96ddf25SVivien Didelot 
431b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
432b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
433b96ddf25SVivien Didelot 			return dp;
434d607525bSVivien Didelot 
435d607525bSVivien Didelot 	return NULL;
436c38c5a66SVivien Didelot }
437c38c5a66SVivien Didelot 
438a8986681SVladimir Oltean static inline bool dsa_port_is_dsa(struct dsa_port *port)
439a8986681SVladimir Oltean {
440a8986681SVladimir Oltean 	return port->type == DSA_PORT_TYPE_DSA;
441a8986681SVladimir Oltean }
442a8986681SVladimir Oltean 
443a8986681SVladimir Oltean static inline bool dsa_port_is_cpu(struct dsa_port *port)
444a8986681SVladimir Oltean {
445a8986681SVladimir Oltean 	return port->type == DSA_PORT_TYPE_CPU;
446a8986681SVladimir Oltean }
447a8986681SVladimir Oltean 
448a8986681SVladimir Oltean static inline bool dsa_port_is_user(struct dsa_port *dp)
449a8986681SVladimir Oltean {
450a8986681SVladimir Oltean 	return dp->type == DSA_PORT_TYPE_USER;
451a8986681SVladimir Oltean }
452a8986681SVladimir Oltean 
453a57d8c21SVladimir Oltean static inline bool dsa_port_is_unused(struct dsa_port *dp)
454a57d8c21SVladimir Oltean {
455a57d8c21SVladimir Oltean 	return dp->type == DSA_PORT_TYPE_UNUSED;
456a57d8c21SVladimir Oltean }
457a57d8c21SVladimir Oltean 
458bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
459bff7b688SVivien Didelot {
460c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
461bff7b688SVivien Didelot }
462bff7b688SVivien Didelot 
463c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
464c8f0b869SBen Hutchings {
465c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
466c8f0b869SBen Hutchings }
467c8f0b869SBen Hutchings 
46860045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
46960045cbfSAndrew Lunn {
470c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
47160045cbfSAndrew Lunn }
47260045cbfSAndrew Lunn 
4732b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
4746cd456f3SVivien Didelot {
475c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
4766cd456f3SVivien Didelot }
4776cd456f3SVivien Didelot 
47882b31898SVladimir Oltean #define dsa_tree_for_each_user_port(_dp, _dst) \
47982b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_dst)->ports, list) \
48082b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
48182b31898SVladimir Oltean 
48282b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \
48382b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
48482b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
48582b31898SVladimir Oltean 
48682b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
48782b31898SVladimir Oltean 	list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
48882b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
48982b31898SVladimir Oltean 
49082b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
49182b31898SVladimir Oltean 	list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
49282b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
49382b31898SVladimir Oltean 
49482b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \
49582b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
49682b31898SVladimir Oltean 		if (!dsa_port_is_unused((_dp)))
49782b31898SVladimir Oltean 
49882b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \
49982b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
50082b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
50182b31898SVladimir Oltean 
50282b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \
50382b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
50482b31898SVladimir Oltean 		if (dsa_port_is_cpu((_dp)))
50582b31898SVladimir Oltean 
50602bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
50702bc6e54SVivien Didelot {
508d0004a02SVladimir Oltean 	struct dsa_port *dp;
509c38c5a66SVivien Didelot 	u32 mask = 0;
51002bc6e54SVivien Didelot 
511d0004a02SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
512d0004a02SVladimir Oltean 		mask |= BIT(dp->index);
513c38c5a66SVivien Didelot 
514c38c5a66SVivien Didelot 	return mask;
515c8652c83SVivien Didelot }
516c8652c83SVivien Didelot 
517c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
518c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
519c5f51765SVivien Didelot {
520c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
521c5f51765SVivien Didelot 	struct dsa_link *dl;
522c5f51765SVivien Didelot 
523c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
524c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
525c5f51765SVivien Didelot 			return dl->dp->index;
526c5f51765SVivien Didelot 
527c5f51765SVivien Didelot 	return ds->num_ports;
528c5f51765SVivien Didelot }
529c5f51765SVivien Didelot 
5303b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
5313b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
5323b8fac5dSVivien Didelot 					    int port)
5333b8fac5dSVivien Didelot {
5343b8fac5dSVivien Didelot 	if (device == ds->index)
5353b8fac5dSVivien Didelot 		return port;
5363b8fac5dSVivien Didelot 	else
537c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
5383b8fac5dSVivien Didelot }
5393b8fac5dSVivien Didelot 
5403b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
54107073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
542c8f0b869SBen Hutchings {
54307073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
54407073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
54507073c79SVivien Didelot 
54607073c79SVivien Didelot 	if (!cpu_dp)
54707073c79SVivien Didelot 		return port;
548c8f0b869SBen Hutchings 
5493b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
550c8f0b869SBen Hutchings }
551c8f0b869SBen Hutchings 
55263609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */
55363609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
55463609c8fSVladimir Oltean {
55563609c8fSVladimir Oltean 	if (dsa_is_unused_port(ds, port))
55663609c8fSVladimir Oltean 		return false;
55763609c8fSVladimir Oltean 
55863609c8fSVladimir Oltean 	return port == dsa_upstream_port(ds, port);
55963609c8fSVladimir Oltean }
56063609c8fSVladimir Oltean 
56163609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
56263609c8fSVladimir Oltean  * that the routing port from @downstream_ds to @upstream_ds is also the port
56363609c8fSVladimir Oltean  * which @downstream_ds uses to reach its dedicated CPU.
56463609c8fSVladimir Oltean  */
56563609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
56663609c8fSVladimir Oltean 					     struct dsa_switch *downstream_ds)
56763609c8fSVladimir Oltean {
56863609c8fSVladimir Oltean 	int routing_port;
56963609c8fSVladimir Oltean 
57063609c8fSVladimir Oltean 	if (upstream_ds == downstream_ds)
57163609c8fSVladimir Oltean 		return true;
57263609c8fSVladimir Oltean 
57363609c8fSVladimir Oltean 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
57463609c8fSVladimir Oltean 
57563609c8fSVladimir Oltean 	return dsa_is_upstream_port(downstream_ds, routing_port);
57663609c8fSVladimir Oltean }
57763609c8fSVladimir Oltean 
578cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
579cf2d45f5SVladimir Oltean {
580cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
581cf2d45f5SVladimir Oltean 
582cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
583cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
584cf2d45f5SVladimir Oltean 	else
585cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
586cf2d45f5SVladimir Oltean }
587cf2d45f5SVladimir Oltean 
588cc76ce9eSTobias Waldekranz static inline
589cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
590cc76ce9eSTobias Waldekranz {
591cc76ce9eSTobias Waldekranz 	if (!dp->bridge_dev)
592cc76ce9eSTobias Waldekranz 		return NULL;
593cc76ce9eSTobias Waldekranz 
594cc76ce9eSTobias Waldekranz 	if (dp->lag_dev)
595cc76ce9eSTobias Waldekranz 		return dp->lag_dev;
596cc76ce9eSTobias Waldekranz 	else if (dp->hsr_dev)
597cc76ce9eSTobias Waldekranz 		return dp->hsr_dev;
598cc76ce9eSTobias Waldekranz 
599cc76ce9eSTobias Waldekranz 	return dp->slave;
600cc76ce9eSTobias Waldekranz }
601cc76ce9eSTobias Waldekranz 
6022bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
6032bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
6049d490b4eSVivien Didelot struct dsa_switch_ops {
60553da0ebaSVladimir Oltean 	/*
60653da0ebaSVladimir Oltean 	 * Tagging protocol helpers called for the CPU ports and DSA links.
60753da0ebaSVladimir Oltean 	 * @get_tag_protocol retrieves the initial tagging protocol and is
60853da0ebaSVladimir Oltean 	 * mandatory. Switches which can operate using multiple tagging
60953da0ebaSVladimir Oltean 	 * protocols should implement @change_tag_protocol and report in
61053da0ebaSVladimir Oltean 	 * @get_tag_protocol the tagger in current use.
61153da0ebaSVladimir Oltean 	 */
6125ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
6134d776482SFlorian Fainelli 						  int port,
6144d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
61553da0ebaSVladimir Oltean 	int	(*change_tag_protocol)(struct dsa_switch *ds, int port,
61653da0ebaSVladimir Oltean 				       enum dsa_tag_protocol proto);
6177b314362SAndrew Lunn 
618fd292c18SVladimir Oltean 	/* Optional switch-wide initialization and destruction methods */
619c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
6205e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
621fd292c18SVladimir Oltean 
622fd292c18SVladimir Oltean 	/* Per-port initialization and destruction methods. Mandatory if the
623fd292c18SVladimir Oltean 	 * driver registers devlink port regions, optional otherwise.
624fd292c18SVladimir Oltean 	 */
625fd292c18SVladimir Oltean 	int	(*port_setup)(struct dsa_switch *ds, int port);
626fd292c18SVladimir Oltean 	void	(*port_teardown)(struct dsa_switch *ds, int port);
627fd292c18SVladimir Oltean 
6286819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
629c8f0b869SBen Hutchings 
630c8f0b869SBen Hutchings 	/*
631c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
632c8f0b869SBen Hutchings 	 */
633c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
634c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
635c8f0b869SBen Hutchings 			     int regnum, u16 val);
636c8f0b869SBen Hutchings 
637c8f0b869SBen Hutchings 	/*
638ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
639ec9436baSFlorian Fainelli 	 */
640ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
641ec9436baSFlorian Fainelli 				struct phy_device *phydev);
642ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
643ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
644ec9436baSFlorian Fainelli 
645ec9436baSFlorian Fainelli 	/*
64611d8f3ddSFlorian Fainelli 	 * PHYLINK integration
64711d8f3ddSFlorian Fainelli 	 */
648072eea6cSRussell King (Oracle) 	void	(*phylink_get_caps)(struct dsa_switch *ds, int port,
649072eea6cSRussell King (Oracle) 				    struct phylink_config *config);
65011d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
65111d8f3ddSFlorian Fainelli 				    unsigned long *supported,
65211d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
65311d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
65411d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
65511d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
65611d8f3ddSFlorian Fainelli 				      unsigned int mode,
65711d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
65811d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
65911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
66011d8f3ddSFlorian Fainelli 					 unsigned int mode,
66111d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
66211d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
66311d8f3ddSFlorian Fainelli 				       unsigned int mode,
66411d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
6655b502a7bSRussell King 				       struct phy_device *phydev,
6665b502a7bSRussell King 				       int speed, int duplex,
6675b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
66811d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
66911d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
67011d8f3ddSFlorian Fainelli 	/*
671c2ec5f2eSOleksij Rempel 	 * Port statistics counters.
672c8f0b869SBen Hutchings 	 */
67389f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
67489f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
675c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
676c8f0b869SBen Hutchings 				     int port, uint64_t *data);
67789f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
678cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
679cf963573SFlorian Fainelli 					 int port, uint64_t *data);
680487d3855SAlvin Šipraga 	void	(*get_eth_phy_stats)(struct dsa_switch *ds, int port,
681487d3855SAlvin Šipraga 				     struct ethtool_eth_phy_stats *phy_stats);
682487d3855SAlvin Šipraga 	void	(*get_eth_mac_stats)(struct dsa_switch *ds, int port,
683487d3855SAlvin Šipraga 				     struct ethtool_eth_mac_stats *mac_stats);
684487d3855SAlvin Šipraga 	void	(*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
685487d3855SAlvin Šipraga 				      struct ethtool_eth_ctrl_stats *ctrl_stats);
686c2ec5f2eSOleksij Rempel 	void	(*get_stats64)(struct dsa_switch *ds, int port,
687c2ec5f2eSOleksij Rempel 				   struct rtnl_link_stats64 *s);
688a71acad9SOleksij Rempel 	void	(*self_test)(struct dsa_switch *ds, int port,
689a71acad9SOleksij Rempel 			     struct ethtool_test *etest, u64 *data);
69024462549SFlorian Fainelli 
69124462549SFlorian Fainelli 	/*
69219e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
69319e57c4eSFlorian Fainelli 	 */
69419e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
69519e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
69619e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
69719e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
69819e57c4eSFlorian Fainelli 
69919e57c4eSFlorian Fainelli 	/*
7000336369dSBrandon Streiff 	 * ethtool timestamp info
7010336369dSBrandon Streiff 	 */
7020336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
7030336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
7040336369dSBrandon Streiff 
7050336369dSBrandon Streiff 	/*
70624462549SFlorian Fainelli 	 * Suspend and resume
70724462549SFlorian Fainelli 	 */
70824462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
70924462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
710b2f2af21SFlorian Fainelli 
711b2f2af21SFlorian Fainelli 	/*
712b2f2af21SFlorian Fainelli 	 * Port enable/disable
713b2f2af21SFlorian Fainelli 	 */
714b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
715b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
71675104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
7177905288fSFlorian Fainelli 
7187905288fSFlorian Fainelli 	/*
71908f50061SVivien Didelot 	 * Port's MAC EEE settings
7207905288fSFlorian Fainelli 	 */
72108f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
7227905288fSFlorian Fainelli 			       struct ethtool_eee *e);
72308f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
7247905288fSFlorian Fainelli 			       struct ethtool_eee *e);
72551579c3fSGuenter Roeck 
7266793abb4SGuenter Roeck 	/* EEPROM access */
7276793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
7286793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
7296793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
7306793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
7316793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
7323d762a0fSGuenter Roeck 
7333d762a0fSGuenter Roeck 	/*
7343d762a0fSGuenter Roeck 	 * Register access.
7353d762a0fSGuenter Roeck 	 */
7363d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
7373d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
7383d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
739b73adef6SFlorian Fainelli 
740b73adef6SFlorian Fainelli 	/*
741e358bef7SVladimir Oltean 	 * Upper device tracking.
742e358bef7SVladimir Oltean 	 */
743e358bef7SVladimir Oltean 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
744e358bef7SVladimir Oltean 				       struct netdev_notifier_changeupper_info *info);
745e358bef7SVladimir Oltean 
746e358bef7SVladimir Oltean 	/*
747b73adef6SFlorian Fainelli 	 * Bridge integration
748b73adef6SFlorian Fainelli 	 */
74934a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
75071327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
751a6692754SVivien Didelot 				    struct net_device *bridge);
752f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
753f123f2fbSVivien Didelot 				     struct net_device *bridge);
754123abc06SVladimir Oltean 	/* Called right after .port_bridge_join() */
755123abc06SVladimir Oltean 	int	(*port_bridge_tx_fwd_offload)(struct dsa_switch *ds, int port,
756123abc06SVladimir Oltean 					      struct net_device *bridge,
7573f9bb030SVladimir Oltean 					      unsigned int bridge_num);
758123abc06SVladimir Oltean 	/* Called right before .port_bridge_leave() */
759123abc06SVladimir Oltean 	void	(*port_bridge_tx_fwd_unoffload)(struct dsa_switch *ds, int port,
760123abc06SVladimir Oltean 						struct net_device *bridge,
7613f9bb030SVladimir Oltean 						unsigned int bridge_num);
76243c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
763b73adef6SFlorian Fainelli 				      u8 state);
764732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
765a8b659e7SVladimir Oltean 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
766a8b659e7SVladimir Oltean 					 struct switchdev_brport_flags flags,
767a8b659e7SVladimir Oltean 					 struct netlink_ext_ack *extack);
768a8b659e7SVladimir Oltean 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
769a8b659e7SVladimir Oltean 				     struct switchdev_brport_flags flags,
770a8b659e7SVladimir Oltean 				     struct netlink_ext_ack *extack);
7712a778e1bSVivien Didelot 
7722a778e1bSVivien Didelot 	/*
77311149536SVivien Didelot 	 * VLAN support
77411149536SVivien Didelot 	 */
775fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
77689153ed6SVladimir Oltean 				       bool vlan_filtering,
77789153ed6SVladimir Oltean 				       struct netlink_ext_ack *extack);
7781958d581SVladimir Oltean 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
77931046a5fSVladimir Oltean 				 const struct switchdev_obj_port_vlan *vlan,
78031046a5fSVladimir Oltean 				 struct netlink_ext_ack *extack);
78176e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
78276e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
78311149536SVivien Didelot 	/*
7842a778e1bSVivien Didelot 	 * Forwarding database
7852a778e1bSVivien Didelot 	 */
7861b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
7876c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
7882a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
7896c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
790ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
7912bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
7928df30255SVivien Didelot 
7938df30255SVivien Didelot 	/*
7948df30255SVivien Didelot 	 * Multicast database
7958df30255SVivien Didelot 	 */
796a52b2da7SVladimir Oltean 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
7973709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
7988df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
7998df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
800bf9f2648SFlorian Fainelli 	/*
801bf9f2648SFlorian Fainelli 	 * RXNFC
802bf9f2648SFlorian Fainelli 	 */
803bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
804bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
805bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
806bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
807f50f2127SFlorian Fainelli 
808f50f2127SFlorian Fainelli 	/*
809f50f2127SFlorian Fainelli 	 * TC integration
810f50f2127SFlorian Fainelli 	 */
811ed11bb1fSVladimir Oltean 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
812ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
813ed11bb1fSVladimir Oltean 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
814ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
815ed11bb1fSVladimir Oltean 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
816ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls, bool ingress);
817f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
818f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
819f50f2127SFlorian Fainelli 				   bool ingress);
820f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
821f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
82234297176SVladimir Oltean 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
82334297176SVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer);
82434297176SVladimir Oltean 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
82547d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
82647d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
82740ef2c93SVivien Didelot 
82840ef2c93SVivien Didelot 	/*
82940ef2c93SVivien Didelot 	 * Cross-chip operations
83040ef2c93SVivien Didelot 	 */
831f66a6a69SVladimir Oltean 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
832f66a6a69SVladimir Oltean 					 int sw_index, int port,
833f66a6a69SVladimir Oltean 					 struct net_device *br);
834f66a6a69SVladimir Oltean 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
835f66a6a69SVladimir Oltean 					  int sw_index, int port,
836f66a6a69SVladimir Oltean 					  struct net_device *br);
837058102a6STobias Waldekranz 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
838058102a6STobias Waldekranz 					int port);
839058102a6STobias Waldekranz 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
840058102a6STobias Waldekranz 				      int port, struct net_device *lag,
841058102a6STobias Waldekranz 				      struct netdev_lag_upper_info *info);
842058102a6STobias Waldekranz 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
843058102a6STobias Waldekranz 				       int port, struct net_device *lag);
8440336369dSBrandon Streiff 
8450336369dSBrandon Streiff 	/*
8460336369dSBrandon Streiff 	 * PTP functionality
8470336369dSBrandon Streiff 	 */
8480336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
8490336369dSBrandon Streiff 				     struct ifreq *ifr);
8500336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
8510336369dSBrandon Streiff 				     struct ifreq *ifr);
8525c5416f5SYangbo Lu 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
8535c5416f5SYangbo Lu 				 struct sk_buff *skb);
85490af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
85590af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
85697a69a0dSVladimir Oltean 
8570f06b855SAndrew Lunn 	/* Devlink parameters, etc */
8586b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
8596b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
8606b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
8616b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
8620f06b855SAndrew Lunn 	int	(*devlink_info_get)(struct dsa_switch *ds,
8630f06b855SAndrew Lunn 				    struct devlink_info_req *req,
8640f06b855SAndrew Lunn 				    struct netlink_ext_ack *extack);
8652a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
8662a6ef763SVladimir Oltean 				       unsigned int sb_index, u16 pool_index,
8672a6ef763SVladimir Oltean 				       struct devlink_sb_pool_info *pool_info);
8682a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
8692a6ef763SVladimir Oltean 				       u16 pool_index, u32 size,
8702a6ef763SVladimir Oltean 				       enum devlink_sb_threshold_type threshold_type,
8712a6ef763SVladimir Oltean 				       struct netlink_ext_ack *extack);
8722a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
8732a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
8742a6ef763SVladimir Oltean 					    u32 *p_threshold);
8752a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
8762a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
8772a6ef763SVladimir Oltean 					    u32 threshold,
8782a6ef763SVladimir Oltean 					    struct netlink_ext_ack *extack);
8792a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
8802a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
8812a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
8822a6ef763SVladimir Oltean 					       u16 *p_pool_index, u32 *p_threshold);
8832a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
8842a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
8852a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
8862a6ef763SVladimir Oltean 					       u16 pool_index, u32 threshold,
8872a6ef763SVladimir Oltean 					       struct netlink_ext_ack *extack);
8882a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
8892a6ef763SVladimir Oltean 					   unsigned int sb_index);
8902a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
8912a6ef763SVladimir Oltean 					    unsigned int sb_index);
8922a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
8932a6ef763SVladimir Oltean 						unsigned int sb_index, u16 pool_index,
8942a6ef763SVladimir Oltean 						u32 *p_cur, u32 *p_max);
8952a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
8962a6ef763SVladimir Oltean 						   unsigned int sb_index, u16 tc_index,
8972a6ef763SVladimir Oltean 						   enum devlink_sb_pool_type pool_type,
8982a6ef763SVladimir Oltean 						   u32 *p_cur, u32 *p_max);
899bfcb8132SVladimir Oltean 
900bfcb8132SVladimir Oltean 	/*
901bfcb8132SVladimir Oltean 	 * MTU change functionality. Switches can also adjust their MRU through
902bfcb8132SVladimir Oltean 	 * this method. By MTU, one understands the SDU (L2 payload) length.
903bfcb8132SVladimir Oltean 	 * If the switch needs to account for the DSA tag on the CPU port, this
904ab88d64aSRandy Dunlap 	 * method needs to do so privately.
905bfcb8132SVladimir Oltean 	 */
906bfcb8132SVladimir Oltean 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
907bfcb8132SVladimir Oltean 				   int new_mtu);
908bfcb8132SVladimir Oltean 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
909058102a6STobias Waldekranz 
910058102a6STobias Waldekranz 	/*
911058102a6STobias Waldekranz 	 * LAG integration
912058102a6STobias Waldekranz 	 */
913058102a6STobias Waldekranz 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
914058102a6STobias Waldekranz 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
915058102a6STobias Waldekranz 				 struct net_device *lag,
916058102a6STobias Waldekranz 				 struct netdev_lag_upper_info *info);
917058102a6STobias Waldekranz 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
918058102a6STobias Waldekranz 				  struct net_device *lag);
91918596f50SGeorge McCollister 
92018596f50SGeorge McCollister 	/*
92118596f50SGeorge McCollister 	 * HSR integration
92218596f50SGeorge McCollister 	 */
92318596f50SGeorge McCollister 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
92418596f50SGeorge McCollister 				 struct net_device *hsr);
92518596f50SGeorge McCollister 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
92618596f50SGeorge McCollister 				  struct net_device *hsr);
927c595c433SHoratiu Vultur 
928c595c433SHoratiu Vultur 	/*
929c595c433SHoratiu Vultur 	 * MRP integration
930c595c433SHoratiu Vultur 	 */
931c595c433SHoratiu Vultur 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
932c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
933c595c433SHoratiu Vultur 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
934c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
935c595c433SHoratiu Vultur 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
936c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
937c595c433SHoratiu Vultur 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
938c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
9395da11eb4SVladimir Oltean 
9405da11eb4SVladimir Oltean 	/*
9415da11eb4SVladimir Oltean 	 * tag_8021q operations
9425da11eb4SVladimir Oltean 	 */
9435da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
9445da11eb4SVladimir Oltean 				      u16 flags);
9455da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
9466b297524SAndrew Lunn };
9476b297524SAndrew Lunn 
9486b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
9496b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
9506b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
9516b297524SAndrew Lunn 
9526b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
9536b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
9546b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
9556b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
9566b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
9576b297524SAndrew Lunn 				const struct devlink_param *params,
9586b297524SAndrew Lunn 				size_t params_count);
9596b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
9606b297524SAndrew Lunn 				   const struct devlink_param *params,
9616b297524SAndrew Lunn 				   size_t params_count);
9625cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
9635cd73fbdSAndrew Lunn 				  const char *resource_name,
9645cd73fbdSAndrew Lunn 				  u64 resource_size,
9655cd73fbdSAndrew Lunn 				  u64 resource_id,
9665cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
9675cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
9685cd73fbdSAndrew Lunn 
9695cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
9705cd73fbdSAndrew Lunn 
9715cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
9725cd73fbdSAndrew Lunn 					   u64 resource_id,
9735cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
9745cd73fbdSAndrew Lunn 					   void *occ_get_priv);
9755cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
9765cd73fbdSAndrew Lunn 					     u64 resource_id);
97797c82c23SAndrew Lunn struct devlink_region *
97897c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds,
97997c82c23SAndrew Lunn 			  const struct devlink_region_ops *ops,
98097c82c23SAndrew Lunn 			  u32 region_max_snapshots, u64 region_size);
98108156ba4SAndrew Lunn struct devlink_region *
98208156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds,
98308156ba4SAndrew Lunn 			       int port,
98408156ba4SAndrew Lunn 			       const struct devlink_port_region_ops *ops,
98508156ba4SAndrew Lunn 			       u32 region_max_snapshots, u64 region_size);
98697c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region);
98797c82c23SAndrew Lunn 
988e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
9895cd73fbdSAndrew Lunn 
9906b297524SAndrew Lunn struct dsa_devlink_priv {
9916b297524SAndrew Lunn 	struct dsa_switch *ds;
992c8f0b869SBen Hutchings };
993c8f0b869SBen Hutchings 
994ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
995ccc3e6b0SAndrew Lunn {
996ccc3e6b0SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
997ccc3e6b0SAndrew Lunn 
998ccc3e6b0SAndrew Lunn 	return dl_priv->ds;
999ccc3e6b0SAndrew Lunn }
1000ccc3e6b0SAndrew Lunn 
10017d1e2a10SAndrew Lunn static inline
10027d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
10037d1e2a10SAndrew Lunn {
10047d1e2a10SAndrew Lunn 	struct devlink *dl = port->devlink;
10057d1e2a10SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
10067d1e2a10SAndrew Lunn 
10077d1e2a10SAndrew Lunn 	return dl_priv->ds;
10087d1e2a10SAndrew Lunn }
10097d1e2a10SAndrew Lunn 
10107d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port)
10117d1e2a10SAndrew Lunn {
10127d1e2a10SAndrew Lunn 	return port->index;
10137d1e2a10SAndrew Lunn }
10147d1e2a10SAndrew Lunn 
1015ab3d408dSFlorian Fainelli struct dsa_switch_driver {
1016ab3d408dSFlorian Fainelli 	struct list_head	list;
1017a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
1018ab3d408dSFlorian Fainelli };
1019ab3d408dSFlorian Fainelli 
102014b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
1021c8f0b869SBen Hutchings 
102273a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
10239eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev)
1024c6e970a0SAndrew Lunn {
1025c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
1026717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
1027c6e970a0SAndrew Lunn #endif
1028c6e970a0SAndrew Lunn 	return false;
1029c6e970a0SAndrew Lunn }
1030c6e970a0SAndrew Lunn 
10319790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail
10329790cf20SVladimir Oltean  * tags, which don't break dissection) can be treated the same from the
10339790cf20SVladimir Oltean  * perspective of the flow dissector.
10349790cf20SVladimir Oltean  *
10359790cf20SVladimir Oltean  * We need to return:
10369790cf20SVladimir Oltean  *  - offset: the (B - A) difference between:
10379790cf20SVladimir Oltean  *    A. the position of the real EtherType and
10389790cf20SVladimir Oltean  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
10399790cf20SVladimir Oltean  *       after the normal EtherType was supposed to be)
10409790cf20SVladimir Oltean  *    The offset in bytes is exactly equal to the tagger overhead (and half of
10419790cf20SVladimir Oltean  *    that, in __be16 shorts).
10429790cf20SVladimir Oltean  *
10439790cf20SVladimir Oltean  *  - proto: the value of the real EtherType.
10449790cf20SVladimir Oltean  */
10459790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
10469790cf20SVladimir Oltean 						__be16 *proto, int *offset)
10479790cf20SVladimir Oltean {
10489790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
10499790cf20SVladimir Oltean 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
10504e500251SVladimir Oltean 	int tag_len = ops->needed_headroom;
10519790cf20SVladimir Oltean 
10529790cf20SVladimir Oltean 	*offset = tag_len;
10539790cf20SVladimir Oltean 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
10549790cf20SVladimir Oltean #endif
10559790cf20SVladimir Oltean }
10569790cf20SVladimir Oltean 
10574cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
10584cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev)
10594cfab356SFlorian Fainelli {
10604cfab356SFlorian Fainelli 	int err = -EOPNOTSUPP;
10614cfab356SFlorian Fainelli 
10624cfab356SFlorian Fainelli 	if (!dev->dsa_ptr)
10634cfab356SFlorian Fainelli 		return err;
10644cfab356SFlorian Fainelli 
10654cfab356SFlorian Fainelli 	if (!dev->dsa_ptr->netdev_ops)
10664cfab356SFlorian Fainelli 		return err;
10674cfab356SFlorian Fainelli 
10684cfab356SFlorian Fainelli 	return 0;
10694cfab356SFlorian Fainelli }
10704cfab356SFlorian Fainelli 
1071a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
10724cfab356SFlorian Fainelli 				    int cmd)
10734cfab356SFlorian Fainelli {
10744cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *ops;
10754cfab356SFlorian Fainelli 	int err;
10764cfab356SFlorian Fainelli 
10774cfab356SFlorian Fainelli 	err = __dsa_netdevice_ops_check(dev);
10784cfab356SFlorian Fainelli 	if (err)
10794cfab356SFlorian Fainelli 		return err;
10804cfab356SFlorian Fainelli 
10814cfab356SFlorian Fainelli 	ops = dev->dsa_ptr->netdev_ops;
10824cfab356SFlorian Fainelli 
1083a7605370SArnd Bergmann 	return ops->ndo_eth_ioctl(dev, ifr, cmd);
10844cfab356SFlorian Fainelli }
10854cfab356SFlorian Fainelli #else
1086a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
10874cfab356SFlorian Fainelli 				    int cmd)
10884cfab356SFlorian Fainelli {
10894cfab356SFlorian Fainelli 	return -EOPNOTSUPP;
10904cfab356SFlorian Fainelli }
10914cfab356SFlorian Fainelli #endif
10924cfab356SFlorian Fainelli 
109383c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
109423c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
10950650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds);
10963b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1097ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
1098ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
1099ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
1100ea825e70SFlorian Fainelli #else
1101ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
1102ea825e70SFlorian Fainelli {
1103ea825e70SFlorian Fainelli 	return 0;
1104ea825e70SFlorian Fainelli }
1105ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
1106ea825e70SFlorian Fainelli {
1107ea825e70SFlorian Fainelli 	return 0;
1108ea825e70SFlorian Fainelli }
1109ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
1110ea825e70SFlorian Fainelli 
111160724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
1112a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev);
111360724d4bSFlorian Fainelli #else
1114a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev)
1115a5e3c9baSVladimir Oltean {
1116a5e3c9baSVladimir Oltean 	return false;
1117a5e3c9baSVladimir Oltean }
111860724d4bSFlorian Fainelli #endif
111960724d4bSFlorian Fainelli 
112097a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1121cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1122cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1123cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
112411d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1125cf963573SFlorian Fainelli 
1126d3b8c049SAndrew Lunn struct dsa_tag_driver {
1127d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
1128d3b8c049SAndrew Lunn 	struct list_head list;
1129d3b8c049SAndrew Lunn 	struct module *owner;
1130d3b8c049SAndrew Lunn };
1131d3b8c049SAndrew Lunn 
1132d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1133d3b8c049SAndrew Lunn 			      unsigned int count,
1134d3b8c049SAndrew Lunn 			      struct module *owner);
1135d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1136d3b8c049SAndrew Lunn 				unsigned int count);
1137d3b8c049SAndrew Lunn 
1138d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
1139d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
1140d3b8c049SAndrew Lunn {									\
1141d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
1142d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
1143d3b8c049SAndrew Lunn 	return 0;							\
1144d3b8c049SAndrew Lunn }									\
1145d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
1146d3b8c049SAndrew Lunn 									\
1147d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
1148d3b8c049SAndrew Lunn {									\
1149d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
1150d3b8c049SAndrew Lunn }									\
1151d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
1152d3b8c049SAndrew Lunn 
1153d3b8c049SAndrew Lunn /**
1154d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1155d3b8c049SAndrew Lunn  * drivers
1156d3b8c049SAndrew Lunn  * @__ops_array: Array of tag driver strucutres
1157d3b8c049SAndrew Lunn  *
1158d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
1159d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
1160d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
1161d3b8c049SAndrew Lunn  */
1162d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
1163d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1164d3b8c049SAndrew Lunn 
1165d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1166d3b8c049SAndrew Lunn 
1167d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
1168d3b8c049SAndrew Lunn  * drivers
1169d3b8c049SAndrew Lunn  */
1170d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
1171d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1172d3b8c049SAndrew Lunn 	.ops = &__ops,							\
1173d3b8c049SAndrew Lunn }
1174d3b8c049SAndrew Lunn 
1175d3b8c049SAndrew Lunn /**
1176d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1177d3b8c049SAndrew Lunn  * driver
1178d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
1179d3b8c049SAndrew Lunn  *
1180d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
1181d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
1182d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
1183d3b8c049SAndrew Lunn  */
1184d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
1185d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
1186d3b8c049SAndrew Lunn 									\
1187d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1188d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
1189d3b8c049SAndrew Lunn };									\
1190d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
119191da11f8SLennert Buytenhek #endif
1192d3b8c049SAndrew Lunn 
1193