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