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