OSDN Git Service

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