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