xref: /openbmc/linux/include/net/dsa.h (revision 79691192)
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>
2496567d5dSAndrew Lunn #include <net/devlink.h>
25f0c24ccfSVivien Didelot #include <net/switchdev.h>
26cf50dcc2SBen Hutchings 
27f50f2127SFlorian Fainelli struct tc_action;
284d56a29fSRussell King struct phy_device;
294d56a29fSRussell King struct fixed_phy_status;
3011d8f3ddSFlorian Fainelli struct phylink_link_state;
31f50f2127SFlorian Fainelli 
32ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
33ac7a04c3SFlorian Fainelli 	DSA_TAG_PROTO_NONE = 0,
34ac7a04c3SFlorian Fainelli 	DSA_TAG_PROTO_BRCM,
35b74b70c4SFlorian Fainelli 	DSA_TAG_PROTO_BRCM_PREPEND,
36eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_DSA,
37eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_EDSA,
3879691192SHauke Mehrtens 	DSA_TAG_PROTO_GSWIP,
398b8010fbSWoojung Huh 	DSA_TAG_PROTO_KSZ,
40e8fe177aSJuergen Beisert 	DSA_TAG_PROTO_LAN9303,
41eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_MTK,
42eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_QCA,
43eb7b7211SAndrew Lunn 	DSA_TAG_PROTO_TRAILER,
4439a7f2a4SAndrew Lunn 	DSA_TAG_LAST,		/* MUST BE LAST */
45ac7a04c3SFlorian Fainelli };
465037d532SFlorian Fainelli 
47e84665c9SLennert Buytenhek #define DSA_MAX_SWITCHES	4
4891da11f8SLennert Buytenhek #define DSA_MAX_PORTS		12
4991da11f8SLennert Buytenhek 
50d390238cSVivien Didelot #define DSA_RTABLE_NONE		-1
51d390238cSVivien Didelot 
52e84665c9SLennert Buytenhek struct dsa_chip_data {
53e84665c9SLennert Buytenhek 	/*
54e84665c9SLennert Buytenhek 	 * How to access the switch configuration registers.
55e84665c9SLennert Buytenhek 	 */
56b4d2394dSAlexander Duyck 	struct device	*host_dev;
57e84665c9SLennert Buytenhek 	int		sw_addr;
58e84665c9SLennert Buytenhek 
5971e0bbdeSFlorian Fainelli 	/*
6071e0bbdeSFlorian Fainelli 	 * Reference to network devices
6171e0bbdeSFlorian Fainelli 	 */
6271e0bbdeSFlorian Fainelli 	struct device	*netdev[DSA_MAX_PORTS];
6371e0bbdeSFlorian Fainelli 
646793abb4SGuenter Roeck 	/* set to size of eeprom if supported by the switch */
656793abb4SGuenter Roeck 	int		eeprom_len;
666793abb4SGuenter Roeck 
67fa981d9aSFlorian Fainelli 	/* Device tree node pointer for this specific switch chip
68fa981d9aSFlorian Fainelli 	 * used during switch setup in case additional properties
69fa981d9aSFlorian Fainelli 	 * and resources needs to be used
70fa981d9aSFlorian Fainelli 	 */
71fa981d9aSFlorian Fainelli 	struct device_node *of_node;
72fa981d9aSFlorian Fainelli 
73e84665c9SLennert Buytenhek 	/*
74e84665c9SLennert Buytenhek 	 * The names of the switch's ports.  Use "cpu" to
75e84665c9SLennert Buytenhek 	 * designate the switch port that the cpu is connected to,
76e84665c9SLennert Buytenhek 	 * "dsa" to indicate that this port is a DSA link to
77e84665c9SLennert Buytenhek 	 * another switch, NULL to indicate the port is unused,
78e84665c9SLennert Buytenhek 	 * or any other string to indicate this is a physical port.
79e84665c9SLennert Buytenhek 	 */
80e84665c9SLennert Buytenhek 	char		*port_names[DSA_MAX_PORTS];
81bd47497aSFlorian Fainelli 	struct device_node *port_dn[DSA_MAX_PORTS];
82e84665c9SLennert Buytenhek 
83e84665c9SLennert Buytenhek 	/*
844a7704ffSAndrew Lunn 	 * An array of which element [a] indicates which port on this
854a7704ffSAndrew Lunn 	 * switch should be used to send packets to that are destined
864a7704ffSAndrew Lunn 	 * for switch a. Can be NULL if there is only one switch chip.
87e84665c9SLennert Buytenhek 	 */
884a7704ffSAndrew Lunn 	s8		rtable[DSA_MAX_SWITCHES];
89e84665c9SLennert Buytenhek };
90e84665c9SLennert Buytenhek 
9191da11f8SLennert Buytenhek struct dsa_platform_data {
9291da11f8SLennert Buytenhek 	/*
9391da11f8SLennert Buytenhek 	 * Reference to a Linux network interface that connects
94e84665c9SLennert Buytenhek 	 * to the root switch chip of the tree.
9591da11f8SLennert Buytenhek 	 */
9691da11f8SLennert Buytenhek 	struct device	*netdev;
97769a0202SFlorian Fainelli 	struct net_device *of_netdev;
9891da11f8SLennert Buytenhek 
9991da11f8SLennert Buytenhek 	/*
100e84665c9SLennert Buytenhek 	 * Info structs describing each of the switch chips
101e84665c9SLennert Buytenhek 	 * connected via this network interface.
10291da11f8SLennert Buytenhek 	 */
103e84665c9SLennert Buytenhek 	int		nr_chips;
104e84665c9SLennert Buytenhek 	struct dsa_chip_data	*chip;
10591da11f8SLennert Buytenhek };
10691da11f8SLennert Buytenhek 
1075075314eSAlexander Duyck struct packet_type;
10890af1059SBrandon Streiff struct dsa_switch;
1093e8a72d1SFlorian Fainelli 
11068277a2cSJohn Crispin struct dsa_device_ops {
11168277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
11268277a2cSJohn Crispin 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
11389e49506SFlorian Westphal 			       struct packet_type *pt);
114598a9680SJohn Crispin 	int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
115598a9680SJohn Crispin 			    int *offset);
11668277a2cSJohn Crispin };
11768277a2cSJohn Crispin 
118cf50dcc2SBen Hutchings struct dsa_switch_tree {
11983c0afaeSAndrew Lunn 	struct list_head	list;
12083c0afaeSAndrew Lunn 
121f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
122f515f192SVivien Didelot 	struct raw_notifier_head	nh;
123f515f192SVivien Didelot 
12483c0afaeSAndrew Lunn 	/* Tree identifier */
12549463b7fSVivien Didelot 	unsigned int index;
12683c0afaeSAndrew Lunn 
12783c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
12883c0afaeSAndrew Lunn 	struct kref refcount;
12983c0afaeSAndrew Lunn 
13083c0afaeSAndrew Lunn 	/* Has this tree been applied to the hardware? */
131ec15dd42SVivien Didelot 	bool setup;
13283c0afaeSAndrew Lunn 
133cf50dcc2SBen Hutchings 	/*
134cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
135cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
136cf50dcc2SBen Hutchings 	 */
137cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
138cf85d08fSLennert Buytenhek 
139cf50dcc2SBen Hutchings 	/*
1408b0d3ea5SVivien Didelot 	 * The switch port to which the CPU is attached.
141cf50dcc2SBen Hutchings 	 */
1428b0d3ea5SVivien Didelot 	struct dsa_port		*cpu_dp;
143cf50dcc2SBen Hutchings 
144cf50dcc2SBen Hutchings 	/*
145cf50dcc2SBen Hutchings 	 * Data for the individual switch chips.
146cf50dcc2SBen Hutchings 	 */
147cf50dcc2SBen Hutchings 	struct dsa_switch	*ds[DSA_MAX_SWITCHES];
148cf50dcc2SBen Hutchings };
149cf50dcc2SBen Hutchings 
150f50f2127SFlorian Fainelli /* TC matchall action types, only mirroring for now */
151f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
152f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
153f50f2127SFlorian Fainelli };
154f50f2127SFlorian Fainelli 
155f50f2127SFlorian Fainelli /* TC mirroring entry */
156f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
157f50f2127SFlorian Fainelli 	u8 to_local_port;
158f50f2127SFlorian Fainelli 	bool ingress;
159f50f2127SFlorian Fainelli };
160f50f2127SFlorian Fainelli 
161f50f2127SFlorian Fainelli /* TC matchall entry */
162f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
163f50f2127SFlorian Fainelli 	struct list_head list;
164f50f2127SFlorian Fainelli 	unsigned long cookie;
165f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
166f50f2127SFlorian Fainelli 	union {
167f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
168f50f2127SFlorian Fainelli 	};
169f50f2127SFlorian Fainelli };
170f50f2127SFlorian Fainelli 
171f50f2127SFlorian Fainelli 
172c8b09808SAndrew Lunn struct dsa_port {
173f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
174f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
175f8b8b1cdSVivien Didelot 	 */
176f8b8b1cdSVivien Didelot 	union {
177f8b8b1cdSVivien Didelot 		struct net_device *master;
178f8b8b1cdSVivien Didelot 		struct net_device *slave;
179f8b8b1cdSVivien Didelot 	};
180f8b8b1cdSVivien Didelot 
18115240248SVivien Didelot 	/* CPU port tagging operations used by master or slave devices */
18215240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
18315240248SVivien Didelot 
1843e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
1853e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
1863e41f93bSVivien Didelot 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
1873e41f93bSVivien Didelot 			       struct packet_type *pt);
1883e41f93bSVivien Didelot 
189057cad2cSVivien Didelot 	enum {
190057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
191057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
192057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
193057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
194057cad2cSVivien Didelot 	} type;
195057cad2cSVivien Didelot 
196818be848SVivien Didelot 	struct dsa_switch	*ds;
197818be848SVivien Didelot 	unsigned int		index;
19871e0bbdeSFlorian Fainelli 	const char		*name;
19924a9332aSVivien Didelot 	const struct dsa_port	*cpu_dp;
200189b0d93SAndrew Lunn 	struct device_node	*dn;
20134a79f63SVivien Didelot 	unsigned int		ageing_time;
202732f794cSVivien Didelot 	u8			stp_state;
203a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
20496567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
205aab9c406SFlorian Fainelli 	struct phylink		*pl;
20667dbb9d4SFlorian Fainelli 	/*
20767dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
20867dbb9d4SFlorian Fainelli 	 */
20967dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
210c8b09808SAndrew Lunn };
211c8b09808SAndrew Lunn 
212c8f0b869SBen Hutchings struct dsa_switch {
213c33063d6SAndrew Lunn 	struct device *dev;
214c33063d6SAndrew Lunn 
215c8f0b869SBen Hutchings 	/*
216c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
217c8f0b869SBen Hutchings 	 */
218c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
21999feaafcSVivien Didelot 	unsigned int		index;
220c8f0b869SBen Hutchings 
221f515f192SVivien Didelot 	/* Listener for switch fabric events */
222f515f192SVivien Didelot 	struct notifier_block	nb;
223f515f192SVivien Didelot 
224c8f0b869SBen Hutchings 	/*
2257543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
2267543a6d5SAndrew Lunn 	 * structure.
2277543a6d5SAndrew Lunn 	 */
2287543a6d5SAndrew Lunn 	void *priv;
2297543a6d5SAndrew Lunn 
2307543a6d5SAndrew Lunn 	/*
231c8f0b869SBen Hutchings 	 * Configuration data for this switch.
232c8f0b869SBen Hutchings 	 */
233ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
234c8f0b869SBen Hutchings 
235c8f0b869SBen Hutchings 	/*
2369d490b4eSVivien Didelot 	 * The switch operations.
237c8f0b869SBen Hutchings 	 */
238a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
239c8f0b869SBen Hutchings 
24066472fc0SAndrew Lunn 	/*
24166472fc0SAndrew Lunn 	 * An array of which element [a] indicates which port on this
24266472fc0SAndrew Lunn 	 * switch should be used to send packets to that are destined
24366472fc0SAndrew Lunn 	 * for switch a. Can be NULL if there is only one switch chip.
24466472fc0SAndrew Lunn 	 */
24566472fc0SAndrew Lunn 	s8		rtable[DSA_MAX_SWITCHES];
24666472fc0SAndrew Lunn 
247c8f0b869SBen Hutchings 	/*
248c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
249c8f0b869SBen Hutchings 	 */
2500d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
251c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
252a0c02161SVivien Didelot 
2530f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
2540f3da6afSVivien Didelot 	unsigned int ageing_time_min;
2550f3da6afSVivien Didelot 	unsigned int ageing_time_max;
2560f3da6afSVivien Didelot 
25796567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
25896567d5dSAndrew Lunn 	struct devlink		*devlink;
25996567d5dSAndrew Lunn 
26055199df6SFlorian Fainelli 	/* Number of switch port queues */
26155199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
26255199df6SFlorian Fainelli 
2630015b80aSSalvatore Mesoraca 	unsigned long		*bitmap;
2640015b80aSSalvatore Mesoraca 	unsigned long		_bitmap;
2650015b80aSSalvatore Mesoraca 
266a0c02161SVivien Didelot 	/* Dynamically allocated ports, keep last */
267a0c02161SVivien Didelot 	size_t num_ports;
268a0c02161SVivien Didelot 	struct dsa_port ports[];
269c8f0b869SBen Hutchings };
270c8f0b869SBen Hutchings 
271c38c5a66SVivien Didelot static inline const struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
272c38c5a66SVivien Didelot {
273c38c5a66SVivien Didelot 	return &ds->ports[p];
274c38c5a66SVivien Didelot }
275c38c5a66SVivien Didelot 
276bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
277bff7b688SVivien Didelot {
278c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
279bff7b688SVivien Didelot }
280bff7b688SVivien Didelot 
281c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
282c8f0b869SBen Hutchings {
283c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
284c8f0b869SBen Hutchings }
285c8f0b869SBen Hutchings 
28660045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
28760045cbfSAndrew Lunn {
288c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
28960045cbfSAndrew Lunn }
29060045cbfSAndrew Lunn 
2912b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
2926cd456f3SVivien Didelot {
293c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
2946cd456f3SVivien Didelot }
2956cd456f3SVivien Didelot 
29602bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
29702bc6e54SVivien Didelot {
298c38c5a66SVivien Didelot 	u32 mask = 0;
299c38c5a66SVivien Didelot 	int p;
30002bc6e54SVivien Didelot 
301c38c5a66SVivien Didelot 	for (p = 0; p < ds->num_ports; p++)
302c38c5a66SVivien Didelot 		if (dsa_is_user_port(ds, p))
303c38c5a66SVivien Didelot 			mask |= BIT(p);
304c38c5a66SVivien Didelot 
305c38c5a66SVivien Didelot 	return mask;
306c8652c83SVivien Didelot }
307c8652c83SVivien Didelot 
3083b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
3093b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
3103b8fac5dSVivien Didelot 					    int port)
3113b8fac5dSVivien Didelot {
3123b8fac5dSVivien Didelot 	if (device == ds->index)
3133b8fac5dSVivien Didelot 		return port;
3143b8fac5dSVivien Didelot 	else
3153b8fac5dSVivien Didelot 		return ds->rtable[device];
3163b8fac5dSVivien Didelot }
3173b8fac5dSVivien Didelot 
3183b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
31907073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
320c8f0b869SBen Hutchings {
32107073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
32207073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
32307073c79SVivien Didelot 
32407073c79SVivien Didelot 	if (!cpu_dp)
32507073c79SVivien Didelot 		return port;
326c8f0b869SBen Hutchings 
3273b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
328c8f0b869SBen Hutchings }
329c8f0b869SBen Hutchings 
3302bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
3312bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
3329d490b4eSVivien Didelot struct dsa_switch_ops {
3332a93c1a3SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
334c8f0b869SBen Hutchings 	/*
335a6a71f19SVivien Didelot 	 * Legacy probing.
336c8f0b869SBen Hutchings 	 */
3370209d144SVivien Didelot 	const char	*(*probe)(struct device *dsa_dev,
3380209d144SVivien Didelot 				  struct device *host_dev, int sw_addr,
3390209d144SVivien Didelot 				  void **priv);
3402a93c1a3SFlorian Fainelli #endif
3417b314362SAndrew Lunn 
3425ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
3435ed4e3ebSFlorian Fainelli 						  int port);
3447b314362SAndrew Lunn 
345c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
3466819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
347c8f0b869SBen Hutchings 
348c8f0b869SBen Hutchings 	/*
349c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
350c8f0b869SBen Hutchings 	 */
351c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
352c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
353c8f0b869SBen Hutchings 			     int regnum, u16 val);
354c8f0b869SBen Hutchings 
355c8f0b869SBen Hutchings 	/*
356ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
357ec9436baSFlorian Fainelli 	 */
358ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
359ec9436baSFlorian Fainelli 				struct phy_device *phydev);
360ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
361ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
362ec9436baSFlorian Fainelli 
363ec9436baSFlorian Fainelli 	/*
36411d8f3ddSFlorian Fainelli 	 * PHYLINK integration
36511d8f3ddSFlorian Fainelli 	 */
36611d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
36711d8f3ddSFlorian Fainelli 				    unsigned long *supported,
36811d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
36911d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
37011d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
37111d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
37211d8f3ddSFlorian Fainelli 				      unsigned int mode,
37311d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
37411d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
37511d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
37611d8f3ddSFlorian Fainelli 					 unsigned int mode,
37711d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
37811d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
37911d8f3ddSFlorian Fainelli 				       unsigned int mode,
38011d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
38111d8f3ddSFlorian Fainelli 				       struct phy_device *phydev);
38211d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
38311d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
38411d8f3ddSFlorian Fainelli 	/*
385c8f0b869SBen Hutchings 	 * ethtool hardware statistics.
386c8f0b869SBen Hutchings 	 */
38789f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
38889f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
389c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
390c8f0b869SBen Hutchings 				     int port, uint64_t *data);
39189f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
392cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
393cf963573SFlorian Fainelli 					 int port, uint64_t *data);
39424462549SFlorian Fainelli 
39524462549SFlorian Fainelli 	/*
39619e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
39719e57c4eSFlorian Fainelli 	 */
39819e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
39919e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
40019e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
40119e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
40219e57c4eSFlorian Fainelli 
40319e57c4eSFlorian Fainelli 	/*
4040336369dSBrandon Streiff 	 * ethtool timestamp info
4050336369dSBrandon Streiff 	 */
4060336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
4070336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
4080336369dSBrandon Streiff 
4090336369dSBrandon Streiff 	/*
41024462549SFlorian Fainelli 	 * Suspend and resume
41124462549SFlorian Fainelli 	 */
41224462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
41324462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
414b2f2af21SFlorian Fainelli 
415b2f2af21SFlorian Fainelli 	/*
416b2f2af21SFlorian Fainelli 	 * Port enable/disable
417b2f2af21SFlorian Fainelli 	 */
418b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
419b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
420b2f2af21SFlorian Fainelli 	void	(*port_disable)(struct dsa_switch *ds, int port,
421b2f2af21SFlorian Fainelli 				struct phy_device *phy);
4227905288fSFlorian Fainelli 
4237905288fSFlorian Fainelli 	/*
42408f50061SVivien Didelot 	 * Port's MAC EEE settings
4257905288fSFlorian Fainelli 	 */
42608f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
4277905288fSFlorian Fainelli 			       struct ethtool_eee *e);
42808f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
4297905288fSFlorian Fainelli 			       struct ethtool_eee *e);
43051579c3fSGuenter Roeck 
4316793abb4SGuenter Roeck 	/* EEPROM access */
4326793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
4336793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
4346793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4356793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
4366793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
4373d762a0fSGuenter Roeck 
4383d762a0fSGuenter Roeck 	/*
4393d762a0fSGuenter Roeck 	 * Register access.
4403d762a0fSGuenter Roeck 	 */
4413d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
4423d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
4433d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
444b73adef6SFlorian Fainelli 
445b73adef6SFlorian Fainelli 	/*
446b73adef6SFlorian Fainelli 	 * Bridge integration
447b73adef6SFlorian Fainelli 	 */
44834a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
44971327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
450a6692754SVivien Didelot 				    struct net_device *bridge);
451f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
452f123f2fbSVivien Didelot 				     struct net_device *bridge);
45343c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
454b73adef6SFlorian Fainelli 				      u8 state);
455732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
4562a778e1bSVivien Didelot 
4572a778e1bSVivien Didelot 	/*
45811149536SVivien Didelot 	 * VLAN support
45911149536SVivien Didelot 	 */
460fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
461fb2dabadSVivien Didelot 				       bool vlan_filtering);
46276e398a6SVivien Didelot 	int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
46380e02360SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
4644d5770b3SVivien Didelot 	void (*port_vlan_add)(struct dsa_switch *ds, int port,
46580e02360SVivien Didelot 			      const struct switchdev_obj_port_vlan *vlan);
46676e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
46776e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
46811149536SVivien Didelot 	/*
4692a778e1bSVivien Didelot 	 * Forwarding database
4702a778e1bSVivien Didelot 	 */
4711b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
4726c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
4732a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
4746c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
475ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
4762bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
4778df30255SVivien Didelot 
4788df30255SVivien Didelot 	/*
4798df30255SVivien Didelot 	 * Multicast database
4808df30255SVivien Didelot 	 */
4818df30255SVivien Didelot 	int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
4823709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
4838df30255SVivien Didelot 	void (*port_mdb_add)(struct dsa_switch *ds, int port,
4843709aadcSVivien Didelot 			     const struct switchdev_obj_port_mdb *mdb);
4858df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
4868df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
487bf9f2648SFlorian Fainelli 	/*
488bf9f2648SFlorian Fainelli 	 * RXNFC
489bf9f2648SFlorian Fainelli 	 */
490bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
491bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
492bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
493bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
494f50f2127SFlorian Fainelli 
495f50f2127SFlorian Fainelli 	/*
496f50f2127SFlorian Fainelli 	 * TC integration
497f50f2127SFlorian Fainelli 	 */
498f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
499f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
500f50f2127SFlorian Fainelli 				   bool ingress);
501f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
502f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
50340ef2c93SVivien Didelot 
50440ef2c93SVivien Didelot 	/*
50540ef2c93SVivien Didelot 	 * Cross-chip operations
50640ef2c93SVivien Didelot 	 */
50740ef2c93SVivien Didelot 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
50840ef2c93SVivien Didelot 					 int port, struct net_device *br);
50940ef2c93SVivien Didelot 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
51040ef2c93SVivien Didelot 					  int port, struct net_device *br);
5110336369dSBrandon Streiff 
5120336369dSBrandon Streiff 	/*
5130336369dSBrandon Streiff 	 * PTP functionality
5140336369dSBrandon Streiff 	 */
5150336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
5160336369dSBrandon Streiff 				     struct ifreq *ifr);
5170336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
5180336369dSBrandon Streiff 				     struct ifreq *ifr);
51990af1059SBrandon Streiff 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
52090af1059SBrandon Streiff 				 struct sk_buff *clone, unsigned int type);
52190af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
52290af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
523c8f0b869SBen Hutchings };
524c8f0b869SBen Hutchings 
525ab3d408dSFlorian Fainelli struct dsa_switch_driver {
526ab3d408dSFlorian Fainelli 	struct list_head	list;
527a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
528ab3d408dSFlorian Fainelli };
529ab3d408dSFlorian Fainelli 
5302a93c1a3SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
531a6a71f19SVivien Didelot /* Legacy driver registration */
532ab3d408dSFlorian Fainelli void register_switch_driver(struct dsa_switch_driver *type);
533ab3d408dSFlorian Fainelli void unregister_switch_driver(struct dsa_switch_driver *type);
534b4d2394dSAlexander Duyck struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
535a6a71f19SVivien Didelot 
5362a93c1a3SFlorian Fainelli #else
5372a93c1a3SFlorian Fainelli static inline void register_switch_driver(struct dsa_switch_driver *type) { }
5382a93c1a3SFlorian Fainelli static inline void unregister_switch_driver(struct dsa_switch_driver *type) { }
5392a93c1a3SFlorian Fainelli static inline struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
5402a93c1a3SFlorian Fainelli {
5412a93c1a3SFlorian Fainelli 	return NULL;
5422a93c1a3SFlorian Fainelli }
5432a93c1a3SFlorian Fainelli #endif
54414b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
545c8f0b869SBen Hutchings 
54673a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
547c6e970a0SAndrew Lunn static inline bool netdev_uses_dsa(struct net_device *dev)
548c6e970a0SAndrew Lunn {
549c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
550717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
551c6e970a0SAndrew Lunn #endif
552c6e970a0SAndrew Lunn 	return false;
553c6e970a0SAndrew Lunn }
554c6e970a0SAndrew Lunn 
555a0c02161SVivien Didelot struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
55683c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
55723c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
558ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
559ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
560ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
561ea825e70SFlorian Fainelli #else
562ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
563ea825e70SFlorian Fainelli {
564ea825e70SFlorian Fainelli 	return 0;
565ea825e70SFlorian Fainelli }
566ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
567ea825e70SFlorian Fainelli {
568ea825e70SFlorian Fainelli 	return 0;
569ea825e70SFlorian Fainelli }
570ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
571ea825e70SFlorian Fainelli 
57260724d4bSFlorian Fainelli enum dsa_notifier_type {
57360724d4bSFlorian Fainelli 	DSA_PORT_REGISTER,
57460724d4bSFlorian Fainelli 	DSA_PORT_UNREGISTER,
57560724d4bSFlorian Fainelli };
57660724d4bSFlorian Fainelli 
57760724d4bSFlorian Fainelli struct dsa_notifier_info {
57860724d4bSFlorian Fainelli 	struct net_device *dev;
57960724d4bSFlorian Fainelli };
58060724d4bSFlorian Fainelli 
58160724d4bSFlorian Fainelli struct dsa_notifier_register_info {
58260724d4bSFlorian Fainelli 	struct dsa_notifier_info info;	/* must be first */
58360724d4bSFlorian Fainelli 	struct net_device *master;
58460724d4bSFlorian Fainelli 	unsigned int port_number;
58560724d4bSFlorian Fainelli 	unsigned int switch_number;
58660724d4bSFlorian Fainelli };
58760724d4bSFlorian Fainelli 
58860724d4bSFlorian Fainelli static inline struct net_device *
58960724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
59060724d4bSFlorian Fainelli {
59160724d4bSFlorian Fainelli 	return info->dev;
59260724d4bSFlorian Fainelli }
59360724d4bSFlorian Fainelli 
59460724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
59560724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb);
59660724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb);
59760724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev,
59860724d4bSFlorian Fainelli 		       struct dsa_notifier_info *info);
59960724d4bSFlorian Fainelli #else
60060724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb)
60160724d4bSFlorian Fainelli {
60260724d4bSFlorian Fainelli 	return 0;
60360724d4bSFlorian Fainelli }
60460724d4bSFlorian Fainelli 
60560724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb)
60660724d4bSFlorian Fainelli {
60760724d4bSFlorian Fainelli 	return 0;
60860724d4bSFlorian Fainelli }
60960724d4bSFlorian Fainelli 
61060724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
61160724d4bSFlorian Fainelli 				     struct dsa_notifier_info *info)
61260724d4bSFlorian Fainelli {
61360724d4bSFlorian Fainelli 	return NOTIFY_DONE;
61460724d4bSFlorian Fainelli }
61560724d4bSFlorian Fainelli #endif
61660724d4bSFlorian Fainelli 
6170a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */
6180a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q)	((p) << 8 | q)
6190a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v)		((v) >> 8)
6200a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v)		((v) & 0xff)
6210a5f14ceSFlorian Fainelli 
622cf963573SFlorian Fainelli 
623cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
624cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
625cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
62611d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
627cf963573SFlorian Fainelli 
62891da11f8SLennert Buytenhek #endif
629