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