1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */ 3 4 #include <linux/etherdevice.h> 5 #include <linux/ipv6.h> 6 #include <linux/types.h> 7 #include <net/netdev_queues.h> 8 9 #include "fbnic.h" 10 #include "fbnic_netdev.h" 11 #include "fbnic_txrx.h" 12 13 int __fbnic_open(struct fbnic_net *fbn) 14 { 15 struct fbnic_dev *fbd = fbn->fbd; 16 int err; 17 18 err = fbnic_alloc_napi_vectors(fbn); 19 if (err) 20 return err; 21 22 err = fbnic_alloc_resources(fbn); 23 if (err) 24 goto free_napi_vectors; 25 26 err = fbnic_set_netif_queues(fbn); 27 if (err) 28 goto free_resources; 29 30 /* Send ownership message and flush to verify FW has seen it */ 31 err = fbnic_fw_xmit_ownership_msg(fbd, true); 32 if (err) { 33 dev_warn(fbd->dev, 34 "Error %d sending host ownership message to the firmware\n", 35 err); 36 goto free_resources; 37 } 38 39 err = fbnic_time_start(fbn); 40 if (err) 41 goto release_ownership; 42 43 err = fbnic_fw_init_heartbeat(fbd, false); 44 if (err) 45 goto time_stop; 46 47 err = fbnic_pcs_irq_enable(fbd); 48 if (err) 49 goto time_stop; 50 /* Pull the BMC config and initialize the RPC */ 51 fbnic_bmc_rpc_init(fbd); 52 fbnic_rss_reinit(fbd, fbn); 53 54 return 0; 55 time_stop: 56 fbnic_time_stop(fbn); 57 release_ownership: 58 fbnic_fw_xmit_ownership_msg(fbn->fbd, false); 59 free_resources: 60 fbnic_free_resources(fbn); 61 free_napi_vectors: 62 fbnic_free_napi_vectors(fbn); 63 return err; 64 } 65 66 static int fbnic_open(struct net_device *netdev) 67 { 68 struct fbnic_net *fbn = netdev_priv(netdev); 69 int err; 70 71 fbnic_napi_name_irqs(fbn->fbd); 72 73 err = __fbnic_open(fbn); 74 if (!err) 75 fbnic_up(fbn); 76 77 return err; 78 } 79 80 static int fbnic_stop(struct net_device *netdev) 81 { 82 struct fbnic_net *fbn = netdev_priv(netdev); 83 84 fbnic_down(fbn); 85 fbnic_pcs_irq_disable(fbn->fbd); 86 87 fbnic_time_stop(fbn); 88 fbnic_fw_xmit_ownership_msg(fbn->fbd, false); 89 90 fbnic_reset_netif_queues(fbn); 91 fbnic_free_resources(fbn); 92 fbnic_free_napi_vectors(fbn); 93 94 return 0; 95 } 96 97 static int fbnic_uc_sync(struct net_device *netdev, const unsigned char *addr) 98 { 99 struct fbnic_net *fbn = netdev_priv(netdev); 100 struct fbnic_mac_addr *avail_addr; 101 102 if (WARN_ON(!is_valid_ether_addr(addr))) 103 return -EADDRNOTAVAIL; 104 105 avail_addr = __fbnic_uc_sync(fbn->fbd, addr); 106 if (!avail_addr) 107 return -ENOSPC; 108 109 /* Add type flag indicating this address is in use by the host */ 110 set_bit(FBNIC_MAC_ADDR_T_UNICAST, avail_addr->act_tcam); 111 112 return 0; 113 } 114 115 static int fbnic_uc_unsync(struct net_device *netdev, const unsigned char *addr) 116 { 117 struct fbnic_net *fbn = netdev_priv(netdev); 118 struct fbnic_dev *fbd = fbn->fbd; 119 int i, ret; 120 121 /* Scan from middle of list to bottom, filling bottom up. 122 * Skip the first entry which is reserved for dev_addr and 123 * leave the last entry to use for promiscuous filtering. 124 */ 125 for (i = fbd->mac_addr_boundary, ret = -ENOENT; 126 i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX && ret; i++) { 127 struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i]; 128 129 if (!ether_addr_equal(mac_addr->value.addr8, addr)) 130 continue; 131 132 ret = __fbnic_uc_unsync(mac_addr); 133 } 134 135 return ret; 136 } 137 138 static int fbnic_mc_sync(struct net_device *netdev, const unsigned char *addr) 139 { 140 struct fbnic_net *fbn = netdev_priv(netdev); 141 struct fbnic_mac_addr *avail_addr; 142 143 if (WARN_ON(!is_multicast_ether_addr(addr))) 144 return -EADDRNOTAVAIL; 145 146 avail_addr = __fbnic_mc_sync(fbn->fbd, addr); 147 if (!avail_addr) 148 return -ENOSPC; 149 150 /* Add type flag indicating this address is in use by the host */ 151 set_bit(FBNIC_MAC_ADDR_T_MULTICAST, avail_addr->act_tcam); 152 153 return 0; 154 } 155 156 static int fbnic_mc_unsync(struct net_device *netdev, const unsigned char *addr) 157 { 158 struct fbnic_net *fbn = netdev_priv(netdev); 159 struct fbnic_dev *fbd = fbn->fbd; 160 int i, ret; 161 162 /* Scan from middle of list to top, filling top down. 163 * Skip over the address reserved for the BMC MAC and 164 * exclude index 0 as that belongs to the broadcast address 165 */ 166 for (i = fbd->mac_addr_boundary, ret = -ENOENT; 167 --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX && ret;) { 168 struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i]; 169 170 if (!ether_addr_equal(mac_addr->value.addr8, addr)) 171 continue; 172 173 ret = __fbnic_mc_unsync(mac_addr); 174 } 175 176 return ret; 177 } 178 179 void __fbnic_set_rx_mode(struct net_device *netdev) 180 { 181 struct fbnic_net *fbn = netdev_priv(netdev); 182 bool uc_promisc = false, mc_promisc = false; 183 struct fbnic_dev *fbd = fbn->fbd; 184 struct fbnic_mac_addr *mac_addr; 185 int err; 186 187 /* Populate host address from dev_addr */ 188 mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX]; 189 if (!ether_addr_equal(mac_addr->value.addr8, netdev->dev_addr) || 190 mac_addr->state != FBNIC_TCAM_S_VALID) { 191 ether_addr_copy(mac_addr->value.addr8, netdev->dev_addr); 192 mac_addr->state = FBNIC_TCAM_S_UPDATE; 193 set_bit(FBNIC_MAC_ADDR_T_UNICAST, mac_addr->act_tcam); 194 } 195 196 /* Populate broadcast address if broadcast is enabled */ 197 mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX]; 198 if (netdev->flags & IFF_BROADCAST) { 199 if (!is_broadcast_ether_addr(mac_addr->value.addr8) || 200 mac_addr->state != FBNIC_TCAM_S_VALID) { 201 eth_broadcast_addr(mac_addr->value.addr8); 202 mac_addr->state = FBNIC_TCAM_S_ADD; 203 } 204 set_bit(FBNIC_MAC_ADDR_T_BROADCAST, mac_addr->act_tcam); 205 } else if (mac_addr->state == FBNIC_TCAM_S_VALID) { 206 __fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_BROADCAST); 207 } 208 209 /* Synchronize unicast and multicast address lists */ 210 err = __dev_uc_sync(netdev, fbnic_uc_sync, fbnic_uc_unsync); 211 if (err == -ENOSPC) 212 uc_promisc = true; 213 err = __dev_mc_sync(netdev, fbnic_mc_sync, fbnic_mc_unsync); 214 if (err == -ENOSPC) 215 mc_promisc = true; 216 217 uc_promisc |= !!(netdev->flags & IFF_PROMISC); 218 mc_promisc |= !!(netdev->flags & IFF_ALLMULTI) || uc_promisc; 219 220 /* Populate last TCAM entry with promiscuous entry and 0/1 bit mask */ 221 mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_PROMISC_IDX]; 222 if (uc_promisc) { 223 if (!is_zero_ether_addr(mac_addr->value.addr8) || 224 mac_addr->state != FBNIC_TCAM_S_VALID) { 225 eth_zero_addr(mac_addr->value.addr8); 226 eth_broadcast_addr(mac_addr->mask.addr8); 227 clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI, 228 mac_addr->act_tcam); 229 set_bit(FBNIC_MAC_ADDR_T_PROMISC, 230 mac_addr->act_tcam); 231 mac_addr->state = FBNIC_TCAM_S_ADD; 232 } 233 } else if (mc_promisc && 234 (!fbnic_bmc_present(fbd) || !fbd->fw_cap.all_multi)) { 235 /* We have to add a special handler for multicast as the 236 * BMC may have an all-multi rule already in place. As such 237 * adding a rule ourselves won't do any good so we will have 238 * to modify the rules for the ALL MULTI below if the BMC 239 * already has the rule in place. 240 */ 241 if (!is_multicast_ether_addr(mac_addr->value.addr8) || 242 mac_addr->state != FBNIC_TCAM_S_VALID) { 243 eth_zero_addr(mac_addr->value.addr8); 244 eth_broadcast_addr(mac_addr->mask.addr8); 245 mac_addr->value.addr8[0] ^= 1; 246 mac_addr->mask.addr8[0] ^= 1; 247 set_bit(FBNIC_MAC_ADDR_T_ALLMULTI, 248 mac_addr->act_tcam); 249 clear_bit(FBNIC_MAC_ADDR_T_PROMISC, 250 mac_addr->act_tcam); 251 mac_addr->state = FBNIC_TCAM_S_ADD; 252 } 253 } else if (mac_addr->state == FBNIC_TCAM_S_VALID) { 254 if (test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam)) { 255 clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI, 256 mac_addr->act_tcam); 257 clear_bit(FBNIC_MAC_ADDR_T_PROMISC, 258 mac_addr->act_tcam); 259 } else { 260 mac_addr->state = FBNIC_TCAM_S_DELETE; 261 } 262 } 263 264 /* Add rules for BMC all multicast if it is enabled */ 265 fbnic_bmc_rpc_all_multi_config(fbd, mc_promisc); 266 267 /* Sift out any unshared BMC rules and place them in BMC only section */ 268 fbnic_sift_macda(fbd); 269 270 /* Write updates to hardware */ 271 fbnic_write_rules(fbd); 272 fbnic_write_macda(fbd); 273 fbnic_write_tce_tcam(fbd); 274 } 275 276 static void fbnic_set_rx_mode(struct net_device *netdev) 277 { 278 /* No need to update the hardware if we are not running */ 279 if (netif_running(netdev)) 280 __fbnic_set_rx_mode(netdev); 281 } 282 283 static int fbnic_set_mac(struct net_device *netdev, void *p) 284 { 285 struct sockaddr *addr = p; 286 287 if (!is_valid_ether_addr(addr->sa_data)) 288 return -EADDRNOTAVAIL; 289 290 eth_hw_addr_set(netdev, addr->sa_data); 291 292 fbnic_set_rx_mode(netdev); 293 294 return 0; 295 } 296 297 void fbnic_clear_rx_mode(struct net_device *netdev) 298 { 299 struct fbnic_net *fbn = netdev_priv(netdev); 300 struct fbnic_dev *fbd = fbn->fbd; 301 int idx; 302 303 for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) { 304 struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx]; 305 306 if (mac_addr->state != FBNIC_TCAM_S_VALID) 307 continue; 308 309 bitmap_clear(mac_addr->act_tcam, 310 FBNIC_MAC_ADDR_T_HOST_START, 311 FBNIC_MAC_ADDR_T_HOST_LEN); 312 313 if (bitmap_empty(mac_addr->act_tcam, 314 FBNIC_RPC_TCAM_ACT_NUM_ENTRIES)) 315 mac_addr->state = FBNIC_TCAM_S_DELETE; 316 } 317 318 /* Write updates to hardware */ 319 fbnic_write_macda(fbd); 320 321 __dev_uc_unsync(netdev, NULL); 322 __dev_mc_unsync(netdev, NULL); 323 } 324 325 static int fbnic_hwtstamp_get(struct net_device *netdev, 326 struct kernel_hwtstamp_config *config) 327 { 328 struct fbnic_net *fbn = netdev_priv(netdev); 329 330 *config = fbn->hwtstamp_config; 331 332 return 0; 333 } 334 335 static int fbnic_hwtstamp_set(struct net_device *netdev, 336 struct kernel_hwtstamp_config *config, 337 struct netlink_ext_ack *extack) 338 { 339 struct fbnic_net *fbn = netdev_priv(netdev); 340 int old_rx_filter; 341 342 if (config->source != HWTSTAMP_SOURCE_NETDEV) 343 return -EOPNOTSUPP; 344 345 if (!kernel_hwtstamp_config_changed(config, &fbn->hwtstamp_config)) 346 return 0; 347 348 /* Upscale the filters */ 349 switch (config->rx_filter) { 350 case HWTSTAMP_FILTER_NONE: 351 case HWTSTAMP_FILTER_ALL: 352 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 353 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 354 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 355 case HWTSTAMP_FILTER_PTP_V2_EVENT: 356 break; 357 case HWTSTAMP_FILTER_NTP_ALL: 358 config->rx_filter = HWTSTAMP_FILTER_ALL; 359 break; 360 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 361 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 362 config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; 363 break; 364 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 365 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 366 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; 367 break; 368 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 369 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 370 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; 371 break; 372 case HWTSTAMP_FILTER_PTP_V2_SYNC: 373 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 374 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 375 break; 376 default: 377 return -ERANGE; 378 } 379 380 /* Configure */ 381 old_rx_filter = fbn->hwtstamp_config.rx_filter; 382 memcpy(&fbn->hwtstamp_config, config, sizeof(*config)); 383 384 if (old_rx_filter != config->rx_filter && netif_running(fbn->netdev)) { 385 fbnic_rss_reinit(fbn->fbd, fbn); 386 fbnic_write_rules(fbn->fbd); 387 } 388 389 /* Save / report back filter configuration 390 * Note that our filter configuration is inexact. Instead of 391 * filtering for a specific UDP port or L2 Ethertype we are 392 * filtering in all UDP or all non-IP packets for timestamping. So 393 * if anything other than FILTER_ALL is requested we report 394 * FILTER_SOME indicating that we will be timestamping a few 395 * additional packets. 396 */ 397 if (config->rx_filter > HWTSTAMP_FILTER_ALL) 398 config->rx_filter = HWTSTAMP_FILTER_SOME; 399 400 return 0; 401 } 402 403 static void fbnic_get_stats64(struct net_device *dev, 404 struct rtnl_link_stats64 *stats64) 405 { 406 u64 tx_bytes, tx_packets, tx_dropped = 0; 407 u64 rx_bytes, rx_packets, rx_dropped = 0; 408 struct fbnic_net *fbn = netdev_priv(dev); 409 struct fbnic_queue_stats *stats; 410 unsigned int start, i; 411 412 stats = &fbn->tx_stats; 413 414 tx_bytes = stats->bytes; 415 tx_packets = stats->packets; 416 tx_dropped = stats->dropped; 417 418 stats64->tx_bytes = tx_bytes; 419 stats64->tx_packets = tx_packets; 420 stats64->tx_dropped = tx_dropped; 421 422 for (i = 0; i < fbn->num_tx_queues; i++) { 423 struct fbnic_ring *txr = fbn->tx[i]; 424 425 if (!txr) 426 continue; 427 428 stats = &txr->stats; 429 do { 430 start = u64_stats_fetch_begin(&stats->syncp); 431 tx_bytes = stats->bytes; 432 tx_packets = stats->packets; 433 tx_dropped = stats->dropped; 434 } while (u64_stats_fetch_retry(&stats->syncp, start)); 435 436 stats64->tx_bytes += tx_bytes; 437 stats64->tx_packets += tx_packets; 438 stats64->tx_dropped += tx_dropped; 439 } 440 441 stats = &fbn->rx_stats; 442 443 rx_bytes = stats->bytes; 444 rx_packets = stats->packets; 445 rx_dropped = stats->dropped; 446 447 stats64->rx_bytes = rx_bytes; 448 stats64->rx_packets = rx_packets; 449 stats64->rx_dropped = rx_dropped; 450 451 for (i = 0; i < fbn->num_rx_queues; i++) { 452 struct fbnic_ring *rxr = fbn->rx[i]; 453 454 if (!rxr) 455 continue; 456 457 stats = &rxr->stats; 458 do { 459 start = u64_stats_fetch_begin(&stats->syncp); 460 rx_bytes = stats->bytes; 461 rx_packets = stats->packets; 462 rx_dropped = stats->dropped; 463 } while (u64_stats_fetch_retry(&stats->syncp, start)); 464 465 stats64->rx_bytes += rx_bytes; 466 stats64->rx_packets += rx_packets; 467 stats64->rx_dropped += rx_dropped; 468 } 469 } 470 471 static const struct net_device_ops fbnic_netdev_ops = { 472 .ndo_open = fbnic_open, 473 .ndo_stop = fbnic_stop, 474 .ndo_validate_addr = eth_validate_addr, 475 .ndo_start_xmit = fbnic_xmit_frame, 476 .ndo_features_check = fbnic_features_check, 477 .ndo_set_mac_address = fbnic_set_mac, 478 .ndo_set_rx_mode = fbnic_set_rx_mode, 479 .ndo_get_stats64 = fbnic_get_stats64, 480 .ndo_hwtstamp_get = fbnic_hwtstamp_get, 481 .ndo_hwtstamp_set = fbnic_hwtstamp_set, 482 }; 483 484 static void fbnic_get_queue_stats_rx(struct net_device *dev, int idx, 485 struct netdev_queue_stats_rx *rx) 486 { 487 struct fbnic_net *fbn = netdev_priv(dev); 488 struct fbnic_ring *rxr = fbn->rx[idx]; 489 struct fbnic_queue_stats *stats; 490 u64 bytes, packets, alloc_fail; 491 u64 csum_complete, csum_none; 492 unsigned int start; 493 494 if (!rxr) 495 return; 496 497 stats = &rxr->stats; 498 do { 499 start = u64_stats_fetch_begin(&stats->syncp); 500 bytes = stats->bytes; 501 packets = stats->packets; 502 alloc_fail = stats->rx.alloc_failed; 503 csum_complete = stats->rx.csum_complete; 504 csum_none = stats->rx.csum_none; 505 } while (u64_stats_fetch_retry(&stats->syncp, start)); 506 507 rx->bytes = bytes; 508 rx->packets = packets; 509 rx->alloc_fail = alloc_fail; 510 rx->csum_complete = csum_complete; 511 rx->csum_none = csum_none; 512 } 513 514 static void fbnic_get_queue_stats_tx(struct net_device *dev, int idx, 515 struct netdev_queue_stats_tx *tx) 516 { 517 struct fbnic_net *fbn = netdev_priv(dev); 518 struct fbnic_ring *txr = fbn->tx[idx]; 519 struct fbnic_queue_stats *stats; 520 u64 stop, wake, csum, lso; 521 unsigned int start; 522 u64 bytes, packets; 523 524 if (!txr) 525 return; 526 527 stats = &txr->stats; 528 do { 529 start = u64_stats_fetch_begin(&stats->syncp); 530 bytes = stats->bytes; 531 packets = stats->packets; 532 csum = stats->twq.csum_partial; 533 lso = stats->twq.lso; 534 stop = stats->twq.stop; 535 wake = stats->twq.wake; 536 } while (u64_stats_fetch_retry(&stats->syncp, start)); 537 538 tx->bytes = bytes; 539 tx->packets = packets; 540 tx->needs_csum = csum + lso; 541 tx->hw_gso_wire_packets = lso; 542 tx->stop = stop; 543 tx->wake = wake; 544 } 545 546 static void fbnic_get_base_stats(struct net_device *dev, 547 struct netdev_queue_stats_rx *rx, 548 struct netdev_queue_stats_tx *tx) 549 { 550 struct fbnic_net *fbn = netdev_priv(dev); 551 552 tx->bytes = fbn->tx_stats.bytes; 553 tx->packets = fbn->tx_stats.packets; 554 tx->needs_csum = fbn->tx_stats.twq.csum_partial + fbn->tx_stats.twq.lso; 555 tx->hw_gso_wire_packets = fbn->tx_stats.twq.lso; 556 tx->stop = fbn->tx_stats.twq.stop; 557 tx->wake = fbn->tx_stats.twq.wake; 558 559 rx->bytes = fbn->rx_stats.bytes; 560 rx->packets = fbn->rx_stats.packets; 561 rx->alloc_fail = fbn->rx_stats.rx.alloc_failed; 562 rx->csum_complete = fbn->rx_stats.rx.csum_complete; 563 rx->csum_none = fbn->rx_stats.rx.csum_none; 564 } 565 566 static const struct netdev_stat_ops fbnic_stat_ops = { 567 .get_queue_stats_rx = fbnic_get_queue_stats_rx, 568 .get_queue_stats_tx = fbnic_get_queue_stats_tx, 569 .get_base_stats = fbnic_get_base_stats, 570 }; 571 572 void fbnic_reset_queues(struct fbnic_net *fbn, 573 unsigned int tx, unsigned int rx) 574 { 575 struct fbnic_dev *fbd = fbn->fbd; 576 unsigned int max_napis; 577 578 max_napis = fbd->num_irqs - FBNIC_NON_NAPI_VECTORS; 579 580 tx = min(tx, max_napis); 581 fbn->num_tx_queues = tx; 582 583 rx = min(rx, max_napis); 584 fbn->num_rx_queues = rx; 585 586 fbn->num_napi = max(tx, rx); 587 } 588 589 /** 590 * fbnic_netdev_free - Free the netdev associate with fbnic 591 * @fbd: Driver specific structure to free netdev from 592 * 593 * Allocate and initialize the netdev and netdev private structure. Bind 594 * together the hardware, netdev, and pci data structures. 595 **/ 596 void fbnic_netdev_free(struct fbnic_dev *fbd) 597 { 598 struct fbnic_net *fbn = netdev_priv(fbd->netdev); 599 600 if (fbn->phylink) 601 phylink_destroy(fbn->phylink); 602 603 free_netdev(fbd->netdev); 604 fbd->netdev = NULL; 605 } 606 607 /** 608 * fbnic_netdev_alloc - Allocate a netdev and associate with fbnic 609 * @fbd: Driver specific structure to associate netdev with 610 * 611 * Allocate and initialize the netdev and netdev private structure. Bind 612 * together the hardware, netdev, and pci data structures. 613 * 614 * Return: Pointer to net_device on success, NULL on failure 615 **/ 616 struct net_device *fbnic_netdev_alloc(struct fbnic_dev *fbd) 617 { 618 struct net_device *netdev; 619 struct fbnic_net *fbn; 620 int default_queues; 621 622 netdev = alloc_etherdev_mq(sizeof(*fbn), FBNIC_MAX_RXQS); 623 if (!netdev) 624 return NULL; 625 626 SET_NETDEV_DEV(netdev, fbd->dev); 627 fbd->netdev = netdev; 628 629 netdev->netdev_ops = &fbnic_netdev_ops; 630 netdev->stat_ops = &fbnic_stat_ops; 631 632 fbnic_set_ethtool_ops(netdev); 633 634 fbn = netdev_priv(netdev); 635 636 fbn->netdev = netdev; 637 fbn->fbd = fbd; 638 639 fbn->txq_size = FBNIC_TXQ_SIZE_DEFAULT; 640 fbn->hpq_size = FBNIC_HPQ_SIZE_DEFAULT; 641 fbn->ppq_size = FBNIC_PPQ_SIZE_DEFAULT; 642 fbn->rcq_size = FBNIC_RCQ_SIZE_DEFAULT; 643 644 fbn->tx_usecs = FBNIC_TX_USECS_DEFAULT; 645 fbn->rx_usecs = FBNIC_RX_USECS_DEFAULT; 646 fbn->rx_max_frames = FBNIC_RX_FRAMES_DEFAULT; 647 648 default_queues = netif_get_num_default_rss_queues(); 649 if (default_queues > fbd->max_num_queues) 650 default_queues = fbd->max_num_queues; 651 652 fbnic_reset_queues(fbn, default_queues, default_queues); 653 654 fbnic_reset_indir_tbl(fbn); 655 fbnic_rss_key_fill(fbn->rss_key); 656 fbnic_rss_init_en_mask(fbn); 657 658 netdev->priv_flags |= IFF_UNICAST_FLT; 659 660 netdev->gso_partial_features = 661 NETIF_F_GSO_GRE | 662 NETIF_F_GSO_GRE_CSUM | 663 NETIF_F_GSO_IPXIP4 | 664 NETIF_F_GSO_UDP_TUNNEL | 665 NETIF_F_GSO_UDP_TUNNEL_CSUM; 666 667 netdev->features |= 668 netdev->gso_partial_features | 669 FBNIC_TUN_GSO_FEATURES | 670 NETIF_F_RXHASH | 671 NETIF_F_SG | 672 NETIF_F_HW_CSUM | 673 NETIF_F_RXCSUM | 674 NETIF_F_TSO | 675 NETIF_F_TSO_ECN | 676 NETIF_F_TSO6 | 677 NETIF_F_GSO_PARTIAL | 678 NETIF_F_GSO_UDP_L4; 679 680 netdev->hw_features |= netdev->features; 681 netdev->vlan_features |= netdev->features; 682 netdev->hw_enc_features |= netdev->features; 683 netdev->features |= NETIF_F_NTUPLE; 684 685 netdev->min_mtu = IPV6_MIN_MTU; 686 netdev->max_mtu = FBNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN; 687 688 /* TBD: This is workaround for BMC as phylink doesn't have support 689 * for leavling the link enabled if a BMC is present. 690 */ 691 netdev->ethtool->wol_enabled = true; 692 693 fbn->fec = FBNIC_FEC_AUTO | FBNIC_FEC_RS; 694 fbn->link_mode = FBNIC_LINK_AUTO | FBNIC_LINK_50R2; 695 netif_carrier_off(netdev); 696 697 netif_tx_stop_all_queues(netdev); 698 699 if (fbnic_phylink_init(netdev)) { 700 fbnic_netdev_free(fbd); 701 return NULL; 702 } 703 704 return netdev; 705 } 706 707 static int fbnic_dsn_to_mac_addr(u64 dsn, char *addr) 708 { 709 addr[0] = (dsn >> 56) & 0xFF; 710 addr[1] = (dsn >> 48) & 0xFF; 711 addr[2] = (dsn >> 40) & 0xFF; 712 addr[3] = (dsn >> 16) & 0xFF; 713 addr[4] = (dsn >> 8) & 0xFF; 714 addr[5] = dsn & 0xFF; 715 716 return is_valid_ether_addr(addr) ? 0 : -EINVAL; 717 } 718 719 /** 720 * fbnic_netdev_register - Initialize general software structures 721 * @netdev: Netdev containing structure to initialize and register 722 * 723 * Initialize the MAC address for the netdev and register it. 724 * 725 * Return: 0 on success, negative on failure 726 **/ 727 int fbnic_netdev_register(struct net_device *netdev) 728 { 729 struct fbnic_net *fbn = netdev_priv(netdev); 730 struct fbnic_dev *fbd = fbn->fbd; 731 u64 dsn = fbd->dsn; 732 u8 addr[ETH_ALEN]; 733 int err; 734 735 err = fbnic_dsn_to_mac_addr(dsn, addr); 736 if (!err) { 737 ether_addr_copy(netdev->perm_addr, addr); 738 eth_hw_addr_set(netdev, addr); 739 } else { 740 /* A randomly assigned MAC address will cause provisioning 741 * issues so instead just fail to spawn the netdev and 742 * avoid any confusion. 743 */ 744 dev_err(fbd->dev, "MAC addr %pM invalid\n", addr); 745 return err; 746 } 747 748 return register_netdev(netdev); 749 } 750 751 void fbnic_netdev_unregister(struct net_device *netdev) 752 { 753 unregister_netdev(netdev); 754 } 755