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