xref: /openbmc/linux/include/net/dsa.h (revision c5f51765)
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
450b42f033SAndrew Lunn 
46ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
470b42f033SAndrew Lunn 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
480b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
490b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
500b42f033SAndrew Lunn 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
510b42f033SAndrew Lunn 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
520b42f033SAndrew Lunn 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
530b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
540b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
550b42f033SAndrew Lunn 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
560b42f033SAndrew Lunn 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
570b42f033SAndrew Lunn 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
580b42f033SAndrew Lunn 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
59f9bbe447SVladimir Oltean 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
60227d07a0SVladimir Oltean 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
61016e43a2STristram Ha 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
62ac7a04c3SFlorian Fainelli };
635037d532SFlorian Fainelli 
645075314eSAlexander Duyck struct packet_type;
6590af1059SBrandon Streiff struct dsa_switch;
663e8a72d1SFlorian Fainelli 
6768277a2cSJohn Crispin struct dsa_device_ops {
6868277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
6968277a2cSJohn Crispin 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
7089e49506SFlorian Westphal 			       struct packet_type *pt);
71598a9680SJohn Crispin 	int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
72598a9680SJohn Crispin 			    int *offset);
73cc1939e4SVladimir Oltean 	/* Used to determine which traffic should match the DSA filter in
74cc1939e4SVladimir Oltean 	 * eth_type_trans, and which, if any, should bypass it and be processed
75cc1939e4SVladimir Oltean 	 * as regular on the master net device.
76cc1939e4SVladimir Oltean 	 */
77cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
78a5dd3087SAndrew Lunn 	unsigned int overhead;
79875138f8SAndrew Lunn 	const char *name;
80056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
8168277a2cSJohn Crispin };
8268277a2cSJohn Crispin 
830b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
840b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
850b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
860b42f033SAndrew Lunn 
87b68b0dd0SVladimir Oltean struct dsa_skb_cb {
88b68b0dd0SVladimir Oltean 	struct sk_buff *clone;
8997a69a0dSVladimir Oltean 	bool deferred_xmit;
90b68b0dd0SVladimir Oltean };
91b68b0dd0SVladimir Oltean 
92b68b0dd0SVladimir Oltean struct __dsa_skb_cb {
93b68b0dd0SVladimir Oltean 	struct dsa_skb_cb cb;
94b68b0dd0SVladimir Oltean 	u8 priv[48 - sizeof(struct dsa_skb_cb)];
95b68b0dd0SVladimir Oltean };
96b68b0dd0SVladimir Oltean 
97b68b0dd0SVladimir Oltean #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
98b68b0dd0SVladimir Oltean 
99b68b0dd0SVladimir Oltean #define DSA_SKB_CB_PRIV(skb)			\
100b68b0dd0SVladimir Oltean 	((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
101b68b0dd0SVladimir Oltean 
102cf50dcc2SBen Hutchings struct dsa_switch_tree {
10383c0afaeSAndrew Lunn 	struct list_head	list;
10483c0afaeSAndrew Lunn 
105f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
106f515f192SVivien Didelot 	struct raw_notifier_head	nh;
107f515f192SVivien Didelot 
10883c0afaeSAndrew Lunn 	/* Tree identifier */
10949463b7fSVivien Didelot 	unsigned int index;
11083c0afaeSAndrew Lunn 
11183c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
11283c0afaeSAndrew Lunn 	struct kref refcount;
11383c0afaeSAndrew Lunn 
11483c0afaeSAndrew Lunn 	/* Has this tree been applied to the hardware? */
115ec15dd42SVivien Didelot 	bool setup;
11683c0afaeSAndrew Lunn 
117cf50dcc2SBen Hutchings 	/*
118cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
119cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
120cf50dcc2SBen Hutchings 	 */
121cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
122cf85d08fSLennert Buytenhek 
123ab8ccae1SVivien Didelot 	/* List of switch ports */
124ab8ccae1SVivien Didelot 	struct list_head ports;
125ab8ccae1SVivien Didelot 
126c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
127c5f51765SVivien Didelot 	struct list_head rtable;
128c5f51765SVivien Didelot 
129cf50dcc2SBen Hutchings 	/*
130cf50dcc2SBen Hutchings 	 * Data for the individual switch chips.
131cf50dcc2SBen Hutchings 	 */
132cf50dcc2SBen Hutchings 	struct dsa_switch	*ds[DSA_MAX_SWITCHES];
133cf50dcc2SBen Hutchings };
134cf50dcc2SBen Hutchings 
135f50f2127SFlorian Fainelli /* TC matchall action types, only mirroring for now */
136f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
137f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
138f50f2127SFlorian Fainelli };
139f50f2127SFlorian Fainelli 
140f50f2127SFlorian Fainelli /* TC mirroring entry */
141f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
142f50f2127SFlorian Fainelli 	u8 to_local_port;
143f50f2127SFlorian Fainelli 	bool ingress;
144f50f2127SFlorian Fainelli };
145f50f2127SFlorian Fainelli 
146f50f2127SFlorian Fainelli /* TC matchall entry */
147f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
148f50f2127SFlorian Fainelli 	struct list_head list;
149f50f2127SFlorian Fainelli 	unsigned long cookie;
150f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
151f50f2127SFlorian Fainelli 	union {
152f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
153f50f2127SFlorian Fainelli 	};
154f50f2127SFlorian Fainelli };
155f50f2127SFlorian Fainelli 
156f50f2127SFlorian Fainelli 
157c8b09808SAndrew Lunn struct dsa_port {
158f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
159f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
160f8b8b1cdSVivien Didelot 	 */
161f8b8b1cdSVivien Didelot 	union {
162f8b8b1cdSVivien Didelot 		struct net_device *master;
163f8b8b1cdSVivien Didelot 		struct net_device *slave;
164f8b8b1cdSVivien Didelot 	};
165f8b8b1cdSVivien Didelot 
16615240248SVivien Didelot 	/* CPU port tagging operations used by master or slave devices */
16715240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
16815240248SVivien Didelot 
1693e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
1703e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
1713e41f93bSVivien Didelot 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
1723e41f93bSVivien Didelot 			       struct packet_type *pt);
173cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
1743e41f93bSVivien Didelot 
175057cad2cSVivien Didelot 	enum {
176057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
177057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
178057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
179057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
180057cad2cSVivien Didelot 	} type;
181057cad2cSVivien Didelot 
182818be848SVivien Didelot 	struct dsa_switch	*ds;
183818be848SVivien Didelot 	unsigned int		index;
18471e0bbdeSFlorian Fainelli 	const char		*name;
18568b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
186a2c7023fSXiaofei Shen 	const char		*mac;
187189b0d93SAndrew Lunn 	struct device_node	*dn;
18834a79f63SVivien Didelot 	unsigned int		ageing_time;
18933162e9aSVladimir Oltean 	bool			vlan_filtering;
190732f794cSVivien Didelot 	u8			stp_state;
191a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
19296567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
193aab9c406SFlorian Fainelli 	struct phylink		*pl;
19444cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
19597a69a0dSVladimir Oltean 
19697a69a0dSVladimir Oltean 	struct work_struct	xmit_work;
19797a69a0dSVladimir Oltean 	struct sk_buff_head	xmit_queue;
19897a69a0dSVladimir Oltean 
199ab8ccae1SVivien Didelot 	struct list_head list;
200ab8ccae1SVivien Didelot 
20167dbb9d4SFlorian Fainelli 	/*
202c362beb0SVladimir Oltean 	 * Give the switch driver somewhere to hang its per-port private data
203c362beb0SVladimir Oltean 	 * structures (accessible from the tagger).
204c362beb0SVladimir Oltean 	 */
205c362beb0SVladimir Oltean 	void *priv;
206c362beb0SVladimir Oltean 
207c362beb0SVladimir Oltean 	/*
20867dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
20967dbb9d4SFlorian Fainelli 	 */
21067dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
211da7b9e9bSFlorian Fainelli 
212da7b9e9bSFlorian Fainelli 	/*
213da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
214da7b9e9bSFlorian Fainelli 	 */
215da7b9e9bSFlorian Fainelli 	const struct net_device_ops *orig_ndo_ops;
216fb35c60cSVivien Didelot 
217fb35c60cSVivien Didelot 	bool setup;
218c8b09808SAndrew Lunn };
219c8b09808SAndrew Lunn 
220c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
221c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
222c5f51765SVivien Didelot  * but this would require some more complex tree walking,
223c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
224c5f51765SVivien Didelot  */
225c5f51765SVivien Didelot struct dsa_link {
226c5f51765SVivien Didelot 	struct dsa_port *dp;
227c5f51765SVivien Didelot 	struct dsa_port *link_dp;
228c5f51765SVivien Didelot 	struct list_head list;
229c5f51765SVivien Didelot };
230c5f51765SVivien Didelot 
231c8f0b869SBen Hutchings struct dsa_switch {
232fb35c60cSVivien Didelot 	bool setup;
233fb35c60cSVivien Didelot 
234c33063d6SAndrew Lunn 	struct device *dev;
235c33063d6SAndrew Lunn 
236c8f0b869SBen Hutchings 	/*
237c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
238c8f0b869SBen Hutchings 	 */
239c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
24099feaafcSVivien Didelot 	unsigned int		index;
241c8f0b869SBen Hutchings 
242f515f192SVivien Didelot 	/* Listener for switch fabric events */
243f515f192SVivien Didelot 	struct notifier_block	nb;
244f515f192SVivien Didelot 
245c8f0b869SBen Hutchings 	/*
2467543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
2477543a6d5SAndrew Lunn 	 * structure.
2487543a6d5SAndrew Lunn 	 */
2497543a6d5SAndrew Lunn 	void *priv;
2507543a6d5SAndrew Lunn 
2517543a6d5SAndrew Lunn 	/*
252c8f0b869SBen Hutchings 	 * Configuration data for this switch.
253c8f0b869SBen Hutchings 	 */
254ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
255c8f0b869SBen Hutchings 
256c8f0b869SBen Hutchings 	/*
2579d490b4eSVivien Didelot 	 * The switch operations.
258c8f0b869SBen Hutchings 	 */
259a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
260c8f0b869SBen Hutchings 
26166472fc0SAndrew Lunn 	/*
26266472fc0SAndrew Lunn 	 * An array of which element [a] indicates which port on this
26366472fc0SAndrew Lunn 	 * switch should be used to send packets to that are destined
26466472fc0SAndrew Lunn 	 * for switch a. Can be NULL if there is only one switch chip.
26566472fc0SAndrew Lunn 	 */
26666472fc0SAndrew Lunn 	s8		rtable[DSA_MAX_SWITCHES];
26766472fc0SAndrew Lunn 
268c8f0b869SBen Hutchings 	/*
269c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
270c8f0b869SBen Hutchings 	 */
2710d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
272c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
273a0c02161SVivien Didelot 
2740f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
2750f3da6afSVivien Didelot 	unsigned int ageing_time_min;
2760f3da6afSVivien Didelot 	unsigned int ageing_time_max;
2770f3da6afSVivien Didelot 
27896567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
27996567d5dSAndrew Lunn 	struct devlink		*devlink;
28096567d5dSAndrew Lunn 
28155199df6SFlorian Fainelli 	/* Number of switch port queues */
28255199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
28355199df6SFlorian Fainelli 
2848f5d16f6SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
2858f5d16f6SVladimir Oltean 	 * settings on ports if not hardware-supported
2868f5d16f6SVladimir Oltean 	 */
2878f5d16f6SVladimir Oltean 	bool			vlan_filtering_is_global;
2888f5d16f6SVladimir Oltean 
28914574676SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
29014574676SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
29114574676SVladimir Oltean 	 */
29214574676SVladimir Oltean 	bool			vlan_filtering;
29314574676SVladimir Oltean 
294a0c02161SVivien Didelot 	size_t num_ports;
295c8f0b869SBen Hutchings };
296c8f0b869SBen Hutchings 
29768bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
298c38c5a66SVivien Didelot {
299b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
300d607525bSVivien Didelot 	struct dsa_port *dp;
301b96ddf25SVivien Didelot 
302b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
303b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
304b96ddf25SVivien Didelot 			return dp;
305d607525bSVivien Didelot 
306d607525bSVivien Didelot 	return NULL;
307c38c5a66SVivien Didelot }
308c38c5a66SVivien Didelot 
309bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
310bff7b688SVivien Didelot {
311c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
312bff7b688SVivien Didelot }
313bff7b688SVivien Didelot 
314c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
315c8f0b869SBen Hutchings {
316c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
317c8f0b869SBen Hutchings }
318c8f0b869SBen Hutchings 
31960045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
32060045cbfSAndrew Lunn {
321c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
32260045cbfSAndrew Lunn }
32360045cbfSAndrew Lunn 
3242b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
3256cd456f3SVivien Didelot {
326c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
3276cd456f3SVivien Didelot }
3286cd456f3SVivien Didelot 
32902bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
33002bc6e54SVivien Didelot {
331c38c5a66SVivien Didelot 	u32 mask = 0;
332c38c5a66SVivien Didelot 	int p;
33302bc6e54SVivien Didelot 
334c38c5a66SVivien Didelot 	for (p = 0; p < ds->num_ports; p++)
335c38c5a66SVivien Didelot 		if (dsa_is_user_port(ds, p))
336c38c5a66SVivien Didelot 			mask |= BIT(p);
337c38c5a66SVivien Didelot 
338c38c5a66SVivien Didelot 	return mask;
339c8652c83SVivien Didelot }
340c8652c83SVivien Didelot 
341c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
342c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
343c5f51765SVivien Didelot {
344c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
345c5f51765SVivien Didelot 	struct dsa_link *dl;
346c5f51765SVivien Didelot 
347c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
348c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
349c5f51765SVivien Didelot 			return dl->dp->index;
350c5f51765SVivien Didelot 
351c5f51765SVivien Didelot 	return ds->num_ports;
352c5f51765SVivien Didelot }
353c5f51765SVivien Didelot 
3543b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
3553b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
3563b8fac5dSVivien Didelot 					    int port)
3573b8fac5dSVivien Didelot {
3583b8fac5dSVivien Didelot 	if (device == ds->index)
3593b8fac5dSVivien Didelot 		return port;
3603b8fac5dSVivien Didelot 	else
361c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
3623b8fac5dSVivien Didelot }
3633b8fac5dSVivien Didelot 
3643b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
36507073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
366c8f0b869SBen Hutchings {
36707073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
36807073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
36907073c79SVivien Didelot 
37007073c79SVivien Didelot 	if (!cpu_dp)
37107073c79SVivien Didelot 		return port;
372c8f0b869SBen Hutchings 
3733b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
374c8f0b869SBen Hutchings }
375c8f0b869SBen Hutchings 
376cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
377cf2d45f5SVladimir Oltean {
378cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
379cf2d45f5SVladimir Oltean 
380cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
381cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
382cf2d45f5SVladimir Oltean 	else
383cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
384cf2d45f5SVladimir Oltean }
385cf2d45f5SVladimir Oltean 
3862bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
3872bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
3889d490b4eSVivien Didelot struct dsa_switch_ops {
3895ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
3905ed4e3ebSFlorian Fainelli 						  int port);
3917b314362SAndrew Lunn 
392c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
3935e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
3946819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
395c8f0b869SBen Hutchings 
396c8f0b869SBen Hutchings 	/*
397c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
398c8f0b869SBen Hutchings 	 */
399c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
400c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
401c8f0b869SBen Hutchings 			     int regnum, u16 val);
402c8f0b869SBen Hutchings 
403c8f0b869SBen Hutchings 	/*
404ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
405ec9436baSFlorian Fainelli 	 */
406ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
407ec9436baSFlorian Fainelli 				struct phy_device *phydev);
408ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
409ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
410ec9436baSFlorian Fainelli 
411ec9436baSFlorian Fainelli 	/*
41211d8f3ddSFlorian Fainelli 	 * PHYLINK integration
41311d8f3ddSFlorian Fainelli 	 */
41411d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
41511d8f3ddSFlorian Fainelli 				    unsigned long *supported,
41611d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
41711d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
41811d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
41911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
42011d8f3ddSFlorian Fainelli 				      unsigned int mode,
42111d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
42211d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
42311d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
42411d8f3ddSFlorian Fainelli 					 unsigned int mode,
42511d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
42611d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
42711d8f3ddSFlorian Fainelli 				       unsigned int mode,
42811d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
42911d8f3ddSFlorian Fainelli 				       struct phy_device *phydev);
43011d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
43111d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
43211d8f3ddSFlorian Fainelli 	/*
433c8f0b869SBen Hutchings 	 * ethtool hardware statistics.
434c8f0b869SBen Hutchings 	 */
43589f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
43689f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
437c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
438c8f0b869SBen Hutchings 				     int port, uint64_t *data);
43989f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
440cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
441cf963573SFlorian Fainelli 					 int port, uint64_t *data);
44224462549SFlorian Fainelli 
44324462549SFlorian Fainelli 	/*
44419e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
44519e57c4eSFlorian Fainelli 	 */
44619e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
44719e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
44819e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
44919e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
45019e57c4eSFlorian Fainelli 
45119e57c4eSFlorian Fainelli 	/*
4520336369dSBrandon Streiff 	 * ethtool timestamp info
4530336369dSBrandon Streiff 	 */
4540336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
4550336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
4560336369dSBrandon Streiff 
4570336369dSBrandon Streiff 	/*
45824462549SFlorian Fainelli 	 * Suspend and resume
45924462549SFlorian Fainelli 	 */
46024462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
46124462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
462b2f2af21SFlorian Fainelli 
463b2f2af21SFlorian Fainelli 	/*
464b2f2af21SFlorian Fainelli 	 * Port enable/disable
465b2f2af21SFlorian Fainelli 	 */
466b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
467b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
46875104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
4697905288fSFlorian Fainelli 
4707905288fSFlorian Fainelli 	/*
47108f50061SVivien Didelot 	 * Port's MAC EEE settings
4727905288fSFlorian Fainelli 	 */
47308f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
4747905288fSFlorian Fainelli 			       struct ethtool_eee *e);
47508f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
4767905288fSFlorian Fainelli 			       struct ethtool_eee *e);
47751579c3fSGuenter Roeck 
4786793abb4SGuenter Roeck 	/* EEPROM access */
4796793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
4806793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
4816793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4826793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
4836793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4843d762a0fSGuenter Roeck 
4853d762a0fSGuenter Roeck 	/*
4863d762a0fSGuenter Roeck 	 * Register access.
4873d762a0fSGuenter Roeck 	 */
4883d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
4893d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
4903d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
491b73adef6SFlorian Fainelli 
492b73adef6SFlorian Fainelli 	/*
493b73adef6SFlorian Fainelli 	 * Bridge integration
494b73adef6SFlorian Fainelli 	 */
49534a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
49671327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
497a6692754SVivien Didelot 				    struct net_device *bridge);
498f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
499f123f2fbSVivien Didelot 				     struct net_device *bridge);
50043c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
501b73adef6SFlorian Fainelli 				      u8 state);
502732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
50357652796SRussell King 	int	(*port_egress_floods)(struct dsa_switch *ds, int port,
50457652796SRussell King 				      bool unicast, bool multicast);
5052a778e1bSVivien Didelot 
5062a778e1bSVivien Didelot 	/*
50711149536SVivien Didelot 	 * VLAN support
50811149536SVivien Didelot 	 */
509fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
510fb2dabadSVivien Didelot 				       bool vlan_filtering);
51176e398a6SVivien Didelot 	int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
51280e02360SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
5134d5770b3SVivien Didelot 	void (*port_vlan_add)(struct dsa_switch *ds, int port,
51480e02360SVivien Didelot 			      const struct switchdev_obj_port_vlan *vlan);
51576e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
51676e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
51711149536SVivien Didelot 	/*
5182a778e1bSVivien Didelot 	 * Forwarding database
5192a778e1bSVivien Didelot 	 */
5201b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
5216c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
5222a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
5236c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
524ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
5252bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
5268df30255SVivien Didelot 
5278df30255SVivien Didelot 	/*
5288df30255SVivien Didelot 	 * Multicast database
5298df30255SVivien Didelot 	 */
5308df30255SVivien Didelot 	int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
5313709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
5328df30255SVivien Didelot 	void (*port_mdb_add)(struct dsa_switch *ds, int port,
5333709aadcSVivien Didelot 			     const struct switchdev_obj_port_mdb *mdb);
5348df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
5358df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
536bf9f2648SFlorian Fainelli 	/*
537bf9f2648SFlorian Fainelli 	 * RXNFC
538bf9f2648SFlorian Fainelli 	 */
539bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
540bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
541bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
542bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
543f50f2127SFlorian Fainelli 
544f50f2127SFlorian Fainelli 	/*
545f50f2127SFlorian Fainelli 	 * TC integration
546f50f2127SFlorian Fainelli 	 */
547f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
548f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
549f50f2127SFlorian Fainelli 				   bool ingress);
550f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
551f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
55247d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
55347d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
55440ef2c93SVivien Didelot 
55540ef2c93SVivien Didelot 	/*
55640ef2c93SVivien Didelot 	 * Cross-chip operations
55740ef2c93SVivien Didelot 	 */
55840ef2c93SVivien Didelot 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
55940ef2c93SVivien Didelot 					 int port, struct net_device *br);
56040ef2c93SVivien Didelot 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
56140ef2c93SVivien Didelot 					  int port, struct net_device *br);
5620336369dSBrandon Streiff 
5630336369dSBrandon Streiff 	/*
5640336369dSBrandon Streiff 	 * PTP functionality
5650336369dSBrandon Streiff 	 */
5660336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
5670336369dSBrandon Streiff 				     struct ifreq *ifr);
5680336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
5690336369dSBrandon Streiff 				     struct ifreq *ifr);
57090af1059SBrandon Streiff 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
57190af1059SBrandon Streiff 				 struct sk_buff *clone, unsigned int type);
57290af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
57390af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
57497a69a0dSVladimir Oltean 
57597a69a0dSVladimir Oltean 	/*
57697a69a0dSVladimir Oltean 	 * Deferred frame Tx
57797a69a0dSVladimir Oltean 	 */
57897a69a0dSVladimir Oltean 	netdev_tx_t (*port_deferred_xmit)(struct dsa_switch *ds, int port,
57997a69a0dSVladimir Oltean 					  struct sk_buff *skb);
5806b297524SAndrew Lunn 	/* Devlink parameters */
5816b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
5826b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
5836b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
5846b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
5856b297524SAndrew Lunn };
5866b297524SAndrew Lunn 
5876b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
5886b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
5896b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
5906b297524SAndrew Lunn 
5916b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
5926b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
5936b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
5946b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
5956b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
5966b297524SAndrew Lunn 				const struct devlink_param *params,
5976b297524SAndrew Lunn 				size_t params_count);
5986b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
5996b297524SAndrew Lunn 				   const struct devlink_param *params,
6006b297524SAndrew Lunn 				   size_t params_count);
6016b297524SAndrew Lunn struct dsa_devlink_priv {
6026b297524SAndrew Lunn 	struct dsa_switch *ds;
603c8f0b869SBen Hutchings };
604c8f0b869SBen Hutchings 
605ab3d408dSFlorian Fainelli struct dsa_switch_driver {
606ab3d408dSFlorian Fainelli 	struct list_head	list;
607a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
608ab3d408dSFlorian Fainelli };
609ab3d408dSFlorian Fainelli 
61014b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
611c8f0b869SBen Hutchings 
61273a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
613c6e970a0SAndrew Lunn static inline bool netdev_uses_dsa(struct net_device *dev)
614c6e970a0SAndrew Lunn {
615c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
616717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
617c6e970a0SAndrew Lunn #endif
618c6e970a0SAndrew Lunn 	return false;
619c6e970a0SAndrew Lunn }
620c6e970a0SAndrew Lunn 
621cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb,
622cc1939e4SVladimir Oltean 				  struct net_device *dev)
623cc1939e4SVladimir Oltean {
624cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
625cc1939e4SVladimir Oltean 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
626cc1939e4SVladimir Oltean #endif
627cc1939e4SVladimir Oltean 	return false;
628cc1939e4SVladimir Oltean }
629cc1939e4SVladimir Oltean 
63083c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
63123c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
632ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
633ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
634ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
635ea825e70SFlorian Fainelli #else
636ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
637ea825e70SFlorian Fainelli {
638ea825e70SFlorian Fainelli 	return 0;
639ea825e70SFlorian Fainelli }
640ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
641ea825e70SFlorian Fainelli {
642ea825e70SFlorian Fainelli 	return 0;
643ea825e70SFlorian Fainelli }
644ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
645ea825e70SFlorian Fainelli 
64660724d4bSFlorian Fainelli enum dsa_notifier_type {
64760724d4bSFlorian Fainelli 	DSA_PORT_REGISTER,
64860724d4bSFlorian Fainelli 	DSA_PORT_UNREGISTER,
64960724d4bSFlorian Fainelli };
65060724d4bSFlorian Fainelli 
65160724d4bSFlorian Fainelli struct dsa_notifier_info {
65260724d4bSFlorian Fainelli 	struct net_device *dev;
65360724d4bSFlorian Fainelli };
65460724d4bSFlorian Fainelli 
65560724d4bSFlorian Fainelli struct dsa_notifier_register_info {
65660724d4bSFlorian Fainelli 	struct dsa_notifier_info info;	/* must be first */
65760724d4bSFlorian Fainelli 	struct net_device *master;
65860724d4bSFlorian Fainelli 	unsigned int port_number;
65960724d4bSFlorian Fainelli 	unsigned int switch_number;
66060724d4bSFlorian Fainelli };
66160724d4bSFlorian Fainelli 
66260724d4bSFlorian Fainelli static inline struct net_device *
66360724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
66460724d4bSFlorian Fainelli {
66560724d4bSFlorian Fainelli 	return info->dev;
66660724d4bSFlorian Fainelli }
66760724d4bSFlorian Fainelli 
66860724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
66960724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb);
67060724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb);
67160724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev,
67260724d4bSFlorian Fainelli 		       struct dsa_notifier_info *info);
67360724d4bSFlorian Fainelli #else
67460724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb)
67560724d4bSFlorian Fainelli {
67660724d4bSFlorian Fainelli 	return 0;
67760724d4bSFlorian Fainelli }
67860724d4bSFlorian Fainelli 
67960724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb)
68060724d4bSFlorian Fainelli {
68160724d4bSFlorian Fainelli 	return 0;
68260724d4bSFlorian Fainelli }
68360724d4bSFlorian Fainelli 
68460724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
68560724d4bSFlorian Fainelli 				     struct dsa_notifier_info *info)
68660724d4bSFlorian Fainelli {
68760724d4bSFlorian Fainelli 	return NOTIFY_DONE;
68860724d4bSFlorian Fainelli }
68960724d4bSFlorian Fainelli #endif
69060724d4bSFlorian Fainelli 
6910a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */
6920a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q)	((p) << 8 | q)
6930a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v)		((v) >> 8)
6940a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v)		((v) & 0xff)
6950a5f14ceSFlorian Fainelli 
696cf963573SFlorian Fainelli 
69797a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
698cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
699cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
700cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
70111d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
702cf963573SFlorian Fainelli 
703d3b8c049SAndrew Lunn struct dsa_tag_driver {
704d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
705d3b8c049SAndrew Lunn 	struct list_head list;
706d3b8c049SAndrew Lunn 	struct module *owner;
707d3b8c049SAndrew Lunn };
708d3b8c049SAndrew Lunn 
709d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
710d3b8c049SAndrew Lunn 			      unsigned int count,
711d3b8c049SAndrew Lunn 			      struct module *owner);
712d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
713d3b8c049SAndrew Lunn 				unsigned int count);
714d3b8c049SAndrew Lunn 
715d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
716d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
717d3b8c049SAndrew Lunn {									\
718d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
719d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
720d3b8c049SAndrew Lunn 	return 0;							\
721d3b8c049SAndrew Lunn }									\
722d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
723d3b8c049SAndrew Lunn 									\
724d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
725d3b8c049SAndrew Lunn {									\
726d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
727d3b8c049SAndrew Lunn }									\
728d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
729d3b8c049SAndrew Lunn 
730d3b8c049SAndrew Lunn /**
731d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
732d3b8c049SAndrew Lunn  * drivers
733d3b8c049SAndrew Lunn  * @__ops_array: Array of tag driver strucutres
734d3b8c049SAndrew Lunn  *
735d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
736d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
737d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
738d3b8c049SAndrew Lunn  */
739d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
740d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
741d3b8c049SAndrew Lunn 
742d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
743d3b8c049SAndrew Lunn 
744d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
745d3b8c049SAndrew Lunn  * drivers
746d3b8c049SAndrew Lunn  */
747d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
748d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
749d3b8c049SAndrew Lunn 	.ops = &__ops,							\
750d3b8c049SAndrew Lunn }
751d3b8c049SAndrew Lunn 
752d3b8c049SAndrew Lunn /**
753d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
754d3b8c049SAndrew Lunn  * driver
755d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
756d3b8c049SAndrew Lunn  *
757d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
758d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
759d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
760d3b8c049SAndrew Lunn  */
761d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
762d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
763d3b8c049SAndrew Lunn 									\
764d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
765d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
766d3b8c049SAndrew Lunn };									\
767d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
76891da11f8SLennert Buytenhek #endif
769d3b8c049SAndrew Lunn 
770