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