xref: /openbmc/linux/include/net/dsa.h (revision b3ae2d350ddfd5aeb04de29c333d6639aeb1dae2)
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 	/* MAC PCS does not provide link state change interrupt, and requires
395 	 * polling. Flag passed on to PHYLINK.
396 	 */
397 	u32			pcs_poll:1;
398 
399 	/* For switches that only have the MRU configurable. To ensure the
400 	 * configured MTU is not exceeded, normalization of MRU on all bridged
401 	 * interfaces is needed.
402 	 */
403 	u32			mtu_enforcement_ingress:1;
404 
405 	/* Listener for switch fabric events */
406 	struct notifier_block	nb;
407 
408 	/*
409 	 * Give the switch driver somewhere to hang its private data
410 	 * structure.
411 	 */
412 	void *priv;
413 
414 	void *tagger_data;
415 
416 	/*
417 	 * Configuration data for this switch.
418 	 */
419 	struct dsa_chip_data	*cd;
420 
421 	/*
422 	 * The switch operations.
423 	 */
424 	const struct dsa_switch_ops	*ops;
425 
426 	/*
427 	 * Slave mii_bus and devices for the individual ports.
428 	 */
429 	u32			phys_mii_mask;
430 	struct mii_bus		*slave_mii_bus;
431 
432 	/* Ageing Time limits in msecs */
433 	unsigned int ageing_time_min;
434 	unsigned int ageing_time_max;
435 
436 	/* Storage for drivers using tag_8021q */
437 	struct dsa_8021q_context *tag_8021q_ctx;
438 
439 	/* devlink used to represent this switch device */
440 	struct devlink		*devlink;
441 
442 	/* Number of switch port queues */
443 	unsigned int		num_tx_queues;
444 
445 	/* Drivers that benefit from having an ID associated with each
446 	 * offloaded LAG should set this to the maximum number of
447 	 * supported IDs. DSA will then maintain a mapping of _at
448 	 * least_ these many IDs, accessible to drivers via
449 	 * dsa_lag_id().
450 	 */
451 	unsigned int		num_lag_ids;
452 
453 	/* Drivers that support bridge forwarding offload or FDB isolation
454 	 * should set this to the maximum number of bridges spanning the same
455 	 * switch tree (or all trees, in the case of cross-tree bridging
456 	 * support) that can be offloaded.
457 	 */
458 	unsigned int		max_num_bridges;
459 
460 	unsigned int		num_ports;
461 };
462 
463 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
464 {
465 	struct dsa_switch_tree *dst = ds->dst;
466 	struct dsa_port *dp;
467 
468 	list_for_each_entry(dp, &dst->ports, list)
469 		if (dp->ds == ds && dp->index == p)
470 			return dp;
471 
472 	return NULL;
473 }
474 
475 static inline bool dsa_port_is_dsa(struct dsa_port *port)
476 {
477 	return port->type == DSA_PORT_TYPE_DSA;
478 }
479 
480 static inline bool dsa_port_is_cpu(struct dsa_port *port)
481 {
482 	return port->type == DSA_PORT_TYPE_CPU;
483 }
484 
485 static inline bool dsa_port_is_user(struct dsa_port *dp)
486 {
487 	return dp->type == DSA_PORT_TYPE_USER;
488 }
489 
490 static inline bool dsa_port_is_unused(struct dsa_port *dp)
491 {
492 	return dp->type == DSA_PORT_TYPE_UNUSED;
493 }
494 
495 static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
496 {
497 	return dsa_port_is_cpu(dp) && dp->master_admin_up &&
498 	       dp->master_oper_up;
499 }
500 
501 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
502 {
503 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
504 }
505 
506 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
507 {
508 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
509 }
510 
511 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
512 {
513 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
514 }
515 
516 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
517 {
518 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
519 }
520 
521 #define dsa_tree_for_each_user_port(_dp, _dst) \
522 	list_for_each_entry((_dp), &(_dst)->ports, list) \
523 		if (dsa_port_is_user((_dp)))
524 
525 #define dsa_switch_for_each_port(_dp, _ds) \
526 	list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
527 		if ((_dp)->ds == (_ds))
528 
529 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
530 	list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
531 		if ((_dp)->ds == (_ds))
532 
533 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
534 	list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
535 		if ((_dp)->ds == (_ds))
536 
537 #define dsa_switch_for_each_available_port(_dp, _ds) \
538 	dsa_switch_for_each_port((_dp), (_ds)) \
539 		if (!dsa_port_is_unused((_dp)))
540 
541 #define dsa_switch_for_each_user_port(_dp, _ds) \
542 	dsa_switch_for_each_port((_dp), (_ds)) \
543 		if (dsa_port_is_user((_dp)))
544 
545 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
546 	dsa_switch_for_each_port((_dp), (_ds)) \
547 		if (dsa_port_is_cpu((_dp)))
548 
549 static inline u32 dsa_user_ports(struct dsa_switch *ds)
550 {
551 	struct dsa_port *dp;
552 	u32 mask = 0;
553 
554 	dsa_switch_for_each_user_port(dp, ds)
555 		mask |= BIT(dp->index);
556 
557 	return mask;
558 }
559 
560 /* Return the local port used to reach an arbitrary switch device */
561 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
562 {
563 	struct dsa_switch_tree *dst = ds->dst;
564 	struct dsa_link *dl;
565 
566 	list_for_each_entry(dl, &dst->rtable, list)
567 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
568 			return dl->dp->index;
569 
570 	return ds->num_ports;
571 }
572 
573 /* Return the local port used to reach an arbitrary switch port */
574 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
575 					    int port)
576 {
577 	if (device == ds->index)
578 		return port;
579 	else
580 		return dsa_routing_port(ds, device);
581 }
582 
583 /* Return the local port used to reach the dedicated CPU port */
584 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
585 {
586 	const struct dsa_port *dp = dsa_to_port(ds, port);
587 	const struct dsa_port *cpu_dp = dp->cpu_dp;
588 
589 	if (!cpu_dp)
590 		return port;
591 
592 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
593 }
594 
595 /* Return true if this is the local port used to reach the CPU port */
596 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
597 {
598 	if (dsa_is_unused_port(ds, port))
599 		return false;
600 
601 	return port == dsa_upstream_port(ds, port);
602 }
603 
604 /* Return true if this is a DSA port leading away from the CPU */
605 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
606 {
607 	return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
608 }
609 
610 /* Return the local port used to reach the CPU port */
611 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
612 {
613 	struct dsa_port *dp;
614 
615 	dsa_switch_for_each_available_port(dp, ds) {
616 		return dsa_upstream_port(ds, dp->index);
617 	}
618 
619 	return ds->num_ports;
620 }
621 
622 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
623  * that the routing port from @downstream_ds to @upstream_ds is also the port
624  * which @downstream_ds uses to reach its dedicated CPU.
625  */
626 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
627 					     struct dsa_switch *downstream_ds)
628 {
629 	int routing_port;
630 
631 	if (upstream_ds == downstream_ds)
632 		return true;
633 
634 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
635 
636 	return dsa_is_upstream_port(downstream_ds, routing_port);
637 }
638 
639 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
640 {
641 	const struct dsa_switch *ds = dp->ds;
642 
643 	if (ds->vlan_filtering_is_global)
644 		return ds->vlan_filtering;
645 	else
646 		return dp->vlan_filtering;
647 }
648 
649 static inline
650 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
651 {
652 	if (!dp->bridge)
653 		return NULL;
654 
655 	if (dp->lag_dev)
656 		return dp->lag_dev;
657 	else if (dp->hsr_dev)
658 		return dp->hsr_dev;
659 
660 	return dp->slave;
661 }
662 
663 static inline struct net_device *
664 dsa_port_bridge_dev_get(const struct dsa_port *dp)
665 {
666 	return dp->bridge ? dp->bridge->dev : NULL;
667 }
668 
669 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
670 {
671 	return dp->bridge ? dp->bridge->num : 0;
672 }
673 
674 static inline bool dsa_port_bridge_same(const struct dsa_port *a,
675 					const struct dsa_port *b)
676 {
677 	struct net_device *br_a = dsa_port_bridge_dev_get(a);
678 	struct net_device *br_b = dsa_port_bridge_dev_get(b);
679 
680 	/* Standalone ports are not in the same bridge with one another */
681 	return (!br_a || !br_b) ? false : (br_a == br_b);
682 }
683 
684 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
685 						 const struct net_device *dev)
686 {
687 	return dsa_port_to_bridge_port(dp) == dev;
688 }
689 
690 static inline bool
691 dsa_port_offloads_bridge_dev(struct dsa_port *dp,
692 			     const struct net_device *bridge_dev)
693 {
694 	/* DSA ports connected to a bridge, and event was emitted
695 	 * for the bridge.
696 	 */
697 	return dsa_port_bridge_dev_get(dp) == bridge_dev;
698 }
699 
700 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
701 					    const struct dsa_bridge *bridge)
702 {
703 	return dsa_port_bridge_dev_get(dp) == bridge->dev;
704 }
705 
706 /* Returns true if any port of this tree offloads the given net_device */
707 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
708 						 const struct net_device *dev)
709 {
710 	struct dsa_port *dp;
711 
712 	list_for_each_entry(dp, &dst->ports, list)
713 		if (dsa_port_offloads_bridge_port(dp, dev))
714 			return true;
715 
716 	return false;
717 }
718 
719 /* Returns true if any port of this tree offloads the given bridge */
720 static inline bool
721 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
722 			     const struct net_device *bridge_dev)
723 {
724 	struct dsa_port *dp;
725 
726 	list_for_each_entry(dp, &dst->ports, list)
727 		if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
728 			return true;
729 
730 	return false;
731 }
732 
733 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
734 			      bool is_static, void *data);
735 struct dsa_switch_ops {
736 	/*
737 	 * Tagging protocol helpers called for the CPU ports and DSA links.
738 	 * @get_tag_protocol retrieves the initial tagging protocol and is
739 	 * mandatory. Switches which can operate using multiple tagging
740 	 * protocols should implement @change_tag_protocol and report in
741 	 * @get_tag_protocol the tagger in current use.
742 	 */
743 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
744 						  int port,
745 						  enum dsa_tag_protocol mprot);
746 	int	(*change_tag_protocol)(struct dsa_switch *ds, int port,
747 				       enum dsa_tag_protocol proto);
748 	/*
749 	 * Method for switch drivers to connect to the tagging protocol driver
750 	 * in current use. The switch driver can provide handlers for certain
751 	 * types of packets for switch management.
752 	 */
753 	int	(*connect_tag_protocol)(struct dsa_switch *ds,
754 					enum dsa_tag_protocol proto);
755 
756 	/* Optional switch-wide initialization and destruction methods */
757 	int	(*setup)(struct dsa_switch *ds);
758 	void	(*teardown)(struct dsa_switch *ds);
759 
760 	/* Per-port initialization and destruction methods. Mandatory if the
761 	 * driver registers devlink port regions, optional otherwise.
762 	 */
763 	int	(*port_setup)(struct dsa_switch *ds, int port);
764 	void	(*port_teardown)(struct dsa_switch *ds, int port);
765 
766 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
767 
768 	/*
769 	 * Access to the switch's PHY registers.
770 	 */
771 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
772 	int	(*phy_write)(struct dsa_switch *ds, int port,
773 			     int regnum, u16 val);
774 
775 	/*
776 	 * Link state adjustment (called from libphy)
777 	 */
778 	void	(*adjust_link)(struct dsa_switch *ds, int port,
779 				struct phy_device *phydev);
780 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
781 				struct fixed_phy_status *st);
782 
783 	/*
784 	 * PHYLINK integration
785 	 */
786 	void	(*phylink_get_caps)(struct dsa_switch *ds, int port,
787 				    struct phylink_config *config);
788 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
789 				    unsigned long *supported,
790 				    struct phylink_link_state *state);
791 	struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
792 						      int port,
793 						      phy_interface_t iface);
794 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
795 					  struct phylink_link_state *state);
796 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
797 				      unsigned int mode,
798 				      const struct phylink_link_state *state);
799 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
800 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
801 					 unsigned int mode,
802 					 phy_interface_t interface);
803 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
804 				       unsigned int mode,
805 				       phy_interface_t interface,
806 				       struct phy_device *phydev,
807 				       int speed, int duplex,
808 				       bool tx_pause, bool rx_pause);
809 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
810 				       struct phylink_link_state *state);
811 	/*
812 	 * Port statistics counters.
813 	 */
814 	void	(*get_strings)(struct dsa_switch *ds, int port,
815 			       u32 stringset, uint8_t *data);
816 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
817 				     int port, uint64_t *data);
818 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
819 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
820 					 int port, uint64_t *data);
821 	void	(*get_eth_phy_stats)(struct dsa_switch *ds, int port,
822 				     struct ethtool_eth_phy_stats *phy_stats);
823 	void	(*get_eth_mac_stats)(struct dsa_switch *ds, int port,
824 				     struct ethtool_eth_mac_stats *mac_stats);
825 	void	(*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
826 				      struct ethtool_eth_ctrl_stats *ctrl_stats);
827 	void	(*get_stats64)(struct dsa_switch *ds, int port,
828 				   struct rtnl_link_stats64 *s);
829 	void	(*self_test)(struct dsa_switch *ds, int port,
830 			     struct ethtool_test *etest, u64 *data);
831 
832 	/*
833 	 * ethtool Wake-on-LAN
834 	 */
835 	void	(*get_wol)(struct dsa_switch *ds, int port,
836 			   struct ethtool_wolinfo *w);
837 	int	(*set_wol)(struct dsa_switch *ds, int port,
838 			   struct ethtool_wolinfo *w);
839 
840 	/*
841 	 * ethtool timestamp info
842 	 */
843 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
844 			       struct ethtool_ts_info *ts);
845 
846 	/*
847 	 * Suspend and resume
848 	 */
849 	int	(*suspend)(struct dsa_switch *ds);
850 	int	(*resume)(struct dsa_switch *ds);
851 
852 	/*
853 	 * Port enable/disable
854 	 */
855 	int	(*port_enable)(struct dsa_switch *ds, int port,
856 			       struct phy_device *phy);
857 	void	(*port_disable)(struct dsa_switch *ds, int port);
858 
859 	/*
860 	 * Port's MAC EEE settings
861 	 */
862 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
863 			       struct ethtool_eee *e);
864 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
865 			       struct ethtool_eee *e);
866 
867 	/* EEPROM access */
868 	int	(*get_eeprom_len)(struct dsa_switch *ds);
869 	int	(*get_eeprom)(struct dsa_switch *ds,
870 			      struct ethtool_eeprom *eeprom, u8 *data);
871 	int	(*set_eeprom)(struct dsa_switch *ds,
872 			      struct ethtool_eeprom *eeprom, u8 *data);
873 
874 	/*
875 	 * Register access.
876 	 */
877 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
878 	void	(*get_regs)(struct dsa_switch *ds, int port,
879 			    struct ethtool_regs *regs, void *p);
880 
881 	/*
882 	 * Upper device tracking.
883 	 */
884 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
885 				       struct netdev_notifier_changeupper_info *info);
886 
887 	/*
888 	 * Bridge integration
889 	 */
890 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
891 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
892 				    struct dsa_bridge bridge,
893 				    bool *tx_fwd_offload);
894 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
895 				     struct dsa_bridge bridge);
896 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
897 				      u8 state);
898 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
899 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
900 					 struct switchdev_brport_flags flags,
901 					 struct netlink_ext_ack *extack);
902 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
903 				     struct switchdev_brport_flags flags,
904 				     struct netlink_ext_ack *extack);
905 
906 	/*
907 	 * VLAN support
908 	 */
909 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
910 				       bool vlan_filtering,
911 				       struct netlink_ext_ack *extack);
912 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
913 				 const struct switchdev_obj_port_vlan *vlan,
914 				 struct netlink_ext_ack *extack);
915 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
916 				 const struct switchdev_obj_port_vlan *vlan);
917 	/*
918 	 * Forwarding database
919 	 */
920 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
921 				const unsigned char *addr, u16 vid);
922 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
923 				const unsigned char *addr, u16 vid);
924 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
925 				 dsa_fdb_dump_cb_t *cb, void *data);
926 
927 	/*
928 	 * Multicast database
929 	 */
930 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
931 				const struct switchdev_obj_port_mdb *mdb);
932 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
933 				const struct switchdev_obj_port_mdb *mdb);
934 	/*
935 	 * RXNFC
936 	 */
937 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
938 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
939 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
940 			     struct ethtool_rxnfc *nfc);
941 
942 	/*
943 	 * TC integration
944 	 */
945 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
946 				  struct flow_cls_offload *cls, bool ingress);
947 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
948 				  struct flow_cls_offload *cls, bool ingress);
949 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
950 				    struct flow_cls_offload *cls, bool ingress);
951 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
952 				   struct dsa_mall_mirror_tc_entry *mirror,
953 				   bool ingress);
954 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
955 				   struct dsa_mall_mirror_tc_entry *mirror);
956 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
957 				    struct dsa_mall_policer_tc_entry *policer);
958 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
959 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
960 				 enum tc_setup_type type, void *type_data);
961 
962 	/*
963 	 * Cross-chip operations
964 	 */
965 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
966 					 int sw_index, int port,
967 					 struct dsa_bridge bridge);
968 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
969 					  int sw_index, int port,
970 					  struct dsa_bridge bridge);
971 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
972 					int port);
973 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
974 				      int port, struct net_device *lag,
975 				      struct netdev_lag_upper_info *info);
976 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
977 				       int port, struct net_device *lag);
978 
979 	/*
980 	 * PTP functionality
981 	 */
982 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
983 				     struct ifreq *ifr);
984 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
985 				     struct ifreq *ifr);
986 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
987 				 struct sk_buff *skb);
988 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
989 				 struct sk_buff *skb, unsigned int type);
990 
991 	/* Devlink parameters, etc */
992 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
993 				     struct devlink_param_gset_ctx *ctx);
994 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
995 				     struct devlink_param_gset_ctx *ctx);
996 	int	(*devlink_info_get)(struct dsa_switch *ds,
997 				    struct devlink_info_req *req,
998 				    struct netlink_ext_ack *extack);
999 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
1000 				       unsigned int sb_index, u16 pool_index,
1001 				       struct devlink_sb_pool_info *pool_info);
1002 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
1003 				       u16 pool_index, u32 size,
1004 				       enum devlink_sb_threshold_type threshold_type,
1005 				       struct netlink_ext_ack *extack);
1006 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
1007 					    unsigned int sb_index, u16 pool_index,
1008 					    u32 *p_threshold);
1009 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
1010 					    unsigned int sb_index, u16 pool_index,
1011 					    u32 threshold,
1012 					    struct netlink_ext_ack *extack);
1013 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
1014 					       unsigned int sb_index, u16 tc_index,
1015 					       enum devlink_sb_pool_type pool_type,
1016 					       u16 *p_pool_index, u32 *p_threshold);
1017 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
1018 					       unsigned int sb_index, u16 tc_index,
1019 					       enum devlink_sb_pool_type pool_type,
1020 					       u16 pool_index, u32 threshold,
1021 					       struct netlink_ext_ack *extack);
1022 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
1023 					   unsigned int sb_index);
1024 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
1025 					    unsigned int sb_index);
1026 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
1027 						unsigned int sb_index, u16 pool_index,
1028 						u32 *p_cur, u32 *p_max);
1029 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
1030 						   unsigned int sb_index, u16 tc_index,
1031 						   enum devlink_sb_pool_type pool_type,
1032 						   u32 *p_cur, u32 *p_max);
1033 
1034 	/*
1035 	 * MTU change functionality. Switches can also adjust their MRU through
1036 	 * this method. By MTU, one understands the SDU (L2 payload) length.
1037 	 * If the switch needs to account for the DSA tag on the CPU port, this
1038 	 * method needs to do so privately.
1039 	 */
1040 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
1041 				   int new_mtu);
1042 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
1043 
1044 	/*
1045 	 * LAG integration
1046 	 */
1047 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
1048 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
1049 				 struct net_device *lag,
1050 				 struct netdev_lag_upper_info *info);
1051 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
1052 				  struct net_device *lag);
1053 
1054 	/*
1055 	 * HSR integration
1056 	 */
1057 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
1058 				 struct net_device *hsr);
1059 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
1060 				  struct net_device *hsr);
1061 
1062 	/*
1063 	 * MRP integration
1064 	 */
1065 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
1066 				const struct switchdev_obj_mrp *mrp);
1067 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
1068 				const struct switchdev_obj_mrp *mrp);
1069 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1070 					  const struct switchdev_obj_ring_role_mrp *mrp);
1071 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1072 					  const struct switchdev_obj_ring_role_mrp *mrp);
1073 
1074 	/*
1075 	 * tag_8021q operations
1076 	 */
1077 	int	(*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
1078 				      u16 flags);
1079 	int	(*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1080 
1081 	/*
1082 	 * DSA master tracking operations
1083 	 */
1084 	void	(*master_state_change)(struct dsa_switch *ds,
1085 				       const struct net_device *master,
1086 				       bool operational);
1087 };
1088 
1089 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
1090 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
1091 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
1092 
1093 int dsa_devlink_param_get(struct devlink *dl, u32 id,
1094 			  struct devlink_param_gset_ctx *ctx);
1095 int dsa_devlink_param_set(struct devlink *dl, u32 id,
1096 			  struct devlink_param_gset_ctx *ctx);
1097 int dsa_devlink_params_register(struct dsa_switch *ds,
1098 				const struct devlink_param *params,
1099 				size_t params_count);
1100 void dsa_devlink_params_unregister(struct dsa_switch *ds,
1101 				   const struct devlink_param *params,
1102 				   size_t params_count);
1103 int dsa_devlink_resource_register(struct dsa_switch *ds,
1104 				  const char *resource_name,
1105 				  u64 resource_size,
1106 				  u64 resource_id,
1107 				  u64 parent_resource_id,
1108 				  const struct devlink_resource_size_params *size_params);
1109 
1110 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
1111 
1112 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
1113 					   u64 resource_id,
1114 					   devlink_resource_occ_get_t *occ_get,
1115 					   void *occ_get_priv);
1116 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
1117 					     u64 resource_id);
1118 struct devlink_region *
1119 dsa_devlink_region_create(struct dsa_switch *ds,
1120 			  const struct devlink_region_ops *ops,
1121 			  u32 region_max_snapshots, u64 region_size);
1122 struct devlink_region *
1123 dsa_devlink_port_region_create(struct dsa_switch *ds,
1124 			       int port,
1125 			       const struct devlink_port_region_ops *ops,
1126 			       u32 region_max_snapshots, u64 region_size);
1127 void dsa_devlink_region_destroy(struct devlink_region *region);
1128 
1129 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
1130 
1131 struct dsa_devlink_priv {
1132 	struct dsa_switch *ds;
1133 };
1134 
1135 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1136 {
1137 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1138 
1139 	return dl_priv->ds;
1140 }
1141 
1142 static inline
1143 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
1144 {
1145 	struct devlink *dl = port->devlink;
1146 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1147 
1148 	return dl_priv->ds;
1149 }
1150 
1151 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1152 {
1153 	return port->index;
1154 }
1155 
1156 struct dsa_switch_driver {
1157 	struct list_head	list;
1158 	const struct dsa_switch_ops *ops;
1159 };
1160 
1161 struct net_device *dsa_dev_to_net_device(struct device *dev);
1162 
1163 /* Keep inline for faster access in hot path */
1164 static inline bool netdev_uses_dsa(const struct net_device *dev)
1165 {
1166 #if IS_ENABLED(CONFIG_NET_DSA)
1167 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
1168 #endif
1169 	return false;
1170 }
1171 
1172 /* All DSA tags that push the EtherType to the right (basically all except tail
1173  * tags, which don't break dissection) can be treated the same from the
1174  * perspective of the flow dissector.
1175  *
1176  * We need to return:
1177  *  - offset: the (B - A) difference between:
1178  *    A. the position of the real EtherType and
1179  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1180  *       after the normal EtherType was supposed to be)
1181  *    The offset in bytes is exactly equal to the tagger overhead (and half of
1182  *    that, in __be16 shorts).
1183  *
1184  *  - proto: the value of the real EtherType.
1185  */
1186 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1187 						__be16 *proto, int *offset)
1188 {
1189 #if IS_ENABLED(CONFIG_NET_DSA)
1190 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1191 	int tag_len = ops->needed_headroom;
1192 
1193 	*offset = tag_len;
1194 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1195 #endif
1196 }
1197 
1198 #if IS_ENABLED(CONFIG_NET_DSA)
1199 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1200 {
1201 	int err = -EOPNOTSUPP;
1202 
1203 	if (!dev->dsa_ptr)
1204 		return err;
1205 
1206 	if (!dev->dsa_ptr->netdev_ops)
1207 		return err;
1208 
1209 	return 0;
1210 }
1211 
1212 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1213 				    int cmd)
1214 {
1215 	const struct dsa_netdevice_ops *ops;
1216 	int err;
1217 
1218 	err = __dsa_netdevice_ops_check(dev);
1219 	if (err)
1220 		return err;
1221 
1222 	ops = dev->dsa_ptr->netdev_ops;
1223 
1224 	return ops->ndo_eth_ioctl(dev, ifr, cmd);
1225 }
1226 #else
1227 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1228 				    int cmd)
1229 {
1230 	return -EOPNOTSUPP;
1231 }
1232 #endif
1233 
1234 void dsa_unregister_switch(struct dsa_switch *ds);
1235 int dsa_register_switch(struct dsa_switch *ds);
1236 void dsa_switch_shutdown(struct dsa_switch *ds);
1237 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1238 void dsa_flush_workqueue(void);
1239 #ifdef CONFIG_PM_SLEEP
1240 int dsa_switch_suspend(struct dsa_switch *ds);
1241 int dsa_switch_resume(struct dsa_switch *ds);
1242 #else
1243 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1244 {
1245 	return 0;
1246 }
1247 static inline int dsa_switch_resume(struct dsa_switch *ds)
1248 {
1249 	return 0;
1250 }
1251 #endif /* CONFIG_PM_SLEEP */
1252 
1253 #if IS_ENABLED(CONFIG_NET_DSA)
1254 bool dsa_slave_dev_check(const struct net_device *dev);
1255 #else
1256 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1257 {
1258 	return false;
1259 }
1260 #endif
1261 
1262 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1263 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1264 
1265 struct dsa_tag_driver {
1266 	const struct dsa_device_ops *ops;
1267 	struct list_head list;
1268 	struct module *owner;
1269 };
1270 
1271 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1272 			      unsigned int count,
1273 			      struct module *owner);
1274 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1275 				unsigned int count);
1276 
1277 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
1278 static int __init dsa_tag_driver_module_init(void)			\
1279 {									\
1280 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
1281 				 THIS_MODULE);				\
1282 	return 0;							\
1283 }									\
1284 module_init(dsa_tag_driver_module_init);				\
1285 									\
1286 static void __exit dsa_tag_driver_module_exit(void)			\
1287 {									\
1288 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
1289 }									\
1290 module_exit(dsa_tag_driver_module_exit)
1291 
1292 /**
1293  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1294  * drivers
1295  * @__ops_array: Array of tag driver structures
1296  *
1297  * Helper macro for DSA tag drivers which do not do anything special
1298  * in module init/exit. Each module may only use this macro once, and
1299  * calling it replaces module_init() and module_exit().
1300  */
1301 #define module_dsa_tag_drivers(__ops_array)				\
1302 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1303 
1304 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1305 
1306 /* Create a static structure we can build a linked list of dsa_tag
1307  * drivers
1308  */
1309 #define DSA_TAG_DRIVER(__ops)						\
1310 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1311 	.ops = &__ops,							\
1312 }
1313 
1314 /**
1315  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1316  * driver
1317  * @__ops: Single tag driver structures
1318  *
1319  * Helper macro for DSA tag drivers which do not do anything special
1320  * in module init/exit. Each module may only use this macro once, and
1321  * calling it replaces module_init() and module_exit().
1322  */
1323 #define module_dsa_tag_driver(__ops)					\
1324 DSA_TAG_DRIVER(__ops);							\
1325 									\
1326 static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1327 	&DSA_TAG_DRIVER_NAME(__ops)					\
1328 };									\
1329 module_dsa_tag_drivers(dsa_tag_driver_array)
1330 #endif
1331 
1332