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 #include <linux/phylink.h> 13 14 #include "prestera.h" 15 #include "prestera_hw.h" 16 #include "prestera_acl.h" 17 #include "prestera_flow.h" 18 #include "prestera_span.h" 19 #include "prestera_rxtx.h" 20 #include "prestera_devlink.h" 21 #include "prestera_ethtool.h" 22 #include "prestera_counter.h" 23 #include "prestera_switchdev.h" 24 25 #define PRESTERA_MTU_DEFAULT 1536 26 27 #define PRESTERA_STATS_DELAY_MS 1000 28 29 #define PRESTERA_MAC_ADDR_NUM_MAX 255 30 31 static struct workqueue_struct *prestera_wq; 32 static struct workqueue_struct *prestera_owq; 33 34 void prestera_queue_work(struct work_struct *work) 35 { 36 queue_work(prestera_owq, work); 37 } 38 39 void prestera_queue_delayed_work(struct delayed_work *work, unsigned long delay) 40 { 41 queue_delayed_work(prestera_wq, work, delay); 42 } 43 44 void prestera_queue_drain(void) 45 { 46 drain_workqueue(prestera_wq); 47 drain_workqueue(prestera_owq); 48 } 49 50 int prestera_port_learning_set(struct prestera_port *port, bool learn) 51 { 52 return prestera_hw_port_learning_set(port, learn); 53 } 54 55 int prestera_port_uc_flood_set(struct prestera_port *port, bool flood) 56 { 57 return prestera_hw_port_uc_flood_set(port, flood); 58 } 59 60 int prestera_port_mc_flood_set(struct prestera_port *port, bool flood) 61 { 62 return prestera_hw_port_mc_flood_set(port, flood); 63 } 64 65 int prestera_port_br_locked_set(struct prestera_port *port, bool br_locked) 66 { 67 return prestera_hw_port_br_locked_set(port, br_locked); 68 } 69 70 int prestera_port_pvid_set(struct prestera_port *port, u16 vid) 71 { 72 enum prestera_accept_frm_type frm_type; 73 int err; 74 75 frm_type = PRESTERA_ACCEPT_FRAME_TYPE_TAGGED; 76 77 if (vid) { 78 err = prestera_hw_vlan_port_vid_set(port, vid); 79 if (err) 80 return err; 81 82 frm_type = PRESTERA_ACCEPT_FRAME_TYPE_ALL; 83 } 84 85 err = prestera_hw_port_accept_frm_type(port, frm_type); 86 if (err && frm_type == PRESTERA_ACCEPT_FRAME_TYPE_ALL) 87 prestera_hw_vlan_port_vid_set(port, port->pvid); 88 89 port->pvid = vid; 90 return 0; 91 } 92 93 struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw, 94 u32 dev_id, u32 hw_id) 95 { 96 struct prestera_port *port = NULL, *tmp; 97 98 read_lock(&sw->port_list_lock); 99 list_for_each_entry(tmp, &sw->port_list, list) { 100 if (tmp->dev_id == dev_id && tmp->hw_id == hw_id) { 101 port = tmp; 102 break; 103 } 104 } 105 read_unlock(&sw->port_list_lock); 106 107 return port; 108 } 109 110 struct prestera_port *prestera_find_port(struct prestera_switch *sw, u32 id) 111 { 112 struct prestera_port *port = NULL, *tmp; 113 114 read_lock(&sw->port_list_lock); 115 list_for_each_entry(tmp, &sw->port_list, list) { 116 if (tmp->id == id) { 117 port = tmp; 118 break; 119 } 120 } 121 read_unlock(&sw->port_list_lock); 122 123 return port; 124 } 125 126 struct prestera_switch *prestera_switch_get(struct net_device *dev) 127 { 128 struct prestera_port *port; 129 130 port = prestera_port_dev_lower_find(dev); 131 return port ? port->sw : NULL; 132 } 133 134 int prestera_port_cfg_mac_read(struct prestera_port *port, 135 struct prestera_port_mac_config *cfg) 136 { 137 *cfg = port->cfg_mac; 138 return 0; 139 } 140 141 int prestera_port_cfg_mac_write(struct prestera_port *port, 142 struct prestera_port_mac_config *cfg) 143 { 144 int err; 145 146 err = prestera_hw_port_mac_mode_set(port, cfg->admin, 147 cfg->mode, cfg->inband, cfg->speed, 148 cfg->duplex, cfg->fec); 149 if (err) 150 return err; 151 152 port->cfg_mac = *cfg; 153 return 0; 154 } 155 156 static int prestera_port_open(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 if (port->phy_link) { 163 phylink_start(port->phy_link); 164 } else { 165 if (port->caps.transceiver == PRESTERA_PORT_TCVR_SFP) { 166 err = prestera_port_cfg_mac_read(port, &cfg_mac); 167 if (!err) { 168 cfg_mac.admin = true; 169 err = prestera_port_cfg_mac_write(port, 170 &cfg_mac); 171 } 172 } else { 173 port->cfg_phy.admin = true; 174 err = prestera_hw_port_phy_mode_set(port, true, 175 port->autoneg, 176 port->cfg_phy.mode, 177 port->adver_link_modes, 178 port->cfg_phy.mdix); 179 } 180 } 181 182 netif_start_queue(dev); 183 184 return err; 185 } 186 187 static int prestera_port_close(struct net_device *dev) 188 { 189 struct prestera_port *port = netdev_priv(dev); 190 struct prestera_port_mac_config cfg_mac; 191 int err = 0; 192 193 netif_stop_queue(dev); 194 195 if (port->phy_link) { 196 phylink_stop(port->phy_link); 197 phylink_disconnect_phy(port->phy_link); 198 err = prestera_port_cfg_mac_read(port, &cfg_mac); 199 if (!err) { 200 cfg_mac.admin = false; 201 prestera_port_cfg_mac_write(port, &cfg_mac); 202 } 203 } else { 204 if (port->caps.transceiver == PRESTERA_PORT_TCVR_SFP) { 205 err = prestera_port_cfg_mac_read(port, &cfg_mac); 206 if (!err) { 207 cfg_mac.admin = false; 208 prestera_port_cfg_mac_write(port, &cfg_mac); 209 } 210 } else { 211 port->cfg_phy.admin = false; 212 err = prestera_hw_port_phy_mode_set(port, false, port->autoneg, 213 port->cfg_phy.mode, 214 port->adver_link_modes, 215 port->cfg_phy.mdix); 216 } 217 } 218 219 return err; 220 } 221 222 static void 223 prestera_port_mac_state_cache_read(struct prestera_port *port, 224 struct prestera_port_mac_state *state) 225 { 226 spin_lock(&port->state_mac_lock); 227 *state = port->state_mac; 228 spin_unlock(&port->state_mac_lock); 229 } 230 231 static void 232 prestera_port_mac_state_cache_write(struct prestera_port *port, 233 struct prestera_port_mac_state *state) 234 { 235 spin_lock(&port->state_mac_lock); 236 port->state_mac = *state; 237 spin_unlock(&port->state_mac_lock); 238 } 239 240 static struct prestera_port *prestera_pcs_to_port(struct phylink_pcs *pcs) 241 { 242 return container_of(pcs, struct prestera_port, phylink_pcs); 243 } 244 245 static void prestera_mac_config(struct phylink_config *config, 246 unsigned int an_mode, 247 const struct phylink_link_state *state) 248 { 249 } 250 251 static void prestera_mac_link_down(struct phylink_config *config, 252 unsigned int mode, phy_interface_t interface) 253 { 254 struct net_device *ndev = to_net_dev(config->dev); 255 struct prestera_port *port = netdev_priv(ndev); 256 struct prestera_port_mac_state state_mac; 257 258 /* Invalidate. Parameters will update on next link event. */ 259 memset(&state_mac, 0, sizeof(state_mac)); 260 state_mac.valid = false; 261 prestera_port_mac_state_cache_write(port, &state_mac); 262 } 263 264 static void prestera_mac_link_up(struct phylink_config *config, 265 struct phy_device *phy, 266 unsigned int mode, phy_interface_t interface, 267 int speed, int duplex, 268 bool tx_pause, bool rx_pause) 269 { 270 } 271 272 static struct phylink_pcs * 273 prestera_mac_select_pcs(struct phylink_config *config, 274 phy_interface_t interface) 275 { 276 struct net_device *dev = to_net_dev(config->dev); 277 struct prestera_port *port = netdev_priv(dev); 278 279 return &port->phylink_pcs; 280 } 281 282 static void prestera_pcs_get_state(struct phylink_pcs *pcs, 283 struct phylink_link_state *state) 284 { 285 struct prestera_port *port = container_of(pcs, struct prestera_port, 286 phylink_pcs); 287 struct prestera_port_mac_state smac; 288 289 prestera_port_mac_state_cache_read(port, &smac); 290 291 if (smac.valid) { 292 state->link = smac.oper ? 1 : 0; 293 /* AN is completed, when port is up */ 294 state->an_complete = (smac.oper && port->autoneg) ? 1 : 0; 295 state->speed = smac.speed; 296 state->duplex = smac.duplex; 297 } else { 298 state->link = 0; 299 state->an_complete = 0; 300 } 301 } 302 303 static int prestera_pcs_config(struct phylink_pcs *pcs, 304 unsigned int mode, 305 phy_interface_t interface, 306 const unsigned long *advertising, 307 bool permit_pause_to_mac) 308 { 309 struct prestera_port *port = prestera_pcs_to_port(pcs); 310 struct prestera_port_mac_config cfg_mac; 311 int err; 312 313 err = prestera_port_cfg_mac_read(port, &cfg_mac); 314 if (err) 315 return err; 316 317 cfg_mac.admin = true; 318 cfg_mac.fec = PRESTERA_PORT_FEC_OFF; 319 320 switch (interface) { 321 case PHY_INTERFACE_MODE_10GBASER: 322 cfg_mac.speed = SPEED_10000; 323 cfg_mac.inband = 0; 324 cfg_mac.mode = PRESTERA_MAC_MODE_SR_LR; 325 break; 326 case PHY_INTERFACE_MODE_2500BASEX: 327 cfg_mac.speed = SPEED_2500; 328 cfg_mac.duplex = DUPLEX_FULL; 329 cfg_mac.inband = test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 330 advertising); 331 cfg_mac.mode = PRESTERA_MAC_MODE_SGMII; 332 break; 333 case PHY_INTERFACE_MODE_SGMII: 334 cfg_mac.inband = 1; 335 cfg_mac.mode = PRESTERA_MAC_MODE_SGMII; 336 break; 337 case PHY_INTERFACE_MODE_1000BASEX: 338 default: 339 cfg_mac.speed = SPEED_1000; 340 cfg_mac.duplex = DUPLEX_FULL; 341 cfg_mac.inband = test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 342 advertising); 343 cfg_mac.mode = PRESTERA_MAC_MODE_1000BASE_X; 344 break; 345 } 346 347 err = prestera_port_cfg_mac_write(port, &cfg_mac); 348 if (err) 349 return err; 350 351 return 0; 352 } 353 354 static void prestera_pcs_an_restart(struct phylink_pcs *pcs) 355 { 356 /* TODO: add 1000basex AN restart support 357 * (Currently FW has no support for 1000baseX AN restart, but it will in the future, 358 * so as for now the function would stay empty.) 359 */ 360 } 361 362 static const struct phylink_mac_ops prestera_mac_ops = { 363 .validate = phylink_generic_validate, 364 .mac_select_pcs = prestera_mac_select_pcs, 365 .mac_config = prestera_mac_config, 366 .mac_link_down = prestera_mac_link_down, 367 .mac_link_up = prestera_mac_link_up, 368 }; 369 370 static const struct phylink_pcs_ops prestera_pcs_ops = { 371 .pcs_get_state = prestera_pcs_get_state, 372 .pcs_config = prestera_pcs_config, 373 .pcs_an_restart = prestera_pcs_an_restart, 374 }; 375 376 static int prestera_port_sfp_bind(struct prestera_port *port) 377 { 378 struct prestera_switch *sw = port->sw; 379 struct device_node *ports, *node; 380 struct fwnode_handle *fwnode; 381 struct phylink *phy_link; 382 int err; 383 384 if (!sw->np) 385 return 0; 386 387 of_node_get(sw->np); 388 ports = of_find_node_by_name(sw->np, "ports"); 389 390 for_each_child_of_node(ports, node) { 391 int num; 392 393 err = of_property_read_u32(node, "prestera,port-num", &num); 394 if (err) { 395 dev_err(sw->dev->dev, 396 "device node %pOF has no valid reg property: %d\n", 397 node, err); 398 goto out; 399 } 400 401 if (port->fp_id != num) 402 continue; 403 404 port->phylink_pcs.ops = &prestera_pcs_ops; 405 406 port->phy_config.dev = &port->dev->dev; 407 port->phy_config.type = PHYLINK_NETDEV; 408 409 fwnode = of_fwnode_handle(node); 410 411 __set_bit(PHY_INTERFACE_MODE_10GBASER, 412 port->phy_config.supported_interfaces); 413 __set_bit(PHY_INTERFACE_MODE_2500BASEX, 414 port->phy_config.supported_interfaces); 415 __set_bit(PHY_INTERFACE_MODE_SGMII, 416 port->phy_config.supported_interfaces); 417 __set_bit(PHY_INTERFACE_MODE_1000BASEX, 418 port->phy_config.supported_interfaces); 419 420 port->phy_config.mac_capabilities = 421 MAC_1000 | MAC_2500FD | MAC_10000FD; 422 423 phy_link = phylink_create(&port->phy_config, fwnode, 424 PHY_INTERFACE_MODE_INTERNAL, 425 &prestera_mac_ops); 426 if (IS_ERR(phy_link)) { 427 netdev_err(port->dev, "failed to create phylink\n"); 428 err = PTR_ERR(phy_link); 429 goto out; 430 } 431 432 port->phy_link = phy_link; 433 break; 434 } 435 436 out: 437 of_node_put(node); 438 of_node_put(ports); 439 return err; 440 } 441 442 static int prestera_port_sfp_unbind(struct prestera_port *port) 443 { 444 if (port->phy_link) 445 phylink_destroy(port->phy_link); 446 447 return 0; 448 } 449 450 static netdev_tx_t prestera_port_xmit(struct sk_buff *skb, 451 struct net_device *dev) 452 { 453 return prestera_rxtx_xmit(netdev_priv(dev), skb); 454 } 455 456 int prestera_is_valid_mac_addr(struct prestera_port *port, const u8 *addr) 457 { 458 if (!is_valid_ether_addr(addr)) 459 return -EADDRNOTAVAIL; 460 461 /* firmware requires that port's MAC address contains first 5 bytes 462 * of the base MAC address 463 */ 464 if (memcmp(port->sw->base_mac, addr, ETH_ALEN - 1)) 465 return -EINVAL; 466 467 return 0; 468 } 469 470 static int prestera_port_set_mac_address(struct net_device *dev, void *p) 471 { 472 struct prestera_port *port = netdev_priv(dev); 473 struct sockaddr *addr = p; 474 int err; 475 476 err = prestera_is_valid_mac_addr(port, addr->sa_data); 477 if (err) 478 return err; 479 480 err = prestera_hw_port_mac_set(port, addr->sa_data); 481 if (err) 482 return err; 483 484 eth_hw_addr_set(dev, addr->sa_data); 485 486 return 0; 487 } 488 489 static int prestera_port_change_mtu(struct net_device *dev, int mtu) 490 { 491 struct prestera_port *port = netdev_priv(dev); 492 int err; 493 494 err = prestera_hw_port_mtu_set(port, mtu); 495 if (err) 496 return err; 497 498 dev->mtu = mtu; 499 500 return 0; 501 } 502 503 static void prestera_port_get_stats64(struct net_device *dev, 504 struct rtnl_link_stats64 *stats) 505 { 506 struct prestera_port *port = netdev_priv(dev); 507 struct prestera_port_stats *port_stats = &port->cached_hw_stats.stats; 508 509 stats->rx_packets = port_stats->broadcast_frames_received + 510 port_stats->multicast_frames_received + 511 port_stats->unicast_frames_received; 512 513 stats->tx_packets = port_stats->broadcast_frames_sent + 514 port_stats->multicast_frames_sent + 515 port_stats->unicast_frames_sent; 516 517 stats->rx_bytes = port_stats->good_octets_received; 518 519 stats->tx_bytes = port_stats->good_octets_sent; 520 521 stats->rx_errors = port_stats->rx_error_frame_received; 522 stats->tx_errors = port_stats->mac_trans_error; 523 524 stats->rx_dropped = port_stats->buffer_overrun; 525 stats->tx_dropped = 0; 526 527 stats->multicast = port_stats->multicast_frames_received; 528 stats->collisions = port_stats->excessive_collision; 529 530 stats->rx_crc_errors = port_stats->bad_crc; 531 } 532 533 static void prestera_port_get_hw_stats(struct prestera_port *port) 534 { 535 prestera_hw_port_stats_get(port, &port->cached_hw_stats.stats); 536 } 537 538 static void prestera_port_stats_update(struct work_struct *work) 539 { 540 struct prestera_port *port = 541 container_of(work, struct prestera_port, 542 cached_hw_stats.caching_dw.work); 543 544 prestera_port_get_hw_stats(port); 545 546 queue_delayed_work(prestera_wq, &port->cached_hw_stats.caching_dw, 547 msecs_to_jiffies(PRESTERA_STATS_DELAY_MS)); 548 } 549 550 static int prestera_port_setup_tc(struct net_device *dev, 551 enum tc_setup_type type, 552 void *type_data) 553 { 554 struct prestera_port *port = netdev_priv(dev); 555 556 switch (type) { 557 case TC_SETUP_BLOCK: 558 return prestera_flow_block_setup(port, type_data); 559 default: 560 return -EOPNOTSUPP; 561 } 562 } 563 564 static const struct net_device_ops prestera_netdev_ops = { 565 .ndo_open = prestera_port_open, 566 .ndo_stop = prestera_port_close, 567 .ndo_start_xmit = prestera_port_xmit, 568 .ndo_setup_tc = prestera_port_setup_tc, 569 .ndo_change_mtu = prestera_port_change_mtu, 570 .ndo_get_stats64 = prestera_port_get_stats64, 571 .ndo_set_mac_address = prestera_port_set_mac_address, 572 }; 573 574 int prestera_port_autoneg_set(struct prestera_port *port, u64 link_modes) 575 { 576 int err; 577 578 if (port->autoneg && port->adver_link_modes == link_modes) 579 return 0; 580 581 err = prestera_hw_port_phy_mode_set(port, port->cfg_phy.admin, 582 true, 0, link_modes, 583 port->cfg_phy.mdix); 584 if (err) 585 return err; 586 587 port->adver_fec = BIT(PRESTERA_PORT_FEC_OFF); 588 port->adver_link_modes = link_modes; 589 port->cfg_phy.mode = 0; 590 port->autoneg = true; 591 592 return 0; 593 } 594 595 static void prestera_port_list_add(struct prestera_port *port) 596 { 597 write_lock(&port->sw->port_list_lock); 598 list_add(&port->list, &port->sw->port_list); 599 write_unlock(&port->sw->port_list_lock); 600 } 601 602 static void prestera_port_list_del(struct prestera_port *port) 603 { 604 write_lock(&port->sw->port_list_lock); 605 list_del(&port->list); 606 write_unlock(&port->sw->port_list_lock); 607 } 608 609 static int prestera_port_create(struct prestera_switch *sw, u32 id) 610 { 611 struct prestera_port_mac_config cfg_mac; 612 struct prestera_port *port; 613 struct net_device *dev; 614 int err; 615 616 dev = alloc_etherdev(sizeof(*port)); 617 if (!dev) 618 return -ENOMEM; 619 620 port = netdev_priv(dev); 621 622 INIT_LIST_HEAD(&port->vlans_list); 623 port->pvid = PRESTERA_DEFAULT_VID; 624 port->lag = NULL; 625 port->dev = dev; 626 port->id = id; 627 port->sw = sw; 628 629 spin_lock_init(&port->state_mac_lock); 630 631 err = prestera_hw_port_info_get(port, &port->dev_id, &port->hw_id, 632 &port->fp_id); 633 if (err) { 634 dev_err(prestera_dev(sw), "Failed to get port(%u) info\n", id); 635 goto err_port_info_get; 636 } 637 638 err = prestera_devlink_port_register(port); 639 if (err) 640 goto err_dl_port_register; 641 642 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_HW_TC; 643 dev->netdev_ops = &prestera_netdev_ops; 644 dev->ethtool_ops = &prestera_ethtool_ops; 645 SET_NETDEV_DEV(dev, sw->dev->dev); 646 SET_NETDEV_DEVLINK_PORT(dev, &port->dl_port); 647 648 if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP) 649 netif_carrier_off(dev); 650 651 dev->mtu = min_t(unsigned int, sw->mtu_max, PRESTERA_MTU_DEFAULT); 652 dev->min_mtu = sw->mtu_min; 653 dev->max_mtu = sw->mtu_max; 654 655 err = prestera_hw_port_mtu_set(port, dev->mtu); 656 if (err) { 657 dev_err(prestera_dev(sw), "Failed to set port(%u) mtu(%d)\n", 658 id, dev->mtu); 659 goto err_port_init; 660 } 661 662 if (port->fp_id >= PRESTERA_MAC_ADDR_NUM_MAX) { 663 err = -EINVAL; 664 goto err_port_init; 665 } 666 667 eth_hw_addr_gen(dev, sw->base_mac, port->fp_id); 668 /* firmware requires that port's MAC address consist of the first 669 * 5 bytes of the base MAC address 670 */ 671 if (memcmp(dev->dev_addr, sw->base_mac, ETH_ALEN - 1)) { 672 dev_warn(prestera_dev(sw), "Port MAC address wraps for port(%u)\n", id); 673 dev_addr_mod(dev, 0, sw->base_mac, ETH_ALEN - 1); 674 } 675 676 err = prestera_hw_port_mac_set(port, dev->dev_addr); 677 if (err) { 678 dev_err(prestera_dev(sw), "Failed to set port(%u) mac addr\n", id); 679 goto err_port_init; 680 } 681 682 err = prestera_hw_port_cap_get(port, &port->caps); 683 if (err) { 684 dev_err(prestera_dev(sw), "Failed to get port(%u) caps\n", id); 685 goto err_port_init; 686 } 687 688 port->adver_link_modes = port->caps.supp_link_modes; 689 port->adver_fec = 0; 690 port->autoneg = true; 691 692 /* initialize config mac */ 693 if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP) { 694 cfg_mac.admin = true; 695 cfg_mac.mode = PRESTERA_MAC_MODE_INTERNAL; 696 } else { 697 cfg_mac.admin = false; 698 cfg_mac.mode = PRESTERA_MAC_MODE_MAX; 699 } 700 cfg_mac.inband = 0; 701 cfg_mac.speed = 0; 702 cfg_mac.duplex = DUPLEX_UNKNOWN; 703 cfg_mac.fec = PRESTERA_PORT_FEC_OFF; 704 705 err = prestera_port_cfg_mac_write(port, &cfg_mac); 706 if (err) { 707 dev_err(prestera_dev(sw), 708 "Failed to set port(%u) mac mode\n", id); 709 goto err_port_init; 710 } 711 712 /* initialize config phy (if this is inegral) */ 713 if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP) { 714 port->cfg_phy.mdix = ETH_TP_MDI_AUTO; 715 port->cfg_phy.admin = false; 716 err = prestera_hw_port_phy_mode_set(port, 717 port->cfg_phy.admin, 718 false, 0, 0, 719 port->cfg_phy.mdix); 720 if (err) { 721 dev_err(prestera_dev(sw), 722 "Failed to set port(%u) phy mode\n", id); 723 goto err_port_init; 724 } 725 } 726 727 err = prestera_rxtx_port_init(port); 728 if (err) 729 goto err_port_init; 730 731 INIT_DELAYED_WORK(&port->cached_hw_stats.caching_dw, 732 &prestera_port_stats_update); 733 734 prestera_port_list_add(port); 735 736 err = register_netdev(dev); 737 if (err) 738 goto err_register_netdev; 739 740 err = prestera_port_sfp_bind(port); 741 if (err) 742 goto err_sfp_bind; 743 744 return 0; 745 746 err_sfp_bind: 747 err_register_netdev: 748 prestera_port_list_del(port); 749 err_port_init: 750 prestera_devlink_port_unregister(port); 751 err_dl_port_register: 752 err_port_info_get: 753 free_netdev(dev); 754 return err; 755 } 756 757 static void prestera_port_destroy(struct prestera_port *port) 758 { 759 struct net_device *dev = port->dev; 760 761 cancel_delayed_work_sync(&port->cached_hw_stats.caching_dw); 762 unregister_netdev(dev); 763 prestera_port_list_del(port); 764 prestera_devlink_port_unregister(port); 765 free_netdev(dev); 766 } 767 768 static void prestera_destroy_ports(struct prestera_switch *sw) 769 { 770 struct prestera_port *port, *tmp; 771 772 list_for_each_entry_safe(port, tmp, &sw->port_list, list) 773 prestera_port_destroy(port); 774 } 775 776 static int prestera_create_ports(struct prestera_switch *sw) 777 { 778 struct prestera_port *port, *tmp; 779 u32 port_idx; 780 int err; 781 782 for (port_idx = 0; port_idx < sw->port_count; port_idx++) { 783 err = prestera_port_create(sw, port_idx); 784 if (err) 785 goto err_port_create; 786 } 787 788 return 0; 789 790 err_port_create: 791 list_for_each_entry_safe(port, tmp, &sw->port_list, list) { 792 prestera_port_sfp_unbind(port); 793 prestera_port_destroy(port); 794 } 795 796 return err; 797 } 798 799 static void prestera_port_handle_event(struct prestera_switch *sw, 800 struct prestera_event *evt, void *arg) 801 { 802 struct prestera_port_mac_state smac; 803 struct prestera_port_event *pevt; 804 struct delayed_work *caching_dw; 805 struct prestera_port *port; 806 807 if (evt->id == PRESTERA_PORT_EVENT_MAC_STATE_CHANGED) { 808 pevt = &evt->port_evt; 809 port = prestera_find_port(sw, pevt->port_id); 810 if (!port || !port->dev) 811 return; 812 813 caching_dw = &port->cached_hw_stats.caching_dw; 814 815 memset(&smac, 0, sizeof(smac)); 816 smac.valid = true; 817 smac.oper = pevt->data.mac.oper; 818 if (smac.oper) { 819 smac.mode = pevt->data.mac.mode; 820 smac.speed = pevt->data.mac.speed; 821 smac.duplex = pevt->data.mac.duplex; 822 smac.fc = pevt->data.mac.fc; 823 smac.fec = pevt->data.mac.fec; 824 } 825 prestera_port_mac_state_cache_write(port, &smac); 826 827 if (port->state_mac.oper) { 828 if (port->phy_link) 829 phylink_mac_change(port->phy_link, true); 830 else 831 netif_carrier_on(port->dev); 832 833 if (!delayed_work_pending(caching_dw)) 834 queue_delayed_work(prestera_wq, caching_dw, 0); 835 } else { 836 if (port->phy_link) 837 phylink_mac_change(port->phy_link, false); 838 else if (netif_running(port->dev) && netif_carrier_ok(port->dev)) 839 netif_carrier_off(port->dev); 840 841 if (delayed_work_pending(caching_dw)) 842 cancel_delayed_work(caching_dw); 843 } 844 } 845 } 846 847 static int prestera_event_handlers_register(struct prestera_switch *sw) 848 { 849 return prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_PORT, 850 prestera_port_handle_event, 851 NULL); 852 } 853 854 static void prestera_event_handlers_unregister(struct prestera_switch *sw) 855 { 856 prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_PORT, 857 prestera_port_handle_event); 858 } 859 860 static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw) 861 { 862 struct device_node *base_mac_np; 863 int ret = 0; 864 865 if (sw->np) { 866 base_mac_np = of_parse_phandle(sw->np, "base-mac-provider", 0); 867 if (base_mac_np) { 868 ret = of_get_mac_address(base_mac_np, sw->base_mac); 869 of_node_put(base_mac_np); 870 } 871 } 872 873 if (!is_valid_ether_addr(sw->base_mac) || ret) { 874 eth_random_addr(sw->base_mac); 875 dev_info(prestera_dev(sw), "using random base mac address\n"); 876 } 877 878 return prestera_hw_switch_mac_set(sw, sw->base_mac); 879 } 880 881 struct prestera_lag *prestera_lag_by_id(struct prestera_switch *sw, u16 id) 882 { 883 return id < sw->lag_max ? &sw->lags[id] : NULL; 884 } 885 886 static struct prestera_lag *prestera_lag_by_dev(struct prestera_switch *sw, 887 struct net_device *dev) 888 { 889 struct prestera_lag *lag; 890 u16 id; 891 892 for (id = 0; id < sw->lag_max; id++) { 893 lag = &sw->lags[id]; 894 if (lag->dev == dev) 895 return lag; 896 } 897 898 return NULL; 899 } 900 901 int prestera_lag_id(struct prestera_switch *sw, 902 struct net_device *lag_dev, u16 *lag_id) 903 { 904 struct prestera_lag *lag; 905 int free_id = -1; 906 int id; 907 908 for (id = 0; id < sw->lag_max; id++) { 909 lag = prestera_lag_by_id(sw, id); 910 if (lag->member_count) { 911 if (lag->dev == lag_dev) { 912 *lag_id = id; 913 return 0; 914 } 915 } else if (free_id < 0) { 916 free_id = id; 917 } 918 } 919 if (free_id < 0) 920 return -ENOSPC; 921 *lag_id = free_id; 922 return 0; 923 } 924 925 static struct prestera_lag *prestera_lag_create(struct prestera_switch *sw, 926 struct net_device *lag_dev) 927 { 928 struct prestera_lag *lag = NULL; 929 u16 id; 930 931 for (id = 0; id < sw->lag_max; id++) { 932 lag = &sw->lags[id]; 933 if (!lag->dev) 934 break; 935 } 936 if (lag) { 937 INIT_LIST_HEAD(&lag->members); 938 lag->dev = lag_dev; 939 } 940 941 return lag; 942 } 943 944 static void prestera_lag_destroy(struct prestera_switch *sw, 945 struct prestera_lag *lag) 946 { 947 WARN_ON(!list_empty(&lag->members)); 948 lag->member_count = 0; 949 lag->dev = NULL; 950 } 951 952 static int prestera_lag_port_add(struct prestera_port *port, 953 struct net_device *lag_dev) 954 { 955 struct prestera_switch *sw = port->sw; 956 struct prestera_lag *lag; 957 int err; 958 959 lag = prestera_lag_by_dev(sw, lag_dev); 960 if (!lag) { 961 lag = prestera_lag_create(sw, lag_dev); 962 if (!lag) 963 return -ENOSPC; 964 } 965 966 if (lag->member_count >= sw->lag_member_max) 967 return -ENOSPC; 968 969 err = prestera_hw_lag_member_add(port, lag->lag_id); 970 if (err) { 971 if (!lag->member_count) 972 prestera_lag_destroy(sw, lag); 973 return err; 974 } 975 976 list_add(&port->lag_member, &lag->members); 977 lag->member_count++; 978 port->lag = lag; 979 980 return 0; 981 } 982 983 static int prestera_lag_port_del(struct prestera_port *port) 984 { 985 struct prestera_switch *sw = port->sw; 986 struct prestera_lag *lag = port->lag; 987 int err; 988 989 if (!lag || !lag->member_count) 990 return -EINVAL; 991 992 err = prestera_hw_lag_member_del(port, lag->lag_id); 993 if (err) 994 return err; 995 996 list_del(&port->lag_member); 997 lag->member_count--; 998 port->lag = NULL; 999 1000 if (netif_is_bridge_port(lag->dev)) { 1001 struct net_device *br_dev; 1002 1003 br_dev = netdev_master_upper_dev_get(lag->dev); 1004 1005 prestera_bridge_port_leave(br_dev, port); 1006 } 1007 1008 if (!lag->member_count) 1009 prestera_lag_destroy(sw, lag); 1010 1011 return 0; 1012 } 1013 1014 bool prestera_port_is_lag_member(const struct prestera_port *port) 1015 { 1016 return !!port->lag; 1017 } 1018 1019 u16 prestera_port_lag_id(const struct prestera_port *port) 1020 { 1021 return port->lag->lag_id; 1022 } 1023 1024 static int prestera_lag_init(struct prestera_switch *sw) 1025 { 1026 u16 id; 1027 1028 sw->lags = kcalloc(sw->lag_max, sizeof(*sw->lags), GFP_KERNEL); 1029 if (!sw->lags) 1030 return -ENOMEM; 1031 1032 for (id = 0; id < sw->lag_max; id++) 1033 sw->lags[id].lag_id = id; 1034 1035 return 0; 1036 } 1037 1038 static void prestera_lag_fini(struct prestera_switch *sw) 1039 { 1040 u8 idx; 1041 1042 for (idx = 0; idx < sw->lag_max; idx++) 1043 WARN_ON(sw->lags[idx].member_count); 1044 1045 kfree(sw->lags); 1046 } 1047 1048 bool prestera_netdev_check(const struct net_device *dev) 1049 { 1050 return dev->netdev_ops == &prestera_netdev_ops; 1051 } 1052 1053 static int prestera_lower_dev_walk(struct net_device *dev, 1054 struct netdev_nested_priv *priv) 1055 { 1056 struct prestera_port **pport = (struct prestera_port **)priv->data; 1057 1058 if (prestera_netdev_check(dev)) { 1059 *pport = netdev_priv(dev); 1060 return 1; 1061 } 1062 1063 return 0; 1064 } 1065 1066 struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev) 1067 { 1068 struct prestera_port *port = NULL; 1069 struct netdev_nested_priv priv = { 1070 .data = (void *)&port, 1071 }; 1072 1073 if (prestera_netdev_check(dev)) 1074 return netdev_priv(dev); 1075 1076 netdev_walk_all_lower_dev(dev, prestera_lower_dev_walk, &priv); 1077 1078 return port; 1079 } 1080 1081 static int prestera_netdev_port_lower_event(struct net_device *dev, 1082 unsigned long event, void *ptr) 1083 { 1084 struct netdev_notifier_changelowerstate_info *info = ptr; 1085 struct netdev_lag_lower_state_info *lower_state_info; 1086 struct prestera_port *port = netdev_priv(dev); 1087 bool enabled; 1088 1089 if (!netif_is_lag_port(dev)) 1090 return 0; 1091 if (!prestera_port_is_lag_member(port)) 1092 return 0; 1093 1094 lower_state_info = info->lower_state_info; 1095 enabled = lower_state_info->link_up && lower_state_info->tx_enabled; 1096 1097 return prestera_hw_lag_member_enable(port, port->lag->lag_id, enabled); 1098 } 1099 1100 static bool prestera_lag_master_check(struct net_device *lag_dev, 1101 struct netdev_lag_upper_info *info, 1102 struct netlink_ext_ack *ext_ack) 1103 { 1104 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) { 1105 NL_SET_ERR_MSG_MOD(ext_ack, "Unsupported LAG Tx type"); 1106 return false; 1107 } 1108 1109 return true; 1110 } 1111 1112 static int prestera_netdev_port_event(struct net_device *lower, 1113 struct net_device *dev, 1114 unsigned long event, void *ptr) 1115 { 1116 struct netdev_notifier_info *info = ptr; 1117 struct netdev_notifier_changeupper_info *cu_info; 1118 struct prestera_port *port = netdev_priv(dev); 1119 struct netlink_ext_ack *extack; 1120 struct net_device *upper; 1121 1122 extack = netdev_notifier_info_to_extack(info); 1123 cu_info = container_of(info, 1124 struct netdev_notifier_changeupper_info, 1125 info); 1126 1127 switch (event) { 1128 case NETDEV_PRECHANGEUPPER: 1129 upper = cu_info->upper_dev; 1130 if (!netif_is_bridge_master(upper) && 1131 !netif_is_lag_master(upper)) { 1132 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type"); 1133 return -EINVAL; 1134 } 1135 1136 if (!cu_info->linking) 1137 break; 1138 1139 if (netdev_has_any_upper_dev(upper)) { 1140 NL_SET_ERR_MSG_MOD(extack, "Upper device is already enslaved"); 1141 return -EINVAL; 1142 } 1143 1144 if (netif_is_lag_master(upper) && 1145 !prestera_lag_master_check(upper, cu_info->upper_info, extack)) 1146 return -EOPNOTSUPP; 1147 if (netif_is_lag_master(upper) && vlan_uses_dev(dev)) { 1148 NL_SET_ERR_MSG_MOD(extack, 1149 "Master device is a LAG master and port has a VLAN"); 1150 return -EINVAL; 1151 } 1152 if (netif_is_lag_port(dev) && is_vlan_dev(upper) && 1153 !netif_is_lag_master(vlan_dev_real_dev(upper))) { 1154 NL_SET_ERR_MSG_MOD(extack, 1155 "Can not put a VLAN on a LAG port"); 1156 return -EINVAL; 1157 } 1158 break; 1159 1160 case NETDEV_CHANGEUPPER: 1161 upper = cu_info->upper_dev; 1162 if (netif_is_bridge_master(upper)) { 1163 if (cu_info->linking) 1164 return prestera_bridge_port_join(upper, port, 1165 extack); 1166 else 1167 prestera_bridge_port_leave(upper, port); 1168 } else if (netif_is_lag_master(upper)) { 1169 if (cu_info->linking) 1170 return prestera_lag_port_add(port, upper); 1171 else 1172 prestera_lag_port_del(port); 1173 } 1174 break; 1175 1176 case NETDEV_CHANGELOWERSTATE: 1177 return prestera_netdev_port_lower_event(dev, event, ptr); 1178 } 1179 1180 return 0; 1181 } 1182 1183 static int prestera_netdevice_lag_event(struct net_device *lag_dev, 1184 unsigned long event, void *ptr) 1185 { 1186 struct net_device *dev; 1187 struct list_head *iter; 1188 int err; 1189 1190 netdev_for_each_lower_dev(lag_dev, dev, iter) { 1191 if (prestera_netdev_check(dev)) { 1192 err = prestera_netdev_port_event(lag_dev, dev, event, 1193 ptr); 1194 if (err) 1195 return err; 1196 } 1197 } 1198 1199 return 0; 1200 } 1201 1202 static int prestera_netdev_event_handler(struct notifier_block *nb, 1203 unsigned long event, void *ptr) 1204 { 1205 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1206 int err = 0; 1207 1208 if (prestera_netdev_check(dev)) 1209 err = prestera_netdev_port_event(dev, dev, event, ptr); 1210 else if (netif_is_lag_master(dev)) 1211 err = prestera_netdevice_lag_event(dev, event, ptr); 1212 1213 return notifier_from_errno(err); 1214 } 1215 1216 struct prestera_mdb_entry * 1217 prestera_mdb_entry_create(struct prestera_switch *sw, 1218 const unsigned char *addr, u16 vid) 1219 { 1220 struct prestera_flood_domain *flood_domain; 1221 struct prestera_mdb_entry *mdb_entry; 1222 1223 mdb_entry = kzalloc(sizeof(*mdb_entry), GFP_KERNEL); 1224 if (!mdb_entry) 1225 goto err_mdb_alloc; 1226 1227 flood_domain = prestera_flood_domain_create(sw); 1228 if (!flood_domain) 1229 goto err_flood_domain_create; 1230 1231 mdb_entry->sw = sw; 1232 mdb_entry->vid = vid; 1233 mdb_entry->flood_domain = flood_domain; 1234 ether_addr_copy(mdb_entry->addr, addr); 1235 1236 if (prestera_hw_mdb_create(mdb_entry)) 1237 goto err_mdb_hw_create; 1238 1239 return mdb_entry; 1240 1241 err_mdb_hw_create: 1242 prestera_flood_domain_destroy(flood_domain); 1243 err_flood_domain_create: 1244 kfree(mdb_entry); 1245 err_mdb_alloc: 1246 return NULL; 1247 } 1248 1249 void prestera_mdb_entry_destroy(struct prestera_mdb_entry *mdb_entry) 1250 { 1251 prestera_hw_mdb_destroy(mdb_entry); 1252 prestera_flood_domain_destroy(mdb_entry->flood_domain); 1253 kfree(mdb_entry); 1254 } 1255 1256 struct prestera_flood_domain * 1257 prestera_flood_domain_create(struct prestera_switch *sw) 1258 { 1259 struct prestera_flood_domain *domain; 1260 1261 domain = kzalloc(sizeof(*domain), GFP_KERNEL); 1262 if (!domain) 1263 return NULL; 1264 1265 domain->sw = sw; 1266 1267 if (prestera_hw_flood_domain_create(domain)) { 1268 kfree(domain); 1269 return NULL; 1270 } 1271 1272 INIT_LIST_HEAD(&domain->flood_domain_port_list); 1273 1274 return domain; 1275 } 1276 1277 void prestera_flood_domain_destroy(struct prestera_flood_domain *flood_domain) 1278 { 1279 WARN_ON(!list_empty(&flood_domain->flood_domain_port_list)); 1280 WARN_ON_ONCE(prestera_hw_flood_domain_destroy(flood_domain)); 1281 kfree(flood_domain); 1282 } 1283 1284 int 1285 prestera_flood_domain_port_create(struct prestera_flood_domain *flood_domain, 1286 struct net_device *dev, 1287 u16 vid) 1288 { 1289 struct prestera_flood_domain_port *flood_domain_port; 1290 bool is_first_port_in_list = false; 1291 int err; 1292 1293 flood_domain_port = kzalloc(sizeof(*flood_domain_port), GFP_KERNEL); 1294 if (!flood_domain_port) { 1295 err = -ENOMEM; 1296 goto err_port_alloc; 1297 } 1298 1299 flood_domain_port->vid = vid; 1300 1301 if (list_empty(&flood_domain->flood_domain_port_list)) 1302 is_first_port_in_list = true; 1303 1304 list_add(&flood_domain_port->flood_domain_port_node, 1305 &flood_domain->flood_domain_port_list); 1306 1307 flood_domain_port->flood_domain = flood_domain; 1308 flood_domain_port->dev = dev; 1309 1310 if (!is_first_port_in_list) { 1311 err = prestera_hw_flood_domain_ports_reset(flood_domain); 1312 if (err) 1313 goto err_prestera_mdb_port_create_hw; 1314 } 1315 1316 err = prestera_hw_flood_domain_ports_set(flood_domain); 1317 if (err) 1318 goto err_prestera_mdb_port_create_hw; 1319 1320 return 0; 1321 1322 err_prestera_mdb_port_create_hw: 1323 list_del(&flood_domain_port->flood_domain_port_node); 1324 kfree(flood_domain_port); 1325 err_port_alloc: 1326 return err; 1327 } 1328 1329 void 1330 prestera_flood_domain_port_destroy(struct prestera_flood_domain_port *port) 1331 { 1332 struct prestera_flood_domain *flood_domain = port->flood_domain; 1333 1334 list_del(&port->flood_domain_port_node); 1335 1336 WARN_ON_ONCE(prestera_hw_flood_domain_ports_reset(flood_domain)); 1337 1338 if (!list_empty(&flood_domain->flood_domain_port_list)) 1339 WARN_ON_ONCE(prestera_hw_flood_domain_ports_set(flood_domain)); 1340 1341 kfree(port); 1342 } 1343 1344 struct prestera_flood_domain_port * 1345 prestera_flood_domain_port_find(struct prestera_flood_domain *flood_domain, 1346 struct net_device *dev, u16 vid) 1347 { 1348 struct prestera_flood_domain_port *flood_domain_port; 1349 1350 list_for_each_entry(flood_domain_port, 1351 &flood_domain->flood_domain_port_list, 1352 flood_domain_port_node) 1353 if (flood_domain_port->dev == dev && 1354 vid == flood_domain_port->vid) 1355 return flood_domain_port; 1356 1357 return NULL; 1358 } 1359 1360 static int prestera_netdev_event_handler_register(struct prestera_switch *sw) 1361 { 1362 sw->netdev_nb.notifier_call = prestera_netdev_event_handler; 1363 1364 return register_netdevice_notifier(&sw->netdev_nb); 1365 } 1366 1367 static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw) 1368 { 1369 unregister_netdevice_notifier(&sw->netdev_nb); 1370 } 1371 1372 static int prestera_switch_init(struct prestera_switch *sw) 1373 { 1374 int err; 1375 1376 sw->np = of_find_compatible_node(NULL, NULL, "marvell,prestera"); 1377 1378 err = prestera_hw_switch_init(sw); 1379 if (err) { 1380 dev_err(prestera_dev(sw), "Failed to init Switch device\n"); 1381 return err; 1382 } 1383 1384 rwlock_init(&sw->port_list_lock); 1385 INIT_LIST_HEAD(&sw->port_list); 1386 1387 err = prestera_switch_set_base_mac_addr(sw); 1388 if (err) 1389 return err; 1390 1391 err = prestera_netdev_event_handler_register(sw); 1392 if (err) 1393 return err; 1394 1395 err = prestera_router_init(sw); 1396 if (err) 1397 goto err_router_init; 1398 1399 err = prestera_switchdev_init(sw); 1400 if (err) 1401 goto err_swdev_register; 1402 1403 err = prestera_rxtx_switch_init(sw); 1404 if (err) 1405 goto err_rxtx_register; 1406 1407 err = prestera_event_handlers_register(sw); 1408 if (err) 1409 goto err_handlers_register; 1410 1411 err = prestera_counter_init(sw); 1412 if (err) 1413 goto err_counter_init; 1414 1415 err = prestera_acl_init(sw); 1416 if (err) 1417 goto err_acl_init; 1418 1419 err = prestera_span_init(sw); 1420 if (err) 1421 goto err_span_init; 1422 1423 err = prestera_devlink_traps_register(sw); 1424 if (err) 1425 goto err_dl_register; 1426 1427 err = prestera_lag_init(sw); 1428 if (err) 1429 goto err_lag_init; 1430 1431 err = prestera_create_ports(sw); 1432 if (err) 1433 goto err_ports_create; 1434 1435 prestera_devlink_register(sw); 1436 return 0; 1437 1438 err_ports_create: 1439 prestera_lag_fini(sw); 1440 err_lag_init: 1441 prestera_devlink_traps_unregister(sw); 1442 err_dl_register: 1443 prestera_span_fini(sw); 1444 err_span_init: 1445 prestera_acl_fini(sw); 1446 err_acl_init: 1447 prestera_counter_fini(sw); 1448 err_counter_init: 1449 prestera_event_handlers_unregister(sw); 1450 err_handlers_register: 1451 prestera_rxtx_switch_fini(sw); 1452 err_rxtx_register: 1453 prestera_switchdev_fini(sw); 1454 err_swdev_register: 1455 prestera_router_fini(sw); 1456 err_router_init: 1457 prestera_netdev_event_handler_unregister(sw); 1458 prestera_hw_switch_fini(sw); 1459 1460 return err; 1461 } 1462 1463 static void prestera_switch_fini(struct prestera_switch *sw) 1464 { 1465 prestera_devlink_unregister(sw); 1466 prestera_destroy_ports(sw); 1467 prestera_lag_fini(sw); 1468 prestera_devlink_traps_unregister(sw); 1469 prestera_span_fini(sw); 1470 prestera_acl_fini(sw); 1471 prestera_counter_fini(sw); 1472 prestera_event_handlers_unregister(sw); 1473 prestera_rxtx_switch_fini(sw); 1474 prestera_switchdev_fini(sw); 1475 prestera_router_fini(sw); 1476 prestera_netdev_event_handler_unregister(sw); 1477 prestera_hw_switch_fini(sw); 1478 of_node_put(sw->np); 1479 } 1480 1481 int prestera_device_register(struct prestera_device *dev) 1482 { 1483 struct prestera_switch *sw; 1484 int err; 1485 1486 sw = prestera_devlink_alloc(dev); 1487 if (!sw) 1488 return -ENOMEM; 1489 1490 dev->priv = sw; 1491 sw->dev = dev; 1492 1493 err = prestera_switch_init(sw); 1494 if (err) { 1495 prestera_devlink_free(sw); 1496 return err; 1497 } 1498 1499 return 0; 1500 } 1501 EXPORT_SYMBOL(prestera_device_register); 1502 1503 void prestera_device_unregister(struct prestera_device *dev) 1504 { 1505 struct prestera_switch *sw = dev->priv; 1506 1507 prestera_switch_fini(sw); 1508 prestera_devlink_free(sw); 1509 } 1510 EXPORT_SYMBOL(prestera_device_unregister); 1511 1512 static int __init prestera_module_init(void) 1513 { 1514 prestera_wq = alloc_workqueue("prestera", 0, 0); 1515 if (!prestera_wq) 1516 return -ENOMEM; 1517 1518 prestera_owq = alloc_ordered_workqueue("prestera_ordered", 0); 1519 if (!prestera_owq) { 1520 destroy_workqueue(prestera_wq); 1521 return -ENOMEM; 1522 } 1523 1524 return 0; 1525 } 1526 1527 static void __exit prestera_module_exit(void) 1528 { 1529 destroy_workqueue(prestera_wq); 1530 destroy_workqueue(prestera_owq); 1531 } 1532 1533 module_init(prestera_module_init); 1534 module_exit(prestera_module_exit); 1535 1536 MODULE_LICENSE("Dual BSD/GPL"); 1537 MODULE_DESCRIPTION("Marvell Prestera switch driver"); 1538