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