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