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