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 of_node_put(np); 558 559 return prestera_hw_switch_mac_set(sw, sw->base_mac); 560 } 561 562 struct prestera_lag *prestera_lag_by_id(struct prestera_switch *sw, u16 id) 563 { 564 return id < sw->lag_max ? &sw->lags[id] : NULL; 565 } 566 567 static struct prestera_lag *prestera_lag_by_dev(struct prestera_switch *sw, 568 struct net_device *dev) 569 { 570 struct prestera_lag *lag; 571 u16 id; 572 573 for (id = 0; id < sw->lag_max; id++) { 574 lag = &sw->lags[id]; 575 if (lag->dev == dev) 576 return lag; 577 } 578 579 return NULL; 580 } 581 582 static struct prestera_lag *prestera_lag_create(struct prestera_switch *sw, 583 struct net_device *lag_dev) 584 { 585 struct prestera_lag *lag = NULL; 586 u16 id; 587 588 for (id = 0; id < sw->lag_max; id++) { 589 lag = &sw->lags[id]; 590 if (!lag->dev) 591 break; 592 } 593 if (lag) { 594 INIT_LIST_HEAD(&lag->members); 595 lag->dev = lag_dev; 596 } 597 598 return lag; 599 } 600 601 static void prestera_lag_destroy(struct prestera_switch *sw, 602 struct prestera_lag *lag) 603 { 604 WARN_ON(!list_empty(&lag->members)); 605 lag->member_count = 0; 606 lag->dev = NULL; 607 } 608 609 static int prestera_lag_port_add(struct prestera_port *port, 610 struct net_device *lag_dev) 611 { 612 struct prestera_switch *sw = port->sw; 613 struct prestera_lag *lag; 614 int err; 615 616 lag = prestera_lag_by_dev(sw, lag_dev); 617 if (!lag) { 618 lag = prestera_lag_create(sw, lag_dev); 619 if (!lag) 620 return -ENOSPC; 621 } 622 623 if (lag->member_count >= sw->lag_member_max) 624 return -ENOSPC; 625 626 err = prestera_hw_lag_member_add(port, lag->lag_id); 627 if (err) { 628 if (!lag->member_count) 629 prestera_lag_destroy(sw, lag); 630 return err; 631 } 632 633 list_add(&port->lag_member, &lag->members); 634 lag->member_count++; 635 port->lag = lag; 636 637 return 0; 638 } 639 640 static int prestera_lag_port_del(struct prestera_port *port) 641 { 642 struct prestera_switch *sw = port->sw; 643 struct prestera_lag *lag = port->lag; 644 int err; 645 646 if (!lag || !lag->member_count) 647 return -EINVAL; 648 649 err = prestera_hw_lag_member_del(port, lag->lag_id); 650 if (err) 651 return err; 652 653 list_del(&port->lag_member); 654 lag->member_count--; 655 port->lag = NULL; 656 657 if (netif_is_bridge_port(lag->dev)) { 658 struct net_device *br_dev; 659 660 br_dev = netdev_master_upper_dev_get(lag->dev); 661 662 prestera_bridge_port_leave(br_dev, port); 663 } 664 665 if (!lag->member_count) 666 prestera_lag_destroy(sw, lag); 667 668 return 0; 669 } 670 671 bool prestera_port_is_lag_member(const struct prestera_port *port) 672 { 673 return !!port->lag; 674 } 675 676 u16 prestera_port_lag_id(const struct prestera_port *port) 677 { 678 return port->lag->lag_id; 679 } 680 681 static int prestera_lag_init(struct prestera_switch *sw) 682 { 683 u16 id; 684 685 sw->lags = kcalloc(sw->lag_max, sizeof(*sw->lags), GFP_KERNEL); 686 if (!sw->lags) 687 return -ENOMEM; 688 689 for (id = 0; id < sw->lag_max; id++) 690 sw->lags[id].lag_id = id; 691 692 return 0; 693 } 694 695 static void prestera_lag_fini(struct prestera_switch *sw) 696 { 697 u8 idx; 698 699 for (idx = 0; idx < sw->lag_max; idx++) 700 WARN_ON(sw->lags[idx].member_count); 701 702 kfree(sw->lags); 703 } 704 705 bool prestera_netdev_check(const struct net_device *dev) 706 { 707 return dev->netdev_ops == &prestera_netdev_ops; 708 } 709 710 static int prestera_lower_dev_walk(struct net_device *dev, 711 struct netdev_nested_priv *priv) 712 { 713 struct prestera_port **pport = (struct prestera_port **)priv->data; 714 715 if (prestera_netdev_check(dev)) { 716 *pport = netdev_priv(dev); 717 return 1; 718 } 719 720 return 0; 721 } 722 723 struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev) 724 { 725 struct prestera_port *port = NULL; 726 struct netdev_nested_priv priv = { 727 .data = (void *)&port, 728 }; 729 730 if (prestera_netdev_check(dev)) 731 return netdev_priv(dev); 732 733 netdev_walk_all_lower_dev(dev, prestera_lower_dev_walk, &priv); 734 735 return port; 736 } 737 738 static int prestera_netdev_port_lower_event(struct net_device *dev, 739 unsigned long event, void *ptr) 740 { 741 struct netdev_notifier_changelowerstate_info *info = ptr; 742 struct netdev_lag_lower_state_info *lower_state_info; 743 struct prestera_port *port = netdev_priv(dev); 744 bool enabled; 745 746 if (!netif_is_lag_port(dev)) 747 return 0; 748 if (!prestera_port_is_lag_member(port)) 749 return 0; 750 751 lower_state_info = info->lower_state_info; 752 enabled = lower_state_info->link_up && lower_state_info->tx_enabled; 753 754 return prestera_hw_lag_member_enable(port, port->lag->lag_id, enabled); 755 } 756 757 static bool prestera_lag_master_check(struct net_device *lag_dev, 758 struct netdev_lag_upper_info *info, 759 struct netlink_ext_ack *ext_ack) 760 { 761 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) { 762 NL_SET_ERR_MSG_MOD(ext_ack, "Unsupported LAG Tx type"); 763 return false; 764 } 765 766 return true; 767 } 768 769 static int prestera_netdev_port_event(struct net_device *lower, 770 struct net_device *dev, 771 unsigned long event, void *ptr) 772 { 773 struct netdev_notifier_info *info = ptr; 774 struct netdev_notifier_changeupper_info *cu_info; 775 struct prestera_port *port = netdev_priv(dev); 776 struct netlink_ext_ack *extack; 777 struct net_device *upper; 778 779 extack = netdev_notifier_info_to_extack(info); 780 cu_info = container_of(info, 781 struct netdev_notifier_changeupper_info, 782 info); 783 784 switch (event) { 785 case NETDEV_PRECHANGEUPPER: 786 upper = cu_info->upper_dev; 787 if (!netif_is_bridge_master(upper) && 788 !netif_is_lag_master(upper)) { 789 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type"); 790 return -EINVAL; 791 } 792 793 if (!cu_info->linking) 794 break; 795 796 if (netdev_has_any_upper_dev(upper)) { 797 NL_SET_ERR_MSG_MOD(extack, "Upper device is already enslaved"); 798 return -EINVAL; 799 } 800 801 if (netif_is_lag_master(upper) && 802 !prestera_lag_master_check(upper, cu_info->upper_info, extack)) 803 return -EOPNOTSUPP; 804 if (netif_is_lag_master(upper) && vlan_uses_dev(dev)) { 805 NL_SET_ERR_MSG_MOD(extack, 806 "Master device is a LAG master and port has a VLAN"); 807 return -EINVAL; 808 } 809 if (netif_is_lag_port(dev) && is_vlan_dev(upper) && 810 !netif_is_lag_master(vlan_dev_real_dev(upper))) { 811 NL_SET_ERR_MSG_MOD(extack, 812 "Can not put a VLAN on a LAG port"); 813 return -EINVAL; 814 } 815 break; 816 817 case NETDEV_CHANGEUPPER: 818 upper = cu_info->upper_dev; 819 if (netif_is_bridge_master(upper)) { 820 if (cu_info->linking) 821 return prestera_bridge_port_join(upper, port, 822 extack); 823 else 824 prestera_bridge_port_leave(upper, port); 825 } else if (netif_is_lag_master(upper)) { 826 if (cu_info->linking) 827 return prestera_lag_port_add(port, upper); 828 else 829 prestera_lag_port_del(port); 830 } 831 break; 832 833 case NETDEV_CHANGELOWERSTATE: 834 return prestera_netdev_port_lower_event(dev, event, ptr); 835 } 836 837 return 0; 838 } 839 840 static int prestera_netdevice_lag_event(struct net_device *lag_dev, 841 unsigned long event, void *ptr) 842 { 843 struct net_device *dev; 844 struct list_head *iter; 845 int err; 846 847 netdev_for_each_lower_dev(lag_dev, dev, iter) { 848 if (prestera_netdev_check(dev)) { 849 err = prestera_netdev_port_event(lag_dev, dev, event, 850 ptr); 851 if (err) 852 return err; 853 } 854 } 855 856 return 0; 857 } 858 859 static int prestera_netdev_event_handler(struct notifier_block *nb, 860 unsigned long event, void *ptr) 861 { 862 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 863 int err = 0; 864 865 if (prestera_netdev_check(dev)) 866 err = prestera_netdev_port_event(dev, dev, event, ptr); 867 else if (netif_is_lag_master(dev)) 868 err = prestera_netdevice_lag_event(dev, event, ptr); 869 870 return notifier_from_errno(err); 871 } 872 873 static int prestera_netdev_event_handler_register(struct prestera_switch *sw) 874 { 875 sw->netdev_nb.notifier_call = prestera_netdev_event_handler; 876 877 return register_netdevice_notifier(&sw->netdev_nb); 878 } 879 880 static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw) 881 { 882 unregister_netdevice_notifier(&sw->netdev_nb); 883 } 884 885 static int prestera_switch_init(struct prestera_switch *sw) 886 { 887 int err; 888 889 err = prestera_hw_switch_init(sw); 890 if (err) { 891 dev_err(prestera_dev(sw), "Failed to init Switch device\n"); 892 return err; 893 } 894 895 rwlock_init(&sw->port_list_lock); 896 INIT_LIST_HEAD(&sw->port_list); 897 898 err = prestera_switch_set_base_mac_addr(sw); 899 if (err) 900 return err; 901 902 err = prestera_netdev_event_handler_register(sw); 903 if (err) 904 return err; 905 906 err = prestera_router_init(sw); 907 if (err) 908 goto err_router_init; 909 910 err = prestera_switchdev_init(sw); 911 if (err) 912 goto err_swdev_register; 913 914 err = prestera_rxtx_switch_init(sw); 915 if (err) 916 goto err_rxtx_register; 917 918 err = prestera_event_handlers_register(sw); 919 if (err) 920 goto err_handlers_register; 921 922 err = prestera_counter_init(sw); 923 if (err) 924 goto err_counter_init; 925 926 err = prestera_acl_init(sw); 927 if (err) 928 goto err_acl_init; 929 930 err = prestera_span_init(sw); 931 if (err) 932 goto err_span_init; 933 934 err = prestera_devlink_traps_register(sw); 935 if (err) 936 goto err_dl_register; 937 938 err = prestera_lag_init(sw); 939 if (err) 940 goto err_lag_init; 941 942 err = prestera_create_ports(sw); 943 if (err) 944 goto err_ports_create; 945 946 prestera_devlink_register(sw); 947 return 0; 948 949 err_ports_create: 950 prestera_lag_fini(sw); 951 err_lag_init: 952 prestera_devlink_traps_unregister(sw); 953 err_dl_register: 954 prestera_span_fini(sw); 955 err_span_init: 956 prestera_acl_fini(sw); 957 err_acl_init: 958 prestera_counter_fini(sw); 959 err_counter_init: 960 prestera_event_handlers_unregister(sw); 961 err_handlers_register: 962 prestera_rxtx_switch_fini(sw); 963 err_rxtx_register: 964 prestera_switchdev_fini(sw); 965 err_swdev_register: 966 prestera_router_fini(sw); 967 err_router_init: 968 prestera_netdev_event_handler_unregister(sw); 969 prestera_hw_switch_fini(sw); 970 971 return err; 972 } 973 974 static void prestera_switch_fini(struct prestera_switch *sw) 975 { 976 prestera_devlink_unregister(sw); 977 prestera_destroy_ports(sw); 978 prestera_lag_fini(sw); 979 prestera_devlink_traps_unregister(sw); 980 prestera_span_fini(sw); 981 prestera_acl_fini(sw); 982 prestera_counter_fini(sw); 983 prestera_event_handlers_unregister(sw); 984 prestera_rxtx_switch_fini(sw); 985 prestera_switchdev_fini(sw); 986 prestera_router_fini(sw); 987 prestera_netdev_event_handler_unregister(sw); 988 prestera_hw_switch_fini(sw); 989 } 990 991 int prestera_device_register(struct prestera_device *dev) 992 { 993 struct prestera_switch *sw; 994 int err; 995 996 sw = prestera_devlink_alloc(dev); 997 if (!sw) 998 return -ENOMEM; 999 1000 dev->priv = sw; 1001 sw->dev = dev; 1002 1003 err = prestera_switch_init(sw); 1004 if (err) { 1005 prestera_devlink_free(sw); 1006 return err; 1007 } 1008 1009 return 0; 1010 } 1011 EXPORT_SYMBOL(prestera_device_register); 1012 1013 void prestera_device_unregister(struct prestera_device *dev) 1014 { 1015 struct prestera_switch *sw = dev->priv; 1016 1017 prestera_switch_fini(sw); 1018 prestera_devlink_free(sw); 1019 } 1020 EXPORT_SYMBOL(prestera_device_unregister); 1021 1022 static int __init prestera_module_init(void) 1023 { 1024 prestera_wq = alloc_workqueue("prestera", 0, 0); 1025 if (!prestera_wq) 1026 return -ENOMEM; 1027 1028 return 0; 1029 } 1030 1031 static void __exit prestera_module_exit(void) 1032 { 1033 destroy_workqueue(prestera_wq); 1034 } 1035 1036 module_init(prestera_module_init); 1037 module_exit(prestera_module_exit); 1038 1039 MODULE_LICENSE("Dual BSD/GPL"); 1040 MODULE_DESCRIPTION("Marvell Prestera switch driver"); 1041