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 12 #include "prestera.h" 13 #include "prestera_hw.h" 14 #include "prestera_rxtx.h" 15 #include "prestera_devlink.h" 16 #include "prestera_ethtool.h" 17 #include "prestera_switchdev.h" 18 19 #define PRESTERA_MTU_DEFAULT 1536 20 21 #define PRESTERA_STATS_DELAY_MS 1000 22 23 #define PRESTERA_MAC_ADDR_NUM_MAX 255 24 25 static struct workqueue_struct *prestera_wq; 26 27 int prestera_port_pvid_set(struct prestera_port *port, u16 vid) 28 { 29 enum prestera_accept_frm_type frm_type; 30 int err; 31 32 frm_type = PRESTERA_ACCEPT_FRAME_TYPE_TAGGED; 33 34 if (vid) { 35 err = prestera_hw_vlan_port_vid_set(port, vid); 36 if (err) 37 return err; 38 39 frm_type = PRESTERA_ACCEPT_FRAME_TYPE_ALL; 40 } 41 42 err = prestera_hw_port_accept_frm_type(port, frm_type); 43 if (err && frm_type == PRESTERA_ACCEPT_FRAME_TYPE_ALL) 44 prestera_hw_vlan_port_vid_set(port, port->pvid); 45 46 port->pvid = vid; 47 return 0; 48 } 49 50 struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw, 51 u32 dev_id, u32 hw_id) 52 { 53 struct prestera_port *port = NULL; 54 55 read_lock(&sw->port_list_lock); 56 list_for_each_entry(port, &sw->port_list, list) { 57 if (port->dev_id == dev_id && port->hw_id == hw_id) 58 break; 59 } 60 read_unlock(&sw->port_list_lock); 61 62 return port; 63 } 64 65 struct prestera_port *prestera_find_port(struct prestera_switch *sw, u32 id) 66 { 67 struct prestera_port *port = NULL; 68 69 read_lock(&sw->port_list_lock); 70 list_for_each_entry(port, &sw->port_list, list) { 71 if (port->id == id) 72 break; 73 } 74 read_unlock(&sw->port_list_lock); 75 76 return port; 77 } 78 79 static int prestera_port_open(struct net_device *dev) 80 { 81 struct prestera_port *port = netdev_priv(dev); 82 int err; 83 84 err = prestera_hw_port_state_set(port, true); 85 if (err) 86 return err; 87 88 netif_start_queue(dev); 89 90 return 0; 91 } 92 93 static int prestera_port_close(struct net_device *dev) 94 { 95 struct prestera_port *port = netdev_priv(dev); 96 97 netif_stop_queue(dev); 98 99 return prestera_hw_port_state_set(port, false); 100 } 101 102 static netdev_tx_t prestera_port_xmit(struct sk_buff *skb, 103 struct net_device *dev) 104 { 105 return prestera_rxtx_xmit(netdev_priv(dev), skb); 106 } 107 108 static int prestera_is_valid_mac_addr(struct prestera_port *port, u8 *addr) 109 { 110 if (!is_valid_ether_addr(addr)) 111 return -EADDRNOTAVAIL; 112 113 /* firmware requires that port's MAC address contains first 5 bytes 114 * of the base MAC address 115 */ 116 if (memcmp(port->sw->base_mac, addr, ETH_ALEN - 1)) 117 return -EINVAL; 118 119 return 0; 120 } 121 122 static int prestera_port_set_mac_address(struct net_device *dev, void *p) 123 { 124 struct prestera_port *port = netdev_priv(dev); 125 struct sockaddr *addr = p; 126 int err; 127 128 err = prestera_is_valid_mac_addr(port, addr->sa_data); 129 if (err) 130 return err; 131 132 err = prestera_hw_port_mac_set(port, addr->sa_data); 133 if (err) 134 return err; 135 136 ether_addr_copy(dev->dev_addr, addr->sa_data); 137 138 return 0; 139 } 140 141 static int prestera_port_change_mtu(struct net_device *dev, int mtu) 142 { 143 struct prestera_port *port = netdev_priv(dev); 144 int err; 145 146 err = prestera_hw_port_mtu_set(port, mtu); 147 if (err) 148 return err; 149 150 dev->mtu = mtu; 151 152 return 0; 153 } 154 155 static void prestera_port_get_stats64(struct net_device *dev, 156 struct rtnl_link_stats64 *stats) 157 { 158 struct prestera_port *port = netdev_priv(dev); 159 struct prestera_port_stats *port_stats = &port->cached_hw_stats.stats; 160 161 stats->rx_packets = port_stats->broadcast_frames_received + 162 port_stats->multicast_frames_received + 163 port_stats->unicast_frames_received; 164 165 stats->tx_packets = port_stats->broadcast_frames_sent + 166 port_stats->multicast_frames_sent + 167 port_stats->unicast_frames_sent; 168 169 stats->rx_bytes = port_stats->good_octets_received; 170 171 stats->tx_bytes = port_stats->good_octets_sent; 172 173 stats->rx_errors = port_stats->rx_error_frame_received; 174 stats->tx_errors = port_stats->mac_trans_error; 175 176 stats->rx_dropped = port_stats->buffer_overrun; 177 stats->tx_dropped = 0; 178 179 stats->multicast = port_stats->multicast_frames_received; 180 stats->collisions = port_stats->excessive_collision; 181 182 stats->rx_crc_errors = port_stats->bad_crc; 183 } 184 185 static void prestera_port_get_hw_stats(struct prestera_port *port) 186 { 187 prestera_hw_port_stats_get(port, &port->cached_hw_stats.stats); 188 } 189 190 static void prestera_port_stats_update(struct work_struct *work) 191 { 192 struct prestera_port *port = 193 container_of(work, struct prestera_port, 194 cached_hw_stats.caching_dw.work); 195 196 prestera_port_get_hw_stats(port); 197 198 queue_delayed_work(prestera_wq, &port->cached_hw_stats.caching_dw, 199 msecs_to_jiffies(PRESTERA_STATS_DELAY_MS)); 200 } 201 202 static const struct net_device_ops prestera_netdev_ops = { 203 .ndo_open = prestera_port_open, 204 .ndo_stop = prestera_port_close, 205 .ndo_start_xmit = prestera_port_xmit, 206 .ndo_change_mtu = prestera_port_change_mtu, 207 .ndo_get_stats64 = prestera_port_get_stats64, 208 .ndo_set_mac_address = prestera_port_set_mac_address, 209 .ndo_get_devlink_port = prestera_devlink_get_port, 210 }; 211 212 int prestera_port_autoneg_set(struct prestera_port *port, bool enable, 213 u64 adver_link_modes, u8 adver_fec) 214 { 215 bool refresh = false; 216 u64 link_modes; 217 int err; 218 u8 fec; 219 220 if (port->caps.type != PRESTERA_PORT_TYPE_TP) 221 return enable ? -EINVAL : 0; 222 223 if (!enable) 224 goto set_autoneg; 225 226 link_modes = port->caps.supp_link_modes & adver_link_modes; 227 fec = port->caps.supp_fec & adver_fec; 228 229 if (!link_modes && !fec) 230 return -EOPNOTSUPP; 231 232 if (link_modes && port->adver_link_modes != link_modes) { 233 port->adver_link_modes = link_modes; 234 refresh = true; 235 } 236 237 if (fec && port->adver_fec != fec) { 238 port->adver_fec = fec; 239 refresh = true; 240 } 241 242 set_autoneg: 243 if (port->autoneg == enable && !refresh) 244 return 0; 245 246 err = prestera_hw_port_autoneg_set(port, enable, port->adver_link_modes, 247 port->adver_fec); 248 if (err) 249 return err; 250 251 port->autoneg = enable; 252 253 return 0; 254 } 255 256 static void prestera_port_list_add(struct prestera_port *port) 257 { 258 write_lock(&port->sw->port_list_lock); 259 list_add(&port->list, &port->sw->port_list); 260 write_unlock(&port->sw->port_list_lock); 261 } 262 263 static void prestera_port_list_del(struct prestera_port *port) 264 { 265 write_lock(&port->sw->port_list_lock); 266 list_del(&port->list); 267 write_unlock(&port->sw->port_list_lock); 268 } 269 270 static int prestera_port_create(struct prestera_switch *sw, u32 id) 271 { 272 struct prestera_port *port; 273 struct net_device *dev; 274 int err; 275 276 dev = alloc_etherdev(sizeof(*port)); 277 if (!dev) 278 return -ENOMEM; 279 280 port = netdev_priv(dev); 281 282 INIT_LIST_HEAD(&port->vlans_list); 283 port->pvid = PRESTERA_DEFAULT_VID; 284 port->dev = dev; 285 port->id = id; 286 port->sw = sw; 287 288 err = prestera_hw_port_info_get(port, &port->dev_id, &port->hw_id, 289 &port->fp_id); 290 if (err) { 291 dev_err(prestera_dev(sw), "Failed to get port(%u) info\n", id); 292 goto err_port_info_get; 293 } 294 295 err = prestera_devlink_port_register(port); 296 if (err) 297 goto err_dl_port_register; 298 299 dev->features |= NETIF_F_NETNS_LOCAL; 300 dev->netdev_ops = &prestera_netdev_ops; 301 dev->ethtool_ops = &prestera_ethtool_ops; 302 303 netif_carrier_off(dev); 304 305 dev->mtu = min_t(unsigned int, sw->mtu_max, PRESTERA_MTU_DEFAULT); 306 dev->min_mtu = sw->mtu_min; 307 dev->max_mtu = sw->mtu_max; 308 309 err = prestera_hw_port_mtu_set(port, dev->mtu); 310 if (err) { 311 dev_err(prestera_dev(sw), "Failed to set port(%u) mtu(%d)\n", 312 id, dev->mtu); 313 goto err_port_init; 314 } 315 316 if (port->fp_id >= PRESTERA_MAC_ADDR_NUM_MAX) { 317 err = -EINVAL; 318 goto err_port_init; 319 } 320 321 /* firmware requires that port's MAC address consist of the first 322 * 5 bytes of the base MAC address 323 */ 324 memcpy(dev->dev_addr, sw->base_mac, dev->addr_len - 1); 325 dev->dev_addr[dev->addr_len - 1] = port->fp_id; 326 327 err = prestera_hw_port_mac_set(port, dev->dev_addr); 328 if (err) { 329 dev_err(prestera_dev(sw), "Failed to set port(%u) mac addr\n", id); 330 goto err_port_init; 331 } 332 333 err = prestera_hw_port_cap_get(port, &port->caps); 334 if (err) { 335 dev_err(prestera_dev(sw), "Failed to get port(%u) caps\n", id); 336 goto err_port_init; 337 } 338 339 port->adver_fec = BIT(PRESTERA_PORT_FEC_OFF); 340 prestera_port_autoneg_set(port, true, port->caps.supp_link_modes, 341 port->caps.supp_fec); 342 343 err = prestera_hw_port_state_set(port, false); 344 if (err) { 345 dev_err(prestera_dev(sw), "Failed to set port(%u) down\n", id); 346 goto err_port_init; 347 } 348 349 err = prestera_rxtx_port_init(port); 350 if (err) 351 goto err_port_init; 352 353 INIT_DELAYED_WORK(&port->cached_hw_stats.caching_dw, 354 &prestera_port_stats_update); 355 356 prestera_port_list_add(port); 357 358 err = register_netdev(dev); 359 if (err) 360 goto err_register_netdev; 361 362 prestera_devlink_port_set(port); 363 364 return 0; 365 366 err_register_netdev: 367 prestera_port_list_del(port); 368 err_port_init: 369 prestera_devlink_port_unregister(port); 370 err_dl_port_register: 371 err_port_info_get: 372 free_netdev(dev); 373 return err; 374 } 375 376 static void prestera_port_destroy(struct prestera_port *port) 377 { 378 struct net_device *dev = port->dev; 379 380 cancel_delayed_work_sync(&port->cached_hw_stats.caching_dw); 381 prestera_devlink_port_clear(port); 382 unregister_netdev(dev); 383 prestera_port_list_del(port); 384 prestera_devlink_port_unregister(port); 385 free_netdev(dev); 386 } 387 388 static void prestera_destroy_ports(struct prestera_switch *sw) 389 { 390 struct prestera_port *port, *tmp; 391 392 list_for_each_entry_safe(port, tmp, &sw->port_list, list) 393 prestera_port_destroy(port); 394 } 395 396 static int prestera_create_ports(struct prestera_switch *sw) 397 { 398 struct prestera_port *port, *tmp; 399 u32 port_idx; 400 int err; 401 402 for (port_idx = 0; port_idx < sw->port_count; port_idx++) { 403 err = prestera_port_create(sw, port_idx); 404 if (err) 405 goto err_port_create; 406 } 407 408 return 0; 409 410 err_port_create: 411 list_for_each_entry_safe(port, tmp, &sw->port_list, list) 412 prestera_port_destroy(port); 413 414 return err; 415 } 416 417 static void prestera_port_handle_event(struct prestera_switch *sw, 418 struct prestera_event *evt, void *arg) 419 { 420 struct delayed_work *caching_dw; 421 struct prestera_port *port; 422 423 port = prestera_find_port(sw, evt->port_evt.port_id); 424 if (!port || !port->dev) 425 return; 426 427 caching_dw = &port->cached_hw_stats.caching_dw; 428 429 if (evt->id == PRESTERA_PORT_EVENT_STATE_CHANGED) { 430 if (evt->port_evt.data.oper_state) { 431 netif_carrier_on(port->dev); 432 if (!delayed_work_pending(caching_dw)) 433 queue_delayed_work(prestera_wq, caching_dw, 0); 434 } else if (netif_running(port->dev) && 435 netif_carrier_ok(port->dev)) { 436 netif_carrier_off(port->dev); 437 if (delayed_work_pending(caching_dw)) 438 cancel_delayed_work(caching_dw); 439 } 440 } 441 } 442 443 static int prestera_event_handlers_register(struct prestera_switch *sw) 444 { 445 return prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_PORT, 446 prestera_port_handle_event, 447 NULL); 448 } 449 450 static void prestera_event_handlers_unregister(struct prestera_switch *sw) 451 { 452 prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_PORT, 453 prestera_port_handle_event); 454 } 455 456 static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw) 457 { 458 struct device_node *base_mac_np; 459 struct device_node *np; 460 int ret; 461 462 np = of_find_compatible_node(NULL, NULL, "marvell,prestera"); 463 base_mac_np = of_parse_phandle(np, "base-mac-provider", 0); 464 465 ret = of_get_mac_address(base_mac_np, sw->base_mac); 466 if (ret) { 467 eth_random_addr(sw->base_mac); 468 dev_info(prestera_dev(sw), "using random base mac address\n"); 469 } 470 of_node_put(base_mac_np); 471 472 return prestera_hw_switch_mac_set(sw, sw->base_mac); 473 } 474 475 bool prestera_netdev_check(const struct net_device *dev) 476 { 477 return dev->netdev_ops == &prestera_netdev_ops; 478 } 479 480 static int prestera_lower_dev_walk(struct net_device *dev, 481 struct netdev_nested_priv *priv) 482 { 483 struct prestera_port **pport = (struct prestera_port **)priv->data; 484 485 if (prestera_netdev_check(dev)) { 486 *pport = netdev_priv(dev); 487 return 1; 488 } 489 490 return 0; 491 } 492 493 struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev) 494 { 495 struct prestera_port *port = NULL; 496 struct netdev_nested_priv priv = { 497 .data = (void *)&port, 498 }; 499 500 if (prestera_netdev_check(dev)) 501 return netdev_priv(dev); 502 503 netdev_walk_all_lower_dev(dev, prestera_lower_dev_walk, &priv); 504 505 return port; 506 } 507 508 static int prestera_netdev_port_event(struct net_device *dev, 509 unsigned long event, void *ptr) 510 { 511 switch (event) { 512 case NETDEV_PRECHANGEUPPER: 513 case NETDEV_CHANGEUPPER: 514 return prestera_bridge_port_event(dev, event, ptr); 515 default: 516 return 0; 517 } 518 } 519 520 static int prestera_netdev_event_handler(struct notifier_block *nb, 521 unsigned long event, void *ptr) 522 { 523 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 524 int err = 0; 525 526 if (prestera_netdev_check(dev)) 527 err = prestera_netdev_port_event(dev, event, ptr); 528 529 return notifier_from_errno(err); 530 } 531 532 static int prestera_netdev_event_handler_register(struct prestera_switch *sw) 533 { 534 sw->netdev_nb.notifier_call = prestera_netdev_event_handler; 535 536 return register_netdevice_notifier(&sw->netdev_nb); 537 } 538 539 static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw) 540 { 541 unregister_netdevice_notifier(&sw->netdev_nb); 542 } 543 544 static int prestera_switch_init(struct prestera_switch *sw) 545 { 546 int err; 547 548 err = prestera_hw_switch_init(sw); 549 if (err) { 550 dev_err(prestera_dev(sw), "Failed to init Switch device\n"); 551 return err; 552 } 553 554 rwlock_init(&sw->port_list_lock); 555 INIT_LIST_HEAD(&sw->port_list); 556 557 err = prestera_switch_set_base_mac_addr(sw); 558 if (err) 559 return err; 560 561 err = prestera_netdev_event_handler_register(sw); 562 if (err) 563 return err; 564 565 err = prestera_switchdev_init(sw); 566 if (err) 567 goto err_swdev_register; 568 569 err = prestera_rxtx_switch_init(sw); 570 if (err) 571 goto err_rxtx_register; 572 573 err = prestera_event_handlers_register(sw); 574 if (err) 575 goto err_handlers_register; 576 577 err = prestera_devlink_register(sw); 578 if (err) 579 goto err_dl_register; 580 581 err = prestera_create_ports(sw); 582 if (err) 583 goto err_ports_create; 584 585 return 0; 586 587 err_ports_create: 588 prestera_devlink_unregister(sw); 589 err_dl_register: 590 prestera_event_handlers_unregister(sw); 591 err_handlers_register: 592 prestera_rxtx_switch_fini(sw); 593 err_rxtx_register: 594 prestera_switchdev_fini(sw); 595 err_swdev_register: 596 prestera_netdev_event_handler_unregister(sw); 597 prestera_hw_switch_fini(sw); 598 599 return err; 600 } 601 602 static void prestera_switch_fini(struct prestera_switch *sw) 603 { 604 prestera_destroy_ports(sw); 605 prestera_devlink_unregister(sw); 606 prestera_event_handlers_unregister(sw); 607 prestera_rxtx_switch_fini(sw); 608 prestera_switchdev_fini(sw); 609 prestera_netdev_event_handler_unregister(sw); 610 prestera_hw_switch_fini(sw); 611 } 612 613 int prestera_device_register(struct prestera_device *dev) 614 { 615 struct prestera_switch *sw; 616 int err; 617 618 sw = prestera_devlink_alloc(); 619 if (!sw) 620 return -ENOMEM; 621 622 dev->priv = sw; 623 sw->dev = dev; 624 625 err = prestera_switch_init(sw); 626 if (err) { 627 prestera_devlink_free(sw); 628 return err; 629 } 630 631 return 0; 632 } 633 EXPORT_SYMBOL(prestera_device_register); 634 635 void prestera_device_unregister(struct prestera_device *dev) 636 { 637 struct prestera_switch *sw = dev->priv; 638 639 prestera_switch_fini(sw); 640 prestera_devlink_free(sw); 641 } 642 EXPORT_SYMBOL(prestera_device_unregister); 643 644 static int __init prestera_module_init(void) 645 { 646 prestera_wq = alloc_workqueue("prestera", 0, 0); 647 if (!prestera_wq) 648 return -ENOMEM; 649 650 return 0; 651 } 652 653 static void __exit prestera_module_exit(void) 654 { 655 destroy_workqueue(prestera_wq); 656 } 657 658 module_init(prestera_module_init); 659 module_exit(prestera_module_exit); 660 661 MODULE_LICENSE("Dual BSD/GPL"); 662 MODULE_DESCRIPTION("Marvell Prestera switch driver"); 663