Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c |
| 3 | * Copyright (c) 2015 Mellanox Technologies. All rights reserved. |
| 4 | * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com> |
| 5 | * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com> |
| 6 | * Copyright (c) 2015 Elad Raz <eladr@mellanox.com> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the names of the copyright holders nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * Alternatively, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 22 | * Software Foundation. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/types.h> |
| 39 | #include <linux/netdevice.h> |
| 40 | #include <linux/etherdevice.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/device.h> |
| 43 | #include <linux/skbuff.h> |
| 44 | #include <linux/if_vlan.h> |
| 45 | #include <linux/if_bridge.h> |
| 46 | #include <linux/workqueue.h> |
| 47 | #include <linux/jiffies.h> |
Ido Schimmel | 4f2c6ae | 2016-01-27 15:16:43 +0100 | [diff] [blame] | 48 | #include <linux/rtnetlink.h> |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 49 | #include <net/switchdev.h> |
| 50 | |
| 51 | #include "spectrum.h" |
| 52 | #include "core.h" |
| 53 | #include "reg.h" |
| 54 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 55 | struct mlxsw_sp_bridge_ops; |
| 56 | |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 57 | struct mlxsw_sp_bridge { |
| 58 | struct mlxsw_sp *mlxsw_sp; |
| 59 | struct { |
| 60 | struct delayed_work dw; |
| 61 | #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100 |
| 62 | unsigned int interval; /* ms */ |
| 63 | } fdb_notify; |
| 64 | #define MLXSW_SP_MIN_AGEING_TIME 10 |
| 65 | #define MLXSW_SP_MAX_AGEING_TIME 1000000 |
| 66 | #define MLXSW_SP_DEFAULT_AGEING_TIME 300 |
| 67 | u32 ageing_time; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 68 | bool vlan_enabled_exists; |
| 69 | struct list_head bridges_list; |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 70 | DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 71 | const struct mlxsw_sp_bridge_ops *bridge_8021q_ops; |
| 72 | const struct mlxsw_sp_bridge_ops *bridge_8021d_ops; |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 75 | struct mlxsw_sp_bridge_device { |
| 76 | struct net_device *dev; |
| 77 | struct list_head list; |
| 78 | struct list_head ports_list; |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 79 | struct list_head mids_list; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 80 | u8 vlan_enabled:1, |
| 81 | multicast_enabled:1; |
| 82 | const struct mlxsw_sp_bridge_ops *ops; |
| 83 | }; |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 84 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 85 | struct mlxsw_sp_bridge_port { |
| 86 | struct net_device *dev; |
| 87 | struct mlxsw_sp_bridge_device *bridge_device; |
| 88 | struct list_head list; |
| 89 | struct list_head vlans_list; |
| 90 | unsigned int ref_count; |
| 91 | u8 stp_state; |
| 92 | unsigned long flags; |
| 93 | bool mrouter; |
| 94 | bool lagged; |
| 95 | union { |
| 96 | u16 lag_id; |
| 97 | u16 system_port; |
| 98 | }; |
| 99 | }; |
Elad Raz | e4b6f69 | 2016-01-10 21:06:27 +0100 | [diff] [blame] | 100 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 101 | struct mlxsw_sp_bridge_vlan { |
| 102 | struct list_head list; |
| 103 | struct list_head port_vlan_list; |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 104 | u16 vid; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 105 | }; |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 106 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 107 | struct mlxsw_sp_bridge_ops { |
| 108 | int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device, |
| 109 | struct mlxsw_sp_bridge_port *bridge_port, |
| 110 | struct mlxsw_sp_port *mlxsw_sp_port); |
| 111 | void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device, |
| 112 | struct mlxsw_sp_bridge_port *bridge_port, |
| 113 | struct mlxsw_sp_port *mlxsw_sp_port); |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 114 | struct mlxsw_sp_fid * |
| 115 | (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device, |
| 116 | u16 vid); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static int |
| 120 | mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp, |
| 121 | struct mlxsw_sp_bridge_port *bridge_port, |
| 122 | u16 fid_index); |
| 123 | |
| 124 | static struct mlxsw_sp_bridge_device * |
| 125 | mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge, |
| 126 | const struct net_device *br_dev) |
| 127 | { |
| 128 | struct mlxsw_sp_bridge_device *bridge_device; |
| 129 | |
| 130 | list_for_each_entry(bridge_device, &bridge->bridges_list, list) |
| 131 | if (bridge_device->dev == br_dev) |
| 132 | return bridge_device; |
| 133 | |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | static struct mlxsw_sp_bridge_device * |
| 138 | mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge, |
| 139 | struct net_device *br_dev) |
| 140 | { |
| 141 | struct device *dev = bridge->mlxsw_sp->bus_info->dev; |
| 142 | struct mlxsw_sp_bridge_device *bridge_device; |
| 143 | bool vlan_enabled = br_vlan_enabled(br_dev); |
| 144 | |
| 145 | if (vlan_enabled && bridge->vlan_enabled_exists) { |
| 146 | dev_err(dev, "Only one VLAN-aware bridge is supported\n"); |
| 147 | return ERR_PTR(-EINVAL); |
Nogah Frankel | 1e5d943 | 2017-02-09 14:54:48 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 150 | bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL); |
| 151 | if (!bridge_device) |
| 152 | return ERR_PTR(-ENOMEM); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 153 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 154 | bridge_device->dev = br_dev; |
| 155 | bridge_device->vlan_enabled = vlan_enabled; |
| 156 | bridge_device->multicast_enabled = br_multicast_enabled(br_dev); |
| 157 | INIT_LIST_HEAD(&bridge_device->ports_list); |
| 158 | if (vlan_enabled) { |
| 159 | bridge->vlan_enabled_exists = true; |
| 160 | bridge_device->ops = bridge->bridge_8021q_ops; |
| 161 | } else { |
| 162 | bridge_device->ops = bridge->bridge_8021d_ops; |
| 163 | } |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 164 | INIT_LIST_HEAD(&bridge_device->mids_list); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 165 | list_add(&bridge_device->list, &bridge->bridges_list); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 166 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 167 | return bridge_device; |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge, |
| 172 | struct mlxsw_sp_bridge_device *bridge_device) |
| 173 | { |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 174 | struct mlxsw_sp_mid *mid, *tmp; |
| 175 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 176 | list_del(&bridge_device->list); |
| 177 | if (bridge_device->vlan_enabled) |
| 178 | bridge->vlan_enabled_exists = false; |
| 179 | WARN_ON(!list_empty(&bridge_device->ports_list)); |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 180 | list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) { |
| 181 | list_del(&mid->list); |
| 182 | clear_bit(mid->mid, bridge->mids_bitmap); |
| 183 | kfree(mid); |
| 184 | } |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 185 | kfree(bridge_device); |
| 186 | } |
| 187 | |
| 188 | static struct mlxsw_sp_bridge_device * |
| 189 | mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge, |
| 190 | struct net_device *br_dev) |
| 191 | { |
| 192 | struct mlxsw_sp_bridge_device *bridge_device; |
| 193 | |
| 194 | bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev); |
| 195 | if (bridge_device) |
| 196 | return bridge_device; |
| 197 | |
| 198 | return mlxsw_sp_bridge_device_create(bridge, br_dev); |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge, |
| 203 | struct mlxsw_sp_bridge_device *bridge_device) |
| 204 | { |
| 205 | if (list_empty(&bridge_device->ports_list)) |
| 206 | mlxsw_sp_bridge_device_destroy(bridge, bridge_device); |
| 207 | } |
| 208 | |
| 209 | static struct mlxsw_sp_bridge_port * |
| 210 | __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device, |
| 211 | const struct net_device *brport_dev) |
| 212 | { |
| 213 | struct mlxsw_sp_bridge_port *bridge_port; |
| 214 | |
| 215 | list_for_each_entry(bridge_port, &bridge_device->ports_list, list) { |
| 216 | if (bridge_port->dev == brport_dev) |
| 217 | return bridge_port; |
| 218 | } |
| 219 | |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | static struct mlxsw_sp_bridge_port * |
| 224 | mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge, |
| 225 | struct net_device *brport_dev) |
| 226 | { |
| 227 | struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev); |
| 228 | struct mlxsw_sp_bridge_device *bridge_device; |
| 229 | |
| 230 | if (!br_dev) |
| 231 | return NULL; |
| 232 | |
| 233 | bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev); |
| 234 | if (!bridge_device) |
| 235 | return NULL; |
| 236 | |
| 237 | return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev); |
| 238 | } |
| 239 | |
| 240 | static struct mlxsw_sp_bridge_port * |
| 241 | mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device, |
| 242 | struct net_device *brport_dev) |
| 243 | { |
| 244 | struct mlxsw_sp_bridge_port *bridge_port; |
| 245 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 246 | |
| 247 | bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL); |
| 248 | if (!bridge_port) |
| 249 | return NULL; |
| 250 | |
| 251 | mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev); |
| 252 | bridge_port->lagged = mlxsw_sp_port->lagged; |
| 253 | if (bridge_port->lagged) |
| 254 | bridge_port->lag_id = mlxsw_sp_port->lag_id; |
| 255 | else |
| 256 | bridge_port->system_port = mlxsw_sp_port->local_port; |
| 257 | bridge_port->dev = brport_dev; |
| 258 | bridge_port->bridge_device = bridge_device; |
| 259 | bridge_port->stp_state = BR_STATE_DISABLED; |
| 260 | bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC; |
| 261 | INIT_LIST_HEAD(&bridge_port->vlans_list); |
| 262 | list_add(&bridge_port->list, &bridge_device->ports_list); |
| 263 | bridge_port->ref_count = 1; |
| 264 | |
| 265 | return bridge_port; |
| 266 | } |
| 267 | |
| 268 | static void |
| 269 | mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port) |
| 270 | { |
| 271 | list_del(&bridge_port->list); |
| 272 | WARN_ON(!list_empty(&bridge_port->vlans_list)); |
| 273 | kfree(bridge_port); |
| 274 | } |
| 275 | |
| 276 | static bool |
| 277 | mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port * |
| 278 | bridge_port) |
| 279 | { |
| 280 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_port->dev); |
| 281 | |
| 282 | /* In case ports were pulled from out of a bridged LAG, then |
| 283 | * it's possible the reference count isn't zero, yet the bridge |
| 284 | * port should be destroyed, as it's no longer an upper of ours. |
| 285 | */ |
| 286 | if (!mlxsw_sp && list_empty(&bridge_port->vlans_list)) |
| 287 | return true; |
| 288 | else if (bridge_port->ref_count == 0) |
| 289 | return true; |
| 290 | else |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | static struct mlxsw_sp_bridge_port * |
| 295 | mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge, |
| 296 | struct net_device *brport_dev) |
| 297 | { |
| 298 | struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev); |
| 299 | struct mlxsw_sp_bridge_device *bridge_device; |
| 300 | struct mlxsw_sp_bridge_port *bridge_port; |
| 301 | int err; |
| 302 | |
| 303 | bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev); |
| 304 | if (bridge_port) { |
| 305 | bridge_port->ref_count++; |
| 306 | return bridge_port; |
| 307 | } |
| 308 | |
| 309 | bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev); |
| 310 | if (IS_ERR(bridge_device)) |
| 311 | return ERR_CAST(bridge_device); |
| 312 | |
| 313 | bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev); |
| 314 | if (!bridge_port) { |
| 315 | err = -ENOMEM; |
| 316 | goto err_bridge_port_create; |
| 317 | } |
| 318 | |
| 319 | return bridge_port; |
| 320 | |
| 321 | err_bridge_port_create: |
| 322 | mlxsw_sp_bridge_device_put(bridge, bridge_device); |
| 323 | return ERR_PTR(err); |
| 324 | } |
| 325 | |
| 326 | static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge, |
| 327 | struct mlxsw_sp_bridge_port *bridge_port) |
| 328 | { |
| 329 | struct mlxsw_sp_bridge_device *bridge_device; |
| 330 | |
| 331 | bridge_port->ref_count--; |
| 332 | if (!mlxsw_sp_bridge_port_should_destroy(bridge_port)) |
| 333 | return; |
| 334 | bridge_device = bridge_port->bridge_device; |
| 335 | mlxsw_sp_bridge_port_destroy(bridge_port); |
| 336 | mlxsw_sp_bridge_device_put(bridge, bridge_device); |
| 337 | } |
| 338 | |
| 339 | static struct mlxsw_sp_port_vlan * |
| 340 | mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port, |
| 341 | const struct mlxsw_sp_bridge_device * |
| 342 | bridge_device, |
| 343 | u16 vid) |
| 344 | { |
| 345 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 346 | |
| 347 | list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list, |
| 348 | list) { |
| 349 | if (!mlxsw_sp_port_vlan->bridge_port) |
| 350 | continue; |
| 351 | if (mlxsw_sp_port_vlan->bridge_port->bridge_device != |
| 352 | bridge_device) |
| 353 | continue; |
| 354 | if (bridge_device->vlan_enabled && |
| 355 | mlxsw_sp_port_vlan->vid != vid) |
| 356 | continue; |
| 357 | return mlxsw_sp_port_vlan; |
| 358 | } |
| 359 | |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | static struct mlxsw_sp_port_vlan* |
| 364 | mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port, |
| 365 | u16 fid_index) |
| 366 | { |
| 367 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 368 | |
| 369 | list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list, |
| 370 | list) { |
| 371 | struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid; |
| 372 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 373 | if (fid && mlxsw_sp_fid_index(fid) == fid_index) |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 374 | return mlxsw_sp_port_vlan; |
| 375 | } |
| 376 | |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | static struct mlxsw_sp_bridge_vlan * |
| 381 | mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port, |
| 382 | u16 vid) |
| 383 | { |
| 384 | struct mlxsw_sp_bridge_vlan *bridge_vlan; |
| 385 | |
| 386 | list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) { |
| 387 | if (bridge_vlan->vid == vid) |
| 388 | return bridge_vlan; |
| 389 | } |
| 390 | |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | static struct mlxsw_sp_bridge_vlan * |
| 395 | mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid) |
| 396 | { |
| 397 | struct mlxsw_sp_bridge_vlan *bridge_vlan; |
| 398 | |
| 399 | bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL); |
| 400 | if (!bridge_vlan) |
| 401 | return NULL; |
| 402 | |
| 403 | INIT_LIST_HEAD(&bridge_vlan->port_vlan_list); |
| 404 | bridge_vlan->vid = vid; |
| 405 | list_add(&bridge_vlan->list, &bridge_port->vlans_list); |
| 406 | |
| 407 | return bridge_vlan; |
| 408 | } |
| 409 | |
| 410 | static void |
| 411 | mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan) |
| 412 | { |
| 413 | list_del(&bridge_vlan->list); |
| 414 | WARN_ON(!list_empty(&bridge_vlan->port_vlan_list)); |
| 415 | kfree(bridge_vlan); |
| 416 | } |
| 417 | |
| 418 | static struct mlxsw_sp_bridge_vlan * |
| 419 | mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid) |
| 420 | { |
| 421 | struct mlxsw_sp_bridge_vlan *bridge_vlan; |
| 422 | |
| 423 | bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid); |
| 424 | if (bridge_vlan) |
| 425 | return bridge_vlan; |
| 426 | |
| 427 | return mlxsw_sp_bridge_vlan_create(bridge_port, vid); |
| 428 | } |
| 429 | |
| 430 | static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan) |
| 431 | { |
| 432 | if (list_empty(&bridge_vlan->port_vlan_list)) |
| 433 | mlxsw_sp_bridge_vlan_destroy(bridge_vlan); |
| 434 | } |
| 435 | |
| 436 | static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge, |
| 437 | struct net_device *dev, |
| 438 | unsigned long *brport_flags) |
| 439 | { |
| 440 | struct mlxsw_sp_bridge_port *bridge_port; |
| 441 | |
| 442 | bridge_port = mlxsw_sp_bridge_port_find(bridge, dev); |
| 443 | if (WARN_ON(!bridge_port)) |
| 444 | return; |
| 445 | |
| 446 | memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags)); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 449 | static int mlxsw_sp_port_attr_get(struct net_device *dev, |
| 450 | struct switchdev_attr *attr) |
| 451 | { |
| 452 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 453 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 454 | |
| 455 | switch (attr->id) { |
| 456 | case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: |
| 457 | attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac); |
| 458 | memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac, |
| 459 | attr->u.ppid.id_len); |
| 460 | break; |
| 461 | case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 462 | mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev, |
| 463 | &attr->u.brport_flags); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 464 | break; |
Arkadi Sharshevsky | c7b566c | 2017-06-08 08:44:17 +0200 | [diff] [blame] | 465 | case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT: |
| 466 | attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD; |
| 467 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 468 | default: |
| 469 | return -EOPNOTSUPP; |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 475 | static int |
| 476 | mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 477 | struct mlxsw_sp_bridge_vlan *bridge_vlan, |
| 478 | u8 state) |
| 479 | { |
| 480 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 481 | |
| 482 | list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list, |
| 483 | bridge_vlan_node) { |
| 484 | if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port) |
| 485 | continue; |
| 486 | return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, |
| 487 | bridge_vlan->vid, state); |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 493 | static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 494 | struct switchdev_trans *trans, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 495 | struct net_device *orig_dev, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 496 | u8 state) |
| 497 | { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 498 | struct mlxsw_sp_bridge_port *bridge_port; |
| 499 | struct mlxsw_sp_bridge_vlan *bridge_vlan; |
Ido Schimmel | 45bfe6b | 2017-05-16 19:38:32 +0200 | [diff] [blame] | 500 | int err; |
| 501 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 502 | if (switchdev_trans_ph_prepare(trans)) |
| 503 | return 0; |
| 504 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 505 | /* It's possible we failed to enslave the port, yet this |
| 506 | * operation is executed due to it being deferred. |
| 507 | */ |
| 508 | bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge, |
| 509 | orig_dev); |
| 510 | if (!bridge_port) |
Ido Schimmel | 45bfe6b | 2017-05-16 19:38:32 +0200 | [diff] [blame] | 511 | return 0; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 512 | |
| 513 | list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) { |
| 514 | err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, |
| 515 | bridge_vlan, state); |
| 516 | if (err) |
| 517 | goto err_port_bridge_vlan_stp_set; |
Ido Schimmel | 45bfe6b | 2017-05-16 19:38:32 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 520 | bridge_port->stp_state = state; |
Ido Schimmel | 45bfe6b | 2017-05-16 19:38:32 +0200 | [diff] [blame] | 521 | |
| 522 | return 0; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 523 | |
| 524 | err_port_bridge_vlan_stp_set: |
| 525 | list_for_each_entry_continue_reverse(bridge_vlan, |
| 526 | &bridge_port->vlans_list, list) |
| 527 | mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan, |
| 528 | bridge_port->stp_state); |
| 529 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 532 | static int |
| 533 | mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 534 | struct mlxsw_sp_bridge_vlan *bridge_vlan, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 535 | enum mlxsw_sp_flood_type packet_type, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 536 | bool member) |
Nogah Frankel | eaa7df3 | 2017-02-09 14:54:43 +0100 | [diff] [blame] | 537 | { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 538 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
Nogah Frankel | eaa7df3 | 2017-02-09 14:54:43 +0100 | [diff] [blame] | 539 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 540 | list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list, |
| 541 | bridge_vlan_node) { |
| 542 | if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port) |
| 543 | continue; |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 544 | return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid, |
| 545 | packet_type, |
| 546 | mlxsw_sp_port->local_port, |
| 547 | member); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static int |
| 554 | mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 555 | struct mlxsw_sp_bridge_port *bridge_port, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 556 | enum mlxsw_sp_flood_type packet_type, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 557 | bool member) |
| 558 | { |
| 559 | struct mlxsw_sp_bridge_vlan *bridge_vlan; |
| 560 | int err; |
| 561 | |
| 562 | list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) { |
| 563 | err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 564 | bridge_vlan, |
| 565 | packet_type, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 566 | member); |
| 567 | if (err) |
| 568 | goto err_port_bridge_vlan_flood_set; |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | return 0; |
| 572 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 573 | err_port_bridge_vlan_flood_set: |
| 574 | list_for_each_entry_continue_reverse(bridge_vlan, |
| 575 | &bridge_port->vlans_list, list) |
| 576 | mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 577 | packet_type, !member); |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 578 | return err; |
| 579 | } |
| 580 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 581 | static int |
| 582 | mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 583 | struct mlxsw_sp_bridge_vlan *bridge_vlan, |
| 584 | bool set) |
Nogah Frankel | 90e0f0c | 2017-02-09 14:54:49 +0100 | [diff] [blame] | 585 | { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 586 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 587 | u16 vid = bridge_vlan->vid; |
Nogah Frankel | 90e0f0c | 2017-02-09 14:54:49 +0100 | [diff] [blame] | 588 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 589 | list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list, |
| 590 | bridge_vlan_node) { |
| 591 | if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port) |
| 592 | continue; |
Ido Schimmel | 7cbc427 | 2017-05-16 19:38:33 +0200 | [diff] [blame] | 593 | return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set); |
Ido Schimmel | 89b548f | 2016-08-24 12:00:27 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int |
| 600 | mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 601 | struct mlxsw_sp_bridge_port *bridge_port, |
| 602 | bool set) |
| 603 | { |
| 604 | struct mlxsw_sp_bridge_vlan *bridge_vlan; |
| 605 | int err; |
| 606 | |
| 607 | list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) { |
| 608 | err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port, |
| 609 | bridge_vlan, set); |
Ido Schimmel | 89b548f | 2016-08-24 12:00:27 +0200 | [diff] [blame] | 610 | if (err) |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 611 | goto err_port_bridge_vlan_learning_set; |
Ido Schimmel | 89b548f | 2016-08-24 12:00:27 +0200 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 616 | err_port_bridge_vlan_learning_set: |
| 617 | list_for_each_entry_continue_reverse(bridge_vlan, |
| 618 | &bridge_port->vlans_list, list) |
| 619 | mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port, |
| 620 | bridge_vlan, !set); |
Ido Schimmel | 89b548f | 2016-08-24 12:00:27 +0200 | [diff] [blame] | 621 | return err; |
| 622 | } |
| 623 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 624 | static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 625 | struct switchdev_trans *trans, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 626 | struct net_device *orig_dev, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 627 | unsigned long brport_flags) |
| 628 | { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 629 | struct mlxsw_sp_bridge_port *bridge_port; |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 630 | int err; |
| 631 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 632 | if (switchdev_trans_ph_prepare(trans)) |
| 633 | return 0; |
| 634 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 635 | bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge, |
| 636 | orig_dev); |
Ido Schimmel | 17b334a | 2017-08-04 14:12:29 +0200 | [diff] [blame] | 637 | if (!bridge_port) |
| 638 | return 0; |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 639 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 640 | err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 641 | MLXSW_SP_FLOOD_TYPE_UC, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 642 | brport_flags & BR_FLOOD); |
| 643 | if (err) |
| 644 | return err; |
Ido Schimmel | 89b548f | 2016-08-24 12:00:27 +0200 | [diff] [blame] | 645 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 646 | err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port, |
| 647 | brport_flags & BR_LEARNING); |
| 648 | if (err) |
| 649 | return err; |
| 650 | |
| 651 | memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags)); |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 652 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time) |
| 657 | { |
| 658 | char sfdat_pl[MLXSW_REG_SFDAT_LEN]; |
| 659 | int err; |
| 660 | |
| 661 | mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time); |
| 662 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl); |
| 663 | if (err) |
| 664 | return err; |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 665 | mlxsw_sp->bridge->ageing_time = ageing_time; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 670 | struct switchdev_trans *trans, |
Jiri Pirko | 135f9ec | 2015-10-28 10:17:02 +0100 | [diff] [blame] | 671 | unsigned long ageing_clock_t) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 672 | { |
| 673 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Jiri Pirko | 135f9ec | 2015-10-28 10:17:02 +0100 | [diff] [blame] | 674 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 675 | u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000; |
| 676 | |
Ido Schimmel | 869f63a | 2016-03-08 12:59:33 -0800 | [diff] [blame] | 677 | if (switchdev_trans_ph_prepare(trans)) { |
| 678 | if (ageing_time < MLXSW_SP_MIN_AGEING_TIME || |
| 679 | ageing_time > MLXSW_SP_MAX_AGEING_TIME) |
| 680 | return -ERANGE; |
| 681 | else |
| 682 | return 0; |
| 683 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 684 | |
| 685 | return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time); |
| 686 | } |
| 687 | |
Elad Raz | 26a4ea0 | 2016-01-06 13:01:10 +0100 | [diff] [blame] | 688 | static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 689 | struct switchdev_trans *trans, |
| 690 | struct net_device *orig_dev, |
| 691 | bool vlan_enabled) |
| 692 | { |
| 693 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 694 | struct mlxsw_sp_bridge_device *bridge_device; |
Elad Raz | 26a4ea0 | 2016-01-06 13:01:10 +0100 | [diff] [blame] | 695 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 696 | if (!switchdev_trans_ph_prepare(trans)) |
| 697 | return 0; |
| 698 | |
| 699 | bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev); |
| 700 | if (WARN_ON(!bridge_device)) |
Elad Raz | 26a4ea0 | 2016-01-06 13:01:10 +0100 | [diff] [blame] | 701 | return -EINVAL; |
Elad Raz | 26a4ea0 | 2016-01-06 13:01:10 +0100 | [diff] [blame] | 702 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 703 | if (bridge_device->vlan_enabled == vlan_enabled) |
| 704 | return 0; |
| 705 | |
| 706 | netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n"); |
| 707 | return -EINVAL; |
Elad Raz | 26a4ea0 | 2016-01-06 13:01:10 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Nogah Frankel | dff37b5 | 2017-09-20 16:15:01 +0200 | [diff] [blame] | 710 | static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 711 | struct switchdev_trans *trans, |
| 712 | struct net_device *orig_dev, |
| 713 | bool is_port_mrouter) |
Nogah Frankel | 8ecd459 | 2017-02-09 14:54:47 +0100 | [diff] [blame] | 714 | { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 715 | struct mlxsw_sp_bridge_port *bridge_port; |
Nogah Frankel | 4eb6a3b | 2017-08-22 10:28:11 +0200 | [diff] [blame] | 716 | int err; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 717 | |
Nogah Frankel | 8ecd459 | 2017-02-09 14:54:47 +0100 | [diff] [blame] | 718 | if (switchdev_trans_ph_prepare(trans)) |
| 719 | return 0; |
| 720 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 721 | bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge, |
| 722 | orig_dev); |
Ido Schimmel | 17b334a | 2017-08-04 14:12:29 +0200 | [diff] [blame] | 723 | if (!bridge_port) |
| 724 | return 0; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 725 | |
| 726 | if (!bridge_port->bridge_device->multicast_enabled) |
Nogah Frankel | 4eb6a3b | 2017-08-22 10:28:11 +0200 | [diff] [blame] | 727 | goto out; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 728 | |
Nogah Frankel | 4eb6a3b | 2017-08-22 10:28:11 +0200 | [diff] [blame] | 729 | err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port, |
| 730 | MLXSW_SP_FLOOD_TYPE_MC, |
Nogah Frankel | dff37b5 | 2017-09-20 16:15:01 +0200 | [diff] [blame] | 731 | is_port_mrouter); |
Nogah Frankel | 4eb6a3b | 2017-08-22 10:28:11 +0200 | [diff] [blame] | 732 | if (err) |
| 733 | return err; |
| 734 | |
| 735 | out: |
Nogah Frankel | dff37b5 | 2017-09-20 16:15:01 +0200 | [diff] [blame] | 736 | bridge_port->mrouter = is_port_mrouter; |
Nogah Frankel | 4eb6a3b | 2017-08-22 10:28:11 +0200 | [diff] [blame] | 737 | return 0; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 741 | struct switchdev_trans *trans, |
| 742 | struct net_device *orig_dev, |
| 743 | bool mc_disabled) |
| 744 | { |
| 745 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 746 | struct mlxsw_sp_bridge_device *bridge_device; |
| 747 | struct mlxsw_sp_bridge_port *bridge_port; |
| 748 | int err; |
| 749 | |
| 750 | if (switchdev_trans_ph_prepare(trans)) |
| 751 | return 0; |
| 752 | |
| 753 | /* It's possible we failed to enslave the port, yet this |
| 754 | * operation is executed due to it being deferred. |
| 755 | */ |
| 756 | bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev); |
| 757 | if (!bridge_device) |
| 758 | return 0; |
| 759 | |
| 760 | list_for_each_entry(bridge_port, &bridge_device->ports_list, list) { |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 761 | enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 762 | bool member = mc_disabled ? true : bridge_port->mrouter; |
| 763 | |
| 764 | err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 765 | bridge_port, |
| 766 | packet_type, member); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 767 | if (err) |
| 768 | return err; |
| 769 | } |
| 770 | |
| 771 | bridge_device->multicast_enabled = !mc_disabled; |
Nogah Frankel | 8ecd459 | 2017-02-09 14:54:47 +0100 | [diff] [blame] | 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 776 | static int mlxsw_sp_port_attr_set(struct net_device *dev, |
| 777 | const struct switchdev_attr *attr, |
| 778 | struct switchdev_trans *trans) |
| 779 | { |
| 780 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 781 | int err; |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 782 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 783 | switch (attr->id) { |
| 784 | case SWITCHDEV_ATTR_ID_PORT_STP_STATE: |
| 785 | err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 786 | attr->orig_dev, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 787 | attr->u.stp_state); |
| 788 | break; |
| 789 | case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: |
| 790 | err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 791 | attr->orig_dev, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 792 | attr->u.brport_flags); |
| 793 | break; |
| 794 | case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: |
| 795 | err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans, |
| 796 | attr->u.ageing_time); |
| 797 | break; |
Elad Raz | 26a4ea0 | 2016-01-06 13:01:10 +0100 | [diff] [blame] | 798 | case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: |
| 799 | err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans, |
| 800 | attr->orig_dev, |
| 801 | attr->u.vlan_filtering); |
| 802 | break; |
Nogah Frankel | 8ecd459 | 2017-02-09 14:54:47 +0100 | [diff] [blame] | 803 | case SWITCHDEV_ATTR_ID_PORT_MROUTER: |
Nogah Frankel | dff37b5 | 2017-09-20 16:15:01 +0200 | [diff] [blame] | 804 | err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans, |
| 805 | attr->orig_dev, |
| 806 | attr->u.mrouter); |
Nogah Frankel | 8ecd459 | 2017-02-09 14:54:47 +0100 | [diff] [blame] | 807 | break; |
Nogah Frankel | 90e0f0c | 2017-02-09 14:54:49 +0100 | [diff] [blame] | 808 | case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED: |
| 809 | err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 810 | attr->orig_dev, |
Nogah Frankel | 90e0f0c | 2017-02-09 14:54:49 +0100 | [diff] [blame] | 811 | attr->u.mc_disabled); |
| 812 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 813 | default: |
| 814 | err = -EOPNOTSUPP; |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | return err; |
| 819 | } |
| 820 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 821 | static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port) |
| 822 | { |
| 823 | const struct mlxsw_sp_bridge_device *bridge_device; |
| 824 | |
| 825 | bridge_device = bridge_port->bridge_device; |
| 826 | return !bridge_device->multicast_enabled ? true : bridge_port->mrouter; |
| 827 | } |
| 828 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 829 | static int |
| 830 | mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, |
| 831 | struct mlxsw_sp_bridge_port *bridge_port) |
| 832 | { |
| 833 | struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port; |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 834 | struct mlxsw_sp_bridge_device *bridge_device; |
| 835 | u8 local_port = mlxsw_sp_port->local_port; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 836 | u16 vid = mlxsw_sp_port_vlan->vid; |
| 837 | struct mlxsw_sp_fid *fid; |
| 838 | int err; |
| 839 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 840 | bridge_device = bridge_port->bridge_device; |
| 841 | fid = bridge_device->ops->fid_get(bridge_device, vid); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 842 | if (IS_ERR(fid)) |
| 843 | return PTR_ERR(fid); |
| 844 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 845 | err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, |
| 846 | bridge_port->flags & BR_FLOOD); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 847 | if (err) |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 848 | goto err_fid_uc_flood_set; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 849 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 850 | err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, |
| 851 | mlxsw_sp_mc_flood(bridge_port)); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 852 | if (err) |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 853 | goto err_fid_mc_flood_set; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 854 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 855 | err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, |
| 856 | true); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 857 | if (err) |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 858 | goto err_fid_bc_flood_set; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 859 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 860 | err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 861 | if (err) |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 862 | goto err_fid_port_vid_map; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 863 | |
| 864 | mlxsw_sp_port_vlan->fid = fid; |
| 865 | |
| 866 | return 0; |
| 867 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 868 | err_fid_port_vid_map: |
| 869 | mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false); |
| 870 | err_fid_bc_flood_set: |
| 871 | mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false); |
| 872 | err_fid_mc_flood_set: |
| 873 | mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false); |
| 874 | err_fid_uc_flood_set: |
| 875 | mlxsw_sp_fid_put(fid); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 876 | return err; |
| 877 | } |
| 878 | |
| 879 | static void |
| 880 | mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan) |
| 881 | { |
| 882 | struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 883 | struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid; |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 884 | u8 local_port = mlxsw_sp_port->local_port; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 885 | u16 vid = mlxsw_sp_port_vlan->vid; |
| 886 | |
| 887 | mlxsw_sp_port_vlan->fid = NULL; |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 888 | mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid); |
| 889 | mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false); |
| 890 | mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false); |
| 891 | mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false); |
| 892 | mlxsw_sp_fid_put(fid); |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 893 | } |
| 894 | |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 895 | static u16 |
| 896 | mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port, |
| 897 | u16 vid, bool is_pvid) |
Ido Schimmel | 584d73d | 2016-08-24 12:00:26 +0200 | [diff] [blame] | 898 | { |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 899 | if (is_pvid) |
| 900 | return vid; |
| 901 | else if (mlxsw_sp_port->pvid == vid) |
| 902 | return 0; /* Dis-allow untagged packets */ |
| 903 | else |
| 904 | return mlxsw_sp_port->pvid; |
Ido Schimmel | 584d73d | 2016-08-24 12:00:26 +0200 | [diff] [blame] | 905 | } |
| 906 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 907 | static int |
| 908 | mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, |
| 909 | struct mlxsw_sp_bridge_port *bridge_port) |
| 910 | { |
| 911 | struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port; |
| 912 | struct mlxsw_sp_bridge_vlan *bridge_vlan; |
| 913 | u16 vid = mlxsw_sp_port_vlan->vid; |
| 914 | int err; |
| 915 | |
| 916 | /* No need to continue if only VLAN flags were changed */ |
| 917 | if (mlxsw_sp_port_vlan->bridge_port) |
| 918 | return 0; |
| 919 | |
| 920 | err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port); |
| 921 | if (err) |
| 922 | return err; |
| 923 | |
| 924 | err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, |
| 925 | bridge_port->flags & BR_LEARNING); |
| 926 | if (err) |
| 927 | goto err_port_vid_learning_set; |
| 928 | |
| 929 | err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, |
| 930 | bridge_port->stp_state); |
| 931 | if (err) |
| 932 | goto err_port_vid_stp_set; |
| 933 | |
| 934 | bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid); |
| 935 | if (!bridge_vlan) { |
| 936 | err = -ENOMEM; |
| 937 | goto err_bridge_vlan_get; |
| 938 | } |
| 939 | |
| 940 | list_add(&mlxsw_sp_port_vlan->bridge_vlan_node, |
| 941 | &bridge_vlan->port_vlan_list); |
| 942 | |
| 943 | mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge, |
| 944 | bridge_port->dev); |
| 945 | mlxsw_sp_port_vlan->bridge_port = bridge_port; |
| 946 | |
| 947 | return 0; |
| 948 | |
| 949 | err_bridge_vlan_get: |
| 950 | mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED); |
| 951 | err_port_vid_stp_set: |
| 952 | mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false); |
| 953 | err_port_vid_learning_set: |
| 954 | mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan); |
| 955 | return err; |
| 956 | } |
| 957 | |
| 958 | void |
| 959 | mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan) |
| 960 | { |
| 961 | struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port; |
| 962 | struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid; |
| 963 | struct mlxsw_sp_bridge_vlan *bridge_vlan; |
| 964 | struct mlxsw_sp_bridge_port *bridge_port; |
| 965 | u16 vid = mlxsw_sp_port_vlan->vid; |
| 966 | bool last; |
| 967 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 968 | if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q && |
| 969 | mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D)) |
| 970 | return; |
| 971 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 972 | bridge_port = mlxsw_sp_port_vlan->bridge_port; |
| 973 | bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid); |
| 974 | last = list_is_singular(&bridge_vlan->port_vlan_list); |
| 975 | |
| 976 | list_del(&mlxsw_sp_port_vlan->bridge_vlan_node); |
| 977 | mlxsw_sp_bridge_vlan_put(bridge_vlan); |
| 978 | mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED); |
| 979 | mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false); |
| 980 | if (last) |
| 981 | mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 982 | bridge_port, |
| 983 | mlxsw_sp_fid_index(fid)); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 984 | mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan); |
| 985 | |
| 986 | mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port); |
| 987 | mlxsw_sp_port_vlan->bridge_port = NULL; |
| 988 | } |
| 989 | |
| 990 | static int |
| 991 | mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 992 | struct mlxsw_sp_bridge_port *bridge_port, |
| 993 | u16 vid, bool is_untagged, bool is_pvid) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 994 | { |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 995 | u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 996 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 997 | u16 old_pvid = mlxsw_sp_port->pvid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 998 | int err; |
| 999 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1000 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid); |
| 1001 | if (IS_ERR(mlxsw_sp_port_vlan)) |
| 1002 | return PTR_ERR(mlxsw_sp_port_vlan); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1003 | |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1004 | err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true, |
| 1005 | is_untagged); |
| 1006 | if (err) |
| 1007 | goto err_port_vlan_set; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1008 | |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1009 | err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid); |
| 1010 | if (err) |
| 1011 | goto err_port_pvid_set; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1012 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1013 | err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port); |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1014 | if (err) |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1015 | goto err_port_vlan_bridge_join; |
Ido Schimmel | 584d73d | 2016-08-24 12:00:26 +0200 | [diff] [blame] | 1016 | |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 1017 | return 0; |
| 1018 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1019 | err_port_vlan_bridge_join: |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1020 | mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid); |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 1021 | err_port_pvid_set: |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1022 | mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false); |
| 1023 | err_port_vlan_set: |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1024 | mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan); |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 1025 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1029 | const struct switchdev_obj_port_vlan *vlan, |
| 1030 | struct switchdev_trans *trans) |
| 1031 | { |
Elad Raz | e4a1305 | 2016-01-06 13:01:09 +0100 | [diff] [blame] | 1032 | bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; |
| 1033 | bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1034 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1035 | struct net_device *orig_dev = vlan->obj.orig_dev; |
| 1036 | struct mlxsw_sp_bridge_port *bridge_port; |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1037 | u16 vid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1038 | |
| 1039 | if (switchdev_trans_ph_prepare(trans)) |
| 1040 | return 0; |
| 1041 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1042 | bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev); |
| 1043 | if (WARN_ON(!bridge_port)) |
| 1044 | return -EINVAL; |
| 1045 | |
| 1046 | if (!bridge_port->bridge_device->vlan_enabled) |
| 1047 | return 0; |
| 1048 | |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1049 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { |
| 1050 | int err; |
| 1051 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1052 | err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port, |
| 1053 | vid, flag_untagged, |
| 1054 | flag_pvid); |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1055 | if (err) |
| 1056 | return err; |
| 1057 | } |
| 1058 | |
| 1059 | return 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1062 | static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged) |
| 1063 | { |
| 1064 | return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID : |
| 1065 | MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID; |
| 1066 | } |
| 1067 | |
| 1068 | static int |
| 1069 | mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp, |
| 1070 | struct mlxsw_sp_bridge_port *bridge_port, |
| 1071 | u16 fid_index) |
| 1072 | { |
| 1073 | bool lagged = bridge_port->lagged; |
| 1074 | char sfdf_pl[MLXSW_REG_SFDF_LEN]; |
| 1075 | u16 system_port; |
| 1076 | |
| 1077 | system_port = lagged ? bridge_port->lag_id : bridge_port->system_port; |
| 1078 | mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged)); |
| 1079 | mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index); |
| 1080 | mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port); |
| 1081 | |
| 1082 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl); |
| 1083 | } |
| 1084 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1085 | static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1086 | { |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1087 | return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS : |
| 1088 | MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY; |
| 1089 | } |
| 1090 | |
| 1091 | static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding) |
| 1092 | { |
| 1093 | return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT : |
| 1094 | MLXSW_REG_SFD_OP_WRITE_REMOVE; |
| 1095 | } |
| 1096 | |
Ido Schimmel | 6e095fd | 2016-07-04 08:23:13 +0200 | [diff] [blame] | 1097 | static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 1098 | const char *mac, u16 fid, bool adding, |
| 1099 | enum mlxsw_reg_sfd_rec_action action, |
| 1100 | bool dynamic) |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1101 | { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1102 | char *sfd_pl; |
| 1103 | int err; |
| 1104 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1105 | sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL); |
| 1106 | if (!sfd_pl) |
| 1107 | return -ENOMEM; |
| 1108 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1109 | mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0); |
| 1110 | mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic), |
Ido Schimmel | 6e095fd | 2016-07-04 08:23:13 +0200 | [diff] [blame] | 1111 | mac, fid, action, local_port); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1112 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl); |
| 1113 | kfree(sfd_pl); |
| 1114 | |
| 1115 | return err; |
| 1116 | } |
| 1117 | |
Ido Schimmel | 6e095fd | 2016-07-04 08:23:13 +0200 | [diff] [blame] | 1118 | static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 1119 | const char *mac, u16 fid, bool adding, |
| 1120 | bool dynamic) |
| 1121 | { |
| 1122 | return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding, |
| 1123 | MLXSW_REG_SFD_REC_ACTION_NOP, dynamic); |
| 1124 | } |
| 1125 | |
| 1126 | int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid, |
| 1127 | bool adding) |
| 1128 | { |
| 1129 | return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding, |
| 1130 | MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER, |
| 1131 | false); |
| 1132 | } |
| 1133 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1134 | static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id, |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1135 | const char *mac, u16 fid, u16 lag_vid, |
| 1136 | bool adding, bool dynamic) |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1137 | { |
| 1138 | char *sfd_pl; |
| 1139 | int err; |
| 1140 | |
| 1141 | sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL); |
| 1142 | if (!sfd_pl) |
| 1143 | return -ENOMEM; |
| 1144 | |
| 1145 | mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0); |
| 1146 | mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic), |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1147 | mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP, |
| 1148 | lag_vid, lag_id); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1149 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1150 | kfree(sfd_pl); |
| 1151 | |
| 1152 | return err; |
| 1153 | } |
| 1154 | |
| 1155 | static int |
Arkadi Sharshevsky | af061378 | 2017-06-08 08:44:20 +0200 | [diff] [blame] | 1156 | mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1157 | struct switchdev_notifier_fdb_info *fdb_info, bool adding) |
| 1158 | { |
| 1159 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1160 | struct net_device *orig_dev = fdb_info->info.dev; |
| 1161 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 1162 | struct mlxsw_sp_bridge_device *bridge_device; |
| 1163 | struct mlxsw_sp_bridge_port *bridge_port; |
| 1164 | u16 fid_index, vid; |
| 1165 | |
| 1166 | bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev); |
| 1167 | if (!bridge_port) |
| 1168 | return -EINVAL; |
| 1169 | |
| 1170 | bridge_device = bridge_port->bridge_device; |
| 1171 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port, |
| 1172 | bridge_device, |
| 1173 | fdb_info->vid); |
| 1174 | if (!mlxsw_sp_port_vlan) |
| 1175 | return 0; |
| 1176 | |
| 1177 | fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid); |
| 1178 | vid = mlxsw_sp_port_vlan->vid; |
| 1179 | |
| 1180 | if (!bridge_port->lagged) |
| 1181 | return mlxsw_sp_port_fdb_uc_op(mlxsw_sp, |
| 1182 | bridge_port->system_port, |
| 1183 | fdb_info->addr, fid_index, |
| 1184 | adding, false); |
| 1185 | else |
| 1186 | return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, |
| 1187 | bridge_port->lag_id, |
| 1188 | fdb_info->addr, fid_index, |
| 1189 | vid, adding, false); |
| 1190 | } |
| 1191 | |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1192 | static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr, |
Nogah Frankel | 5f9abc5 | 2017-09-20 16:15:05 +0200 | [diff] [blame^] | 1193 | u16 fid, u16 mid_idx, bool adding) |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1194 | { |
| 1195 | char *sfd_pl; |
| 1196 | int err; |
| 1197 | |
| 1198 | sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL); |
| 1199 | if (!sfd_pl) |
| 1200 | return -ENOMEM; |
| 1201 | |
| 1202 | mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0); |
| 1203 | mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid, |
Nogah Frankel | 5f9abc5 | 2017-09-20 16:15:05 +0200 | [diff] [blame^] | 1204 | MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1205 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl); |
| 1206 | kfree(sfd_pl); |
| 1207 | return err; |
| 1208 | } |
| 1209 | |
Nogah Frankel | 5f9abc5 | 2017-09-20 16:15:05 +0200 | [diff] [blame^] | 1210 | /* clean the an entry from the HW and write there a full new entry */ |
| 1211 | static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, |
| 1212 | u16 mid_idx) |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1213 | { |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1214 | char *smid_pl; |
| 1215 | int err, i; |
| 1216 | |
| 1217 | smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL); |
| 1218 | if (!smid_pl) |
| 1219 | return -ENOMEM; |
| 1220 | |
Nogah Frankel | 5f9abc5 | 2017-09-20 16:15:05 +0200 | [diff] [blame^] | 1221 | mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false); |
| 1222 | for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) { |
| 1223 | if (mlxsw_sp->ports[i]) |
| 1224 | mlxsw_reg_smid_port_mask_set(smid_pl, i, 1); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1225 | } |
Nogah Frankel | 5f9abc5 | 2017-09-20 16:15:05 +0200 | [diff] [blame^] | 1226 | |
| 1227 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl); |
| 1228 | kfree(smid_pl); |
| 1229 | return err; |
| 1230 | } |
| 1231 | |
| 1232 | static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1233 | u16 mid_idx, bool add) |
| 1234 | { |
| 1235 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1236 | char *smid_pl; |
| 1237 | int err; |
| 1238 | |
| 1239 | smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL); |
| 1240 | if (!smid_pl) |
| 1241 | return -ENOMEM; |
| 1242 | |
| 1243 | mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1244 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl); |
| 1245 | kfree(smid_pl); |
| 1246 | return err; |
| 1247 | } |
| 1248 | |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 1249 | static struct |
| 1250 | mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device, |
| 1251 | const unsigned char *addr, |
| 1252 | u16 fid) |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1253 | { |
| 1254 | struct mlxsw_sp_mid *mid; |
| 1255 | |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 1256 | list_for_each_entry(mid, &bridge_device->mids_list, list) { |
Ido Schimmel | 46d0847 | 2016-10-30 10:09:22 +0100 | [diff] [blame] | 1257 | if (ether_addr_equal(mid->addr, addr) && mid->fid == fid) |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1258 | return mid; |
| 1259 | } |
| 1260 | return NULL; |
| 1261 | } |
| 1262 | |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 1263 | static struct |
| 1264 | mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp, |
| 1265 | struct mlxsw_sp_bridge_device *bridge_device, |
| 1266 | const unsigned char *addr, |
| 1267 | u16 fid) |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1268 | { |
| 1269 | struct mlxsw_sp_mid *mid; |
Nogah Frankel | 4cdc35e | 2017-09-20 16:15:02 +0200 | [diff] [blame] | 1270 | size_t alloc_size; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1271 | u16 mid_idx; |
| 1272 | |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 1273 | mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap, |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1274 | MLXSW_SP_MID_MAX); |
| 1275 | if (mid_idx == MLXSW_SP_MID_MAX) |
| 1276 | return NULL; |
| 1277 | |
| 1278 | mid = kzalloc(sizeof(*mid), GFP_KERNEL); |
| 1279 | if (!mid) |
| 1280 | return NULL; |
| 1281 | |
Nogah Frankel | 4cdc35e | 2017-09-20 16:15:02 +0200 | [diff] [blame] | 1282 | alloc_size = sizeof(unsigned long) * |
| 1283 | BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core)); |
| 1284 | mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL); |
| 1285 | if (!mid->ports_in_mid) { |
| 1286 | kfree(mid); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 1290 | set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1291 | ether_addr_copy(mid->addr, addr); |
Ido Schimmel | 46d0847 | 2016-10-30 10:09:22 +0100 | [diff] [blame] | 1292 | mid->fid = fid; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1293 | mid->mid = mid_idx; |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 1294 | list_add_tail(&mid->list, &bridge_device->mids_list); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1295 | |
| 1296 | return mid; |
| 1297 | } |
| 1298 | |
Nogah Frankel | 0161b95 | 2017-09-20 16:15:03 +0200 | [diff] [blame] | 1299 | static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1300 | struct mlxsw_sp_mid *mid) |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1301 | { |
Nogah Frankel | 4cdc35e | 2017-09-20 16:15:02 +0200 | [diff] [blame] | 1302 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1303 | |
| 1304 | clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid); |
Nogah Frankel | 0161b95 | 2017-09-20 16:15:03 +0200 | [diff] [blame] | 1305 | if (bitmap_empty(mid->ports_in_mid, |
| 1306 | mlxsw_core_max_ports(mlxsw_sp->core))) { |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1307 | list_del(&mid->list); |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 1308 | clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap); |
Nogah Frankel | 4cdc35e | 2017-09-20 16:15:02 +0200 | [diff] [blame] | 1309 | kfree(mid->ports_in_mid); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1310 | kfree(mid); |
| 1311 | return 1; |
| 1312 | } |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1317 | const struct switchdev_obj_port_mdb *mdb, |
| 1318 | struct switchdev_trans *trans) |
| 1319 | { |
| 1320 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1321 | struct net_device *orig_dev = mdb->obj.orig_dev; |
| 1322 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1323 | struct net_device *dev = mlxsw_sp_port->dev; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1324 | struct mlxsw_sp_bridge_device *bridge_device; |
| 1325 | struct mlxsw_sp_bridge_port *bridge_port; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1326 | struct mlxsw_sp_mid *mid; |
Nogah Frankel | 0161b95 | 2017-09-20 16:15:03 +0200 | [diff] [blame] | 1327 | bool is_new_mid = false; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1328 | u16 fid_index; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1329 | int err = 0; |
| 1330 | |
| 1331 | if (switchdev_trans_ph_prepare(trans)) |
| 1332 | return 0; |
| 1333 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1334 | bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev); |
Ido Schimmel | 17b334a | 2017-08-04 14:12:29 +0200 | [diff] [blame] | 1335 | if (!bridge_port) |
| 1336 | return 0; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1337 | |
| 1338 | bridge_device = bridge_port->bridge_device; |
| 1339 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port, |
| 1340 | bridge_device, |
| 1341 | mdb->vid); |
Ido Schimmel | 17b334a | 2017-08-04 14:12:29 +0200 | [diff] [blame] | 1342 | if (!mlxsw_sp_port_vlan) |
| 1343 | return 0; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1344 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 1345 | fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1346 | |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 1347 | mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1348 | if (!mid) { |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 1349 | mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr, |
| 1350 | fid_index); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1351 | if (!mid) { |
| 1352 | netdev_err(dev, "Unable to allocate MC group\n"); |
| 1353 | return -ENOMEM; |
| 1354 | } |
Nogah Frankel | 0161b95 | 2017-09-20 16:15:03 +0200 | [diff] [blame] | 1355 | is_new_mid = true; |
Nogah Frankel | 5f9abc5 | 2017-09-20 16:15:05 +0200 | [diff] [blame^] | 1356 | mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid->mid); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1357 | } |
Nogah Frankel | 4cdc35e | 2017-09-20 16:15:02 +0200 | [diff] [blame] | 1358 | set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1359 | |
Nogah Frankel | 5f9abc5 | 2017-09-20 16:15:05 +0200 | [diff] [blame^] | 1360 | err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1361 | if (err) { |
| 1362 | netdev_err(dev, "Unable to set SMID\n"); |
| 1363 | goto err_out; |
| 1364 | } |
| 1365 | |
Nogah Frankel | 0161b95 | 2017-09-20 16:15:03 +0200 | [diff] [blame] | 1366 | if (is_new_mid) { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1367 | err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid_index, |
| 1368 | mid->mid, true); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1369 | if (err) { |
| 1370 | netdev_err(dev, "Unable to set MC SFD\n"); |
| 1371 | goto err_out; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | return 0; |
| 1376 | |
| 1377 | err_out: |
Nogah Frankel | 0161b95 | 2017-09-20 16:15:03 +0200 | [diff] [blame] | 1378 | mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1379 | return err; |
| 1380 | } |
| 1381 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1382 | static int mlxsw_sp_port_obj_add(struct net_device *dev, |
| 1383 | const struct switchdev_obj *obj, |
| 1384 | struct switchdev_trans *trans) |
| 1385 | { |
| 1386 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1387 | int err = 0; |
| 1388 | |
| 1389 | switch (obj->id) { |
| 1390 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
| 1391 | err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, |
| 1392 | SWITCHDEV_OBJ_PORT_VLAN(obj), |
| 1393 | trans); |
| 1394 | break; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1395 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 1396 | err = mlxsw_sp_port_mdb_add(mlxsw_sp_port, |
| 1397 | SWITCHDEV_OBJ_PORT_MDB(obj), |
| 1398 | trans); |
| 1399 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1400 | default: |
| 1401 | err = -EOPNOTSUPP; |
| 1402 | break; |
| 1403 | } |
| 1404 | |
| 1405 | return err; |
| 1406 | } |
| 1407 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1408 | static void |
| 1409 | mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1410 | struct mlxsw_sp_bridge_port *bridge_port, u16 vid) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1411 | { |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1412 | u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1413 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1414 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1415 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid); |
| 1416 | if (WARN_ON(!mlxsw_sp_port_vlan)) |
| 1417 | return; |
| 1418 | |
| 1419 | mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan); |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1420 | mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid); |
| 1421 | mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1422 | mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1426 | const struct switchdev_obj_port_vlan *vlan) |
| 1427 | { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1428 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1429 | struct net_device *orig_dev = vlan->obj.orig_dev; |
| 1430 | struct mlxsw_sp_bridge_port *bridge_port; |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1431 | u16 vid; |
| 1432 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1433 | bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev); |
| 1434 | if (WARN_ON(!bridge_port)) |
| 1435 | return -EINVAL; |
| 1436 | |
| 1437 | if (!bridge_port->bridge_device->vlan_enabled) |
| 1438 | return 0; |
| 1439 | |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1440 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1441 | mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid); |
Ido Schimmel | fe9ccc7 | 2017-05-16 19:38:31 +0200 | [diff] [blame] | 1442 | |
| 1443 | return 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1446 | static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1447 | const struct switchdev_obj_port_mdb *mdb) |
| 1448 | { |
| 1449 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1450 | struct net_device *orig_dev = mdb->obj.orig_dev; |
| 1451 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 1452 | struct mlxsw_sp_bridge_device *bridge_device; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1453 | struct net_device *dev = mlxsw_sp_port->dev; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1454 | struct mlxsw_sp_bridge_port *bridge_port; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1455 | struct mlxsw_sp_mid *mid; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1456 | u16 fid_index; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1457 | u16 mid_idx; |
| 1458 | int err = 0; |
| 1459 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1460 | bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev); |
Ido Schimmel | 17b334a | 2017-08-04 14:12:29 +0200 | [diff] [blame] | 1461 | if (!bridge_port) |
| 1462 | return 0; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1463 | |
| 1464 | bridge_device = bridge_port->bridge_device; |
| 1465 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port, |
| 1466 | bridge_device, |
| 1467 | mdb->vid); |
Ido Schimmel | 17b334a | 2017-08-04 14:12:29 +0200 | [diff] [blame] | 1468 | if (!mlxsw_sp_port_vlan) |
| 1469 | return 0; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1470 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 1471 | fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1472 | |
Nogah Frankel | b80888a | 2017-09-20 16:15:04 +0200 | [diff] [blame] | 1473 | mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1474 | if (!mid) { |
| 1475 | netdev_err(dev, "Unable to remove port from MC DB\n"); |
| 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | |
Nogah Frankel | 5f9abc5 | 2017-09-20 16:15:05 +0200 | [diff] [blame^] | 1479 | err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1480 | if (err) |
| 1481 | netdev_err(dev, "Unable to remove port from SMID\n"); |
| 1482 | |
| 1483 | mid_idx = mid->mid; |
Nogah Frankel | 0161b95 | 2017-09-20 16:15:03 +0200 | [diff] [blame] | 1484 | if (mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid)) { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1485 | err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid_index, |
| 1486 | mid_idx, false); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1487 | if (err) |
| 1488 | netdev_err(dev, "Unable to remove MC SFD\n"); |
| 1489 | } |
| 1490 | |
| 1491 | return err; |
| 1492 | } |
| 1493 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1494 | static int mlxsw_sp_port_obj_del(struct net_device *dev, |
| 1495 | const struct switchdev_obj *obj) |
| 1496 | { |
| 1497 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1498 | int err = 0; |
| 1499 | |
| 1500 | switch (obj->id) { |
| 1501 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
| 1502 | err = mlxsw_sp_port_vlans_del(mlxsw_sp_port, |
| 1503 | SWITCHDEV_OBJ_PORT_VLAN(obj)); |
| 1504 | break; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1505 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 1506 | err = mlxsw_sp_port_mdb_del(mlxsw_sp_port, |
| 1507 | SWITCHDEV_OBJ_PORT_MDB(obj)); |
Dan Carpenter | 00ae40e | 2016-01-13 15:28:23 +0300 | [diff] [blame] | 1508 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1509 | default: |
| 1510 | err = -EOPNOTSUPP; |
| 1511 | break; |
| 1512 | } |
| 1513 | |
| 1514 | return err; |
| 1515 | } |
| 1516 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1517 | static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp, |
| 1518 | u16 lag_id) |
| 1519 | { |
| 1520 | struct mlxsw_sp_port *mlxsw_sp_port; |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 1521 | u64 max_lag_members; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1522 | int i; |
| 1523 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 1524 | max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core, |
| 1525 | MAX_LAG_MEMBERS); |
| 1526 | for (i = 0; i < max_lag_members; i++) { |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1527 | mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i); |
| 1528 | if (mlxsw_sp_port) |
| 1529 | return mlxsw_sp_port; |
| 1530 | } |
| 1531 | return NULL; |
| 1532 | } |
| 1533 | |
Jiri Pirko | c7070fc | 2015-10-28 10:17:05 +0100 | [diff] [blame] | 1534 | static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1535 | .switchdev_port_attr_get = mlxsw_sp_port_attr_get, |
| 1536 | .switchdev_port_attr_set = mlxsw_sp_port_attr_set, |
| 1537 | .switchdev_port_obj_add = mlxsw_sp_port_obj_add, |
| 1538 | .switchdev_port_obj_del = mlxsw_sp_port_obj_del, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1539 | }; |
| 1540 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1541 | static int |
| 1542 | mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device, |
| 1543 | struct mlxsw_sp_bridge_port *bridge_port, |
| 1544 | struct mlxsw_sp_port *mlxsw_sp_port) |
| 1545 | { |
| 1546 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 1547 | |
| 1548 | if (is_vlan_dev(bridge_port->dev)) |
| 1549 | return -EINVAL; |
| 1550 | |
| 1551 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1); |
| 1552 | if (WARN_ON(!mlxsw_sp_port_vlan)) |
| 1553 | return -EINVAL; |
| 1554 | |
| 1555 | /* Let VLAN-aware bridge take care of its own VLANs */ |
| 1556 | mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan); |
| 1557 | |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | static void |
| 1562 | mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device, |
| 1563 | struct mlxsw_sp_bridge_port *bridge_port, |
| 1564 | struct mlxsw_sp_port *mlxsw_sp_port) |
| 1565 | { |
| 1566 | mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1); |
| 1567 | /* Make sure untagged frames are allowed to ingress */ |
| 1568 | mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1); |
| 1569 | } |
| 1570 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 1571 | static struct mlxsw_sp_fid * |
| 1572 | mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device, |
| 1573 | u16 vid) |
| 1574 | { |
| 1575 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev); |
| 1576 | |
| 1577 | return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid); |
| 1578 | } |
| 1579 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1580 | static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = { |
| 1581 | .port_join = mlxsw_sp_bridge_8021q_port_join, |
| 1582 | .port_leave = mlxsw_sp_bridge_8021q_port_leave, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 1583 | .fid_get = mlxsw_sp_bridge_8021q_fid_get, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1584 | }; |
| 1585 | |
| 1586 | static bool |
| 1587 | mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port, |
| 1588 | const struct net_device *br_dev) |
| 1589 | { |
| 1590 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 1591 | |
| 1592 | list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list, |
| 1593 | list) { |
| 1594 | if (mlxsw_sp_port_vlan->bridge_port && |
| 1595 | mlxsw_sp_port_vlan->bridge_port->bridge_device->dev == |
| 1596 | br_dev) |
| 1597 | return true; |
| 1598 | } |
| 1599 | |
| 1600 | return false; |
| 1601 | } |
| 1602 | |
| 1603 | static int |
| 1604 | mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device, |
| 1605 | struct mlxsw_sp_bridge_port *bridge_port, |
| 1606 | struct mlxsw_sp_port *mlxsw_sp_port) |
| 1607 | { |
| 1608 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1609 | u16 vid; |
| 1610 | |
| 1611 | if (!is_vlan_dev(bridge_port->dev)) |
| 1612 | return -EINVAL; |
| 1613 | vid = vlan_dev_vlan_id(bridge_port->dev); |
| 1614 | |
| 1615 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid); |
| 1616 | if (WARN_ON(!mlxsw_sp_port_vlan)) |
| 1617 | return -EINVAL; |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1618 | |
| 1619 | if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) { |
| 1620 | netdev_err(mlxsw_sp_port->dev, "Can't bridge VLAN uppers of the same port\n"); |
| 1621 | return -EINVAL; |
| 1622 | } |
| 1623 | |
| 1624 | /* Port is no longer usable as a router interface */ |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 1625 | if (mlxsw_sp_port_vlan->fid) |
| 1626 | mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1627 | |
| 1628 | return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port); |
| 1629 | } |
| 1630 | |
| 1631 | static void |
| 1632 | mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device, |
| 1633 | struct mlxsw_sp_bridge_port *bridge_port, |
| 1634 | struct mlxsw_sp_port *mlxsw_sp_port) |
| 1635 | { |
| 1636 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 1637 | u16 vid = vlan_dev_vlan_id(bridge_port->dev); |
| 1638 | |
| 1639 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid); |
| 1640 | if (WARN_ON(!mlxsw_sp_port_vlan)) |
| 1641 | return; |
| 1642 | |
| 1643 | mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan); |
| 1644 | } |
| 1645 | |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 1646 | static struct mlxsw_sp_fid * |
| 1647 | mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device, |
| 1648 | u16 vid) |
| 1649 | { |
| 1650 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev); |
| 1651 | |
| 1652 | return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex); |
| 1653 | } |
| 1654 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1655 | static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = { |
| 1656 | .port_join = mlxsw_sp_bridge_8021d_port_join, |
| 1657 | .port_leave = mlxsw_sp_bridge_8021d_port_leave, |
Ido Schimmel | a110748 | 2017-05-26 08:37:39 +0200 | [diff] [blame] | 1658 | .fid_get = mlxsw_sp_bridge_8021d_fid_get, |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1659 | }; |
| 1660 | |
| 1661 | int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1662 | struct net_device *brport_dev, |
| 1663 | struct net_device *br_dev) |
| 1664 | { |
| 1665 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1666 | struct mlxsw_sp_bridge_device *bridge_device; |
| 1667 | struct mlxsw_sp_bridge_port *bridge_port; |
| 1668 | int err; |
| 1669 | |
| 1670 | bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev); |
| 1671 | if (IS_ERR(bridge_port)) |
| 1672 | return PTR_ERR(bridge_port); |
| 1673 | bridge_device = bridge_port->bridge_device; |
| 1674 | |
| 1675 | err = bridge_device->ops->port_join(bridge_device, bridge_port, |
| 1676 | mlxsw_sp_port); |
| 1677 | if (err) |
| 1678 | goto err_port_join; |
| 1679 | |
| 1680 | return 0; |
| 1681 | |
| 1682 | err_port_join: |
| 1683 | mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port); |
| 1684 | return err; |
| 1685 | } |
| 1686 | |
| 1687 | void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1688 | struct net_device *brport_dev, |
| 1689 | struct net_device *br_dev) |
| 1690 | { |
| 1691 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1692 | struct mlxsw_sp_bridge_device *bridge_device; |
| 1693 | struct mlxsw_sp_bridge_port *bridge_port; |
| 1694 | |
| 1695 | bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev); |
| 1696 | if (!bridge_device) |
| 1697 | return; |
| 1698 | bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev); |
| 1699 | if (!bridge_port) |
| 1700 | return; |
| 1701 | |
| 1702 | bridge_device->ops->port_leave(bridge_device, bridge_port, |
| 1703 | mlxsw_sp_port); |
| 1704 | mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port); |
| 1705 | } |
| 1706 | |
Arkadi Sharshevsky | 1b40dc3 | 2017-06-08 08:44:19 +0200 | [diff] [blame] | 1707 | static void |
| 1708 | mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type, |
| 1709 | const char *mac, u16 vid, |
| 1710 | struct net_device *dev) |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1711 | { |
| 1712 | struct switchdev_notifier_fdb_info info; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1713 | |
Arkadi Sharshevsky | a989cdb | 2017-06-08 08:44:16 +0200 | [diff] [blame] | 1714 | info.addr = mac; |
| 1715 | info.vid = vid; |
Arkadi Sharshevsky | 1b40dc3 | 2017-06-08 08:44:19 +0200 | [diff] [blame] | 1716 | call_switchdev_notifiers(type, dev, &info.info); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1717 | } |
| 1718 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1719 | static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp, |
| 1720 | char *sfn_pl, int rec_index, |
| 1721 | bool adding) |
| 1722 | { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1723 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 1724 | struct mlxsw_sp_bridge_device *bridge_device; |
| 1725 | struct mlxsw_sp_bridge_port *bridge_port; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1726 | struct mlxsw_sp_port *mlxsw_sp_port; |
Arkadi Sharshevsky | 1b40dc3 | 2017-06-08 08:44:19 +0200 | [diff] [blame] | 1727 | enum switchdev_notifier_type type; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1728 | char mac[ETH_ALEN]; |
| 1729 | u8 local_port; |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1730 | u16 vid, fid; |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1731 | bool do_notification = true; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1732 | int err; |
| 1733 | |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1734 | mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1735 | mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 1736 | if (!mlxsw_sp_port) { |
| 1737 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n"); |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1738 | goto just_remove; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1739 | } |
| 1740 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1741 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid); |
| 1742 | if (!mlxsw_sp_port_vlan) { |
| 1743 | netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n"); |
| 1744 | goto just_remove; |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1745 | } |
| 1746 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1747 | bridge_port = mlxsw_sp_port_vlan->bridge_port; |
| 1748 | if (!bridge_port) { |
| 1749 | netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n"); |
| 1750 | goto just_remove; |
| 1751 | } |
| 1752 | |
| 1753 | bridge_device = bridge_port->bridge_device; |
| 1754 | vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0; |
| 1755 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1756 | do_fdb_op: |
Jiri Pirko | 2fa9d45 | 2016-01-07 11:50:29 +0100 | [diff] [blame] | 1757 | err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1758 | adding, true); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1759 | if (err) { |
Ido Schimmel | c0e01ea | 2017-05-18 13:03:52 +0200 | [diff] [blame] | 1760 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n"); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1761 | return; |
| 1762 | } |
| 1763 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1764 | if (!do_notification) |
| 1765 | return; |
Arkadi Sharshevsky | 1b40dc3 | 2017-06-08 08:44:19 +0200 | [diff] [blame] | 1766 | type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE; |
| 1767 | mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev); |
Arkadi Sharshevsky | a989cdb | 2017-06-08 08:44:16 +0200 | [diff] [blame] | 1768 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1769 | return; |
| 1770 | |
| 1771 | just_remove: |
| 1772 | adding = false; |
| 1773 | do_notification = false; |
| 1774 | goto do_fdb_op; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1775 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1776 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1777 | static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp, |
| 1778 | char *sfn_pl, int rec_index, |
| 1779 | bool adding) |
| 1780 | { |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1781 | struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; |
| 1782 | struct mlxsw_sp_bridge_device *bridge_device; |
| 1783 | struct mlxsw_sp_bridge_port *bridge_port; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1784 | struct mlxsw_sp_port *mlxsw_sp_port; |
Arkadi Sharshevsky | 1b40dc3 | 2017-06-08 08:44:19 +0200 | [diff] [blame] | 1785 | enum switchdev_notifier_type type; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1786 | char mac[ETH_ALEN]; |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1787 | u16 lag_vid = 0; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1788 | u16 lag_id; |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1789 | u16 vid, fid; |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1790 | bool do_notification = true; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1791 | int err; |
| 1792 | |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1793 | mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1794 | mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id); |
| 1795 | if (!mlxsw_sp_port) { |
| 1796 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n"); |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1797 | goto just_remove; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1798 | } |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1799 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1800 | mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid); |
| 1801 | if (!mlxsw_sp_port_vlan) { |
| 1802 | netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n"); |
| 1803 | goto just_remove; |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1804 | } |
| 1805 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 1806 | bridge_port = mlxsw_sp_port_vlan->bridge_port; |
| 1807 | if (!bridge_port) { |
| 1808 | netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n"); |
| 1809 | goto just_remove; |
| 1810 | } |
| 1811 | |
| 1812 | bridge_device = bridge_port->bridge_device; |
| 1813 | vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0; |
| 1814 | lag_vid = mlxsw_sp_port_vlan->vid; |
| 1815 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1816 | do_fdb_op: |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1817 | err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid, |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1818 | adding, true); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1819 | if (err) { |
Ido Schimmel | c0e01ea | 2017-05-18 13:03:52 +0200 | [diff] [blame] | 1820 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n"); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1821 | return; |
| 1822 | } |
| 1823 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1824 | if (!do_notification) |
| 1825 | return; |
Arkadi Sharshevsky | 1b40dc3 | 2017-06-08 08:44:19 +0200 | [diff] [blame] | 1826 | type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE; |
| 1827 | mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev); |
Arkadi Sharshevsky | a989cdb | 2017-06-08 08:44:16 +0200 | [diff] [blame] | 1828 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1829 | return; |
| 1830 | |
| 1831 | just_remove: |
| 1832 | adding = false; |
| 1833 | do_notification = false; |
| 1834 | goto do_fdb_op; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp, |
| 1838 | char *sfn_pl, int rec_index) |
| 1839 | { |
| 1840 | switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) { |
| 1841 | case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC: |
| 1842 | mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl, |
| 1843 | rec_index, true); |
| 1844 | break; |
| 1845 | case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC: |
| 1846 | mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl, |
| 1847 | rec_index, false); |
| 1848 | break; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1849 | case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG: |
| 1850 | mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl, |
| 1851 | rec_index, true); |
| 1852 | break; |
| 1853 | case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG: |
| 1854 | mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl, |
| 1855 | rec_index, false); |
| 1856 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp) |
| 1861 | { |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 1862 | struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge; |
| 1863 | |
| 1864 | mlxsw_core_schedule_dw(&bridge->fdb_notify.dw, |
| 1865 | msecs_to_jiffies(bridge->fdb_notify.interval)); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | static void mlxsw_sp_fdb_notify_work(struct work_struct *work) |
| 1869 | { |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 1870 | struct mlxsw_sp_bridge *bridge; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1871 | struct mlxsw_sp *mlxsw_sp; |
| 1872 | char *sfn_pl; |
| 1873 | u8 num_rec; |
| 1874 | int i; |
| 1875 | int err; |
| 1876 | |
| 1877 | sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL); |
| 1878 | if (!sfn_pl) |
| 1879 | return; |
| 1880 | |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 1881 | bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work); |
| 1882 | mlxsw_sp = bridge->mlxsw_sp; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1883 | |
Ido Schimmel | 4f2c6ae | 2016-01-27 15:16:43 +0100 | [diff] [blame] | 1884 | rtnl_lock(); |
Ido Schimmel | 1803e0f | 2016-08-24 12:00:23 +0200 | [diff] [blame] | 1885 | mlxsw_reg_sfn_pack(sfn_pl); |
| 1886 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl); |
| 1887 | if (err) { |
| 1888 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n"); |
| 1889 | goto out; |
| 1890 | } |
| 1891 | num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl); |
| 1892 | for (i = 0; i < num_rec; i++) |
| 1893 | mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1894 | |
Ido Schimmel | 1803e0f | 2016-08-24 12:00:23 +0200 | [diff] [blame] | 1895 | out: |
Ido Schimmel | 4f2c6ae | 2016-01-27 15:16:43 +0100 | [diff] [blame] | 1896 | rtnl_unlock(); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1897 | kfree(sfn_pl); |
| 1898 | mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp); |
| 1899 | } |
| 1900 | |
Arkadi Sharshevsky | af061378 | 2017-06-08 08:44:20 +0200 | [diff] [blame] | 1901 | struct mlxsw_sp_switchdev_event_work { |
| 1902 | struct work_struct work; |
| 1903 | struct switchdev_notifier_fdb_info fdb_info; |
| 1904 | struct net_device *dev; |
| 1905 | unsigned long event; |
| 1906 | }; |
| 1907 | |
| 1908 | static void mlxsw_sp_switchdev_event_work(struct work_struct *work) |
| 1909 | { |
| 1910 | struct mlxsw_sp_switchdev_event_work *switchdev_work = |
| 1911 | container_of(work, struct mlxsw_sp_switchdev_event_work, work); |
| 1912 | struct net_device *dev = switchdev_work->dev; |
| 1913 | struct switchdev_notifier_fdb_info *fdb_info; |
| 1914 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 1915 | int err; |
| 1916 | |
| 1917 | rtnl_lock(); |
| 1918 | mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev); |
| 1919 | if (!mlxsw_sp_port) |
| 1920 | goto out; |
| 1921 | |
| 1922 | switch (switchdev_work->event) { |
| 1923 | case SWITCHDEV_FDB_ADD_TO_DEVICE: |
| 1924 | fdb_info = &switchdev_work->fdb_info; |
| 1925 | err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true); |
| 1926 | if (err) |
| 1927 | break; |
| 1928 | mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, |
| 1929 | fdb_info->addr, |
| 1930 | fdb_info->vid, dev); |
| 1931 | break; |
| 1932 | case SWITCHDEV_FDB_DEL_TO_DEVICE: |
| 1933 | fdb_info = &switchdev_work->fdb_info; |
| 1934 | mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false); |
| 1935 | break; |
| 1936 | } |
| 1937 | |
| 1938 | out: |
| 1939 | rtnl_unlock(); |
| 1940 | kfree(switchdev_work->fdb_info.addr); |
| 1941 | kfree(switchdev_work); |
| 1942 | dev_put(dev); |
| 1943 | } |
| 1944 | |
| 1945 | /* Called under rcu_read_lock() */ |
| 1946 | static int mlxsw_sp_switchdev_event(struct notifier_block *unused, |
| 1947 | unsigned long event, void *ptr) |
| 1948 | { |
| 1949 | struct net_device *dev = switchdev_notifier_info_to_dev(ptr); |
| 1950 | struct mlxsw_sp_switchdev_event_work *switchdev_work; |
| 1951 | struct switchdev_notifier_fdb_info *fdb_info = ptr; |
| 1952 | |
| 1953 | if (!mlxsw_sp_port_dev_lower_find_rcu(dev)) |
| 1954 | return NOTIFY_DONE; |
| 1955 | |
| 1956 | switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); |
| 1957 | if (!switchdev_work) |
| 1958 | return NOTIFY_BAD; |
| 1959 | |
| 1960 | INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work); |
| 1961 | switchdev_work->dev = dev; |
| 1962 | switchdev_work->event = event; |
| 1963 | |
| 1964 | switch (event) { |
| 1965 | case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */ |
| 1966 | case SWITCHDEV_FDB_DEL_TO_DEVICE: |
| 1967 | memcpy(&switchdev_work->fdb_info, ptr, |
| 1968 | sizeof(switchdev_work->fdb_info)); |
| 1969 | switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); |
Ido Schimmel | 6f49793 | 2017-07-12 09:12:55 +0200 | [diff] [blame] | 1970 | if (!switchdev_work->fdb_info.addr) |
| 1971 | goto err_addr_alloc; |
Arkadi Sharshevsky | af061378 | 2017-06-08 08:44:20 +0200 | [diff] [blame] | 1972 | ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, |
| 1973 | fdb_info->addr); |
| 1974 | /* Take a reference on the device. This can be either |
| 1975 | * upper device containig mlxsw_sp_port or just a |
| 1976 | * mlxsw_sp_port |
| 1977 | */ |
| 1978 | dev_hold(dev); |
| 1979 | break; |
| 1980 | default: |
| 1981 | kfree(switchdev_work); |
| 1982 | return NOTIFY_DONE; |
| 1983 | } |
| 1984 | |
| 1985 | mlxsw_core_schedule_work(&switchdev_work->work); |
| 1986 | |
| 1987 | return NOTIFY_DONE; |
Ido Schimmel | 6f49793 | 2017-07-12 09:12:55 +0200 | [diff] [blame] | 1988 | |
| 1989 | err_addr_alloc: |
| 1990 | kfree(switchdev_work); |
| 1991 | return NOTIFY_BAD; |
Arkadi Sharshevsky | af061378 | 2017-06-08 08:44:20 +0200 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | static struct notifier_block mlxsw_sp_switchdev_notifier = { |
| 1995 | .notifier_call = mlxsw_sp_switchdev_event, |
| 1996 | }; |
| 1997 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1998 | static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp) |
| 1999 | { |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 2000 | struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2001 | int err; |
| 2002 | |
| 2003 | err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME); |
| 2004 | if (err) { |
| 2005 | dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n"); |
| 2006 | return err; |
| 2007 | } |
Arkadi Sharshevsky | af061378 | 2017-06-08 08:44:20 +0200 | [diff] [blame] | 2008 | |
| 2009 | err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier); |
| 2010 | if (err) { |
| 2011 | dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n"); |
| 2012 | return err; |
| 2013 | } |
| 2014 | |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 2015 | INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work); |
| 2016 | bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2017 | mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp); |
| 2018 | return 0; |
| 2019 | } |
| 2020 | |
| 2021 | static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp) |
| 2022 | { |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 2023 | cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw); |
Arkadi Sharshevsky | af061378 | 2017-06-08 08:44:20 +0200 | [diff] [blame] | 2024 | unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier); |
| 2025 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2026 | } |
| 2027 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2028 | int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp) |
| 2029 | { |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 2030 | struct mlxsw_sp_bridge *bridge; |
| 2031 | |
| 2032 | bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL); |
| 2033 | if (!bridge) |
| 2034 | return -ENOMEM; |
| 2035 | mlxsw_sp->bridge = bridge; |
| 2036 | bridge->mlxsw_sp = mlxsw_sp; |
| 2037 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 2038 | INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list); |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 2039 | |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 2040 | bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops; |
| 2041 | bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops; |
| 2042 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2043 | return mlxsw_sp_fdb_init(mlxsw_sp); |
| 2044 | } |
| 2045 | |
| 2046 | void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp) |
| 2047 | { |
| 2048 | mlxsw_sp_fdb_fini(mlxsw_sp); |
Ido Schimmel | c57529e | 2017-05-26 08:37:31 +0200 | [diff] [blame] | 2049 | WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list)); |
Ido Schimmel | 5f6935c | 2017-05-16 19:38:26 +0200 | [diff] [blame] | 2050 | kfree(mlxsw_sp->bridge); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2051 | } |
| 2052 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2053 | void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port) |
| 2054 | { |
| 2055 | mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops; |
| 2056 | } |
| 2057 | |
| 2058 | void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port) |
| 2059 | { |
| 2060 | } |