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