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