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 | ||
cfbed329 VD |
125 | int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br) |
126 | { | |
127 | struct dsa_notifier_bridge_info info = { | |
f66a6a69 | 128 | .tree_index = dp->ds->dst->index, |
cfbed329 VD |
129 | .sw_index = dp->ds->index, |
130 | .port = dp->index, | |
131 | .br = br, | |
132 | }; | |
133 | int err; | |
134 | ||
c1388063 | 135 | /* Set the flooding mode before joining the port in the switch */ |
bae33f2b | 136 | err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD); |
c1388063 RK |
137 | if (err) |
138 | return err; | |
139 | ||
140 | /* Here the interface is already bridged. Reflect the current | |
141 | * configuration so that drivers can program their chips accordingly. | |
cfbed329 VD |
142 | */ |
143 | dp->bridge_dev = br; | |
144 | ||
f66a6a69 | 145 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info); |
cfbed329 VD |
146 | |
147 | /* The bridging is rolled back on error */ | |
c1388063 | 148 | if (err) { |
bae33f2b | 149 | dsa_port_bridge_flags(dp, 0); |
cfbed329 | 150 | dp->bridge_dev = NULL; |
c1388063 | 151 | } |
cfbed329 VD |
152 | |
153 | return err; | |
154 | } | |
155 | ||
156 | void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) | |
157 | { | |
158 | struct dsa_notifier_bridge_info info = { | |
f66a6a69 | 159 | .tree_index = dp->ds->dst->index, |
cfbed329 VD |
160 | .sw_index = dp->ds->index, |
161 | .port = dp->index, | |
162 | .br = br, | |
163 | }; | |
164 | int err; | |
165 | ||
166 | /* Here the port is already unbridged. Reflect the current configuration | |
167 | * so that drivers can program their chips accordingly. | |
168 | */ | |
169 | dp->bridge_dev = NULL; | |
170 | ||
f66a6a69 | 171 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
cfbed329 VD |
172 | if (err) |
173 | pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n"); | |
174 | ||
c1388063 | 175 | /* Port is leaving the bridge, disable flooding */ |
bae33f2b | 176 | dsa_port_bridge_flags(dp, 0); |
c1388063 | 177 | |
cfbed329 VD |
178 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
179 | * so allow it to be in BR_STATE_FORWARDING to be kept functional | |
180 | */ | |
181 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); | |
182 | } | |
4d61d304 | 183 | |
058102a6 TW |
184 | int dsa_port_lag_change(struct dsa_port *dp, |
185 | struct netdev_lag_lower_state_info *linfo) | |
186 | { | |
187 | struct dsa_notifier_lag_info info = { | |
188 | .sw_index = dp->ds->index, | |
189 | .port = dp->index, | |
190 | }; | |
191 | bool tx_enabled; | |
192 | ||
193 | if (!dp->lag_dev) | |
194 | return 0; | |
195 | ||
196 | /* On statically configured aggregates (e.g. loadbalance | |
197 | * without LACP) ports will always be tx_enabled, even if the | |
198 | * link is down. Thus we require both link_up and tx_enabled | |
199 | * in order to include it in the tx set. | |
200 | */ | |
201 | tx_enabled = linfo->link_up && linfo->tx_enabled; | |
202 | ||
203 | if (tx_enabled == dp->lag_tx_enabled) | |
204 | return 0; | |
205 | ||
206 | dp->lag_tx_enabled = tx_enabled; | |
207 | ||
208 | return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info); | |
209 | } | |
210 | ||
211 | int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag, | |
212 | struct netdev_lag_upper_info *uinfo) | |
213 | { | |
214 | struct dsa_notifier_lag_info info = { | |
215 | .sw_index = dp->ds->index, | |
216 | .port = dp->index, | |
217 | .lag = lag, | |
218 | .info = uinfo, | |
219 | }; | |
220 | int err; | |
221 | ||
222 | dsa_lag_map(dp->ds->dst, lag); | |
223 | dp->lag_dev = lag; | |
224 | ||
225 | err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info); | |
226 | if (err) { | |
227 | dp->lag_dev = NULL; | |
228 | dsa_lag_unmap(dp->ds->dst, lag); | |
229 | } | |
230 | ||
231 | return err; | |
232 | } | |
233 | ||
234 | void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag) | |
235 | { | |
236 | struct dsa_notifier_lag_info info = { | |
237 | .sw_index = dp->ds->index, | |
238 | .port = dp->index, | |
239 | .lag = lag, | |
240 | }; | |
241 | int err; | |
242 | ||
243 | if (!dp->lag_dev) | |
244 | return; | |
245 | ||
246 | /* Port might have been part of a LAG that in turn was | |
247 | * attached to a bridge. | |
248 | */ | |
249 | if (dp->bridge_dev) | |
250 | dsa_port_bridge_leave(dp, dp->bridge_dev); | |
251 | ||
252 | dp->lag_tx_enabled = false; | |
253 | dp->lag_dev = NULL; | |
254 | ||
255 | err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); | |
256 | if (err) | |
257 | pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n", | |
258 | err); | |
259 | ||
260 | dsa_lag_unmap(dp->ds->dst, lag); | |
261 | } | |
262 | ||
adb256eb | 263 | /* Must be called under rcu_read_lock() */ |
8f5d16f6 VO |
264 | static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp, |
265 | bool vlan_filtering) | |
266 | { | |
267 | struct dsa_switch *ds = dp->ds; | |
adb256eb VO |
268 | int err, i; |
269 | ||
270 | /* VLAN awareness was off, so the question is "can we turn it on". | |
271 | * We may have had 8021q uppers, those need to go. Make sure we don't | |
272 | * enter an inconsistent state: deny changing the VLAN awareness state | |
273 | * as long as we have 8021q uppers. | |
274 | */ | |
275 | if (vlan_filtering && dsa_is_user_port(ds, dp->index)) { | |
276 | struct net_device *upper_dev, *slave = dp->slave; | |
277 | struct net_device *br = dp->bridge_dev; | |
278 | struct list_head *iter; | |
279 | ||
280 | netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { | |
281 | struct bridge_vlan_info br_info; | |
282 | u16 vid; | |
283 | ||
284 | if (!is_vlan_dev(upper_dev)) | |
285 | continue; | |
286 | ||
287 | vid = vlan_dev_vlan_id(upper_dev); | |
288 | ||
289 | /* br_vlan_get_info() returns -EINVAL or -ENOENT if the | |
290 | * device, respectively the VID is not found, returning | |
291 | * 0 means success, which is a failure for us here. | |
292 | */ | |
293 | err = br_vlan_get_info(br, vid, &br_info); | |
294 | if (err == 0) { | |
295 | dev_err(ds->dev, "Must remove upper %s first\n", | |
296 | upper_dev->name); | |
297 | return false; | |
298 | } | |
299 | } | |
300 | } | |
8f5d16f6 VO |
301 | |
302 | if (!ds->vlan_filtering_is_global) | |
303 | return true; | |
304 | ||
305 | /* For cases where enabling/disabling VLAN awareness is global to the | |
306 | * switch, we need to handle the case where multiple bridges span | |
307 | * different ports of the same switch device and one of them has a | |
308 | * different setting than what is being requested. | |
309 | */ | |
310 | for (i = 0; i < ds->num_ports; i++) { | |
311 | struct net_device *other_bridge; | |
312 | ||
313 | other_bridge = dsa_to_port(ds, i)->bridge_dev; | |
314 | if (!other_bridge) | |
315 | continue; | |
316 | /* If it's the same bridge, it also has same | |
317 | * vlan_filtering setting => no need to check | |
318 | */ | |
319 | if (other_bridge == dp->bridge_dev) | |
320 | continue; | |
321 | if (br_vlan_enabled(other_bridge) != vlan_filtering) { | |
322 | dev_err(ds->dev, "VLAN filtering is a global setting\n"); | |
323 | return false; | |
324 | } | |
325 | } | |
326 | return true; | |
327 | } | |
328 | ||
bae33f2b | 329 | int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering) |
4d61d304 VD |
330 | { |
331 | struct dsa_switch *ds = dp->ds; | |
bae33f2b | 332 | bool apply; |
33162e9a | 333 | int err; |
4d61d304 | 334 | |
bae33f2b VO |
335 | if (!ds->ops->port_vlan_filtering) |
336 | return -EOPNOTSUPP; | |
adb256eb | 337 | |
bae33f2b VO |
338 | /* We are called from dsa_slave_switchdev_blocking_event(), |
339 | * which is not under rcu_read_lock(), unlike | |
340 | * dsa_slave_switchdev_event(). | |
341 | */ | |
342 | rcu_read_lock(); | |
343 | apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering); | |
344 | rcu_read_unlock(); | |
345 | if (!apply) | |
346 | return -EINVAL; | |
8f5d16f6 | 347 | |
ec9121e7 VO |
348 | if (dsa_port_is_vlan_filtering(dp) == vlan_filtering) |
349 | return 0; | |
350 | ||
bae33f2b | 351 | err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering); |
8f5d16f6 VO |
352 | if (err) |
353 | return err; | |
354 | ||
bae33f2b VO |
355 | if (ds->vlan_filtering_is_global) |
356 | ds->vlan_filtering = vlan_filtering; | |
357 | else | |
358 | dp->vlan_filtering = vlan_filtering; | |
2e554a7a | 359 | |
4d61d304 VD |
360 | return 0; |
361 | } | |
d87bd94e | 362 | |
54a0ed0d RK |
363 | /* This enforces legacy behavior for switch drivers which assume they can't |
364 | * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0 | |
365 | */ | |
366 | bool dsa_port_skip_vlan_configuration(struct dsa_port *dp) | |
367 | { | |
368 | struct dsa_switch *ds = dp->ds; | |
369 | ||
370 | if (!dp->bridge_dev) | |
371 | return false; | |
372 | ||
373 | return (!ds->configure_vlan_while_not_filtering && | |
374 | !br_vlan_enabled(dp->bridge_dev)); | |
375 | } | |
376 | ||
bae33f2b | 377 | int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock) |
d87bd94e VD |
378 | { |
379 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); | |
380 | unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); | |
bae33f2b | 381 | struct dsa_notifier_ageing_time_info info; |
bae33f2b VO |
382 | int err; |
383 | ||
384 | info.ageing_time = ageing_time; | |
d87bd94e | 385 | |
bae33f2b VO |
386 | err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
387 | if (err) | |
388 | return err; | |
d87bd94e | 389 | |
d87bd94e | 390 | dp->ageing_time = ageing_time; |
d87bd94e | 391 | |
77b61365 | 392 | return 0; |
d87bd94e | 393 | } |
d1cffff0 | 394 | |
bae33f2b | 395 | int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags) |
ea87005a FF |
396 | { |
397 | struct dsa_switch *ds = dp->ds; | |
398 | ||
399 | if (!ds->ops->port_egress_floods || | |
400 | (flags & ~(BR_FLOOD | BR_MCAST_FLOOD))) | |
401 | return -EINVAL; | |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
bae33f2b | 406 | int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags) |
57652796 RK |
407 | { |
408 | struct dsa_switch *ds = dp->ds; | |
409 | int port = dp->index; | |
410 | int err = 0; | |
411 | ||
57652796 RK |
412 | if (ds->ops->port_egress_floods) |
413 | err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD, | |
414 | flags & BR_MCAST_FLOOD); | |
415 | ||
416 | return err; | |
417 | } | |
418 | ||
bae33f2b | 419 | int dsa_port_mrouter(struct dsa_port *dp, bool mrouter) |
08cc83cc VD |
420 | { |
421 | struct dsa_switch *ds = dp->ds; | |
422 | int port = dp->index; | |
423 | ||
bae33f2b VO |
424 | if (!ds->ops->port_egress_floods) |
425 | return -EOPNOTSUPP; | |
08cc83cc VD |
426 | |
427 | return ds->ops->port_egress_floods(ds, port, true, mrouter); | |
428 | } | |
429 | ||
bfcb8132 VO |
430 | int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu, |
431 | bool propagate_upstream) | |
432 | { | |
433 | struct dsa_notifier_mtu_info info = { | |
434 | .sw_index = dp->ds->index, | |
435 | .propagate_upstream = propagate_upstream, | |
436 | .port = dp->index, | |
437 | .mtu = new_mtu, | |
438 | }; | |
439 | ||
440 | return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info); | |
441 | } | |
442 | ||
2acf4e6a AS |
443 | int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
444 | u16 vid) | |
d1cffff0 | 445 | { |
685fb6a4 VD |
446 | struct dsa_notifier_fdb_info info = { |
447 | .sw_index = dp->ds->index, | |
448 | .port = dp->index, | |
2acf4e6a AS |
449 | .addr = addr, |
450 | .vid = vid, | |
685fb6a4 | 451 | }; |
d1cffff0 | 452 | |
685fb6a4 | 453 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); |
d1cffff0 VD |
454 | } |
455 | ||
2acf4e6a AS |
456 | int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
457 | u16 vid) | |
d1cffff0 | 458 | { |
685fb6a4 VD |
459 | struct dsa_notifier_fdb_info info = { |
460 | .sw_index = dp->ds->index, | |
461 | .port = dp->index, | |
2acf4e6a AS |
462 | .addr = addr, |
463 | .vid = vid, | |
464 | ||
685fb6a4 | 465 | }; |
d1cffff0 | 466 | |
685fb6a4 | 467 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); |
d1cffff0 VD |
468 | } |
469 | ||
de40fc5d VD |
470 | int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) |
471 | { | |
472 | struct dsa_switch *ds = dp->ds; | |
473 | int port = dp->index; | |
474 | ||
475 | if (!ds->ops->port_fdb_dump) | |
476 | return -EOPNOTSUPP; | |
477 | ||
478 | return ds->ops->port_fdb_dump(ds, port, cb, data); | |
479 | } | |
480 | ||
bb9f6031 | 481 | int dsa_port_mdb_add(const struct dsa_port *dp, |
ffb68fc5 | 482 | const struct switchdev_obj_port_mdb *mdb) |
3a9afea3 | 483 | { |
8ae5bcdc VD |
484 | struct dsa_notifier_mdb_info info = { |
485 | .sw_index = dp->ds->index, | |
486 | .port = dp->index, | |
8ae5bcdc VD |
487 | .mdb = mdb, |
488 | }; | |
3a9afea3 | 489 | |
8ae5bcdc | 490 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); |
3a9afea3 VD |
491 | } |
492 | ||
bb9f6031 | 493 | int dsa_port_mdb_del(const struct dsa_port *dp, |
3a9afea3 VD |
494 | const struct switchdev_obj_port_mdb *mdb) |
495 | { | |
8ae5bcdc VD |
496 | struct dsa_notifier_mdb_info info = { |
497 | .sw_index = dp->ds->index, | |
498 | .port = dp->index, | |
499 | .mdb = mdb, | |
500 | }; | |
3a9afea3 | 501 | |
8ae5bcdc | 502 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); |
3a9afea3 VD |
503 | } |
504 | ||
076e7133 | 505 | int dsa_port_vlan_add(struct dsa_port *dp, |
ffb68fc5 | 506 | const struct switchdev_obj_port_vlan *vlan) |
076e7133 | 507 | { |
d0c627b8 VD |
508 | struct dsa_notifier_vlan_info info = { |
509 | .sw_index = dp->ds->index, | |
510 | .port = dp->index, | |
d0c627b8 VD |
511 | .vlan = vlan, |
512 | }; | |
076e7133 | 513 | |
c5335d73 | 514 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); |
076e7133 VD |
515 | } |
516 | ||
517 | int dsa_port_vlan_del(struct dsa_port *dp, | |
518 | const struct switchdev_obj_port_vlan *vlan) | |
519 | { | |
d0c627b8 VD |
520 | struct dsa_notifier_vlan_info info = { |
521 | .sw_index = dp->ds->index, | |
522 | .port = dp->index, | |
523 | .vlan = vlan, | |
524 | }; | |
076e7133 | 525 | |
c5335d73 | 526 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); |
076e7133 | 527 | } |
57ab1ca2 | 528 | |
53da0eba VO |
529 | void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp, |
530 | const struct dsa_device_ops *tag_ops) | |
531 | { | |
532 | cpu_dp->filter = tag_ops->filter; | |
533 | cpu_dp->rcv = tag_ops->rcv; | |
534 | cpu_dp->tag_ops = tag_ops; | |
535 | } | |
536 | ||
6207a78c | 537 | static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp) |
33615367 | 538 | { |
33615367 | 539 | struct device_node *phy_dn; |
33615367 | 540 | struct phy_device *phydev; |
33615367 | 541 | |
6207a78c | 542 | phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0); |
33615367 | 543 | if (!phy_dn) |
6207a78c | 544 | return NULL; |
33615367 SR |
545 | |
546 | phydev = of_phy_find_device(phy_dn); | |
547 | if (!phydev) { | |
6207a78c FF |
548 | of_node_put(phy_dn); |
549 | return ERR_PTR(-EPROBE_DEFER); | |
33615367 SR |
550 | } |
551 | ||
9919a363 | 552 | of_node_put(phy_dn); |
6207a78c FF |
553 | return phydev; |
554 | } | |
555 | ||
8ae67496 FF |
556 | static void dsa_port_phylink_validate(struct phylink_config *config, |
557 | unsigned long *supported, | |
558 | struct phylink_link_state *state) | |
77373d49 IC |
559 | { |
560 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
561 | struct dsa_switch *ds = dp->ds; | |
562 | ||
563 | if (!ds->ops->phylink_validate) | |
564 | return; | |
565 | ||
566 | ds->ops->phylink_validate(ds, dp->index, supported, state); | |
567 | } | |
77373d49 | 568 | |
8ae67496 FF |
569 | static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config, |
570 | struct phylink_link_state *state) | |
77373d49 IC |
571 | { |
572 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
573 | struct dsa_switch *ds = dp->ds; | |
87615c96 | 574 | int err; |
77373d49 | 575 | |
d46b7e4f RK |
576 | /* Only called for inband modes */ |
577 | if (!ds->ops->phylink_mac_link_state) { | |
578 | state->link = 0; | |
579 | return; | |
580 | } | |
77373d49 | 581 | |
87615c96 RK |
582 | err = ds->ops->phylink_mac_link_state(ds, dp->index, state); |
583 | if (err < 0) { | |
584 | dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n", | |
585 | dp->index, err); | |
d46b7e4f | 586 | state->link = 0; |
87615c96 | 587 | } |
77373d49 | 588 | } |
77373d49 | 589 | |
8ae67496 FF |
590 | static void dsa_port_phylink_mac_config(struct phylink_config *config, |
591 | unsigned int mode, | |
592 | const struct phylink_link_state *state) | |
77373d49 IC |
593 | { |
594 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
595 | struct dsa_switch *ds = dp->ds; | |
596 | ||
597 | if (!ds->ops->phylink_mac_config) | |
598 | return; | |
599 | ||
600 | ds->ops->phylink_mac_config(ds, dp->index, mode, state); | |
601 | } | |
77373d49 | 602 | |
8ae67496 | 603 | static void dsa_port_phylink_mac_an_restart(struct phylink_config *config) |
77373d49 IC |
604 | { |
605 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
606 | struct dsa_switch *ds = dp->ds; | |
607 | ||
608 | if (!ds->ops->phylink_mac_an_restart) | |
609 | return; | |
610 | ||
611 | ds->ops->phylink_mac_an_restart(ds, dp->index); | |
612 | } | |
77373d49 | 613 | |
8ae67496 FF |
614 | static void dsa_port_phylink_mac_link_down(struct phylink_config *config, |
615 | unsigned int mode, | |
616 | phy_interface_t interface) | |
77373d49 IC |
617 | { |
618 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
0e279218 | 619 | struct phy_device *phydev = NULL; |
77373d49 IC |
620 | struct dsa_switch *ds = dp->ds; |
621 | ||
0e279218 IC |
622 | if (dsa_is_user_port(ds, dp->index)) |
623 | phydev = dp->slave->phydev; | |
624 | ||
77373d49 | 625 | if (!ds->ops->phylink_mac_link_down) { |
0e279218 IC |
626 | if (ds->ops->adjust_link && phydev) |
627 | ds->ops->adjust_link(ds, dp->index, phydev); | |
77373d49 IC |
628 | return; |
629 | } | |
630 | ||
631 | ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); | |
632 | } | |
77373d49 | 633 | |
8ae67496 | 634 | static void dsa_port_phylink_mac_link_up(struct phylink_config *config, |
91a208f2 | 635 | struct phy_device *phydev, |
8ae67496 FF |
636 | unsigned int mode, |
637 | phy_interface_t interface, | |
91a208f2 RK |
638 | int speed, int duplex, |
639 | bool tx_pause, bool rx_pause) | |
77373d49 IC |
640 | { |
641 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); | |
77373d49 IC |
642 | struct dsa_switch *ds = dp->ds; |
643 | ||
644 | if (!ds->ops->phylink_mac_link_up) { | |
0e279218 IC |
645 | if (ds->ops->adjust_link && phydev) |
646 | ds->ops->adjust_link(ds, dp->index, phydev); | |
77373d49 IC |
647 | return; |
648 | } | |
649 | ||
5b502a7b RK |
650 | ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev, |
651 | speed, duplex, tx_pause, rx_pause); | |
77373d49 | 652 | } |
77373d49 IC |
653 | |
654 | const struct phylink_mac_ops dsa_port_phylink_mac_ops = { | |
655 | .validate = dsa_port_phylink_validate, | |
d46b7e4f | 656 | .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state, |
77373d49 IC |
657 | .mac_config = dsa_port_phylink_mac_config, |
658 | .mac_an_restart = dsa_port_phylink_mac_an_restart, | |
659 | .mac_link_down = dsa_port_phylink_mac_link_down, | |
660 | .mac_link_up = dsa_port_phylink_mac_link_up, | |
661 | }; | |
662 | ||
6207a78c FF |
663 | static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) |
664 | { | |
665 | struct dsa_switch *ds = dp->ds; | |
666 | struct phy_device *phydev; | |
667 | int port = dp->index; | |
668 | int err = 0; | |
669 | ||
670 | phydev = dsa_port_get_phy_device(dp); | |
671 | if (!phydev) | |
672 | return 0; | |
673 | ||
674 | if (IS_ERR(phydev)) | |
675 | return PTR_ERR(phydev); | |
676 | ||
33615367 | 677 | if (enable) { |
33615367 SR |
678 | err = genphy_resume(phydev); |
679 | if (err < 0) | |
680 | goto err_put_dev; | |
681 | ||
682 | err = genphy_read_status(phydev); | |
683 | if (err < 0) | |
684 | goto err_put_dev; | |
685 | } else { | |
686 | err = genphy_suspend(phydev); | |
687 | if (err < 0) | |
688 | goto err_put_dev; | |
689 | } | |
690 | ||
691 | if (ds->ops->adjust_link) | |
692 | ds->ops->adjust_link(ds, port, phydev); | |
693 | ||
694 | dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev)); | |
695 | ||
696 | err_put_dev: | |
697 | put_device(&phydev->mdio.dev); | |
33615367 SR |
698 | return err; |
699 | } | |
700 | ||
701 | static int dsa_port_fixed_link_register_of(struct dsa_port *dp) | |
57ab1ca2 VD |
702 | { |
703 | struct device_node *dn = dp->dn; | |
704 | struct dsa_switch *ds = dp->ds; | |
705 | struct phy_device *phydev; | |
706 | int port = dp->index; | |
0c65b2b9 | 707 | phy_interface_t mode; |
57ab1ca2 VD |
708 | int err; |
709 | ||
33615367 SR |
710 | err = of_phy_register_fixed_link(dn); |
711 | if (err) { | |
712 | dev_err(ds->dev, | |
713 | "failed to register the fixed PHY of port %d\n", | |
714 | port); | |
715 | return err; | |
716 | } | |
57ab1ca2 | 717 | |
33615367 | 718 | phydev = of_phy_find_device(dn); |
57ab1ca2 | 719 | |
0c65b2b9 AL |
720 | err = of_get_phy_mode(dn, &mode); |
721 | if (err) | |
33615367 SR |
722 | mode = PHY_INTERFACE_MODE_NA; |
723 | phydev->interface = mode; | |
57ab1ca2 | 724 | |
33615367 | 725 | genphy_read_status(phydev); |
57ab1ca2 | 726 | |
33615367 SR |
727 | if (ds->ops->adjust_link) |
728 | ds->ops->adjust_link(ds, port, phydev); | |
57ab1ca2 | 729 | |
33615367 | 730 | put_device(&phydev->mdio.dev); |
57ab1ca2 VD |
731 | |
732 | return 0; | |
733 | } | |
734 | ||
0e279218 IC |
735 | static int dsa_port_phylink_register(struct dsa_port *dp) |
736 | { | |
737 | struct dsa_switch *ds = dp->ds; | |
738 | struct device_node *port_dn = dp->dn; | |
0c65b2b9 AL |
739 | phy_interface_t mode; |
740 | int err; | |
0e279218 | 741 | |
0c65b2b9 AL |
742 | err = of_get_phy_mode(port_dn, &mode); |
743 | if (err) | |
0e279218 IC |
744 | mode = PHY_INTERFACE_MODE_NA; |
745 | ||
746 | dp->pl_config.dev = ds->dev; | |
747 | dp->pl_config.type = PHYLINK_DEV; | |
787cac3f | 748 | dp->pl_config.pcs_poll = ds->pcs_poll; |
0e279218 IC |
749 | |
750 | dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), | |
751 | mode, &dsa_port_phylink_mac_ops); | |
752 | if (IS_ERR(dp->pl)) { | |
753 | pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); | |
754 | return PTR_ERR(dp->pl); | |
755 | } | |
756 | ||
757 | err = phylink_of_phy_connect(dp->pl, port_dn, 0); | |
2131fba5 | 758 | if (err && err != -ENODEV) { |
0e279218 IC |
759 | pr_err("could not attach to PHY: %d\n", err); |
760 | goto err_phy_connect; | |
761 | } | |
762 | ||
0e279218 IC |
763 | return 0; |
764 | ||
765 | err_phy_connect: | |
766 | phylink_destroy(dp->pl); | |
767 | return err; | |
768 | } | |
769 | ||
33615367 | 770 | int dsa_port_link_register_of(struct dsa_port *dp) |
57ab1ca2 | 771 | { |
0e279218 | 772 | struct dsa_switch *ds = dp->ds; |
a20f9970 | 773 | struct device_node *phy_np; |
3be98b2d | 774 | int port = dp->index; |
0e279218 | 775 | |
a20f9970 AL |
776 | if (!ds->ops->adjust_link) { |
777 | phy_np = of_parse_phandle(dp->dn, "phy-handle", 0); | |
3be98b2d AL |
778 | if (of_phy_is_fixed_link(dp->dn) || phy_np) { |
779 | if (ds->ops->phylink_mac_link_down) | |
780 | ds->ops->phylink_mac_link_down(ds, port, | |
781 | MLO_AN_FIXED, PHY_INTERFACE_MODE_NA); | |
a20f9970 | 782 | return dsa_port_phylink_register(dp); |
3be98b2d | 783 | } |
a20f9970 AL |
784 | return 0; |
785 | } | |
0e279218 IC |
786 | |
787 | dev_warn(ds->dev, | |
788 | "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); | |
789 | ||
33615367 SR |
790 | if (of_phy_is_fixed_link(dp->dn)) |
791 | return dsa_port_fixed_link_register_of(dp); | |
792 | else | |
793 | return dsa_port_setup_phy_of(dp, true); | |
794 | } | |
57ab1ca2 | 795 | |
33615367 SR |
796 | void dsa_port_link_unregister_of(struct dsa_port *dp) |
797 | { | |
0e279218 IC |
798 | struct dsa_switch *ds = dp->ds; |
799 | ||
a20f9970 | 800 | if (!ds->ops->adjust_link && dp->pl) { |
0e279218 IC |
801 | rtnl_lock(); |
802 | phylink_disconnect_phy(dp->pl); | |
803 | rtnl_unlock(); | |
804 | phylink_destroy(dp->pl); | |
a20f9970 | 805 | dp->pl = NULL; |
0e279218 IC |
806 | return; |
807 | } | |
808 | ||
33615367 SR |
809 | if (of_phy_is_fixed_link(dp->dn)) |
810 | of_phy_deregister_fixed_link(dp->dn); | |
811 | else | |
812 | dsa_port_setup_phy_of(dp, false); | |
57ab1ca2 | 813 | } |
cf963573 FF |
814 | |
815 | int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data) | |
816 | { | |
817 | struct phy_device *phydev; | |
818 | int ret = -EOPNOTSUPP; | |
819 | ||
820 | if (of_phy_is_fixed_link(dp->dn)) | |
821 | return ret; | |
822 | ||
823 | phydev = dsa_port_get_phy_device(dp); | |
824 | if (IS_ERR_OR_NULL(phydev)) | |
825 | return ret; | |
826 | ||
827 | ret = phy_ethtool_get_strings(phydev, data); | |
828 | put_device(&phydev->mdio.dev); | |
829 | ||
830 | return ret; | |
831 | } | |
832 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings); | |
833 | ||
834 | int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data) | |
835 | { | |
836 | struct phy_device *phydev; | |
837 | int ret = -EOPNOTSUPP; | |
838 | ||
839 | if (of_phy_is_fixed_link(dp->dn)) | |
840 | return ret; | |
841 | ||
842 | phydev = dsa_port_get_phy_device(dp); | |
843 | if (IS_ERR_OR_NULL(phydev)) | |
844 | return ret; | |
845 | ||
846 | ret = phy_ethtool_get_stats(phydev, NULL, data); | |
847 | put_device(&phydev->mdio.dev); | |
848 | ||
849 | return ret; | |
850 | } | |
851 | EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats); | |
852 | ||
853 | int dsa_port_get_phy_sset_count(struct dsa_port *dp) | |
854 | { | |
855 | struct phy_device *phydev; | |
856 | int ret = -EOPNOTSUPP; | |
857 | ||
858 | if (of_phy_is_fixed_link(dp->dn)) | |
859 | return ret; | |
860 | ||
861 | phydev = dsa_port_get_phy_device(dp); | |
862 | if (IS_ERR_OR_NULL(phydev)) | |
863 | return ret; | |
864 | ||
865 | ret = phy_ethtool_get_sset_count(phydev); | |
866 | put_device(&phydev->mdio.dev); | |
867 | ||
868 | return ret; | |
869 | } | |
870 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count); |