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