xref: /linux/include/net/dsa.h (revision bb5b94f5bbe75470912b70fb08880fc5273aa62d)
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 
55 enum dsa_tag_protocol {
56 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
57 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
58 	DSA_TAG_PROTO_BRCM_LEGACY	= DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
59 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
60 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
61 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
62 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
63 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
64 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
65 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
66 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
67 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
68 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
69 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
70 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
71 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
72 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
73 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
74 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
75 	DSA_TAG_PROTO_HELLCREEK		= DSA_TAG_PROTO_HELLCREEK_VALUE,
76 	DSA_TAG_PROTO_XRS700X		= DSA_TAG_PROTO_XRS700X_VALUE,
77 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
78 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
79 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
80 };
81 
82 struct packet_type;
83 struct dsa_switch;
84 
85 struct dsa_device_ops {
86 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
87 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
88 			       struct packet_type *pt);
89 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
90 			     int *offset);
91 	/* Used to determine which traffic should match the DSA filter in
92 	 * eth_type_trans, and which, if any, should bypass it and be processed
93 	 * as regular on the master net device.
94 	 */
95 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
96 	unsigned int needed_headroom;
97 	unsigned int needed_tailroom;
98 	const char *name;
99 	enum dsa_tag_protocol proto;
100 	/* Some tagging protocols either mangle or shift the destination MAC
101 	 * address, in which case the DSA master would drop packets on ingress
102 	 * if what it understands out of the destination MAC address is not in
103 	 * its RX filter.
104 	 */
105 	bool promisc_on_master;
106 };
107 
108 /* This structure defines the control interfaces that are overlayed by the
109  * DSA layer on top of the DSA CPU/management net_device instance. This is
110  * used by the core net_device layer while calling various net_device_ops
111  * function pointers.
112  */
113 struct dsa_netdevice_ops {
114 	int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr,
115 			    int cmd);
116 };
117 
118 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
119 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
120 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
121 
122 struct dsa_switch_tree {
123 	struct list_head	list;
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 	/* Has this tree been applied to the hardware? */
135 	bool setup;
136 
137 	/* Tagging protocol operations */
138 	const struct dsa_device_ops *tag_ops;
139 
140 	/* Default tagging protocol preferred by the switches in this
141 	 * tree.
142 	 */
143 	enum dsa_tag_protocol default_proto;
144 
145 	/*
146 	 * Configuration data for the platform device that owns
147 	 * this dsa switch tree instance.
148 	 */
149 	struct dsa_platform_data	*pd;
150 
151 	/* List of switch ports */
152 	struct list_head ports;
153 
154 	/* List of DSA links composing the routing table */
155 	struct list_head rtable;
156 
157 	/* Maps offloaded LAG netdevs to a zero-based linear ID for
158 	 * drivers that need it.
159 	 */
160 	struct net_device **lags;
161 	unsigned int lags_len;
162 };
163 
164 #define dsa_lags_foreach_id(_id, _dst)				\
165 	for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++)	\
166 		if ((_dst)->lags[(_id)])
167 
168 #define dsa_lag_foreach_port(_dp, _dst, _lag)			\
169 	list_for_each_entry((_dp), &(_dst)->ports, list)	\
170 		if ((_dp)->lag_dev == (_lag))
171 
172 #define dsa_hsr_foreach_port(_dp, _ds, _hsr)			\
173 	list_for_each_entry((_dp), &(_ds)->dst->ports, list)	\
174 		if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
175 
176 static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
177 					     unsigned int id)
178 {
179 	return dst->lags[id];
180 }
181 
182 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
183 			     struct net_device *lag)
184 {
185 	unsigned int id;
186 
187 	dsa_lags_foreach_id(id, dst) {
188 		if (dsa_lag_dev(dst, id) == lag)
189 			return id;
190 	}
191 
192 	return -ENODEV;
193 }
194 
195 /* TC matchall action types */
196 enum dsa_port_mall_action_type {
197 	DSA_PORT_MALL_MIRROR,
198 	DSA_PORT_MALL_POLICER,
199 };
200 
201 /* TC mirroring entry */
202 struct dsa_mall_mirror_tc_entry {
203 	u8 to_local_port;
204 	bool ingress;
205 };
206 
207 /* TC port policer entry */
208 struct dsa_mall_policer_tc_entry {
209 	u32 burst;
210 	u64 rate_bytes_per_sec;
211 };
212 
213 /* TC matchall entry */
214 struct dsa_mall_tc_entry {
215 	struct list_head list;
216 	unsigned long cookie;
217 	enum dsa_port_mall_action_type type;
218 	union {
219 		struct dsa_mall_mirror_tc_entry mirror;
220 		struct dsa_mall_policer_tc_entry policer;
221 	};
222 };
223 
224 
225 struct dsa_port {
226 	/* A CPU port is physically connected to a master device.
227 	 * A user port exposed to userspace has a slave device.
228 	 */
229 	union {
230 		struct net_device *master;
231 		struct net_device *slave;
232 	};
233 
234 	/* Copy of the tagging protocol operations, for quicker access
235 	 * in the data path. Valid only for the CPU ports.
236 	 */
237 	const struct dsa_device_ops *tag_ops;
238 
239 	/* Copies for faster access in master receive hot path */
240 	struct dsa_switch_tree *dst;
241 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
242 			       struct packet_type *pt);
243 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
244 
245 	enum {
246 		DSA_PORT_TYPE_UNUSED = 0,
247 		DSA_PORT_TYPE_CPU,
248 		DSA_PORT_TYPE_DSA,
249 		DSA_PORT_TYPE_USER,
250 	} type;
251 
252 	struct dsa_switch	*ds;
253 	unsigned int		index;
254 	const char		*name;
255 	struct dsa_port		*cpu_dp;
256 	u8			mac[ETH_ALEN];
257 	struct device_node	*dn;
258 	unsigned int		ageing_time;
259 	bool			vlan_filtering;
260 	u8			stp_state;
261 	struct net_device	*bridge_dev;
262 	struct devlink_port	devlink_port;
263 	bool			devlink_port_setup;
264 	struct phylink		*pl;
265 	struct phylink_config	pl_config;
266 	struct net_device	*lag_dev;
267 	bool			lag_tx_enabled;
268 	struct net_device	*hsr_dev;
269 
270 	struct list_head list;
271 
272 	/*
273 	 * Give the switch driver somewhere to hang its per-port private data
274 	 * structures (accessible from the tagger).
275 	 */
276 	void *priv;
277 
278 	/*
279 	 * Original copy of the master netdev ethtool_ops
280 	 */
281 	const struct ethtool_ops *orig_ethtool_ops;
282 
283 	/*
284 	 * Original copy of the master netdev net_device_ops
285 	 */
286 	const struct dsa_netdevice_ops *netdev_ops;
287 
288 	/* List of MAC addresses that must be forwarded on this port.
289 	 * These are only valid on CPU ports and DSA links.
290 	 */
291 	struct list_head	fdbs;
292 	struct list_head	mdbs;
293 
294 	bool setup;
295 };
296 
297 /* TODO: ideally DSA ports would have a single dp->link_dp member,
298  * and no dst->rtable nor this struct dsa_link would be needed,
299  * but this would require some more complex tree walking,
300  * so keep it stupid at the moment and list them all.
301  */
302 struct dsa_link {
303 	struct dsa_port *dp;
304 	struct dsa_port *link_dp;
305 	struct list_head list;
306 };
307 
308 struct dsa_mac_addr {
309 	unsigned char addr[ETH_ALEN];
310 	u16 vid;
311 	refcount_t refcount;
312 	struct list_head list;
313 };
314 
315 struct dsa_switch {
316 	bool setup;
317 
318 	struct device *dev;
319 
320 	/*
321 	 * Parent switch tree, and switch index.
322 	 */
323 	struct dsa_switch_tree	*dst;
324 	unsigned int		index;
325 
326 	/* Listener for switch fabric events */
327 	struct notifier_block	nb;
328 
329 	/*
330 	 * Give the switch driver somewhere to hang its private data
331 	 * structure.
332 	 */
333 	void *priv;
334 
335 	/*
336 	 * Configuration data for this switch.
337 	 */
338 	struct dsa_chip_data	*cd;
339 
340 	/*
341 	 * The switch operations.
342 	 */
343 	const struct dsa_switch_ops	*ops;
344 
345 	/*
346 	 * Slave mii_bus and devices for the individual ports.
347 	 */
348 	u32			phys_mii_mask;
349 	struct mii_bus		*slave_mii_bus;
350 
351 	/* Ageing Time limits in msecs */
352 	unsigned int ageing_time_min;
353 	unsigned int ageing_time_max;
354 
355 	/* devlink used to represent this switch device */
356 	struct devlink		*devlink;
357 
358 	/* Number of switch port queues */
359 	unsigned int		num_tx_queues;
360 
361 	/* Disallow bridge core from requesting different VLAN awareness
362 	 * settings on ports if not hardware-supported
363 	 */
364 	bool			vlan_filtering_is_global;
365 
366 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
367 	 * that have vlan_filtering=0. All drivers should ideally set this (and
368 	 * then the option would get removed), but it is unknown whether this
369 	 * would break things or not.
370 	 */
371 	bool			configure_vlan_while_not_filtering;
372 
373 	/* If the switch driver always programs the CPU port as egress tagged
374 	 * despite the VLAN configuration indicating otherwise, then setting
375 	 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
376 	 * default_pvid VLAN tagged frames to offer a consistent behavior
377 	 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
378 	 */
379 	bool			untag_bridge_pvid;
380 
381 	/* Let DSA manage the FDB entries towards the CPU, based on the
382 	 * software bridge database.
383 	 */
384 	bool			assisted_learning_on_cpu_port;
385 
386 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
387 	 * should be retrieved from here and not from the per-port settings.
388 	 */
389 	bool			vlan_filtering;
390 
391 	/* MAC PCS does not provide link state change interrupt, and requires
392 	 * polling. Flag passed on to PHYLINK.
393 	 */
394 	bool			pcs_poll;
395 
396 	/* For switches that only have the MRU configurable. To ensure the
397 	 * configured MTU is not exceeded, normalization of MRU on all bridged
398 	 * interfaces is needed.
399 	 */
400 	bool			mtu_enforcement_ingress;
401 
402 	/* Drivers that benefit from having an ID associated with each
403 	 * offloaded LAG should set this to the maximum number of
404 	 * supported IDs. DSA will then maintain a mapping of _at
405 	 * least_ these many IDs, accessible to drivers via
406 	 * dsa_lag_id().
407 	 */
408 	unsigned int		num_lag_ids;
409 
410 	size_t num_ports;
411 };
412 
413 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
414 {
415 	struct dsa_switch_tree *dst = ds->dst;
416 	struct dsa_port *dp;
417 
418 	list_for_each_entry(dp, &dst->ports, list)
419 		if (dp->ds == ds && dp->index == p)
420 			return dp;
421 
422 	return NULL;
423 }
424 
425 static inline bool dsa_port_is_dsa(struct dsa_port *port)
426 {
427 	return port->type == DSA_PORT_TYPE_DSA;
428 }
429 
430 static inline bool dsa_port_is_cpu(struct dsa_port *port)
431 {
432 	return port->type == DSA_PORT_TYPE_CPU;
433 }
434 
435 static inline bool dsa_port_is_user(struct dsa_port *dp)
436 {
437 	return dp->type == DSA_PORT_TYPE_USER;
438 }
439 
440 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
441 {
442 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
443 }
444 
445 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
446 {
447 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
448 }
449 
450 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
451 {
452 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
453 }
454 
455 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
456 {
457 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
458 }
459 
460 static inline u32 dsa_user_ports(struct dsa_switch *ds)
461 {
462 	u32 mask = 0;
463 	int p;
464 
465 	for (p = 0; p < ds->num_ports; p++)
466 		if (dsa_is_user_port(ds, p))
467 			mask |= BIT(p);
468 
469 	return mask;
470 }
471 
472 /* Return the local port used to reach an arbitrary switch device */
473 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
474 {
475 	struct dsa_switch_tree *dst = ds->dst;
476 	struct dsa_link *dl;
477 
478 	list_for_each_entry(dl, &dst->rtable, list)
479 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
480 			return dl->dp->index;
481 
482 	return ds->num_ports;
483 }
484 
485 /* Return the local port used to reach an arbitrary switch port */
486 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
487 					    int port)
488 {
489 	if (device == ds->index)
490 		return port;
491 	else
492 		return dsa_routing_port(ds, device);
493 }
494 
495 /* Return the local port used to reach the dedicated CPU port */
496 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
497 {
498 	const struct dsa_port *dp = dsa_to_port(ds, port);
499 	const struct dsa_port *cpu_dp = dp->cpu_dp;
500 
501 	if (!cpu_dp)
502 		return port;
503 
504 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
505 }
506 
507 /* Return true if this is the local port used to reach the CPU port */
508 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
509 {
510 	if (dsa_is_unused_port(ds, port))
511 		return false;
512 
513 	return port == dsa_upstream_port(ds, port);
514 }
515 
516 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
517  * that the routing port from @downstream_ds to @upstream_ds is also the port
518  * which @downstream_ds uses to reach its dedicated CPU.
519  */
520 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
521 					     struct dsa_switch *downstream_ds)
522 {
523 	int routing_port;
524 
525 	if (upstream_ds == downstream_ds)
526 		return true;
527 
528 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
529 
530 	return dsa_is_upstream_port(downstream_ds, routing_port);
531 }
532 
533 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
534 {
535 	const struct dsa_switch *ds = dp->ds;
536 
537 	if (ds->vlan_filtering_is_global)
538 		return ds->vlan_filtering;
539 	else
540 		return dp->vlan_filtering;
541 }
542 
543 static inline
544 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
545 {
546 	if (!dp->bridge_dev)
547 		return NULL;
548 
549 	if (dp->lag_dev)
550 		return dp->lag_dev;
551 	else if (dp->hsr_dev)
552 		return dp->hsr_dev;
553 
554 	return dp->slave;
555 }
556 
557 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
558 			      bool is_static, void *data);
559 struct dsa_switch_ops {
560 	/*
561 	 * Tagging protocol helpers called for the CPU ports and DSA links.
562 	 * @get_tag_protocol retrieves the initial tagging protocol and is
563 	 * mandatory. Switches which can operate using multiple tagging
564 	 * protocols should implement @change_tag_protocol and report in
565 	 * @get_tag_protocol the tagger in current use.
566 	 */
567 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
568 						  int port,
569 						  enum dsa_tag_protocol mprot);
570 	int	(*change_tag_protocol)(struct dsa_switch *ds, int port,
571 				       enum dsa_tag_protocol proto);
572 
573 	int	(*setup)(struct dsa_switch *ds);
574 	void	(*teardown)(struct dsa_switch *ds);
575 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
576 
577 	/*
578 	 * Access to the switch's PHY registers.
579 	 */
580 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
581 	int	(*phy_write)(struct dsa_switch *ds, int port,
582 			     int regnum, u16 val);
583 
584 	/*
585 	 * Link state adjustment (called from libphy)
586 	 */
587 	void	(*adjust_link)(struct dsa_switch *ds, int port,
588 				struct phy_device *phydev);
589 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
590 				struct fixed_phy_status *st);
591 
592 	/*
593 	 * PHYLINK integration
594 	 */
595 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
596 				    unsigned long *supported,
597 				    struct phylink_link_state *state);
598 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
599 					  struct phylink_link_state *state);
600 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
601 				      unsigned int mode,
602 				      const struct phylink_link_state *state);
603 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
604 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
605 					 unsigned int mode,
606 					 phy_interface_t interface);
607 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
608 				       unsigned int mode,
609 				       phy_interface_t interface,
610 				       struct phy_device *phydev,
611 				       int speed, int duplex,
612 				       bool tx_pause, bool rx_pause);
613 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
614 				       struct phylink_link_state *state);
615 	/*
616 	 * Port statistics counters.
617 	 */
618 	void	(*get_strings)(struct dsa_switch *ds, int port,
619 			       u32 stringset, uint8_t *data);
620 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
621 				     int port, uint64_t *data);
622 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
623 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
624 					 int port, uint64_t *data);
625 	void	(*get_stats64)(struct dsa_switch *ds, int port,
626 				   struct rtnl_link_stats64 *s);
627 	void	(*self_test)(struct dsa_switch *ds, int port,
628 			     struct ethtool_test *etest, u64 *data);
629 
630 	/*
631 	 * ethtool Wake-on-LAN
632 	 */
633 	void	(*get_wol)(struct dsa_switch *ds, int port,
634 			   struct ethtool_wolinfo *w);
635 	int	(*set_wol)(struct dsa_switch *ds, int port,
636 			   struct ethtool_wolinfo *w);
637 
638 	/*
639 	 * ethtool timestamp info
640 	 */
641 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
642 			       struct ethtool_ts_info *ts);
643 
644 	/*
645 	 * Suspend and resume
646 	 */
647 	int	(*suspend)(struct dsa_switch *ds);
648 	int	(*resume)(struct dsa_switch *ds);
649 
650 	/*
651 	 * Port enable/disable
652 	 */
653 	int	(*port_enable)(struct dsa_switch *ds, int port,
654 			       struct phy_device *phy);
655 	void	(*port_disable)(struct dsa_switch *ds, int port);
656 
657 	/*
658 	 * Port's MAC EEE settings
659 	 */
660 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
661 			       struct ethtool_eee *e);
662 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
663 			       struct ethtool_eee *e);
664 
665 	/* EEPROM access */
666 	int	(*get_eeprom_len)(struct dsa_switch *ds);
667 	int	(*get_eeprom)(struct dsa_switch *ds,
668 			      struct ethtool_eeprom *eeprom, u8 *data);
669 	int	(*set_eeprom)(struct dsa_switch *ds,
670 			      struct ethtool_eeprom *eeprom, u8 *data);
671 
672 	/*
673 	 * Register access.
674 	 */
675 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
676 	void	(*get_regs)(struct dsa_switch *ds, int port,
677 			    struct ethtool_regs *regs, void *p);
678 
679 	/*
680 	 * Upper device tracking.
681 	 */
682 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
683 				       struct netdev_notifier_changeupper_info *info);
684 
685 	/*
686 	 * Bridge integration
687 	 */
688 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
689 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
690 				    struct net_device *bridge);
691 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
692 				     struct net_device *bridge);
693 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
694 				      u8 state);
695 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
696 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
697 					 struct switchdev_brport_flags flags,
698 					 struct netlink_ext_ack *extack);
699 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
700 				     struct switchdev_brport_flags flags,
701 				     struct netlink_ext_ack *extack);
702 	int	(*port_set_mrouter)(struct dsa_switch *ds, int port, bool mrouter,
703 				    struct netlink_ext_ack *extack);
704 
705 	/*
706 	 * VLAN support
707 	 */
708 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
709 				       bool vlan_filtering,
710 				       struct netlink_ext_ack *extack);
711 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
712 				 const struct switchdev_obj_port_vlan *vlan,
713 				 struct netlink_ext_ack *extack);
714 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
715 				 const struct switchdev_obj_port_vlan *vlan);
716 	/*
717 	 * Forwarding database
718 	 */
719 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
720 				const unsigned char *addr, u16 vid);
721 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
722 				const unsigned char *addr, u16 vid);
723 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
724 				 dsa_fdb_dump_cb_t *cb, void *data);
725 
726 	/*
727 	 * Multicast database
728 	 */
729 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
730 				const struct switchdev_obj_port_mdb *mdb);
731 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
732 				const struct switchdev_obj_port_mdb *mdb);
733 	/*
734 	 * RXNFC
735 	 */
736 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
737 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
738 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
739 			     struct ethtool_rxnfc *nfc);
740 
741 	/*
742 	 * TC integration
743 	 */
744 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
745 				  struct flow_cls_offload *cls, bool ingress);
746 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
747 				  struct flow_cls_offload *cls, bool ingress);
748 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
749 				    struct flow_cls_offload *cls, bool ingress);
750 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
751 				   struct dsa_mall_mirror_tc_entry *mirror,
752 				   bool ingress);
753 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
754 				   struct dsa_mall_mirror_tc_entry *mirror);
755 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
756 				    struct dsa_mall_policer_tc_entry *policer);
757 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
758 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
759 				 enum tc_setup_type type, void *type_data);
760 
761 	/*
762 	 * Cross-chip operations
763 	 */
764 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
765 					 int sw_index, int port,
766 					 struct net_device *br);
767 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
768 					  int sw_index, int port,
769 					  struct net_device *br);
770 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
771 					int port);
772 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
773 				      int port, struct net_device *lag,
774 				      struct netdev_lag_upper_info *info);
775 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
776 				       int port, struct net_device *lag);
777 
778 	/*
779 	 * PTP functionality
780 	 */
781 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
782 				     struct ifreq *ifr);
783 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
784 				     struct ifreq *ifr);
785 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
786 				 struct sk_buff *skb);
787 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
788 				 struct sk_buff *skb, unsigned int type);
789 
790 	/* Devlink parameters, etc */
791 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
792 				     struct devlink_param_gset_ctx *ctx);
793 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
794 				     struct devlink_param_gset_ctx *ctx);
795 	int	(*devlink_info_get)(struct dsa_switch *ds,
796 				    struct devlink_info_req *req,
797 				    struct netlink_ext_ack *extack);
798 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
799 				       unsigned int sb_index, u16 pool_index,
800 				       struct devlink_sb_pool_info *pool_info);
801 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
802 				       u16 pool_index, u32 size,
803 				       enum devlink_sb_threshold_type threshold_type,
804 				       struct netlink_ext_ack *extack);
805 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
806 					    unsigned int sb_index, u16 pool_index,
807 					    u32 *p_threshold);
808 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
809 					    unsigned int sb_index, u16 pool_index,
810 					    u32 threshold,
811 					    struct netlink_ext_ack *extack);
812 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
813 					       unsigned int sb_index, u16 tc_index,
814 					       enum devlink_sb_pool_type pool_type,
815 					       u16 *p_pool_index, u32 *p_threshold);
816 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
817 					       unsigned int sb_index, u16 tc_index,
818 					       enum devlink_sb_pool_type pool_type,
819 					       u16 pool_index, u32 threshold,
820 					       struct netlink_ext_ack *extack);
821 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
822 					   unsigned int sb_index);
823 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
824 					    unsigned int sb_index);
825 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
826 						unsigned int sb_index, u16 pool_index,
827 						u32 *p_cur, u32 *p_max);
828 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
829 						   unsigned int sb_index, u16 tc_index,
830 						   enum devlink_sb_pool_type pool_type,
831 						   u32 *p_cur, u32 *p_max);
832 
833 	/*
834 	 * MTU change functionality. Switches can also adjust their MRU through
835 	 * this method. By MTU, one understands the SDU (L2 payload) length.
836 	 * If the switch needs to account for the DSA tag on the CPU port, this
837 	 * method needs to do so privately.
838 	 */
839 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
840 				   int new_mtu);
841 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
842 
843 	/*
844 	 * LAG integration
845 	 */
846 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
847 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
848 				 struct net_device *lag,
849 				 struct netdev_lag_upper_info *info);
850 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
851 				  struct net_device *lag);
852 
853 	/*
854 	 * HSR integration
855 	 */
856 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
857 				 struct net_device *hsr);
858 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
859 				  struct net_device *hsr);
860 
861 	/*
862 	 * MRP integration
863 	 */
864 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
865 				const struct switchdev_obj_mrp *mrp);
866 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
867 				const struct switchdev_obj_mrp *mrp);
868 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
869 					  const struct switchdev_obj_ring_role_mrp *mrp);
870 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
871 					  const struct switchdev_obj_ring_role_mrp *mrp);
872 };
873 
874 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
875 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
876 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
877 
878 int dsa_devlink_param_get(struct devlink *dl, u32 id,
879 			  struct devlink_param_gset_ctx *ctx);
880 int dsa_devlink_param_set(struct devlink *dl, u32 id,
881 			  struct devlink_param_gset_ctx *ctx);
882 int dsa_devlink_params_register(struct dsa_switch *ds,
883 				const struct devlink_param *params,
884 				size_t params_count);
885 void dsa_devlink_params_unregister(struct dsa_switch *ds,
886 				   const struct devlink_param *params,
887 				   size_t params_count);
888 int dsa_devlink_resource_register(struct dsa_switch *ds,
889 				  const char *resource_name,
890 				  u64 resource_size,
891 				  u64 resource_id,
892 				  u64 parent_resource_id,
893 				  const struct devlink_resource_size_params *size_params);
894 
895 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
896 
897 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
898 					   u64 resource_id,
899 					   devlink_resource_occ_get_t *occ_get,
900 					   void *occ_get_priv);
901 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
902 					     u64 resource_id);
903 struct devlink_region *
904 dsa_devlink_region_create(struct dsa_switch *ds,
905 			  const struct devlink_region_ops *ops,
906 			  u32 region_max_snapshots, u64 region_size);
907 struct devlink_region *
908 dsa_devlink_port_region_create(struct dsa_switch *ds,
909 			       int port,
910 			       const struct devlink_port_region_ops *ops,
911 			       u32 region_max_snapshots, u64 region_size);
912 void dsa_devlink_region_destroy(struct devlink_region *region);
913 
914 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
915 
916 struct dsa_devlink_priv {
917 	struct dsa_switch *ds;
918 };
919 
920 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
921 {
922 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
923 
924 	return dl_priv->ds;
925 }
926 
927 static inline
928 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
929 {
930 	struct devlink *dl = port->devlink;
931 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
932 
933 	return dl_priv->ds;
934 }
935 
936 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
937 {
938 	return port->index;
939 }
940 
941 struct dsa_switch_driver {
942 	struct list_head	list;
943 	const struct dsa_switch_ops *ops;
944 };
945 
946 struct net_device *dsa_dev_to_net_device(struct device *dev);
947 
948 /* Keep inline for faster access in hot path */
949 static inline bool netdev_uses_dsa(const struct net_device *dev)
950 {
951 #if IS_ENABLED(CONFIG_NET_DSA)
952 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
953 #endif
954 	return false;
955 }
956 
957 static inline bool dsa_can_decode(const struct sk_buff *skb,
958 				  struct net_device *dev)
959 {
960 #if IS_ENABLED(CONFIG_NET_DSA)
961 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
962 #endif
963 	return false;
964 }
965 
966 /* All DSA tags that push the EtherType to the right (basically all except tail
967  * tags, which don't break dissection) can be treated the same from the
968  * perspective of the flow dissector.
969  *
970  * We need to return:
971  *  - offset: the (B - A) difference between:
972  *    A. the position of the real EtherType and
973  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
974  *       after the normal EtherType was supposed to be)
975  *    The offset in bytes is exactly equal to the tagger overhead (and half of
976  *    that, in __be16 shorts).
977  *
978  *  - proto: the value of the real EtherType.
979  */
980 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
981 						__be16 *proto, int *offset)
982 {
983 #if IS_ENABLED(CONFIG_NET_DSA)
984 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
985 	int tag_len = ops->needed_headroom;
986 
987 	*offset = tag_len;
988 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
989 #endif
990 }
991 
992 #if IS_ENABLED(CONFIG_NET_DSA)
993 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
994 {
995 	int err = -EOPNOTSUPP;
996 
997 	if (!dev->dsa_ptr)
998 		return err;
999 
1000 	if (!dev->dsa_ptr->netdev_ops)
1001 		return err;
1002 
1003 	return 0;
1004 }
1005 
1006 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
1007 				   int cmd)
1008 {
1009 	const struct dsa_netdevice_ops *ops;
1010 	int err;
1011 
1012 	err = __dsa_netdevice_ops_check(dev);
1013 	if (err)
1014 		return err;
1015 
1016 	ops = dev->dsa_ptr->netdev_ops;
1017 
1018 	return ops->ndo_do_ioctl(dev, ifr, cmd);
1019 }
1020 #else
1021 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
1022 				   int cmd)
1023 {
1024 	return -EOPNOTSUPP;
1025 }
1026 #endif
1027 
1028 void dsa_unregister_switch(struct dsa_switch *ds);
1029 int dsa_register_switch(struct dsa_switch *ds);
1030 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1031 #ifdef CONFIG_PM_SLEEP
1032 int dsa_switch_suspend(struct dsa_switch *ds);
1033 int dsa_switch_resume(struct dsa_switch *ds);
1034 #else
1035 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1036 {
1037 	return 0;
1038 }
1039 static inline int dsa_switch_resume(struct dsa_switch *ds)
1040 {
1041 	return 0;
1042 }
1043 #endif /* CONFIG_PM_SLEEP */
1044 
1045 #if IS_ENABLED(CONFIG_NET_DSA)
1046 bool dsa_slave_dev_check(const struct net_device *dev);
1047 #else
1048 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1049 {
1050 	return false;
1051 }
1052 #endif
1053 
1054 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1055 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1056 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1057 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
1058 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1059 
1060 struct dsa_tag_driver {
1061 	const struct dsa_device_ops *ops;
1062 	struct list_head list;
1063 	struct module *owner;
1064 };
1065 
1066 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1067 			      unsigned int count,
1068 			      struct module *owner);
1069 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1070 				unsigned int count);
1071 
1072 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
1073 static int __init dsa_tag_driver_module_init(void)			\
1074 {									\
1075 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
1076 				 THIS_MODULE);				\
1077 	return 0;							\
1078 }									\
1079 module_init(dsa_tag_driver_module_init);				\
1080 									\
1081 static void __exit dsa_tag_driver_module_exit(void)			\
1082 {									\
1083 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
1084 }									\
1085 module_exit(dsa_tag_driver_module_exit)
1086 
1087 /**
1088  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1089  * drivers
1090  * @__ops_array: Array of tag driver strucutres
1091  *
1092  * Helper macro for DSA tag drivers which do not do anything special
1093  * in module init/exit. Each module may only use this macro once, and
1094  * calling it replaces module_init() and module_exit().
1095  */
1096 #define module_dsa_tag_drivers(__ops_array)				\
1097 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1098 
1099 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1100 
1101 /* Create a static structure we can build a linked list of dsa_tag
1102  * drivers
1103  */
1104 #define DSA_TAG_DRIVER(__ops)						\
1105 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1106 	.ops = &__ops,							\
1107 }
1108 
1109 /**
1110  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1111  * driver
1112  * @__ops: Single tag driver structures
1113  *
1114  * Helper macro for DSA tag drivers which do not do anything special
1115  * in module init/exit. Each module may only use this macro once, and
1116  * calling it replaces module_init() and module_exit().
1117  */
1118 #define module_dsa_tag_driver(__ops)					\
1119 DSA_TAG_DRIVER(__ops);							\
1120 									\
1121 static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1122 	&DSA_TAG_DRIVER_NAME(__ops)					\
1123 };									\
1124 module_dsa_tag_drivers(dsa_tag_driver_array)
1125 #endif
1126 
1127