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