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