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