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