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