1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */ 3 4 #include <linux/etherdevice.h> 5 #include <linux/jiffies.h> 6 #include <linux/list.h> 7 #include <linux/module.h> 8 #include <linux/netdev_features.h> 9 #include <linux/of.h> 10 #include <linux/of_net.h> 11 #include <linux/if_vlan.h> 12 13 #include "prestera.h" 14 #include "prestera_hw.h" 15 #include "prestera_acl.h" 16 #include "prestera_flow.h" 17 #include "prestera_span.h" 18 #include "prestera_rxtx.h" 19 #include "prestera_devlink.h" 20 #include "prestera_ethtool.h" 21 #include "prestera_counter.h" 22 #include "prestera_switchdev.h" 23 24 #define PRESTERA_MTU_DEFAULT 1536 25 26 #define PRESTERA_STATS_DELAY_MS 1000 27 28 #define PRESTERA_MAC_ADDR_NUM_MAX 255 29 30 static struct workqueue_struct *prestera_wq; 31 static struct workqueue_struct *prestera_owq; 32 33 void prestera_queue_work(struct work_struct *work) 34 { 35 queue_work(prestera_owq, work); 36 } 37 38 int prestera_port_learning_set(struct prestera_port *port, bool learn) 39 { 40 return prestera_hw_port_learning_set(port, learn); 41 } 42 43 int prestera_port_uc_flood_set(struct prestera_port *port, bool flood) 44 { 45 return prestera_hw_port_uc_flood_set(port, flood); 46 } 47 48 int prestera_port_mc_flood_set(struct prestera_port *port, bool flood) 49 { 50 return prestera_hw_port_mc_flood_set(port, flood); 51 } 52 53 int prestera_port_pvid_set(struct prestera_port *port, u16 vid) 54 { 55 enum prestera_accept_frm_type frm_type; 56 int err; 57 58 frm_type = PRESTERA_ACCEPT_FRAME_TYPE_TAGGED; 59 60 if (vid) { 61 err = prestera_hw_vlan_port_vid_set(port, vid); 62 if (err) 63 return err; 64 65 frm_type = PRESTERA_ACCEPT_FRAME_TYPE_ALL; 66 } 67 68 err = prestera_hw_port_accept_frm_type(port, frm_type); 69 if (err && frm_type == PRESTERA_ACCEPT_FRAME_TYPE_ALL) 70 prestera_hw_vlan_port_vid_set(port, port->pvid); 71 72 port->pvid = vid; 73 return 0; 74 } 75 76 struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw, 77 u32 dev_id, u32 hw_id) 78 { 79 struct prestera_port *port = NULL, *tmp; 80 81 read_lock(&sw->port_list_lock); 82 list_for_each_entry(tmp, &sw->port_list, list) { 83 if (tmp->dev_id == dev_id && tmp->hw_id == hw_id) { 84 port = tmp; 85 break; 86 } 87 } 88 read_unlock(&sw->port_list_lock); 89 90 return port; 91 } 92 93 struct prestera_port *prestera_find_port(struct prestera_switch *sw, u32 id) 94 { 95 struct prestera_port *port = NULL, *tmp; 96 97 read_lock(&sw->port_list_lock); 98 list_for_each_entry(tmp, &sw->port_list, list) { 99 if (tmp->id == id) { 100 port = tmp; 101 break; 102 } 103 } 104 read_unlock(&sw->port_list_lock); 105 106 return port; 107 } 108 109 int prestera_port_cfg_mac_read(struct prestera_port *port, 110 struct prestera_port_mac_config *cfg) 111 { 112 *cfg = port->cfg_mac; 113 return 0; 114 } 115 116 int prestera_port_cfg_mac_write(struct prestera_port *port, 117 struct prestera_port_mac_config *cfg) 118 { 119 int err; 120 121 err = prestera_hw_port_mac_mode_set(port, cfg->admin, 122 cfg->mode, cfg->inband, cfg->speed, 123 cfg->duplex, cfg->fec); 124 if (err) 125 return err; 126 127 port->cfg_mac = *cfg; 128 return 0; 129 } 130 131 static int prestera_port_open(struct net_device *dev) 132 { 133 struct prestera_port *port = netdev_priv(dev); 134 struct prestera_port_mac_config cfg_mac; 135 int err = 0; 136 137 if (port->caps.transceiver == PRESTERA_PORT_TCVR_SFP) { 138 err = prestera_port_cfg_mac_read(port, &cfg_mac); 139 if (!err) { 140 cfg_mac.admin = true; 141 err = prestera_port_cfg_mac_write(port, &cfg_mac); 142 } 143 } else { 144 port->cfg_phy.admin = true; 145 err = prestera_hw_port_phy_mode_set(port, true, port->autoneg, 146 port->cfg_phy.mode, 147 port->adver_link_modes, 148 port->cfg_phy.mdix); 149 } 150 151 netif_start_queue(dev); 152 153 return err; 154 } 155 156 static int prestera_port_close(struct net_device *dev) 157 { 158 struct prestera_port *port = netdev_priv(dev); 159 struct prestera_port_mac_config cfg_mac; 160 int err = 0; 161 162 netif_stop_queue(dev); 163 164 if (port->caps.transceiver == PRESTERA_PORT_TCVR_SFP) { 165 err = prestera_port_cfg_mac_read(port, &cfg_mac); 166 if (!err) { 167 cfg_mac.admin = false; 168 prestera_port_cfg_mac_write(port, &cfg_mac); 169 } 170 } else { 171 port->cfg_phy.admin = false; 172 err = prestera_hw_port_phy_mode_set(port, false, port->autoneg, 173 port->cfg_phy.mode, 174 port->adver_link_modes, 175 port->cfg_phy.mdix); 176 } 177 178 return err; 179 } 180 181 static netdev_tx_t prestera_port_xmit(struct sk_buff *skb, 182 struct net_device *dev) 183 { 184 return prestera_rxtx_xmit(netdev_priv(dev), skb); 185 } 186 187 int prestera_is_valid_mac_addr(struct prestera_port *port, const u8 *addr) 188 { 189 if (!is_valid_ether_addr(addr)) 190 return -EADDRNOTAVAIL; 191 192 /* firmware requires that port's MAC address contains first 5 bytes 193 * of the base MAC address 194 */ 195 if (memcmp(port->sw->base_mac, addr, ETH_ALEN - 1)) 196 return -EINVAL; 197 198 return 0; 199 } 200 201 static int prestera_port_set_mac_address(struct net_device *dev, void *p) 202 { 203 struct prestera_port *port = netdev_priv(dev); 204 struct sockaddr *addr = p; 205 int err; 206 207 err = prestera_is_valid_mac_addr(port, addr->sa_data); 208 if (err) 209 return err; 210 211 err = prestera_hw_port_mac_set(port, addr->sa_data); 212 if (err) 213 return err; 214 215 eth_hw_addr_set(dev, addr->sa_data); 216 217 return 0; 218 } 219 220 static int prestera_port_change_mtu(struct net_device *dev, int mtu) 221 { 222 struct prestera_port *port = netdev_priv(dev); 223 int err; 224 225 err = prestera_hw_port_mtu_set(port, mtu); 226 if (err) 227 return err; 228 229 dev->mtu = mtu; 230 231 return 0; 232 } 233 234 static void prestera_port_get_stats64(struct net_device *dev, 235 struct rtnl_link_stats64 *stats) 236 { 237 struct prestera_port *port = netdev_priv(dev); 238 struct prestera_port_stats *port_stats = &port->cached_hw_stats.stats; 239 240 stats->rx_packets = port_stats->broadcast_frames_received + 241 port_stats->multicast_frames_received + 242 port_stats->unicast_frames_received; 243 244 stats->tx_packets = port_stats->broadcast_frames_sent + 245 port_stats->multicast_frames_sent + 246 port_stats->unicast_frames_sent; 247 248 stats->rx_bytes = port_stats->good_octets_received; 249 250 stats->tx_bytes = port_stats->good_octets_sent; 251 252 stats->rx_errors = port_stats->rx_error_frame_received; 253 stats->tx_errors = port_stats->mac_trans_error; 254 255 stats->rx_dropped = port_stats->buffer_overrun; 256 stats->tx_dropped = 0; 257 258 stats->multicast = port_stats->multicast_frames_received; 259 stats->collisions = port_stats->excessive_collision; 260 261 stats->rx_crc_errors = port_stats->bad_crc; 262 } 263 264 static void prestera_port_get_hw_stats(struct prestera_port *port) 265 { 266 prestera_hw_port_stats_get(port, &port->cached_hw_stats.stats); 267 } 268 269 static void prestera_port_stats_update(struct work_struct *work) 270 { 271 struct prestera_port *port = 272 container_of(work, struct prestera_port, 273 cached_hw_stats.caching_dw.work); 274 275 prestera_port_get_hw_stats(port); 276 277 queue_delayed_work(prestera_wq, &port->cached_hw_stats.caching_dw, 278 msecs_to_jiffies(PRESTERA_STATS_DELAY_MS)); 279 } 280 281 static int prestera_port_setup_tc(struct net_device *dev, 282 enum tc_setup_type type, 283 void *type_data) 284 { 285 struct prestera_port *port = netdev_priv(dev); 286 287 switch (type) { 288 case TC_SETUP_BLOCK: 289 return prestera_flow_block_setup(port, type_data); 290 default: 291 return -EOPNOTSUPP; 292 } 293 } 294 295 static const struct net_device_ops prestera_netdev_ops = { 296 .ndo_open = prestera_port_open, 297 .ndo_stop = prestera_port_close, 298 .ndo_start_xmit = prestera_port_xmit, 299 .ndo_setup_tc = prestera_port_setup_tc, 300 .ndo_change_mtu = prestera_port_change_mtu, 301 .ndo_get_stats64 = prestera_port_get_stats64, 302 .ndo_set_mac_address = prestera_port_set_mac_address, 303 .ndo_get_devlink_port = prestera_devlink_get_port, 304 }; 305 306 int prestera_port_autoneg_set(struct prestera_port *port, u64 link_modes) 307 { 308 int err; 309 310 if (port->autoneg && port->adver_link_modes == link_modes) 311 return 0; 312 313 err = prestera_hw_port_phy_mode_set(port, port->cfg_phy.admin, 314 true, 0, link_modes, 315 port->cfg_phy.mdix); 316 if (err) 317 return err; 318 319 port->adver_fec = BIT(PRESTERA_PORT_FEC_OFF); 320 port->adver_link_modes = link_modes; 321 port->cfg_phy.mode = 0; 322 port->autoneg = true; 323 324 return 0; 325 } 326 327 static void prestera_port_list_add(struct prestera_port *port) 328 { 329 write_lock(&port->sw->port_list_lock); 330 list_add(&port->list, &port->sw->port_list); 331 write_unlock(&port->sw->port_list_lock); 332 } 333 334 static void prestera_port_list_del(struct prestera_port *port) 335 { 336 write_lock(&port->sw->port_list_lock); 337 list_del(&port->list); 338 write_unlock(&port->sw->port_list_lock); 339 } 340 341 static int prestera_port_create(struct prestera_switch *sw, u32 id) 342 { 343 struct prestera_port_mac_config cfg_mac; 344 struct prestera_port *port; 345 struct net_device *dev; 346 int err; 347 348 dev = alloc_etherdev(sizeof(*port)); 349 if (!dev) 350 return -ENOMEM; 351 352 port = netdev_priv(dev); 353 354 INIT_LIST_HEAD(&port->vlans_list); 355 port->pvid = PRESTERA_DEFAULT_VID; 356 port->lag = NULL; 357 port->dev = dev; 358 port->id = id; 359 port->sw = sw; 360 361 err = prestera_hw_port_info_get(port, &port->dev_id, &port->hw_id, 362 &port->fp_id); 363 if (err) { 364 dev_err(prestera_dev(sw), "Failed to get port(%u) info\n", id); 365 goto err_port_info_get; 366 } 367 368 err = prestera_devlink_port_register(port); 369 if (err) 370 goto err_dl_port_register; 371 372 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_HW_TC; 373 dev->netdev_ops = &prestera_netdev_ops; 374 dev->ethtool_ops = &prestera_ethtool_ops; 375 376 netif_carrier_off(dev); 377 378 dev->mtu = min_t(unsigned int, sw->mtu_max, PRESTERA_MTU_DEFAULT); 379 dev->min_mtu = sw->mtu_min; 380 dev->max_mtu = sw->mtu_max; 381 382 err = prestera_hw_port_mtu_set(port, dev->mtu); 383 if (err) { 384 dev_err(prestera_dev(sw), "Failed to set port(%u) mtu(%d)\n", 385 id, dev->mtu); 386 goto err_port_init; 387 } 388 389 if (port->fp_id >= PRESTERA_MAC_ADDR_NUM_MAX) { 390 err = -EINVAL; 391 goto err_port_init; 392 } 393 394 eth_hw_addr_gen(dev, sw->base_mac, port->fp_id); 395 /* firmware requires that port's MAC address consist of the first 396 * 5 bytes of the base MAC address 397 */ 398 if (memcmp(dev->dev_addr, sw->base_mac, ETH_ALEN - 1)) { 399 dev_warn(prestera_dev(sw), "Port MAC address wraps for port(%u)\n", id); 400 dev_addr_mod(dev, 0, sw->base_mac, ETH_ALEN - 1); 401 } 402 403 err = prestera_hw_port_mac_set(port, dev->dev_addr); 404 if (err) { 405 dev_err(prestera_dev(sw), "Failed to set port(%u) mac addr\n", id); 406 goto err_port_init; 407 } 408 409 err = prestera_hw_port_cap_get(port, &port->caps); 410 if (err) { 411 dev_err(prestera_dev(sw), "Failed to get port(%u) caps\n", id); 412 goto err_port_init; 413 } 414 415 port->adver_link_modes = port->caps.supp_link_modes; 416 port->adver_fec = 0; 417 port->autoneg = true; 418 419 /* initialize config mac */ 420 if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP) { 421 cfg_mac.admin = true; 422 cfg_mac.mode = PRESTERA_MAC_MODE_INTERNAL; 423 } else { 424 cfg_mac.admin = false; 425 cfg_mac.mode = PRESTERA_MAC_MODE_MAX; 426 } 427 cfg_mac.inband = false; 428 cfg_mac.speed = 0; 429 cfg_mac.duplex = DUPLEX_UNKNOWN; 430 cfg_mac.fec = PRESTERA_PORT_FEC_OFF; 431 432 err = prestera_port_cfg_mac_write(port, &cfg_mac); 433 if (err) { 434 dev_err(prestera_dev(sw), 435 "Failed to set port(%u) mac mode\n", id); 436 goto err_port_init; 437 } 438 439 /* initialize config phy (if this is inegral) */ 440 if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP) { 441 port->cfg_phy.mdix = ETH_TP_MDI_AUTO; 442 port->cfg_phy.admin = false; 443 err = prestera_hw_port_phy_mode_set(port, 444 port->cfg_phy.admin, 445 false, 0, 0, 446 port->cfg_phy.mdix); 447 if (err) { 448 dev_err(prestera_dev(sw), 449 "Failed to set port(%u) phy mode\n", id); 450 goto err_port_init; 451 } 452 } 453 454 err = prestera_rxtx_port_init(port); 455 if (err) 456 goto err_port_init; 457 458 INIT_DELAYED_WORK(&port->cached_hw_stats.caching_dw, 459 &prestera_port_stats_update); 460 461 prestera_port_list_add(port); 462 463 err = register_netdev(dev); 464 if (err) 465 goto err_register_netdev; 466 467 prestera_devlink_port_set(port); 468 469 return 0; 470 471 err_register_netdev: 472 prestera_port_list_del(port); 473 err_port_init: 474 prestera_devlink_port_unregister(port); 475 err_dl_port_register: 476 err_port_info_get: 477 free_netdev(dev); 478 return err; 479 } 480 481 static void prestera_port_destroy(struct prestera_port *port) 482 { 483 struct net_device *dev = port->dev; 484 485 cancel_delayed_work_sync(&port->cached_hw_stats.caching_dw); 486 prestera_devlink_port_clear(port); 487 unregister_netdev(dev); 488 prestera_port_list_del(port); 489 prestera_devlink_port_unregister(port); 490 free_netdev(dev); 491 } 492 493 static void prestera_destroy_ports(struct prestera_switch *sw) 494 { 495 struct prestera_port *port, *tmp; 496 497 list_for_each_entry_safe(port, tmp, &sw->port_list, list) 498 prestera_port_destroy(port); 499 } 500 501 static int prestera_create_ports(struct prestera_switch *sw) 502 { 503 struct prestera_port *port, *tmp; 504 u32 port_idx; 505 int err; 506 507 for (port_idx = 0; port_idx < sw->port_count; port_idx++) { 508 err = prestera_port_create(sw, port_idx); 509 if (err) 510 goto err_port_create; 511 } 512 513 return 0; 514 515 err_port_create: 516 list_for_each_entry_safe(port, tmp, &sw->port_list, list) 517 prestera_port_destroy(port); 518 519 return err; 520 } 521 522 static void prestera_port_handle_event(struct prestera_switch *sw, 523 struct prestera_event *evt, void *arg) 524 { 525 struct delayed_work *caching_dw; 526 struct prestera_port *port; 527 528 port = prestera_find_port(sw, evt->port_evt.port_id); 529 if (!port || !port->dev) 530 return; 531 532 caching_dw = &port->cached_hw_stats.caching_dw; 533 534 prestera_ethtool_port_state_changed(port, &evt->port_evt); 535 536 if (evt->id == PRESTERA_PORT_EVENT_MAC_STATE_CHANGED) { 537 if (port->state_mac.oper) { 538 netif_carrier_on(port->dev); 539 if (!delayed_work_pending(caching_dw)) 540 queue_delayed_work(prestera_wq, caching_dw, 0); 541 } else if (netif_running(port->dev) && 542 netif_carrier_ok(port->dev)) { 543 netif_carrier_off(port->dev); 544 if (delayed_work_pending(caching_dw)) 545 cancel_delayed_work(caching_dw); 546 } 547 } 548 } 549 550 static int prestera_event_handlers_register(struct prestera_switch *sw) 551 { 552 return prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_PORT, 553 prestera_port_handle_event, 554 NULL); 555 } 556 557 static void prestera_event_handlers_unregister(struct prestera_switch *sw) 558 { 559 prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_PORT, 560 prestera_port_handle_event); 561 } 562 563 static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw) 564 { 565 struct device_node *base_mac_np; 566 struct device_node *np; 567 int ret; 568 569 np = of_find_compatible_node(NULL, NULL, "marvell,prestera"); 570 base_mac_np = of_parse_phandle(np, "base-mac-provider", 0); 571 572 ret = of_get_mac_address(base_mac_np, sw->base_mac); 573 if (ret) { 574 eth_random_addr(sw->base_mac); 575 dev_info(prestera_dev(sw), "using random base mac address\n"); 576 } 577 of_node_put(base_mac_np); 578 of_node_put(np); 579 580 return prestera_hw_switch_mac_set(sw, sw->base_mac); 581 } 582 583 struct prestera_lag *prestera_lag_by_id(struct prestera_switch *sw, u16 id) 584 { 585 return id < sw->lag_max ? &sw->lags[id] : NULL; 586 } 587 588 static struct prestera_lag *prestera_lag_by_dev(struct prestera_switch *sw, 589 struct net_device *dev) 590 { 591 struct prestera_lag *lag; 592 u16 id; 593 594 for (id = 0; id < sw->lag_max; id++) { 595 lag = &sw->lags[id]; 596 if (lag->dev == dev) 597 return lag; 598 } 599 600 return NULL; 601 } 602 603 int prestera_lag_id(struct prestera_switch *sw, 604 struct net_device *lag_dev, u16 *lag_id) 605 { 606 struct prestera_lag *lag; 607 int free_id = -1; 608 int id; 609 610 for (id = 0; id < sw->lag_max; id++) { 611 lag = prestera_lag_by_id(sw, id); 612 if (lag->member_count) { 613 if (lag->dev == lag_dev) { 614 *lag_id = id; 615 return 0; 616 } 617 } else if (free_id < 0) { 618 free_id = id; 619 } 620 } 621 if (free_id < 0) 622 return -ENOSPC; 623 *lag_id = free_id; 624 return 0; 625 } 626 627 static struct prestera_lag *prestera_lag_create(struct prestera_switch *sw, 628 struct net_device *lag_dev) 629 { 630 struct prestera_lag *lag = NULL; 631 u16 id; 632 633 for (id = 0; id < sw->lag_max; id++) { 634 lag = &sw->lags[id]; 635 if (!lag->dev) 636 break; 637 } 638 if (lag) { 639 INIT_LIST_HEAD(&lag->members); 640 lag->dev = lag_dev; 641 } 642 643 return lag; 644 } 645 646 static void prestera_lag_destroy(struct prestera_switch *sw, 647 struct prestera_lag *lag) 648 { 649 WARN_ON(!list_empty(&lag->members)); 650 lag->member_count = 0; 651 lag->dev = NULL; 652 } 653 654 static int prestera_lag_port_add(struct prestera_port *port, 655 struct net_device *lag_dev) 656 { 657 struct prestera_switch *sw = port->sw; 658 struct prestera_lag *lag; 659 int err; 660 661 lag = prestera_lag_by_dev(sw, lag_dev); 662 if (!lag) { 663 lag = prestera_lag_create(sw, lag_dev); 664 if (!lag) 665 return -ENOSPC; 666 } 667 668 if (lag->member_count >= sw->lag_member_max) 669 return -ENOSPC; 670 671 err = prestera_hw_lag_member_add(port, lag->lag_id); 672 if (err) { 673 if (!lag->member_count) 674 prestera_lag_destroy(sw, lag); 675 return err; 676 } 677 678 list_add(&port->lag_member, &lag->members); 679 lag->member_count++; 680 port->lag = lag; 681 682 return 0; 683 } 684 685 static int prestera_lag_port_del(struct prestera_port *port) 686 { 687 struct prestera_switch *sw = port->sw; 688 struct prestera_lag *lag = port->lag; 689 int err; 690 691 if (!lag || !lag->member_count) 692 return -EINVAL; 693 694 err = prestera_hw_lag_member_del(port, lag->lag_id); 695 if (err) 696 return err; 697 698 list_del(&port->lag_member); 699 lag->member_count--; 700 port->lag = NULL; 701 702 if (netif_is_bridge_port(lag->dev)) { 703 struct net_device *br_dev; 704 705 br_dev = netdev_master_upper_dev_get(lag->dev); 706 707 prestera_bridge_port_leave(br_dev, port); 708 } 709 710 if (!lag->member_count) 711 prestera_lag_destroy(sw, lag); 712 713 return 0; 714 } 715 716 bool prestera_port_is_lag_member(const struct prestera_port *port) 717 { 718 return !!port->lag; 719 } 720 721 u16 prestera_port_lag_id(const struct prestera_port *port) 722 { 723 return port->lag->lag_id; 724 } 725 726 static int prestera_lag_init(struct prestera_switch *sw) 727 { 728 u16 id; 729 730 sw->lags = kcalloc(sw->lag_max, sizeof(*sw->lags), GFP_KERNEL); 731 if (!sw->lags) 732 return -ENOMEM; 733 734 for (id = 0; id < sw->lag_max; id++) 735 sw->lags[id].lag_id = id; 736 737 return 0; 738 } 739 740 static void prestera_lag_fini(struct prestera_switch *sw) 741 { 742 u8 idx; 743 744 for (idx = 0; idx < sw->lag_max; idx++) 745 WARN_ON(sw->lags[idx].member_count); 746 747 kfree(sw->lags); 748 } 749 750 bool prestera_netdev_check(const struct net_device *dev) 751 { 752 return dev->netdev_ops == &prestera_netdev_ops; 753 } 754 755 static int prestera_lower_dev_walk(struct net_device *dev, 756 struct netdev_nested_priv *priv) 757 { 758 struct prestera_port **pport = (struct prestera_port **)priv->data; 759 760 if (prestera_netdev_check(dev)) { 761 *pport = netdev_priv(dev); 762 return 1; 763 } 764 765 return 0; 766 } 767 768 struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev) 769 { 770 struct prestera_port *port = NULL; 771 struct netdev_nested_priv priv = { 772 .data = (void *)&port, 773 }; 774 775 if (prestera_netdev_check(dev)) 776 return netdev_priv(dev); 777 778 netdev_walk_all_lower_dev(dev, prestera_lower_dev_walk, &priv); 779 780 return port; 781 } 782 783 static int prestera_netdev_port_lower_event(struct net_device *dev, 784 unsigned long event, void *ptr) 785 { 786 struct netdev_notifier_changelowerstate_info *info = ptr; 787 struct netdev_lag_lower_state_info *lower_state_info; 788 struct prestera_port *port = netdev_priv(dev); 789 bool enabled; 790 791 if (!netif_is_lag_port(dev)) 792 return 0; 793 if (!prestera_port_is_lag_member(port)) 794 return 0; 795 796 lower_state_info = info->lower_state_info; 797 enabled = lower_state_info->link_up && lower_state_info->tx_enabled; 798 799 return prestera_hw_lag_member_enable(port, port->lag->lag_id, enabled); 800 } 801 802 static bool prestera_lag_master_check(struct net_device *lag_dev, 803 struct netdev_lag_upper_info *info, 804 struct netlink_ext_ack *ext_ack) 805 { 806 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) { 807 NL_SET_ERR_MSG_MOD(ext_ack, "Unsupported LAG Tx type"); 808 return false; 809 } 810 811 return true; 812 } 813 814 static int prestera_netdev_port_event(struct net_device *lower, 815 struct net_device *dev, 816 unsigned long event, void *ptr) 817 { 818 struct netdev_notifier_info *info = ptr; 819 struct netdev_notifier_changeupper_info *cu_info; 820 struct prestera_port *port = netdev_priv(dev); 821 struct netlink_ext_ack *extack; 822 struct net_device *upper; 823 824 extack = netdev_notifier_info_to_extack(info); 825 cu_info = container_of(info, 826 struct netdev_notifier_changeupper_info, 827 info); 828 829 switch (event) { 830 case NETDEV_PRECHANGEUPPER: 831 upper = cu_info->upper_dev; 832 if (!netif_is_bridge_master(upper) && 833 !netif_is_lag_master(upper)) { 834 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type"); 835 return -EINVAL; 836 } 837 838 if (!cu_info->linking) 839 break; 840 841 if (netdev_has_any_upper_dev(upper)) { 842 NL_SET_ERR_MSG_MOD(extack, "Upper device is already enslaved"); 843 return -EINVAL; 844 } 845 846 if (netif_is_lag_master(upper) && 847 !prestera_lag_master_check(upper, cu_info->upper_info, extack)) 848 return -EOPNOTSUPP; 849 if (netif_is_lag_master(upper) && vlan_uses_dev(dev)) { 850 NL_SET_ERR_MSG_MOD(extack, 851 "Master device is a LAG master and port has a VLAN"); 852 return -EINVAL; 853 } 854 if (netif_is_lag_port(dev) && is_vlan_dev(upper) && 855 !netif_is_lag_master(vlan_dev_real_dev(upper))) { 856 NL_SET_ERR_MSG_MOD(extack, 857 "Can not put a VLAN on a LAG port"); 858 return -EINVAL; 859 } 860 break; 861 862 case NETDEV_CHANGEUPPER: 863 upper = cu_info->upper_dev; 864 if (netif_is_bridge_master(upper)) { 865 if (cu_info->linking) 866 return prestera_bridge_port_join(upper, port, 867 extack); 868 else 869 prestera_bridge_port_leave(upper, port); 870 } else if (netif_is_lag_master(upper)) { 871 if (cu_info->linking) 872 return prestera_lag_port_add(port, upper); 873 else 874 prestera_lag_port_del(port); 875 } 876 break; 877 878 case NETDEV_CHANGELOWERSTATE: 879 return prestera_netdev_port_lower_event(dev, event, ptr); 880 } 881 882 return 0; 883 } 884 885 static int prestera_netdevice_lag_event(struct net_device *lag_dev, 886 unsigned long event, void *ptr) 887 { 888 struct net_device *dev; 889 struct list_head *iter; 890 int err; 891 892 netdev_for_each_lower_dev(lag_dev, dev, iter) { 893 if (prestera_netdev_check(dev)) { 894 err = prestera_netdev_port_event(lag_dev, dev, event, 895 ptr); 896 if (err) 897 return err; 898 } 899 } 900 901 return 0; 902 } 903 904 static int prestera_netdev_event_handler(struct notifier_block *nb, 905 unsigned long event, void *ptr) 906 { 907 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 908 int err = 0; 909 910 if (prestera_netdev_check(dev)) 911 err = prestera_netdev_port_event(dev, dev, event, ptr); 912 else if (netif_is_lag_master(dev)) 913 err = prestera_netdevice_lag_event(dev, event, ptr); 914 915 return notifier_from_errno(err); 916 } 917 918 struct prestera_mdb_entry * 919 prestera_mdb_entry_create(struct prestera_switch *sw, 920 const unsigned char *addr, u16 vid) 921 { 922 struct prestera_flood_domain *flood_domain; 923 struct prestera_mdb_entry *mdb_entry; 924 925 mdb_entry = kzalloc(sizeof(*mdb_entry), GFP_KERNEL); 926 if (!mdb_entry) 927 goto err_mdb_alloc; 928 929 flood_domain = prestera_flood_domain_create(sw); 930 if (!flood_domain) 931 goto err_flood_domain_create; 932 933 mdb_entry->sw = sw; 934 mdb_entry->vid = vid; 935 mdb_entry->flood_domain = flood_domain; 936 ether_addr_copy(mdb_entry->addr, addr); 937 938 if (prestera_hw_mdb_create(mdb_entry)) 939 goto err_mdb_hw_create; 940 941 return mdb_entry; 942 943 err_mdb_hw_create: 944 prestera_flood_domain_destroy(flood_domain); 945 err_flood_domain_create: 946 kfree(mdb_entry); 947 err_mdb_alloc: 948 return NULL; 949 } 950 951 void prestera_mdb_entry_destroy(struct prestera_mdb_entry *mdb_entry) 952 { 953 prestera_hw_mdb_destroy(mdb_entry); 954 prestera_flood_domain_destroy(mdb_entry->flood_domain); 955 kfree(mdb_entry); 956 } 957 958 struct prestera_flood_domain * 959 prestera_flood_domain_create(struct prestera_switch *sw) 960 { 961 struct prestera_flood_domain *domain; 962 963 domain = kzalloc(sizeof(*domain), GFP_KERNEL); 964 if (!domain) 965 return NULL; 966 967 domain->sw = sw; 968 969 if (prestera_hw_flood_domain_create(domain)) { 970 kfree(domain); 971 return NULL; 972 } 973 974 INIT_LIST_HEAD(&domain->flood_domain_port_list); 975 976 return domain; 977 } 978 979 void prestera_flood_domain_destroy(struct prestera_flood_domain *flood_domain) 980 { 981 WARN_ON(!list_empty(&flood_domain->flood_domain_port_list)); 982 WARN_ON_ONCE(prestera_hw_flood_domain_destroy(flood_domain)); 983 kfree(flood_domain); 984 } 985 986 int 987 prestera_flood_domain_port_create(struct prestera_flood_domain *flood_domain, 988 struct net_device *dev, 989 u16 vid) 990 { 991 struct prestera_flood_domain_port *flood_domain_port; 992 bool is_first_port_in_list = false; 993 int err; 994 995 flood_domain_port = kzalloc(sizeof(*flood_domain_port), GFP_KERNEL); 996 if (!flood_domain_port) { 997 err = -ENOMEM; 998 goto err_port_alloc; 999 } 1000 1001 flood_domain_port->vid = vid; 1002 1003 if (list_empty(&flood_domain->flood_domain_port_list)) 1004 is_first_port_in_list = true; 1005 1006 list_add(&flood_domain_port->flood_domain_port_node, 1007 &flood_domain->flood_domain_port_list); 1008 1009 flood_domain_port->flood_domain = flood_domain; 1010 flood_domain_port->dev = dev; 1011 1012 if (!is_first_port_in_list) { 1013 err = prestera_hw_flood_domain_ports_reset(flood_domain); 1014 if (err) 1015 goto err_prestera_mdb_port_create_hw; 1016 } 1017 1018 err = prestera_hw_flood_domain_ports_set(flood_domain); 1019 if (err) 1020 goto err_prestera_mdb_port_create_hw; 1021 1022 return 0; 1023 1024 err_prestera_mdb_port_create_hw: 1025 list_del(&flood_domain_port->flood_domain_port_node); 1026 kfree(flood_domain_port); 1027 err_port_alloc: 1028 return err; 1029 } 1030 1031 void 1032 prestera_flood_domain_port_destroy(struct prestera_flood_domain_port *port) 1033 { 1034 struct prestera_flood_domain *flood_domain = port->flood_domain; 1035 1036 list_del(&port->flood_domain_port_node); 1037 1038 WARN_ON_ONCE(prestera_hw_flood_domain_ports_reset(flood_domain)); 1039 1040 if (!list_empty(&flood_domain->flood_domain_port_list)) 1041 WARN_ON_ONCE(prestera_hw_flood_domain_ports_set(flood_domain)); 1042 1043 kfree(port); 1044 } 1045 1046 struct prestera_flood_domain_port * 1047 prestera_flood_domain_port_find(struct prestera_flood_domain *flood_domain, 1048 struct net_device *dev, u16 vid) 1049 { 1050 struct prestera_flood_domain_port *flood_domain_port; 1051 1052 list_for_each_entry(flood_domain_port, 1053 &flood_domain->flood_domain_port_list, 1054 flood_domain_port_node) 1055 if (flood_domain_port->dev == dev && 1056 vid == flood_domain_port->vid) 1057 return flood_domain_port; 1058 1059 return NULL; 1060 } 1061 1062 static int prestera_netdev_event_handler_register(struct prestera_switch *sw) 1063 { 1064 sw->netdev_nb.notifier_call = prestera_netdev_event_handler; 1065 1066 return register_netdevice_notifier(&sw->netdev_nb); 1067 } 1068 1069 static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw) 1070 { 1071 unregister_netdevice_notifier(&sw->netdev_nb); 1072 } 1073 1074 static int prestera_switch_init(struct prestera_switch *sw) 1075 { 1076 int err; 1077 1078 err = prestera_hw_switch_init(sw); 1079 if (err) { 1080 dev_err(prestera_dev(sw), "Failed to init Switch device\n"); 1081 return err; 1082 } 1083 1084 rwlock_init(&sw->port_list_lock); 1085 INIT_LIST_HEAD(&sw->port_list); 1086 1087 err = prestera_switch_set_base_mac_addr(sw); 1088 if (err) 1089 return err; 1090 1091 err = prestera_netdev_event_handler_register(sw); 1092 if (err) 1093 return err; 1094 1095 err = prestera_router_init(sw); 1096 if (err) 1097 goto err_router_init; 1098 1099 err = prestera_switchdev_init(sw); 1100 if (err) 1101 goto err_swdev_register; 1102 1103 err = prestera_rxtx_switch_init(sw); 1104 if (err) 1105 goto err_rxtx_register; 1106 1107 err = prestera_event_handlers_register(sw); 1108 if (err) 1109 goto err_handlers_register; 1110 1111 err = prestera_counter_init(sw); 1112 if (err) 1113 goto err_counter_init; 1114 1115 err = prestera_acl_init(sw); 1116 if (err) 1117 goto err_acl_init; 1118 1119 err = prestera_span_init(sw); 1120 if (err) 1121 goto err_span_init; 1122 1123 err = prestera_devlink_traps_register(sw); 1124 if (err) 1125 goto err_dl_register; 1126 1127 err = prestera_lag_init(sw); 1128 if (err) 1129 goto err_lag_init; 1130 1131 err = prestera_create_ports(sw); 1132 if (err) 1133 goto err_ports_create; 1134 1135 prestera_devlink_register(sw); 1136 return 0; 1137 1138 err_ports_create: 1139 prestera_lag_fini(sw); 1140 err_lag_init: 1141 prestera_devlink_traps_unregister(sw); 1142 err_dl_register: 1143 prestera_span_fini(sw); 1144 err_span_init: 1145 prestera_acl_fini(sw); 1146 err_acl_init: 1147 prestera_counter_fini(sw); 1148 err_counter_init: 1149 prestera_event_handlers_unregister(sw); 1150 err_handlers_register: 1151 prestera_rxtx_switch_fini(sw); 1152 err_rxtx_register: 1153 prestera_switchdev_fini(sw); 1154 err_swdev_register: 1155 prestera_router_fini(sw); 1156 err_router_init: 1157 prestera_netdev_event_handler_unregister(sw); 1158 prestera_hw_switch_fini(sw); 1159 1160 return err; 1161 } 1162 1163 static void prestera_switch_fini(struct prestera_switch *sw) 1164 { 1165 prestera_devlink_unregister(sw); 1166 prestera_destroy_ports(sw); 1167 prestera_lag_fini(sw); 1168 prestera_devlink_traps_unregister(sw); 1169 prestera_span_fini(sw); 1170 prestera_acl_fini(sw); 1171 prestera_counter_fini(sw); 1172 prestera_event_handlers_unregister(sw); 1173 prestera_rxtx_switch_fini(sw); 1174 prestera_switchdev_fini(sw); 1175 prestera_router_fini(sw); 1176 prestera_netdev_event_handler_unregister(sw); 1177 prestera_hw_switch_fini(sw); 1178 } 1179 1180 int prestera_device_register(struct prestera_device *dev) 1181 { 1182 struct prestera_switch *sw; 1183 int err; 1184 1185 sw = prestera_devlink_alloc(dev); 1186 if (!sw) 1187 return -ENOMEM; 1188 1189 dev->priv = sw; 1190 sw->dev = dev; 1191 1192 err = prestera_switch_init(sw); 1193 if (err) { 1194 prestera_devlink_free(sw); 1195 return err; 1196 } 1197 1198 return 0; 1199 } 1200 EXPORT_SYMBOL(prestera_device_register); 1201 1202 void prestera_device_unregister(struct prestera_device *dev) 1203 { 1204 struct prestera_switch *sw = dev->priv; 1205 1206 prestera_switch_fini(sw); 1207 prestera_devlink_free(sw); 1208 } 1209 EXPORT_SYMBOL(prestera_device_unregister); 1210 1211 static int __init prestera_module_init(void) 1212 { 1213 prestera_wq = alloc_workqueue("prestera", 0, 0); 1214 if (!prestera_wq) 1215 return -ENOMEM; 1216 1217 prestera_owq = alloc_ordered_workqueue("prestera_ordered", 0); 1218 if (!prestera_owq) { 1219 destroy_workqueue(prestera_wq); 1220 return -ENOMEM; 1221 } 1222 1223 return 0; 1224 } 1225 1226 static void __exit prestera_module_exit(void) 1227 { 1228 destroy_workqueue(prestera_wq); 1229 destroy_workqueue(prestera_owq); 1230 } 1231 1232 module_init(prestera_module_init); 1233 module_exit(prestera_module_exit); 1234 1235 MODULE_LICENSE("Dual BSD/GPL"); 1236 MODULE_DESCRIPTION("Marvell Prestera switch driver"); 1237