xref: /openbmc/linux/include/net/dsa.h (revision 97c82c23)
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);
77598a9680SJohn Crispin 	int (*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;
8768277a2cSJohn Crispin };
8868277a2cSJohn Crispin 
894cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the
904cfab356SFlorian Fainelli  * DSA layer on top of the DSA CPU/management net_device instance. This is
914cfab356SFlorian Fainelli  * used by the core net_device layer while calling various net_device_ops
924cfab356SFlorian Fainelli  * function pointers.
934cfab356SFlorian Fainelli  */
944cfab356SFlorian Fainelli struct dsa_netdevice_ops {
954cfab356SFlorian Fainelli 	int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr,
964cfab356SFlorian Fainelli 			    int cmd);
974cfab356SFlorian Fainelli };
984cfab356SFlorian Fainelli 
990b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
1000b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
1010b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
1020b42f033SAndrew Lunn 
103b68b0dd0SVladimir Oltean struct dsa_skb_cb {
104b68b0dd0SVladimir Oltean 	struct sk_buff *clone;
105b68b0dd0SVladimir Oltean };
106b68b0dd0SVladimir Oltean 
107b68b0dd0SVladimir Oltean struct __dsa_skb_cb {
108b68b0dd0SVladimir Oltean 	struct dsa_skb_cb cb;
109b68b0dd0SVladimir Oltean 	u8 priv[48 - sizeof(struct dsa_skb_cb)];
110b68b0dd0SVladimir Oltean };
111b68b0dd0SVladimir Oltean 
112b68b0dd0SVladimir Oltean #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
113b68b0dd0SVladimir Oltean 
114b68b0dd0SVladimir Oltean #define DSA_SKB_CB_PRIV(skb)			\
115b68b0dd0SVladimir Oltean 	((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
116b68b0dd0SVladimir Oltean 
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 
132cf50dcc2SBen Hutchings 	/*
133cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
134cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
135cf50dcc2SBen Hutchings 	 */
136cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
137cf85d08fSLennert Buytenhek 
138ab8ccae1SVivien Didelot 	/* List of switch ports */
139ab8ccae1SVivien Didelot 	struct list_head ports;
140ab8ccae1SVivien Didelot 
141c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
142c5f51765SVivien Didelot 	struct list_head rtable;
143cf50dcc2SBen Hutchings };
144cf50dcc2SBen Hutchings 
14534297176SVladimir Oltean /* TC matchall action types */
146f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
147f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
14834297176SVladimir Oltean 	DSA_PORT_MALL_POLICER,
149f50f2127SFlorian Fainelli };
150f50f2127SFlorian Fainelli 
151f50f2127SFlorian Fainelli /* TC mirroring entry */
152f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
153f50f2127SFlorian Fainelli 	u8 to_local_port;
154f50f2127SFlorian Fainelli 	bool ingress;
155f50f2127SFlorian Fainelli };
156f50f2127SFlorian Fainelli 
15734297176SVladimir Oltean /* TC port policer entry */
15834297176SVladimir Oltean struct dsa_mall_policer_tc_entry {
1595f035af7SPo Liu 	u32 burst;
16034297176SVladimir Oltean 	u64 rate_bytes_per_sec;
16134297176SVladimir Oltean };
16234297176SVladimir Oltean 
163f50f2127SFlorian Fainelli /* TC matchall entry */
164f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
165f50f2127SFlorian Fainelli 	struct list_head list;
166f50f2127SFlorian Fainelli 	unsigned long cookie;
167f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
168f50f2127SFlorian Fainelli 	union {
169f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
17034297176SVladimir Oltean 		struct dsa_mall_policer_tc_entry policer;
171f50f2127SFlorian Fainelli 	};
172f50f2127SFlorian Fainelli };
173f50f2127SFlorian Fainelli 
174f50f2127SFlorian Fainelli 
175c8b09808SAndrew Lunn struct dsa_port {
176f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
177f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
178f8b8b1cdSVivien Didelot 	 */
179f8b8b1cdSVivien Didelot 	union {
180f8b8b1cdSVivien Didelot 		struct net_device *master;
181f8b8b1cdSVivien Didelot 		struct net_device *slave;
182f8b8b1cdSVivien Didelot 	};
183f8b8b1cdSVivien Didelot 
18415240248SVivien Didelot 	/* CPU port tagging operations used by master or slave devices */
18515240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
18615240248SVivien Didelot 
1873e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
1883e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
1893e41f93bSVivien Didelot 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
1903e41f93bSVivien Didelot 			       struct packet_type *pt);
191cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
1923e41f93bSVivien Didelot 
193057cad2cSVivien Didelot 	enum {
194057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
195057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
196057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
197057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
198057cad2cSVivien Didelot 	} type;
199057cad2cSVivien Didelot 
200818be848SVivien Didelot 	struct dsa_switch	*ds;
201818be848SVivien Didelot 	unsigned int		index;
20271e0bbdeSFlorian Fainelli 	const char		*name;
20368b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
204a2c7023fSXiaofei Shen 	const char		*mac;
205189b0d93SAndrew Lunn 	struct device_node	*dn;
20634a79f63SVivien Didelot 	unsigned int		ageing_time;
20733162e9aSVladimir Oltean 	bool			vlan_filtering;
208732f794cSVivien Didelot 	u8			stp_state;
209a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
21096567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
211aab9c406SFlorian Fainelli 	struct phylink		*pl;
21244cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
21397a69a0dSVladimir Oltean 
214ab8ccae1SVivien Didelot 	struct list_head list;
215ab8ccae1SVivien Didelot 
21667dbb9d4SFlorian Fainelli 	/*
217c362beb0SVladimir Oltean 	 * Give the switch driver somewhere to hang its per-port private data
218c362beb0SVladimir Oltean 	 * structures (accessible from the tagger).
219c362beb0SVladimir Oltean 	 */
220c362beb0SVladimir Oltean 	void *priv;
221c362beb0SVladimir Oltean 
222c362beb0SVladimir Oltean 	/*
22367dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
22467dbb9d4SFlorian Fainelli 	 */
22567dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
226da7b9e9bSFlorian Fainelli 
227da7b9e9bSFlorian Fainelli 	/*
228da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
229da7b9e9bSFlorian Fainelli 	 */
2304cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *netdev_ops;
231fb35c60cSVivien Didelot 
232fb35c60cSVivien Didelot 	bool setup;
233c8b09808SAndrew Lunn };
234c8b09808SAndrew Lunn 
235c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
236c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
237c5f51765SVivien Didelot  * but this would require some more complex tree walking,
238c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
239c5f51765SVivien Didelot  */
240c5f51765SVivien Didelot struct dsa_link {
241c5f51765SVivien Didelot 	struct dsa_port *dp;
242c5f51765SVivien Didelot 	struct dsa_port *link_dp;
243c5f51765SVivien Didelot 	struct list_head list;
244c5f51765SVivien Didelot };
245c5f51765SVivien Didelot 
246c8f0b869SBen Hutchings struct dsa_switch {
247fb35c60cSVivien Didelot 	bool setup;
248fb35c60cSVivien Didelot 
249c33063d6SAndrew Lunn 	struct device *dev;
250c33063d6SAndrew Lunn 
251c8f0b869SBen Hutchings 	/*
252c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
253c8f0b869SBen Hutchings 	 */
254c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
25599feaafcSVivien Didelot 	unsigned int		index;
256c8f0b869SBen Hutchings 
257f515f192SVivien Didelot 	/* Listener for switch fabric events */
258f515f192SVivien Didelot 	struct notifier_block	nb;
259f515f192SVivien Didelot 
260c8f0b869SBen Hutchings 	/*
2617543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
2627543a6d5SAndrew Lunn 	 * structure.
2637543a6d5SAndrew Lunn 	 */
2647543a6d5SAndrew Lunn 	void *priv;
2657543a6d5SAndrew Lunn 
2667543a6d5SAndrew Lunn 	/*
267c8f0b869SBen Hutchings 	 * Configuration data for this switch.
268c8f0b869SBen Hutchings 	 */
269ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
270c8f0b869SBen Hutchings 
271c8f0b869SBen Hutchings 	/*
2729d490b4eSVivien Didelot 	 * The switch operations.
273c8f0b869SBen Hutchings 	 */
274a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
275c8f0b869SBen Hutchings 
27666472fc0SAndrew Lunn 	/*
277c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
278c8f0b869SBen Hutchings 	 */
2790d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
280c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
281a0c02161SVivien Didelot 
2820f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
2830f3da6afSVivien Didelot 	unsigned int ageing_time_min;
2840f3da6afSVivien Didelot 	unsigned int ageing_time_max;
2850f3da6afSVivien Didelot 
28696567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
28796567d5dSAndrew Lunn 	struct devlink		*devlink;
28896567d5dSAndrew Lunn 
28955199df6SFlorian Fainelli 	/* Number of switch port queues */
29055199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
29155199df6SFlorian Fainelli 
2928f5d16f6SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
2938f5d16f6SVladimir Oltean 	 * settings on ports if not hardware-supported
2948f5d16f6SVladimir Oltean 	 */
2958f5d16f6SVladimir Oltean 	bool			vlan_filtering_is_global;
2968f5d16f6SVladimir Oltean 
29754a0ed0dSRussell King 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
29854a0ed0dSRussell King 	 * that have vlan_filtering=0. All drivers should ideally set this (and
29954a0ed0dSRussell King 	 * then the option would get removed), but it is unknown whether this
30054a0ed0dSRussell King 	 * would break things or not.
30154a0ed0dSRussell King 	 */
30254a0ed0dSRussell King 	bool			configure_vlan_while_not_filtering;
30354a0ed0dSRussell King 
30414574676SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
30514574676SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
30614574676SVladimir Oltean 	 */
30714574676SVladimir Oltean 	bool			vlan_filtering;
30814574676SVladimir Oltean 
309787cac3fSVladimir Oltean 	/* MAC PCS does not provide link state change interrupt, and requires
310787cac3fSVladimir Oltean 	 * polling. Flag passed on to PHYLINK.
311787cac3fSVladimir Oltean 	 */
312787cac3fSVladimir Oltean 	bool			pcs_poll;
313787cac3fSVladimir Oltean 
314bff33f7eSVladimir Oltean 	/* For switches that only have the MRU configurable. To ensure the
315bff33f7eSVladimir Oltean 	 * configured MTU is not exceeded, normalization of MRU on all bridged
316bff33f7eSVladimir Oltean 	 * interfaces is needed.
317bff33f7eSVladimir Oltean 	 */
318bff33f7eSVladimir Oltean 	bool			mtu_enforcement_ingress;
319bff33f7eSVladimir Oltean 
320a0c02161SVivien Didelot 	size_t num_ports;
321c8f0b869SBen Hutchings };
322c8f0b869SBen Hutchings 
32368bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
324c38c5a66SVivien Didelot {
325b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
326d607525bSVivien Didelot 	struct dsa_port *dp;
327b96ddf25SVivien Didelot 
328b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
329b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
330b96ddf25SVivien Didelot 			return dp;
331d607525bSVivien Didelot 
332d607525bSVivien Didelot 	return NULL;
333c38c5a66SVivien Didelot }
334c38c5a66SVivien Didelot 
335bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
336bff7b688SVivien Didelot {
337c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
338bff7b688SVivien Didelot }
339bff7b688SVivien Didelot 
340c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
341c8f0b869SBen Hutchings {
342c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
343c8f0b869SBen Hutchings }
344c8f0b869SBen Hutchings 
34560045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
34660045cbfSAndrew Lunn {
347c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
34860045cbfSAndrew Lunn }
34960045cbfSAndrew Lunn 
3502b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
3516cd456f3SVivien Didelot {
352c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
3536cd456f3SVivien Didelot }
3546cd456f3SVivien Didelot 
35502bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
35602bc6e54SVivien Didelot {
357c38c5a66SVivien Didelot 	u32 mask = 0;
358c38c5a66SVivien Didelot 	int p;
35902bc6e54SVivien Didelot 
360c38c5a66SVivien Didelot 	for (p = 0; p < ds->num_ports; p++)
361c38c5a66SVivien Didelot 		if (dsa_is_user_port(ds, p))
362c38c5a66SVivien Didelot 			mask |= BIT(p);
363c38c5a66SVivien Didelot 
364c38c5a66SVivien Didelot 	return mask;
365c8652c83SVivien Didelot }
366c8652c83SVivien Didelot 
367c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
368c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
369c5f51765SVivien Didelot {
370c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
371c5f51765SVivien Didelot 	struct dsa_link *dl;
372c5f51765SVivien Didelot 
373c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
374c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
375c5f51765SVivien Didelot 			return dl->dp->index;
376c5f51765SVivien Didelot 
377c5f51765SVivien Didelot 	return ds->num_ports;
378c5f51765SVivien Didelot }
379c5f51765SVivien Didelot 
3803b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
3813b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
3823b8fac5dSVivien Didelot 					    int port)
3833b8fac5dSVivien Didelot {
3843b8fac5dSVivien Didelot 	if (device == ds->index)
3853b8fac5dSVivien Didelot 		return port;
3863b8fac5dSVivien Didelot 	else
387c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
3883b8fac5dSVivien Didelot }
3893b8fac5dSVivien Didelot 
3903b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
39107073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
392c8f0b869SBen Hutchings {
39307073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
39407073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
39507073c79SVivien Didelot 
39607073c79SVivien Didelot 	if (!cpu_dp)
39707073c79SVivien Didelot 		return port;
398c8f0b869SBen Hutchings 
3993b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
400c8f0b869SBen Hutchings }
401c8f0b869SBen Hutchings 
402cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
403cf2d45f5SVladimir Oltean {
404cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
405cf2d45f5SVladimir Oltean 
406cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
407cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
408cf2d45f5SVladimir Oltean 	else
409cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
410cf2d45f5SVladimir Oltean }
411cf2d45f5SVladimir Oltean 
4122bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
4132bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
4149d490b4eSVivien Didelot struct dsa_switch_ops {
4155ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
4164d776482SFlorian Fainelli 						  int port,
4174d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
4187b314362SAndrew Lunn 
419c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
4205e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
4216819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
422c8f0b869SBen Hutchings 
423c8f0b869SBen Hutchings 	/*
424c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
425c8f0b869SBen Hutchings 	 */
426c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
427c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
428c8f0b869SBen Hutchings 			     int regnum, u16 val);
429c8f0b869SBen Hutchings 
430c8f0b869SBen Hutchings 	/*
431ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
432ec9436baSFlorian Fainelli 	 */
433ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
434ec9436baSFlorian Fainelli 				struct phy_device *phydev);
435ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
436ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
437ec9436baSFlorian Fainelli 
438ec9436baSFlorian Fainelli 	/*
43911d8f3ddSFlorian Fainelli 	 * PHYLINK integration
44011d8f3ddSFlorian Fainelli 	 */
44111d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
44211d8f3ddSFlorian Fainelli 				    unsigned long *supported,
44311d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
44411d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
44511d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
44611d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
44711d8f3ddSFlorian Fainelli 				      unsigned int mode,
44811d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
44911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
45011d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
45111d8f3ddSFlorian Fainelli 					 unsigned int mode,
45211d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
45311d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
45411d8f3ddSFlorian Fainelli 				       unsigned int mode,
45511d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
4565b502a7bSRussell King 				       struct phy_device *phydev,
4575b502a7bSRussell King 				       int speed, int duplex,
4585b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
45911d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
46011d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
46111d8f3ddSFlorian Fainelli 	/*
462c8f0b869SBen Hutchings 	 * ethtool hardware statistics.
463c8f0b869SBen Hutchings 	 */
46489f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
46589f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
466c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
467c8f0b869SBen Hutchings 				     int port, uint64_t *data);
46889f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
469cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
470cf963573SFlorian Fainelli 					 int port, uint64_t *data);
47124462549SFlorian Fainelli 
47224462549SFlorian Fainelli 	/*
47319e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
47419e57c4eSFlorian Fainelli 	 */
47519e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
47619e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
47719e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
47819e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
47919e57c4eSFlorian Fainelli 
48019e57c4eSFlorian Fainelli 	/*
4810336369dSBrandon Streiff 	 * ethtool timestamp info
4820336369dSBrandon Streiff 	 */
4830336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
4840336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
4850336369dSBrandon Streiff 
4860336369dSBrandon Streiff 	/*
48724462549SFlorian Fainelli 	 * Suspend and resume
48824462549SFlorian Fainelli 	 */
48924462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
49024462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
491b2f2af21SFlorian Fainelli 
492b2f2af21SFlorian Fainelli 	/*
493b2f2af21SFlorian Fainelli 	 * Port enable/disable
494b2f2af21SFlorian Fainelli 	 */
495b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
496b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
49775104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
4987905288fSFlorian Fainelli 
4997905288fSFlorian Fainelli 	/*
50008f50061SVivien Didelot 	 * Port's MAC EEE settings
5017905288fSFlorian Fainelli 	 */
50208f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
5037905288fSFlorian Fainelli 			       struct ethtool_eee *e);
50408f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
5057905288fSFlorian Fainelli 			       struct ethtool_eee *e);
50651579c3fSGuenter Roeck 
5076793abb4SGuenter Roeck 	/* EEPROM access */
5086793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
5096793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
5106793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
5116793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
5126793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
5133d762a0fSGuenter Roeck 
5143d762a0fSGuenter Roeck 	/*
5153d762a0fSGuenter Roeck 	 * Register access.
5163d762a0fSGuenter Roeck 	 */
5173d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
5183d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
5193d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
520b73adef6SFlorian Fainelli 
521b73adef6SFlorian Fainelli 	/*
522b73adef6SFlorian Fainelli 	 * Bridge integration
523b73adef6SFlorian Fainelli 	 */
52434a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
52571327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
526a6692754SVivien Didelot 				    struct net_device *bridge);
527f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
528f123f2fbSVivien Didelot 				     struct net_device *bridge);
52943c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
530b73adef6SFlorian Fainelli 				      u8 state);
531732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
53257652796SRussell King 	int	(*port_egress_floods)(struct dsa_switch *ds, int port,
53357652796SRussell King 				      bool unicast, bool multicast);
5342a778e1bSVivien Didelot 
5352a778e1bSVivien Didelot 	/*
53611149536SVivien Didelot 	 * VLAN support
53711149536SVivien Didelot 	 */
538fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
539fb2dabadSVivien Didelot 				       bool vlan_filtering);
54076e398a6SVivien Didelot 	int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
54180e02360SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
5424d5770b3SVivien Didelot 	void (*port_vlan_add)(struct dsa_switch *ds, int port,
54380e02360SVivien Didelot 			      const struct switchdev_obj_port_vlan *vlan);
54476e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
54576e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
54611149536SVivien Didelot 	/*
5472a778e1bSVivien Didelot 	 * Forwarding database
5482a778e1bSVivien Didelot 	 */
5491b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
5506c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
5512a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
5526c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
553ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
5542bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
5558df30255SVivien Didelot 
5568df30255SVivien Didelot 	/*
5578df30255SVivien Didelot 	 * Multicast database
5588df30255SVivien Didelot 	 */
5598df30255SVivien Didelot 	int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
5603709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
5618df30255SVivien Didelot 	void (*port_mdb_add)(struct dsa_switch *ds, int port,
5623709aadcSVivien Didelot 			     const struct switchdev_obj_port_mdb *mdb);
5638df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
5648df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
565bf9f2648SFlorian Fainelli 	/*
566bf9f2648SFlorian Fainelli 	 * RXNFC
567bf9f2648SFlorian Fainelli 	 */
568bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
569bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
570bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
571bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
572f50f2127SFlorian Fainelli 
573f50f2127SFlorian Fainelli 	/*
574f50f2127SFlorian Fainelli 	 * TC integration
575f50f2127SFlorian Fainelli 	 */
576ed11bb1fSVladimir Oltean 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
577ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
578ed11bb1fSVladimir Oltean 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
579ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
580ed11bb1fSVladimir Oltean 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
581ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls, bool ingress);
582f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
583f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
584f50f2127SFlorian Fainelli 				   bool ingress);
585f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
586f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
58734297176SVladimir Oltean 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
58834297176SVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer);
58934297176SVladimir Oltean 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
59047d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
59147d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
59240ef2c93SVivien Didelot 
59340ef2c93SVivien Didelot 	/*
59440ef2c93SVivien Didelot 	 * Cross-chip operations
59540ef2c93SVivien Didelot 	 */
596f66a6a69SVladimir Oltean 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
597f66a6a69SVladimir Oltean 					 int sw_index, int port,
598f66a6a69SVladimir Oltean 					 struct net_device *br);
599f66a6a69SVladimir Oltean 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
600f66a6a69SVladimir Oltean 					  int sw_index, int port,
601f66a6a69SVladimir Oltean 					  struct net_device *br);
6020336369dSBrandon Streiff 
6030336369dSBrandon Streiff 	/*
6040336369dSBrandon Streiff 	 * PTP functionality
6050336369dSBrandon Streiff 	 */
6060336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
6070336369dSBrandon Streiff 				     struct ifreq *ifr);
6080336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
6090336369dSBrandon Streiff 				     struct ifreq *ifr);
61090af1059SBrandon Streiff 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
61190af1059SBrandon Streiff 				 struct sk_buff *clone, unsigned int type);
61290af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
61390af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
61497a69a0dSVladimir Oltean 
6156b297524SAndrew Lunn 	/* Devlink parameters */
6166b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
6176b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
6186b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
6196b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
620bfcb8132SVladimir Oltean 
621bfcb8132SVladimir Oltean 	/*
622bfcb8132SVladimir Oltean 	 * MTU change functionality. Switches can also adjust their MRU through
623bfcb8132SVladimir Oltean 	 * this method. By MTU, one understands the SDU (L2 payload) length.
624bfcb8132SVladimir Oltean 	 * If the switch needs to account for the DSA tag on the CPU port, this
625ab88d64aSRandy Dunlap 	 * method needs to do so privately.
626bfcb8132SVladimir Oltean 	 */
627bfcb8132SVladimir Oltean 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
628bfcb8132SVladimir Oltean 				   int new_mtu);
629bfcb8132SVladimir Oltean 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
6306b297524SAndrew Lunn };
6316b297524SAndrew Lunn 
6326b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
6336b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
6346b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
6356b297524SAndrew Lunn 
6366b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
6376b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
6386b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
6396b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
6406b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
6416b297524SAndrew Lunn 				const struct devlink_param *params,
6426b297524SAndrew Lunn 				size_t params_count);
6436b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
6446b297524SAndrew Lunn 				   const struct devlink_param *params,
6456b297524SAndrew Lunn 				   size_t params_count);
6465cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
6475cd73fbdSAndrew Lunn 				  const char *resource_name,
6485cd73fbdSAndrew Lunn 				  u64 resource_size,
6495cd73fbdSAndrew Lunn 				  u64 resource_id,
6505cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
6515cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
6525cd73fbdSAndrew Lunn 
6535cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
6545cd73fbdSAndrew Lunn 
6555cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
6565cd73fbdSAndrew Lunn 					   u64 resource_id,
6575cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
6585cd73fbdSAndrew Lunn 					   void *occ_get_priv);
6595cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
6605cd73fbdSAndrew Lunn 					     u64 resource_id);
66197c82c23SAndrew Lunn struct devlink_region *
66297c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds,
66397c82c23SAndrew Lunn 			  const struct devlink_region_ops *ops,
66497c82c23SAndrew Lunn 			  u32 region_max_snapshots, u64 region_size);
66597c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region);
66697c82c23SAndrew Lunn 
667e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
6685cd73fbdSAndrew Lunn 
6696b297524SAndrew Lunn struct dsa_devlink_priv {
6706b297524SAndrew Lunn 	struct dsa_switch *ds;
671c8f0b869SBen Hutchings };
672c8f0b869SBen Hutchings 
673ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
674ccc3e6b0SAndrew Lunn {
675ccc3e6b0SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
676ccc3e6b0SAndrew Lunn 
677ccc3e6b0SAndrew Lunn 	return dl_priv->ds;
678ccc3e6b0SAndrew Lunn }
679ccc3e6b0SAndrew Lunn 
680ab3d408dSFlorian Fainelli struct dsa_switch_driver {
681ab3d408dSFlorian Fainelli 	struct list_head	list;
682a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
683ab3d408dSFlorian Fainelli };
684ab3d408dSFlorian Fainelli 
68514b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
686c8f0b869SBen Hutchings 
68773a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
6889eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev)
689c6e970a0SAndrew Lunn {
690c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
691717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
692c6e970a0SAndrew Lunn #endif
693c6e970a0SAndrew Lunn 	return false;
694c6e970a0SAndrew Lunn }
695c6e970a0SAndrew Lunn 
696cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb,
697cc1939e4SVladimir Oltean 				  struct net_device *dev)
698cc1939e4SVladimir Oltean {
699cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
700cc1939e4SVladimir Oltean 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
701cc1939e4SVladimir Oltean #endif
702cc1939e4SVladimir Oltean 	return false;
703cc1939e4SVladimir Oltean }
704cc1939e4SVladimir Oltean 
7054cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
7064cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev)
7074cfab356SFlorian Fainelli {
7084cfab356SFlorian Fainelli 	int err = -EOPNOTSUPP;
7094cfab356SFlorian Fainelli 
7104cfab356SFlorian Fainelli 	if (!dev->dsa_ptr)
7114cfab356SFlorian Fainelli 		return err;
7124cfab356SFlorian Fainelli 
7134cfab356SFlorian Fainelli 	if (!dev->dsa_ptr->netdev_ops)
7144cfab356SFlorian Fainelli 		return err;
7154cfab356SFlorian Fainelli 
7164cfab356SFlorian Fainelli 	return 0;
7174cfab356SFlorian Fainelli }
7184cfab356SFlorian Fainelli 
7194cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
7204cfab356SFlorian Fainelli 				   int cmd)
7214cfab356SFlorian Fainelli {
7224cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *ops;
7234cfab356SFlorian Fainelli 	int err;
7244cfab356SFlorian Fainelli 
7254cfab356SFlorian Fainelli 	err = __dsa_netdevice_ops_check(dev);
7264cfab356SFlorian Fainelli 	if (err)
7274cfab356SFlorian Fainelli 		return err;
7284cfab356SFlorian Fainelli 
7294cfab356SFlorian Fainelli 	ops = dev->dsa_ptr->netdev_ops;
7304cfab356SFlorian Fainelli 
7314cfab356SFlorian Fainelli 	return ops->ndo_do_ioctl(dev, ifr, cmd);
7324cfab356SFlorian Fainelli }
7334cfab356SFlorian Fainelli #else
7344cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
7354cfab356SFlorian Fainelli 				   int cmd)
7364cfab356SFlorian Fainelli {
7374cfab356SFlorian Fainelli 	return -EOPNOTSUPP;
7384cfab356SFlorian Fainelli }
7394cfab356SFlorian Fainelli #endif
7404cfab356SFlorian Fainelli 
74183c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
74223c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
7433b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
744ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
745ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
746ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
747ea825e70SFlorian Fainelli #else
748ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
749ea825e70SFlorian Fainelli {
750ea825e70SFlorian Fainelli 	return 0;
751ea825e70SFlorian Fainelli }
752ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
753ea825e70SFlorian Fainelli {
754ea825e70SFlorian Fainelli 	return 0;
755ea825e70SFlorian Fainelli }
756ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
757ea825e70SFlorian Fainelli 
75860724d4bSFlorian Fainelli enum dsa_notifier_type {
75960724d4bSFlorian Fainelli 	DSA_PORT_REGISTER,
76060724d4bSFlorian Fainelli 	DSA_PORT_UNREGISTER,
76160724d4bSFlorian Fainelli };
76260724d4bSFlorian Fainelli 
76360724d4bSFlorian Fainelli struct dsa_notifier_info {
76460724d4bSFlorian Fainelli 	struct net_device *dev;
76560724d4bSFlorian Fainelli };
76660724d4bSFlorian Fainelli 
76760724d4bSFlorian Fainelli struct dsa_notifier_register_info {
76860724d4bSFlorian Fainelli 	struct dsa_notifier_info info;	/* must be first */
76960724d4bSFlorian Fainelli 	struct net_device *master;
77060724d4bSFlorian Fainelli 	unsigned int port_number;
77160724d4bSFlorian Fainelli 	unsigned int switch_number;
77260724d4bSFlorian Fainelli };
77360724d4bSFlorian Fainelli 
77460724d4bSFlorian Fainelli static inline struct net_device *
77560724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
77660724d4bSFlorian Fainelli {
77760724d4bSFlorian Fainelli 	return info->dev;
77860724d4bSFlorian Fainelli }
77960724d4bSFlorian Fainelli 
78060724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
78160724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb);
78260724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb);
78360724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev,
78460724d4bSFlorian Fainelli 		       struct dsa_notifier_info *info);
78560724d4bSFlorian Fainelli #else
78660724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb)
78760724d4bSFlorian Fainelli {
78860724d4bSFlorian Fainelli 	return 0;
78960724d4bSFlorian Fainelli }
79060724d4bSFlorian Fainelli 
79160724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb)
79260724d4bSFlorian Fainelli {
79360724d4bSFlorian Fainelli 	return 0;
79460724d4bSFlorian Fainelli }
79560724d4bSFlorian Fainelli 
79660724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
79760724d4bSFlorian Fainelli 				     struct dsa_notifier_info *info)
79860724d4bSFlorian Fainelli {
79960724d4bSFlorian Fainelli 	return NOTIFY_DONE;
80060724d4bSFlorian Fainelli }
80160724d4bSFlorian Fainelli #endif
80260724d4bSFlorian Fainelli 
8030a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */
8040a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q)	((p) << 8 | q)
8050a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v)		((v) >> 8)
8060a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v)		((v) & 0xff)
8070a5f14ceSFlorian Fainelli 
808cf963573SFlorian Fainelli 
80997a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
810cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
811cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
812cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
81311d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
814cf963573SFlorian Fainelli 
815d3b8c049SAndrew Lunn struct dsa_tag_driver {
816d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
817d3b8c049SAndrew Lunn 	struct list_head list;
818d3b8c049SAndrew Lunn 	struct module *owner;
819d3b8c049SAndrew Lunn };
820d3b8c049SAndrew Lunn 
821d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
822d3b8c049SAndrew Lunn 			      unsigned int count,
823d3b8c049SAndrew Lunn 			      struct module *owner);
824d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
825d3b8c049SAndrew Lunn 				unsigned int count);
826d3b8c049SAndrew Lunn 
827d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
828d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
829d3b8c049SAndrew Lunn {									\
830d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
831d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
832d3b8c049SAndrew Lunn 	return 0;							\
833d3b8c049SAndrew Lunn }									\
834d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
835d3b8c049SAndrew Lunn 									\
836d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
837d3b8c049SAndrew Lunn {									\
838d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
839d3b8c049SAndrew Lunn }									\
840d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
841d3b8c049SAndrew Lunn 
842d3b8c049SAndrew Lunn /**
843d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
844d3b8c049SAndrew Lunn  * drivers
845d3b8c049SAndrew Lunn  * @__ops_array: Array of tag driver strucutres
846d3b8c049SAndrew Lunn  *
847d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
848d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
849d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
850d3b8c049SAndrew Lunn  */
851d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
852d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
853d3b8c049SAndrew Lunn 
854d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
855d3b8c049SAndrew Lunn 
856d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
857d3b8c049SAndrew Lunn  * drivers
858d3b8c049SAndrew Lunn  */
859d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
860d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
861d3b8c049SAndrew Lunn 	.ops = &__ops,							\
862d3b8c049SAndrew Lunn }
863d3b8c049SAndrew Lunn 
864d3b8c049SAndrew Lunn /**
865d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
866d3b8c049SAndrew Lunn  * driver
867d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
868d3b8c049SAndrew Lunn  *
869d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
870d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
871d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
872d3b8c049SAndrew Lunn  */
873d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
874d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
875d3b8c049SAndrew Lunn 									\
876d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
877d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
878d3b8c049SAndrew Lunn };									\
879d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
88091da11f8SLennert Buytenhek #endif
881d3b8c049SAndrew Lunn 
882