xref: /linux/include/net/dsa.h (revision 9790cf20a8c4bb8d774797c238fa3643f4336e46)
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
480b42f033SAndrew Lunn 
49ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
500b42f033SAndrew Lunn 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
510b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
520b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
530b42f033SAndrew Lunn 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
540b42f033SAndrew Lunn 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
550b42f033SAndrew Lunn 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
560b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
570b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
580b42f033SAndrew Lunn 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
590b42f033SAndrew Lunn 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
600b42f033SAndrew Lunn 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
610b42f033SAndrew Lunn 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
62f9bbe447SVladimir Oltean 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
63227d07a0SVladimir Oltean 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
64016e43a2STristram Ha 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
658dce89aaSVladimir Oltean 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
6648fda74fSOleksij Rempel 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
67efd7fe68SLinus Walleij 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
68ac7a04c3SFlorian Fainelli };
695037d532SFlorian Fainelli 
705075314eSAlexander Duyck struct packet_type;
7190af1059SBrandon Streiff struct dsa_switch;
723e8a72d1SFlorian Fainelli 
7368277a2cSJohn Crispin struct dsa_device_ops {
7468277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
7568277a2cSJohn Crispin 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
7689e49506SFlorian Westphal 			       struct packet_type *pt);
772e8cb1b3SVladimir Oltean 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
78598a9680SJohn Crispin 			     int *offset);
79cc1939e4SVladimir Oltean 	/* Used to determine which traffic should match the DSA filter in
80cc1939e4SVladimir Oltean 	 * eth_type_trans, and which, if any, should bypass it and be processed
81cc1939e4SVladimir Oltean 	 * as regular on the master net device.
82cc1939e4SVladimir Oltean 	 */
83cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
84a5dd3087SAndrew Lunn 	unsigned int overhead;
85875138f8SAndrew Lunn 	const char *name;
86056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
87c3975400SVladimir Oltean 	/* Some tagging protocols either mangle or shift the destination MAC
88c3975400SVladimir Oltean 	 * address, in which case the DSA master would drop packets on ingress
89c3975400SVladimir Oltean 	 * if what it understands out of the destination MAC address is not in
90c3975400SVladimir Oltean 	 * its RX filter.
91c3975400SVladimir Oltean 	 */
92c3975400SVladimir Oltean 	bool promisc_on_master;
9368277a2cSJohn Crispin };
9468277a2cSJohn Crispin 
954cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the
964cfab356SFlorian Fainelli  * DSA layer on top of the DSA CPU/management net_device instance. This is
974cfab356SFlorian Fainelli  * used by the core net_device layer while calling various net_device_ops
984cfab356SFlorian Fainelli  * function pointers.
994cfab356SFlorian Fainelli  */
1004cfab356SFlorian Fainelli struct dsa_netdevice_ops {
1014cfab356SFlorian Fainelli 	int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr,
1024cfab356SFlorian Fainelli 			    int cmd);
1034cfab356SFlorian Fainelli };
1044cfab356SFlorian Fainelli 
1050b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
1060b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
1070b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
1080b42f033SAndrew Lunn 
109b68b0dd0SVladimir Oltean struct dsa_skb_cb {
110b68b0dd0SVladimir Oltean 	struct sk_buff *clone;
111b68b0dd0SVladimir Oltean };
112b68b0dd0SVladimir Oltean 
113b68b0dd0SVladimir Oltean struct __dsa_skb_cb {
114b68b0dd0SVladimir Oltean 	struct dsa_skb_cb cb;
115b68b0dd0SVladimir Oltean 	u8 priv[48 - sizeof(struct dsa_skb_cb)];
116b68b0dd0SVladimir Oltean };
117b68b0dd0SVladimir Oltean 
118b68b0dd0SVladimir Oltean #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
119b68b0dd0SVladimir Oltean 
120b68b0dd0SVladimir Oltean #define DSA_SKB_CB_PRIV(skb)			\
121b68b0dd0SVladimir Oltean 	((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
122b68b0dd0SVladimir Oltean 
123cf50dcc2SBen Hutchings struct dsa_switch_tree {
12483c0afaeSAndrew Lunn 	struct list_head	list;
12583c0afaeSAndrew Lunn 
126f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
127f515f192SVivien Didelot 	struct raw_notifier_head	nh;
128f515f192SVivien Didelot 
12983c0afaeSAndrew Lunn 	/* Tree identifier */
13049463b7fSVivien Didelot 	unsigned int index;
13183c0afaeSAndrew Lunn 
13283c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
13383c0afaeSAndrew Lunn 	struct kref refcount;
13483c0afaeSAndrew Lunn 
13583c0afaeSAndrew Lunn 	/* Has this tree been applied to the hardware? */
136ec15dd42SVivien Didelot 	bool setup;
13783c0afaeSAndrew Lunn 
138cf50dcc2SBen Hutchings 	/*
139cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
140cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
141cf50dcc2SBen Hutchings 	 */
142cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
143cf85d08fSLennert Buytenhek 
144ab8ccae1SVivien Didelot 	/* List of switch ports */
145ab8ccae1SVivien Didelot 	struct list_head ports;
146ab8ccae1SVivien Didelot 
147c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
148c5f51765SVivien Didelot 	struct list_head rtable;
149cf50dcc2SBen Hutchings };
150cf50dcc2SBen Hutchings 
15134297176SVladimir Oltean /* TC matchall action types */
152f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
153f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
15434297176SVladimir Oltean 	DSA_PORT_MALL_POLICER,
155f50f2127SFlorian Fainelli };
156f50f2127SFlorian Fainelli 
157f50f2127SFlorian Fainelli /* TC mirroring entry */
158f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
159f50f2127SFlorian Fainelli 	u8 to_local_port;
160f50f2127SFlorian Fainelli 	bool ingress;
161f50f2127SFlorian Fainelli };
162f50f2127SFlorian Fainelli 
16334297176SVladimir Oltean /* TC port policer entry */
16434297176SVladimir Oltean struct dsa_mall_policer_tc_entry {
1655f035af7SPo Liu 	u32 burst;
16634297176SVladimir Oltean 	u64 rate_bytes_per_sec;
16734297176SVladimir Oltean };
16834297176SVladimir Oltean 
169f50f2127SFlorian Fainelli /* TC matchall entry */
170f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
171f50f2127SFlorian Fainelli 	struct list_head list;
172f50f2127SFlorian Fainelli 	unsigned long cookie;
173f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
174f50f2127SFlorian Fainelli 	union {
175f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
17634297176SVladimir Oltean 		struct dsa_mall_policer_tc_entry policer;
177f50f2127SFlorian Fainelli 	};
178f50f2127SFlorian Fainelli };
179f50f2127SFlorian Fainelli 
180f50f2127SFlorian Fainelli 
181c8b09808SAndrew Lunn struct dsa_port {
182f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
183f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
184f8b8b1cdSVivien Didelot 	 */
185f8b8b1cdSVivien Didelot 	union {
186f8b8b1cdSVivien Didelot 		struct net_device *master;
187f8b8b1cdSVivien Didelot 		struct net_device *slave;
188f8b8b1cdSVivien Didelot 	};
189f8b8b1cdSVivien Didelot 
19015240248SVivien Didelot 	/* CPU port tagging operations used by master or slave devices */
19115240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
19215240248SVivien Didelot 
1933e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
1943e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
1953e41f93bSVivien Didelot 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
1963e41f93bSVivien Didelot 			       struct packet_type *pt);
197cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
1983e41f93bSVivien Didelot 
199057cad2cSVivien Didelot 	enum {
200057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
201057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
202057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
203057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
204057cad2cSVivien Didelot 	} type;
205057cad2cSVivien Didelot 
206818be848SVivien Didelot 	struct dsa_switch	*ds;
207818be848SVivien Didelot 	unsigned int		index;
20871e0bbdeSFlorian Fainelli 	const char		*name;
20968b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
210a2c7023fSXiaofei Shen 	const char		*mac;
211189b0d93SAndrew Lunn 	struct device_node	*dn;
21234a79f63SVivien Didelot 	unsigned int		ageing_time;
21333162e9aSVladimir Oltean 	bool			vlan_filtering;
214732f794cSVivien Didelot 	u8			stp_state;
215a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
21696567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
217aab9c406SFlorian Fainelli 	struct phylink		*pl;
21844cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
21997a69a0dSVladimir Oltean 
220ab8ccae1SVivien Didelot 	struct list_head list;
221ab8ccae1SVivien Didelot 
22267dbb9d4SFlorian Fainelli 	/*
223c362beb0SVladimir Oltean 	 * Give the switch driver somewhere to hang its per-port private data
224c362beb0SVladimir Oltean 	 * structures (accessible from the tagger).
225c362beb0SVladimir Oltean 	 */
226c362beb0SVladimir Oltean 	void *priv;
227c362beb0SVladimir Oltean 
228c362beb0SVladimir Oltean 	/*
22967dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
23067dbb9d4SFlorian Fainelli 	 */
23167dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
232da7b9e9bSFlorian Fainelli 
233da7b9e9bSFlorian Fainelli 	/*
234da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
235da7b9e9bSFlorian Fainelli 	 */
2364cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *netdev_ops;
237fb35c60cSVivien Didelot 
238fb35c60cSVivien Didelot 	bool setup;
239c8b09808SAndrew Lunn };
240c8b09808SAndrew Lunn 
241c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
242c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
243c5f51765SVivien Didelot  * but this would require some more complex tree walking,
244c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
245c5f51765SVivien Didelot  */
246c5f51765SVivien Didelot struct dsa_link {
247c5f51765SVivien Didelot 	struct dsa_port *dp;
248c5f51765SVivien Didelot 	struct dsa_port *link_dp;
249c5f51765SVivien Didelot 	struct list_head list;
250c5f51765SVivien Didelot };
251c5f51765SVivien Didelot 
252c8f0b869SBen Hutchings struct dsa_switch {
253fb35c60cSVivien Didelot 	bool setup;
254fb35c60cSVivien Didelot 
255c33063d6SAndrew Lunn 	struct device *dev;
256c33063d6SAndrew Lunn 
257c8f0b869SBen Hutchings 	/*
258c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
259c8f0b869SBen Hutchings 	 */
260c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
26199feaafcSVivien Didelot 	unsigned int		index;
262c8f0b869SBen Hutchings 
263f515f192SVivien Didelot 	/* Listener for switch fabric events */
264f515f192SVivien Didelot 	struct notifier_block	nb;
265f515f192SVivien Didelot 
266c8f0b869SBen Hutchings 	/*
2677543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
2687543a6d5SAndrew Lunn 	 * structure.
2697543a6d5SAndrew Lunn 	 */
2707543a6d5SAndrew Lunn 	void *priv;
2717543a6d5SAndrew Lunn 
2727543a6d5SAndrew Lunn 	/*
273c8f0b869SBen Hutchings 	 * Configuration data for this switch.
274c8f0b869SBen Hutchings 	 */
275ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
276c8f0b869SBen Hutchings 
277c8f0b869SBen Hutchings 	/*
2789d490b4eSVivien Didelot 	 * The switch operations.
279c8f0b869SBen Hutchings 	 */
280a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
281c8f0b869SBen Hutchings 
28266472fc0SAndrew Lunn 	/*
283c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
284c8f0b869SBen Hutchings 	 */
2850d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
286c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
287a0c02161SVivien Didelot 
2880f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
2890f3da6afSVivien Didelot 	unsigned int ageing_time_min;
2900f3da6afSVivien Didelot 	unsigned int ageing_time_max;
2910f3da6afSVivien Didelot 
29296567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
29396567d5dSAndrew Lunn 	struct devlink		*devlink;
29496567d5dSAndrew Lunn 
29555199df6SFlorian Fainelli 	/* Number of switch port queues */
29655199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
29755199df6SFlorian Fainelli 
2988f5d16f6SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
2998f5d16f6SVladimir Oltean 	 * settings on ports if not hardware-supported
3008f5d16f6SVladimir Oltean 	 */
3018f5d16f6SVladimir Oltean 	bool			vlan_filtering_is_global;
3028f5d16f6SVladimir Oltean 
30354a0ed0dSRussell King 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
30454a0ed0dSRussell King 	 * that have vlan_filtering=0. All drivers should ideally set this (and
30554a0ed0dSRussell King 	 * then the option would get removed), but it is unknown whether this
30654a0ed0dSRussell King 	 * would break things or not.
30754a0ed0dSRussell King 	 */
30854a0ed0dSRussell King 	bool			configure_vlan_while_not_filtering;
30954a0ed0dSRussell King 
31014574676SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
31114574676SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
31214574676SVladimir Oltean 	 */
31314574676SVladimir Oltean 	bool			vlan_filtering;
31414574676SVladimir Oltean 
315787cac3fSVladimir Oltean 	/* MAC PCS does not provide link state change interrupt, and requires
316787cac3fSVladimir Oltean 	 * polling. Flag passed on to PHYLINK.
317787cac3fSVladimir Oltean 	 */
318787cac3fSVladimir Oltean 	bool			pcs_poll;
319787cac3fSVladimir Oltean 
320bff33f7eSVladimir Oltean 	/* For switches that only have the MRU configurable. To ensure the
321bff33f7eSVladimir Oltean 	 * configured MTU is not exceeded, normalization of MRU on all bridged
322bff33f7eSVladimir Oltean 	 * interfaces is needed.
323bff33f7eSVladimir Oltean 	 */
324bff33f7eSVladimir Oltean 	bool			mtu_enforcement_ingress;
325bff33f7eSVladimir Oltean 
326a0c02161SVivien Didelot 	size_t num_ports;
327c8f0b869SBen Hutchings };
328c8f0b869SBen Hutchings 
32968bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
330c38c5a66SVivien Didelot {
331b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
332d607525bSVivien Didelot 	struct dsa_port *dp;
333b96ddf25SVivien Didelot 
334b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
335b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
336b96ddf25SVivien Didelot 			return dp;
337d607525bSVivien Didelot 
338d607525bSVivien Didelot 	return NULL;
339c38c5a66SVivien Didelot }
340c38c5a66SVivien Didelot 
341bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
342bff7b688SVivien Didelot {
343c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
344bff7b688SVivien Didelot }
345bff7b688SVivien Didelot 
346c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
347c8f0b869SBen Hutchings {
348c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
349c8f0b869SBen Hutchings }
350c8f0b869SBen Hutchings 
35160045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
35260045cbfSAndrew Lunn {
353c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
35460045cbfSAndrew Lunn }
35560045cbfSAndrew Lunn 
3562b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
3576cd456f3SVivien Didelot {
358c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
3596cd456f3SVivien Didelot }
3606cd456f3SVivien Didelot 
36102bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
36202bc6e54SVivien Didelot {
363c38c5a66SVivien Didelot 	u32 mask = 0;
364c38c5a66SVivien Didelot 	int p;
36502bc6e54SVivien Didelot 
366c38c5a66SVivien Didelot 	for (p = 0; p < ds->num_ports; p++)
367c38c5a66SVivien Didelot 		if (dsa_is_user_port(ds, p))
368c38c5a66SVivien Didelot 			mask |= BIT(p);
369c38c5a66SVivien Didelot 
370c38c5a66SVivien Didelot 	return mask;
371c8652c83SVivien Didelot }
372c8652c83SVivien Didelot 
373c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
374c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
375c5f51765SVivien Didelot {
376c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
377c5f51765SVivien Didelot 	struct dsa_link *dl;
378c5f51765SVivien Didelot 
379c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
380c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
381c5f51765SVivien Didelot 			return dl->dp->index;
382c5f51765SVivien Didelot 
383c5f51765SVivien Didelot 	return ds->num_ports;
384c5f51765SVivien Didelot }
385c5f51765SVivien Didelot 
3863b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
3873b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
3883b8fac5dSVivien Didelot 					    int port)
3893b8fac5dSVivien Didelot {
3903b8fac5dSVivien Didelot 	if (device == ds->index)
3913b8fac5dSVivien Didelot 		return port;
3923b8fac5dSVivien Didelot 	else
393c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
3943b8fac5dSVivien Didelot }
3953b8fac5dSVivien Didelot 
3963b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
39707073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
398c8f0b869SBen Hutchings {
39907073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
40007073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
40107073c79SVivien Didelot 
40207073c79SVivien Didelot 	if (!cpu_dp)
40307073c79SVivien Didelot 		return port;
404c8f0b869SBen Hutchings 
4053b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
406c8f0b869SBen Hutchings }
407c8f0b869SBen Hutchings 
408cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
409cf2d45f5SVladimir Oltean {
410cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
411cf2d45f5SVladimir Oltean 
412cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
413cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
414cf2d45f5SVladimir Oltean 	else
415cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
416cf2d45f5SVladimir Oltean }
417cf2d45f5SVladimir Oltean 
4182bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
4192bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
4209d490b4eSVivien Didelot struct dsa_switch_ops {
4215ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
4224d776482SFlorian Fainelli 						  int port,
4234d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
4247b314362SAndrew Lunn 
425c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
4265e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
4276819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
428c8f0b869SBen Hutchings 
429c8f0b869SBen Hutchings 	/*
430c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
431c8f0b869SBen Hutchings 	 */
432c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
433c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
434c8f0b869SBen Hutchings 			     int regnum, u16 val);
435c8f0b869SBen Hutchings 
436c8f0b869SBen Hutchings 	/*
437ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
438ec9436baSFlorian Fainelli 	 */
439ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
440ec9436baSFlorian Fainelli 				struct phy_device *phydev);
441ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
442ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
443ec9436baSFlorian Fainelli 
444ec9436baSFlorian Fainelli 	/*
44511d8f3ddSFlorian Fainelli 	 * PHYLINK integration
44611d8f3ddSFlorian Fainelli 	 */
44711d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
44811d8f3ddSFlorian Fainelli 				    unsigned long *supported,
44911d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
45011d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
45111d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
45211d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
45311d8f3ddSFlorian Fainelli 				      unsigned int mode,
45411d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
45511d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
45611d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
45711d8f3ddSFlorian Fainelli 					 unsigned int mode,
45811d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
45911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
46011d8f3ddSFlorian Fainelli 				       unsigned int mode,
46111d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
4625b502a7bSRussell King 				       struct phy_device *phydev,
4635b502a7bSRussell King 				       int speed, int duplex,
4645b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
46511d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
46611d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
46711d8f3ddSFlorian Fainelli 	/*
468c8f0b869SBen Hutchings 	 * ethtool hardware statistics.
469c8f0b869SBen Hutchings 	 */
47089f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
47189f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
472c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
473c8f0b869SBen Hutchings 				     int port, uint64_t *data);
47489f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
475cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
476cf963573SFlorian Fainelli 					 int port, uint64_t *data);
47724462549SFlorian Fainelli 
47824462549SFlorian Fainelli 	/*
47919e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
48019e57c4eSFlorian Fainelli 	 */
48119e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
48219e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
48319e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
48419e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
48519e57c4eSFlorian Fainelli 
48619e57c4eSFlorian Fainelli 	/*
4870336369dSBrandon Streiff 	 * ethtool timestamp info
4880336369dSBrandon Streiff 	 */
4890336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
4900336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
4910336369dSBrandon Streiff 
4920336369dSBrandon Streiff 	/*
49324462549SFlorian Fainelli 	 * Suspend and resume
49424462549SFlorian Fainelli 	 */
49524462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
49624462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
497b2f2af21SFlorian Fainelli 
498b2f2af21SFlorian Fainelli 	/*
499b2f2af21SFlorian Fainelli 	 * Port enable/disable
500b2f2af21SFlorian Fainelli 	 */
501b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
502b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
50375104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
5047905288fSFlorian Fainelli 
5057905288fSFlorian Fainelli 	/*
50608f50061SVivien Didelot 	 * Port's MAC EEE settings
5077905288fSFlorian Fainelli 	 */
50808f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
5097905288fSFlorian Fainelli 			       struct ethtool_eee *e);
51008f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
5117905288fSFlorian Fainelli 			       struct ethtool_eee *e);
51251579c3fSGuenter Roeck 
5136793abb4SGuenter Roeck 	/* EEPROM access */
5146793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
5156793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
5166793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
5176793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
5186793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
5193d762a0fSGuenter Roeck 
5203d762a0fSGuenter Roeck 	/*
5213d762a0fSGuenter Roeck 	 * Register access.
5223d762a0fSGuenter Roeck 	 */
5233d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
5243d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
5253d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
526b73adef6SFlorian Fainelli 
527b73adef6SFlorian Fainelli 	/*
528b73adef6SFlorian Fainelli 	 * Bridge integration
529b73adef6SFlorian Fainelli 	 */
53034a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
53171327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
532a6692754SVivien Didelot 				    struct net_device *bridge);
533f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
534f123f2fbSVivien Didelot 				     struct net_device *bridge);
53543c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
536b73adef6SFlorian Fainelli 				      u8 state);
537732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
53857652796SRussell King 	int	(*port_egress_floods)(struct dsa_switch *ds, int port,
53957652796SRussell King 				      bool unicast, bool multicast);
5402a778e1bSVivien Didelot 
5412a778e1bSVivien Didelot 	/*
54211149536SVivien Didelot 	 * VLAN support
54311149536SVivien Didelot 	 */
544fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
545fb2dabadSVivien Didelot 				       bool vlan_filtering);
54676e398a6SVivien Didelot 	int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
54780e02360SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
5484d5770b3SVivien Didelot 	void (*port_vlan_add)(struct dsa_switch *ds, int port,
54980e02360SVivien Didelot 			      const struct switchdev_obj_port_vlan *vlan);
55076e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
55176e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
55211149536SVivien Didelot 	/*
5532a778e1bSVivien Didelot 	 * Forwarding database
5542a778e1bSVivien Didelot 	 */
5551b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
5566c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
5572a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
5586c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
559ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
5602bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
5618df30255SVivien Didelot 
5628df30255SVivien Didelot 	/*
5638df30255SVivien Didelot 	 * Multicast database
5648df30255SVivien Didelot 	 */
5658df30255SVivien Didelot 	int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
5663709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
5678df30255SVivien Didelot 	void (*port_mdb_add)(struct dsa_switch *ds, int port,
5683709aadcSVivien Didelot 			     const struct switchdev_obj_port_mdb *mdb);
5698df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
5708df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
571bf9f2648SFlorian Fainelli 	/*
572bf9f2648SFlorian Fainelli 	 * RXNFC
573bf9f2648SFlorian Fainelli 	 */
574bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
575bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
576bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
577bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
578f50f2127SFlorian Fainelli 
579f50f2127SFlorian Fainelli 	/*
580f50f2127SFlorian Fainelli 	 * TC integration
581f50f2127SFlorian Fainelli 	 */
582ed11bb1fSVladimir Oltean 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
583ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
584ed11bb1fSVladimir Oltean 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
585ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
586ed11bb1fSVladimir Oltean 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
587ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls, bool ingress);
588f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
589f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
590f50f2127SFlorian Fainelli 				   bool ingress);
591f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
592f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
59334297176SVladimir Oltean 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
59434297176SVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer);
59534297176SVladimir Oltean 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
59647d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
59747d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
59840ef2c93SVivien Didelot 
59940ef2c93SVivien Didelot 	/*
60040ef2c93SVivien Didelot 	 * Cross-chip operations
60140ef2c93SVivien Didelot 	 */
602f66a6a69SVladimir Oltean 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
603f66a6a69SVladimir Oltean 					 int sw_index, int port,
604f66a6a69SVladimir Oltean 					 struct net_device *br);
605f66a6a69SVladimir Oltean 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
606f66a6a69SVladimir Oltean 					  int sw_index, int port,
607f66a6a69SVladimir Oltean 					  struct net_device *br);
6080336369dSBrandon Streiff 
6090336369dSBrandon Streiff 	/*
6100336369dSBrandon Streiff 	 * PTP functionality
6110336369dSBrandon Streiff 	 */
6120336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
6130336369dSBrandon Streiff 				     struct ifreq *ifr);
6140336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
6150336369dSBrandon Streiff 				     struct ifreq *ifr);
61690af1059SBrandon Streiff 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
61790af1059SBrandon Streiff 				 struct sk_buff *clone, unsigned int type);
61890af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
61990af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
62097a69a0dSVladimir Oltean 
6210f06b855SAndrew Lunn 	/* Devlink parameters, etc */
6226b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
6236b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
6246b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
6256b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
6260f06b855SAndrew Lunn 	int	(*devlink_info_get)(struct dsa_switch *ds,
6270f06b855SAndrew Lunn 				    struct devlink_info_req *req,
6280f06b855SAndrew Lunn 				    struct netlink_ext_ack *extack);
629bfcb8132SVladimir Oltean 
630bfcb8132SVladimir Oltean 	/*
631bfcb8132SVladimir Oltean 	 * MTU change functionality. Switches can also adjust their MRU through
632bfcb8132SVladimir Oltean 	 * this method. By MTU, one understands the SDU (L2 payload) length.
633bfcb8132SVladimir Oltean 	 * If the switch needs to account for the DSA tag on the CPU port, this
634ab88d64aSRandy Dunlap 	 * method needs to do so privately.
635bfcb8132SVladimir Oltean 	 */
636bfcb8132SVladimir Oltean 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
637bfcb8132SVladimir Oltean 				   int new_mtu);
638bfcb8132SVladimir Oltean 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
6396b297524SAndrew Lunn };
6406b297524SAndrew Lunn 
6416b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
6426b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
6436b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
6446b297524SAndrew Lunn 
6456b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
6466b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
6476b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
6486b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
6496b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
6506b297524SAndrew Lunn 				const struct devlink_param *params,
6516b297524SAndrew Lunn 				size_t params_count);
6526b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
6536b297524SAndrew Lunn 				   const struct devlink_param *params,
6546b297524SAndrew Lunn 				   size_t params_count);
6555cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
6565cd73fbdSAndrew Lunn 				  const char *resource_name,
6575cd73fbdSAndrew Lunn 				  u64 resource_size,
6585cd73fbdSAndrew Lunn 				  u64 resource_id,
6595cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
6605cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
6615cd73fbdSAndrew Lunn 
6625cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
6635cd73fbdSAndrew Lunn 
6645cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
6655cd73fbdSAndrew Lunn 					   u64 resource_id,
6665cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
6675cd73fbdSAndrew Lunn 					   void *occ_get_priv);
6685cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
6695cd73fbdSAndrew Lunn 					     u64 resource_id);
67097c82c23SAndrew Lunn struct devlink_region *
67197c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds,
67297c82c23SAndrew Lunn 			  const struct devlink_region_ops *ops,
67397c82c23SAndrew Lunn 			  u32 region_max_snapshots, u64 region_size);
67497c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region);
67597c82c23SAndrew Lunn 
676e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
6775cd73fbdSAndrew Lunn 
6786b297524SAndrew Lunn struct dsa_devlink_priv {
6796b297524SAndrew Lunn 	struct dsa_switch *ds;
680c8f0b869SBen Hutchings };
681c8f0b869SBen Hutchings 
682ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
683ccc3e6b0SAndrew Lunn {
684ccc3e6b0SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
685ccc3e6b0SAndrew Lunn 
686ccc3e6b0SAndrew Lunn 	return dl_priv->ds;
687ccc3e6b0SAndrew Lunn }
688ccc3e6b0SAndrew Lunn 
689ab3d408dSFlorian Fainelli struct dsa_switch_driver {
690ab3d408dSFlorian Fainelli 	struct list_head	list;
691a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
692ab3d408dSFlorian Fainelli };
693ab3d408dSFlorian Fainelli 
69414b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
695c8f0b869SBen Hutchings 
69673a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
6979eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev)
698c6e970a0SAndrew Lunn {
699c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
700717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
701c6e970a0SAndrew Lunn #endif
702c6e970a0SAndrew Lunn 	return false;
703c6e970a0SAndrew Lunn }
704c6e970a0SAndrew Lunn 
705cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb,
706cc1939e4SVladimir Oltean 				  struct net_device *dev)
707cc1939e4SVladimir Oltean {
708cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
709cc1939e4SVladimir Oltean 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
710cc1939e4SVladimir Oltean #endif
711cc1939e4SVladimir Oltean 	return false;
712cc1939e4SVladimir Oltean }
713cc1939e4SVladimir Oltean 
714*9790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail
715*9790cf20SVladimir Oltean  * tags, which don't break dissection) can be treated the same from the
716*9790cf20SVladimir Oltean  * perspective of the flow dissector.
717*9790cf20SVladimir Oltean  *
718*9790cf20SVladimir Oltean  * We need to return:
719*9790cf20SVladimir Oltean  *  - offset: the (B - A) difference between:
720*9790cf20SVladimir Oltean  *    A. the position of the real EtherType and
721*9790cf20SVladimir Oltean  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
722*9790cf20SVladimir Oltean  *       after the normal EtherType was supposed to be)
723*9790cf20SVladimir Oltean  *    The offset in bytes is exactly equal to the tagger overhead (and half of
724*9790cf20SVladimir Oltean  *    that, in __be16 shorts).
725*9790cf20SVladimir Oltean  *
726*9790cf20SVladimir Oltean  *  - proto: the value of the real EtherType.
727*9790cf20SVladimir Oltean  */
728*9790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
729*9790cf20SVladimir Oltean 						__be16 *proto, int *offset)
730*9790cf20SVladimir Oltean {
731*9790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
732*9790cf20SVladimir Oltean 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
733*9790cf20SVladimir Oltean 	int tag_len = ops->overhead;
734*9790cf20SVladimir Oltean 
735*9790cf20SVladimir Oltean 	*offset = tag_len;
736*9790cf20SVladimir Oltean 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
737*9790cf20SVladimir Oltean #endif
738*9790cf20SVladimir Oltean }
739*9790cf20SVladimir Oltean 
7404cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
7414cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev)
7424cfab356SFlorian Fainelli {
7434cfab356SFlorian Fainelli 	int err = -EOPNOTSUPP;
7444cfab356SFlorian Fainelli 
7454cfab356SFlorian Fainelli 	if (!dev->dsa_ptr)
7464cfab356SFlorian Fainelli 		return err;
7474cfab356SFlorian Fainelli 
7484cfab356SFlorian Fainelli 	if (!dev->dsa_ptr->netdev_ops)
7494cfab356SFlorian Fainelli 		return err;
7504cfab356SFlorian Fainelli 
7514cfab356SFlorian Fainelli 	return 0;
7524cfab356SFlorian Fainelli }
7534cfab356SFlorian Fainelli 
7544cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
7554cfab356SFlorian Fainelli 				   int cmd)
7564cfab356SFlorian Fainelli {
7574cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *ops;
7584cfab356SFlorian Fainelli 	int err;
7594cfab356SFlorian Fainelli 
7604cfab356SFlorian Fainelli 	err = __dsa_netdevice_ops_check(dev);
7614cfab356SFlorian Fainelli 	if (err)
7624cfab356SFlorian Fainelli 		return err;
7634cfab356SFlorian Fainelli 
7644cfab356SFlorian Fainelli 	ops = dev->dsa_ptr->netdev_ops;
7654cfab356SFlorian Fainelli 
7664cfab356SFlorian Fainelli 	return ops->ndo_do_ioctl(dev, ifr, cmd);
7674cfab356SFlorian Fainelli }
7684cfab356SFlorian Fainelli #else
7694cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
7704cfab356SFlorian Fainelli 				   int cmd)
7714cfab356SFlorian Fainelli {
7724cfab356SFlorian Fainelli 	return -EOPNOTSUPP;
7734cfab356SFlorian Fainelli }
7744cfab356SFlorian Fainelli #endif
7754cfab356SFlorian Fainelli 
77683c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
77723c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
7783b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
779ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
780ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
781ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
782ea825e70SFlorian Fainelli #else
783ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
784ea825e70SFlorian Fainelli {
785ea825e70SFlorian Fainelli 	return 0;
786ea825e70SFlorian Fainelli }
787ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
788ea825e70SFlorian Fainelli {
789ea825e70SFlorian Fainelli 	return 0;
790ea825e70SFlorian Fainelli }
791ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
792ea825e70SFlorian Fainelli 
79360724d4bSFlorian Fainelli enum dsa_notifier_type {
79460724d4bSFlorian Fainelli 	DSA_PORT_REGISTER,
79560724d4bSFlorian Fainelli 	DSA_PORT_UNREGISTER,
79660724d4bSFlorian Fainelli };
79760724d4bSFlorian Fainelli 
79860724d4bSFlorian Fainelli struct dsa_notifier_info {
79960724d4bSFlorian Fainelli 	struct net_device *dev;
80060724d4bSFlorian Fainelli };
80160724d4bSFlorian Fainelli 
80260724d4bSFlorian Fainelli struct dsa_notifier_register_info {
80360724d4bSFlorian Fainelli 	struct dsa_notifier_info info;	/* must be first */
80460724d4bSFlorian Fainelli 	struct net_device *master;
80560724d4bSFlorian Fainelli 	unsigned int port_number;
80660724d4bSFlorian Fainelli 	unsigned int switch_number;
80760724d4bSFlorian Fainelli };
80860724d4bSFlorian Fainelli 
80960724d4bSFlorian Fainelli static inline struct net_device *
81060724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
81160724d4bSFlorian Fainelli {
81260724d4bSFlorian Fainelli 	return info->dev;
81360724d4bSFlorian Fainelli }
81460724d4bSFlorian Fainelli 
81560724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
81660724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb);
81760724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb);
81860724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev,
81960724d4bSFlorian Fainelli 		       struct dsa_notifier_info *info);
82060724d4bSFlorian Fainelli #else
82160724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb)
82260724d4bSFlorian Fainelli {
82360724d4bSFlorian Fainelli 	return 0;
82460724d4bSFlorian Fainelli }
82560724d4bSFlorian Fainelli 
82660724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb)
82760724d4bSFlorian Fainelli {
82860724d4bSFlorian Fainelli 	return 0;
82960724d4bSFlorian Fainelli }
83060724d4bSFlorian Fainelli 
83160724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
83260724d4bSFlorian Fainelli 				     struct dsa_notifier_info *info)
83360724d4bSFlorian Fainelli {
83460724d4bSFlorian Fainelli 	return NOTIFY_DONE;
83560724d4bSFlorian Fainelli }
83660724d4bSFlorian Fainelli #endif
83760724d4bSFlorian Fainelli 
8380a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */
8390a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q)	((p) << 8 | q)
8400a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v)		((v) >> 8)
8410a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v)		((v) & 0xff)
8420a5f14ceSFlorian Fainelli 
843cf963573SFlorian Fainelli 
84497a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
845cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
846cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
847cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
84811d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
849cf963573SFlorian Fainelli 
850d3b8c049SAndrew Lunn struct dsa_tag_driver {
851d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
852d3b8c049SAndrew Lunn 	struct list_head list;
853d3b8c049SAndrew Lunn 	struct module *owner;
854d3b8c049SAndrew Lunn };
855d3b8c049SAndrew Lunn 
856d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
857d3b8c049SAndrew Lunn 			      unsigned int count,
858d3b8c049SAndrew Lunn 			      struct module *owner);
859d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
860d3b8c049SAndrew Lunn 				unsigned int count);
861d3b8c049SAndrew Lunn 
862d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
863d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
864d3b8c049SAndrew Lunn {									\
865d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
866d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
867d3b8c049SAndrew Lunn 	return 0;							\
868d3b8c049SAndrew Lunn }									\
869d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
870d3b8c049SAndrew Lunn 									\
871d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
872d3b8c049SAndrew Lunn {									\
873d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
874d3b8c049SAndrew Lunn }									\
875d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
876d3b8c049SAndrew Lunn 
877d3b8c049SAndrew Lunn /**
878d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
879d3b8c049SAndrew Lunn  * drivers
880d3b8c049SAndrew Lunn  * @__ops_array: Array of tag driver strucutres
881d3b8c049SAndrew Lunn  *
882d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
883d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
884d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
885d3b8c049SAndrew Lunn  */
886d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
887d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
888d3b8c049SAndrew Lunn 
889d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
890d3b8c049SAndrew Lunn 
891d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
892d3b8c049SAndrew Lunn  * drivers
893d3b8c049SAndrew Lunn  */
894d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
895d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
896d3b8c049SAndrew Lunn 	.ops = &__ops,							\
897d3b8c049SAndrew Lunn }
898d3b8c049SAndrew Lunn 
899d3b8c049SAndrew Lunn /**
900d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
901d3b8c049SAndrew Lunn  * driver
902d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
903d3b8c049SAndrew Lunn  *
904d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
905d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
906d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
907d3b8c049SAndrew Lunn  */
908d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
909d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
910d3b8c049SAndrew Lunn 									\
911d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
912d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
913d3b8c049SAndrew Lunn };									\
914d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
91591da11f8SLennert Buytenhek #endif
916d3b8c049SAndrew Lunn 
917