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