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