Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Handling of a single switch port |
| 4 | * |
| 5 | * Copyright (c) 2017 Savoir-faire Linux Inc. |
| 6 | * Vivien Didelot <vivien.didelot@savoirfairelinux.com> |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/if_bridge.h> |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 10 | #include <linux/notifier.h> |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 11 | #include <linux/of_mdio.h> |
| 12 | #include <linux/of_net.h> |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 13 | |
| 14 | #include "dsa_priv.h" |
| 15 | |
Vladimir Oltean | 886f8e2 | 2021-01-29 03:00:04 +0200 | [diff] [blame] | 16 | /** |
| 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 Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 28 | static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v) |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 29 | { |
Vladimir Oltean | 886f8e2 | 2021-01-29 03:00:04 +0200 | [diff] [blame] | 30 | return dsa_tree_notify(dp->ds->dst, e, v); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 31 | } |
| 32 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 33 | int dsa_port_set_state(struct dsa_port *dp, u8 state) |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 34 | { |
| 35 | struct dsa_switch *ds = dp->ds; |
| 36 | int port = dp->index; |
| 37 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 38 | if (!ds->ops->port_stp_state_set) |
| 39 | return -EOPNOTSUPP; |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 40 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 41 | ds->ops->port_stp_state_set(ds, port, state); |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 42 | |
| 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 Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 62 | static void dsa_port_set_state_now(struct dsa_port *dp, u8 state) |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 63 | { |
| 64 | int err; |
| 65 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 66 | err = dsa_port_set_state(dp, state); |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 67 | if (err) |
| 68 | pr_err("DSA: failed to set STP state %u (%d)\n", state, err); |
| 69 | } |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 70 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 71 | int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy) |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 72 | { |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 73 | 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 King | 9c2054a | 2019-02-20 10:32:52 +0000 | [diff] [blame] | 83 | if (!dp->bridge_dev) |
| 84 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 85 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 86 | if (dp->pl) |
| 87 | phylink_start(dp->pl); |
| 88 | |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 92 | int 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 | |
| 103 | void dsa_port_disable_rt(struct dsa_port *dp) |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 104 | { |
| 105 | struct dsa_switch *ds = dp->ds; |
| 106 | int port = dp->index; |
| 107 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 108 | if (dp->pl) |
| 109 | phylink_stop(dp->pl); |
| 110 | |
Russell King | 9c2054a | 2019-02-20 10:32:52 +0000 | [diff] [blame] | 111 | if (!dp->bridge_dev) |
| 112 | dsa_port_set_state_now(dp, BR_STATE_DISABLED); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 113 | |
| 114 | if (ds->ops->port_disable) |
Andrew Lunn | 75104db | 2019-02-24 20:44:43 +0100 | [diff] [blame] | 115 | ds->ops->port_disable(ds, port); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 118 | void dsa_port_disable(struct dsa_port *dp) |
| 119 | { |
| 120 | rtnl_lock(); |
| 121 | dsa_port_disable_rt(dp); |
| 122 | rtnl_unlock(); |
| 123 | } |
| 124 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 125 | static int dsa_port_inherit_brport_flags(struct dsa_port *dp, |
| 126 | struct netlink_ext_ack *extack) |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 127 | { |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 128 | 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 Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 132 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 133 | for_each_set_bit(flag, &mask, 32) { |
| 134 | struct switchdev_brport_flags flags = {0}; |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 135 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 136 | flags.mask = BIT(flag); |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 137 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 138 | if (br_port_flag_is_set(brport_dev, BIT(flag))) |
| 139 | flags.val = BIT(flag); |
Vladimir Oltean | e18f4c1 | 2021-02-12 17:15:55 +0200 | [diff] [blame] | 140 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 141 | err = dsa_port_bridge_flags(dp, flags, extack); |
| 142 | if (err && err != -EOPNOTSUPP) |
| 143 | return err; |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 144 | } |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static 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 | |
| 170 | static int dsa_port_switchdev_sync(struct dsa_port *dp, |
| 171 | struct netlink_ext_ack *extack) |
| 172 | { |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 173 | struct net_device *brport_dev = dsa_port_to_bridge_port(dp); |
| 174 | struct net_device *br = dp->bridge_dev; |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 175 | int err; |
| 176 | |
| 177 | err = dsa_port_inherit_brport_flags(dp, extack); |
| 178 | if (err) |
| 179 | return err; |
| 180 | |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 181 | 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 Oltean | 7e8c185 | 2021-06-27 14:54:27 +0300 | [diff] [blame] | 197 | err = br_mdb_replay(br, brport_dev, dp, true, |
| 198 | &dsa_slave_switchdev_blocking_notifier, extack); |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 199 | if (err && err != -EOPNOTSUPP) |
| 200 | return err; |
| 201 | |
Vladimir Oltean | 7e8c185 | 2021-06-27 14:54:27 +0300 | [diff] [blame] | 202 | err = br_fdb_replay(br, brport_dev, dp, true, |
| 203 | &dsa_slave_switchdev_notifier); |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 204 | if (err && err != -EOPNOTSUPP) |
| 205 | return err; |
| 206 | |
Vladimir Oltean | 7e8c185 | 2021-06-27 14:54:27 +0300 | [diff] [blame] | 207 | err = br_vlan_replay(br, brport_dev, dp, true, |
| 208 | &dsa_slave_switchdev_blocking_notifier, extack); |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 209 | if (err && err != -EOPNOTSUPP) |
| 210 | return err; |
| 211 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 215 | static int dsa_port_switchdev_unsync_objs(struct dsa_port *dp, |
| 216 | struct net_device *br, |
| 217 | struct netlink_ext_ack *extack) |
| 218 | { |
| 219 | struct net_device *brport_dev = dsa_port_to_bridge_port(dp); |
| 220 | int err; |
| 221 | |
| 222 | /* Delete the switchdev objects left on this port */ |
| 223 | err = br_mdb_replay(br, brport_dev, dp, false, |
| 224 | &dsa_slave_switchdev_blocking_notifier, extack); |
| 225 | if (err && err != -EOPNOTSUPP) |
| 226 | return err; |
| 227 | |
| 228 | err = br_fdb_replay(br, brport_dev, dp, false, |
| 229 | &dsa_slave_switchdev_notifier); |
| 230 | if (err && err != -EOPNOTSUPP) |
| 231 | return err; |
| 232 | |
| 233 | err = br_vlan_replay(br, brport_dev, dp, false, |
| 234 | &dsa_slave_switchdev_blocking_notifier, extack); |
| 235 | if (err && err != -EOPNOTSUPP) |
| 236 | return err; |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp) |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 242 | { |
| 243 | /* Configure the port for standalone mode (no address learning, |
| 244 | * flood everything). |
| 245 | * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events |
| 246 | * when the user requests it through netlink or sysfs, but not |
| 247 | * automatically at port join or leave, so we need to handle resetting |
| 248 | * the brport flags ourselves. But we even prefer it that way, because |
| 249 | * otherwise, some setups might never get the notification they need, |
| 250 | * for example, when a port leaves a LAG that offloads the bridge, |
| 251 | * it becomes standalone, but as far as the bridge is concerned, no |
| 252 | * port ever left. |
| 253 | */ |
| 254 | dsa_port_clear_brport_flags(dp); |
| 255 | |
| 256 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
| 257 | * so allow it to be in BR_STATE_FORWARDING to be kept functional |
| 258 | */ |
| 259 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 260 | |
| 261 | /* VLAN filtering is handled by dsa_switch_bridge_leave */ |
| 262 | |
| 263 | /* Some drivers treat the notification for having a local multicast |
| 264 | * router by allowing multicast to be flooded to the CPU, so we should |
| 265 | * allow this in standalone mode too. |
| 266 | */ |
| 267 | dsa_port_mrouter(dp->cpu_dp, true, NULL); |
| 268 | |
| 269 | /* Ageing time may be global to the switch chip, so don't change it |
| 270 | * here because we have no good reason (or value) to change it to. |
| 271 | */ |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Vladimir Oltean | 2afc526 | 2021-03-23 01:51:48 +0200 | [diff] [blame] | 274 | int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br, |
| 275 | struct netlink_ext_ack *extack) |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 276 | { |
| 277 | struct dsa_notifier_bridge_info info = { |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 278 | .tree_index = dp->ds->dst->index, |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 279 | .sw_index = dp->ds->index, |
| 280 | .port = dp->index, |
| 281 | .br = br, |
| 282 | }; |
| 283 | int err; |
| 284 | |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 285 | /* Here the interface is already bridged. Reflect the current |
| 286 | * configuration so that drivers can program their chips accordingly. |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 287 | */ |
| 288 | dp->bridge_dev = br; |
| 289 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 290 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info); |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 291 | if (err) |
| 292 | goto out_rollback; |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 293 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 294 | err = dsa_port_switchdev_sync(dp, extack); |
| 295 | if (err) |
| 296 | goto out_rollback_unbridge; |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 297 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 298 | return 0; |
| 299 | |
| 300 | out_rollback_unbridge: |
| 301 | dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
| 302 | out_rollback: |
| 303 | dp->bridge_dev = NULL; |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 304 | return err; |
| 305 | } |
| 306 | |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 307 | int dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br, |
| 308 | struct netlink_ext_ack *extack) |
| 309 | { |
| 310 | return dsa_port_switchdev_unsync_objs(dp, br, extack); |
| 311 | } |
| 312 | |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 313 | void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) |
| 314 | { |
| 315 | struct dsa_notifier_bridge_info info = { |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 316 | .tree_index = dp->ds->dst->index, |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 317 | .sw_index = dp->ds->index, |
| 318 | .port = dp->index, |
| 319 | .br = br, |
| 320 | }; |
| 321 | int err; |
| 322 | |
| 323 | /* Here the port is already unbridged. Reflect the current configuration |
| 324 | * so that drivers can program their chips accordingly. |
| 325 | */ |
| 326 | dp->bridge_dev = NULL; |
| 327 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 328 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 329 | if (err) |
| 330 | pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n"); |
| 331 | |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 332 | dsa_port_switchdev_unsync_attrs(dp); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 333 | } |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 334 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 335 | int dsa_port_lag_change(struct dsa_port *dp, |
| 336 | struct netdev_lag_lower_state_info *linfo) |
| 337 | { |
| 338 | struct dsa_notifier_lag_info info = { |
| 339 | .sw_index = dp->ds->index, |
| 340 | .port = dp->index, |
| 341 | }; |
| 342 | bool tx_enabled; |
| 343 | |
| 344 | if (!dp->lag_dev) |
| 345 | return 0; |
| 346 | |
| 347 | /* On statically configured aggregates (e.g. loadbalance |
| 348 | * without LACP) ports will always be tx_enabled, even if the |
| 349 | * link is down. Thus we require both link_up and tx_enabled |
| 350 | * in order to include it in the tx set. |
| 351 | */ |
| 352 | tx_enabled = linfo->link_up && linfo->tx_enabled; |
| 353 | |
| 354 | if (tx_enabled == dp->lag_tx_enabled) |
| 355 | return 0; |
| 356 | |
| 357 | dp->lag_tx_enabled = tx_enabled; |
| 358 | |
| 359 | return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info); |
| 360 | } |
| 361 | |
| 362 | int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag, |
Vladimir Oltean | 2afc526 | 2021-03-23 01:51:48 +0200 | [diff] [blame] | 363 | struct netdev_lag_upper_info *uinfo, |
| 364 | struct netlink_ext_ack *extack) |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 365 | { |
| 366 | struct dsa_notifier_lag_info info = { |
| 367 | .sw_index = dp->ds->index, |
| 368 | .port = dp->index, |
| 369 | .lag = lag, |
| 370 | .info = uinfo, |
| 371 | }; |
Vladimir Oltean | 185c9a7 | 2021-03-23 01:51:47 +0200 | [diff] [blame] | 372 | struct net_device *bridge_dev; |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 373 | int err; |
| 374 | |
| 375 | dsa_lag_map(dp->ds->dst, lag); |
| 376 | dp->lag_dev = lag; |
| 377 | |
| 378 | err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info); |
Vladimir Oltean | 185c9a7 | 2021-03-23 01:51:47 +0200 | [diff] [blame] | 379 | if (err) |
| 380 | goto err_lag_join; |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 381 | |
Vladimir Oltean | 185c9a7 | 2021-03-23 01:51:47 +0200 | [diff] [blame] | 382 | bridge_dev = netdev_master_upper_dev_get(lag); |
| 383 | if (!bridge_dev || !netif_is_bridge_master(bridge_dev)) |
| 384 | return 0; |
| 385 | |
Vladimir Oltean | 2afc526 | 2021-03-23 01:51:48 +0200 | [diff] [blame] | 386 | err = dsa_port_bridge_join(dp, bridge_dev, extack); |
Vladimir Oltean | 185c9a7 | 2021-03-23 01:51:47 +0200 | [diff] [blame] | 387 | if (err) |
| 388 | goto err_bridge_join; |
| 389 | |
| 390 | return 0; |
| 391 | |
| 392 | err_bridge_join: |
| 393 | dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); |
| 394 | err_lag_join: |
| 395 | dp->lag_dev = NULL; |
| 396 | dsa_lag_unmap(dp->ds->dst, lag); |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 397 | return err; |
| 398 | } |
| 399 | |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 400 | int dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag, |
| 401 | struct netlink_ext_ack *extack) |
| 402 | { |
| 403 | if (dp->bridge_dev) |
| 404 | return dsa_port_pre_bridge_leave(dp, dp->bridge_dev, extack); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 409 | void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag) |
| 410 | { |
| 411 | struct dsa_notifier_lag_info info = { |
| 412 | .sw_index = dp->ds->index, |
| 413 | .port = dp->index, |
| 414 | .lag = lag, |
| 415 | }; |
| 416 | int err; |
| 417 | |
| 418 | if (!dp->lag_dev) |
| 419 | return; |
| 420 | |
| 421 | /* Port might have been part of a LAG that in turn was |
| 422 | * attached to a bridge. |
| 423 | */ |
| 424 | if (dp->bridge_dev) |
| 425 | dsa_port_bridge_leave(dp, dp->bridge_dev); |
| 426 | |
| 427 | dp->lag_tx_enabled = false; |
| 428 | dp->lag_dev = NULL; |
| 429 | |
| 430 | err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); |
| 431 | if (err) |
| 432 | pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n", |
| 433 | err); |
| 434 | |
| 435 | dsa_lag_unmap(dp->ds->dst, lag); |
| 436 | } |
| 437 | |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 438 | /* Must be called under rcu_read_lock() */ |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 439 | static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp, |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 440 | bool vlan_filtering, |
| 441 | struct netlink_ext_ack *extack) |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 442 | { |
| 443 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 444 | int err, i; |
| 445 | |
| 446 | /* VLAN awareness was off, so the question is "can we turn it on". |
| 447 | * We may have had 8021q uppers, those need to go. Make sure we don't |
| 448 | * enter an inconsistent state: deny changing the VLAN awareness state |
| 449 | * as long as we have 8021q uppers. |
| 450 | */ |
| 451 | if (vlan_filtering && dsa_is_user_port(ds, dp->index)) { |
| 452 | struct net_device *upper_dev, *slave = dp->slave; |
| 453 | struct net_device *br = dp->bridge_dev; |
| 454 | struct list_head *iter; |
| 455 | |
| 456 | netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { |
| 457 | struct bridge_vlan_info br_info; |
| 458 | u16 vid; |
| 459 | |
| 460 | if (!is_vlan_dev(upper_dev)) |
| 461 | continue; |
| 462 | |
| 463 | vid = vlan_dev_vlan_id(upper_dev); |
| 464 | |
| 465 | /* br_vlan_get_info() returns -EINVAL or -ENOENT if the |
| 466 | * device, respectively the VID is not found, returning |
| 467 | * 0 means success, which is a failure for us here. |
| 468 | */ |
| 469 | err = br_vlan_get_info(br, vid, &br_info); |
| 470 | if (err == 0) { |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 471 | NL_SET_ERR_MSG_MOD(extack, |
| 472 | "Must first remove VLAN uppers having VIDs also present in bridge"); |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | } |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 477 | |
| 478 | if (!ds->vlan_filtering_is_global) |
| 479 | return true; |
| 480 | |
| 481 | /* For cases where enabling/disabling VLAN awareness is global to the |
| 482 | * switch, we need to handle the case where multiple bridges span |
| 483 | * different ports of the same switch device and one of them has a |
| 484 | * different setting than what is being requested. |
| 485 | */ |
| 486 | for (i = 0; i < ds->num_ports; i++) { |
| 487 | struct net_device *other_bridge; |
| 488 | |
| 489 | other_bridge = dsa_to_port(ds, i)->bridge_dev; |
| 490 | if (!other_bridge) |
| 491 | continue; |
| 492 | /* If it's the same bridge, it also has same |
| 493 | * vlan_filtering setting => no need to check |
| 494 | */ |
| 495 | if (other_bridge == dp->bridge_dev) |
| 496 | continue; |
| 497 | if (br_vlan_enabled(other_bridge) != vlan_filtering) { |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 498 | NL_SET_ERR_MSG_MOD(extack, |
| 499 | "VLAN filtering is a global setting"); |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | return true; |
| 504 | } |
| 505 | |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 506 | int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering, |
| 507 | struct netlink_ext_ack *extack) |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 508 | { |
| 509 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 510 | bool apply; |
Vladimir Oltean | 33162e9 | 2019-04-28 21:45:43 +0300 | [diff] [blame] | 511 | int err; |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 512 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 513 | if (!ds->ops->port_vlan_filtering) |
| 514 | return -EOPNOTSUPP; |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 515 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 516 | /* We are called from dsa_slave_switchdev_blocking_event(), |
| 517 | * which is not under rcu_read_lock(), unlike |
| 518 | * dsa_slave_switchdev_event(). |
| 519 | */ |
| 520 | rcu_read_lock(); |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 521 | apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack); |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 522 | rcu_read_unlock(); |
| 523 | if (!apply) |
| 524 | return -EINVAL; |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 525 | |
Vladimir Oltean | ec9121e | 2019-04-28 21:45:51 +0300 | [diff] [blame] | 526 | if (dsa_port_is_vlan_filtering(dp) == vlan_filtering) |
| 527 | return 0; |
| 528 | |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 529 | err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering, |
| 530 | extack); |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 531 | if (err) |
| 532 | return err; |
| 533 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 534 | if (ds->vlan_filtering_is_global) |
| 535 | ds->vlan_filtering = vlan_filtering; |
| 536 | else |
| 537 | dp->vlan_filtering = vlan_filtering; |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 538 | |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 539 | return 0; |
| 540 | } |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 541 | |
Russell King | 54a0ed0 | 2020-05-12 20:20:25 +0300 | [diff] [blame] | 542 | /* This enforces legacy behavior for switch drivers which assume they can't |
| 543 | * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0 |
| 544 | */ |
| 545 | bool dsa_port_skip_vlan_configuration(struct dsa_port *dp) |
| 546 | { |
| 547 | struct dsa_switch *ds = dp->ds; |
| 548 | |
| 549 | if (!dp->bridge_dev) |
| 550 | return false; |
| 551 | |
| 552 | return (!ds->configure_vlan_while_not_filtering && |
| 553 | !br_vlan_enabled(dp->bridge_dev)); |
| 554 | } |
| 555 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 556 | int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock) |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 557 | { |
| 558 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); |
| 559 | unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 560 | struct dsa_notifier_ageing_time_info info; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 561 | int err; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 562 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 563 | info.ageing_time = ageing_time; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 564 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 565 | err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
| 566 | if (err) |
| 567 | return err; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 568 | |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 569 | dp->ageing_time = ageing_time; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 570 | |
Vladimir Oltean | 77b6136 | 2021-01-09 02:01:51 +0200 | [diff] [blame] | 571 | return 0; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 572 | } |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 573 | |
Vladimir Oltean | e18f4c1 | 2021-02-12 17:15:55 +0200 | [diff] [blame] | 574 | int dsa_port_pre_bridge_flags(const struct dsa_port *dp, |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 575 | struct switchdev_brport_flags flags, |
| 576 | struct netlink_ext_ack *extack) |
Florian Fainelli | ea87005 | 2019-02-20 16:58:22 -0800 | [diff] [blame] | 577 | { |
| 578 | struct dsa_switch *ds = dp->ds; |
| 579 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 580 | if (!ds->ops->port_pre_bridge_flags) |
Florian Fainelli | ea87005 | 2019-02-20 16:58:22 -0800 | [diff] [blame] | 581 | return -EINVAL; |
| 582 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 583 | return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack); |
Florian Fainelli | ea87005 | 2019-02-20 16:58:22 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Vladimir Oltean | e18f4c1 | 2021-02-12 17:15:55 +0200 | [diff] [blame] | 586 | int dsa_port_bridge_flags(const struct dsa_port *dp, |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 587 | struct switchdev_brport_flags flags, |
| 588 | struct netlink_ext_ack *extack) |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 589 | { |
| 590 | struct dsa_switch *ds = dp->ds; |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 591 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 592 | if (!ds->ops->port_bridge_flags) |
Oleksij Rempel | 70a7c48 | 2021-04-21 15:05:40 +0200 | [diff] [blame] | 593 | return -EOPNOTSUPP; |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 594 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 595 | return ds->ops->port_bridge_flags(ds, dp->index, flags, extack); |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 598 | int dsa_port_mrouter(struct dsa_port *dp, bool mrouter, |
| 599 | struct netlink_ext_ack *extack) |
Vivien Didelot | 08cc83c | 2019-07-08 23:31:13 -0400 | [diff] [blame] | 600 | { |
| 601 | struct dsa_switch *ds = dp->ds; |
Vivien Didelot | 08cc83c | 2019-07-08 23:31:13 -0400 | [diff] [blame] | 602 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 603 | if (!ds->ops->port_set_mrouter) |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 604 | return -EOPNOTSUPP; |
Vivien Didelot | 08cc83c | 2019-07-08 23:31:13 -0400 | [diff] [blame] | 605 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 606 | return ds->ops->port_set_mrouter(ds, dp->index, mrouter, extack); |
Vivien Didelot | 08cc83c | 2019-07-08 23:31:13 -0400 | [diff] [blame] | 607 | } |
| 608 | |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 609 | int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu, |
Vladimir Oltean | 88faba2 | 2021-06-21 19:42:18 +0300 | [diff] [blame] | 610 | bool targeted_match) |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 611 | { |
| 612 | struct dsa_notifier_mtu_info info = { |
| 613 | .sw_index = dp->ds->index, |
Vladimir Oltean | 88faba2 | 2021-06-21 19:42:18 +0300 | [diff] [blame] | 614 | .targeted_match = targeted_match, |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 615 | .port = dp->index, |
| 616 | .mtu = new_mtu, |
| 617 | }; |
| 618 | |
| 619 | return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info); |
| 620 | } |
| 621 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 622 | int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
| 623 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 624 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 625 | struct dsa_notifier_fdb_info info = { |
| 626 | .sw_index = dp->ds->index, |
| 627 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 628 | .addr = addr, |
| 629 | .vid = vid, |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 630 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 631 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 632 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 633 | } |
| 634 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 635 | int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
| 636 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 637 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 638 | struct dsa_notifier_fdb_info info = { |
| 639 | .sw_index = dp->ds->index, |
| 640 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 641 | .addr = addr, |
| 642 | .vid = vid, |
| 643 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 644 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 645 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 646 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 647 | } |
| 648 | |
Vladimir Oltean | 3dc80af | 2021-06-29 17:06:51 +0300 | [diff] [blame] | 649 | int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
| 650 | u16 vid) |
| 651 | { |
| 652 | struct dsa_notifier_fdb_info info = { |
| 653 | .sw_index = dp->ds->index, |
| 654 | .port = dp->index, |
| 655 | .addr = addr, |
| 656 | .vid = vid, |
| 657 | }; |
Vladimir Oltean | 26ee7b0 | 2021-06-29 17:06:53 +0300 | [diff] [blame^] | 658 | struct dsa_port *cpu_dp = dp->cpu_dp; |
| 659 | int err; |
| 660 | |
| 661 | err = dev_uc_add(cpu_dp->master, addr); |
| 662 | if (err) |
| 663 | return err; |
Vladimir Oltean | 3dc80af | 2021-06-29 17:06:51 +0300 | [diff] [blame] | 664 | |
| 665 | return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info); |
| 666 | } |
| 667 | |
| 668 | int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
| 669 | u16 vid) |
| 670 | { |
| 671 | struct dsa_notifier_fdb_info info = { |
| 672 | .sw_index = dp->ds->index, |
| 673 | .port = dp->index, |
| 674 | .addr = addr, |
| 675 | .vid = vid, |
| 676 | }; |
Vladimir Oltean | 26ee7b0 | 2021-06-29 17:06:53 +0300 | [diff] [blame^] | 677 | struct dsa_port *cpu_dp = dp->cpu_dp; |
| 678 | int err; |
| 679 | |
| 680 | err = dev_uc_del(cpu_dp->master, addr); |
| 681 | if (err) |
| 682 | return err; |
Vladimir Oltean | 3dc80af | 2021-06-29 17:06:51 +0300 | [diff] [blame] | 683 | |
| 684 | return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info); |
| 685 | } |
| 686 | |
Vivien Didelot | de40fc5 | 2017-09-20 19:32:14 -0400 | [diff] [blame] | 687 | int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) |
| 688 | { |
| 689 | struct dsa_switch *ds = dp->ds; |
| 690 | int port = dp->index; |
| 691 | |
| 692 | if (!ds->ops->port_fdb_dump) |
| 693 | return -EOPNOTSUPP; |
| 694 | |
| 695 | return ds->ops->port_fdb_dump(ds, port, cb, data); |
| 696 | } |
| 697 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 698 | int dsa_port_mdb_add(const struct dsa_port *dp, |
Vladimir Oltean | ffb68fc | 2021-01-09 02:01:48 +0200 | [diff] [blame] | 699 | const struct switchdev_obj_port_mdb *mdb) |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 700 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 701 | struct dsa_notifier_mdb_info info = { |
| 702 | .sw_index = dp->ds->index, |
| 703 | .port = dp->index, |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 704 | .mdb = mdb, |
| 705 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 706 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 707 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 710 | int dsa_port_mdb_del(const struct dsa_port *dp, |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 711 | const struct switchdev_obj_port_mdb *mdb) |
| 712 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 713 | struct dsa_notifier_mdb_info info = { |
| 714 | .sw_index = dp->ds->index, |
| 715 | .port = dp->index, |
| 716 | .mdb = mdb, |
| 717 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 718 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 719 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 720 | } |
| 721 | |
Vladimir Oltean | b8e997c | 2021-06-29 17:06:49 +0300 | [diff] [blame] | 722 | int dsa_port_host_mdb_add(const struct dsa_port *dp, |
| 723 | const struct switchdev_obj_port_mdb *mdb) |
| 724 | { |
| 725 | struct dsa_notifier_mdb_info info = { |
| 726 | .sw_index = dp->ds->index, |
| 727 | .port = dp->index, |
| 728 | .mdb = mdb, |
| 729 | }; |
Vladimir Oltean | 26ee7b0 | 2021-06-29 17:06:53 +0300 | [diff] [blame^] | 730 | struct dsa_port *cpu_dp = dp->cpu_dp; |
| 731 | int err; |
| 732 | |
| 733 | err = dev_mc_add(cpu_dp->master, mdb->addr); |
| 734 | if (err) |
| 735 | return err; |
Vladimir Oltean | b8e997c | 2021-06-29 17:06:49 +0300 | [diff] [blame] | 736 | |
| 737 | return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info); |
| 738 | } |
| 739 | |
| 740 | int dsa_port_host_mdb_del(const struct dsa_port *dp, |
| 741 | const struct switchdev_obj_port_mdb *mdb) |
| 742 | { |
| 743 | struct dsa_notifier_mdb_info info = { |
| 744 | .sw_index = dp->ds->index, |
| 745 | .port = dp->index, |
| 746 | .mdb = mdb, |
| 747 | }; |
Vladimir Oltean | 26ee7b0 | 2021-06-29 17:06:53 +0300 | [diff] [blame^] | 748 | struct dsa_port *cpu_dp = dp->cpu_dp; |
| 749 | int err; |
| 750 | |
| 751 | err = dev_mc_del(cpu_dp->master, mdb->addr); |
| 752 | if (err) |
| 753 | return err; |
Vladimir Oltean | b8e997c | 2021-06-29 17:06:49 +0300 | [diff] [blame] | 754 | |
| 755 | return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info); |
| 756 | } |
| 757 | |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 758 | int dsa_port_vlan_add(struct dsa_port *dp, |
Vladimir Oltean | 31046a5 | 2021-02-13 22:43:18 +0200 | [diff] [blame] | 759 | const struct switchdev_obj_port_vlan *vlan, |
| 760 | struct netlink_ext_ack *extack) |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 761 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 762 | struct dsa_notifier_vlan_info info = { |
| 763 | .sw_index = dp->ds->index, |
| 764 | .port = dp->index, |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 765 | .vlan = vlan, |
Vladimir Oltean | 31046a5 | 2021-02-13 22:43:18 +0200 | [diff] [blame] | 766 | .extack = extack, |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 767 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 768 | |
Vivien Didelot | c5335d7 | 2019-08-25 13:25:18 -0400 | [diff] [blame] | 769 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | int dsa_port_vlan_del(struct dsa_port *dp, |
| 773 | const struct switchdev_obj_port_vlan *vlan) |
| 774 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 775 | struct dsa_notifier_vlan_info info = { |
| 776 | .sw_index = dp->ds->index, |
| 777 | .port = dp->index, |
| 778 | .vlan = vlan, |
| 779 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 780 | |
Vivien Didelot | c5335d7 | 2019-08-25 13:25:18 -0400 | [diff] [blame] | 781 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 782 | } |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 783 | |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 784 | int dsa_port_mrp_add(const struct dsa_port *dp, |
| 785 | const struct switchdev_obj_mrp *mrp) |
| 786 | { |
| 787 | struct dsa_notifier_mrp_info info = { |
| 788 | .sw_index = dp->ds->index, |
| 789 | .port = dp->index, |
| 790 | .mrp = mrp, |
| 791 | }; |
| 792 | |
| 793 | return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info); |
| 794 | } |
| 795 | |
| 796 | int dsa_port_mrp_del(const struct dsa_port *dp, |
| 797 | const struct switchdev_obj_mrp *mrp) |
| 798 | { |
| 799 | struct dsa_notifier_mrp_info info = { |
| 800 | .sw_index = dp->ds->index, |
| 801 | .port = dp->index, |
| 802 | .mrp = mrp, |
| 803 | }; |
| 804 | |
| 805 | return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info); |
| 806 | } |
| 807 | |
| 808 | int dsa_port_mrp_add_ring_role(const struct dsa_port *dp, |
| 809 | const struct switchdev_obj_ring_role_mrp *mrp) |
| 810 | { |
| 811 | struct dsa_notifier_mrp_ring_role_info info = { |
| 812 | .sw_index = dp->ds->index, |
| 813 | .port = dp->index, |
| 814 | .mrp = mrp, |
| 815 | }; |
| 816 | |
| 817 | return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info); |
| 818 | } |
| 819 | |
| 820 | int dsa_port_mrp_del_ring_role(const struct dsa_port *dp, |
| 821 | const struct switchdev_obj_ring_role_mrp *mrp) |
| 822 | { |
| 823 | struct dsa_notifier_mrp_ring_role_info info = { |
| 824 | .sw_index = dp->ds->index, |
| 825 | .port = dp->index, |
| 826 | .mrp = mrp, |
| 827 | }; |
| 828 | |
| 829 | return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info); |
| 830 | } |
| 831 | |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 832 | void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp, |
| 833 | const struct dsa_device_ops *tag_ops) |
| 834 | { |
| 835 | cpu_dp->filter = tag_ops->filter; |
| 836 | cpu_dp->rcv = tag_ops->rcv; |
| 837 | cpu_dp->tag_ops = tag_ops; |
| 838 | } |
| 839 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 840 | static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp) |
| 841 | { |
| 842 | struct device_node *phy_dn; |
| 843 | struct phy_device *phydev; |
| 844 | |
| 845 | phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0); |
| 846 | if (!phy_dn) |
| 847 | return NULL; |
| 848 | |
| 849 | phydev = of_phy_find_device(phy_dn); |
| 850 | if (!phydev) { |
| 851 | of_node_put(phy_dn); |
| 852 | return ERR_PTR(-EPROBE_DEFER); |
| 853 | } |
| 854 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 855 | of_node_put(phy_dn); |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 856 | return phydev; |
| 857 | } |
| 858 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 859 | static void dsa_port_phylink_validate(struct phylink_config *config, |
| 860 | unsigned long *supported, |
| 861 | struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 862 | { |
| 863 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 864 | struct dsa_switch *ds = dp->ds; |
| 865 | |
| 866 | if (!ds->ops->phylink_validate) |
| 867 | return; |
| 868 | |
| 869 | ds->ops->phylink_validate(ds, dp->index, supported, state); |
| 870 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 871 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 872 | static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config, |
| 873 | struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 874 | { |
| 875 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 876 | struct dsa_switch *ds = dp->ds; |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 877 | int err; |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 878 | |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 879 | /* Only called for inband modes */ |
| 880 | if (!ds->ops->phylink_mac_link_state) { |
| 881 | state->link = 0; |
| 882 | return; |
| 883 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 884 | |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 885 | err = ds->ops->phylink_mac_link_state(ds, dp->index, state); |
| 886 | if (err < 0) { |
| 887 | dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n", |
| 888 | dp->index, err); |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 889 | state->link = 0; |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 890 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 891 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 892 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 893 | static void dsa_port_phylink_mac_config(struct phylink_config *config, |
| 894 | unsigned int mode, |
| 895 | const struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 896 | { |
| 897 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 898 | struct dsa_switch *ds = dp->ds; |
| 899 | |
| 900 | if (!ds->ops->phylink_mac_config) |
| 901 | return; |
| 902 | |
| 903 | ds->ops->phylink_mac_config(ds, dp->index, mode, state); |
| 904 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 905 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 906 | static void dsa_port_phylink_mac_an_restart(struct phylink_config *config) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 907 | { |
| 908 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 909 | struct dsa_switch *ds = dp->ds; |
| 910 | |
| 911 | if (!ds->ops->phylink_mac_an_restart) |
| 912 | return; |
| 913 | |
| 914 | ds->ops->phylink_mac_an_restart(ds, dp->index); |
| 915 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 916 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 917 | static void dsa_port_phylink_mac_link_down(struct phylink_config *config, |
| 918 | unsigned int mode, |
| 919 | phy_interface_t interface) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 920 | { |
| 921 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 922 | struct phy_device *phydev = NULL; |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 923 | struct dsa_switch *ds = dp->ds; |
| 924 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 925 | if (dsa_is_user_port(ds, dp->index)) |
| 926 | phydev = dp->slave->phydev; |
| 927 | |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 928 | if (!ds->ops->phylink_mac_link_down) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 929 | if (ds->ops->adjust_link && phydev) |
| 930 | ds->ops->adjust_link(ds, dp->index, phydev); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 931 | return; |
| 932 | } |
| 933 | |
| 934 | ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); |
| 935 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 936 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 937 | static void dsa_port_phylink_mac_link_up(struct phylink_config *config, |
Russell King | 91a208f | 2020-02-26 10:23:41 +0000 | [diff] [blame] | 938 | struct phy_device *phydev, |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 939 | unsigned int mode, |
| 940 | phy_interface_t interface, |
Russell King | 91a208f | 2020-02-26 10:23:41 +0000 | [diff] [blame] | 941 | int speed, int duplex, |
| 942 | bool tx_pause, bool rx_pause) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 943 | { |
| 944 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 945 | struct dsa_switch *ds = dp->ds; |
| 946 | |
| 947 | if (!ds->ops->phylink_mac_link_up) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 948 | if (ds->ops->adjust_link && phydev) |
| 949 | ds->ops->adjust_link(ds, dp->index, phydev); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 950 | return; |
| 951 | } |
| 952 | |
Russell King | 5b502a7 | 2020-02-26 10:23:46 +0000 | [diff] [blame] | 953 | ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev, |
| 954 | speed, duplex, tx_pause, rx_pause); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 955 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 956 | |
| 957 | const struct phylink_mac_ops dsa_port_phylink_mac_ops = { |
| 958 | .validate = dsa_port_phylink_validate, |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 959 | .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state, |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 960 | .mac_config = dsa_port_phylink_mac_config, |
| 961 | .mac_an_restart = dsa_port_phylink_mac_an_restart, |
| 962 | .mac_link_down = dsa_port_phylink_mac_link_down, |
| 963 | .mac_link_up = dsa_port_phylink_mac_link_up, |
| 964 | }; |
| 965 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 966 | static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) |
| 967 | { |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 968 | struct dsa_switch *ds = dp->ds; |
| 969 | struct phy_device *phydev; |
| 970 | int port = dp->index; |
| 971 | int err = 0; |
| 972 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 973 | phydev = dsa_port_get_phy_device(dp); |
| 974 | if (!phydev) |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 975 | return 0; |
| 976 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 977 | if (IS_ERR(phydev)) |
| 978 | return PTR_ERR(phydev); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 979 | |
| 980 | if (enable) { |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 981 | err = genphy_resume(phydev); |
| 982 | if (err < 0) |
| 983 | goto err_put_dev; |
| 984 | |
| 985 | err = genphy_read_status(phydev); |
| 986 | if (err < 0) |
| 987 | goto err_put_dev; |
| 988 | } else { |
| 989 | err = genphy_suspend(phydev); |
| 990 | if (err < 0) |
| 991 | goto err_put_dev; |
| 992 | } |
| 993 | |
| 994 | if (ds->ops->adjust_link) |
| 995 | ds->ops->adjust_link(ds, port, phydev); |
| 996 | |
| 997 | dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev)); |
| 998 | |
| 999 | err_put_dev: |
| 1000 | put_device(&phydev->mdio.dev); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1001 | return err; |
| 1002 | } |
| 1003 | |
| 1004 | static int dsa_port_fixed_link_register_of(struct dsa_port *dp) |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1005 | { |
| 1006 | struct device_node *dn = dp->dn; |
| 1007 | struct dsa_switch *ds = dp->ds; |
| 1008 | struct phy_device *phydev; |
| 1009 | int port = dp->index; |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 1010 | phy_interface_t mode; |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1011 | int err; |
| 1012 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1013 | err = of_phy_register_fixed_link(dn); |
| 1014 | if (err) { |
| 1015 | dev_err(ds->dev, |
| 1016 | "failed to register the fixed PHY of port %d\n", |
| 1017 | port); |
| 1018 | return err; |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1019 | } |
| 1020 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1021 | phydev = of_phy_find_device(dn); |
| 1022 | |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 1023 | err = of_get_phy_mode(dn, &mode); |
| 1024 | if (err) |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1025 | mode = PHY_INTERFACE_MODE_NA; |
| 1026 | phydev->interface = mode; |
| 1027 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1028 | genphy_read_status(phydev); |
| 1029 | |
| 1030 | if (ds->ops->adjust_link) |
| 1031 | ds->ops->adjust_link(ds, port, phydev); |
| 1032 | |
| 1033 | put_device(&phydev->mdio.dev); |
| 1034 | |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |
| 1037 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1038 | static int dsa_port_phylink_register(struct dsa_port *dp) |
| 1039 | { |
| 1040 | struct dsa_switch *ds = dp->ds; |
| 1041 | struct device_node *port_dn = dp->dn; |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 1042 | phy_interface_t mode; |
| 1043 | int err; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1044 | |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 1045 | err = of_get_phy_mode(port_dn, &mode); |
| 1046 | if (err) |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1047 | mode = PHY_INTERFACE_MODE_NA; |
| 1048 | |
| 1049 | dp->pl_config.dev = ds->dev; |
| 1050 | dp->pl_config.type = PHYLINK_DEV; |
Vladimir Oltean | 787cac3 | 2020-01-06 03:34:12 +0200 | [diff] [blame] | 1051 | dp->pl_config.pcs_poll = ds->pcs_poll; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1052 | |
| 1053 | dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), |
| 1054 | mode, &dsa_port_phylink_mac_ops); |
| 1055 | if (IS_ERR(dp->pl)) { |
| 1056 | pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); |
| 1057 | return PTR_ERR(dp->pl); |
| 1058 | } |
| 1059 | |
| 1060 | err = phylink_of_phy_connect(dp->pl, port_dn, 0); |
Florian Fainelli | 2131fba | 2019-06-10 12:31:49 -0700 | [diff] [blame] | 1061 | if (err && err != -ENODEV) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1062 | pr_err("could not attach to PHY: %d\n", err); |
| 1063 | goto err_phy_connect; |
| 1064 | } |
| 1065 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1066 | return 0; |
| 1067 | |
| 1068 | err_phy_connect: |
| 1069 | phylink_destroy(dp->pl); |
| 1070 | return err; |
| 1071 | } |
| 1072 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1073 | int dsa_port_link_register_of(struct dsa_port *dp) |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1074 | { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1075 | struct dsa_switch *ds = dp->ds; |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1076 | struct device_node *phy_np; |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 1077 | int port = dp->index; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1078 | |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1079 | if (!ds->ops->adjust_link) { |
| 1080 | phy_np = of_parse_phandle(dp->dn, "phy-handle", 0); |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 1081 | if (of_phy_is_fixed_link(dp->dn) || phy_np) { |
| 1082 | if (ds->ops->phylink_mac_link_down) |
| 1083 | ds->ops->phylink_mac_link_down(ds, port, |
| 1084 | MLO_AN_FIXED, PHY_INTERFACE_MODE_NA); |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1085 | return dsa_port_phylink_register(dp); |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 1086 | } |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1089 | |
| 1090 | dev_warn(ds->dev, |
| 1091 | "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); |
| 1092 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1093 | if (of_phy_is_fixed_link(dp->dn)) |
| 1094 | return dsa_port_fixed_link_register_of(dp); |
| 1095 | else |
| 1096 | return dsa_port_setup_phy_of(dp, true); |
| 1097 | } |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1098 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1099 | void dsa_port_link_unregister_of(struct dsa_port *dp) |
| 1100 | { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1101 | struct dsa_switch *ds = dp->ds; |
| 1102 | |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1103 | if (!ds->ops->adjust_link && dp->pl) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1104 | rtnl_lock(); |
| 1105 | phylink_disconnect_phy(dp->pl); |
| 1106 | rtnl_unlock(); |
| 1107 | phylink_destroy(dp->pl); |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1108 | dp->pl = NULL; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1109 | return; |
| 1110 | } |
| 1111 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1112 | if (of_phy_is_fixed_link(dp->dn)) |
| 1113 | of_phy_deregister_fixed_link(dp->dn); |
| 1114 | else |
| 1115 | dsa_port_setup_phy_of(dp, false); |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1116 | } |
Florian Fainelli | cf96357 | 2018-04-25 12:12:52 -0700 | [diff] [blame] | 1117 | |
| 1118 | int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data) |
| 1119 | { |
| 1120 | struct phy_device *phydev; |
| 1121 | int ret = -EOPNOTSUPP; |
| 1122 | |
| 1123 | if (of_phy_is_fixed_link(dp->dn)) |
| 1124 | return ret; |
| 1125 | |
| 1126 | phydev = dsa_port_get_phy_device(dp); |
| 1127 | if (IS_ERR_OR_NULL(phydev)) |
| 1128 | return ret; |
| 1129 | |
| 1130 | ret = phy_ethtool_get_strings(phydev, data); |
| 1131 | put_device(&phydev->mdio.dev); |
| 1132 | |
| 1133 | return ret; |
| 1134 | } |
| 1135 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings); |
| 1136 | |
| 1137 | int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data) |
| 1138 | { |
| 1139 | struct phy_device *phydev; |
| 1140 | int ret = -EOPNOTSUPP; |
| 1141 | |
| 1142 | if (of_phy_is_fixed_link(dp->dn)) |
| 1143 | return ret; |
| 1144 | |
| 1145 | phydev = dsa_port_get_phy_device(dp); |
| 1146 | if (IS_ERR_OR_NULL(phydev)) |
| 1147 | return ret; |
| 1148 | |
| 1149 | ret = phy_ethtool_get_stats(phydev, NULL, data); |
| 1150 | put_device(&phydev->mdio.dev); |
| 1151 | |
| 1152 | return ret; |
| 1153 | } |
| 1154 | EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats); |
| 1155 | |
| 1156 | int dsa_port_get_phy_sset_count(struct dsa_port *dp) |
| 1157 | { |
| 1158 | struct phy_device *phydev; |
| 1159 | int ret = -EOPNOTSUPP; |
| 1160 | |
| 1161 | if (of_phy_is_fixed_link(dp->dn)) |
| 1162 | return ret; |
| 1163 | |
| 1164 | phydev = dsa_port_get_phy_device(dp); |
| 1165 | if (IS_ERR_OR_NULL(phydev)) |
| 1166 | return ret; |
| 1167 | |
| 1168 | ret = phy_ethtool_get_sset_count(phydev); |
| 1169 | put_device(&phydev->mdio.dev); |
| 1170 | |
| 1171 | return ret; |
| 1172 | } |
| 1173 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count); |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 1174 | |
| 1175 | int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr) |
| 1176 | { |
| 1177 | struct dsa_notifier_hsr_info info = { |
| 1178 | .sw_index = dp->ds->index, |
| 1179 | .port = dp->index, |
| 1180 | .hsr = hsr, |
| 1181 | }; |
| 1182 | int err; |
| 1183 | |
| 1184 | dp->hsr_dev = hsr; |
| 1185 | |
| 1186 | err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info); |
| 1187 | if (err) |
| 1188 | dp->hsr_dev = NULL; |
| 1189 | |
| 1190 | return err; |
| 1191 | } |
| 1192 | |
| 1193 | void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr) |
| 1194 | { |
| 1195 | struct dsa_notifier_hsr_info info = { |
| 1196 | .sw_index = dp->ds->index, |
| 1197 | .port = dp->index, |
| 1198 | .hsr = hsr, |
| 1199 | }; |
| 1200 | int err; |
| 1201 | |
| 1202 | dp->hsr_dev = NULL; |
| 1203 | |
| 1204 | err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info); |
| 1205 | if (err) |
| 1206 | pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n"); |
| 1207 | } |