xref: /openbmc/linux/include/net/dsa.h (revision 08156ba4)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
291da11f8SLennert Buytenhek /*
391da11f8SLennert Buytenhek  * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
4e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
591da11f8SLennert Buytenhek  */
691da11f8SLennert Buytenhek 
791da11f8SLennert Buytenhek #ifndef __LINUX_NET_DSA_H
891da11f8SLennert Buytenhek #define __LINUX_NET_DSA_H
991da11f8SLennert Buytenhek 
104d56a29fSRussell King #include <linux/if.h>
11ea1f51beSAxel Lin #include <linux/if_ether.h>
12c8f0b869SBen Hutchings #include <linux/list.h>
13f515f192SVivien Didelot #include <linux/notifier.h>
14cf50dcc2SBen Hutchings #include <linux/timer.h>
15cf50dcc2SBen Hutchings #include <linux/workqueue.h>
16fa981d9aSFlorian Fainelli #include <linux/of.h>
17a2820543SFlorian Fainelli #include <linux/ethtool.h>
180336369dSBrandon Streiff #include <linux/net_tstamp.h>
1911d8f3ddSFlorian Fainelli #include <linux/phy.h>
20ecfc9372SFlorian Fainelli #include <linux/platform_data/dsa.h>
2144cc27e4SIoana Ciornei #include <linux/phylink.h>
2296567d5dSAndrew Lunn #include <net/devlink.h>
23f0c24ccfSVivien Didelot #include <net/switchdev.h>
24cf50dcc2SBen Hutchings 
25f50f2127SFlorian Fainelli struct tc_action;
264d56a29fSRussell King struct phy_device;
274d56a29fSRussell King struct fixed_phy_status;
2811d8f3ddSFlorian Fainelli struct phylink_link_state;
29f50f2127SFlorian Fainelli 
300b42f033SAndrew Lunn #define DSA_TAG_PROTO_NONE_VALUE		0
310b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_VALUE		1
320b42f033SAndrew Lunn #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE	2
330b42f033SAndrew Lunn #define DSA_TAG_PROTO_DSA_VALUE			3
340b42f033SAndrew Lunn #define DSA_TAG_PROTO_EDSA_VALUE		4
350b42f033SAndrew Lunn #define DSA_TAG_PROTO_GSWIP_VALUE		5
360b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9477_VALUE		6
370b42f033SAndrew Lunn #define DSA_TAG_PROTO_KSZ9893_VALUE		7
380b42f033SAndrew Lunn #define DSA_TAG_PROTO_LAN9303_VALUE		8
390b42f033SAndrew Lunn #define DSA_TAG_PROTO_MTK_VALUE			9
400b42f033SAndrew Lunn #define DSA_TAG_PROTO_QCA_VALUE			10
410b42f033SAndrew Lunn #define DSA_TAG_PROTO_TRAILER_VALUE		11
42f9bbe447SVladimir Oltean #define DSA_TAG_PROTO_8021Q_VALUE		12
43227d07a0SVladimir Oltean #define DSA_TAG_PROTO_SJA1105_VALUE		13
44016e43a2STristram Ha #define DSA_TAG_PROTO_KSZ8795_VALUE		14
458dce89aaSVladimir Oltean #define DSA_TAG_PROTO_OCELOT_VALUE		15
4648fda74fSOleksij Rempel #define DSA_TAG_PROTO_AR9331_VALUE		16
47efd7fe68SLinus Walleij #define DSA_TAG_PROTO_RTL4_A_VALUE		17
480b42f033SAndrew Lunn 
49ac7a04c3SFlorian Fainelli enum dsa_tag_protocol {
500b42f033SAndrew Lunn 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
510b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
520b42f033SAndrew Lunn 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
530b42f033SAndrew Lunn 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
540b42f033SAndrew Lunn 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
550b42f033SAndrew Lunn 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
560b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
570b42f033SAndrew Lunn 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
580b42f033SAndrew Lunn 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
590b42f033SAndrew Lunn 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
600b42f033SAndrew Lunn 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
610b42f033SAndrew Lunn 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
62f9bbe447SVladimir Oltean 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
63227d07a0SVladimir Oltean 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
64016e43a2STristram Ha 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
658dce89aaSVladimir Oltean 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
6648fda74fSOleksij Rempel 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
67efd7fe68SLinus Walleij 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
68ac7a04c3SFlorian Fainelli };
695037d532SFlorian Fainelli 
705075314eSAlexander Duyck struct packet_type;
7190af1059SBrandon Streiff struct dsa_switch;
723e8a72d1SFlorian Fainelli 
7368277a2cSJohn Crispin struct dsa_device_ops {
7468277a2cSJohn Crispin 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
7568277a2cSJohn Crispin 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
7689e49506SFlorian Westphal 			       struct packet_type *pt);
772e8cb1b3SVladimir Oltean 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
78598a9680SJohn Crispin 			     int *offset);
79cc1939e4SVladimir Oltean 	/* Used to determine which traffic should match the DSA filter in
80cc1939e4SVladimir Oltean 	 * eth_type_trans, and which, if any, should bypass it and be processed
81cc1939e4SVladimir Oltean 	 * as regular on the master net device.
82cc1939e4SVladimir Oltean 	 */
83cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
84a5dd3087SAndrew Lunn 	unsigned int overhead;
85875138f8SAndrew Lunn 	const char *name;
86056eed2fSAndrew Lunn 	enum dsa_tag_protocol proto;
87c3975400SVladimir Oltean 	/* Some tagging protocols either mangle or shift the destination MAC
88c3975400SVladimir Oltean 	 * address, in which case the DSA master would drop packets on ingress
89c3975400SVladimir Oltean 	 * if what it understands out of the destination MAC address is not in
90c3975400SVladimir Oltean 	 * its RX filter.
91c3975400SVladimir Oltean 	 */
92c3975400SVladimir Oltean 	bool promisc_on_master;
937a6ffe76SVladimir Oltean 	bool tail_tag;
9468277a2cSJohn Crispin };
9568277a2cSJohn Crispin 
964cfab356SFlorian Fainelli /* This structure defines the control interfaces that are overlayed by the
974cfab356SFlorian Fainelli  * DSA layer on top of the DSA CPU/management net_device instance. This is
984cfab356SFlorian Fainelli  * used by the core net_device layer while calling various net_device_ops
994cfab356SFlorian Fainelli  * function pointers.
1004cfab356SFlorian Fainelli  */
1014cfab356SFlorian Fainelli struct dsa_netdevice_ops {
1024cfab356SFlorian Fainelli 	int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr,
1034cfab356SFlorian Fainelli 			    int cmd);
1044cfab356SFlorian Fainelli };
1054cfab356SFlorian Fainelli 
1060b42f033SAndrew Lunn #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
1070b42f033SAndrew Lunn #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
1080b42f033SAndrew Lunn 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
1090b42f033SAndrew Lunn 
110b68b0dd0SVladimir Oltean struct dsa_skb_cb {
111b68b0dd0SVladimir Oltean 	struct sk_buff *clone;
112b68b0dd0SVladimir Oltean };
113b68b0dd0SVladimir Oltean 
114b68b0dd0SVladimir Oltean struct __dsa_skb_cb {
115b68b0dd0SVladimir Oltean 	struct dsa_skb_cb cb;
116b68b0dd0SVladimir Oltean 	u8 priv[48 - sizeof(struct dsa_skb_cb)];
117b68b0dd0SVladimir Oltean };
118b68b0dd0SVladimir Oltean 
119b68b0dd0SVladimir Oltean #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
120b68b0dd0SVladimir Oltean 
121b68b0dd0SVladimir Oltean #define DSA_SKB_CB_PRIV(skb)			\
122b68b0dd0SVladimir Oltean 	((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
123b68b0dd0SVladimir Oltean 
124cf50dcc2SBen Hutchings struct dsa_switch_tree {
12583c0afaeSAndrew Lunn 	struct list_head	list;
12683c0afaeSAndrew Lunn 
127f515f192SVivien Didelot 	/* Notifier chain for switch-wide events */
128f515f192SVivien Didelot 	struct raw_notifier_head	nh;
129f515f192SVivien Didelot 
13083c0afaeSAndrew Lunn 	/* Tree identifier */
13149463b7fSVivien Didelot 	unsigned int index;
13283c0afaeSAndrew Lunn 
13383c0afaeSAndrew Lunn 	/* Number of switches attached to this tree */
13483c0afaeSAndrew Lunn 	struct kref refcount;
13583c0afaeSAndrew Lunn 
13683c0afaeSAndrew Lunn 	/* Has this tree been applied to the hardware? */
137ec15dd42SVivien Didelot 	bool setup;
13883c0afaeSAndrew Lunn 
139cf50dcc2SBen Hutchings 	/*
140cf50dcc2SBen Hutchings 	 * Configuration data for the platform device that owns
141cf50dcc2SBen Hutchings 	 * this dsa switch tree instance.
142cf50dcc2SBen Hutchings 	 */
143cf50dcc2SBen Hutchings 	struct dsa_platform_data	*pd;
144cf85d08fSLennert Buytenhek 
145ab8ccae1SVivien Didelot 	/* List of switch ports */
146ab8ccae1SVivien Didelot 	struct list_head ports;
147ab8ccae1SVivien Didelot 
148c5f51765SVivien Didelot 	/* List of DSA links composing the routing table */
149c5f51765SVivien Didelot 	struct list_head rtable;
150cf50dcc2SBen Hutchings };
151cf50dcc2SBen Hutchings 
15234297176SVladimir Oltean /* TC matchall action types */
153f50f2127SFlorian Fainelli enum dsa_port_mall_action_type {
154f50f2127SFlorian Fainelli 	DSA_PORT_MALL_MIRROR,
15534297176SVladimir Oltean 	DSA_PORT_MALL_POLICER,
156f50f2127SFlorian Fainelli };
157f50f2127SFlorian Fainelli 
158f50f2127SFlorian Fainelli /* TC mirroring entry */
159f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry {
160f50f2127SFlorian Fainelli 	u8 to_local_port;
161f50f2127SFlorian Fainelli 	bool ingress;
162f50f2127SFlorian Fainelli };
163f50f2127SFlorian Fainelli 
16434297176SVladimir Oltean /* TC port policer entry */
16534297176SVladimir Oltean struct dsa_mall_policer_tc_entry {
1665f035af7SPo Liu 	u32 burst;
16734297176SVladimir Oltean 	u64 rate_bytes_per_sec;
16834297176SVladimir Oltean };
16934297176SVladimir Oltean 
170f50f2127SFlorian Fainelli /* TC matchall entry */
171f50f2127SFlorian Fainelli struct dsa_mall_tc_entry {
172f50f2127SFlorian Fainelli 	struct list_head list;
173f50f2127SFlorian Fainelli 	unsigned long cookie;
174f50f2127SFlorian Fainelli 	enum dsa_port_mall_action_type type;
175f50f2127SFlorian Fainelli 	union {
176f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry mirror;
17734297176SVladimir Oltean 		struct dsa_mall_policer_tc_entry policer;
178f50f2127SFlorian Fainelli 	};
179f50f2127SFlorian Fainelli };
180f50f2127SFlorian Fainelli 
181f50f2127SFlorian Fainelli 
182c8b09808SAndrew Lunn struct dsa_port {
183f8b8b1cdSVivien Didelot 	/* A CPU port is physically connected to a master device.
184f8b8b1cdSVivien Didelot 	 * A user port exposed to userspace has a slave device.
185f8b8b1cdSVivien Didelot 	 */
186f8b8b1cdSVivien Didelot 	union {
187f8b8b1cdSVivien Didelot 		struct net_device *master;
188f8b8b1cdSVivien Didelot 		struct net_device *slave;
189f8b8b1cdSVivien Didelot 	};
190f8b8b1cdSVivien Didelot 
19115240248SVivien Didelot 	/* CPU port tagging operations used by master or slave devices */
19215240248SVivien Didelot 	const struct dsa_device_ops *tag_ops;
19315240248SVivien Didelot 
1943e41f93bSVivien Didelot 	/* Copies for faster access in master receive hot path */
1953e41f93bSVivien Didelot 	struct dsa_switch_tree *dst;
1963e41f93bSVivien Didelot 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
1973e41f93bSVivien Didelot 			       struct packet_type *pt);
198cc1939e4SVladimir Oltean 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
1993e41f93bSVivien Didelot 
200057cad2cSVivien Didelot 	enum {
201057cad2cSVivien Didelot 		DSA_PORT_TYPE_UNUSED = 0,
202057cad2cSVivien Didelot 		DSA_PORT_TYPE_CPU,
203057cad2cSVivien Didelot 		DSA_PORT_TYPE_DSA,
204057cad2cSVivien Didelot 		DSA_PORT_TYPE_USER,
205057cad2cSVivien Didelot 	} type;
206057cad2cSVivien Didelot 
207818be848SVivien Didelot 	struct dsa_switch	*ds;
208818be848SVivien Didelot 	unsigned int		index;
20971e0bbdeSFlorian Fainelli 	const char		*name;
21068b2d4a8SVivien Didelot 	struct dsa_port		*cpu_dp;
211a2c7023fSXiaofei Shen 	const char		*mac;
212189b0d93SAndrew Lunn 	struct device_node	*dn;
21334a79f63SVivien Didelot 	unsigned int		ageing_time;
21433162e9aSVladimir Oltean 	bool			vlan_filtering;
215732f794cSVivien Didelot 	u8			stp_state;
216a5e9a02eSVivien Didelot 	struct net_device	*bridge_dev;
21796567d5dSAndrew Lunn 	struct devlink_port	devlink_port;
2183122433eSAndrew Lunn 	bool			devlink_port_setup;
219aab9c406SFlorian Fainelli 	struct phylink		*pl;
22044cc27e4SIoana Ciornei 	struct phylink_config	pl_config;
22197a69a0dSVladimir Oltean 
222ab8ccae1SVivien Didelot 	struct list_head list;
223ab8ccae1SVivien Didelot 
22467dbb9d4SFlorian Fainelli 	/*
225c362beb0SVladimir Oltean 	 * Give the switch driver somewhere to hang its per-port private data
226c362beb0SVladimir Oltean 	 * structures (accessible from the tagger).
227c362beb0SVladimir Oltean 	 */
228c362beb0SVladimir Oltean 	void *priv;
229c362beb0SVladimir Oltean 
230c362beb0SVladimir Oltean 	/*
23167dbb9d4SFlorian Fainelli 	 * Original copy of the master netdev ethtool_ops
23267dbb9d4SFlorian Fainelli 	 */
23367dbb9d4SFlorian Fainelli 	const struct ethtool_ops *orig_ethtool_ops;
234da7b9e9bSFlorian Fainelli 
235da7b9e9bSFlorian Fainelli 	/*
236da7b9e9bSFlorian Fainelli 	 * Original copy of the master netdev net_device_ops
237da7b9e9bSFlorian Fainelli 	 */
2384cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *netdev_ops;
239fb35c60cSVivien Didelot 
240fb35c60cSVivien Didelot 	bool setup;
241c8b09808SAndrew Lunn };
242c8b09808SAndrew Lunn 
243c5f51765SVivien Didelot /* TODO: ideally DSA ports would have a single dp->link_dp member,
244c5f51765SVivien Didelot  * and no dst->rtable nor this struct dsa_link would be needed,
245c5f51765SVivien Didelot  * but this would require some more complex tree walking,
246c5f51765SVivien Didelot  * so keep it stupid at the moment and list them all.
247c5f51765SVivien Didelot  */
248c5f51765SVivien Didelot struct dsa_link {
249c5f51765SVivien Didelot 	struct dsa_port *dp;
250c5f51765SVivien Didelot 	struct dsa_port *link_dp;
251c5f51765SVivien Didelot 	struct list_head list;
252c5f51765SVivien Didelot };
253c5f51765SVivien Didelot 
254c8f0b869SBen Hutchings struct dsa_switch {
255fb35c60cSVivien Didelot 	bool setup;
256fb35c60cSVivien Didelot 
257c33063d6SAndrew Lunn 	struct device *dev;
258c33063d6SAndrew Lunn 
259c8f0b869SBen Hutchings 	/*
260c8f0b869SBen Hutchings 	 * Parent switch tree, and switch index.
261c8f0b869SBen Hutchings 	 */
262c8f0b869SBen Hutchings 	struct dsa_switch_tree	*dst;
26399feaafcSVivien Didelot 	unsigned int		index;
264c8f0b869SBen Hutchings 
265f515f192SVivien Didelot 	/* Listener for switch fabric events */
266f515f192SVivien Didelot 	struct notifier_block	nb;
267f515f192SVivien Didelot 
268c8f0b869SBen Hutchings 	/*
2697543a6d5SAndrew Lunn 	 * Give the switch driver somewhere to hang its private data
2707543a6d5SAndrew Lunn 	 * structure.
2717543a6d5SAndrew Lunn 	 */
2727543a6d5SAndrew Lunn 	void *priv;
2737543a6d5SAndrew Lunn 
2747543a6d5SAndrew Lunn 	/*
275c8f0b869SBen Hutchings 	 * Configuration data for this switch.
276c8f0b869SBen Hutchings 	 */
277ff04955cSAndrew Lunn 	struct dsa_chip_data	*cd;
278c8f0b869SBen Hutchings 
279c8f0b869SBen Hutchings 	/*
2809d490b4eSVivien Didelot 	 * The switch operations.
281c8f0b869SBen Hutchings 	 */
282a82f67afSFlorian Fainelli 	const struct dsa_switch_ops	*ops;
283c8f0b869SBen Hutchings 
28466472fc0SAndrew Lunn 	/*
285c8f0b869SBen Hutchings 	 * Slave mii_bus and devices for the individual ports.
286c8f0b869SBen Hutchings 	 */
2870d8bcdd3SFlorian Fainelli 	u32			phys_mii_mask;
288c8f0b869SBen Hutchings 	struct mii_bus		*slave_mii_bus;
289a0c02161SVivien Didelot 
2900f3da6afSVivien Didelot 	/* Ageing Time limits in msecs */
2910f3da6afSVivien Didelot 	unsigned int ageing_time_min;
2920f3da6afSVivien Didelot 	unsigned int ageing_time_max;
2930f3da6afSVivien Didelot 
29496567d5dSAndrew Lunn 	/* devlink used to represent this switch device */
29596567d5dSAndrew Lunn 	struct devlink		*devlink;
29696567d5dSAndrew Lunn 
29755199df6SFlorian Fainelli 	/* Number of switch port queues */
29855199df6SFlorian Fainelli 	unsigned int		num_tx_queues;
29955199df6SFlorian Fainelli 
3008f5d16f6SVladimir Oltean 	/* Disallow bridge core from requesting different VLAN awareness
3018f5d16f6SVladimir Oltean 	 * settings on ports if not hardware-supported
3028f5d16f6SVladimir Oltean 	 */
3038f5d16f6SVladimir Oltean 	bool			vlan_filtering_is_global;
3048f5d16f6SVladimir Oltean 
30554a0ed0dSRussell King 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
30654a0ed0dSRussell King 	 * that have vlan_filtering=0. All drivers should ideally set this (and
30754a0ed0dSRussell King 	 * then the option would get removed), but it is unknown whether this
30854a0ed0dSRussell King 	 * would break things or not.
30954a0ed0dSRussell King 	 */
31054a0ed0dSRussell King 	bool			configure_vlan_while_not_filtering;
31154a0ed0dSRussell King 
3121dc0408cSFlorian Fainelli 	/* If the switch driver always programs the CPU port as egress tagged
3131dc0408cSFlorian Fainelli 	 * despite the VLAN configuration indicating otherwise, then setting
3141dc0408cSFlorian Fainelli 	 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
3151dc0408cSFlorian Fainelli 	 * default_pvid VLAN tagged frames to offer a consistent behavior
3161dc0408cSFlorian Fainelli 	 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
3171dc0408cSFlorian Fainelli 	 */
3181dc0408cSFlorian Fainelli 	bool			untag_bridge_pvid;
3191dc0408cSFlorian Fainelli 
32014574676SVladimir Oltean 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
32114574676SVladimir Oltean 	 * should be retrieved from here and not from the per-port settings.
32214574676SVladimir Oltean 	 */
32314574676SVladimir Oltean 	bool			vlan_filtering;
32414574676SVladimir Oltean 
325787cac3fSVladimir Oltean 	/* MAC PCS does not provide link state change interrupt, and requires
326787cac3fSVladimir Oltean 	 * polling. Flag passed on to PHYLINK.
327787cac3fSVladimir Oltean 	 */
328787cac3fSVladimir Oltean 	bool			pcs_poll;
329787cac3fSVladimir Oltean 
330bff33f7eSVladimir Oltean 	/* For switches that only have the MRU configurable. To ensure the
331bff33f7eSVladimir Oltean 	 * configured MTU is not exceeded, normalization of MRU on all bridged
332bff33f7eSVladimir Oltean 	 * interfaces is needed.
333bff33f7eSVladimir Oltean 	 */
334bff33f7eSVladimir Oltean 	bool			mtu_enforcement_ingress;
335bff33f7eSVladimir Oltean 
336a0c02161SVivien Didelot 	size_t num_ports;
337c8f0b869SBen Hutchings };
338c8f0b869SBen Hutchings 
33968bb8ea8SVivien Didelot static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
340c38c5a66SVivien Didelot {
341b96ddf25SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
342d607525bSVivien Didelot 	struct dsa_port *dp;
343b96ddf25SVivien Didelot 
344b96ddf25SVivien Didelot 	list_for_each_entry(dp, &dst->ports, list)
345b96ddf25SVivien Didelot 		if (dp->ds == ds && dp->index == p)
346b96ddf25SVivien Didelot 			return dp;
347d607525bSVivien Didelot 
348d607525bSVivien Didelot 	return NULL;
349c38c5a66SVivien Didelot }
350c38c5a66SVivien Didelot 
351bff7b688SVivien Didelot static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
352bff7b688SVivien Didelot {
353c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
354bff7b688SVivien Didelot }
355bff7b688SVivien Didelot 
356c8f0b869SBen Hutchings static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
357c8f0b869SBen Hutchings {
358c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
359c8f0b869SBen Hutchings }
360c8f0b869SBen Hutchings 
36160045cbfSAndrew Lunn static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
36260045cbfSAndrew Lunn {
363c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
36460045cbfSAndrew Lunn }
36560045cbfSAndrew Lunn 
3662b3e9891SVivien Didelot static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
3676cd456f3SVivien Didelot {
368c38c5a66SVivien Didelot 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
3696cd456f3SVivien Didelot }
3706cd456f3SVivien Didelot 
37102bc6e54SVivien Didelot static inline u32 dsa_user_ports(struct dsa_switch *ds)
37202bc6e54SVivien Didelot {
373c38c5a66SVivien Didelot 	u32 mask = 0;
374c38c5a66SVivien Didelot 	int p;
37502bc6e54SVivien Didelot 
376c38c5a66SVivien Didelot 	for (p = 0; p < ds->num_ports; p++)
377c38c5a66SVivien Didelot 		if (dsa_is_user_port(ds, p))
378c38c5a66SVivien Didelot 			mask |= BIT(p);
379c38c5a66SVivien Didelot 
380c38c5a66SVivien Didelot 	return mask;
381c8652c83SVivien Didelot }
382c8652c83SVivien Didelot 
383c5f51765SVivien Didelot /* Return the local port used to reach an arbitrary switch device */
384c5f51765SVivien Didelot static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
385c5f51765SVivien Didelot {
386c5f51765SVivien Didelot 	struct dsa_switch_tree *dst = ds->dst;
387c5f51765SVivien Didelot 	struct dsa_link *dl;
388c5f51765SVivien Didelot 
389c5f51765SVivien Didelot 	list_for_each_entry(dl, &dst->rtable, list)
390c5f51765SVivien Didelot 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
391c5f51765SVivien Didelot 			return dl->dp->index;
392c5f51765SVivien Didelot 
393c5f51765SVivien Didelot 	return ds->num_ports;
394c5f51765SVivien Didelot }
395c5f51765SVivien Didelot 
3963b8fac5dSVivien Didelot /* Return the local port used to reach an arbitrary switch port */
3973b8fac5dSVivien Didelot static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
3983b8fac5dSVivien Didelot 					    int port)
3993b8fac5dSVivien Didelot {
4003b8fac5dSVivien Didelot 	if (device == ds->index)
4013b8fac5dSVivien Didelot 		return port;
4023b8fac5dSVivien Didelot 	else
403c5f51765SVivien Didelot 		return dsa_routing_port(ds, device);
4043b8fac5dSVivien Didelot }
4053b8fac5dSVivien Didelot 
4063b8fac5dSVivien Didelot /* Return the local port used to reach the dedicated CPU port */
40707073c79SVivien Didelot static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
408c8f0b869SBen Hutchings {
40907073c79SVivien Didelot 	const struct dsa_port *dp = dsa_to_port(ds, port);
41007073c79SVivien Didelot 	const struct dsa_port *cpu_dp = dp->cpu_dp;
41107073c79SVivien Didelot 
41207073c79SVivien Didelot 	if (!cpu_dp)
41307073c79SVivien Didelot 		return port;
414c8f0b869SBen Hutchings 
4153b8fac5dSVivien Didelot 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
416c8f0b869SBen Hutchings }
417c8f0b869SBen Hutchings 
418cf2d45f5SVladimir Oltean static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
419cf2d45f5SVladimir Oltean {
420cf2d45f5SVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
421cf2d45f5SVladimir Oltean 
422cf2d45f5SVladimir Oltean 	if (ds->vlan_filtering_is_global)
423cf2d45f5SVladimir Oltean 		return ds->vlan_filtering;
424cf2d45f5SVladimir Oltean 	else
425cf2d45f5SVladimir Oltean 		return dp->vlan_filtering;
426cf2d45f5SVladimir Oltean }
427cf2d45f5SVladimir Oltean 
4282bedde1aSArkadi Sharshevsky typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
4292bedde1aSArkadi Sharshevsky 			      bool is_static, void *data);
4309d490b4eSVivien Didelot struct dsa_switch_ops {
4315ed4e3ebSFlorian Fainelli 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
4324d776482SFlorian Fainelli 						  int port,
4334d776482SFlorian Fainelli 						  enum dsa_tag_protocol mprot);
4347b314362SAndrew Lunn 
435c8f0b869SBen Hutchings 	int	(*setup)(struct dsa_switch *ds);
4365e3f847aSVladimir Oltean 	void	(*teardown)(struct dsa_switch *ds);
4376819563eSFlorian Fainelli 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
438c8f0b869SBen Hutchings 
439c8f0b869SBen Hutchings 	/*
440c8f0b869SBen Hutchings 	 * Access to the switch's PHY registers.
441c8f0b869SBen Hutchings 	 */
442c8f0b869SBen Hutchings 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
443c8f0b869SBen Hutchings 	int	(*phy_write)(struct dsa_switch *ds, int port,
444c8f0b869SBen Hutchings 			     int regnum, u16 val);
445c8f0b869SBen Hutchings 
446c8f0b869SBen Hutchings 	/*
447ec9436baSFlorian Fainelli 	 * Link state adjustment (called from libphy)
448ec9436baSFlorian Fainelli 	 */
449ec9436baSFlorian Fainelli 	void	(*adjust_link)(struct dsa_switch *ds, int port,
450ec9436baSFlorian Fainelli 				struct phy_device *phydev);
451ce31b31cSFlorian Fainelli 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
452ce31b31cSFlorian Fainelli 				struct fixed_phy_status *st);
453ec9436baSFlorian Fainelli 
454ec9436baSFlorian Fainelli 	/*
45511d8f3ddSFlorian Fainelli 	 * PHYLINK integration
45611d8f3ddSFlorian Fainelli 	 */
45711d8f3ddSFlorian Fainelli 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
45811d8f3ddSFlorian Fainelli 				    unsigned long *supported,
45911d8f3ddSFlorian Fainelli 				    struct phylink_link_state *state);
46011d8f3ddSFlorian Fainelli 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
46111d8f3ddSFlorian Fainelli 					  struct phylink_link_state *state);
46211d8f3ddSFlorian Fainelli 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
46311d8f3ddSFlorian Fainelli 				      unsigned int mode,
46411d8f3ddSFlorian Fainelli 				      const struct phylink_link_state *state);
46511d8f3ddSFlorian Fainelli 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
46611d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
46711d8f3ddSFlorian Fainelli 					 unsigned int mode,
46811d8f3ddSFlorian Fainelli 					 phy_interface_t interface);
46911d8f3ddSFlorian Fainelli 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
47011d8f3ddSFlorian Fainelli 				       unsigned int mode,
47111d8f3ddSFlorian Fainelli 				       phy_interface_t interface,
4725b502a7bSRussell King 				       struct phy_device *phydev,
4735b502a7bSRussell King 				       int speed, int duplex,
4745b502a7bSRussell King 				       bool tx_pause, bool rx_pause);
47511d8f3ddSFlorian Fainelli 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
47611d8f3ddSFlorian Fainelli 				       struct phylink_link_state *state);
47711d8f3ddSFlorian Fainelli 	/*
478c8f0b869SBen Hutchings 	 * ethtool hardware statistics.
479c8f0b869SBen Hutchings 	 */
48089f09048SFlorian Fainelli 	void	(*get_strings)(struct dsa_switch *ds, int port,
48189f09048SFlorian Fainelli 			       u32 stringset, uint8_t *data);
482c8f0b869SBen Hutchings 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
483c8f0b869SBen Hutchings 				     int port, uint64_t *data);
48489f09048SFlorian Fainelli 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
485cf963573SFlorian Fainelli 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
486cf963573SFlorian Fainelli 					 int port, uint64_t *data);
48724462549SFlorian Fainelli 
48824462549SFlorian Fainelli 	/*
48919e57c4eSFlorian Fainelli 	 * ethtool Wake-on-LAN
49019e57c4eSFlorian Fainelli 	 */
49119e57c4eSFlorian Fainelli 	void	(*get_wol)(struct dsa_switch *ds, int port,
49219e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
49319e57c4eSFlorian Fainelli 	int	(*set_wol)(struct dsa_switch *ds, int port,
49419e57c4eSFlorian Fainelli 			   struct ethtool_wolinfo *w);
49519e57c4eSFlorian Fainelli 
49619e57c4eSFlorian Fainelli 	/*
4970336369dSBrandon Streiff 	 * ethtool timestamp info
4980336369dSBrandon Streiff 	 */
4990336369dSBrandon Streiff 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
5000336369dSBrandon Streiff 			       struct ethtool_ts_info *ts);
5010336369dSBrandon Streiff 
5020336369dSBrandon Streiff 	/*
50324462549SFlorian Fainelli 	 * Suspend and resume
50424462549SFlorian Fainelli 	 */
50524462549SFlorian Fainelli 	int	(*suspend)(struct dsa_switch *ds);
50624462549SFlorian Fainelli 	int	(*resume)(struct dsa_switch *ds);
507b2f2af21SFlorian Fainelli 
508b2f2af21SFlorian Fainelli 	/*
509b2f2af21SFlorian Fainelli 	 * Port enable/disable
510b2f2af21SFlorian Fainelli 	 */
511b2f2af21SFlorian Fainelli 	int	(*port_enable)(struct dsa_switch *ds, int port,
512b2f2af21SFlorian Fainelli 			       struct phy_device *phy);
51375104db0SAndrew Lunn 	void	(*port_disable)(struct dsa_switch *ds, int port);
5147905288fSFlorian Fainelli 
5157905288fSFlorian Fainelli 	/*
51608f50061SVivien Didelot 	 * Port's MAC EEE settings
5177905288fSFlorian Fainelli 	 */
51808f50061SVivien Didelot 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
5197905288fSFlorian Fainelli 			       struct ethtool_eee *e);
52008f50061SVivien Didelot 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
5217905288fSFlorian Fainelli 			       struct ethtool_eee *e);
52251579c3fSGuenter Roeck 
5236793abb4SGuenter Roeck 	/* EEPROM access */
5246793abb4SGuenter Roeck 	int	(*get_eeprom_len)(struct dsa_switch *ds);
5256793abb4SGuenter Roeck 	int	(*get_eeprom)(struct dsa_switch *ds,
5266793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
5276793abb4SGuenter Roeck 	int	(*set_eeprom)(struct dsa_switch *ds,
5286793abb4SGuenter Roeck 			      struct ethtool_eeprom *eeprom, u8 *data);
5293d762a0fSGuenter Roeck 
5303d762a0fSGuenter Roeck 	/*
5313d762a0fSGuenter Roeck 	 * Register access.
5323d762a0fSGuenter Roeck 	 */
5333d762a0fSGuenter Roeck 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
5343d762a0fSGuenter Roeck 	void	(*get_regs)(struct dsa_switch *ds, int port,
5353d762a0fSGuenter Roeck 			    struct ethtool_regs *regs, void *p);
536b73adef6SFlorian Fainelli 
537b73adef6SFlorian Fainelli 	/*
538b73adef6SFlorian Fainelli 	 * Bridge integration
539b73adef6SFlorian Fainelli 	 */
54034a79f63SVivien Didelot 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
54171327a4eSVivien Didelot 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
542a6692754SVivien Didelot 				    struct net_device *bridge);
543f123f2fbSVivien Didelot 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
544f123f2fbSVivien Didelot 				     struct net_device *bridge);
54543c44a9fSVivien Didelot 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
546b73adef6SFlorian Fainelli 				      u8 state);
547732f794cSVivien Didelot 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
54857652796SRussell King 	int	(*port_egress_floods)(struct dsa_switch *ds, int port,
54957652796SRussell King 				      bool unicast, bool multicast);
5502a778e1bSVivien Didelot 
5512a778e1bSVivien Didelot 	/*
55211149536SVivien Didelot 	 * VLAN support
55311149536SVivien Didelot 	 */
554fb2dabadSVivien Didelot 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
555fb2dabadSVivien Didelot 				       bool vlan_filtering);
55676e398a6SVivien Didelot 	int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
55780e02360SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
5584d5770b3SVivien Didelot 	void (*port_vlan_add)(struct dsa_switch *ds, int port,
55980e02360SVivien Didelot 			      const struct switchdev_obj_port_vlan *vlan);
56076e398a6SVivien Didelot 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
56176e398a6SVivien Didelot 				 const struct switchdev_obj_port_vlan *vlan);
56211149536SVivien Didelot 	/*
5632a778e1bSVivien Didelot 	 * Forwarding database
5642a778e1bSVivien Didelot 	 */
5651b6dd556SArkadi Sharshevsky 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
5666c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
5672a778e1bSVivien Didelot 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
5686c2c1dcbSArkadi Sharshevsky 				const unsigned char *addr, u16 vid);
569ea70ba98SVivien Didelot 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
5702bedde1aSArkadi Sharshevsky 				 dsa_fdb_dump_cb_t *cb, void *data);
5718df30255SVivien Didelot 
5728df30255SVivien Didelot 	/*
5738df30255SVivien Didelot 	 * Multicast database
5748df30255SVivien Didelot 	 */
5758df30255SVivien Didelot 	int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
5763709aadcSVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
5778df30255SVivien Didelot 	void (*port_mdb_add)(struct dsa_switch *ds, int port,
5783709aadcSVivien Didelot 			     const struct switchdev_obj_port_mdb *mdb);
5798df30255SVivien Didelot 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
5808df30255SVivien Didelot 				const struct switchdev_obj_port_mdb *mdb);
581bf9f2648SFlorian Fainelli 	/*
582bf9f2648SFlorian Fainelli 	 * RXNFC
583bf9f2648SFlorian Fainelli 	 */
584bf9f2648SFlorian Fainelli 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
585bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
586bf9f2648SFlorian Fainelli 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
587bf9f2648SFlorian Fainelli 			     struct ethtool_rxnfc *nfc);
588f50f2127SFlorian Fainelli 
589f50f2127SFlorian Fainelli 	/*
590f50f2127SFlorian Fainelli 	 * TC integration
591f50f2127SFlorian Fainelli 	 */
592ed11bb1fSVladimir Oltean 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
593ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
594ed11bb1fSVladimir Oltean 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
595ed11bb1fSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress);
596ed11bb1fSVladimir Oltean 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
597ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls, bool ingress);
598f50f2127SFlorian Fainelli 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
599f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror,
600f50f2127SFlorian Fainelli 				   bool ingress);
601f50f2127SFlorian Fainelli 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
602f50f2127SFlorian Fainelli 				   struct dsa_mall_mirror_tc_entry *mirror);
60334297176SVladimir Oltean 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
60434297176SVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer);
60534297176SVladimir Oltean 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
60647d23af2SVladimir Oltean 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
60747d23af2SVladimir Oltean 				 enum tc_setup_type type, void *type_data);
60840ef2c93SVivien Didelot 
60940ef2c93SVivien Didelot 	/*
61040ef2c93SVivien Didelot 	 * Cross-chip operations
61140ef2c93SVivien Didelot 	 */
612f66a6a69SVladimir Oltean 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
613f66a6a69SVladimir Oltean 					 int sw_index, int port,
614f66a6a69SVladimir Oltean 					 struct net_device *br);
615f66a6a69SVladimir Oltean 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
616f66a6a69SVladimir Oltean 					  int sw_index, int port,
617f66a6a69SVladimir Oltean 					  struct net_device *br);
6180336369dSBrandon Streiff 
6190336369dSBrandon Streiff 	/*
6200336369dSBrandon Streiff 	 * PTP functionality
6210336369dSBrandon Streiff 	 */
6220336369dSBrandon Streiff 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
6230336369dSBrandon Streiff 				     struct ifreq *ifr);
6240336369dSBrandon Streiff 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
6250336369dSBrandon Streiff 				     struct ifreq *ifr);
62690af1059SBrandon Streiff 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
62790af1059SBrandon Streiff 				 struct sk_buff *clone, unsigned int type);
62890af1059SBrandon Streiff 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
62990af1059SBrandon Streiff 				 struct sk_buff *skb, unsigned int type);
63097a69a0dSVladimir Oltean 
6310f06b855SAndrew Lunn 	/* Devlink parameters, etc */
6326b297524SAndrew Lunn 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
6336b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
6346b297524SAndrew Lunn 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
6356b297524SAndrew Lunn 				     struct devlink_param_gset_ctx *ctx);
6360f06b855SAndrew Lunn 	int	(*devlink_info_get)(struct dsa_switch *ds,
6370f06b855SAndrew Lunn 				    struct devlink_info_req *req,
6380f06b855SAndrew Lunn 				    struct netlink_ext_ack *extack);
639bfcb8132SVladimir Oltean 
640bfcb8132SVladimir Oltean 	/*
641bfcb8132SVladimir Oltean 	 * MTU change functionality. Switches can also adjust their MRU through
642bfcb8132SVladimir Oltean 	 * this method. By MTU, one understands the SDU (L2 payload) length.
643bfcb8132SVladimir Oltean 	 * If the switch needs to account for the DSA tag on the CPU port, this
644ab88d64aSRandy Dunlap 	 * method needs to do so privately.
645bfcb8132SVladimir Oltean 	 */
646bfcb8132SVladimir Oltean 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
647bfcb8132SVladimir Oltean 				   int new_mtu);
648bfcb8132SVladimir Oltean 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
6496b297524SAndrew Lunn };
6506b297524SAndrew Lunn 
6516b297524SAndrew Lunn #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
6526b297524SAndrew Lunn 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
6536b297524SAndrew Lunn 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
6546b297524SAndrew Lunn 
6556b297524SAndrew Lunn int dsa_devlink_param_get(struct devlink *dl, u32 id,
6566b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
6576b297524SAndrew Lunn int dsa_devlink_param_set(struct devlink *dl, u32 id,
6586b297524SAndrew Lunn 			  struct devlink_param_gset_ctx *ctx);
6596b297524SAndrew Lunn int dsa_devlink_params_register(struct dsa_switch *ds,
6606b297524SAndrew Lunn 				const struct devlink_param *params,
6616b297524SAndrew Lunn 				size_t params_count);
6626b297524SAndrew Lunn void dsa_devlink_params_unregister(struct dsa_switch *ds,
6636b297524SAndrew Lunn 				   const struct devlink_param *params,
6646b297524SAndrew Lunn 				   size_t params_count);
6655cd73fbdSAndrew Lunn int dsa_devlink_resource_register(struct dsa_switch *ds,
6665cd73fbdSAndrew Lunn 				  const char *resource_name,
6675cd73fbdSAndrew Lunn 				  u64 resource_size,
6685cd73fbdSAndrew Lunn 				  u64 resource_id,
6695cd73fbdSAndrew Lunn 				  u64 parent_resource_id,
6705cd73fbdSAndrew Lunn 				  const struct devlink_resource_size_params *size_params);
6715cd73fbdSAndrew Lunn 
6725cd73fbdSAndrew Lunn void dsa_devlink_resources_unregister(struct dsa_switch *ds);
6735cd73fbdSAndrew Lunn 
6745cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
6755cd73fbdSAndrew Lunn 					   u64 resource_id,
6765cd73fbdSAndrew Lunn 					   devlink_resource_occ_get_t *occ_get,
6775cd73fbdSAndrew Lunn 					   void *occ_get_priv);
6785cd73fbdSAndrew Lunn void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
6795cd73fbdSAndrew Lunn 					     u64 resource_id);
68097c82c23SAndrew Lunn struct devlink_region *
68197c82c23SAndrew Lunn dsa_devlink_region_create(struct dsa_switch *ds,
68297c82c23SAndrew Lunn 			  const struct devlink_region_ops *ops,
68397c82c23SAndrew Lunn 			  u32 region_max_snapshots, u64 region_size);
68408156ba4SAndrew Lunn struct devlink_region *
68508156ba4SAndrew Lunn dsa_devlink_port_region_create(struct dsa_switch *ds,
68608156ba4SAndrew Lunn 			       int port,
68708156ba4SAndrew Lunn 			       const struct devlink_port_region_ops *ops,
68808156ba4SAndrew Lunn 			       u32 region_max_snapshots, u64 region_size);
68997c82c23SAndrew Lunn void dsa_devlink_region_destroy(struct devlink_region *region);
69097c82c23SAndrew Lunn 
691e1eea811SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
6925cd73fbdSAndrew Lunn 
6936b297524SAndrew Lunn struct dsa_devlink_priv {
6946b297524SAndrew Lunn 	struct dsa_switch *ds;
695c8f0b869SBen Hutchings };
696c8f0b869SBen Hutchings 
697ccc3e6b0SAndrew Lunn static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
698ccc3e6b0SAndrew Lunn {
699ccc3e6b0SAndrew Lunn 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
700ccc3e6b0SAndrew Lunn 
701ccc3e6b0SAndrew Lunn 	return dl_priv->ds;
702ccc3e6b0SAndrew Lunn }
703ccc3e6b0SAndrew Lunn 
704ab3d408dSFlorian Fainelli struct dsa_switch_driver {
705ab3d408dSFlorian Fainelli 	struct list_head	list;
706a82f67afSFlorian Fainelli 	const struct dsa_switch_ops *ops;
707ab3d408dSFlorian Fainelli };
708ab3d408dSFlorian Fainelli 
70914b89f36SFlorian Fainelli struct net_device *dsa_dev_to_net_device(struct device *dev);
710c8f0b869SBen Hutchings 
71173a7ece8SVivien Didelot /* Keep inline for faster access in hot path */
7129eb8eff0SVladimir Oltean static inline bool netdev_uses_dsa(const struct net_device *dev)
713c6e970a0SAndrew Lunn {
714c6e970a0SAndrew Lunn #if IS_ENABLED(CONFIG_NET_DSA)
715717ffbfbSVivien Didelot 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
716c6e970a0SAndrew Lunn #endif
717c6e970a0SAndrew Lunn 	return false;
718c6e970a0SAndrew Lunn }
719c6e970a0SAndrew Lunn 
720cc1939e4SVladimir Oltean static inline bool dsa_can_decode(const struct sk_buff *skb,
721cc1939e4SVladimir Oltean 				  struct net_device *dev)
722cc1939e4SVladimir Oltean {
723cc1939e4SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
724cc1939e4SVladimir Oltean 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
725cc1939e4SVladimir Oltean #endif
726cc1939e4SVladimir Oltean 	return false;
727cc1939e4SVladimir Oltean }
728cc1939e4SVladimir Oltean 
7299790cf20SVladimir Oltean /* All DSA tags that push the EtherType to the right (basically all except tail
7309790cf20SVladimir Oltean  * tags, which don't break dissection) can be treated the same from the
7319790cf20SVladimir Oltean  * perspective of the flow dissector.
7329790cf20SVladimir Oltean  *
7339790cf20SVladimir Oltean  * We need to return:
7349790cf20SVladimir Oltean  *  - offset: the (B - A) difference between:
7359790cf20SVladimir Oltean  *    A. the position of the real EtherType and
7369790cf20SVladimir Oltean  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
7379790cf20SVladimir Oltean  *       after the normal EtherType was supposed to be)
7389790cf20SVladimir Oltean  *    The offset in bytes is exactly equal to the tagger overhead (and half of
7399790cf20SVladimir Oltean  *    that, in __be16 shorts).
7409790cf20SVladimir Oltean  *
7419790cf20SVladimir Oltean  *  - proto: the value of the real EtherType.
7429790cf20SVladimir Oltean  */
7439790cf20SVladimir Oltean static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
7449790cf20SVladimir Oltean 						__be16 *proto, int *offset)
7459790cf20SVladimir Oltean {
7469790cf20SVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA)
7479790cf20SVladimir Oltean 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
7489790cf20SVladimir Oltean 	int tag_len = ops->overhead;
7499790cf20SVladimir Oltean 
7509790cf20SVladimir Oltean 	*offset = tag_len;
7519790cf20SVladimir Oltean 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
7529790cf20SVladimir Oltean #endif
7539790cf20SVladimir Oltean }
7549790cf20SVladimir Oltean 
7554cfab356SFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
7564cfab356SFlorian Fainelli static inline int __dsa_netdevice_ops_check(struct net_device *dev)
7574cfab356SFlorian Fainelli {
7584cfab356SFlorian Fainelli 	int err = -EOPNOTSUPP;
7594cfab356SFlorian Fainelli 
7604cfab356SFlorian Fainelli 	if (!dev->dsa_ptr)
7614cfab356SFlorian Fainelli 		return err;
7624cfab356SFlorian Fainelli 
7634cfab356SFlorian Fainelli 	if (!dev->dsa_ptr->netdev_ops)
7644cfab356SFlorian Fainelli 		return err;
7654cfab356SFlorian Fainelli 
7664cfab356SFlorian Fainelli 	return 0;
7674cfab356SFlorian Fainelli }
7684cfab356SFlorian Fainelli 
7694cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
7704cfab356SFlorian Fainelli 				   int cmd)
7714cfab356SFlorian Fainelli {
7724cfab356SFlorian Fainelli 	const struct dsa_netdevice_ops *ops;
7734cfab356SFlorian Fainelli 	int err;
7744cfab356SFlorian Fainelli 
7754cfab356SFlorian Fainelli 	err = __dsa_netdevice_ops_check(dev);
7764cfab356SFlorian Fainelli 	if (err)
7774cfab356SFlorian Fainelli 		return err;
7784cfab356SFlorian Fainelli 
7794cfab356SFlorian Fainelli 	ops = dev->dsa_ptr->netdev_ops;
7804cfab356SFlorian Fainelli 
7814cfab356SFlorian Fainelli 	return ops->ndo_do_ioctl(dev, ifr, cmd);
7824cfab356SFlorian Fainelli }
7834cfab356SFlorian Fainelli #else
7844cfab356SFlorian Fainelli static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
7854cfab356SFlorian Fainelli 				   int cmd)
7864cfab356SFlorian Fainelli {
7874cfab356SFlorian Fainelli 	return -EOPNOTSUPP;
7884cfab356SFlorian Fainelli }
7894cfab356SFlorian Fainelli #endif
7904cfab356SFlorian Fainelli 
79183c0afaeSAndrew Lunn void dsa_unregister_switch(struct dsa_switch *ds);
79223c9ee49SVivien Didelot int dsa_register_switch(struct dsa_switch *ds);
7933b7bc1f0SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
794ea825e70SFlorian Fainelli #ifdef CONFIG_PM_SLEEP
795ea825e70SFlorian Fainelli int dsa_switch_suspend(struct dsa_switch *ds);
796ea825e70SFlorian Fainelli int dsa_switch_resume(struct dsa_switch *ds);
797ea825e70SFlorian Fainelli #else
798ea825e70SFlorian Fainelli static inline int dsa_switch_suspend(struct dsa_switch *ds)
799ea825e70SFlorian Fainelli {
800ea825e70SFlorian Fainelli 	return 0;
801ea825e70SFlorian Fainelli }
802ea825e70SFlorian Fainelli static inline int dsa_switch_resume(struct dsa_switch *ds)
803ea825e70SFlorian Fainelli {
804ea825e70SFlorian Fainelli 	return 0;
805ea825e70SFlorian Fainelli }
806ea825e70SFlorian Fainelli #endif /* CONFIG_PM_SLEEP */
807ea825e70SFlorian Fainelli 
80860724d4bSFlorian Fainelli enum dsa_notifier_type {
80960724d4bSFlorian Fainelli 	DSA_PORT_REGISTER,
81060724d4bSFlorian Fainelli 	DSA_PORT_UNREGISTER,
81160724d4bSFlorian Fainelli };
81260724d4bSFlorian Fainelli 
81360724d4bSFlorian Fainelli struct dsa_notifier_info {
81460724d4bSFlorian Fainelli 	struct net_device *dev;
81560724d4bSFlorian Fainelli };
81660724d4bSFlorian Fainelli 
81760724d4bSFlorian Fainelli struct dsa_notifier_register_info {
81860724d4bSFlorian Fainelli 	struct dsa_notifier_info info;	/* must be first */
81960724d4bSFlorian Fainelli 	struct net_device *master;
82060724d4bSFlorian Fainelli 	unsigned int port_number;
82160724d4bSFlorian Fainelli 	unsigned int switch_number;
82260724d4bSFlorian Fainelli };
82360724d4bSFlorian Fainelli 
82460724d4bSFlorian Fainelli static inline struct net_device *
82560724d4bSFlorian Fainelli dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
82660724d4bSFlorian Fainelli {
82760724d4bSFlorian Fainelli 	return info->dev;
82860724d4bSFlorian Fainelli }
82960724d4bSFlorian Fainelli 
83060724d4bSFlorian Fainelli #if IS_ENABLED(CONFIG_NET_DSA)
83160724d4bSFlorian Fainelli int register_dsa_notifier(struct notifier_block *nb);
83260724d4bSFlorian Fainelli int unregister_dsa_notifier(struct notifier_block *nb);
83360724d4bSFlorian Fainelli int call_dsa_notifiers(unsigned long val, struct net_device *dev,
83460724d4bSFlorian Fainelli 		       struct dsa_notifier_info *info);
83560724d4bSFlorian Fainelli #else
83660724d4bSFlorian Fainelli static inline int register_dsa_notifier(struct notifier_block *nb)
83760724d4bSFlorian Fainelli {
83860724d4bSFlorian Fainelli 	return 0;
83960724d4bSFlorian Fainelli }
84060724d4bSFlorian Fainelli 
84160724d4bSFlorian Fainelli static inline int unregister_dsa_notifier(struct notifier_block *nb)
84260724d4bSFlorian Fainelli {
84360724d4bSFlorian Fainelli 	return 0;
84460724d4bSFlorian Fainelli }
84560724d4bSFlorian Fainelli 
84660724d4bSFlorian Fainelli static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
84760724d4bSFlorian Fainelli 				     struct dsa_notifier_info *info)
84860724d4bSFlorian Fainelli {
84960724d4bSFlorian Fainelli 	return NOTIFY_DONE;
85060724d4bSFlorian Fainelli }
85160724d4bSFlorian Fainelli #endif
85260724d4bSFlorian Fainelli 
8530a5f14ceSFlorian Fainelli /* Broadcom tag specific helpers to insert and extract queue/port number */
8540a5f14ceSFlorian Fainelli #define BRCM_TAG_SET_PORT_QUEUE(p, q)	((p) << 8 | q)
8550a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_PORT(v)		((v) >> 8)
8560a5f14ceSFlorian Fainelli #define BRCM_TAG_GET_QUEUE(v)		((v) & 0xff)
8570a5f14ceSFlorian Fainelli 
858cf963573SFlorian Fainelli 
85997a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
860cf963573SFlorian Fainelli int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
861cf963573SFlorian Fainelli int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
862cf963573SFlorian Fainelli int dsa_port_get_phy_sset_count(struct dsa_port *dp);
86311d8f3ddSFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
864cf963573SFlorian Fainelli 
865d3b8c049SAndrew Lunn struct dsa_tag_driver {
866d3b8c049SAndrew Lunn 	const struct dsa_device_ops *ops;
867d3b8c049SAndrew Lunn 	struct list_head list;
868d3b8c049SAndrew Lunn 	struct module *owner;
869d3b8c049SAndrew Lunn };
870d3b8c049SAndrew Lunn 
871d3b8c049SAndrew Lunn void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
872d3b8c049SAndrew Lunn 			      unsigned int count,
873d3b8c049SAndrew Lunn 			      struct module *owner);
874d3b8c049SAndrew Lunn void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
875d3b8c049SAndrew Lunn 				unsigned int count);
876d3b8c049SAndrew Lunn 
877d3b8c049SAndrew Lunn #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
878d3b8c049SAndrew Lunn static int __init dsa_tag_driver_module_init(void)			\
879d3b8c049SAndrew Lunn {									\
880d3b8c049SAndrew Lunn 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
881d3b8c049SAndrew Lunn 				 THIS_MODULE);				\
882d3b8c049SAndrew Lunn 	return 0;							\
883d3b8c049SAndrew Lunn }									\
884d3b8c049SAndrew Lunn module_init(dsa_tag_driver_module_init);				\
885d3b8c049SAndrew Lunn 									\
886d3b8c049SAndrew Lunn static void __exit dsa_tag_driver_module_exit(void)			\
887d3b8c049SAndrew Lunn {									\
888d3b8c049SAndrew Lunn 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
889d3b8c049SAndrew Lunn }									\
890d3b8c049SAndrew Lunn module_exit(dsa_tag_driver_module_exit)
891d3b8c049SAndrew Lunn 
892d3b8c049SAndrew Lunn /**
893d3b8c049SAndrew Lunn  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
894d3b8c049SAndrew Lunn  * drivers
895d3b8c049SAndrew Lunn  * @__ops_array: Array of tag driver strucutres
896d3b8c049SAndrew Lunn  *
897d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
898d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
899d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
900d3b8c049SAndrew Lunn  */
901d3b8c049SAndrew Lunn #define module_dsa_tag_drivers(__ops_array)				\
902d3b8c049SAndrew Lunn dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
903d3b8c049SAndrew Lunn 
904d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
905d3b8c049SAndrew Lunn 
906d3b8c049SAndrew Lunn /* Create a static structure we can build a linked list of dsa_tag
907d3b8c049SAndrew Lunn  * drivers
908d3b8c049SAndrew Lunn  */
909d3b8c049SAndrew Lunn #define DSA_TAG_DRIVER(__ops)						\
910d3b8c049SAndrew Lunn static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
911d3b8c049SAndrew Lunn 	.ops = &__ops,							\
912d3b8c049SAndrew Lunn }
913d3b8c049SAndrew Lunn 
914d3b8c049SAndrew Lunn /**
915d3b8c049SAndrew Lunn  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
916d3b8c049SAndrew Lunn  * driver
917d3b8c049SAndrew Lunn  * @__ops: Single tag driver structures
918d3b8c049SAndrew Lunn  *
919d3b8c049SAndrew Lunn  * Helper macro for DSA tag drivers which do not do anything special
920d3b8c049SAndrew Lunn  * in module init/exit. Each module may only use this macro once, and
921d3b8c049SAndrew Lunn  * calling it replaces module_init() and module_exit().
922d3b8c049SAndrew Lunn  */
923d3b8c049SAndrew Lunn #define module_dsa_tag_driver(__ops)					\
924d3b8c049SAndrew Lunn DSA_TAG_DRIVER(__ops);							\
925d3b8c049SAndrew Lunn 									\
926d3b8c049SAndrew Lunn static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
927d3b8c049SAndrew Lunn 	&DSA_TAG_DRIVER_NAME(__ops)					\
928d3b8c049SAndrew Lunn };									\
929d3b8c049SAndrew Lunn module_dsa_tag_drivers(dsa_tag_driver_array)
93091da11f8SLennert Buytenhek #endif
931d3b8c049SAndrew Lunn 
932