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 static int prestera_netdev_event_handler_register(struct prestera_switch *sw) 919 { 920 sw->netdev_nb.notifier_call = prestera_netdev_event_handler; 921 922 return register_netdevice_notifier(&sw->netdev_nb); 923 } 924 925 static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw) 926 { 927 unregister_netdevice_notifier(&sw->netdev_nb); 928 } 929 930 static int prestera_switch_init(struct prestera_switch *sw) 931 { 932 int err; 933 934 err = prestera_hw_switch_init(sw); 935 if (err) { 936 dev_err(prestera_dev(sw), "Failed to init Switch device\n"); 937 return err; 938 } 939 940 rwlock_init(&sw->port_list_lock); 941 INIT_LIST_HEAD(&sw->port_list); 942 943 err = prestera_switch_set_base_mac_addr(sw); 944 if (err) 945 return err; 946 947 err = prestera_netdev_event_handler_register(sw); 948 if (err) 949 return err; 950 951 err = prestera_router_init(sw); 952 if (err) 953 goto err_router_init; 954 955 err = prestera_switchdev_init(sw); 956 if (err) 957 goto err_swdev_register; 958 959 err = prestera_rxtx_switch_init(sw); 960 if (err) 961 goto err_rxtx_register; 962 963 err = prestera_event_handlers_register(sw); 964 if (err) 965 goto err_handlers_register; 966 967 err = prestera_counter_init(sw); 968 if (err) 969 goto err_counter_init; 970 971 err = prestera_acl_init(sw); 972 if (err) 973 goto err_acl_init; 974 975 err = prestera_span_init(sw); 976 if (err) 977 goto err_span_init; 978 979 err = prestera_devlink_traps_register(sw); 980 if (err) 981 goto err_dl_register; 982 983 err = prestera_lag_init(sw); 984 if (err) 985 goto err_lag_init; 986 987 err = prestera_create_ports(sw); 988 if (err) 989 goto err_ports_create; 990 991 prestera_devlink_register(sw); 992 return 0; 993 994 err_ports_create: 995 prestera_lag_fini(sw); 996 err_lag_init: 997 prestera_devlink_traps_unregister(sw); 998 err_dl_register: 999 prestera_span_fini(sw); 1000 err_span_init: 1001 prestera_acl_fini(sw); 1002 err_acl_init: 1003 prestera_counter_fini(sw); 1004 err_counter_init: 1005 prestera_event_handlers_unregister(sw); 1006 err_handlers_register: 1007 prestera_rxtx_switch_fini(sw); 1008 err_rxtx_register: 1009 prestera_switchdev_fini(sw); 1010 err_swdev_register: 1011 prestera_router_fini(sw); 1012 err_router_init: 1013 prestera_netdev_event_handler_unregister(sw); 1014 prestera_hw_switch_fini(sw); 1015 1016 return err; 1017 } 1018 1019 static void prestera_switch_fini(struct prestera_switch *sw) 1020 { 1021 prestera_devlink_unregister(sw); 1022 prestera_destroy_ports(sw); 1023 prestera_lag_fini(sw); 1024 prestera_devlink_traps_unregister(sw); 1025 prestera_span_fini(sw); 1026 prestera_acl_fini(sw); 1027 prestera_counter_fini(sw); 1028 prestera_event_handlers_unregister(sw); 1029 prestera_rxtx_switch_fini(sw); 1030 prestera_switchdev_fini(sw); 1031 prestera_router_fini(sw); 1032 prestera_netdev_event_handler_unregister(sw); 1033 prestera_hw_switch_fini(sw); 1034 } 1035 1036 int prestera_device_register(struct prestera_device *dev) 1037 { 1038 struct prestera_switch *sw; 1039 int err; 1040 1041 sw = prestera_devlink_alloc(dev); 1042 if (!sw) 1043 return -ENOMEM; 1044 1045 dev->priv = sw; 1046 sw->dev = dev; 1047 1048 err = prestera_switch_init(sw); 1049 if (err) { 1050 prestera_devlink_free(sw); 1051 return err; 1052 } 1053 1054 return 0; 1055 } 1056 EXPORT_SYMBOL(prestera_device_register); 1057 1058 void prestera_device_unregister(struct prestera_device *dev) 1059 { 1060 struct prestera_switch *sw = dev->priv; 1061 1062 prestera_switch_fini(sw); 1063 prestera_devlink_free(sw); 1064 } 1065 EXPORT_SYMBOL(prestera_device_unregister); 1066 1067 static int __init prestera_module_init(void) 1068 { 1069 prestera_wq = alloc_workqueue("prestera", 0, 0); 1070 if (!prestera_wq) 1071 return -ENOMEM; 1072 1073 prestera_owq = alloc_ordered_workqueue("prestera_ordered", 0); 1074 if (!prestera_owq) { 1075 destroy_workqueue(prestera_wq); 1076 return -ENOMEM; 1077 } 1078 1079 return 0; 1080 } 1081 1082 static void __exit prestera_module_exit(void) 1083 { 1084 destroy_workqueue(prestera_wq); 1085 destroy_workqueue(prestera_owq); 1086 } 1087 1088 module_init(prestera_module_init); 1089 module_exit(prestera_module_exit); 1090 1091 MODULE_LICENSE("Dual BSD/GPL"); 1092 MODULE_DESCRIPTION("Marvell Prestera switch driver"); 1093