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