xref: /linux/include/net/dsa.h (revision d3b8c04988ca1685700e345a82a1396df79e6291)
191da11f8SLennert Buytenhek /*
291da11f8SLennert Buytenhek  * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
3e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
491da11f8SLennert Buytenhek  *
591da11f8SLennert Buytenhek  * This program is free software; you can redistribute it and/or modify
691da11f8SLennert Buytenhek  * it under the terms of the GNU General Public License as published by
791da11f8SLennert Buytenhek  * the Free Software Foundation; either version 2 of the License, or
891da11f8SLennert Buytenhek  * (at your option) any later version.
991da11f8SLennert Buytenhek  */
1091da11f8SLennert Buytenhek 
1191da11f8SLennert Buytenhek #ifndef __LINUX_NET_DSA_H
1291da11f8SLennert Buytenhek #define __LINUX_NET_DSA_H
1391da11f8SLennert Buytenhek 
144d56a29fSRussell King #include <linux/if.h>
15ea1f51beSAxel Lin #include <linux/if_ether.h>
16c8f0b869SBen Hutchings #include <linux/list.h>
17f515f192SVivien Didelot #include <linux/notifier.h>
18cf50dcc2SBen Hutchings #include <linux/timer.h>
19cf50dcc2SBen Hutchings #include <linux/workqueue.h>
20fa981d9aSFlorian Fainelli #include <linux/of.h>
21a2820543SFlorian Fainelli #include <linux/ethtool.h>
220336369dSBrandon Streiff #include <linux/net_tstamp.h>
2311d8f3ddSFlorian Fainelli #include <linux/phy.h>
24ecfc9372SFlorian Fainelli #include <linux/platform_data/dsa.h>
2596567d5dSAndrew Lunn #include <net/devlink.h>
26f0c24ccfSVivien Didelot #include <net/switchdev.h>
27cf50dcc2SBen Hutchings 
28f50f2127SFlorian Fainelli struct tc_action;
294d56a29fSRussell King struct phy_device;
304d56a29fSRussell King struct fixed_phy_status;
3111d8f3ddSFlorian Fainelli struct phylink_link_state;
32f50f2127SFlorian Fainelli 
330b42f033SAndrew Lunn #define DSA_TAG_PROTO_NONE_VALUE		0
340b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_VALUE		1
350b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE	2
360b42f033SAndrew Lunn #define DSA_TAG_PROTO_DSA_VALUE			3
370b42f033SAndrew Lunn #define DSA_TAG_PROTO_EDSA_VALUE		4
380b42f033SAndrew Lunn #define DSA_TAG_PROTO_GSWIP_VALUE		5
390b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9477_VALUE		6
400b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9893_VALUE		7
410b42f033SAndrew Lunn #define DSA_TAG_PROTO_LAN9303_VALUE		8
420b42f033SAndrew Lunn #define DSA_TAG_PROTO_MTK_VALUE			9
430b42f033SAndrew Lunn #define DSA_TAG_PROTO_QCA_VALUE			10
440b42f033SAndrew Lunn #define DSA_TAG_PROTO_TRAILER_VALUE		11
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,
5939a7f2a4SAndrew Lunn 	DSA_TAG_LAST,			/* MUST BE LAST */
60ac7a04c3SFlorian Fainelli };
615037d532SFlorian Fainelli 
625075314eSAlexander Duyck struct packet_type;
6390af1059SBrandon Streiff struct dsa_switch;
643e8a72d1SFlorian Fainelli 
6568277a2cSJohn Crispin struct dsa_device_ops {
6668277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
6768277a2cSJohn Crispin 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
6889e49506SFlorian Westphal 			       struct packet_type *pt);
69598a9680SJohn Crispin 	int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
70598a9680SJohn Crispin 			    int *offset);
71a5dd3087SAndrew Lunn 	unsigned int overhead;
72875138f8SAndrew Lunn 	const char *name;
73056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
7468277a2cSJohn Crispin };
7568277a2cSJohn Crispin 
760b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
770b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
780b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
790b42f033SAndrew Lunn 
80cf50dcc2SBen Hutchings struct dsa_switch_tree {
8183c0afaeSAndrew Lunn 	struct list_head	list;
8283c0afaeSAndrew Lunn 
83f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
84f515f192SVivien Didelot 	struct raw_notifier_head	nh;
85f515f192SVivien Didelot 
8683c0afaeSAndrew Lunn 	/* Tree identifier */
8749463b7fSVivien Didelot 	unsigned int index;
8883c0afaeSAndrew Lunn 
8983c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
9083c0afaeSAndrew Lunn 	struct kref refcount;
9183c0afaeSAndrew Lunn 
9283c0afaeSAndrew Lunn 	/* Has this tree been applied to the hardware? */
93ec15dd42SVivien Didelot 	bool setup;
9483c0afaeSAndrew Lunn 
95cf50dcc2SBen Hutchings 	/*
96cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
97cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
98cf50dcc2SBen Hutchings 	 */
99cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
100cf85d08fSLennert Buytenhek 
101cf50dcc2SBen Hutchings 	/*
1028b0d3ea5SVivien Didelot 	 * The switch port to which the CPU is attached.
103cf50dcc2SBen Hutchings 	 */
1048b0d3ea5SVivien Didelot 	struct dsa_port		*cpu_dp;
105cf50dcc2SBen Hutchings 
106cf50dcc2SBen Hutchings 	/*
107cf50dcc2SBen Hutchings 	 * Data for the individual switch chips.
108cf50dcc2SBen Hutchings 	 */
109cf50dcc2SBen Hutchings 	struct dsa_switch	*ds[DSA_MAX_SWITCHES];
110cf50dcc2SBen Hutchings };
111cf50dcc2SBen Hutchings 
112f50f2127SFlorian Fainelli /* TC matchall action types, only mirroring for now */
113f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
114f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
115f50f2127SFlorian Fainelli };
116f50f2127SFlorian Fainelli 
117f50f2127SFlorian Fainelli /* TC mirroring entry */
118f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
119f50f2127SFlorian Fainelli 	u8 to_local_port;
120f50f2127SFlorian Fainelli 	bool ingress;
121f50f2127SFlorian Fainelli };
122f50f2127SFlorian Fainelli 
123f50f2127SFlorian Fainelli /* TC matchall entry */
124f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
125f50f2127SFlorian Fainelli 	struct list_head list;
126f50f2127SFlorian Fainelli 	unsigned long cookie;
127f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
128f50f2127SFlorian Fainelli 	union {
129f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
130f50f2127SFlorian Fainelli 	};
131f50f2127SFlorian Fainelli };
132f50f2127SFlorian Fainelli 
133f50f2127SFlorian Fainelli 
134c8b09808SAndrew Lunn struct dsa_port {
135f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
136f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
137f8b8b1cdSVivien Didelot 	 */
138f8b8b1cdSVivien Didelot 	union {
139f8b8b1cdSVivien Didelot 		struct net_device *master;
140f8b8b1cdSVivien Didelot 		struct net_device *slave;
141f8b8b1cdSVivien Didelot 	};
142f8b8b1cdSVivien Didelot 
14315240248SVivien Didelot 	/* CPU port tagging operations used by master or slave devices */
14415240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
14515240248SVivien Didelot 
1463e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
1473e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
1483e41f93bSVivien Didelot 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
1493e41f93bSVivien Didelot 			       struct packet_type *pt);
1503e41f93bSVivien Didelot 
151057cad2cSVivien Didelot 	enum {
152057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
153057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
154057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
155057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
156057cad2cSVivien Didelot 	} type;
157057cad2cSVivien Didelot 
158818be848SVivien Didelot 	struct dsa_switch	*ds;
159818be848SVivien Didelot 	unsigned int		index;
16071e0bbdeSFlorian Fainelli 	const char		*name;
16124a9332aSVivien Didelot 	const struct dsa_port	*cpu_dp;
162a2c7023fSXiaofei Shen 	const char		*mac;
163189b0d93SAndrew Lunn 	struct device_node	*dn;
16434a79f63SVivien Didelot 	unsigned int		ageing_time;
165732f794cSVivien Didelot 	u8			stp_state;
166a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
16796567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
168aab9c406SFlorian Fainelli 	struct phylink		*pl;
16967dbb9d4SFlorian Fainelli 	/*
17067dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
17167dbb9d4SFlorian Fainelli 	 */
17267dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
173da7b9e9bSFlorian Fainelli 
174da7b9e9bSFlorian Fainelli 	/*
175da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
176da7b9e9bSFlorian Fainelli 	 */
177da7b9e9bSFlorian Fainelli 	const struct net_device_ops *orig_ndo_ops;
178c8b09808SAndrew Lunn };
179c8b09808SAndrew Lunn 
180c8f0b869SBen Hutchings struct dsa_switch {
181c33063d6SAndrew Lunn 	struct device *dev;
182c33063d6SAndrew Lunn 
183c8f0b869SBen Hutchings 	/*
184c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
185c8f0b869SBen Hutchings 	 */
186c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
18799feaafcSVivien Didelot 	unsigned int		index;
188c8f0b869SBen Hutchings 
189f515f192SVivien Didelot 	/* Listener for switch fabric events */
190f515f192SVivien Didelot 	struct notifier_block	nb;
191f515f192SVivien Didelot 
192c8f0b869SBen Hutchings 	/*
1937543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
1947543a6d5SAndrew Lunn 	 * structure.
1957543a6d5SAndrew Lunn 	 */
1967543a6d5SAndrew Lunn 	void *priv;
1977543a6d5SAndrew Lunn 
1987543a6d5SAndrew Lunn 	/*
199c8f0b869SBen Hutchings 	 * Configuration data for this switch.
200c8f0b869SBen Hutchings 	 */
201ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
202c8f0b869SBen Hutchings 
203c8f0b869SBen Hutchings 	/*
2049d490b4eSVivien Didelot 	 * The switch operations.
205c8f0b869SBen Hutchings 	 */
206a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
207c8f0b869SBen Hutchings 
20866472fc0SAndrew Lunn 	/*
20966472fc0SAndrew Lunn 	 * An array of which element [a] indicates which port on this
21066472fc0SAndrew Lunn 	 * switch should be used to send packets to that are destined
21166472fc0SAndrew Lunn 	 * for switch a. Can be NULL if there is only one switch chip.
21266472fc0SAndrew Lunn 	 */
21366472fc0SAndrew Lunn 	s8		rtable[DSA_MAX_SWITCHES];
21466472fc0SAndrew Lunn 
215c8f0b869SBen Hutchings 	/*
216c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
217c8f0b869SBen Hutchings 	 */
2180d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
219c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
220a0c02161SVivien Didelot 
2210f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
2220f3da6afSVivien Didelot 	unsigned int ageing_time_min;
2230f3da6afSVivien Didelot 	unsigned int ageing_time_max;
2240f3da6afSVivien Didelot 
22596567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
22696567d5dSAndrew Lunn 	struct devlink		*devlink;
22796567d5dSAndrew Lunn 
22855199df6SFlorian Fainelli 	/* Number of switch port queues */
22955199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
23055199df6SFlorian Fainelli 
2310015b80aSSalvatore Mesoraca 	unsigned long		*bitmap;
2320015b80aSSalvatore Mesoraca 	unsigned long		_bitmap;
2330015b80aSSalvatore Mesoraca 
234a0c02161SVivien Didelot 	/* Dynamically allocated ports, keep last */
235a0c02161SVivien Didelot 	size_t num_ports;
236a0c02161SVivien Didelot 	struct dsa_port ports[];
237c8f0b869SBen Hutchings };
238c8f0b869SBen Hutchings 
239c38c5a66SVivien Didelot static inline const struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
240c38c5a66SVivien Didelot {
241c38c5a66SVivien Didelot 	return &ds->ports[p];
242c38c5a66SVivien Didelot }
243c38c5a66SVivien Didelot 
244bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
245bff7b688SVivien Didelot {
246c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
247bff7b688SVivien Didelot }
248bff7b688SVivien Didelot 
249c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
250c8f0b869SBen Hutchings {
251c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
252c8f0b869SBen Hutchings }
253c8f0b869SBen Hutchings 
25460045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
25560045cbfSAndrew Lunn {
256c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
25760045cbfSAndrew Lunn }
25860045cbfSAndrew Lunn 
2592b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
2606cd456f3SVivien Didelot {
261c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
2626cd456f3SVivien Didelot }
2636cd456f3SVivien Didelot 
26402bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
26502bc6e54SVivien Didelot {
266c38c5a66SVivien Didelot 	u32 mask = 0;
267c38c5a66SVivien Didelot 	int p;
26802bc6e54SVivien Didelot 
269c38c5a66SVivien Didelot 	for (p = 0; p < ds->num_ports; p++)
270c38c5a66SVivien Didelot 		if (dsa_is_user_port(ds, p))
271c38c5a66SVivien Didelot 			mask |= BIT(p);
272c38c5a66SVivien Didelot 
273c38c5a66SVivien Didelot 	return mask;
274c8652c83SVivien Didelot }
275c8652c83SVivien Didelot 
2763b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
2773b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
2783b8fac5dSVivien Didelot 					    int port)
2793b8fac5dSVivien Didelot {
2803b8fac5dSVivien Didelot 	if (device == ds->index)
2813b8fac5dSVivien Didelot 		return port;
2823b8fac5dSVivien Didelot 	else
2833b8fac5dSVivien Didelot 		return ds->rtable[device];
2843b8fac5dSVivien Didelot }
2853b8fac5dSVivien Didelot 
2863b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
28707073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
288c8f0b869SBen Hutchings {
28907073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
29007073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
29107073c79SVivien Didelot 
29207073c79SVivien Didelot 	if (!cpu_dp)
29307073c79SVivien Didelot 		return port;
294c8f0b869SBen Hutchings 
2953b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
296c8f0b869SBen Hutchings }
297c8f0b869SBen Hutchings 
2982bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
2992bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
3009d490b4eSVivien Didelot struct dsa_switch_ops {
3012a93c1a3SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
302c8f0b869SBen Hutchings 	/*
303a6a71f19SVivien Didelot 	 * Legacy probing.
304c8f0b869SBen Hutchings 	 */
3050209d144SVivien Didelot 	const char	*(*probe)(struct device *dsa_dev,
3060209d144SVivien Didelot 				  struct device *host_dev, int sw_addr,
3070209d144SVivien Didelot 				  void **priv);
3082a93c1a3SFlorian Fainelli #endif
3097b314362SAndrew Lunn 
3105ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
3115ed4e3ebSFlorian Fainelli 						  int port);
3127b314362SAndrew Lunn 
313c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
3146819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
315c8f0b869SBen Hutchings 
316c8f0b869SBen Hutchings 	/*
317c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
318c8f0b869SBen Hutchings 	 */
319c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
320c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
321c8f0b869SBen Hutchings 			     int regnum, u16 val);
322c8f0b869SBen Hutchings 
323c8f0b869SBen Hutchings 	/*
324ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
325ec9436baSFlorian Fainelli 	 */
326ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
327ec9436baSFlorian Fainelli 				struct phy_device *phydev);
328ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
329ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
330ec9436baSFlorian Fainelli 
331ec9436baSFlorian Fainelli 	/*
33211d8f3ddSFlorian Fainelli 	 * PHYLINK integration
33311d8f3ddSFlorian Fainelli 	 */
33411d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
33511d8f3ddSFlorian Fainelli 				    unsigned long *supported,
33611d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
33711d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
33811d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
33911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
34011d8f3ddSFlorian Fainelli 				      unsigned int mode,
34111d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
34211d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
34311d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
34411d8f3ddSFlorian Fainelli 					 unsigned int mode,
34511d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
34611d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
34711d8f3ddSFlorian Fainelli 				       unsigned int mode,
34811d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
34911d8f3ddSFlorian Fainelli 				       struct phy_device *phydev);
35011d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
35111d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
35211d8f3ddSFlorian Fainelli 	/*
353c8f0b869SBen Hutchings 	 * ethtool hardware statistics.
354c8f0b869SBen Hutchings 	 */
35589f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
35689f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
357c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
358c8f0b869SBen Hutchings 				     int port, uint64_t *data);
35989f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
360cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
361cf963573SFlorian Fainelli 					 int port, uint64_t *data);
36224462549SFlorian Fainelli 
36324462549SFlorian Fainelli 	/*
36419e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
36519e57c4eSFlorian Fainelli 	 */
36619e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
36719e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
36819e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
36919e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
37019e57c4eSFlorian Fainelli 
37119e57c4eSFlorian Fainelli 	/*
3720336369dSBrandon Streiff 	 * ethtool timestamp info
3730336369dSBrandon Streiff 	 */
3740336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
3750336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
3760336369dSBrandon Streiff 
3770336369dSBrandon Streiff 	/*
37824462549SFlorian Fainelli 	 * Suspend and resume
37924462549SFlorian Fainelli 	 */
38024462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
38124462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
382b2f2af21SFlorian Fainelli 
383b2f2af21SFlorian Fainelli 	/*
384b2f2af21SFlorian Fainelli 	 * Port enable/disable
385b2f2af21SFlorian Fainelli 	 */
386b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
387b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
38875104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
3897905288fSFlorian Fainelli 
3907905288fSFlorian Fainelli 	/*
39108f50061SVivien Didelot 	 * Port's MAC EEE settings
3927905288fSFlorian Fainelli 	 */
39308f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
3947905288fSFlorian Fainelli 			       struct ethtool_eee *e);
39508f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
3967905288fSFlorian Fainelli 			       struct ethtool_eee *e);
39751579c3fSGuenter Roeck 
3986793abb4SGuenter Roeck 	/* EEPROM access */
3996793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
4006793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
4016793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4026793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
4036793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4043d762a0fSGuenter Roeck 
4053d762a0fSGuenter Roeck 	/*
4063d762a0fSGuenter Roeck 	 * Register access.
4073d762a0fSGuenter Roeck 	 */
4083d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
4093d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
4103d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
411b73adef6SFlorian Fainelli 
412b73adef6SFlorian Fainelli 	/*
413b73adef6SFlorian Fainelli 	 * Bridge integration
414b73adef6SFlorian Fainelli 	 */
41534a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
41671327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
417a6692754SVivien Didelot 				    struct net_device *bridge);
418f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
419f123f2fbSVivien Didelot 				     struct net_device *bridge);
42043c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
421b73adef6SFlorian Fainelli 				      u8 state);
422732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
42357652796SRussell King 	int	(*port_egress_floods)(struct dsa_switch *ds, int port,
42457652796SRussell King 				      bool unicast, bool multicast);
4252a778e1bSVivien Didelot 
4262a778e1bSVivien Didelot 	/*
42711149536SVivien Didelot 	 * VLAN support
42811149536SVivien Didelot 	 */
429fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
430fb2dabadSVivien Didelot 				       bool vlan_filtering);
43176e398a6SVivien Didelot 	int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
43280e02360SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
4334d5770b3SVivien Didelot 	void (*port_vlan_add)(struct dsa_switch *ds, int port,
43480e02360SVivien Didelot 			      const struct switchdev_obj_port_vlan *vlan);
43576e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
43676e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
43711149536SVivien Didelot 	/*
4382a778e1bSVivien Didelot 	 * Forwarding database
4392a778e1bSVivien Didelot 	 */
4401b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
4416c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
4422a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
4436c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
444ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
4452bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
4468df30255SVivien Didelot 
4478df30255SVivien Didelot 	/*
4488df30255SVivien Didelot 	 * Multicast database
4498df30255SVivien Didelot 	 */
4508df30255SVivien Didelot 	int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
4513709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
4528df30255SVivien Didelot 	void (*port_mdb_add)(struct dsa_switch *ds, int port,
4533709aadcSVivien Didelot 			     const struct switchdev_obj_port_mdb *mdb);
4548df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
4558df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
456bf9f2648SFlorian Fainelli 	/*
457bf9f2648SFlorian Fainelli 	 * RXNFC
458bf9f2648SFlorian Fainelli 	 */
459bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
460bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
461bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
462bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
463f50f2127SFlorian Fainelli 
464f50f2127SFlorian Fainelli 	/*
465f50f2127SFlorian Fainelli 	 * TC integration
466f50f2127SFlorian Fainelli 	 */
467f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
468f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
469f50f2127SFlorian Fainelli 				   bool ingress);
470f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
471f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
47240ef2c93SVivien Didelot 
47340ef2c93SVivien Didelot 	/*
47440ef2c93SVivien Didelot 	 * Cross-chip operations
47540ef2c93SVivien Didelot 	 */
47640ef2c93SVivien Didelot 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
47740ef2c93SVivien Didelot 					 int port, struct net_device *br);
47840ef2c93SVivien Didelot 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
47940ef2c93SVivien Didelot 					  int port, struct net_device *br);
4800336369dSBrandon Streiff 
4810336369dSBrandon Streiff 	/*
4820336369dSBrandon Streiff 	 * PTP functionality
4830336369dSBrandon Streiff 	 */
4840336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
4850336369dSBrandon Streiff 				     struct ifreq *ifr);
4860336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
4870336369dSBrandon Streiff 				     struct ifreq *ifr);
48890af1059SBrandon Streiff 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
48990af1059SBrandon Streiff 				 struct sk_buff *clone, unsigned int type);
49090af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
49190af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
492c8f0b869SBen Hutchings };
493c8f0b869SBen Hutchings 
494ab3d408dSFlorian Fainelli struct dsa_switch_driver {
495ab3d408dSFlorian Fainelli 	struct list_head	list;
496a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
497ab3d408dSFlorian Fainelli };
498ab3d408dSFlorian Fainelli 
4992a93c1a3SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
500a6a71f19SVivien Didelot /* Legacy driver registration */
501ab3d408dSFlorian Fainelli void register_switch_driver(struct dsa_switch_driver *type);
502ab3d408dSFlorian Fainelli void unregister_switch_driver(struct dsa_switch_driver *type);
503b4d2394dSAlexander Duyck struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
504a6a71f19SVivien Didelot 
5052a93c1a3SFlorian Fainelli #else
5062a93c1a3SFlorian Fainelli static inline void register_switch_driver(struct dsa_switch_driver *type) { }
5072a93c1a3SFlorian Fainelli static inline void unregister_switch_driver(struct dsa_switch_driver *type) { }
5082a93c1a3SFlorian Fainelli static inline struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
5092a93c1a3SFlorian Fainelli {
5102a93c1a3SFlorian Fainelli 	return NULL;
5112a93c1a3SFlorian Fainelli }
5122a93c1a3SFlorian Fainelli #endif
51314b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
514c8f0b869SBen Hutchings 
51573a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
516c6e970a0SAndrew Lunn static inline bool netdev_uses_dsa(struct net_device *dev)
517c6e970a0SAndrew Lunn {
518c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
519717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
520c6e970a0SAndrew Lunn #endif
521c6e970a0SAndrew Lunn 	return false;
522c6e970a0SAndrew Lunn }
523c6e970a0SAndrew Lunn 
524a0c02161SVivien Didelot struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
52583c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
52623c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
527ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
528ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
529ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
530ea825e70SFlorian Fainelli #else
531ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
532ea825e70SFlorian Fainelli {
533ea825e70SFlorian Fainelli 	return 0;
534ea825e70SFlorian Fainelli }
535ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
536ea825e70SFlorian Fainelli {
537ea825e70SFlorian Fainelli 	return 0;
538ea825e70SFlorian Fainelli }
539ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
540ea825e70SFlorian Fainelli 
54160724d4bSFlorian Fainelli enum dsa_notifier_type {
54260724d4bSFlorian Fainelli 	DSA_PORT_REGISTER,
54360724d4bSFlorian Fainelli 	DSA_PORT_UNREGISTER,
54460724d4bSFlorian Fainelli };
54560724d4bSFlorian Fainelli 
54660724d4bSFlorian Fainelli struct dsa_notifier_info {
54760724d4bSFlorian Fainelli 	struct net_device *dev;
54860724d4bSFlorian Fainelli };
54960724d4bSFlorian Fainelli 
55060724d4bSFlorian Fainelli struct dsa_notifier_register_info {
55160724d4bSFlorian Fainelli 	struct dsa_notifier_info info;	/* must be first */
55260724d4bSFlorian Fainelli 	struct net_device *master;
55360724d4bSFlorian Fainelli 	unsigned int port_number;
55460724d4bSFlorian Fainelli 	unsigned int switch_number;
55560724d4bSFlorian Fainelli };
55660724d4bSFlorian Fainelli 
55760724d4bSFlorian Fainelli static inline struct net_device *
55860724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
55960724d4bSFlorian Fainelli {
56060724d4bSFlorian Fainelli 	return info->dev;
56160724d4bSFlorian Fainelli }
56260724d4bSFlorian Fainelli 
56360724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
56460724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb);
56560724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb);
56660724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev,
56760724d4bSFlorian Fainelli 		       struct dsa_notifier_info *info);
56860724d4bSFlorian Fainelli #else
56960724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb)
57060724d4bSFlorian Fainelli {
57160724d4bSFlorian Fainelli 	return 0;
57260724d4bSFlorian Fainelli }
57360724d4bSFlorian Fainelli 
57460724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb)
57560724d4bSFlorian Fainelli {
57660724d4bSFlorian Fainelli 	return 0;
57760724d4bSFlorian Fainelli }
57860724d4bSFlorian Fainelli 
57960724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
58060724d4bSFlorian Fainelli 				     struct dsa_notifier_info *info)
58160724d4bSFlorian Fainelli {
58260724d4bSFlorian Fainelli 	return NOTIFY_DONE;
58360724d4bSFlorian Fainelli }
58460724d4bSFlorian Fainelli #endif
58560724d4bSFlorian Fainelli 
5860a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */
5870a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q)	((p) << 8 | q)
5880a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v)		((v) >> 8)
5890a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v)		((v) & 0xff)
5900a5f14ceSFlorian Fainelli 
591cf963573SFlorian Fainelli 
592cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
593cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
594cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
59511d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
596cf963573SFlorian Fainelli 
597*d3b8c049SAndrew Lunn struct dsa_tag_driver {
598*d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
599*d3b8c049SAndrew Lunn 	struct list_head list;
600*d3b8c049SAndrew Lunn 	struct module *owner;
601*d3b8c049SAndrew Lunn };
602*d3b8c049SAndrew Lunn 
603*d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
604*d3b8c049SAndrew Lunn 			      unsigned int count,
605*d3b8c049SAndrew Lunn 			      struct module *owner);
606*d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
607*d3b8c049SAndrew Lunn 				unsigned int count);
608*d3b8c049SAndrew Lunn 
609*d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
610*d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
611*d3b8c049SAndrew Lunn {									\
612*d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
613*d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
614*d3b8c049SAndrew Lunn 	return 0;							\
615*d3b8c049SAndrew Lunn }									\
616*d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
617*d3b8c049SAndrew Lunn 									\
618*d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
619*d3b8c049SAndrew Lunn {									\
620*d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
621*d3b8c049SAndrew Lunn }									\
622*d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
623*d3b8c049SAndrew Lunn 
624*d3b8c049SAndrew Lunn /**
625*d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
626*d3b8c049SAndrew Lunn  * drivers
627*d3b8c049SAndrew Lunn  * @__ops_array: Array of tag driver strucutres
628*d3b8c049SAndrew Lunn  *
629*d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
630*d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
631*d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
632*d3b8c049SAndrew Lunn  */
633*d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
634*d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
635*d3b8c049SAndrew Lunn 
636*d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
637*d3b8c049SAndrew Lunn 
638*d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
639*d3b8c049SAndrew Lunn  * drivers
640*d3b8c049SAndrew Lunn  */
641*d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
642*d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
643*d3b8c049SAndrew Lunn 	.ops = &__ops,							\
644*d3b8c049SAndrew Lunn }
645*d3b8c049SAndrew Lunn 
646*d3b8c049SAndrew Lunn /**
647*d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
648*d3b8c049SAndrew Lunn  * driver
649*d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
650*d3b8c049SAndrew Lunn  *
651*d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
652*d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
653*d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
654*d3b8c049SAndrew Lunn  */
655*d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
656*d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
657*d3b8c049SAndrew Lunn 									\
658*d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
659*d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
660*d3b8c049SAndrew Lunn };									\
661*d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
66291da11f8SLennert Buytenhek #endif
663*d3b8c049SAndrew Lunn 
664