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