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