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