xref: /linux/include/net/dsa.h (revision d0f482bb06f9447d44d2cae0386a0bd768c3cc16)
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 	bool setup;
289 };
290 
291 /* TODO: ideally DSA ports would have a single dp->link_dp member,
292  * and no dst->rtable nor this struct dsa_link would be needed,
293  * but this would require some more complex tree walking,
294  * so keep it stupid at the moment and list them all.
295  */
296 struct dsa_link {
297 	struct dsa_port *dp;
298 	struct dsa_port *link_dp;
299 	struct list_head list;
300 };
301 
302 struct dsa_switch {
303 	bool setup;
304 
305 	struct device *dev;
306 
307 	/*
308 	 * Parent switch tree, and switch index.
309 	 */
310 	struct dsa_switch_tree	*dst;
311 	unsigned int		index;
312 
313 	/* Listener for switch fabric events */
314 	struct notifier_block	nb;
315 
316 	/*
317 	 * Give the switch driver somewhere to hang its private data
318 	 * structure.
319 	 */
320 	void *priv;
321 
322 	/*
323 	 * Configuration data for this switch.
324 	 */
325 	struct dsa_chip_data	*cd;
326 
327 	/*
328 	 * The switch operations.
329 	 */
330 	const struct dsa_switch_ops	*ops;
331 
332 	/*
333 	 * Slave mii_bus and devices for the individual ports.
334 	 */
335 	u32			phys_mii_mask;
336 	struct mii_bus		*slave_mii_bus;
337 
338 	/* Ageing Time limits in msecs */
339 	unsigned int ageing_time_min;
340 	unsigned int ageing_time_max;
341 
342 	/* devlink used to represent this switch device */
343 	struct devlink		*devlink;
344 
345 	/* Number of switch port queues */
346 	unsigned int		num_tx_queues;
347 
348 	/* Disallow bridge core from requesting different VLAN awareness
349 	 * settings on ports if not hardware-supported
350 	 */
351 	bool			vlan_filtering_is_global;
352 
353 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
354 	 * that have vlan_filtering=0. All drivers should ideally set this (and
355 	 * then the option would get removed), but it is unknown whether this
356 	 * would break things or not.
357 	 */
358 	bool			configure_vlan_while_not_filtering;
359 
360 	/* If the switch driver always programs the CPU port as egress tagged
361 	 * despite the VLAN configuration indicating otherwise, then setting
362 	 * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
363 	 * default_pvid VLAN tagged frames to offer a consistent behavior
364 	 * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
365 	 */
366 	bool			untag_bridge_pvid;
367 
368 	/* Let DSA manage the FDB entries towards the CPU, based on the
369 	 * software bridge database.
370 	 */
371 	bool			assisted_learning_on_cpu_port;
372 
373 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
374 	 * should be retrieved from here and not from the per-port settings.
375 	 */
376 	bool			vlan_filtering;
377 
378 	/* MAC PCS does not provide link state change interrupt, and requires
379 	 * polling. Flag passed on to PHYLINK.
380 	 */
381 	bool			pcs_poll;
382 
383 	/* For switches that only have the MRU configurable. To ensure the
384 	 * configured MTU is not exceeded, normalization of MRU on all bridged
385 	 * interfaces is needed.
386 	 */
387 	bool			mtu_enforcement_ingress;
388 
389 	/* Drivers that benefit from having an ID associated with each
390 	 * offloaded LAG should set this to the maximum number of
391 	 * supported IDs. DSA will then maintain a mapping of _at
392 	 * least_ these many IDs, accessible to drivers via
393 	 * dsa_lag_id().
394 	 */
395 	unsigned int		num_lag_ids;
396 
397 	size_t num_ports;
398 };
399 
400 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
401 {
402 	struct dsa_switch_tree *dst = ds->dst;
403 	struct dsa_port *dp;
404 
405 	list_for_each_entry(dp, &dst->ports, list)
406 		if (dp->ds == ds && dp->index == p)
407 			return dp;
408 
409 	return NULL;
410 }
411 
412 static inline bool dsa_port_is_dsa(struct dsa_port *port)
413 {
414 	return port->type == DSA_PORT_TYPE_DSA;
415 }
416 
417 static inline bool dsa_port_is_cpu(struct dsa_port *port)
418 {
419 	return port->type == DSA_PORT_TYPE_CPU;
420 }
421 
422 static inline bool dsa_port_is_user(struct dsa_port *dp)
423 {
424 	return dp->type == DSA_PORT_TYPE_USER;
425 }
426 
427 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
428 {
429 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
430 }
431 
432 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
433 {
434 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
435 }
436 
437 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
438 {
439 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
440 }
441 
442 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
443 {
444 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
445 }
446 
447 static inline u32 dsa_user_ports(struct dsa_switch *ds)
448 {
449 	u32 mask = 0;
450 	int p;
451 
452 	for (p = 0; p < ds->num_ports; p++)
453 		if (dsa_is_user_port(ds, p))
454 			mask |= BIT(p);
455 
456 	return mask;
457 }
458 
459 /* Return the local port used to reach an arbitrary switch device */
460 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
461 {
462 	struct dsa_switch_tree *dst = ds->dst;
463 	struct dsa_link *dl;
464 
465 	list_for_each_entry(dl, &dst->rtable, list)
466 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
467 			return dl->dp->index;
468 
469 	return ds->num_ports;
470 }
471 
472 /* Return the local port used to reach an arbitrary switch port */
473 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
474 					    int port)
475 {
476 	if (device == ds->index)
477 		return port;
478 	else
479 		return dsa_routing_port(ds, device);
480 }
481 
482 /* Return the local port used to reach the dedicated CPU port */
483 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
484 {
485 	const struct dsa_port *dp = dsa_to_port(ds, port);
486 	const struct dsa_port *cpu_dp = dp->cpu_dp;
487 
488 	if (!cpu_dp)
489 		return port;
490 
491 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
492 }
493 
494 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
495 {
496 	const struct dsa_switch *ds = dp->ds;
497 
498 	if (ds->vlan_filtering_is_global)
499 		return ds->vlan_filtering;
500 	else
501 		return dp->vlan_filtering;
502 }
503 
504 static inline
505 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
506 {
507 	if (!dp->bridge_dev)
508 		return NULL;
509 
510 	if (dp->lag_dev)
511 		return dp->lag_dev;
512 	else if (dp->hsr_dev)
513 		return dp->hsr_dev;
514 
515 	return dp->slave;
516 }
517 
518 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
519 			      bool is_static, void *data);
520 struct dsa_switch_ops {
521 	/*
522 	 * Tagging protocol helpers called for the CPU ports and DSA links.
523 	 * @get_tag_protocol retrieves the initial tagging protocol and is
524 	 * mandatory. Switches which can operate using multiple tagging
525 	 * protocols should implement @change_tag_protocol and report in
526 	 * @get_tag_protocol the tagger in current use.
527 	 */
528 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
529 						  int port,
530 						  enum dsa_tag_protocol mprot);
531 	int	(*change_tag_protocol)(struct dsa_switch *ds, int port,
532 				       enum dsa_tag_protocol proto);
533 
534 	int	(*setup)(struct dsa_switch *ds);
535 	void	(*teardown)(struct dsa_switch *ds);
536 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
537 
538 	/*
539 	 * Access to the switch's PHY registers.
540 	 */
541 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
542 	int	(*phy_write)(struct dsa_switch *ds, int port,
543 			     int regnum, u16 val);
544 
545 	/*
546 	 * Link state adjustment (called from libphy)
547 	 */
548 	void	(*adjust_link)(struct dsa_switch *ds, int port,
549 				struct phy_device *phydev);
550 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
551 				struct fixed_phy_status *st);
552 
553 	/*
554 	 * PHYLINK integration
555 	 */
556 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
557 				    unsigned long *supported,
558 				    struct phylink_link_state *state);
559 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
560 					  struct phylink_link_state *state);
561 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
562 				      unsigned int mode,
563 				      const struct phylink_link_state *state);
564 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
565 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
566 					 unsigned int mode,
567 					 phy_interface_t interface);
568 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
569 				       unsigned int mode,
570 				       phy_interface_t interface,
571 				       struct phy_device *phydev,
572 				       int speed, int duplex,
573 				       bool tx_pause, bool rx_pause);
574 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
575 				       struct phylink_link_state *state);
576 	/*
577 	 * Port statistics counters.
578 	 */
579 	void	(*get_strings)(struct dsa_switch *ds, int port,
580 			       u32 stringset, uint8_t *data);
581 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
582 				     int port, uint64_t *data);
583 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
584 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
585 					 int port, uint64_t *data);
586 	void	(*get_stats64)(struct dsa_switch *ds, int port,
587 				   struct rtnl_link_stats64 *s);
588 	void	(*self_test)(struct dsa_switch *ds, int port,
589 			     struct ethtool_test *etest, u64 *data);
590 
591 	/*
592 	 * ethtool Wake-on-LAN
593 	 */
594 	void	(*get_wol)(struct dsa_switch *ds, int port,
595 			   struct ethtool_wolinfo *w);
596 	int	(*set_wol)(struct dsa_switch *ds, int port,
597 			   struct ethtool_wolinfo *w);
598 
599 	/*
600 	 * ethtool timestamp info
601 	 */
602 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
603 			       struct ethtool_ts_info *ts);
604 
605 	/*
606 	 * Suspend and resume
607 	 */
608 	int	(*suspend)(struct dsa_switch *ds);
609 	int	(*resume)(struct dsa_switch *ds);
610 
611 	/*
612 	 * Port enable/disable
613 	 */
614 	int	(*port_enable)(struct dsa_switch *ds, int port,
615 			       struct phy_device *phy);
616 	void	(*port_disable)(struct dsa_switch *ds, int port);
617 
618 	/*
619 	 * Port's MAC EEE settings
620 	 */
621 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
622 			       struct ethtool_eee *e);
623 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
624 			       struct ethtool_eee *e);
625 
626 	/* EEPROM access */
627 	int	(*get_eeprom_len)(struct dsa_switch *ds);
628 	int	(*get_eeprom)(struct dsa_switch *ds,
629 			      struct ethtool_eeprom *eeprom, u8 *data);
630 	int	(*set_eeprom)(struct dsa_switch *ds,
631 			      struct ethtool_eeprom *eeprom, u8 *data);
632 
633 	/*
634 	 * Register access.
635 	 */
636 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
637 	void	(*get_regs)(struct dsa_switch *ds, int port,
638 			    struct ethtool_regs *regs, void *p);
639 
640 	/*
641 	 * Upper device tracking.
642 	 */
643 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
644 				       struct netdev_notifier_changeupper_info *info);
645 
646 	/*
647 	 * Bridge integration
648 	 */
649 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
650 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
651 				    struct net_device *bridge);
652 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
653 				     struct net_device *bridge);
654 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
655 				      u8 state);
656 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
657 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
658 					 struct switchdev_brport_flags flags,
659 					 struct netlink_ext_ack *extack);
660 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
661 				     struct switchdev_brport_flags flags,
662 				     struct netlink_ext_ack *extack);
663 	int	(*port_set_mrouter)(struct dsa_switch *ds, int port, bool mrouter,
664 				    struct netlink_ext_ack *extack);
665 
666 	/*
667 	 * VLAN support
668 	 */
669 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
670 				       bool vlan_filtering,
671 				       struct netlink_ext_ack *extack);
672 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
673 				 const struct switchdev_obj_port_vlan *vlan,
674 				 struct netlink_ext_ack *extack);
675 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
676 				 const struct switchdev_obj_port_vlan *vlan);
677 	/*
678 	 * Forwarding database
679 	 */
680 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
681 				const unsigned char *addr, u16 vid);
682 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
683 				const unsigned char *addr, u16 vid);
684 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
685 				 dsa_fdb_dump_cb_t *cb, void *data);
686 
687 	/*
688 	 * Multicast database
689 	 */
690 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
691 				const struct switchdev_obj_port_mdb *mdb);
692 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
693 				const struct switchdev_obj_port_mdb *mdb);
694 	/*
695 	 * RXNFC
696 	 */
697 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
698 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
699 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
700 			     struct ethtool_rxnfc *nfc);
701 
702 	/*
703 	 * TC integration
704 	 */
705 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
706 				  struct flow_cls_offload *cls, bool ingress);
707 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
708 				  struct flow_cls_offload *cls, bool ingress);
709 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
710 				    struct flow_cls_offload *cls, bool ingress);
711 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
712 				   struct dsa_mall_mirror_tc_entry *mirror,
713 				   bool ingress);
714 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
715 				   struct dsa_mall_mirror_tc_entry *mirror);
716 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
717 				    struct dsa_mall_policer_tc_entry *policer);
718 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
719 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
720 				 enum tc_setup_type type, void *type_data);
721 
722 	/*
723 	 * Cross-chip operations
724 	 */
725 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
726 					 int sw_index, int port,
727 					 struct net_device *br);
728 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
729 					  int sw_index, int port,
730 					  struct net_device *br);
731 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
732 					int port);
733 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
734 				      int port, struct net_device *lag,
735 				      struct netdev_lag_upper_info *info);
736 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
737 				       int port, struct net_device *lag);
738 
739 	/*
740 	 * PTP functionality
741 	 */
742 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
743 				     struct ifreq *ifr);
744 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
745 				     struct ifreq *ifr);
746 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
747 				 struct sk_buff *skb);
748 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
749 				 struct sk_buff *skb, unsigned int type);
750 
751 	/* Devlink parameters, etc */
752 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
753 				     struct devlink_param_gset_ctx *ctx);
754 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
755 				     struct devlink_param_gset_ctx *ctx);
756 	int	(*devlink_info_get)(struct dsa_switch *ds,
757 				    struct devlink_info_req *req,
758 				    struct netlink_ext_ack *extack);
759 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
760 				       unsigned int sb_index, u16 pool_index,
761 				       struct devlink_sb_pool_info *pool_info);
762 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
763 				       u16 pool_index, u32 size,
764 				       enum devlink_sb_threshold_type threshold_type,
765 				       struct netlink_ext_ack *extack);
766 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
767 					    unsigned int sb_index, u16 pool_index,
768 					    u32 *p_threshold);
769 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
770 					    unsigned int sb_index, u16 pool_index,
771 					    u32 threshold,
772 					    struct netlink_ext_ack *extack);
773 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
774 					       unsigned int sb_index, u16 tc_index,
775 					       enum devlink_sb_pool_type pool_type,
776 					       u16 *p_pool_index, u32 *p_threshold);
777 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
778 					       unsigned int sb_index, u16 tc_index,
779 					       enum devlink_sb_pool_type pool_type,
780 					       u16 pool_index, u32 threshold,
781 					       struct netlink_ext_ack *extack);
782 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
783 					   unsigned int sb_index);
784 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
785 					    unsigned int sb_index);
786 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
787 						unsigned int sb_index, u16 pool_index,
788 						u32 *p_cur, u32 *p_max);
789 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
790 						   unsigned int sb_index, u16 tc_index,
791 						   enum devlink_sb_pool_type pool_type,
792 						   u32 *p_cur, u32 *p_max);
793 
794 	/*
795 	 * MTU change functionality. Switches can also adjust their MRU through
796 	 * this method. By MTU, one understands the SDU (L2 payload) length.
797 	 * If the switch needs to account for the DSA tag on the CPU port, this
798 	 * method needs to do so privately.
799 	 */
800 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
801 				   int new_mtu);
802 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
803 
804 	/*
805 	 * LAG integration
806 	 */
807 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
808 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
809 				 struct net_device *lag,
810 				 struct netdev_lag_upper_info *info);
811 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
812 				  struct net_device *lag);
813 
814 	/*
815 	 * HSR integration
816 	 */
817 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
818 				 struct net_device *hsr);
819 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
820 				  struct net_device *hsr);
821 
822 	/*
823 	 * MRP integration
824 	 */
825 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
826 				const struct switchdev_obj_mrp *mrp);
827 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
828 				const struct switchdev_obj_mrp *mrp);
829 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
830 					  const struct switchdev_obj_ring_role_mrp *mrp);
831 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
832 					  const struct switchdev_obj_ring_role_mrp *mrp);
833 };
834 
835 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
836 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
837 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
838 
839 int dsa_devlink_param_get(struct devlink *dl, u32 id,
840 			  struct devlink_param_gset_ctx *ctx);
841 int dsa_devlink_param_set(struct devlink *dl, u32 id,
842 			  struct devlink_param_gset_ctx *ctx);
843 int dsa_devlink_params_register(struct dsa_switch *ds,
844 				const struct devlink_param *params,
845 				size_t params_count);
846 void dsa_devlink_params_unregister(struct dsa_switch *ds,
847 				   const struct devlink_param *params,
848 				   size_t params_count);
849 int dsa_devlink_resource_register(struct dsa_switch *ds,
850 				  const char *resource_name,
851 				  u64 resource_size,
852 				  u64 resource_id,
853 				  u64 parent_resource_id,
854 				  const struct devlink_resource_size_params *size_params);
855 
856 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
857 
858 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
859 					   u64 resource_id,
860 					   devlink_resource_occ_get_t *occ_get,
861 					   void *occ_get_priv);
862 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
863 					     u64 resource_id);
864 struct devlink_region *
865 dsa_devlink_region_create(struct dsa_switch *ds,
866 			  const struct devlink_region_ops *ops,
867 			  u32 region_max_snapshots, u64 region_size);
868 struct devlink_region *
869 dsa_devlink_port_region_create(struct dsa_switch *ds,
870 			       int port,
871 			       const struct devlink_port_region_ops *ops,
872 			       u32 region_max_snapshots, u64 region_size);
873 void dsa_devlink_region_destroy(struct devlink_region *region);
874 
875 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
876 
877 struct dsa_devlink_priv {
878 	struct dsa_switch *ds;
879 };
880 
881 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
882 {
883 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
884 
885 	return dl_priv->ds;
886 }
887 
888 static inline
889 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
890 {
891 	struct devlink *dl = port->devlink;
892 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
893 
894 	return dl_priv->ds;
895 }
896 
897 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
898 {
899 	return port->index;
900 }
901 
902 struct dsa_switch_driver {
903 	struct list_head	list;
904 	const struct dsa_switch_ops *ops;
905 };
906 
907 struct net_device *dsa_dev_to_net_device(struct device *dev);
908 
909 /* Keep inline for faster access in hot path */
910 static inline bool netdev_uses_dsa(const struct net_device *dev)
911 {
912 #if IS_ENABLED(CONFIG_NET_DSA)
913 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
914 #endif
915 	return false;
916 }
917 
918 static inline bool dsa_can_decode(const struct sk_buff *skb,
919 				  struct net_device *dev)
920 {
921 #if IS_ENABLED(CONFIG_NET_DSA)
922 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
923 #endif
924 	return false;
925 }
926 
927 /* All DSA tags that push the EtherType to the right (basically all except tail
928  * tags, which don't break dissection) can be treated the same from the
929  * perspective of the flow dissector.
930  *
931  * We need to return:
932  *  - offset: the (B - A) difference between:
933  *    A. the position of the real EtherType and
934  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
935  *       after the normal EtherType was supposed to be)
936  *    The offset in bytes is exactly equal to the tagger overhead (and half of
937  *    that, in __be16 shorts).
938  *
939  *  - proto: the value of the real EtherType.
940  */
941 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
942 						__be16 *proto, int *offset)
943 {
944 #if IS_ENABLED(CONFIG_NET_DSA)
945 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
946 	int tag_len = ops->needed_headroom;
947 
948 	*offset = tag_len;
949 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
950 #endif
951 }
952 
953 #if IS_ENABLED(CONFIG_NET_DSA)
954 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
955 {
956 	int err = -EOPNOTSUPP;
957 
958 	if (!dev->dsa_ptr)
959 		return err;
960 
961 	if (!dev->dsa_ptr->netdev_ops)
962 		return err;
963 
964 	return 0;
965 }
966 
967 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
968 				   int cmd)
969 {
970 	const struct dsa_netdevice_ops *ops;
971 	int err;
972 
973 	err = __dsa_netdevice_ops_check(dev);
974 	if (err)
975 		return err;
976 
977 	ops = dev->dsa_ptr->netdev_ops;
978 
979 	return ops->ndo_do_ioctl(dev, ifr, cmd);
980 }
981 #else
982 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
983 				   int cmd)
984 {
985 	return -EOPNOTSUPP;
986 }
987 #endif
988 
989 void dsa_unregister_switch(struct dsa_switch *ds);
990 int dsa_register_switch(struct dsa_switch *ds);
991 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
992 #ifdef CONFIG_PM_SLEEP
993 int dsa_switch_suspend(struct dsa_switch *ds);
994 int dsa_switch_resume(struct dsa_switch *ds);
995 #else
996 static inline int dsa_switch_suspend(struct dsa_switch *ds)
997 {
998 	return 0;
999 }
1000 static inline int dsa_switch_resume(struct dsa_switch *ds)
1001 {
1002 	return 0;
1003 }
1004 #endif /* CONFIG_PM_SLEEP */
1005 
1006 #if IS_ENABLED(CONFIG_NET_DSA)
1007 bool dsa_slave_dev_check(const struct net_device *dev);
1008 #else
1009 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1010 {
1011 	return false;
1012 }
1013 #endif
1014 
1015 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1016 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1017 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1018 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
1019 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1020 
1021 struct dsa_tag_driver {
1022 	const struct dsa_device_ops *ops;
1023 	struct list_head list;
1024 	struct module *owner;
1025 };
1026 
1027 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1028 			      unsigned int count,
1029 			      struct module *owner);
1030 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1031 				unsigned int count);
1032 
1033 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
1034 static int __init dsa_tag_driver_module_init(void)			\
1035 {									\
1036 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
1037 				 THIS_MODULE);				\
1038 	return 0;							\
1039 }									\
1040 module_init(dsa_tag_driver_module_init);				\
1041 									\
1042 static void __exit dsa_tag_driver_module_exit(void)			\
1043 {									\
1044 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
1045 }									\
1046 module_exit(dsa_tag_driver_module_exit)
1047 
1048 /**
1049  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1050  * drivers
1051  * @__ops_array: Array of tag driver strucutres
1052  *
1053  * Helper macro for DSA tag drivers which do not do anything special
1054  * in module init/exit. Each module may only use this macro once, and
1055  * calling it replaces module_init() and module_exit().
1056  */
1057 #define module_dsa_tag_drivers(__ops_array)				\
1058 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1059 
1060 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1061 
1062 /* Create a static structure we can build a linked list of dsa_tag
1063  * drivers
1064  */
1065 #define DSA_TAG_DRIVER(__ops)						\
1066 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1067 	.ops = &__ops,							\
1068 }
1069 
1070 /**
1071  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1072  * driver
1073  * @__ops: Single tag driver structures
1074  *
1075  * Helper macro for DSA tag drivers which do not do anything special
1076  * in module init/exit. Each module may only use this macro once, and
1077  * calling it replaces module_init() and module_exit().
1078  */
1079 #define module_dsa_tag_driver(__ops)					\
1080 DSA_TAG_DRIVER(__ops);							\
1081 									\
1082 static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1083 	&DSA_TAG_DRIVER_NAME(__ops)					\
1084 };									\
1085 module_dsa_tag_drivers(dsa_tag_driver_array)
1086 #endif
1087 
1088