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 { 435 netif_carrier_off(port->dev); 436 if (delayed_work_pending(caching_dw)) 437 cancel_delayed_work(caching_dw); 438 } 439 } 440 } 441 442 static int prestera_event_handlers_register(struct prestera_switch *sw) 443 { 444 return prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_PORT, 445 prestera_port_handle_event, 446 NULL); 447 } 448 449 static void prestera_event_handlers_unregister(struct prestera_switch *sw) 450 { 451 prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_PORT, 452 prestera_port_handle_event); 453 } 454 455 static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw) 456 { 457 struct device_node *base_mac_np; 458 struct device_node *np; 459 const char *base_mac; 460 461 np = of_find_compatible_node(NULL, NULL, "marvell,prestera"); 462 base_mac_np = of_parse_phandle(np, "base-mac-provider", 0); 463 464 base_mac = of_get_mac_address(base_mac_np); 465 of_node_put(base_mac_np); 466 if (!IS_ERR(base_mac)) 467 ether_addr_copy(sw->base_mac, base_mac); 468 469 if (!is_valid_ether_addr(sw->base_mac)) { 470 eth_random_addr(sw->base_mac); 471 dev_info(prestera_dev(sw), "using random base mac address\n"); 472 } 473 474 return prestera_hw_switch_mac_set(sw, sw->base_mac); 475 } 476 477 bool prestera_netdev_check(const struct net_device *dev) 478 { 479 return dev->netdev_ops == &prestera_netdev_ops; 480 } 481 482 static int prestera_lower_dev_walk(struct net_device *dev, 483 struct netdev_nested_priv *priv) 484 { 485 struct prestera_port **pport = (struct prestera_port **)priv->data; 486 487 if (prestera_netdev_check(dev)) { 488 *pport = netdev_priv(dev); 489 return 1; 490 } 491 492 return 0; 493 } 494 495 struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev) 496 { 497 struct prestera_port *port = NULL; 498 struct netdev_nested_priv priv = { 499 .data = (void *)&port, 500 }; 501 502 if (prestera_netdev_check(dev)) 503 return netdev_priv(dev); 504 505 netdev_walk_all_lower_dev(dev, prestera_lower_dev_walk, &priv); 506 507 return port; 508 } 509 510 static int prestera_netdev_port_event(struct net_device *dev, 511 unsigned long event, void *ptr) 512 { 513 switch (event) { 514 case NETDEV_PRECHANGEUPPER: 515 case NETDEV_CHANGEUPPER: 516 return prestera_bridge_port_event(dev, event, ptr); 517 default: 518 return 0; 519 } 520 } 521 522 static int prestera_netdev_event_handler(struct notifier_block *nb, 523 unsigned long event, void *ptr) 524 { 525 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 526 int err = 0; 527 528 if (prestera_netdev_check(dev)) 529 err = prestera_netdev_port_event(dev, event, ptr); 530 531 return notifier_from_errno(err); 532 } 533 534 static int prestera_netdev_event_handler_register(struct prestera_switch *sw) 535 { 536 sw->netdev_nb.notifier_call = prestera_netdev_event_handler; 537 538 return register_netdevice_notifier(&sw->netdev_nb); 539 } 540 541 static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw) 542 { 543 unregister_netdevice_notifier(&sw->netdev_nb); 544 } 545 546 static int prestera_switch_init(struct prestera_switch *sw) 547 { 548 int err; 549 550 err = prestera_hw_switch_init(sw); 551 if (err) { 552 dev_err(prestera_dev(sw), "Failed to init Switch device\n"); 553 return err; 554 } 555 556 rwlock_init(&sw->port_list_lock); 557 INIT_LIST_HEAD(&sw->port_list); 558 559 err = prestera_switch_set_base_mac_addr(sw); 560 if (err) 561 return err; 562 563 err = prestera_netdev_event_handler_register(sw); 564 if (err) 565 return err; 566 567 err = prestera_switchdev_init(sw); 568 if (err) 569 goto err_swdev_register; 570 571 err = prestera_rxtx_switch_init(sw); 572 if (err) 573 goto err_rxtx_register; 574 575 err = prestera_event_handlers_register(sw); 576 if (err) 577 goto err_handlers_register; 578 579 err = prestera_devlink_register(sw); 580 if (err) 581 goto err_dl_register; 582 583 err = prestera_create_ports(sw); 584 if (err) 585 goto err_ports_create; 586 587 return 0; 588 589 err_ports_create: 590 prestera_devlink_unregister(sw); 591 err_dl_register: 592 prestera_event_handlers_unregister(sw); 593 err_handlers_register: 594 prestera_rxtx_switch_fini(sw); 595 err_rxtx_register: 596 prestera_switchdev_fini(sw); 597 err_swdev_register: 598 prestera_netdev_event_handler_unregister(sw); 599 prestera_hw_switch_fini(sw); 600 601 return err; 602 } 603 604 static void prestera_switch_fini(struct prestera_switch *sw) 605 { 606 prestera_destroy_ports(sw); 607 prestera_devlink_unregister(sw); 608 prestera_event_handlers_unregister(sw); 609 prestera_rxtx_switch_fini(sw); 610 prestera_switchdev_fini(sw); 611 prestera_netdev_event_handler_unregister(sw); 612 prestera_hw_switch_fini(sw); 613 } 614 615 int prestera_device_register(struct prestera_device *dev) 616 { 617 struct prestera_switch *sw; 618 int err; 619 620 sw = prestera_devlink_alloc(); 621 if (!sw) 622 return -ENOMEM; 623 624 dev->priv = sw; 625 sw->dev = dev; 626 627 err = prestera_switch_init(sw); 628 if (err) { 629 prestera_devlink_free(sw); 630 return err; 631 } 632 633 return 0; 634 } 635 EXPORT_SYMBOL(prestera_device_register); 636 637 void prestera_device_unregister(struct prestera_device *dev) 638 { 639 struct prestera_switch *sw = dev->priv; 640 641 prestera_switch_fini(sw); 642 prestera_devlink_free(sw); 643 } 644 EXPORT_SYMBOL(prestera_device_unregister); 645 646 static int __init prestera_module_init(void) 647 { 648 prestera_wq = alloc_workqueue("prestera", 0, 0); 649 if (!prestera_wq) 650 return -ENOMEM; 651 652 return 0; 653 } 654 655 static void __exit prestera_module_exit(void) 656 { 657 destroy_workqueue(prestera_wq); 658 } 659 660 module_init(prestera_module_init); 661 module_exit(prestera_module_exit); 662 663 MODULE_LICENSE("Dual BSD/GPL"); 664 MODULE_DESCRIPTION("Marvell Prestera switch driver"); 665