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