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 #define DSA_TAG_PROTO_SEVILLE_VALUE		21
52 #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE		22
53 #define DSA_TAG_PROTO_SJA1110_VALUE		23
54 #define DSA_TAG_PROTO_RTL8_4_VALUE		24
55 #define DSA_TAG_PROTO_RTL8_4T_VALUE		25
56 #define DSA_TAG_PROTO_RZN1_A5PSW_VALUE		26
57 #define DSA_TAG_PROTO_LAN937X_VALUE		27
58 
59 enum dsa_tag_protocol {
60 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
61 	DSA_TAG_PROTO_BRCM		= DSA_TAG_PROTO_BRCM_VALUE,
62 	DSA_TAG_PROTO_BRCM_LEGACY	= DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
63 	DSA_TAG_PROTO_BRCM_PREPEND	= DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
64 	DSA_TAG_PROTO_DSA		= DSA_TAG_PROTO_DSA_VALUE,
65 	DSA_TAG_PROTO_EDSA		= DSA_TAG_PROTO_EDSA_VALUE,
66 	DSA_TAG_PROTO_GSWIP		= DSA_TAG_PROTO_GSWIP_VALUE,
67 	DSA_TAG_PROTO_KSZ9477		= DSA_TAG_PROTO_KSZ9477_VALUE,
68 	DSA_TAG_PROTO_KSZ9893		= DSA_TAG_PROTO_KSZ9893_VALUE,
69 	DSA_TAG_PROTO_LAN9303		= DSA_TAG_PROTO_LAN9303_VALUE,
70 	DSA_TAG_PROTO_MTK		= DSA_TAG_PROTO_MTK_VALUE,
71 	DSA_TAG_PROTO_QCA		= DSA_TAG_PROTO_QCA_VALUE,
72 	DSA_TAG_PROTO_TRAILER		= DSA_TAG_PROTO_TRAILER_VALUE,
73 	DSA_TAG_PROTO_8021Q		= DSA_TAG_PROTO_8021Q_VALUE,
74 	DSA_TAG_PROTO_SJA1105		= DSA_TAG_PROTO_SJA1105_VALUE,
75 	DSA_TAG_PROTO_KSZ8795		= DSA_TAG_PROTO_KSZ8795_VALUE,
76 	DSA_TAG_PROTO_OCELOT		= DSA_TAG_PROTO_OCELOT_VALUE,
77 	DSA_TAG_PROTO_AR9331		= DSA_TAG_PROTO_AR9331_VALUE,
78 	DSA_TAG_PROTO_RTL4_A		= DSA_TAG_PROTO_RTL4_A_VALUE,
79 	DSA_TAG_PROTO_HELLCREEK		= DSA_TAG_PROTO_HELLCREEK_VALUE,
80 	DSA_TAG_PROTO_XRS700X		= DSA_TAG_PROTO_XRS700X_VALUE,
81 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
82 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
83 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
84 	DSA_TAG_PROTO_RTL8_4		= DSA_TAG_PROTO_RTL8_4_VALUE,
85 	DSA_TAG_PROTO_RTL8_4T		= DSA_TAG_PROTO_RTL8_4T_VALUE,
86 	DSA_TAG_PROTO_RZN1_A5PSW	= DSA_TAG_PROTO_RZN1_A5PSW_VALUE,
87 	DSA_TAG_PROTO_LAN937X		= DSA_TAG_PROTO_LAN937X_VALUE,
88 };
89 
90 struct dsa_switch;
91 
92 struct dsa_device_ops {
93 	struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
94 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
95 	void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
96 			     int *offset);
97 	int (*connect)(struct dsa_switch *ds);
98 	void (*disconnect)(struct dsa_switch *ds);
99 	unsigned int needed_headroom;
100 	unsigned int needed_tailroom;
101 	const char *name;
102 	enum dsa_tag_protocol proto;
103 	/* Some tagging protocols either mangle or shift the destination MAC
104 	 * address, in which case the DSA master would drop packets on ingress
105 	 * if what it understands out of the destination MAC address is not in
106 	 * its RX filter.
107 	 */
108 	bool promisc_on_master;
109 };
110 
111 /* This structure defines the control interfaces that are overlayed by the
112  * DSA layer on top of the DSA CPU/management net_device instance. This is
113  * used by the core net_device layer while calling various net_device_ops
114  * function pointers.
115  */
116 struct dsa_netdevice_ops {
117 	int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr,
118 			     int cmd);
119 };
120 
121 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
122 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)				\
123 	MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
124 
125 struct dsa_lag {
126 	struct net_device *dev;
127 	unsigned int id;
128 	struct mutex fdb_lock;
129 	struct list_head fdbs;
130 	refcount_t refcount;
131 };
132 
133 struct dsa_switch_tree {
134 	struct list_head	list;
135 
136 	/* List of switch ports */
137 	struct list_head ports;
138 
139 	/* Notifier chain for switch-wide events */
140 	struct raw_notifier_head	nh;
141 
142 	/* Tree identifier */
143 	unsigned int index;
144 
145 	/* Number of switches attached to this tree */
146 	struct kref refcount;
147 
148 	/* Maps offloaded LAG netdevs to a zero-based linear ID for
149 	 * drivers that need it.
150 	 */
151 	struct dsa_lag **lags;
152 
153 	/* Tagging protocol operations */
154 	const struct dsa_device_ops *tag_ops;
155 
156 	/* Default tagging protocol preferred by the switches in this
157 	 * tree.
158 	 */
159 	enum dsa_tag_protocol default_proto;
160 
161 	/* Has this tree been applied to the hardware? */
162 	bool setup;
163 
164 	/*
165 	 * Configuration data for the platform device that owns
166 	 * this dsa switch tree instance.
167 	 */
168 	struct dsa_platform_data	*pd;
169 
170 	/* List of DSA links composing the routing table */
171 	struct list_head rtable;
172 
173 	/* Length of "lags" array */
174 	unsigned int lags_len;
175 
176 	/* Track the largest switch index within a tree */
177 	unsigned int last_switch;
178 };
179 
180 /* LAG IDs are one-based, the dst->lags array is zero-based */
181 #define dsa_lags_foreach_id(_id, _dst)				\
182 	for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++)	\
183 		if ((_dst)->lags[(_id) - 1])
184 
185 #define dsa_lag_foreach_port(_dp, _dst, _lag)			\
186 	list_for_each_entry((_dp), &(_dst)->ports, list)	\
187 		if (dsa_port_offloads_lag((_dp), (_lag)))
188 
189 #define dsa_hsr_foreach_port(_dp, _ds, _hsr)			\
190 	list_for_each_entry((_dp), &(_ds)->dst->ports, list)	\
191 		if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
192 
dsa_lag_by_id(struct dsa_switch_tree * dst,unsigned int id)193 static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
194 					    unsigned int id)
195 {
196 	/* DSA LAG IDs are one-based, dst->lags is zero-based */
197 	return dst->lags[id - 1];
198 }
199 
dsa_lag_id(struct dsa_switch_tree * dst,struct net_device * lag_dev)200 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
201 			     struct net_device *lag_dev)
202 {
203 	unsigned int id;
204 
205 	dsa_lags_foreach_id(id, dst) {
206 		struct dsa_lag *lag = dsa_lag_by_id(dst, id);
207 
208 		if (lag->dev == lag_dev)
209 			return lag->id;
210 	}
211 
212 	return -ENODEV;
213 }
214 
215 /* TC matchall action types */
216 enum dsa_port_mall_action_type {
217 	DSA_PORT_MALL_MIRROR,
218 	DSA_PORT_MALL_POLICER,
219 };
220 
221 /* TC mirroring entry */
222 struct dsa_mall_mirror_tc_entry {
223 	u8 to_local_port;
224 	bool ingress;
225 };
226 
227 /* TC port policer entry */
228 struct dsa_mall_policer_tc_entry {
229 	u32 burst;
230 	u64 rate_bytes_per_sec;
231 };
232 
233 /* TC matchall entry */
234 struct dsa_mall_tc_entry {
235 	struct list_head list;
236 	unsigned long cookie;
237 	enum dsa_port_mall_action_type type;
238 	union {
239 		struct dsa_mall_mirror_tc_entry mirror;
240 		struct dsa_mall_policer_tc_entry policer;
241 	};
242 };
243 
244 struct dsa_bridge {
245 	struct net_device *dev;
246 	unsigned int num;
247 	bool tx_fwd_offload;
248 	refcount_t refcount;
249 };
250 
251 struct dsa_port {
252 	/* A CPU port is physically connected to a master device.
253 	 * A user port exposed to userspace has a slave device.
254 	 */
255 	union {
256 		struct net_device *master;
257 		struct net_device *slave;
258 	};
259 
260 	/* Copy of the tagging protocol operations, for quicker access
261 	 * in the data path. Valid only for the CPU ports.
262 	 */
263 	const struct dsa_device_ops *tag_ops;
264 
265 	/* Copies for faster access in master receive hot path */
266 	struct dsa_switch_tree *dst;
267 	struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
268 
269 	struct dsa_switch	*ds;
270 
271 	unsigned int		index;
272 
273 	enum {
274 		DSA_PORT_TYPE_UNUSED = 0,
275 		DSA_PORT_TYPE_CPU,
276 		DSA_PORT_TYPE_DSA,
277 		DSA_PORT_TYPE_USER,
278 	} type;
279 
280 	const char		*name;
281 	struct dsa_port		*cpu_dp;
282 	u8			mac[ETH_ALEN];
283 
284 	u8			stp_state;
285 
286 	/* Warning: the following bit fields are not atomic, and updating them
287 	 * can only be done from code paths where concurrency is not possible
288 	 * (probe time or under rtnl_lock).
289 	 */
290 	u8			vlan_filtering:1;
291 
292 	/* Managed by DSA on user ports and by drivers on CPU and DSA ports */
293 	u8			learning:1;
294 
295 	u8			lag_tx_enabled:1;
296 
297 	/* Master state bits, valid only on CPU ports */
298 	u8			master_admin_up:1;
299 	u8			master_oper_up:1;
300 
301 	/* Valid only on user ports */
302 	u8			cpu_port_in_lag:1;
303 
304 	u8			setup:1;
305 
306 	struct device_node	*dn;
307 	unsigned int		ageing_time;
308 
309 	struct dsa_bridge	*bridge;
310 	struct devlink_port	devlink_port;
311 	struct phylink		*pl;
312 	struct phylink_config	pl_config;
313 	struct dsa_lag		*lag;
314 	struct net_device	*hsr_dev;
315 
316 	struct list_head list;
317 
318 	/*
319 	 * Original copy of the master netdev ethtool_ops
320 	 */
321 	const struct ethtool_ops *orig_ethtool_ops;
322 
323 	/*
324 	 * Original copy of the master netdev net_device_ops
325 	 */
326 	const struct dsa_netdevice_ops *netdev_ops;
327 
328 	/* List of MAC addresses that must be forwarded on this port.
329 	 * These are only valid on CPU ports and DSA links.
330 	 */
331 	struct mutex		addr_lists_lock;
332 	struct list_head	fdbs;
333 	struct list_head	mdbs;
334 
335 	/* List of VLANs that CPU and DSA ports are members of. */
336 	struct mutex		vlans_lock;
337 	struct list_head	vlans;
338 };
339 
340 /* TODO: ideally DSA ports would have a single dp->link_dp member,
341  * and no dst->rtable nor this struct dsa_link would be needed,
342  * but this would require some more complex tree walking,
343  * so keep it stupid at the moment and list them all.
344  */
345 struct dsa_link {
346 	struct dsa_port *dp;
347 	struct dsa_port *link_dp;
348 	struct list_head list;
349 };
350 
351 enum dsa_db_type {
352 	DSA_DB_PORT,
353 	DSA_DB_LAG,
354 	DSA_DB_BRIDGE,
355 };
356 
357 struct dsa_db {
358 	enum dsa_db_type type;
359 
360 	union {
361 		const struct dsa_port *dp;
362 		struct dsa_lag lag;
363 		struct dsa_bridge bridge;
364 	};
365 };
366 
367 struct dsa_mac_addr {
368 	unsigned char addr[ETH_ALEN];
369 	u16 vid;
370 	refcount_t refcount;
371 	struct list_head list;
372 	struct dsa_db db;
373 };
374 
375 struct dsa_vlan {
376 	u16 vid;
377 	refcount_t refcount;
378 	struct list_head list;
379 };
380 
381 struct dsa_switch {
382 	struct device *dev;
383 
384 	/*
385 	 * Parent switch tree, and switch index.
386 	 */
387 	struct dsa_switch_tree	*dst;
388 	unsigned int		index;
389 
390 	/* Warning: the following bit fields are not atomic, and updating them
391 	 * can only be done from code paths where concurrency is not possible
392 	 * (probe time or under rtnl_lock).
393 	 */
394 	u32			setup:1;
395 
396 	/* Disallow bridge core from requesting different VLAN awareness
397 	 * settings on ports if not hardware-supported
398 	 */
399 	u32			vlan_filtering_is_global:1;
400 
401 	/* Keep VLAN filtering enabled on ports not offloading any upper */
402 	u32			needs_standalone_vlan_filtering:1;
403 
404 	/* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
405 	 * that have vlan_filtering=0. All drivers should ideally set this (and
406 	 * then the option would get removed), but it is unknown whether this
407 	 * would break things or not.
408 	 */
409 	u32			configure_vlan_while_not_filtering:1;
410 
411 	/* If the switch driver always programs the CPU port as egress tagged
412 	 * despite the VLAN configuration indicating otherwise, then setting
413 	 * @untag_bridge_pvid will force the DSA receive path to pop the
414 	 * bridge's default_pvid VLAN tagged frames to offer a consistent
415 	 * behavior between a vlan_filtering=0 and vlan_filtering=1 bridge
416 	 * device.
417 	 */
418 	u32			untag_bridge_pvid:1;
419 
420 	/* Let DSA manage the FDB entries towards the
421 	 * CPU, based on the software bridge database.
422 	 */
423 	u32			assisted_learning_on_cpu_port:1;
424 
425 	/* In case vlan_filtering_is_global is set, the VLAN awareness state
426 	 * should be retrieved from here and not from the per-port settings.
427 	 */
428 	u32			vlan_filtering:1;
429 
430 	/* For switches that only have the MRU configurable. To ensure the
431 	 * configured MTU is not exceeded, normalization of MRU on all bridged
432 	 * interfaces is needed.
433 	 */
434 	u32			mtu_enforcement_ingress:1;
435 
436 	/* Drivers that isolate the FDBs of multiple bridges must set this
437 	 * to true to receive the bridge as an argument in .port_fdb_{add,del}
438 	 * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
439 	 * passed as zero.
440 	 */
441 	u32			fdb_isolation:1;
442 
443 	/* Listener for switch fabric events */
444 	struct notifier_block	nb;
445 
446 	/*
447 	 * Give the switch driver somewhere to hang its private data
448 	 * structure.
449 	 */
450 	void *priv;
451 
452 	void *tagger_data;
453 
454 	/*
455 	 * Configuration data for this switch.
456 	 */
457 	struct dsa_chip_data	*cd;
458 
459 	/*
460 	 * The switch operations.
461 	 */
462 	const struct dsa_switch_ops	*ops;
463 
464 	/*
465 	 * Slave mii_bus and devices for the individual ports.
466 	 */
467 	u32			phys_mii_mask;
468 	struct mii_bus		*slave_mii_bus;
469 
470 	/* Ageing Time limits in msecs */
471 	unsigned int ageing_time_min;
472 	unsigned int ageing_time_max;
473 
474 	/* Storage for drivers using tag_8021q */
475 	struct dsa_8021q_context *tag_8021q_ctx;
476 
477 	/* devlink used to represent this switch device */
478 	struct devlink		*devlink;
479 
480 	/* Number of switch port queues */
481 	unsigned int		num_tx_queues;
482 
483 	/* Drivers that benefit from having an ID associated with each
484 	 * offloaded LAG should set this to the maximum number of
485 	 * supported IDs. DSA will then maintain a mapping of _at
486 	 * least_ these many IDs, accessible to drivers via
487 	 * dsa_lag_id().
488 	 */
489 	unsigned int		num_lag_ids;
490 
491 	/* Drivers that support bridge forwarding offload or FDB isolation
492 	 * should set this to the maximum number of bridges spanning the same
493 	 * switch tree (or all trees, in the case of cross-tree bridging
494 	 * support) that can be offloaded.
495 	 */
496 	unsigned int		max_num_bridges;
497 
498 	unsigned int		num_ports;
499 };
500 
dsa_to_port(struct dsa_switch * ds,int p)501 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
502 {
503 	struct dsa_switch_tree *dst = ds->dst;
504 	struct dsa_port *dp;
505 
506 	list_for_each_entry(dp, &dst->ports, list)
507 		if (dp->ds == ds && dp->index == p)
508 			return dp;
509 
510 	return NULL;
511 }
512 
dsa_port_is_dsa(struct dsa_port * port)513 static inline bool dsa_port_is_dsa(struct dsa_port *port)
514 {
515 	return port->type == DSA_PORT_TYPE_DSA;
516 }
517 
dsa_port_is_cpu(struct dsa_port * port)518 static inline bool dsa_port_is_cpu(struct dsa_port *port)
519 {
520 	return port->type == DSA_PORT_TYPE_CPU;
521 }
522 
dsa_port_is_user(struct dsa_port * dp)523 static inline bool dsa_port_is_user(struct dsa_port *dp)
524 {
525 	return dp->type == DSA_PORT_TYPE_USER;
526 }
527 
dsa_port_is_unused(struct dsa_port * dp)528 static inline bool dsa_port_is_unused(struct dsa_port *dp)
529 {
530 	return dp->type == DSA_PORT_TYPE_UNUSED;
531 }
532 
dsa_port_master_is_operational(struct dsa_port * dp)533 static inline bool dsa_port_master_is_operational(struct dsa_port *dp)
534 {
535 	return dsa_port_is_cpu(dp) && dp->master_admin_up &&
536 	       dp->master_oper_up;
537 }
538 
dsa_is_unused_port(struct dsa_switch * ds,int p)539 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
540 {
541 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
542 }
543 
dsa_is_cpu_port(struct dsa_switch * ds,int p)544 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
545 {
546 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
547 }
548 
dsa_is_dsa_port(struct dsa_switch * ds,int p)549 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
550 {
551 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
552 }
553 
dsa_is_user_port(struct dsa_switch * ds,int p)554 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
555 {
556 	return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
557 }
558 
559 #define dsa_tree_for_each_user_port(_dp, _dst) \
560 	list_for_each_entry((_dp), &(_dst)->ports, list) \
561 		if (dsa_port_is_user((_dp)))
562 
563 #define dsa_tree_for_each_user_port_continue_reverse(_dp, _dst) \
564 	list_for_each_entry_continue_reverse((_dp), &(_dst)->ports, list) \
565 		if (dsa_port_is_user((_dp)))
566 
567 #define dsa_tree_for_each_cpu_port(_dp, _dst) \
568 	list_for_each_entry((_dp), &(_dst)->ports, list) \
569 		if (dsa_port_is_cpu((_dp)))
570 
571 #define dsa_switch_for_each_port(_dp, _ds) \
572 	list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
573 		if ((_dp)->ds == (_ds))
574 
575 #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
576 	list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
577 		if ((_dp)->ds == (_ds))
578 
579 #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
580 	list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
581 		if ((_dp)->ds == (_ds))
582 
583 #define dsa_switch_for_each_available_port(_dp, _ds) \
584 	dsa_switch_for_each_port((_dp), (_ds)) \
585 		if (!dsa_port_is_unused((_dp)))
586 
587 #define dsa_switch_for_each_user_port(_dp, _ds) \
588 	dsa_switch_for_each_port((_dp), (_ds)) \
589 		if (dsa_port_is_user((_dp)))
590 
591 #define dsa_switch_for_each_cpu_port(_dp, _ds) \
592 	dsa_switch_for_each_port((_dp), (_ds)) \
593 		if (dsa_port_is_cpu((_dp)))
594 
595 #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \
596 	dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
597 		if (dsa_port_is_cpu((_dp)))
598 
dsa_user_ports(struct dsa_switch * ds)599 static inline u32 dsa_user_ports(struct dsa_switch *ds)
600 {
601 	struct dsa_port *dp;
602 	u32 mask = 0;
603 
604 	dsa_switch_for_each_user_port(dp, ds)
605 		mask |= BIT(dp->index);
606 
607 	return mask;
608 }
609 
dsa_cpu_ports(struct dsa_switch * ds)610 static inline u32 dsa_cpu_ports(struct dsa_switch *ds)
611 {
612 	struct dsa_port *cpu_dp;
613 	u32 mask = 0;
614 
615 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
616 		mask |= BIT(cpu_dp->index);
617 
618 	return mask;
619 }
620 
621 /* Return the local port used to reach an arbitrary switch device */
dsa_routing_port(struct dsa_switch * ds,int device)622 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
623 {
624 	struct dsa_switch_tree *dst = ds->dst;
625 	struct dsa_link *dl;
626 
627 	list_for_each_entry(dl, &dst->rtable, list)
628 		if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
629 			return dl->dp->index;
630 
631 	return ds->num_ports;
632 }
633 
634 /* Return the local port used to reach an arbitrary switch port */
dsa_towards_port(struct dsa_switch * ds,int device,int port)635 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
636 					    int port)
637 {
638 	if (device == ds->index)
639 		return port;
640 	else
641 		return dsa_routing_port(ds, device);
642 }
643 
644 /* Return the local port used to reach the dedicated CPU port */
dsa_upstream_port(struct dsa_switch * ds,int port)645 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
646 {
647 	const struct dsa_port *dp = dsa_to_port(ds, port);
648 	const struct dsa_port *cpu_dp = dp->cpu_dp;
649 
650 	if (!cpu_dp)
651 		return port;
652 
653 	return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
654 }
655 
656 /* Return true if this is the local port used to reach the CPU port */
dsa_is_upstream_port(struct dsa_switch * ds,int port)657 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
658 {
659 	if (dsa_is_unused_port(ds, port))
660 		return false;
661 
662 	return port == dsa_upstream_port(ds, port);
663 }
664 
665 /* Return true if this is a DSA port leading away from the CPU */
dsa_is_downstream_port(struct dsa_switch * ds,int port)666 static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
667 {
668 	return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
669 }
670 
671 /* Return the local port used to reach the CPU port */
dsa_switch_upstream_port(struct dsa_switch * ds)672 static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
673 {
674 	struct dsa_port *dp;
675 
676 	dsa_switch_for_each_available_port(dp, ds) {
677 		return dsa_upstream_port(ds, dp->index);
678 	}
679 
680 	return ds->num_ports;
681 }
682 
683 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
684  * that the routing port from @downstream_ds to @upstream_ds is also the port
685  * which @downstream_ds uses to reach its dedicated CPU.
686  */
dsa_switch_is_upstream_of(struct dsa_switch * upstream_ds,struct dsa_switch * downstream_ds)687 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
688 					     struct dsa_switch *downstream_ds)
689 {
690 	int routing_port;
691 
692 	if (upstream_ds == downstream_ds)
693 		return true;
694 
695 	routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
696 
697 	return dsa_is_upstream_port(downstream_ds, routing_port);
698 }
699 
dsa_port_is_vlan_filtering(const struct dsa_port * dp)700 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
701 {
702 	const struct dsa_switch *ds = dp->ds;
703 
704 	if (ds->vlan_filtering_is_global)
705 		return ds->vlan_filtering;
706 	else
707 		return dp->vlan_filtering;
708 }
709 
dsa_port_lag_id_get(struct dsa_port * dp)710 static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
711 {
712 	return dp->lag ? dp->lag->id : 0;
713 }
714 
dsa_port_lag_dev_get(struct dsa_port * dp)715 static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
716 {
717 	return dp->lag ? dp->lag->dev : NULL;
718 }
719 
dsa_port_offloads_lag(struct dsa_port * dp,const struct dsa_lag * lag)720 static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
721 					 const struct dsa_lag *lag)
722 {
723 	return dsa_port_lag_dev_get(dp) == lag->dev;
724 }
725 
dsa_port_to_master(const struct dsa_port * dp)726 static inline struct net_device *dsa_port_to_master(const struct dsa_port *dp)
727 {
728 	if (dp->cpu_port_in_lag)
729 		return dsa_port_lag_dev_get(dp->cpu_dp);
730 
731 	return dp->cpu_dp->master;
732 }
733 
734 static inline
dsa_port_to_bridge_port(const struct dsa_port * dp)735 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
736 {
737 	if (!dp->bridge)
738 		return NULL;
739 
740 	if (dp->lag)
741 		return dp->lag->dev;
742 	else if (dp->hsr_dev)
743 		return dp->hsr_dev;
744 
745 	return dp->slave;
746 }
747 
748 static inline struct net_device *
dsa_port_bridge_dev_get(const struct dsa_port * dp)749 dsa_port_bridge_dev_get(const struct dsa_port *dp)
750 {
751 	return dp->bridge ? dp->bridge->dev : NULL;
752 }
753 
dsa_port_bridge_num_get(struct dsa_port * dp)754 static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
755 {
756 	return dp->bridge ? dp->bridge->num : 0;
757 }
758 
dsa_port_bridge_same(const struct dsa_port * a,const struct dsa_port * b)759 static inline bool dsa_port_bridge_same(const struct dsa_port *a,
760 					const struct dsa_port *b)
761 {
762 	struct net_device *br_a = dsa_port_bridge_dev_get(a);
763 	struct net_device *br_b = dsa_port_bridge_dev_get(b);
764 
765 	/* Standalone ports are not in the same bridge with one another */
766 	return (!br_a || !br_b) ? false : (br_a == br_b);
767 }
768 
dsa_port_offloads_bridge_port(struct dsa_port * dp,const struct net_device * dev)769 static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
770 						 const struct net_device *dev)
771 {
772 	return dsa_port_to_bridge_port(dp) == dev;
773 }
774 
775 static inline bool
dsa_port_offloads_bridge_dev(struct dsa_port * dp,const struct net_device * bridge_dev)776 dsa_port_offloads_bridge_dev(struct dsa_port *dp,
777 			     const struct net_device *bridge_dev)
778 {
779 	/* DSA ports connected to a bridge, and event was emitted
780 	 * for the bridge.
781 	 */
782 	return dsa_port_bridge_dev_get(dp) == bridge_dev;
783 }
784 
dsa_port_offloads_bridge(struct dsa_port * dp,const struct dsa_bridge * bridge)785 static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
786 					    const struct dsa_bridge *bridge)
787 {
788 	return dsa_port_bridge_dev_get(dp) == bridge->dev;
789 }
790 
791 /* Returns true if any port of this tree offloads the given net_device */
dsa_tree_offloads_bridge_port(struct dsa_switch_tree * dst,const struct net_device * dev)792 static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
793 						 const struct net_device *dev)
794 {
795 	struct dsa_port *dp;
796 
797 	list_for_each_entry(dp, &dst->ports, list)
798 		if (dsa_port_offloads_bridge_port(dp, dev))
799 			return true;
800 
801 	return false;
802 }
803 
804 /* Returns true if any port of this tree offloads the given bridge */
805 static inline bool
dsa_tree_offloads_bridge_dev(struct dsa_switch_tree * dst,const struct net_device * bridge_dev)806 dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
807 			     const struct net_device *bridge_dev)
808 {
809 	struct dsa_port *dp;
810 
811 	list_for_each_entry(dp, &dst->ports, list)
812 		if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
813 			return true;
814 
815 	return false;
816 }
817 
dsa_port_tree_same(const struct dsa_port * a,const struct dsa_port * b)818 static inline bool dsa_port_tree_same(const struct dsa_port *a,
819 				      const struct dsa_port *b)
820 {
821 	return a->ds->dst == b->ds->dst;
822 }
823 
824 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
825 			      bool is_static, void *data);
826 struct dsa_switch_ops {
827 	/*
828 	 * Tagging protocol helpers called for the CPU ports and DSA links.
829 	 * @get_tag_protocol retrieves the initial tagging protocol and is
830 	 * mandatory. Switches which can operate using multiple tagging
831 	 * protocols should implement @change_tag_protocol and report in
832 	 * @get_tag_protocol the tagger in current use.
833 	 */
834 	enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
835 						  int port,
836 						  enum dsa_tag_protocol mprot);
837 	int	(*change_tag_protocol)(struct dsa_switch *ds,
838 				       enum dsa_tag_protocol proto);
839 	/*
840 	 * Method for switch drivers to connect to the tagging protocol driver
841 	 * in current use. The switch driver can provide handlers for certain
842 	 * types of packets for switch management.
843 	 */
844 	int	(*connect_tag_protocol)(struct dsa_switch *ds,
845 					enum dsa_tag_protocol proto);
846 
847 	int	(*port_change_master)(struct dsa_switch *ds, int port,
848 				      struct net_device *master,
849 				      struct netlink_ext_ack *extack);
850 
851 	/* Optional switch-wide initialization and destruction methods */
852 	int	(*setup)(struct dsa_switch *ds);
853 	void	(*teardown)(struct dsa_switch *ds);
854 
855 	/* Per-port initialization and destruction methods. Mandatory if the
856 	 * driver registers devlink port regions, optional otherwise.
857 	 */
858 	int	(*port_setup)(struct dsa_switch *ds, int port);
859 	void	(*port_teardown)(struct dsa_switch *ds, int port);
860 
861 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
862 
863 	/*
864 	 * Access to the switch's PHY registers.
865 	 */
866 	int	(*phy_read)(struct dsa_switch *ds, int port, int regnum);
867 	int	(*phy_write)(struct dsa_switch *ds, int port,
868 			     int regnum, u16 val);
869 
870 	/*
871 	 * Link state adjustment (called from libphy)
872 	 */
873 	void	(*adjust_link)(struct dsa_switch *ds, int port,
874 				struct phy_device *phydev);
875 	void	(*fixed_link_update)(struct dsa_switch *ds, int port,
876 				struct fixed_phy_status *st);
877 
878 	/*
879 	 * PHYLINK integration
880 	 */
881 	void	(*phylink_get_caps)(struct dsa_switch *ds, int port,
882 				    struct phylink_config *config);
883 	void	(*phylink_validate)(struct dsa_switch *ds, int port,
884 				    unsigned long *supported,
885 				    struct phylink_link_state *state);
886 	struct phylink_pcs *(*phylink_mac_select_pcs)(struct dsa_switch *ds,
887 						      int port,
888 						      phy_interface_t iface);
889 	int	(*phylink_mac_link_state)(struct dsa_switch *ds, int port,
890 					  struct phylink_link_state *state);
891 	void	(*phylink_mac_config)(struct dsa_switch *ds, int port,
892 				      unsigned int mode,
893 				      const struct phylink_link_state *state);
894 	void	(*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
895 	void	(*phylink_mac_link_down)(struct dsa_switch *ds, int port,
896 					 unsigned int mode,
897 					 phy_interface_t interface);
898 	void	(*phylink_mac_link_up)(struct dsa_switch *ds, int port,
899 				       unsigned int mode,
900 				       phy_interface_t interface,
901 				       struct phy_device *phydev,
902 				       int speed, int duplex,
903 				       bool tx_pause, bool rx_pause);
904 	void	(*phylink_fixed_state)(struct dsa_switch *ds, int port,
905 				       struct phylink_link_state *state);
906 	/*
907 	 * Port statistics counters.
908 	 */
909 	void	(*get_strings)(struct dsa_switch *ds, int port,
910 			       u32 stringset, uint8_t *data);
911 	void	(*get_ethtool_stats)(struct dsa_switch *ds,
912 				     int port, uint64_t *data);
913 	int	(*get_sset_count)(struct dsa_switch *ds, int port, int sset);
914 	void	(*get_ethtool_phy_stats)(struct dsa_switch *ds,
915 					 int port, uint64_t *data);
916 	void	(*get_eth_phy_stats)(struct dsa_switch *ds, int port,
917 				     struct ethtool_eth_phy_stats *phy_stats);
918 	void	(*get_eth_mac_stats)(struct dsa_switch *ds, int port,
919 				     struct ethtool_eth_mac_stats *mac_stats);
920 	void	(*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
921 				      struct ethtool_eth_ctrl_stats *ctrl_stats);
922 	void	(*get_rmon_stats)(struct dsa_switch *ds, int port,
923 				  struct ethtool_rmon_stats *rmon_stats,
924 				  const struct ethtool_rmon_hist_range **ranges);
925 	void	(*get_stats64)(struct dsa_switch *ds, int port,
926 				   struct rtnl_link_stats64 *s);
927 	void	(*get_pause_stats)(struct dsa_switch *ds, int port,
928 				   struct ethtool_pause_stats *pause_stats);
929 	void	(*self_test)(struct dsa_switch *ds, int port,
930 			     struct ethtool_test *etest, u64 *data);
931 
932 	/*
933 	 * ethtool Wake-on-LAN
934 	 */
935 	void	(*get_wol)(struct dsa_switch *ds, int port,
936 			   struct ethtool_wolinfo *w);
937 	int	(*set_wol)(struct dsa_switch *ds, int port,
938 			   struct ethtool_wolinfo *w);
939 
940 	/*
941 	 * ethtool timestamp info
942 	 */
943 	int	(*get_ts_info)(struct dsa_switch *ds, int port,
944 			       struct ethtool_ts_info *ts);
945 
946 	/*
947 	 * DCB ops
948 	 */
949 	int	(*port_get_default_prio)(struct dsa_switch *ds, int port);
950 	int	(*port_set_default_prio)(struct dsa_switch *ds, int port,
951 					 u8 prio);
952 	int	(*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
953 	int	(*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
954 				      u8 prio);
955 	int	(*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
956 				      u8 prio);
957 
958 	/*
959 	 * Suspend and resume
960 	 */
961 	int	(*suspend)(struct dsa_switch *ds);
962 	int	(*resume)(struct dsa_switch *ds);
963 
964 	/*
965 	 * Port enable/disable
966 	 */
967 	int	(*port_enable)(struct dsa_switch *ds, int port,
968 			       struct phy_device *phy);
969 	void	(*port_disable)(struct dsa_switch *ds, int port);
970 
971 	/*
972 	 * Port's MAC EEE settings
973 	 */
974 	int	(*set_mac_eee)(struct dsa_switch *ds, int port,
975 			       struct ethtool_eee *e);
976 	int	(*get_mac_eee)(struct dsa_switch *ds, int port,
977 			       struct ethtool_eee *e);
978 
979 	/* EEPROM access */
980 	int	(*get_eeprom_len)(struct dsa_switch *ds);
981 	int	(*get_eeprom)(struct dsa_switch *ds,
982 			      struct ethtool_eeprom *eeprom, u8 *data);
983 	int	(*set_eeprom)(struct dsa_switch *ds,
984 			      struct ethtool_eeprom *eeprom, u8 *data);
985 
986 	/*
987 	 * Register access.
988 	 */
989 	int	(*get_regs_len)(struct dsa_switch *ds, int port);
990 	void	(*get_regs)(struct dsa_switch *ds, int port,
991 			    struct ethtool_regs *regs, void *p);
992 
993 	/*
994 	 * Upper device tracking.
995 	 */
996 	int	(*port_prechangeupper)(struct dsa_switch *ds, int port,
997 				       struct netdev_notifier_changeupper_info *info);
998 
999 	/*
1000 	 * Bridge integration
1001 	 */
1002 	int	(*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
1003 	int	(*port_bridge_join)(struct dsa_switch *ds, int port,
1004 				    struct dsa_bridge bridge,
1005 				    bool *tx_fwd_offload,
1006 				    struct netlink_ext_ack *extack);
1007 	void	(*port_bridge_leave)(struct dsa_switch *ds, int port,
1008 				     struct dsa_bridge bridge);
1009 	void	(*port_stp_state_set)(struct dsa_switch *ds, int port,
1010 				      u8 state);
1011 	int	(*port_mst_state_set)(struct dsa_switch *ds, int port,
1012 				      const struct switchdev_mst_state *state);
1013 	void	(*port_fast_age)(struct dsa_switch *ds, int port);
1014 	int	(*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
1015 	int	(*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
1016 					 struct switchdev_brport_flags flags,
1017 					 struct netlink_ext_ack *extack);
1018 	int	(*port_bridge_flags)(struct dsa_switch *ds, int port,
1019 				     struct switchdev_brport_flags flags,
1020 				     struct netlink_ext_ack *extack);
1021 	void	(*port_set_host_flood)(struct dsa_switch *ds, int port,
1022 				       bool uc, bool mc);
1023 
1024 	/*
1025 	 * VLAN support
1026 	 */
1027 	int	(*port_vlan_filtering)(struct dsa_switch *ds, int port,
1028 				       bool vlan_filtering,
1029 				       struct netlink_ext_ack *extack);
1030 	int	(*port_vlan_add)(struct dsa_switch *ds, int port,
1031 				 const struct switchdev_obj_port_vlan *vlan,
1032 				 struct netlink_ext_ack *extack);
1033 	int	(*port_vlan_del)(struct dsa_switch *ds, int port,
1034 				 const struct switchdev_obj_port_vlan *vlan);
1035 	int	(*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge,
1036 				 const struct switchdev_vlan_msti *msti);
1037 
1038 	/*
1039 	 * Forwarding database
1040 	 */
1041 	int	(*port_fdb_add)(struct dsa_switch *ds, int port,
1042 				const unsigned char *addr, u16 vid,
1043 				struct dsa_db db);
1044 	int	(*port_fdb_del)(struct dsa_switch *ds, int port,
1045 				const unsigned char *addr, u16 vid,
1046 				struct dsa_db db);
1047 	int	(*port_fdb_dump)(struct dsa_switch *ds, int port,
1048 				 dsa_fdb_dump_cb_t *cb, void *data);
1049 	int	(*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
1050 			       const unsigned char *addr, u16 vid,
1051 			       struct dsa_db db);
1052 	int	(*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
1053 			       const unsigned char *addr, u16 vid,
1054 			       struct dsa_db db);
1055 
1056 	/*
1057 	 * Multicast database
1058 	 */
1059 	int	(*port_mdb_add)(struct dsa_switch *ds, int port,
1060 				const struct switchdev_obj_port_mdb *mdb,
1061 				struct dsa_db db);
1062 	int	(*port_mdb_del)(struct dsa_switch *ds, int port,
1063 				const struct switchdev_obj_port_mdb *mdb,
1064 				struct dsa_db db);
1065 	/*
1066 	 * RXNFC
1067 	 */
1068 	int	(*get_rxnfc)(struct dsa_switch *ds, int port,
1069 			     struct ethtool_rxnfc *nfc, u32 *rule_locs);
1070 	int	(*set_rxnfc)(struct dsa_switch *ds, int port,
1071 			     struct ethtool_rxnfc *nfc);
1072 
1073 	/*
1074 	 * TC integration
1075 	 */
1076 	int	(*cls_flower_add)(struct dsa_switch *ds, int port,
1077 				  struct flow_cls_offload *cls, bool ingress);
1078 	int	(*cls_flower_del)(struct dsa_switch *ds, int port,
1079 				  struct flow_cls_offload *cls, bool ingress);
1080 	int	(*cls_flower_stats)(struct dsa_switch *ds, int port,
1081 				    struct flow_cls_offload *cls, bool ingress);
1082 	int	(*port_mirror_add)(struct dsa_switch *ds, int port,
1083 				   struct dsa_mall_mirror_tc_entry *mirror,
1084 				   bool ingress, struct netlink_ext_ack *extack);
1085 	void	(*port_mirror_del)(struct dsa_switch *ds, int port,
1086 				   struct dsa_mall_mirror_tc_entry *mirror);
1087 	int	(*port_policer_add)(struct dsa_switch *ds, int port,
1088 				    struct dsa_mall_policer_tc_entry *policer);
1089 	void	(*port_policer_del)(struct dsa_switch *ds, int port);
1090 	int	(*port_setup_tc)(struct dsa_switch *ds, int port,
1091 				 enum tc_setup_type type, void *type_data);
1092 
1093 	/*
1094 	 * Cross-chip operations
1095 	 */
1096 	int	(*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
1097 					 int sw_index, int port,
1098 					 struct dsa_bridge bridge,
1099 					 struct netlink_ext_ack *extack);
1100 	void	(*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
1101 					  int sw_index, int port,
1102 					  struct dsa_bridge bridge);
1103 	int	(*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
1104 					int port);
1105 	int	(*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
1106 				      int port, struct dsa_lag lag,
1107 				      struct netdev_lag_upper_info *info,
1108 				      struct netlink_ext_ack *extack);
1109 	int	(*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
1110 				       int port, struct dsa_lag lag);
1111 
1112 	/*
1113 	 * PTP functionality
1114 	 */
1115 	int	(*port_hwtstamp_get)(struct dsa_switch *ds, int port,
1116 				     struct ifreq *ifr);
1117 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
1118 				     struct ifreq *ifr);
1119 	void	(*port_txtstamp)(struct dsa_switch *ds, int port,
1120 				 struct sk_buff *skb);
1121 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
1122 				 struct sk_buff *skb, unsigned int type);
1123 
1124 	/* Devlink parameters, etc */
1125 	int	(*devlink_param_get)(struct dsa_switch *ds, u32 id,
1126 				     struct devlink_param_gset_ctx *ctx);
1127 	int	(*devlink_param_set)(struct dsa_switch *ds, u32 id,
1128 				     struct devlink_param_gset_ctx *ctx);
1129 	int	(*devlink_info_get)(struct dsa_switch *ds,
1130 				    struct devlink_info_req *req,
1131 				    struct netlink_ext_ack *extack);
1132 	int	(*devlink_sb_pool_get)(struct dsa_switch *ds,
1133 				       unsigned int sb_index, u16 pool_index,
1134 				       struct devlink_sb_pool_info *pool_info);
1135 	int	(*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
1136 				       u16 pool_index, u32 size,
1137 				       enum devlink_sb_threshold_type threshold_type,
1138 				       struct netlink_ext_ack *extack);
1139 	int	(*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
1140 					    unsigned int sb_index, u16 pool_index,
1141 					    u32 *p_threshold);
1142 	int	(*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
1143 					    unsigned int sb_index, u16 pool_index,
1144 					    u32 threshold,
1145 					    struct netlink_ext_ack *extack);
1146 	int	(*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
1147 					       unsigned int sb_index, u16 tc_index,
1148 					       enum devlink_sb_pool_type pool_type,
1149 					       u16 *p_pool_index, u32 *p_threshold);
1150 	int	(*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
1151 					       unsigned int sb_index, u16 tc_index,
1152 					       enum devlink_sb_pool_type pool_type,
1153 					       u16 pool_index, u32 threshold,
1154 					       struct netlink_ext_ack *extack);
1155 	int	(*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
1156 					   unsigned int sb_index);
1157 	int	(*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
1158 					    unsigned int sb_index);
1159 	int	(*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
1160 						unsigned int sb_index, u16 pool_index,
1161 						u32 *p_cur, u32 *p_max);
1162 	int	(*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
1163 						   unsigned int sb_index, u16 tc_index,
1164 						   enum devlink_sb_pool_type pool_type,
1165 						   u32 *p_cur, u32 *p_max);
1166 
1167 	/*
1168 	 * MTU change functionality. Switches can also adjust their MRU through
1169 	 * this method. By MTU, one understands the SDU (L2 payload) length.
1170 	 * If the switch needs to account for the DSA tag on the CPU port, this
1171 	 * method needs to do so privately.
1172 	 */
1173 	int	(*port_change_mtu)(struct dsa_switch *ds, int port,
1174 				   int new_mtu);
1175 	int	(*port_max_mtu)(struct dsa_switch *ds, int port);
1176 
1177 	/*
1178 	 * LAG integration
1179 	 */
1180 	int	(*port_lag_change)(struct dsa_switch *ds, int port);
1181 	int	(*port_lag_join)(struct dsa_switch *ds, int port,
1182 				 struct dsa_lag lag,
1183 				 struct netdev_lag_upper_info *info,
1184 				 struct netlink_ext_ack *extack);
1185 	int	(*port_lag_leave)(struct dsa_switch *ds, int port,
1186 				  struct dsa_lag lag);
1187 
1188 	/*
1189 	 * HSR integration
1190 	 */
1191 	int	(*port_hsr_join)(struct dsa_switch *ds, int port,
1192 				 struct net_device *hsr);
1193 	int	(*port_hsr_leave)(struct dsa_switch *ds, int port,
1194 				  struct net_device *hsr);
1195 
1196 	/*
1197 	 * MRP integration
1198 	 */
1199 	int	(*port_mrp_add)(struct dsa_switch *ds, int port,
1200 				const struct switchdev_obj_mrp *mrp);
1201 	int	(*port_mrp_del)(struct dsa_switch *ds, int port,
1202 				const struct switchdev_obj_mrp *mrp);
1203 	int	(*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
1204 					  const struct switchdev_obj_ring_role_mrp *mrp);
1205 	int	(*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
1206 					  const struct switchdev_obj_ring_role_mrp *mrp);
1207 
1208 	/*
1209 	 * tag_8021q operations
1210 	 */
1211 	int	(*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
1212 				      u16 flags);
1213 	int	(*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
1214 
1215 	/*
1216 	 * DSA master tracking operations
1217 	 */
1218 	void	(*master_state_change)(struct dsa_switch *ds,
1219 				       const struct net_device *master,
1220 				       bool operational);
1221 };
1222 
1223 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)		\
1224 	DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,		\
1225 			     dsa_devlink_param_get, dsa_devlink_param_set, NULL)
1226 
1227 int dsa_devlink_param_get(struct devlink *dl, u32 id,
1228 			  struct devlink_param_gset_ctx *ctx);
1229 int dsa_devlink_param_set(struct devlink *dl, u32 id,
1230 			  struct devlink_param_gset_ctx *ctx);
1231 int dsa_devlink_params_register(struct dsa_switch *ds,
1232 				const struct devlink_param *params,
1233 				size_t params_count);
1234 void dsa_devlink_params_unregister(struct dsa_switch *ds,
1235 				   const struct devlink_param *params,
1236 				   size_t params_count);
1237 int dsa_devlink_resource_register(struct dsa_switch *ds,
1238 				  const char *resource_name,
1239 				  u64 resource_size,
1240 				  u64 resource_id,
1241 				  u64 parent_resource_id,
1242 				  const struct devlink_resource_size_params *size_params);
1243 
1244 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
1245 
1246 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
1247 					   u64 resource_id,
1248 					   devlink_resource_occ_get_t *occ_get,
1249 					   void *occ_get_priv);
1250 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
1251 					     u64 resource_id);
1252 struct devlink_region *
1253 dsa_devlink_region_create(struct dsa_switch *ds,
1254 			  const struct devlink_region_ops *ops,
1255 			  u32 region_max_snapshots, u64 region_size);
1256 struct devlink_region *
1257 dsa_devlink_port_region_create(struct dsa_switch *ds,
1258 			       int port,
1259 			       const struct devlink_port_region_ops *ops,
1260 			       u32 region_max_snapshots, u64 region_size);
1261 void dsa_devlink_region_destroy(struct devlink_region *region);
1262 
1263 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
1264 
1265 struct dsa_devlink_priv {
1266 	struct dsa_switch *ds;
1267 };
1268 
dsa_devlink_to_ds(struct devlink * dl)1269 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
1270 {
1271 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1272 
1273 	return dl_priv->ds;
1274 }
1275 
1276 static inline
dsa_devlink_port_to_ds(struct devlink_port * port)1277 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
1278 {
1279 	struct devlink *dl = port->devlink;
1280 	struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
1281 
1282 	return dl_priv->ds;
1283 }
1284 
dsa_devlink_port_to_port(struct devlink_port * port)1285 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
1286 {
1287 	return port->index;
1288 }
1289 
1290 struct dsa_switch_driver {
1291 	struct list_head	list;
1292 	const struct dsa_switch_ops *ops;
1293 };
1294 
1295 struct net_device *dsa_dev_to_net_device(struct device *dev);
1296 
1297 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
1298 				 const unsigned char *addr, u16 vid,
1299 				 struct dsa_db db);
1300 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
1301 				 const struct switchdev_obj_port_mdb *mdb,
1302 				 struct dsa_db db);
1303 
1304 /* Keep inline for faster access in hot path */
netdev_uses_dsa(const struct net_device * dev)1305 static inline bool netdev_uses_dsa(const struct net_device *dev)
1306 {
1307 #if IS_ENABLED(CONFIG_NET_DSA)
1308 	return dev->dsa_ptr && dev->dsa_ptr->rcv;
1309 #endif
1310 	return false;
1311 }
1312 
1313 /* All DSA tags that push the EtherType to the right (basically all except tail
1314  * tags, which don't break dissection) can be treated the same from the
1315  * perspective of the flow dissector.
1316  *
1317  * We need to return:
1318  *  - offset: the (B - A) difference between:
1319  *    A. the position of the real EtherType and
1320  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
1321  *       after the normal EtherType was supposed to be)
1322  *    The offset in bytes is exactly equal to the tagger overhead (and half of
1323  *    that, in __be16 shorts).
1324  *
1325  *  - proto: the value of the real EtherType.
1326  */
dsa_tag_generic_flow_dissect(const struct sk_buff * skb,__be16 * proto,int * offset)1327 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
1328 						__be16 *proto, int *offset)
1329 {
1330 #if IS_ENABLED(CONFIG_NET_DSA)
1331 	const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
1332 	int tag_len = ops->needed_headroom;
1333 
1334 	*offset = tag_len;
1335 	*proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
1336 #endif
1337 }
1338 
1339 #if IS_ENABLED(CONFIG_NET_DSA)
__dsa_netdevice_ops_check(struct net_device * dev)1340 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
1341 {
1342 	int err = -EOPNOTSUPP;
1343 
1344 	if (!dev->dsa_ptr)
1345 		return err;
1346 
1347 	if (!dev->dsa_ptr->netdev_ops)
1348 		return err;
1349 
1350 	return 0;
1351 }
1352 
dsa_ndo_eth_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1353 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1354 				    int cmd)
1355 {
1356 	const struct dsa_netdevice_ops *ops;
1357 	int err;
1358 
1359 	err = __dsa_netdevice_ops_check(dev);
1360 	if (err)
1361 		return err;
1362 
1363 	ops = dev->dsa_ptr->netdev_ops;
1364 
1365 	return ops->ndo_eth_ioctl(dev, ifr, cmd);
1366 }
1367 #else
dsa_ndo_eth_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1368 static inline int dsa_ndo_eth_ioctl(struct net_device *dev, struct ifreq *ifr,
1369 				    int cmd)
1370 {
1371 	return -EOPNOTSUPP;
1372 }
1373 #endif
1374 
1375 void dsa_unregister_switch(struct dsa_switch *ds);
1376 int dsa_register_switch(struct dsa_switch *ds);
1377 void dsa_switch_shutdown(struct dsa_switch *ds);
1378 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1379 void dsa_flush_workqueue(void);
1380 #ifdef CONFIG_PM_SLEEP
1381 int dsa_switch_suspend(struct dsa_switch *ds);
1382 int dsa_switch_resume(struct dsa_switch *ds);
1383 #else
dsa_switch_suspend(struct dsa_switch * ds)1384 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1385 {
1386 	return 0;
1387 }
dsa_switch_resume(struct dsa_switch * ds)1388 static inline int dsa_switch_resume(struct dsa_switch *ds)
1389 {
1390 	return 0;
1391 }
1392 #endif /* CONFIG_PM_SLEEP */
1393 
1394 #if IS_ENABLED(CONFIG_NET_DSA)
1395 bool dsa_slave_dev_check(const struct net_device *dev);
1396 #else
dsa_slave_dev_check(const struct net_device * dev)1397 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1398 {
1399 	return false;
1400 }
1401 #endif
1402 
1403 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1404 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1405 
1406 struct dsa_tag_driver {
1407 	const struct dsa_device_ops *ops;
1408 	struct list_head list;
1409 	struct module *owner;
1410 };
1411 
1412 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1413 			      unsigned int count,
1414 			      struct module *owner);
1415 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1416 				unsigned int count);
1417 
1418 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
1419 static int __init dsa_tag_driver_module_init(void)			\
1420 {									\
1421 	dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,	\
1422 				 THIS_MODULE);				\
1423 	return 0;							\
1424 }									\
1425 module_init(dsa_tag_driver_module_init);				\
1426 									\
1427 static void __exit dsa_tag_driver_module_exit(void)			\
1428 {									\
1429 	dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);	\
1430 }									\
1431 module_exit(dsa_tag_driver_module_exit)
1432 
1433 /**
1434  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1435  * drivers
1436  * @__ops_array: Array of tag driver structures
1437  *
1438  * Helper macro for DSA tag drivers which do not do anything special
1439  * in module init/exit. Each module may only use this macro once, and
1440  * calling it replaces module_init() and module_exit().
1441  */
1442 #define module_dsa_tag_drivers(__ops_array)				\
1443 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1444 
1445 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1446 
1447 /* Create a static structure we can build a linked list of dsa_tag
1448  * drivers
1449  */
1450 #define DSA_TAG_DRIVER(__ops)						\
1451 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {		\
1452 	.ops = &__ops,							\
1453 }
1454 
1455 /**
1456  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1457  * driver
1458  * @__ops: Single tag driver structures
1459  *
1460  * Helper macro for DSA tag drivers which do not do anything special
1461  * in module init/exit. Each module may only use this macro once, and
1462  * calling it replaces module_init() and module_exit().
1463  */
1464 #define module_dsa_tag_driver(__ops)					\
1465 DSA_TAG_DRIVER(__ops);							\
1466 									\
1467 static struct dsa_tag_driver *dsa_tag_driver_array[] =	{		\
1468 	&DSA_TAG_DRIVER_NAME(__ops)					\
1469 };									\
1470 module_dsa_tag_drivers(dsa_tag_driver_array)
1471 #endif
1472 
1473