xref: /linux/include/net/dsa.h (revision 7c83a7c539abe9f980996063ac20532a7a7f6eb1)
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 
52 enum dsa_tag_protocol {
53 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
54 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
55 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
56 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
57 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
58 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
59 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
60 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
61 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
62 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
63 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
64 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
65 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
66 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
67 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
68 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
69 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
70 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
71 	DSA_TAG_PROTO_HELLCREEK		= DSA_TAG_PROTO_HELLCREEK_VALUE,
72 	DSA_TAG_PROTO_XRS700X		= DSA_TAG_PROTO_XRS700X_VALUE,
73 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
74 };
75 
76 struct packet_type;
77 struct dsa_switch;
78 
79 struct dsa_device_ops {
80 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
81 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
82 			       struct packet_type *pt);
83 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
84 			     int *offset);
85 	/* Used to determine which traffic should match the DSA filter in
86 	 * eth_type_trans, and which, if any, should bypass it and be processed
87 	 * as regular on the master net device.
88 	 */
89 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
90 	unsigned int overhead;
91 	const char *name;
92 	enum dsa_tag_protocol proto;
93 	/* Some tagging protocols either mangle or shift the destination MAC
94 	 * address, in which case the DSA master would drop packets on ingress
95 	 * if what it understands out of the destination MAC address is not in
96 	 * its RX filter.
97 	 */
98 	bool promisc_on_master;
99 	bool tail_tag;
100 };
101 
102 /* This structure defines the control interfaces that are overlayed by the
103  * DSA layer on top of the DSA CPU/management net_device instance. This is
104  * used by the core net_device layer while calling various net_device_ops
105  * function pointers.
106  */
107 struct dsa_netdevice_ops {
108 	int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr,
109 			    int cmd);
110 };
111 
112 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
113 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
114 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
115 
116 struct dsa_skb_cb {
117 	struct sk_buff *clone;
118 };
119 
120 struct __dsa_skb_cb {
121 	struct dsa_skb_cb cb;
122 	u8 priv[48 - sizeof(struct dsa_skb_cb)];
123 };
124 
125 #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
126 
127 #define DSA_SKB_CB_PRIV(skb)			\
128 	((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
129 
130 struct dsa_switch_tree {
131 	struct list_head	list;
132 
133 	/* Notifier chain for switch-wide events */
134 	struct raw_notifier_head	nh;
135 
136 	/* Tree identifier */
137 	unsigned int index;
138 
139 	/* Number of switches attached to this tree */
140 	struct kref refcount;
141 
142 	/* Has this tree been applied to the hardware? */
143 	bool setup;
144 
145 	/* Tagging protocol operations */
146 	const struct dsa_device_ops *tag_ops;
147 
148 	/*
149 	 * Configuration data for the platform device that owns
150 	 * this dsa switch tree instance.
151 	 */
152 	struct dsa_platform_data	*pd;
153 
154 	/* List of switch ports */
155 	struct list_head ports;
156 
157 	/* List of DSA links composing the routing table */
158 	struct list_head rtable;
159 
160 	/* Maps offloaded LAG netdevs to a zero-based linear ID for
161 	 * drivers that need it.
162 	 */
163 	struct net_device **lags;
164 	unsigned int lags_len;
165 };
166 
167 #define dsa_lags_foreach_id(_id, _dst)				\
168 	for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++)	\
169 		if ((_dst)->lags[(_id)])
170 
171 #define dsa_lag_foreach_port(_dp, _dst, _lag)			\
172 	list_for_each_entry((_dp), &(_dst)->ports, list)	\
173 		if ((_dp)->lag_dev == (_lag))
174 
175 static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
176 					     unsigned int id)
177 {
178 	return dst->lags[id];
179 }
180 
181 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
182 			     struct net_device *lag)
183 {
184 	unsigned int id;
185 
186 	dsa_lags_foreach_id(id, dst) {
187 		if (dsa_lag_dev(dst, id) == lag)
188 			return id;
189 	}
190 
191 	return -ENODEV;
192 }
193 
194 /* TC matchall action types */
195 enum dsa_port_mall_action_type {
196 	DSA_PORT_MALL_MIRROR,
197 	DSA_PORT_MALL_POLICER,
198 };
199 
200 /* TC mirroring entry */
201 struct dsa_mall_mirror_tc_entry {
202 	u8 to_local_port;
203 	bool ingress;
204 };
205 
206 /* TC port policer entry */
207 struct dsa_mall_policer_tc_entry {
208 	u32 burst;
209 	u64 rate_bytes_per_sec;
210 };
211 
212 /* TC matchall entry */
213 struct dsa_mall_tc_entry {
214 	struct list_head list;
215 	unsigned long cookie;
216 	enum dsa_port_mall_action_type type;
217 	union {
218 		struct dsa_mall_mirror_tc_entry mirror;
219 		struct dsa_mall_policer_tc_entry policer;
220 	};
221 };
222 
223 
224 struct dsa_port {
225 	/* A CPU port is physically connected to a master device.
226 	 * A user port exposed to userspace has a slave device.
227 	 */
228 	union {
229 		struct net_device *master;
230 		struct net_device *slave;
231 	};
232 
233 	/* Copy of the tagging protocol operations, for quicker access
234 	 * in the data path. Valid only for the CPU ports.
235 	 */
236 	const struct dsa_device_ops *tag_ops;
237 
238 	/* Copies for faster access in master receive hot path */
239 	struct dsa_switch_tree *dst;
240 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
241 			       struct packet_type *pt);
242 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
243 
244 	enum {
245 		DSA_PORT_TYPE_UNUSED = 0,
246 		DSA_PORT_TYPE_CPU,
247 		DSA_PORT_TYPE_DSA,
248 		DSA_PORT_TYPE_USER,
249 	} type;
250 
251 	struct dsa_switch	*ds;
252 	unsigned int		index;
253 	const char		*name;
254 	struct dsa_port		*cpu_dp;
255 	const char		*mac;
256 	struct device_node	*dn;
257 	unsigned int		ageing_time;
258 	bool			vlan_filtering;
259 	u8			stp_state;
260 	struct net_device	*bridge_dev;
261 	struct devlink_port	devlink_port;
262 	bool			devlink_port_setup;
263 	struct phylink		*pl;
264 	struct phylink_config	pl_config;
265 	struct net_device	*lag_dev;
266 	bool			lag_tx_enabled;
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 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
488 			      bool is_static, void *data);
489 struct dsa_switch_ops {
490 	/*
491 	 * Tagging protocol helpers called for the CPU ports and DSA links.
492 	 * @get_tag_protocol retrieves the initial tagging protocol and is
493 	 * mandatory. Switches which can operate using multiple tagging
494 	 * protocols should implement @change_tag_protocol and report in
495 	 * @get_tag_protocol the tagger in current use.
496 	 */
497 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
498 						  int port,
499 						  enum dsa_tag_protocol mprot);
500 	int	(*change_tag_protocol)(struct dsa_switch *ds, int port,
501 				       enum dsa_tag_protocol proto);
502 
503 	int	(*setup)(struct dsa_switch *ds);
504 	void	(*teardown)(struct dsa_switch *ds);
505 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
506 
507 	/*
508 	 * Access to the switch's PHY registers.
509 	 */
510 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
511 	int	(*phy_write)(struct dsa_switch *ds, int port,
512 			     int regnum, u16 val);
513 
514 	/*
515 	 * Link state adjustment (called from libphy)
516 	 */
517 	void	(*adjust_link)(struct dsa_switch *ds, int port,
518 				struct phy_device *phydev);
519 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
520 				struct fixed_phy_status *st);
521 
522 	/*
523 	 * PHYLINK integration
524 	 */
525 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
526 				    unsigned long *supported,
527 				    struct phylink_link_state *state);
528 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
529 					  struct phylink_link_state *state);
530 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
531 				      unsigned int mode,
532 				      const struct phylink_link_state *state);
533 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
534 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
535 					 unsigned int mode,
536 					 phy_interface_t interface);
537 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
538 				       unsigned int mode,
539 				       phy_interface_t interface,
540 				       struct phy_device *phydev,
541 				       int speed, int duplex,
542 				       bool tx_pause, bool rx_pause);
543 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
544 				       struct phylink_link_state *state);
545 	/*
546 	 * Port statistics counters.
547 	 */
548 	void	(*get_strings)(struct dsa_switch *ds, int port,
549 			       u32 stringset, uint8_t *data);
550 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
551 				     int port, uint64_t *data);
552 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
553 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
554 					 int port, uint64_t *data);
555 	void	(*get_stats64)(struct dsa_switch *ds, int port,
556 				   struct rtnl_link_stats64 *s);
557 
558 	/*
559 	 * ethtool Wake-on-LAN
560 	 */
561 	void	(*get_wol)(struct dsa_switch *ds, int port,
562 			   struct ethtool_wolinfo *w);
563 	int	(*set_wol)(struct dsa_switch *ds, int port,
564 			   struct ethtool_wolinfo *w);
565 
566 	/*
567 	 * ethtool timestamp info
568 	 */
569 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
570 			       struct ethtool_ts_info *ts);
571 
572 	/*
573 	 * Suspend and resume
574 	 */
575 	int	(*suspend)(struct dsa_switch *ds);
576 	int	(*resume)(struct dsa_switch *ds);
577 
578 	/*
579 	 * Port enable/disable
580 	 */
581 	int	(*port_enable)(struct dsa_switch *ds, int port,
582 			       struct phy_device *phy);
583 	void	(*port_disable)(struct dsa_switch *ds, int port);
584 
585 	/*
586 	 * Port's MAC EEE settings
587 	 */
588 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
589 			       struct ethtool_eee *e);
590 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
591 			       struct ethtool_eee *e);
592 
593 	/* EEPROM access */
594 	int	(*get_eeprom_len)(struct dsa_switch *ds);
595 	int	(*get_eeprom)(struct dsa_switch *ds,
596 			      struct ethtool_eeprom *eeprom, u8 *data);
597 	int	(*set_eeprom)(struct dsa_switch *ds,
598 			      struct ethtool_eeprom *eeprom, u8 *data);
599 
600 	/*
601 	 * Register access.
602 	 */
603 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
604 	void	(*get_regs)(struct dsa_switch *ds, int port,
605 			    struct ethtool_regs *regs, void *p);
606 
607 	/*
608 	 * Upper device tracking.
609 	 */
610 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
611 				       struct netdev_notifier_changeupper_info *info);
612 
613 	/*
614 	 * Bridge integration
615 	 */
616 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
617 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
618 				    struct net_device *bridge);
619 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
620 				     struct net_device *bridge);
621 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
622 				      u8 state);
623 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
624 	int	(*port_egress_floods)(struct dsa_switch *ds, int port,
625 				      bool unicast, bool multicast);
626 
627 	/*
628 	 * VLAN support
629 	 */
630 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
631 				       bool vlan_filtering);
632 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
633 				 const struct switchdev_obj_port_vlan *vlan);
634 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
635 				 const struct switchdev_obj_port_vlan *vlan);
636 	/*
637 	 * Forwarding database
638 	 */
639 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
640 				const unsigned char *addr, u16 vid);
641 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
642 				const unsigned char *addr, u16 vid);
643 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
644 				 dsa_fdb_dump_cb_t *cb, void *data);
645 
646 	/*
647 	 * Multicast database
648 	 */
649 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
650 				const struct switchdev_obj_port_mdb *mdb);
651 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
652 				const struct switchdev_obj_port_mdb *mdb);
653 	/*
654 	 * RXNFC
655 	 */
656 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
657 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
658 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
659 			     struct ethtool_rxnfc *nfc);
660 
661 	/*
662 	 * TC integration
663 	 */
664 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
665 				  struct flow_cls_offload *cls, bool ingress);
666 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
667 				  struct flow_cls_offload *cls, bool ingress);
668 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
669 				    struct flow_cls_offload *cls, bool ingress);
670 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
671 				   struct dsa_mall_mirror_tc_entry *mirror,
672 				   bool ingress);
673 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
674 				   struct dsa_mall_mirror_tc_entry *mirror);
675 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
676 				    struct dsa_mall_policer_tc_entry *policer);
677 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
678 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
679 				 enum tc_setup_type type, void *type_data);
680 
681 	/*
682 	 * Cross-chip operations
683 	 */
684 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
685 					 int sw_index, int port,
686 					 struct net_device *br);
687 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
688 					  int sw_index, int port,
689 					  struct net_device *br);
690 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
691 					int port);
692 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
693 				      int port, struct net_device *lag,
694 				      struct netdev_lag_upper_info *info);
695 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
696 				       int port, struct net_device *lag);
697 
698 	/*
699 	 * PTP functionality
700 	 */
701 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
702 				     struct ifreq *ifr);
703 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
704 				     struct ifreq *ifr);
705 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
706 				 struct sk_buff *clone, unsigned int type);
707 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
708 				 struct sk_buff *skb, unsigned int type);
709 
710 	/* Devlink parameters, etc */
711 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
712 				     struct devlink_param_gset_ctx *ctx);
713 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
714 				     struct devlink_param_gset_ctx *ctx);
715 	int	(*devlink_info_get)(struct dsa_switch *ds,
716 				    struct devlink_info_req *req,
717 				    struct netlink_ext_ack *extack);
718 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
719 				       unsigned int sb_index, u16 pool_index,
720 				       struct devlink_sb_pool_info *pool_info);
721 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
722 				       u16 pool_index, u32 size,
723 				       enum devlink_sb_threshold_type threshold_type,
724 				       struct netlink_ext_ack *extack);
725 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
726 					    unsigned int sb_index, u16 pool_index,
727 					    u32 *p_threshold);
728 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
729 					    unsigned int sb_index, u16 pool_index,
730 					    u32 threshold,
731 					    struct netlink_ext_ack *extack);
732 	int	(*devlink_sb_tc_pool_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 					       u16 *p_pool_index, u32 *p_threshold);
736 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
737 					       unsigned int sb_index, u16 tc_index,
738 					       enum devlink_sb_pool_type pool_type,
739 					       u16 pool_index, u32 threshold,
740 					       struct netlink_ext_ack *extack);
741 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
742 					   unsigned int sb_index);
743 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
744 					    unsigned int sb_index);
745 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
746 						unsigned int sb_index, u16 pool_index,
747 						u32 *p_cur, u32 *p_max);
748 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
749 						   unsigned int sb_index, u16 tc_index,
750 						   enum devlink_sb_pool_type pool_type,
751 						   u32 *p_cur, u32 *p_max);
752 
753 	/*
754 	 * MTU change functionality. Switches can also adjust their MRU through
755 	 * this method. By MTU, one understands the SDU (L2 payload) length.
756 	 * If the switch needs to account for the DSA tag on the CPU port, this
757 	 * method needs to do so privately.
758 	 */
759 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
760 				   int new_mtu);
761 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
762 
763 	/*
764 	 * LAG integration
765 	 */
766 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
767 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
768 				 struct net_device *lag,
769 				 struct netdev_lag_upper_info *info);
770 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
771 				  struct net_device *lag);
772 };
773 
774 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
775 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
776 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
777 
778 int dsa_devlink_param_get(struct devlink *dl, u32 id,
779 			  struct devlink_param_gset_ctx *ctx);
780 int dsa_devlink_param_set(struct devlink *dl, u32 id,
781 			  struct devlink_param_gset_ctx *ctx);
782 int dsa_devlink_params_register(struct dsa_switch *ds,
783 				const struct devlink_param *params,
784 				size_t params_count);
785 void dsa_devlink_params_unregister(struct dsa_switch *ds,
786 				   const struct devlink_param *params,
787 				   size_t params_count);
788 int dsa_devlink_resource_register(struct dsa_switch *ds,
789 				  const char *resource_name,
790 				  u64 resource_size,
791 				  u64 resource_id,
792 				  u64 parent_resource_id,
793 				  const struct devlink_resource_size_params *size_params);
794 
795 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
796 
797 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
798 					   u64 resource_id,
799 					   devlink_resource_occ_get_t *occ_get,
800 					   void *occ_get_priv);
801 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
802 					     u64 resource_id);
803 struct devlink_region *
804 dsa_devlink_region_create(struct dsa_switch *ds,
805 			  const struct devlink_region_ops *ops,
806 			  u32 region_max_snapshots, u64 region_size);
807 struct devlink_region *
808 dsa_devlink_port_region_create(struct dsa_switch *ds,
809 			       int port,
810 			       const struct devlink_port_region_ops *ops,
811 			       u32 region_max_snapshots, u64 region_size);
812 void dsa_devlink_region_destroy(struct devlink_region *region);
813 
814 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
815 
816 struct dsa_devlink_priv {
817 	struct dsa_switch *ds;
818 };
819 
820 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
821 {
822 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
823 
824 	return dl_priv->ds;
825 }
826 
827 static inline
828 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
829 {
830 	struct devlink *dl = port->devlink;
831 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
832 
833 	return dl_priv->ds;
834 }
835 
836 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
837 {
838 	return port->index;
839 }
840 
841 struct dsa_switch_driver {
842 	struct list_head	list;
843 	const struct dsa_switch_ops *ops;
844 };
845 
846 struct net_device *dsa_dev_to_net_device(struct device *dev);
847 
848 /* Keep inline for faster access in hot path */
849 static inline bool netdev_uses_dsa(const struct net_device *dev)
850 {
851 #if IS_ENABLED(CONFIG_NET_DSA)
852 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
853 #endif
854 	return false;
855 }
856 
857 static inline bool dsa_can_decode(const struct sk_buff *skb,
858 				  struct net_device *dev)
859 {
860 #if IS_ENABLED(CONFIG_NET_DSA)
861 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
862 #endif
863 	return false;
864 }
865 
866 /* All DSA tags that push the EtherType to the right (basically all except tail
867  * tags, which don't break dissection) can be treated the same from the
868  * perspective of the flow dissector.
869  *
870  * We need to return:
871  *  - offset: the (B - A) difference between:
872  *    A. the position of the real EtherType and
873  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
874  *       after the normal EtherType was supposed to be)
875  *    The offset in bytes is exactly equal to the tagger overhead (and half of
876  *    that, in __be16 shorts).
877  *
878  *  - proto: the value of the real EtherType.
879  */
880 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
881 						__be16 *proto, int *offset)
882 {
883 #if IS_ENABLED(CONFIG_NET_DSA)
884 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
885 	int tag_len = ops->overhead;
886 
887 	*offset = tag_len;
888 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
889 #endif
890 }
891 
892 #if IS_ENABLED(CONFIG_NET_DSA)
893 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
894 {
895 	int err = -EOPNOTSUPP;
896 
897 	if (!dev->dsa_ptr)
898 		return err;
899 
900 	if (!dev->dsa_ptr->netdev_ops)
901 		return err;
902 
903 	return 0;
904 }
905 
906 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
907 				   int cmd)
908 {
909 	const struct dsa_netdevice_ops *ops;
910 	int err;
911 
912 	err = __dsa_netdevice_ops_check(dev);
913 	if (err)
914 		return err;
915 
916 	ops = dev->dsa_ptr->netdev_ops;
917 
918 	return ops->ndo_do_ioctl(dev, ifr, cmd);
919 }
920 #else
921 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
922 				   int cmd)
923 {
924 	return -EOPNOTSUPP;
925 }
926 #endif
927 
928 void dsa_unregister_switch(struct dsa_switch *ds);
929 int dsa_register_switch(struct dsa_switch *ds);
930 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
931 #ifdef CONFIG_PM_SLEEP
932 int dsa_switch_suspend(struct dsa_switch *ds);
933 int dsa_switch_resume(struct dsa_switch *ds);
934 #else
935 static inline int dsa_switch_suspend(struct dsa_switch *ds)
936 {
937 	return 0;
938 }
939 static inline int dsa_switch_resume(struct dsa_switch *ds)
940 {
941 	return 0;
942 }
943 #endif /* CONFIG_PM_SLEEP */
944 
945 #if IS_ENABLED(CONFIG_NET_DSA)
946 bool dsa_slave_dev_check(const struct net_device *dev);
947 #else
948 static inline bool dsa_slave_dev_check(const struct net_device *dev)
949 {
950 	return false;
951 }
952 #endif
953 
954 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
955 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
956 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
957 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
958 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
959 
960 struct dsa_tag_driver {
961 	const struct dsa_device_ops *ops;
962 	struct list_head list;
963 	struct module *owner;
964 };
965 
966 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
967 			      unsigned int count,
968 			      struct module *owner);
969 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
970 				unsigned int count);
971 
972 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
973 static int __init dsa_tag_driver_module_init(void)			\
974 {									\
975 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
976 				 THIS_MODULE);				\
977 	return 0;							\
978 }									\
979 module_init(dsa_tag_driver_module_init);				\
980 									\
981 static void __exit dsa_tag_driver_module_exit(void)			\
982 {									\
983 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
984 }									\
985 module_exit(dsa_tag_driver_module_exit)
986 
987 /**
988  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
989  * drivers
990  * @__ops_array: Array of tag driver strucutres
991  *
992  * Helper macro for DSA tag drivers which do not do anything special
993  * in module init/exit. Each module may only use this macro once, and
994  * calling it replaces module_init() and module_exit().
995  */
996 #define module_dsa_tag_drivers(__ops_array)				\
997 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
998 
999 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1000 
1001 /* Create a static structure we can build a linked list of dsa_tag
1002  * drivers
1003  */
1004 #define DSA_TAG_DRIVER(__ops)						\
1005 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1006 	.ops = &__ops,							\
1007 }
1008 
1009 /**
1010  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1011  * driver
1012  * @__ops: Single tag driver structures
1013  *
1014  * Helper macro for DSA tag drivers which do not do anything special
1015  * in module init/exit. Each module may only use this macro once, and
1016  * calling it replaces module_init() and module_exit().
1017  */
1018 #define module_dsa_tag_driver(__ops)					\
1019 DSA_TAG_DRIVER(__ops);							\
1020 									\
1021 static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1022 	&DSA_TAG_DRIVER_NAME(__ops)					\
1023 };									\
1024 module_dsa_tag_drivers(dsa_tag_driver_array)
1025 #endif
1026 
1027