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