hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/net/dsa/port.c
....@@ -1,13 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Handling of a single switch port
34 *
45 * Copyright (c) 2017 Savoir-faire Linux Inc.
56 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version.
117 */
128
139 #include <linux/if_bridge.h>
....@@ -16,6 +12,23 @@
1612 #include <linux/of_net.h>
1713
1814 #include "dsa_priv.h"
15
+
16
+static int dsa_broadcast(unsigned long e, void *v)
17
+{
18
+ struct dsa_switch_tree *dst;
19
+ int err = 0;
20
+
21
+ list_for_each_entry(dst, &dsa_tree_list, list) {
22
+ struct raw_notifier_head *nh = &dst->nh;
23
+
24
+ err = raw_notifier_call_chain(nh, e, v);
25
+ err = notifier_to_errno(err);
26
+ if (err)
27
+ break;
28
+ }
29
+
30
+ return err;
31
+}
1932
2033 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
2134 {
....@@ -67,7 +80,7 @@
6780 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
6881 }
6982
70
-int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
83
+int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
7184 {
7285 struct dsa_switch *ds = dp->ds;
7386 int port = dp->index;
....@@ -82,40 +95,72 @@
8295 if (!dp->bridge_dev)
8396 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
8497
98
+ if (dp->pl)
99
+ phylink_start(dp->pl);
100
+
85101 return 0;
86102 }
87103
88
-void dsa_port_disable(struct dsa_port *dp, struct phy_device *phy)
104
+int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
105
+{
106
+ int err;
107
+
108
+ rtnl_lock();
109
+ err = dsa_port_enable_rt(dp, phy);
110
+ rtnl_unlock();
111
+
112
+ return err;
113
+}
114
+
115
+void dsa_port_disable_rt(struct dsa_port *dp)
89116 {
90117 struct dsa_switch *ds = dp->ds;
91118 int port = dp->index;
119
+
120
+ if (dp->pl)
121
+ phylink_stop(dp->pl);
92122
93123 if (!dp->bridge_dev)
94124 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
95125
96126 if (ds->ops->port_disable)
97
- ds->ops->port_disable(ds, port, phy);
127
+ ds->ops->port_disable(ds, port);
128
+}
129
+
130
+void dsa_port_disable(struct dsa_port *dp)
131
+{
132
+ rtnl_lock();
133
+ dsa_port_disable_rt(dp);
134
+ rtnl_unlock();
98135 }
99136
100137 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
101138 {
102139 struct dsa_notifier_bridge_info info = {
140
+ .tree_index = dp->ds->dst->index,
103141 .sw_index = dp->ds->index,
104142 .port = dp->index,
105143 .br = br,
106144 };
107145 int err;
108146
109
- /* Here the port is already bridged. Reflect the current configuration
110
- * so that drivers can program their chips accordingly.
147
+ /* Set the flooding mode before joining the port in the switch */
148
+ err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL);
149
+ if (err)
150
+ return err;
151
+
152
+ /* Here the interface is already bridged. Reflect the current
153
+ * configuration so that drivers can program their chips accordingly.
111154 */
112155 dp->bridge_dev = br;
113156
114
- err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
157
+ err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
115158
116159 /* The bridging is rolled back on error */
117
- if (err)
160
+ if (err) {
161
+ dsa_port_bridge_flags(dp, 0, NULL);
118162 dp->bridge_dev = NULL;
163
+ }
119164
120165 return err;
121166 }
....@@ -123,6 +168,7 @@
123168 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
124169 {
125170 struct dsa_notifier_bridge_info info = {
171
+ .tree_index = dp->ds->dst->index,
126172 .sw_index = dp->ds->index,
127173 .port = dp->index,
128174 .br = br,
....@@ -134,9 +180,12 @@
134180 */
135181 dp->bridge_dev = NULL;
136182
137
- err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
183
+ err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
138184 if (err)
139185 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
186
+
187
+ /* Port is leaving the bridge, disable flooding */
188
+ dsa_port_bridge_flags(dp, 0, NULL);
140189
141190 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
142191 * so allow it to be in BR_STATE_FORWARDING to be kept functional
....@@ -144,20 +193,125 @@
144193 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
145194 }
146195
196
+/* Must be called under rcu_read_lock() */
197
+static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
198
+ bool vlan_filtering)
199
+{
200
+ struct dsa_switch *ds = dp->ds;
201
+ int err, i;
202
+
203
+ /* VLAN awareness was off, so the question is "can we turn it on".
204
+ * We may have had 8021q uppers, those need to go. Make sure we don't
205
+ * enter an inconsistent state: deny changing the VLAN awareness state
206
+ * as long as we have 8021q uppers.
207
+ */
208
+ if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
209
+ struct net_device *upper_dev, *slave = dp->slave;
210
+ struct net_device *br = dp->bridge_dev;
211
+ struct list_head *iter;
212
+
213
+ netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
214
+ struct bridge_vlan_info br_info;
215
+ u16 vid;
216
+
217
+ if (!is_vlan_dev(upper_dev))
218
+ continue;
219
+
220
+ vid = vlan_dev_vlan_id(upper_dev);
221
+
222
+ /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
223
+ * device, respectively the VID is not found, returning
224
+ * 0 means success, which is a failure for us here.
225
+ */
226
+ err = br_vlan_get_info(br, vid, &br_info);
227
+ if (err == 0) {
228
+ dev_err(ds->dev, "Must remove upper %s first\n",
229
+ upper_dev->name);
230
+ return false;
231
+ }
232
+ }
233
+ }
234
+
235
+ if (!ds->vlan_filtering_is_global)
236
+ return true;
237
+
238
+ /* For cases where enabling/disabling VLAN awareness is global to the
239
+ * switch, we need to handle the case where multiple bridges span
240
+ * different ports of the same switch device and one of them has a
241
+ * different setting than what is being requested.
242
+ */
243
+ for (i = 0; i < ds->num_ports; i++) {
244
+ struct net_device *other_bridge;
245
+
246
+ other_bridge = dsa_to_port(ds, i)->bridge_dev;
247
+ if (!other_bridge)
248
+ continue;
249
+ /* If it's the same bridge, it also has same
250
+ * vlan_filtering setting => no need to check
251
+ */
252
+ if (other_bridge == dp->bridge_dev)
253
+ continue;
254
+ if (br_vlan_enabled(other_bridge) != vlan_filtering) {
255
+ dev_err(ds->dev, "VLAN filtering is a global setting\n");
256
+ return false;
257
+ }
258
+ }
259
+ return true;
260
+}
261
+
147262 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
148263 struct switchdev_trans *trans)
149264 {
150265 struct dsa_switch *ds = dp->ds;
266
+ int err;
151267
152
- /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
153
- if (switchdev_trans_ph_prepare(trans))
268
+ if (switchdev_trans_ph_prepare(trans)) {
269
+ bool apply;
270
+
271
+ if (!ds->ops->port_vlan_filtering)
272
+ return -EOPNOTSUPP;
273
+
274
+ /* We are called from dsa_slave_switchdev_blocking_event(),
275
+ * which is not under rcu_read_lock(), unlike
276
+ * dsa_slave_switchdev_event().
277
+ */
278
+ rcu_read_lock();
279
+ apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering);
280
+ rcu_read_unlock();
281
+ if (!apply)
282
+ return -EINVAL;
283
+ }
284
+
285
+ if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
154286 return 0;
155287
156
- if (ds->ops->port_vlan_filtering)
157
- return ds->ops->port_vlan_filtering(ds, dp->index,
158
- vlan_filtering);
288
+ err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
289
+ trans);
290
+ if (err)
291
+ return err;
292
+
293
+ if (switchdev_trans_ph_commit(trans)) {
294
+ if (ds->vlan_filtering_is_global)
295
+ ds->vlan_filtering = vlan_filtering;
296
+ else
297
+ dp->vlan_filtering = vlan_filtering;
298
+ }
159299
160300 return 0;
301
+}
302
+
303
+/* This enforces legacy behavior for switch drivers which assume they can't
304
+ * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
305
+ */
306
+bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
307
+{
308
+ struct dsa_switch *ds = dp->ds;
309
+
310
+ if (!dp->bridge_dev)
311
+ return false;
312
+
313
+ return (!ds->configure_vlan_while_not_filtering &&
314
+ !br_vlan_enabled(dp->bridge_dev));
161315 }
162316
163317 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
....@@ -176,6 +330,60 @@
176330 dp->ageing_time = ageing_time;
177331
178332 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
333
+}
334
+
335
+int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags,
336
+ struct switchdev_trans *trans)
337
+{
338
+ struct dsa_switch *ds = dp->ds;
339
+
340
+ if (!ds->ops->port_egress_floods ||
341
+ (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
342
+ return -EINVAL;
343
+
344
+ return 0;
345
+}
346
+
347
+int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags,
348
+ struct switchdev_trans *trans)
349
+{
350
+ struct dsa_switch *ds = dp->ds;
351
+ int port = dp->index;
352
+ int err = 0;
353
+
354
+ if (switchdev_trans_ph_prepare(trans))
355
+ return 0;
356
+
357
+ if (ds->ops->port_egress_floods)
358
+ err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
359
+ flags & BR_MCAST_FLOOD);
360
+
361
+ return err;
362
+}
363
+
364
+int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
365
+ struct switchdev_trans *trans)
366
+{
367
+ struct dsa_switch *ds = dp->ds;
368
+ int port = dp->index;
369
+
370
+ if (switchdev_trans_ph_prepare(trans))
371
+ return ds->ops->port_egress_floods ? 0 : -EOPNOTSUPP;
372
+
373
+ return ds->ops->port_egress_floods(ds, port, true, mrouter);
374
+}
375
+
376
+int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
377
+ bool propagate_upstream)
378
+{
379
+ struct dsa_notifier_mtu_info info = {
380
+ .sw_index = dp->ds->index,
381
+ .propagate_upstream = propagate_upstream,
382
+ .port = dp->index,
383
+ .mtu = new_mtu,
384
+ };
385
+
386
+ return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
179387 }
180388
181389 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
....@@ -253,13 +461,7 @@
253461 .vlan = vlan,
254462 };
255463
256
- if (netif_is_bridge_master(vlan->obj.orig_dev))
257
- return -EOPNOTSUPP;
258
-
259
- if (br_vlan_enabled(dp->bridge_dev))
260
- return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
261
-
262
- return 0;
464
+ return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
263465 }
264466
265467 int dsa_port_vlan_del(struct dsa_port *dp,
....@@ -271,13 +473,7 @@
271473 .vlan = vlan,
272474 };
273475
274
- if (netif_is_bridge_master(vlan->obj.orig_dev))
275
- return -EOPNOTSUPP;
276
-
277
- if (br_vlan_enabled(dp->bridge_dev))
278
- return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
279
-
280
- return 0;
476
+ return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
281477 }
282478
283479 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
....@@ -295,8 +491,116 @@
295491 return ERR_PTR(-EPROBE_DEFER);
296492 }
297493
494
+ of_node_put(phy_dn);
298495 return phydev;
299496 }
497
+
498
+static void dsa_port_phylink_validate(struct phylink_config *config,
499
+ unsigned long *supported,
500
+ struct phylink_link_state *state)
501
+{
502
+ struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
503
+ struct dsa_switch *ds = dp->ds;
504
+
505
+ if (!ds->ops->phylink_validate)
506
+ return;
507
+
508
+ ds->ops->phylink_validate(ds, dp->index, supported, state);
509
+}
510
+
511
+static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
512
+ struct phylink_link_state *state)
513
+{
514
+ struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
515
+ struct dsa_switch *ds = dp->ds;
516
+ int err;
517
+
518
+ /* Only called for inband modes */
519
+ if (!ds->ops->phylink_mac_link_state) {
520
+ state->link = 0;
521
+ return;
522
+ }
523
+
524
+ err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
525
+ if (err < 0) {
526
+ dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
527
+ dp->index, err);
528
+ state->link = 0;
529
+ }
530
+}
531
+
532
+static void dsa_port_phylink_mac_config(struct phylink_config *config,
533
+ unsigned int mode,
534
+ const struct phylink_link_state *state)
535
+{
536
+ struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
537
+ struct dsa_switch *ds = dp->ds;
538
+
539
+ if (!ds->ops->phylink_mac_config)
540
+ return;
541
+
542
+ ds->ops->phylink_mac_config(ds, dp->index, mode, state);
543
+}
544
+
545
+static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
546
+{
547
+ struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
548
+ struct dsa_switch *ds = dp->ds;
549
+
550
+ if (!ds->ops->phylink_mac_an_restart)
551
+ return;
552
+
553
+ ds->ops->phylink_mac_an_restart(ds, dp->index);
554
+}
555
+
556
+static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
557
+ unsigned int mode,
558
+ phy_interface_t interface)
559
+{
560
+ struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
561
+ struct phy_device *phydev = NULL;
562
+ struct dsa_switch *ds = dp->ds;
563
+
564
+ if (dsa_is_user_port(ds, dp->index))
565
+ phydev = dp->slave->phydev;
566
+
567
+ if (!ds->ops->phylink_mac_link_down) {
568
+ if (ds->ops->adjust_link && phydev)
569
+ ds->ops->adjust_link(ds, dp->index, phydev);
570
+ return;
571
+ }
572
+
573
+ ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
574
+}
575
+
576
+static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
577
+ struct phy_device *phydev,
578
+ unsigned int mode,
579
+ phy_interface_t interface,
580
+ int speed, int duplex,
581
+ bool tx_pause, bool rx_pause)
582
+{
583
+ struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
584
+ struct dsa_switch *ds = dp->ds;
585
+
586
+ if (!ds->ops->phylink_mac_link_up) {
587
+ if (ds->ops->adjust_link && phydev)
588
+ ds->ops->adjust_link(ds, dp->index, phydev);
589
+ return;
590
+ }
591
+
592
+ ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
593
+ speed, duplex, tx_pause, rx_pause);
594
+}
595
+
596
+const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
597
+ .validate = dsa_port_phylink_validate,
598
+ .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
599
+ .mac_config = dsa_port_phylink_mac_config,
600
+ .mac_an_restart = dsa_port_phylink_mac_an_restart,
601
+ .mac_link_down = dsa_port_phylink_mac_link_down,
602
+ .mac_link_up = dsa_port_phylink_mac_link_up,
603
+};
300604
301605 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
302606 {
....@@ -313,10 +617,6 @@
313617 return PTR_ERR(phydev);
314618
315619 if (enable) {
316
- err = genphy_config_init(phydev);
317
- if (err < 0)
318
- goto err_put_dev;
319
-
320620 err = genphy_resume(phydev);
321621 if (err < 0)
322622 goto err_put_dev;
....@@ -346,7 +646,7 @@
346646 struct dsa_switch *ds = dp->ds;
347647 struct phy_device *phydev;
348648 int port = dp->index;
349
- int mode;
649
+ phy_interface_t mode;
350650 int err;
351651
352652 err = of_phy_register_fixed_link(dn);
....@@ -359,12 +659,11 @@
359659
360660 phydev = of_phy_find_device(dn);
361661
362
- mode = of_get_phy_mode(dn);
363
- if (mode < 0)
662
+ err = of_get_phy_mode(dn, &mode);
663
+ if (err)
364664 mode = PHY_INTERFACE_MODE_NA;
365665 phydev->interface = mode;
366666
367
- genphy_config_init(phydev);
368667 genphy_read_status(phydev);
369668
370669 if (ds->ops->adjust_link)
....@@ -375,8 +674,63 @@
375674 return 0;
376675 }
377676
677
+static int dsa_port_phylink_register(struct dsa_port *dp)
678
+{
679
+ struct dsa_switch *ds = dp->ds;
680
+ struct device_node *port_dn = dp->dn;
681
+ phy_interface_t mode;
682
+ int err;
683
+
684
+ err = of_get_phy_mode(port_dn, &mode);
685
+ if (err)
686
+ mode = PHY_INTERFACE_MODE_NA;
687
+
688
+ dp->pl_config.dev = ds->dev;
689
+ dp->pl_config.type = PHYLINK_DEV;
690
+ dp->pl_config.pcs_poll = ds->pcs_poll;
691
+
692
+ dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
693
+ mode, &dsa_port_phylink_mac_ops);
694
+ if (IS_ERR(dp->pl)) {
695
+ pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
696
+ return PTR_ERR(dp->pl);
697
+ }
698
+
699
+ err = phylink_of_phy_connect(dp->pl, port_dn, 0);
700
+ if (err && err != -ENODEV) {
701
+ pr_err("could not attach to PHY: %d\n", err);
702
+ goto err_phy_connect;
703
+ }
704
+
705
+ return 0;
706
+
707
+err_phy_connect:
708
+ phylink_destroy(dp->pl);
709
+ return err;
710
+}
711
+
378712 int dsa_port_link_register_of(struct dsa_port *dp)
379713 {
714
+ struct dsa_switch *ds = dp->ds;
715
+ struct device_node *phy_np;
716
+ int port = dp->index;
717
+
718
+ if (!ds->ops->adjust_link) {
719
+ phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
720
+ if (of_phy_is_fixed_link(dp->dn) || phy_np) {
721
+ if (ds->ops->phylink_mac_link_down)
722
+ ds->ops->phylink_mac_link_down(ds, port,
723
+ MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
724
+ of_node_put(phy_np);
725
+ return dsa_port_phylink_register(dp);
726
+ }
727
+ of_node_put(phy_np);
728
+ return 0;
729
+ }
730
+
731
+ dev_warn(ds->dev,
732
+ "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
733
+
380734 if (of_phy_is_fixed_link(dp->dn))
381735 return dsa_port_fixed_link_register_of(dp);
382736 else
....@@ -385,6 +739,17 @@
385739
386740 void dsa_port_link_unregister_of(struct dsa_port *dp)
387741 {
742
+ struct dsa_switch *ds = dp->ds;
743
+
744
+ if (!ds->ops->adjust_link && dp->pl) {
745
+ rtnl_lock();
746
+ phylink_disconnect_phy(dp->pl);
747
+ rtnl_unlock();
748
+ phylink_destroy(dp->pl);
749
+ dp->pl = NULL;
750
+ return;
751
+ }
752
+
388753 if (of_phy_is_fixed_link(dp->dn))
389754 of_phy_deregister_fixed_link(dp->dn);
390755 else