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