Linux 6.12-rc1
[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>
95f510d0 10#include <linux/netdevice.h>
cfbed329 11#include <linux/notifier.h>
57ab1ca2
VD
12#include <linux/of_mdio.h>
13#include <linux/of_net.h>
a40c175b 14
47d2ce03 15#include "dsa.h"
022bba63 16#include "port.h"
0c603136 17#include "switch.h"
19d05ea7 18#include "tag_8021q.h"
6ca80638 19#include "user.h"
a40c175b 20
886f8e26
VO
21/**
22 * dsa_port_notify - Notify the switching fabric of changes to a port
23 * @dp: port on which change occurred
24 * @e: event, must be of type DSA_NOTIFIER_*
25 * @v: event-specific value.
26 *
27 * Notify all switches in the DSA tree that this port's switch belongs to,
28 * including this switch itself, of an event. Allows the other switches to
29 * reconfigure themselves for cross-chip operations. Can also be used to
30 * reconfigure ports without net_devices (CPU ports, DSA links) whenever
31 * a user port's state changes.
32 */
bb9f6031 33static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
cfbed329 34{
886f8e26 35 return dsa_tree_notify(dp->ds->dst, e, v);
cfbed329
VD
36}
37
7414af30 38static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp, u16 vid)
9264e4ad
VO
39{
40 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
41 struct switchdev_notifier_fdb_info info = {
7414af30 42 .vid = vid,
9264e4ad
VO
43 };
44
45 /* When the port becomes standalone it has already left the bridge.
46 * Don't notify the bridge in that case.
47 */
48 if (!brport_dev)
49 return;
50
51 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
52 brport_dev, &info.info, NULL);
53}
54
045c45d1
VO
55static void dsa_port_fast_age(const struct dsa_port *dp)
56{
57 struct dsa_switch *ds = dp->ds;
58
59 if (!ds->ops->port_fast_age)
60 return;
61
62 ds->ops->port_fast_age(ds, dp->index);
9264e4ad 63
7414af30
TW
64 /* flush all VLANs */
65 dsa_port_notify_bridge_fdb_flush(dp, 0);
66}
67
68static int dsa_port_vlan_fast_age(const struct dsa_port *dp, u16 vid)
69{
70 struct dsa_switch *ds = dp->ds;
71 int err;
72
73 if (!ds->ops->port_vlan_fast_age)
74 return -EOPNOTSUPP;
75
76 err = ds->ops->port_vlan_fast_age(ds, dp->index, vid);
77
78 if (!err)
79 dsa_port_notify_bridge_fdb_flush(dp, vid);
80
81 return err;
82}
83
84static int dsa_port_msti_fast_age(const struct dsa_port *dp, u16 msti)
85{
86 DECLARE_BITMAP(vids, VLAN_N_VID) = { 0 };
87 int err, vid;
88
89 err = br_mst_get_info(dsa_port_bridge_dev_get(dp), msti, vids);
90 if (err)
91 return err;
92
93 for_each_set_bit(vid, vids, VLAN_N_VID) {
94 err = dsa_port_vlan_fast_age(dp, vid);
95 if (err)
96 return err;
97 }
98
99 return 0;
045c45d1
VO
100}
101
a4ffe09f
VO
102static bool dsa_port_can_configure_learning(struct dsa_port *dp)
103{
104 struct switchdev_brport_flags flags = {
105 .mask = BR_LEARNING,
106 };
107 struct dsa_switch *ds = dp->ds;
108 int err;
109
110 if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags)
111 return false;
112
113 err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL);
114 return !err;
115}
116
ff6ac4d0 117bool dsa_port_supports_hwtstamp(struct dsa_port *dp)
ed1fe1be
VO
118{
119 struct dsa_switch *ds = dp->ds;
ff6ac4d0 120 struct ifreq ifr = {};
ed1fe1be
VO
121 int err;
122
123 if (!ds->ops->port_hwtstamp_get || !ds->ops->port_hwtstamp_set)
124 return false;
125
126 /* "See through" shim implementations of the "get" method.
ff6ac4d0
VO
127 * Since we can't cook up a complete ioctl request structure, this will
128 * fail in copy_to_user() with -EFAULT, which hopefully is enough to
129 * detect a valid implementation.
ed1fe1be 130 */
ff6ac4d0 131 err = ds->ops->port_hwtstamp_get(ds, dp->index, &ifr);
ed1fe1be
VO
132 return err != -EOPNOTSUPP;
133}
134
39f32101 135int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
a40c175b
VD
136{
137 struct dsa_switch *ds = dp->ds;
138 int port = dp->index;
139
bae33f2b
VO
140 if (!ds->ops->port_stp_state_set)
141 return -EOPNOTSUPP;
a40c175b 142
bae33f2b 143 ds->ops->port_stp_state_set(ds, port, state);
a40c175b 144
a4ffe09f
VO
145 if (!dsa_port_can_configure_learning(dp) ||
146 (do_fast_age && dp->learning)) {
a40c175b
VD
147 /* Fast age FDB entries or flush appropriate forwarding database
148 * for the given port, if we are moving it from Learning or
149 * Forwarding state, to Disabled or Blocking or Listening state.
39f32101
VO
150 * Ports that were standalone before the STP state change don't
151 * need to fast age the FDB, since address learning is off in
152 * standalone mode.
a40c175b
VD
153 */
154
155 if ((dp->stp_state == BR_STATE_LEARNING ||
156 dp->stp_state == BR_STATE_FORWARDING) &&
157 (state == BR_STATE_DISABLED ||
158 state == BR_STATE_BLOCKING ||
159 state == BR_STATE_LISTENING))
045c45d1 160 dsa_port_fast_age(dp);
a40c175b
VD
161 }
162
163 dp->stp_state = state;
164
165 return 0;
166}
167
39f32101
VO
168static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
169 bool do_fast_age)
a40c175b 170{
211987f3 171 struct dsa_switch *ds = dp->ds;
a40c175b
VD
172 int err;
173
39f32101 174 err = dsa_port_set_state(dp, state, do_fast_age);
211987f3
VO
175 if (err && err != -EOPNOTSUPP) {
176 dev_err(ds->dev, "port %d failed to set STP state %u: %pe\n",
177 dp->index, state, ERR_PTR(err));
178 }
a40c175b 179}
cfbed329 180
7414af30
TW
181int dsa_port_set_mst_state(struct dsa_port *dp,
182 const struct switchdev_mst_state *state,
183 struct netlink_ext_ack *extack)
184{
185 struct dsa_switch *ds = dp->ds;
186 u8 prev_state;
187 int err;
188
189 if (!ds->ops->port_mst_state_set)
190 return -EOPNOTSUPP;
191
192 err = br_mst_get_state(dsa_port_to_bridge_port(dp), state->msti,
193 &prev_state);
194 if (err)
195 return err;
196
197 err = ds->ops->port_mst_state_set(ds, dp->index, state);
198 if (err)
199 return err;
200
201 if (!(dp->learning &&
202 (prev_state == BR_STATE_LEARNING ||
203 prev_state == BR_STATE_FORWARDING) &&
204 (state->state == BR_STATE_DISABLED ||
205 state->state == BR_STATE_BLOCKING ||
206 state->state == BR_STATE_LISTENING)))
207 return 0;
208
209 err = dsa_port_msti_fast_age(dp, state->msti);
210 if (err)
211 NL_SET_ERR_MSG_MOD(extack,
212 "Unable to flush associated VLANs");
213
214 return 0;
215}
216
8640f8dc 217int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
fb8a6a2b 218{
fb8a6a2b
VD
219 struct dsa_switch *ds = dp->ds;
220 int port = dp->index;
221 int err;
222
223 if (ds->ops->port_enable) {
224 err = ds->ops->port_enable(ds, port, phy);
225 if (err)
226 return err;
227 }
228
d3eed0e5 229 if (!dp->bridge)
39f32101 230 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
fb8a6a2b 231
8640f8dc
RK
232 if (dp->pl)
233 phylink_start(dp->pl);
234
fb8a6a2b
VD
235 return 0;
236}
237
8640f8dc
RK
238int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
239{
240 int err;
241
242 rtnl_lock();
243 err = dsa_port_enable_rt(dp, phy);
244 rtnl_unlock();
245
246 return err;
247}
248
249void dsa_port_disable_rt(struct dsa_port *dp)
fb8a6a2b
VD
250{
251 struct dsa_switch *ds = dp->ds;
252 int port = dp->index;
253
8640f8dc
RK
254 if (dp->pl)
255 phylink_stop(dp->pl);
256
d3eed0e5 257 if (!dp->bridge)
39f32101 258 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
fb8a6a2b
VD
259
260 if (ds->ops->port_disable)
75104db0 261 ds->ops->port_disable(ds, port);
fb8a6a2b
VD
262}
263
8640f8dc
RK
264void dsa_port_disable(struct dsa_port *dp)
265{
266 rtnl_lock();
267 dsa_port_disable_rt(dp);
268 rtnl_unlock();
269}
270
8e9e678e
VO
271static void dsa_port_reset_vlan_filtering(struct dsa_port *dp,
272 struct dsa_bridge bridge)
273{
274 struct netlink_ext_ack extack = {0};
275 bool change_vlan_filtering = false;
276 struct dsa_switch *ds = dp->ds;
1699b4d5 277 struct dsa_port *other_dp;
8e9e678e
VO
278 bool vlan_filtering;
279 int err;
280
281 if (ds->needs_standalone_vlan_filtering &&
282 !br_vlan_enabled(bridge.dev)) {
283 change_vlan_filtering = true;
284 vlan_filtering = true;
285 } else if (!ds->needs_standalone_vlan_filtering &&
286 br_vlan_enabled(bridge.dev)) {
287 change_vlan_filtering = true;
288 vlan_filtering = false;
289 }
290
291 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
6ca80638 292 * event for changing vlan_filtering setting upon user ports leaving
8e9e678e
VO
293 * it. That is a good thing, because that lets us handle it and also
294 * handle the case where the switch's vlan_filtering setting is global
295 * (not per port). When that happens, the correct moment to trigger the
296 * vlan_filtering callback is only when the last port leaves the last
297 * VLAN-aware bridge.
298 */
299 if (change_vlan_filtering && ds->vlan_filtering_is_global) {
1699b4d5
VO
300 dsa_switch_for_each_port(other_dp, ds) {
301 struct net_device *br = dsa_port_bridge_dev_get(other_dp);
8e9e678e
VO
302
303 if (br && br_vlan_enabled(br)) {
304 change_vlan_filtering = false;
305 break;
306 }
307 }
308 }
309
310 if (!change_vlan_filtering)
311 return;
312
313 err = dsa_port_vlan_filtering(dp, vlan_filtering, &extack);
314 if (extack._msg) {
315 dev_err(ds->dev, "port %d: %s\n", dp->index,
316 extack._msg);
317 }
318 if (err && err != -EOPNOTSUPP) {
319 dev_err(ds->dev,
320 "port %d failed to reset VLAN filtering to %d: %pe\n",
321 dp->index, vlan_filtering, ERR_PTR(err));
322 }
323}
324
5961d6a1
VO
325static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
326 struct netlink_ext_ack *extack)
5e38c158 327{
5961d6a1 328 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
b9e8b58f 329 BR_BCAST_FLOOD | BR_PORT_LOCKED;
5961d6a1
VO
330 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
331 int flag, err;
5e38c158 332
5961d6a1
VO
333 for_each_set_bit(flag, &mask, 32) {
334 struct switchdev_brport_flags flags = {0};
5e38c158 335
5961d6a1 336 flags.mask = BIT(flag);
e18f4c18 337
5961d6a1
VO
338 if (br_port_flag_is_set(brport_dev, BIT(flag)))
339 flags.val = BIT(flag);
5e38c158 340
5961d6a1
VO
341 err = dsa_port_bridge_flags(dp, flags, extack);
342 if (err && err != -EOPNOTSUPP)
343 return err;
344 }
345
346 return 0;
347}
348
349static void dsa_port_clear_brport_flags(struct dsa_port *dp)
350{
351 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
352 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
b9e8b58f 353 BR_BCAST_FLOOD | BR_PORT_LOCKED;
5961d6a1
VO
354 int flag, err;
355
356 for_each_set_bit(flag, &mask, 32) {
357 struct switchdev_brport_flags flags = {0};
358
359 flags.mask = BIT(flag);
360 flags.val = val & BIT(flag);
361
362 err = dsa_port_bridge_flags(dp, flags, NULL);
363 if (err && err != -EOPNOTSUPP)
364 dev_err(dp->ds->dev,
365 "failed to clear bridge port flag %lu: %pe\n",
366 flags.val, ERR_PTR(err));
5e38c158
VO
367 }
368}
369
4e51bf44
VO
370static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
371 struct netlink_ext_ack *extack)
5961d6a1 372{
010e269f 373 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
36cbf39b 374 struct net_device *br = dsa_port_bridge_dev_get(dp);
5961d6a1
VO
375 int err;
376
377 err = dsa_port_inherit_brport_flags(dp, extack);
378 if (err)
379 return err;
380
39f32101 381 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
010e269f
VO
382 if (err && err != -EOPNOTSUPP)
383 return err;
384
385 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
386 if (err && err != -EOPNOTSUPP)
387 return err;
388
010e269f
VO
389 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
390 if (err && err != -EOPNOTSUPP)
391 return err;
392
74918945
VO
393 return 0;
394}
395
8e9e678e
VO
396static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp,
397 struct dsa_bridge bridge)
5961d6a1
VO
398{
399 /* Configure the port for standalone mode (no address learning,
400 * flood everything).
401 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
402 * when the user requests it through netlink or sysfs, but not
403 * automatically at port join or leave, so we need to handle resetting
404 * the brport flags ourselves. But we even prefer it that way, because
405 * otherwise, some setups might never get the notification they need,
406 * for example, when a port leaves a LAG that offloads the bridge,
407 * it becomes standalone, but as far as the bridge is concerned, no
408 * port ever left.
409 */
410 dsa_port_clear_brport_flags(dp);
411
412 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
413 * so allow it to be in BR_STATE_FORWARDING to be kept functional
414 */
39f32101 415 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
010e269f 416
8e9e678e 417 dsa_port_reset_vlan_filtering(dp, bridge);
010e269f 418
010e269f
VO
419 /* Ageing time may be global to the switch chip, so don't change it
420 * here because we have no good reason (or value) to change it to.
421 */
5961d6a1
VO
422}
423
947c8746
VO
424static int dsa_port_bridge_create(struct dsa_port *dp,
425 struct net_device *br,
426 struct netlink_ext_ack *extack)
427{
428 struct dsa_switch *ds = dp->ds;
d3eed0e5 429 struct dsa_bridge *bridge;
947c8746 430
d3eed0e5
VO
431 bridge = dsa_tree_bridge_find(ds->dst, br);
432 if (bridge) {
433 refcount_inc(&bridge->refcount);
434 dp->bridge = bridge;
947c8746 435 return 0;
d3eed0e5
VO
436 }
437
438 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
439 if (!bridge)
440 return -ENOMEM;
441
442 refcount_set(&bridge->refcount, 1);
443
444 bridge->dev = br;
947c8746 445
d3eed0e5
VO
446 bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges);
447 if (ds->max_num_bridges && !bridge->num) {
947c8746
VO
448 NL_SET_ERR_MSG_MOD(extack,
449 "Range of offloadable bridges exceeded");
d3eed0e5 450 kfree(bridge);
947c8746 451 return -EOPNOTSUPP;
123abc06
VO
452 }
453
d3eed0e5 454 dp->bridge = bridge;
947c8746
VO
455
456 return 0;
457}
458
459static void dsa_port_bridge_destroy(struct dsa_port *dp,
460 const struct net_device *br)
461{
d3eed0e5
VO
462 struct dsa_bridge *bridge = dp->bridge;
463
464 dp->bridge = NULL;
947c8746 465
d3eed0e5
VO
466 if (!refcount_dec_and_test(&bridge->refcount))
467 return;
947c8746 468
d3eed0e5
VO
469 if (bridge->num)
470 dsa_bridge_num_put(br, bridge->num);
947c8746 471
d3eed0e5 472 kfree(bridge);
123abc06
VO
473}
474
332afc4c
TW
475static bool dsa_port_supports_mst(struct dsa_port *dp)
476{
8e6598a7
TW
477 struct dsa_switch *ds = dp->ds;
478
479 return ds->ops->vlan_msti_set &&
7414af30
TW
480 ds->ops->port_mst_state_set &&
481 ds->ops->port_vlan_fast_age &&
8e6598a7 482 dsa_port_can_configure_learning(dp);
332afc4c
TW
483}
484
2afc526a
VO
485int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
486 struct netlink_ext_ack *extack)
cfbed329
VD
487{
488 struct dsa_notifier_bridge_info info = {
726816a1 489 .dp = dp,
06b9cce4 490 .extack = extack,
cfbed329 491 };
6ca80638 492 struct net_device *dev = dp->user;
2f5dc00f 493 struct net_device *brport_dev;
cfbed329
VD
494 int err;
495
332afc4c
TW
496 if (br_mst_enabled(br) && !dsa_port_supports_mst(dp))
497 return -EOPNOTSUPP;
498
c1388063
RK
499 /* Here the interface is already bridged. Reflect the current
500 * configuration so that drivers can program their chips accordingly.
cfbed329 501 */
947c8746
VO
502 err = dsa_port_bridge_create(dp, br, extack);
503 if (err)
504 return err;
cfbed329 505
2f5dc00f
VO
506 brport_dev = dsa_port_to_bridge_port(dp);
507
d3eed0e5 508 info.bridge = *dp->bridge;
f66a6a69 509 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
5961d6a1
VO
510 if (err)
511 goto out_rollback;
cfbed329 512
857fdd74
VO
513 /* Drivers which support bridge TX forwarding should set this */
514 dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
123abc06 515
4e51bf44 516 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
6ca80638
FF
517 &dsa_user_switchdev_notifier,
518 &dsa_user_switchdev_blocking_notifier,
857fdd74 519 dp->bridge->tx_fwd_offload, extack);
5961d6a1
VO
520 if (err)
521 goto out_rollback_unbridge;
522
4e51bf44 523 err = dsa_port_switchdev_sync_attrs(dp, extack);
2f5dc00f
VO
524 if (err)
525 goto out_rollback_unoffload;
526
5961d6a1 527 return 0;
cfbed329 528
2f5dc00f 529out_rollback_unoffload:
4e51bf44 530 switchdev_bridge_port_unoffload(brport_dev, dp,
6ca80638
FF
531 &dsa_user_switchdev_notifier,
532 &dsa_user_switchdev_blocking_notifier);
630fd482 533 dsa_flush_workqueue();
5961d6a1
VO
534out_rollback_unbridge:
535 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
536out_rollback:
947c8746 537 dsa_port_bridge_destroy(dp, br);
cfbed329
VD
538 return err;
539}
540
4e51bf44 541void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
74918945 542{
2f5dc00f
VO
543 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
544
09dba21b
VO
545 /* Don't try to unoffload something that is not offloaded */
546 if (!brport_dev)
547 return;
548
4e51bf44 549 switchdev_bridge_port_unoffload(brport_dev, dp,
6ca80638
FF
550 &dsa_user_switchdev_notifier,
551 &dsa_user_switchdev_blocking_notifier);
d7d0d423
VO
552
553 dsa_flush_workqueue();
74918945
VO
554}
555
cfbed329
VD
556void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
557{
558 struct dsa_notifier_bridge_info info = {
726816a1 559 .dp = dp,
cfbed329
VD
560 };
561 int err;
562
342b6419
AÅ 
563 /* If the port could not be offloaded to begin with, then
564 * there is nothing to do.
565 */
566 if (!dp->bridge)
567 return;
568
569 info.bridge = *dp->bridge;
570
cfbed329
VD
571 /* Here the port is already unbridged. Reflect the current configuration
572 * so that drivers can program their chips accordingly.
573 */
947c8746 574 dsa_port_bridge_destroy(dp, br);
cfbed329 575
f66a6a69 576 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
cfbed329 577 if (err)
ab97462b
VO
578 dev_err(dp->ds->dev,
579 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
580 dp->index, ERR_PTR(err));
cfbed329 581
8e9e678e 582 dsa_port_switchdev_unsync_attrs(dp, info.bridge);
cfbed329 583}
4d61d304 584
058102a6
TW
585int dsa_port_lag_change(struct dsa_port *dp,
586 struct netdev_lag_lower_state_info *linfo)
587{
588 struct dsa_notifier_lag_info info = {
726816a1 589 .dp = dp,
058102a6
TW
590 };
591 bool tx_enabled;
592
dedd6a00 593 if (!dp->lag)
058102a6
TW
594 return 0;
595
596 /* On statically configured aggregates (e.g. loadbalance
597 * without LACP) ports will always be tx_enabled, even if the
598 * link is down. Thus we require both link_up and tx_enabled
599 * in order to include it in the tx set.
600 */
601 tx_enabled = linfo->link_up && linfo->tx_enabled;
602
603 if (tx_enabled == dp->lag_tx_enabled)
604 return 0;
605
606 dp->lag_tx_enabled = tx_enabled;
607
608 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
609}
610
dedd6a00
VO
611static int dsa_port_lag_create(struct dsa_port *dp,
612 struct net_device *lag_dev)
613{
614 struct dsa_switch *ds = dp->ds;
615 struct dsa_lag *lag;
616
617 lag = dsa_tree_lag_find(ds->dst, lag_dev);
618 if (lag) {
619 refcount_inc(&lag->refcount);
620 dp->lag = lag;
621 return 0;
622 }
623
624 lag = kzalloc(sizeof(*lag), GFP_KERNEL);
625 if (!lag)
626 return -ENOMEM;
627
628 refcount_set(&lag->refcount, 1);
e212fa7c
VO
629 mutex_init(&lag->fdb_lock);
630 INIT_LIST_HEAD(&lag->fdbs);
dedd6a00
VO
631 lag->dev = lag_dev;
632 dsa_lag_map(ds->dst, lag);
633 dp->lag = lag;
634
635 return 0;
636}
637
638static void dsa_port_lag_destroy(struct dsa_port *dp)
639{
640 struct dsa_lag *lag = dp->lag;
641
642 dp->lag = NULL;
643 dp->lag_tx_enabled = false;
644
645 if (!refcount_dec_and_test(&lag->refcount))
646 return;
647
e212fa7c 648 WARN_ON(!list_empty(&lag->fdbs));
dedd6a00
VO
649 dsa_lag_unmap(dp->ds->dst, lag);
650 kfree(lag);
651}
652
46a76724 653int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
2afc526a
VO
654 struct netdev_lag_upper_info *uinfo,
655 struct netlink_ext_ack *extack)
058102a6
TW
656{
657 struct dsa_notifier_lag_info info = {
726816a1 658 .dp = dp,
058102a6 659 .info = uinfo,
2e359b00 660 .extack = extack,
058102a6 661 };
185c9a76 662 struct net_device *bridge_dev;
058102a6
TW
663 int err;
664
dedd6a00
VO
665 err = dsa_port_lag_create(dp, lag_dev);
666 if (err)
667 goto err_lag_create;
058102a6 668
dedd6a00 669 info.lag = *dp->lag;
058102a6 670 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
185c9a76
VO
671 if (err)
672 goto err_lag_join;
058102a6 673
46a76724 674 bridge_dev = netdev_master_upper_dev_get(lag_dev);
185c9a76
VO
675 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
676 return 0;
677
2afc526a 678 err = dsa_port_bridge_join(dp, bridge_dev, extack);
185c9a76
VO
679 if (err)
680 goto err_bridge_join;
681
682 return 0;
683
684err_bridge_join:
685 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
686err_lag_join:
dedd6a00
VO
687 dsa_port_lag_destroy(dp);
688err_lag_create:
058102a6
TW
689 return err;
690}
691
46a76724 692void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
74918945 693{
36cbf39b
VO
694 struct net_device *br = dsa_port_bridge_dev_get(dp);
695
696 if (br)
697 dsa_port_pre_bridge_leave(dp, br);
74918945
VO
698}
699
46a76724 700void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
058102a6 701{
36cbf39b 702 struct net_device *br = dsa_port_bridge_dev_get(dp);
058102a6 703 struct dsa_notifier_lag_info info = {
726816a1 704 .dp = dp,
058102a6
TW
705 };
706 int err;
707
dedd6a00 708 if (!dp->lag)
058102a6
TW
709 return;
710
711 /* Port might have been part of a LAG that in turn was
712 * attached to a bridge.
713 */
36cbf39b
VO
714 if (br)
715 dsa_port_bridge_leave(dp, br);
058102a6 716
dedd6a00
VO
717 info.lag = *dp->lag;
718
719 dsa_port_lag_destroy(dp);
058102a6
TW
720
721 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
722 if (err)
ab97462b
VO
723 dev_err(dp->ds->dev,
724 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
725 dp->index, ERR_PTR(err));
058102a6
TW
726}
727
adb256eb 728/* Must be called under rcu_read_lock() */
8f5d16f6 729static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
89153ed6
VO
730 bool vlan_filtering,
731 struct netlink_ext_ack *extack)
8f5d16f6
VO
732{
733 struct dsa_switch *ds = dp->ds;
d0004a02
VO
734 struct dsa_port *other_dp;
735 int err;
adb256eb
VO
736
737 /* VLAN awareness was off, so the question is "can we turn it on".
738 * We may have had 8021q uppers, those need to go. Make sure we don't
739 * enter an inconsistent state: deny changing the VLAN awareness state
740 * as long as we have 8021q uppers.
741 */
57d77986 742 if (vlan_filtering && dsa_port_is_user(dp)) {
36cbf39b 743 struct net_device *br = dsa_port_bridge_dev_get(dp);
6ca80638 744 struct net_device *upper_dev, *user = dp->user;
adb256eb
VO
745 struct list_head *iter;
746
6ca80638 747 netdev_for_each_upper_dev_rcu(user, upper_dev, iter) {
adb256eb
VO
748 struct bridge_vlan_info br_info;
749 u16 vid;
750
751 if (!is_vlan_dev(upper_dev))
752 continue;
753
754 vid = vlan_dev_vlan_id(upper_dev);
755
756 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
757 * device, respectively the VID is not found, returning
758 * 0 means success, which is a failure for us here.
759 */
760 err = br_vlan_get_info(br, vid, &br_info);
761 if (err == 0) {
89153ed6
VO
762 NL_SET_ERR_MSG_MOD(extack,
763 "Must first remove VLAN uppers having VIDs also present in bridge");
adb256eb
VO
764 return false;
765 }
766 }
767 }
8f5d16f6
VO
768
769 if (!ds->vlan_filtering_is_global)
770 return true;
771
772 /* For cases where enabling/disabling VLAN awareness is global to the
773 * switch, we need to handle the case where multiple bridges span
774 * different ports of the same switch device and one of them has a
775 * different setting than what is being requested.
776 */
d0004a02 777 dsa_switch_for_each_port(other_dp, ds) {
36cbf39b 778 struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
8f5d16f6 779
8f5d16f6
VO
780 /* If it's the same bridge, it also has same
781 * vlan_filtering setting => no need to check
782 */
36cbf39b 783 if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
8f5d16f6 784 continue;
36cbf39b
VO
785
786 if (br_vlan_enabled(other_br) != vlan_filtering) {
89153ed6
VO
787 NL_SET_ERR_MSG_MOD(extack,
788 "VLAN filtering is a global setting");
8f5d16f6
VO
789 return false;
790 }
791 }
792 return true;
793}
794
89153ed6
VO
795int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
796 struct netlink_ext_ack *extack)
4d61d304 797{
06cfb2df 798 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
4d61d304 799 struct dsa_switch *ds = dp->ds;
bae33f2b 800 bool apply;
33162e9a 801 int err;
4d61d304 802
bae33f2b
VO
803 if (!ds->ops->port_vlan_filtering)
804 return -EOPNOTSUPP;
adb256eb 805
6ca80638 806 /* We are called from dsa_user_switchdev_blocking_event(),
bae33f2b 807 * which is not under rcu_read_lock(), unlike
6ca80638 808 * dsa_user_switchdev_event().
bae33f2b
VO
809 */
810 rcu_read_lock();
89153ed6 811 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
bae33f2b
VO
812 rcu_read_unlock();
813 if (!apply)
814 return -EINVAL;
8f5d16f6 815
ec9121e7
VO
816 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
817 return 0;
818
89153ed6
VO
819 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
820 extack);
8f5d16f6
VO
821 if (err)
822 return err;
823
06cfb2df 824 if (ds->vlan_filtering_is_global) {
d0004a02 825 struct dsa_port *other_dp;
06cfb2df 826
bae33f2b 827 ds->vlan_filtering = vlan_filtering;
06cfb2df 828
d0004a02 829 dsa_switch_for_each_user_port(other_dp, ds) {
6ca80638 830 struct net_device *user = other_dp->user;
06cfb2df
VO
831
832 /* We might be called in the unbind path, so not
6ca80638 833 * all user devices might still be registered.
06cfb2df 834 */
6ca80638 835 if (!user)
06cfb2df
VO
836 continue;
837
6ca80638
FF
838 err = dsa_user_manage_vlan_filtering(user,
839 vlan_filtering);
06cfb2df
VO
840 if (err)
841 goto restore;
842 }
843 } else {
bae33f2b 844 dp->vlan_filtering = vlan_filtering;
2e554a7a 845
6ca80638
FF
846 err = dsa_user_manage_vlan_filtering(dp->user,
847 vlan_filtering);
06cfb2df
VO
848 if (err)
849 goto restore;
850 }
851
4d61d304 852 return 0;
06cfb2df
VO
853
854restore:
855 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
856
857 if (ds->vlan_filtering_is_global)
858 ds->vlan_filtering = old_vlan_filtering;
859 else
860 dp->vlan_filtering = old_vlan_filtering;
861
862 return err;
4d61d304 863}
d87bd94e 864
54a0ed0d 865/* This enforces legacy behavior for switch drivers which assume they can't
6ca80638 866 * receive VLAN configuration when joining a bridge with vlan_filtering=0
54a0ed0d
RK
867 */
868bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
869{
36cbf39b 870 struct net_device *br = dsa_port_bridge_dev_get(dp);
54a0ed0d
RK
871 struct dsa_switch *ds = dp->ds;
872
36cbf39b 873 if (!br)
54a0ed0d
RK
874 return false;
875
36cbf39b 876 return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
54a0ed0d
RK
877}
878
bae33f2b 879int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
d87bd94e
VD
880{
881 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
882 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
bae33f2b 883 struct dsa_notifier_ageing_time_info info;
bae33f2b
VO
884 int err;
885
886 info.ageing_time = ageing_time;
d87bd94e 887
bae33f2b
VO
888 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
889 if (err)
890 return err;
d87bd94e 891
d87bd94e 892 dp->ageing_time = ageing_time;
d87bd94e 893
77b61365 894 return 0;
d87bd94e 895}
d1cffff0 896
332afc4c
TW
897int dsa_port_mst_enable(struct dsa_port *dp, bool on,
898 struct netlink_ext_ack *extack)
899{
7414af30 900 if (on && !dsa_port_supports_mst(dp)) {
332afc4c
TW
901 NL_SET_ERR_MSG_MOD(extack, "Hardware does not support MST");
902 return -EINVAL;
903 }
904
905 return 0;
906}
907
e18f4c18 908int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
a8b659e7
VO
909 struct switchdev_brport_flags flags,
910 struct netlink_ext_ack *extack)
ea87005a
FF
911{
912 struct dsa_switch *ds = dp->ds;
913
a8b659e7 914 if (!ds->ops->port_pre_bridge_flags)
ea87005a
FF
915 return -EINVAL;
916
a8b659e7 917 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
ea87005a
FF
918}
919
045c45d1 920int dsa_port_bridge_flags(struct dsa_port *dp,
a8b659e7
VO
921 struct switchdev_brport_flags flags,
922 struct netlink_ext_ack *extack)
57652796
RK
923{
924 struct dsa_switch *ds = dp->ds;
045c45d1 925 int err;
57652796 926
a8b659e7 927 if (!ds->ops->port_bridge_flags)
70a7c484 928 return -EOPNOTSUPP;
57652796 929
045c45d1
VO
930 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
931 if (err)
932 return err;
933
934 if (flags.mask & BR_LEARNING) {
935 bool learning = flags.val & BR_LEARNING;
936
937 if (learning == dp->learning)
938 return 0;
939
bee7c577
VO
940 if ((dp->learning && !learning) &&
941 (dp->stp_state == BR_STATE_LEARNING ||
942 dp->stp_state == BR_STATE_FORWARDING))
045c45d1
VO
943 dsa_port_fast_age(dp);
944
945 dp->learning = learning;
946 }
947
948 return 0;
57652796
RK
949}
950
72c3b0c7
VO
951void dsa_port_set_host_flood(struct dsa_port *dp, bool uc, bool mc)
952{
953 struct dsa_switch *ds = dp->ds;
954
955 if (ds->ops->port_set_host_flood)
956 ds->ops->port_set_host_flood(ds, dp->index, uc, mc);
957}
958
8e6598a7
TW
959int dsa_port_vlan_msti(struct dsa_port *dp,
960 const struct switchdev_vlan_msti *msti)
961{
962 struct dsa_switch *ds = dp->ds;
963
964 if (!ds->ops->vlan_msti_set)
965 return -EOPNOTSUPP;
966
967 return ds->ops->vlan_msti_set(ds, *dp->bridge, msti);
968}
969
be6ff966 970int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu)
bfcb8132
VO
971{
972 struct dsa_notifier_mtu_info info = {
726816a1 973 .dp = dp,
bfcb8132
VO
974 .mtu = new_mtu,
975 };
976
977 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
978}
979
2acf4e6a
AS
980int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
981 u16 vid)
d1cffff0 982{
685fb6a4 983 struct dsa_notifier_fdb_info info = {
726816a1 984 .dp = dp,
2acf4e6a
AS
985 .addr = addr,
986 .vid = vid,
c2693363
VO
987 .db = {
988 .type = DSA_DB_BRIDGE,
989 .bridge = *dp->bridge,
990 },
685fb6a4 991 };
d1cffff0 992
c2693363
VO
993 /* Refcounting takes bridge.num as a key, and should be global for all
994 * bridges in the absence of FDB isolation, and per bridge otherwise.
995 * Force the bridge.num to zero here in the absence of FDB isolation.
996 */
997 if (!dp->ds->fdb_isolation)
998 info.db.bridge.num = 0;
999
685fb6a4 1000 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
d1cffff0
VD
1001}
1002
2acf4e6a
AS
1003int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
1004 u16 vid)
d1cffff0 1005{
685fb6a4 1006 struct dsa_notifier_fdb_info info = {
726816a1 1007 .dp = dp,
2acf4e6a
AS
1008 .addr = addr,
1009 .vid = vid,
c2693363
VO
1010 .db = {
1011 .type = DSA_DB_BRIDGE,
1012 .bridge = *dp->bridge,
1013 },
685fb6a4 1014 };
d1cffff0 1015
c2693363
VO
1016 if (!dp->ds->fdb_isolation)
1017 info.db.bridge.num = 0;
1018
685fb6a4 1019 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
d1cffff0
VD
1020}
1021
5e8a1e03
VO
1022static int dsa_port_host_fdb_add(struct dsa_port *dp,
1023 const unsigned char *addr, u16 vid,
1024 struct dsa_db db)
3dc80afc
VO
1025{
1026 struct dsa_notifier_fdb_info info = {
726816a1 1027 .dp = dp,
3dc80afc
VO
1028 .addr = addr,
1029 .vid = vid,
5e8a1e03
VO
1030 .db = db,
1031 };
1032
5e8a1e03
VO
1033 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
1034}
1035
1036int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
1037 const unsigned char *addr, u16 vid)
1038{
1039 struct dsa_db db = {
1040 .type = DSA_DB_PORT,
1041 .dp = dp,
3dc80afc 1042 };
5e8a1e03
VO
1043
1044 return dsa_port_host_fdb_add(dp, addr, vid, db);
1045}
1046
1047int dsa_port_bridge_host_fdb_add(struct dsa_port *dp,
1048 const unsigned char *addr, u16 vid)
1049{
6ca80638 1050 struct net_device *conduit = dsa_port_to_conduit(dp);
5e8a1e03
VO
1051 struct dsa_db db = {
1052 .type = DSA_DB_BRIDGE,
1053 .bridge = *dp->bridge,
1054 };
26ee7b06
VO
1055 int err;
1056
eb1ab765
VO
1057 if (!dp->ds->fdb_isolation)
1058 db.bridge.num = 0;
1059
6ca80638 1060 /* Avoid a call to __dev_set_promiscuity() on the conduit, which
8940e6b6
VO
1061 * requires rtnl_lock(), since we can't guarantee that is held here,
1062 * and we can't take it either.
1063 */
6ca80638
FF
1064 if (conduit->priv_flags & IFF_UNICAST_FLT) {
1065 err = dev_uc_add(conduit, addr);
8940e6b6
VO
1066 if (err)
1067 return err;
1068 }
3dc80afc 1069
5e8a1e03 1070 return dsa_port_host_fdb_add(dp, addr, vid, db);
3dc80afc
VO
1071}
1072
5e8a1e03
VO
1073static int dsa_port_host_fdb_del(struct dsa_port *dp,
1074 const unsigned char *addr, u16 vid,
1075 struct dsa_db db)
3dc80afc
VO
1076{
1077 struct dsa_notifier_fdb_info info = {
726816a1 1078 .dp = dp,
3dc80afc
VO
1079 .addr = addr,
1080 .vid = vid,
5e8a1e03
VO
1081 .db = db,
1082 };
1083
5e8a1e03
VO
1084 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
1085}
1086
1087int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
1088 const unsigned char *addr, u16 vid)
1089{
1090 struct dsa_db db = {
1091 .type = DSA_DB_PORT,
1092 .dp = dp,
3dc80afc 1093 };
5e8a1e03
VO
1094
1095 return dsa_port_host_fdb_del(dp, addr, vid, db);
1096}
1097
1098int dsa_port_bridge_host_fdb_del(struct dsa_port *dp,
1099 const unsigned char *addr, u16 vid)
1100{
6ca80638 1101 struct net_device *conduit = dsa_port_to_conduit(dp);
5e8a1e03
VO
1102 struct dsa_db db = {
1103 .type = DSA_DB_BRIDGE,
1104 .bridge = *dp->bridge,
1105 };
26ee7b06
VO
1106 int err;
1107
eb1ab765
VO
1108 if (!dp->ds->fdb_isolation)
1109 db.bridge.num = 0;
1110
6ca80638
FF
1111 if (conduit->priv_flags & IFF_UNICAST_FLT) {
1112 err = dev_uc_del(conduit, addr);
8940e6b6
VO
1113 if (err)
1114 return err;
1115 }
3dc80afc 1116
5e8a1e03 1117 return dsa_port_host_fdb_del(dp, addr, vid, db);
3dc80afc
VO
1118}
1119
e212fa7c
VO
1120int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
1121 u16 vid)
1122{
1123 struct dsa_notifier_lag_fdb_info info = {
1124 .lag = dp->lag,
1125 .addr = addr,
1126 .vid = vid,
c2693363
VO
1127 .db = {
1128 .type = DSA_DB_BRIDGE,
1129 .bridge = *dp->bridge,
1130 },
e212fa7c
VO
1131 };
1132
c2693363
VO
1133 if (!dp->ds->fdb_isolation)
1134 info.db.bridge.num = 0;
1135
e212fa7c
VO
1136 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
1137}
1138
1139int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
1140 u16 vid)
1141{
1142 struct dsa_notifier_lag_fdb_info info = {
1143 .lag = dp->lag,
1144 .addr = addr,
1145 .vid = vid,
c2693363
VO
1146 .db = {
1147 .type = DSA_DB_BRIDGE,
1148 .bridge = *dp->bridge,
1149 },
e212fa7c
VO
1150 };
1151
c2693363
VO
1152 if (!dp->ds->fdb_isolation)
1153 info.db.bridge.num = 0;
1154
e212fa7c
VO
1155 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_DEL, &info);
1156}
1157
de40fc5d
VD
1158int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
1159{
1160 struct dsa_switch *ds = dp->ds;
1161 int port = dp->index;
1162
1163 if (!ds->ops->port_fdb_dump)
1164 return -EOPNOTSUPP;
1165
1166 return ds->ops->port_fdb_dump(ds, port, cb, data);
1167}
1168
bb9f6031 1169int dsa_port_mdb_add(const struct dsa_port *dp,
ffb68fc5 1170 const struct switchdev_obj_port_mdb *mdb)
3a9afea3 1171{
8ae5bcdc 1172 struct dsa_notifier_mdb_info info = {
726816a1 1173 .dp = dp,
8ae5bcdc 1174 .mdb = mdb,
c2693363
VO
1175 .db = {
1176 .type = DSA_DB_BRIDGE,
1177 .bridge = *dp->bridge,
1178 },
8ae5bcdc 1179 };
3a9afea3 1180
c2693363
VO
1181 if (!dp->ds->fdb_isolation)
1182 info.db.bridge.num = 0;
1183
8ae5bcdc 1184 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
3a9afea3
VD
1185}
1186
bb9f6031 1187int dsa_port_mdb_del(const struct dsa_port *dp,
3a9afea3
VD
1188 const struct switchdev_obj_port_mdb *mdb)
1189{
8ae5bcdc 1190 struct dsa_notifier_mdb_info info = {
726816a1 1191 .dp = dp,
8ae5bcdc 1192 .mdb = mdb,
c2693363
VO
1193 .db = {
1194 .type = DSA_DB_BRIDGE,
1195 .bridge = *dp->bridge,
1196 },
8ae5bcdc 1197 };
3a9afea3 1198
c2693363
VO
1199 if (!dp->ds->fdb_isolation)
1200 info.db.bridge.num = 0;
1201
8ae5bcdc 1202 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
3a9afea3
VD
1203}
1204
5e8a1e03
VO
1205static int dsa_port_host_mdb_add(const struct dsa_port *dp,
1206 const struct switchdev_obj_port_mdb *mdb,
1207 struct dsa_db db)
b8e997c4
VO
1208{
1209 struct dsa_notifier_mdb_info info = {
726816a1 1210 .dp = dp,
b8e997c4 1211 .mdb = mdb,
5e8a1e03
VO
1212 .db = db,
1213 };
1214
5e8a1e03
VO
1215 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
1216}
1217
1218int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
1219 const struct switchdev_obj_port_mdb *mdb)
1220{
1221 struct dsa_db db = {
1222 .type = DSA_DB_PORT,
1223 .dp = dp,
b8e997c4 1224 };
5e8a1e03
VO
1225
1226 return dsa_port_host_mdb_add(dp, mdb, db);
1227}
1228
1229int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
1230 const struct switchdev_obj_port_mdb *mdb)
1231{
6ca80638 1232 struct net_device *conduit = dsa_port_to_conduit(dp);
5e8a1e03
VO
1233 struct dsa_db db = {
1234 .type = DSA_DB_BRIDGE,
1235 .bridge = *dp->bridge,
1236 };
26ee7b06
VO
1237 int err;
1238
eb1ab765
VO
1239 if (!dp->ds->fdb_isolation)
1240 db.bridge.num = 0;
1241
6ca80638 1242 err = dev_mc_add(conduit, mdb->addr);
26ee7b06
VO
1243 if (err)
1244 return err;
b8e997c4 1245
5e8a1e03 1246 return dsa_port_host_mdb_add(dp, mdb, db);
b8e997c4
VO
1247}
1248
5e8a1e03
VO
1249static int dsa_port_host_mdb_del(const struct dsa_port *dp,
1250 const struct switchdev_obj_port_mdb *mdb,
1251 struct dsa_db db)
b8e997c4
VO
1252{
1253 struct dsa_notifier_mdb_info info = {
726816a1 1254 .dp = dp,
b8e997c4 1255 .mdb = mdb,
5e8a1e03
VO
1256 .db = db,
1257 };
1258
5e8a1e03
VO
1259 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
1260}
1261
1262int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
1263 const struct switchdev_obj_port_mdb *mdb)
1264{
1265 struct dsa_db db = {
1266 .type = DSA_DB_PORT,
1267 .dp = dp,
b8e997c4 1268 };
5e8a1e03
VO
1269
1270 return dsa_port_host_mdb_del(dp, mdb, db);
1271}
1272
1273int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
1274 const struct switchdev_obj_port_mdb *mdb)
1275{
6ca80638 1276 struct net_device *conduit = dsa_port_to_conduit(dp);
5e8a1e03
VO
1277 struct dsa_db db = {
1278 .type = DSA_DB_BRIDGE,
1279 .bridge = *dp->bridge,
1280 };
26ee7b06
VO
1281 int err;
1282
eb1ab765
VO
1283 if (!dp->ds->fdb_isolation)
1284 db.bridge.num = 0;
1285
6ca80638 1286 err = dev_mc_del(conduit, mdb->addr);
26ee7b06
VO
1287 if (err)
1288 return err;
b8e997c4 1289
5e8a1e03 1290 return dsa_port_host_mdb_del(dp, mdb, db);
b8e997c4
VO
1291}
1292
076e7133 1293int dsa_port_vlan_add(struct dsa_port *dp,
31046a5f
VO
1294 const struct switchdev_obj_port_vlan *vlan,
1295 struct netlink_ext_ack *extack)
076e7133 1296{
d0c627b8 1297 struct dsa_notifier_vlan_info info = {
726816a1 1298 .dp = dp,
d0c627b8 1299 .vlan = vlan,
31046a5f 1300 .extack = extack,
d0c627b8 1301 };
076e7133 1302
c5335d73 1303 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
076e7133
VD
1304}
1305
1306int dsa_port_vlan_del(struct dsa_port *dp,
1307 const struct switchdev_obj_port_vlan *vlan)
1308{
d0c627b8 1309 struct dsa_notifier_vlan_info info = {
726816a1 1310 .dp = dp,
d0c627b8
VD
1311 .vlan = vlan,
1312 };
076e7133 1313
c5335d73 1314 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
076e7133 1315}
57ab1ca2 1316
134ef238
VO
1317int dsa_port_host_vlan_add(struct dsa_port *dp,
1318 const struct switchdev_obj_port_vlan *vlan,
1319 struct netlink_ext_ack *extack)
1320{
6ca80638 1321 struct net_device *conduit = dsa_port_to_conduit(dp);
134ef238 1322 struct dsa_notifier_vlan_info info = {
726816a1 1323 .dp = dp,
134ef238
VO
1324 .vlan = vlan,
1325 .extack = extack,
1326 };
134ef238
VO
1327 int err;
1328
1329 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
1330 if (err && err != -EOPNOTSUPP)
1331 return err;
1332
6ca80638 1333 vlan_vid_add(conduit, htons(ETH_P_8021Q), vlan->vid);
134ef238
VO
1334
1335 return err;
1336}
1337
1338int dsa_port_host_vlan_del(struct dsa_port *dp,
1339 const struct switchdev_obj_port_vlan *vlan)
1340{
6ca80638 1341 struct net_device *conduit = dsa_port_to_conduit(dp);
134ef238 1342 struct dsa_notifier_vlan_info info = {
726816a1 1343 .dp = dp,
134ef238
VO
1344 .vlan = vlan,
1345 };
134ef238
VO
1346 int err;
1347
1348 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
1349 if (err && err != -EOPNOTSUPP)
1350 return err;
1351
6ca80638 1352 vlan_vid_del(conduit, htons(ETH_P_8021Q), vlan->vid);
134ef238
VO
1353
1354 return err;
1355}
1356
c595c433
HV
1357int dsa_port_mrp_add(const struct dsa_port *dp,
1358 const struct switchdev_obj_mrp *mrp)
1359{
cad69019
VO
1360 struct dsa_switch *ds = dp->ds;
1361
1362 if (!ds->ops->port_mrp_add)
1363 return -EOPNOTSUPP;
c595c433 1364
cad69019 1365 return ds->ops->port_mrp_add(ds, dp->index, mrp);
c595c433
HV
1366}
1367
1368int dsa_port_mrp_del(const struct dsa_port *dp,
1369 const struct switchdev_obj_mrp *mrp)
1370{
cad69019
VO
1371 struct dsa_switch *ds = dp->ds;
1372
1373 if (!ds->ops->port_mrp_del)
1374 return -EOPNOTSUPP;
c595c433 1375
cad69019 1376 return ds->ops->port_mrp_del(ds, dp->index, mrp);
c595c433
HV
1377}
1378
1379int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
1380 const struct switchdev_obj_ring_role_mrp *mrp)
1381{
cad69019
VO
1382 struct dsa_switch *ds = dp->ds;
1383
1384 if (!ds->ops->port_mrp_add_ring_role)
1385 return -EOPNOTSUPP;
c595c433 1386
cad69019 1387 return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
c595c433
HV
1388}
1389
1390int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
1391 const struct switchdev_obj_ring_role_mrp *mrp)
1392{
cad69019
VO
1393 struct dsa_switch *ds = dp->ds;
1394
1395 if (!ds->ops->port_mrp_del_ring_role)
1396 return -EOPNOTSUPP;
c595c433 1397
cad69019 1398 return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
c595c433
HV
1399}
1400
6ca80638
FF
1401static int dsa_port_assign_conduit(struct dsa_port *dp,
1402 struct net_device *conduit,
1403 struct netlink_ext_ack *extack,
1404 bool fail_on_err)
95f510d0
VO
1405{
1406 struct dsa_switch *ds = dp->ds;
1407 int port = dp->index, err;
1408
6ca80638 1409 err = ds->ops->port_change_conduit(ds, port, conduit, extack);
95f510d0 1410 if (err && !fail_on_err)
6ca80638
FF
1411 dev_err(ds->dev, "port %d failed to assign conduit %s: %pe\n",
1412 port, conduit->name, ERR_PTR(err));
95f510d0
VO
1413
1414 if (err && fail_on_err)
1415 return err;
1416
6ca80638
FF
1417 dp->cpu_dp = conduit->dsa_ptr;
1418 dp->cpu_port_in_lag = netif_is_lag_master(conduit);
95f510d0
VO
1419
1420 return 0;
1421}
1422
1423/* Change the dp->cpu_dp affinity for a user port. Note that both cross-chip
1424 * notifiers and drivers have implicit assumptions about user-to-CPU-port
1425 * mappings, so we unfortunately cannot delay the deletion of the objects
1426 * (switchdev, standalone addresses, standalone VLANs) on the old CPU port
1427 * until the new CPU port has been set up. So we need to completely tear down
1428 * the old CPU port before changing it, and restore it on errors during the
1429 * bringup of the new one.
1430 */
6ca80638
FF
1431int dsa_port_change_conduit(struct dsa_port *dp, struct net_device *conduit,
1432 struct netlink_ext_ack *extack)
95f510d0
VO
1433{
1434 struct net_device *bridge_dev = dsa_port_bridge_dev_get(dp);
6ca80638
FF
1435 struct net_device *old_conduit = dsa_port_to_conduit(dp);
1436 struct net_device *dev = dp->user;
95f510d0
VO
1437 struct dsa_switch *ds = dp->ds;
1438 bool vlan_filtering;
1439 int err, tmp;
1440
1441 /* Bridges may hold host FDB, MDB and VLAN objects. These need to be
1442 * migrated, so dynamically unoffload and later reoffload the bridge
1443 * port.
1444 */
1445 if (bridge_dev) {
1446 dsa_port_pre_bridge_leave(dp, bridge_dev);
1447 dsa_port_bridge_leave(dp, bridge_dev);
1448 }
1449
1450 /* The port might still be VLAN filtering even if it's no longer
1451 * under a bridge, either due to ds->vlan_filtering_is_global or
1452 * ds->needs_standalone_vlan_filtering. In turn this means VLANs
1453 * on the CPU port.
1454 */
1455 vlan_filtering = dsa_port_is_vlan_filtering(dp);
1456 if (vlan_filtering) {
6ca80638 1457 err = dsa_user_manage_vlan_filtering(dev, false);
95f510d0
VO
1458 if (err) {
1459 NL_SET_ERR_MSG_MOD(extack,
1460 "Failed to remove standalone VLANs");
1461 goto rewind_old_bridge;
1462 }
1463 }
1464
1465 /* Standalone addresses, and addresses of upper interfaces like
1466 * VLAN, LAG, HSR need to be migrated.
1467 */
6ca80638 1468 dsa_user_unsync_ha(dev);
95f510d0 1469
eef8e906
MB
1470 /* If live-changing, we also need to uninstall the user device address
1471 * from the port FDB and the conduit interface.
1472 */
1473 if (dev->flags & IFF_UP)
1474 dsa_user_host_uc_uninstall(dev);
1475
6ca80638 1476 err = dsa_port_assign_conduit(dp, conduit, extack, true);
95f510d0
VO
1477 if (err)
1478 goto rewind_old_addrs;
1479
eef8e906
MB
1480 /* If the port doesn't have its own MAC address and relies on the DSA
1481 * conduit's one, inherit it again from the new DSA conduit.
1482 */
1483 if (is_zero_ether_addr(dp->mac))
1484 eth_hw_addr_inherit(dev, conduit);
1485
1486 /* If live-changing, we need to install the user device address to the
1487 * port FDB and the conduit interface.
1488 */
1489 if (dev->flags & IFF_UP) {
1490 err = dsa_user_host_uc_install(dev, dev->dev_addr);
1491 if (err) {
1492 NL_SET_ERR_MSG_MOD(extack,
1493 "Failed to install host UC address");
1494 goto rewind_addr_inherit;
1495 }
1496 }
1497
6ca80638 1498 dsa_user_sync_ha(dev);
95f510d0
VO
1499
1500 if (vlan_filtering) {
6ca80638 1501 err = dsa_user_manage_vlan_filtering(dev, true);
95f510d0
VO
1502 if (err) {
1503 NL_SET_ERR_MSG_MOD(extack,
1504 "Failed to restore standalone VLANs");
1505 goto rewind_new_addrs;
1506 }
1507 }
1508
1509 if (bridge_dev) {
1510 err = dsa_port_bridge_join(dp, bridge_dev, extack);
1511 if (err && err == -EOPNOTSUPP) {
1512 NL_SET_ERR_MSG_MOD(extack,
1513 "Failed to reoffload bridge");
1514 goto rewind_new_vlan;
1515 }
1516 }
1517
1518 return 0;
1519
1520rewind_new_vlan:
1521 if (vlan_filtering)
6ca80638 1522 dsa_user_manage_vlan_filtering(dev, false);
95f510d0
VO
1523
1524rewind_new_addrs:
6ca80638 1525 dsa_user_unsync_ha(dev);
95f510d0 1526
eef8e906
MB
1527 if (dev->flags & IFF_UP)
1528 dsa_user_host_uc_uninstall(dev);
1529
1530rewind_addr_inherit:
1531 if (is_zero_ether_addr(dp->mac))
1532 eth_hw_addr_inherit(dev, old_conduit);
1533
6ca80638 1534 dsa_port_assign_conduit(dp, old_conduit, NULL, false);
95f510d0
VO
1535
1536/* Restore the objects on the old CPU port */
1537rewind_old_addrs:
eef8e906
MB
1538 if (dev->flags & IFF_UP) {
1539 tmp = dsa_user_host_uc_install(dev, dev->dev_addr);
1540 if (tmp) {
1541 dev_err(ds->dev,
1542 "port %d failed to restore host UC address: %pe\n",
1543 dp->index, ERR_PTR(tmp));
1544 }
1545 }
1546
6ca80638 1547 dsa_user_sync_ha(dev);
95f510d0
VO
1548
1549 if (vlan_filtering) {
6ca80638 1550 tmp = dsa_user_manage_vlan_filtering(dev, true);
95f510d0
VO
1551 if (tmp) {
1552 dev_err(ds->dev,
1553 "port %d failed to restore standalone VLANs: %pe\n",
1554 dp->index, ERR_PTR(tmp));
1555 }
1556 }
1557
1558rewind_old_bridge:
1559 if (bridge_dev) {
1560 tmp = dsa_port_bridge_join(dp, bridge_dev, extack);
1561 if (tmp) {
1562 dev_err(ds->dev,
1563 "port %d failed to rejoin bridge %s: %pe\n",
1564 dp->index, bridge_dev->name, ERR_PTR(tmp));
1565 }
1566 }
1567
1568 return err;
1569}
1570
53da0eba
VO
1571void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
1572 const struct dsa_device_ops *tag_ops)
1573{
53da0eba
VO
1574 cpu_dp->rcv = tag_ops->rcv;
1575 cpu_dp->tag_ops = tag_ops;
1576}
1577
bde01822
RKO
1578static struct phylink_pcs *
1579dsa_port_phylink_mac_select_pcs(struct phylink_config *config,
1580 phy_interface_t interface)
1581{
dd0c9855 1582 struct dsa_port *dp = dsa_phylink_to_port(config);
10544570 1583 struct phylink_pcs *pcs = ERR_PTR(-EOPNOTSUPP);
bde01822 1584 struct dsa_switch *ds = dp->ds;
bde01822
RKO
1585
1586 if (ds->ops->phylink_mac_select_pcs)
1587 pcs = ds->ops->phylink_mac_select_pcs(ds, dp->index, interface);
1588
1589 return pcs;
1590}
1591
8ae67496
FF
1592static void dsa_port_phylink_mac_config(struct phylink_config *config,
1593 unsigned int mode,
1594 const struct phylink_link_state *state)
77373d49 1595{
dd0c9855 1596 struct dsa_port *dp = dsa_phylink_to_port(config);
77373d49
IC
1597 struct dsa_switch *ds = dp->ds;
1598
1599 if (!ds->ops->phylink_mac_config)
1600 return;
1601
1602 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1603}
77373d49 1604
8ae67496
FF
1605static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1606 unsigned int mode,
1607 phy_interface_t interface)
77373d49 1608{
dd0c9855 1609 struct dsa_port *dp = dsa_phylink_to_port(config);
77373d49
IC
1610 struct dsa_switch *ds = dp->ds;
1611
8a021a86 1612 if (!ds->ops->phylink_mac_link_down)
77373d49 1613 return;
77373d49
IC
1614
1615 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1616}
77373d49 1617
8ae67496 1618static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
91a208f2 1619 struct phy_device *phydev,
8ae67496
FF
1620 unsigned int mode,
1621 phy_interface_t interface,
91a208f2
RK
1622 int speed, int duplex,
1623 bool tx_pause, bool rx_pause)
77373d49 1624{
dd0c9855 1625 struct dsa_port *dp = dsa_phylink_to_port(config);
77373d49
IC
1626 struct dsa_switch *ds = dp->ds;
1627
8a021a86 1628 if (!ds->ops->phylink_mac_link_up)
77373d49 1629 return;
77373d49 1630
5b502a7b
RK
1631 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1632 speed, duplex, tx_pause, rx_pause);
77373d49 1633}
77373d49 1634
21bd64bd 1635static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
bde01822 1636 .mac_select_pcs = dsa_port_phylink_mac_select_pcs,
77373d49 1637 .mac_config = dsa_port_phylink_mac_config,
77373d49
IC
1638 .mac_link_down = dsa_port_phylink_mac_link_down,
1639 .mac_link_up = dsa_port_phylink_mac_link_up,
1640};
1641
21bd64bd
RKO
1642int dsa_port_phylink_create(struct dsa_port *dp)
1643{
cae425cb 1644 const struct phylink_mac_ops *mac_ops;
21bd64bd
RKO
1645 struct dsa_switch *ds = dp->ds;
1646 phy_interface_t mode;
cf5ca4dd 1647 struct phylink *pl;
21bd64bd
RKO
1648 int err;
1649
1650 err = of_get_phy_mode(dp->dn, &mode);
1651 if (err)
1652 mode = PHY_INTERFACE_MODE_NA;
1653
9945c1fb 1654 if (ds->ops->phylink_get_caps) {
072eea6c 1655 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
9945c1fb
RKO
1656 } else {
1657 /* For legacy drivers */
14562277
RKO
1658 if (mode != PHY_INTERFACE_MODE_NA) {
1659 __set_bit(mode, dp->pl_config.supported_interfaces);
1660 } else {
1661 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1662 dp->pl_config.supported_interfaces);
1663 __set_bit(PHY_INTERFACE_MODE_GMII,
1664 dp->pl_config.supported_interfaces);
1665 }
9945c1fb 1666 }
21bd64bd 1667
cae425cb
RKO
1668 mac_ops = &dsa_port_phylink_mac_ops;
1669 if (ds->phylink_mac_ops)
1670 mac_ops = ds->phylink_mac_ops;
1671
1672 pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn), mode,
1673 mac_ops);
cf5ca4dd 1674 if (IS_ERR(pl)) {
557f0501 1675 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(pl));
cf5ca4dd 1676 return PTR_ERR(pl);
21bd64bd
RKO
1677 }
1678
cf5ca4dd
VO
1679 dp->pl = pl;
1680
21bd64bd
RKO
1681 return 0;
1682}
1683
cf5ca4dd
VO
1684void dsa_port_phylink_destroy(struct dsa_port *dp)
1685{
1686 phylink_destroy(dp->pl);
1687 dp->pl = NULL;
1688}
1689
770375ff 1690static int dsa_shared_port_phylink_register(struct dsa_port *dp)
0e279218
IC
1691{
1692 struct dsa_switch *ds = dp->ds;
1693 struct device_node *port_dn = dp->dn;
0c65b2b9 1694 int err;
0e279218 1695
0e279218
IC
1696 dp->pl_config.dev = ds->dev;
1697 dp->pl_config.type = PHYLINK_DEV;
1698
21bd64bd
RKO
1699 err = dsa_port_phylink_create(dp);
1700 if (err)
1701 return err;
0e279218
IC
1702
1703 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
2131fba5 1704 if (err && err != -ENODEV) {
0e279218
IC
1705 pr_err("could not attach to PHY: %d\n", err);
1706 goto err_phy_connect;
1707 }
1708
0e279218
IC
1709 return 0;
1710
1711err_phy_connect:
cf5ca4dd 1712 dsa_port_phylink_destroy(dp);
0e279218
IC
1713 return err;
1714}
1715
e09e9873
VO
1716/* During the initial DSA driver migration to OF, port nodes were sometimes
1717 * added to device trees with no indication of how they should operate from a
1718 * link management perspective (phy-handle, fixed-link, etc). Additionally, the
1719 * phy-mode may be absent. The interpretation of these port OF nodes depends on
1720 * their type.
1721 *
1722 * User ports with no phy-handle or fixed-link are expected to connect to an
6ca80638 1723 * internal PHY located on the ds->user_mii_bus at an MDIO address equal to
e09e9873
VO
1724 * the port number. This description is still actively supported.
1725 *
1726 * Shared (CPU and DSA) ports with no phy-handle or fixed-link are expected to
1727 * operate at the maximum speed that their phy-mode is capable of. If the
1728 * phy-mode is absent, they are expected to operate using the phy-mode
1729 * supported by the port that gives the highest link speed. It is unspecified
1730 * if the port should use flow control or not, half duplex or full duplex, or
1731 * if the phy-mode is a SERDES link, whether in-band autoneg is expected to be
1732 * enabled or not.
1733 *
1734 * In the latter case of shared ports, omitting the link management description
1735 * from the firmware node is deprecated and strongly discouraged. DSA uses
1736 * phylink, which rejects the firmware nodes of these ports for lacking
1737 * required properties.
1738 *
1739 * For switches in this table, DSA will skip enforcing validation and will
1740 * later omit registering a phylink instance for the shared ports, if they lack
1741 * a fixed-link, a phy-handle, or a managed = "in-band-status" property.
1742 * It becomes the responsibility of the driver to ensure that these ports
1743 * operate at the maximum speed (whatever this means) and will interoperate
6ca80638 1744 * with the DSA conduit or other cascade port, since phylink methods will not be
e09e9873
VO
1745 * invoked for them.
1746 *
1747 * If you are considering expanding this table for newly introduced switches,
1748 * think again. It is OK to remove switches from this table if there aren't DT
1749 * blobs in circulation which rely on defaulting the shared ports.
1750 */
1751static const char * const dsa_switches_apply_workarounds[] = {
1752#if IS_ENABLED(CONFIG_NET_DSA_XRS700X)
1753 "arrow,xrs7003e",
1754 "arrow,xrs7003f",
1755 "arrow,xrs7004e",
1756 "arrow,xrs7004f",
1757#endif
1758#if IS_ENABLED(CONFIG_B53)
1759 "brcm,bcm5325",
1760 "brcm,bcm53115",
1761 "brcm,bcm53125",
1762 "brcm,bcm53128",
1763 "brcm,bcm5365",
1764 "brcm,bcm5389",
1765 "brcm,bcm5395",
1766 "brcm,bcm5397",
1767 "brcm,bcm5398",
1768 "brcm,bcm53010-srab",
1769 "brcm,bcm53011-srab",
1770 "brcm,bcm53012-srab",
1771 "brcm,bcm53018-srab",
1772 "brcm,bcm53019-srab",
1773 "brcm,bcm5301x-srab",
1774 "brcm,bcm11360-srab",
1775 "brcm,bcm58522-srab",
1776 "brcm,bcm58525-srab",
1777 "brcm,bcm58535-srab",
1778 "brcm,bcm58622-srab",
1779 "brcm,bcm58623-srab",
1780 "brcm,bcm58625-srab",
1781 "brcm,bcm88312-srab",
1782 "brcm,cygnus-srab",
1783 "brcm,nsp-srab",
1784 "brcm,omega-srab",
1785 "brcm,bcm3384-switch",
1786 "brcm,bcm6328-switch",
1787 "brcm,bcm6368-switch",
1788 "brcm,bcm63xx-switch",
1789#endif
1790#if IS_ENABLED(CONFIG_NET_DSA_BCM_SF2)
1791 "brcm,bcm7445-switch-v4.0",
1792 "brcm,bcm7278-switch-v4.0",
1793 "brcm,bcm7278-switch-v4.8",
1794#endif
1795#if IS_ENABLED(CONFIG_NET_DSA_LANTIQ_GSWIP)
1796 "lantiq,xrx200-gswip",
1797 "lantiq,xrx300-gswip",
1798 "lantiq,xrx330-gswip",
1799#endif
1800#if IS_ENABLED(CONFIG_NET_DSA_MV88E6060)
1801 "marvell,mv88e6060",
1802#endif
1803#if IS_ENABLED(CONFIG_NET_DSA_MV88E6XXX)
1804 "marvell,mv88e6085",
1805 "marvell,mv88e6190",
1806 "marvell,mv88e6250",
1807#endif
1808#if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON)
1809 "microchip,ksz8765",
1810 "microchip,ksz8794",
1811 "microchip,ksz8795",
1812 "microchip,ksz8863",
1813 "microchip,ksz8873",
1814 "microchip,ksz9477",
1815 "microchip,ksz9897",
1816 "microchip,ksz9893",
1817 "microchip,ksz9563",
1818 "microchip,ksz8563",
1819 "microchip,ksz9567",
1820#endif
1821#if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_MDIO)
1822 "smsc,lan9303-mdio",
1823#endif
1824#if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_I2C)
1825 "smsc,lan9303-i2c",
1826#endif
1827 NULL,
1828};
1829
1830static void dsa_shared_port_validate_of(struct dsa_port *dp,
1831 bool *missing_phy_mode,
1832 bool *missing_link_description)
1833{
1834 struct device_node *dn = dp->dn, *phy_np;
1835 struct dsa_switch *ds = dp->ds;
1836 phy_interface_t mode;
1837
1838 *missing_phy_mode = false;
1839 *missing_link_description = false;
1840
1841 if (of_get_phy_mode(dn, &mode)) {
1842 *missing_phy_mode = true;
1843 dev_err(ds->dev,
1844 "OF node %pOF of %s port %d lacks the required \"phy-mode\" property\n",
1845 dn, dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1846 }
1847
1848 /* Note: of_phy_is_fixed_link() also returns true for
1849 * managed = "in-band-status"
1850 */
1851 if (of_phy_is_fixed_link(dn))
1852 return;
1853
1854 phy_np = of_parse_phandle(dn, "phy-handle", 0);
1855 if (phy_np) {
1856 of_node_put(phy_np);
1857 return;
1858 }
1859
1860 *missing_link_description = true;
1861
1862 dev_err(ds->dev,
1863 "OF node %pOF of %s port %d lacks the required \"phy-handle\", \"fixed-link\" or \"managed\" properties\n",
1864 dn, dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1865}
1866
cae425cb
RKO
1867static void dsa_shared_port_link_down(struct dsa_port *dp)
1868{
1869 struct dsa_switch *ds = dp->ds;
1870
1871 if (ds->phylink_mac_ops && ds->phylink_mac_ops->mac_link_down)
1872 ds->phylink_mac_ops->mac_link_down(&dp->pl_config, MLO_AN_FIXED,
1873 PHY_INTERFACE_MODE_NA);
1874 else if (ds->ops->phylink_mac_link_down)
1875 ds->ops->phylink_mac_link_down(ds, dp->index, MLO_AN_FIXED,
1876 PHY_INTERFACE_MODE_NA);
1877}
1878
770375ff 1879int dsa_shared_port_link_register_of(struct dsa_port *dp)
57ab1ca2 1880{
0e279218 1881 struct dsa_switch *ds = dp->ds;
e09e9873
VO
1882 bool missing_link_description;
1883 bool missing_phy_mode;
0e279218 1884
e09e9873
VO
1885 dsa_shared_port_validate_of(dp, &missing_phy_mode,
1886 &missing_link_description);
1887
1888 if ((missing_phy_mode || missing_link_description) &&
1889 !of_device_compatible_match(ds->dev->of_node,
1890 dsa_switches_apply_workarounds))
1891 return -EINVAL;
1892
8a021a86
FF
1893 if (missing_link_description) {
1894 dev_warn(ds->dev,
1895 "Skipping phylink registration for %s port %d\n",
1896 dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1897 } else {
1898 dsa_shared_port_link_down(dp);
e09e9873 1899
8a021a86 1900 return dsa_shared_port_phylink_register(dp);
a20f9970 1901 }
0e279218 1902
8a021a86 1903 return 0;
33615367 1904}
57ab1ca2 1905
770375ff 1906void dsa_shared_port_link_unregister_of(struct dsa_port *dp)
33615367 1907{
8a021a86 1908 if (dp->pl) {
0e279218
IC
1909 rtnl_lock();
1910 phylink_disconnect_phy(dp->pl);
1911 rtnl_unlock();
cf5ca4dd 1912 dsa_port_phylink_destroy(dp);
0e279218
IC
1913 return;
1914 }
57ab1ca2 1915}
cf963573 1916
fefe5dc4
VO
1917int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr,
1918 struct netlink_ext_ack *extack)
18596f50 1919{
a68dc7b9 1920 struct dsa_switch *ds = dp->ds;
18596f50
GM
1921 int err;
1922
a68dc7b9
VO
1923 if (!ds->ops->port_hsr_join)
1924 return -EOPNOTSUPP;
1925
18596f50
GM
1926 dp->hsr_dev = hsr;
1927
fefe5dc4 1928 err = ds->ops->port_hsr_join(ds, dp->index, hsr, extack);
18596f50
GM
1929 if (err)
1930 dp->hsr_dev = NULL;
1931
1932 return err;
1933}
1934
1935void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1936{
a68dc7b9 1937 struct dsa_switch *ds = dp->ds;
18596f50
GM
1938 int err;
1939
1940 dp->hsr_dev = NULL;
1941
a68dc7b9
VO
1942 if (ds->ops->port_hsr_leave) {
1943 err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
1944 if (err)
1945 dev_err(dp->ds->dev,
1946 "port %d failed to leave HSR %s: %pe\n",
1947 dp->index, hsr->name, ERR_PTR(err));
1948 }
18596f50 1949}
c64b9c05 1950
724395f4 1951int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
c64b9c05
VO
1952{
1953 struct dsa_notifier_tag_8021q_vlan_info info = {
726816a1 1954 .dp = dp,
c64b9c05
VO
1955 .vid = vid,
1956 };
1957
724395f4
VO
1958 if (broadcast)
1959 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1960
1961 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
c64b9c05
VO
1962}
1963
724395f4 1964void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
c64b9c05
VO
1965{
1966 struct dsa_notifier_tag_8021q_vlan_info info = {
726816a1 1967 .dp = dp,
c64b9c05
VO
1968 .vid = vid,
1969 };
1970 int err;
1971
724395f4
VO
1972 if (broadcast)
1973 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1974 else
1975 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
c64b9c05 1976 if (err)
ab97462b
VO
1977 dev_err(dp->ds->dev,
1978 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1979 dp->index, vid, ERR_PTR(err));
c64b9c05 1980}