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