net: dsa: propagate extack to .port_vlan_add
[linux-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
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 28static 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 33int 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 62static 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 71int 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
92int 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
103void 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
118void dsa_port_disable(struct dsa_port *dp)
119{
120 rtnl_lock();
121 dsa_port_disable_rt(dp);
122 rtnl_unlock();
123}
124
5e38c158
VO
125static 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
147int 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
178void 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
216int 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
243int 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
266void 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
VO
296static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
297 bool vlan_filtering)
298{
299 struct dsa_switch *ds = dp->ds;
adb256eb
VO
300 int err, i;
301
302 /* VLAN awareness was off, so the question is "can we turn it on".
303 * We may have had 8021q uppers, those need to go. Make sure we don't
304 * enter an inconsistent state: deny changing the VLAN awareness state
305 * as long as we have 8021q uppers.
306 */
307 if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
308 struct net_device *upper_dev, *slave = dp->slave;
309 struct net_device *br = dp->bridge_dev;
310 struct list_head *iter;
311
312 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
313 struct bridge_vlan_info br_info;
314 u16 vid;
315
316 if (!is_vlan_dev(upper_dev))
317 continue;
318
319 vid = vlan_dev_vlan_id(upper_dev);
320
321 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
322 * device, respectively the VID is not found, returning
323 * 0 means success, which is a failure for us here.
324 */
325 err = br_vlan_get_info(br, vid, &br_info);
326 if (err == 0) {
327 dev_err(ds->dev, "Must remove upper %s first\n",
328 upper_dev->name);
329 return false;
330 }
331 }
332 }
8f5d16f6
VO
333
334 if (!ds->vlan_filtering_is_global)
335 return true;
336
337 /* For cases where enabling/disabling VLAN awareness is global to the
338 * switch, we need to handle the case where multiple bridges span
339 * different ports of the same switch device and one of them has a
340 * different setting than what is being requested.
341 */
342 for (i = 0; i < ds->num_ports; i++) {
343 struct net_device *other_bridge;
344
345 other_bridge = dsa_to_port(ds, i)->bridge_dev;
346 if (!other_bridge)
347 continue;
348 /* If it's the same bridge, it also has same
349 * vlan_filtering setting => no need to check
350 */
351 if (other_bridge == dp->bridge_dev)
352 continue;
353 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
354 dev_err(ds->dev, "VLAN filtering is a global setting\n");
355 return false;
356 }
357 }
358 return true;
359}
360
bae33f2b 361int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering)
4d61d304
VD
362{
363 struct dsa_switch *ds = dp->ds;
bae33f2b 364 bool apply;
33162e9a 365 int err;
4d61d304 366
bae33f2b
VO
367 if (!ds->ops->port_vlan_filtering)
368 return -EOPNOTSUPP;
adb256eb 369
bae33f2b
VO
370 /* We are called from dsa_slave_switchdev_blocking_event(),
371 * which is not under rcu_read_lock(), unlike
372 * dsa_slave_switchdev_event().
373 */
374 rcu_read_lock();
375 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering);
376 rcu_read_unlock();
377 if (!apply)
378 return -EINVAL;
8f5d16f6 379
ec9121e7
VO
380 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
381 return 0;
382
bae33f2b 383 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering);
8f5d16f6
VO
384 if (err)
385 return err;
386
bae33f2b
VO
387 if (ds->vlan_filtering_is_global)
388 ds->vlan_filtering = vlan_filtering;
389 else
390 dp->vlan_filtering = vlan_filtering;
2e554a7a 391
4d61d304
VD
392 return 0;
393}
d87bd94e 394
54a0ed0d
RK
395/* This enforces legacy behavior for switch drivers which assume they can't
396 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
397 */
398bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
399{
400 struct dsa_switch *ds = dp->ds;
401
402 if (!dp->bridge_dev)
403 return false;
404
405 return (!ds->configure_vlan_while_not_filtering &&
406 !br_vlan_enabled(dp->bridge_dev));
407}
408
bae33f2b 409int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
d87bd94e
VD
410{
411 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
412 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
bae33f2b 413 struct dsa_notifier_ageing_time_info info;
bae33f2b
VO
414 int err;
415
416 info.ageing_time = ageing_time;
d87bd94e 417
bae33f2b
VO
418 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
419 if (err)
420 return err;
d87bd94e 421
d87bd94e 422 dp->ageing_time = ageing_time;
d87bd94e 423
77b61365 424 return 0;
d87bd94e 425}
d1cffff0 426
e18f4c18 427int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
a8b659e7
VO
428 struct switchdev_brport_flags flags,
429 struct netlink_ext_ack *extack)
ea87005a
FF
430{
431 struct dsa_switch *ds = dp->ds;
432
a8b659e7 433 if (!ds->ops->port_pre_bridge_flags)
ea87005a
FF
434 return -EINVAL;
435
a8b659e7 436 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
ea87005a
FF
437}
438
e18f4c18 439int dsa_port_bridge_flags(const struct dsa_port *dp,
a8b659e7
VO
440 struct switchdev_brport_flags flags,
441 struct netlink_ext_ack *extack)
57652796
RK
442{
443 struct dsa_switch *ds = dp->ds;
57652796 444
a8b659e7
VO
445 if (!ds->ops->port_bridge_flags)
446 return -EINVAL;
57652796 447
a8b659e7 448 return ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
57652796
RK
449}
450
a8b659e7
VO
451int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
452 struct netlink_ext_ack *extack)
08cc83cc
VD
453{
454 struct dsa_switch *ds = dp->ds;
08cc83cc 455
a8b659e7 456 if (!ds->ops->port_set_mrouter)
bae33f2b 457 return -EOPNOTSUPP;
08cc83cc 458
a8b659e7 459 return ds->ops->port_set_mrouter(ds, dp->index, mrouter, extack);
08cc83cc
VD
460}
461
bfcb8132
VO
462int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
463 bool propagate_upstream)
464{
465 struct dsa_notifier_mtu_info info = {
466 .sw_index = dp->ds->index,
467 .propagate_upstream = propagate_upstream,
468 .port = dp->index,
469 .mtu = new_mtu,
470 };
471
472 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
473}
474
2acf4e6a
AS
475int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
476 u16 vid)
d1cffff0 477{
685fb6a4
VD
478 struct dsa_notifier_fdb_info info = {
479 .sw_index = dp->ds->index,
480 .port = dp->index,
2acf4e6a
AS
481 .addr = addr,
482 .vid = vid,
685fb6a4 483 };
d1cffff0 484
685fb6a4 485 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
d1cffff0
VD
486}
487
2acf4e6a
AS
488int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
489 u16 vid)
d1cffff0 490{
685fb6a4
VD
491 struct dsa_notifier_fdb_info info = {
492 .sw_index = dp->ds->index,
493 .port = dp->index,
2acf4e6a
AS
494 .addr = addr,
495 .vid = vid,
496
685fb6a4 497 };
d1cffff0 498
685fb6a4 499 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
d1cffff0
VD
500}
501
de40fc5d
VD
502int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
503{
504 struct dsa_switch *ds = dp->ds;
505 int port = dp->index;
506
507 if (!ds->ops->port_fdb_dump)
508 return -EOPNOTSUPP;
509
510 return ds->ops->port_fdb_dump(ds, port, cb, data);
511}
512
bb9f6031 513int dsa_port_mdb_add(const struct dsa_port *dp,
ffb68fc5 514 const struct switchdev_obj_port_mdb *mdb)
3a9afea3 515{
8ae5bcdc
VD
516 struct dsa_notifier_mdb_info info = {
517 .sw_index = dp->ds->index,
518 .port = dp->index,
8ae5bcdc
VD
519 .mdb = mdb,
520 };
3a9afea3 521
8ae5bcdc 522 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
3a9afea3
VD
523}
524
bb9f6031 525int dsa_port_mdb_del(const struct dsa_port *dp,
3a9afea3
VD
526 const struct switchdev_obj_port_mdb *mdb)
527{
8ae5bcdc
VD
528 struct dsa_notifier_mdb_info info = {
529 .sw_index = dp->ds->index,
530 .port = dp->index,
531 .mdb = mdb,
532 };
3a9afea3 533
8ae5bcdc 534 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
3a9afea3
VD
535}
536
076e7133 537int dsa_port_vlan_add(struct dsa_port *dp,
31046a5f
VO
538 const struct switchdev_obj_port_vlan *vlan,
539 struct netlink_ext_ack *extack)
076e7133 540{
d0c627b8
VD
541 struct dsa_notifier_vlan_info info = {
542 .sw_index = dp->ds->index,
543 .port = dp->index,
d0c627b8 544 .vlan = vlan,
31046a5f 545 .extack = extack,
d0c627b8 546 };
076e7133 547
c5335d73 548 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
076e7133
VD
549}
550
551int dsa_port_vlan_del(struct dsa_port *dp,
552 const struct switchdev_obj_port_vlan *vlan)
553{
d0c627b8
VD
554 struct dsa_notifier_vlan_info info = {
555 .sw_index = dp->ds->index,
556 .port = dp->index,
557 .vlan = vlan,
558 };
076e7133 559
c5335d73 560 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
076e7133 561}
57ab1ca2 562
53da0eba
VO
563void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
564 const struct dsa_device_ops *tag_ops)
565{
566 cpu_dp->filter = tag_ops->filter;
567 cpu_dp->rcv = tag_ops->rcv;
568 cpu_dp->tag_ops = tag_ops;
569}
570
6207a78c 571static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
33615367 572{
33615367 573 struct device_node *phy_dn;
33615367 574 struct phy_device *phydev;
33615367 575
6207a78c 576 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
33615367 577 if (!phy_dn)
6207a78c 578 return NULL;
33615367
SR
579
580 phydev = of_phy_find_device(phy_dn);
581 if (!phydev) {
6207a78c
FF
582 of_node_put(phy_dn);
583 return ERR_PTR(-EPROBE_DEFER);
33615367
SR
584 }
585
9919a363 586 of_node_put(phy_dn);
6207a78c
FF
587 return phydev;
588}
589
8ae67496
FF
590static void dsa_port_phylink_validate(struct phylink_config *config,
591 unsigned long *supported,
592 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_validate)
598 return;
599
600 ds->ops->phylink_validate(ds, dp->index, supported, state);
601}
77373d49 602
8ae67496
FF
603static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
604 struct phylink_link_state *state)
77373d49
IC
605{
606 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
607 struct dsa_switch *ds = dp->ds;
87615c96 608 int err;
77373d49 609
d46b7e4f
RK
610 /* Only called for inband modes */
611 if (!ds->ops->phylink_mac_link_state) {
612 state->link = 0;
613 return;
614 }
77373d49 615
87615c96
RK
616 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
617 if (err < 0) {
618 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
619 dp->index, err);
d46b7e4f 620 state->link = 0;
87615c96 621 }
77373d49 622}
77373d49 623
8ae67496
FF
624static void dsa_port_phylink_mac_config(struct phylink_config *config,
625 unsigned int mode,
626 const struct phylink_link_state *state)
77373d49
IC
627{
628 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
629 struct dsa_switch *ds = dp->ds;
630
631 if (!ds->ops->phylink_mac_config)
632 return;
633
634 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
635}
77373d49 636
8ae67496 637static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
77373d49
IC
638{
639 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
640 struct dsa_switch *ds = dp->ds;
641
642 if (!ds->ops->phylink_mac_an_restart)
643 return;
644
645 ds->ops->phylink_mac_an_restart(ds, dp->index);
646}
77373d49 647
8ae67496
FF
648static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
649 unsigned int mode,
650 phy_interface_t interface)
77373d49
IC
651{
652 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
0e279218 653 struct phy_device *phydev = NULL;
77373d49
IC
654 struct dsa_switch *ds = dp->ds;
655
0e279218
IC
656 if (dsa_is_user_port(ds, dp->index))
657 phydev = dp->slave->phydev;
658
77373d49 659 if (!ds->ops->phylink_mac_link_down) {
0e279218
IC
660 if (ds->ops->adjust_link && phydev)
661 ds->ops->adjust_link(ds, dp->index, phydev);
77373d49
IC
662 return;
663 }
664
665 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
666}
77373d49 667
8ae67496 668static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
91a208f2 669 struct phy_device *phydev,
8ae67496
FF
670 unsigned int mode,
671 phy_interface_t interface,
91a208f2
RK
672 int speed, int duplex,
673 bool tx_pause, bool rx_pause)
77373d49
IC
674{
675 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
77373d49
IC
676 struct dsa_switch *ds = dp->ds;
677
678 if (!ds->ops->phylink_mac_link_up) {
0e279218
IC
679 if (ds->ops->adjust_link && phydev)
680 ds->ops->adjust_link(ds, dp->index, phydev);
77373d49
IC
681 return;
682 }
683
5b502a7b
RK
684 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
685 speed, duplex, tx_pause, rx_pause);
77373d49 686}
77373d49
IC
687
688const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
689 .validate = dsa_port_phylink_validate,
d46b7e4f 690 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
77373d49
IC
691 .mac_config = dsa_port_phylink_mac_config,
692 .mac_an_restart = dsa_port_phylink_mac_an_restart,
693 .mac_link_down = dsa_port_phylink_mac_link_down,
694 .mac_link_up = dsa_port_phylink_mac_link_up,
695};
696
6207a78c
FF
697static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
698{
699 struct dsa_switch *ds = dp->ds;
700 struct phy_device *phydev;
701 int port = dp->index;
702 int err = 0;
703
704 phydev = dsa_port_get_phy_device(dp);
705 if (!phydev)
706 return 0;
707
708 if (IS_ERR(phydev))
709 return PTR_ERR(phydev);
710
33615367 711 if (enable) {
33615367
SR
712 err = genphy_resume(phydev);
713 if (err < 0)
714 goto err_put_dev;
715
716 err = genphy_read_status(phydev);
717 if (err < 0)
718 goto err_put_dev;
719 } else {
720 err = genphy_suspend(phydev);
721 if (err < 0)
722 goto err_put_dev;
723 }
724
725 if (ds->ops->adjust_link)
726 ds->ops->adjust_link(ds, port, phydev);
727
728 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
729
730err_put_dev:
731 put_device(&phydev->mdio.dev);
33615367
SR
732 return err;
733}
734
735static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
57ab1ca2
VD
736{
737 struct device_node *dn = dp->dn;
738 struct dsa_switch *ds = dp->ds;
739 struct phy_device *phydev;
740 int port = dp->index;
0c65b2b9 741 phy_interface_t mode;
57ab1ca2
VD
742 int err;
743
33615367
SR
744 err = of_phy_register_fixed_link(dn);
745 if (err) {
746 dev_err(ds->dev,
747 "failed to register the fixed PHY of port %d\n",
748 port);
749 return err;
750 }
57ab1ca2 751
33615367 752 phydev = of_phy_find_device(dn);
57ab1ca2 753
0c65b2b9
AL
754 err = of_get_phy_mode(dn, &mode);
755 if (err)
33615367
SR
756 mode = PHY_INTERFACE_MODE_NA;
757 phydev->interface = mode;
57ab1ca2 758
33615367 759 genphy_read_status(phydev);
57ab1ca2 760
33615367
SR
761 if (ds->ops->adjust_link)
762 ds->ops->adjust_link(ds, port, phydev);
57ab1ca2 763
33615367 764 put_device(&phydev->mdio.dev);
57ab1ca2
VD
765
766 return 0;
767}
768
0e279218
IC
769static int dsa_port_phylink_register(struct dsa_port *dp)
770{
771 struct dsa_switch *ds = dp->ds;
772 struct device_node *port_dn = dp->dn;
0c65b2b9
AL
773 phy_interface_t mode;
774 int err;
0e279218 775
0c65b2b9
AL
776 err = of_get_phy_mode(port_dn, &mode);
777 if (err)
0e279218
IC
778 mode = PHY_INTERFACE_MODE_NA;
779
780 dp->pl_config.dev = ds->dev;
781 dp->pl_config.type = PHYLINK_DEV;
787cac3f 782 dp->pl_config.pcs_poll = ds->pcs_poll;
0e279218
IC
783
784 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
785 mode, &dsa_port_phylink_mac_ops);
786 if (IS_ERR(dp->pl)) {
787 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
788 return PTR_ERR(dp->pl);
789 }
790
791 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
2131fba5 792 if (err && err != -ENODEV) {
0e279218
IC
793 pr_err("could not attach to PHY: %d\n", err);
794 goto err_phy_connect;
795 }
796
0e279218
IC
797 return 0;
798
799err_phy_connect:
800 phylink_destroy(dp->pl);
801 return err;
802}
803
33615367 804int dsa_port_link_register_of(struct dsa_port *dp)
57ab1ca2 805{
0e279218 806 struct dsa_switch *ds = dp->ds;
a20f9970 807 struct device_node *phy_np;
3be98b2d 808 int port = dp->index;
0e279218 809
a20f9970
AL
810 if (!ds->ops->adjust_link) {
811 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
3be98b2d
AL
812 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
813 if (ds->ops->phylink_mac_link_down)
814 ds->ops->phylink_mac_link_down(ds, port,
815 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
a20f9970 816 return dsa_port_phylink_register(dp);
3be98b2d 817 }
a20f9970
AL
818 return 0;
819 }
0e279218
IC
820
821 dev_warn(ds->dev,
822 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
823
33615367
SR
824 if (of_phy_is_fixed_link(dp->dn))
825 return dsa_port_fixed_link_register_of(dp);
826 else
827 return dsa_port_setup_phy_of(dp, true);
828}
57ab1ca2 829
33615367
SR
830void dsa_port_link_unregister_of(struct dsa_port *dp)
831{
0e279218
IC
832 struct dsa_switch *ds = dp->ds;
833
a20f9970 834 if (!ds->ops->adjust_link && dp->pl) {
0e279218
IC
835 rtnl_lock();
836 phylink_disconnect_phy(dp->pl);
837 rtnl_unlock();
838 phylink_destroy(dp->pl);
a20f9970 839 dp->pl = NULL;
0e279218
IC
840 return;
841 }
842
33615367
SR
843 if (of_phy_is_fixed_link(dp->dn))
844 of_phy_deregister_fixed_link(dp->dn);
845 else
846 dsa_port_setup_phy_of(dp, false);
57ab1ca2 847}
cf963573
FF
848
849int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
850{
851 struct phy_device *phydev;
852 int ret = -EOPNOTSUPP;
853
854 if (of_phy_is_fixed_link(dp->dn))
855 return ret;
856
857 phydev = dsa_port_get_phy_device(dp);
858 if (IS_ERR_OR_NULL(phydev))
859 return ret;
860
861 ret = phy_ethtool_get_strings(phydev, data);
862 put_device(&phydev->mdio.dev);
863
864 return ret;
865}
866EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
867
868int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
869{
870 struct phy_device *phydev;
871 int ret = -EOPNOTSUPP;
872
873 if (of_phy_is_fixed_link(dp->dn))
874 return ret;
875
876 phydev = dsa_port_get_phy_device(dp);
877 if (IS_ERR_OR_NULL(phydev))
878 return ret;
879
880 ret = phy_ethtool_get_stats(phydev, NULL, data);
881 put_device(&phydev->mdio.dev);
882
883 return ret;
884}
885EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
886
887int dsa_port_get_phy_sset_count(struct dsa_port *dp)
888{
889 struct phy_device *phydev;
890 int ret = -EOPNOTSUPP;
891
892 if (of_phy_is_fixed_link(dp->dn))
893 return ret;
894
895 phydev = dsa_port_get_phy_device(dp);
896 if (IS_ERR_OR_NULL(phydev))
897 return ret;
898
899 ret = phy_ethtool_get_sset_count(phydev);
900 put_device(&phydev->mdio.dev);
901
902 return ret;
903}
904EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
18596f50
GM
905
906int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
907{
908 struct dsa_notifier_hsr_info info = {
909 .sw_index = dp->ds->index,
910 .port = dp->index,
911 .hsr = hsr,
912 };
913 int err;
914
915 dp->hsr_dev = hsr;
916
917 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
918 if (err)
919 dp->hsr_dev = NULL;
920
921 return err;
922}
923
924void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
925{
926 struct dsa_notifier_hsr_info info = {
927 .sw_index = dp->ds->index,
928 .port = dp->index,
929 .hsr = hsr,
930 };
931 int err;
932
933 dp->hsr_dev = NULL;
934
935 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
936 if (err)
937 pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n");
938}