2 * Handling of a single switch port
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
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.
13 #include <linux/if_bridge.h>
14 #include <linux/notifier.h>
15 #include <linux/of_mdio.h>
16 #include <linux/of_net.h>
20 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
22 struct raw_notifier_head *nh = &dp->ds->dst->nh;
25 err = raw_notifier_call_chain(nh, e, v);
27 return notifier_to_errno(err);
30 int dsa_port_set_state(struct dsa_port *dp, u8 state,
31 struct switchdev_trans *trans)
33 struct dsa_switch *ds = dp->ds;
36 if (switchdev_trans_ph_prepare(trans))
37 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
39 if (ds->ops->port_stp_state_set)
40 ds->ops->port_stp_state_set(ds, port, state);
42 if (ds->ops->port_fast_age) {
43 /* Fast age FDB entries or flush appropriate forwarding database
44 * for the given port, if we are moving it from Learning or
45 * Forwarding state, to Disabled or Blocking or Listening state.
48 if ((dp->stp_state == BR_STATE_LEARNING ||
49 dp->stp_state == BR_STATE_FORWARDING) &&
50 (state == BR_STATE_DISABLED ||
51 state == BR_STATE_BLOCKING ||
52 state == BR_STATE_LISTENING))
53 ds->ops->port_fast_age(ds, port);
56 dp->stp_state = state;
61 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
65 err = dsa_port_set_state(dp, state, NULL);
67 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
70 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
72 struct dsa_switch *ds = dp->ds;
76 if (ds->ops->port_enable) {
77 err = ds->ops->port_enable(ds, port, phy);
83 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
88 void dsa_port_disable(struct dsa_port *dp)
90 struct dsa_switch *ds = dp->ds;
94 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
96 if (ds->ops->port_disable)
97 ds->ops->port_disable(ds, port);
100 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
102 struct dsa_notifier_bridge_info info = {
103 .sw_index = dp->ds->index,
109 /* Set the flooding mode before joining the port in the switch */
110 err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL);
114 /* Here the interface is already bridged. Reflect the current
115 * configuration so that drivers can program their chips accordingly.
119 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
121 /* The bridging is rolled back on error */
123 dsa_port_bridge_flags(dp, 0, NULL);
124 dp->bridge_dev = NULL;
130 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
132 struct dsa_notifier_bridge_info info = {
133 .sw_index = dp->ds->index,
139 /* Here the port is already unbridged. Reflect the current configuration
140 * so that drivers can program their chips accordingly.
142 dp->bridge_dev = NULL;
144 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
146 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
148 /* Port is leaving the bridge, disable flooding */
149 dsa_port_bridge_flags(dp, 0, NULL);
151 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
152 * so allow it to be in BR_STATE_FORWARDING to be kept functional
154 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
157 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
158 struct switchdev_trans *trans)
160 struct dsa_switch *ds = dp->ds;
162 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
163 if (switchdev_trans_ph_prepare(trans))
166 if (ds->ops->port_vlan_filtering)
167 return ds->ops->port_vlan_filtering(ds, dp->index,
173 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
174 struct switchdev_trans *trans)
176 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
177 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
178 struct dsa_notifier_ageing_time_info info = {
179 .ageing_time = ageing_time,
183 if (switchdev_trans_ph_prepare(trans))
184 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
186 dp->ageing_time = ageing_time;
188 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
191 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags,
192 struct switchdev_trans *trans)
194 struct dsa_switch *ds = dp->ds;
196 if (!ds->ops->port_egress_floods ||
197 (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
203 int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags,
204 struct switchdev_trans *trans)
206 struct dsa_switch *ds = dp->ds;
207 int port = dp->index;
210 if (switchdev_trans_ph_prepare(trans))
213 if (ds->ops->port_egress_floods)
214 err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
215 flags & BR_MCAST_FLOOD);
220 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
223 struct dsa_notifier_fdb_info info = {
224 .sw_index = dp->ds->index,
230 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
233 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
236 struct dsa_notifier_fdb_info info = {
237 .sw_index = dp->ds->index,
244 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
247 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
249 struct dsa_switch *ds = dp->ds;
250 int port = dp->index;
252 if (!ds->ops->port_fdb_dump)
255 return ds->ops->port_fdb_dump(ds, port, cb, data);
258 int dsa_port_mdb_add(const struct dsa_port *dp,
259 const struct switchdev_obj_port_mdb *mdb,
260 struct switchdev_trans *trans)
262 struct dsa_notifier_mdb_info info = {
263 .sw_index = dp->ds->index,
269 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
272 int dsa_port_mdb_del(const struct dsa_port *dp,
273 const struct switchdev_obj_port_mdb *mdb)
275 struct dsa_notifier_mdb_info info = {
276 .sw_index = dp->ds->index,
281 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
284 int dsa_port_vlan_add(struct dsa_port *dp,
285 const struct switchdev_obj_port_vlan *vlan,
286 struct switchdev_trans *trans)
288 struct dsa_notifier_vlan_info info = {
289 .sw_index = dp->ds->index,
295 /* Can be called from dsa_slave_port_obj_add() or
296 * dsa_slave_vlan_rx_add_vid()
298 if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev))
299 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
304 int dsa_port_vlan_del(struct dsa_port *dp,
305 const struct switchdev_obj_port_vlan *vlan)
307 struct dsa_notifier_vlan_info info = {
308 .sw_index = dp->ds->index,
313 if (vlan->obj.orig_dev && netif_is_bridge_master(vlan->obj.orig_dev))
316 /* Can be called from dsa_slave_port_obj_del() or
317 * dsa_slave_vlan_rx_kill_vid()
319 if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev))
320 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
325 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
327 struct device_node *phy_dn;
328 struct phy_device *phydev;
330 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
334 phydev = of_phy_find_device(phy_dn);
337 return ERR_PTR(-EPROBE_DEFER);
344 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
346 struct dsa_switch *ds = dp->ds;
347 struct phy_device *phydev;
348 int port = dp->index;
351 phydev = dsa_port_get_phy_device(dp);
356 return PTR_ERR(phydev);
359 err = genphy_config_init(phydev);
363 err = genphy_resume(phydev);
367 err = genphy_read_status(phydev);
371 err = genphy_suspend(phydev);
376 if (ds->ops->adjust_link)
377 ds->ops->adjust_link(ds, port, phydev);
379 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
382 put_device(&phydev->mdio.dev);
386 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
388 struct device_node *dn = dp->dn;
389 struct dsa_switch *ds = dp->ds;
390 struct phy_device *phydev;
391 int port = dp->index;
395 err = of_phy_register_fixed_link(dn);
398 "failed to register the fixed PHY of port %d\n",
403 phydev = of_phy_find_device(dn);
405 mode = of_get_phy_mode(dn);
407 mode = PHY_INTERFACE_MODE_NA;
408 phydev->interface = mode;
410 genphy_config_init(phydev);
411 genphy_read_status(phydev);
413 if (ds->ops->adjust_link)
414 ds->ops->adjust_link(ds, port, phydev);
416 put_device(&phydev->mdio.dev);
421 int dsa_port_link_register_of(struct dsa_port *dp)
423 if (of_phy_is_fixed_link(dp->dn))
424 return dsa_port_fixed_link_register_of(dp);
426 return dsa_port_setup_phy_of(dp, true);
429 void dsa_port_link_unregister_of(struct dsa_port *dp)
431 if (of_phy_is_fixed_link(dp->dn))
432 of_phy_deregister_fixed_link(dp->dn);
434 dsa_port_setup_phy_of(dp, false);
437 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
439 struct phy_device *phydev;
440 int ret = -EOPNOTSUPP;
442 if (of_phy_is_fixed_link(dp->dn))
445 phydev = dsa_port_get_phy_device(dp);
446 if (IS_ERR_OR_NULL(phydev))
449 ret = phy_ethtool_get_strings(phydev, data);
450 put_device(&phydev->mdio.dev);
454 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
456 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
458 struct phy_device *phydev;
459 int ret = -EOPNOTSUPP;
461 if (of_phy_is_fixed_link(dp->dn))
464 phydev = dsa_port_get_phy_device(dp);
465 if (IS_ERR_OR_NULL(phydev))
468 ret = phy_ethtool_get_stats(phydev, NULL, data);
469 put_device(&phydev->mdio.dev);
473 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
475 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
477 struct phy_device *phydev;
478 int ret = -EOPNOTSUPP;
480 if (of_phy_is_fixed_link(dp->dn))
483 phydev = dsa_port_get_phy_device(dp);
484 if (IS_ERR_OR_NULL(phydev))
487 ret = phy_ethtool_get_sset_count(phydev);
488 put_device(&phydev->mdio.dev);
492 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);