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