linux/drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
<<
>>
Prefs
   1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
   2/* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
   3
   4#include <linux/kernel.h>
   5#include <linux/types.h>
   6#include <linux/netdevice.h>
   7#include <linux/etherdevice.h>
   8#include <linux/slab.h>
   9#include <linux/device.h>
  10#include <linux/skbuff.h>
  11#include <linux/if_vlan.h>
  12#include <linux/if_bridge.h>
  13#include <linux/workqueue.h>
  14#include <linux/jiffies.h>
  15#include <linux/rtnetlink.h>
  16#include <linux/netlink.h>
  17#include <net/switchdev.h>
  18#include <net/vxlan.h>
  19
  20#include "spectrum_span.h"
  21#include "spectrum_switchdev.h"
  22#include "spectrum.h"
  23#include "core.h"
  24#include "reg.h"
  25
  26struct mlxsw_sp_bridge_ops;
  27
  28struct mlxsw_sp_bridge {
  29        struct mlxsw_sp *mlxsw_sp;
  30        struct {
  31                struct delayed_work dw;
  32#define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
  33                unsigned int interval; /* ms */
  34        } fdb_notify;
  35#define MLXSW_SP_MIN_AGEING_TIME 10
  36#define MLXSW_SP_MAX_AGEING_TIME 1000000
  37#define MLXSW_SP_DEFAULT_AGEING_TIME 300
  38        u32 ageing_time;
  39        bool vlan_enabled_exists;
  40        struct list_head bridges_list;
  41        DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX);
  42        const struct mlxsw_sp_bridge_ops *bridge_8021q_ops;
  43        const struct mlxsw_sp_bridge_ops *bridge_8021d_ops;
  44        const struct mlxsw_sp_bridge_ops *bridge_8021ad_ops;
  45};
  46
  47struct mlxsw_sp_bridge_device {
  48        struct net_device *dev;
  49        struct list_head list;
  50        struct list_head ports_list;
  51        struct list_head mids_list;
  52        u8 vlan_enabled:1,
  53           multicast_enabled:1,
  54           mrouter:1;
  55        const struct mlxsw_sp_bridge_ops *ops;
  56};
  57
  58struct mlxsw_sp_bridge_port {
  59        struct net_device *dev;
  60        struct mlxsw_sp_bridge_device *bridge_device;
  61        struct list_head list;
  62        struct list_head vlans_list;
  63        unsigned int ref_count;
  64        u8 stp_state;
  65        unsigned long flags;
  66        bool mrouter;
  67        bool lagged;
  68        union {
  69                u16 lag_id;
  70                u16 system_port;
  71        };
  72};
  73
  74struct mlxsw_sp_bridge_vlan {
  75        struct list_head list;
  76        struct list_head port_vlan_list;
  77        u16 vid;
  78};
  79
  80struct mlxsw_sp_bridge_ops {
  81        int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
  82                         struct mlxsw_sp_bridge_port *bridge_port,
  83                         struct mlxsw_sp_port *mlxsw_sp_port,
  84                         struct netlink_ext_ack *extack);
  85        void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device,
  86                           struct mlxsw_sp_bridge_port *bridge_port,
  87                           struct mlxsw_sp_port *mlxsw_sp_port);
  88        int (*vxlan_join)(struct mlxsw_sp_bridge_device *bridge_device,
  89                          const struct net_device *vxlan_dev, u16 vid,
  90                          struct netlink_ext_ack *extack);
  91        struct mlxsw_sp_fid *
  92                (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
  93                           u16 vid, struct netlink_ext_ack *extack);
  94        struct mlxsw_sp_fid *
  95                (*fid_lookup)(struct mlxsw_sp_bridge_device *bridge_device,
  96                              u16 vid);
  97        u16 (*fid_vid)(struct mlxsw_sp_bridge_device *bridge_device,
  98                       const struct mlxsw_sp_fid *fid);
  99};
 100
 101struct mlxsw_sp_switchdev_ops {
 102        void (*init)(struct mlxsw_sp *mlxsw_sp);
 103};
 104
 105static int
 106mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
 107                               struct mlxsw_sp_bridge_port *bridge_port,
 108                               u16 fid_index);
 109
 110static void
 111mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
 112                               struct mlxsw_sp_bridge_port *bridge_port);
 113
 114static void
 115mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
 116                                   struct mlxsw_sp_bridge_device
 117                                   *bridge_device);
 118
 119static void
 120mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
 121                                 struct mlxsw_sp_bridge_port *bridge_port,
 122                                 bool add);
 123
 124static struct mlxsw_sp_bridge_device *
 125mlxsw_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
 137bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
 138                                         const struct net_device *br_dev)
 139{
 140        return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
 141}
 142
 143static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device *dev,
 144                                                    struct netdev_nested_priv *priv)
 145{
 146        struct mlxsw_sp *mlxsw_sp = priv->data;
 147
 148        mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
 149        return 0;
 150}
 151
 152static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp *mlxsw_sp,
 153                                                struct net_device *dev)
 154{
 155        struct netdev_nested_priv priv = {
 156                .data = (void *)mlxsw_sp,
 157        };
 158
 159        mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
 160        netdev_walk_all_upper_dev_rcu(dev,
 161                                      mlxsw_sp_bridge_device_upper_rif_destroy,
 162                                      &priv);
 163}
 164
 165static int mlxsw_sp_bridge_device_vxlan_init(struct mlxsw_sp_bridge *bridge,
 166                                             struct net_device *br_dev,
 167                                             struct netlink_ext_ack *extack)
 168{
 169        struct net_device *dev, *stop_dev;
 170        struct list_head *iter;
 171        int err;
 172
 173        netdev_for_each_lower_dev(br_dev, dev, iter) {
 174                if (netif_is_vxlan(dev) && netif_running(dev)) {
 175                        err = mlxsw_sp_bridge_vxlan_join(bridge->mlxsw_sp,
 176                                                         br_dev, dev, 0,
 177                                                         extack);
 178                        if (err) {
 179                                stop_dev = dev;
 180                                goto err_vxlan_join;
 181                        }
 182                }
 183        }
 184
 185        return 0;
 186
 187err_vxlan_join:
 188        netdev_for_each_lower_dev(br_dev, dev, iter) {
 189                if (netif_is_vxlan(dev) && netif_running(dev)) {
 190                        if (stop_dev == dev)
 191                                break;
 192                        mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
 193                }
 194        }
 195        return err;
 196}
 197
 198static void mlxsw_sp_bridge_device_vxlan_fini(struct mlxsw_sp_bridge *bridge,
 199                                              struct net_device *br_dev)
 200{
 201        struct net_device *dev;
 202        struct list_head *iter;
 203
 204        netdev_for_each_lower_dev(br_dev, dev, iter) {
 205                if (netif_is_vxlan(dev) && netif_running(dev))
 206                        mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
 207        }
 208}
 209
 210static struct mlxsw_sp_bridge_device *
 211mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
 212                              struct net_device *br_dev,
 213                              struct netlink_ext_ack *extack)
 214{
 215        struct device *dev = bridge->mlxsw_sp->bus_info->dev;
 216        struct mlxsw_sp_bridge_device *bridge_device;
 217        bool vlan_enabled = br_vlan_enabled(br_dev);
 218        int err;
 219
 220        if (vlan_enabled && bridge->vlan_enabled_exists) {
 221                dev_err(dev, "Only one VLAN-aware bridge is supported\n");
 222                NL_SET_ERR_MSG_MOD(extack, "Only one VLAN-aware bridge is supported");
 223                return ERR_PTR(-EINVAL);
 224        }
 225
 226        bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
 227        if (!bridge_device)
 228                return ERR_PTR(-ENOMEM);
 229
 230        bridge_device->dev = br_dev;
 231        bridge_device->vlan_enabled = vlan_enabled;
 232        bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
 233        bridge_device->mrouter = br_multicast_router(br_dev);
 234        INIT_LIST_HEAD(&bridge_device->ports_list);
 235        if (vlan_enabled) {
 236                u16 proto;
 237
 238                bridge->vlan_enabled_exists = true;
 239                br_vlan_get_proto(br_dev, &proto);
 240                if (proto == ETH_P_8021AD)
 241                        bridge_device->ops = bridge->bridge_8021ad_ops;
 242                else
 243                        bridge_device->ops = bridge->bridge_8021q_ops;
 244        } else {
 245                bridge_device->ops = bridge->bridge_8021d_ops;
 246        }
 247        INIT_LIST_HEAD(&bridge_device->mids_list);
 248        list_add(&bridge_device->list, &bridge->bridges_list);
 249
 250        /* It is possible we already have VXLAN devices enslaved to the bridge.
 251         * In which case, we need to replay their configuration as if they were
 252         * just now enslaved to the bridge.
 253         */
 254        err = mlxsw_sp_bridge_device_vxlan_init(bridge, br_dev, extack);
 255        if (err)
 256                goto err_vxlan_init;
 257
 258        return bridge_device;
 259
 260err_vxlan_init:
 261        list_del(&bridge_device->list);
 262        if (bridge_device->vlan_enabled)
 263                bridge->vlan_enabled_exists = false;
 264        kfree(bridge_device);
 265        return ERR_PTR(err);
 266}
 267
 268static void
 269mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
 270                               struct mlxsw_sp_bridge_device *bridge_device)
 271{
 272        mlxsw_sp_bridge_device_vxlan_fini(bridge, bridge_device->dev);
 273        mlxsw_sp_bridge_device_rifs_destroy(bridge->mlxsw_sp,
 274                                            bridge_device->dev);
 275        list_del(&bridge_device->list);
 276        if (bridge_device->vlan_enabled)
 277                bridge->vlan_enabled_exists = false;
 278        WARN_ON(!list_empty(&bridge_device->ports_list));
 279        WARN_ON(!list_empty(&bridge_device->mids_list));
 280        kfree(bridge_device);
 281}
 282
 283static struct mlxsw_sp_bridge_device *
 284mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
 285                           struct net_device *br_dev,
 286                           struct netlink_ext_ack *extack)
 287{
 288        struct mlxsw_sp_bridge_device *bridge_device;
 289
 290        bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
 291        if (bridge_device)
 292                return bridge_device;
 293
 294        return mlxsw_sp_bridge_device_create(bridge, br_dev, extack);
 295}
 296
 297static void
 298mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
 299                           struct mlxsw_sp_bridge_device *bridge_device)
 300{
 301        if (list_empty(&bridge_device->ports_list))
 302                mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
 303}
 304
 305static struct mlxsw_sp_bridge_port *
 306__mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
 307                            const struct net_device *brport_dev)
 308{
 309        struct mlxsw_sp_bridge_port *bridge_port;
 310
 311        list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
 312                if (bridge_port->dev == brport_dev)
 313                        return bridge_port;
 314        }
 315
 316        return NULL;
 317}
 318
 319struct mlxsw_sp_bridge_port *
 320mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
 321                          struct net_device *brport_dev)
 322{
 323        struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
 324        struct mlxsw_sp_bridge_device *bridge_device;
 325
 326        if (!br_dev)
 327                return NULL;
 328
 329        bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
 330        if (!bridge_device)
 331                return NULL;
 332
 333        return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
 334}
 335
 336static struct mlxsw_sp_bridge_port *
 337mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
 338                            struct net_device *brport_dev)
 339{
 340        struct mlxsw_sp_bridge_port *bridge_port;
 341        struct mlxsw_sp_port *mlxsw_sp_port;
 342
 343        bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
 344        if (!bridge_port)
 345                return NULL;
 346
 347        mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
 348        bridge_port->lagged = mlxsw_sp_port->lagged;
 349        if (bridge_port->lagged)
 350                bridge_port->lag_id = mlxsw_sp_port->lag_id;
 351        else
 352                bridge_port->system_port = mlxsw_sp_port->local_port;
 353        bridge_port->dev = brport_dev;
 354        bridge_port->bridge_device = bridge_device;
 355        bridge_port->stp_state = BR_STATE_DISABLED;
 356        bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
 357                             BR_MCAST_FLOOD;
 358        INIT_LIST_HEAD(&bridge_port->vlans_list);
 359        list_add(&bridge_port->list, &bridge_device->ports_list);
 360        bridge_port->ref_count = 1;
 361
 362        return bridge_port;
 363}
 364
 365static void
 366mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
 367{
 368        list_del(&bridge_port->list);
 369        WARN_ON(!list_empty(&bridge_port->vlans_list));
 370        kfree(bridge_port);
 371}
 372
 373static struct mlxsw_sp_bridge_port *
 374mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
 375                         struct net_device *brport_dev,
 376                         struct netlink_ext_ack *extack)
 377{
 378        struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
 379        struct mlxsw_sp_bridge_device *bridge_device;
 380        struct mlxsw_sp_bridge_port *bridge_port;
 381        int err;
 382
 383        bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
 384        if (bridge_port) {
 385                bridge_port->ref_count++;
 386                return bridge_port;
 387        }
 388
 389        bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev, extack);
 390        if (IS_ERR(bridge_device))
 391                return ERR_CAST(bridge_device);
 392
 393        bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
 394        if (!bridge_port) {
 395                err = -ENOMEM;
 396                goto err_bridge_port_create;
 397        }
 398
 399        return bridge_port;
 400
 401err_bridge_port_create:
 402        mlxsw_sp_bridge_device_put(bridge, bridge_device);
 403        return ERR_PTR(err);
 404}
 405
 406static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
 407                                     struct mlxsw_sp_bridge_port *bridge_port)
 408{
 409        struct mlxsw_sp_bridge_device *bridge_device;
 410
 411        if (--bridge_port->ref_count != 0)
 412                return;
 413        bridge_device = bridge_port->bridge_device;
 414        mlxsw_sp_bridge_port_destroy(bridge_port);
 415        mlxsw_sp_bridge_device_put(bridge, bridge_device);
 416}
 417
 418static struct mlxsw_sp_port_vlan *
 419mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
 420                                  const struct mlxsw_sp_bridge_device *
 421                                  bridge_device,
 422                                  u16 vid)
 423{
 424        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
 425
 426        list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
 427                            list) {
 428                if (!mlxsw_sp_port_vlan->bridge_port)
 429                        continue;
 430                if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
 431                    bridge_device)
 432                        continue;
 433                if (bridge_device->vlan_enabled &&
 434                    mlxsw_sp_port_vlan->vid != vid)
 435                        continue;
 436                return mlxsw_sp_port_vlan;
 437        }
 438
 439        return NULL;
 440}
 441
 442static struct mlxsw_sp_port_vlan*
 443mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
 444                               u16 fid_index)
 445{
 446        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
 447
 448        list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
 449                            list) {
 450                struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
 451
 452                if (fid && mlxsw_sp_fid_index(fid) == fid_index)
 453                        return mlxsw_sp_port_vlan;
 454        }
 455
 456        return NULL;
 457}
 458
 459static struct mlxsw_sp_bridge_vlan *
 460mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
 461                          u16 vid)
 462{
 463        struct mlxsw_sp_bridge_vlan *bridge_vlan;
 464
 465        list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
 466                if (bridge_vlan->vid == vid)
 467                        return bridge_vlan;
 468        }
 469
 470        return NULL;
 471}
 472
 473static struct mlxsw_sp_bridge_vlan *
 474mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
 475{
 476        struct mlxsw_sp_bridge_vlan *bridge_vlan;
 477
 478        bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
 479        if (!bridge_vlan)
 480                return NULL;
 481
 482        INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
 483        bridge_vlan->vid = vid;
 484        list_add(&bridge_vlan->list, &bridge_port->vlans_list);
 485
 486        return bridge_vlan;
 487}
 488
 489static void
 490mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
 491{
 492        list_del(&bridge_vlan->list);
 493        WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
 494        kfree(bridge_vlan);
 495}
 496
 497static struct mlxsw_sp_bridge_vlan *
 498mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
 499{
 500        struct mlxsw_sp_bridge_vlan *bridge_vlan;
 501
 502        bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
 503        if (bridge_vlan)
 504                return bridge_vlan;
 505
 506        return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
 507}
 508
 509static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
 510{
 511        if (list_empty(&bridge_vlan->port_vlan_list))
 512                mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
 513}
 514
 515static int
 516mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
 517                                  struct mlxsw_sp_bridge_vlan *bridge_vlan,
 518                                  u8 state)
 519{
 520        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
 521
 522        list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
 523                            bridge_vlan_node) {
 524                if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
 525                        continue;
 526                return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
 527                                                 bridge_vlan->vid, state);
 528        }
 529
 530        return 0;
 531}
 532
 533static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
 534                                            struct net_device *orig_dev,
 535                                            u8 state)
 536{
 537        struct mlxsw_sp_bridge_port *bridge_port;
 538        struct mlxsw_sp_bridge_vlan *bridge_vlan;
 539        int err;
 540
 541        /* It's possible we failed to enslave the port, yet this
 542         * operation is executed due to it being deferred.
 543         */
 544        bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
 545                                                orig_dev);
 546        if (!bridge_port)
 547                return 0;
 548
 549        list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
 550                err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
 551                                                        bridge_vlan, state);
 552                if (err)
 553                        goto err_port_bridge_vlan_stp_set;
 554        }
 555
 556        bridge_port->stp_state = state;
 557
 558        return 0;
 559
 560err_port_bridge_vlan_stp_set:
 561        list_for_each_entry_continue_reverse(bridge_vlan,
 562                                             &bridge_port->vlans_list, list)
 563                mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
 564                                                  bridge_port->stp_state);
 565        return err;
 566}
 567
 568static int
 569mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
 570                                    struct mlxsw_sp_bridge_vlan *bridge_vlan,
 571                                    enum mlxsw_sp_flood_type packet_type,
 572                                    bool member)
 573{
 574        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
 575
 576        list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
 577                            bridge_vlan_node) {
 578                if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
 579                        continue;
 580                return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
 581                                              packet_type,
 582                                              mlxsw_sp_port->local_port,
 583                                              member);
 584        }
 585
 586        return 0;
 587}
 588
 589static int
 590mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
 591                                     struct mlxsw_sp_bridge_port *bridge_port,
 592                                     enum mlxsw_sp_flood_type packet_type,
 593                                     bool member)
 594{
 595        struct mlxsw_sp_bridge_vlan *bridge_vlan;
 596        int err;
 597
 598        list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
 599                err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
 600                                                          bridge_vlan,
 601                                                          packet_type,
 602                                                          member);
 603                if (err)
 604                        goto err_port_bridge_vlan_flood_set;
 605        }
 606
 607        return 0;
 608
 609err_port_bridge_vlan_flood_set:
 610        list_for_each_entry_continue_reverse(bridge_vlan,
 611                                             &bridge_port->vlans_list, list)
 612                mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
 613                                                    packet_type, !member);
 614        return err;
 615}
 616
 617static int
 618mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
 619                                       struct mlxsw_sp_bridge_vlan *bridge_vlan,
 620                                       bool set)
 621{
 622        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
 623        u16 vid = bridge_vlan->vid;
 624
 625        list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
 626                            bridge_vlan_node) {
 627                if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
 628                        continue;
 629                return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
 630        }
 631
 632        return 0;
 633}
 634
 635static int
 636mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
 637                                  struct mlxsw_sp_bridge_port *bridge_port,
 638                                  bool set)
 639{
 640        struct mlxsw_sp_bridge_vlan *bridge_vlan;
 641        int err;
 642
 643        list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
 644                err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
 645                                                             bridge_vlan, set);
 646                if (err)
 647                        goto err_port_bridge_vlan_learning_set;
 648        }
 649
 650        return 0;
 651
 652err_port_bridge_vlan_learning_set:
 653        list_for_each_entry_continue_reverse(bridge_vlan,
 654                                             &bridge_port->vlans_list, list)
 655                mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
 656                                                       bridge_vlan, !set);
 657        return err;
 658}
 659
 660static int
 661mlxsw_sp_port_attr_br_pre_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
 662                                    struct switchdev_brport_flags flags)
 663{
 664        if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
 665                return -EINVAL;
 666
 667        return 0;
 668}
 669
 670static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
 671                                           struct net_device *orig_dev,
 672                                           struct switchdev_brport_flags flags)
 673{
 674        struct mlxsw_sp_bridge_port *bridge_port;
 675        int err;
 676
 677        bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
 678                                                orig_dev);
 679        if (!bridge_port)
 680                return 0;
 681
 682        if (flags.mask & BR_FLOOD) {
 683                err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
 684                                                           bridge_port,
 685                                                           MLXSW_SP_FLOOD_TYPE_UC,
 686                                                           flags.val & BR_FLOOD);
 687                if (err)
 688                        return err;
 689        }
 690
 691        if (flags.mask & BR_LEARNING) {
 692                err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port,
 693                                                        bridge_port,
 694                                                        flags.val & BR_LEARNING);
 695                if (err)
 696                        return err;
 697        }
 698
 699        if (bridge_port->bridge_device->multicast_enabled)
 700                goto out;
 701
 702        if (flags.mask & BR_MCAST_FLOOD) {
 703                err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
 704                                                           bridge_port,
 705                                                           MLXSW_SP_FLOOD_TYPE_MC,
 706                                                           flags.val & BR_MCAST_FLOOD);
 707                if (err)
 708                        return err;
 709        }
 710
 711out:
 712        memcpy(&bridge_port->flags, &flags.val, sizeof(flags.val));
 713        return 0;
 714}
 715
 716static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
 717{
 718        char sfdat_pl[MLXSW_REG_SFDAT_LEN];
 719        int err;
 720
 721        mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
 722        err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
 723        if (err)
 724                return err;
 725        mlxsw_sp->bridge->ageing_time = ageing_time;
 726        return 0;
 727}
 728
 729static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
 730                                            unsigned long ageing_clock_t)
 731{
 732        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
 733        unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
 734        u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
 735
 736        if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
 737            ageing_time > MLXSW_SP_MAX_AGEING_TIME)
 738                return -ERANGE;
 739
 740        return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
 741}
 742
 743static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
 744                                          struct net_device *orig_dev,
 745                                          bool vlan_enabled)
 746{
 747        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
 748        struct mlxsw_sp_bridge_device *bridge_device;
 749
 750        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
 751        if (WARN_ON(!bridge_device))
 752                return -EINVAL;
 753
 754        if (bridge_device->vlan_enabled == vlan_enabled)
 755                return 0;
 756
 757        netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
 758        return -EINVAL;
 759}
 760
 761static int mlxsw_sp_port_attr_br_vlan_proto_set(struct mlxsw_sp_port *mlxsw_sp_port,
 762                                                struct net_device *orig_dev,
 763                                                u16 vlan_proto)
 764{
 765        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
 766        struct mlxsw_sp_bridge_device *bridge_device;
 767
 768        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
 769        if (WARN_ON(!bridge_device))
 770                return -EINVAL;
 771
 772        netdev_err(bridge_device->dev, "VLAN protocol can't be changed on existing bridge\n");
 773        return -EINVAL;
 774}
 775
 776static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
 777                                          struct net_device *orig_dev,
 778                                          bool is_port_mrouter)
 779{
 780        struct mlxsw_sp_bridge_port *bridge_port;
 781        int err;
 782
 783        bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
 784                                                orig_dev);
 785        if (!bridge_port)
 786                return 0;
 787
 788        if (!bridge_port->bridge_device->multicast_enabled)
 789                goto out;
 790
 791        err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
 792                                                   MLXSW_SP_FLOOD_TYPE_MC,
 793                                                   is_port_mrouter);
 794        if (err)
 795                return err;
 796
 797        mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
 798                                         is_port_mrouter);
 799out:
 800        bridge_port->mrouter = is_port_mrouter;
 801        return 0;
 802}
 803
 804static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
 805{
 806        const struct mlxsw_sp_bridge_device *bridge_device;
 807
 808        bridge_device = bridge_port->bridge_device;
 809        return bridge_device->multicast_enabled ? bridge_port->mrouter :
 810                                        bridge_port->flags & BR_MCAST_FLOOD;
 811}
 812
 813static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
 814                                         struct net_device *orig_dev,
 815                                         bool mc_disabled)
 816{
 817        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
 818        struct mlxsw_sp_bridge_device *bridge_device;
 819        struct mlxsw_sp_bridge_port *bridge_port;
 820        int err;
 821
 822        /* It's possible we failed to enslave the port, yet this
 823         * operation is executed due to it being deferred.
 824         */
 825        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
 826        if (!bridge_device)
 827                return 0;
 828
 829        if (bridge_device->multicast_enabled != !mc_disabled) {
 830                bridge_device->multicast_enabled = !mc_disabled;
 831                mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
 832                                                   bridge_device);
 833        }
 834
 835        list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
 836                enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
 837                bool member = mlxsw_sp_mc_flood(bridge_port);
 838
 839                err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
 840                                                           bridge_port,
 841                                                           packet_type, member);
 842                if (err)
 843                        return err;
 844        }
 845
 846        bridge_device->multicast_enabled = !mc_disabled;
 847
 848        return 0;
 849}
 850
 851static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
 852                                         u16 mid_idx, bool add)
 853{
 854        char *smid_pl;
 855        int err;
 856
 857        smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
 858        if (!smid_pl)
 859                return -ENOMEM;
 860
 861        mlxsw_reg_smid_pack(smid_pl, mid_idx,
 862                            mlxsw_sp_router_port(mlxsw_sp), add);
 863        err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
 864        kfree(smid_pl);
 865        return err;
 866}
 867
 868static void
 869mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
 870                                   struct mlxsw_sp_bridge_device *bridge_device,
 871                                   bool add)
 872{
 873        struct mlxsw_sp_mid *mid;
 874
 875        list_for_each_entry(mid, &bridge_device->mids_list, list)
 876                mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
 877}
 878
 879static int
 880mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
 881                                  struct net_device *orig_dev,
 882                                  bool is_mrouter)
 883{
 884        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
 885        struct mlxsw_sp_bridge_device *bridge_device;
 886
 887        /* It's possible we failed to enslave the port, yet this
 888         * operation is executed due to it being deferred.
 889         */
 890        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
 891        if (!bridge_device)
 892                return 0;
 893
 894        if (bridge_device->mrouter != is_mrouter)
 895                mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
 896                                                   is_mrouter);
 897        bridge_device->mrouter = is_mrouter;
 898        return 0;
 899}
 900
 901static int mlxsw_sp_port_attr_set(struct net_device *dev, const void *ctx,
 902                                  const struct switchdev_attr *attr,
 903                                  struct netlink_ext_ack *extack)
 904{
 905        struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
 906        int err;
 907
 908        switch (attr->id) {
 909        case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
 910                err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port,
 911                                                       attr->orig_dev,
 912                                                       attr->u.stp_state);
 913                break;
 914        case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
 915                err = mlxsw_sp_port_attr_br_pre_flags_set(mlxsw_sp_port,
 916                                                          attr->u.brport_flags);
 917                break;
 918        case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
 919                err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port,
 920                                                      attr->orig_dev,
 921                                                      attr->u.brport_flags);
 922                break;
 923        case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
 924                err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port,
 925                                                       attr->u.ageing_time);
 926                break;
 927        case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
 928                err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port,
 929                                                     attr->orig_dev,
 930                                                     attr->u.vlan_filtering);
 931                break;
 932        case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL:
 933                err = mlxsw_sp_port_attr_br_vlan_proto_set(mlxsw_sp_port,
 934                                                           attr->orig_dev,
 935                                                           attr->u.vlan_protocol);
 936                break;
 937        case SWITCHDEV_ATTR_ID_PORT_MROUTER:
 938                err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port,
 939                                                     attr->orig_dev,
 940                                                     attr->u.mrouter);
 941                break;
 942        case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
 943                err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port,
 944                                                    attr->orig_dev,
 945                                                    attr->u.mc_disabled);
 946                break;
 947        case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
 948                err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port,
 949                                                        attr->orig_dev,
 950                                                        attr->u.mrouter);
 951                break;
 952        default:
 953                err = -EOPNOTSUPP;
 954                break;
 955        }
 956
 957        mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
 958
 959        return err;
 960}
 961
 962static int
 963mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
 964                            struct mlxsw_sp_bridge_port *bridge_port,
 965                            struct netlink_ext_ack *extack)
 966{
 967        struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
 968        struct mlxsw_sp_bridge_device *bridge_device;
 969        u8 local_port = mlxsw_sp_port->local_port;
 970        u16 vid = mlxsw_sp_port_vlan->vid;
 971        struct mlxsw_sp_fid *fid;
 972        int err;
 973
 974        bridge_device = bridge_port->bridge_device;
 975        fid = bridge_device->ops->fid_get(bridge_device, vid, extack);
 976        if (IS_ERR(fid))
 977                return PTR_ERR(fid);
 978
 979        err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
 980                                     bridge_port->flags & BR_FLOOD);
 981        if (err)
 982                goto err_fid_uc_flood_set;
 983
 984        err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
 985                                     mlxsw_sp_mc_flood(bridge_port));
 986        if (err)
 987                goto err_fid_mc_flood_set;
 988
 989        err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
 990                                     true);
 991        if (err)
 992                goto err_fid_bc_flood_set;
 993
 994        err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
 995        if (err)
 996                goto err_fid_port_vid_map;
 997
 998        mlxsw_sp_port_vlan->fid = fid;
 999
1000        return 0;
1001
1002err_fid_port_vid_map:
1003        mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1004err_fid_bc_flood_set:
1005        mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1006err_fid_mc_flood_set:
1007        mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1008err_fid_uc_flood_set:
1009        mlxsw_sp_fid_put(fid);
1010        return err;
1011}
1012
1013static void
1014mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1015{
1016        struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1017        struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1018        u8 local_port = mlxsw_sp_port->local_port;
1019        u16 vid = mlxsw_sp_port_vlan->vid;
1020
1021        mlxsw_sp_port_vlan->fid = NULL;
1022        mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
1023        mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1024        mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1025        mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1026        mlxsw_sp_fid_put(fid);
1027}
1028
1029static u16
1030mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
1031                             u16 vid, bool is_pvid)
1032{
1033        if (is_pvid)
1034                return vid;
1035        else if (mlxsw_sp_port->pvid == vid)
1036                return 0;       /* Dis-allow untagged packets */
1037        else
1038                return mlxsw_sp_port->pvid;
1039}
1040
1041static int
1042mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1043                               struct mlxsw_sp_bridge_port *bridge_port,
1044                               struct netlink_ext_ack *extack)
1045{
1046        struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1047        struct mlxsw_sp_bridge_vlan *bridge_vlan;
1048        u16 vid = mlxsw_sp_port_vlan->vid;
1049        int err;
1050
1051        /* No need to continue if only VLAN flags were changed */
1052        if (mlxsw_sp_port_vlan->bridge_port)
1053                return 0;
1054
1055        err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port,
1056                                          extack);
1057        if (err)
1058                return err;
1059
1060        err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1061                                             bridge_port->flags & BR_LEARNING);
1062        if (err)
1063                goto err_port_vid_learning_set;
1064
1065        err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1066                                        bridge_port->stp_state);
1067        if (err)
1068                goto err_port_vid_stp_set;
1069
1070        bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1071        if (!bridge_vlan) {
1072                err = -ENOMEM;
1073                goto err_bridge_vlan_get;
1074        }
1075
1076        list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1077                 &bridge_vlan->port_vlan_list);
1078
1079        mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1080                                 bridge_port->dev, extack);
1081        mlxsw_sp_port_vlan->bridge_port = bridge_port;
1082
1083        return 0;
1084
1085err_bridge_vlan_get:
1086        mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1087err_port_vid_stp_set:
1088        mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1089err_port_vid_learning_set:
1090        mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1091        return err;
1092}
1093
1094void
1095mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1096{
1097        struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1098        struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1099        struct mlxsw_sp_bridge_vlan *bridge_vlan;
1100        struct mlxsw_sp_bridge_port *bridge_port;
1101        u16 vid = mlxsw_sp_port_vlan->vid;
1102        bool last_port, last_vlan;
1103
1104        if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1105                    mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1106                return;
1107
1108        bridge_port = mlxsw_sp_port_vlan->bridge_port;
1109        last_vlan = list_is_singular(&bridge_port->vlans_list);
1110        bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1111        last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1112
1113        list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1114        mlxsw_sp_bridge_vlan_put(bridge_vlan);
1115        mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1116        mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1117        if (last_port)
1118                mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1119                                               bridge_port,
1120                                               mlxsw_sp_fid_index(fid));
1121        if (last_vlan)
1122                mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1123
1124        mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1125
1126        mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1127        mlxsw_sp_port_vlan->bridge_port = NULL;
1128}
1129
1130static int
1131mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1132                              struct mlxsw_sp_bridge_port *bridge_port,
1133                              u16 vid, bool is_untagged, bool is_pvid,
1134                              struct netlink_ext_ack *extack)
1135{
1136        u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1137        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1138        u16 old_pvid = mlxsw_sp_port->pvid;
1139        u16 proto;
1140        int err;
1141
1142        /* The only valid scenario in which a port-vlan already exists, is if
1143         * the VLAN flags were changed and the port-vlan is associated with the
1144         * correct bridge port
1145         */
1146        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1147        if (mlxsw_sp_port_vlan &&
1148            mlxsw_sp_port_vlan->bridge_port != bridge_port)
1149                return -EEXIST;
1150
1151        if (!mlxsw_sp_port_vlan) {
1152                mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
1153                                                               vid);
1154                if (IS_ERR(mlxsw_sp_port_vlan))
1155                        return PTR_ERR(mlxsw_sp_port_vlan);
1156        }
1157
1158        err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1159                                     is_untagged);
1160        if (err)
1161                goto err_port_vlan_set;
1162
1163        br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1164        err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1165        if (err)
1166                goto err_port_pvid_set;
1167
1168        err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
1169                                             extack);
1170        if (err)
1171                goto err_port_vlan_bridge_join;
1172
1173        return 0;
1174
1175err_port_vlan_bridge_join:
1176        mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid, proto);
1177err_port_pvid_set:
1178        mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1179err_port_vlan_set:
1180        mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1181        return err;
1182}
1183
1184static int
1185mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1186                                const struct net_device *br_dev,
1187                                const struct switchdev_obj_port_vlan *vlan)
1188{
1189        u16 pvid;
1190
1191        pvid = mlxsw_sp_rif_vid(mlxsw_sp, br_dev);
1192        if (!pvid)
1193                return 0;
1194
1195        if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1196                if (vlan->vid != pvid) {
1197                        netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1198                        return -EBUSY;
1199                }
1200        } else {
1201                if (vlan->vid == pvid) {
1202                        netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1203                        return -EBUSY;
1204                }
1205        }
1206
1207        return 0;
1208}
1209
1210static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1211                                   const struct switchdev_obj_port_vlan *vlan,
1212                                   struct netlink_ext_ack *extack)
1213{
1214        bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1215        bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1216        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1217        struct net_device *orig_dev = vlan->obj.orig_dev;
1218        struct mlxsw_sp_bridge_port *bridge_port;
1219
1220        if (netif_is_bridge_master(orig_dev)) {
1221                int err = 0;
1222
1223                if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1224                    br_vlan_enabled(orig_dev))
1225                        err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1226                                                              orig_dev, vlan);
1227                if (!err)
1228                        err = -EOPNOTSUPP;
1229                return err;
1230        }
1231
1232        bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1233        if (WARN_ON(!bridge_port))
1234                return -EINVAL;
1235
1236        if (!bridge_port->bridge_device->vlan_enabled)
1237                return 0;
1238
1239        return mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1240                                             vlan->vid, flag_untagged,
1241                                             flag_pvid, extack);
1242}
1243
1244static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1245{
1246        return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1247                        MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1248}
1249
1250static int
1251mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1252                               struct mlxsw_sp_bridge_port *bridge_port,
1253                               u16 fid_index)
1254{
1255        bool lagged = bridge_port->lagged;
1256        char sfdf_pl[MLXSW_REG_SFDF_LEN];
1257        u16 system_port;
1258
1259        system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1260        mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1261        mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1262        mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1263
1264        return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1265}
1266
1267static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1268{
1269        return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1270                         MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_MLAG;
1271}
1272
1273static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1274{
1275        return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1276                        MLXSW_REG_SFD_OP_WRITE_REMOVE;
1277}
1278
1279static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp *mlxsw_sp,
1280                                          const char *mac, u16 fid,
1281                                          enum mlxsw_sp_l3proto proto,
1282                                          const union mlxsw_sp_l3addr *addr,
1283                                          bool adding, bool dynamic)
1284{
1285        enum mlxsw_reg_sfd_uc_tunnel_protocol sfd_proto;
1286        char *sfd_pl;
1287        u8 num_rec;
1288        u32 uip;
1289        int err;
1290
1291        switch (proto) {
1292        case MLXSW_SP_L3_PROTO_IPV4:
1293                uip = be32_to_cpu(addr->addr4);
1294                sfd_proto = MLXSW_REG_SFD_UC_TUNNEL_PROTOCOL_IPV4;
1295                break;
1296        case MLXSW_SP_L3_PROTO_IPV6:
1297        default:
1298                WARN_ON(1);
1299                return -EOPNOTSUPP;
1300        }
1301
1302        sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1303        if (!sfd_pl)
1304                return -ENOMEM;
1305
1306        mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1307        mlxsw_reg_sfd_uc_tunnel_pack(sfd_pl, 0,
1308                                     mlxsw_sp_sfd_rec_policy(dynamic), mac, fid,
1309                                     MLXSW_REG_SFD_REC_ACTION_NOP, uip,
1310                                     sfd_proto);
1311        num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1312        err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1313        if (err)
1314                goto out;
1315
1316        if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1317                err = -EBUSY;
1318
1319out:
1320        kfree(sfd_pl);
1321        return err;
1322}
1323
1324static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1325                                     const char *mac, u16 fid, bool adding,
1326                                     enum mlxsw_reg_sfd_rec_action action,
1327                                     enum mlxsw_reg_sfd_rec_policy policy)
1328{
1329        char *sfd_pl;
1330        u8 num_rec;
1331        int err;
1332
1333        sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1334        if (!sfd_pl)
1335                return -ENOMEM;
1336
1337        mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1338        mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy, mac, fid, action, local_port);
1339        num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1340        err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1341        if (err)
1342                goto out;
1343
1344        if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1345                err = -EBUSY;
1346
1347out:
1348        kfree(sfd_pl);
1349        return err;
1350}
1351
1352static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1353                                   const char *mac, u16 fid, bool adding,
1354                                   bool dynamic)
1355{
1356        return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1357                                         MLXSW_REG_SFD_REC_ACTION_NOP,
1358                                         mlxsw_sp_sfd_rec_policy(dynamic));
1359}
1360
1361int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1362                        bool adding)
1363{
1364        return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1365                                         MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1366                                         MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY);
1367}
1368
1369static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1370                                       const char *mac, u16 fid, u16 lag_vid,
1371                                       bool adding, bool dynamic)
1372{
1373        char *sfd_pl;
1374        u8 num_rec;
1375        int err;
1376
1377        sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1378        if (!sfd_pl)
1379                return -ENOMEM;
1380
1381        mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1382        mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1383                                  mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1384                                  lag_vid, lag_id);
1385        num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1386        err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1387        if (err)
1388                goto out;
1389
1390        if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1391                err = -EBUSY;
1392
1393out:
1394        kfree(sfd_pl);
1395        return err;
1396}
1397
1398static int
1399mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1400                      struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1401{
1402        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1403        struct net_device *orig_dev = fdb_info->info.dev;
1404        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1405        struct mlxsw_sp_bridge_device *bridge_device;
1406        struct mlxsw_sp_bridge_port *bridge_port;
1407        u16 fid_index, vid;
1408
1409        bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1410        if (!bridge_port)
1411                return -EINVAL;
1412
1413        bridge_device = bridge_port->bridge_device;
1414        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1415                                                               bridge_device,
1416                                                               fdb_info->vid);
1417        if (!mlxsw_sp_port_vlan)
1418                return 0;
1419
1420        fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1421        vid = mlxsw_sp_port_vlan->vid;
1422
1423        if (!bridge_port->lagged)
1424                return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1425                                               bridge_port->system_port,
1426                                               fdb_info->addr, fid_index,
1427                                               adding, false);
1428        else
1429                return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1430                                                   bridge_port->lag_id,
1431                                                   fdb_info->addr, fid_index,
1432                                                   vid, adding, false);
1433}
1434
1435static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1436                                u16 fid, u16 mid_idx, bool adding)
1437{
1438        char *sfd_pl;
1439        u8 num_rec;
1440        int err;
1441
1442        sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1443        if (!sfd_pl)
1444                return -ENOMEM;
1445
1446        mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1447        mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1448                              MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1449        num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1450        err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1451        if (err)
1452                goto out;
1453
1454        if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1455                err = -EBUSY;
1456
1457out:
1458        kfree(sfd_pl);
1459        return err;
1460}
1461
1462static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1463                                         long *ports_bitmap,
1464                                         bool set_router_port)
1465{
1466        char *smid_pl;
1467        int err, i;
1468
1469        smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1470        if (!smid_pl)
1471                return -ENOMEM;
1472
1473        mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1474        for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1475                if (mlxsw_sp->ports[i])
1476                        mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1477        }
1478
1479        mlxsw_reg_smid_port_mask_set(smid_pl,
1480                                     mlxsw_sp_router_port(mlxsw_sp), 1);
1481
1482        for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1483                mlxsw_reg_smid_port_set(smid_pl, i, 1);
1484
1485        mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1486                                set_router_port);
1487
1488        err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1489        kfree(smid_pl);
1490        return err;
1491}
1492
1493static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1494                                  u16 mid_idx, bool add)
1495{
1496        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1497        char *smid_pl;
1498        int err;
1499
1500        smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1501        if (!smid_pl)
1502                return -ENOMEM;
1503
1504        mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1505        err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1506        kfree(smid_pl);
1507        return err;
1508}
1509
1510static struct
1511mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1512                                const unsigned char *addr,
1513                                u16 fid)
1514{
1515        struct mlxsw_sp_mid *mid;
1516
1517        list_for_each_entry(mid, &bridge_device->mids_list, list) {
1518                if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1519                        return mid;
1520        }
1521        return NULL;
1522}
1523
1524static void
1525mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1526                                      struct mlxsw_sp_bridge_port *bridge_port,
1527                                      unsigned long *ports_bitmap)
1528{
1529        struct mlxsw_sp_port *mlxsw_sp_port;
1530        u64 max_lag_members, i;
1531        int lag_id;
1532
1533        if (!bridge_port->lagged) {
1534                set_bit(bridge_port->system_port, ports_bitmap);
1535        } else {
1536                max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1537                                                     MAX_LAG_MEMBERS);
1538                lag_id = bridge_port->lag_id;
1539                for (i = 0; i < max_lag_members; i++) {
1540                        mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1541                                                                 lag_id, i);
1542                        if (mlxsw_sp_port)
1543                                set_bit(mlxsw_sp_port->local_port,
1544                                        ports_bitmap);
1545                }
1546        }
1547}
1548
1549static void
1550mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1551                                struct mlxsw_sp_bridge_device *bridge_device,
1552                                struct mlxsw_sp *mlxsw_sp)
1553{
1554        struct mlxsw_sp_bridge_port *bridge_port;
1555
1556        list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1557                if (bridge_port->mrouter) {
1558                        mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1559                                                              bridge_port,
1560                                                              flood_bitmap);
1561                }
1562        }
1563}
1564
1565static bool
1566mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1567                            struct mlxsw_sp_mid *mid,
1568                            struct mlxsw_sp_bridge_device *bridge_device)
1569{
1570        long *flood_bitmap;
1571        int num_of_ports;
1572        int alloc_size;
1573        u16 mid_idx;
1574        int err;
1575
1576        mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1577                                      MLXSW_SP_MID_MAX);
1578        if (mid_idx == MLXSW_SP_MID_MAX)
1579                return false;
1580
1581        num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1582        alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1583        flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1584        if (!flood_bitmap)
1585                return false;
1586
1587        bitmap_copy(flood_bitmap,  mid->ports_in_mid, num_of_ports);
1588        mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1589
1590        mid->mid = mid_idx;
1591        err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1592                                            bridge_device->mrouter);
1593        kfree(flood_bitmap);
1594        if (err)
1595                return false;
1596
1597        err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1598                                   true);
1599        if (err)
1600                return false;
1601
1602        set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1603        mid->in_hw = true;
1604        return true;
1605}
1606
1607static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1608                                        struct mlxsw_sp_mid *mid)
1609{
1610        if (!mid->in_hw)
1611                return 0;
1612
1613        clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1614        mid->in_hw = false;
1615        return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1616                                    false);
1617}
1618
1619static struct
1620mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1621                                  struct mlxsw_sp_bridge_device *bridge_device,
1622                                  const unsigned char *addr,
1623                                  u16 fid)
1624{
1625        struct mlxsw_sp_mid *mid;
1626        size_t alloc_size;
1627
1628        mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1629        if (!mid)
1630                return NULL;
1631
1632        alloc_size = sizeof(unsigned long) *
1633                     BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1634
1635        mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1636        if (!mid->ports_in_mid)
1637                goto err_ports_in_mid_alloc;
1638
1639        ether_addr_copy(mid->addr, addr);
1640        mid->fid = fid;
1641        mid->in_hw = false;
1642
1643        if (!bridge_device->multicast_enabled)
1644                goto out;
1645
1646        if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1647                goto err_write_mdb_entry;
1648
1649out:
1650        list_add_tail(&mid->list, &bridge_device->mids_list);
1651        return mid;
1652
1653err_write_mdb_entry:
1654        kfree(mid->ports_in_mid);
1655err_ports_in_mid_alloc:
1656        kfree(mid);
1657        return NULL;
1658}
1659
1660static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1661                                         struct mlxsw_sp_mid *mid)
1662{
1663        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1664        int err = 0;
1665
1666        clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1667        if (bitmap_empty(mid->ports_in_mid,
1668                         mlxsw_core_max_ports(mlxsw_sp->core))) {
1669                err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1670                list_del(&mid->list);
1671                kfree(mid->ports_in_mid);
1672                kfree(mid);
1673        }
1674        return err;
1675}
1676
1677static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1678                                 const struct switchdev_obj_port_mdb *mdb)
1679{
1680        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1681        struct net_device *orig_dev = mdb->obj.orig_dev;
1682        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1683        struct net_device *dev = mlxsw_sp_port->dev;
1684        struct mlxsw_sp_bridge_device *bridge_device;
1685        struct mlxsw_sp_bridge_port *bridge_port;
1686        struct mlxsw_sp_mid *mid;
1687        u16 fid_index;
1688        int err = 0;
1689
1690        bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1691        if (!bridge_port)
1692                return 0;
1693
1694        bridge_device = bridge_port->bridge_device;
1695        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1696                                                               bridge_device,
1697                                                               mdb->vid);
1698        if (!mlxsw_sp_port_vlan)
1699                return 0;
1700
1701        fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1702
1703        mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1704        if (!mid) {
1705                mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1706                                          fid_index);
1707                if (!mid) {
1708                        netdev_err(dev, "Unable to allocate MC group\n");
1709                        return -ENOMEM;
1710                }
1711        }
1712        set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1713
1714        if (!bridge_device->multicast_enabled)
1715                return 0;
1716
1717        if (bridge_port->mrouter)
1718                return 0;
1719
1720        err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1721        if (err) {
1722                netdev_err(dev, "Unable to set SMID\n");
1723                goto err_out;
1724        }
1725
1726        return 0;
1727
1728err_out:
1729        mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1730        return err;
1731}
1732
1733static void
1734mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1735                                   struct mlxsw_sp_bridge_device
1736                                   *bridge_device)
1737{
1738        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1739        struct mlxsw_sp_mid *mid;
1740        bool mc_enabled;
1741
1742        mc_enabled = bridge_device->multicast_enabled;
1743
1744        list_for_each_entry(mid, &bridge_device->mids_list, list) {
1745                if (mc_enabled)
1746                        mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1747                                                    bridge_device);
1748                else
1749                        mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1750        }
1751}
1752
1753static void
1754mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1755                                 struct mlxsw_sp_bridge_port *bridge_port,
1756                                 bool add)
1757{
1758        struct mlxsw_sp_bridge_device *bridge_device;
1759        struct mlxsw_sp_mid *mid;
1760
1761        bridge_device = bridge_port->bridge_device;
1762
1763        list_for_each_entry(mid, &bridge_device->mids_list, list) {
1764                if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1765                        mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1766        }
1767}
1768
1769static int mlxsw_sp_port_obj_add(struct net_device *dev, const void *ctx,
1770                                 const struct switchdev_obj *obj,
1771                                 struct netlink_ext_ack *extack)
1772{
1773        struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1774        const struct switchdev_obj_port_vlan *vlan;
1775        int err = 0;
1776
1777        switch (obj->id) {
1778        case SWITCHDEV_OBJ_ID_PORT_VLAN:
1779                vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1780
1781                err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, extack);
1782
1783                /* The event is emitted before the changes are actually
1784                 * applied to the bridge. Therefore schedule the respin
1785                 * call for later, so that the respin logic sees the
1786                 * updated bridge state.
1787                 */
1788                mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1789                break;
1790        case SWITCHDEV_OBJ_ID_PORT_MDB:
1791                err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1792                                            SWITCHDEV_OBJ_PORT_MDB(obj));
1793                break;
1794        default:
1795                err = -EOPNOTSUPP;
1796                break;
1797        }
1798
1799        return err;
1800}
1801
1802static void
1803mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1804                              struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1805{
1806        u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : mlxsw_sp_port->pvid;
1807        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1808        u16 proto;
1809
1810        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1811        if (WARN_ON(!mlxsw_sp_port_vlan))
1812                return;
1813
1814        mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1815        br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1816        mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1817        mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1818        mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1819}
1820
1821static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1822                                   const struct switchdev_obj_port_vlan *vlan)
1823{
1824        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1825        struct net_device *orig_dev = vlan->obj.orig_dev;
1826        struct mlxsw_sp_bridge_port *bridge_port;
1827
1828        if (netif_is_bridge_master(orig_dev))
1829                return -EOPNOTSUPP;
1830
1831        bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1832        if (WARN_ON(!bridge_port))
1833                return -EINVAL;
1834
1835        if (!bridge_port->bridge_device->vlan_enabled)
1836                return 0;
1837
1838        mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vlan->vid);
1839
1840        return 0;
1841}
1842
1843static int
1844__mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1845                        struct mlxsw_sp_bridge_port *bridge_port,
1846                        struct mlxsw_sp_mid *mid)
1847{
1848        struct net_device *dev = mlxsw_sp_port->dev;
1849        int err;
1850
1851        if (bridge_port->bridge_device->multicast_enabled &&
1852            !bridge_port->mrouter) {
1853                err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1854                if (err)
1855                        netdev_err(dev, "Unable to remove port from SMID\n");
1856        }
1857
1858        err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1859        if (err)
1860                netdev_err(dev, "Unable to remove MC SFD\n");
1861
1862        return err;
1863}
1864
1865static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1866                                 const struct switchdev_obj_port_mdb *mdb)
1867{
1868        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1869        struct net_device *orig_dev = mdb->obj.orig_dev;
1870        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1871        struct mlxsw_sp_bridge_device *bridge_device;
1872        struct net_device *dev = mlxsw_sp_port->dev;
1873        struct mlxsw_sp_bridge_port *bridge_port;
1874        struct mlxsw_sp_mid *mid;
1875        u16 fid_index;
1876
1877        bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1878        if (!bridge_port)
1879                return 0;
1880
1881        bridge_device = bridge_port->bridge_device;
1882        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1883                                                               bridge_device,
1884                                                               mdb->vid);
1885        if (!mlxsw_sp_port_vlan)
1886                return 0;
1887
1888        fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1889
1890        mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1891        if (!mid) {
1892                netdev_err(dev, "Unable to remove port from MC DB\n");
1893                return -EINVAL;
1894        }
1895
1896        return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1897}
1898
1899static void
1900mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1901                               struct mlxsw_sp_bridge_port *bridge_port)
1902{
1903        struct mlxsw_sp_bridge_device *bridge_device;
1904        struct mlxsw_sp_mid *mid, *tmp;
1905
1906        bridge_device = bridge_port->bridge_device;
1907
1908        list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1909                if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1910                        __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1911                                                mid);
1912                } else if (bridge_device->multicast_enabled &&
1913                           bridge_port->mrouter) {
1914                        mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1915                }
1916        }
1917}
1918
1919static int mlxsw_sp_port_obj_del(struct net_device *dev, const void *ctx,
1920                                 const struct switchdev_obj *obj)
1921{
1922        struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1923        int err = 0;
1924
1925        switch (obj->id) {
1926        case SWITCHDEV_OBJ_ID_PORT_VLAN:
1927                err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1928                                              SWITCHDEV_OBJ_PORT_VLAN(obj));
1929                break;
1930        case SWITCHDEV_OBJ_ID_PORT_MDB:
1931                err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1932                                            SWITCHDEV_OBJ_PORT_MDB(obj));
1933                break;
1934        default:
1935                err = -EOPNOTSUPP;
1936                break;
1937        }
1938
1939        mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1940
1941        return err;
1942}
1943
1944static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1945                                                   u16 lag_id)
1946{
1947        struct mlxsw_sp_port *mlxsw_sp_port;
1948        u64 max_lag_members;
1949        int i;
1950
1951        max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1952                                             MAX_LAG_MEMBERS);
1953        for (i = 0; i < max_lag_members; i++) {
1954                mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1955                if (mlxsw_sp_port)
1956                        return mlxsw_sp_port;
1957        }
1958        return NULL;
1959}
1960
1961static int
1962mlxsw_sp_bridge_vlan_aware_port_join(struct mlxsw_sp_bridge_port *bridge_port,
1963                                     struct mlxsw_sp_port *mlxsw_sp_port,
1964                                     struct netlink_ext_ack *extack)
1965{
1966        if (is_vlan_dev(bridge_port->dev)) {
1967                NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1968                return -EINVAL;
1969        }
1970
1971        /* Port is no longer usable as a router interface */
1972        if (mlxsw_sp_port->default_vlan->fid)
1973                mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
1974
1975        return 0;
1976}
1977
1978static int
1979mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1980                                struct mlxsw_sp_bridge_port *bridge_port,
1981                                struct mlxsw_sp_port *mlxsw_sp_port,
1982                                struct netlink_ext_ack *extack)
1983{
1984        return mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
1985                                                    extack);
1986}
1987
1988static void
1989mlxsw_sp_bridge_vlan_aware_port_leave(struct mlxsw_sp_port *mlxsw_sp_port)
1990{
1991        /* Make sure untagged frames are allowed to ingress */
1992        mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID,
1993                               ETH_P_8021Q);
1994}
1995
1996static void
1997mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1998                                 struct mlxsw_sp_bridge_port *bridge_port,
1999                                 struct mlxsw_sp_port *mlxsw_sp_port)
2000{
2001        mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2002}
2003
2004static int
2005mlxsw_sp_bridge_vlan_aware_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2006                                      const struct net_device *vxlan_dev,
2007                                      u16 vid, u16 ethertype,
2008                                      struct netlink_ext_ack *extack)
2009{
2010        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2011        struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2012        struct mlxsw_sp_nve_params params = {
2013                .type = MLXSW_SP_NVE_TYPE_VXLAN,
2014                .vni = vxlan->cfg.vni,
2015                .dev = vxlan_dev,
2016                .ethertype = ethertype,
2017        };
2018        struct mlxsw_sp_fid *fid;
2019        int err;
2020
2021        /* If the VLAN is 0, we need to find the VLAN that is configured as
2022         * PVID and egress untagged on the bridge port of the VxLAN device.
2023         * It is possible no such VLAN exists
2024         */
2025        if (!vid) {
2026                err = mlxsw_sp_vxlan_mapped_vid(vxlan_dev, &vid);
2027                if (err || !vid)
2028                        return err;
2029        }
2030
2031        fid = mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2032        if (IS_ERR(fid)) {
2033                NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1Q FID");
2034                return PTR_ERR(fid);
2035        }
2036
2037        if (mlxsw_sp_fid_vni_is_set(fid)) {
2038                NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2039                err = -EINVAL;
2040                goto err_vni_exists;
2041        }
2042
2043        err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, &params, extack);
2044        if (err)
2045                goto err_nve_fid_enable;
2046
2047        return 0;
2048
2049err_nve_fid_enable:
2050err_vni_exists:
2051        mlxsw_sp_fid_put(fid);
2052        return err;
2053}
2054
2055static int
2056mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2057                                 const struct net_device *vxlan_dev, u16 vid,
2058                                 struct netlink_ext_ack *extack)
2059{
2060        return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2061                                                     vid, ETH_P_8021Q, extack);
2062}
2063
2064static struct net_device *
2065mlxsw_sp_bridge_8021q_vxlan_dev_find(struct net_device *br_dev, u16 vid)
2066{
2067        struct net_device *dev;
2068        struct list_head *iter;
2069
2070        netdev_for_each_lower_dev(br_dev, dev, iter) {
2071                u16 pvid;
2072                int err;
2073
2074                if (!netif_is_vxlan(dev))
2075                        continue;
2076
2077                err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
2078                if (err || pvid != vid)
2079                        continue;
2080
2081                return dev;
2082        }
2083
2084        return NULL;
2085}
2086
2087static struct mlxsw_sp_fid *
2088mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2089                              u16 vid, struct netlink_ext_ack *extack)
2090{
2091        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2092
2093        return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2094}
2095
2096static struct mlxsw_sp_fid *
2097mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2098                                 u16 vid)
2099{
2100        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2101
2102        return mlxsw_sp_fid_8021q_lookup(mlxsw_sp, vid);
2103}
2104
2105static u16
2106mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2107                              const struct mlxsw_sp_fid *fid)
2108{
2109        return mlxsw_sp_fid_8021q_vid(fid);
2110}
2111
2112static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
2113        .port_join      = mlxsw_sp_bridge_8021q_port_join,
2114        .port_leave     = mlxsw_sp_bridge_8021q_port_leave,
2115        .vxlan_join     = mlxsw_sp_bridge_8021q_vxlan_join,
2116        .fid_get        = mlxsw_sp_bridge_8021q_fid_get,
2117        .fid_lookup     = mlxsw_sp_bridge_8021q_fid_lookup,
2118        .fid_vid        = mlxsw_sp_bridge_8021q_fid_vid,
2119};
2120
2121static bool
2122mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
2123                           const struct net_device *br_dev)
2124{
2125        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2126
2127        list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
2128                            list) {
2129                if (mlxsw_sp_port_vlan->bridge_port &&
2130                    mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
2131                    br_dev)
2132                        return true;
2133        }
2134
2135        return false;
2136}
2137
2138static int
2139mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2140                                struct mlxsw_sp_bridge_port *bridge_port,
2141                                struct mlxsw_sp_port *mlxsw_sp_port,
2142                                struct netlink_ext_ack *extack)
2143{
2144        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2145        struct net_device *dev = bridge_port->dev;
2146        u16 vid;
2147
2148        vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2149        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2150        if (WARN_ON(!mlxsw_sp_port_vlan))
2151                return -EINVAL;
2152
2153        if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2154                NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2155                return -EINVAL;
2156        }
2157
2158        /* Port is no longer usable as a router interface */
2159        if (mlxsw_sp_port_vlan->fid)
2160                mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2161
2162        return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
2163                                              extack);
2164}
2165
2166static void
2167mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2168                                 struct mlxsw_sp_bridge_port *bridge_port,
2169                                 struct mlxsw_sp_port *mlxsw_sp_port)
2170{
2171        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2172        struct net_device *dev = bridge_port->dev;
2173        u16 vid;
2174
2175        vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2176        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2177        if (!mlxsw_sp_port_vlan || !mlxsw_sp_port_vlan->bridge_port)
2178                return;
2179
2180        mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2181}
2182
2183static int
2184mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2185                                 const struct net_device *vxlan_dev, u16 vid,
2186                                 struct netlink_ext_ack *extack)
2187{
2188        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2189        struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2190        struct mlxsw_sp_nve_params params = {
2191                .type = MLXSW_SP_NVE_TYPE_VXLAN,
2192                .vni = vxlan->cfg.vni,
2193                .dev = vxlan_dev,
2194                .ethertype = ETH_P_8021Q,
2195        };
2196        struct mlxsw_sp_fid *fid;
2197        int err;
2198
2199        fid = mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2200        if (IS_ERR(fid)) {
2201                NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1D FID");
2202                return -EINVAL;
2203        }
2204
2205        if (mlxsw_sp_fid_vni_is_set(fid)) {
2206                NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2207                err = -EINVAL;
2208                goto err_vni_exists;
2209        }
2210
2211        err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, &params, extack);
2212        if (err)
2213                goto err_nve_fid_enable;
2214
2215        return 0;
2216
2217err_nve_fid_enable:
2218err_vni_exists:
2219        mlxsw_sp_fid_put(fid);
2220        return err;
2221}
2222
2223static struct mlxsw_sp_fid *
2224mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2225                              u16 vid, struct netlink_ext_ack *extack)
2226{
2227        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2228
2229        return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2230}
2231
2232static struct mlxsw_sp_fid *
2233mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2234                                 u16 vid)
2235{
2236        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2237
2238        /* The only valid VLAN for a VLAN-unaware bridge is 0 */
2239        if (vid)
2240                return NULL;
2241
2242        return mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2243}
2244
2245static u16
2246mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2247                              const struct mlxsw_sp_fid *fid)
2248{
2249        return 0;
2250}
2251
2252static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2253        .port_join      = mlxsw_sp_bridge_8021d_port_join,
2254        .port_leave     = mlxsw_sp_bridge_8021d_port_leave,
2255        .vxlan_join     = mlxsw_sp_bridge_8021d_vxlan_join,
2256        .fid_get        = mlxsw_sp_bridge_8021d_fid_get,
2257        .fid_lookup     = mlxsw_sp_bridge_8021d_fid_lookup,
2258        .fid_vid        = mlxsw_sp_bridge_8021d_fid_vid,
2259};
2260
2261static int
2262mlxsw_sp_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2263                                 struct mlxsw_sp_bridge_port *bridge_port,
2264                                 struct mlxsw_sp_port *mlxsw_sp_port,
2265                                 struct netlink_ext_ack *extack)
2266{
2267        int err;
2268
2269        err = mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, true, false);
2270        if (err)
2271                return err;
2272
2273        err = mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
2274                                                   extack);
2275        if (err)
2276                goto err_bridge_vlan_aware_port_join;
2277
2278        return 0;
2279
2280err_bridge_vlan_aware_port_join:
2281        mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2282        return err;
2283}
2284
2285static void
2286mlxsw_sp_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2287                                  struct mlxsw_sp_bridge_port *bridge_port,
2288                                  struct mlxsw_sp_port *mlxsw_sp_port)
2289{
2290        mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2291        mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2292}
2293
2294static int
2295mlxsw_sp_bridge_8021ad_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2296                                  const struct net_device *vxlan_dev, u16 vid,
2297                                  struct netlink_ext_ack *extack)
2298{
2299        return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2300                                                     vid, ETH_P_8021AD, extack);
2301}
2302
2303static const struct mlxsw_sp_bridge_ops mlxsw_sp1_bridge_8021ad_ops = {
2304        .port_join      = mlxsw_sp_bridge_8021ad_port_join,
2305        .port_leave     = mlxsw_sp_bridge_8021ad_port_leave,
2306        .vxlan_join     = mlxsw_sp_bridge_8021ad_vxlan_join,
2307        .fid_get        = mlxsw_sp_bridge_8021q_fid_get,
2308        .fid_lookup     = mlxsw_sp_bridge_8021q_fid_lookup,
2309        .fid_vid        = mlxsw_sp_bridge_8021q_fid_vid,
2310};
2311
2312static int
2313mlxsw_sp2_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2314                                  struct mlxsw_sp_bridge_port *bridge_port,
2315                                  struct mlxsw_sp_port *mlxsw_sp_port,
2316                                  struct netlink_ext_ack *extack)
2317{
2318        int err;
2319
2320        /* The EtherType of decapsulated packets is determined at the egress
2321         * port to allow 802.1d and 802.1ad bridges with VXLAN devices to
2322         * co-exist.
2323         */
2324        err = mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021AD);
2325        if (err)
2326                return err;
2327
2328        err = mlxsw_sp_bridge_8021ad_port_join(bridge_device, bridge_port,
2329                                               mlxsw_sp_port, extack);
2330        if (err)
2331                goto err_bridge_8021ad_port_join;
2332
2333        return 0;
2334
2335err_bridge_8021ad_port_join:
2336        mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q);
2337        return err;
2338}
2339
2340static void
2341mlxsw_sp2_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2342                                   struct mlxsw_sp_bridge_port *bridge_port,
2343                                   struct mlxsw_sp_port *mlxsw_sp_port)
2344{
2345        mlxsw_sp_bridge_8021ad_port_leave(bridge_device, bridge_port,
2346                                          mlxsw_sp_port);
2347        mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q);
2348}
2349
2350static const struct mlxsw_sp_bridge_ops mlxsw_sp2_bridge_8021ad_ops = {
2351        .port_join      = mlxsw_sp2_bridge_8021ad_port_join,
2352        .port_leave     = mlxsw_sp2_bridge_8021ad_port_leave,
2353        .vxlan_join     = mlxsw_sp_bridge_8021ad_vxlan_join,
2354        .fid_get        = mlxsw_sp_bridge_8021q_fid_get,
2355        .fid_lookup     = mlxsw_sp_bridge_8021q_fid_lookup,
2356        .fid_vid        = mlxsw_sp_bridge_8021q_fid_vid,
2357};
2358
2359int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2360                              struct net_device *brport_dev,
2361                              struct net_device *br_dev,
2362                              struct netlink_ext_ack *extack)
2363{
2364        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2365        struct mlxsw_sp_bridge_device *bridge_device;
2366        struct mlxsw_sp_bridge_port *bridge_port;
2367        int err;
2368
2369        bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev,
2370                                               extack);
2371        if (IS_ERR(bridge_port))
2372                return PTR_ERR(bridge_port);
2373        bridge_device = bridge_port->bridge_device;
2374
2375        err = bridge_device->ops->port_join(bridge_device, bridge_port,
2376                                            mlxsw_sp_port, extack);
2377        if (err)
2378                goto err_port_join;
2379
2380        return 0;
2381
2382err_port_join:
2383        mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2384        return err;
2385}
2386
2387void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2388                                struct net_device *brport_dev,
2389                                struct net_device *br_dev)
2390{
2391        struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2392        struct mlxsw_sp_bridge_device *bridge_device;
2393        struct mlxsw_sp_bridge_port *bridge_port;
2394
2395        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2396        if (!bridge_device)
2397                return;
2398        bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2399        if (!bridge_port)
2400                return;
2401
2402        bridge_device->ops->port_leave(bridge_device, bridge_port,
2403                                       mlxsw_sp_port);
2404        mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2405}
2406
2407int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp *mlxsw_sp,
2408                               const struct net_device *br_dev,
2409                               const struct net_device *vxlan_dev, u16 vid,
2410                               struct netlink_ext_ack *extack)
2411{
2412        struct mlxsw_sp_bridge_device *bridge_device;
2413
2414        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2415        if (WARN_ON(!bridge_device))
2416                return -EINVAL;
2417
2418        return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid,
2419                                              extack);
2420}
2421
2422void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp *mlxsw_sp,
2423                                 const struct net_device *vxlan_dev)
2424{
2425        struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2426        struct mlxsw_sp_fid *fid;
2427
2428        /* If the VxLAN device is down, then the FID does not have a VNI */
2429        fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan->cfg.vni);
2430        if (!fid)
2431                return;
2432
2433        mlxsw_sp_nve_fid_disable(mlxsw_sp, fid);
2434        /* Drop both the reference we just took during lookup and the reference
2435         * the VXLAN device took.
2436         */
2437        mlxsw_sp_fid_put(fid);
2438        mlxsw_sp_fid_put(fid);
2439}
2440
2441static void
2442mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr *vxlan_addr,
2443                                      enum mlxsw_sp_l3proto *proto,
2444                                      union mlxsw_sp_l3addr *addr)
2445{
2446        if (vxlan_addr->sa.sa_family == AF_INET) {
2447                addr->addr4 = vxlan_addr->sin.sin_addr.s_addr;
2448                *proto = MLXSW_SP_L3_PROTO_IPV4;
2449        } else {
2450                addr->addr6 = vxlan_addr->sin6.sin6_addr;
2451                *proto = MLXSW_SP_L3_PROTO_IPV6;
2452        }
2453}
2454
2455static void
2456mlxsw_sp_switchdev_addr_vxlan_convert(enum mlxsw_sp_l3proto proto,
2457                                      const union mlxsw_sp_l3addr *addr,
2458                                      union vxlan_addr *vxlan_addr)
2459{
2460        switch (proto) {
2461        case MLXSW_SP_L3_PROTO_IPV4:
2462                vxlan_addr->sa.sa_family = AF_INET;
2463                vxlan_addr->sin.sin_addr.s_addr = addr->addr4;
2464                break;
2465        case MLXSW_SP_L3_PROTO_IPV6:
2466                vxlan_addr->sa.sa_family = AF_INET6;
2467                vxlan_addr->sin6.sin6_addr = addr->addr6;
2468                break;
2469        }
2470}
2471
2472static void mlxsw_sp_fdb_vxlan_call_notifiers(struct net_device *dev,
2473                                              const char *mac,
2474                                              enum mlxsw_sp_l3proto proto,
2475                                              union mlxsw_sp_l3addr *addr,
2476                                              __be32 vni, bool adding)
2477{
2478        struct switchdev_notifier_vxlan_fdb_info info;
2479        struct vxlan_dev *vxlan = netdev_priv(dev);
2480        enum switchdev_notifier_type type;
2481
2482        type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE :
2483                        SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE;
2484        mlxsw_sp_switchdev_addr_vxlan_convert(proto, addr, &info.remote_ip);
2485        info.remote_port = vxlan->cfg.dst_port;
2486        info.remote_vni = vni;
2487        info.remote_ifindex = 0;
2488        ether_addr_copy(info.eth_addr, mac);
2489        info.vni = vni;
2490        info.offloaded = adding;
2491        call_switchdev_notifiers(type, dev, &info.info, NULL);
2492}
2493
2494static void mlxsw_sp_fdb_nve_call_notifiers(struct net_device *dev,
2495                                            const char *mac,
2496                                            enum mlxsw_sp_l3proto proto,
2497                                            union mlxsw_sp_l3addr *addr,
2498                                            __be32 vni,
2499                                            bool adding)
2500{
2501        if (netif_is_vxlan(dev))
2502                mlxsw_sp_fdb_vxlan_call_notifiers(dev, mac, proto, addr, vni,
2503                                                  adding);
2504}
2505
2506static void
2507mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2508                            const char *mac, u16 vid,
2509                            struct net_device *dev, bool offloaded)
2510{
2511        struct switchdev_notifier_fdb_info info = {};
2512
2513        info.addr = mac;
2514        info.vid = vid;
2515        info.offloaded = offloaded;
2516        call_switchdev_notifiers(type, dev, &info.info, NULL);
2517}
2518
2519static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2520                                            char *sfn_pl, int rec_index,
2521                                            bool adding)
2522{
2523        unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
2524        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2525        struct mlxsw_sp_bridge_device *bridge_device;
2526        struct mlxsw_sp_bridge_port *bridge_port;
2527        struct mlxsw_sp_port *mlxsw_sp_port;
2528        enum switchdev_notifier_type type;
2529        char mac[ETH_ALEN];
2530        u8 local_port;
2531        u16 vid, fid;
2532        bool do_notification = true;
2533        int err;
2534
2535        mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2536
2537        if (WARN_ON_ONCE(local_port >= max_ports))
2538                return;
2539        mlxsw_sp_port = mlxsw_sp->ports[local_port];
2540        if (!mlxsw_sp_port) {
2541                dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2542                goto just_remove;
2543        }
2544
2545        if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2546                goto just_remove;
2547
2548        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2549        if (!mlxsw_sp_port_vlan) {
2550                netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2551                goto just_remove;
2552        }
2553
2554        bridge_port = mlxsw_sp_port_vlan->bridge_port;
2555        if (!bridge_port) {
2556                netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2557                goto just_remove;
2558        }
2559
2560        bridge_device = bridge_port->bridge_device;
2561        vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2562
2563do_fdb_op:
2564        err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2565                                      adding, true);
2566        if (err) {
2567                dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2568                return;
2569        }
2570
2571        if (!do_notification)
2572                return;
2573        type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2574        mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2575
2576        return;
2577
2578just_remove:
2579        adding = false;
2580        do_notification = false;
2581        goto do_fdb_op;
2582}
2583
2584static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2585                                                char *sfn_pl, int rec_index,
2586                                                bool adding)
2587{
2588        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2589        struct mlxsw_sp_bridge_device *bridge_device;
2590        struct mlxsw_sp_bridge_port *bridge_port;
2591        struct mlxsw_sp_port *mlxsw_sp_port;
2592        enum switchdev_notifier_type type;
2593        char mac[ETH_ALEN];
2594        u16 lag_vid = 0;
2595        u16 lag_id;
2596        u16 vid, fid;
2597        bool do_notification = true;
2598        int err;
2599
2600        mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2601        mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2602        if (!mlxsw_sp_port) {
2603                dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2604                goto just_remove;
2605        }
2606
2607        if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2608                goto just_remove;
2609
2610        mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2611        if (!mlxsw_sp_port_vlan) {
2612                netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2613                goto just_remove;
2614        }
2615
2616        bridge_port = mlxsw_sp_port_vlan->bridge_port;
2617        if (!bridge_port) {
2618                netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2619                goto just_remove;
2620        }
2621
2622        bridge_device = bridge_port->bridge_device;
2623        vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2624        lag_vid = mlxsw_sp_fid_lag_vid_valid(mlxsw_sp_port_vlan->fid) ?
2625                  mlxsw_sp_port_vlan->vid : 0;
2626
2627do_fdb_op:
2628        err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2629                                          adding, true);
2630        if (err) {
2631                dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2632                return;
2633        }
2634
2635        if (!do_notification)
2636                return;
2637        type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2638        mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2639
2640        return;
2641
2642just_remove:
2643        adding = false;
2644        do_notification = false;
2645        goto do_fdb_op;
2646}
2647
2648static int
2649__mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2650                                            const struct mlxsw_sp_fid *fid,
2651                                            bool adding,
2652                                            struct net_device **nve_dev,
2653                                            u16 *p_vid, __be32 *p_vni)
2654{
2655        struct mlxsw_sp_bridge_device *bridge_device;
2656        struct net_device *br_dev, *dev;
2657        int nve_ifindex;
2658        int err;
2659
2660        err = mlxsw_sp_fid_nve_ifindex(fid, &nve_ifindex);
2661        if (err)
2662                return err;
2663
2664        err = mlxsw_sp_fid_vni(fid, p_vni);
2665        if (err)
2666                return err;
2667
2668        dev = __dev_get_by_index(mlxsw_sp_net(mlxsw_sp), nve_ifindex);
2669        if (!dev)
2670                return -EINVAL;
2671        *nve_dev = dev;
2672
2673        if (!netif_running(dev))
2674                return -EINVAL;
2675
2676        if (adding && !br_port_flag_is_set(dev, BR_LEARNING))
2677                return -EINVAL;
2678
2679        if (adding && netif_is_vxlan(dev)) {
2680                struct vxlan_dev *vxlan = netdev_priv(dev);
2681
2682                if (!(vxlan->cfg.flags & VXLAN_F_LEARN))
2683                        return -EINVAL;
2684        }
2685
2686        br_dev = netdev_master_upper_dev_get(dev);
2687        if (!br_dev)
2688                return -EINVAL;
2689
2690        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2691        if (!bridge_device)
2692                return -EINVAL;
2693
2694        *p_vid = bridge_device->ops->fid_vid(bridge_device, fid);
2695
2696        return 0;
2697}
2698
2699static void mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2700                                                      char *sfn_pl,
2701                                                      int rec_index,
2702                                                      bool adding)
2703{
2704        enum mlxsw_reg_sfn_uc_tunnel_protocol sfn_proto;
2705        enum switchdev_notifier_type type;
2706        struct net_device *nve_dev;
2707        union mlxsw_sp_l3addr addr;
2708        struct mlxsw_sp_fid *fid;
2709        char mac[ETH_ALEN];
2710        u16 fid_index, vid;
2711        __be32 vni;
2712        u32 uip;
2713        int err;
2714
2715        mlxsw_reg_sfn_uc_tunnel_unpack(sfn_pl, rec_index, mac, &fid_index,
2716                                       &uip, &sfn_proto);
2717
2718        fid = mlxsw_sp_fid_lookup_by_index(mlxsw_sp, fid_index);
2719        if (!fid)
2720                goto err_fid_lookup;
2721
2722        err = mlxsw_sp_nve_learned_ip_resolve(mlxsw_sp, uip,
2723                                              (enum mlxsw_sp_l3proto) sfn_proto,
2724                                              &addr);
2725        if (err)
2726                goto err_ip_resolve;
2727
2728        err = __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, fid, adding,
2729                                                          &nve_dev, &vid, &vni);
2730        if (err)
2731                goto err_fdb_process;
2732
2733        err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2734                                             (enum mlxsw_sp_l3proto) sfn_proto,
2735                                             &addr, adding, true);
2736        if (err)
2737                goto err_fdb_op;
2738
2739        mlxsw_sp_fdb_nve_call_notifiers(nve_dev, mac,
2740                                        (enum mlxsw_sp_l3proto) sfn_proto,
2741                                        &addr, vni, adding);
2742
2743        type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE :
2744                        SWITCHDEV_FDB_DEL_TO_BRIDGE;
2745        mlxsw_sp_fdb_call_notifiers(type, mac, vid, nve_dev, adding);
2746
2747        mlxsw_sp_fid_put(fid);
2748
2749        return;
2750
2751err_fdb_op:
2752err_fdb_process:
2753err_ip_resolve:
2754        mlxsw_sp_fid_put(fid);
2755err_fid_lookup:
2756        /* Remove an FDB entry in case we cannot process it. Otherwise the
2757         * device will keep sending the same notification over and over again.
2758         */
2759        mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2760                                       (enum mlxsw_sp_l3proto) sfn_proto, &addr,
2761                                       false, true);
2762}
2763
2764static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2765                                            char *sfn_pl, int rec_index)
2766{
2767        switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2768        case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2769                mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2770                                                rec_index, true);
2771                break;
2772        case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2773                mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2774                                                rec_index, false);
2775                break;
2776        case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2777                mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2778                                                    rec_index, true);
2779                break;
2780        case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2781                mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2782                                                    rec_index, false);
2783                break;
2784        case MLXSW_REG_SFN_REC_TYPE_LEARNED_UNICAST_TUNNEL:
2785                mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2786                                                          rec_index, true);
2787                break;
2788        case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_UNICAST_TUNNEL:
2789                mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2790                                                          rec_index, false);
2791                break;
2792        }
2793}
2794
2795static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp,
2796                                              bool no_delay)
2797{
2798        struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2799        unsigned int interval = no_delay ? 0 : bridge->fdb_notify.interval;
2800
2801        mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2802                               msecs_to_jiffies(interval));
2803}
2804
2805#define MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION 10
2806
2807static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2808{
2809        struct mlxsw_sp_bridge *bridge;
2810        struct mlxsw_sp *mlxsw_sp;
2811        char *sfn_pl;
2812        int queries;
2813        u8 num_rec;
2814        int i;
2815        int err;
2816
2817        sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2818        if (!sfn_pl)
2819                return;
2820
2821        bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2822        mlxsw_sp = bridge->mlxsw_sp;
2823
2824        rtnl_lock();
2825        queries = MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION;
2826        while (queries > 0) {
2827                mlxsw_reg_sfn_pack(sfn_pl);
2828                err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2829                if (err) {
2830                        dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2831                        goto out;
2832                }
2833                num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2834                for (i = 0; i < num_rec; i++)
2835                        mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2836                if (num_rec != MLXSW_REG_SFN_REC_MAX_COUNT)
2837                        goto out;
2838                queries--;
2839        }
2840
2841out:
2842        rtnl_unlock();
2843        kfree(sfn_pl);
2844        mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, !queries);
2845}
2846
2847struct mlxsw_sp_switchdev_event_work {
2848        struct work_struct work;
2849        union {
2850                struct switchdev_notifier_fdb_info fdb_info;
2851                struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2852        };
2853        struct net_device *dev;
2854        unsigned long event;
2855};
2856
2857static void
2858mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp,
2859                                          struct mlxsw_sp_switchdev_event_work *
2860                                          switchdev_work,
2861                                          struct mlxsw_sp_fid *fid, __be32 vni)
2862{
2863        struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2864        struct switchdev_notifier_fdb_info *fdb_info;
2865        struct net_device *dev = switchdev_work->dev;
2866        enum mlxsw_sp_l3proto proto;
2867        union mlxsw_sp_l3addr addr;
2868        int err;
2869
2870        fdb_info = &switchdev_work->fdb_info;
2871        err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info);
2872        if (err)
2873                return;
2874
2875        mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip,
2876                                              &proto, &addr);
2877
2878        switch (switchdev_work->event) {
2879        case SWITCHDEV_FDB_ADD_TO_DEVICE:
2880                err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2881                                                     vxlan_fdb_info.eth_addr,
2882                                                     mlxsw_sp_fid_index(fid),
2883                                                     proto, &addr, true, false);
2884                if (err)
2885                        return;
2886                vxlan_fdb_info.offloaded = true;
2887                call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2888                                         &vxlan_fdb_info.info, NULL);
2889                mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2890                                            vxlan_fdb_info.eth_addr,
2891                                            fdb_info->vid, dev, true);
2892                break;
2893        case SWITCHDEV_FDB_DEL_TO_DEVICE:
2894                err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2895                                                     vxlan_fdb_info.eth_addr,
2896                                                     mlxsw_sp_fid_index(fid),
2897                                                     proto, &addr, false,
2898                                                     false);
2899                vxlan_fdb_info.offloaded = false;
2900                call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2901                                         &vxlan_fdb_info.info, NULL);
2902                break;
2903        }
2904}
2905
2906static void
2907mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work *
2908                                        switchdev_work)
2909{
2910        struct mlxsw_sp_bridge_device *bridge_device;
2911        struct net_device *dev = switchdev_work->dev;
2912        struct net_device *br_dev;
2913        struct mlxsw_sp *mlxsw_sp;
2914        struct mlxsw_sp_fid *fid;
2915        __be32 vni;
2916        int err;
2917
2918        if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE &&
2919            switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE)
2920                return;
2921
2922        if (switchdev_work->event == SWITCHDEV_FDB_ADD_TO_DEVICE &&
2923            (!switchdev_work->fdb_info.added_by_user ||
2924             switchdev_work->fdb_info.is_local))
2925                return;
2926
2927        if (!netif_running(dev))
2928                return;
2929        br_dev = netdev_master_upper_dev_get(dev);
2930        if (!br_dev)
2931                return;
2932        if (!netif_is_bridge_master(br_dev))
2933                return;
2934        mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2935        if (!mlxsw_sp)
2936                return;
2937        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2938        if (!bridge_device)
2939                return;
2940
2941        fid = bridge_device->ops->fid_lookup(bridge_device,
2942                                             switchdev_work->fdb_info.vid);
2943        if (!fid)
2944                return;
2945
2946        err = mlxsw_sp_fid_vni(fid, &vni);
2947        if (err)
2948                goto out;
2949
2950        mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid,
2951                                                  vni);
2952
2953out:
2954        mlxsw_sp_fid_put(fid);
2955}
2956
2957static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work)
2958{
2959        struct mlxsw_sp_switchdev_event_work *switchdev_work =
2960                container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2961        struct net_device *dev = switchdev_work->dev;
2962        struct switchdev_notifier_fdb_info *fdb_info;
2963        struct mlxsw_sp_port *mlxsw_sp_port;
2964        int err;
2965
2966        rtnl_lock();
2967        if (netif_is_vxlan(dev)) {
2968                mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work);
2969                goto out;
2970        }
2971
2972        mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2973        if (!mlxsw_sp_port)
2974                goto out;
2975
2976        switch (switchdev_work->event) {
2977        case SWITCHDEV_FDB_ADD_TO_DEVICE:
2978                fdb_info = &switchdev_work->fdb_info;
2979                if (!fdb_info->added_by_user || fdb_info->is_local)
2980                        break;
2981                err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2982                if (err)
2983                        break;
2984                mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2985                                            fdb_info->addr,
2986                                            fdb_info->vid, dev, true);
2987                break;
2988        case SWITCHDEV_FDB_DEL_TO_DEVICE:
2989                fdb_info = &switchdev_work->fdb_info;
2990                mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2991                break;
2992        case SWITCHDEV_FDB_ADD_TO_BRIDGE:
2993        case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2994                /* These events are only used to potentially update an existing
2995                 * SPAN mirror.
2996                 */
2997                break;
2998        }
2999
3000        mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
3001
3002out:
3003        rtnl_unlock();
3004        kfree(switchdev_work->fdb_info.addr);
3005        kfree(switchdev_work);
3006        dev_put(dev);
3007}
3008
3009static void
3010mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp,
3011                                 struct mlxsw_sp_switchdev_event_work *
3012                                 switchdev_work)
3013{
3014        struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3015        struct mlxsw_sp_bridge_device *bridge_device;
3016        struct net_device *dev = switchdev_work->dev;
3017        u8 all_zeros_mac[ETH_ALEN] = { 0 };
3018        enum mlxsw_sp_l3proto proto;
3019        union mlxsw_sp_l3addr addr;
3020        struct net_device *br_dev;
3021        struct mlxsw_sp_fid *fid;
3022        u16 vid;
3023        int err;
3024
3025        vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3026        br_dev = netdev_master_upper_dev_get(dev);
3027
3028        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3029        if (!bridge_device)
3030                return;
3031
3032        fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3033        if (!fid)
3034                return;
3035
3036        mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3037                                              &proto, &addr);
3038
3039        if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3040                err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr);
3041                if (err) {
3042                        mlxsw_sp_fid_put(fid);
3043                        return;
3044                }
3045                vxlan_fdb_info->offloaded = true;
3046                call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3047                                         &vxlan_fdb_info->info, NULL);
3048                mlxsw_sp_fid_put(fid);
3049                return;
3050        }
3051
3052        /* The device has a single FDB table, whereas Linux has two - one
3053         * in the bridge driver and another in the VxLAN driver. We only
3054         * program an entry to the device if the MAC points to the VxLAN
3055         * device in the bridge's FDB table
3056         */
3057        vid = bridge_device->ops->fid_vid(bridge_device, fid);
3058        if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev)
3059                goto err_br_fdb_find;
3060
3061        err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3062                                             mlxsw_sp_fid_index(fid), proto,
3063                                             &addr, true, false);
3064        if (err)
3065                goto err_fdb_tunnel_uc_op;
3066        vxlan_fdb_info->offloaded = true;
3067        call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3068                                 &vxlan_fdb_info->info, NULL);
3069        mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3070                                    vxlan_fdb_info->eth_addr, vid, dev, true);
3071
3072        mlxsw_sp_fid_put(fid);
3073
3074        return;
3075
3076err_fdb_tunnel_uc_op:
3077err_br_fdb_find:
3078        mlxsw_sp_fid_put(fid);
3079}
3080
3081static void
3082mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp,
3083                                 struct mlxsw_sp_switchdev_event_work *
3084                                 switchdev_work)
3085{
3086        struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3087        struct mlxsw_sp_bridge_device *bridge_device;
3088        struct net_device *dev = switchdev_work->dev;
3089        struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3090        u8 all_zeros_mac[ETH_ALEN] = { 0 };
3091        enum mlxsw_sp_l3proto proto;
3092        union mlxsw_sp_l3addr addr;
3093        struct mlxsw_sp_fid *fid;
3094        u16 vid;
3095
3096        vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3097
3098        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3099        if (!bridge_device)
3100                return;
3101
3102        fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3103        if (!fid)
3104                return;
3105
3106        mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3107                                              &proto, &addr);
3108
3109        if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3110                mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr);
3111                mlxsw_sp_fid_put(fid);
3112                return;
3113        }
3114
3115        mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3116                                       mlxsw_sp_fid_index(fid), proto, &addr,
3117                                       false, false);
3118        vid = bridge_device->ops->fid_vid(bridge_device, fid);
3119        mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3120                                    vxlan_fdb_info->eth_addr, vid, dev, false);
3121
3122        mlxsw_sp_fid_put(fid);
3123}
3124
3125static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work)
3126{
3127        struct mlxsw_sp_switchdev_event_work *switchdev_work =
3128                container_of(work, struct mlxsw_sp_switchdev_event_work, work);
3129        struct net_device *dev = switchdev_work->dev;
3130        struct mlxsw_sp *mlxsw_sp;
3131        struct net_device *br_dev;
3132
3133        rtnl_lock();
3134
3135        if (!netif_running(dev))
3136                goto out;
3137        br_dev = netdev_master_upper_dev_get(dev);
3138        if (!br_dev)
3139                goto out;
3140        if (!netif_is_bridge_master(br_dev))
3141                goto out;
3142        mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3143        if (!mlxsw_sp)
3144                goto out;
3145
3146        switch (switchdev_work->event) {
3147        case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3148                mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work);
3149                break;
3150        case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3151                mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work);
3152                break;
3153        }
3154
3155out:
3156        rtnl_unlock();
3157        kfree(switchdev_work);
3158        dev_put(dev);
3159}
3160
3161static int
3162mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work *
3163                                      switchdev_work,
3164                                      struct switchdev_notifier_info *info)
3165{
3166        struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev);
3167        struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3168        struct vxlan_config *cfg = &vxlan->cfg;
3169        struct netlink_ext_ack *extack;
3170
3171        extack = switchdev_notifier_info_to_extack(info);
3172        vxlan_fdb_info = container_of(info,
3173                                      struct switchdev_notifier_vxlan_fdb_info,
3174                                      info);
3175
3176        if (vxlan_fdb_info->remote_port != cfg->dst_port) {
3177                NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default remote port is not supported");
3178                return -EOPNOTSUPP;
3179        }
3180        if (vxlan_fdb_info->remote_vni != cfg->vni ||
3181            vxlan_fdb_info->vni != cfg->vni) {
3182                NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default VNI is not supported");
3183                return -EOPNOTSUPP;
3184        }
3185        if (vxlan_fdb_info->remote_ifindex) {
3186                NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Local interface is not supported");
3187                return -EOPNOTSUPP;
3188        }
3189        if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr)) {
3190                NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast MAC addresses not supported");
3191                return -EOPNOTSUPP;
3192        }
3193        if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip)) {
3194                NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast destination IP is not supported");
3195                return -EOPNOTSUPP;
3196        }
3197
3198        switchdev_work->vxlan_fdb_info = *vxlan_fdb_info;
3199
3200        return 0;
3201}
3202
3203/* Called under rcu_read_lock() */
3204static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
3205                                    unsigned long event, void *ptr)
3206{
3207        struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3208        struct mlxsw_sp_switchdev_event_work *switchdev_work;
3209        struct switchdev_notifier_fdb_info *fdb_info;
3210        struct switchdev_notifier_info *info = ptr;
3211        struct net_device *br_dev;
3212        int err;
3213
3214        if (event == SWITCHDEV_PORT_ATTR_SET) {
3215                err = switchdev_handle_port_attr_set(dev, ptr,
3216                                                     mlxsw_sp_port_dev_check,
3217                                                     mlxsw_sp_port_attr_set);
3218                return notifier_from_errno(err);
3219        }
3220
3221        /* Tunnel devices are not our uppers, so check their master instead */
3222        br_dev = netdev_master_upper_dev_get_rcu(dev);
3223        if (!br_dev)
3224                return NOTIFY_DONE;
3225        if (!netif_is_bridge_master(br_dev))
3226                return NOTIFY_DONE;
3227        if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
3228                return NOTIFY_DONE;
3229
3230        switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3231        if (!switchdev_work)
3232                return NOTIFY_BAD;
3233
3234        switchdev_work->dev = dev;
3235        switchdev_work->event = event;
3236
3237        switch (event) {
3238        case SWITCHDEV_FDB_ADD_TO_DEVICE:
3239        case SWITCHDEV_FDB_DEL_TO_DEVICE:
3240        case SWITCHDEV_FDB_ADD_TO_BRIDGE:
3241        case SWITCHDEV_FDB_DEL_TO_BRIDGE:
3242                fdb_info = container_of(info,
3243                                        struct switchdev_notifier_fdb_info,
3244                                        info);
3245                INIT_WORK(&switchdev_work->work,
3246                          mlxsw_sp_switchdev_bridge_fdb_event_work);
3247                memcpy(&switchdev_work->fdb_info, ptr,
3248                       sizeof(switchdev_work->fdb_info));
3249                switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
3250                if (!switchdev_work->fdb_info.addr)
3251                        goto err_addr_alloc;
3252                ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
3253                                fdb_info->addr);
3254                /* Take a reference on the device. This can be either
3255                 * upper device containig mlxsw_sp_port or just a
3256                 * mlxsw_sp_port
3257                 */
3258                dev_hold(dev);
3259                break;
3260        case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3261        case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3262                INIT_WORK(&switchdev_work->work,
3263                          mlxsw_sp_switchdev_vxlan_fdb_event_work);
3264                err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work,
3265                                                            info);
3266                if (err)
3267                        goto err_vxlan_work_prepare;
3268                dev_hold(dev);
3269                break;
3270        default:
3271                kfree(switchdev_work);
3272                return NOTIFY_DONE;
3273        }
3274
3275        mlxsw_core_schedule_work(&switchdev_work->work);
3276
3277        return NOTIFY_DONE;
3278
3279err_vxlan_work_prepare:
3280err_addr_alloc:
3281        kfree(switchdev_work);
3282        return NOTIFY_BAD;
3283}
3284
3285struct notifier_block mlxsw_sp_switchdev_notifier = {
3286        .notifier_call = mlxsw_sp_switchdev_event,
3287};
3288
3289static int
3290mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp *mlxsw_sp,
3291                                  struct mlxsw_sp_bridge_device *bridge_device,
3292                                  const struct net_device *vxlan_dev, u16 vid,
3293                                  bool flag_untagged, bool flag_pvid,
3294                                  struct netlink_ext_ack *extack)
3295{
3296        struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3297        __be32 vni = vxlan->cfg.vni;
3298        struct mlxsw_sp_fid *fid;
3299        u16 old_vid;
3300        int err;
3301
3302        /* We cannot have the same VLAN as PVID and egress untagged on multiple
3303         * VxLAN devices. Note that we get this notification before the VLAN is
3304         * actually added to the bridge's database, so it is not possible for
3305         * the lookup function to return 'vxlan_dev'
3306         */
3307        if (flag_untagged && flag_pvid &&
3308            mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device->dev, vid)) {
3309                NL_SET_ERR_MSG_MOD(extack, "VLAN already mapped to a different VNI");
3310                return -EINVAL;
3311        }
3312
3313        if (!netif_running(vxlan_dev))
3314                return 0;
3315
3316        /* First case: FID is not associated with this VNI, but the new VLAN
3317         * is both PVID and egress untagged. Need to enable NVE on the FID, if
3318         * it exists
3319         */
3320        fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3321        if (!fid) {
3322                if (!flag_untagged || !flag_pvid)
3323                        return 0;
3324                return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev,
3325                                                      vid, extack);
3326        }
3327
3328        /* Second case: FID is associated with the VNI and the VLAN associated
3329         * with the FID is the same as the notified VLAN. This means the flags
3330         * (PVID / egress untagged) were toggled and that NVE should be
3331         * disabled on the FID
3332         */
3333        old_vid = mlxsw_sp_fid_8021q_vid(fid);
3334        if (vid == old_vid) {
3335                if (WARN_ON(flag_untagged && flag_pvid)) {
3336                        mlxsw_sp_fid_put(fid);
3337                        return -EINVAL;
3338                }
3339                mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3340                mlxsw_sp_fid_put(fid);
3341                return 0;
3342        }
3343
3344        /* Third case: A new VLAN was configured on the VxLAN device, but this
3345         * VLAN is not PVID, so there is nothing to do.
3346         */
3347        if (!flag_pvid) {
3348                mlxsw_sp_fid_put(fid);
3349                return 0;
3350        }
3351
3352        /* Fourth case: Thew new VLAN is PVID, which means the VLAN currently
3353         * mapped to the VNI should be unmapped
3354         */
3355        mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3356        mlxsw_sp_fid_put(fid);
3357
3358        /* Fifth case: The new VLAN is also egress untagged, which means the
3359         * VLAN needs to be mapped to the VNI
3360         */
3361        if (!flag_untagged)
3362                return 0;
3363
3364        err = bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid, extack);
3365        if (err)
3366                goto err_vxlan_join;
3367
3368        return 0;
3369
3370err_vxlan_join:
3371        bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, old_vid, NULL);
3372        return err;
3373}
3374
3375static void
3376mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp *mlxsw_sp,
3377                                  struct mlxsw_sp_bridge_device *bridge_device,
3378                                  const struct net_device *vxlan_dev, u16 vid)
3379{
3380        struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3381        __be32 vni = vxlan->cfg.vni;
3382        struct mlxsw_sp_fid *fid;
3383
3384        if (!netif_running(vxlan_dev))
3385                return;
3386
3387        fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3388        if (!fid)
3389                return;
3390
3391        /* A different VLAN than the one mapped to the VNI is deleted */
3392        if (mlxsw_sp_fid_8021q_vid(fid) != vid)
3393                goto out;
3394
3395        mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3396
3397out:
3398        mlxsw_sp_fid_put(fid);
3399}
3400
3401static int
3402mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device *vxlan_dev,
3403                                   struct switchdev_notifier_port_obj_info *
3404                                   port_obj_info)
3405{
3406        struct switchdev_obj_port_vlan *vlan =
3407                SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3408        bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
3409        bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
3410        struct mlxsw_sp_bridge_device *bridge_device;
3411        struct netlink_ext_ack *extack;
3412        struct mlxsw_sp *mlxsw_sp;
3413        struct net_device *br_dev;
3414
3415        extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
3416        br_dev = netdev_master_upper_dev_get(vxlan_dev);
3417        if (!br_dev)
3418                return 0;
3419
3420        mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3421        if (!mlxsw_sp)
3422                return 0;
3423
3424        port_obj_info->handled = true;
3425
3426        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3427        if (!bridge_device)
3428                return -EINVAL;
3429
3430        if (!bridge_device->vlan_enabled)
3431                return 0;
3432
3433        return mlxsw_sp_switchdev_vxlan_vlan_add(mlxsw_sp, bridge_device,
3434                                                 vxlan_dev, vlan->vid,
3435                                                 flag_untagged,
3436                                                 flag_pvid, extack);
3437}
3438
3439static void
3440mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device *vxlan_dev,
3441                                   struct switchdev_notifier_port_obj_info *
3442                                   port_obj_info)
3443{
3444        struct switchdev_obj_port_vlan *vlan =
3445                SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3446        struct mlxsw_sp_bridge_device *bridge_device;
3447        struct mlxsw_sp *mlxsw_sp;
3448        struct net_device *br_dev;
3449
3450        br_dev = netdev_master_upper_dev_get(vxlan_dev);
3451        if (!br_dev)
3452                return;
3453
3454        mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3455        if (!mlxsw_sp)
3456                return;
3457
3458        port_obj_info->handled = true;
3459
3460        bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3461        if (!bridge_device)
3462                return;
3463
3464        if (!bridge_device->vlan_enabled)
3465                return;
3466
3467        mlxsw_sp_switchdev_vxlan_vlan_del(mlxsw_sp, bridge_device, vxlan_dev,
3468                                          vlan->vid);
3469}
3470
3471static int
3472mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device *vxlan_dev,
3473                                        struct switchdev_notifier_port_obj_info *
3474                                        port_obj_info)
3475{
3476        int err = 0;
3477
3478        switch (port_obj_info->obj->id) {
3479        case SWITCHDEV_OBJ_ID_PORT_VLAN:
3480                err = mlxsw_sp_switchdev_vxlan_vlans_add(vxlan_dev,
3481                                                         port_obj_info);
3482                break;
3483        default:
3484                break;
3485        }
3486
3487        return err;
3488}
3489
3490static void
3491mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device *vxlan_dev,
3492                                        struct switchdev_notifier_port_obj_info *
3493                                        port_obj_info)
3494{
3495        switch (port_obj_info->obj->id) {
3496        case SWITCHDEV_OBJ_ID_PORT_VLAN:
3497                mlxsw_sp_switchdev_vxlan_vlans_del(vxlan_dev, port_obj_info);
3498                break;
3499        default:
3500                break;
3501        }
3502}
3503
3504static int mlxsw_sp_switchdev_blocking_event(struct notifier_block *unused,
3505                                             unsigned long event, void *ptr)
3506{
3507        struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3508        int err = 0;
3509
3510        switch (event) {
3511        case SWITCHDEV_PORT_OBJ_ADD:
3512                if (netif_is_vxlan(dev))
3513                        err = mlxsw_sp_switchdev_handle_vxlan_obj_add(dev, ptr);
3514                else
3515                        err = switchdev_handle_port_obj_add(dev, ptr,
3516                                                        mlxsw_sp_port_dev_check,
3517                                                        mlxsw_sp_port_obj_add);
3518                return notifier_from_errno(err);
3519        case SWITCHDEV_PORT_OBJ_DEL:
3520                if (netif_is_vxlan(dev))
3521                        mlxsw_sp_switchdev_handle_vxlan_obj_del(dev, ptr);
3522                else
3523                        err = switchdev_handle_port_obj_del(dev, ptr,
3524                                                        mlxsw_sp_port_dev_check,
3525                                                        mlxsw_sp_port_obj_del);
3526                return notifier_from_errno(err);
3527        case SWITCHDEV_PORT_ATTR_SET:
3528                err = switchdev_handle_port_attr_set(dev, ptr,
3529                                                     mlxsw_sp_port_dev_check,
3530                                                     mlxsw_sp_port_attr_set);
3531                return notifier_from_errno(err);
3532        }
3533
3534        return NOTIFY_DONE;
3535}
3536
3537static struct notifier_block mlxsw_sp_switchdev_blocking_notifier = {
3538        .notifier_call = mlxsw_sp_switchdev_blocking_event,
3539};
3540
3541u8
3542mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
3543{
3544        return bridge_port->stp_state;
3545}
3546
3547static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
3548{
3549        struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
3550        struct notifier_block *nb;
3551        int err;
3552
3553        err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
3554        if (err) {
3555                dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
3556                return err;
3557        }
3558
3559        err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3560        if (err) {
3561                dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
3562                return err;
3563        }
3564
3565        nb = &mlxsw_sp_switchdev_blocking_notifier;
3566        err = register_switchdev_blocking_notifier(nb);
3567        if (err) {
3568                dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev blocking notifier\n");
3569                goto err_register_switchdev_blocking_notifier;
3570        }
3571
3572        INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
3573        bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
3574        mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, false);
3575        return 0;
3576
3577err_register_switchdev_blocking_notifier:
3578        unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3579        return err;
3580}
3581
3582static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
3583{
3584        struct notifier_block *nb;
3585
3586        cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
3587
3588        nb = &mlxsw_sp_switchdev_blocking_notifier;
3589        unregister_switchdev_blocking_notifier(nb);
3590
3591        unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3592}
3593
3594static void mlxsw_sp1_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3595{
3596        mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp1_bridge_8021ad_ops;
3597}
3598
3599const struct mlxsw_sp_switchdev_ops mlxsw_sp1_switchdev_ops = {
3600        .init   = mlxsw_sp1_switchdev_init,
3601};
3602
3603static void mlxsw_sp2_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3604{
3605        mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp2_bridge_8021ad_ops;
3606}
3607
3608const struct mlxsw_sp_switchdev_ops mlxsw_sp2_switchdev_ops = {
3609        .init   = mlxsw_sp2_switchdev_init,
3610};
3611
3612int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3613{
3614        struct mlxsw_sp_bridge *bridge;
3615
3616        bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
3617        if (!bridge)
3618                return -ENOMEM;
3619        mlxsw_sp->bridge = bridge;
3620        bridge->mlxsw_sp = mlxsw_sp;
3621
3622        INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
3623
3624        bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
3625        bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
3626
3627        mlxsw_sp->switchdev_ops->init(mlxsw_sp);
3628
3629        return mlxsw_sp_fdb_init(mlxsw_sp);
3630}
3631
3632void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
3633{
3634        mlxsw_sp_fdb_fini(mlxsw_sp);
3635        WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
3636        kfree(mlxsw_sp->bridge);
3637}
3638
3639