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