blob: d81c283b7358fa9417cd116e9e9133f16a825b68 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vivien Didelota40c1752017-05-19 17:00:44 -04002/*
3 * Handling of a single switch port
4 *
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelota40c1752017-05-19 17:00:44 -04007 */
8
9#include <linux/if_bridge.h>
Vivien Didelotcfbed322017-05-19 17:00:45 -040010#include <linux/notifier.h>
Vivien Didelot57ab1ca2017-10-26 10:50:07 -040011#include <linux/of_mdio.h>
12#include <linux/of_net.h>
Vivien Didelota40c1752017-05-19 17:00:44 -040013
14#include "dsa_priv.h"
15
Vladimir Oltean886f8e22021-01-29 03:00:04 +020016/**
17 * dsa_port_notify - Notify the switching fabric of changes to a port
18 * @dp: port on which change occurred
19 * @e: event, must be of type DSA_NOTIFIER_*
20 * @v: event-specific value.
21 *
22 * Notify all switches in the DSA tree that this port's switch belongs to,
23 * including this switch itself, of an event. Allows the other switches to
24 * reconfigure themselves for cross-chip operations. Can also be used to
25 * reconfigure ports without net_devices (CPU ports, DSA links) whenever
26 * a user port's state changes.
27 */
Andrew Lunnbb9f603172017-11-09 23:11:01 +010028static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
Vivien Didelotcfbed322017-05-19 17:00:45 -040029{
Vladimir Oltean886f8e22021-01-29 03:00:04 +020030 return dsa_tree_notify(dp->ds->dst, e, v);
Vivien Didelotcfbed322017-05-19 17:00:45 -040031}
32
Vladimir Olteanbae33f22021-01-09 02:01:50 +020033int dsa_port_set_state(struct dsa_port *dp, u8 state)
Vivien Didelota40c1752017-05-19 17:00:44 -040034{
35 struct dsa_switch *ds = dp->ds;
36 int port = dp->index;
37
Vladimir Olteanbae33f22021-01-09 02:01:50 +020038 if (!ds->ops->port_stp_state_set)
39 return -EOPNOTSUPP;
Vivien Didelota40c1752017-05-19 17:00:44 -040040
Vladimir Olteanbae33f22021-01-09 02:01:50 +020041 ds->ops->port_stp_state_set(ds, port, state);
Vivien Didelota40c1752017-05-19 17:00:44 -040042
43 if (ds->ops->port_fast_age) {
44 /* Fast age FDB entries or flush appropriate forwarding database
45 * for the given port, if we are moving it from Learning or
46 * Forwarding state, to Disabled or Blocking or Listening state.
47 */
48
49 if ((dp->stp_state == BR_STATE_LEARNING ||
50 dp->stp_state == BR_STATE_FORWARDING) &&
51 (state == BR_STATE_DISABLED ||
52 state == BR_STATE_BLOCKING ||
53 state == BR_STATE_LISTENING))
54 ds->ops->port_fast_age(ds, port);
55 }
56
57 dp->stp_state = state;
58
59 return 0;
60}
61
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040062static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
Vivien Didelota40c1752017-05-19 17:00:44 -040063{
64 int err;
65
Vladimir Olteanbae33f22021-01-09 02:01:50 +020066 err = dsa_port_set_state(dp, state);
Vivien Didelota40c1752017-05-19 17:00:44 -040067 if (err)
68 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
69}
Vivien Didelotcfbed322017-05-19 17:00:45 -040070
Russell King8640f8d2020-03-03 15:01:46 +000071int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040072{
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040073 struct dsa_switch *ds = dp->ds;
74 int port = dp->index;
75 int err;
76
77 if (ds->ops->port_enable) {
78 err = ds->ops->port_enable(ds, port, phy);
79 if (err)
80 return err;
81 }
82
Russell King9c2054a2019-02-20 10:32:52 +000083 if (!dp->bridge_dev)
84 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040085
Russell King8640f8d2020-03-03 15:01:46 +000086 if (dp->pl)
87 phylink_start(dp->pl);
88
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040089 return 0;
90}
91
Russell King8640f8d2020-03-03 15:01:46 +000092int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
93{
94 int err;
95
96 rtnl_lock();
97 err = dsa_port_enable_rt(dp, phy);
98 rtnl_unlock();
99
100 return err;
101}
102
103void dsa_port_disable_rt(struct dsa_port *dp)
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400104{
105 struct dsa_switch *ds = dp->ds;
106 int port = dp->index;
107
Russell King8640f8d2020-03-03 15:01:46 +0000108 if (dp->pl)
109 phylink_stop(dp->pl);
110
Russell King9c2054a2019-02-20 10:32:52 +0000111 if (!dp->bridge_dev)
112 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400113
114 if (ds->ops->port_disable)
Andrew Lunn75104db2019-02-24 20:44:43 +0100115 ds->ops->port_disable(ds, port);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400116}
117
Russell King8640f8d2020-03-03 15:01:46 +0000118void dsa_port_disable(struct dsa_port *dp)
119{
120 rtnl_lock();
121 dsa_port_disable_rt(dp);
122 rtnl_unlock();
123}
124
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200125static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
126 struct netlink_ext_ack *extack)
Vladimir Oltean5e38c152021-02-12 17:15:54 +0200127{
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200128 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
129 BR_BCAST_FLOOD;
130 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
131 int flag, err;
Vladimir Oltean5e38c152021-02-12 17:15:54 +0200132
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200133 for_each_set_bit(flag, &mask, 32) {
134 struct switchdev_brport_flags flags = {0};
Vladimir Oltean5e38c152021-02-12 17:15:54 +0200135
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200136 flags.mask = BIT(flag);
Vladimir Oltean5e38c152021-02-12 17:15:54 +0200137
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200138 if (br_port_flag_is_set(brport_dev, BIT(flag)))
139 flags.val = BIT(flag);
Vladimir Olteane18f4c12021-02-12 17:15:55 +0200140
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200141 err = dsa_port_bridge_flags(dp, flags, extack);
142 if (err && err != -EOPNOTSUPP)
143 return err;
Vladimir Oltean5e38c152021-02-12 17:15:54 +0200144 }
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200145
146 return 0;
147}
148
149static void dsa_port_clear_brport_flags(struct dsa_port *dp)
150{
151 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
152 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
153 BR_BCAST_FLOOD;
154 int flag, err;
155
156 for_each_set_bit(flag, &mask, 32) {
157 struct switchdev_brport_flags flags = {0};
158
159 flags.mask = BIT(flag);
160 flags.val = val & BIT(flag);
161
162 err = dsa_port_bridge_flags(dp, flags, NULL);
163 if (err && err != -EOPNOTSUPP)
164 dev_err(dp->ds->dev,
165 "failed to clear bridge port flag %lu: %pe\n",
166 flags.val, ERR_PTR(err));
167 }
168}
169
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300170static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
171 struct netlink_ext_ack *extack)
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200172{
Vladimir Oltean010e2692021-03-23 01:51:50 +0200173 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
174 struct net_device *br = dp->bridge_dev;
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200175 int err;
176
177 err = dsa_port_inherit_brport_flags(dp, extack);
178 if (err)
179 return err;
180
Vladimir Oltean010e2692021-03-23 01:51:50 +0200181 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev));
182 if (err && err != -EOPNOTSUPP)
183 return err;
184
185 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
186 if (err && err != -EOPNOTSUPP)
187 return err;
188
189 err = dsa_port_mrouter(dp->cpu_dp, br_multicast_router(br), extack);
190 if (err && err != -EOPNOTSUPP)
191 return err;
192
193 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
194 if (err && err != -EOPNOTSUPP)
195 return err;
196
Vladimir Oltean74918942021-06-27 14:54:29 +0300197 return 0;
198}
199
200static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp)
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200201{
202 /* Configure the port for standalone mode (no address learning,
203 * flood everything).
204 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
205 * when the user requests it through netlink or sysfs, but not
206 * automatically at port join or leave, so we need to handle resetting
207 * the brport flags ourselves. But we even prefer it that way, because
208 * otherwise, some setups might never get the notification they need,
209 * for example, when a port leaves a LAG that offloads the bridge,
210 * it becomes standalone, but as far as the bridge is concerned, no
211 * port ever left.
212 */
213 dsa_port_clear_brport_flags(dp);
214
215 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
216 * so allow it to be in BR_STATE_FORWARDING to be kept functional
217 */
218 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
Vladimir Oltean010e2692021-03-23 01:51:50 +0200219
220 /* VLAN filtering is handled by dsa_switch_bridge_leave */
221
222 /* Some drivers treat the notification for having a local multicast
223 * router by allowing multicast to be flooded to the CPU, so we should
224 * allow this in standalone mode too.
225 */
226 dsa_port_mrouter(dp->cpu_dp, true, NULL);
227
228 /* Ageing time may be global to the switch chip, so don't change it
229 * here because we have no good reason (or value) to change it to.
230 */
Vladimir Oltean5e38c152021-02-12 17:15:54 +0200231}
232
Vladimir Oltean2afc5262021-03-23 01:51:48 +0200233int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
234 struct netlink_ext_ack *extack)
Vivien Didelotcfbed322017-05-19 17:00:45 -0400235{
236 struct dsa_notifier_bridge_info info = {
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300237 .tree_index = dp->ds->dst->index,
Vivien Didelotcfbed322017-05-19 17:00:45 -0400238 .sw_index = dp->ds->index,
239 .port = dp->index,
240 .br = br,
241 };
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300242 struct net_device *dev = dp->slave;
243 struct net_device *brport_dev;
Vivien Didelotcfbed322017-05-19 17:00:45 -0400244 int err;
245
Russell Kingc1388062019-02-20 15:35:06 -0800246 /* Here the interface is already bridged. Reflect the current
247 * configuration so that drivers can program their chips accordingly.
Vivien Didelotcfbed322017-05-19 17:00:45 -0400248 */
249 dp->bridge_dev = br;
250
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300251 brport_dev = dsa_port_to_bridge_port(dp);
252
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300253 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200254 if (err)
255 goto out_rollback;
Vivien Didelotcfbed322017-05-19 17:00:45 -0400256
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300257 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
258 &dsa_slave_switchdev_notifier,
259 &dsa_slave_switchdev_blocking_notifier,
260 extack);
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200261 if (err)
262 goto out_rollback_unbridge;
Vivien Didelotcfbed322017-05-19 17:00:45 -0400263
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300264 err = dsa_port_switchdev_sync_attrs(dp, extack);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300265 if (err)
266 goto out_rollback_unoffload;
267
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200268 return 0;
269
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300270out_rollback_unoffload:
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300271 switchdev_bridge_port_unoffload(brport_dev, dp,
272 &dsa_slave_switchdev_notifier,
273 &dsa_slave_switchdev_blocking_notifier);
Vladimir Oltean5961d6a2021-03-23 01:51:49 +0200274out_rollback_unbridge:
275 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
276out_rollback:
277 dp->bridge_dev = NULL;
Vivien Didelotcfbed322017-05-19 17:00:45 -0400278 return err;
279}
280
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300281void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
Vladimir Oltean74918942021-06-27 14:54:29 +0300282{
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300283 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
284
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300285 switchdev_bridge_port_unoffload(brport_dev, dp,
286 &dsa_slave_switchdev_notifier,
287 &dsa_slave_switchdev_blocking_notifier);
Vladimir Oltean74918942021-06-27 14:54:29 +0300288}
289
Vivien Didelotcfbed322017-05-19 17:00:45 -0400290void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
291{
292 struct dsa_notifier_bridge_info info = {
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300293 .tree_index = dp->ds->dst->index,
Vivien Didelotcfbed322017-05-19 17:00:45 -0400294 .sw_index = dp->ds->index,
295 .port = dp->index,
296 .br = br,
297 };
298 int err;
299
300 /* Here the port is already unbridged. Reflect the current configuration
301 * so that drivers can program their chips accordingly.
302 */
303 dp->bridge_dev = NULL;
304
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300305 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
Vivien Didelotcfbed322017-05-19 17:00:45 -0400306 if (err)
307 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
308
Vladimir Oltean74918942021-06-27 14:54:29 +0300309 dsa_port_switchdev_unsync_attrs(dp);
Vivien Didelotcfbed322017-05-19 17:00:45 -0400310}
Vivien Didelot4d61d302017-05-19 17:00:46 -0400311
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100312int dsa_port_lag_change(struct dsa_port *dp,
313 struct netdev_lag_lower_state_info *linfo)
314{
315 struct dsa_notifier_lag_info info = {
316 .sw_index = dp->ds->index,
317 .port = dp->index,
318 };
319 bool tx_enabled;
320
321 if (!dp->lag_dev)
322 return 0;
323
324 /* On statically configured aggregates (e.g. loadbalance
325 * without LACP) ports will always be tx_enabled, even if the
326 * link is down. Thus we require both link_up and tx_enabled
327 * in order to include it in the tx set.
328 */
329 tx_enabled = linfo->link_up && linfo->tx_enabled;
330
331 if (tx_enabled == dp->lag_tx_enabled)
332 return 0;
333
334 dp->lag_tx_enabled = tx_enabled;
335
336 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
337}
338
339int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
Vladimir Oltean2afc5262021-03-23 01:51:48 +0200340 struct netdev_lag_upper_info *uinfo,
341 struct netlink_ext_ack *extack)
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100342{
343 struct dsa_notifier_lag_info info = {
344 .sw_index = dp->ds->index,
345 .port = dp->index,
346 .lag = lag,
347 .info = uinfo,
348 };
Vladimir Oltean185c9a72021-03-23 01:51:47 +0200349 struct net_device *bridge_dev;
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100350 int err;
351
352 dsa_lag_map(dp->ds->dst, lag);
353 dp->lag_dev = lag;
354
355 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
Vladimir Oltean185c9a72021-03-23 01:51:47 +0200356 if (err)
357 goto err_lag_join;
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100358
Vladimir Oltean185c9a72021-03-23 01:51:47 +0200359 bridge_dev = netdev_master_upper_dev_get(lag);
360 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
361 return 0;
362
Vladimir Oltean2afc5262021-03-23 01:51:48 +0200363 err = dsa_port_bridge_join(dp, bridge_dev, extack);
Vladimir Oltean185c9a72021-03-23 01:51:47 +0200364 if (err)
365 goto err_bridge_join;
366
367 return 0;
368
369err_bridge_join:
370 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
371err_lag_join:
372 dp->lag_dev = NULL;
373 dsa_lag_unmap(dp->ds->dst, lag);
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100374 return err;
375}
376
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300377void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag)
Vladimir Oltean74918942021-06-27 14:54:29 +0300378{
379 if (dp->bridge_dev)
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300380 dsa_port_pre_bridge_leave(dp, dp->bridge_dev);
Vladimir Oltean74918942021-06-27 14:54:29 +0300381}
382
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100383void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
384{
385 struct dsa_notifier_lag_info info = {
386 .sw_index = dp->ds->index,
387 .port = dp->index,
388 .lag = lag,
389 };
390 int err;
391
392 if (!dp->lag_dev)
393 return;
394
395 /* Port might have been part of a LAG that in turn was
396 * attached to a bridge.
397 */
398 if (dp->bridge_dev)
399 dsa_port_bridge_leave(dp, dp->bridge_dev);
400
401 dp->lag_tx_enabled = false;
402 dp->lag_dev = NULL;
403
404 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
405 if (err)
406 pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n",
407 err);
408
409 dsa_lag_unmap(dp->ds->dst, lag);
410}
411
Vladimir Olteanadb256e2020-09-21 03:10:28 +0300412/* Must be called under rcu_read_lock() */
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300413static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200414 bool vlan_filtering,
415 struct netlink_ext_ack *extack)
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300416{
417 struct dsa_switch *ds = dp->ds;
Vladimir Olteanadb256e2020-09-21 03:10:28 +0300418 int err, i;
419
420 /* VLAN awareness was off, so the question is "can we turn it on".
421 * We may have had 8021q uppers, those need to go. Make sure we don't
422 * enter an inconsistent state: deny changing the VLAN awareness state
423 * as long as we have 8021q uppers.
424 */
425 if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
426 struct net_device *upper_dev, *slave = dp->slave;
427 struct net_device *br = dp->bridge_dev;
428 struct list_head *iter;
429
430 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
431 struct bridge_vlan_info br_info;
432 u16 vid;
433
434 if (!is_vlan_dev(upper_dev))
435 continue;
436
437 vid = vlan_dev_vlan_id(upper_dev);
438
439 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
440 * device, respectively the VID is not found, returning
441 * 0 means success, which is a failure for us here.
442 */
443 err = br_vlan_get_info(br, vid, &br_info);
444 if (err == 0) {
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200445 NL_SET_ERR_MSG_MOD(extack,
446 "Must first remove VLAN uppers having VIDs also present in bridge");
Vladimir Olteanadb256e2020-09-21 03:10:28 +0300447 return false;
448 }
449 }
450 }
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300451
452 if (!ds->vlan_filtering_is_global)
453 return true;
454
455 /* For cases where enabling/disabling VLAN awareness is global to the
456 * switch, we need to handle the case where multiple bridges span
457 * different ports of the same switch device and one of them has a
458 * different setting than what is being requested.
459 */
460 for (i = 0; i < ds->num_ports; i++) {
461 struct net_device *other_bridge;
462
463 other_bridge = dsa_to_port(ds, i)->bridge_dev;
464 if (!other_bridge)
465 continue;
466 /* If it's the same bridge, it also has same
467 * vlan_filtering setting => no need to check
468 */
469 if (other_bridge == dp->bridge_dev)
470 continue;
471 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200472 NL_SET_ERR_MSG_MOD(extack,
473 "VLAN filtering is a global setting");
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300474 return false;
475 }
476 }
477 return true;
478}
479
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200480int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
481 struct netlink_ext_ack *extack)
Vivien Didelot4d61d302017-05-19 17:00:46 -0400482{
483 struct dsa_switch *ds = dp->ds;
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200484 bool apply;
Vladimir Oltean33162e92019-04-28 21:45:43 +0300485 int err;
Vivien Didelot4d61d302017-05-19 17:00:46 -0400486
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200487 if (!ds->ops->port_vlan_filtering)
488 return -EOPNOTSUPP;
Vladimir Olteanadb256e2020-09-21 03:10:28 +0300489
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200490 /* We are called from dsa_slave_switchdev_blocking_event(),
491 * which is not under rcu_read_lock(), unlike
492 * dsa_slave_switchdev_event().
493 */
494 rcu_read_lock();
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200495 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200496 rcu_read_unlock();
497 if (!apply)
498 return -EINVAL;
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300499
Vladimir Olteanec9121e2019-04-28 21:45:51 +0300500 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
501 return 0;
502
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200503 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
504 extack);
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300505 if (err)
506 return err;
507
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200508 if (ds->vlan_filtering_is_global)
509 ds->vlan_filtering = vlan_filtering;
510 else
511 dp->vlan_filtering = vlan_filtering;
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300512
Vivien Didelot4d61d302017-05-19 17:00:46 -0400513 return 0;
514}
Vivien Didelotd87bd942017-05-19 17:00:47 -0400515
Russell King54a0ed02020-05-12 20:20:25 +0300516/* This enforces legacy behavior for switch drivers which assume they can't
517 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
518 */
519bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
520{
521 struct dsa_switch *ds = dp->ds;
522
523 if (!dp->bridge_dev)
524 return false;
525
526 return (!ds->configure_vlan_while_not_filtering &&
527 !br_vlan_enabled(dp->bridge_dev));
528}
529
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200530int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
Vivien Didelotd87bd942017-05-19 17:00:47 -0400531{
532 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
533 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200534 struct dsa_notifier_ageing_time_info info;
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200535 int err;
Vivien Didelotd87bd942017-05-19 17:00:47 -0400536
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200537 info.ageing_time = ageing_time;
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200538
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200539 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
540 if (err)
541 return err;
Vivien Didelotd87bd942017-05-19 17:00:47 -0400542
Vivien Didelotd87bd942017-05-19 17:00:47 -0400543 dp->ageing_time = ageing_time;
Vivien Didelotd87bd942017-05-19 17:00:47 -0400544
Vladimir Oltean77b61362021-01-09 02:01:51 +0200545 return 0;
Vivien Didelotd87bd942017-05-19 17:00:47 -0400546}
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400547
Vladimir Olteane18f4c12021-02-12 17:15:55 +0200548int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200549 struct switchdev_brport_flags flags,
550 struct netlink_ext_ack *extack)
Florian Fainelliea870052019-02-20 16:58:22 -0800551{
552 struct dsa_switch *ds = dp->ds;
553
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200554 if (!ds->ops->port_pre_bridge_flags)
Florian Fainelliea870052019-02-20 16:58:22 -0800555 return -EINVAL;
556
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200557 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
Florian Fainelliea870052019-02-20 16:58:22 -0800558}
559
Vladimir Olteane18f4c12021-02-12 17:15:55 +0200560int dsa_port_bridge_flags(const struct dsa_port *dp,
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200561 struct switchdev_brport_flags flags,
562 struct netlink_ext_ack *extack)
Russell King57652792019-02-20 15:35:04 -0800563{
564 struct dsa_switch *ds = dp->ds;
Russell King57652792019-02-20 15:35:04 -0800565
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200566 if (!ds->ops->port_bridge_flags)
Oleksij Rempel70a7c482021-04-21 15:05:40 +0200567 return -EOPNOTSUPP;
Russell King57652792019-02-20 15:35:04 -0800568
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200569 return ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
Russell King57652792019-02-20 15:35:04 -0800570}
571
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200572int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
573 struct netlink_ext_ack *extack)
Vivien Didelot08cc83c2019-07-08 23:31:13 -0400574{
575 struct dsa_switch *ds = dp->ds;
Vivien Didelot08cc83c2019-07-08 23:31:13 -0400576
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200577 if (!ds->ops->port_set_mrouter)
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200578 return -EOPNOTSUPP;
Vivien Didelot08cc83c2019-07-08 23:31:13 -0400579
Vladimir Olteana8b659e2021-02-12 17:15:56 +0200580 return ds->ops->port_set_mrouter(ds, dp->index, mrouter, extack);
Vivien Didelot08cc83c2019-07-08 23:31:13 -0400581}
582
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200583int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
Vladimir Oltean88faba22021-06-21 19:42:18 +0300584 bool targeted_match)
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200585{
586 struct dsa_notifier_mtu_info info = {
587 .sw_index = dp->ds->index,
Vladimir Oltean88faba22021-06-21 19:42:18 +0300588 .targeted_match = targeted_match,
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200589 .port = dp->index,
590 .mtu = new_mtu,
591 };
592
593 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
594}
595
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300596int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
597 u16 vid)
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400598{
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400599 struct dsa_notifier_fdb_info info = {
600 .sw_index = dp->ds->index,
601 .port = dp->index,
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300602 .addr = addr,
603 .vid = vid,
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400604 };
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400605
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400606 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400607}
608
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300609int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
610 u16 vid)
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400611{
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400612 struct dsa_notifier_fdb_info info = {
613 .sw_index = dp->ds->index,
614 .port = dp->index,
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300615 .addr = addr,
616 .vid = vid,
617
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400618 };
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400619
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400620 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400621}
622
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300623int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
624 u16 vid)
625{
626 struct dsa_notifier_fdb_info info = {
627 .sw_index = dp->ds->index,
628 .port = dp->index,
629 .addr = addr,
630 .vid = vid,
631 };
Vladimir Oltean26ee7b02021-06-29 17:06:53 +0300632 struct dsa_port *cpu_dp = dp->cpu_dp;
633 int err;
634
635 err = dev_uc_add(cpu_dp->master, addr);
636 if (err)
637 return err;
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300638
639 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
640}
641
642int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
643 u16 vid)
644{
645 struct dsa_notifier_fdb_info info = {
646 .sw_index = dp->ds->index,
647 .port = dp->index,
648 .addr = addr,
649 .vid = vid,
650 };
Vladimir Oltean26ee7b02021-06-29 17:06:53 +0300651 struct dsa_port *cpu_dp = dp->cpu_dp;
652 int err;
653
654 err = dev_uc_del(cpu_dp->master, addr);
655 if (err)
656 return err;
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300657
658 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
659}
660
Vivien Didelotde40fc52017-09-20 19:32:14 -0400661int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
662{
663 struct dsa_switch *ds = dp->ds;
664 int port = dp->index;
665
666 if (!ds->ops->port_fdb_dump)
667 return -EOPNOTSUPP;
668
669 return ds->ops->port_fdb_dump(ds, port, cb, data);
670}
671
Andrew Lunnbb9f603172017-11-09 23:11:01 +0100672int dsa_port_mdb_add(const struct dsa_port *dp,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200673 const struct switchdev_obj_port_mdb *mdb)
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400674{
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400675 struct dsa_notifier_mdb_info info = {
676 .sw_index = dp->ds->index,
677 .port = dp->index,
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400678 .mdb = mdb,
679 };
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400680
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400681 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400682}
683
Andrew Lunnbb9f603172017-11-09 23:11:01 +0100684int dsa_port_mdb_del(const struct dsa_port *dp,
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400685 const struct switchdev_obj_port_mdb *mdb)
686{
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400687 struct dsa_notifier_mdb_info info = {
688 .sw_index = dp->ds->index,
689 .port = dp->index,
690 .mdb = mdb,
691 };
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400692
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400693 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400694}
695
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300696int dsa_port_host_mdb_add(const struct dsa_port *dp,
697 const struct switchdev_obj_port_mdb *mdb)
698{
699 struct dsa_notifier_mdb_info info = {
700 .sw_index = dp->ds->index,
701 .port = dp->index,
702 .mdb = mdb,
703 };
Vladimir Oltean26ee7b02021-06-29 17:06:53 +0300704 struct dsa_port *cpu_dp = dp->cpu_dp;
705 int err;
706
707 err = dev_mc_add(cpu_dp->master, mdb->addr);
708 if (err)
709 return err;
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300710
711 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
712}
713
714int dsa_port_host_mdb_del(const struct dsa_port *dp,
715 const struct switchdev_obj_port_mdb *mdb)
716{
717 struct dsa_notifier_mdb_info info = {
718 .sw_index = dp->ds->index,
719 .port = dp->index,
720 .mdb = mdb,
721 };
Vladimir Oltean26ee7b02021-06-29 17:06:53 +0300722 struct dsa_port *cpu_dp = dp->cpu_dp;
723 int err;
724
725 err = dev_mc_del(cpu_dp->master, mdb->addr);
726 if (err)
727 return err;
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300728
729 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
730}
731
Vivien Didelot076e7132017-05-19 17:00:50 -0400732int dsa_port_vlan_add(struct dsa_port *dp,
Vladimir Oltean31046a52021-02-13 22:43:18 +0200733 const struct switchdev_obj_port_vlan *vlan,
734 struct netlink_ext_ack *extack)
Vivien Didelot076e7132017-05-19 17:00:50 -0400735{
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400736 struct dsa_notifier_vlan_info info = {
737 .sw_index = dp->ds->index,
738 .port = dp->index,
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400739 .vlan = vlan,
Vladimir Oltean31046a52021-02-13 22:43:18 +0200740 .extack = extack,
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400741 };
Vivien Didelot076e7132017-05-19 17:00:50 -0400742
Vivien Didelotc5335d72019-08-25 13:25:18 -0400743 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
Vivien Didelot076e7132017-05-19 17:00:50 -0400744}
745
746int dsa_port_vlan_del(struct dsa_port *dp,
747 const struct switchdev_obj_port_vlan *vlan)
748{
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400749 struct dsa_notifier_vlan_info info = {
750 .sw_index = dp->ds->index,
751 .port = dp->index,
752 .vlan = vlan,
753 };
Vivien Didelot076e7132017-05-19 17:00:50 -0400754
Vivien Didelotc5335d72019-08-25 13:25:18 -0400755 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
Vivien Didelot076e7132017-05-19 17:00:50 -0400756}
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400757
Horatiu Vulturc595c432021-02-16 22:42:04 +0100758int dsa_port_mrp_add(const struct dsa_port *dp,
759 const struct switchdev_obj_mrp *mrp)
760{
761 struct dsa_notifier_mrp_info info = {
762 .sw_index = dp->ds->index,
763 .port = dp->index,
764 .mrp = mrp,
765 };
766
767 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info);
768}
769
770int dsa_port_mrp_del(const struct dsa_port *dp,
771 const struct switchdev_obj_mrp *mrp)
772{
773 struct dsa_notifier_mrp_info info = {
774 .sw_index = dp->ds->index,
775 .port = dp->index,
776 .mrp = mrp,
777 };
778
779 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info);
780}
781
782int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
783 const struct switchdev_obj_ring_role_mrp *mrp)
784{
785 struct dsa_notifier_mrp_ring_role_info info = {
786 .sw_index = dp->ds->index,
787 .port = dp->index,
788 .mrp = mrp,
789 };
790
791 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info);
792}
793
794int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
795 const struct switchdev_obj_ring_role_mrp *mrp)
796{
797 struct dsa_notifier_mrp_ring_role_info info = {
798 .sw_index = dp->ds->index,
799 .port = dp->index,
800 .mrp = mrp,
801 };
802
803 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info);
804}
805
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200806void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
807 const struct dsa_device_ops *tag_ops)
808{
809 cpu_dp->filter = tag_ops->filter;
810 cpu_dp->rcv = tag_ops->rcv;
811 cpu_dp->tag_ops = tag_ops;
812}
813
Florian Fainelli6207a782018-04-25 12:12:51 -0700814static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
815{
816 struct device_node *phy_dn;
817 struct phy_device *phydev;
818
819 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
820 if (!phy_dn)
821 return NULL;
822
823 phydev = of_phy_find_device(phy_dn);
824 if (!phydev) {
825 of_node_put(phy_dn);
826 return ERR_PTR(-EPROBE_DEFER);
827 }
828
Wen Yang9919a362019-02-25 15:22:19 +0800829 of_node_put(phy_dn);
Florian Fainelli6207a782018-04-25 12:12:51 -0700830 return phydev;
831}
832
Florian Fainelli8ae67492019-12-16 10:32:47 -0800833static void dsa_port_phylink_validate(struct phylink_config *config,
834 unsigned long *supported,
835 struct phylink_link_state *state)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300836{
837 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
838 struct dsa_switch *ds = dp->ds;
839
840 if (!ds->ops->phylink_validate)
841 return;
842
843 ds->ops->phylink_validate(ds, dp->index, supported, state);
844}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300845
Florian Fainelli8ae67492019-12-16 10:32:47 -0800846static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
847 struct phylink_link_state *state)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300848{
849 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
850 struct dsa_switch *ds = dp->ds;
Russell King87615c962020-03-14 10:15:28 +0000851 int err;
Ioana Ciornei77373d42019-05-28 20:38:15 +0300852
Russell Kingd46b7e42019-11-21 00:36:22 +0000853 /* Only called for inband modes */
854 if (!ds->ops->phylink_mac_link_state) {
855 state->link = 0;
856 return;
857 }
Ioana Ciornei77373d42019-05-28 20:38:15 +0300858
Russell King87615c962020-03-14 10:15:28 +0000859 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
860 if (err < 0) {
861 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
862 dp->index, err);
Russell Kingd46b7e42019-11-21 00:36:22 +0000863 state->link = 0;
Russell King87615c962020-03-14 10:15:28 +0000864 }
Ioana Ciornei77373d42019-05-28 20:38:15 +0300865}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300866
Florian Fainelli8ae67492019-12-16 10:32:47 -0800867static void dsa_port_phylink_mac_config(struct phylink_config *config,
868 unsigned int mode,
869 const struct phylink_link_state *state)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300870{
871 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
872 struct dsa_switch *ds = dp->ds;
873
874 if (!ds->ops->phylink_mac_config)
875 return;
876
877 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
878}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300879
Florian Fainelli8ae67492019-12-16 10:32:47 -0800880static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300881{
882 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
883 struct dsa_switch *ds = dp->ds;
884
885 if (!ds->ops->phylink_mac_an_restart)
886 return;
887
888 ds->ops->phylink_mac_an_restart(ds, dp->index);
889}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300890
Florian Fainelli8ae67492019-12-16 10:32:47 -0800891static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
892 unsigned int mode,
893 phy_interface_t interface)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300894{
895 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
Ioana Ciornei0e279212019-05-28 20:38:16 +0300896 struct phy_device *phydev = NULL;
Ioana Ciornei77373d42019-05-28 20:38:15 +0300897 struct dsa_switch *ds = dp->ds;
898
Ioana Ciornei0e279212019-05-28 20:38:16 +0300899 if (dsa_is_user_port(ds, dp->index))
900 phydev = dp->slave->phydev;
901
Ioana Ciornei77373d42019-05-28 20:38:15 +0300902 if (!ds->ops->phylink_mac_link_down) {
Ioana Ciornei0e279212019-05-28 20:38:16 +0300903 if (ds->ops->adjust_link && phydev)
904 ds->ops->adjust_link(ds, dp->index, phydev);
Ioana Ciornei77373d42019-05-28 20:38:15 +0300905 return;
906 }
907
908 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
909}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300910
Florian Fainelli8ae67492019-12-16 10:32:47 -0800911static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
Russell King91a208f2020-02-26 10:23:41 +0000912 struct phy_device *phydev,
Florian Fainelli8ae67492019-12-16 10:32:47 -0800913 unsigned int mode,
914 phy_interface_t interface,
Russell King91a208f2020-02-26 10:23:41 +0000915 int speed, int duplex,
916 bool tx_pause, bool rx_pause)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300917{
918 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
Ioana Ciornei77373d42019-05-28 20:38:15 +0300919 struct dsa_switch *ds = dp->ds;
920
921 if (!ds->ops->phylink_mac_link_up) {
Ioana Ciornei0e279212019-05-28 20:38:16 +0300922 if (ds->ops->adjust_link && phydev)
923 ds->ops->adjust_link(ds, dp->index, phydev);
Ioana Ciornei77373d42019-05-28 20:38:15 +0300924 return;
925 }
926
Russell King5b502a72020-02-26 10:23:46 +0000927 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
928 speed, duplex, tx_pause, rx_pause);
Ioana Ciornei77373d42019-05-28 20:38:15 +0300929}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300930
931const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
932 .validate = dsa_port_phylink_validate,
Russell Kingd46b7e42019-11-21 00:36:22 +0000933 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
Ioana Ciornei77373d42019-05-28 20:38:15 +0300934 .mac_config = dsa_port_phylink_mac_config,
935 .mac_an_restart = dsa_port_phylink_mac_an_restart,
936 .mac_link_down = dsa_port_phylink_mac_link_down,
937 .mac_link_up = dsa_port_phylink_mac_link_up,
938};
939
Sebastian Reichel33615362018-01-23 16:03:46 +0100940static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
941{
Sebastian Reichel33615362018-01-23 16:03:46 +0100942 struct dsa_switch *ds = dp->ds;
943 struct phy_device *phydev;
944 int port = dp->index;
945 int err = 0;
946
Florian Fainelli6207a782018-04-25 12:12:51 -0700947 phydev = dsa_port_get_phy_device(dp);
948 if (!phydev)
Sebastian Reichel33615362018-01-23 16:03:46 +0100949 return 0;
950
Florian Fainelli6207a782018-04-25 12:12:51 -0700951 if (IS_ERR(phydev))
952 return PTR_ERR(phydev);
Sebastian Reichel33615362018-01-23 16:03:46 +0100953
954 if (enable) {
Sebastian Reichel33615362018-01-23 16:03:46 +0100955 err = genphy_resume(phydev);
956 if (err < 0)
957 goto err_put_dev;
958
959 err = genphy_read_status(phydev);
960 if (err < 0)
961 goto err_put_dev;
962 } else {
963 err = genphy_suspend(phydev);
964 if (err < 0)
965 goto err_put_dev;
966 }
967
968 if (ds->ops->adjust_link)
969 ds->ops->adjust_link(ds, port, phydev);
970
971 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
972
973err_put_dev:
974 put_device(&phydev->mdio.dev);
Sebastian Reichel33615362018-01-23 16:03:46 +0100975 return err;
976}
977
978static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400979{
980 struct device_node *dn = dp->dn;
981 struct dsa_switch *ds = dp->ds;
982 struct phy_device *phydev;
983 int port = dp->index;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100984 phy_interface_t mode;
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400985 int err;
986
Sebastian Reichel33615362018-01-23 16:03:46 +0100987 err = of_phy_register_fixed_link(dn);
988 if (err) {
989 dev_err(ds->dev,
990 "failed to register the fixed PHY of port %d\n",
991 port);
992 return err;
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400993 }
994
Sebastian Reichel33615362018-01-23 16:03:46 +0100995 phydev = of_phy_find_device(dn);
996
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100997 err = of_get_phy_mode(dn, &mode);
998 if (err)
Sebastian Reichel33615362018-01-23 16:03:46 +0100999 mode = PHY_INTERFACE_MODE_NA;
1000 phydev->interface = mode;
1001
Sebastian Reichel33615362018-01-23 16:03:46 +01001002 genphy_read_status(phydev);
1003
1004 if (ds->ops->adjust_link)
1005 ds->ops->adjust_link(ds, port, phydev);
1006
1007 put_device(&phydev->mdio.dev);
1008
Vivien Didelot57ab1ca2017-10-26 10:50:07 -04001009 return 0;
1010}
1011
Ioana Ciornei0e279212019-05-28 20:38:16 +03001012static int dsa_port_phylink_register(struct dsa_port *dp)
1013{
1014 struct dsa_switch *ds = dp->ds;
1015 struct device_node *port_dn = dp->dn;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001016 phy_interface_t mode;
1017 int err;
Ioana Ciornei0e279212019-05-28 20:38:16 +03001018
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001019 err = of_get_phy_mode(port_dn, &mode);
1020 if (err)
Ioana Ciornei0e279212019-05-28 20:38:16 +03001021 mode = PHY_INTERFACE_MODE_NA;
1022
1023 dp->pl_config.dev = ds->dev;
1024 dp->pl_config.type = PHYLINK_DEV;
Vladimir Oltean787cac32020-01-06 03:34:12 +02001025 dp->pl_config.pcs_poll = ds->pcs_poll;
Ioana Ciornei0e279212019-05-28 20:38:16 +03001026
1027 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
1028 mode, &dsa_port_phylink_mac_ops);
1029 if (IS_ERR(dp->pl)) {
1030 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1031 return PTR_ERR(dp->pl);
1032 }
1033
1034 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
Florian Fainelli2131fba2019-06-10 12:31:49 -07001035 if (err && err != -ENODEV) {
Ioana Ciornei0e279212019-05-28 20:38:16 +03001036 pr_err("could not attach to PHY: %d\n", err);
1037 goto err_phy_connect;
1038 }
1039
Ioana Ciornei0e279212019-05-28 20:38:16 +03001040 return 0;
1041
1042err_phy_connect:
1043 phylink_destroy(dp->pl);
1044 return err;
1045}
1046
Sebastian Reichel33615362018-01-23 16:03:46 +01001047int dsa_port_link_register_of(struct dsa_port *dp)
Vivien Didelot57ab1ca2017-10-26 10:50:07 -04001048{
Ioana Ciornei0e279212019-05-28 20:38:16 +03001049 struct dsa_switch *ds = dp->ds;
Andrew Lunna20f9972020-03-11 16:24:24 +01001050 struct device_node *phy_np;
Andrew Lunn3be98b22020-04-14 02:34:39 +02001051 int port = dp->index;
Ioana Ciornei0e279212019-05-28 20:38:16 +03001052
Andrew Lunna20f9972020-03-11 16:24:24 +01001053 if (!ds->ops->adjust_link) {
1054 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
Andrew Lunn3be98b22020-04-14 02:34:39 +02001055 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1056 if (ds->ops->phylink_mac_link_down)
1057 ds->ops->phylink_mac_link_down(ds, port,
1058 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
Andrew Lunna20f9972020-03-11 16:24:24 +01001059 return dsa_port_phylink_register(dp);
Andrew Lunn3be98b22020-04-14 02:34:39 +02001060 }
Andrew Lunna20f9972020-03-11 16:24:24 +01001061 return 0;
1062 }
Ioana Ciornei0e279212019-05-28 20:38:16 +03001063
1064 dev_warn(ds->dev,
1065 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1066
Sebastian Reichel33615362018-01-23 16:03:46 +01001067 if (of_phy_is_fixed_link(dp->dn))
1068 return dsa_port_fixed_link_register_of(dp);
1069 else
1070 return dsa_port_setup_phy_of(dp, true);
1071}
Vivien Didelot57ab1ca2017-10-26 10:50:07 -04001072
Sebastian Reichel33615362018-01-23 16:03:46 +01001073void dsa_port_link_unregister_of(struct dsa_port *dp)
1074{
Ioana Ciornei0e279212019-05-28 20:38:16 +03001075 struct dsa_switch *ds = dp->ds;
1076
Andrew Lunna20f9972020-03-11 16:24:24 +01001077 if (!ds->ops->adjust_link && dp->pl) {
Ioana Ciornei0e279212019-05-28 20:38:16 +03001078 rtnl_lock();
1079 phylink_disconnect_phy(dp->pl);
1080 rtnl_unlock();
1081 phylink_destroy(dp->pl);
Andrew Lunna20f9972020-03-11 16:24:24 +01001082 dp->pl = NULL;
Ioana Ciornei0e279212019-05-28 20:38:16 +03001083 return;
1084 }
1085
Sebastian Reichel33615362018-01-23 16:03:46 +01001086 if (of_phy_is_fixed_link(dp->dn))
1087 of_phy_deregister_fixed_link(dp->dn);
1088 else
1089 dsa_port_setup_phy_of(dp, false);
Vivien Didelot57ab1ca2017-10-26 10:50:07 -04001090}
Florian Fainellicf963572018-04-25 12:12:52 -07001091
1092int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
1093{
1094 struct phy_device *phydev;
1095 int ret = -EOPNOTSUPP;
1096
1097 if (of_phy_is_fixed_link(dp->dn))
1098 return ret;
1099
1100 phydev = dsa_port_get_phy_device(dp);
1101 if (IS_ERR_OR_NULL(phydev))
1102 return ret;
1103
1104 ret = phy_ethtool_get_strings(phydev, data);
1105 put_device(&phydev->mdio.dev);
1106
1107 return ret;
1108}
1109EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
1110
1111int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
1112{
1113 struct phy_device *phydev;
1114 int ret = -EOPNOTSUPP;
1115
1116 if (of_phy_is_fixed_link(dp->dn))
1117 return ret;
1118
1119 phydev = dsa_port_get_phy_device(dp);
1120 if (IS_ERR_OR_NULL(phydev))
1121 return ret;
1122
1123 ret = phy_ethtool_get_stats(phydev, NULL, data);
1124 put_device(&phydev->mdio.dev);
1125
1126 return ret;
1127}
1128EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
1129
1130int dsa_port_get_phy_sset_count(struct dsa_port *dp)
1131{
1132 struct phy_device *phydev;
1133 int ret = -EOPNOTSUPP;
1134
1135 if (of_phy_is_fixed_link(dp->dn))
1136 return ret;
1137
1138 phydev = dsa_port_get_phy_device(dp);
1139 if (IS_ERR_OR_NULL(phydev))
1140 return ret;
1141
1142 ret = phy_ethtool_get_sset_count(phydev);
1143 put_device(&phydev->mdio.dev);
1144
1145 return ret;
1146}
1147EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
George McCollister18596f52021-02-09 19:02:12 -06001148
1149int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1150{
1151 struct dsa_notifier_hsr_info info = {
1152 .sw_index = dp->ds->index,
1153 .port = dp->index,
1154 .hsr = hsr,
1155 };
1156 int err;
1157
1158 dp->hsr_dev = hsr;
1159
1160 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
1161 if (err)
1162 dp->hsr_dev = NULL;
1163
1164 return err;
1165}
1166
1167void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1168{
1169 struct dsa_notifier_hsr_info info = {
1170 .sw_index = dp->ds->index,
1171 .port = dp->index,
1172 .hsr = hsr,
1173 };
1174 int err;
1175
1176 dp->hsr_dev = NULL;
1177
1178 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
1179 if (err)
1180 pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n");
1181}
Vladimir Olteanc64b9c02021-07-19 20:14:52 +03001182
1183int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid)
1184{
1185 struct dsa_notifier_tag_8021q_vlan_info info = {
1186 .tree_index = dp->ds->dst->index,
1187 .sw_index = dp->ds->index,
1188 .port = dp->index,
1189 .vid = vid,
1190 };
1191
1192 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1193}
1194
1195void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid)
1196{
1197 struct dsa_notifier_tag_8021q_vlan_info info = {
1198 .tree_index = dp->ds->dst->index,
1199 .sw_index = dp->ds->index,
1200 .port = dp->index,
1201 .vid = vid,
1202 };
1203 int err;
1204
1205 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1206 if (err)
1207 pr_err("DSA: failed to notify tag_8021q VLAN deletion: %pe\n",
1208 ERR_PTR(err));
1209}