net: switchdev: remove the transaction structure from port attributes
[linux-2.6-block.git] / net / dsa / port.c
CommitLineData
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
16static 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 33static 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 43int 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 72static 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 81int 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
102int 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
113void 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
128void dsa_port_disable(struct dsa_port *dp)
129{
130 rtnl_lock();
131 dsa_port_disable_rt(dp);
132 rtnl_unlock();
133}
134
cfbed329
VD
135int 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
166void 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
195static 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 260int 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 */
297bool 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 308int 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
VO
312 struct dsa_notifier_ageing_time_info info;
313 struct switchdev_trans trans;
314 int err;
315
316 info.ageing_time = ageing_time;
317 info.trans = &trans;
d87bd94e 318
bae33f2b
VO
319 trans.ph_prepare = true;
320 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
321 if (err)
322 return err;
d87bd94e 323
d87bd94e 324 dp->ageing_time = ageing_time;
d87bd94e 325
bae33f2b 326 trans.ph_prepare = false;
1faabf74 327 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
d87bd94e 328}
d1cffff0 329
bae33f2b 330int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags)
ea87005a
FF
331{
332 struct dsa_switch *ds = dp->ds;
333
334 if (!ds->ops->port_egress_floods ||
335 (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
336 return -EINVAL;
337
338 return 0;
339}
340
bae33f2b 341int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags)
57652796
RK
342{
343 struct dsa_switch *ds = dp->ds;
344 int port = dp->index;
345 int err = 0;
346
57652796
RK
347 if (ds->ops->port_egress_floods)
348 err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
349 flags & BR_MCAST_FLOOD);
350
351 return err;
352}
353
bae33f2b 354int dsa_port_mrouter(struct dsa_port *dp, bool mrouter)
08cc83cc
VD
355{
356 struct dsa_switch *ds = dp->ds;
357 int port = dp->index;
358
bae33f2b
VO
359 if (!ds->ops->port_egress_floods)
360 return -EOPNOTSUPP;
08cc83cc
VD
361
362 return ds->ops->port_egress_floods(ds, port, true, mrouter);
363}
364
bfcb8132
VO
365int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
366 bool propagate_upstream)
367{
368 struct dsa_notifier_mtu_info info = {
369 .sw_index = dp->ds->index,
370 .propagate_upstream = propagate_upstream,
371 .port = dp->index,
372 .mtu = new_mtu,
373 };
374
375 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
376}
377
2acf4e6a
AS
378int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
379 u16 vid)
d1cffff0 380{
685fb6a4
VD
381 struct dsa_notifier_fdb_info info = {
382 .sw_index = dp->ds->index,
383 .port = dp->index,
2acf4e6a
AS
384 .addr = addr,
385 .vid = vid,
685fb6a4 386 };
d1cffff0 387
685fb6a4 388 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
d1cffff0
VD
389}
390
2acf4e6a
AS
391int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
392 u16 vid)
d1cffff0 393{
685fb6a4
VD
394 struct dsa_notifier_fdb_info info = {
395 .sw_index = dp->ds->index,
396 .port = dp->index,
2acf4e6a
AS
397 .addr = addr,
398 .vid = vid,
399
685fb6a4 400 };
d1cffff0 401
685fb6a4 402 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
d1cffff0
VD
403}
404
de40fc5d
VD
405int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
406{
407 struct dsa_switch *ds = dp->ds;
408 int port = dp->index;
409
410 if (!ds->ops->port_fdb_dump)
411 return -EOPNOTSUPP;
412
413 return ds->ops->port_fdb_dump(ds, port, cb, data);
414}
415
bb9f6031 416int dsa_port_mdb_add(const struct dsa_port *dp,
ffb68fc5 417 const struct switchdev_obj_port_mdb *mdb)
3a9afea3 418{
8ae5bcdc
VD
419 struct dsa_notifier_mdb_info info = {
420 .sw_index = dp->ds->index,
421 .port = dp->index,
8ae5bcdc
VD
422 .mdb = mdb,
423 };
3a9afea3 424
8ae5bcdc 425 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
3a9afea3
VD
426}
427
bb9f6031 428int dsa_port_mdb_del(const struct dsa_port *dp,
3a9afea3
VD
429 const struct switchdev_obj_port_mdb *mdb)
430{
8ae5bcdc
VD
431 struct dsa_notifier_mdb_info info = {
432 .sw_index = dp->ds->index,
433 .port = dp->index,
434 .mdb = mdb,
435 };
3a9afea3 436
8ae5bcdc 437 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
3a9afea3
VD
438}
439
076e7133 440int dsa_port_vlan_add(struct dsa_port *dp,
ffb68fc5 441 const struct switchdev_obj_port_vlan *vlan)
076e7133 442{
d0c627b8
VD
443 struct dsa_notifier_vlan_info info = {
444 .sw_index = dp->ds->index,
445 .port = dp->index,
d0c627b8
VD
446 .vlan = vlan,
447 };
076e7133 448
c5335d73 449 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
076e7133
VD
450}
451
452int dsa_port_vlan_del(struct dsa_port *dp,
453 const struct switchdev_obj_port_vlan *vlan)
454{
d0c627b8
VD
455 struct dsa_notifier_vlan_info info = {
456 .sw_index = dp->ds->index,
457 .port = dp->index,
458 .vlan = vlan,
459 };
076e7133 460
c5335d73 461 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
076e7133 462}
57ab1ca2 463
6207a78c 464static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
33615367 465{
33615367 466 struct device_node *phy_dn;
33615367 467 struct phy_device *phydev;
33615367 468
6207a78c 469 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
33615367 470 if (!phy_dn)
6207a78c 471 return NULL;
33615367
SR
472
473 phydev = of_phy_find_device(phy_dn);
474 if (!phydev) {
6207a78c
FF
475 of_node_put(phy_dn);
476 return ERR_PTR(-EPROBE_DEFER);
33615367
SR
477 }
478
9919a363 479 of_node_put(phy_dn);
6207a78c
FF
480 return phydev;
481}
482
8ae67496
FF
483static void dsa_port_phylink_validate(struct phylink_config *config,
484 unsigned long *supported,
485 struct phylink_link_state *state)
77373d49
IC
486{
487 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
488 struct dsa_switch *ds = dp->ds;
489
490 if (!ds->ops->phylink_validate)
491 return;
492
493 ds->ops->phylink_validate(ds, dp->index, supported, state);
494}
77373d49 495
8ae67496
FF
496static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
497 struct phylink_link_state *state)
77373d49
IC
498{
499 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
500 struct dsa_switch *ds = dp->ds;
87615c96 501 int err;
77373d49 502
d46b7e4f
RK
503 /* Only called for inband modes */
504 if (!ds->ops->phylink_mac_link_state) {
505 state->link = 0;
506 return;
507 }
77373d49 508
87615c96
RK
509 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
510 if (err < 0) {
511 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
512 dp->index, err);
d46b7e4f 513 state->link = 0;
87615c96 514 }
77373d49 515}
77373d49 516
8ae67496
FF
517static void dsa_port_phylink_mac_config(struct phylink_config *config,
518 unsigned int mode,
519 const struct phylink_link_state *state)
77373d49
IC
520{
521 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
522 struct dsa_switch *ds = dp->ds;
523
524 if (!ds->ops->phylink_mac_config)
525 return;
526
527 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
528}
77373d49 529
8ae67496 530static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
77373d49
IC
531{
532 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
533 struct dsa_switch *ds = dp->ds;
534
535 if (!ds->ops->phylink_mac_an_restart)
536 return;
537
538 ds->ops->phylink_mac_an_restart(ds, dp->index);
539}
77373d49 540
8ae67496
FF
541static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
542 unsigned int mode,
543 phy_interface_t interface)
77373d49
IC
544{
545 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
0e279218 546 struct phy_device *phydev = NULL;
77373d49
IC
547 struct dsa_switch *ds = dp->ds;
548
0e279218
IC
549 if (dsa_is_user_port(ds, dp->index))
550 phydev = dp->slave->phydev;
551
77373d49 552 if (!ds->ops->phylink_mac_link_down) {
0e279218
IC
553 if (ds->ops->adjust_link && phydev)
554 ds->ops->adjust_link(ds, dp->index, phydev);
77373d49
IC
555 return;
556 }
557
558 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
559}
77373d49 560
8ae67496 561static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
91a208f2 562 struct phy_device *phydev,
8ae67496
FF
563 unsigned int mode,
564 phy_interface_t interface,
91a208f2
RK
565 int speed, int duplex,
566 bool tx_pause, bool rx_pause)
77373d49
IC
567{
568 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
77373d49
IC
569 struct dsa_switch *ds = dp->ds;
570
571 if (!ds->ops->phylink_mac_link_up) {
0e279218
IC
572 if (ds->ops->adjust_link && phydev)
573 ds->ops->adjust_link(ds, dp->index, phydev);
77373d49
IC
574 return;
575 }
576
5b502a7b
RK
577 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
578 speed, duplex, tx_pause, rx_pause);
77373d49 579}
77373d49
IC
580
581const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
582 .validate = dsa_port_phylink_validate,
d46b7e4f 583 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
77373d49
IC
584 .mac_config = dsa_port_phylink_mac_config,
585 .mac_an_restart = dsa_port_phylink_mac_an_restart,
586 .mac_link_down = dsa_port_phylink_mac_link_down,
587 .mac_link_up = dsa_port_phylink_mac_link_up,
588};
589
6207a78c
FF
590static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
591{
592 struct dsa_switch *ds = dp->ds;
593 struct phy_device *phydev;
594 int port = dp->index;
595 int err = 0;
596
597 phydev = dsa_port_get_phy_device(dp);
598 if (!phydev)
599 return 0;
600
601 if (IS_ERR(phydev))
602 return PTR_ERR(phydev);
603
33615367 604 if (enable) {
33615367
SR
605 err = genphy_resume(phydev);
606 if (err < 0)
607 goto err_put_dev;
608
609 err = genphy_read_status(phydev);
610 if (err < 0)
611 goto err_put_dev;
612 } else {
613 err = genphy_suspend(phydev);
614 if (err < 0)
615 goto err_put_dev;
616 }
617
618 if (ds->ops->adjust_link)
619 ds->ops->adjust_link(ds, port, phydev);
620
621 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
622
623err_put_dev:
624 put_device(&phydev->mdio.dev);
33615367
SR
625 return err;
626}
627
628static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
57ab1ca2
VD
629{
630 struct device_node *dn = dp->dn;
631 struct dsa_switch *ds = dp->ds;
632 struct phy_device *phydev;
633 int port = dp->index;
0c65b2b9 634 phy_interface_t mode;
57ab1ca2
VD
635 int err;
636
33615367
SR
637 err = of_phy_register_fixed_link(dn);
638 if (err) {
639 dev_err(ds->dev,
640 "failed to register the fixed PHY of port %d\n",
641 port);
642 return err;
643 }
57ab1ca2 644
33615367 645 phydev = of_phy_find_device(dn);
57ab1ca2 646
0c65b2b9
AL
647 err = of_get_phy_mode(dn, &mode);
648 if (err)
33615367
SR
649 mode = PHY_INTERFACE_MODE_NA;
650 phydev->interface = mode;
57ab1ca2 651
33615367 652 genphy_read_status(phydev);
57ab1ca2 653
33615367
SR
654 if (ds->ops->adjust_link)
655 ds->ops->adjust_link(ds, port, phydev);
57ab1ca2 656
33615367 657 put_device(&phydev->mdio.dev);
57ab1ca2
VD
658
659 return 0;
660}
661
0e279218
IC
662static int dsa_port_phylink_register(struct dsa_port *dp)
663{
664 struct dsa_switch *ds = dp->ds;
665 struct device_node *port_dn = dp->dn;
0c65b2b9
AL
666 phy_interface_t mode;
667 int err;
0e279218 668
0c65b2b9
AL
669 err = of_get_phy_mode(port_dn, &mode);
670 if (err)
0e279218
IC
671 mode = PHY_INTERFACE_MODE_NA;
672
673 dp->pl_config.dev = ds->dev;
674 dp->pl_config.type = PHYLINK_DEV;
787cac3f 675 dp->pl_config.pcs_poll = ds->pcs_poll;
0e279218
IC
676
677 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
678 mode, &dsa_port_phylink_mac_ops);
679 if (IS_ERR(dp->pl)) {
680 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
681 return PTR_ERR(dp->pl);
682 }
683
684 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
2131fba5 685 if (err && err != -ENODEV) {
0e279218
IC
686 pr_err("could not attach to PHY: %d\n", err);
687 goto err_phy_connect;
688 }
689
0e279218
IC
690 return 0;
691
692err_phy_connect:
693 phylink_destroy(dp->pl);
694 return err;
695}
696
33615367 697int dsa_port_link_register_of(struct dsa_port *dp)
57ab1ca2 698{
0e279218 699 struct dsa_switch *ds = dp->ds;
a20f9970 700 struct device_node *phy_np;
3be98b2d 701 int port = dp->index;
0e279218 702
a20f9970
AL
703 if (!ds->ops->adjust_link) {
704 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
3be98b2d
AL
705 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
706 if (ds->ops->phylink_mac_link_down)
707 ds->ops->phylink_mac_link_down(ds, port,
708 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
a20f9970 709 return dsa_port_phylink_register(dp);
3be98b2d 710 }
a20f9970
AL
711 return 0;
712 }
0e279218
IC
713
714 dev_warn(ds->dev,
715 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
716
33615367
SR
717 if (of_phy_is_fixed_link(dp->dn))
718 return dsa_port_fixed_link_register_of(dp);
719 else
720 return dsa_port_setup_phy_of(dp, true);
721}
57ab1ca2 722
33615367
SR
723void dsa_port_link_unregister_of(struct dsa_port *dp)
724{
0e279218
IC
725 struct dsa_switch *ds = dp->ds;
726
a20f9970 727 if (!ds->ops->adjust_link && dp->pl) {
0e279218
IC
728 rtnl_lock();
729 phylink_disconnect_phy(dp->pl);
730 rtnl_unlock();
731 phylink_destroy(dp->pl);
a20f9970 732 dp->pl = NULL;
0e279218
IC
733 return;
734 }
735
33615367
SR
736 if (of_phy_is_fixed_link(dp->dn))
737 of_phy_deregister_fixed_link(dp->dn);
738 else
739 dsa_port_setup_phy_of(dp, false);
57ab1ca2 740}
cf963573
FF
741
742int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
743{
744 struct phy_device *phydev;
745 int ret = -EOPNOTSUPP;
746
747 if (of_phy_is_fixed_link(dp->dn))
748 return ret;
749
750 phydev = dsa_port_get_phy_device(dp);
751 if (IS_ERR_OR_NULL(phydev))
752 return ret;
753
754 ret = phy_ethtool_get_strings(phydev, data);
755 put_device(&phydev->mdio.dev);
756
757 return ret;
758}
759EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
760
761int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
762{
763 struct phy_device *phydev;
764 int ret = -EOPNOTSUPP;
765
766 if (of_phy_is_fixed_link(dp->dn))
767 return ret;
768
769 phydev = dsa_port_get_phy_device(dp);
770 if (IS_ERR_OR_NULL(phydev))
771 return ret;
772
773 ret = phy_ethtool_get_stats(phydev, NULL, data);
774 put_device(&phydev->mdio.dev);
775
776 return ret;
777}
778EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
779
780int dsa_port_get_phy_sset_count(struct dsa_port *dp)
781{
782 struct phy_device *phydev;
783 int ret = -EOPNOTSUPP;
784
785 if (of_phy_is_fixed_link(dp->dn))
786 return ret;
787
788 phydev = dsa_port_get_phy_device(dp);
789 if (IS_ERR_OR_NULL(phydev))
790 return ret;
791
792 ret = phy_ethtool_get_sset_count(phydev);
793 put_device(&phydev->mdio.dev);
794
795 return ret;
796}
797EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);