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