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