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