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