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