xref: /linux/include/net/dsa.h (revision efd7fe68f0c6c9649757bf80cbc382fd21e764c9)
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 
49 enum dsa_tag_protocol {
50 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
51 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
52 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
53 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
54 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
55 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
56 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
57 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
58 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
59 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
60 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
61 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
62 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
63 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
64 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
65 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
66 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
67 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
68 };
69 
70 struct packet_type;
71 struct dsa_switch;
72 
73 struct dsa_device_ops {
74 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
75 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
76 			       struct packet_type *pt);
77 	int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
78 			    int *offset);
79 	/* Used to determine which traffic should match the DSA filter in
80 	 * eth_type_trans, and which, if any, should bypass it and be processed
81 	 * as regular on the master net device.
82 	 */
83 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
84 	unsigned int overhead;
85 	const char *name;
86 	enum dsa_tag_protocol proto;
87 };
88 
89 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
90 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
91 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
92 
93 struct dsa_skb_cb {
94 	struct sk_buff *clone;
95 };
96 
97 struct __dsa_skb_cb {
98 	struct dsa_skb_cb cb;
99 	u8 priv[48 - sizeof(struct dsa_skb_cb)];
100 };
101 
102 #define DSA_SKB_CB(skb) ((struct dsa_skb_cb *)((skb)->cb))
103 
104 #define DSA_SKB_CB_PRIV(skb)			\
105 	((void *)(skb)->cb + offsetof(struct __dsa_skb_cb, priv))
106 
107 struct dsa_switch_tree {
108 	struct list_head	list;
109 
110 	/* Notifier chain for switch-wide events */
111 	struct raw_notifier_head	nh;
112 
113 	/* Tree identifier */
114 	unsigned int index;
115 
116 	/* Number of switches attached to this tree */
117 	struct kref refcount;
118 
119 	/* Has this tree been applied to the hardware? */
120 	bool setup;
121 
122 	/*
123 	 * Configuration data for the platform device that owns
124 	 * this dsa switch tree instance.
125 	 */
126 	struct dsa_platform_data	*pd;
127 
128 	/* List of switch ports */
129 	struct list_head ports;
130 
131 	/* List of DSA links composing the routing table */
132 	struct list_head rtable;
133 };
134 
135 /* TC matchall action types */
136 enum dsa_port_mall_action_type {
137 	DSA_PORT_MALL_MIRROR,
138 	DSA_PORT_MALL_POLICER,
139 };
140 
141 /* TC mirroring entry */
142 struct dsa_mall_mirror_tc_entry {
143 	u8 to_local_port;
144 	bool ingress;
145 };
146 
147 /* TC port policer entry */
148 struct dsa_mall_policer_tc_entry {
149 	u32 burst;
150 	u64 rate_bytes_per_sec;
151 };
152 
153 /* TC matchall entry */
154 struct dsa_mall_tc_entry {
155 	struct list_head list;
156 	unsigned long cookie;
157 	enum dsa_port_mall_action_type type;
158 	union {
159 		struct dsa_mall_mirror_tc_entry mirror;
160 		struct dsa_mall_policer_tc_entry policer;
161 	};
162 };
163 
164 
165 struct dsa_port {
166 	/* A CPU port is physically connected to a master device.
167 	 * A user port exposed to userspace has a slave device.
168 	 */
169 	union {
170 		struct net_device *master;
171 		struct net_device *slave;
172 	};
173 
174 	/* CPU port tagging operations used by master or slave devices */
175 	const struct dsa_device_ops *tag_ops;
176 
177 	/* Copies for faster access in master receive hot path */
178 	struct dsa_switch_tree *dst;
179 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
180 			       struct packet_type *pt);
181 	bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
182 
183 	enum {
184 		DSA_PORT_TYPE_UNUSED = 0,
185 		DSA_PORT_TYPE_CPU,
186 		DSA_PORT_TYPE_DSA,
187 		DSA_PORT_TYPE_USER,
188 	} type;
189 
190 	struct dsa_switch	*ds;
191 	unsigned int		index;
192 	const char		*name;
193 	struct dsa_port		*cpu_dp;
194 	const char		*mac;
195 	struct device_node	*dn;
196 	unsigned int		ageing_time;
197 	bool			vlan_filtering;
198 	u8			stp_state;
199 	struct net_device	*bridge_dev;
200 	struct devlink_port	devlink_port;
201 	struct phylink		*pl;
202 	struct phylink_config	pl_config;
203 
204 	struct list_head list;
205 
206 	/*
207 	 * Give the switch driver somewhere to hang its per-port private data
208 	 * structures (accessible from the tagger).
209 	 */
210 	void *priv;
211 
212 	/*
213 	 * Original copy of the master netdev ethtool_ops
214 	 */
215 	const struct ethtool_ops *orig_ethtool_ops;
216 
217 	/*
218 	 * Original copy of the master netdev net_device_ops
219 	 */
220 	const struct net_device_ops *orig_ndo_ops;
221 
222 	bool setup;
223 };
224 
225 /* TODO: ideally DSA ports would have a single dp->link_dp member,
226  * and no dst->rtable nor this struct dsa_link would be needed,
227  * but this would require some more complex tree walking,
228  * so keep it stupid at the moment and list them all.
229  */
230 struct dsa_link {
231 	struct dsa_port *dp;
232 	struct dsa_port *link_dp;
233 	struct list_head list;
234 };
235 
236 struct dsa_switch {
237 	bool setup;
238 
239 	struct device *dev;
240 
241 	/*
242 	 * Parent switch tree, and switch index.
243 	 */
244 	struct dsa_switch_tree	*dst;
245 	unsigned int		index;
246 
247 	/* Listener for switch fabric events */
248 	struct notifier_block	nb;
249 
250 	/*
251 	 * Give the switch driver somewhere to hang its private data
252 	 * structure.
253 	 */
254 	void *priv;
255 
256 	/*
257 	 * Configuration data for this switch.
258 	 */
259 	struct dsa_chip_data	*cd;
260 
261 	/*
262 	 * The switch operations.
263 	 */
264 	const struct dsa_switch_ops	*ops;
265 
266 	/*
267 	 * Slave mii_bus and devices for the individual ports.
268 	 */
269 	u32			phys_mii_mask;
270 	struct mii_bus		*slave_mii_bus;
271 
272 	/* Ageing Time limits in msecs */
273 	unsigned int ageing_time_min;
274 	unsigned int ageing_time_max;
275 
276 	/* devlink used to represent this switch device */
277 	struct devlink		*devlink;
278 
279 	/* Number of switch port queues */
280 	unsigned int		num_tx_queues;
281 
282 	/* Disallow bridge core from requesting different VLAN awareness
283 	 * settings on ports if not hardware-supported
284 	 */
285 	bool			vlan_filtering_is_global;
286 
287 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
288 	 * that have vlan_filtering=0. All drivers should ideally set this (and
289 	 * then the option would get removed), but it is unknown whether this
290 	 * would break things or not.
291 	 */
292 	bool			configure_vlan_while_not_filtering;
293 
294 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
295 	 * should be retrieved from here and not from the per-port settings.
296 	 */
297 	bool			vlan_filtering;
298 
299 	/* MAC PCS does not provide link state change interrupt, and requires
300 	 * polling. Flag passed on to PHYLINK.
301 	 */
302 	bool			pcs_poll;
303 
304 	/* For switches that only have the MRU configurable. To ensure the
305 	 * configured MTU is not exceeded, normalization of MRU on all bridged
306 	 * interfaces is needed.
307 	 */
308 	bool			mtu_enforcement_ingress;
309 
310 	size_t num_ports;
311 };
312 
313 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
314 {
315 	struct dsa_switch_tree *dst = ds->dst;
316 	struct dsa_port *dp;
317 
318 	list_for_each_entry(dp, &dst->ports, list)
319 		if (dp->ds == ds && dp->index == p)
320 			return dp;
321 
322 	return NULL;
323 }
324 
325 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
326 {
327 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
328 }
329 
330 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
331 {
332 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
333 }
334 
335 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
336 {
337 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
338 }
339 
340 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
341 {
342 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
343 }
344 
345 static inline u32 dsa_user_ports(struct dsa_switch *ds)
346 {
347 	u32 mask = 0;
348 	int p;
349 
350 	for (p = 0; p < ds->num_ports; p++)
351 		if (dsa_is_user_port(ds, p))
352 			mask |= BIT(p);
353 
354 	return mask;
355 }
356 
357 /* Return the local port used to reach an arbitrary switch device */
358 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
359 {
360 	struct dsa_switch_tree *dst = ds->dst;
361 	struct dsa_link *dl;
362 
363 	list_for_each_entry(dl, &dst->rtable, list)
364 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
365 			return dl->dp->index;
366 
367 	return ds->num_ports;
368 }
369 
370 /* Return the local port used to reach an arbitrary switch port */
371 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
372 					    int port)
373 {
374 	if (device == ds->index)
375 		return port;
376 	else
377 		return dsa_routing_port(ds, device);
378 }
379 
380 /* Return the local port used to reach the dedicated CPU port */
381 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
382 {
383 	const struct dsa_port *dp = dsa_to_port(ds, port);
384 	const struct dsa_port *cpu_dp = dp->cpu_dp;
385 
386 	if (!cpu_dp)
387 		return port;
388 
389 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
390 }
391 
392 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
393 {
394 	const struct dsa_switch *ds = dp->ds;
395 
396 	if (ds->vlan_filtering_is_global)
397 		return ds->vlan_filtering;
398 	else
399 		return dp->vlan_filtering;
400 }
401 
402 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
403 			      bool is_static, void *data);
404 struct dsa_switch_ops {
405 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
406 						  int port,
407 						  enum dsa_tag_protocol mprot);
408 
409 	int	(*setup)(struct dsa_switch *ds);
410 	void	(*teardown)(struct dsa_switch *ds);
411 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
412 
413 	/*
414 	 * Access to the switch's PHY registers.
415 	 */
416 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
417 	int	(*phy_write)(struct dsa_switch *ds, int port,
418 			     int regnum, u16 val);
419 
420 	/*
421 	 * Link state adjustment (called from libphy)
422 	 */
423 	void	(*adjust_link)(struct dsa_switch *ds, int port,
424 				struct phy_device *phydev);
425 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
426 				struct fixed_phy_status *st);
427 
428 	/*
429 	 * PHYLINK integration
430 	 */
431 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
432 				    unsigned long *supported,
433 				    struct phylink_link_state *state);
434 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
435 					  struct phylink_link_state *state);
436 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
437 				      unsigned int mode,
438 				      const struct phylink_link_state *state);
439 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
440 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
441 					 unsigned int mode,
442 					 phy_interface_t interface);
443 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
444 				       unsigned int mode,
445 				       phy_interface_t interface,
446 				       struct phy_device *phydev,
447 				       int speed, int duplex,
448 				       bool tx_pause, bool rx_pause);
449 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
450 				       struct phylink_link_state *state);
451 	/*
452 	 * ethtool hardware statistics.
453 	 */
454 	void	(*get_strings)(struct dsa_switch *ds, int port,
455 			       u32 stringset, uint8_t *data);
456 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
457 				     int port, uint64_t *data);
458 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
459 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
460 					 int port, uint64_t *data);
461 
462 	/*
463 	 * ethtool Wake-on-LAN
464 	 */
465 	void	(*get_wol)(struct dsa_switch *ds, int port,
466 			   struct ethtool_wolinfo *w);
467 	int	(*set_wol)(struct dsa_switch *ds, int port,
468 			   struct ethtool_wolinfo *w);
469 
470 	/*
471 	 * ethtool timestamp info
472 	 */
473 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
474 			       struct ethtool_ts_info *ts);
475 
476 	/*
477 	 * Suspend and resume
478 	 */
479 	int	(*suspend)(struct dsa_switch *ds);
480 	int	(*resume)(struct dsa_switch *ds);
481 
482 	/*
483 	 * Port enable/disable
484 	 */
485 	int	(*port_enable)(struct dsa_switch *ds, int port,
486 			       struct phy_device *phy);
487 	void	(*port_disable)(struct dsa_switch *ds, int port);
488 
489 	/*
490 	 * Port's MAC EEE settings
491 	 */
492 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
493 			       struct ethtool_eee *e);
494 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
495 			       struct ethtool_eee *e);
496 
497 	/* EEPROM access */
498 	int	(*get_eeprom_len)(struct dsa_switch *ds);
499 	int	(*get_eeprom)(struct dsa_switch *ds,
500 			      struct ethtool_eeprom *eeprom, u8 *data);
501 	int	(*set_eeprom)(struct dsa_switch *ds,
502 			      struct ethtool_eeprom *eeprom, u8 *data);
503 
504 	/*
505 	 * Register access.
506 	 */
507 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
508 	void	(*get_regs)(struct dsa_switch *ds, int port,
509 			    struct ethtool_regs *regs, void *p);
510 
511 	/*
512 	 * Bridge integration
513 	 */
514 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
515 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
516 				    struct net_device *bridge);
517 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
518 				     struct net_device *bridge);
519 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
520 				      u8 state);
521 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
522 	int	(*port_egress_floods)(struct dsa_switch *ds, int port,
523 				      bool unicast, bool multicast);
524 
525 	/*
526 	 * VLAN support
527 	 */
528 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
529 				       bool vlan_filtering);
530 	int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
531 				 const struct switchdev_obj_port_vlan *vlan);
532 	void (*port_vlan_add)(struct dsa_switch *ds, int port,
533 			      const struct switchdev_obj_port_vlan *vlan);
534 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
535 				 const struct switchdev_obj_port_vlan *vlan);
536 	/*
537 	 * Forwarding database
538 	 */
539 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
540 				const unsigned char *addr, u16 vid);
541 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
542 				const unsigned char *addr, u16 vid);
543 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
544 				 dsa_fdb_dump_cb_t *cb, void *data);
545 
546 	/*
547 	 * Multicast database
548 	 */
549 	int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
550 				const struct switchdev_obj_port_mdb *mdb);
551 	void (*port_mdb_add)(struct dsa_switch *ds, int port,
552 			     const struct switchdev_obj_port_mdb *mdb);
553 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
554 				const struct switchdev_obj_port_mdb *mdb);
555 	/*
556 	 * RXNFC
557 	 */
558 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
559 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
560 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
561 			     struct ethtool_rxnfc *nfc);
562 
563 	/*
564 	 * TC integration
565 	 */
566 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
567 				  struct flow_cls_offload *cls, bool ingress);
568 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
569 				  struct flow_cls_offload *cls, bool ingress);
570 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
571 				    struct flow_cls_offload *cls, bool ingress);
572 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
573 				   struct dsa_mall_mirror_tc_entry *mirror,
574 				   bool ingress);
575 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
576 				   struct dsa_mall_mirror_tc_entry *mirror);
577 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
578 				    struct dsa_mall_policer_tc_entry *policer);
579 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
580 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
581 				 enum tc_setup_type type, void *type_data);
582 
583 	/*
584 	 * Cross-chip operations
585 	 */
586 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
587 					 int sw_index, int port,
588 					 struct net_device *br);
589 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
590 					  int sw_index, int port,
591 					  struct net_device *br);
592 
593 	/*
594 	 * PTP functionality
595 	 */
596 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
597 				     struct ifreq *ifr);
598 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
599 				     struct ifreq *ifr);
600 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
601 				 struct sk_buff *clone, unsigned int type);
602 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
603 				 struct sk_buff *skb, unsigned int type);
604 
605 	/* Devlink parameters */
606 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
607 				     struct devlink_param_gset_ctx *ctx);
608 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
609 				     struct devlink_param_gset_ctx *ctx);
610 
611 	/*
612 	 * MTU change functionality. Switches can also adjust their MRU through
613 	 * this method. By MTU, one understands the SDU (L2 payload) length.
614 	 * If the switch needs to account for the DSA tag on the CPU port, this
615 	 * method needs to to do so privately.
616 	 */
617 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
618 				   int new_mtu);
619 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
620 };
621 
622 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
623 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
624 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
625 
626 int dsa_devlink_param_get(struct devlink *dl, u32 id,
627 			  struct devlink_param_gset_ctx *ctx);
628 int dsa_devlink_param_set(struct devlink *dl, u32 id,
629 			  struct devlink_param_gset_ctx *ctx);
630 int dsa_devlink_params_register(struct dsa_switch *ds,
631 				const struct devlink_param *params,
632 				size_t params_count);
633 void dsa_devlink_params_unregister(struct dsa_switch *ds,
634 				   const struct devlink_param *params,
635 				   size_t params_count);
636 int dsa_devlink_resource_register(struct dsa_switch *ds,
637 				  const char *resource_name,
638 				  u64 resource_size,
639 				  u64 resource_id,
640 				  u64 parent_resource_id,
641 				  const struct devlink_resource_size_params *size_params);
642 
643 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
644 
645 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
646 					   u64 resource_id,
647 					   devlink_resource_occ_get_t *occ_get,
648 					   void *occ_get_priv);
649 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
650 					     u64 resource_id);
651 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
652 
653 struct dsa_devlink_priv {
654 	struct dsa_switch *ds;
655 };
656 
657 struct dsa_switch_driver {
658 	struct list_head	list;
659 	const struct dsa_switch_ops *ops;
660 };
661 
662 struct net_device *dsa_dev_to_net_device(struct device *dev);
663 
664 /* Keep inline for faster access in hot path */
665 static inline bool netdev_uses_dsa(const struct net_device *dev)
666 {
667 #if IS_ENABLED(CONFIG_NET_DSA)
668 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
669 #endif
670 	return false;
671 }
672 
673 static inline bool dsa_can_decode(const struct sk_buff *skb,
674 				  struct net_device *dev)
675 {
676 #if IS_ENABLED(CONFIG_NET_DSA)
677 	return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
678 #endif
679 	return false;
680 }
681 
682 void dsa_unregister_switch(struct dsa_switch *ds);
683 int dsa_register_switch(struct dsa_switch *ds);
684 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
685 #ifdef CONFIG_PM_SLEEP
686 int dsa_switch_suspend(struct dsa_switch *ds);
687 int dsa_switch_resume(struct dsa_switch *ds);
688 #else
689 static inline int dsa_switch_suspend(struct dsa_switch *ds)
690 {
691 	return 0;
692 }
693 static inline int dsa_switch_resume(struct dsa_switch *ds)
694 {
695 	return 0;
696 }
697 #endif /* CONFIG_PM_SLEEP */
698 
699 enum dsa_notifier_type {
700 	DSA_PORT_REGISTER,
701 	DSA_PORT_UNREGISTER,
702 };
703 
704 struct dsa_notifier_info {
705 	struct net_device *dev;
706 };
707 
708 struct dsa_notifier_register_info {
709 	struct dsa_notifier_info info;	/* must be first */
710 	struct net_device *master;
711 	unsigned int port_number;
712 	unsigned int switch_number;
713 };
714 
715 static inline struct net_device *
716 dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
717 {
718 	return info->dev;
719 }
720 
721 #if IS_ENABLED(CONFIG_NET_DSA)
722 int register_dsa_notifier(struct notifier_block *nb);
723 int unregister_dsa_notifier(struct notifier_block *nb);
724 int call_dsa_notifiers(unsigned long val, struct net_device *dev,
725 		       struct dsa_notifier_info *info);
726 #else
727 static inline int register_dsa_notifier(struct notifier_block *nb)
728 {
729 	return 0;
730 }
731 
732 static inline int unregister_dsa_notifier(struct notifier_block *nb)
733 {
734 	return 0;
735 }
736 
737 static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
738 				     struct dsa_notifier_info *info)
739 {
740 	return NOTIFY_DONE;
741 }
742 #endif
743 
744 /* Broadcom tag specific helpers to insert and extract queue/port number */
745 #define BRCM_TAG_SET_PORT_QUEUE(p, q)	((p) << 8 | q)
746 #define BRCM_TAG_GET_PORT(v)		((v) >> 8)
747 #define BRCM_TAG_GET_QUEUE(v)		((v) & 0xff)
748 
749 
750 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
751 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
752 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
753 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
754 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
755 
756 struct dsa_tag_driver {
757 	const struct dsa_device_ops *ops;
758 	struct list_head list;
759 	struct module *owner;
760 };
761 
762 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
763 			      unsigned int count,
764 			      struct module *owner);
765 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
766 				unsigned int count);
767 
768 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
769 static int __init dsa_tag_driver_module_init(void)			\
770 {									\
771 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
772 				 THIS_MODULE);				\
773 	return 0;							\
774 }									\
775 module_init(dsa_tag_driver_module_init);				\
776 									\
777 static void __exit dsa_tag_driver_module_exit(void)			\
778 {									\
779 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
780 }									\
781 module_exit(dsa_tag_driver_module_exit)
782 
783 /**
784  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
785  * drivers
786  * @__ops_array: Array of tag driver strucutres
787  *
788  * Helper macro for DSA tag drivers which do not do anything special
789  * in module init/exit. Each module may only use this macro once, and
790  * calling it replaces module_init() and module_exit().
791  */
792 #define module_dsa_tag_drivers(__ops_array)				\
793 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
794 
795 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
796 
797 /* Create a static structure we can build a linked list of dsa_tag
798  * drivers
799  */
800 #define DSA_TAG_DRIVER(__ops)						\
801 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
802 	.ops = &__ops,							\
803 }
804 
805 /**
806  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
807  * driver
808  * @__ops: Single tag driver structures
809  *
810  * Helper macro for DSA tag drivers which do not do anything special
811  * in module init/exit. Each module may only use this macro once, and
812  * calling it replaces module_init() and module_exit().
813  */
814 #define module_dsa_tag_driver(__ops)					\
815 DSA_TAG_DRIVER(__ops);							\
816 									\
817 static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
818 	&DSA_TAG_DRIVER_NAME(__ops)					\
819 };									\
820 module_dsa_tag_drivers(dsa_tag_driver_array)
821 #endif
822 
823