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 26 struct mlxsw_sp_bridge_ops; 27 28 struct 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 47 struct 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 58 struct 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 74 struct mlxsw_sp_bridge_vlan { 75 struct list_head list; 76 struct list_head port_vlan_list; 77 u16 vid; 78 }; 79 80 struct 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 101 struct mlxsw_sp_switchdev_ops { 102 void (*init)(struct mlxsw_sp *mlxsw_sp); 103 }; 104 105 static int 106 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp, 107 struct mlxsw_sp_bridge_port *bridge_port, 108 u16 fid_index); 109 110 static void 111 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port, 112 struct mlxsw_sp_bridge_port *bridge_port); 113 114 static void 115 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port, 116 struct mlxsw_sp_bridge_device 117 *bridge_device); 118 119 static void 120 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port, 121 struct mlxsw_sp_bridge_port *bridge_port, 122 bool add); 123 124 static struct mlxsw_sp_bridge_device * 125 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge, 126 const struct net_device *br_dev) 127 { 128 struct mlxsw_sp_bridge_device *bridge_device; 129 130 list_for_each_entry(bridge_device, &bridge->bridges_list, list) 131 if (bridge_device->dev == br_dev) 132 return bridge_device; 133 134 return NULL; 135 } 136 137 bool 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 143 static 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 152 static 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 165 static 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 187 err_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 198 static 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 210 static struct mlxsw_sp_bridge_device * 211 mlxsw_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 260 err_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 268 static void 269 mlxsw_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 283 static struct mlxsw_sp_bridge_device * 284 mlxsw_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 297 static void 298 mlxsw_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 305 static 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 319 struct mlxsw_sp_bridge_port * 320 mlxsw_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 336 static struct mlxsw_sp_bridge_port * 337 mlxsw_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 365 static void 366 mlxsw_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 373 static struct mlxsw_sp_bridge_port * 374 mlxsw_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 401 err_bridge_port_create: 402 mlxsw_sp_bridge_device_put(bridge, bridge_device); 403 return ERR_PTR(err); 404 } 405 406 static 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 418 static struct mlxsw_sp_port_vlan * 419 mlxsw_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 442 static struct mlxsw_sp_port_vlan* 443 mlxsw_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 459 static struct mlxsw_sp_bridge_vlan * 460 mlxsw_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 473 static struct mlxsw_sp_bridge_vlan * 474 mlxsw_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 489 static void 490 mlxsw_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 497 static struct mlxsw_sp_bridge_vlan * 498 mlxsw_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 509 static 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 515 static int 516 mlxsw_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 533 static 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 560 err_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 568 static int 569 mlxsw_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 589 static int 590 mlxsw_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 609 err_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 617 static int 618 mlxsw_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 635 static int 636 mlxsw_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 652 err_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 660 static int 661 mlxsw_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 670 static 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 711 out: 712 memcpy(&bridge_port->flags, &flags.val, sizeof(flags.val)); 713 return 0; 714 } 715 716 static 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 729 static 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 743 static 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 761 static 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 776 static 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); 799 out: 800 bridge_port->mrouter = is_port_mrouter; 801 return 0; 802 } 803 804 static 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 813 static 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 851 static 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 868 static void 869 mlxsw_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 879 static int 880 mlxsw_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 901 static int mlxsw_sp_port_attr_set(struct net_device *dev, 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 962 static int 963 mlxsw_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 1002 err_fid_port_vid_map: 1003 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false); 1004 err_fid_bc_flood_set: 1005 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false); 1006 err_fid_mc_flood_set: 1007 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false); 1008 err_fid_uc_flood_set: 1009 mlxsw_sp_fid_put(fid); 1010 return err; 1011 } 1012 1013 static void 1014 mlxsw_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 1029 static u16 1030 mlxsw_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 1041 static int 1042 mlxsw_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 1085 err_bridge_vlan_get: 1086 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED); 1087 err_port_vid_stp_set: 1088 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false); 1089 err_port_vid_learning_set: 1090 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan); 1091 return err; 1092 } 1093 1094 void 1095 mlxsw_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 1130 static int 1131 mlxsw_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 1175 err_port_vlan_bridge_join: 1176 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid, proto); 1177 err_port_pvid_set: 1178 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false); 1179 err_port_vlan_set: 1180 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan); 1181 return err; 1182 } 1183 1184 static int 1185 mlxsw_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 1210 static 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 1244 static 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 1250 static int 1251 mlxsw_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 1267 static 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 1273 static 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 1279 static 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 1319 out: 1320 kfree(sfd_pl); 1321 return err; 1322 } 1323 1324 static 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 1347 out: 1348 kfree(sfd_pl); 1349 return err; 1350 } 1351 1352 static 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 1361 int 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 1369 static 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 1393 out: 1394 kfree(sfd_pl); 1395 return err; 1396 } 1397 1398 static int 1399 mlxsw_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 1435 static 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 1457 out: 1458 kfree(sfd_pl); 1459 return err; 1460 } 1461 1462 static 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 1493 static 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 1510 static struct 1511 mlxsw_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 1524 static void 1525 mlxsw_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 1549 static void 1550 mlxsw_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 1565 static bool 1566 mlxsw_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 1607 static 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 1619 static struct 1620 mlxsw_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 1649 out: 1650 list_add_tail(&mid->list, &bridge_device->mids_list); 1651 return mid; 1652 1653 err_write_mdb_entry: 1654 kfree(mid->ports_in_mid); 1655 err_ports_in_mid_alloc: 1656 kfree(mid); 1657 return NULL; 1658 } 1659 1660 static 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 1677 static 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 1728 err_out: 1729 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid); 1730 return err; 1731 } 1732 1733 static void 1734 mlxsw_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 1753 static void 1754 mlxsw_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 1769 static int mlxsw_sp_port_obj_add(struct net_device *dev, 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 1802 static void 1803 mlxsw_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 1821 static 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 1843 static 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 1865 static 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 1899 static void 1900 mlxsw_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 1919 static int mlxsw_sp_port_obj_del(struct net_device *dev, 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 1944 static 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 1961 static int 1962 mlxsw_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 1978 static int 1979 mlxsw_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 1988 static void 1989 mlxsw_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 1996 static void 1997 mlxsw_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 2004 static int 2005 mlxsw_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, ¶ms, extack); 2044 if (err) 2045 goto err_nve_fid_enable; 2046 2047 return 0; 2048 2049 err_nve_fid_enable: 2050 err_vni_exists: 2051 mlxsw_sp_fid_put(fid); 2052 return err; 2053 } 2054 2055 static int 2056 mlxsw_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 2064 static struct net_device * 2065 mlxsw_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 2087 static struct mlxsw_sp_fid * 2088 mlxsw_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 2096 static struct mlxsw_sp_fid * 2097 mlxsw_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 2105 static u16 2106 mlxsw_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 2112 static 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 2121 static bool 2122 mlxsw_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 2138 static int 2139 mlxsw_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 2166 static void 2167 mlxsw_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 2183 static int 2184 mlxsw_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, ¶ms, extack); 2212 if (err) 2213 goto err_nve_fid_enable; 2214 2215 return 0; 2216 2217 err_nve_fid_enable: 2218 err_vni_exists: 2219 mlxsw_sp_fid_put(fid); 2220 return err; 2221 } 2222 2223 static struct mlxsw_sp_fid * 2224 mlxsw_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 2232 static struct mlxsw_sp_fid * 2233 mlxsw_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 2245 static u16 2246 mlxsw_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 2252 static 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 2261 static int 2262 mlxsw_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 2280 err_bridge_vlan_aware_port_join: 2281 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true); 2282 return err; 2283 } 2284 2285 static void 2286 mlxsw_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 2294 static int 2295 mlxsw_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 2303 static 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 2312 static int 2313 mlxsw_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 2335 err_bridge_8021ad_port_join: 2336 mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q); 2337 return err; 2338 } 2339 2340 static void 2341 mlxsw_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 2350 static 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 2359 int 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 2382 err_port_join: 2383 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port); 2384 return err; 2385 } 2386 2387 void 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 2407 int 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 2422 void 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 2441 static void 2442 mlxsw_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 2455 static void 2456 mlxsw_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 2472 static 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 2494 static 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 2506 static void 2507 mlxsw_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 2519 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp, 2520 char *sfn_pl, int rec_index, 2521 bool adding) 2522 { 2523 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; 2524 struct mlxsw_sp_bridge_device *bridge_device; 2525 struct mlxsw_sp_bridge_port *bridge_port; 2526 struct mlxsw_sp_port *mlxsw_sp_port; 2527 enum switchdev_notifier_type type; 2528 char mac[ETH_ALEN]; 2529 u8 local_port; 2530 u16 vid, fid; 2531 bool do_notification = true; 2532 int err; 2533 2534 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port); 2535 mlxsw_sp_port = mlxsw_sp->ports[local_port]; 2536 if (!mlxsw_sp_port) { 2537 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n"); 2538 goto just_remove; 2539 } 2540 2541 if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid)) 2542 goto just_remove; 2543 2544 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid); 2545 if (!mlxsw_sp_port_vlan) { 2546 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n"); 2547 goto just_remove; 2548 } 2549 2550 bridge_port = mlxsw_sp_port_vlan->bridge_port; 2551 if (!bridge_port) { 2552 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n"); 2553 goto just_remove; 2554 } 2555 2556 bridge_device = bridge_port->bridge_device; 2557 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0; 2558 2559 do_fdb_op: 2560 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, 2561 adding, true); 2562 if (err) { 2563 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n"); 2564 return; 2565 } 2566 2567 if (!do_notification) 2568 return; 2569 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE; 2570 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding); 2571 2572 return; 2573 2574 just_remove: 2575 adding = false; 2576 do_notification = false; 2577 goto do_fdb_op; 2578 } 2579 2580 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp, 2581 char *sfn_pl, int rec_index, 2582 bool adding) 2583 { 2584 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan; 2585 struct mlxsw_sp_bridge_device *bridge_device; 2586 struct mlxsw_sp_bridge_port *bridge_port; 2587 struct mlxsw_sp_port *mlxsw_sp_port; 2588 enum switchdev_notifier_type type; 2589 char mac[ETH_ALEN]; 2590 u16 lag_vid = 0; 2591 u16 lag_id; 2592 u16 vid, fid; 2593 bool do_notification = true; 2594 int err; 2595 2596 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id); 2597 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id); 2598 if (!mlxsw_sp_port) { 2599 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n"); 2600 goto just_remove; 2601 } 2602 2603 if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid)) 2604 goto just_remove; 2605 2606 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid); 2607 if (!mlxsw_sp_port_vlan) { 2608 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n"); 2609 goto just_remove; 2610 } 2611 2612 bridge_port = mlxsw_sp_port_vlan->bridge_port; 2613 if (!bridge_port) { 2614 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n"); 2615 goto just_remove; 2616 } 2617 2618 bridge_device = bridge_port->bridge_device; 2619 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0; 2620 lag_vid = mlxsw_sp_fid_lag_vid_valid(mlxsw_sp_port_vlan->fid) ? 2621 mlxsw_sp_port_vlan->vid : 0; 2622 2623 do_fdb_op: 2624 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid, 2625 adding, true); 2626 if (err) { 2627 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n"); 2628 return; 2629 } 2630 2631 if (!do_notification) 2632 return; 2633 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE; 2634 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding); 2635 2636 return; 2637 2638 just_remove: 2639 adding = false; 2640 do_notification = false; 2641 goto do_fdb_op; 2642 } 2643 2644 static int 2645 __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp, 2646 const struct mlxsw_sp_fid *fid, 2647 bool adding, 2648 struct net_device **nve_dev, 2649 u16 *p_vid, __be32 *p_vni) 2650 { 2651 struct mlxsw_sp_bridge_device *bridge_device; 2652 struct net_device *br_dev, *dev; 2653 int nve_ifindex; 2654 int err; 2655 2656 err = mlxsw_sp_fid_nve_ifindex(fid, &nve_ifindex); 2657 if (err) 2658 return err; 2659 2660 err = mlxsw_sp_fid_vni(fid, p_vni); 2661 if (err) 2662 return err; 2663 2664 dev = __dev_get_by_index(mlxsw_sp_net(mlxsw_sp), nve_ifindex); 2665 if (!dev) 2666 return -EINVAL; 2667 *nve_dev = dev; 2668 2669 if (!netif_running(dev)) 2670 return -EINVAL; 2671 2672 if (adding && !br_port_flag_is_set(dev, BR_LEARNING)) 2673 return -EINVAL; 2674 2675 if (adding && netif_is_vxlan(dev)) { 2676 struct vxlan_dev *vxlan = netdev_priv(dev); 2677 2678 if (!(vxlan->cfg.flags & VXLAN_F_LEARN)) 2679 return -EINVAL; 2680 } 2681 2682 br_dev = netdev_master_upper_dev_get(dev); 2683 if (!br_dev) 2684 return -EINVAL; 2685 2686 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev); 2687 if (!bridge_device) 2688 return -EINVAL; 2689 2690 *p_vid = bridge_device->ops->fid_vid(bridge_device, fid); 2691 2692 return 0; 2693 } 2694 2695 static void mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp, 2696 char *sfn_pl, 2697 int rec_index, 2698 bool adding) 2699 { 2700 enum mlxsw_reg_sfn_uc_tunnel_protocol sfn_proto; 2701 enum switchdev_notifier_type type; 2702 struct net_device *nve_dev; 2703 union mlxsw_sp_l3addr addr; 2704 struct mlxsw_sp_fid *fid; 2705 char mac[ETH_ALEN]; 2706 u16 fid_index, vid; 2707 __be32 vni; 2708 u32 uip; 2709 int err; 2710 2711 mlxsw_reg_sfn_uc_tunnel_unpack(sfn_pl, rec_index, mac, &fid_index, 2712 &uip, &sfn_proto); 2713 2714 fid = mlxsw_sp_fid_lookup_by_index(mlxsw_sp, fid_index); 2715 if (!fid) 2716 goto err_fid_lookup; 2717 2718 err = mlxsw_sp_nve_learned_ip_resolve(mlxsw_sp, uip, 2719 (enum mlxsw_sp_l3proto) sfn_proto, 2720 &addr); 2721 if (err) 2722 goto err_ip_resolve; 2723 2724 err = __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, fid, adding, 2725 &nve_dev, &vid, &vni); 2726 if (err) 2727 goto err_fdb_process; 2728 2729 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index, 2730 (enum mlxsw_sp_l3proto) sfn_proto, 2731 &addr, adding, true); 2732 if (err) 2733 goto err_fdb_op; 2734 2735 mlxsw_sp_fdb_nve_call_notifiers(nve_dev, mac, 2736 (enum mlxsw_sp_l3proto) sfn_proto, 2737 &addr, vni, adding); 2738 2739 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : 2740 SWITCHDEV_FDB_DEL_TO_BRIDGE; 2741 mlxsw_sp_fdb_call_notifiers(type, mac, vid, nve_dev, adding); 2742 2743 mlxsw_sp_fid_put(fid); 2744 2745 return; 2746 2747 err_fdb_op: 2748 err_fdb_process: 2749 err_ip_resolve: 2750 mlxsw_sp_fid_put(fid); 2751 err_fid_lookup: 2752 /* Remove an FDB entry in case we cannot process it. Otherwise the 2753 * device will keep sending the same notification over and over again. 2754 */ 2755 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index, 2756 (enum mlxsw_sp_l3proto) sfn_proto, &addr, 2757 false, true); 2758 } 2759 2760 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp, 2761 char *sfn_pl, int rec_index) 2762 { 2763 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) { 2764 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC: 2765 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl, 2766 rec_index, true); 2767 break; 2768 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC: 2769 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl, 2770 rec_index, false); 2771 break; 2772 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG: 2773 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl, 2774 rec_index, true); 2775 break; 2776 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG: 2777 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl, 2778 rec_index, false); 2779 break; 2780 case MLXSW_REG_SFN_REC_TYPE_LEARNED_UNICAST_TUNNEL: 2781 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl, 2782 rec_index, true); 2783 break; 2784 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_UNICAST_TUNNEL: 2785 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl, 2786 rec_index, false); 2787 break; 2788 } 2789 } 2790 2791 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp, 2792 bool no_delay) 2793 { 2794 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge; 2795 unsigned int interval = no_delay ? 0 : bridge->fdb_notify.interval; 2796 2797 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw, 2798 msecs_to_jiffies(interval)); 2799 } 2800 2801 #define MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION 10 2802 2803 static void mlxsw_sp_fdb_notify_work(struct work_struct *work) 2804 { 2805 struct mlxsw_sp_bridge *bridge; 2806 struct mlxsw_sp *mlxsw_sp; 2807 char *sfn_pl; 2808 int queries; 2809 u8 num_rec; 2810 int i; 2811 int err; 2812 2813 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL); 2814 if (!sfn_pl) 2815 return; 2816 2817 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work); 2818 mlxsw_sp = bridge->mlxsw_sp; 2819 2820 rtnl_lock(); 2821 queries = MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION; 2822 while (queries > 0) { 2823 mlxsw_reg_sfn_pack(sfn_pl); 2824 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl); 2825 if (err) { 2826 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n"); 2827 goto out; 2828 } 2829 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl); 2830 for (i = 0; i < num_rec; i++) 2831 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i); 2832 if (num_rec != MLXSW_REG_SFN_REC_MAX_COUNT) 2833 goto out; 2834 queries--; 2835 } 2836 2837 out: 2838 rtnl_unlock(); 2839 kfree(sfn_pl); 2840 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, !queries); 2841 } 2842 2843 struct mlxsw_sp_switchdev_event_work { 2844 struct work_struct work; 2845 union { 2846 struct switchdev_notifier_fdb_info fdb_info; 2847 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info; 2848 }; 2849 struct net_device *dev; 2850 unsigned long event; 2851 }; 2852 2853 static void 2854 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp, 2855 struct mlxsw_sp_switchdev_event_work * 2856 switchdev_work, 2857 struct mlxsw_sp_fid *fid, __be32 vni) 2858 { 2859 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info; 2860 struct switchdev_notifier_fdb_info *fdb_info; 2861 struct net_device *dev = switchdev_work->dev; 2862 enum mlxsw_sp_l3proto proto; 2863 union mlxsw_sp_l3addr addr; 2864 int err; 2865 2866 fdb_info = &switchdev_work->fdb_info; 2867 err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info); 2868 if (err) 2869 return; 2870 2871 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip, 2872 &proto, &addr); 2873 2874 switch (switchdev_work->event) { 2875 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2876 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, 2877 vxlan_fdb_info.eth_addr, 2878 mlxsw_sp_fid_index(fid), 2879 proto, &addr, true, false); 2880 if (err) 2881 return; 2882 vxlan_fdb_info.offloaded = true; 2883 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev, 2884 &vxlan_fdb_info.info, NULL); 2885 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, 2886 vxlan_fdb_info.eth_addr, 2887 fdb_info->vid, dev, true); 2888 break; 2889 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2890 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, 2891 vxlan_fdb_info.eth_addr, 2892 mlxsw_sp_fid_index(fid), 2893 proto, &addr, false, 2894 false); 2895 vxlan_fdb_info.offloaded = false; 2896 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev, 2897 &vxlan_fdb_info.info, NULL); 2898 break; 2899 } 2900 } 2901 2902 static void 2903 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work * 2904 switchdev_work) 2905 { 2906 struct mlxsw_sp_bridge_device *bridge_device; 2907 struct net_device *dev = switchdev_work->dev; 2908 struct net_device *br_dev; 2909 struct mlxsw_sp *mlxsw_sp; 2910 struct mlxsw_sp_fid *fid; 2911 __be32 vni; 2912 int err; 2913 2914 if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE && 2915 switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE) 2916 return; 2917 2918 if (switchdev_work->event == SWITCHDEV_FDB_ADD_TO_DEVICE && 2919 (!switchdev_work->fdb_info.added_by_user || 2920 switchdev_work->fdb_info.is_local)) 2921 return; 2922 2923 if (!netif_running(dev)) 2924 return; 2925 br_dev = netdev_master_upper_dev_get(dev); 2926 if (!br_dev) 2927 return; 2928 if (!netif_is_bridge_master(br_dev)) 2929 return; 2930 mlxsw_sp = mlxsw_sp_lower_get(br_dev); 2931 if (!mlxsw_sp) 2932 return; 2933 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev); 2934 if (!bridge_device) 2935 return; 2936 2937 fid = bridge_device->ops->fid_lookup(bridge_device, 2938 switchdev_work->fdb_info.vid); 2939 if (!fid) 2940 return; 2941 2942 err = mlxsw_sp_fid_vni(fid, &vni); 2943 if (err) 2944 goto out; 2945 2946 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid, 2947 vni); 2948 2949 out: 2950 mlxsw_sp_fid_put(fid); 2951 } 2952 2953 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work) 2954 { 2955 struct mlxsw_sp_switchdev_event_work *switchdev_work = 2956 container_of(work, struct mlxsw_sp_switchdev_event_work, work); 2957 struct net_device *dev = switchdev_work->dev; 2958 struct switchdev_notifier_fdb_info *fdb_info; 2959 struct mlxsw_sp_port *mlxsw_sp_port; 2960 int err; 2961 2962 rtnl_lock(); 2963 if (netif_is_vxlan(dev)) { 2964 mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work); 2965 goto out; 2966 } 2967 2968 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev); 2969 if (!mlxsw_sp_port) 2970 goto out; 2971 2972 switch (switchdev_work->event) { 2973 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2974 fdb_info = &switchdev_work->fdb_info; 2975 if (!fdb_info->added_by_user || fdb_info->is_local) 2976 break; 2977 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true); 2978 if (err) 2979 break; 2980 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, 2981 fdb_info->addr, 2982 fdb_info->vid, dev, true); 2983 break; 2984 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2985 fdb_info = &switchdev_work->fdb_info; 2986 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false); 2987 break; 2988 case SWITCHDEV_FDB_ADD_TO_BRIDGE: 2989 case SWITCHDEV_FDB_DEL_TO_BRIDGE: 2990 /* These events are only used to potentially update an existing 2991 * SPAN mirror. 2992 */ 2993 break; 2994 } 2995 2996 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp); 2997 2998 out: 2999 rtnl_unlock(); 3000 kfree(switchdev_work->fdb_info.addr); 3001 kfree(switchdev_work); 3002 dev_put(dev); 3003 } 3004 3005 static void 3006 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp, 3007 struct mlxsw_sp_switchdev_event_work * 3008 switchdev_work) 3009 { 3010 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info; 3011 struct mlxsw_sp_bridge_device *bridge_device; 3012 struct net_device *dev = switchdev_work->dev; 3013 u8 all_zeros_mac[ETH_ALEN] = { 0 }; 3014 enum mlxsw_sp_l3proto proto; 3015 union mlxsw_sp_l3addr addr; 3016 struct net_device *br_dev; 3017 struct mlxsw_sp_fid *fid; 3018 u16 vid; 3019 int err; 3020 3021 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info; 3022 br_dev = netdev_master_upper_dev_get(dev); 3023 3024 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev); 3025 if (!bridge_device) 3026 return; 3027 3028 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni); 3029 if (!fid) 3030 return; 3031 3032 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip, 3033 &proto, &addr); 3034 3035 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) { 3036 err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr); 3037 if (err) { 3038 mlxsw_sp_fid_put(fid); 3039 return; 3040 } 3041 vxlan_fdb_info->offloaded = true; 3042 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev, 3043 &vxlan_fdb_info->info, NULL); 3044 mlxsw_sp_fid_put(fid); 3045 return; 3046 } 3047 3048 /* The device has a single FDB table, whereas Linux has two - one 3049 * in the bridge driver and another in the VxLAN driver. We only 3050 * program an entry to the device if the MAC points to the VxLAN 3051 * device in the bridge's FDB table 3052 */ 3053 vid = bridge_device->ops->fid_vid(bridge_device, fid); 3054 if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev) 3055 goto err_br_fdb_find; 3056 3057 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr, 3058 mlxsw_sp_fid_index(fid), proto, 3059 &addr, true, false); 3060 if (err) 3061 goto err_fdb_tunnel_uc_op; 3062 vxlan_fdb_info->offloaded = true; 3063 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev, 3064 &vxlan_fdb_info->info, NULL); 3065 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, 3066 vxlan_fdb_info->eth_addr, vid, dev, true); 3067 3068 mlxsw_sp_fid_put(fid); 3069 3070 return; 3071 3072 err_fdb_tunnel_uc_op: 3073 err_br_fdb_find: 3074 mlxsw_sp_fid_put(fid); 3075 } 3076 3077 static void 3078 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp, 3079 struct mlxsw_sp_switchdev_event_work * 3080 switchdev_work) 3081 { 3082 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info; 3083 struct mlxsw_sp_bridge_device *bridge_device; 3084 struct net_device *dev = switchdev_work->dev; 3085 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3086 u8 all_zeros_mac[ETH_ALEN] = { 0 }; 3087 enum mlxsw_sp_l3proto proto; 3088 union mlxsw_sp_l3addr addr; 3089 struct mlxsw_sp_fid *fid; 3090 u16 vid; 3091 3092 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info; 3093 3094 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev); 3095 if (!bridge_device) 3096 return; 3097 3098 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni); 3099 if (!fid) 3100 return; 3101 3102 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip, 3103 &proto, &addr); 3104 3105 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) { 3106 mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr); 3107 mlxsw_sp_fid_put(fid); 3108 return; 3109 } 3110 3111 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr, 3112 mlxsw_sp_fid_index(fid), proto, &addr, 3113 false, false); 3114 vid = bridge_device->ops->fid_vid(bridge_device, fid); 3115 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, 3116 vxlan_fdb_info->eth_addr, vid, dev, false); 3117 3118 mlxsw_sp_fid_put(fid); 3119 } 3120 3121 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work) 3122 { 3123 struct mlxsw_sp_switchdev_event_work *switchdev_work = 3124 container_of(work, struct mlxsw_sp_switchdev_event_work, work); 3125 struct net_device *dev = switchdev_work->dev; 3126 struct mlxsw_sp *mlxsw_sp; 3127 struct net_device *br_dev; 3128 3129 rtnl_lock(); 3130 3131 if (!netif_running(dev)) 3132 goto out; 3133 br_dev = netdev_master_upper_dev_get(dev); 3134 if (!br_dev) 3135 goto out; 3136 if (!netif_is_bridge_master(br_dev)) 3137 goto out; 3138 mlxsw_sp = mlxsw_sp_lower_get(br_dev); 3139 if (!mlxsw_sp) 3140 goto out; 3141 3142 switch (switchdev_work->event) { 3143 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE: 3144 mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work); 3145 break; 3146 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE: 3147 mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work); 3148 break; 3149 } 3150 3151 out: 3152 rtnl_unlock(); 3153 kfree(switchdev_work); 3154 dev_put(dev); 3155 } 3156 3157 static int 3158 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work * 3159 switchdev_work, 3160 struct switchdev_notifier_info *info) 3161 { 3162 struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev); 3163 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info; 3164 struct vxlan_config *cfg = &vxlan->cfg; 3165 struct netlink_ext_ack *extack; 3166 3167 extack = switchdev_notifier_info_to_extack(info); 3168 vxlan_fdb_info = container_of(info, 3169 struct switchdev_notifier_vxlan_fdb_info, 3170 info); 3171 3172 if (vxlan_fdb_info->remote_port != cfg->dst_port) { 3173 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default remote port is not supported"); 3174 return -EOPNOTSUPP; 3175 } 3176 if (vxlan_fdb_info->remote_vni != cfg->vni || 3177 vxlan_fdb_info->vni != cfg->vni) { 3178 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default VNI is not supported"); 3179 return -EOPNOTSUPP; 3180 } 3181 if (vxlan_fdb_info->remote_ifindex) { 3182 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Local interface is not supported"); 3183 return -EOPNOTSUPP; 3184 } 3185 if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr)) { 3186 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast MAC addresses not supported"); 3187 return -EOPNOTSUPP; 3188 } 3189 if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip)) { 3190 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast destination IP is not supported"); 3191 return -EOPNOTSUPP; 3192 } 3193 3194 switchdev_work->vxlan_fdb_info = *vxlan_fdb_info; 3195 3196 return 0; 3197 } 3198 3199 /* Called under rcu_read_lock() */ 3200 static int mlxsw_sp_switchdev_event(struct notifier_block *unused, 3201 unsigned long event, void *ptr) 3202 { 3203 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 3204 struct mlxsw_sp_switchdev_event_work *switchdev_work; 3205 struct switchdev_notifier_fdb_info *fdb_info; 3206 struct switchdev_notifier_info *info = ptr; 3207 struct net_device *br_dev; 3208 int err; 3209 3210 if (event == SWITCHDEV_PORT_ATTR_SET) { 3211 err = switchdev_handle_port_attr_set(dev, ptr, 3212 mlxsw_sp_port_dev_check, 3213 mlxsw_sp_port_attr_set); 3214 return notifier_from_errno(err); 3215 } 3216 3217 /* Tunnel devices are not our uppers, so check their master instead */ 3218 br_dev = netdev_master_upper_dev_get_rcu(dev); 3219 if (!br_dev) 3220 return NOTIFY_DONE; 3221 if (!netif_is_bridge_master(br_dev)) 3222 return NOTIFY_DONE; 3223 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev)) 3224 return NOTIFY_DONE; 3225 3226 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 3227 if (!switchdev_work) 3228 return NOTIFY_BAD; 3229 3230 switchdev_work->dev = dev; 3231 switchdev_work->event = event; 3232 3233 switch (event) { 3234 case SWITCHDEV_FDB_ADD_TO_DEVICE: 3235 case SWITCHDEV_FDB_DEL_TO_DEVICE: 3236 case SWITCHDEV_FDB_ADD_TO_BRIDGE: 3237 case SWITCHDEV_FDB_DEL_TO_BRIDGE: 3238 fdb_info = container_of(info, 3239 struct switchdev_notifier_fdb_info, 3240 info); 3241 INIT_WORK(&switchdev_work->work, 3242 mlxsw_sp_switchdev_bridge_fdb_event_work); 3243 memcpy(&switchdev_work->fdb_info, ptr, 3244 sizeof(switchdev_work->fdb_info)); 3245 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 3246 if (!switchdev_work->fdb_info.addr) 3247 goto err_addr_alloc; 3248 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 3249 fdb_info->addr); 3250 /* Take a reference on the device. This can be either 3251 * upper device containig mlxsw_sp_port or just a 3252 * mlxsw_sp_port 3253 */ 3254 dev_hold(dev); 3255 break; 3256 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE: 3257 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE: 3258 INIT_WORK(&switchdev_work->work, 3259 mlxsw_sp_switchdev_vxlan_fdb_event_work); 3260 err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work, 3261 info); 3262 if (err) 3263 goto err_vxlan_work_prepare; 3264 dev_hold(dev); 3265 break; 3266 default: 3267 kfree(switchdev_work); 3268 return NOTIFY_DONE; 3269 } 3270 3271 mlxsw_core_schedule_work(&switchdev_work->work); 3272 3273 return NOTIFY_DONE; 3274 3275 err_vxlan_work_prepare: 3276 err_addr_alloc: 3277 kfree(switchdev_work); 3278 return NOTIFY_BAD; 3279 } 3280 3281 struct notifier_block mlxsw_sp_switchdev_notifier = { 3282 .notifier_call = mlxsw_sp_switchdev_event, 3283 }; 3284 3285 static int 3286 mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp *mlxsw_sp, 3287 struct mlxsw_sp_bridge_device *bridge_device, 3288 const struct net_device *vxlan_dev, u16 vid, 3289 bool flag_untagged, bool flag_pvid, 3290 struct netlink_ext_ack *extack) 3291 { 3292 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev); 3293 __be32 vni = vxlan->cfg.vni; 3294 struct mlxsw_sp_fid *fid; 3295 u16 old_vid; 3296 int err; 3297 3298 /* We cannot have the same VLAN as PVID and egress untagged on multiple 3299 * VxLAN devices. Note that we get this notification before the VLAN is 3300 * actually added to the bridge's database, so it is not possible for 3301 * the lookup function to return 'vxlan_dev' 3302 */ 3303 if (flag_untagged && flag_pvid && 3304 mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device->dev, vid)) { 3305 NL_SET_ERR_MSG_MOD(extack, "VLAN already mapped to a different VNI"); 3306 return -EINVAL; 3307 } 3308 3309 if (!netif_running(vxlan_dev)) 3310 return 0; 3311 3312 /* First case: FID is not associated with this VNI, but the new VLAN 3313 * is both PVID and egress untagged. Need to enable NVE on the FID, if 3314 * it exists 3315 */ 3316 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni); 3317 if (!fid) { 3318 if (!flag_untagged || !flag_pvid) 3319 return 0; 3320 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, 3321 vid, extack); 3322 } 3323 3324 /* Second case: FID is associated with the VNI and the VLAN associated 3325 * with the FID is the same as the notified VLAN. This means the flags 3326 * (PVID / egress untagged) were toggled and that NVE should be 3327 * disabled on the FID 3328 */ 3329 old_vid = mlxsw_sp_fid_8021q_vid(fid); 3330 if (vid == old_vid) { 3331 if (WARN_ON(flag_untagged && flag_pvid)) { 3332 mlxsw_sp_fid_put(fid); 3333 return -EINVAL; 3334 } 3335 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev); 3336 mlxsw_sp_fid_put(fid); 3337 return 0; 3338 } 3339 3340 /* Third case: A new VLAN was configured on the VxLAN device, but this 3341 * VLAN is not PVID, so there is nothing to do. 3342 */ 3343 if (!flag_pvid) { 3344 mlxsw_sp_fid_put(fid); 3345 return 0; 3346 } 3347 3348 /* Fourth case: Thew new VLAN is PVID, which means the VLAN currently 3349 * mapped to the VNI should be unmapped 3350 */ 3351 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev); 3352 mlxsw_sp_fid_put(fid); 3353 3354 /* Fifth case: The new VLAN is also egress untagged, which means the 3355 * VLAN needs to be mapped to the VNI 3356 */ 3357 if (!flag_untagged) 3358 return 0; 3359 3360 err = bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid, extack); 3361 if (err) 3362 goto err_vxlan_join; 3363 3364 return 0; 3365 3366 err_vxlan_join: 3367 bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, old_vid, NULL); 3368 return err; 3369 } 3370 3371 static void 3372 mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp *mlxsw_sp, 3373 struct mlxsw_sp_bridge_device *bridge_device, 3374 const struct net_device *vxlan_dev, u16 vid) 3375 { 3376 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev); 3377 __be32 vni = vxlan->cfg.vni; 3378 struct mlxsw_sp_fid *fid; 3379 3380 if (!netif_running(vxlan_dev)) 3381 return; 3382 3383 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni); 3384 if (!fid) 3385 return; 3386 3387 /* A different VLAN than the one mapped to the VNI is deleted */ 3388 if (mlxsw_sp_fid_8021q_vid(fid) != vid) 3389 goto out; 3390 3391 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev); 3392 3393 out: 3394 mlxsw_sp_fid_put(fid); 3395 } 3396 3397 static int 3398 mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device *vxlan_dev, 3399 struct switchdev_notifier_port_obj_info * 3400 port_obj_info) 3401 { 3402 struct switchdev_obj_port_vlan *vlan = 3403 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj); 3404 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 3405 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; 3406 struct mlxsw_sp_bridge_device *bridge_device; 3407 struct netlink_ext_ack *extack; 3408 struct mlxsw_sp *mlxsw_sp; 3409 struct net_device *br_dev; 3410 3411 extack = switchdev_notifier_info_to_extack(&port_obj_info->info); 3412 br_dev = netdev_master_upper_dev_get(vxlan_dev); 3413 if (!br_dev) 3414 return 0; 3415 3416 mlxsw_sp = mlxsw_sp_lower_get(br_dev); 3417 if (!mlxsw_sp) 3418 return 0; 3419 3420 port_obj_info->handled = true; 3421 3422 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev); 3423 if (!bridge_device) 3424 return -EINVAL; 3425 3426 if (!bridge_device->vlan_enabled) 3427 return 0; 3428 3429 return mlxsw_sp_switchdev_vxlan_vlan_add(mlxsw_sp, bridge_device, 3430 vxlan_dev, vlan->vid, 3431 flag_untagged, 3432 flag_pvid, extack); 3433 } 3434 3435 static void 3436 mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device *vxlan_dev, 3437 struct switchdev_notifier_port_obj_info * 3438 port_obj_info) 3439 { 3440 struct switchdev_obj_port_vlan *vlan = 3441 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj); 3442 struct mlxsw_sp_bridge_device *bridge_device; 3443 struct mlxsw_sp *mlxsw_sp; 3444 struct net_device *br_dev; 3445 3446 br_dev = netdev_master_upper_dev_get(vxlan_dev); 3447 if (!br_dev) 3448 return; 3449 3450 mlxsw_sp = mlxsw_sp_lower_get(br_dev); 3451 if (!mlxsw_sp) 3452 return; 3453 3454 port_obj_info->handled = true; 3455 3456 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev); 3457 if (!bridge_device) 3458 return; 3459 3460 if (!bridge_device->vlan_enabled) 3461 return; 3462 3463 mlxsw_sp_switchdev_vxlan_vlan_del(mlxsw_sp, bridge_device, vxlan_dev, 3464 vlan->vid); 3465 } 3466 3467 static int 3468 mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device *vxlan_dev, 3469 struct switchdev_notifier_port_obj_info * 3470 port_obj_info) 3471 { 3472 int err = 0; 3473 3474 switch (port_obj_info->obj->id) { 3475 case SWITCHDEV_OBJ_ID_PORT_VLAN: 3476 err = mlxsw_sp_switchdev_vxlan_vlans_add(vxlan_dev, 3477 port_obj_info); 3478 break; 3479 default: 3480 break; 3481 } 3482 3483 return err; 3484 } 3485 3486 static void 3487 mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device *vxlan_dev, 3488 struct switchdev_notifier_port_obj_info * 3489 port_obj_info) 3490 { 3491 switch (port_obj_info->obj->id) { 3492 case SWITCHDEV_OBJ_ID_PORT_VLAN: 3493 mlxsw_sp_switchdev_vxlan_vlans_del(vxlan_dev, port_obj_info); 3494 break; 3495 default: 3496 break; 3497 } 3498 } 3499 3500 static int mlxsw_sp_switchdev_blocking_event(struct notifier_block *unused, 3501 unsigned long event, void *ptr) 3502 { 3503 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 3504 int err = 0; 3505 3506 switch (event) { 3507 case SWITCHDEV_PORT_OBJ_ADD: 3508 if (netif_is_vxlan(dev)) 3509 err = mlxsw_sp_switchdev_handle_vxlan_obj_add(dev, ptr); 3510 else 3511 err = switchdev_handle_port_obj_add(dev, ptr, 3512 mlxsw_sp_port_dev_check, 3513 mlxsw_sp_port_obj_add); 3514 return notifier_from_errno(err); 3515 case SWITCHDEV_PORT_OBJ_DEL: 3516 if (netif_is_vxlan(dev)) 3517 mlxsw_sp_switchdev_handle_vxlan_obj_del(dev, ptr); 3518 else 3519 err = switchdev_handle_port_obj_del(dev, ptr, 3520 mlxsw_sp_port_dev_check, 3521 mlxsw_sp_port_obj_del); 3522 return notifier_from_errno(err); 3523 case SWITCHDEV_PORT_ATTR_SET: 3524 err = switchdev_handle_port_attr_set(dev, ptr, 3525 mlxsw_sp_port_dev_check, 3526 mlxsw_sp_port_attr_set); 3527 return notifier_from_errno(err); 3528 } 3529 3530 return NOTIFY_DONE; 3531 } 3532 3533 static struct notifier_block mlxsw_sp_switchdev_blocking_notifier = { 3534 .notifier_call = mlxsw_sp_switchdev_blocking_event, 3535 }; 3536 3537 u8 3538 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port) 3539 { 3540 return bridge_port->stp_state; 3541 } 3542 3543 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp) 3544 { 3545 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge; 3546 struct notifier_block *nb; 3547 int err; 3548 3549 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME); 3550 if (err) { 3551 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n"); 3552 return err; 3553 } 3554 3555 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier); 3556 if (err) { 3557 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n"); 3558 return err; 3559 } 3560 3561 nb = &mlxsw_sp_switchdev_blocking_notifier; 3562 err = register_switchdev_blocking_notifier(nb); 3563 if (err) { 3564 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev blocking notifier\n"); 3565 goto err_register_switchdev_blocking_notifier; 3566 } 3567 3568 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work); 3569 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL; 3570 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, false); 3571 return 0; 3572 3573 err_register_switchdev_blocking_notifier: 3574 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier); 3575 return err; 3576 } 3577 3578 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp) 3579 { 3580 struct notifier_block *nb; 3581 3582 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw); 3583 3584 nb = &mlxsw_sp_switchdev_blocking_notifier; 3585 unregister_switchdev_blocking_notifier(nb); 3586 3587 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier); 3588 } 3589 3590 static void mlxsw_sp1_switchdev_init(struct mlxsw_sp *mlxsw_sp) 3591 { 3592 mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp1_bridge_8021ad_ops; 3593 } 3594 3595 const struct mlxsw_sp_switchdev_ops mlxsw_sp1_switchdev_ops = { 3596 .init = mlxsw_sp1_switchdev_init, 3597 }; 3598 3599 static void mlxsw_sp2_switchdev_init(struct mlxsw_sp *mlxsw_sp) 3600 { 3601 mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp2_bridge_8021ad_ops; 3602 } 3603 3604 const struct mlxsw_sp_switchdev_ops mlxsw_sp2_switchdev_ops = { 3605 .init = mlxsw_sp2_switchdev_init, 3606 }; 3607 3608 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp) 3609 { 3610 struct mlxsw_sp_bridge *bridge; 3611 3612 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL); 3613 if (!bridge) 3614 return -ENOMEM; 3615 mlxsw_sp->bridge = bridge; 3616 bridge->mlxsw_sp = mlxsw_sp; 3617 3618 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list); 3619 3620 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops; 3621 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops; 3622 3623 mlxsw_sp->switchdev_ops->init(mlxsw_sp); 3624 3625 return mlxsw_sp_fdb_init(mlxsw_sp); 3626 } 3627 3628 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp) 3629 { 3630 mlxsw_sp_fdb_fini(mlxsw_sp); 3631 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list)); 3632 kfree(mlxsw_sp->bridge); 3633 } 3634 3635