Merge tag 'cgroup-for-6.0-rc2-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[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
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
7414af30 33static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp, u16 vid)
9264e4ad
VO
34{
35 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
36 struct switchdev_notifier_fdb_info info = {
7414af30 37 .vid = vid,
9264e4ad
VO
38 };
39
40 /* When the port becomes standalone it has already left the bridge.
41 * Don't notify the bridge in that case.
42 */
43 if (!brport_dev)
44 return;
45
46 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
47 brport_dev, &info.info, NULL);
48}
49
045c45d1
VO
50static void dsa_port_fast_age(const struct dsa_port *dp)
51{
52 struct dsa_switch *ds = dp->ds;
53
54 if (!ds->ops->port_fast_age)
55 return;
56
57 ds->ops->port_fast_age(ds, dp->index);
9264e4ad 58
7414af30
TW
59 /* flush all VLANs */
60 dsa_port_notify_bridge_fdb_flush(dp, 0);
61}
62
63static int dsa_port_vlan_fast_age(const struct dsa_port *dp, u16 vid)
64{
65 struct dsa_switch *ds = dp->ds;
66 int err;
67
68 if (!ds->ops->port_vlan_fast_age)
69 return -EOPNOTSUPP;
70
71 err = ds->ops->port_vlan_fast_age(ds, dp->index, vid);
72
73 if (!err)
74 dsa_port_notify_bridge_fdb_flush(dp, vid);
75
76 return err;
77}
78
79static int dsa_port_msti_fast_age(const struct dsa_port *dp, u16 msti)
80{
81 DECLARE_BITMAP(vids, VLAN_N_VID) = { 0 };
82 int err, vid;
83
84 err = br_mst_get_info(dsa_port_bridge_dev_get(dp), msti, vids);
85 if (err)
86 return err;
87
88 for_each_set_bit(vid, vids, VLAN_N_VID) {
89 err = dsa_port_vlan_fast_age(dp, vid);
90 if (err)
91 return err;
92 }
93
94 return 0;
045c45d1
VO
95}
96
a4ffe09f
VO
97static bool dsa_port_can_configure_learning(struct dsa_port *dp)
98{
99 struct switchdev_brport_flags flags = {
100 .mask = BR_LEARNING,
101 };
102 struct dsa_switch *ds = dp->ds;
103 int err;
104
105 if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags)
106 return false;
107
108 err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL);
109 return !err;
110}
111
39f32101 112int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
a40c175b
VD
113{
114 struct dsa_switch *ds = dp->ds;
115 int port = dp->index;
116
bae33f2b
VO
117 if (!ds->ops->port_stp_state_set)
118 return -EOPNOTSUPP;
a40c175b 119
bae33f2b 120 ds->ops->port_stp_state_set(ds, port, state);
a40c175b 121
a4ffe09f
VO
122 if (!dsa_port_can_configure_learning(dp) ||
123 (do_fast_age && dp->learning)) {
a40c175b
VD
124 /* Fast age FDB entries or flush appropriate forwarding database
125 * for the given port, if we are moving it from Learning or
126 * Forwarding state, to Disabled or Blocking or Listening state.
39f32101
VO
127 * Ports that were standalone before the STP state change don't
128 * need to fast age the FDB, since address learning is off in
129 * standalone mode.
a40c175b
VD
130 */
131
132 if ((dp->stp_state == BR_STATE_LEARNING ||
133 dp->stp_state == BR_STATE_FORWARDING) &&
134 (state == BR_STATE_DISABLED ||
135 state == BR_STATE_BLOCKING ||
136 state == BR_STATE_LISTENING))
045c45d1 137 dsa_port_fast_age(dp);
a40c175b
VD
138 }
139
140 dp->stp_state = state;
141
142 return 0;
143}
144
39f32101
VO
145static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
146 bool do_fast_age)
a40c175b 147{
211987f3 148 struct dsa_switch *ds = dp->ds;
a40c175b
VD
149 int err;
150
39f32101 151 err = dsa_port_set_state(dp, state, do_fast_age);
211987f3
VO
152 if (err && err != -EOPNOTSUPP) {
153 dev_err(ds->dev, "port %d failed to set STP state %u: %pe\n",
154 dp->index, state, ERR_PTR(err));
155 }
a40c175b 156}
cfbed329 157
7414af30
TW
158int dsa_port_set_mst_state(struct dsa_port *dp,
159 const struct switchdev_mst_state *state,
160 struct netlink_ext_ack *extack)
161{
162 struct dsa_switch *ds = dp->ds;
163 u8 prev_state;
164 int err;
165
166 if (!ds->ops->port_mst_state_set)
167 return -EOPNOTSUPP;
168
169 err = br_mst_get_state(dsa_port_to_bridge_port(dp), state->msti,
170 &prev_state);
171 if (err)
172 return err;
173
174 err = ds->ops->port_mst_state_set(ds, dp->index, state);
175 if (err)
176 return err;
177
178 if (!(dp->learning &&
179 (prev_state == BR_STATE_LEARNING ||
180 prev_state == BR_STATE_FORWARDING) &&
181 (state->state == BR_STATE_DISABLED ||
182 state->state == BR_STATE_BLOCKING ||
183 state->state == BR_STATE_LISTENING)))
184 return 0;
185
186 err = dsa_port_msti_fast_age(dp, state->msti);
187 if (err)
188 NL_SET_ERR_MSG_MOD(extack,
189 "Unable to flush associated VLANs");
190
191 return 0;
192}
193
8640f8dc 194int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
fb8a6a2b 195{
fb8a6a2b
VD
196 struct dsa_switch *ds = dp->ds;
197 int port = dp->index;
198 int err;
199
200 if (ds->ops->port_enable) {
201 err = ds->ops->port_enable(ds, port, phy);
202 if (err)
203 return err;
204 }
205
d3eed0e5 206 if (!dp->bridge)
39f32101 207 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
fb8a6a2b 208
8640f8dc
RK
209 if (dp->pl)
210 phylink_start(dp->pl);
211
fb8a6a2b
VD
212 return 0;
213}
214
8640f8dc
RK
215int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
216{
217 int err;
218
219 rtnl_lock();
220 err = dsa_port_enable_rt(dp, phy);
221 rtnl_unlock();
222
223 return err;
224}
225
226void dsa_port_disable_rt(struct dsa_port *dp)
fb8a6a2b
VD
227{
228 struct dsa_switch *ds = dp->ds;
229 int port = dp->index;
230
8640f8dc
RK
231 if (dp->pl)
232 phylink_stop(dp->pl);
233
d3eed0e5 234 if (!dp->bridge)
39f32101 235 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
fb8a6a2b
VD
236
237 if (ds->ops->port_disable)
75104db0 238 ds->ops->port_disable(ds, port);
fb8a6a2b
VD
239}
240
8640f8dc
RK
241void dsa_port_disable(struct dsa_port *dp)
242{
243 rtnl_lock();
244 dsa_port_disable_rt(dp);
245 rtnl_unlock();
246}
247
8e9e678e
VO
248static void dsa_port_reset_vlan_filtering(struct dsa_port *dp,
249 struct dsa_bridge bridge)
250{
251 struct netlink_ext_ack extack = {0};
252 bool change_vlan_filtering = false;
253 struct dsa_switch *ds = dp->ds;
1699b4d5 254 struct dsa_port *other_dp;
8e9e678e
VO
255 bool vlan_filtering;
256 int err;
257
258 if (ds->needs_standalone_vlan_filtering &&
259 !br_vlan_enabled(bridge.dev)) {
260 change_vlan_filtering = true;
261 vlan_filtering = true;
262 } else if (!ds->needs_standalone_vlan_filtering &&
263 br_vlan_enabled(bridge.dev)) {
264 change_vlan_filtering = true;
265 vlan_filtering = false;
266 }
267
268 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
269 * event for changing vlan_filtering setting upon slave ports leaving
270 * it. That is a good thing, because that lets us handle it and also
271 * handle the case where the switch's vlan_filtering setting is global
272 * (not per port). When that happens, the correct moment to trigger the
273 * vlan_filtering callback is only when the last port leaves the last
274 * VLAN-aware bridge.
275 */
276 if (change_vlan_filtering && ds->vlan_filtering_is_global) {
1699b4d5
VO
277 dsa_switch_for_each_port(other_dp, ds) {
278 struct net_device *br = dsa_port_bridge_dev_get(other_dp);
8e9e678e
VO
279
280 if (br && br_vlan_enabled(br)) {
281 change_vlan_filtering = false;
282 break;
283 }
284 }
285 }
286
287 if (!change_vlan_filtering)
288 return;
289
290 err = dsa_port_vlan_filtering(dp, vlan_filtering, &extack);
291 if (extack._msg) {
292 dev_err(ds->dev, "port %d: %s\n", dp->index,
293 extack._msg);
294 }
295 if (err && err != -EOPNOTSUPP) {
296 dev_err(ds->dev,
297 "port %d failed to reset VLAN filtering to %d: %pe\n",
298 dp->index, vlan_filtering, ERR_PTR(err));
299 }
300}
301
5961d6a1
VO
302static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
303 struct netlink_ext_ack *extack)
5e38c158 304{
5961d6a1 305 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
b9e8b58f 306 BR_BCAST_FLOOD | BR_PORT_LOCKED;
5961d6a1
VO
307 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
308 int flag, err;
5e38c158 309
5961d6a1
VO
310 for_each_set_bit(flag, &mask, 32) {
311 struct switchdev_brport_flags flags = {0};
5e38c158 312
5961d6a1 313 flags.mask = BIT(flag);
e18f4c18 314
5961d6a1
VO
315 if (br_port_flag_is_set(brport_dev, BIT(flag)))
316 flags.val = BIT(flag);
5e38c158 317
5961d6a1
VO
318 err = dsa_port_bridge_flags(dp, flags, extack);
319 if (err && err != -EOPNOTSUPP)
320 return err;
321 }
322
323 return 0;
324}
325
326static void dsa_port_clear_brport_flags(struct dsa_port *dp)
327{
328 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
329 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
b9e8b58f 330 BR_BCAST_FLOOD | BR_PORT_LOCKED;
5961d6a1
VO
331 int flag, err;
332
333 for_each_set_bit(flag, &mask, 32) {
334 struct switchdev_brport_flags flags = {0};
335
336 flags.mask = BIT(flag);
337 flags.val = val & BIT(flag);
338
339 err = dsa_port_bridge_flags(dp, flags, NULL);
340 if (err && err != -EOPNOTSUPP)
341 dev_err(dp->ds->dev,
342 "failed to clear bridge port flag %lu: %pe\n",
343 flags.val, ERR_PTR(err));
5e38c158
VO
344 }
345}
346
4e51bf44
VO
347static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
348 struct netlink_ext_ack *extack)
5961d6a1 349{
010e269f 350 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
36cbf39b 351 struct net_device *br = dsa_port_bridge_dev_get(dp);
5961d6a1
VO
352 int err;
353
354 err = dsa_port_inherit_brport_flags(dp, extack);
355 if (err)
356 return err;
357
39f32101 358 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
010e269f
VO
359 if (err && err != -EOPNOTSUPP)
360 return err;
361
362 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
363 if (err && err != -EOPNOTSUPP)
364 return err;
365
010e269f
VO
366 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
367 if (err && err != -EOPNOTSUPP)
368 return err;
369
74918945
VO
370 return 0;
371}
372
8e9e678e
VO
373static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp,
374 struct dsa_bridge bridge)
5961d6a1
VO
375{
376 /* Configure the port for standalone mode (no address learning,
377 * flood everything).
378 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
379 * when the user requests it through netlink or sysfs, but not
380 * automatically at port join or leave, so we need to handle resetting
381 * the brport flags ourselves. But we even prefer it that way, because
382 * otherwise, some setups might never get the notification they need,
383 * for example, when a port leaves a LAG that offloads the bridge,
384 * it becomes standalone, but as far as the bridge is concerned, no
385 * port ever left.
386 */
387 dsa_port_clear_brport_flags(dp);
388
389 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
390 * so allow it to be in BR_STATE_FORWARDING to be kept functional
391 */
39f32101 392 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
010e269f 393
8e9e678e 394 dsa_port_reset_vlan_filtering(dp, bridge);
010e269f 395
010e269f
VO
396 /* Ageing time may be global to the switch chip, so don't change it
397 * here because we have no good reason (or value) to change it to.
398 */
5961d6a1
VO
399}
400
947c8746
VO
401static int dsa_port_bridge_create(struct dsa_port *dp,
402 struct net_device *br,
403 struct netlink_ext_ack *extack)
404{
405 struct dsa_switch *ds = dp->ds;
d3eed0e5 406 struct dsa_bridge *bridge;
947c8746 407
d3eed0e5
VO
408 bridge = dsa_tree_bridge_find(ds->dst, br);
409 if (bridge) {
410 refcount_inc(&bridge->refcount);
411 dp->bridge = bridge;
947c8746 412 return 0;
d3eed0e5
VO
413 }
414
415 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
416 if (!bridge)
417 return -ENOMEM;
418
419 refcount_set(&bridge->refcount, 1);
420
421 bridge->dev = br;
947c8746 422
d3eed0e5
VO
423 bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges);
424 if (ds->max_num_bridges && !bridge->num) {
947c8746
VO
425 NL_SET_ERR_MSG_MOD(extack,
426 "Range of offloadable bridges exceeded");
d3eed0e5 427 kfree(bridge);
947c8746 428 return -EOPNOTSUPP;
123abc06
VO
429 }
430
d3eed0e5 431 dp->bridge = bridge;
947c8746
VO
432
433 return 0;
434}
435
436static void dsa_port_bridge_destroy(struct dsa_port *dp,
437 const struct net_device *br)
438{
d3eed0e5
VO
439 struct dsa_bridge *bridge = dp->bridge;
440
441 dp->bridge = NULL;
947c8746 442
d3eed0e5
VO
443 if (!refcount_dec_and_test(&bridge->refcount))
444 return;
947c8746 445
d3eed0e5
VO
446 if (bridge->num)
447 dsa_bridge_num_put(br, bridge->num);
947c8746 448
d3eed0e5 449 kfree(bridge);
123abc06
VO
450}
451
332afc4c
TW
452static bool dsa_port_supports_mst(struct dsa_port *dp)
453{
8e6598a7
TW
454 struct dsa_switch *ds = dp->ds;
455
456 return ds->ops->vlan_msti_set &&
7414af30
TW
457 ds->ops->port_mst_state_set &&
458 ds->ops->port_vlan_fast_age &&
8e6598a7 459 dsa_port_can_configure_learning(dp);
332afc4c
TW
460}
461
2afc526a
VO
462int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
463 struct netlink_ext_ack *extack)
cfbed329
VD
464{
465 struct dsa_notifier_bridge_info info = {
726816a1 466 .dp = dp,
06b9cce4 467 .extack = extack,
cfbed329 468 };
2f5dc00f
VO
469 struct net_device *dev = dp->slave;
470 struct net_device *brport_dev;
cfbed329
VD
471 int err;
472
332afc4c
TW
473 if (br_mst_enabled(br) && !dsa_port_supports_mst(dp))
474 return -EOPNOTSUPP;
475
c1388063
RK
476 /* Here the interface is already bridged. Reflect the current
477 * configuration so that drivers can program their chips accordingly.
cfbed329 478 */
947c8746
VO
479 err = dsa_port_bridge_create(dp, br, extack);
480 if (err)
481 return err;
cfbed329 482
2f5dc00f
VO
483 brport_dev = dsa_port_to_bridge_port(dp);
484
d3eed0e5 485 info.bridge = *dp->bridge;
f66a6a69 486 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
5961d6a1
VO
487 if (err)
488 goto out_rollback;
cfbed329 489
857fdd74
VO
490 /* Drivers which support bridge TX forwarding should set this */
491 dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
123abc06 492
4e51bf44
VO
493 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
494 &dsa_slave_switchdev_notifier,
495 &dsa_slave_switchdev_blocking_notifier,
857fdd74 496 dp->bridge->tx_fwd_offload, extack);
5961d6a1
VO
497 if (err)
498 goto out_rollback_unbridge;
499
4e51bf44 500 err = dsa_port_switchdev_sync_attrs(dp, extack);
2f5dc00f
VO
501 if (err)
502 goto out_rollback_unoffload;
503
5961d6a1 504 return 0;
cfbed329 505
2f5dc00f 506out_rollback_unoffload:
4e51bf44
VO
507 switchdev_bridge_port_unoffload(brport_dev, dp,
508 &dsa_slave_switchdev_notifier,
509 &dsa_slave_switchdev_blocking_notifier);
630fd482 510 dsa_flush_workqueue();
5961d6a1
VO
511out_rollback_unbridge:
512 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
513out_rollback:
947c8746 514 dsa_port_bridge_destroy(dp, br);
cfbed329
VD
515 return err;
516}
517
4e51bf44 518void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
74918945 519{
2f5dc00f
VO
520 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
521
09dba21b
VO
522 /* Don't try to unoffload something that is not offloaded */
523 if (!brport_dev)
524 return;
525
4e51bf44
VO
526 switchdev_bridge_port_unoffload(brport_dev, dp,
527 &dsa_slave_switchdev_notifier,
528 &dsa_slave_switchdev_blocking_notifier);
d7d0d423
VO
529
530 dsa_flush_workqueue();
74918945
VO
531}
532
cfbed329
VD
533void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
534{
535 struct dsa_notifier_bridge_info info = {
726816a1 536 .dp = dp,
cfbed329
VD
537 };
538 int err;
539
342b6419
AÅ 
540 /* If the port could not be offloaded to begin with, then
541 * there is nothing to do.
542 */
543 if (!dp->bridge)
544 return;
545
546 info.bridge = *dp->bridge;
547
cfbed329
VD
548 /* Here the port is already unbridged. Reflect the current configuration
549 * so that drivers can program their chips accordingly.
550 */
947c8746 551 dsa_port_bridge_destroy(dp, br);
cfbed329 552
f66a6a69 553 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
cfbed329 554 if (err)
ab97462b
VO
555 dev_err(dp->ds->dev,
556 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
557 dp->index, ERR_PTR(err));
cfbed329 558
8e9e678e 559 dsa_port_switchdev_unsync_attrs(dp, info.bridge);
cfbed329 560}
4d61d304 561
058102a6
TW
562int dsa_port_lag_change(struct dsa_port *dp,
563 struct netdev_lag_lower_state_info *linfo)
564{
565 struct dsa_notifier_lag_info info = {
726816a1 566 .dp = dp,
058102a6
TW
567 };
568 bool tx_enabled;
569
dedd6a00 570 if (!dp->lag)
058102a6
TW
571 return 0;
572
573 /* On statically configured aggregates (e.g. loadbalance
574 * without LACP) ports will always be tx_enabled, even if the
575 * link is down. Thus we require both link_up and tx_enabled
576 * in order to include it in the tx set.
577 */
578 tx_enabled = linfo->link_up && linfo->tx_enabled;
579
580 if (tx_enabled == dp->lag_tx_enabled)
581 return 0;
582
583 dp->lag_tx_enabled = tx_enabled;
584
585 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
586}
587
dedd6a00
VO
588static int dsa_port_lag_create(struct dsa_port *dp,
589 struct net_device *lag_dev)
590{
591 struct dsa_switch *ds = dp->ds;
592 struct dsa_lag *lag;
593
594 lag = dsa_tree_lag_find(ds->dst, lag_dev);
595 if (lag) {
596 refcount_inc(&lag->refcount);
597 dp->lag = lag;
598 return 0;
599 }
600
601 lag = kzalloc(sizeof(*lag), GFP_KERNEL);
602 if (!lag)
603 return -ENOMEM;
604
605 refcount_set(&lag->refcount, 1);
e212fa7c
VO
606 mutex_init(&lag->fdb_lock);
607 INIT_LIST_HEAD(&lag->fdbs);
dedd6a00
VO
608 lag->dev = lag_dev;
609 dsa_lag_map(ds->dst, lag);
610 dp->lag = lag;
611
612 return 0;
613}
614
615static void dsa_port_lag_destroy(struct dsa_port *dp)
616{
617 struct dsa_lag *lag = dp->lag;
618
619 dp->lag = NULL;
620 dp->lag_tx_enabled = false;
621
622 if (!refcount_dec_and_test(&lag->refcount))
623 return;
624
e212fa7c 625 WARN_ON(!list_empty(&lag->fdbs));
dedd6a00
VO
626 dsa_lag_unmap(dp->ds->dst, lag);
627 kfree(lag);
628}
629
46a76724 630int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
2afc526a
VO
631 struct netdev_lag_upper_info *uinfo,
632 struct netlink_ext_ack *extack)
058102a6
TW
633{
634 struct dsa_notifier_lag_info info = {
726816a1 635 .dp = dp,
058102a6
TW
636 .info = uinfo,
637 };
185c9a76 638 struct net_device *bridge_dev;
058102a6
TW
639 int err;
640
dedd6a00
VO
641 err = dsa_port_lag_create(dp, lag_dev);
642 if (err)
643 goto err_lag_create;
058102a6 644
dedd6a00 645 info.lag = *dp->lag;
058102a6 646 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
185c9a76
VO
647 if (err)
648 goto err_lag_join;
058102a6 649
46a76724 650 bridge_dev = netdev_master_upper_dev_get(lag_dev);
185c9a76
VO
651 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
652 return 0;
653
2afc526a 654 err = dsa_port_bridge_join(dp, bridge_dev, extack);
185c9a76
VO
655 if (err)
656 goto err_bridge_join;
657
658 return 0;
659
660err_bridge_join:
661 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
662err_lag_join:
dedd6a00
VO
663 dsa_port_lag_destroy(dp);
664err_lag_create:
058102a6
TW
665 return err;
666}
667
46a76724 668void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
74918945 669{
36cbf39b
VO
670 struct net_device *br = dsa_port_bridge_dev_get(dp);
671
672 if (br)
673 dsa_port_pre_bridge_leave(dp, br);
74918945
VO
674}
675
46a76724 676void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
058102a6 677{
36cbf39b 678 struct net_device *br = dsa_port_bridge_dev_get(dp);
058102a6 679 struct dsa_notifier_lag_info info = {
726816a1 680 .dp = dp,
058102a6
TW
681 };
682 int err;
683
dedd6a00 684 if (!dp->lag)
058102a6
TW
685 return;
686
687 /* Port might have been part of a LAG that in turn was
688 * attached to a bridge.
689 */
36cbf39b
VO
690 if (br)
691 dsa_port_bridge_leave(dp, br);
058102a6 692
dedd6a00
VO
693 info.lag = *dp->lag;
694
695 dsa_port_lag_destroy(dp);
058102a6
TW
696
697 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
698 if (err)
ab97462b
VO
699 dev_err(dp->ds->dev,
700 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
701 dp->index, ERR_PTR(err));
058102a6
TW
702}
703
adb256eb 704/* Must be called under rcu_read_lock() */
8f5d16f6 705static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
89153ed6
VO
706 bool vlan_filtering,
707 struct netlink_ext_ack *extack)
8f5d16f6
VO
708{
709 struct dsa_switch *ds = dp->ds;
d0004a02
VO
710 struct dsa_port *other_dp;
711 int err;
adb256eb
VO
712
713 /* VLAN awareness was off, so the question is "can we turn it on".
714 * We may have had 8021q uppers, those need to go. Make sure we don't
715 * enter an inconsistent state: deny changing the VLAN awareness state
716 * as long as we have 8021q uppers.
717 */
57d77986 718 if (vlan_filtering && dsa_port_is_user(dp)) {
36cbf39b 719 struct net_device *br = dsa_port_bridge_dev_get(dp);
adb256eb 720 struct net_device *upper_dev, *slave = dp->slave;
adb256eb
VO
721 struct list_head *iter;
722
723 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
724 struct bridge_vlan_info br_info;
725 u16 vid;
726
727 if (!is_vlan_dev(upper_dev))
728 continue;
729
730 vid = vlan_dev_vlan_id(upper_dev);
731
732 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
733 * device, respectively the VID is not found, returning
734 * 0 means success, which is a failure for us here.
735 */
736 err = br_vlan_get_info(br, vid, &br_info);
737 if (err == 0) {
89153ed6
VO
738 NL_SET_ERR_MSG_MOD(extack,
739 "Must first remove VLAN uppers having VIDs also present in bridge");
adb256eb
VO
740 return false;
741 }
742 }
743 }
8f5d16f6
VO
744
745 if (!ds->vlan_filtering_is_global)
746 return true;
747
748 /* For cases where enabling/disabling VLAN awareness is global to the
749 * switch, we need to handle the case where multiple bridges span
750 * different ports of the same switch device and one of them has a
751 * different setting than what is being requested.
752 */
d0004a02 753 dsa_switch_for_each_port(other_dp, ds) {
36cbf39b 754 struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
8f5d16f6 755
8f5d16f6
VO
756 /* If it's the same bridge, it also has same
757 * vlan_filtering setting => no need to check
758 */
36cbf39b 759 if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
8f5d16f6 760 continue;
36cbf39b
VO
761
762 if (br_vlan_enabled(other_br) != vlan_filtering) {
89153ed6
VO
763 NL_SET_ERR_MSG_MOD(extack,
764 "VLAN filtering is a global setting");
8f5d16f6
VO
765 return false;
766 }
767 }
768 return true;
769}
770
89153ed6
VO
771int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
772 struct netlink_ext_ack *extack)
4d61d304 773{
06cfb2df 774 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
4d61d304 775 struct dsa_switch *ds = dp->ds;
bae33f2b 776 bool apply;
33162e9a 777 int err;
4d61d304 778
bae33f2b
VO
779 if (!ds->ops->port_vlan_filtering)
780 return -EOPNOTSUPP;
adb256eb 781
bae33f2b
VO
782 /* We are called from dsa_slave_switchdev_blocking_event(),
783 * which is not under rcu_read_lock(), unlike
784 * dsa_slave_switchdev_event().
785 */
786 rcu_read_lock();
89153ed6 787 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
bae33f2b
VO
788 rcu_read_unlock();
789 if (!apply)
790 return -EINVAL;
8f5d16f6 791
ec9121e7
VO
792 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
793 return 0;
794
89153ed6
VO
795 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
796 extack);
8f5d16f6
VO
797 if (err)
798 return err;
799
06cfb2df 800 if (ds->vlan_filtering_is_global) {
d0004a02 801 struct dsa_port *other_dp;
06cfb2df 802
bae33f2b 803 ds->vlan_filtering = vlan_filtering;
06cfb2df 804
d0004a02 805 dsa_switch_for_each_user_port(other_dp, ds) {
4db2a5ef 806 struct net_device *slave = other_dp->slave;
06cfb2df
VO
807
808 /* We might be called in the unbind path, so not
809 * all slave devices might still be registered.
810 */
06cfb2df
VO
811 if (!slave)
812 continue;
813
814 err = dsa_slave_manage_vlan_filtering(slave,
815 vlan_filtering);
816 if (err)
817 goto restore;
818 }
819 } else {
bae33f2b 820 dp->vlan_filtering = vlan_filtering;
2e554a7a 821
06cfb2df
VO
822 err = dsa_slave_manage_vlan_filtering(dp->slave,
823 vlan_filtering);
824 if (err)
825 goto restore;
826 }
827
4d61d304 828 return 0;
06cfb2df
VO
829
830restore:
831 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
832
833 if (ds->vlan_filtering_is_global)
834 ds->vlan_filtering = old_vlan_filtering;
835 else
836 dp->vlan_filtering = old_vlan_filtering;
837
838 return err;
4d61d304 839}
d87bd94e 840
54a0ed0d
RK
841/* This enforces legacy behavior for switch drivers which assume they can't
842 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
843 */
844bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
845{
36cbf39b 846 struct net_device *br = dsa_port_bridge_dev_get(dp);
54a0ed0d
RK
847 struct dsa_switch *ds = dp->ds;
848
36cbf39b 849 if (!br)
54a0ed0d
RK
850 return false;
851
36cbf39b 852 return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
54a0ed0d
RK
853}
854
bae33f2b 855int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
d87bd94e
VD
856{
857 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
858 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
bae33f2b 859 struct dsa_notifier_ageing_time_info info;
bae33f2b
VO
860 int err;
861
862 info.ageing_time = ageing_time;
d87bd94e 863
bae33f2b
VO
864 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
865 if (err)
866 return err;
d87bd94e 867
d87bd94e 868 dp->ageing_time = ageing_time;
d87bd94e 869
77b61365 870 return 0;
d87bd94e 871}
d1cffff0 872
332afc4c
TW
873int dsa_port_mst_enable(struct dsa_port *dp, bool on,
874 struct netlink_ext_ack *extack)
875{
7414af30 876 if (on && !dsa_port_supports_mst(dp)) {
332afc4c
TW
877 NL_SET_ERR_MSG_MOD(extack, "Hardware does not support MST");
878 return -EINVAL;
879 }
880
881 return 0;
882}
883
e18f4c18 884int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
a8b659e7
VO
885 struct switchdev_brport_flags flags,
886 struct netlink_ext_ack *extack)
ea87005a
FF
887{
888 struct dsa_switch *ds = dp->ds;
889
a8b659e7 890 if (!ds->ops->port_pre_bridge_flags)
ea87005a
FF
891 return -EINVAL;
892
a8b659e7 893 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
ea87005a
FF
894}
895
045c45d1 896int dsa_port_bridge_flags(struct dsa_port *dp,
a8b659e7
VO
897 struct switchdev_brport_flags flags,
898 struct netlink_ext_ack *extack)
57652796
RK
899{
900 struct dsa_switch *ds = dp->ds;
045c45d1 901 int err;
57652796 902
a8b659e7 903 if (!ds->ops->port_bridge_flags)
70a7c484 904 return -EOPNOTSUPP;
57652796 905
045c45d1
VO
906 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
907 if (err)
908 return err;
909
910 if (flags.mask & BR_LEARNING) {
911 bool learning = flags.val & BR_LEARNING;
912
913 if (learning == dp->learning)
914 return 0;
915
bee7c577
VO
916 if ((dp->learning && !learning) &&
917 (dp->stp_state == BR_STATE_LEARNING ||
918 dp->stp_state == BR_STATE_FORWARDING))
045c45d1
VO
919 dsa_port_fast_age(dp);
920
921 dp->learning = learning;
922 }
923
924 return 0;
57652796
RK
925}
926
72c3b0c7
VO
927void dsa_port_set_host_flood(struct dsa_port *dp, bool uc, bool mc)
928{
929 struct dsa_switch *ds = dp->ds;
930
931 if (ds->ops->port_set_host_flood)
932 ds->ops->port_set_host_flood(ds, dp->index, uc, mc);
933}
934
8e6598a7
TW
935int dsa_port_vlan_msti(struct dsa_port *dp,
936 const struct switchdev_vlan_msti *msti)
937{
938 struct dsa_switch *ds = dp->ds;
939
940 if (!ds->ops->vlan_msti_set)
941 return -EOPNOTSUPP;
942
943 return ds->ops->vlan_msti_set(ds, *dp->bridge, msti);
944}
945
be6ff966 946int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu)
bfcb8132
VO
947{
948 struct dsa_notifier_mtu_info info = {
726816a1 949 .dp = dp,
bfcb8132
VO
950 .mtu = new_mtu,
951 };
952
953 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
954}
955
2acf4e6a
AS
956int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
957 u16 vid)
d1cffff0 958{
685fb6a4 959 struct dsa_notifier_fdb_info info = {
726816a1 960 .dp = dp,
2acf4e6a
AS
961 .addr = addr,
962 .vid = vid,
c2693363
VO
963 .db = {
964 .type = DSA_DB_BRIDGE,
965 .bridge = *dp->bridge,
966 },
685fb6a4 967 };
d1cffff0 968
c2693363
VO
969 /* Refcounting takes bridge.num as a key, and should be global for all
970 * bridges in the absence of FDB isolation, and per bridge otherwise.
971 * Force the bridge.num to zero here in the absence of FDB isolation.
972 */
973 if (!dp->ds->fdb_isolation)
974 info.db.bridge.num = 0;
975
685fb6a4 976 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
d1cffff0
VD
977}
978
2acf4e6a
AS
979int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
980 u16 vid)
d1cffff0 981{
685fb6a4 982 struct dsa_notifier_fdb_info info = {
726816a1 983 .dp = dp,
2acf4e6a
AS
984 .addr = addr,
985 .vid = vid,
c2693363
VO
986 .db = {
987 .type = DSA_DB_BRIDGE,
988 .bridge = *dp->bridge,
989 },
685fb6a4 990 };
d1cffff0 991
c2693363
VO
992 if (!dp->ds->fdb_isolation)
993 info.db.bridge.num = 0;
994
685fb6a4 995 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
d1cffff0
VD
996}
997
5e8a1e03
VO
998static int dsa_port_host_fdb_add(struct dsa_port *dp,
999 const unsigned char *addr, u16 vid,
1000 struct dsa_db db)
3dc80afc
VO
1001{
1002 struct dsa_notifier_fdb_info info = {
726816a1 1003 .dp = dp,
3dc80afc
VO
1004 .addr = addr,
1005 .vid = vid,
5e8a1e03
VO
1006 .db = db,
1007 };
1008
1009 if (!dp->ds->fdb_isolation)
1010 info.db.bridge.num = 0;
1011
1012 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
1013}
1014
1015int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
1016 const unsigned char *addr, u16 vid)
1017{
1018 struct dsa_db db = {
1019 .type = DSA_DB_PORT,
1020 .dp = dp,
3dc80afc 1021 };
5e8a1e03
VO
1022
1023 return dsa_port_host_fdb_add(dp, addr, vid, db);
1024}
1025
1026int dsa_port_bridge_host_fdb_add(struct dsa_port *dp,
1027 const unsigned char *addr, u16 vid)
1028{
26ee7b06 1029 struct dsa_port *cpu_dp = dp->cpu_dp;
5e8a1e03
VO
1030 struct dsa_db db = {
1031 .type = DSA_DB_BRIDGE,
1032 .bridge = *dp->bridge,
1033 };
26ee7b06
VO
1034 int err;
1035
8940e6b6
VO
1036 /* Avoid a call to __dev_set_promiscuity() on the master, which
1037 * requires rtnl_lock(), since we can't guarantee that is held here,
1038 * and we can't take it either.
1039 */
1040 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
1041 err = dev_uc_add(cpu_dp->master, addr);
1042 if (err)
1043 return err;
1044 }
3dc80afc 1045
5e8a1e03 1046 return dsa_port_host_fdb_add(dp, addr, vid, db);
3dc80afc
VO
1047}
1048
5e8a1e03
VO
1049static int dsa_port_host_fdb_del(struct dsa_port *dp,
1050 const unsigned char *addr, u16 vid,
1051 struct dsa_db db)
3dc80afc
VO
1052{
1053 struct dsa_notifier_fdb_info info = {
726816a1 1054 .dp = dp,
3dc80afc
VO
1055 .addr = addr,
1056 .vid = vid,
5e8a1e03
VO
1057 .db = db,
1058 };
1059
1060 if (!dp->ds->fdb_isolation)
1061 info.db.bridge.num = 0;
1062
1063 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
1064}
1065
1066int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
1067 const unsigned char *addr, u16 vid)
1068{
1069 struct dsa_db db = {
1070 .type = DSA_DB_PORT,
1071 .dp = dp,
3dc80afc 1072 };
5e8a1e03
VO
1073
1074 return dsa_port_host_fdb_del(dp, addr, vid, db);
1075}
1076
1077int dsa_port_bridge_host_fdb_del(struct dsa_port *dp,
1078 const unsigned char *addr, u16 vid)
1079{
26ee7b06 1080 struct dsa_port *cpu_dp = dp->cpu_dp;
5e8a1e03
VO
1081 struct dsa_db db = {
1082 .type = DSA_DB_BRIDGE,
1083 .bridge = *dp->bridge,
1084 };
26ee7b06
VO
1085 int err;
1086
8940e6b6
VO
1087 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
1088 err = dev_uc_del(cpu_dp->master, addr);
1089 if (err)
1090 return err;
1091 }
3dc80afc 1092
5e8a1e03 1093 return dsa_port_host_fdb_del(dp, addr, vid, db);
3dc80afc
VO
1094}
1095
e212fa7c
VO
1096int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
1097 u16 vid)
1098{
1099 struct dsa_notifier_lag_fdb_info info = {
1100 .lag = dp->lag,
1101 .addr = addr,
1102 .vid = vid,
c2693363
VO
1103 .db = {
1104 .type = DSA_DB_BRIDGE,
1105 .bridge = *dp->bridge,
1106 },
e212fa7c
VO
1107 };
1108
c2693363
VO
1109 if (!dp->ds->fdb_isolation)
1110 info.db.bridge.num = 0;
1111
e212fa7c
VO
1112 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
1113}
1114
1115int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
1116 u16 vid)
1117{
1118 struct dsa_notifier_lag_fdb_info info = {
1119 .lag = dp->lag,
1120 .addr = addr,
1121 .vid = vid,
c2693363
VO
1122 .db = {
1123 .type = DSA_DB_BRIDGE,
1124 .bridge = *dp->bridge,
1125 },
e212fa7c
VO
1126 };
1127
c2693363
VO
1128 if (!dp->ds->fdb_isolation)
1129 info.db.bridge.num = 0;
1130
e212fa7c
VO
1131 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_DEL, &info);
1132}
1133
de40fc5d
VD
1134int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
1135{
1136 struct dsa_switch *ds = dp->ds;
1137 int port = dp->index;
1138
1139 if (!ds->ops->port_fdb_dump)
1140 return -EOPNOTSUPP;
1141
1142 return ds->ops->port_fdb_dump(ds, port, cb, data);
1143}
1144
bb9f6031 1145int dsa_port_mdb_add(const struct dsa_port *dp,
ffb68fc5 1146 const struct switchdev_obj_port_mdb *mdb)
3a9afea3 1147{
8ae5bcdc 1148 struct dsa_notifier_mdb_info info = {
726816a1 1149 .dp = dp,
8ae5bcdc 1150 .mdb = mdb,
c2693363
VO
1151 .db = {
1152 .type = DSA_DB_BRIDGE,
1153 .bridge = *dp->bridge,
1154 },
8ae5bcdc 1155 };
3a9afea3 1156
c2693363
VO
1157 if (!dp->ds->fdb_isolation)
1158 info.db.bridge.num = 0;
1159
8ae5bcdc 1160 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
3a9afea3
VD
1161}
1162
bb9f6031 1163int dsa_port_mdb_del(const struct dsa_port *dp,
3a9afea3
VD
1164 const struct switchdev_obj_port_mdb *mdb)
1165{
8ae5bcdc 1166 struct dsa_notifier_mdb_info info = {
726816a1 1167 .dp = dp,
8ae5bcdc 1168 .mdb = mdb,
c2693363
VO
1169 .db = {
1170 .type = DSA_DB_BRIDGE,
1171 .bridge = *dp->bridge,
1172 },
8ae5bcdc 1173 };
3a9afea3 1174
c2693363
VO
1175 if (!dp->ds->fdb_isolation)
1176 info.db.bridge.num = 0;
1177
8ae5bcdc 1178 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
3a9afea3
VD
1179}
1180
5e8a1e03
VO
1181static int dsa_port_host_mdb_add(const struct dsa_port *dp,
1182 const struct switchdev_obj_port_mdb *mdb,
1183 struct dsa_db db)
b8e997c4
VO
1184{
1185 struct dsa_notifier_mdb_info info = {
726816a1 1186 .dp = dp,
b8e997c4 1187 .mdb = mdb,
5e8a1e03
VO
1188 .db = db,
1189 };
1190
1191 if (!dp->ds->fdb_isolation)
1192 info.db.bridge.num = 0;
1193
1194 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
1195}
1196
1197int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
1198 const struct switchdev_obj_port_mdb *mdb)
1199{
1200 struct dsa_db db = {
1201 .type = DSA_DB_PORT,
1202 .dp = dp,
b8e997c4 1203 };
5e8a1e03
VO
1204
1205 return dsa_port_host_mdb_add(dp, mdb, db);
1206}
1207
1208int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
1209 const struct switchdev_obj_port_mdb *mdb)
1210{
26ee7b06 1211 struct dsa_port *cpu_dp = dp->cpu_dp;
5e8a1e03
VO
1212 struct dsa_db db = {
1213 .type = DSA_DB_BRIDGE,
1214 .bridge = *dp->bridge,
1215 };
26ee7b06
VO
1216 int err;
1217
1218 err = dev_mc_add(cpu_dp->master, mdb->addr);
1219 if (err)
1220 return err;
b8e997c4 1221
5e8a1e03 1222 return dsa_port_host_mdb_add(dp, mdb, db);
b8e997c4
VO
1223}
1224
5e8a1e03
VO
1225static int dsa_port_host_mdb_del(const struct dsa_port *dp,
1226 const struct switchdev_obj_port_mdb *mdb,
1227 struct dsa_db db)
b8e997c4
VO
1228{
1229 struct dsa_notifier_mdb_info info = {
726816a1 1230 .dp = dp,
b8e997c4 1231 .mdb = mdb,
5e8a1e03
VO
1232 .db = db,
1233 };
1234
1235 if (!dp->ds->fdb_isolation)
1236 info.db.bridge.num = 0;
1237
1238 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
1239}
1240
1241int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
1242 const struct switchdev_obj_port_mdb *mdb)
1243{
1244 struct dsa_db db = {
1245 .type = DSA_DB_PORT,
1246 .dp = dp,
b8e997c4 1247 };
5e8a1e03
VO
1248
1249 return dsa_port_host_mdb_del(dp, mdb, db);
1250}
1251
1252int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
1253 const struct switchdev_obj_port_mdb *mdb)
1254{
26ee7b06 1255 struct dsa_port *cpu_dp = dp->cpu_dp;
5e8a1e03
VO
1256 struct dsa_db db = {
1257 .type = DSA_DB_BRIDGE,
1258 .bridge = *dp->bridge,
1259 };
26ee7b06
VO
1260 int err;
1261
1262 err = dev_mc_del(cpu_dp->master, mdb->addr);
1263 if (err)
1264 return err;
b8e997c4 1265
5e8a1e03 1266 return dsa_port_host_mdb_del(dp, mdb, db);
b8e997c4
VO
1267}
1268
076e7133 1269int dsa_port_vlan_add(struct dsa_port *dp,
31046a5f
VO
1270 const struct switchdev_obj_port_vlan *vlan,
1271 struct netlink_ext_ack *extack)
076e7133 1272{
d0c627b8 1273 struct dsa_notifier_vlan_info info = {
726816a1 1274 .dp = dp,
d0c627b8 1275 .vlan = vlan,
31046a5f 1276 .extack = extack,
d0c627b8 1277 };
076e7133 1278
c5335d73 1279 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
076e7133
VD
1280}
1281
1282int dsa_port_vlan_del(struct dsa_port *dp,
1283 const struct switchdev_obj_port_vlan *vlan)
1284{
d0c627b8 1285 struct dsa_notifier_vlan_info info = {
726816a1 1286 .dp = dp,
d0c627b8
VD
1287 .vlan = vlan,
1288 };
076e7133 1289
c5335d73 1290 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
076e7133 1291}
57ab1ca2 1292
134ef238
VO
1293int dsa_port_host_vlan_add(struct dsa_port *dp,
1294 const struct switchdev_obj_port_vlan *vlan,
1295 struct netlink_ext_ack *extack)
1296{
1297 struct dsa_notifier_vlan_info info = {
726816a1 1298 .dp = dp,
134ef238
VO
1299 .vlan = vlan,
1300 .extack = extack,
1301 };
1302 struct dsa_port *cpu_dp = dp->cpu_dp;
1303 int err;
1304
1305 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
1306 if (err && err != -EOPNOTSUPP)
1307 return err;
1308
1309 vlan_vid_add(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1310
1311 return err;
1312}
1313
1314int dsa_port_host_vlan_del(struct dsa_port *dp,
1315 const struct switchdev_obj_port_vlan *vlan)
1316{
1317 struct dsa_notifier_vlan_info info = {
726816a1 1318 .dp = dp,
134ef238
VO
1319 .vlan = vlan,
1320 };
1321 struct dsa_port *cpu_dp = dp->cpu_dp;
1322 int err;
1323
1324 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
1325 if (err && err != -EOPNOTSUPP)
1326 return err;
1327
1328 vlan_vid_del(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1329
1330 return err;
1331}
1332
c595c433
HV
1333int dsa_port_mrp_add(const struct dsa_port *dp,
1334 const struct switchdev_obj_mrp *mrp)
1335{
cad69019
VO
1336 struct dsa_switch *ds = dp->ds;
1337
1338 if (!ds->ops->port_mrp_add)
1339 return -EOPNOTSUPP;
c595c433 1340
cad69019 1341 return ds->ops->port_mrp_add(ds, dp->index, mrp);
c595c433
HV
1342}
1343
1344int dsa_port_mrp_del(const struct dsa_port *dp,
1345 const struct switchdev_obj_mrp *mrp)
1346{
cad69019
VO
1347 struct dsa_switch *ds = dp->ds;
1348
1349 if (!ds->ops->port_mrp_del)
1350 return -EOPNOTSUPP;
c595c433 1351
cad69019 1352 return ds->ops->port_mrp_del(ds, dp->index, mrp);
c595c433
HV
1353}
1354
1355int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
1356 const struct switchdev_obj_ring_role_mrp *mrp)
1357{
cad69019
VO
1358 struct dsa_switch *ds = dp->ds;
1359
1360 if (!ds->ops->port_mrp_add_ring_role)
1361 return -EOPNOTSUPP;
c595c433 1362
cad69019 1363 return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
c595c433
HV
1364}
1365
1366int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
1367 const struct switchdev_obj_ring_role_mrp *mrp)
1368{
cad69019
VO
1369 struct dsa_switch *ds = dp->ds;
1370
1371 if (!ds->ops->port_mrp_del_ring_role)
1372 return -EOPNOTSUPP;
c595c433 1373
cad69019 1374 return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
c595c433
HV
1375}
1376
53da0eba
VO
1377void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
1378 const struct dsa_device_ops *tag_ops)
1379{
53da0eba
VO
1380 cpu_dp->rcv = tag_ops->rcv;
1381 cpu_dp->tag_ops = tag_ops;
1382}
1383
6207a78c 1384static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
33615367 1385{
33615367 1386 struct device_node *phy_dn;
33615367 1387 struct phy_device *phydev;
33615367 1388
6207a78c 1389 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
33615367 1390 if (!phy_dn)
6207a78c 1391 return NULL;
33615367
SR
1392
1393 phydev = of_phy_find_device(phy_dn);
1394 if (!phydev) {
6207a78c
FF
1395 of_node_put(phy_dn);
1396 return ERR_PTR(-EPROBE_DEFER);
33615367
SR
1397 }
1398
9919a363 1399 of_node_put(phy_dn);
6207a78c
FF
1400 return phydev;
1401}
1402
8ae67496
FF
1403static void dsa_port_phylink_validate(struct phylink_config *config,
1404 unsigned long *supported,
1405 struct phylink_link_state *state)
77373d49
IC
1406{
1407 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1408 struct dsa_switch *ds = dp->ds;
1409
5938bce4
RKO
1410 if (!ds->ops->phylink_validate) {
1411 if (config->mac_capabilities)
1412 phylink_generic_validate(config, supported, state);
77373d49 1413 return;
5938bce4 1414 }
77373d49
IC
1415
1416 ds->ops->phylink_validate(ds, dp->index, supported, state);
1417}
77373d49 1418
8ae67496
FF
1419static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
1420 struct phylink_link_state *state)
77373d49
IC
1421{
1422 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1423 struct dsa_switch *ds = dp->ds;
87615c96 1424 int err;
77373d49 1425
d46b7e4f
RK
1426 /* Only called for inband modes */
1427 if (!ds->ops->phylink_mac_link_state) {
1428 state->link = 0;
1429 return;
1430 }
77373d49 1431
87615c96
RK
1432 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
1433 if (err < 0) {
1434 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
1435 dp->index, err);
d46b7e4f 1436 state->link = 0;
87615c96 1437 }
77373d49 1438}
77373d49 1439
bde01822
RKO
1440static struct phylink_pcs *
1441dsa_port_phylink_mac_select_pcs(struct phylink_config *config,
1442 phy_interface_t interface)
1443{
1444 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
10544570 1445 struct phylink_pcs *pcs = ERR_PTR(-EOPNOTSUPP);
bde01822 1446 struct dsa_switch *ds = dp->ds;
bde01822
RKO
1447
1448 if (ds->ops->phylink_mac_select_pcs)
1449 pcs = ds->ops->phylink_mac_select_pcs(ds, dp->index, interface);
1450
1451 return pcs;
1452}
1453
8ae67496
FF
1454static void dsa_port_phylink_mac_config(struct phylink_config *config,
1455 unsigned int mode,
1456 const struct phylink_link_state *state)
77373d49
IC
1457{
1458 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1459 struct dsa_switch *ds = dp->ds;
1460
1461 if (!ds->ops->phylink_mac_config)
1462 return;
1463
1464 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1465}
77373d49 1466
8ae67496 1467static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
77373d49
IC
1468{
1469 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1470 struct dsa_switch *ds = dp->ds;
1471
1472 if (!ds->ops->phylink_mac_an_restart)
1473 return;
1474
1475 ds->ops->phylink_mac_an_restart(ds, dp->index);
1476}
77373d49 1477
8ae67496
FF
1478static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1479 unsigned int mode,
1480 phy_interface_t interface)
77373d49
IC
1481{
1482 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
0e279218 1483 struct phy_device *phydev = NULL;
77373d49
IC
1484 struct dsa_switch *ds = dp->ds;
1485
57d77986 1486 if (dsa_port_is_user(dp))
0e279218
IC
1487 phydev = dp->slave->phydev;
1488
77373d49 1489 if (!ds->ops->phylink_mac_link_down) {
0e279218
IC
1490 if (ds->ops->adjust_link && phydev)
1491 ds->ops->adjust_link(ds, dp->index, phydev);
77373d49
IC
1492 return;
1493 }
1494
1495 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1496}
77373d49 1497
8ae67496 1498static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
91a208f2 1499 struct phy_device *phydev,
8ae67496
FF
1500 unsigned int mode,
1501 phy_interface_t interface,
91a208f2
RK
1502 int speed, int duplex,
1503 bool tx_pause, bool rx_pause)
77373d49
IC
1504{
1505 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
77373d49
IC
1506 struct dsa_switch *ds = dp->ds;
1507
1508 if (!ds->ops->phylink_mac_link_up) {
0e279218
IC
1509 if (ds->ops->adjust_link && phydev)
1510 ds->ops->adjust_link(ds, dp->index, phydev);
77373d49
IC
1511 return;
1512 }
1513
5b502a7b
RK
1514 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1515 speed, duplex, tx_pause, rx_pause);
77373d49 1516}
77373d49 1517
21bd64bd 1518static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
77373d49 1519 .validate = dsa_port_phylink_validate,
bde01822 1520 .mac_select_pcs = dsa_port_phylink_mac_select_pcs,
d46b7e4f 1521 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
77373d49
IC
1522 .mac_config = dsa_port_phylink_mac_config,
1523 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1524 .mac_link_down = dsa_port_phylink_mac_link_down,
1525 .mac_link_up = dsa_port_phylink_mac_link_up,
1526};
1527
21bd64bd
RKO
1528int dsa_port_phylink_create(struct dsa_port *dp)
1529{
1530 struct dsa_switch *ds = dp->ds;
1531 phy_interface_t mode;
1532 int err;
1533
1534 err = of_get_phy_mode(dp->dn, &mode);
1535 if (err)
1536 mode = PHY_INTERFACE_MODE_NA;
1537
0a9f0794
RKO
1538 /* Presence of phylink_mac_link_state or phylink_mac_an_restart is
1539 * an indicator of a legacy phylink driver.
1540 */
1541 if (ds->ops->phylink_mac_link_state ||
1542 ds->ops->phylink_mac_an_restart)
1543 dp->pl_config.legacy_pre_march2020 = true;
1544
072eea6c
RKO
1545 if (ds->ops->phylink_get_caps)
1546 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
21bd64bd
RKO
1547
1548 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn),
1549 mode, &dsa_port_phylink_mac_ops);
1550 if (IS_ERR(dp->pl)) {
1551 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1552 return PTR_ERR(dp->pl);
1553 }
1554
1555 return 0;
1556}
1557
6207a78c
FF
1558static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1559{
1560 struct dsa_switch *ds = dp->ds;
1561 struct phy_device *phydev;
1562 int port = dp->index;
1563 int err = 0;
1564
1565 phydev = dsa_port_get_phy_device(dp);
1566 if (!phydev)
1567 return 0;
1568
1569 if (IS_ERR(phydev))
1570 return PTR_ERR(phydev);
1571
33615367 1572 if (enable) {
33615367
SR
1573 err = genphy_resume(phydev);
1574 if (err < 0)
1575 goto err_put_dev;
1576
1577 err = genphy_read_status(phydev);
1578 if (err < 0)
1579 goto err_put_dev;
1580 } else {
1581 err = genphy_suspend(phydev);
1582 if (err < 0)
1583 goto err_put_dev;
1584 }
1585
1586 if (ds->ops->adjust_link)
1587 ds->ops->adjust_link(ds, port, phydev);
1588
1589 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1590
1591err_put_dev:
1592 put_device(&phydev->mdio.dev);
33615367
SR
1593 return err;
1594}
1595
1596static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
57ab1ca2
VD
1597{
1598 struct device_node *dn = dp->dn;
1599 struct dsa_switch *ds = dp->ds;
1600 struct phy_device *phydev;
1601 int port = dp->index;
0c65b2b9 1602 phy_interface_t mode;
57ab1ca2
VD
1603 int err;
1604
33615367
SR
1605 err = of_phy_register_fixed_link(dn);
1606 if (err) {
1607 dev_err(ds->dev,
1608 "failed to register the fixed PHY of port %d\n",
1609 port);
1610 return err;
1611 }
57ab1ca2 1612
33615367 1613 phydev = of_phy_find_device(dn);
57ab1ca2 1614
0c65b2b9
AL
1615 err = of_get_phy_mode(dn, &mode);
1616 if (err)
33615367
SR
1617 mode = PHY_INTERFACE_MODE_NA;
1618 phydev->interface = mode;
57ab1ca2 1619
33615367 1620 genphy_read_status(phydev);
57ab1ca2 1621
33615367
SR
1622 if (ds->ops->adjust_link)
1623 ds->ops->adjust_link(ds, port, phydev);
57ab1ca2 1624
33615367 1625 put_device(&phydev->mdio.dev);
57ab1ca2
VD
1626
1627 return 0;
1628}
1629
0e279218
IC
1630static int dsa_port_phylink_register(struct dsa_port *dp)
1631{
1632 struct dsa_switch *ds = dp->ds;
1633 struct device_node *port_dn = dp->dn;
0c65b2b9 1634 int err;
0e279218 1635
0e279218
IC
1636 dp->pl_config.dev = ds->dev;
1637 dp->pl_config.type = PHYLINK_DEV;
1638
21bd64bd
RKO
1639 err = dsa_port_phylink_create(dp);
1640 if (err)
1641 return err;
0e279218
IC
1642
1643 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
2131fba5 1644 if (err && err != -ENODEV) {
0e279218
IC
1645 pr_err("could not attach to PHY: %d\n", err);
1646 goto err_phy_connect;
1647 }
1648
0e279218
IC
1649 return 0;
1650
1651err_phy_connect:
1652 phylink_destroy(dp->pl);
1653 return err;
1654}
1655
33615367 1656int dsa_port_link_register_of(struct dsa_port *dp)
57ab1ca2 1657{
0e279218 1658 struct dsa_switch *ds = dp->ds;
a20f9970 1659 struct device_node *phy_np;
3be98b2d 1660 int port = dp->index;
0e279218 1661
a20f9970
AL
1662 if (!ds->ops->adjust_link) {
1663 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
3be98b2d
AL
1664 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1665 if (ds->ops->phylink_mac_link_down)
1666 ds->ops->phylink_mac_link_down(ds, port,
1667 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
fc06b286 1668 of_node_put(phy_np);
a20f9970 1669 return dsa_port_phylink_register(dp);
3be98b2d 1670 }
fc06b286 1671 of_node_put(phy_np);
a20f9970
AL
1672 return 0;
1673 }
0e279218
IC
1674
1675 dev_warn(ds->dev,
1676 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1677
33615367
SR
1678 if (of_phy_is_fixed_link(dp->dn))
1679 return dsa_port_fixed_link_register_of(dp);
1680 else
1681 return dsa_port_setup_phy_of(dp, true);
1682}
57ab1ca2 1683
33615367
SR
1684void dsa_port_link_unregister_of(struct dsa_port *dp)
1685{
0e279218
IC
1686 struct dsa_switch *ds = dp->ds;
1687
a20f9970 1688 if (!ds->ops->adjust_link && dp->pl) {
0e279218
IC
1689 rtnl_lock();
1690 phylink_disconnect_phy(dp->pl);
1691 rtnl_unlock();
1692 phylink_destroy(dp->pl);
a20f9970 1693 dp->pl = NULL;
0e279218
IC
1694 return;
1695 }
1696
33615367
SR
1697 if (of_phy_is_fixed_link(dp->dn))
1698 of_phy_deregister_fixed_link(dp->dn);
1699 else
1700 dsa_port_setup_phy_of(dp, false);
57ab1ca2 1701}
cf963573 1702
18596f50
GM
1703int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1704{
a68dc7b9 1705 struct dsa_switch *ds = dp->ds;
18596f50
GM
1706 int err;
1707
a68dc7b9
VO
1708 if (!ds->ops->port_hsr_join)
1709 return -EOPNOTSUPP;
1710
18596f50
GM
1711 dp->hsr_dev = hsr;
1712
a68dc7b9 1713 err = ds->ops->port_hsr_join(ds, dp->index, hsr);
18596f50
GM
1714 if (err)
1715 dp->hsr_dev = NULL;
1716
1717 return err;
1718}
1719
1720void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1721{
a68dc7b9 1722 struct dsa_switch *ds = dp->ds;
18596f50
GM
1723 int err;
1724
1725 dp->hsr_dev = NULL;
1726
a68dc7b9
VO
1727 if (ds->ops->port_hsr_leave) {
1728 err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
1729 if (err)
1730 dev_err(dp->ds->dev,
1731 "port %d failed to leave HSR %s: %pe\n",
1732 dp->index, hsr->name, ERR_PTR(err));
1733 }
18596f50 1734}
c64b9c05 1735
724395f4 1736int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
c64b9c05
VO
1737{
1738 struct dsa_notifier_tag_8021q_vlan_info info = {
726816a1 1739 .dp = dp,
c64b9c05
VO
1740 .vid = vid,
1741 };
1742
724395f4
VO
1743 if (broadcast)
1744 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1745
1746 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
c64b9c05
VO
1747}
1748
724395f4 1749void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
c64b9c05
VO
1750{
1751 struct dsa_notifier_tag_8021q_vlan_info info = {
726816a1 1752 .dp = dp,
c64b9c05
VO
1753 .vid = vid,
1754 };
1755 int err;
1756
724395f4
VO
1757 if (broadcast)
1758 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1759 else
1760 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
c64b9c05 1761 if (err)
ab97462b
VO
1762 dev_err(dp->ds->dev,
1763 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1764 dp->index, vid, ERR_PTR(err));
c64b9c05 1765}