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