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