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