net: dsa: reference count the MDB entries at the cross-chip notifier level
[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
55 enum dsa_tag_protocol {
56         DSA_TAG_PROTO_NONE              = DSA_TAG_PROTO_NONE_VALUE,
57         DSA_TAG_PROTO_BRCM              = DSA_TAG_PROTO_BRCM_VALUE,
58         DSA_TAG_PROTO_BRCM_LEGACY       = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
59         DSA_TAG_PROTO_BRCM_PREPEND      = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
60         DSA_TAG_PROTO_DSA               = DSA_TAG_PROTO_DSA_VALUE,
61         DSA_TAG_PROTO_EDSA              = DSA_TAG_PROTO_EDSA_VALUE,
62         DSA_TAG_PROTO_GSWIP             = DSA_TAG_PROTO_GSWIP_VALUE,
63         DSA_TAG_PROTO_KSZ9477           = DSA_TAG_PROTO_KSZ9477_VALUE,
64         DSA_TAG_PROTO_KSZ9893           = DSA_TAG_PROTO_KSZ9893_VALUE,
65         DSA_TAG_PROTO_LAN9303           = DSA_TAG_PROTO_LAN9303_VALUE,
66         DSA_TAG_PROTO_MTK               = DSA_TAG_PROTO_MTK_VALUE,
67         DSA_TAG_PROTO_QCA               = DSA_TAG_PROTO_QCA_VALUE,
68         DSA_TAG_PROTO_TRAILER           = DSA_TAG_PROTO_TRAILER_VALUE,
69         DSA_TAG_PROTO_8021Q             = DSA_TAG_PROTO_8021Q_VALUE,
70         DSA_TAG_PROTO_SJA1105           = DSA_TAG_PROTO_SJA1105_VALUE,
71         DSA_TAG_PROTO_KSZ8795           = DSA_TAG_PROTO_KSZ8795_VALUE,
72         DSA_TAG_PROTO_OCELOT            = DSA_TAG_PROTO_OCELOT_VALUE,
73         DSA_TAG_PROTO_AR9331            = DSA_TAG_PROTO_AR9331_VALUE,
74         DSA_TAG_PROTO_RTL4_A            = DSA_TAG_PROTO_RTL4_A_VALUE,
75         DSA_TAG_PROTO_HELLCREEK         = DSA_TAG_PROTO_HELLCREEK_VALUE,
76         DSA_TAG_PROTO_XRS700X           = DSA_TAG_PROTO_XRS700X_VALUE,
77         DSA_TAG_PROTO_OCELOT_8021Q      = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
78         DSA_TAG_PROTO_SEVILLE           = DSA_TAG_PROTO_SEVILLE_VALUE,
79         DSA_TAG_PROTO_SJA1110           = DSA_TAG_PROTO_SJA1110_VALUE,
80 };
81
82 struct packet_type;
83 struct dsa_switch;
84
85 struct dsa_device_ops {
86         struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
87         struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
88                                struct packet_type *pt);
89         void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
90                              int *offset);
91         /* Used to determine which traffic should match the DSA filter in
92          * eth_type_trans, and which, if any, should bypass it and be processed
93          * as regular on the master net device.
94          */
95         bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
96         unsigned int needed_headroom;
97         unsigned int needed_tailroom;
98         const char *name;
99         enum dsa_tag_protocol proto;
100         /* Some tagging protocols either mangle or shift the destination MAC
101          * address, in which case the DSA master would drop packets on ingress
102          * if what it understands out of the destination MAC address is not in
103          * its RX filter.
104          */
105         bool promisc_on_master;
106 };
107
108 /* This structure defines the control interfaces that are overlayed by the
109  * DSA layer on top of the DSA CPU/management net_device instance. This is
110  * used by the core net_device layer while calling various net_device_ops
111  * function pointers.
112  */
113 struct dsa_netdevice_ops {
114         int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr,
115                             int cmd);
116 };
117
118 #define DSA_TAG_DRIVER_ALIAS "dsa_tag-"
119 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto)                            \
120         MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __stringify(__proto##_VALUE))
121
122 struct dsa_switch_tree {
123         struct list_head        list;
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         /* Has this tree been applied to the hardware? */
135         bool setup;
136
137         /* Tagging protocol operations */
138         const struct dsa_device_ops *tag_ops;
139
140         /* Default tagging protocol preferred by the switches in this
141          * tree.
142          */
143         enum dsa_tag_protocol default_proto;
144
145         /*
146          * Configuration data for the platform device that owns
147          * this dsa switch tree instance.
148          */
149         struct dsa_platform_data        *pd;
150
151         /* List of switch ports */
152         struct list_head ports;
153
154         /* List of DSA links composing the routing table */
155         struct list_head rtable;
156
157         /* Maps offloaded LAG netdevs to a zero-based linear ID for
158          * drivers that need it.
159          */
160         struct net_device **lags;
161         unsigned int lags_len;
162 };
163
164 #define dsa_lags_foreach_id(_id, _dst)                          \
165         for ((_id) = 0; (_id) < (_dst)->lags_len; (_id)++)      \
166                 if ((_dst)->lags[(_id)])
167
168 #define dsa_lag_foreach_port(_dp, _dst, _lag)                   \
169         list_for_each_entry((_dp), &(_dst)->ports, list)        \
170                 if ((_dp)->lag_dev == (_lag))
171
172 #define dsa_hsr_foreach_port(_dp, _ds, _hsr)                    \
173         list_for_each_entry((_dp), &(_ds)->dst->ports, list)    \
174                 if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
175
176 static inline struct net_device *dsa_lag_dev(struct dsa_switch_tree *dst,
177                                              unsigned int id)
178 {
179         return dst->lags[id];
180 }
181
182 static inline int dsa_lag_id(struct dsa_switch_tree *dst,
183                              struct net_device *lag)
184 {
185         unsigned int id;
186
187         dsa_lags_foreach_id(id, dst) {
188                 if (dsa_lag_dev(dst, id) == lag)
189                         return id;
190         }
191
192         return -ENODEV;
193 }
194
195 /* TC matchall action types */
196 enum dsa_port_mall_action_type {
197         DSA_PORT_MALL_MIRROR,
198         DSA_PORT_MALL_POLICER,
199 };
200
201 /* TC mirroring entry */
202 struct dsa_mall_mirror_tc_entry {
203         u8 to_local_port;
204         bool ingress;
205 };
206
207 /* TC port policer entry */
208 struct dsa_mall_policer_tc_entry {
209         u32 burst;
210         u64 rate_bytes_per_sec;
211 };
212
213 /* TC matchall entry */
214 struct dsa_mall_tc_entry {
215         struct list_head list;
216         unsigned long cookie;
217         enum dsa_port_mall_action_type type;
218         union {
219                 struct dsa_mall_mirror_tc_entry mirror;
220                 struct dsa_mall_policer_tc_entry policer;
221         };
222 };
223
224
225 struct dsa_port {
226         /* A CPU port is physically connected to a master device.
227          * A user port exposed to userspace has a slave device.
228          */
229         union {
230                 struct net_device *master;
231                 struct net_device *slave;
232         };
233
234         /* Copy of the tagging protocol operations, for quicker access
235          * in the data path. Valid only for the CPU ports.
236          */
237         const struct dsa_device_ops *tag_ops;
238
239         /* Copies for faster access in master receive hot path */
240         struct dsa_switch_tree *dst;
241         struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
242                                struct packet_type *pt);
243         bool (*filter)(const struct sk_buff *skb, struct net_device *dev);
244
245         enum {
246                 DSA_PORT_TYPE_UNUSED = 0,
247                 DSA_PORT_TYPE_CPU,
248                 DSA_PORT_TYPE_DSA,
249                 DSA_PORT_TYPE_USER,
250         } type;
251
252         struct dsa_switch       *ds;
253         unsigned int            index;
254         const char              *name;
255         struct dsa_port         *cpu_dp;
256         u8                      mac[ETH_ALEN];
257         struct device_node      *dn;
258         unsigned int            ageing_time;
259         bool                    vlan_filtering;
260         u8                      stp_state;
261         struct net_device       *bridge_dev;
262         struct devlink_port     devlink_port;
263         bool                    devlink_port_setup;
264         struct phylink          *pl;
265         struct phylink_config   pl_config;
266         struct net_device       *lag_dev;
267         bool                    lag_tx_enabled;
268         struct net_device       *hsr_dev;
269
270         struct list_head list;
271
272         /*
273          * Give the switch driver somewhere to hang its per-port private data
274          * structures (accessible from the tagger).
275          */
276         void *priv;
277
278         /*
279          * Original copy of the master netdev ethtool_ops
280          */
281         const struct ethtool_ops *orig_ethtool_ops;
282
283         /*
284          * Original copy of the master netdev net_device_ops
285          */
286         const struct dsa_netdevice_ops *netdev_ops;
287
288         /* List of MAC addresses that must be forwarded on this port.
289          * These are only valid on CPU ports and DSA links.
290          */
291         struct list_head        mdbs;
292
293         bool setup;
294 };
295
296 /* TODO: ideally DSA ports would have a single dp->link_dp member,
297  * and no dst->rtable nor this struct dsa_link would be needed,
298  * but this would require some more complex tree walking,
299  * so keep it stupid at the moment and list them all.
300  */
301 struct dsa_link {
302         struct dsa_port *dp;
303         struct dsa_port *link_dp;
304         struct list_head list;
305 };
306
307 struct dsa_mac_addr {
308         unsigned char addr[ETH_ALEN];
309         u16 vid;
310         refcount_t refcount;
311         struct list_head list;
312 };
313
314 struct dsa_switch {
315         bool setup;
316
317         struct device *dev;
318
319         /*
320          * Parent switch tree, and switch index.
321          */
322         struct dsa_switch_tree  *dst;
323         unsigned int            index;
324
325         /* Listener for switch fabric events */
326         struct notifier_block   nb;
327
328         /*
329          * Give the switch driver somewhere to hang its private data
330          * structure.
331          */
332         void *priv;
333
334         /*
335          * Configuration data for this switch.
336          */
337         struct dsa_chip_data    *cd;
338
339         /*
340          * The switch operations.
341          */
342         const struct dsa_switch_ops     *ops;
343
344         /*
345          * Slave mii_bus and devices for the individual ports.
346          */
347         u32                     phys_mii_mask;
348         struct mii_bus          *slave_mii_bus;
349
350         /* Ageing Time limits in msecs */
351         unsigned int ageing_time_min;
352         unsigned int ageing_time_max;
353
354         /* devlink used to represent this switch device */
355         struct devlink          *devlink;
356
357         /* Number of switch port queues */
358         unsigned int            num_tx_queues;
359
360         /* Disallow bridge core from requesting different VLAN awareness
361          * settings on ports if not hardware-supported
362          */
363         bool                    vlan_filtering_is_global;
364
365         /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
366          * that have vlan_filtering=0. All drivers should ideally set this (and
367          * then the option would get removed), but it is unknown whether this
368          * would break things or not.
369          */
370         bool                    configure_vlan_while_not_filtering;
371
372         /* If the switch driver always programs the CPU port as egress tagged
373          * despite the VLAN configuration indicating otherwise, then setting
374          * @untag_bridge_pvid will force the DSA receive path to pop the bridge's
375          * default_pvid VLAN tagged frames to offer a consistent behavior
376          * between a vlan_filtering=0 and vlan_filtering=1 bridge device.
377          */
378         bool                    untag_bridge_pvid;
379
380         /* Let DSA manage the FDB entries towards the CPU, based on the
381          * software bridge database.
382          */
383         bool                    assisted_learning_on_cpu_port;
384
385         /* In case vlan_filtering_is_global is set, the VLAN awareness state
386          * should be retrieved from here and not from the per-port settings.
387          */
388         bool                    vlan_filtering;
389
390         /* MAC PCS does not provide link state change interrupt, and requires
391          * polling. Flag passed on to PHYLINK.
392          */
393         bool                    pcs_poll;
394
395         /* For switches that only have the MRU configurable. To ensure the
396          * configured MTU is not exceeded, normalization of MRU on all bridged
397          * interfaces is needed.
398          */
399         bool                    mtu_enforcement_ingress;
400
401         /* Drivers that benefit from having an ID associated with each
402          * offloaded LAG should set this to the maximum number of
403          * supported IDs. DSA will then maintain a mapping of _at
404          * least_ these many IDs, accessible to drivers via
405          * dsa_lag_id().
406          */
407         unsigned int            num_lag_ids;
408
409         size_t num_ports;
410 };
411
412 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
413 {
414         struct dsa_switch_tree *dst = ds->dst;
415         struct dsa_port *dp;
416
417         list_for_each_entry(dp, &dst->ports, list)
418                 if (dp->ds == ds && dp->index == p)
419                         return dp;
420
421         return NULL;
422 }
423
424 static inline bool dsa_port_is_dsa(struct dsa_port *port)
425 {
426         return port->type == DSA_PORT_TYPE_DSA;
427 }
428
429 static inline bool dsa_port_is_cpu(struct dsa_port *port)
430 {
431         return port->type == DSA_PORT_TYPE_CPU;
432 }
433
434 static inline bool dsa_port_is_user(struct dsa_port *dp)
435 {
436         return dp->type == DSA_PORT_TYPE_USER;
437 }
438
439 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
440 {
441         return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
442 }
443
444 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
445 {
446         return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
447 }
448
449 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
450 {
451         return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
452 }
453
454 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
455 {
456         return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
457 }
458
459 static inline u32 dsa_user_ports(struct dsa_switch *ds)
460 {
461         u32 mask = 0;
462         int p;
463
464         for (p = 0; p < ds->num_ports; p++)
465                 if (dsa_is_user_port(ds, p))
466                         mask |= BIT(p);
467
468         return mask;
469 }
470
471 /* Return the local port used to reach an arbitrary switch device */
472 static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
473 {
474         struct dsa_switch_tree *dst = ds->dst;
475         struct dsa_link *dl;
476
477         list_for_each_entry(dl, &dst->rtable, list)
478                 if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
479                         return dl->dp->index;
480
481         return ds->num_ports;
482 }
483
484 /* Return the local port used to reach an arbitrary switch port */
485 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
486                                             int port)
487 {
488         if (device == ds->index)
489                 return port;
490         else
491                 return dsa_routing_port(ds, device);
492 }
493
494 /* Return the local port used to reach the dedicated CPU port */
495 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
496 {
497         const struct dsa_port *dp = dsa_to_port(ds, port);
498         const struct dsa_port *cpu_dp = dp->cpu_dp;
499
500         if (!cpu_dp)
501                 return port;
502
503         return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
504 }
505
506 /* Return true if this is the local port used to reach the CPU port */
507 static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
508 {
509         if (dsa_is_unused_port(ds, port))
510                 return false;
511
512         return port == dsa_upstream_port(ds, port);
513 }
514
515 /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
516  * that the routing port from @downstream_ds to @upstream_ds is also the port
517  * which @downstream_ds uses to reach its dedicated CPU.
518  */
519 static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
520                                              struct dsa_switch *downstream_ds)
521 {
522         int routing_port;
523
524         if (upstream_ds == downstream_ds)
525                 return true;
526
527         routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
528
529         return dsa_is_upstream_port(downstream_ds, routing_port);
530 }
531
532 static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
533 {
534         const struct dsa_switch *ds = dp->ds;
535
536         if (ds->vlan_filtering_is_global)
537                 return ds->vlan_filtering;
538         else
539                 return dp->vlan_filtering;
540 }
541
542 static inline
543 struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
544 {
545         if (!dp->bridge_dev)
546                 return NULL;
547
548         if (dp->lag_dev)
549                 return dp->lag_dev;
550         else if (dp->hsr_dev)
551                 return dp->hsr_dev;
552
553         return dp->slave;
554 }
555
556 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
557                               bool is_static, void *data);
558 struct dsa_switch_ops {
559         /*
560          * Tagging protocol helpers called for the CPU ports and DSA links.
561          * @get_tag_protocol retrieves the initial tagging protocol and is
562          * mandatory. Switches which can operate using multiple tagging
563          * protocols should implement @change_tag_protocol and report in
564          * @get_tag_protocol the tagger in current use.
565          */
566         enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
567                                                   int port,
568                                                   enum dsa_tag_protocol mprot);
569         int     (*change_tag_protocol)(struct dsa_switch *ds, int port,
570                                        enum dsa_tag_protocol proto);
571
572         int     (*setup)(struct dsa_switch *ds);
573         void    (*teardown)(struct dsa_switch *ds);
574         u32     (*get_phy_flags)(struct dsa_switch *ds, int port);
575
576         /*
577          * Access to the switch's PHY registers.
578          */
579         int     (*phy_read)(struct dsa_switch *ds, int port, int regnum);
580         int     (*phy_write)(struct dsa_switch *ds, int port,
581                              int regnum, u16 val);
582
583         /*
584          * Link state adjustment (called from libphy)
585          */
586         void    (*adjust_link)(struct dsa_switch *ds, int port,
587                                 struct phy_device *phydev);
588         void    (*fixed_link_update)(struct dsa_switch *ds, int port,
589                                 struct fixed_phy_status *st);
590
591         /*
592          * PHYLINK integration
593          */
594         void    (*phylink_validate)(struct dsa_switch *ds, int port,
595                                     unsigned long *supported,
596                                     struct phylink_link_state *state);
597         int     (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
598                                           struct phylink_link_state *state);
599         void    (*phylink_mac_config)(struct dsa_switch *ds, int port,
600                                       unsigned int mode,
601                                       const struct phylink_link_state *state);
602         void    (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
603         void    (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
604                                          unsigned int mode,
605                                          phy_interface_t interface);
606         void    (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
607                                        unsigned int mode,
608                                        phy_interface_t interface,
609                                        struct phy_device *phydev,
610                                        int speed, int duplex,
611                                        bool tx_pause, bool rx_pause);
612         void    (*phylink_fixed_state)(struct dsa_switch *ds, int port,
613                                        struct phylink_link_state *state);
614         /*
615          * Port statistics counters.
616          */
617         void    (*get_strings)(struct dsa_switch *ds, int port,
618                                u32 stringset, uint8_t *data);
619         void    (*get_ethtool_stats)(struct dsa_switch *ds,
620                                      int port, uint64_t *data);
621         int     (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
622         void    (*get_ethtool_phy_stats)(struct dsa_switch *ds,
623                                          int port, uint64_t *data);
624         void    (*get_stats64)(struct dsa_switch *ds, int port,
625                                    struct rtnl_link_stats64 *s);
626         void    (*self_test)(struct dsa_switch *ds, int port,
627                              struct ethtool_test *etest, u64 *data);
628
629         /*
630          * ethtool Wake-on-LAN
631          */
632         void    (*get_wol)(struct dsa_switch *ds, int port,
633                            struct ethtool_wolinfo *w);
634         int     (*set_wol)(struct dsa_switch *ds, int port,
635                            struct ethtool_wolinfo *w);
636
637         /*
638          * ethtool timestamp info
639          */
640         int     (*get_ts_info)(struct dsa_switch *ds, int port,
641                                struct ethtool_ts_info *ts);
642
643         /*
644          * Suspend and resume
645          */
646         int     (*suspend)(struct dsa_switch *ds);
647         int     (*resume)(struct dsa_switch *ds);
648
649         /*
650          * Port enable/disable
651          */
652         int     (*port_enable)(struct dsa_switch *ds, int port,
653                                struct phy_device *phy);
654         void    (*port_disable)(struct dsa_switch *ds, int port);
655
656         /*
657          * Port's MAC EEE settings
658          */
659         int     (*set_mac_eee)(struct dsa_switch *ds, int port,
660                                struct ethtool_eee *e);
661         int     (*get_mac_eee)(struct dsa_switch *ds, int port,
662                                struct ethtool_eee *e);
663
664         /* EEPROM access */
665         int     (*get_eeprom_len)(struct dsa_switch *ds);
666         int     (*get_eeprom)(struct dsa_switch *ds,
667                               struct ethtool_eeprom *eeprom, u8 *data);
668         int     (*set_eeprom)(struct dsa_switch *ds,
669                               struct ethtool_eeprom *eeprom, u8 *data);
670
671         /*
672          * Register access.
673          */
674         int     (*get_regs_len)(struct dsa_switch *ds, int port);
675         void    (*get_regs)(struct dsa_switch *ds, int port,
676                             struct ethtool_regs *regs, void *p);
677
678         /*
679          * Upper device tracking.
680          */
681         int     (*port_prechangeupper)(struct dsa_switch *ds, int port,
682                                        struct netdev_notifier_changeupper_info *info);
683
684         /*
685          * Bridge integration
686          */
687         int     (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
688         int     (*port_bridge_join)(struct dsa_switch *ds, int port,
689                                     struct net_device *bridge);
690         void    (*port_bridge_leave)(struct dsa_switch *ds, int port,
691                                      struct net_device *bridge);
692         void    (*port_stp_state_set)(struct dsa_switch *ds, int port,
693                                       u8 state);
694         void    (*port_fast_age)(struct dsa_switch *ds, int port);
695         int     (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
696                                          struct switchdev_brport_flags flags,
697                                          struct netlink_ext_ack *extack);
698         int     (*port_bridge_flags)(struct dsa_switch *ds, int port,
699                                      struct switchdev_brport_flags flags,
700                                      struct netlink_ext_ack *extack);
701         int     (*port_set_mrouter)(struct dsa_switch *ds, int port, bool mrouter,
702                                     struct netlink_ext_ack *extack);
703
704         /*
705          * VLAN support
706          */
707         int     (*port_vlan_filtering)(struct dsa_switch *ds, int port,
708                                        bool vlan_filtering,
709                                        struct netlink_ext_ack *extack);
710         int     (*port_vlan_add)(struct dsa_switch *ds, int port,
711                                  const struct switchdev_obj_port_vlan *vlan,
712                                  struct netlink_ext_ack *extack);
713         int     (*port_vlan_del)(struct dsa_switch *ds, int port,
714                                  const struct switchdev_obj_port_vlan *vlan);
715         /*
716          * Forwarding database
717          */
718         int     (*port_fdb_add)(struct dsa_switch *ds, int port,
719                                 const unsigned char *addr, u16 vid);
720         int     (*port_fdb_del)(struct dsa_switch *ds, int port,
721                                 const unsigned char *addr, u16 vid);
722         int     (*port_fdb_dump)(struct dsa_switch *ds, int port,
723                                  dsa_fdb_dump_cb_t *cb, void *data);
724
725         /*
726          * Multicast database
727          */
728         int     (*port_mdb_add)(struct dsa_switch *ds, int port,
729                                 const struct switchdev_obj_port_mdb *mdb);
730         int     (*port_mdb_del)(struct dsa_switch *ds, int port,
731                                 const struct switchdev_obj_port_mdb *mdb);
732         /*
733          * RXNFC
734          */
735         int     (*get_rxnfc)(struct dsa_switch *ds, int port,
736                              struct ethtool_rxnfc *nfc, u32 *rule_locs);
737         int     (*set_rxnfc)(struct dsa_switch *ds, int port,
738                              struct ethtool_rxnfc *nfc);
739
740         /*
741          * TC integration
742          */
743         int     (*cls_flower_add)(struct dsa_switch *ds, int port,
744                                   struct flow_cls_offload *cls, bool ingress);
745         int     (*cls_flower_del)(struct dsa_switch *ds, int port,
746                                   struct flow_cls_offload *cls, bool ingress);
747         int     (*cls_flower_stats)(struct dsa_switch *ds, int port,
748                                     struct flow_cls_offload *cls, bool ingress);
749         int     (*port_mirror_add)(struct dsa_switch *ds, int port,
750                                    struct dsa_mall_mirror_tc_entry *mirror,
751                                    bool ingress);
752         void    (*port_mirror_del)(struct dsa_switch *ds, int port,
753                                    struct dsa_mall_mirror_tc_entry *mirror);
754         int     (*port_policer_add)(struct dsa_switch *ds, int port,
755                                     struct dsa_mall_policer_tc_entry *policer);
756         void    (*port_policer_del)(struct dsa_switch *ds, int port);
757         int     (*port_setup_tc)(struct dsa_switch *ds, int port,
758                                  enum tc_setup_type type, void *type_data);
759
760         /*
761          * Cross-chip operations
762          */
763         int     (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
764                                          int sw_index, int port,
765                                          struct net_device *br);
766         void    (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
767                                           int sw_index, int port,
768                                           struct net_device *br);
769         int     (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
770                                         int port);
771         int     (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
772                                       int port, struct net_device *lag,
773                                       struct netdev_lag_upper_info *info);
774         int     (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
775                                        int port, struct net_device *lag);
776
777         /*
778          * PTP functionality
779          */
780         int     (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
781                                      struct ifreq *ifr);
782         int     (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
783                                      struct ifreq *ifr);
784         void    (*port_txtstamp)(struct dsa_switch *ds, int port,
785                                  struct sk_buff *skb);
786         bool    (*port_rxtstamp)(struct dsa_switch *ds, int port,
787                                  struct sk_buff *skb, unsigned int type);
788
789         /* Devlink parameters, etc */
790         int     (*devlink_param_get)(struct dsa_switch *ds, u32 id,
791                                      struct devlink_param_gset_ctx *ctx);
792         int     (*devlink_param_set)(struct dsa_switch *ds, u32 id,
793                                      struct devlink_param_gset_ctx *ctx);
794         int     (*devlink_info_get)(struct dsa_switch *ds,
795                                     struct devlink_info_req *req,
796                                     struct netlink_ext_ack *extack);
797         int     (*devlink_sb_pool_get)(struct dsa_switch *ds,
798                                        unsigned int sb_index, u16 pool_index,
799                                        struct devlink_sb_pool_info *pool_info);
800         int     (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
801                                        u16 pool_index, u32 size,
802                                        enum devlink_sb_threshold_type threshold_type,
803                                        struct netlink_ext_ack *extack);
804         int     (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
805                                             unsigned int sb_index, u16 pool_index,
806                                             u32 *p_threshold);
807         int     (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
808                                             unsigned int sb_index, u16 pool_index,
809                                             u32 threshold,
810                                             struct netlink_ext_ack *extack);
811         int     (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
812                                                unsigned int sb_index, u16 tc_index,
813                                                enum devlink_sb_pool_type pool_type,
814                                                u16 *p_pool_index, u32 *p_threshold);
815         int     (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
816                                                unsigned int sb_index, u16 tc_index,
817                                                enum devlink_sb_pool_type pool_type,
818                                                u16 pool_index, u32 threshold,
819                                                struct netlink_ext_ack *extack);
820         int     (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
821                                            unsigned int sb_index);
822         int     (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
823                                             unsigned int sb_index);
824         int     (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
825                                                 unsigned int sb_index, u16 pool_index,
826                                                 u32 *p_cur, u32 *p_max);
827         int     (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
828                                                    unsigned int sb_index, u16 tc_index,
829                                                    enum devlink_sb_pool_type pool_type,
830                                                    u32 *p_cur, u32 *p_max);
831
832         /*
833          * MTU change functionality. Switches can also adjust their MRU through
834          * this method. By MTU, one understands the SDU (L2 payload) length.
835          * If the switch needs to account for the DSA tag on the CPU port, this
836          * method needs to do so privately.
837          */
838         int     (*port_change_mtu)(struct dsa_switch *ds, int port,
839                                    int new_mtu);
840         int     (*port_max_mtu)(struct dsa_switch *ds, int port);
841
842         /*
843          * LAG integration
844          */
845         int     (*port_lag_change)(struct dsa_switch *ds, int port);
846         int     (*port_lag_join)(struct dsa_switch *ds, int port,
847                                  struct net_device *lag,
848                                  struct netdev_lag_upper_info *info);
849         int     (*port_lag_leave)(struct dsa_switch *ds, int port,
850                                   struct net_device *lag);
851
852         /*
853          * HSR integration
854          */
855         int     (*port_hsr_join)(struct dsa_switch *ds, int port,
856                                  struct net_device *hsr);
857         int     (*port_hsr_leave)(struct dsa_switch *ds, int port,
858                                   struct net_device *hsr);
859
860         /*
861          * MRP integration
862          */
863         int     (*port_mrp_add)(struct dsa_switch *ds, int port,
864                                 const struct switchdev_obj_mrp *mrp);
865         int     (*port_mrp_del)(struct dsa_switch *ds, int port,
866                                 const struct switchdev_obj_mrp *mrp);
867         int     (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
868                                           const struct switchdev_obj_ring_role_mrp *mrp);
869         int     (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
870                                           const struct switchdev_obj_ring_role_mrp *mrp);
871 };
872
873 #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes)            \
874         DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes,                \
875                              dsa_devlink_param_get, dsa_devlink_param_set, NULL)
876
877 int dsa_devlink_param_get(struct devlink *dl, u32 id,
878                           struct devlink_param_gset_ctx *ctx);
879 int dsa_devlink_param_set(struct devlink *dl, u32 id,
880                           struct devlink_param_gset_ctx *ctx);
881 int dsa_devlink_params_register(struct dsa_switch *ds,
882                                 const struct devlink_param *params,
883                                 size_t params_count);
884 void dsa_devlink_params_unregister(struct dsa_switch *ds,
885                                    const struct devlink_param *params,
886                                    size_t params_count);
887 int dsa_devlink_resource_register(struct dsa_switch *ds,
888                                   const char *resource_name,
889                                   u64 resource_size,
890                                   u64 resource_id,
891                                   u64 parent_resource_id,
892                                   const struct devlink_resource_size_params *size_params);
893
894 void dsa_devlink_resources_unregister(struct dsa_switch *ds);
895
896 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
897                                            u64 resource_id,
898                                            devlink_resource_occ_get_t *occ_get,
899                                            void *occ_get_priv);
900 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
901                                              u64 resource_id);
902 struct devlink_region *
903 dsa_devlink_region_create(struct dsa_switch *ds,
904                           const struct devlink_region_ops *ops,
905                           u32 region_max_snapshots, u64 region_size);
906 struct devlink_region *
907 dsa_devlink_port_region_create(struct dsa_switch *ds,
908                                int port,
909                                const struct devlink_port_region_ops *ops,
910                                u32 region_max_snapshots, u64 region_size);
911 void dsa_devlink_region_destroy(struct devlink_region *region);
912
913 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
914
915 struct dsa_devlink_priv {
916         struct dsa_switch *ds;
917 };
918
919 static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
920 {
921         struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
922
923         return dl_priv->ds;
924 }
925
926 static inline
927 struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
928 {
929         struct devlink *dl = port->devlink;
930         struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
931
932         return dl_priv->ds;
933 }
934
935 static inline int dsa_devlink_port_to_port(struct devlink_port *port)
936 {
937         return port->index;
938 }
939
940 struct dsa_switch_driver {
941         struct list_head        list;
942         const struct dsa_switch_ops *ops;
943 };
944
945 struct net_device *dsa_dev_to_net_device(struct device *dev);
946
947 /* Keep inline for faster access in hot path */
948 static inline bool netdev_uses_dsa(const struct net_device *dev)
949 {
950 #if IS_ENABLED(CONFIG_NET_DSA)
951         return dev->dsa_ptr && dev->dsa_ptr->rcv;
952 #endif
953         return false;
954 }
955
956 static inline bool dsa_can_decode(const struct sk_buff *skb,
957                                   struct net_device *dev)
958 {
959 #if IS_ENABLED(CONFIG_NET_DSA)
960         return !dev->dsa_ptr->filter || dev->dsa_ptr->filter(skb, dev);
961 #endif
962         return false;
963 }
964
965 /* All DSA tags that push the EtherType to the right (basically all except tail
966  * tags, which don't break dissection) can be treated the same from the
967  * perspective of the flow dissector.
968  *
969  * We need to return:
970  *  - offset: the (B - A) difference between:
971  *    A. the position of the real EtherType and
972  *    B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
973  *       after the normal EtherType was supposed to be)
974  *    The offset in bytes is exactly equal to the tagger overhead (and half of
975  *    that, in __be16 shorts).
976  *
977  *  - proto: the value of the real EtherType.
978  */
979 static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
980                                                 __be16 *proto, int *offset)
981 {
982 #if IS_ENABLED(CONFIG_NET_DSA)
983         const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
984         int tag_len = ops->needed_headroom;
985
986         *offset = tag_len;
987         *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
988 #endif
989 }
990
991 #if IS_ENABLED(CONFIG_NET_DSA)
992 static inline int __dsa_netdevice_ops_check(struct net_device *dev)
993 {
994         int err = -EOPNOTSUPP;
995
996         if (!dev->dsa_ptr)
997                 return err;
998
999         if (!dev->dsa_ptr->netdev_ops)
1000                 return err;
1001
1002         return 0;
1003 }
1004
1005 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
1006                                    int cmd)
1007 {
1008         const struct dsa_netdevice_ops *ops;
1009         int err;
1010
1011         err = __dsa_netdevice_ops_check(dev);
1012         if (err)
1013                 return err;
1014
1015         ops = dev->dsa_ptr->netdev_ops;
1016
1017         return ops->ndo_do_ioctl(dev, ifr, cmd);
1018 }
1019 #else
1020 static inline int dsa_ndo_do_ioctl(struct net_device *dev, struct ifreq *ifr,
1021                                    int cmd)
1022 {
1023         return -EOPNOTSUPP;
1024 }
1025 #endif
1026
1027 void dsa_unregister_switch(struct dsa_switch *ds);
1028 int dsa_register_switch(struct dsa_switch *ds);
1029 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
1030 #ifdef CONFIG_PM_SLEEP
1031 int dsa_switch_suspend(struct dsa_switch *ds);
1032 int dsa_switch_resume(struct dsa_switch *ds);
1033 #else
1034 static inline int dsa_switch_suspend(struct dsa_switch *ds)
1035 {
1036         return 0;
1037 }
1038 static inline int dsa_switch_resume(struct dsa_switch *ds)
1039 {
1040         return 0;
1041 }
1042 #endif /* CONFIG_PM_SLEEP */
1043
1044 #if IS_ENABLED(CONFIG_NET_DSA)
1045 bool dsa_slave_dev_check(const struct net_device *dev);
1046 #else
1047 static inline bool dsa_slave_dev_check(const struct net_device *dev)
1048 {
1049         return false;
1050 }
1051 #endif
1052
1053 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
1054 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
1055 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
1056 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
1057 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
1058
1059 struct dsa_tag_driver {
1060         const struct dsa_device_ops *ops;
1061         struct list_head list;
1062         struct module *owner;
1063 };
1064
1065 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
1066                               unsigned int count,
1067                               struct module *owner);
1068 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
1069                                 unsigned int count);
1070
1071 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
1072 static int __init dsa_tag_driver_module_init(void)                      \
1073 {                                                                       \
1074         dsa_tag_drivers_register(__dsa_tag_drivers_array, __count,      \
1075                                  THIS_MODULE);                          \
1076         return 0;                                                       \
1077 }                                                                       \
1078 module_init(dsa_tag_driver_module_init);                                \
1079                                                                         \
1080 static void __exit dsa_tag_driver_module_exit(void)                     \
1081 {                                                                       \
1082         dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count);   \
1083 }                                                                       \
1084 module_exit(dsa_tag_driver_module_exit)
1085
1086 /**
1087  * module_dsa_tag_drivers() - Helper macro for registering DSA tag
1088  * drivers
1089  * @__ops_array: Array of tag driver strucutres
1090  *
1091  * Helper macro for DSA tag drivers which do not do anything special
1092  * in module init/exit. Each module may only use this macro once, and
1093  * calling it replaces module_init() and module_exit().
1094  */
1095 #define module_dsa_tag_drivers(__ops_array)                             \
1096 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
1097
1098 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
1099
1100 /* Create a static structure we can build a linked list of dsa_tag
1101  * drivers
1102  */
1103 #define DSA_TAG_DRIVER(__ops)                                           \
1104 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = {             \
1105         .ops = &__ops,                                                  \
1106 }
1107
1108 /**
1109  * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
1110  * driver
1111  * @__ops: Single tag driver structures
1112  *
1113  * Helper macro for DSA tag drivers which do not do anything special
1114  * in module init/exit. Each module may only use this macro once, and
1115  * calling it replaces module_init() and module_exit().
1116  */
1117 #define module_dsa_tag_driver(__ops)                                    \
1118 DSA_TAG_DRIVER(__ops);                                                  \
1119                                                                         \
1120 static struct dsa_tag_driver *dsa_tag_driver_array[] =  {               \
1121         &DSA_TAG_DRIVER_NAME(__ops)                                     \
1122 };                                                                      \
1123 module_dsa_tag_drivers(dsa_tag_driver_array)
1124 #endif
1125