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