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