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