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