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