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