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