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