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