xref: /linux/include/net/dsa.h (revision c36461825469a9ceee2346a2e89286c522525da7)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  */
6 
7 #ifndef __LINUX_NET_DSA_H
8 #define __LINUX_NET_DSA_H
9 
10 #include <linux/if.h>
11 #include <linux/if_ether.h>
12 #include <linux/list.h>
13 #include <linux/notifier.h>
14 #include <linux/timer.h>
15 #include <linux/workqueue.h>
16 #include <linux/of.h>
17 #include <linux/ethtool.h>
18 #include <linux/net_tstamp.h>
19 #include <linux/phy.h>
20 #include <linux/platform_data/dsa.h>
21 #include <linux/phylink.h>
22 #include <net/devlink.h>
23 #include <net/switchdev.h>
24 
25 struct dsa_8021q_context;
26 struct tc_action;
27 
28 #define DSA_TAG_PROTO_NONE_VALUE		0
29 #define DSA_TAG_PROTO_BRCM_VALUE		1
30 #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE	2
31 #define DSA_TAG_PROTO_DSA_VALUE			3
32 #define DSA_TAG_PROTO_EDSA_VALUE		4
33 #define DSA_TAG_PROTO_GSWIP_VALUE		5
34 #define DSA_TAG_PROTO_KSZ9477_VALUE		6
35 #define DSA_TAG_PROTO_KSZ9893_VALUE		7
36 #define DSA_TAG_PROTO_LAN9303_VALUE		8
37 #define DSA_TAG_PROTO_MTK_VALUE			9
38 #define DSA_TAG_PROTO_QCA_VALUE			10
39 #define DSA_TAG_PROTO_TRAILER_VALUE		11
40 #define DSA_TAG_PROTO_8021Q_VALUE		12
41 #define DSA_TAG_PROTO_SJA1105_VALUE		13
42 #define DSA_TAG_PROTO_KSZ8795_VALUE		14
43 #define DSA_TAG_PROTO_OCELOT_VALUE		15
44 #define DSA_TAG_PROTO_AR9331_VALUE		16
45 #define DSA_TAG_PROTO_RTL4_A_VALUE		17
46 #define DSA_TAG_PROTO_HELLCREEK_VALUE		18
47 #define DSA_TAG_PROTO_XRS700X_VALUE		19
48 #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE	20
49 #define DSA_TAG_PROTO_SEVILLE_VALUE		21
50 #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE		22
51 #define DSA_TAG_PROTO_SJA1110_VALUE		23
52 #define DSA_TAG_PROTO_RTL8_4_VALUE		24
53 #define DSA_TAG_PROTO_RTL8_4T_VALUE		25
54 #define DSA_TAG_PROTO_RZN1_A5PSW_VALUE		26
55 #define DSA_TAG_PROTO_LAN937X_VALUE		27
56 #define DSA_TAG_PROTO_VSC73XX_8021Q_VALUE	28
57 #define DSA_TAG_PROTO_BRCM_LEGACY_FCS_VALUE	29
58 #define DSA_TAG_PROTO_YT921X_VALUE		30
59 #define DSA_TAG_PROTO_MXL_GSW1XX_VALUE		31
60 #define DSA_TAG_PROTO_MXL862_VALUE		32
61 #define DSA_TAG_PROTO_NETC_VALUE		33
62 #define DSA_TAG_PROTO_KSZ8463_VALUE		34
63 #define DSA_TAG_PROTO_MT7628_VALUE		35
64 
65 enum dsa_tag_protocol {
66 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
67 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
68 	DSA_TAG_PROTO_BRCM_LEGACY	= DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
69 	DSA_TAG_PROTO_BRCM_LEGACY_FCS	= DSA_TAG_PROTO_BRCM_LEGACY_FCS_VALUE,
70 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
71 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
72 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
73 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
74 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
75 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
76 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
77 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
78 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
79 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
80 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
81 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
82 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
83 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
84 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
85 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
86 	DSA_TAG_PROTO_HELLCREEK		= DSA_TAG_PROTO_HELLCREEK_VALUE,
87 	DSA_TAG_PROTO_XRS700X		= DSA_TAG_PROTO_XRS700X_VALUE,
88 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
89 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
90 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
91 	DSA_TAG_PROTO_RTL8_4		= DSA_TAG_PROTO_RTL8_4_VALUE,
92 	DSA_TAG_PROTO_RTL8_4T		= DSA_TAG_PROTO_RTL8_4T_VALUE,
93 	DSA_TAG_PROTO_RZN1_A5PSW	= DSA_TAG_PROTO_RZN1_A5PSW_VALUE,
94 	DSA_TAG_PROTO_LAN937X		= DSA_TAG_PROTO_LAN937X_VALUE,
95 	DSA_TAG_PROTO_VSC73XX_8021Q	= DSA_TAG_PROTO_VSC73XX_8021Q_VALUE,
96 	DSA_TAG_PROTO_YT921X		= DSA_TAG_PROTO_YT921X_VALUE,
97 	DSA_TAG_PROTO_MXL_GSW1XX	= DSA_TAG_PROTO_MXL_GSW1XX_VALUE,
98 	DSA_TAG_PROTO_MXL862		= DSA_TAG_PROTO_MXL862_VALUE,
99 	DSA_TAG_PROTO_NETC		= DSA_TAG_PROTO_NETC_VALUE,
100 	DSA_TAG_PROTO_KSZ8463		= DSA_TAG_PROTO_KSZ8463_VALUE,
101 	DSA_TAG_PROTO_MT7628		= DSA_TAG_PROTO_MT7628_VALUE,
102 };
103 
104 struct dsa_switch;
105 
106 struct dsa_device_ops {
107 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
108 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
109 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
110 			     int *offset);
111 	int (*connect)(struct dsa_switch *ds);
112 	void (*disconnect)(struct dsa_switch *ds);
113 	unsigned int needed_headroom;
114 	unsigned int needed_tailroom;
115 	const char *name;
116 	enum dsa_tag_protocol proto;
117 	/* Some tagging protocols either mangle or shift the destination MAC
118 	 * address, in which case the DSA conduit would drop packets on ingress
119 	 * if what it understands out of the destination MAC address is not in
120 	 * its RX filter.
121 	 */
122 	bool promisc_on_conduit;
123 };
124 
125 struct dsa_lag {
126 	struct net_device *dev;
127 	unsigned int id;
128 	struct mutex fdb_lock;
129 	struct list_head fdbs;
130 	refcount_t refcount;
131 };
132 
133 struct dsa_switch_tree {
134 	struct list_head	list;
135 
136 	/* List of switch ports */
137 	struct list_head ports;
138 
139 	/* Notifier chain for switch-wide events */
140 	struct raw_notifier_head	nh;
141 
142 	/* Tree identifier */
143 	unsigned int index;
144 
145 	/* Number of switches attached to this tree */
146 	struct kref refcount;
147 
148 	/* Maps offloaded LAG netdevs to a zero-based linear ID for
149 	 * drivers that need it.
150 	 */
151 	struct dsa_lag **lags;
152 
153 	/* Tagging protocol operations */
154 	const struct dsa_device_ops *tag_ops;
155 
156 	/* Default tagging protocol preferred by the switches in this
157 	 * tree.
158 	 */
159 	enum dsa_tag_protocol default_proto;
160 
161 	/* Has this tree been applied to the hardware? */
162 	bool setup;
163 
164 	/*
165 	 * Configuration data for the platform device that owns
166 	 * this dsa switch tree instance.
167 	 */
168 	struct dsa_platform_data	*pd;
169 
170 	/* List of DSA links composing the routing table */
171 	struct list_head rtable;
172 
173 	/* Length of "lags" array */
174 	unsigned int lags_len;
175 
176 	/* Track the largest switch index within a tree */
177 	unsigned int last_switch;
178 };
179 
180 /* LAG IDs are one-based, the dst->lags array is zero-based */
181 #define dsa_lags_foreach_id(_id, _dst)				\
182 	for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++)	\
183 		if ((_dst)->lags[(_id) - 1])
184 
185 #define dsa_lag_foreach_port(_dp, _dst, _lag)			\
186 	list_for_each_entry((_dp), &(_dst)->ports, list)	\
187 		if (dsa_port_offloads_lag((_dp), (_lag)))
188 
189 #define dsa_hsr_foreach_port(_dp, _ds, _hsr)			\
190 	list_for_each_entry((_dp), &(_ds)->dst->ports, list)	\
191 		if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
192 
193 static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
194 					    unsigned int id)
195 {
196 	/* DSA LAG IDs are one-based, dst->lags is zero-based */
197 	return dst->lags[id - 1];
198 }
199 
200 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
201 			     struct net_device *lag_dev)
202 {
203 	unsigned int id;
204 
205 	dsa_lags_foreach_id(id, dst) {
206 		struct dsa_lag *lag = dsa_lag_by_id(dst, id);
207 
208 		if (lag->dev == lag_dev)
209 			return lag->id;
210 	}
211 
212 	return -ENODEV;
213 }
214 
215 /* TC matchall action types */
216 enum dsa_port_mall_action_type {
217 	DSA_PORT_MALL_MIRROR,
218 	DSA_PORT_MALL_POLICER,
219 };
220 
221 /* TC mirroring entry */
222 struct dsa_mall_mirror_tc_entry {
223 	u8 to_local_port;
224 	bool ingress;
225 };
226 
227 /* TC matchall entry */
228 struct dsa_mall_tc_entry {
229 	struct list_head list;
230 	unsigned long cookie;
231 	enum dsa_port_mall_action_type type;
232 	union {
233 		struct dsa_mall_mirror_tc_entry mirror;
234 		struct flow_action_police policer;
235 	};
236 };
237 
238 struct dsa_bridge {
239 	struct net_device *dev;
240 	unsigned int num;
241 	bool tx_fwd_offload;
242 	refcount_t refcount;
243 };
244 
245 struct dsa_port {
246 	/* A CPU port is physically connected to a conduit device. A user port
247 	 * exposes a network device to user-space, called 'user' here.
248 	 */
249 	union {
250 		struct net_device *conduit;
251 		struct net_device *user;
252 	};
253 
254 	/* Copy of the tagging protocol operations, for quicker access
255 	 * in the data path. Valid only for the CPU ports.
256 	 */
257 	const struct dsa_device_ops *tag_ops;
258 
259 	/* Copies for faster access in conduit receive hot path */
260 	struct dsa_switch_tree *dst;
261 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
262 
263 	struct dsa_switch	*ds;
264 
265 	unsigned int		index;
266 
267 	enum {
268 		DSA_PORT_TYPE_UNUSED = 0,
269 		DSA_PORT_TYPE_CPU,
270 		DSA_PORT_TYPE_DSA,
271 		DSA_PORT_TYPE_USER,
272 	} type;
273 
274 	const char		*name;
275 	struct dsa_port		*cpu_dp;
276 	u8			mac[ETH_ALEN];
277 
278 	u8			stp_state;
279 
280 	/* Warning: the following bit fields are not atomic, and updating them
281 	 * can only be done from code paths where concurrency is not possible
282 	 * (probe time or under rtnl_lock).
283 	 */
284 	u8			vlan_filtering:1;
285 
286 	/* Managed by DSA on user ports and by drivers on CPU and DSA ports */
287 	u8			learning:1;
288 
289 	u8			lag_tx_enabled:1;
290 
291 	/* conduit state bits, valid only on CPU ports */
292 	u8			conduit_admin_up:1;
293 	u8			conduit_oper_up:1;
294 
295 	/* Valid only on user ports */
296 	u8			cpu_port_in_lag:1;
297 
298 	u8			setup:1;
299 
300 	struct device_node	*dn;
301 	unsigned int		ageing_time;
302 
303 	struct dsa_bridge	*bridge;
304 	struct devlink_port	devlink_port;
305 	struct phylink		*pl;
306 	struct phylink_config	pl_config;
307 	netdevice_tracker	conduit_tracker;
308 	struct dsa_lag		*lag;
309 	struct net_device	*hsr_dev;
310 
311 	struct list_head list;
312 
313 	/*
314 	 * Original copy of the conduit netdev ethtool_ops
315 	 */
316 	const struct ethtool_ops *orig_ethtool_ops;
317 
318 	/* List of MAC addresses that must be forwarded on this port.
319 	 * These are only valid on CPU ports and DSA links.
320 	 */
321 	struct mutex		addr_lists_lock;
322 	struct list_head	fdbs;
323 	struct list_head	mdbs;
324 
325 	struct mutex		vlans_lock;
326 	union {
327 		/* List of VLANs that CPU and DSA ports are members of.
328 		 * Access to this is serialized by the sleepable @vlans_lock.
329 		 */
330 		struct list_head	vlans;
331 		/* List of VLANs that user ports are members of.
332 		 * Access to this is serialized by netif_addr_lock_bh().
333 		 */
334 		struct list_head	user_vlans;
335 	};
336 };
337 
338 static inline struct dsa_port *
339 dsa_phylink_to_port(struct phylink_config *config)
340 {
341 	return container_of(config, struct dsa_port, pl_config);
342 }
343 
344 /* TODO: ideally DSA ports would have a single dp->link_dp member,
345  * and no dst->rtable nor this struct dsa_link would be needed,
346  * but this would require some more complex tree walking,
347  * so keep it stupid at the moment and list them all.
348  */
349 struct dsa_link {
350 	struct dsa_port *dp;
351 	struct dsa_port *link_dp;
352 	struct list_head list;
353 };
354 
355 enum dsa_db_type {
356 	DSA_DB_PORT,
357 	DSA_DB_LAG,
358 	DSA_DB_BRIDGE,
359 };
360 
361 struct dsa_db {
362 	enum dsa_db_type type;
363 
364 	union {
365 		const struct dsa_port *dp;
366 		struct dsa_lag lag;
367 		struct dsa_bridge bridge;
368 	};
369 };
370 
371 struct dsa_mac_addr {
372 	unsigned char addr[ETH_ALEN];
373 	u16 vid;
374 	refcount_t refcount;
375 	struct list_head list;
376 	struct dsa_db db;
377 };
378 
379 struct dsa_vlan {
380 	u16 vid;
381 	refcount_t refcount;
382 	struct list_head list;
383 };
384 
385 struct dsa_switch {
386 	struct device *dev;
387 
388 	/*
389 	 * Parent switch tree, and switch index.
390 	 */
391 	struct dsa_switch_tree	*dst;
392 	unsigned int		index;
393 
394 	/* Warning: the following bit fields are not atomic, and updating them
395 	 * can only be done from code paths where concurrency is not possible
396 	 * (probe time or under rtnl_lock).
397 	 */
398 	u32			setup:1;
399 
400 	/* Disallow bridge core from requesting different VLAN awareness
401 	 * settings on ports if not hardware-supported
402 	 */
403 	u32			vlan_filtering_is_global:1;
404 
405 	/* Keep VLAN filtering enabled on ports not offloading any upper */
406 	u32			needs_standalone_vlan_filtering:1;
407 
408 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
409 	 * that have vlan_filtering=0. All drivers should ideally set this (and
410 	 * then the option would get removed), but it is unknown whether this
411 	 * would break things or not.
412 	 */
413 	u32			configure_vlan_while_not_filtering:1;
414 
415 	/* Pop the default_pvid of VLAN-unaware bridge ports from tagged frames.
416 	 * DEPRECATED: Do NOT set this field in new drivers. Instead look at
417 	 * the dsa_software_vlan_untag() comments.
418 	 */
419 	u32			untag_bridge_pvid:1;
420 	/* Pop the default_pvid of VLAN-aware bridge ports from tagged frames.
421 	 * Useful if the switch cannot preserve the VLAN tag as seen on the
422 	 * wire for user port ingress, and chooses to send all frames as
423 	 * VLAN-tagged to the CPU, including those which were originally
424 	 * untagged.
425 	 */
426 	u32			untag_vlan_aware_bridge_pvid:1;
427 
428 	/* Let DSA manage the FDB entries towards the
429 	 * CPU, based on the software bridge database.
430 	 */
431 	u32			assisted_learning_on_cpu_port:1;
432 
433 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
434 	 * should be retrieved from here and not from the per-port settings.
435 	 */
436 	u32			vlan_filtering:1;
437 
438 	/* For switches that only have the MRU configurable. To ensure the
439 	 * configured MTU is not exceeded, normalization of MRU on all bridged
440 	 * interfaces is needed.
441 	 */
442 	u32			mtu_enforcement_ingress:1;
443 
444 	/* Drivers that isolate the FDBs of multiple bridges must set this
445 	 * to true to receive the bridge as an argument in .port_fdb_{add,del}
446 	 * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
447 	 * passed as zero.
448 	 */
449 	u32			fdb_isolation:1;
450 
451 	/* Drivers that have global DSCP mapping settings must set this to
452 	 * true to automatically apply the settings to all ports.
453 	 */
454 	u32			dscp_prio_mapping_is_global:1;
455 
456 	/* Listener for switch fabric events */
457 	struct notifier_block	nb;
458 
459 	/*
460 	 * Give the switch driver somewhere to hang its private data
461 	 * structure.
462 	 */
463 	void *priv;
464 
465 	void *tagger_data;
466 
467 	/*
468 	 * Configuration data for this switch.
469 	 */
470 	struct dsa_chip_data	*cd;
471 
472 	/*
473 	 * The switch operations.
474 	 */
475 	const struct dsa_switch_ops	*ops;
476 
477 	/*
478 	 * Allow a DSA switch driver to override the phylink MAC ops
479 	 */
480 	const struct phylink_mac_ops	*phylink_mac_ops;
481 
482 	/*
483 	 * User mii_bus and devices for the individual ports.
484 	 */
485 	u32			phys_mii_mask;
486 	struct mii_bus		*user_mii_bus;
487 
488 	/* Ageing Time limits in msecs */
489 	unsigned int ageing_time_min;
490 	unsigned int ageing_time_max;
491 
492 	/* Storage for drivers using tag_8021q */
493 	struct dsa_8021q_context *tag_8021q_ctx;
494 
495 	/* devlink used to represent this switch device */
496 	struct devlink		*devlink;
497 
498 	/* Number of switch port queues */
499 	unsigned int		num_tx_queues;
500 
501 	/* Drivers that benefit from having an ID associated with each
502 	 * offloaded LAG should set this to the maximum number of
503 	 * supported IDs. DSA will then maintain a mapping of _at
504 	 * least_ these many IDs, accessible to drivers via
505 	 * dsa_lag_id().
506 	 */
507 	unsigned int		num_lag_ids;
508 
509 	/* Drivers that support bridge forwarding offload or FDB isolation
510 	 * should set this to the maximum number of bridges spanning the same
511 	 * switch tree (or all trees, in the case of cross-tree bridging
512 	 * support) that can be offloaded.
513 	 */
514 	unsigned int		max_num_bridges;
515 
516 	unsigned int		num_ports;
517 };
518 
519 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
520 {
521 	struct dsa_switch_tree *dst = ds->dst;
522 	struct dsa_port *dp;
523 
524 	list_for_each_entry(dp, &dst->ports, list)
525 		if (dp->ds == ds && dp->index == p)
526 			return dp;
527 
528 	return NULL;
529 }
530 
531 static inline bool dsa_port_is_dsa(struct dsa_port *port)
532 {
533 	return port->type == DSA_PORT_TYPE_DSA;
534 }
535 
536 static inline bool dsa_port_is_cpu(struct dsa_port *port)
537 {
538 	return port->type == DSA_PORT_TYPE_CPU;
539 }
540 
541 static inline bool dsa_port_is_user(struct dsa_port *dp)
542 {
543 	return dp->type == DSA_PORT_TYPE_USER;
544 }
545 
546 static inline bool dsa_port_is_unused(struct dsa_port *dp)
547 {
548 	return dp->type == DSA_PORT_TYPE_UNUSED;
549 }
550 
551 static inline bool dsa_port_conduit_is_operational(struct dsa_port *dp)
552 {
553 	return dsa_port_is_cpu(dp) && dp->conduit_admin_up &&
554 	       dp->conduit_oper_up;
555 }
556 
557 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
558 {
559 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
560 }
561 
562 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
563 {
564 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
565 }
566 
567 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
568 {
569 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
570 }
571 
572 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
573 {
574 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
575 }
576 
577 #define dsa_tree_for_each_user_port(_dp, _dst) \
578 	list_for_each_entry((_dp), &(_dst)->ports, list) \
579 		if (dsa_port_is_user((_dp)))
580 
581 #define dsa_tree_for_each_user_port_continue_reverse(_dp, _dst) \
582 	list_for_each_entry_continue_reverse((_dp), &(_dst)->ports, list) \
583 		if (dsa_port_is_user((_dp)))
584 
585 #define dsa_tree_for_each_cpu_port(_dp, _dst) \
586 	list_for_each_entry((_dp), &(_dst)->ports, list) \
587 		if (dsa_port_is_cpu((_dp)))
588 
589 #define dsa_switch_for_each_port(_dp, _ds) \
590 	list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
591 		if ((_dp)->ds == (_ds))
592 
593 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
594 	list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
595 		if ((_dp)->ds == (_ds))
596 
597 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
598 	list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
599 		if ((_dp)->ds == (_ds))
600 
601 #define dsa_switch_for_each_available_port(_dp, _ds) \
602 	dsa_switch_for_each_port((_dp), (_ds)) \
603 		if (!dsa_port_is_unused((_dp)))
604 
605 #define dsa_switch_for_each_user_port(_dp, _ds) \
606 	dsa_switch_for_each_port((_dp), (_ds)) \
607 		if (dsa_port_is_user((_dp)))
608 
609 #define dsa_switch_for_each_user_port_continue_reverse(_dp, _ds) \
610 	dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
611 		if (dsa_port_is_user((_dp)))
612 
613 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
614 	dsa_switch_for_each_port((_dp), (_ds)) \
615 		if (dsa_port_is_cpu((_dp)))
616 
617 #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \
618 	dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
619 		if (dsa_port_is_cpu((_dp)))
620 
621 static inline u32 dsa_user_ports(struct dsa_switch *ds)
622 {
623 	struct dsa_port *dp;
624 	u32 mask = 0;
625 
626 	dsa_switch_for_each_user_port(dp, ds)
627 		mask |= BIT(dp->index);
628 
629 	return mask;
630 }
631 
632 static inline u32 dsa_cpu_ports(struct dsa_switch *ds)
633 {
634 	struct dsa_port *cpu_dp;
635 	u32 mask = 0;
636 
637 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
638 		mask |= BIT(cpu_dp->index);
639 
640 	return mask;
641 }
642 
643 /* Return the local port used to reach an arbitrary switch device */
644 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
645 {
646 	struct dsa_switch_tree *dst = ds->dst;
647 	struct dsa_link *dl;
648 
649 	list_for_each_entry(dl, &dst->rtable, list)
650 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
651 			return dl->dp->index;
652 
653 	return ds->num_ports;
654 }
655 
656 /* Return the local port used to reach an arbitrary switch port */
657 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
658 					    int port)
659 {
660 	if (device == ds->index)
661 		return port;
662 	else
663 		return dsa_routing_port(ds, device);
664 }
665 
666 /* Return the local port used to reach the dedicated CPU port */
667 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
668 {
669 	const struct dsa_port *dp = dsa_to_port(ds, port);
670 	const struct dsa_port *cpu_dp = dp->cpu_dp;
671 
672 	if (!cpu_dp)
673 		return port;
674 
675 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
676 }
677 
678 /* Return true if this is the local port used to reach the CPU port */
679 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
680 {
681 	if (dsa_is_unused_port(ds, port))
682 		return false;
683 
684 	return port == dsa_upstream_port(ds, port);
685 }
686 
687 /* Return true if this is a DSA port leading away from the CPU */
688 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
689 {
690 	return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
691 }
692 
693 /* Return the local port used to reach the CPU port */
694 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
695 {
696 	struct dsa_port *dp;
697 
698 	dsa_switch_for_each_available_port(dp, ds) {
699 		return dsa_upstream_port(ds, dp->index);
700 	}
701 
702 	return ds->num_ports;
703 }
704 
705 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
706  * that the routing port from @downstream_ds to @upstream_ds is also the port
707  * which @downstream_ds uses to reach its dedicated CPU.
708  */
709 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
710 					     struct dsa_switch *downstream_ds)
711 {
712 	int routing_port;
713 
714 	if (upstream_ds == downstream_ds)
715 		return true;
716 
717 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
718 
719 	return dsa_is_upstream_port(downstream_ds, routing_port);
720 }
721 
722 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
723 {
724 	const struct dsa_switch *ds = dp->ds;
725 
726 	if (ds->vlan_filtering_is_global)
727 		return ds->vlan_filtering;
728 	else
729 		return dp->vlan_filtering;
730 }
731 
732 static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
733 {
734 	return dp->lag ? dp->lag->id : 0;
735 }
736 
737 static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
738 {
739 	return dp->lag ? dp->lag->dev : NULL;
740 }
741 
742 static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
743 					 const struct dsa_lag *lag)
744 {
745 	return dsa_port_lag_dev_get(dp) == lag->dev;
746 }
747 
748 static inline struct net_device *dsa_port_to_conduit(const struct dsa_port *dp)
749 {
750 	if (dp->cpu_port_in_lag)
751 		return dsa_port_lag_dev_get(dp->cpu_dp);
752 
753 	return dp->cpu_dp->conduit;
754 }
755 
756 static inline
757 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
758 {
759 	if (!dp->bridge)
760 		return NULL;
761 
762 	if (dp->lag)
763 		return dp->lag->dev;
764 	else if (dp->hsr_dev)
765 		return dp->hsr_dev;
766 
767 	return dp->user;
768 }
769 
770 static inline struct net_device *
771 dsa_port_bridge_dev_get(const struct dsa_port *dp)
772 {
773 	return dp->bridge ? dp->bridge->dev : NULL;
774 }
775 
776 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
777 {
778 	return dp->bridge ? dp->bridge->num : 0;
779 }
780 
781 static inline bool dsa_port_bridge_same(const struct dsa_port *a,
782 					const struct dsa_port *b)
783 {
784 	struct net_device *br_a = dsa_port_bridge_dev_get(a);
785 	struct net_device *br_b = dsa_port_bridge_dev_get(b);
786 
787 	/* Standalone ports are not in the same bridge with one another */
788 	return (!br_a || !br_b) ? false : (br_a == br_b);
789 }
790 
791 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
792 						 const struct net_device *dev)
793 {
794 	return dsa_port_to_bridge_port(dp) == dev;
795 }
796 
797 static inline bool
798 dsa_port_offloads_bridge_dev(struct dsa_port *dp,
799 			     const struct net_device *bridge_dev)
800 {
801 	/* DSA ports connected to a bridge, and event was emitted
802 	 * for the bridge.
803 	 */
804 	return dsa_port_bridge_dev_get(dp) == bridge_dev;
805 }
806 
807 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
808 					    const struct dsa_bridge *bridge)
809 {
810 	return dsa_port_bridge_dev_get(dp) == bridge->dev;
811 }
812 
813 /* Returns true if any port of this tree offloads the given net_device */
814 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
815 						 const struct net_device *dev)
816 {
817 	struct dsa_port *dp;
818 
819 	list_for_each_entry(dp, &dst->ports, list)
820 		if (dsa_port_offloads_bridge_port(dp, dev))
821 			return true;
822 
823 	return false;
824 }
825 
826 /* Returns true if any port of this tree offloads the given bridge */
827 static inline bool
828 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
829 			     const struct net_device *bridge_dev)
830 {
831 	struct dsa_port *dp;
832 
833 	list_for_each_entry(dp, &dst->ports, list)
834 		if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
835 			return true;
836 
837 	return false;
838 }
839 
840 #define dsa_switch_for_each_bridge_member(_dp, _ds, _bdev) \
841 	dsa_switch_for_each_user_port(_dp, _ds) \
842 		if (dsa_port_offloads_bridge_dev(_dp, _bdev))
843 
844 static inline u32
845 dsa_bridge_ports(struct dsa_switch *ds, const struct net_device *bdev)
846 {
847 	struct dsa_port *dp;
848 	u32 mask = 0;
849 
850 	dsa_switch_for_each_bridge_member(dp, ds, bdev)
851 		mask |= BIT(dp->index);
852 
853 	return mask;
854 }
855 
856 static inline bool dsa_port_tree_same(const struct dsa_port *a,
857 				      const struct dsa_port *b)
858 {
859 	return a->ds->dst == b->ds->dst;
860 }
861 
862 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
863 			      bool is_static, void *data);
864 struct dsa_switch_ops {
865 	/*
866 	 * Tagging protocol helpers called for the CPU ports and DSA links.
867 	 * @get_tag_protocol retrieves the initial tagging protocol and is
868 	 * mandatory. Switches which can operate using multiple tagging
869 	 * protocols should implement @change_tag_protocol and report in
870 	 * @get_tag_protocol the tagger in current use.
871 	 */
872 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
873 						  int port,
874 						  enum dsa_tag_protocol mprot);
875 	int	(*change_tag_protocol)(struct dsa_switch *ds,
876 				       enum dsa_tag_protocol proto);
877 	/*
878 	 * Method for switch drivers to connect to the tagging protocol driver
879 	 * in current use. The switch driver can provide handlers for certain
880 	 * types of packets for switch management.
881 	 */
882 	int	(*connect_tag_protocol)(struct dsa_switch *ds,
883 					enum dsa_tag_protocol proto);
884 
885 	int	(*port_change_conduit)(struct dsa_switch *ds, int port,
886 				       struct net_device *conduit,
887 				       struct netlink_ext_ack *extack);
888 
889 	/* Optional switch-wide initialization and destruction methods */
890 	int	(*setup)(struct dsa_switch *ds);
891 	void	(*teardown)(struct dsa_switch *ds);
892 
893 	/* Per-port initialization and destruction methods. Mandatory if the
894 	 * driver registers devlink port regions, optional otherwise.
895 	 */
896 	int	(*port_setup)(struct dsa_switch *ds, int port);
897 	void	(*port_teardown)(struct dsa_switch *ds, int port);
898 
899 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
900 
901 	/*
902 	 * Access to the switch's PHY registers.
903 	 */
904 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
905 	int	(*phy_write)(struct dsa_switch *ds, int port,
906 			     int regnum, u16 val);
907 
908 	/*
909 	 * PHYLINK integration
910 	 */
911 	void	(*phylink_get_caps)(struct dsa_switch *ds, int port,
912 				    struct phylink_config *config);
913 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
914 				       struct phylink_link_state *state);
915 	/*
916 	 * Port statistics counters.
917 	 */
918 	void	(*get_strings)(struct dsa_switch *ds, int port,
919 			       u32 stringset, uint8_t *data);
920 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
921 				     int port, uint64_t *data);
922 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
923 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
924 					 int port, uint64_t *data);
925 	void	(*get_eth_phy_stats)(struct dsa_switch *ds, int port,
926 				     struct ethtool_eth_phy_stats *phy_stats);
927 	void	(*get_eth_mac_stats)(struct dsa_switch *ds, int port,
928 				     struct ethtool_eth_mac_stats *mac_stats);
929 	void	(*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
930 				      struct ethtool_eth_ctrl_stats *ctrl_stats);
931 	void	(*get_rmon_stats)(struct dsa_switch *ds, int port,
932 				  struct ethtool_rmon_stats *rmon_stats,
933 				  const struct ethtool_rmon_hist_range **ranges);
934 	void	(*get_ts_stats)(struct dsa_switch *ds, int port,
935 				struct ethtool_ts_stats *ts_stats);
936 	void	(*get_stats64)(struct dsa_switch *ds, int port,
937 				   struct rtnl_link_stats64 *s);
938 	void	(*get_pause_stats)(struct dsa_switch *ds, int port,
939 				   struct ethtool_pause_stats *pause_stats);
940 	void	(*self_test)(struct dsa_switch *ds, int port,
941 			     struct ethtool_test *etest, u64 *data);
942 
943 	/*
944 	 * ethtool Wake-on-LAN
945 	 */
946 	void	(*get_wol)(struct dsa_switch *ds, int port,
947 			   struct ethtool_wolinfo *w);
948 	int	(*set_wol)(struct dsa_switch *ds, int port,
949 			   struct ethtool_wolinfo *w);
950 
951 	/*
952 	 * ethtool timestamp info
953 	 */
954 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
955 			       struct kernel_ethtool_ts_info *ts);
956 
957 	/*
958 	 * ethtool MAC merge layer
959 	 */
960 	int	(*get_mm)(struct dsa_switch *ds, int port,
961 			  struct ethtool_mm_state *state);
962 	int	(*set_mm)(struct dsa_switch *ds, int port,
963 			  struct ethtool_mm_cfg *cfg,
964 			  struct netlink_ext_ack *extack);
965 	void	(*get_mm_stats)(struct dsa_switch *ds, int port,
966 				struct ethtool_mm_stats *stats);
967 
968 	/*
969 	 * DCB ops
970 	 */
971 	int	(*port_get_default_prio)(struct dsa_switch *ds, int port);
972 	int	(*port_set_default_prio)(struct dsa_switch *ds, int port,
973 					 u8 prio);
974 	int	(*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
975 	int	(*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
976 				      u8 prio);
977 	int	(*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
978 				      u8 prio);
979 	int	(*port_set_apptrust)(struct dsa_switch *ds, int port,
980 				     const u8 *sel, int nsel);
981 	int	(*port_get_apptrust)(struct dsa_switch *ds, int port, u8 *sel,
982 				     int *nsel);
983 
984 	/*
985 	 * Suspend and resume
986 	 */
987 	int	(*suspend)(struct dsa_switch *ds);
988 	int	(*resume)(struct dsa_switch *ds);
989 
990 	/*
991 	 * Port enable/disable
992 	 */
993 	int	(*port_enable)(struct dsa_switch *ds, int port,
994 			       struct phy_device *phy);
995 	void	(*port_disable)(struct dsa_switch *ds, int port);
996 
997 
998 	/*
999 	 * Notification for MAC address changes on user ports. Drivers can
1000 	 * currently only veto operations. They should not use the method to
1001 	 * program the hardware, since the operation is not rolled back in case
1002 	 * of other errors.
1003 	 */
1004 	int	(*port_set_mac_address)(struct dsa_switch *ds, int port,
1005 					const unsigned char *addr);
1006 
1007 	/*
1008 	 * Compatibility between device trees defining multiple CPU ports and
1009 	 * drivers which are not OK to use by default the numerically smallest
1010 	 * CPU port of a switch for its local ports. This can return NULL,
1011 	 * meaning "don't know/don't care".
1012 	 */
1013 	struct dsa_port *(*preferred_default_local_cpu_port)(struct dsa_switch *ds);
1014 
1015 	/*
1016 	 * Port's MAC EEE settings
1017 	 */
1018 	bool	(*support_eee)(struct dsa_switch *ds, int port);
1019 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
1020 			       struct ethtool_keee *e);
1021 
1022 	/* EEPROM access */
1023 	int	(*get_eeprom_len)(struct dsa_switch *ds);
1024 	int	(*get_eeprom)(struct dsa_switch *ds,
1025 			      struct ethtool_eeprom *eeprom, u8 *data);
1026 	int	(*set_eeprom)(struct dsa_switch *ds,
1027 			      struct ethtool_eeprom *eeprom, u8 *data);
1028 
1029 	/*
1030 	 * Register access.
1031 	 */
1032 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
1033 	void	(*get_regs)(struct dsa_switch *ds, int port,
1034 			    struct ethtool_regs *regs, void *p);
1035 
1036 	/*
1037 	 * Upper device tracking.
1038 	 */
1039 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
1040 				       struct netdev_notifier_changeupper_info *info);
1041 
1042 	/*
1043 	 * Bridge integration
1044 	 */
1045 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
1046 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
1047 				    struct dsa_bridge bridge,
1048 				    bool *tx_fwd_offload,
1049 				    struct netlink_ext_ack *extack);
1050 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
1051 				     struct dsa_bridge bridge);
1052 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
1053 				      u8 state);
1054 	int	(*port_mst_state_set)(struct dsa_switch *ds, int port,
1055 				      const struct switchdev_mst_state *state);
1056 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
1057 	int	(*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
1058 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
1059 					 struct switchdev_brport_flags flags,
1060 					 struct netlink_ext_ack *extack);
1061 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
1062 				     struct switchdev_brport_flags flags,
1063 				     struct netlink_ext_ack *extack);
1064 	void	(*port_set_host_flood)(struct dsa_switch *ds, int port,
1065 				       bool uc, bool mc);
1066 
1067 	/*
1068 	 * VLAN support
1069 	 */
1070 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
1071 				       bool vlan_filtering,
1072 				       struct netlink_ext_ack *extack);
1073 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
1074 				 const struct switchdev_obj_port_vlan *vlan,
1075 				 struct netlink_ext_ack *extack);
1076 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
1077 				 const struct switchdev_obj_port_vlan *vlan);
1078 	int	(*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge,
1079 				 const struct switchdev_vlan_msti *msti);
1080 
1081 	/*
1082 	 * Forwarding database
1083 	 */
1084 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
1085 				const unsigned char *addr, u16 vid,
1086 				struct dsa_db db);
1087 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
1088 				const unsigned char *addr, u16 vid,
1089 				struct dsa_db db);
1090 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
1091 				 dsa_fdb_dump_cb_t *cb, void *data);
1092 	int	(*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
1093 			       const unsigned char *addr, u16 vid,
1094 			       struct dsa_db db);
1095 	int	(*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
1096 			       const unsigned char *addr, u16 vid,
1097 			       struct dsa_db db);
1098 
1099 	/*
1100 	 * Multicast database
1101 	 */
1102 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
1103 				const struct switchdev_obj_port_mdb *mdb,
1104 				struct dsa_db db);
1105 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
1106 				const struct switchdev_obj_port_mdb *mdb,
1107 				struct dsa_db db);
1108 	/*
1109 	 * RXNFC
1110 	 */
1111 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
1112 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
1113 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
1114 			     struct ethtool_rxnfc *nfc);
1115 
1116 	/*
1117 	 * TC integration
1118 	 */
1119 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
1120 				  struct flow_cls_offload *cls, bool ingress);
1121 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
1122 				  struct flow_cls_offload *cls, bool ingress);
1123 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
1124 				    struct flow_cls_offload *cls, bool ingress);
1125 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
1126 				   struct dsa_mall_mirror_tc_entry *mirror,
1127 				   bool ingress, struct netlink_ext_ack *extack);
1128 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
1129 				   struct dsa_mall_mirror_tc_entry *mirror);
1130 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
1131 				    const struct flow_action_police *policer,
1132 				    struct netlink_ext_ack *extack);
1133 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
1134 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
1135 				 enum tc_setup_type type, void *type_data);
1136 
1137 	/*
1138 	 * Cross-chip operations
1139 	 */
1140 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
1141 					 int sw_index, int port,
1142 					 struct dsa_bridge bridge,
1143 					 struct netlink_ext_ack *extack);
1144 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
1145 					  int sw_index, int port,
1146 					  struct dsa_bridge bridge);
1147 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
1148 					int port);
1149 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
1150 				      int port, struct dsa_lag lag,
1151 				      struct netdev_lag_upper_info *info,
1152 				      struct netlink_ext_ack *extack);
1153 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
1154 				       int port, struct dsa_lag lag);
1155 
1156 	/*
1157 	 * PTP functionality
1158 	 */
1159 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
1160 				     struct kernel_hwtstamp_config *config);
1161 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
1162 				     struct kernel_hwtstamp_config *config,
1163 				     struct netlink_ext_ack *extack);
1164 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
1165 				 struct sk_buff *skb);
1166 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
1167 				 struct sk_buff *skb, unsigned int type);
1168 
1169 	/* Devlink parameters, etc */
1170 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
1171 				     struct devlink_param_gset_ctx *ctx);
1172 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
1173 				     struct devlink_param_gset_ctx *ctx);
1174 	int	(*devlink_info_get)(struct dsa_switch *ds,
1175 				    struct devlink_info_req *req,
1176 				    struct netlink_ext_ack *extack);
1177 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
1178 				       unsigned int sb_index, u16 pool_index,
1179 				       struct devlink_sb_pool_info *pool_info);
1180 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
1181 				       u16 pool_index, u32 size,
1182 				       enum devlink_sb_threshold_type threshold_type,
1183 				       struct netlink_ext_ack *extack);
1184 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
1185 					    unsigned int sb_index, u16 pool_index,
1186 					    u32 *p_threshold);
1187 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
1188 					    unsigned int sb_index, u16 pool_index,
1189 					    u32 threshold,
1190 					    struct netlink_ext_ack *extack);
1191 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
1192 					       unsigned int sb_index, u16 tc_index,
1193 					       enum devlink_sb_pool_type pool_type,
1194 					       u16 *p_pool_index, u32 *p_threshold);
1195 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
1196 					       unsigned int sb_index, u16 tc_index,
1197 					       enum devlink_sb_pool_type pool_type,
1198 					       u16 pool_index, u32 threshold,
1199 					       struct netlink_ext_ack *extack);
1200 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
1201 					   unsigned int sb_index);
1202 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
1203 					    unsigned int sb_index);
1204 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
1205 						unsigned int sb_index, u16 pool_index,
1206 						u32 *p_cur, u32 *p_max);
1207 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
1208 						   unsigned int sb_index, u16 tc_index,
1209 						   enum devlink_sb_pool_type pool_type,
1210 						   u32 *p_cur, u32 *p_max);
1211 
1212 	/*
1213 	 * MTU change functionality. Switches can also adjust their MRU through
1214 	 * this method. By MTU, one understands the SDU (L2 payload) length.
1215 	 * If the switch needs to account for the DSA tag on the CPU port, this
1216 	 * method needs to do so privately.
1217 	 */
1218 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
1219 				   int new_mtu);
1220 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
1221 
1222 	/*
1223 	 * LAG integration
1224 	 */
1225 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
1226 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
1227 				 struct dsa_lag lag,
1228 				 struct netdev_lag_upper_info *info,
1229 				 struct netlink_ext_ack *extack);
1230 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
1231 				  struct dsa_lag lag);
1232 
1233 	/*
1234 	 * HSR integration
1235 	 */
1236 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
1237 				 struct net_device *hsr,
1238 				 struct netlink_ext_ack *extack);
1239 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
1240 				  struct net_device *hsr);
1241 
1242 	/*
1243 	 * MRP integration
1244 	 */
1245 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
1246 				const struct switchdev_obj_mrp *mrp);
1247 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
1248 				const struct switchdev_obj_mrp *mrp);
1249 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1250 					  const struct switchdev_obj_ring_role_mrp *mrp);
1251 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1252 					  const struct switchdev_obj_ring_role_mrp *mrp);
1253 
1254 	/*
1255 	 * tag_8021q operations
1256 	 */
1257 	int	(*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
1258 				      u16 flags);
1259 	int	(*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1260 
1261 	/*
1262 	 * DSA conduit tracking operations
1263 	 */
1264 	void	(*conduit_state_change)(struct dsa_switch *ds,
1265 					const struct net_device *conduit,
1266 					bool operational);
1267 };
1268 
1269 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
1270 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
1271 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
1272 
1273 int dsa_devlink_param_get(struct devlink *dl, u32 id,
1274 			  struct devlink_param_gset_ctx *ctx,
1275 			  struct netlink_ext_ack *extack);
1276 int dsa_devlink_param_set(struct devlink *dl, u32 id,
1277 			  struct devlink_param_gset_ctx *ctx,
1278 			  struct netlink_ext_ack *extack);
1279 int dsa_devlink_params_register(struct dsa_switch *ds,
1280 				const struct devlink_param *params,
1281 				size_t params_count);
1282 void dsa_devlink_params_unregister(struct dsa_switch *ds,
1283 				   const struct devlink_param *params,
1284 				   size_t params_count);
1285 int dsa_devlink_resource_register(struct dsa_switch *ds,
1286 				  const char *resource_name,
1287 				  u64 resource_size,
1288 				  u64 resource_id,
1289 				  u64 parent_resource_id,
1290 				  const struct devlink_resource_size_params *size_params);
1291 
1292 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
1293 
1294 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
1295 					   u64 resource_id,
1296 					   devlink_resource_occ_get_t *occ_get,
1297 					   void *occ_get_priv);
1298 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
1299 					     u64 resource_id);
1300 struct devlink_region *
1301 dsa_devlink_region_create(struct dsa_switch *ds,
1302 			  const struct devlink_region_ops *ops,
1303 			  u32 region_max_snapshots, u64 region_size);
1304 struct devlink_region *
1305 dsa_devlink_port_region_create(struct dsa_switch *ds,
1306 			       int port,
1307 			       const struct devlink_port_region_ops *ops,
1308 			       u32 region_max_snapshots, u64 region_size);
1309 void dsa_devlink_region_destroy(struct devlink_region *region);
1310 
1311 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
1312 
1313 struct dsa_devlink_priv {
1314 	struct dsa_switch *ds;
1315 };
1316 
1317 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1318 {
1319 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1320 
1321 	return dl_priv->ds;
1322 }
1323 
1324 static inline
1325 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
1326 {
1327 	struct devlink *dl = port->devlink;
1328 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1329 
1330 	return dl_priv->ds;
1331 }
1332 
1333 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1334 {
1335 	return port->index;
1336 }
1337 
1338 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
1339 				 const unsigned char *addr, u16 vid,
1340 				 struct dsa_db db);
1341 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
1342 				 const struct switchdev_obj_port_mdb *mdb,
1343 				 struct dsa_db db);
1344 
1345 int dsa_port_simple_hsr_validate(struct dsa_switch *ds, int port,
1346 				 struct net_device *hsr,
1347 				 struct netlink_ext_ack *extack);
1348 int dsa_port_simple_hsr_join(struct dsa_switch *ds, int port,
1349 			     struct net_device *hsr,
1350 			     struct netlink_ext_ack *extack);
1351 int dsa_port_simple_hsr_leave(struct dsa_switch *ds, int port,
1352 			      struct net_device *hsr);
1353 
1354 /* Keep inline for faster access in hot path */
1355 static inline bool netdev_uses_dsa(const struct net_device *dev)
1356 {
1357 #if IS_ENABLED(CONFIG_NET_DSA)
1358 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
1359 #endif
1360 	return false;
1361 }
1362 
1363 /* All DSA tags that push the EtherType to the right (basically all except tail
1364  * tags, which don't break dissection) can be treated the same from the
1365  * perspective of the flow dissector.
1366  *
1367  * We need to return:
1368  *  - offset: the (B - A) difference between:
1369  *    A. the position of the real EtherType and
1370  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1371  *       after the normal EtherType was supposed to be)
1372  *    The offset in bytes is exactly equal to the tagger overhead (and half of
1373  *    that, in __be16 shorts).
1374  *
1375  *  - proto: the value of the real EtherType.
1376  */
1377 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1378 						__be16 *proto, int *offset)
1379 {
1380 #if IS_ENABLED(CONFIG_NET_DSA)
1381 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1382 	int tag_len = ops->needed_headroom;
1383 
1384 	*offset = tag_len;
1385 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1386 #endif
1387 }
1388 
1389 void dsa_unregister_switch(struct dsa_switch *ds);
1390 int dsa_register_switch(struct dsa_switch *ds);
1391 void dsa_switch_shutdown(struct dsa_switch *ds);
1392 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1393 void dsa_flush_workqueue(void);
1394 #ifdef CONFIG_PM_SLEEP
1395 int dsa_switch_suspend(struct dsa_switch *ds);
1396 int dsa_switch_resume(struct dsa_switch *ds);
1397 #else
1398 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1399 {
1400 	return 0;
1401 }
1402 static inline int dsa_switch_resume(struct dsa_switch *ds)
1403 {
1404 	return 0;
1405 }
1406 #endif /* CONFIG_PM_SLEEP */
1407 
1408 #if IS_ENABLED(CONFIG_NET_DSA)
1409 bool dsa_user_dev_check(const struct net_device *dev);
1410 #else
1411 static inline bool dsa_user_dev_check(const struct net_device *dev)
1412 {
1413 	return false;
1414 }
1415 #endif
1416 
1417 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1418 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1419 bool dsa_supports_eee(struct dsa_switch *ds, int port);
1420 
1421 #endif
1422