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 | 9264e4a | 2021-08-08 17:35:25 +0300 | [diff] [blame] | 33 | static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp) |
| 34 | { |
| 35 | struct net_device *brport_dev = dsa_port_to_bridge_port(dp); |
| 36 | struct switchdev_notifier_fdb_info info = { |
| 37 | /* flush all VLANs */ |
| 38 | .vid = 0, |
| 39 | }; |
| 40 | |
| 41 | /* When the port becomes standalone it has already left the bridge. |
| 42 | * Don't notify the bridge in that case. |
| 43 | */ |
| 44 | if (!brport_dev) |
| 45 | return; |
| 46 | |
| 47 | call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE, |
| 48 | brport_dev, &info.info, NULL); |
| 49 | } |
| 50 | |
Vladimir Oltean | 045c45d | 2021-08-08 17:35:23 +0300 | [diff] [blame] | 51 | static void dsa_port_fast_age(const struct dsa_port *dp) |
| 52 | { |
| 53 | struct dsa_switch *ds = dp->ds; |
| 54 | |
| 55 | if (!ds->ops->port_fast_age) |
| 56 | return; |
| 57 | |
| 58 | ds->ops->port_fast_age(ds, dp->index); |
Vladimir Oltean | 9264e4a | 2021-08-08 17:35:25 +0300 | [diff] [blame] | 59 | |
| 60 | dsa_port_notify_bridge_fdb_flush(dp); |
Vladimir Oltean | 045c45d | 2021-08-08 17:35:23 +0300 | [diff] [blame] | 61 | } |
| 62 | |
Vladimir Oltean | a4ffe09 | 2021-08-09 01:56:48 +0300 | [diff] [blame] | 63 | static bool dsa_port_can_configure_learning(struct dsa_port *dp) |
| 64 | { |
| 65 | struct switchdev_brport_flags flags = { |
| 66 | .mask = BR_LEARNING, |
| 67 | }; |
| 68 | struct dsa_switch *ds = dp->ds; |
| 69 | int err; |
| 70 | |
| 71 | if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags) |
| 72 | return false; |
| 73 | |
| 74 | err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL); |
| 75 | return !err; |
| 76 | } |
| 77 | |
Vladimir Oltean | 39f3210 | 2021-08-08 14:16:37 +0300 | [diff] [blame] | 78 | int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age) |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 79 | { |
| 80 | struct dsa_switch *ds = dp->ds; |
| 81 | int port = dp->index; |
| 82 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 83 | if (!ds->ops->port_stp_state_set) |
| 84 | return -EOPNOTSUPP; |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 85 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 86 | ds->ops->port_stp_state_set(ds, port, state); |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 87 | |
Vladimir Oltean | a4ffe09 | 2021-08-09 01:56:48 +0300 | [diff] [blame] | 88 | if (!dsa_port_can_configure_learning(dp) || |
| 89 | (do_fast_age && dp->learning)) { |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 90 | /* Fast age FDB entries or flush appropriate forwarding database |
| 91 | * for the given port, if we are moving it from Learning or |
| 92 | * Forwarding state, to Disabled or Blocking or Listening state. |
Vladimir Oltean | 39f3210 | 2021-08-08 14:16:37 +0300 | [diff] [blame] | 93 | * Ports that were standalone before the STP state change don't |
| 94 | * need to fast age the FDB, since address learning is off in |
| 95 | * standalone mode. |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 96 | */ |
| 97 | |
| 98 | if ((dp->stp_state == BR_STATE_LEARNING || |
| 99 | dp->stp_state == BR_STATE_FORWARDING) && |
| 100 | (state == BR_STATE_DISABLED || |
| 101 | state == BR_STATE_BLOCKING || |
| 102 | state == BR_STATE_LISTENING)) |
Vladimir Oltean | 045c45d | 2021-08-08 17:35:23 +0300 | [diff] [blame] | 103 | dsa_port_fast_age(dp); |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | dp->stp_state = state; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Vladimir Oltean | 39f3210 | 2021-08-08 14:16:37 +0300 | [diff] [blame] | 111 | static void dsa_port_set_state_now(struct dsa_port *dp, u8 state, |
| 112 | bool do_fast_age) |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 113 | { |
| 114 | int err; |
| 115 | |
Vladimir Oltean | 39f3210 | 2021-08-08 14:16:37 +0300 | [diff] [blame] | 116 | err = dsa_port_set_state(dp, state, do_fast_age); |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 117 | if (err) |
| 118 | pr_err("DSA: failed to set STP state %u (%d)\n", state, err); |
| 119 | } |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 120 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 121 | 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] | 122 | { |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 123 | struct dsa_switch *ds = dp->ds; |
| 124 | int port = dp->index; |
| 125 | int err; |
| 126 | |
| 127 | if (ds->ops->port_enable) { |
| 128 | err = ds->ops->port_enable(ds, port, phy); |
| 129 | if (err) |
| 130 | return err; |
| 131 | } |
| 132 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 133 | if (!dp->bridge) |
Vladimir Oltean | 39f3210 | 2021-08-08 14:16:37 +0300 | [diff] [blame] | 134 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 135 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 136 | if (dp->pl) |
| 137 | phylink_start(dp->pl); |
| 138 | |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 142 | int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy) |
| 143 | { |
| 144 | int err; |
| 145 | |
| 146 | rtnl_lock(); |
| 147 | err = dsa_port_enable_rt(dp, phy); |
| 148 | rtnl_unlock(); |
| 149 | |
| 150 | return err; |
| 151 | } |
| 152 | |
| 153 | void dsa_port_disable_rt(struct dsa_port *dp) |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 154 | { |
| 155 | struct dsa_switch *ds = dp->ds; |
| 156 | int port = dp->index; |
| 157 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 158 | if (dp->pl) |
| 159 | phylink_stop(dp->pl); |
| 160 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 161 | if (!dp->bridge) |
Vladimir Oltean | 39f3210 | 2021-08-08 14:16:37 +0300 | [diff] [blame] | 162 | dsa_port_set_state_now(dp, BR_STATE_DISABLED, false); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 163 | |
| 164 | if (ds->ops->port_disable) |
Andrew Lunn | 75104db | 2019-02-24 20:44:43 +0100 | [diff] [blame] | 165 | ds->ops->port_disable(ds, port); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 168 | void dsa_port_disable(struct dsa_port *dp) |
| 169 | { |
| 170 | rtnl_lock(); |
| 171 | dsa_port_disable_rt(dp); |
| 172 | rtnl_unlock(); |
| 173 | } |
| 174 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 175 | static int dsa_port_inherit_brport_flags(struct dsa_port *dp, |
| 176 | struct netlink_ext_ack *extack) |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 177 | { |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 178 | const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | |
| 179 | BR_BCAST_FLOOD; |
| 180 | struct net_device *brport_dev = dsa_port_to_bridge_port(dp); |
| 181 | int flag, err; |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 182 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 183 | for_each_set_bit(flag, &mask, 32) { |
| 184 | struct switchdev_brport_flags flags = {0}; |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 185 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 186 | flags.mask = BIT(flag); |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 187 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 188 | if (br_port_flag_is_set(brport_dev, BIT(flag))) |
| 189 | flags.val = BIT(flag); |
Vladimir Oltean | e18f4c1 | 2021-02-12 17:15:55 +0200 | [diff] [blame] | 190 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 191 | err = dsa_port_bridge_flags(dp, flags, extack); |
| 192 | if (err && err != -EOPNOTSUPP) |
| 193 | return err; |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 194 | } |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static void dsa_port_clear_brport_flags(struct dsa_port *dp) |
| 200 | { |
| 201 | const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD; |
| 202 | const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | |
| 203 | BR_BCAST_FLOOD; |
| 204 | int flag, err; |
| 205 | |
| 206 | for_each_set_bit(flag, &mask, 32) { |
| 207 | struct switchdev_brport_flags flags = {0}; |
| 208 | |
| 209 | flags.mask = BIT(flag); |
| 210 | flags.val = val & BIT(flag); |
| 211 | |
| 212 | err = dsa_port_bridge_flags(dp, flags, NULL); |
| 213 | if (err && err != -EOPNOTSUPP) |
| 214 | dev_err(dp->ds->dev, |
| 215 | "failed to clear bridge port flag %lu: %pe\n", |
| 216 | flags.val, ERR_PTR(err)); |
| 217 | } |
| 218 | } |
| 219 | |
Vladimir Oltean | 4e51bf4 | 2021-07-21 19:24:03 +0300 | [diff] [blame] | 220 | static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp, |
| 221 | struct netlink_ext_ack *extack) |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 222 | { |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 223 | struct net_device *brport_dev = dsa_port_to_bridge_port(dp); |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 224 | struct net_device *br = dsa_port_bridge_dev_get(dp); |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 225 | int err; |
| 226 | |
| 227 | err = dsa_port_inherit_brport_flags(dp, extack); |
| 228 | if (err) |
| 229 | return err; |
| 230 | |
Vladimir Oltean | 39f3210 | 2021-08-08 14:16:37 +0300 | [diff] [blame] | 231 | err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false); |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 232 | if (err && err != -EOPNOTSUPP) |
| 233 | return err; |
| 234 | |
| 235 | err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack); |
| 236 | if (err && err != -EOPNOTSUPP) |
| 237 | return err; |
| 238 | |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 239 | err = dsa_port_ageing_time(dp, br_get_ageing_time(br)); |
| 240 | if (err && err != -EOPNOTSUPP) |
| 241 | return err; |
| 242 | |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp) |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 247 | { |
| 248 | /* Configure the port for standalone mode (no address learning, |
| 249 | * flood everything). |
| 250 | * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events |
| 251 | * when the user requests it through netlink or sysfs, but not |
| 252 | * automatically at port join or leave, so we need to handle resetting |
| 253 | * the brport flags ourselves. But we even prefer it that way, because |
| 254 | * otherwise, some setups might never get the notification they need, |
| 255 | * for example, when a port leaves a LAG that offloads the bridge, |
| 256 | * it becomes standalone, but as far as the bridge is concerned, no |
| 257 | * port ever left. |
| 258 | */ |
| 259 | dsa_port_clear_brport_flags(dp); |
| 260 | |
| 261 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
| 262 | * so allow it to be in BR_STATE_FORWARDING to be kept functional |
| 263 | */ |
Vladimir Oltean | 39f3210 | 2021-08-08 14:16:37 +0300 | [diff] [blame] | 264 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true); |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 265 | |
| 266 | /* VLAN filtering is handled by dsa_switch_bridge_leave */ |
| 267 | |
Vladimir Oltean | 010e269 | 2021-03-23 01:51:50 +0200 | [diff] [blame] | 268 | /* Ageing time may be global to the switch chip, so don't change it |
| 269 | * here because we have no good reason (or value) to change it to. |
| 270 | */ |
Vladimir Oltean | 5e38c15 | 2021-02-12 17:15:54 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 273 | static int dsa_port_bridge_create(struct dsa_port *dp, |
| 274 | struct net_device *br, |
| 275 | struct netlink_ext_ack *extack) |
| 276 | { |
| 277 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 278 | struct dsa_bridge *bridge; |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 279 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 280 | bridge = dsa_tree_bridge_find(ds->dst, br); |
| 281 | if (bridge) { |
| 282 | refcount_inc(&bridge->refcount); |
| 283 | dp->bridge = bridge; |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 284 | return 0; |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 285 | } |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 286 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 287 | bridge = kzalloc(sizeof(*bridge), GFP_KERNEL); |
| 288 | if (!bridge) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | refcount_set(&bridge->refcount, 1); |
| 292 | |
| 293 | bridge->dev = br; |
| 294 | |
| 295 | bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges); |
| 296 | if (ds->max_num_bridges && !bridge->num) { |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 297 | NL_SET_ERR_MSG_MOD(extack, |
| 298 | "Range of offloadable bridges exceeded"); |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 299 | kfree(bridge); |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 300 | return -EOPNOTSUPP; |
Vladimir Oltean | 123abc06 | 2021-07-22 18:55:40 +0300 | [diff] [blame] | 301 | } |
| 302 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 303 | dp->bridge = bridge; |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static void dsa_port_bridge_destroy(struct dsa_port *dp, |
| 309 | const struct net_device *br) |
| 310 | { |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 311 | struct dsa_bridge *bridge = dp->bridge; |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 312 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 313 | dp->bridge = NULL; |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 314 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 315 | if (!refcount_dec_and_test(&bridge->refcount)) |
| 316 | return; |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 317 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 318 | if (bridge->num) |
| 319 | dsa_bridge_num_put(br, bridge->num); |
| 320 | |
| 321 | kfree(bridge); |
Vladimir Oltean | 123abc06 | 2021-07-22 18:55:40 +0300 | [diff] [blame] | 322 | } |
| 323 | |
Vladimir Oltean | 2afc526 | 2021-03-23 01:51:48 +0200 | [diff] [blame] | 324 | int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br, |
| 325 | struct netlink_ext_ack *extack) |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 326 | { |
| 327 | struct dsa_notifier_bridge_info info = { |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 328 | .tree_index = dp->ds->dst->index, |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 329 | .sw_index = dp->ds->index, |
| 330 | .port = dp->index, |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 331 | }; |
Vladimir Oltean | 2f5dc00 | 2021-07-21 19:24:01 +0300 | [diff] [blame] | 332 | struct net_device *dev = dp->slave; |
| 333 | struct net_device *brport_dev; |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 334 | int err; |
| 335 | |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 336 | /* Here the interface is already bridged. Reflect the current |
| 337 | * configuration so that drivers can program their chips accordingly. |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 338 | */ |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 339 | err = dsa_port_bridge_create(dp, br, extack); |
| 340 | if (err) |
| 341 | return err; |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 342 | |
Vladimir Oltean | 2f5dc00 | 2021-07-21 19:24:01 +0300 | [diff] [blame] | 343 | brport_dev = dsa_port_to_bridge_port(dp); |
| 344 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 345 | info.bridge = *dp->bridge; |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 346 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info); |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 347 | if (err) |
| 348 | goto out_rollback; |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 349 | |
Vladimir Oltean | 857fdd7 | 2021-12-06 18:57:58 +0200 | [diff] [blame] | 350 | /* Drivers which support bridge TX forwarding should set this */ |
| 351 | dp->bridge->tx_fwd_offload = info.tx_fwd_offload; |
Vladimir Oltean | 123abc06 | 2021-07-22 18:55:40 +0300 | [diff] [blame] | 352 | |
Vladimir Oltean | 4e51bf4 | 2021-07-21 19:24:03 +0300 | [diff] [blame] | 353 | err = switchdev_bridge_port_offload(brport_dev, dev, dp, |
| 354 | &dsa_slave_switchdev_notifier, |
| 355 | &dsa_slave_switchdev_blocking_notifier, |
Vladimir Oltean | 857fdd7 | 2021-12-06 18:57:58 +0200 | [diff] [blame] | 356 | dp->bridge->tx_fwd_offload, extack); |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 357 | if (err) |
| 358 | goto out_rollback_unbridge; |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 359 | |
Vladimir Oltean | 4e51bf4 | 2021-07-21 19:24:03 +0300 | [diff] [blame] | 360 | err = dsa_port_switchdev_sync_attrs(dp, extack); |
Vladimir Oltean | 2f5dc00 | 2021-07-21 19:24:01 +0300 | [diff] [blame] | 361 | if (err) |
| 362 | goto out_rollback_unoffload; |
| 363 | |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 364 | return 0; |
| 365 | |
Vladimir Oltean | 2f5dc00 | 2021-07-21 19:24:01 +0300 | [diff] [blame] | 366 | out_rollback_unoffload: |
Vladimir Oltean | 4e51bf4 | 2021-07-21 19:24:03 +0300 | [diff] [blame] | 367 | switchdev_bridge_port_unoffload(brport_dev, dp, |
| 368 | &dsa_slave_switchdev_notifier, |
| 369 | &dsa_slave_switchdev_blocking_notifier); |
Vladimir Oltean | 5961d6a | 2021-03-23 01:51:49 +0200 | [diff] [blame] | 370 | out_rollback_unbridge: |
| 371 | dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
| 372 | out_rollback: |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 373 | dsa_port_bridge_destroy(dp, br); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 374 | return err; |
| 375 | } |
| 376 | |
Vladimir Oltean | 4e51bf4 | 2021-07-21 19:24:03 +0300 | [diff] [blame] | 377 | void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br) |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 378 | { |
Vladimir Oltean | 2f5dc00 | 2021-07-21 19:24:01 +0300 | [diff] [blame] | 379 | struct net_device *brport_dev = dsa_port_to_bridge_port(dp); |
| 380 | |
Vladimir Oltean | 09dba21 | 2021-08-24 00:22:55 +0300 | [diff] [blame] | 381 | /* Don't try to unoffload something that is not offloaded */ |
| 382 | if (!brport_dev) |
| 383 | return; |
| 384 | |
Vladimir Oltean | 4e51bf4 | 2021-07-21 19:24:03 +0300 | [diff] [blame] | 385 | switchdev_bridge_port_unoffload(brport_dev, dp, |
| 386 | &dsa_slave_switchdev_notifier, |
| 387 | &dsa_slave_switchdev_blocking_notifier); |
Vladimir Oltean | d7d0d42 | 2021-10-26 12:25:55 +0300 | [diff] [blame] | 388 | |
| 389 | dsa_flush_workqueue(); |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 390 | } |
| 391 | |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 392 | void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) |
| 393 | { |
| 394 | struct dsa_notifier_bridge_info info = { |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 395 | .tree_index = dp->ds->dst->index, |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 396 | .sw_index = dp->ds->index, |
| 397 | .port = dp->index, |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 398 | .bridge = *dp->bridge, |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 399 | }; |
| 400 | int err; |
| 401 | |
| 402 | /* Here the port is already unbridged. Reflect the current configuration |
| 403 | * so that drivers can program their chips accordingly. |
| 404 | */ |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 405 | dsa_port_bridge_destroy(dp, br); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 406 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 407 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 408 | if (err) |
Vladimir Oltean | ab97462 | 2021-08-11 16:46:05 +0300 | [diff] [blame] | 409 | dev_err(dp->ds->dev, |
| 410 | "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n", |
| 411 | dp->index, ERR_PTR(err)); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 412 | |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 413 | dsa_port_switchdev_unsync_attrs(dp); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 414 | } |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 415 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 416 | int dsa_port_lag_change(struct dsa_port *dp, |
| 417 | struct netdev_lag_lower_state_info *linfo) |
| 418 | { |
| 419 | struct dsa_notifier_lag_info info = { |
| 420 | .sw_index = dp->ds->index, |
| 421 | .port = dp->index, |
| 422 | }; |
| 423 | bool tx_enabled; |
| 424 | |
| 425 | if (!dp->lag_dev) |
| 426 | return 0; |
| 427 | |
| 428 | /* On statically configured aggregates (e.g. loadbalance |
| 429 | * without LACP) ports will always be tx_enabled, even if the |
| 430 | * link is down. Thus we require both link_up and tx_enabled |
| 431 | * in order to include it in the tx set. |
| 432 | */ |
| 433 | tx_enabled = linfo->link_up && linfo->tx_enabled; |
| 434 | |
| 435 | if (tx_enabled == dp->lag_tx_enabled) |
| 436 | return 0; |
| 437 | |
| 438 | dp->lag_tx_enabled = tx_enabled; |
| 439 | |
| 440 | return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info); |
| 441 | } |
| 442 | |
| 443 | 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] | 444 | struct netdev_lag_upper_info *uinfo, |
| 445 | struct netlink_ext_ack *extack) |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 446 | { |
| 447 | struct dsa_notifier_lag_info info = { |
| 448 | .sw_index = dp->ds->index, |
| 449 | .port = dp->index, |
| 450 | .lag = lag, |
| 451 | .info = uinfo, |
| 452 | }; |
Vladimir Oltean | 185c9a7 | 2021-03-23 01:51:47 +0200 | [diff] [blame] | 453 | struct net_device *bridge_dev; |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 454 | int err; |
| 455 | |
| 456 | dsa_lag_map(dp->ds->dst, lag); |
| 457 | dp->lag_dev = lag; |
| 458 | |
| 459 | err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info); |
Vladimir Oltean | 185c9a7 | 2021-03-23 01:51:47 +0200 | [diff] [blame] | 460 | if (err) |
| 461 | goto err_lag_join; |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 462 | |
Vladimir Oltean | 185c9a7 | 2021-03-23 01:51:47 +0200 | [diff] [blame] | 463 | bridge_dev = netdev_master_upper_dev_get(lag); |
| 464 | if (!bridge_dev || !netif_is_bridge_master(bridge_dev)) |
| 465 | return 0; |
| 466 | |
Vladimir Oltean | 2afc526 | 2021-03-23 01:51:48 +0200 | [diff] [blame] | 467 | err = dsa_port_bridge_join(dp, bridge_dev, extack); |
Vladimir Oltean | 185c9a7 | 2021-03-23 01:51:47 +0200 | [diff] [blame] | 468 | if (err) |
| 469 | goto err_bridge_join; |
| 470 | |
| 471 | return 0; |
| 472 | |
| 473 | err_bridge_join: |
| 474 | dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); |
| 475 | err_lag_join: |
| 476 | dp->lag_dev = NULL; |
| 477 | dsa_lag_unmap(dp->ds->dst, lag); |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 478 | return err; |
| 479 | } |
| 480 | |
Vladimir Oltean | 4e51bf4 | 2021-07-21 19:24:03 +0300 | [diff] [blame] | 481 | void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag) |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 482 | { |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 483 | struct net_device *br = dsa_port_bridge_dev_get(dp); |
| 484 | |
| 485 | if (br) |
| 486 | dsa_port_pre_bridge_leave(dp, br); |
Vladimir Oltean | 7491894 | 2021-06-27 14:54:29 +0300 | [diff] [blame] | 487 | } |
| 488 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 489 | void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag) |
| 490 | { |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 491 | struct net_device *br = dsa_port_bridge_dev_get(dp); |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 492 | struct dsa_notifier_lag_info info = { |
| 493 | .sw_index = dp->ds->index, |
| 494 | .port = dp->index, |
| 495 | .lag = lag, |
| 496 | }; |
| 497 | int err; |
| 498 | |
| 499 | if (!dp->lag_dev) |
| 500 | return; |
| 501 | |
| 502 | /* Port might have been part of a LAG that in turn was |
| 503 | * attached to a bridge. |
| 504 | */ |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 505 | if (br) |
| 506 | dsa_port_bridge_leave(dp, br); |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 507 | |
| 508 | dp->lag_tx_enabled = false; |
| 509 | dp->lag_dev = NULL; |
| 510 | |
| 511 | err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); |
| 512 | if (err) |
Vladimir Oltean | ab97462 | 2021-08-11 16:46:05 +0300 | [diff] [blame] | 513 | dev_err(dp->ds->dev, |
| 514 | "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n", |
| 515 | dp->index, ERR_PTR(err)); |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 516 | |
| 517 | dsa_lag_unmap(dp->ds->dst, lag); |
| 518 | } |
| 519 | |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 520 | /* Must be called under rcu_read_lock() */ |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 521 | static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp, |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 522 | bool vlan_filtering, |
| 523 | struct netlink_ext_ack *extack) |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 524 | { |
| 525 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | d0004a0 | 2021-10-20 20:49:50 +0300 | [diff] [blame] | 526 | struct dsa_port *other_dp; |
| 527 | int err; |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 528 | |
| 529 | /* VLAN awareness was off, so the question is "can we turn it on". |
| 530 | * We may have had 8021q uppers, those need to go. Make sure we don't |
| 531 | * enter an inconsistent state: deny changing the VLAN awareness state |
| 532 | * as long as we have 8021q uppers. |
| 533 | */ |
Vladimir Oltean | 57d7798 | 2021-10-20 20:49:52 +0300 | [diff] [blame] | 534 | if (vlan_filtering && dsa_port_is_user(dp)) { |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 535 | struct net_device *br = dsa_port_bridge_dev_get(dp); |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 536 | struct net_device *upper_dev, *slave = dp->slave; |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 537 | struct list_head *iter; |
| 538 | |
| 539 | netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { |
| 540 | struct bridge_vlan_info br_info; |
| 541 | u16 vid; |
| 542 | |
| 543 | if (!is_vlan_dev(upper_dev)) |
| 544 | continue; |
| 545 | |
| 546 | vid = vlan_dev_vlan_id(upper_dev); |
| 547 | |
| 548 | /* br_vlan_get_info() returns -EINVAL or -ENOENT if the |
| 549 | * device, respectively the VID is not found, returning |
| 550 | * 0 means success, which is a failure for us here. |
| 551 | */ |
| 552 | err = br_vlan_get_info(br, vid, &br_info); |
| 553 | if (err == 0) { |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 554 | NL_SET_ERR_MSG_MOD(extack, |
| 555 | "Must first remove VLAN uppers having VIDs also present in bridge"); |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 556 | return false; |
| 557 | } |
| 558 | } |
| 559 | } |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 560 | |
| 561 | if (!ds->vlan_filtering_is_global) |
| 562 | return true; |
| 563 | |
| 564 | /* For cases where enabling/disabling VLAN awareness is global to the |
| 565 | * switch, we need to handle the case where multiple bridges span |
| 566 | * different ports of the same switch device and one of them has a |
| 567 | * different setting than what is being requested. |
| 568 | */ |
Vladimir Oltean | d0004a0 | 2021-10-20 20:49:50 +0300 | [diff] [blame] | 569 | dsa_switch_for_each_port(other_dp, ds) { |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 570 | struct net_device *other_br = dsa_port_bridge_dev_get(other_dp); |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 571 | |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 572 | /* If it's the same bridge, it also has same |
| 573 | * vlan_filtering setting => no need to check |
| 574 | */ |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 575 | if (!other_br || other_br == dsa_port_bridge_dev_get(dp)) |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 576 | continue; |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 577 | |
| 578 | if (br_vlan_enabled(other_br) != vlan_filtering) { |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 579 | NL_SET_ERR_MSG_MOD(extack, |
| 580 | "VLAN filtering is a global setting"); |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 581 | return false; |
| 582 | } |
| 583 | } |
| 584 | return true; |
| 585 | } |
| 586 | |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 587 | int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering, |
| 588 | struct netlink_ext_ack *extack) |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 589 | { |
Vladimir Oltean | 06cfb2d | 2021-08-24 00:22:57 +0300 | [diff] [blame] | 590 | bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp); |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 591 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 592 | bool apply; |
Vladimir Oltean | 33162e9 | 2019-04-28 21:45:43 +0300 | [diff] [blame] | 593 | int err; |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 594 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 595 | if (!ds->ops->port_vlan_filtering) |
| 596 | return -EOPNOTSUPP; |
Vladimir Oltean | adb256e | 2020-09-21 03:10:28 +0300 | [diff] [blame] | 597 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 598 | /* We are called from dsa_slave_switchdev_blocking_event(), |
| 599 | * which is not under rcu_read_lock(), unlike |
| 600 | * dsa_slave_switchdev_event(). |
| 601 | */ |
| 602 | rcu_read_lock(); |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 603 | apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack); |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 604 | rcu_read_unlock(); |
| 605 | if (!apply) |
| 606 | return -EINVAL; |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 607 | |
Vladimir Oltean | ec9121e | 2019-04-28 21:45:51 +0300 | [diff] [blame] | 608 | if (dsa_port_is_vlan_filtering(dp) == vlan_filtering) |
| 609 | return 0; |
| 610 | |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 611 | err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering, |
| 612 | extack); |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 613 | if (err) |
| 614 | return err; |
| 615 | |
Vladimir Oltean | 06cfb2d | 2021-08-24 00:22:57 +0300 | [diff] [blame] | 616 | if (ds->vlan_filtering_is_global) { |
Vladimir Oltean | d0004a0 | 2021-10-20 20:49:50 +0300 | [diff] [blame] | 617 | struct dsa_port *other_dp; |
Vladimir Oltean | 06cfb2d | 2021-08-24 00:22:57 +0300 | [diff] [blame] | 618 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 619 | ds->vlan_filtering = vlan_filtering; |
Vladimir Oltean | 06cfb2d | 2021-08-24 00:22:57 +0300 | [diff] [blame] | 620 | |
Vladimir Oltean | d0004a0 | 2021-10-20 20:49:50 +0300 | [diff] [blame] | 621 | dsa_switch_for_each_user_port(other_dp, ds) { |
| 622 | struct net_device *slave = dp->slave; |
Vladimir Oltean | 06cfb2d | 2021-08-24 00:22:57 +0300 | [diff] [blame] | 623 | |
| 624 | /* We might be called in the unbind path, so not |
| 625 | * all slave devices might still be registered. |
| 626 | */ |
Vladimir Oltean | 06cfb2d | 2021-08-24 00:22:57 +0300 | [diff] [blame] | 627 | if (!slave) |
| 628 | continue; |
| 629 | |
| 630 | err = dsa_slave_manage_vlan_filtering(slave, |
| 631 | vlan_filtering); |
| 632 | if (err) |
| 633 | goto restore; |
| 634 | } |
| 635 | } else { |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 636 | dp->vlan_filtering = vlan_filtering; |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 637 | |
Vladimir Oltean | 06cfb2d | 2021-08-24 00:22:57 +0300 | [diff] [blame] | 638 | err = dsa_slave_manage_vlan_filtering(dp->slave, |
| 639 | vlan_filtering); |
| 640 | if (err) |
| 641 | goto restore; |
| 642 | } |
| 643 | |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 644 | return 0; |
Vladimir Oltean | 06cfb2d | 2021-08-24 00:22:57 +0300 | [diff] [blame] | 645 | |
| 646 | restore: |
| 647 | ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL); |
| 648 | |
| 649 | if (ds->vlan_filtering_is_global) |
| 650 | ds->vlan_filtering = old_vlan_filtering; |
| 651 | else |
| 652 | dp->vlan_filtering = old_vlan_filtering; |
| 653 | |
| 654 | return err; |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 655 | } |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 656 | |
Russell King | 54a0ed0 | 2020-05-12 20:20:25 +0300 | [diff] [blame] | 657 | /* This enforces legacy behavior for switch drivers which assume they can't |
| 658 | * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0 |
| 659 | */ |
| 660 | bool dsa_port_skip_vlan_configuration(struct dsa_port *dp) |
| 661 | { |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 662 | struct net_device *br = dsa_port_bridge_dev_get(dp); |
Russell King | 54a0ed0 | 2020-05-12 20:20:25 +0300 | [diff] [blame] | 663 | struct dsa_switch *ds = dp->ds; |
| 664 | |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 665 | if (!br) |
Russell King | 54a0ed0 | 2020-05-12 20:20:25 +0300 | [diff] [blame] | 666 | return false; |
| 667 | |
Vladimir Oltean | 36cbf39 | 2021-12-06 18:57:52 +0200 | [diff] [blame] | 668 | return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br); |
Russell King | 54a0ed0 | 2020-05-12 20:20:25 +0300 | [diff] [blame] | 669 | } |
| 670 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 671 | 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] | 672 | { |
| 673 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); |
| 674 | unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 675 | struct dsa_notifier_ageing_time_info info; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 676 | int err; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 677 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 678 | info.ageing_time = ageing_time; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 679 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 680 | err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
| 681 | if (err) |
| 682 | return err; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 683 | |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 684 | dp->ageing_time = ageing_time; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 685 | |
Vladimir Oltean | 77b6136 | 2021-01-09 02:01:51 +0200 | [diff] [blame] | 686 | return 0; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 687 | } |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 688 | |
Vladimir Oltean | e18f4c1 | 2021-02-12 17:15:55 +0200 | [diff] [blame] | 689 | int dsa_port_pre_bridge_flags(const struct dsa_port *dp, |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 690 | struct switchdev_brport_flags flags, |
| 691 | struct netlink_ext_ack *extack) |
Florian Fainelli | ea87005 | 2019-02-20 16:58:22 -0800 | [diff] [blame] | 692 | { |
| 693 | struct dsa_switch *ds = dp->ds; |
| 694 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 695 | if (!ds->ops->port_pre_bridge_flags) |
Florian Fainelli | ea87005 | 2019-02-20 16:58:22 -0800 | [diff] [blame] | 696 | return -EINVAL; |
| 697 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 698 | return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack); |
Florian Fainelli | ea87005 | 2019-02-20 16:58:22 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Vladimir Oltean | 045c45d | 2021-08-08 17:35:23 +0300 | [diff] [blame] | 701 | int dsa_port_bridge_flags(struct dsa_port *dp, |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 702 | struct switchdev_brport_flags flags, |
| 703 | struct netlink_ext_ack *extack) |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 704 | { |
| 705 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | 045c45d | 2021-08-08 17:35:23 +0300 | [diff] [blame] | 706 | int err; |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 707 | |
Vladimir Oltean | a8b659e | 2021-02-12 17:15:56 +0200 | [diff] [blame] | 708 | if (!ds->ops->port_bridge_flags) |
Oleksij Rempel | 70a7c48 | 2021-04-21 15:05:40 +0200 | [diff] [blame] | 709 | return -EOPNOTSUPP; |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 710 | |
Vladimir Oltean | 045c45d | 2021-08-08 17:35:23 +0300 | [diff] [blame] | 711 | err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack); |
| 712 | if (err) |
| 713 | return err; |
| 714 | |
| 715 | if (flags.mask & BR_LEARNING) { |
| 716 | bool learning = flags.val & BR_LEARNING; |
| 717 | |
| 718 | if (learning == dp->learning) |
| 719 | return 0; |
| 720 | |
Vladimir Oltean | bee7c57 | 2021-08-09 01:56:49 +0300 | [diff] [blame] | 721 | if ((dp->learning && !learning) && |
| 722 | (dp->stp_state == BR_STATE_LEARNING || |
| 723 | dp->stp_state == BR_STATE_FORWARDING)) |
Vladimir Oltean | 045c45d | 2021-08-08 17:35:23 +0300 | [diff] [blame] | 724 | dsa_port_fast_age(dp); |
| 725 | |
| 726 | dp->learning = learning; |
| 727 | } |
| 728 | |
| 729 | return 0; |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 732 | int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu, |
Vladimir Oltean | 88faba2 | 2021-06-21 19:42:18 +0300 | [diff] [blame] | 733 | bool targeted_match) |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 734 | { |
| 735 | struct dsa_notifier_mtu_info info = { |
| 736 | .sw_index = dp->ds->index, |
Vladimir Oltean | 88faba2 | 2021-06-21 19:42:18 +0300 | [diff] [blame] | 737 | .targeted_match = targeted_match, |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 738 | .port = dp->index, |
| 739 | .mtu = new_mtu, |
| 740 | }; |
| 741 | |
| 742 | return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info); |
| 743 | } |
| 744 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 745 | int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
| 746 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 747 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 748 | struct dsa_notifier_fdb_info info = { |
| 749 | .sw_index = dp->ds->index, |
| 750 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 751 | .addr = addr, |
| 752 | .vid = vid, |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 753 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 754 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 755 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 756 | } |
| 757 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 758 | int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
| 759 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 760 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 761 | struct dsa_notifier_fdb_info info = { |
| 762 | .sw_index = dp->ds->index, |
| 763 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 764 | .addr = addr, |
| 765 | .vid = vid, |
| 766 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 767 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 768 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 769 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 770 | } |
| 771 | |
Vladimir Oltean | 3dc80af | 2021-06-29 17:06:51 +0300 | [diff] [blame] | 772 | int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
| 773 | u16 vid) |
| 774 | { |
| 775 | struct dsa_notifier_fdb_info info = { |
| 776 | .sw_index = dp->ds->index, |
| 777 | .port = dp->index, |
| 778 | .addr = addr, |
| 779 | .vid = vid, |
| 780 | }; |
Vladimir Oltean | 26ee7b0 | 2021-06-29 17:06:53 +0300 | [diff] [blame] | 781 | struct dsa_port *cpu_dp = dp->cpu_dp; |
| 782 | int err; |
| 783 | |
| 784 | err = dev_uc_add(cpu_dp->master, addr); |
| 785 | if (err) |
| 786 | return err; |
Vladimir Oltean | 3dc80af | 2021-06-29 17:06:51 +0300 | [diff] [blame] | 787 | |
| 788 | return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info); |
| 789 | } |
| 790 | |
| 791 | int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
| 792 | u16 vid) |
| 793 | { |
| 794 | struct dsa_notifier_fdb_info info = { |
| 795 | .sw_index = dp->ds->index, |
| 796 | .port = dp->index, |
| 797 | .addr = addr, |
| 798 | .vid = vid, |
| 799 | }; |
Vladimir Oltean | 26ee7b0 | 2021-06-29 17:06:53 +0300 | [diff] [blame] | 800 | struct dsa_port *cpu_dp = dp->cpu_dp; |
| 801 | int err; |
| 802 | |
| 803 | err = dev_uc_del(cpu_dp->master, addr); |
| 804 | if (err) |
| 805 | return err; |
Vladimir Oltean | 3dc80af | 2021-06-29 17:06:51 +0300 | [diff] [blame] | 806 | |
| 807 | return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info); |
| 808 | } |
| 809 | |
Vivien Didelot | de40fc5 | 2017-09-20 19:32:14 -0400 | [diff] [blame] | 810 | int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) |
| 811 | { |
| 812 | struct dsa_switch *ds = dp->ds; |
| 813 | int port = dp->index; |
| 814 | |
| 815 | if (!ds->ops->port_fdb_dump) |
| 816 | return -EOPNOTSUPP; |
| 817 | |
| 818 | return ds->ops->port_fdb_dump(ds, port, cb, data); |
| 819 | } |
| 820 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 821 | int dsa_port_mdb_add(const struct dsa_port *dp, |
Vladimir Oltean | ffb68fc | 2021-01-09 02:01:48 +0200 | [diff] [blame] | 822 | const struct switchdev_obj_port_mdb *mdb) |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 823 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 824 | struct dsa_notifier_mdb_info info = { |
| 825 | .sw_index = dp->ds->index, |
| 826 | .port = dp->index, |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 827 | .mdb = mdb, |
| 828 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 829 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 830 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 831 | } |
| 832 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 833 | int dsa_port_mdb_del(const struct dsa_port *dp, |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 834 | const struct switchdev_obj_port_mdb *mdb) |
| 835 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 836 | struct dsa_notifier_mdb_info info = { |
| 837 | .sw_index = dp->ds->index, |
| 838 | .port = dp->index, |
| 839 | .mdb = mdb, |
| 840 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 841 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 842 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 843 | } |
| 844 | |
Vladimir Oltean | b8e997c | 2021-06-29 17:06:49 +0300 | [diff] [blame] | 845 | int dsa_port_host_mdb_add(const struct dsa_port *dp, |
| 846 | const struct switchdev_obj_port_mdb *mdb) |
| 847 | { |
| 848 | struct dsa_notifier_mdb_info info = { |
| 849 | .sw_index = dp->ds->index, |
| 850 | .port = dp->index, |
| 851 | .mdb = mdb, |
| 852 | }; |
Vladimir Oltean | 26ee7b0 | 2021-06-29 17:06:53 +0300 | [diff] [blame] | 853 | struct dsa_port *cpu_dp = dp->cpu_dp; |
| 854 | int err; |
| 855 | |
| 856 | err = dev_mc_add(cpu_dp->master, mdb->addr); |
| 857 | if (err) |
| 858 | return err; |
Vladimir Oltean | b8e997c | 2021-06-29 17:06:49 +0300 | [diff] [blame] | 859 | |
| 860 | return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info); |
| 861 | } |
| 862 | |
| 863 | int dsa_port_host_mdb_del(const struct dsa_port *dp, |
| 864 | const struct switchdev_obj_port_mdb *mdb) |
| 865 | { |
| 866 | struct dsa_notifier_mdb_info info = { |
| 867 | .sw_index = dp->ds->index, |
| 868 | .port = dp->index, |
| 869 | .mdb = mdb, |
| 870 | }; |
Vladimir Oltean | 26ee7b0 | 2021-06-29 17:06:53 +0300 | [diff] [blame] | 871 | struct dsa_port *cpu_dp = dp->cpu_dp; |
| 872 | int err; |
| 873 | |
| 874 | err = dev_mc_del(cpu_dp->master, mdb->addr); |
| 875 | if (err) |
| 876 | return err; |
Vladimir Oltean | b8e997c | 2021-06-29 17:06:49 +0300 | [diff] [blame] | 877 | |
| 878 | return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info); |
| 879 | } |
| 880 | |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 881 | int dsa_port_vlan_add(struct dsa_port *dp, |
Vladimir Oltean | 31046a5 | 2021-02-13 22:43:18 +0200 | [diff] [blame] | 882 | const struct switchdev_obj_port_vlan *vlan, |
| 883 | struct netlink_ext_ack *extack) |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 884 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 885 | struct dsa_notifier_vlan_info info = { |
| 886 | .sw_index = dp->ds->index, |
| 887 | .port = dp->index, |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 888 | .vlan = vlan, |
Vladimir Oltean | 31046a5 | 2021-02-13 22:43:18 +0200 | [diff] [blame] | 889 | .extack = extack, |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 890 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 891 | |
Vivien Didelot | c5335d7 | 2019-08-25 13:25:18 -0400 | [diff] [blame] | 892 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | int dsa_port_vlan_del(struct dsa_port *dp, |
| 896 | const struct switchdev_obj_port_vlan *vlan) |
| 897 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 898 | struct dsa_notifier_vlan_info info = { |
| 899 | .sw_index = dp->ds->index, |
| 900 | .port = dp->index, |
| 901 | .vlan = vlan, |
| 902 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 903 | |
Vivien Didelot | c5335d7 | 2019-08-25 13:25:18 -0400 | [diff] [blame] | 904 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 905 | } |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 906 | |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 907 | int dsa_port_mrp_add(const struct dsa_port *dp, |
| 908 | const struct switchdev_obj_mrp *mrp) |
| 909 | { |
Vladimir Oltean | cad6901 | 2022-01-05 15:18:12 +0200 | [diff] [blame] | 910 | struct dsa_switch *ds = dp->ds; |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 911 | |
Vladimir Oltean | cad6901 | 2022-01-05 15:18:12 +0200 | [diff] [blame] | 912 | if (!ds->ops->port_mrp_add) |
| 913 | return -EOPNOTSUPP; |
| 914 | |
| 915 | return ds->ops->port_mrp_add(ds, dp->index, mrp); |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | int dsa_port_mrp_del(const struct dsa_port *dp, |
| 919 | const struct switchdev_obj_mrp *mrp) |
| 920 | { |
Vladimir Oltean | cad6901 | 2022-01-05 15:18:12 +0200 | [diff] [blame] | 921 | struct dsa_switch *ds = dp->ds; |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 922 | |
Vladimir Oltean | cad6901 | 2022-01-05 15:18:12 +0200 | [diff] [blame] | 923 | if (!ds->ops->port_mrp_del) |
| 924 | return -EOPNOTSUPP; |
| 925 | |
| 926 | return ds->ops->port_mrp_del(ds, dp->index, mrp); |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | int dsa_port_mrp_add_ring_role(const struct dsa_port *dp, |
| 930 | const struct switchdev_obj_ring_role_mrp *mrp) |
| 931 | { |
Vladimir Oltean | cad6901 | 2022-01-05 15:18:12 +0200 | [diff] [blame] | 932 | struct dsa_switch *ds = dp->ds; |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 933 | |
Vladimir Oltean | cad6901 | 2022-01-05 15:18:12 +0200 | [diff] [blame] | 934 | if (!ds->ops->port_mrp_add_ring_role) |
| 935 | return -EOPNOTSUPP; |
| 936 | |
| 937 | return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp); |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | int dsa_port_mrp_del_ring_role(const struct dsa_port *dp, |
| 941 | const struct switchdev_obj_ring_role_mrp *mrp) |
| 942 | { |
Vladimir Oltean | cad6901 | 2022-01-05 15:18:12 +0200 | [diff] [blame] | 943 | struct dsa_switch *ds = dp->ds; |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 944 | |
Vladimir Oltean | cad6901 | 2022-01-05 15:18:12 +0200 | [diff] [blame] | 945 | if (!ds->ops->port_mrp_del_ring_role) |
| 946 | return -EOPNOTSUPP; |
| 947 | |
| 948 | return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp); |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 949 | } |
| 950 | |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 951 | void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp, |
| 952 | const struct dsa_device_ops *tag_ops) |
| 953 | { |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 954 | cpu_dp->rcv = tag_ops->rcv; |
| 955 | cpu_dp->tag_ops = tag_ops; |
| 956 | } |
| 957 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 958 | static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp) |
| 959 | { |
| 960 | struct device_node *phy_dn; |
| 961 | struct phy_device *phydev; |
| 962 | |
| 963 | phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0); |
| 964 | if (!phy_dn) |
| 965 | return NULL; |
| 966 | |
| 967 | phydev = of_phy_find_device(phy_dn); |
| 968 | if (!phydev) { |
| 969 | of_node_put(phy_dn); |
| 970 | return ERR_PTR(-EPROBE_DEFER); |
| 971 | } |
| 972 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 973 | of_node_put(phy_dn); |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 974 | return phydev; |
| 975 | } |
| 976 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 977 | static void dsa_port_phylink_validate(struct phylink_config *config, |
| 978 | unsigned long *supported, |
| 979 | struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 980 | { |
| 981 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 982 | struct dsa_switch *ds = dp->ds; |
| 983 | |
Russell King (Oracle) | 5938bce | 2021-11-30 13:10:06 +0000 | [diff] [blame] | 984 | if (!ds->ops->phylink_validate) { |
| 985 | if (config->mac_capabilities) |
| 986 | phylink_generic_validate(config, supported, state); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 987 | return; |
Russell King (Oracle) | 5938bce | 2021-11-30 13:10:06 +0000 | [diff] [blame] | 988 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 989 | |
| 990 | ds->ops->phylink_validate(ds, dp->index, supported, state); |
| 991 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 992 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 993 | static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config, |
| 994 | struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 995 | { |
| 996 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 997 | struct dsa_switch *ds = dp->ds; |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 998 | int err; |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 999 | |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 1000 | /* Only called for inband modes */ |
| 1001 | if (!ds->ops->phylink_mac_link_state) { |
| 1002 | state->link = 0; |
| 1003 | return; |
| 1004 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1005 | |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 1006 | err = ds->ops->phylink_mac_link_state(ds, dp->index, state); |
| 1007 | if (err < 0) { |
| 1008 | dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n", |
| 1009 | dp->index, err); |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 1010 | state->link = 0; |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 1011 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1012 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1013 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 1014 | static void dsa_port_phylink_mac_config(struct phylink_config *config, |
| 1015 | unsigned int mode, |
| 1016 | const struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1017 | { |
| 1018 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 1019 | struct dsa_switch *ds = dp->ds; |
| 1020 | |
| 1021 | if (!ds->ops->phylink_mac_config) |
| 1022 | return; |
| 1023 | |
| 1024 | ds->ops->phylink_mac_config(ds, dp->index, mode, state); |
| 1025 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1026 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 1027 | static void dsa_port_phylink_mac_an_restart(struct phylink_config *config) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1028 | { |
| 1029 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 1030 | struct dsa_switch *ds = dp->ds; |
| 1031 | |
| 1032 | if (!ds->ops->phylink_mac_an_restart) |
| 1033 | return; |
| 1034 | |
| 1035 | ds->ops->phylink_mac_an_restart(ds, dp->index); |
| 1036 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1037 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 1038 | static void dsa_port_phylink_mac_link_down(struct phylink_config *config, |
| 1039 | unsigned int mode, |
| 1040 | phy_interface_t interface) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1041 | { |
| 1042 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1043 | struct phy_device *phydev = NULL; |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1044 | struct dsa_switch *ds = dp->ds; |
| 1045 | |
Vladimir Oltean | 57d7798 | 2021-10-20 20:49:52 +0300 | [diff] [blame] | 1046 | if (dsa_port_is_user(dp)) |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1047 | phydev = dp->slave->phydev; |
| 1048 | |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1049 | if (!ds->ops->phylink_mac_link_down) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1050 | if (ds->ops->adjust_link && phydev) |
| 1051 | ds->ops->adjust_link(ds, dp->index, phydev); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); |
| 1056 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1057 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 1058 | static void dsa_port_phylink_mac_link_up(struct phylink_config *config, |
Russell King | 91a208f | 2020-02-26 10:23:41 +0000 | [diff] [blame] | 1059 | struct phy_device *phydev, |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 1060 | unsigned int mode, |
| 1061 | phy_interface_t interface, |
Russell King | 91a208f | 2020-02-26 10:23:41 +0000 | [diff] [blame] | 1062 | int speed, int duplex, |
| 1063 | bool tx_pause, bool rx_pause) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1064 | { |
| 1065 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1066 | struct dsa_switch *ds = dp->ds; |
| 1067 | |
| 1068 | if (!ds->ops->phylink_mac_link_up) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1069 | if (ds->ops->adjust_link && phydev) |
| 1070 | ds->ops->adjust_link(ds, dp->index, phydev); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1071 | return; |
| 1072 | } |
| 1073 | |
Russell King | 5b502a7 | 2020-02-26 10:23:46 +0000 | [diff] [blame] | 1074 | ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev, |
| 1075 | speed, duplex, tx_pause, rx_pause); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1076 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1077 | |
Russell King (Oracle) | 21bd64b | 2021-11-30 13:09:55 +0000 | [diff] [blame] | 1078 | static const struct phylink_mac_ops dsa_port_phylink_mac_ops = { |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1079 | .validate = dsa_port_phylink_validate, |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 1080 | .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state, |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 1081 | .mac_config = dsa_port_phylink_mac_config, |
| 1082 | .mac_an_restart = dsa_port_phylink_mac_an_restart, |
| 1083 | .mac_link_down = dsa_port_phylink_mac_link_down, |
| 1084 | .mac_link_up = dsa_port_phylink_mac_link_up, |
| 1085 | }; |
| 1086 | |
Russell King (Oracle) | 21bd64b | 2021-11-30 13:09:55 +0000 | [diff] [blame] | 1087 | int dsa_port_phylink_create(struct dsa_port *dp) |
| 1088 | { |
| 1089 | struct dsa_switch *ds = dp->ds; |
| 1090 | phy_interface_t mode; |
| 1091 | int err; |
| 1092 | |
| 1093 | err = of_get_phy_mode(dp->dn, &mode); |
| 1094 | if (err) |
| 1095 | mode = PHY_INTERFACE_MODE_NA; |
| 1096 | |
Russell King (Oracle) | 0a9f079 | 2021-12-09 13:11:38 +0000 | [diff] [blame] | 1097 | /* Presence of phylink_mac_link_state or phylink_mac_an_restart is |
| 1098 | * an indicator of a legacy phylink driver. |
| 1099 | */ |
| 1100 | if (ds->ops->phylink_mac_link_state || |
| 1101 | ds->ops->phylink_mac_an_restart) |
| 1102 | dp->pl_config.legacy_pre_march2020 = true; |
| 1103 | |
Russell King (Oracle) | 072eea6 | 2021-11-30 13:10:01 +0000 | [diff] [blame] | 1104 | if (ds->ops->phylink_get_caps) |
| 1105 | ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config); |
Russell King (Oracle) | 21bd64b | 2021-11-30 13:09:55 +0000 | [diff] [blame] | 1106 | |
| 1107 | dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn), |
| 1108 | mode, &dsa_port_phylink_mac_ops); |
| 1109 | if (IS_ERR(dp->pl)) { |
| 1110 | pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); |
| 1111 | return PTR_ERR(dp->pl); |
| 1112 | } |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1117 | static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) |
| 1118 | { |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1119 | struct dsa_switch *ds = dp->ds; |
| 1120 | struct phy_device *phydev; |
| 1121 | int port = dp->index; |
| 1122 | int err = 0; |
| 1123 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 1124 | phydev = dsa_port_get_phy_device(dp); |
| 1125 | if (!phydev) |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1126 | return 0; |
| 1127 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 1128 | if (IS_ERR(phydev)) |
| 1129 | return PTR_ERR(phydev); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1130 | |
| 1131 | if (enable) { |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1132 | err = genphy_resume(phydev); |
| 1133 | if (err < 0) |
| 1134 | goto err_put_dev; |
| 1135 | |
| 1136 | err = genphy_read_status(phydev); |
| 1137 | if (err < 0) |
| 1138 | goto err_put_dev; |
| 1139 | } else { |
| 1140 | err = genphy_suspend(phydev); |
| 1141 | if (err < 0) |
| 1142 | goto err_put_dev; |
| 1143 | } |
| 1144 | |
| 1145 | if (ds->ops->adjust_link) |
| 1146 | ds->ops->adjust_link(ds, port, phydev); |
| 1147 | |
| 1148 | dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev)); |
| 1149 | |
| 1150 | err_put_dev: |
| 1151 | put_device(&phydev->mdio.dev); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1152 | return err; |
| 1153 | } |
| 1154 | |
| 1155 | static int dsa_port_fixed_link_register_of(struct dsa_port *dp) |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1156 | { |
| 1157 | struct device_node *dn = dp->dn; |
| 1158 | struct dsa_switch *ds = dp->ds; |
| 1159 | struct phy_device *phydev; |
| 1160 | int port = dp->index; |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 1161 | phy_interface_t mode; |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1162 | int err; |
| 1163 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1164 | err = of_phy_register_fixed_link(dn); |
| 1165 | if (err) { |
| 1166 | dev_err(ds->dev, |
| 1167 | "failed to register the fixed PHY of port %d\n", |
| 1168 | port); |
| 1169 | return err; |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1170 | } |
| 1171 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1172 | phydev = of_phy_find_device(dn); |
| 1173 | |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 1174 | err = of_get_phy_mode(dn, &mode); |
| 1175 | if (err) |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1176 | mode = PHY_INTERFACE_MODE_NA; |
| 1177 | phydev->interface = mode; |
| 1178 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1179 | genphy_read_status(phydev); |
| 1180 | |
| 1181 | if (ds->ops->adjust_link) |
| 1182 | ds->ops->adjust_link(ds, port, phydev); |
| 1183 | |
| 1184 | put_device(&phydev->mdio.dev); |
| 1185 | |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1189 | static int dsa_port_phylink_register(struct dsa_port *dp) |
| 1190 | { |
| 1191 | struct dsa_switch *ds = dp->ds; |
| 1192 | struct device_node *port_dn = dp->dn; |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 1193 | int err; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1194 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1195 | dp->pl_config.dev = ds->dev; |
| 1196 | dp->pl_config.type = PHYLINK_DEV; |
Vladimir Oltean | 787cac3 | 2020-01-06 03:34:12 +0200 | [diff] [blame] | 1197 | dp->pl_config.pcs_poll = ds->pcs_poll; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1198 | |
Russell King (Oracle) | 21bd64b | 2021-11-30 13:09:55 +0000 | [diff] [blame] | 1199 | err = dsa_port_phylink_create(dp); |
| 1200 | if (err) |
| 1201 | return err; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1202 | |
| 1203 | err = phylink_of_phy_connect(dp->pl, port_dn, 0); |
Florian Fainelli | 2131fba | 2019-06-10 12:31:49 -0700 | [diff] [blame] | 1204 | if (err && err != -ENODEV) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1205 | pr_err("could not attach to PHY: %d\n", err); |
| 1206 | goto err_phy_connect; |
| 1207 | } |
| 1208 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1209 | return 0; |
| 1210 | |
| 1211 | err_phy_connect: |
| 1212 | phylink_destroy(dp->pl); |
| 1213 | return err; |
| 1214 | } |
| 1215 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1216 | int dsa_port_link_register_of(struct dsa_port *dp) |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1217 | { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1218 | struct dsa_switch *ds = dp->ds; |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1219 | struct device_node *phy_np; |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 1220 | int port = dp->index; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1221 | |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1222 | if (!ds->ops->adjust_link) { |
| 1223 | phy_np = of_parse_phandle(dp->dn, "phy-handle", 0); |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 1224 | if (of_phy_is_fixed_link(dp->dn) || phy_np) { |
| 1225 | if (ds->ops->phylink_mac_link_down) |
| 1226 | ds->ops->phylink_mac_link_down(ds, port, |
| 1227 | MLO_AN_FIXED, PHY_INTERFACE_MODE_NA); |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1228 | return dsa_port_phylink_register(dp); |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 1229 | } |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1230 | return 0; |
| 1231 | } |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1232 | |
| 1233 | dev_warn(ds->dev, |
| 1234 | "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); |
| 1235 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1236 | if (of_phy_is_fixed_link(dp->dn)) |
| 1237 | return dsa_port_fixed_link_register_of(dp); |
| 1238 | else |
| 1239 | return dsa_port_setup_phy_of(dp, true); |
| 1240 | } |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1241 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1242 | void dsa_port_link_unregister_of(struct dsa_port *dp) |
| 1243 | { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1244 | struct dsa_switch *ds = dp->ds; |
| 1245 | |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1246 | if (!ds->ops->adjust_link && dp->pl) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1247 | rtnl_lock(); |
| 1248 | phylink_disconnect_phy(dp->pl); |
| 1249 | rtnl_unlock(); |
| 1250 | phylink_destroy(dp->pl); |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 1251 | dp->pl = NULL; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 1252 | return; |
| 1253 | } |
| 1254 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 1255 | if (of_phy_is_fixed_link(dp->dn)) |
| 1256 | of_phy_deregister_fixed_link(dp->dn); |
| 1257 | else |
| 1258 | dsa_port_setup_phy_of(dp, false); |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 1259 | } |
Florian Fainelli | cf96357 | 2018-04-25 12:12:52 -0700 | [diff] [blame] | 1260 | |
| 1261 | int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data) |
| 1262 | { |
| 1263 | struct phy_device *phydev; |
| 1264 | int ret = -EOPNOTSUPP; |
| 1265 | |
| 1266 | if (of_phy_is_fixed_link(dp->dn)) |
| 1267 | return ret; |
| 1268 | |
| 1269 | phydev = dsa_port_get_phy_device(dp); |
| 1270 | if (IS_ERR_OR_NULL(phydev)) |
| 1271 | return ret; |
| 1272 | |
| 1273 | ret = phy_ethtool_get_strings(phydev, data); |
| 1274 | put_device(&phydev->mdio.dev); |
| 1275 | |
| 1276 | return ret; |
| 1277 | } |
| 1278 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings); |
| 1279 | |
| 1280 | int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data) |
| 1281 | { |
| 1282 | struct phy_device *phydev; |
| 1283 | int ret = -EOPNOTSUPP; |
| 1284 | |
| 1285 | if (of_phy_is_fixed_link(dp->dn)) |
| 1286 | return ret; |
| 1287 | |
| 1288 | phydev = dsa_port_get_phy_device(dp); |
| 1289 | if (IS_ERR_OR_NULL(phydev)) |
| 1290 | return ret; |
| 1291 | |
| 1292 | ret = phy_ethtool_get_stats(phydev, NULL, data); |
| 1293 | put_device(&phydev->mdio.dev); |
| 1294 | |
| 1295 | return ret; |
| 1296 | } |
| 1297 | EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats); |
| 1298 | |
| 1299 | int dsa_port_get_phy_sset_count(struct dsa_port *dp) |
| 1300 | { |
| 1301 | struct phy_device *phydev; |
| 1302 | int ret = -EOPNOTSUPP; |
| 1303 | |
| 1304 | if (of_phy_is_fixed_link(dp->dn)) |
| 1305 | return ret; |
| 1306 | |
| 1307 | phydev = dsa_port_get_phy_device(dp); |
| 1308 | if (IS_ERR_OR_NULL(phydev)) |
| 1309 | return ret; |
| 1310 | |
| 1311 | ret = phy_ethtool_get_sset_count(phydev); |
| 1312 | put_device(&phydev->mdio.dev); |
| 1313 | |
| 1314 | return ret; |
| 1315 | } |
| 1316 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count); |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 1317 | |
| 1318 | int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr) |
| 1319 | { |
Vladimir Oltean | a68dc7b | 2022-01-05 15:18:13 +0200 | [diff] [blame] | 1320 | struct dsa_switch *ds = dp->ds; |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 1321 | int err; |
| 1322 | |
Vladimir Oltean | a68dc7b | 2022-01-05 15:18:13 +0200 | [diff] [blame] | 1323 | if (!ds->ops->port_hsr_join) |
| 1324 | return -EOPNOTSUPP; |
| 1325 | |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 1326 | dp->hsr_dev = hsr; |
| 1327 | |
Vladimir Oltean | a68dc7b | 2022-01-05 15:18:13 +0200 | [diff] [blame] | 1328 | err = ds->ops->port_hsr_join(ds, dp->index, hsr); |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 1329 | if (err) |
| 1330 | dp->hsr_dev = NULL; |
| 1331 | |
| 1332 | return err; |
| 1333 | } |
| 1334 | |
| 1335 | void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr) |
| 1336 | { |
Vladimir Oltean | a68dc7b | 2022-01-05 15:18:13 +0200 | [diff] [blame] | 1337 | struct dsa_switch *ds = dp->ds; |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 1338 | int err; |
| 1339 | |
| 1340 | dp->hsr_dev = NULL; |
| 1341 | |
Vladimir Oltean | a68dc7b | 2022-01-05 15:18:13 +0200 | [diff] [blame] | 1342 | if (ds->ops->port_hsr_leave) { |
| 1343 | err = ds->ops->port_hsr_leave(ds, dp->index, hsr); |
| 1344 | if (err) |
| 1345 | dev_err(dp->ds->dev, |
| 1346 | "port %d failed to leave HSR %s: %pe\n", |
| 1347 | dp->index, hsr->name, ERR_PTR(err)); |
| 1348 | } |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 1349 | } |
Vladimir Oltean | c64b9c0 | 2021-07-19 20:14:52 +0300 | [diff] [blame] | 1350 | |
Vladimir Oltean | 724395f | 2021-08-11 16:46:06 +0300 | [diff] [blame] | 1351 | int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast) |
Vladimir Oltean | c64b9c0 | 2021-07-19 20:14:52 +0300 | [diff] [blame] | 1352 | { |
| 1353 | struct dsa_notifier_tag_8021q_vlan_info info = { |
| 1354 | .tree_index = dp->ds->dst->index, |
| 1355 | .sw_index = dp->ds->index, |
| 1356 | .port = dp->index, |
| 1357 | .vid = vid, |
| 1358 | }; |
| 1359 | |
Vladimir Oltean | 724395f | 2021-08-11 16:46:06 +0300 | [diff] [blame] | 1360 | if (broadcast) |
| 1361 | return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info); |
| 1362 | |
| 1363 | return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info); |
Vladimir Oltean | c64b9c0 | 2021-07-19 20:14:52 +0300 | [diff] [blame] | 1364 | } |
| 1365 | |
Vladimir Oltean | 724395f | 2021-08-11 16:46:06 +0300 | [diff] [blame] | 1366 | void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast) |
Vladimir Oltean | c64b9c0 | 2021-07-19 20:14:52 +0300 | [diff] [blame] | 1367 | { |
| 1368 | struct dsa_notifier_tag_8021q_vlan_info info = { |
| 1369 | .tree_index = dp->ds->dst->index, |
| 1370 | .sw_index = dp->ds->index, |
| 1371 | .port = dp->index, |
| 1372 | .vid = vid, |
| 1373 | }; |
| 1374 | int err; |
| 1375 | |
Vladimir Oltean | 724395f | 2021-08-11 16:46:06 +0300 | [diff] [blame] | 1376 | if (broadcast) |
| 1377 | err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info); |
| 1378 | else |
| 1379 | err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info); |
Vladimir Oltean | c64b9c0 | 2021-07-19 20:14:52 +0300 | [diff] [blame] | 1380 | if (err) |
Vladimir Oltean | ab97462 | 2021-08-11 16:46:05 +0300 | [diff] [blame] | 1381 | dev_err(dp->ds->dev, |
| 1382 | "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n", |
| 1383 | dp->index, vid, ERR_PTR(err)); |
Vladimir Oltean | c64b9c0 | 2021-07-19 20:14:52 +0300 | [diff] [blame] | 1384 | } |