xref: /linux/include/net/dsa.h (revision 857fdd74fb38dad4d0333401fb1826cd8cf0df2c)
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 
222d3eed0e5SVladimir Oltean struct dsa_bridge {
223d3eed0e5SVladimir Oltean 	struct net_device *dev;
224d3eed0e5SVladimir Oltean 	unsigned int num;
225*857fdd74SVladimir Oltean 	bool tx_fwd_offload;
226d3eed0e5SVladimir Oltean 	refcount_t refcount;
227d3eed0e5SVladimir Oltean };
228f50f2127SFlorian Fainelli 
229c8b09808SAndrew Lunn struct dsa_port {
230f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
231f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
232f8b8b1cdSVivien Didelot 	 */
233f8b8b1cdSVivien Didelot 	union {
234f8b8b1cdSVivien Didelot 		struct net_device *master;
235f8b8b1cdSVivien Didelot 		struct net_device *slave;
236f8b8b1cdSVivien Didelot 	};
237f8b8b1cdSVivien Didelot 
238357f203bSVladimir Oltean 	/* Copy of the tagging protocol operations, for quicker access
239357f203bSVladimir Oltean 	 * in the data path. Valid only for the CPU ports.
240357f203bSVladimir Oltean 	 */
24115240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
24215240248SVivien Didelot 
2433e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
2443e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
24529a097b7SVladimir Oltean 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
2463e41f93bSVivien Didelot 
247057cad2cSVivien Didelot 	enum {
248057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
249057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
250057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
251057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
252057cad2cSVivien Didelot 	} type;
253057cad2cSVivien Didelot 
254818be848SVivien Didelot 	struct dsa_switch	*ds;
255818be848SVivien Didelot 	unsigned int		index;
25671e0bbdeSFlorian Fainelli 	const char		*name;
25768b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
25883216e39SMichael Walle 	u8			mac[ETH_ALEN];
259189b0d93SAndrew Lunn 	struct device_node	*dn;
26034a79f63SVivien Didelot 	unsigned int		ageing_time;
26133162e9aSVladimir Oltean 	bool			vlan_filtering;
2625313a37bSVladimir Oltean 	/* Managed by DSA on user ports and by drivers on CPU and DSA ports */
263045c45d1SVladimir Oltean 	bool			learning;
264732f794cSVivien Didelot 	u8			stp_state;
265d3eed0e5SVladimir Oltean 	struct dsa_bridge	*bridge;
26696567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
2673122433eSAndrew Lunn 	bool			devlink_port_setup;
268aab9c406SFlorian Fainelli 	struct phylink		*pl;
26944cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
270058102a6STobias Waldekranz 	struct net_device	*lag_dev;
271058102a6STobias Waldekranz 	bool			lag_tx_enabled;
27218596f50SGeorge McCollister 	struct net_device	*hsr_dev;
27397a69a0dSVladimir Oltean 
274ab8ccae1SVivien Didelot 	struct list_head list;
275ab8ccae1SVivien Didelot 
27667dbb9d4SFlorian Fainelli 	/*
277c362beb0SVladimir Oltean 	 * Give the switch driver somewhere to hang its per-port private data
278c362beb0SVladimir Oltean 	 * structures (accessible from the tagger).
279c362beb0SVladimir Oltean 	 */
280c362beb0SVladimir Oltean 	void *priv;
281c362beb0SVladimir Oltean 
282c362beb0SVladimir Oltean 	/*
28367dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
28467dbb9d4SFlorian Fainelli 	 */
28567dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
286da7b9e9bSFlorian Fainelli 
287da7b9e9bSFlorian Fainelli 	/*
288da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
289da7b9e9bSFlorian Fainelli 	 */
2904cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *netdev_ops;
291fb35c60cSVivien Didelot 
292161ca59dSVladimir Oltean 	/* List of MAC addresses that must be forwarded on this port.
293161ca59dSVladimir Oltean 	 * These are only valid on CPU ports and DSA links.
294161ca59dSVladimir Oltean 	 */
295338a3a47SVladimir Oltean 	struct mutex		addr_lists_lock;
2963f6e32f9SVladimir Oltean 	struct list_head	fdbs;
297161ca59dSVladimir Oltean 	struct list_head	mdbs;
298161ca59dSVladimir Oltean 
299fb35c60cSVivien Didelot 	bool setup;
300c8b09808SAndrew Lunn };
301c8b09808SAndrew Lunn 
302c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
303c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
304c5f51765SVivien Didelot  * but this would require some more complex tree walking,
305c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
306c5f51765SVivien Didelot  */
307c5f51765SVivien Didelot struct dsa_link {
308c5f51765SVivien Didelot 	struct dsa_port *dp;
309c5f51765SVivien Didelot 	struct dsa_port *link_dp;
310c5f51765SVivien Didelot 	struct list_head list;
311c5f51765SVivien Didelot };
312c5f51765SVivien Didelot 
313161ca59dSVladimir Oltean struct dsa_mac_addr {
314161ca59dSVladimir Oltean 	unsigned char addr[ETH_ALEN];
315161ca59dSVladimir Oltean 	u16 vid;
316161ca59dSVladimir Oltean 	refcount_t refcount;
317161ca59dSVladimir Oltean 	struct list_head list;
318161ca59dSVladimir Oltean };
319161ca59dSVladimir Oltean 
320c8f0b869SBen Hutchings struct dsa_switch {
321fb35c60cSVivien Didelot 	bool setup;
322fb35c60cSVivien Didelot 
323c33063d6SAndrew Lunn 	struct device *dev;
324c33063d6SAndrew Lunn 
325c8f0b869SBen Hutchings 	/*
326c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
327c8f0b869SBen Hutchings 	 */
328c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
32999feaafcSVivien Didelot 	unsigned int		index;
330c8f0b869SBen Hutchings 
331f515f192SVivien Didelot 	/* Listener for switch fabric events */
332f515f192SVivien Didelot 	struct notifier_block	nb;
333f515f192SVivien Didelot 
334c8f0b869SBen Hutchings 	/*
3357543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
3367543a6d5SAndrew Lunn 	 * structure.
3377543a6d5SAndrew Lunn 	 */
3387543a6d5SAndrew Lunn 	void *priv;
3397543a6d5SAndrew Lunn 
3407543a6d5SAndrew Lunn 	/*
341c8f0b869SBen Hutchings 	 * Configuration data for this switch.
342c8f0b869SBen Hutchings 	 */
343ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
344c8f0b869SBen Hutchings 
345c8f0b869SBen Hutchings 	/*
3469d490b4eSVivien Didelot 	 * The switch operations.
347c8f0b869SBen Hutchings 	 */
348a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
349c8f0b869SBen Hutchings 
35066472fc0SAndrew Lunn 	/*
351c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
352c8f0b869SBen Hutchings 	 */
3530d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
354c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
355a0c02161SVivien Didelot 
3560f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
3570f3da6afSVivien Didelot 	unsigned int ageing_time_min;
3580f3da6afSVivien Didelot 	unsigned int ageing_time_max;
3590f3da6afSVivien Didelot 
360d7b1fd52SVladimir Oltean 	/* Storage for drivers using tag_8021q */
361d7b1fd52SVladimir Oltean 	struct dsa_8021q_context *tag_8021q_ctx;
362d7b1fd52SVladimir Oltean 
36396567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
36496567d5dSAndrew Lunn 	struct devlink		*devlink;
36596567d5dSAndrew Lunn 
36655199df6SFlorian Fainelli 	/* Number of switch port queues */
36755199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
36855199df6SFlorian Fainelli 
3698f5d16f6SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
3708f5d16f6SVladimir Oltean 	 * settings on ports if not hardware-supported
3718f5d16f6SVladimir Oltean 	 */
3728f5d16f6SVladimir Oltean 	bool			vlan_filtering_is_global;
3738f5d16f6SVladimir Oltean 
37458adf9dcSVladimir Oltean 	/* Keep VLAN filtering enabled on ports not offloading any upper. */
37558adf9dcSVladimir Oltean 	bool			needs_standalone_vlan_filtering;
37658adf9dcSVladimir Oltean 
37754a0ed0dSRussell King 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
37854a0ed0dSRussell King 	 * that have vlan_filtering=0. All drivers should ideally set this (and
37954a0ed0dSRussell King 	 * then the option would get removed), but it is unknown whether this
38054a0ed0dSRussell King 	 * would break things or not.
38154a0ed0dSRussell King 	 */
38254a0ed0dSRussell King 	bool			configure_vlan_while_not_filtering;
38354a0ed0dSRussell King 
3841dc0408cSFlorian Fainelli 	/* If the switch driver always programs the CPU port as egress tagged
3851dc0408cSFlorian Fainelli 	 * despite the VLAN configuration indicating otherwise, then setting
3861dc0408cSFlorian Fainelli 	 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
3871dc0408cSFlorian Fainelli 	 * default_pvid VLAN tagged frames to offer a consistent behavior
3881dc0408cSFlorian Fainelli 	 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
3891dc0408cSFlorian Fainelli 	 */
3901dc0408cSFlorian Fainelli 	bool			untag_bridge_pvid;
3911dc0408cSFlorian Fainelli 
392d5f19486SVladimir Oltean 	/* Let DSA manage the FDB entries towards the CPU, based on the
393d5f19486SVladimir Oltean 	 * software bridge database.
394d5f19486SVladimir Oltean 	 */
395d5f19486SVladimir Oltean 	bool			assisted_learning_on_cpu_port;
396d5f19486SVladimir Oltean 
39714574676SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
39814574676SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
39914574676SVladimir Oltean 	 */
40014574676SVladimir Oltean 	bool			vlan_filtering;
40114574676SVladimir Oltean 
402787cac3fSVladimir Oltean 	/* MAC PCS does not provide link state change interrupt, and requires
403787cac3fSVladimir Oltean 	 * polling. Flag passed on to PHYLINK.
404787cac3fSVladimir Oltean 	 */
405787cac3fSVladimir Oltean 	bool			pcs_poll;
406787cac3fSVladimir Oltean 
407bff33f7eSVladimir Oltean 	/* For switches that only have the MRU configurable. To ensure the
408bff33f7eSVladimir Oltean 	 * configured MTU is not exceeded, normalization of MRU on all bridged
409bff33f7eSVladimir Oltean 	 * interfaces is needed.
410bff33f7eSVladimir Oltean 	 */
411bff33f7eSVladimir Oltean 	bool			mtu_enforcement_ingress;
412bff33f7eSVladimir Oltean 
413058102a6STobias Waldekranz 	/* Drivers that benefit from having an ID associated with each
414058102a6STobias Waldekranz 	 * offloaded LAG should set this to the maximum number of
415058102a6STobias Waldekranz 	 * supported IDs. DSA will then maintain a mapping of _at
416058102a6STobias Waldekranz 	 * least_ these many IDs, accessible to drivers via
417058102a6STobias Waldekranz 	 * dsa_lag_id().
418058102a6STobias Waldekranz 	 */
419058102a6STobias Waldekranz 	unsigned int		num_lag_ids;
420058102a6STobias Waldekranz 
421947c8746SVladimir Oltean 	/* Drivers that support bridge forwarding offload or FDB isolation
422947c8746SVladimir Oltean 	 * should set this to the maximum number of bridges spanning the same
423947c8746SVladimir Oltean 	 * switch tree (or all trees, in the case of cross-tree bridging
424947c8746SVladimir Oltean 	 * support) that can be offloaded.
425123abc06SVladimir Oltean 	 */
426947c8746SVladimir Oltean 	unsigned int		max_num_bridges;
427123abc06SVladimir Oltean 
428a0c02161SVivien Didelot 	size_t num_ports;
429c8f0b869SBen Hutchings };
430c8f0b869SBen Hutchings 
43168bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
432c38c5a66SVivien Didelot {
433b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
434d607525bSVivien Didelot 	struct dsa_port *dp;
435b96ddf25SVivien Didelot 
436b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
437b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
438b96ddf25SVivien Didelot 			return dp;
439d607525bSVivien Didelot 
440d607525bSVivien Didelot 	return NULL;
441c38c5a66SVivien Didelot }
442c38c5a66SVivien Didelot 
443a8986681SVladimir Oltean static inline bool dsa_port_is_dsa(struct dsa_port *port)
444a8986681SVladimir Oltean {
445a8986681SVladimir Oltean 	return port->type == DSA_PORT_TYPE_DSA;
446a8986681SVladimir Oltean }
447a8986681SVladimir Oltean 
448a8986681SVladimir Oltean static inline bool dsa_port_is_cpu(struct dsa_port *port)
449a8986681SVladimir Oltean {
450a8986681SVladimir Oltean 	return port->type == DSA_PORT_TYPE_CPU;
451a8986681SVladimir Oltean }
452a8986681SVladimir Oltean 
453a8986681SVladimir Oltean static inline bool dsa_port_is_user(struct dsa_port *dp)
454a8986681SVladimir Oltean {
455a8986681SVladimir Oltean 	return dp->type == DSA_PORT_TYPE_USER;
456a8986681SVladimir Oltean }
457a8986681SVladimir Oltean 
458a57d8c21SVladimir Oltean static inline bool dsa_port_is_unused(struct dsa_port *dp)
459a57d8c21SVladimir Oltean {
460a57d8c21SVladimir Oltean 	return dp->type == DSA_PORT_TYPE_UNUSED;
461a57d8c21SVladimir Oltean }
462a57d8c21SVladimir Oltean 
463bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
464bff7b688SVivien Didelot {
465c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
466bff7b688SVivien Didelot }
467bff7b688SVivien Didelot 
468c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
469c8f0b869SBen Hutchings {
470c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
471c8f0b869SBen Hutchings }
472c8f0b869SBen Hutchings 
47360045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
47460045cbfSAndrew Lunn {
475c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
47660045cbfSAndrew Lunn }
47760045cbfSAndrew Lunn 
4782b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
4796cd456f3SVivien Didelot {
480c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
4816cd456f3SVivien Didelot }
4826cd456f3SVivien Didelot 
48382b31898SVladimir Oltean #define dsa_tree_for_each_user_port(_dp, _dst) \
48482b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_dst)->ports, list) \
48582b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
48682b31898SVladimir Oltean 
48782b31898SVladimir Oltean #define dsa_switch_for_each_port(_dp, _ds) \
48882b31898SVladimir Oltean 	list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
48982b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
49082b31898SVladimir Oltean 
49182b31898SVladimir Oltean #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
49282b31898SVladimir Oltean 	list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
49382b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
49482b31898SVladimir Oltean 
49582b31898SVladimir Oltean #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
49682b31898SVladimir Oltean 	list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
49782b31898SVladimir Oltean 		if ((_dp)->ds == (_ds))
49882b31898SVladimir Oltean 
49982b31898SVladimir Oltean #define dsa_switch_for_each_available_port(_dp, _ds) \
50082b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
50182b31898SVladimir Oltean 		if (!dsa_port_is_unused((_dp)))
50282b31898SVladimir Oltean 
50382b31898SVladimir Oltean #define dsa_switch_for_each_user_port(_dp, _ds) \
50482b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
50582b31898SVladimir Oltean 		if (dsa_port_is_user((_dp)))
50682b31898SVladimir Oltean 
50782b31898SVladimir Oltean #define dsa_switch_for_each_cpu_port(_dp, _ds) \
50882b31898SVladimir Oltean 	dsa_switch_for_each_port((_dp), (_ds)) \
50982b31898SVladimir Oltean 		if (dsa_port_is_cpu((_dp)))
51082b31898SVladimir Oltean 
51102bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
51202bc6e54SVivien Didelot {
513d0004a02SVladimir Oltean 	struct dsa_port *dp;
514c38c5a66SVivien Didelot 	u32 mask = 0;
51502bc6e54SVivien Didelot 
516d0004a02SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
517d0004a02SVladimir Oltean 		mask |= BIT(dp->index);
518c38c5a66SVivien Didelot 
519c38c5a66SVivien Didelot 	return mask;
520c8652c83SVivien Didelot }
521c8652c83SVivien Didelot 
522c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
523c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
524c5f51765SVivien Didelot {
525c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
526c5f51765SVivien Didelot 	struct dsa_link *dl;
527c5f51765SVivien Didelot 
528c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
529c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
530c5f51765SVivien Didelot 			return dl->dp->index;
531c5f51765SVivien Didelot 
532c5f51765SVivien Didelot 	return ds->num_ports;
533c5f51765SVivien Didelot }
534c5f51765SVivien Didelot 
5353b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
5363b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
5373b8fac5dSVivien Didelot 					    int port)
5383b8fac5dSVivien Didelot {
5393b8fac5dSVivien Didelot 	if (device == ds->index)
5403b8fac5dSVivien Didelot 		return port;
5413b8fac5dSVivien Didelot 	else
542c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
5433b8fac5dSVivien Didelot }
5443b8fac5dSVivien Didelot 
5453b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
54607073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
547c8f0b869SBen Hutchings {
54807073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
54907073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
55007073c79SVivien Didelot 
55107073c79SVivien Didelot 	if (!cpu_dp)
55207073c79SVivien Didelot 		return port;
553c8f0b869SBen Hutchings 
5543b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
555c8f0b869SBen Hutchings }
556c8f0b869SBen Hutchings 
55763609c8fSVladimir Oltean /* Return true if this is the local port used to reach the CPU port */
55863609c8fSVladimir Oltean static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
55963609c8fSVladimir Oltean {
56063609c8fSVladimir Oltean 	if (dsa_is_unused_port(ds, port))
56163609c8fSVladimir Oltean 		return false;
56263609c8fSVladimir Oltean 
56363609c8fSVladimir Oltean 	return port == dsa_upstream_port(ds, port);
56463609c8fSVladimir Oltean }
56563609c8fSVladimir Oltean 
56663609c8fSVladimir Oltean /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
56763609c8fSVladimir Oltean  * that the routing port from @downstream_ds to @upstream_ds is also the port
56863609c8fSVladimir Oltean  * which @downstream_ds uses to reach its dedicated CPU.
56963609c8fSVladimir Oltean  */
57063609c8fSVladimir Oltean static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
57163609c8fSVladimir Oltean 					     struct dsa_switch *downstream_ds)
57263609c8fSVladimir Oltean {
57363609c8fSVladimir Oltean 	int routing_port;
57463609c8fSVladimir Oltean 
57563609c8fSVladimir Oltean 	if (upstream_ds == downstream_ds)
57663609c8fSVladimir Oltean 		return true;
57763609c8fSVladimir Oltean 
57863609c8fSVladimir Oltean 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
57963609c8fSVladimir Oltean 
58063609c8fSVladimir Oltean 	return dsa_is_upstream_port(downstream_ds, routing_port);
58163609c8fSVladimir Oltean }
58263609c8fSVladimir Oltean 
583cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
584cf2d45f5SVladimir Oltean {
585cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
586cf2d45f5SVladimir Oltean 
587cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
588cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
589cf2d45f5SVladimir Oltean 	else
590cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
591cf2d45f5SVladimir Oltean }
592cf2d45f5SVladimir Oltean 
593cc76ce9eSTobias Waldekranz static inline
594cc76ce9eSTobias Waldekranz struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
595cc76ce9eSTobias Waldekranz {
596d3eed0e5SVladimir Oltean 	if (!dp->bridge)
597cc76ce9eSTobias Waldekranz 		return NULL;
598cc76ce9eSTobias Waldekranz 
599cc76ce9eSTobias Waldekranz 	if (dp->lag_dev)
600cc76ce9eSTobias Waldekranz 		return dp->lag_dev;
601cc76ce9eSTobias Waldekranz 	else if (dp->hsr_dev)
602cc76ce9eSTobias Waldekranz 		return dp->hsr_dev;
603cc76ce9eSTobias Waldekranz 
604cc76ce9eSTobias Waldekranz 	return dp->slave;
605cc76ce9eSTobias Waldekranz }
606cc76ce9eSTobias Waldekranz 
60736cbf39bSVladimir Oltean static inline struct net_device *
60836cbf39bSVladimir Oltean dsa_port_bridge_dev_get(const struct dsa_port *dp)
60936cbf39bSVladimir Oltean {
610d3eed0e5SVladimir Oltean 	return dp->bridge ? dp->bridge->dev : NULL;
61136cbf39bSVladimir Oltean }
61236cbf39bSVladimir Oltean 
61336cbf39bSVladimir Oltean static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
61436cbf39bSVladimir Oltean {
615d3eed0e5SVladimir Oltean 	return dp->bridge ? dp->bridge->num : 0;
61636cbf39bSVladimir Oltean }
61736cbf39bSVladimir Oltean 
61836cbf39bSVladimir Oltean static inline bool dsa_port_bridge_same(const struct dsa_port *a,
61936cbf39bSVladimir Oltean 					const struct dsa_port *b)
62036cbf39bSVladimir Oltean {
62136cbf39bSVladimir Oltean 	struct net_device *br_a = dsa_port_bridge_dev_get(a);
62236cbf39bSVladimir Oltean 	struct net_device *br_b = dsa_port_bridge_dev_get(b);
62336cbf39bSVladimir Oltean 
62436cbf39bSVladimir Oltean 	/* Standalone ports are not in the same bridge with one another */
62536cbf39bSVladimir Oltean 	return (!br_a || !br_b) ? false : (br_a == br_b);
62636cbf39bSVladimir Oltean }
62736cbf39bSVladimir Oltean 
6286a43cba3SVladimir Oltean static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
6296a43cba3SVladimir Oltean 						 const struct net_device *dev)
6306a43cba3SVladimir Oltean {
6316a43cba3SVladimir Oltean 	return dsa_port_to_bridge_port(dp) == dev;
6326a43cba3SVladimir Oltean }
6336a43cba3SVladimir Oltean 
6346a43cba3SVladimir Oltean static inline bool
6356a43cba3SVladimir Oltean dsa_port_offloads_bridge_dev(struct dsa_port *dp,
6366a43cba3SVladimir Oltean 			     const struct net_device *bridge_dev)
6376a43cba3SVladimir Oltean {
6386a43cba3SVladimir Oltean 	/* DSA ports connected to a bridge, and event was emitted
6396a43cba3SVladimir Oltean 	 * for the bridge.
6406a43cba3SVladimir Oltean 	 */
6416a43cba3SVladimir Oltean 	return dsa_port_bridge_dev_get(dp) == bridge_dev;
6426a43cba3SVladimir Oltean }
6436a43cba3SVladimir Oltean 
644d3eed0e5SVladimir Oltean static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
645d3eed0e5SVladimir Oltean 					    const struct dsa_bridge *bridge)
646d3eed0e5SVladimir Oltean {
647d3eed0e5SVladimir Oltean 	return dsa_port_bridge_dev_get(dp) == bridge->dev;
648d3eed0e5SVladimir Oltean }
649d3eed0e5SVladimir Oltean 
6506a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given net_device */
6516a43cba3SVladimir Oltean static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
6526a43cba3SVladimir Oltean 						 const struct net_device *dev)
6536a43cba3SVladimir Oltean {
6546a43cba3SVladimir Oltean 	struct dsa_port *dp;
6556a43cba3SVladimir Oltean 
6566a43cba3SVladimir Oltean 	list_for_each_entry(dp, &dst->ports, list)
6576a43cba3SVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, dev))
6586a43cba3SVladimir Oltean 			return true;
6596a43cba3SVladimir Oltean 
6606a43cba3SVladimir Oltean 	return false;
6616a43cba3SVladimir Oltean }
6626a43cba3SVladimir Oltean 
6636a43cba3SVladimir Oltean /* Returns true if any port of this tree offloads the given bridge */
6646a43cba3SVladimir Oltean static inline bool
6656a43cba3SVladimir Oltean dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
6666a43cba3SVladimir Oltean 			     const struct net_device *bridge_dev)
6676a43cba3SVladimir Oltean {
6686a43cba3SVladimir Oltean 	struct dsa_port *dp;
6696a43cba3SVladimir Oltean 
6706a43cba3SVladimir Oltean 	list_for_each_entry(dp, &dst->ports, list)
6716a43cba3SVladimir Oltean 		if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
6726a43cba3SVladimir Oltean 			return true;
6736a43cba3SVladimir Oltean 
6746a43cba3SVladimir Oltean 	return false;
6756a43cba3SVladimir Oltean }
6766a43cba3SVladimir Oltean 
6772bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
6782bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
6799d490b4eSVivien Didelot struct dsa_switch_ops {
68053da0ebaSVladimir Oltean 	/*
68153da0ebaSVladimir Oltean 	 * Tagging protocol helpers called for the CPU ports and DSA links.
68253da0ebaSVladimir Oltean 	 * @get_tag_protocol retrieves the initial tagging protocol and is
68353da0ebaSVladimir Oltean 	 * mandatory. Switches which can operate using multiple tagging
68453da0ebaSVladimir Oltean 	 * protocols should implement @change_tag_protocol and report in
68553da0ebaSVladimir Oltean 	 * @get_tag_protocol the tagger in current use.
68653da0ebaSVladimir Oltean 	 */
6875ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
6884d776482SFlorian Fainelli 						  int port,
6894d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
69053da0ebaSVladimir Oltean 	int	(*change_tag_protocol)(struct dsa_switch *ds, int port,
69153da0ebaSVladimir Oltean 				       enum dsa_tag_protocol proto);
6927b314362SAndrew Lunn 
693fd292c18SVladimir Oltean 	/* Optional switch-wide initialization and destruction methods */
694c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
6955e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
696fd292c18SVladimir Oltean 
697fd292c18SVladimir Oltean 	/* Per-port initialization and destruction methods. Mandatory if the
698fd292c18SVladimir Oltean 	 * driver registers devlink port regions, optional otherwise.
699fd292c18SVladimir Oltean 	 */
700fd292c18SVladimir Oltean 	int	(*port_setup)(struct dsa_switch *ds, int port);
701fd292c18SVladimir Oltean 	void	(*port_teardown)(struct dsa_switch *ds, int port);
702fd292c18SVladimir Oltean 
7036819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
704c8f0b869SBen Hutchings 
705c8f0b869SBen Hutchings 	/*
706c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
707c8f0b869SBen Hutchings 	 */
708c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
709c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
710c8f0b869SBen Hutchings 			     int regnum, u16 val);
711c8f0b869SBen Hutchings 
712c8f0b869SBen Hutchings 	/*
713ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
714ec9436baSFlorian Fainelli 	 */
715ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
716ec9436baSFlorian Fainelli 				struct phy_device *phydev);
717ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
718ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
719ec9436baSFlorian Fainelli 
720ec9436baSFlorian Fainelli 	/*
72111d8f3ddSFlorian Fainelli 	 * PHYLINK integration
72211d8f3ddSFlorian Fainelli 	 */
723072eea6cSRussell King (Oracle) 	void	(*phylink_get_caps)(struct dsa_switch *ds, int port,
724072eea6cSRussell King (Oracle) 				    struct phylink_config *config);
72511d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
72611d8f3ddSFlorian Fainelli 				    unsigned long *supported,
72711d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
72811d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
72911d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
73011d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
73111d8f3ddSFlorian Fainelli 				      unsigned int mode,
73211d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
73311d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
73411d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
73511d8f3ddSFlorian Fainelli 					 unsigned int mode,
73611d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
73711d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
73811d8f3ddSFlorian Fainelli 				       unsigned int mode,
73911d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
7405b502a7bSRussell King 				       struct phy_device *phydev,
7415b502a7bSRussell King 				       int speed, int duplex,
7425b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
74311d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
74411d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
74511d8f3ddSFlorian Fainelli 	/*
746c2ec5f2eSOleksij Rempel 	 * Port statistics counters.
747c8f0b869SBen Hutchings 	 */
74889f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
74989f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
750c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
751c8f0b869SBen Hutchings 				     int port, uint64_t *data);
75289f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
753cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
754cf963573SFlorian Fainelli 					 int port, uint64_t *data);
755487d3855SAlvin Šipraga 	void	(*get_eth_phy_stats)(struct dsa_switch *ds, int port,
756487d3855SAlvin Šipraga 				     struct ethtool_eth_phy_stats *phy_stats);
757487d3855SAlvin Šipraga 	void	(*get_eth_mac_stats)(struct dsa_switch *ds, int port,
758487d3855SAlvin Šipraga 				     struct ethtool_eth_mac_stats *mac_stats);
759487d3855SAlvin Šipraga 	void	(*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
760487d3855SAlvin Šipraga 				      struct ethtool_eth_ctrl_stats *ctrl_stats);
761c2ec5f2eSOleksij Rempel 	void	(*get_stats64)(struct dsa_switch *ds, int port,
762c2ec5f2eSOleksij Rempel 				   struct rtnl_link_stats64 *s);
763a71acad9SOleksij Rempel 	void	(*self_test)(struct dsa_switch *ds, int port,
764a71acad9SOleksij Rempel 			     struct ethtool_test *etest, u64 *data);
76524462549SFlorian Fainelli 
76624462549SFlorian Fainelli 	/*
76719e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
76819e57c4eSFlorian Fainelli 	 */
76919e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
77019e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
77119e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
77219e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
77319e57c4eSFlorian Fainelli 
77419e57c4eSFlorian Fainelli 	/*
7750336369dSBrandon Streiff 	 * ethtool timestamp info
7760336369dSBrandon Streiff 	 */
7770336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
7780336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
7790336369dSBrandon Streiff 
7800336369dSBrandon Streiff 	/*
78124462549SFlorian Fainelli 	 * Suspend and resume
78224462549SFlorian Fainelli 	 */
78324462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
78424462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
785b2f2af21SFlorian Fainelli 
786b2f2af21SFlorian Fainelli 	/*
787b2f2af21SFlorian Fainelli 	 * Port enable/disable
788b2f2af21SFlorian Fainelli 	 */
789b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
790b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
79175104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
7927905288fSFlorian Fainelli 
7937905288fSFlorian Fainelli 	/*
79408f50061SVivien Didelot 	 * Port's MAC EEE settings
7957905288fSFlorian Fainelli 	 */
79608f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
7977905288fSFlorian Fainelli 			       struct ethtool_eee *e);
79808f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
7997905288fSFlorian Fainelli 			       struct ethtool_eee *e);
80051579c3fSGuenter Roeck 
8016793abb4SGuenter Roeck 	/* EEPROM access */
8026793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
8036793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
8046793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
8056793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
8066793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
8073d762a0fSGuenter Roeck 
8083d762a0fSGuenter Roeck 	/*
8093d762a0fSGuenter Roeck 	 * Register access.
8103d762a0fSGuenter Roeck 	 */
8113d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
8123d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
8133d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
814b73adef6SFlorian Fainelli 
815b73adef6SFlorian Fainelli 	/*
816e358bef7SVladimir Oltean 	 * Upper device tracking.
817e358bef7SVladimir Oltean 	 */
818e358bef7SVladimir Oltean 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
819e358bef7SVladimir Oltean 				       struct netdev_notifier_changeupper_info *info);
820e358bef7SVladimir Oltean 
821e358bef7SVladimir Oltean 	/*
822b73adef6SFlorian Fainelli 	 * Bridge integration
823b73adef6SFlorian Fainelli 	 */
82434a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
82571327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
826b079922bSVladimir Oltean 				    struct dsa_bridge bridge,
827b079922bSVladimir Oltean 				    bool *tx_fwd_offload);
828f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
829d3eed0e5SVladimir Oltean 				     struct dsa_bridge bridge);
83043c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
831b73adef6SFlorian Fainelli 				      u8 state);
832732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
833a8b659e7SVladimir Oltean 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
834a8b659e7SVladimir Oltean 					 struct switchdev_brport_flags flags,
835a8b659e7SVladimir Oltean 					 struct netlink_ext_ack *extack);
836a8b659e7SVladimir Oltean 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
837a8b659e7SVladimir Oltean 				     struct switchdev_brport_flags flags,
838a8b659e7SVladimir Oltean 				     struct netlink_ext_ack *extack);
8392a778e1bSVivien Didelot 
8402a778e1bSVivien Didelot 	/*
84111149536SVivien Didelot 	 * VLAN support
84211149536SVivien Didelot 	 */
843fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
84489153ed6SVladimir Oltean 				       bool vlan_filtering,
84589153ed6SVladimir Oltean 				       struct netlink_ext_ack *extack);
8461958d581SVladimir Oltean 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
84731046a5fSVladimir Oltean 				 const struct switchdev_obj_port_vlan *vlan,
84831046a5fSVladimir Oltean 				 struct netlink_ext_ack *extack);
84976e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
85076e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
85111149536SVivien Didelot 	/*
8522a778e1bSVivien Didelot 	 * Forwarding database
8532a778e1bSVivien Didelot 	 */
8541b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
8556c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
8562a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
8576c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
858ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
8592bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
8608df30255SVivien Didelot 
8618df30255SVivien Didelot 	/*
8628df30255SVivien Didelot 	 * Multicast database
8638df30255SVivien Didelot 	 */
864a52b2da7SVladimir Oltean 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
8653709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
8668df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
8678df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
868bf9f2648SFlorian Fainelli 	/*
869bf9f2648SFlorian Fainelli 	 * RXNFC
870bf9f2648SFlorian Fainelli 	 */
871bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
872bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
873bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
874bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
875f50f2127SFlorian Fainelli 
876f50f2127SFlorian Fainelli 	/*
877f50f2127SFlorian Fainelli 	 * TC integration
878f50f2127SFlorian Fainelli 	 */
879ed11bb1fSVladimir Oltean 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
880ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
881ed11bb1fSVladimir Oltean 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
882ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
883ed11bb1fSVladimir Oltean 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
884ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls, bool ingress);
885f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
886f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
887f50f2127SFlorian Fainelli 				   bool ingress);
888f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
889f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
89034297176SVladimir Oltean 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
89134297176SVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer);
89234297176SVladimir Oltean 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
89347d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
89447d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
89540ef2c93SVivien Didelot 
89640ef2c93SVivien Didelot 	/*
89740ef2c93SVivien Didelot 	 * Cross-chip operations
89840ef2c93SVivien Didelot 	 */
899f66a6a69SVladimir Oltean 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
900f66a6a69SVladimir Oltean 					 int sw_index, int port,
901d3eed0e5SVladimir Oltean 					 struct dsa_bridge bridge);
902f66a6a69SVladimir Oltean 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
903f66a6a69SVladimir Oltean 					  int sw_index, int port,
904d3eed0e5SVladimir Oltean 					  struct dsa_bridge bridge);
905058102a6STobias Waldekranz 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
906058102a6STobias Waldekranz 					int port);
907058102a6STobias Waldekranz 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
908058102a6STobias Waldekranz 				      int port, struct net_device *lag,
909058102a6STobias Waldekranz 				      struct netdev_lag_upper_info *info);
910058102a6STobias Waldekranz 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
911058102a6STobias Waldekranz 				       int port, struct net_device *lag);
9120336369dSBrandon Streiff 
9130336369dSBrandon Streiff 	/*
9140336369dSBrandon Streiff 	 * PTP functionality
9150336369dSBrandon Streiff 	 */
9160336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
9170336369dSBrandon Streiff 				     struct ifreq *ifr);
9180336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
9190336369dSBrandon Streiff 				     struct ifreq *ifr);
9205c5416f5SYangbo Lu 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
9215c5416f5SYangbo Lu 				 struct sk_buff *skb);
92290af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
92390af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
92497a69a0dSVladimir Oltean 
9250f06b855SAndrew Lunn 	/* Devlink parameters, etc */
9266b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
9276b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
9286b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
9296b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
9300f06b855SAndrew Lunn 	int	(*devlink_info_get)(struct dsa_switch *ds,
9310f06b855SAndrew Lunn 				    struct devlink_info_req *req,
9320f06b855SAndrew Lunn 				    struct netlink_ext_ack *extack);
9332a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
9342a6ef763SVladimir Oltean 				       unsigned int sb_index, u16 pool_index,
9352a6ef763SVladimir Oltean 				       struct devlink_sb_pool_info *pool_info);
9362a6ef763SVladimir Oltean 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
9372a6ef763SVladimir Oltean 				       u16 pool_index, u32 size,
9382a6ef763SVladimir Oltean 				       enum devlink_sb_threshold_type threshold_type,
9392a6ef763SVladimir Oltean 				       struct netlink_ext_ack *extack);
9402a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
9412a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
9422a6ef763SVladimir Oltean 					    u32 *p_threshold);
9432a6ef763SVladimir Oltean 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
9442a6ef763SVladimir Oltean 					    unsigned int sb_index, u16 pool_index,
9452a6ef763SVladimir Oltean 					    u32 threshold,
9462a6ef763SVladimir Oltean 					    struct netlink_ext_ack *extack);
9472a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
9482a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
9492a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
9502a6ef763SVladimir Oltean 					       u16 *p_pool_index, u32 *p_threshold);
9512a6ef763SVladimir Oltean 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
9522a6ef763SVladimir Oltean 					       unsigned int sb_index, u16 tc_index,
9532a6ef763SVladimir Oltean 					       enum devlink_sb_pool_type pool_type,
9542a6ef763SVladimir Oltean 					       u16 pool_index, u32 threshold,
9552a6ef763SVladimir Oltean 					       struct netlink_ext_ack *extack);
9562a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
9572a6ef763SVladimir Oltean 					   unsigned int sb_index);
9582a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
9592a6ef763SVladimir Oltean 					    unsigned int sb_index);
9602a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
9612a6ef763SVladimir Oltean 						unsigned int sb_index, u16 pool_index,
9622a6ef763SVladimir Oltean 						u32 *p_cur, u32 *p_max);
9632a6ef763SVladimir Oltean 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
9642a6ef763SVladimir Oltean 						   unsigned int sb_index, u16 tc_index,
9652a6ef763SVladimir Oltean 						   enum devlink_sb_pool_type pool_type,
9662a6ef763SVladimir Oltean 						   u32 *p_cur, u32 *p_max);
967bfcb8132SVladimir Oltean 
968bfcb8132SVladimir Oltean 	/*
969bfcb8132SVladimir Oltean 	 * MTU change functionality. Switches can also adjust their MRU through
970bfcb8132SVladimir Oltean 	 * this method. By MTU, one understands the SDU (L2 payload) length.
971bfcb8132SVladimir Oltean 	 * If the switch needs to account for the DSA tag on the CPU port, this
972ab88d64aSRandy Dunlap 	 * method needs to do so privately.
973bfcb8132SVladimir Oltean 	 */
974bfcb8132SVladimir Oltean 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
975bfcb8132SVladimir Oltean 				   int new_mtu);
976bfcb8132SVladimir Oltean 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
977058102a6STobias Waldekranz 
978058102a6STobias Waldekranz 	/*
979058102a6STobias Waldekranz 	 * LAG integration
980058102a6STobias Waldekranz 	 */
981058102a6STobias Waldekranz 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
982058102a6STobias Waldekranz 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
983058102a6STobias Waldekranz 				 struct net_device *lag,
984058102a6STobias Waldekranz 				 struct netdev_lag_upper_info *info);
985058102a6STobias Waldekranz 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
986058102a6STobias Waldekranz 				  struct net_device *lag);
98718596f50SGeorge McCollister 
98818596f50SGeorge McCollister 	/*
98918596f50SGeorge McCollister 	 * HSR integration
99018596f50SGeorge McCollister 	 */
99118596f50SGeorge McCollister 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
99218596f50SGeorge McCollister 				 struct net_device *hsr);
99318596f50SGeorge McCollister 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
99418596f50SGeorge McCollister 				  struct net_device *hsr);
995c595c433SHoratiu Vultur 
996c595c433SHoratiu Vultur 	/*
997c595c433SHoratiu Vultur 	 * MRP integration
998c595c433SHoratiu Vultur 	 */
999c595c433SHoratiu Vultur 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
1000c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
1001c595c433SHoratiu Vultur 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
1002c595c433SHoratiu Vultur 				const struct switchdev_obj_mrp *mrp);
1003c595c433SHoratiu Vultur 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1004c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
1005c595c433SHoratiu Vultur 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1006c595c433SHoratiu Vultur 					  const struct switchdev_obj_ring_role_mrp *mrp);
10075da11eb4SVladimir Oltean 
10085da11eb4SVladimir Oltean 	/*
10095da11eb4SVladimir Oltean 	 * tag_8021q operations
10105da11eb4SVladimir Oltean 	 */
10115da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
10125da11eb4SVladimir Oltean 				      u16 flags);
10135da11eb4SVladimir Oltean 	int	(*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
10146b297524SAndrew Lunn };
10156b297524SAndrew Lunn 
10166b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
10176b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
10186b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
10196b297524SAndrew Lunn 
10206b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
10216b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
10226b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
10236b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
10246b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
10256b297524SAndrew Lunn 				const struct devlink_param *params,
10266b297524SAndrew Lunn 				size_t params_count);
10276b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
10286b297524SAndrew Lunn 				   const struct devlink_param *params,
10296b297524SAndrew Lunn 				   size_t params_count);
10305cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
10315cd73fbdSAndrew Lunn 				  const char *resource_name,
10325cd73fbdSAndrew Lunn 				  u64 resource_size,
10335cd73fbdSAndrew Lunn 				  u64 resource_id,
10345cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
10355cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
10365cd73fbdSAndrew Lunn 
10375cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
10385cd73fbdSAndrew Lunn 
10395cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
10405cd73fbdSAndrew Lunn 					   u64 resource_id,
10415cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
10425cd73fbdSAndrew Lunn 					   void *occ_get_priv);
10435cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
10445cd73fbdSAndrew Lunn 					     u64 resource_id);
104597c82c23SAndrew Lunn struct devlink_region *
104697c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds,
104797c82c23SAndrew Lunn 			  const struct devlink_region_ops *ops,
104897c82c23SAndrew Lunn 			  u32 region_max_snapshots, u64 region_size);
104908156ba4SAndrew Lunn struct devlink_region *
105008156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds,
105108156ba4SAndrew Lunn 			       int port,
105208156ba4SAndrew Lunn 			       const struct devlink_port_region_ops *ops,
105308156ba4SAndrew Lunn 			       u32 region_max_snapshots, u64 region_size);
105497c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region);
105597c82c23SAndrew Lunn 
1056e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
10575cd73fbdSAndrew Lunn 
10586b297524SAndrew Lunn struct dsa_devlink_priv {
10596b297524SAndrew Lunn 	struct dsa_switch *ds;
1060c8f0b869SBen Hutchings };
1061c8f0b869SBen Hutchings 
1062ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1063ccc3e6b0SAndrew Lunn {
1064ccc3e6b0SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1065ccc3e6b0SAndrew Lunn 
1066ccc3e6b0SAndrew Lunn 	return dl_priv->ds;
1067ccc3e6b0SAndrew Lunn }
1068ccc3e6b0SAndrew Lunn 
10697d1e2a10SAndrew Lunn static inline
10707d1e2a10SAndrew Lunn struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
10717d1e2a10SAndrew Lunn {
10727d1e2a10SAndrew Lunn 	struct devlink *dl = port->devlink;
10737d1e2a10SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
10747d1e2a10SAndrew Lunn 
10757d1e2a10SAndrew Lunn 	return dl_priv->ds;
10767d1e2a10SAndrew Lunn }
10777d1e2a10SAndrew Lunn 
10787d1e2a10SAndrew Lunn static inline int dsa_devlink_port_to_port(struct devlink_port *port)
10797d1e2a10SAndrew Lunn {
10807d1e2a10SAndrew Lunn 	return port->index;
10817d1e2a10SAndrew Lunn }
10827d1e2a10SAndrew Lunn 
1083ab3d408dSFlorian Fainelli struct dsa_switch_driver {
1084ab3d408dSFlorian Fainelli 	struct list_head	list;
1085a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
1086ab3d408dSFlorian Fainelli };
1087ab3d408dSFlorian Fainelli 
108814b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
1089c8f0b869SBen Hutchings 
109073a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
10919eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev)
1092c6e970a0SAndrew Lunn {
1093c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
1094717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
1095c6e970a0SAndrew Lunn #endif
1096c6e970a0SAndrew Lunn 	return false;
1097c6e970a0SAndrew Lunn }
1098c6e970a0SAndrew Lunn 
10999790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail
11009790cf20SVladimir Oltean  * tags, which don't break dissection) can be treated the same from the
11019790cf20SVladimir Oltean  * perspective of the flow dissector.
11029790cf20SVladimir Oltean  *
11039790cf20SVladimir Oltean  * We need to return:
11049790cf20SVladimir Oltean  *  - offset: the (B - A) difference between:
11059790cf20SVladimir Oltean  *    A. the position of the real EtherType and
11069790cf20SVladimir Oltean  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
11079790cf20SVladimir Oltean  *       after the normal EtherType was supposed to be)
11089790cf20SVladimir Oltean  *    The offset in bytes is exactly equal to the tagger overhead (and half of
11099790cf20SVladimir Oltean  *    that, in __be16 shorts).
11109790cf20SVladimir Oltean  *
11119790cf20SVladimir Oltean  *  - proto: the value of the real EtherType.
11129790cf20SVladimir Oltean  */
11139790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
11149790cf20SVladimir Oltean 						__be16 *proto, int *offset)
11159790cf20SVladimir Oltean {
11169790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
11179790cf20SVladimir Oltean 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
11184e500251SVladimir Oltean 	int tag_len = ops->needed_headroom;
11199790cf20SVladimir Oltean 
11209790cf20SVladimir Oltean 	*offset = tag_len;
11219790cf20SVladimir Oltean 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
11229790cf20SVladimir Oltean #endif
11239790cf20SVladimir Oltean }
11249790cf20SVladimir Oltean 
11254cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
11264cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev)
11274cfab356SFlorian Fainelli {
11284cfab356SFlorian Fainelli 	int err = -EOPNOTSUPP;
11294cfab356SFlorian Fainelli 
11304cfab356SFlorian Fainelli 	if (!dev->dsa_ptr)
11314cfab356SFlorian Fainelli 		return err;
11324cfab356SFlorian Fainelli 
11334cfab356SFlorian Fainelli 	if (!dev->dsa_ptr->netdev_ops)
11344cfab356SFlorian Fainelli 		return err;
11354cfab356SFlorian Fainelli 
11364cfab356SFlorian Fainelli 	return 0;
11374cfab356SFlorian Fainelli }
11384cfab356SFlorian Fainelli 
1139a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
11404cfab356SFlorian Fainelli 				    int cmd)
11414cfab356SFlorian Fainelli {
11424cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *ops;
11434cfab356SFlorian Fainelli 	int err;
11444cfab356SFlorian Fainelli 
11454cfab356SFlorian Fainelli 	err = __dsa_netdevice_ops_check(dev);
11464cfab356SFlorian Fainelli 	if (err)
11474cfab356SFlorian Fainelli 		return err;
11484cfab356SFlorian Fainelli 
11494cfab356SFlorian Fainelli 	ops = dev->dsa_ptr->netdev_ops;
11504cfab356SFlorian Fainelli 
1151a7605370SArnd Bergmann 	return ops->ndo_eth_ioctl(dev, ifr, cmd);
11524cfab356SFlorian Fainelli }
11534cfab356SFlorian Fainelli #else
1154a7605370SArnd Bergmann static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
11554cfab356SFlorian Fainelli 				    int cmd)
11564cfab356SFlorian Fainelli {
11574cfab356SFlorian Fainelli 	return -EOPNOTSUPP;
11584cfab356SFlorian Fainelli }
11594cfab356SFlorian Fainelli #endif
11604cfab356SFlorian Fainelli 
116183c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
116223c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
11630650bf52SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds);
11643b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1165ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
1166ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
1167ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
1168ea825e70SFlorian Fainelli #else
1169ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
1170ea825e70SFlorian Fainelli {
1171ea825e70SFlorian Fainelli 	return 0;
1172ea825e70SFlorian Fainelli }
1173ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
1174ea825e70SFlorian Fainelli {
1175ea825e70SFlorian Fainelli 	return 0;
1176ea825e70SFlorian Fainelli }
1177ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
1178ea825e70SFlorian Fainelli 
117960724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
1180a5e3c9baSVladimir Oltean bool dsa_slave_dev_check(const struct net_device *dev);
118160724d4bSFlorian Fainelli #else
1182a5e3c9baSVladimir Oltean static inline bool dsa_slave_dev_check(const struct net_device *dev)
1183a5e3c9baSVladimir Oltean {
1184a5e3c9baSVladimir Oltean 	return false;
1185a5e3c9baSVladimir Oltean }
118660724d4bSFlorian Fainelli #endif
118760724d4bSFlorian Fainelli 
118897a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1189cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1190cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1191cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
119211d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1193cf963573SFlorian Fainelli 
1194d3b8c049SAndrew Lunn struct dsa_tag_driver {
1195d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
1196d3b8c049SAndrew Lunn 	struct list_head list;
1197d3b8c049SAndrew Lunn 	struct module *owner;
1198d3b8c049SAndrew Lunn };
1199d3b8c049SAndrew Lunn 
1200d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1201d3b8c049SAndrew Lunn 			      unsigned int count,
1202d3b8c049SAndrew Lunn 			      struct module *owner);
1203d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1204d3b8c049SAndrew Lunn 				unsigned int count);
1205d3b8c049SAndrew Lunn 
1206d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
1207d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
1208d3b8c049SAndrew Lunn {									\
1209d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
1210d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
1211d3b8c049SAndrew Lunn 	return 0;							\
1212d3b8c049SAndrew Lunn }									\
1213d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
1214d3b8c049SAndrew Lunn 									\
1215d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
1216d3b8c049SAndrew Lunn {									\
1217d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
1218d3b8c049SAndrew Lunn }									\
1219d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
1220d3b8c049SAndrew Lunn 
1221d3b8c049SAndrew Lunn /**
1222d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1223d3b8c049SAndrew Lunn  * drivers
1224d3b8c049SAndrew Lunn  * @__ops_array: Array of tag driver strucutres
1225d3b8c049SAndrew Lunn  *
1226d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
1227d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
1228d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
1229d3b8c049SAndrew Lunn  */
1230d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
1231d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1232d3b8c049SAndrew Lunn 
1233d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1234d3b8c049SAndrew Lunn 
1235d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
1236d3b8c049SAndrew Lunn  * drivers
1237d3b8c049SAndrew Lunn  */
1238d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
1239d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1240d3b8c049SAndrew Lunn 	.ops = &__ops,							\
1241d3b8c049SAndrew Lunn }
1242d3b8c049SAndrew Lunn 
1243d3b8c049SAndrew Lunn /**
1244d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1245d3b8c049SAndrew Lunn  * driver
1246d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
1247d3b8c049SAndrew Lunn  *
1248d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
1249d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
1250d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
1251d3b8c049SAndrew Lunn  */
1252d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
1253d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
1254d3b8c049SAndrew Lunn 									\
1255d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1256d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
1257d3b8c049SAndrew Lunn };									\
1258d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
125991da11f8SLennert Buytenhek #endif
1260d3b8c049SAndrew Lunn 
1261