xref: /openbmc/linux/include/net/dsa.h (revision 90af1059)
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>
2396567d5dSAndrew Lunn #include <net/devlink.h>
24f0c24ccfSVivien Didelot #include <net/switchdev.h>
25cf50dcc2SBen Hutchings 
26f50f2127SFlorian Fainelli struct tc_action;
274d56a29fSRussell King struct phy_device;
284d56a29fSRussell King struct fixed_phy_status;
29f50f2127SFlorian Fainelli 
30ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
31ac7a04c3SFlorian Fainelli 	DSA_TAG_PROTO_NONE = 0,
32ac7a04c3SFlorian Fainelli 	DSA_TAG_PROTO_BRCM,
33b74b70c4SFlorian Fainelli 	DSA_TAG_PROTO_BRCM_PREPEND,
34eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_DSA,
35eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_EDSA,
368b8010fbSWoojung Huh 	DSA_TAG_PROTO_KSZ,
37e8fe177aSJuergen Beisert 	DSA_TAG_PROTO_LAN9303,
38eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_MTK,
39eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_QCA,
40eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_TRAILER,
4139a7f2a4SAndrew Lunn 	DSA_TAG_LAST,		/* MUST BE LAST */
42ac7a04c3SFlorian Fainelli };
435037d532SFlorian Fainelli 
44e84665c9SLennert Buytenhek #define DSA_MAX_SWITCHES	4
4591da11f8SLennert Buytenhek #define DSA_MAX_PORTS		12
4691da11f8SLennert Buytenhek 
47d390238cSVivien Didelot #define DSA_RTABLE_NONE		-1
48d390238cSVivien Didelot 
49e84665c9SLennert Buytenhek struct dsa_chip_data {
50e84665c9SLennert Buytenhek 	/*
51e84665c9SLennert Buytenhek 	 * How to access the switch configuration registers.
52e84665c9SLennert Buytenhek 	 */
53b4d2394dSAlexander Duyck 	struct device	*host_dev;
54e84665c9SLennert Buytenhek 	int		sw_addr;
55e84665c9SLennert Buytenhek 
5671e0bbdeSFlorian Fainelli 	/*
5771e0bbdeSFlorian Fainelli 	 * Reference to network devices
5871e0bbdeSFlorian Fainelli 	 */
5971e0bbdeSFlorian Fainelli 	struct device	*netdev[DSA_MAX_PORTS];
6071e0bbdeSFlorian Fainelli 
616793abb4SGuenter Roeck 	/* set to size of eeprom if supported by the switch */
626793abb4SGuenter Roeck 	int		eeprom_len;
636793abb4SGuenter Roeck 
64fa981d9aSFlorian Fainelli 	/* Device tree node pointer for this specific switch chip
65fa981d9aSFlorian Fainelli 	 * used during switch setup in case additional properties
66fa981d9aSFlorian Fainelli 	 * and resources needs to be used
67fa981d9aSFlorian Fainelli 	 */
68fa981d9aSFlorian Fainelli 	struct device_node *of_node;
69fa981d9aSFlorian Fainelli 
70e84665c9SLennert Buytenhek 	/*
71e84665c9SLennert Buytenhek 	 * The names of the switch's ports.  Use "cpu" to
72e84665c9SLennert Buytenhek 	 * designate the switch port that the cpu is connected to,
73e84665c9SLennert Buytenhek 	 * "dsa" to indicate that this port is a DSA link to
74e84665c9SLennert Buytenhek 	 * another switch, NULL to indicate the port is unused,
75e84665c9SLennert Buytenhek 	 * or any other string to indicate this is a physical port.
76e84665c9SLennert Buytenhek 	 */
77e84665c9SLennert Buytenhek 	char		*port_names[DSA_MAX_PORTS];
78bd47497aSFlorian Fainelli 	struct device_node *port_dn[DSA_MAX_PORTS];
79e84665c9SLennert Buytenhek 
80e84665c9SLennert Buytenhek 	/*
814a7704ffSAndrew Lunn 	 * An array of which element [a] indicates which port on this
824a7704ffSAndrew Lunn 	 * switch should be used to send packets to that are destined
834a7704ffSAndrew Lunn 	 * for switch a. Can be NULL if there is only one switch chip.
84e84665c9SLennert Buytenhek 	 */
854a7704ffSAndrew Lunn 	s8		rtable[DSA_MAX_SWITCHES];
86e84665c9SLennert Buytenhek };
87e84665c9SLennert Buytenhek 
8891da11f8SLennert Buytenhek struct dsa_platform_data {
8991da11f8SLennert Buytenhek 	/*
9091da11f8SLennert Buytenhek 	 * Reference to a Linux network interface that connects
91e84665c9SLennert Buytenhek 	 * to the root switch chip of the tree.
9291da11f8SLennert Buytenhek 	 */
9391da11f8SLennert Buytenhek 	struct device	*netdev;
94769a0202SFlorian Fainelli 	struct net_device *of_netdev;
9591da11f8SLennert Buytenhek 
9691da11f8SLennert Buytenhek 	/*
97e84665c9SLennert Buytenhek 	 * Info structs describing each of the switch chips
98e84665c9SLennert Buytenhek 	 * connected via this network interface.
9991da11f8SLennert Buytenhek 	 */
100e84665c9SLennert Buytenhek 	int		nr_chips;
101e84665c9SLennert Buytenhek 	struct dsa_chip_data	*chip;
10291da11f8SLennert Buytenhek };
10391da11f8SLennert Buytenhek 
1045075314eSAlexander Duyck struct packet_type;
10590af1059SBrandon Streiff struct dsa_switch;
1063e8a72d1SFlorian Fainelli 
10768277a2cSJohn Crispin struct dsa_device_ops {
10868277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
10968277a2cSJohn Crispin 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
11089e49506SFlorian Westphal 			       struct packet_type *pt);
111598a9680SJohn Crispin 	int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
112598a9680SJohn Crispin 			    int *offset);
11368277a2cSJohn Crispin };
11468277a2cSJohn Crispin 
115cf50dcc2SBen Hutchings struct dsa_switch_tree {
11683c0afaeSAndrew Lunn 	struct list_head	list;
11783c0afaeSAndrew Lunn 
118f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
119f515f192SVivien Didelot 	struct raw_notifier_head	nh;
120f515f192SVivien Didelot 
12183c0afaeSAndrew Lunn 	/* Tree identifier */
12249463b7fSVivien Didelot 	unsigned int index;
12383c0afaeSAndrew Lunn 
12483c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
12583c0afaeSAndrew Lunn 	struct kref refcount;
12683c0afaeSAndrew Lunn 
12783c0afaeSAndrew Lunn 	/* Has this tree been applied to the hardware? */
128ec15dd42SVivien Didelot 	bool setup;
12983c0afaeSAndrew Lunn 
130cf50dcc2SBen Hutchings 	/*
131cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
132cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
133cf50dcc2SBen Hutchings 	 */
134cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
135cf85d08fSLennert Buytenhek 
136cf50dcc2SBen Hutchings 	/*
1378b0d3ea5SVivien Didelot 	 * The switch port to which the CPU is attached.
138cf50dcc2SBen Hutchings 	 */
1398b0d3ea5SVivien Didelot 	struct dsa_port		*cpu_dp;
140cf50dcc2SBen Hutchings 
141cf50dcc2SBen Hutchings 	/*
142cf50dcc2SBen Hutchings 	 * Data for the individual switch chips.
143cf50dcc2SBen Hutchings 	 */
144cf50dcc2SBen Hutchings 	struct dsa_switch	*ds[DSA_MAX_SWITCHES];
145cf50dcc2SBen Hutchings };
146cf50dcc2SBen Hutchings 
147f50f2127SFlorian Fainelli /* TC matchall action types, only mirroring for now */
148f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
149f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
150f50f2127SFlorian Fainelli };
151f50f2127SFlorian Fainelli 
152f50f2127SFlorian Fainelli /* TC mirroring entry */
153f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
154f50f2127SFlorian Fainelli 	u8 to_local_port;
155f50f2127SFlorian Fainelli 	bool ingress;
156f50f2127SFlorian Fainelli };
157f50f2127SFlorian Fainelli 
158f50f2127SFlorian Fainelli /* TC matchall entry */
159f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
160f50f2127SFlorian Fainelli 	struct list_head list;
161f50f2127SFlorian Fainelli 	unsigned long cookie;
162f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
163f50f2127SFlorian Fainelli 	union {
164f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
165f50f2127SFlorian Fainelli 	};
166f50f2127SFlorian Fainelli };
167f50f2127SFlorian Fainelli 
168f50f2127SFlorian Fainelli 
169c8b09808SAndrew Lunn struct dsa_port {
170f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
171f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
172f8b8b1cdSVivien Didelot 	 */
173f8b8b1cdSVivien Didelot 	union {
174f8b8b1cdSVivien Didelot 		struct net_device *master;
175f8b8b1cdSVivien Didelot 		struct net_device *slave;
176f8b8b1cdSVivien Didelot 	};
177f8b8b1cdSVivien Didelot 
17815240248SVivien Didelot 	/* CPU port tagging operations used by master or slave devices */
17915240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
18015240248SVivien Didelot 
1813e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
1823e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
1833e41f93bSVivien Didelot 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
1843e41f93bSVivien Didelot 			       struct packet_type *pt);
1853e41f93bSVivien Didelot 
186057cad2cSVivien Didelot 	enum {
187057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
188057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
189057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
190057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
191057cad2cSVivien Didelot 	} type;
192057cad2cSVivien Didelot 
193818be848SVivien Didelot 	struct dsa_switch	*ds;
194818be848SVivien Didelot 	unsigned int		index;
19571e0bbdeSFlorian Fainelli 	const char		*name;
19624a9332aSVivien Didelot 	const struct dsa_port	*cpu_dp;
197189b0d93SAndrew Lunn 	struct device_node	*dn;
19834a79f63SVivien Didelot 	unsigned int		ageing_time;
199732f794cSVivien Didelot 	u8			stp_state;
200a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
20196567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
20267dbb9d4SFlorian Fainelli 	/*
20367dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
20467dbb9d4SFlorian Fainelli 	 */
20567dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
206c8b09808SAndrew Lunn };
207c8b09808SAndrew Lunn 
208c8f0b869SBen Hutchings struct dsa_switch {
209c33063d6SAndrew Lunn 	struct device *dev;
210c33063d6SAndrew Lunn 
211c8f0b869SBen Hutchings 	/*
212c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
213c8f0b869SBen Hutchings 	 */
214c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
21599feaafcSVivien Didelot 	unsigned int		index;
216c8f0b869SBen Hutchings 
217f515f192SVivien Didelot 	/* Listener for switch fabric events */
218f515f192SVivien Didelot 	struct notifier_block	nb;
219f515f192SVivien Didelot 
220c8f0b869SBen Hutchings 	/*
2217543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
2227543a6d5SAndrew Lunn 	 * structure.
2237543a6d5SAndrew Lunn 	 */
2247543a6d5SAndrew Lunn 	void *priv;
2257543a6d5SAndrew Lunn 
2267543a6d5SAndrew Lunn 	/*
227c8f0b869SBen Hutchings 	 * Configuration data for this switch.
228c8f0b869SBen Hutchings 	 */
229ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
230c8f0b869SBen Hutchings 
231c8f0b869SBen Hutchings 	/*
2329d490b4eSVivien Didelot 	 * The switch operations.
233c8f0b869SBen Hutchings 	 */
234a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
235c8f0b869SBen Hutchings 
23666472fc0SAndrew Lunn 	/*
23766472fc0SAndrew Lunn 	 * An array of which element [a] indicates which port on this
23866472fc0SAndrew Lunn 	 * switch should be used to send packets to that are destined
23966472fc0SAndrew Lunn 	 * for switch a. Can be NULL if there is only one switch chip.
24066472fc0SAndrew Lunn 	 */
24166472fc0SAndrew Lunn 	s8		rtable[DSA_MAX_SWITCHES];
24266472fc0SAndrew Lunn 
243c8f0b869SBen Hutchings 	/*
244c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
245c8f0b869SBen Hutchings 	 */
2460d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
247c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
248a0c02161SVivien Didelot 
2490f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
2500f3da6afSVivien Didelot 	unsigned int ageing_time_min;
2510f3da6afSVivien Didelot 	unsigned int ageing_time_max;
2520f3da6afSVivien Didelot 
25396567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
25496567d5dSAndrew Lunn 	struct devlink		*devlink;
25596567d5dSAndrew Lunn 
25655199df6SFlorian Fainelli 	/* Number of switch port queues */
25755199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
25855199df6SFlorian Fainelli 
259a0c02161SVivien Didelot 	/* Dynamically allocated ports, keep last */
260a0c02161SVivien Didelot 	size_t num_ports;
261a0c02161SVivien Didelot 	struct dsa_port ports[];
262c8f0b869SBen Hutchings };
263c8f0b869SBen Hutchings 
264c38c5a66SVivien Didelot static inline const struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
265c38c5a66SVivien Didelot {
266c38c5a66SVivien Didelot 	return &ds->ports[p];
267c38c5a66SVivien Didelot }
268c38c5a66SVivien Didelot 
269bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
270bff7b688SVivien Didelot {
271c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
272bff7b688SVivien Didelot }
273bff7b688SVivien Didelot 
274c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
275c8f0b869SBen Hutchings {
276c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
277c8f0b869SBen Hutchings }
278c8f0b869SBen Hutchings 
27960045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
28060045cbfSAndrew Lunn {
281c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
28260045cbfSAndrew Lunn }
28360045cbfSAndrew Lunn 
2842b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
2856cd456f3SVivien Didelot {
286c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
2876cd456f3SVivien Didelot }
2886cd456f3SVivien Didelot 
28902bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
29002bc6e54SVivien Didelot {
291c38c5a66SVivien Didelot 	u32 mask = 0;
292c38c5a66SVivien Didelot 	int p;
29302bc6e54SVivien Didelot 
294c38c5a66SVivien Didelot 	for (p = 0; p < ds->num_ports; p++)
295c38c5a66SVivien Didelot 		if (dsa_is_user_port(ds, p))
296c38c5a66SVivien Didelot 			mask |= BIT(p);
297c38c5a66SVivien Didelot 
298c38c5a66SVivien Didelot 	return mask;
299c8652c83SVivien Didelot }
300c8652c83SVivien Didelot 
3013b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
3023b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
3033b8fac5dSVivien Didelot 					    int port)
3043b8fac5dSVivien Didelot {
3053b8fac5dSVivien Didelot 	if (device == ds->index)
3063b8fac5dSVivien Didelot 		return port;
3073b8fac5dSVivien Didelot 	else
3083b8fac5dSVivien Didelot 		return ds->rtable[device];
3093b8fac5dSVivien Didelot }
3103b8fac5dSVivien Didelot 
3113b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
31207073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
313c8f0b869SBen Hutchings {
31407073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
31507073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
31607073c79SVivien Didelot 
31707073c79SVivien Didelot 	if (!cpu_dp)
31807073c79SVivien Didelot 		return port;
319c8f0b869SBen Hutchings 
3203b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
321c8f0b869SBen Hutchings }
322c8f0b869SBen Hutchings 
3232bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
3242bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
3259d490b4eSVivien Didelot struct dsa_switch_ops {
3262a93c1a3SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
327c8f0b869SBen Hutchings 	/*
328a6a71f19SVivien Didelot 	 * Legacy probing.
329c8f0b869SBen Hutchings 	 */
3300209d144SVivien Didelot 	const char	*(*probe)(struct device *dsa_dev,
3310209d144SVivien Didelot 				  struct device *host_dev, int sw_addr,
3320209d144SVivien Didelot 				  void **priv);
3332a93c1a3SFlorian Fainelli #endif
3347b314362SAndrew Lunn 
3355ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
3365ed4e3ebSFlorian Fainelli 						  int port);
3377b314362SAndrew Lunn 
338c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
3396819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
340c8f0b869SBen Hutchings 
341c8f0b869SBen Hutchings 	/*
342c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
343c8f0b869SBen Hutchings 	 */
344c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
345c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
346c8f0b869SBen Hutchings 			     int regnum, u16 val);
347c8f0b869SBen Hutchings 
348c8f0b869SBen Hutchings 	/*
349ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
350ec9436baSFlorian Fainelli 	 */
351ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
352ec9436baSFlorian Fainelli 				struct phy_device *phydev);
353ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
354ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
355ec9436baSFlorian Fainelli 
356ec9436baSFlorian Fainelli 	/*
357c8f0b869SBen Hutchings 	 * ethtool hardware statistics.
358c8f0b869SBen Hutchings 	 */
359c8f0b869SBen Hutchings 	void	(*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
360c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
361c8f0b869SBen Hutchings 				     int port, uint64_t *data);
362c8f0b869SBen Hutchings 	int	(*get_sset_count)(struct dsa_switch *ds);
36324462549SFlorian Fainelli 
36424462549SFlorian Fainelli 	/*
36519e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
36619e57c4eSFlorian Fainelli 	 */
36719e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
36819e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
36919e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
37019e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
37119e57c4eSFlorian Fainelli 
37219e57c4eSFlorian Fainelli 	/*
3730336369dSBrandon Streiff 	 * ethtool timestamp info
3740336369dSBrandon Streiff 	 */
3750336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
3760336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
3770336369dSBrandon Streiff 
3780336369dSBrandon Streiff 	/*
37924462549SFlorian Fainelli 	 * Suspend and resume
38024462549SFlorian Fainelli 	 */
38124462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
38224462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
383b2f2af21SFlorian Fainelli 
384b2f2af21SFlorian Fainelli 	/*
385b2f2af21SFlorian Fainelli 	 * Port enable/disable
386b2f2af21SFlorian Fainelli 	 */
387b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
388b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
389b2f2af21SFlorian Fainelli 	void	(*port_disable)(struct dsa_switch *ds, int port,
390b2f2af21SFlorian Fainelli 				struct phy_device *phy);
3917905288fSFlorian Fainelli 
3927905288fSFlorian Fainelli 	/*
39308f50061SVivien Didelot 	 * Port's MAC EEE settings
3947905288fSFlorian Fainelli 	 */
39508f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
3967905288fSFlorian Fainelli 			       struct ethtool_eee *e);
39708f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
3987905288fSFlorian Fainelli 			       struct ethtool_eee *e);
39951579c3fSGuenter Roeck 
4006793abb4SGuenter Roeck 	/* EEPROM access */
4016793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
4026793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
4036793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4046793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
4056793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4063d762a0fSGuenter Roeck 
4073d762a0fSGuenter Roeck 	/*
4083d762a0fSGuenter Roeck 	 * Register access.
4093d762a0fSGuenter Roeck 	 */
4103d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
4113d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
4123d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
413b73adef6SFlorian Fainelli 
414b73adef6SFlorian Fainelli 	/*
415b73adef6SFlorian Fainelli 	 * Bridge integration
416b73adef6SFlorian Fainelli 	 */
41734a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
41871327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
419a6692754SVivien Didelot 				    struct net_device *bridge);
420f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
421f123f2fbSVivien Didelot 				     struct net_device *bridge);
42243c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
423b73adef6SFlorian Fainelli 				      u8 state);
424732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
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 
59191da11f8SLennert Buytenhek #endif
592