Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / dsa / port.c
blob363eab6df51b4c44e90dcc7de87f48945d3e1d46
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Handling of a single switch port
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 */
9 #include <linux/if_bridge.h>
10 #include <linux/notifier.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_net.h>
14 #include "dsa_priv.h"
16 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
18 struct raw_notifier_head *nh = &dp->ds->dst->nh;
19 int err;
21 err = raw_notifier_call_chain(nh, e, v);
23 return notifier_to_errno(err);
26 int dsa_port_set_state(struct dsa_port *dp, u8 state,
27 struct switchdev_trans *trans)
29 struct dsa_switch *ds = dp->ds;
30 int port = dp->index;
32 if (switchdev_trans_ph_prepare(trans))
33 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
35 if (ds->ops->port_stp_state_set)
36 ds->ops->port_stp_state_set(ds, port, state);
38 if (ds->ops->port_fast_age) {
39 /* Fast age FDB entries or flush appropriate forwarding database
40 * for the given port, if we are moving it from Learning or
41 * Forwarding state, to Disabled or Blocking or Listening state.
44 if ((dp->stp_state == BR_STATE_LEARNING ||
45 dp->stp_state == BR_STATE_FORWARDING) &&
46 (state == BR_STATE_DISABLED ||
47 state == BR_STATE_BLOCKING ||
48 state == BR_STATE_LISTENING))
49 ds->ops->port_fast_age(ds, port);
52 dp->stp_state = state;
54 return 0;
57 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
59 int err;
61 err = dsa_port_set_state(dp, state, NULL);
62 if (err)
63 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
66 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
68 struct dsa_switch *ds = dp->ds;
69 int port = dp->index;
70 int err;
72 if (ds->ops->port_enable) {
73 err = ds->ops->port_enable(ds, port, phy);
74 if (err)
75 return err;
78 if (!dp->bridge_dev)
79 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
81 return 0;
84 void dsa_port_disable(struct dsa_port *dp)
86 struct dsa_switch *ds = dp->ds;
87 int port = dp->index;
89 if (!dp->bridge_dev)
90 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
92 if (ds->ops->port_disable)
93 ds->ops->port_disable(ds, port);
96 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
98 struct dsa_notifier_bridge_info info = {
99 .sw_index = dp->ds->index,
100 .port = dp->index,
101 .br = br,
103 int err;
105 /* Set the flooding mode before joining the port in the switch */
106 err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL);
107 if (err)
108 return err;
110 /* Here the interface is already bridged. Reflect the current
111 * configuration so that drivers can program their chips accordingly.
113 dp->bridge_dev = br;
115 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
117 /* The bridging is rolled back on error */
118 if (err) {
119 dsa_port_bridge_flags(dp, 0, NULL);
120 dp->bridge_dev = NULL;
123 return err;
126 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
128 struct dsa_notifier_bridge_info info = {
129 .sw_index = dp->ds->index,
130 .port = dp->index,
131 .br = br,
133 int err;
135 /* Here the port is already unbridged. Reflect the current configuration
136 * so that drivers can program their chips accordingly.
138 dp->bridge_dev = NULL;
140 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
141 if (err)
142 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
144 /* Port is leaving the bridge, disable flooding */
145 dsa_port_bridge_flags(dp, 0, NULL);
147 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
148 * so allow it to be in BR_STATE_FORWARDING to be kept functional
150 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
153 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
154 bool vlan_filtering)
156 struct dsa_switch *ds = dp->ds;
157 int i;
159 if (!ds->vlan_filtering_is_global)
160 return true;
162 /* For cases where enabling/disabling VLAN awareness is global to the
163 * switch, we need to handle the case where multiple bridges span
164 * different ports of the same switch device and one of them has a
165 * different setting than what is being requested.
167 for (i = 0; i < ds->num_ports; i++) {
168 struct net_device *other_bridge;
170 other_bridge = dsa_to_port(ds, i)->bridge_dev;
171 if (!other_bridge)
172 continue;
173 /* If it's the same bridge, it also has same
174 * vlan_filtering setting => no need to check
176 if (other_bridge == dp->bridge_dev)
177 continue;
178 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
179 dev_err(ds->dev, "VLAN filtering is a global setting\n");
180 return false;
183 return true;
186 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
187 struct switchdev_trans *trans)
189 struct dsa_switch *ds = dp->ds;
190 int err;
192 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
193 if (switchdev_trans_ph_prepare(trans))
194 return 0;
196 if (!ds->ops->port_vlan_filtering)
197 return 0;
199 if (!dsa_port_can_apply_vlan_filtering(dp, vlan_filtering))
200 return -EINVAL;
202 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
203 return 0;
205 err = ds->ops->port_vlan_filtering(ds, dp->index,
206 vlan_filtering);
207 if (err)
208 return err;
210 if (ds->vlan_filtering_is_global)
211 ds->vlan_filtering = vlan_filtering;
212 else
213 dp->vlan_filtering = vlan_filtering;
214 return 0;
217 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
218 struct switchdev_trans *trans)
220 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
221 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
222 struct dsa_notifier_ageing_time_info info = {
223 .ageing_time = ageing_time,
224 .trans = trans,
227 if (switchdev_trans_ph_prepare(trans))
228 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
230 dp->ageing_time = ageing_time;
232 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
235 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags,
236 struct switchdev_trans *trans)
238 struct dsa_switch *ds = dp->ds;
240 if (!ds->ops->port_egress_floods ||
241 (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
242 return -EINVAL;
244 return 0;
247 int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags,
248 struct switchdev_trans *trans)
250 struct dsa_switch *ds = dp->ds;
251 int port = dp->index;
252 int err = 0;
254 if (switchdev_trans_ph_prepare(trans))
255 return 0;
257 if (ds->ops->port_egress_floods)
258 err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
259 flags & BR_MCAST_FLOOD);
261 return err;
264 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
265 u16 vid)
267 struct dsa_notifier_fdb_info info = {
268 .sw_index = dp->ds->index,
269 .port = dp->index,
270 .addr = addr,
271 .vid = vid,
274 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
277 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
278 u16 vid)
280 struct dsa_notifier_fdb_info info = {
281 .sw_index = dp->ds->index,
282 .port = dp->index,
283 .addr = addr,
284 .vid = vid,
288 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
291 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
293 struct dsa_switch *ds = dp->ds;
294 int port = dp->index;
296 if (!ds->ops->port_fdb_dump)
297 return -EOPNOTSUPP;
299 return ds->ops->port_fdb_dump(ds, port, cb, data);
302 int dsa_port_mdb_add(const struct dsa_port *dp,
303 const struct switchdev_obj_port_mdb *mdb,
304 struct switchdev_trans *trans)
306 struct dsa_notifier_mdb_info info = {
307 .sw_index = dp->ds->index,
308 .port = dp->index,
309 .trans = trans,
310 .mdb = mdb,
313 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
316 int dsa_port_mdb_del(const struct dsa_port *dp,
317 const struct switchdev_obj_port_mdb *mdb)
319 struct dsa_notifier_mdb_info info = {
320 .sw_index = dp->ds->index,
321 .port = dp->index,
322 .mdb = mdb,
325 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
328 int dsa_port_vlan_add(struct dsa_port *dp,
329 const struct switchdev_obj_port_vlan *vlan,
330 struct switchdev_trans *trans)
332 struct dsa_notifier_vlan_info info = {
333 .sw_index = dp->ds->index,
334 .port = dp->index,
335 .trans = trans,
336 .vlan = vlan,
339 /* Can be called from dsa_slave_port_obj_add() or
340 * dsa_slave_vlan_rx_add_vid()
342 if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev))
343 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
345 return 0;
348 int dsa_port_vlan_del(struct dsa_port *dp,
349 const struct switchdev_obj_port_vlan *vlan)
351 struct dsa_notifier_vlan_info info = {
352 .sw_index = dp->ds->index,
353 .port = dp->index,
354 .vlan = vlan,
357 if (vlan->obj.orig_dev && netif_is_bridge_master(vlan->obj.orig_dev))
358 return -EOPNOTSUPP;
360 /* Can be called from dsa_slave_port_obj_del() or
361 * dsa_slave_vlan_rx_kill_vid()
363 if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev))
364 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
366 return 0;
369 int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags)
371 struct switchdev_obj_port_vlan vlan = {
372 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
373 .flags = flags,
374 .vid_begin = vid,
375 .vid_end = vid,
377 struct switchdev_trans trans;
378 int err;
380 trans.ph_prepare = true;
381 err = dsa_port_vlan_add(dp, &vlan, &trans);
382 if (err == -EOPNOTSUPP)
383 return 0;
385 trans.ph_prepare = false;
386 return dsa_port_vlan_add(dp, &vlan, &trans);
388 EXPORT_SYMBOL(dsa_port_vid_add);
390 int dsa_port_vid_del(struct dsa_port *dp, u16 vid)
392 struct switchdev_obj_port_vlan vlan = {
393 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
394 .vid_begin = vid,
395 .vid_end = vid,
398 return dsa_port_vlan_del(dp, &vlan);
400 EXPORT_SYMBOL(dsa_port_vid_del);
402 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
404 struct device_node *phy_dn;
405 struct phy_device *phydev;
407 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
408 if (!phy_dn)
409 return NULL;
411 phydev = of_phy_find_device(phy_dn);
412 if (!phydev) {
413 of_node_put(phy_dn);
414 return ERR_PTR(-EPROBE_DEFER);
417 of_node_put(phy_dn);
418 return phydev;
421 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
423 struct dsa_switch *ds = dp->ds;
424 struct phy_device *phydev;
425 int port = dp->index;
426 int err = 0;
428 phydev = dsa_port_get_phy_device(dp);
429 if (!phydev)
430 return 0;
432 if (IS_ERR(phydev))
433 return PTR_ERR(phydev);
435 if (enable) {
436 err = genphy_config_init(phydev);
437 if (err < 0)
438 goto err_put_dev;
440 err = genphy_resume(phydev);
441 if (err < 0)
442 goto err_put_dev;
444 err = genphy_read_status(phydev);
445 if (err < 0)
446 goto err_put_dev;
447 } else {
448 err = genphy_suspend(phydev);
449 if (err < 0)
450 goto err_put_dev;
453 if (ds->ops->adjust_link)
454 ds->ops->adjust_link(ds, port, phydev);
456 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
458 err_put_dev:
459 put_device(&phydev->mdio.dev);
460 return err;
463 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
465 struct device_node *dn = dp->dn;
466 struct dsa_switch *ds = dp->ds;
467 struct phy_device *phydev;
468 int port = dp->index;
469 int mode;
470 int err;
472 err = of_phy_register_fixed_link(dn);
473 if (err) {
474 dev_err(ds->dev,
475 "failed to register the fixed PHY of port %d\n",
476 port);
477 return err;
480 phydev = of_phy_find_device(dn);
482 mode = of_get_phy_mode(dn);
483 if (mode < 0)
484 mode = PHY_INTERFACE_MODE_NA;
485 phydev->interface = mode;
487 genphy_config_init(phydev);
488 genphy_read_status(phydev);
490 if (ds->ops->adjust_link)
491 ds->ops->adjust_link(ds, port, phydev);
493 put_device(&phydev->mdio.dev);
495 return 0;
498 int dsa_port_link_register_of(struct dsa_port *dp)
500 if (of_phy_is_fixed_link(dp->dn))
501 return dsa_port_fixed_link_register_of(dp);
502 else
503 return dsa_port_setup_phy_of(dp, true);
506 void dsa_port_link_unregister_of(struct dsa_port *dp)
508 if (of_phy_is_fixed_link(dp->dn))
509 of_phy_deregister_fixed_link(dp->dn);
510 else
511 dsa_port_setup_phy_of(dp, false);
514 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
516 struct phy_device *phydev;
517 int ret = -EOPNOTSUPP;
519 if (of_phy_is_fixed_link(dp->dn))
520 return ret;
522 phydev = dsa_port_get_phy_device(dp);
523 if (IS_ERR_OR_NULL(phydev))
524 return ret;
526 ret = phy_ethtool_get_strings(phydev, data);
527 put_device(&phydev->mdio.dev);
529 return ret;
531 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
533 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
535 struct phy_device *phydev;
536 int ret = -EOPNOTSUPP;
538 if (of_phy_is_fixed_link(dp->dn))
539 return ret;
541 phydev = dsa_port_get_phy_device(dp);
542 if (IS_ERR_OR_NULL(phydev))
543 return ret;
545 ret = phy_ethtool_get_stats(phydev, NULL, data);
546 put_device(&phydev->mdio.dev);
548 return ret;
550 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
552 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
554 struct phy_device *phydev;
555 int ret = -EOPNOTSUPP;
557 if (of_phy_is_fixed_link(dp->dn))
558 return ret;
560 phydev = dsa_port_get_phy_device(dp);
561 if (IS_ERR_OR_NULL(phydev))
562 return ret;
564 ret = phy_ethtool_get_sset_count(phydev);
565 put_device(&phydev->mdio.dev);
567 return ret;
569 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);