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