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