Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
a40c175b VD |
2 | /* |
3 | * Handling of a single switch port | |
4 | * | |
5 | * Copyright (c) 2017 Savoir-faire Linux Inc. | |
6 | * Vivien Didelot <vivien.didelot@savoirfairelinux.com> | |
a40c175b VD |
7 | */ |
8 | ||
9 | #include <linux/if_bridge.h> | |
cfbed329 | 10 | #include <linux/notifier.h> |
57ab1ca2 VD |
11 | #include <linux/of_mdio.h> |
12 | #include <linux/of_net.h> | |
a40c175b VD |
13 | |
14 | #include "dsa_priv.h" | |
15 | ||
886f8e26 VO |
16 | /** |
17 | * dsa_port_notify - Notify the switching fabric of changes to a port | |
18 | * @dp: port on which change occurred | |
19 | * @e: event, must be of type DSA_NOTIFIER_* | |
20 | * @v: event-specific value. | |
21 | * | |
22 | * Notify all switches in the DSA tree that this port's switch belongs to, | |
23 | * including this switch itself, of an event. Allows the other switches to | |
24 | * reconfigure themselves for cross-chip operations. Can also be used to | |
25 | * reconfigure ports without net_devices (CPU ports, DSA links) whenever | |
26 | * a user port's state changes. | |
27 | */ | |
bb9f6031 | 28 | static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v) |
cfbed329 | 29 | { |
886f8e26 | 30 | return dsa_tree_notify(dp->ds->dst, e, v); |
cfbed329 VD |
31 | } |
32 | ||
bae33f2b | 33 | int dsa_port_set_state(struct dsa_port *dp, u8 state) |
a40c175b VD |
34 | { |
35 | struct dsa_switch *ds = dp->ds; | |
36 | int port = dp->index; | |
37 | ||
bae33f2b VO |
38 | if (!ds->ops->port_stp_state_set) |
39 | return -EOPNOTSUPP; | |
a40c175b | 40 | |
bae33f2b | 41 | ds->ops->port_stp_state_set(ds, port, state); |
a40c175b VD |
42 | |
43 | if (ds->ops->port_fast_age) { | |
44 | /* Fast age FDB entries or flush appropriate forwarding database | |
45 | * for the given port, if we are moving it from Learning or | |
46 | * Forwarding state, to Disabled or Blocking or Listening state. | |
47 | */ | |
48 | ||
49 | if ((dp->stp_state == BR_STATE_LEARNING || | |
50 | dp->stp_state == BR_STATE_FORWARDING) && | |
51 | (state == BR_STATE_DISABLED || | |
52 | state == BR_STATE_BLOCKING || | |
53 | state == BR_STATE_LISTENING)) | |
54 | ds->ops->port_fast_age(ds, port); | |
55 | } | |
56 | ||
57 | dp->stp_state = state; | |
58 | ||
59 | return 0; | |
60 | } | |
61 | ||
fb8a6a2b | 62 | static void dsa_port_set_state_now(struct dsa_port *dp, u8 state) |
a40c175b VD |
63 | { |
64 | int err; | |
65 | ||
bae33f2b | 66 | err = dsa_port_set_state(dp, state); |
a40c175b VD |
67 | if (err) |
68 | pr_err("DSA: failed to set STP state %u (%d)\n", state, err); | |
69 | } | |
cfbed329 | 70 | |
8640f8dc | 71 | int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy) |
fb8a6a2b | 72 | { |
fb8a6a2b VD |
73 | struct dsa_switch *ds = dp->ds; |
74 | int port = dp->index; | |
75 | int err; | |
76 | ||
77 | if (ds->ops->port_enable) { | |
78 | err = ds->ops->port_enable(ds, port, phy); | |
79 | if (err) | |
80 | return err; | |
81 | } | |
82 | ||
9c2054a5 RK |
83 | if (!dp->bridge_dev) |
84 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); | |
fb8a6a2b | 85 | |
8640f8dc RK |
86 | if (dp->pl) |
87 | phylink_start(dp->pl); | |
88 | ||
fb8a6a2b VD |
89 | return 0; |
90 | } | |
91 | ||
8640f8dc RK |
92 | int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy) |
93 | { | |
94 | int err; | |
95 | ||
96 | rtnl_lock(); | |
97 | err = dsa_port_enable_rt(dp, phy); | |
98 | rtnl_unlock(); | |
99 | ||
100 | return err; | |
101 | } | |
102 | ||
103 | void dsa_port_disable_rt(struct dsa_port *dp) | |
fb8a6a2b VD |
104 | { |
105 | struct dsa_switch *ds = dp->ds; | |
106 | int port = dp->index; | |
107 | ||
8640f8dc RK |
108 | if (dp->pl) |
109 | phylink_stop(dp->pl); | |
110 | ||
9c2054a5 RK |
111 | if (!dp->bridge_dev) |
112 | dsa_port_set_state_now(dp, BR_STATE_DISABLED); | |
fb8a6a2b VD |
113 | |
114 | if (ds->ops->port_disable) | |
75104db0 | 115 | ds->ops->port_disable(ds, port); |
fb8a6a2b VD |
116 | } |
117 | ||
8640f8dc RK |
118 | void dsa_port_disable(struct dsa_port *dp) |
119 | { | |
120 | rtnl_lock(); | |
121 | dsa_port_disable_rt(dp); | |
122 | rtnl_unlock(); | |
123 | } | |
124 | ||
5e38c158 VO |
125 | static void dsa_port_change_brport_flags(struct dsa_port *dp, |
126 | bool bridge_offload) | |
127 | { | |
e18f4c18 VO |
128 | struct switchdev_brport_flags flags; |
129 | int flag; | |
5e38c158 | 130 | |
e18f4c18 | 131 | flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD; |
5e38c158 | 132 | if (bridge_offload) |
e18f4c18 | 133 | flags.val = flags.mask; |
5e38c158 | 134 | else |
e18f4c18 | 135 | flags.val = flags.mask & ~BR_LEARNING; |
5e38c158 | 136 | |
e18f4c18 VO |
137 | for_each_set_bit(flag, &flags.mask, 32) { |
138 | struct switchdev_brport_flags tmp; | |
139 | ||
140 | tmp.val = flags.val & BIT(flag); | |
141 | tmp.mask = BIT(flag); | |
5e38c158 | 142 | |
a8b659e7 | 143 | dsa_port_bridge_flags(dp, tmp, NULL); |
5e38c158 VO |
144 | } |
145 | } | |
146 | ||
cfbed329 VD |
147 | int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br) |
148 | { | |
149 | struct dsa_notifier_bridge_info info = { | |
f66a6a69 | 150 | .tree_index = dp->ds->dst->index, |
cfbed329 VD |
151 | .sw_index = dp->ds->index, |
152 | .port = dp->index, | |
153 | .br = br, | |
154 | }; | |
155 | int err; | |
156 | ||
5e38c158 VO |
157 | /* Notify the port driver to set its configurable flags in a way that |
158 | * matches the initial settings of a bridge port. | |
159 | */ | |
160 | dsa_port_change_brport_flags(dp, true); | |
c1388063 RK |
161 | |
162 | /* Here the interface is already bridged. Reflect the current | |
163 | * configuration so that drivers can program their chips accordingly. | |
cfbed329 VD |
164 | */ |
165 | dp->bridge_dev = br; | |
166 | ||
f66a6a69 | 167 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info); |
cfbed329 VD |
168 | |
169 | /* The bridging is rolled back on error */ | |
c1388063 | 170 | if (err) { |
5e38c158 | 171 | dsa_port_change_brport_flags(dp, false); |
cfbed329 | 172 | dp->bridge_dev = NULL; |
c1388063 | 173 | } |
cfbed329 VD |
174 | |
175 | return err; | |
176 | } | |
177 | ||
178 | void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) | |
179 | { | |
180 | struct dsa_notifier_bridge_info info = { | |
f66a6a69 | 181 | .tree_index = dp->ds->dst->index, |
cfbed329 VD |
182 | .sw_index = dp->ds->index, |
183 | .port = dp->index, | |
184 | .br = br, | |
185 | }; | |
186 | int err; | |
187 | ||
188 | /* Here the port is already unbridged. Reflect the current configuration | |
189 | * so that drivers can program their chips accordingly. | |
190 | */ | |
191 | dp->bridge_dev = NULL; | |
192 | ||
f66a6a69 | 193 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
cfbed329 VD |
194 | if (err) |
195 | pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n"); | |
196 | ||
5e38c158 VO |
197 | /* Configure the port for standalone mode (no address learning, |
198 | * flood everything). | |
199 | * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events | |
200 | * when the user requests it through netlink or sysfs, but not | |
201 | * automatically at port join or leave, so we need to handle resetting | |
202 | * the brport flags ourselves. But we even prefer it that way, because | |
203 | * otherwise, some setups might never get the notification they need, | |
204 | * for example, when a port leaves a LAG that offloads the bridge, | |
205 | * it becomes standalone, but as far as the bridge is concerned, no | |
206 | * port ever left. | |
207 | */ | |
208 | dsa_port_change_brport_flags(dp, false); | |
c1388063 | 209 | |
cfbed329 VD |
210 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
211 | * so allow it to be in BR_STATE_FORWARDING to be kept functional | |
212 | */ | |
213 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); | |
214 | } | |
4d61d304 | 215 | |
058102a6 TW |
216 | int dsa_port_lag_change(struct dsa_port *dp, |
217 | struct netdev_lag_lower_state_info *linfo) | |
218 | { | |
219 | struct dsa_notifier_lag_info info = { | |
220 | .sw_index = dp->ds->index, | |
221 | .port = dp->index, | |
222 | }; | |
223 | bool tx_enabled; | |
224 | ||
225 | if (!dp->lag_dev) | |
226 | return 0; | |
227 | ||
228 | /* On statically configured aggregates (e.g. loadbalance | |
229 | * without LACP) ports will always be tx_enabled, even if the | |
230 | * link is down. Thus we require both link_up and tx_enabled | |
231 | * in order to include it in the tx set. | |
232 | */ | |
233 | tx_enabled = linfo->link_up && linfo->tx_enabled; | |
234 | ||
235 | if (tx_enabled == dp->lag_tx_enabled) | |
236 | return 0; | |
237 | ||
238 | dp->lag_tx_enabled = tx_enabled; | |
239 | ||
240 | return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info); | |
241 | } | |
242 | ||
243 | int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag, | |
244 | struct netdev_lag_upper_info *uinfo) | |
245 | { | |
246 | struct dsa_notifier_lag_info info = { | |
247 | .sw_index = dp->ds->index, | |
248 | .port = dp->index, | |
249 | .lag = lag, | |
250 | .info = uinfo, | |
251 | }; | |
252 | int err; | |
253 | ||
254 | dsa_lag_map(dp->ds->dst, lag); | |
255 | dp->lag_dev = lag; | |
256 | ||
257 | err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info); | |
258 | if (err) { | |
259 | dp->lag_dev = NULL; | |
260 | dsa_lag_unmap(dp->ds->dst, lag); | |
261 | } | |
262 | ||
263 | return err; | |
264 | } | |
265 | ||
266 | void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag) | |
267 | { | |
268 | struct dsa_notifier_lag_info info = { | |
269 | .sw_index = dp->ds->index, | |
270 | .port = dp->index, | |
271 | .lag = lag, | |
272 | }; | |
273 | int err; | |
274 | ||
275 | if (!dp->lag_dev) | |
276 | return; | |
277 | ||
278 | /* Port might have been part of a LAG that in turn was | |
279 | * attached to a bridge. | |
280 | */ | |
281 | if (dp->bridge_dev) | |
282 | dsa_port_bridge_leave(dp, dp->bridge_dev); | |
283 | ||
284 | dp->lag_tx_enabled = false; | |
285 | dp->lag_dev = NULL; | |
286 | ||
287 | err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); | |
288 | if (err) | |
289 | pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n", | |
290 | err); | |
291 | ||
292 | dsa_lag_unmap(dp->ds->dst, lag); | |
293 | } | |
294 | ||
adb256eb | 295 | /* Must be called under rcu_read_lock() */ |
8f5d16f6 | 296 | static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp, |
89153ed6 VO |
297 | bool vlan_filtering, |
298 | struct netlink_ext_ack *extack) | |
8f5d16f6 VO |
299 | { |
300 | struct dsa_switch *ds = dp->ds; | |
adb256eb VO |
301 | int err, i; |
302 | ||
303 | /* VLAN awareness was off, so the question is "can we turn it on". | |
304 | * We may have had 8021q uppers, those need to go. Make sure we don't | |
305 | * enter an inconsistent state: deny changing the VLAN awareness state | |
306 | * as long as we have 8021q uppers. | |
307 | */ | |
308 | if (vlan_filtering && dsa_is_user_port(ds, dp->index)) { | |
309 | struct net_device *upper_dev, *slave = dp->slave; | |
310 | struct net_device *br = dp->bridge_dev; | |
311 | struct list_head *iter; | |
312 | ||
313 | netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { | |
314 | struct bridge_vlan_info br_info; | |
315 | u16 vid; | |
316 | ||
317 | if (!is_vlan_dev(upper_dev)) | |
318 | continue; | |
319 | ||
320 | vid = vlan_dev_vlan_id(upper_dev); | |
321 | ||
322 | /* br_vlan_get_info() returns -EINVAL or -ENOENT if the | |
323 | * device, respectively the VID is not found, returning | |
324 | * 0 means success, which is a failure for us here. | |
325 | */ | |
326 | err = br_vlan_get_info(br, vid, &br_info); | |
327 | if (err == 0) { | |
89153ed6 VO |
328 | NL_SET_ERR_MSG_MOD(extack, |
329 | "Must first remove VLAN uppers having VIDs also present in bridge"); | |
adb256eb VO |
330 | return false; |
331 | } | |
332 | } | |
333 | } | |
8f5d16f6 VO |
334 | |
335 | if (!ds->vlan_filtering_is_global) | |
336 | return true; | |
337 | ||
338 | /* For cases where enabling/disabling VLAN awareness is global to the | |
339 | * switch, we need to handle the case where multiple bridges span | |
340 | * different ports of the same switch device and one of them has a | |
341 | * different setting than what is being requested. | |
342 | */ | |
343 | for (i = 0; i < ds->num_ports; i++) { | |
344 | struct net_device *other_bridge; | |
345 | ||
346 | other_bridge = dsa_to_port(ds, i)->bridge_dev; | |
347 | if (!other_bridge) | |
348 | continue; | |
349 | /* If it's the same bridge, it also has same | |
350 | * vlan_filtering setting => no need to check | |
351 | */ | |
352 | if (other_bridge == dp->bridge_dev) | |
353 | continue; | |
354 | if (br_vlan_enabled(other_bridge) != vlan_filtering) { | |
89153ed6 VO |
355 | NL_SET_ERR_MSG_MOD(extack, |
356 | "VLAN filtering is a global setting"); | |
8f5d16f6 VO |
357 | return false; |
358 | } | |
359 | } | |
360 | return true; | |
361 | } | |
362 | ||
89153ed6 VO |
363 | int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering, |
364 | struct netlink_ext_ack *extack) | |
4d61d304 VD |
365 | { |
366 | struct dsa_switch *ds = dp->ds; | |
bae33f2b | 367 | bool apply; |
33162e9a | 368 | int err; |
4d61d304 | 369 | |
bae33f2b VO |
370 | if (!ds->ops->port_vlan_filtering) |
371 | return -EOPNOTSUPP; | |
adb256eb | 372 | |
bae33f2b VO |
373 | /* We are called from dsa_slave_switchdev_blocking_event(), |
374 | * which is not under rcu_read_lock(), unlike | |
375 | * dsa_slave_switchdev_event(). | |
376 | */ | |
377 | rcu_read_lock(); | |
89153ed6 | 378 | apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack); |
bae33f2b VO |
379 | rcu_read_unlock(); |
380 | if (!apply) | |
381 | return -EINVAL; | |
8f5d16f6 | 382 | |
ec9121e7 VO |
383 | if (dsa_port_is_vlan_filtering(dp) == vlan_filtering) |
384 | return 0; | |
385 | ||
89153ed6 VO |
386 | err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering, |
387 | extack); | |
8f5d16f6 VO |
388 | if (err) |
389 | return err; | |
390 | ||
bae33f2b VO |
391 | if (ds->vlan_filtering_is_global) |
392 | ds->vlan_filtering = vlan_filtering; | |
393 | else | |
394 | dp->vlan_filtering = vlan_filtering; | |
2e554a7a | 395 | |
4d61d304 VD |
396 | return 0; |
397 | } | |
d87bd94e | 398 | |
54a0ed0d RK |
399 | /* This enforces legacy behavior for switch drivers which assume they can't |
400 | * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0 | |
401 | */ | |
402 | bool dsa_port_skip_vlan_configuration(struct dsa_port *dp) | |
403 | { | |
404 | struct dsa_switch *ds = dp->ds; | |
405 | ||
406 | if (!dp->bridge_dev) | |
407 | return false; | |
408 | ||
409 | return (!ds->configure_vlan_while_not_filtering && | |
410 | !br_vlan_enabled(dp->bridge_dev)); | |
411 | } | |
412 | ||
bae33f2b | 413 | int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock) |
d87bd94e VD |
414 | { |
415 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); | |
416 | unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); | |
bae33f2b | 417 | struct dsa_notifier_ageing_time_info info; |
bae33f2b VO |
418 | int err; |
419 | ||
420 | info.ageing_time = ageing_time; | |
d87bd94e | 421 | |
bae33f2b VO |
422 | err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
423 | if (err) | |
424 | return err; | |
d87bd94e | 425 | |
d87bd94e | 426 | dp->ageing_time = ageing_time; |
d87bd94e | 427 | |
77b61365 | 428 | return 0; |
d87bd94e | 429 | } |
d1cffff0 | 430 | |
e18f4c18 | 431 | int dsa_port_pre_bridge_flags(const struct dsa_port *dp, |
a8b659e7 VO |
432 | struct switchdev_brport_flags flags, |
433 | struct netlink_ext_ack *extack) | |
ea87005a FF |
434 | { |
435 | struct dsa_switch *ds = dp->ds; | |
436 | ||
a8b659e7 | 437 | if (!ds->ops->port_pre_bridge_flags) |
ea87005a FF |
438 | return -EINVAL; |
439 | ||
a8b659e7 | 440 | return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack); |
ea87005a FF |
441 | } |
442 | ||
e18f4c18 | 443 | int dsa_port_bridge_flags(const struct dsa_port *dp, |
a8b659e7 VO |
444 | struct switchdev_brport_flags flags, |
445 | struct netlink_ext_ack *extack) | |
57652796 RK |
446 | { |
447 | struct dsa_switch *ds = dp->ds; | |
57652796 | 448 | |
a8b659e7 VO |
449 | if (!ds->ops->port_bridge_flags) |
450 | return -EINVAL; | |
57652796 | 451 | |
a8b659e7 | 452 | return ds->ops->port_bridge_flags(ds, dp->index, flags, extack); |
57652796 RK |
453 | } |
454 | ||
a8b659e7 VO |
455 | int dsa_port_mrouter(struct dsa_port *dp, bool mrouter, |
456 | struct netlink_ext_ack *extack) | |
08cc83cc VD |
457 | { |
458 | struct dsa_switch *ds = dp->ds; | |
08cc83cc | 459 | |
a8b659e7 | 460 | if (!ds->ops->port_set_mrouter) |
bae33f2b | 461 | return -EOPNOTSUPP; |
08cc83cc | 462 | |
a8b659e7 | 463 | return ds->ops->port_set_mrouter(ds, dp->index, mrouter, extack); |
08cc83cc VD |
464 | } |
465 | ||
bfcb8132 VO |
466 | int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu, |
467 | bool propagate_upstream) | |
468 | { | |
469 | struct dsa_notifier_mtu_info info = { | |
470 | .sw_index = dp->ds->index, | |
471 | .propagate_upstream = propagate_upstream, | |
472 | .port = dp->index, | |
473 | .mtu = new_mtu, | |
474 | }; | |
475 | ||
476 | return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info); | |
477 | } | |
478 | ||
2acf4e6a AS |
479 | int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
480 | u16 vid) | |
d1cffff0 | 481 | { |
685fb6a4 VD |
482 | struct dsa_notifier_fdb_info info = { |
483 | .sw_index = dp->ds->index, | |
484 | .port = dp->index, | |
2acf4e6a AS |
485 | .addr = addr, |
486 | .vid = vid, | |
685fb6a4 | 487 | }; |
d1cffff0 | 488 | |
685fb6a4 | 489 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); |
d1cffff0 VD |
490 | } |
491 | ||
2acf4e6a AS |
492 | int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
493 | u16 vid) | |
d1cffff0 | 494 | { |
685fb6a4 VD |
495 | struct dsa_notifier_fdb_info info = { |
496 | .sw_index = dp->ds->index, | |
497 | .port = dp->index, | |
2acf4e6a AS |
498 | .addr = addr, |
499 | .vid = vid, | |
500 | ||
685fb6a4 | 501 | }; |
d1cffff0 | 502 | |
685fb6a4 | 503 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); |
d1cffff0 VD |
504 | } |
505 | ||
de40fc5d VD |
506 | int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) |
507 | { | |
508 | struct dsa_switch *ds = dp->ds; | |
509 | int port = dp->index; | |
510 | ||
511 | if (!ds->ops->port_fdb_dump) | |
512 | return -EOPNOTSUPP; | |
513 | ||
514 | return ds->ops->port_fdb_dump(ds, port, cb, data); | |
515 | } | |
516 | ||
bb9f6031 | 517 | int dsa_port_mdb_add(const struct dsa_port *dp, |
ffb68fc5 | 518 | const struct switchdev_obj_port_mdb *mdb) |
3a9afea3 | 519 | { |
8ae5bcdc VD |
520 | struct dsa_notifier_mdb_info info = { |
521 | .sw_index = dp->ds->index, | |
522 | .port = dp->index, | |
8ae5bcdc VD |
523 | .mdb = mdb, |
524 | }; | |
3a9afea3 | 525 | |
8ae5bcdc | 526 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); |
3a9afea3 VD |
527 | } |
528 | ||
bb9f6031 | 529 | int dsa_port_mdb_del(const struct dsa_port *dp, |
3a9afea3 VD |
530 | const struct switchdev_obj_port_mdb *mdb) |
531 | { | |
8ae5bcdc VD |
532 | struct dsa_notifier_mdb_info info = { |
533 | .sw_index = dp->ds->index, | |
534 | .port = dp->index, | |
535 | .mdb = mdb, | |
536 | }; | |
3a9afea3 | 537 | |
8ae5bcdc | 538 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); |
3a9afea3 VD |
539 | } |
540 | ||
076e7133 | 541 | int dsa_port_vlan_add(struct dsa_port *dp, |
31046a5f VO |
542 | const struct switchdev_obj_port_vlan *vlan, |
543 | struct netlink_ext_ack *extack) | |
076e7133 | 544 | { |
d0c627b8 VD |
545 | struct dsa_notifier_vlan_info info = { |
546 | .sw_index = dp->ds->index, | |
547 | .port = dp->index, | |
d0c627b8 | 548 | .vlan = vlan, |
31046a5f | 549 | .extack = extack, |
d0c627b8 | 550 | }; |
076e7133 | 551 | |
c5335d73 | 552 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); |
076e7133 VD |
553 | } |
554 | ||
555 | int dsa_port_vlan_del(struct dsa_port *dp, | |
556 | const struct switchdev_obj_port_vlan *vlan) | |
557 | { | |
d0c627b8 VD |
558 | struct dsa_notifier_vlan_info info = { |
559 | .sw_index = dp->ds->index, | |
560 | .port = dp->index, | |
561 | .vlan = vlan, | |
562 | }; | |
076e7133 | 563 | |
c5335d73 | 564 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); |
076e7133 | 565 | } |
57ab1ca2 | 566 | |
c595c433 HV |
567 | int dsa_port_mrp_add(const struct dsa_port *dp, |
568 | const struct switchdev_obj_mrp *mrp) | |
569 | { | |
570 | struct dsa_notifier_mrp_info info = { | |
571 | .sw_index = dp->ds->index, | |
572 | .port = dp->index, | |
573 | .mrp = mrp, | |
574 | }; | |
575 | ||
576 | return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info); | |
577 | } | |
578 | ||
579 | int dsa_port_mrp_del(const struct dsa_port *dp, | |
580 | const struct switchdev_obj_mrp *mrp) | |
581 | { | |
582 | struct dsa_notifier_mrp_info info = { | |
583 | .sw_index = dp->ds->index, | |
584 | .port = dp->index, | |
585 | .mrp = mrp, | |
586 | }; | |
587 | ||
588 | return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info); | |
589 | } | |
590 | ||
591 | int dsa_port_mrp_add_ring_role(const struct dsa_port *dp, | |
592 | const struct switchdev_obj_ring_role_mrp *mrp) | |
593 | { | |
594 | struct dsa_notifier_mrp_ring_role_info info = { | |
595 | .sw_index = dp->ds->index, | |
596 | .port = dp->index, | |
597 | .mrp = mrp, | |
598 | }; | |
599 | ||
600 | return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info); | |
601 | } | |
602 | ||
603 | int dsa_port_mrp_del_ring_role(const struct dsa_port *dp, | |
604 | const struct switchdev_obj_ring_role_mrp *mrp) | |
605 | { | |
606 | struct dsa_notifier_mrp_ring_role_info info = { | |
607 | .sw_index = dp->ds->index, | |
608 | .port = dp->index, | |
609 | .mrp = mrp, | |
610 | }; | |
611 | ||
612 | return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info); | |
613 | } | |
614 | ||
53da0eba VO |
615 | void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp, |
616 | const struct dsa_device_ops *tag_ops) | |
617 | { | |
618 | cpu_dp->filter = tag_ops->filter; | |
619 | cpu_dp->rcv = tag_ops->rcv; | |
620 | cpu_dp->tag_ops = tag_ops; | |
621 | } | |
622 | ||
6207a78c | 623 | static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp) |
33615367 | 624 | { |
33615367 | 625 | struct device_node *phy_dn; |
33615367 | 626 | struct phy_device *phydev; |
33615367 | 627 | |
6207a78c | 628 | phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0); |
33615367 | 629 | if (!phy_dn) |
6207a78c | 630 | return NULL; |
33615367 SR |
631 | |
632 | phydev = of_phy_find_device(phy_dn); | |
633 | if (!phydev) { | |
6207a78c FF |
634 | of_node_put(phy_dn); |
635 | return ERR_PTR(-EPROBE_DEFER); | |
33615367 SR |
636 | } |
637 | ||
9919a363 | 638 | of_node_put(phy_dn); |
6207a78c FF |
639 | return phydev; |
640 | } | |
641 | ||
8ae67496 FF |
642 | static void dsa_port_phylink_validate(struct phylink_config *config, |
643 | unsigned long *supported, | |
644 | struct phylink_link_state *state) | |
77373d49 IC |
645 | { |
646 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
647 | struct dsa_switch *ds = dp->ds; | |
648 | ||
649 | if (!ds->ops->phylink_validate) | |
650 | return; | |
651 | ||
652 | ds->ops->phylink_validate(ds, dp->index, supported, state); | |
653 | } | |
77373d49 | 654 | |
8ae67496 FF |
655 | static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config, |
656 | struct phylink_link_state *state) | |
77373d49 IC |
657 | { |
658 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
659 | struct dsa_switch *ds = dp->ds; | |
87615c96 | 660 | int err; |
77373d49 | 661 | |
d46b7e4f RK |
662 | /* Only called for inband modes */ |
663 | if (!ds->ops->phylink_mac_link_state) { | |
664 | state->link = 0; | |
665 | return; | |
666 | } | |
77373d49 | 667 | |
87615c96 RK |
668 | err = ds->ops->phylink_mac_link_state(ds, dp->index, state); |
669 | if (err < 0) { | |
670 | dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n", | |
671 | dp->index, err); | |
d46b7e4f | 672 | state->link = 0; |
87615c96 | 673 | } |
77373d49 | 674 | } |
77373d49 | 675 | |
8ae67496 FF |
676 | static void dsa_port_phylink_mac_config(struct phylink_config *config, |
677 | unsigned int mode, | |
678 | const struct phylink_link_state *state) | |
77373d49 IC |
679 | { |
680 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
681 | struct dsa_switch *ds = dp->ds; | |
682 | ||
683 | if (!ds->ops->phylink_mac_config) | |
684 | return; | |
685 | ||
686 | ds->ops->phylink_mac_config(ds, dp->index, mode, state); | |
687 | } | |
77373d49 | 688 | |
8ae67496 | 689 | static void dsa_port_phylink_mac_an_restart(struct phylink_config *config) |
77373d49 IC |
690 | { |
691 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
692 | struct dsa_switch *ds = dp->ds; | |
693 | ||
694 | if (!ds->ops->phylink_mac_an_restart) | |
695 | return; | |
696 | ||
697 | ds->ops->phylink_mac_an_restart(ds, dp->index); | |
698 | } | |
77373d49 | 699 | |
8ae67496 FF |
700 | static void dsa_port_phylink_mac_link_down(struct phylink_config *config, |
701 | unsigned int mode, | |
702 | phy_interface_t interface) | |
77373d49 IC |
703 | { |
704 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
0e279218 | 705 | struct phy_device *phydev = NULL; |
77373d49 IC |
706 | struct dsa_switch *ds = dp->ds; |
707 | ||
0e279218 IC |
708 | if (dsa_is_user_port(ds, dp->index)) |
709 | phydev = dp->slave->phydev; | |
710 | ||
77373d49 | 711 | if (!ds->ops->phylink_mac_link_down) { |
0e279218 IC |
712 | if (ds->ops->adjust_link && phydev) |
713 | ds->ops->adjust_link(ds, dp->index, phydev); | |
77373d49 IC |
714 | return; |
715 | } | |
716 | ||
717 | ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); | |
718 | } | |
77373d49 | 719 | |
8ae67496 | 720 | static void dsa_port_phylink_mac_link_up(struct phylink_config *config, |
91a208f2 | 721 | struct phy_device *phydev, |
8ae67496 FF |
722 | unsigned int mode, |
723 | phy_interface_t interface, | |
91a208f2 RK |
724 | int speed, int duplex, |
725 | bool tx_pause, bool rx_pause) | |
77373d49 IC |
726 | { |
727 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
77373d49 IC |
728 | struct dsa_switch *ds = dp->ds; |
729 | ||
730 | if (!ds->ops->phylink_mac_link_up) { | |
0e279218 IC |
731 | if (ds->ops->adjust_link && phydev) |
732 | ds->ops->adjust_link(ds, dp->index, phydev); | |
77373d49 IC |
733 | return; |
734 | } | |
735 | ||
5b502a7b RK |
736 | ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev, |
737 | speed, duplex, tx_pause, rx_pause); | |
77373d49 | 738 | } |
77373d49 IC |
739 | |
740 | const struct phylink_mac_ops dsa_port_phylink_mac_ops = { | |
741 | .validate = dsa_port_phylink_validate, | |
d46b7e4f | 742 | .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state, |
77373d49 IC |
743 | .mac_config = dsa_port_phylink_mac_config, |
744 | .mac_an_restart = dsa_port_phylink_mac_an_restart, | |
745 | .mac_link_down = dsa_port_phylink_mac_link_down, | |
746 | .mac_link_up = dsa_port_phylink_mac_link_up, | |
747 | }; | |
748 | ||
6207a78c FF |
749 | static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) |
750 | { | |
751 | struct dsa_switch *ds = dp->ds; | |
752 | struct phy_device *phydev; | |
753 | int port = dp->index; | |
754 | int err = 0; | |
755 | ||
756 | phydev = dsa_port_get_phy_device(dp); | |
757 | if (!phydev) | |
758 | return 0; | |
759 | ||
760 | if (IS_ERR(phydev)) | |
761 | return PTR_ERR(phydev); | |
762 | ||
33615367 | 763 | if (enable) { |
33615367 SR |
764 | err = genphy_resume(phydev); |
765 | if (err < 0) | |
766 | goto err_put_dev; | |
767 | ||
768 | err = genphy_read_status(phydev); | |
769 | if (err < 0) | |
770 | goto err_put_dev; | |
771 | } else { | |
772 | err = genphy_suspend(phydev); | |
773 | if (err < 0) | |
774 | goto err_put_dev; | |
775 | } | |
776 | ||
777 | if (ds->ops->adjust_link) | |
778 | ds->ops->adjust_link(ds, port, phydev); | |
779 | ||
780 | dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev)); | |
781 | ||
782 | err_put_dev: | |
783 | put_device(&phydev->mdio.dev); | |
33615367 SR |
784 | return err; |
785 | } | |
786 | ||
787 | static int dsa_port_fixed_link_register_of(struct dsa_port *dp) | |
57ab1ca2 VD |
788 | { |
789 | struct device_node *dn = dp->dn; | |
790 | struct dsa_switch *ds = dp->ds; | |
791 | struct phy_device *phydev; | |
792 | int port = dp->index; | |
0c65b2b9 | 793 | phy_interface_t mode; |
57ab1ca2 VD |
794 | int err; |
795 | ||
33615367 SR |
796 | err = of_phy_register_fixed_link(dn); |
797 | if (err) { | |
798 | dev_err(ds->dev, | |
799 | "failed to register the fixed PHY of port %d\n", | |
800 | port); | |
801 | return err; | |
802 | } | |
57ab1ca2 | 803 | |
33615367 | 804 | phydev = of_phy_find_device(dn); |
57ab1ca2 | 805 | |
0c65b2b9 AL |
806 | err = of_get_phy_mode(dn, &mode); |
807 | if (err) | |
33615367 SR |
808 | mode = PHY_INTERFACE_MODE_NA; |
809 | phydev->interface = mode; | |
57ab1ca2 | 810 | |
33615367 | 811 | genphy_read_status(phydev); |
57ab1ca2 | 812 | |
33615367 SR |
813 | if (ds->ops->adjust_link) |
814 | ds->ops->adjust_link(ds, port, phydev); | |
57ab1ca2 | 815 | |
33615367 | 816 | put_device(&phydev->mdio.dev); |
57ab1ca2 VD |
817 | |
818 | return 0; | |
819 | } | |
820 | ||
0e279218 IC |
821 | static int dsa_port_phylink_register(struct dsa_port *dp) |
822 | { | |
823 | struct dsa_switch *ds = dp->ds; | |
824 | struct device_node *port_dn = dp->dn; | |
0c65b2b9 AL |
825 | phy_interface_t mode; |
826 | int err; | |
0e279218 | 827 | |
0c65b2b9 AL |
828 | err = of_get_phy_mode(port_dn, &mode); |
829 | if (err) | |
0e279218 IC |
830 | mode = PHY_INTERFACE_MODE_NA; |
831 | ||
832 | dp->pl_config.dev = ds->dev; | |
833 | dp->pl_config.type = PHYLINK_DEV; | |
787cac3f | 834 | dp->pl_config.pcs_poll = ds->pcs_poll; |
0e279218 IC |
835 | |
836 | dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), | |
837 | mode, &dsa_port_phylink_mac_ops); | |
838 | if (IS_ERR(dp->pl)) { | |
839 | pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); | |
840 | return PTR_ERR(dp->pl); | |
841 | } | |
842 | ||
843 | err = phylink_of_phy_connect(dp->pl, port_dn, 0); | |
2131fba5 | 844 | if (err && err != -ENODEV) { |
0e279218 IC |
845 | pr_err("could not attach to PHY: %d\n", err); |
846 | goto err_phy_connect; | |
847 | } | |
848 | ||
0e279218 IC |
849 | return 0; |
850 | ||
851 | err_phy_connect: | |
852 | phylink_destroy(dp->pl); | |
853 | return err; | |
854 | } | |
855 | ||
33615367 | 856 | int dsa_port_link_register_of(struct dsa_port *dp) |
57ab1ca2 | 857 | { |
0e279218 | 858 | struct dsa_switch *ds = dp->ds; |
a20f9970 | 859 | struct device_node *phy_np; |
3be98b2d | 860 | int port = dp->index; |
0e279218 | 861 | |
a20f9970 AL |
862 | if (!ds->ops->adjust_link) { |
863 | phy_np = of_parse_phandle(dp->dn, "phy-handle", 0); | |
3be98b2d AL |
864 | if (of_phy_is_fixed_link(dp->dn) || phy_np) { |
865 | if (ds->ops->phylink_mac_link_down) | |
866 | ds->ops->phylink_mac_link_down(ds, port, | |
867 | MLO_AN_FIXED, PHY_INTERFACE_MODE_NA); | |
a20f9970 | 868 | return dsa_port_phylink_register(dp); |
3be98b2d | 869 | } |
a20f9970 AL |
870 | return 0; |
871 | } | |
0e279218 IC |
872 | |
873 | dev_warn(ds->dev, | |
874 | "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); | |
875 | ||
33615367 SR |
876 | if (of_phy_is_fixed_link(dp->dn)) |
877 | return dsa_port_fixed_link_register_of(dp); | |
878 | else | |
879 | return dsa_port_setup_phy_of(dp, true); | |
880 | } | |
57ab1ca2 | 881 | |
33615367 SR |
882 | void dsa_port_link_unregister_of(struct dsa_port *dp) |
883 | { | |
0e279218 IC |
884 | struct dsa_switch *ds = dp->ds; |
885 | ||
a20f9970 | 886 | if (!ds->ops->adjust_link && dp->pl) { |
0e279218 IC |
887 | rtnl_lock(); |
888 | phylink_disconnect_phy(dp->pl); | |
889 | rtnl_unlock(); | |
890 | phylink_destroy(dp->pl); | |
a20f9970 | 891 | dp->pl = NULL; |
0e279218 IC |
892 | return; |
893 | } | |
894 | ||
33615367 SR |
895 | if (of_phy_is_fixed_link(dp->dn)) |
896 | of_phy_deregister_fixed_link(dp->dn); | |
897 | else | |
898 | dsa_port_setup_phy_of(dp, false); | |
57ab1ca2 | 899 | } |
cf963573 FF |
900 | |
901 | int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data) | |
902 | { | |
903 | struct phy_device *phydev; | |
904 | int ret = -EOPNOTSUPP; | |
905 | ||
906 | if (of_phy_is_fixed_link(dp->dn)) | |
907 | return ret; | |
908 | ||
909 | phydev = dsa_port_get_phy_device(dp); | |
910 | if (IS_ERR_OR_NULL(phydev)) | |
911 | return ret; | |
912 | ||
913 | ret = phy_ethtool_get_strings(phydev, data); | |
914 | put_device(&phydev->mdio.dev); | |
915 | ||
916 | return ret; | |
917 | } | |
918 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings); | |
919 | ||
920 | int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data) | |
921 | { | |
922 | struct phy_device *phydev; | |
923 | int ret = -EOPNOTSUPP; | |
924 | ||
925 | if (of_phy_is_fixed_link(dp->dn)) | |
926 | return ret; | |
927 | ||
928 | phydev = dsa_port_get_phy_device(dp); | |
929 | if (IS_ERR_OR_NULL(phydev)) | |
930 | return ret; | |
931 | ||
932 | ret = phy_ethtool_get_stats(phydev, NULL, data); | |
933 | put_device(&phydev->mdio.dev); | |
934 | ||
935 | return ret; | |
936 | } | |
937 | EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats); | |
938 | ||
939 | int dsa_port_get_phy_sset_count(struct dsa_port *dp) | |
940 | { | |
941 | struct phy_device *phydev; | |
942 | int ret = -EOPNOTSUPP; | |
943 | ||
944 | if (of_phy_is_fixed_link(dp->dn)) | |
945 | return ret; | |
946 | ||
947 | phydev = dsa_port_get_phy_device(dp); | |
948 | if (IS_ERR_OR_NULL(phydev)) | |
949 | return ret; | |
950 | ||
951 | ret = phy_ethtool_get_sset_count(phydev); | |
952 | put_device(&phydev->mdio.dev); | |
953 | ||
954 | return ret; | |
955 | } | |
956 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count); | |
18596f50 GM |
957 | |
958 | int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr) | |
959 | { | |
960 | struct dsa_notifier_hsr_info info = { | |
961 | .sw_index = dp->ds->index, | |
962 | .port = dp->index, | |
963 | .hsr = hsr, | |
964 | }; | |
965 | int err; | |
966 | ||
967 | dp->hsr_dev = hsr; | |
968 | ||
969 | err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info); | |
970 | if (err) | |
971 | dp->hsr_dev = NULL; | |
972 | ||
973 | return err; | |
974 | } | |
975 | ||
976 | void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr) | |
977 | { | |
978 | struct dsa_notifier_hsr_info info = { | |
979 | .sw_index = dp->ds->index, | |
980 | .port = dp->index, | |
981 | .hsr = hsr, | |
982 | }; | |
983 | int err; | |
984 | ||
985 | dp->hsr_dev = NULL; | |
986 | ||
987 | err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info); | |
988 | if (err) | |
989 | pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n"); | |
990 | } |