xref: /linux/include/net/dsa.h (revision 5b502a7b2992008a1fd5962ba032771b03c4e840)
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
470b42f033SAndrew Lunn 
48ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
490b42f033SAndrew Lunn 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
500b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
510b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
520b42f033SAndrew Lunn 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
530b42f033SAndrew Lunn 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
540b42f033SAndrew Lunn 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
550b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
560b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
570b42f033SAndrew Lunn 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
580b42f033SAndrew Lunn 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
590b42f033SAndrew Lunn 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
600b42f033SAndrew Lunn 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
61f9bbe447SVladimir Oltean 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
62227d07a0SVladimir Oltean 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
63016e43a2STristram Ha 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
648dce89aaSVladimir Oltean 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
6548fda74fSOleksij Rempel 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
66ac7a04c3SFlorian Fainelli };
675037d532SFlorian Fainelli 
685075314eSAlexander Duyck struct packet_type;
6990af1059SBrandon Streiff struct dsa_switch;
703e8a72d1SFlorian Fainelli 
7168277a2cSJohn Crispin struct dsa_device_ops {
7268277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
7368277a2cSJohn Crispin 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
7489e49506SFlorian Westphal 			       struct packet_type *pt);
75598a9680SJohn Crispin 	int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
76598a9680SJohn Crispin 			    int *offset);
77cc1939e4SVladimir Oltean 	/* Used to determine which traffic should match the DSA filter in
78cc1939e4SVladimir Oltean 	 * eth_type_trans, and which, if any, should bypass it and be processed
79cc1939e4SVladimir Oltean 	 * as regular on the master net device.
80cc1939e4SVladimir Oltean 	 */
81cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
82a5dd3087SAndrew Lunn 	unsigned int overhead;
83875138f8SAndrew Lunn 	const char *name;
84056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
8568277a2cSJohn Crispin };
8668277a2cSJohn Crispin 
870b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
880b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
890b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
900b42f033SAndrew Lunn 
91b68b0dd0SVladimir Oltean struct dsa_skb_cb {
92b68b0dd0SVladimir Oltean 	struct sk_buff *clone;
93b68b0dd0SVladimir Oltean };
94b68b0dd0SVladimir Oltean 
95b68b0dd0SVladimir Oltean struct __dsa_skb_cb {
96b68b0dd0SVladimir Oltean 	struct dsa_skb_cb cb;
97b68b0dd0SVladimir Oltean 	u8 priv[48 - sizeof(struct dsa_skb_cb)];
98b68b0dd0SVladimir Oltean };
99b68b0dd0SVladimir Oltean 
100b68b0dd0SVladimir Oltean #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
101b68b0dd0SVladimir Oltean 
102b68b0dd0SVladimir Oltean #define DSA_SKB_CB_PRIV(skb)			\
103b68b0dd0SVladimir Oltean 	((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
104b68b0dd0SVladimir Oltean 
105cf50dcc2SBen Hutchings struct dsa_switch_tree {
10683c0afaeSAndrew Lunn 	struct list_head	list;
10783c0afaeSAndrew Lunn 
108f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
109f515f192SVivien Didelot 	struct raw_notifier_head	nh;
110f515f192SVivien Didelot 
11183c0afaeSAndrew Lunn 	/* Tree identifier */
11249463b7fSVivien Didelot 	unsigned int index;
11383c0afaeSAndrew Lunn 
11483c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
11583c0afaeSAndrew Lunn 	struct kref refcount;
11683c0afaeSAndrew Lunn 
11783c0afaeSAndrew Lunn 	/* Has this tree been applied to the hardware? */
118ec15dd42SVivien Didelot 	bool setup;
11983c0afaeSAndrew Lunn 
120cf50dcc2SBen Hutchings 	/*
121cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
122cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
123cf50dcc2SBen Hutchings 	 */
124cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
125cf85d08fSLennert Buytenhek 
126ab8ccae1SVivien Didelot 	/* List of switch ports */
127ab8ccae1SVivien Didelot 	struct list_head ports;
128ab8ccae1SVivien Didelot 
129c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
130c5f51765SVivien Didelot 	struct list_head rtable;
131cf50dcc2SBen Hutchings };
132cf50dcc2SBen Hutchings 
133f50f2127SFlorian Fainelli /* TC matchall action types, only mirroring for now */
134f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
135f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
136f50f2127SFlorian Fainelli };
137f50f2127SFlorian Fainelli 
138f50f2127SFlorian Fainelli /* TC mirroring entry */
139f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
140f50f2127SFlorian Fainelli 	u8 to_local_port;
141f50f2127SFlorian Fainelli 	bool ingress;
142f50f2127SFlorian Fainelli };
143f50f2127SFlorian Fainelli 
144f50f2127SFlorian Fainelli /* TC matchall entry */
145f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
146f50f2127SFlorian Fainelli 	struct list_head list;
147f50f2127SFlorian Fainelli 	unsigned long cookie;
148f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
149f50f2127SFlorian Fainelli 	union {
150f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
151f50f2127SFlorian Fainelli 	};
152f50f2127SFlorian Fainelli };
153f50f2127SFlorian Fainelli 
154f50f2127SFlorian Fainelli 
155c8b09808SAndrew Lunn struct dsa_port {
156f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
157f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
158f8b8b1cdSVivien Didelot 	 */
159f8b8b1cdSVivien Didelot 	union {
160f8b8b1cdSVivien Didelot 		struct net_device *master;
161f8b8b1cdSVivien Didelot 		struct net_device *slave;
162f8b8b1cdSVivien Didelot 	};
163f8b8b1cdSVivien Didelot 
16415240248SVivien Didelot 	/* CPU port tagging operations used by master or slave devices */
16515240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
16615240248SVivien Didelot 
1673e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
1683e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
1693e41f93bSVivien Didelot 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
1703e41f93bSVivien Didelot 			       struct packet_type *pt);
171cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
1723e41f93bSVivien Didelot 
173057cad2cSVivien Didelot 	enum {
174057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
175057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
176057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
177057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
178057cad2cSVivien Didelot 	} type;
179057cad2cSVivien Didelot 
180818be848SVivien Didelot 	struct dsa_switch	*ds;
181818be848SVivien Didelot 	unsigned int		index;
18271e0bbdeSFlorian Fainelli 	const char		*name;
18368b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
184a2c7023fSXiaofei Shen 	const char		*mac;
185189b0d93SAndrew Lunn 	struct device_node	*dn;
18634a79f63SVivien Didelot 	unsigned int		ageing_time;
18733162e9aSVladimir Oltean 	bool			vlan_filtering;
188732f794cSVivien Didelot 	u8			stp_state;
189a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
19096567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
191aab9c406SFlorian Fainelli 	struct phylink		*pl;
19244cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
19397a69a0dSVladimir Oltean 
194ab8ccae1SVivien Didelot 	struct list_head list;
195ab8ccae1SVivien Didelot 
19667dbb9d4SFlorian Fainelli 	/*
197c362beb0SVladimir Oltean 	 * Give the switch driver somewhere to hang its per-port private data
198c362beb0SVladimir Oltean 	 * structures (accessible from the tagger).
199c362beb0SVladimir Oltean 	 */
200c362beb0SVladimir Oltean 	void *priv;
201c362beb0SVladimir Oltean 
202c362beb0SVladimir Oltean 	/*
20367dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
20467dbb9d4SFlorian Fainelli 	 */
20567dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
206da7b9e9bSFlorian Fainelli 
207da7b9e9bSFlorian Fainelli 	/*
208da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
209da7b9e9bSFlorian Fainelli 	 */
210da7b9e9bSFlorian Fainelli 	const struct net_device_ops *orig_ndo_ops;
211fb35c60cSVivien Didelot 
212fb35c60cSVivien Didelot 	bool setup;
213c8b09808SAndrew Lunn };
214c8b09808SAndrew Lunn 
215c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
216c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
217c5f51765SVivien Didelot  * but this would require some more complex tree walking,
218c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
219c5f51765SVivien Didelot  */
220c5f51765SVivien Didelot struct dsa_link {
221c5f51765SVivien Didelot 	struct dsa_port *dp;
222c5f51765SVivien Didelot 	struct dsa_port *link_dp;
223c5f51765SVivien Didelot 	struct list_head list;
224c5f51765SVivien Didelot };
225c5f51765SVivien Didelot 
226c8f0b869SBen Hutchings struct dsa_switch {
227fb35c60cSVivien Didelot 	bool setup;
228fb35c60cSVivien Didelot 
229c33063d6SAndrew Lunn 	struct device *dev;
230c33063d6SAndrew Lunn 
231c8f0b869SBen Hutchings 	/*
232c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
233c8f0b869SBen Hutchings 	 */
234c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
23599feaafcSVivien Didelot 	unsigned int		index;
236c8f0b869SBen Hutchings 
237f515f192SVivien Didelot 	/* Listener for switch fabric events */
238f515f192SVivien Didelot 	struct notifier_block	nb;
239f515f192SVivien Didelot 
240c8f0b869SBen Hutchings 	/*
2417543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
2427543a6d5SAndrew Lunn 	 * structure.
2437543a6d5SAndrew Lunn 	 */
2447543a6d5SAndrew Lunn 	void *priv;
2457543a6d5SAndrew Lunn 
2467543a6d5SAndrew Lunn 	/*
247c8f0b869SBen Hutchings 	 * Configuration data for this switch.
248c8f0b869SBen Hutchings 	 */
249ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
250c8f0b869SBen Hutchings 
251c8f0b869SBen Hutchings 	/*
2529d490b4eSVivien Didelot 	 * The switch operations.
253c8f0b869SBen Hutchings 	 */
254a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
255c8f0b869SBen Hutchings 
25666472fc0SAndrew Lunn 	/*
257c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
258c8f0b869SBen Hutchings 	 */
2590d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
260c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
261a0c02161SVivien Didelot 
2620f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
2630f3da6afSVivien Didelot 	unsigned int ageing_time_min;
2640f3da6afSVivien Didelot 	unsigned int ageing_time_max;
2650f3da6afSVivien Didelot 
26696567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
26796567d5dSAndrew Lunn 	struct devlink		*devlink;
26896567d5dSAndrew Lunn 
26955199df6SFlorian Fainelli 	/* Number of switch port queues */
27055199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
27155199df6SFlorian Fainelli 
2728f5d16f6SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
2738f5d16f6SVladimir Oltean 	 * settings on ports if not hardware-supported
2748f5d16f6SVladimir Oltean 	 */
2758f5d16f6SVladimir Oltean 	bool			vlan_filtering_is_global;
2768f5d16f6SVladimir Oltean 
27714574676SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
27814574676SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
27914574676SVladimir Oltean 	 */
28014574676SVladimir Oltean 	bool			vlan_filtering;
28114574676SVladimir Oltean 
282787cac3fSVladimir Oltean 	/* MAC PCS does not provide link state change interrupt, and requires
283787cac3fSVladimir Oltean 	 * polling. Flag passed on to PHYLINK.
284787cac3fSVladimir Oltean 	 */
285787cac3fSVladimir Oltean 	bool			pcs_poll;
286787cac3fSVladimir Oltean 
287a0c02161SVivien Didelot 	size_t num_ports;
288c8f0b869SBen Hutchings };
289c8f0b869SBen Hutchings 
29068bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
291c38c5a66SVivien Didelot {
292b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
293d607525bSVivien Didelot 	struct dsa_port *dp;
294b96ddf25SVivien Didelot 
295b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
296b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
297b96ddf25SVivien Didelot 			return dp;
298d607525bSVivien Didelot 
299d607525bSVivien Didelot 	return NULL;
300c38c5a66SVivien Didelot }
301c38c5a66SVivien Didelot 
302bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
303bff7b688SVivien Didelot {
304c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
305bff7b688SVivien Didelot }
306bff7b688SVivien Didelot 
307c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
308c8f0b869SBen Hutchings {
309c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
310c8f0b869SBen Hutchings }
311c8f0b869SBen Hutchings 
31260045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
31360045cbfSAndrew Lunn {
314c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
31560045cbfSAndrew Lunn }
31660045cbfSAndrew Lunn 
3172b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
3186cd456f3SVivien Didelot {
319c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
3206cd456f3SVivien Didelot }
3216cd456f3SVivien Didelot 
32202bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
32302bc6e54SVivien Didelot {
324c38c5a66SVivien Didelot 	u32 mask = 0;
325c38c5a66SVivien Didelot 	int p;
32602bc6e54SVivien Didelot 
327c38c5a66SVivien Didelot 	for (p = 0; p < ds->num_ports; p++)
328c38c5a66SVivien Didelot 		if (dsa_is_user_port(ds, p))
329c38c5a66SVivien Didelot 			mask |= BIT(p);
330c38c5a66SVivien Didelot 
331c38c5a66SVivien Didelot 	return mask;
332c8652c83SVivien Didelot }
333c8652c83SVivien Didelot 
334c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
335c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
336c5f51765SVivien Didelot {
337c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
338c5f51765SVivien Didelot 	struct dsa_link *dl;
339c5f51765SVivien Didelot 
340c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
341c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
342c5f51765SVivien Didelot 			return dl->dp->index;
343c5f51765SVivien Didelot 
344c5f51765SVivien Didelot 	return ds->num_ports;
345c5f51765SVivien Didelot }
346c5f51765SVivien Didelot 
3473b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
3483b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
3493b8fac5dSVivien Didelot 					    int port)
3503b8fac5dSVivien Didelot {
3513b8fac5dSVivien Didelot 	if (device == ds->index)
3523b8fac5dSVivien Didelot 		return port;
3533b8fac5dSVivien Didelot 	else
354c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
3553b8fac5dSVivien Didelot }
3563b8fac5dSVivien Didelot 
3573b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
35807073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
359c8f0b869SBen Hutchings {
36007073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
36107073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
36207073c79SVivien Didelot 
36307073c79SVivien Didelot 	if (!cpu_dp)
36407073c79SVivien Didelot 		return port;
365c8f0b869SBen Hutchings 
3663b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
367c8f0b869SBen Hutchings }
368c8f0b869SBen Hutchings 
369cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
370cf2d45f5SVladimir Oltean {
371cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
372cf2d45f5SVladimir Oltean 
373cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
374cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
375cf2d45f5SVladimir Oltean 	else
376cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
377cf2d45f5SVladimir Oltean }
378cf2d45f5SVladimir Oltean 
3792bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
3802bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
3819d490b4eSVivien Didelot struct dsa_switch_ops {
3825ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
3834d776482SFlorian Fainelli 						  int port,
3844d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
3857b314362SAndrew Lunn 
386c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
3875e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
3886819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
389c8f0b869SBen Hutchings 
390c8f0b869SBen Hutchings 	/*
391c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
392c8f0b869SBen Hutchings 	 */
393c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
394c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
395c8f0b869SBen Hutchings 			     int regnum, u16 val);
396c8f0b869SBen Hutchings 
397c8f0b869SBen Hutchings 	/*
398ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
399ec9436baSFlorian Fainelli 	 */
400ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
401ec9436baSFlorian Fainelli 				struct phy_device *phydev);
402ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
403ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
404ec9436baSFlorian Fainelli 
405ec9436baSFlorian Fainelli 	/*
40611d8f3ddSFlorian Fainelli 	 * PHYLINK integration
40711d8f3ddSFlorian Fainelli 	 */
40811d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
40911d8f3ddSFlorian Fainelli 				    unsigned long *supported,
41011d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
41111d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
41211d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
41311d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
41411d8f3ddSFlorian Fainelli 				      unsigned int mode,
41511d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
41611d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
41711d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
41811d8f3ddSFlorian Fainelli 					 unsigned int mode,
41911d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
42011d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
42111d8f3ddSFlorian Fainelli 				       unsigned int mode,
42211d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
423*5b502a7bSRussell King 				       struct phy_device *phydev,
424*5b502a7bSRussell King 				       int speed, int duplex,
425*5b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
42611d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
42711d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
42811d8f3ddSFlorian Fainelli 	/*
429c8f0b869SBen Hutchings 	 * ethtool hardware statistics.
430c8f0b869SBen Hutchings 	 */
43189f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
43289f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
433c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
434c8f0b869SBen Hutchings 				     int port, uint64_t *data);
43589f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
436cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
437cf963573SFlorian Fainelli 					 int port, uint64_t *data);
43824462549SFlorian Fainelli 
43924462549SFlorian Fainelli 	/*
44019e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
44119e57c4eSFlorian Fainelli 	 */
44219e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
44319e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
44419e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
44519e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
44619e57c4eSFlorian Fainelli 
44719e57c4eSFlorian Fainelli 	/*
4480336369dSBrandon Streiff 	 * ethtool timestamp info
4490336369dSBrandon Streiff 	 */
4500336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
4510336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
4520336369dSBrandon Streiff 
4530336369dSBrandon Streiff 	/*
45424462549SFlorian Fainelli 	 * Suspend and resume
45524462549SFlorian Fainelli 	 */
45624462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
45724462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
458b2f2af21SFlorian Fainelli 
459b2f2af21SFlorian Fainelli 	/*
460b2f2af21SFlorian Fainelli 	 * Port enable/disable
461b2f2af21SFlorian Fainelli 	 */
462b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
463b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
46475104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
4657905288fSFlorian Fainelli 
4667905288fSFlorian Fainelli 	/*
46708f50061SVivien Didelot 	 * Port's MAC EEE settings
4687905288fSFlorian Fainelli 	 */
46908f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
4707905288fSFlorian Fainelli 			       struct ethtool_eee *e);
47108f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
4727905288fSFlorian Fainelli 			       struct ethtool_eee *e);
47351579c3fSGuenter Roeck 
4746793abb4SGuenter Roeck 	/* EEPROM access */
4756793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
4766793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
4776793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4786793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
4796793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4803d762a0fSGuenter Roeck 
4813d762a0fSGuenter Roeck 	/*
4823d762a0fSGuenter Roeck 	 * Register access.
4833d762a0fSGuenter Roeck 	 */
4843d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
4853d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
4863d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
487b73adef6SFlorian Fainelli 
488b73adef6SFlorian Fainelli 	/*
489b73adef6SFlorian Fainelli 	 * Bridge integration
490b73adef6SFlorian Fainelli 	 */
49134a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
49271327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
493a6692754SVivien Didelot 				    struct net_device *bridge);
494f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
495f123f2fbSVivien Didelot 				     struct net_device *bridge);
49643c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
497b73adef6SFlorian Fainelli 				      u8 state);
498732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
49957652796SRussell King 	int	(*port_egress_floods)(struct dsa_switch *ds, int port,
50057652796SRussell King 				      bool unicast, bool multicast);
5012a778e1bSVivien Didelot 
5022a778e1bSVivien Didelot 	/*
50311149536SVivien Didelot 	 * VLAN support
50411149536SVivien Didelot 	 */
505fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
506fb2dabadSVivien Didelot 				       bool vlan_filtering);
50776e398a6SVivien Didelot 	int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
50880e02360SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
5094d5770b3SVivien Didelot 	void (*port_vlan_add)(struct dsa_switch *ds, int port,
51080e02360SVivien Didelot 			      const struct switchdev_obj_port_vlan *vlan);
51176e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
51276e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
51311149536SVivien Didelot 	/*
5142a778e1bSVivien Didelot 	 * Forwarding database
5152a778e1bSVivien Didelot 	 */
5161b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
5176c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
5182a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
5196c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
520ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
5212bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
5228df30255SVivien Didelot 
5238df30255SVivien Didelot 	/*
5248df30255SVivien Didelot 	 * Multicast database
5258df30255SVivien Didelot 	 */
5268df30255SVivien Didelot 	int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
5273709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
5288df30255SVivien Didelot 	void (*port_mdb_add)(struct dsa_switch *ds, int port,
5293709aadcSVivien Didelot 			     const struct switchdev_obj_port_mdb *mdb);
5308df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
5318df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
532bf9f2648SFlorian Fainelli 	/*
533bf9f2648SFlorian Fainelli 	 * RXNFC
534bf9f2648SFlorian Fainelli 	 */
535bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
536bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
537bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
538bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
539f50f2127SFlorian Fainelli 
540f50f2127SFlorian Fainelli 	/*
541f50f2127SFlorian Fainelli 	 * TC integration
542f50f2127SFlorian Fainelli 	 */
543f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
544f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
545f50f2127SFlorian Fainelli 				   bool ingress);
546f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
547f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
54847d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
54947d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
55040ef2c93SVivien Didelot 
55140ef2c93SVivien Didelot 	/*
55240ef2c93SVivien Didelot 	 * Cross-chip operations
55340ef2c93SVivien Didelot 	 */
55440ef2c93SVivien Didelot 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
55540ef2c93SVivien Didelot 					 int port, struct net_device *br);
55640ef2c93SVivien Didelot 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
55740ef2c93SVivien Didelot 					  int port, struct net_device *br);
5580336369dSBrandon Streiff 
5590336369dSBrandon Streiff 	/*
5600336369dSBrandon Streiff 	 * PTP functionality
5610336369dSBrandon Streiff 	 */
5620336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
5630336369dSBrandon Streiff 				     struct ifreq *ifr);
5640336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
5650336369dSBrandon Streiff 				     struct ifreq *ifr);
56690af1059SBrandon Streiff 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
56790af1059SBrandon Streiff 				 struct sk_buff *clone, unsigned int type);
56890af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
56990af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
57097a69a0dSVladimir Oltean 
5716b297524SAndrew Lunn 	/* Devlink parameters */
5726b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
5736b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
5746b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
5756b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
5766b297524SAndrew Lunn };
5776b297524SAndrew Lunn 
5786b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
5796b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
5806b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
5816b297524SAndrew Lunn 
5826b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
5836b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
5846b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
5856b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
5866b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
5876b297524SAndrew Lunn 				const struct devlink_param *params,
5886b297524SAndrew Lunn 				size_t params_count);
5896b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
5906b297524SAndrew Lunn 				   const struct devlink_param *params,
5916b297524SAndrew Lunn 				   size_t params_count);
5925cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
5935cd73fbdSAndrew Lunn 				  const char *resource_name,
5945cd73fbdSAndrew Lunn 				  u64 resource_size,
5955cd73fbdSAndrew Lunn 				  u64 resource_id,
5965cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
5975cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
5985cd73fbdSAndrew Lunn 
5995cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
6005cd73fbdSAndrew Lunn 
6015cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
6025cd73fbdSAndrew Lunn 					   u64 resource_id,
6035cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
6045cd73fbdSAndrew Lunn 					   void *occ_get_priv);
6055cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
6065cd73fbdSAndrew Lunn 					     u64 resource_id);
6075cd73fbdSAndrew Lunn 
6086b297524SAndrew Lunn struct dsa_devlink_priv {
6096b297524SAndrew Lunn 	struct dsa_switch *ds;
610c8f0b869SBen Hutchings };
611c8f0b869SBen Hutchings 
612ab3d408dSFlorian Fainelli struct dsa_switch_driver {
613ab3d408dSFlorian Fainelli 	struct list_head	list;
614a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
615ab3d408dSFlorian Fainelli };
616ab3d408dSFlorian Fainelli 
61714b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
618c8f0b869SBen Hutchings 
61973a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
620c6e970a0SAndrew Lunn static inline bool netdev_uses_dsa(struct net_device *dev)
621c6e970a0SAndrew Lunn {
622c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
623717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
624c6e970a0SAndrew Lunn #endif
625c6e970a0SAndrew Lunn 	return false;
626c6e970a0SAndrew Lunn }
627c6e970a0SAndrew Lunn 
628cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb,
629cc1939e4SVladimir Oltean 				  struct net_device *dev)
630cc1939e4SVladimir Oltean {
631cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
632cc1939e4SVladimir Oltean 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
633cc1939e4SVladimir Oltean #endif
634cc1939e4SVladimir Oltean 	return false;
635cc1939e4SVladimir Oltean }
636cc1939e4SVladimir Oltean 
63783c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
63823c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
639ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
640ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
641ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
642ea825e70SFlorian Fainelli #else
643ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
644ea825e70SFlorian Fainelli {
645ea825e70SFlorian Fainelli 	return 0;
646ea825e70SFlorian Fainelli }
647ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
648ea825e70SFlorian Fainelli {
649ea825e70SFlorian Fainelli 	return 0;
650ea825e70SFlorian Fainelli }
651ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
652ea825e70SFlorian Fainelli 
65360724d4bSFlorian Fainelli enum dsa_notifier_type {
65460724d4bSFlorian Fainelli 	DSA_PORT_REGISTER,
65560724d4bSFlorian Fainelli 	DSA_PORT_UNREGISTER,
65660724d4bSFlorian Fainelli };
65760724d4bSFlorian Fainelli 
65860724d4bSFlorian Fainelli struct dsa_notifier_info {
65960724d4bSFlorian Fainelli 	struct net_device *dev;
66060724d4bSFlorian Fainelli };
66160724d4bSFlorian Fainelli 
66260724d4bSFlorian Fainelli struct dsa_notifier_register_info {
66360724d4bSFlorian Fainelli 	struct dsa_notifier_info info;	/* must be first */
66460724d4bSFlorian Fainelli 	struct net_device *master;
66560724d4bSFlorian Fainelli 	unsigned int port_number;
66660724d4bSFlorian Fainelli 	unsigned int switch_number;
66760724d4bSFlorian Fainelli };
66860724d4bSFlorian Fainelli 
66960724d4bSFlorian Fainelli static inline struct net_device *
67060724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
67160724d4bSFlorian Fainelli {
67260724d4bSFlorian Fainelli 	return info->dev;
67360724d4bSFlorian Fainelli }
67460724d4bSFlorian Fainelli 
67560724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
67660724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb);
67760724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb);
67860724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev,
67960724d4bSFlorian Fainelli 		       struct dsa_notifier_info *info);
68060724d4bSFlorian Fainelli #else
68160724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb)
68260724d4bSFlorian Fainelli {
68360724d4bSFlorian Fainelli 	return 0;
68460724d4bSFlorian Fainelli }
68560724d4bSFlorian Fainelli 
68660724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb)
68760724d4bSFlorian Fainelli {
68860724d4bSFlorian Fainelli 	return 0;
68960724d4bSFlorian Fainelli }
69060724d4bSFlorian Fainelli 
69160724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
69260724d4bSFlorian Fainelli 				     struct dsa_notifier_info *info)
69360724d4bSFlorian Fainelli {
69460724d4bSFlorian Fainelli 	return NOTIFY_DONE;
69560724d4bSFlorian Fainelli }
69660724d4bSFlorian Fainelli #endif
69760724d4bSFlorian Fainelli 
6980a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */
6990a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q)	((p) << 8 | q)
7000a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v)		((v) >> 8)
7010a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v)		((v) & 0xff)
7020a5f14ceSFlorian Fainelli 
703cf963573SFlorian Fainelli 
70497a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
705cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
706cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
707cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
70811d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
709cf963573SFlorian Fainelli 
710d3b8c049SAndrew Lunn struct dsa_tag_driver {
711d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
712d3b8c049SAndrew Lunn 	struct list_head list;
713d3b8c049SAndrew Lunn 	struct module *owner;
714d3b8c049SAndrew Lunn };
715d3b8c049SAndrew Lunn 
716d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
717d3b8c049SAndrew Lunn 			      unsigned int count,
718d3b8c049SAndrew Lunn 			      struct module *owner);
719d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
720d3b8c049SAndrew Lunn 				unsigned int count);
721d3b8c049SAndrew Lunn 
722d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
723d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
724d3b8c049SAndrew Lunn {									\
725d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
726d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
727d3b8c049SAndrew Lunn 	return 0;							\
728d3b8c049SAndrew Lunn }									\
729d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
730d3b8c049SAndrew Lunn 									\
731d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
732d3b8c049SAndrew Lunn {									\
733d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
734d3b8c049SAndrew Lunn }									\
735d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
736d3b8c049SAndrew Lunn 
737d3b8c049SAndrew Lunn /**
738d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
739d3b8c049SAndrew Lunn  * drivers
740d3b8c049SAndrew Lunn  * @__ops_array: Array of tag driver strucutres
741d3b8c049SAndrew Lunn  *
742d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
743d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
744d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
745d3b8c049SAndrew Lunn  */
746d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
747d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
748d3b8c049SAndrew Lunn 
749d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
750d3b8c049SAndrew Lunn 
751d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
752d3b8c049SAndrew Lunn  * drivers
753d3b8c049SAndrew Lunn  */
754d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
755d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
756d3b8c049SAndrew Lunn 	.ops = &__ops,							\
757d3b8c049SAndrew Lunn }
758d3b8c049SAndrew Lunn 
759d3b8c049SAndrew Lunn /**
760d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
761d3b8c049SAndrew Lunn  * driver
762d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
763d3b8c049SAndrew Lunn  *
764d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
765d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
766d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
767d3b8c049SAndrew Lunn  */
768d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
769d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
770d3b8c049SAndrew Lunn 									\
771d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
772d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
773d3b8c049SAndrew Lunn };									\
774d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
77591da11f8SLennert Buytenhek #endif
776d3b8c049SAndrew Lunn 
777