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