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), "Failed to set port(%u) mac mode\n", id); 409 goto err_port_init; 410 } 411 412 /* initialize config phy (if this is inegral) */ 413 if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP) { 414 port->cfg_phy.mdix = ETH_TP_MDI_AUTO; 415 port->cfg_phy.admin = false; 416 err = prestera_hw_port_phy_mode_set(port, 417 port->cfg_phy.admin, 418 false, 0, 0, 419 port->cfg_phy.mdix); 420 if (err) { 421 dev_err(prestera_dev(sw), "Failed to set port(%u) phy mode\n", id); 422 goto err_port_init; 423 } 424 } 425 426 err = prestera_rxtx_port_init(port); 427 if (err) 428 goto err_port_init; 429 430 INIT_DELAYED_WORK(&port->cached_hw_stats.caching_dw, 431 &prestera_port_stats_update); 432 433 prestera_port_list_add(port); 434 435 err = register_netdev(dev); 436 if (err) 437 goto err_register_netdev; 438 439 prestera_devlink_port_set(port); 440 441 return 0; 442 443 err_register_netdev: 444 prestera_port_list_del(port); 445 err_port_init: 446 prestera_devlink_port_unregister(port); 447 err_dl_port_register: 448 err_port_info_get: 449 free_netdev(dev); 450 return err; 451 } 452 453 static void prestera_port_destroy(struct prestera_port *port) 454 { 455 struct net_device *dev = port->dev; 456 457 cancel_delayed_work_sync(&port->cached_hw_stats.caching_dw); 458 prestera_devlink_port_clear(port); 459 unregister_netdev(dev); 460 prestera_port_list_del(port); 461 prestera_devlink_port_unregister(port); 462 free_netdev(dev); 463 } 464 465 static void prestera_destroy_ports(struct prestera_switch *sw) 466 { 467 struct prestera_port *port, *tmp; 468 469 list_for_each_entry_safe(port, tmp, &sw->port_list, list) 470 prestera_port_destroy(port); 471 } 472 473 static int prestera_create_ports(struct prestera_switch *sw) 474 { 475 struct prestera_port *port, *tmp; 476 u32 port_idx; 477 int err; 478 479 for (port_idx = 0; port_idx < sw->port_count; port_idx++) { 480 err = prestera_port_create(sw, port_idx); 481 if (err) 482 goto err_port_create; 483 } 484 485 return 0; 486 487 err_port_create: 488 list_for_each_entry_safe(port, tmp, &sw->port_list, list) 489 prestera_port_destroy(port); 490 491 return err; 492 } 493 494 static void prestera_port_handle_event(struct prestera_switch *sw, 495 struct prestera_event *evt, void *arg) 496 { 497 struct delayed_work *caching_dw; 498 struct prestera_port *port; 499 500 port = prestera_find_port(sw, evt->port_evt.port_id); 501 if (!port || !port->dev) 502 return; 503 504 caching_dw = &port->cached_hw_stats.caching_dw; 505 506 prestera_ethtool_port_state_changed(port, &evt->port_evt); 507 508 if (evt->id == PRESTERA_PORT_EVENT_MAC_STATE_CHANGED) { 509 if (port->state_mac.oper) { 510 netif_carrier_on(port->dev); 511 if (!delayed_work_pending(caching_dw)) 512 queue_delayed_work(prestera_wq, caching_dw, 0); 513 } else if (netif_running(port->dev) && 514 netif_carrier_ok(port->dev)) { 515 netif_carrier_off(port->dev); 516 if (delayed_work_pending(caching_dw)) 517 cancel_delayed_work(caching_dw); 518 } 519 } 520 } 521 522 static int prestera_event_handlers_register(struct prestera_switch *sw) 523 { 524 return prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_PORT, 525 prestera_port_handle_event, 526 NULL); 527 } 528 529 static void prestera_event_handlers_unregister(struct prestera_switch *sw) 530 { 531 prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_PORT, 532 prestera_port_handle_event); 533 } 534 535 static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw) 536 { 537 struct device_node *base_mac_np; 538 struct device_node *np; 539 int ret; 540 541 np = of_find_compatible_node(NULL, NULL, "marvell,prestera"); 542 base_mac_np = of_parse_phandle(np, "base-mac-provider", 0); 543 544 ret = of_get_mac_address(base_mac_np, sw->base_mac); 545 if (ret) { 546 eth_random_addr(sw->base_mac); 547 dev_info(prestera_dev(sw), "using random base mac address\n"); 548 } 549 of_node_put(base_mac_np); 550 551 return prestera_hw_switch_mac_set(sw, sw->base_mac); 552 } 553 554 struct prestera_lag *prestera_lag_by_id(struct prestera_switch *sw, u16 id) 555 { 556 return id < sw->lag_max ? &sw->lags[id] : NULL; 557 } 558 559 static struct prestera_lag *prestera_lag_by_dev(struct prestera_switch *sw, 560 struct net_device *dev) 561 { 562 struct prestera_lag *lag; 563 u16 id; 564 565 for (id = 0; id < sw->lag_max; id++) { 566 lag = &sw->lags[id]; 567 if (lag->dev == dev) 568 return lag; 569 } 570 571 return NULL; 572 } 573 574 static struct prestera_lag *prestera_lag_create(struct prestera_switch *sw, 575 struct net_device *lag_dev) 576 { 577 struct prestera_lag *lag = NULL; 578 u16 id; 579 580 for (id = 0; id < sw->lag_max; id++) { 581 lag = &sw->lags[id]; 582 if (!lag->dev) 583 break; 584 } 585 if (lag) { 586 INIT_LIST_HEAD(&lag->members); 587 lag->dev = lag_dev; 588 } 589 590 return lag; 591 } 592 593 static void prestera_lag_destroy(struct prestera_switch *sw, 594 struct prestera_lag *lag) 595 { 596 WARN_ON(!list_empty(&lag->members)); 597 lag->member_count = 0; 598 lag->dev = NULL; 599 } 600 601 static int prestera_lag_port_add(struct prestera_port *port, 602 struct net_device *lag_dev) 603 { 604 struct prestera_switch *sw = port->sw; 605 struct prestera_lag *lag; 606 int err; 607 608 lag = prestera_lag_by_dev(sw, lag_dev); 609 if (!lag) { 610 lag = prestera_lag_create(sw, lag_dev); 611 if (!lag) 612 return -ENOSPC; 613 } 614 615 if (lag->member_count >= sw->lag_member_max) 616 return -ENOSPC; 617 618 err = prestera_hw_lag_member_add(port, lag->lag_id); 619 if (err) { 620 if (!lag->member_count) 621 prestera_lag_destroy(sw, lag); 622 return err; 623 } 624 625 list_add(&port->lag_member, &lag->members); 626 lag->member_count++; 627 port->lag = lag; 628 629 return 0; 630 } 631 632 static int prestera_lag_port_del(struct prestera_port *port) 633 { 634 struct prestera_switch *sw = port->sw; 635 struct prestera_lag *lag = port->lag; 636 int err; 637 638 if (!lag || !lag->member_count) 639 return -EINVAL; 640 641 err = prestera_hw_lag_member_del(port, lag->lag_id); 642 if (err) 643 return err; 644 645 list_del(&port->lag_member); 646 lag->member_count--; 647 port->lag = NULL; 648 649 if (netif_is_bridge_port(lag->dev)) { 650 struct net_device *br_dev; 651 652 br_dev = netdev_master_upper_dev_get(lag->dev); 653 654 prestera_bridge_port_leave(br_dev, port); 655 } 656 657 if (!lag->member_count) 658 prestera_lag_destroy(sw, lag); 659 660 return 0; 661 } 662 663 bool prestera_port_is_lag_member(const struct prestera_port *port) 664 { 665 return !!port->lag; 666 } 667 668 u16 prestera_port_lag_id(const struct prestera_port *port) 669 { 670 return port->lag->lag_id; 671 } 672 673 static int prestera_lag_init(struct prestera_switch *sw) 674 { 675 u16 id; 676 677 sw->lags = kcalloc(sw->lag_max, sizeof(*sw->lags), GFP_KERNEL); 678 if (!sw->lags) 679 return -ENOMEM; 680 681 for (id = 0; id < sw->lag_max; id++) 682 sw->lags[id].lag_id = id; 683 684 return 0; 685 } 686 687 static void prestera_lag_fini(struct prestera_switch *sw) 688 { 689 u8 idx; 690 691 for (idx = 0; idx < sw->lag_max; idx++) 692 WARN_ON(sw->lags[idx].member_count); 693 694 kfree(sw->lags); 695 } 696 697 bool prestera_netdev_check(const struct net_device *dev) 698 { 699 return dev->netdev_ops == &prestera_netdev_ops; 700 } 701 702 static int prestera_lower_dev_walk(struct net_device *dev, 703 struct netdev_nested_priv *priv) 704 { 705 struct prestera_port **pport = (struct prestera_port **)priv->data; 706 707 if (prestera_netdev_check(dev)) { 708 *pport = netdev_priv(dev); 709 return 1; 710 } 711 712 return 0; 713 } 714 715 struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev) 716 { 717 struct prestera_port *port = NULL; 718 struct netdev_nested_priv priv = { 719 .data = (void *)&port, 720 }; 721 722 if (prestera_netdev_check(dev)) 723 return netdev_priv(dev); 724 725 netdev_walk_all_lower_dev(dev, prestera_lower_dev_walk, &priv); 726 727 return port; 728 } 729 730 static int prestera_netdev_port_lower_event(struct net_device *dev, 731 unsigned long event, void *ptr) 732 { 733 struct netdev_notifier_changelowerstate_info *info = ptr; 734 struct netdev_lag_lower_state_info *lower_state_info; 735 struct prestera_port *port = netdev_priv(dev); 736 bool enabled; 737 738 if (!netif_is_lag_port(dev)) 739 return 0; 740 if (!prestera_port_is_lag_member(port)) 741 return 0; 742 743 lower_state_info = info->lower_state_info; 744 enabled = lower_state_info->link_up && lower_state_info->tx_enabled; 745 746 return prestera_hw_lag_member_enable(port, port->lag->lag_id, enabled); 747 } 748 749 static bool prestera_lag_master_check(struct net_device *lag_dev, 750 struct netdev_lag_upper_info *info, 751 struct netlink_ext_ack *ext_ack) 752 { 753 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) { 754 NL_SET_ERR_MSG_MOD(ext_ack, "Unsupported LAG Tx type"); 755 return false; 756 } 757 758 return true; 759 } 760 761 static int prestera_netdev_port_event(struct net_device *lower, 762 struct net_device *dev, 763 unsigned long event, void *ptr) 764 { 765 struct netdev_notifier_changeupper_info *info = ptr; 766 struct prestera_port *port = netdev_priv(dev); 767 struct netlink_ext_ack *extack; 768 struct net_device *upper; 769 770 extack = netdev_notifier_info_to_extack(&info->info); 771 upper = info->upper_dev; 772 773 switch (event) { 774 case NETDEV_PRECHANGEUPPER: 775 if (!netif_is_bridge_master(upper) && 776 !netif_is_lag_master(upper)) { 777 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type"); 778 return -EINVAL; 779 } 780 781 if (!info->linking) 782 break; 783 784 if (netdev_has_any_upper_dev(upper)) { 785 NL_SET_ERR_MSG_MOD(extack, "Upper device is already enslaved"); 786 return -EINVAL; 787 } 788 789 if (netif_is_lag_master(upper) && 790 !prestera_lag_master_check(upper, info->upper_info, extack)) 791 return -EOPNOTSUPP; 792 if (netif_is_lag_master(upper) && vlan_uses_dev(dev)) { 793 NL_SET_ERR_MSG_MOD(extack, 794 "Master device is a LAG master and port has a VLAN"); 795 return -EINVAL; 796 } 797 if (netif_is_lag_port(dev) && is_vlan_dev(upper) && 798 !netif_is_lag_master(vlan_dev_real_dev(upper))) { 799 NL_SET_ERR_MSG_MOD(extack, 800 "Can not put a VLAN on a LAG port"); 801 return -EINVAL; 802 } 803 break; 804 805 case NETDEV_CHANGEUPPER: 806 if (netif_is_bridge_master(upper)) { 807 if (info->linking) 808 return prestera_bridge_port_join(upper, port, 809 extack); 810 else 811 prestera_bridge_port_leave(upper, port); 812 } else if (netif_is_lag_master(upper)) { 813 if (info->linking) 814 return prestera_lag_port_add(port, upper); 815 else 816 prestera_lag_port_del(port); 817 } 818 break; 819 820 case NETDEV_CHANGELOWERSTATE: 821 return prestera_netdev_port_lower_event(dev, event, ptr); 822 } 823 824 return 0; 825 } 826 827 static int prestera_netdevice_lag_event(struct net_device *lag_dev, 828 unsigned long event, void *ptr) 829 { 830 struct net_device *dev; 831 struct list_head *iter; 832 int err; 833 834 netdev_for_each_lower_dev(lag_dev, dev, iter) { 835 if (prestera_netdev_check(dev)) { 836 err = prestera_netdev_port_event(lag_dev, dev, event, 837 ptr); 838 if (err) 839 return err; 840 } 841 } 842 843 return 0; 844 } 845 846 static int prestera_netdev_event_handler(struct notifier_block *nb, 847 unsigned long event, void *ptr) 848 { 849 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 850 int err = 0; 851 852 if (prestera_netdev_check(dev)) 853 err = prestera_netdev_port_event(dev, dev, event, ptr); 854 else if (netif_is_lag_master(dev)) 855 err = prestera_netdevice_lag_event(dev, event, ptr); 856 857 return notifier_from_errno(err); 858 } 859 860 static int prestera_netdev_event_handler_register(struct prestera_switch *sw) 861 { 862 sw->netdev_nb.notifier_call = prestera_netdev_event_handler; 863 864 return register_netdevice_notifier(&sw->netdev_nb); 865 } 866 867 static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw) 868 { 869 unregister_netdevice_notifier(&sw->netdev_nb); 870 } 871 872 static int prestera_switch_init(struct prestera_switch *sw) 873 { 874 int err; 875 876 err = prestera_hw_switch_init(sw); 877 if (err) { 878 dev_err(prestera_dev(sw), "Failed to init Switch device\n"); 879 return err; 880 } 881 882 rwlock_init(&sw->port_list_lock); 883 INIT_LIST_HEAD(&sw->port_list); 884 885 err = prestera_switch_set_base_mac_addr(sw); 886 if (err) 887 return err; 888 889 err = prestera_netdev_event_handler_register(sw); 890 if (err) 891 return err; 892 893 err = prestera_switchdev_init(sw); 894 if (err) 895 goto err_swdev_register; 896 897 err = prestera_rxtx_switch_init(sw); 898 if (err) 899 goto err_rxtx_register; 900 901 err = prestera_event_handlers_register(sw); 902 if (err) 903 goto err_handlers_register; 904 905 err = prestera_acl_init(sw); 906 if (err) 907 goto err_acl_init; 908 909 err = prestera_span_init(sw); 910 if (err) 911 goto err_span_init; 912 913 err = prestera_devlink_traps_register(sw); 914 if (err) 915 goto err_dl_register; 916 917 err = prestera_lag_init(sw); 918 if (err) 919 goto err_lag_init; 920 921 err = prestera_create_ports(sw); 922 if (err) 923 goto err_ports_create; 924 925 prestera_devlink_register(sw); 926 return 0; 927 928 err_ports_create: 929 prestera_lag_fini(sw); 930 err_lag_init: 931 prestera_devlink_traps_unregister(sw); 932 err_dl_register: 933 prestera_span_fini(sw); 934 err_span_init: 935 prestera_acl_fini(sw); 936 err_acl_init: 937 prestera_event_handlers_unregister(sw); 938 err_handlers_register: 939 prestera_rxtx_switch_fini(sw); 940 err_rxtx_register: 941 prestera_switchdev_fini(sw); 942 err_swdev_register: 943 prestera_netdev_event_handler_unregister(sw); 944 prestera_hw_switch_fini(sw); 945 946 return err; 947 } 948 949 static void prestera_switch_fini(struct prestera_switch *sw) 950 { 951 prestera_devlink_unregister(sw); 952 prestera_destroy_ports(sw); 953 prestera_lag_fini(sw); 954 prestera_devlink_traps_unregister(sw); 955 prestera_span_fini(sw); 956 prestera_acl_fini(sw); 957 prestera_event_handlers_unregister(sw); 958 prestera_rxtx_switch_fini(sw); 959 prestera_switchdev_fini(sw); 960 prestera_netdev_event_handler_unregister(sw); 961 prestera_hw_switch_fini(sw); 962 } 963 964 int prestera_device_register(struct prestera_device *dev) 965 { 966 struct prestera_switch *sw; 967 int err; 968 969 sw = prestera_devlink_alloc(dev); 970 if (!sw) 971 return -ENOMEM; 972 973 dev->priv = sw; 974 sw->dev = dev; 975 976 err = prestera_switch_init(sw); 977 if (err) { 978 prestera_devlink_free(sw); 979 return err; 980 } 981 982 return 0; 983 } 984 EXPORT_SYMBOL(prestera_device_register); 985 986 void prestera_device_unregister(struct prestera_device *dev) 987 { 988 struct prestera_switch *sw = dev->priv; 989 990 prestera_switch_fini(sw); 991 prestera_devlink_free(sw); 992 } 993 EXPORT_SYMBOL(prestera_device_unregister); 994 995 static int __init prestera_module_init(void) 996 { 997 prestera_wq = alloc_workqueue("prestera", 0, 0); 998 if (!prestera_wq) 999 return -ENOMEM; 1000 1001 return 0; 1002 } 1003 1004 static void __exit prestera_module_exit(void) 1005 { 1006 destroy_workqueue(prestera_wq); 1007 } 1008 1009 module_init(prestera_module_init); 1010 module_exit(prestera_module_exit); 1011 1012 MODULE_LICENSE("Dual BSD/GPL"); 1013 MODULE_DESCRIPTION("Marvell Prestera switch driver"); 1014