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