1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* 3 * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved. 4 */ 5 6 #include <linux/pci.h> 7 8 #include "ena_netdev.h" 9 10 struct ena_stats { 11 char name[ETH_GSTRING_LEN]; 12 int stat_offset; 13 }; 14 15 #define ENA_STAT_ENA_COM_ENTRY(stat) { \ 16 .name = #stat, \ 17 .stat_offset = offsetof(struct ena_com_stats_admin, stat) / sizeof(u64) \ 18 } 19 20 #define ENA_STAT_ENTRY(stat, stat_type) { \ 21 .name = #stat, \ 22 .stat_offset = offsetof(struct ena_stats_##stat_type, stat) / sizeof(u64) \ 23 } 24 25 #define ENA_STAT_HW_ENTRY(stat, stat_type) { \ 26 .name = #stat, \ 27 .stat_offset = offsetof(struct ena_admin_##stat_type, stat) / sizeof(u64) \ 28 } 29 30 #define ENA_STAT_RX_ENTRY(stat) \ 31 ENA_STAT_ENTRY(stat, rx) 32 33 #define ENA_STAT_TX_ENTRY(stat) \ 34 ENA_STAT_ENTRY(stat, tx) 35 36 #define ENA_STAT_GLOBAL_ENTRY(stat) \ 37 ENA_STAT_ENTRY(stat, dev) 38 39 #define ENA_STAT_ENI_ENTRY(stat) \ 40 ENA_STAT_HW_ENTRY(stat, eni_stats) 41 42 static const struct ena_stats ena_stats_global_strings[] = { 43 ENA_STAT_GLOBAL_ENTRY(tx_timeout), 44 ENA_STAT_GLOBAL_ENTRY(suspend), 45 ENA_STAT_GLOBAL_ENTRY(resume), 46 ENA_STAT_GLOBAL_ENTRY(wd_expired), 47 ENA_STAT_GLOBAL_ENTRY(interface_up), 48 ENA_STAT_GLOBAL_ENTRY(interface_down), 49 ENA_STAT_GLOBAL_ENTRY(admin_q_pause), 50 }; 51 52 static const struct ena_stats ena_stats_eni_strings[] = { 53 ENA_STAT_ENI_ENTRY(bw_in_allowance_exceeded), 54 ENA_STAT_ENI_ENTRY(bw_out_allowance_exceeded), 55 ENA_STAT_ENI_ENTRY(pps_allowance_exceeded), 56 ENA_STAT_ENI_ENTRY(conntrack_allowance_exceeded), 57 ENA_STAT_ENI_ENTRY(linklocal_allowance_exceeded), 58 }; 59 60 static const struct ena_stats ena_stats_tx_strings[] = { 61 ENA_STAT_TX_ENTRY(cnt), 62 ENA_STAT_TX_ENTRY(bytes), 63 ENA_STAT_TX_ENTRY(queue_stop), 64 ENA_STAT_TX_ENTRY(queue_wakeup), 65 ENA_STAT_TX_ENTRY(dma_mapping_err), 66 ENA_STAT_TX_ENTRY(linearize), 67 ENA_STAT_TX_ENTRY(linearize_failed), 68 ENA_STAT_TX_ENTRY(napi_comp), 69 ENA_STAT_TX_ENTRY(tx_poll), 70 ENA_STAT_TX_ENTRY(doorbells), 71 ENA_STAT_TX_ENTRY(prepare_ctx_err), 72 ENA_STAT_TX_ENTRY(bad_req_id), 73 ENA_STAT_TX_ENTRY(llq_buffer_copy), 74 ENA_STAT_TX_ENTRY(missed_tx), 75 ENA_STAT_TX_ENTRY(unmask_interrupt), 76 }; 77 78 static const struct ena_stats ena_stats_rx_strings[] = { 79 ENA_STAT_RX_ENTRY(cnt), 80 ENA_STAT_RX_ENTRY(bytes), 81 ENA_STAT_RX_ENTRY(rx_copybreak_pkt), 82 ENA_STAT_RX_ENTRY(csum_good), 83 ENA_STAT_RX_ENTRY(refil_partial), 84 ENA_STAT_RX_ENTRY(bad_csum), 85 ENA_STAT_RX_ENTRY(page_alloc_fail), 86 ENA_STAT_RX_ENTRY(skb_alloc_fail), 87 ENA_STAT_RX_ENTRY(dma_mapping_err), 88 ENA_STAT_RX_ENTRY(bad_desc_num), 89 ENA_STAT_RX_ENTRY(bad_req_id), 90 ENA_STAT_RX_ENTRY(empty_rx_ring), 91 ENA_STAT_RX_ENTRY(csum_unchecked), 92 ENA_STAT_RX_ENTRY(xdp_aborted), 93 ENA_STAT_RX_ENTRY(xdp_drop), 94 ENA_STAT_RX_ENTRY(xdp_pass), 95 ENA_STAT_RX_ENTRY(xdp_tx), 96 ENA_STAT_RX_ENTRY(xdp_invalid), 97 }; 98 99 static const struct ena_stats ena_stats_ena_com_strings[] = { 100 ENA_STAT_ENA_COM_ENTRY(aborted_cmd), 101 ENA_STAT_ENA_COM_ENTRY(submitted_cmd), 102 ENA_STAT_ENA_COM_ENTRY(completed_cmd), 103 ENA_STAT_ENA_COM_ENTRY(out_of_space), 104 ENA_STAT_ENA_COM_ENTRY(no_completion), 105 }; 106 107 #define ENA_STATS_ARRAY_GLOBAL ARRAY_SIZE(ena_stats_global_strings) 108 #define ENA_STATS_ARRAY_TX ARRAY_SIZE(ena_stats_tx_strings) 109 #define ENA_STATS_ARRAY_RX ARRAY_SIZE(ena_stats_rx_strings) 110 #define ENA_STATS_ARRAY_ENA_COM ARRAY_SIZE(ena_stats_ena_com_strings) 111 #define ENA_STATS_ARRAY_ENI(adapter) \ 112 (ARRAY_SIZE(ena_stats_eni_strings) * (adapter)->eni_stats_supported) 113 114 static void ena_safe_update_stat(u64 *src, u64 *dst, 115 struct u64_stats_sync *syncp) 116 { 117 unsigned int start; 118 119 do { 120 start = u64_stats_fetch_begin_irq(syncp); 121 *(dst) = *src; 122 } while (u64_stats_fetch_retry_irq(syncp, start)); 123 } 124 125 static void ena_queue_stats(struct ena_adapter *adapter, u64 **data) 126 { 127 const struct ena_stats *ena_stats; 128 struct ena_ring *ring; 129 130 u64 *ptr; 131 int i, j; 132 133 for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) { 134 /* Tx stats */ 135 ring = &adapter->tx_ring[i]; 136 137 for (j = 0; j < ENA_STATS_ARRAY_TX; j++) { 138 ena_stats = &ena_stats_tx_strings[j]; 139 140 ptr = (u64 *)&ring->tx_stats + ena_stats->stat_offset; 141 142 ena_safe_update_stat(ptr, (*data)++, &ring->syncp); 143 } 144 /* XDP TX queues don't have a RX queue counterpart */ 145 if (!ENA_IS_XDP_INDEX(adapter, i)) { 146 /* Rx stats */ 147 ring = &adapter->rx_ring[i]; 148 149 for (j = 0; j < ENA_STATS_ARRAY_RX; j++) { 150 ena_stats = &ena_stats_rx_strings[j]; 151 152 ptr = (u64 *)&ring->rx_stats + 153 ena_stats->stat_offset; 154 155 ena_safe_update_stat(ptr, (*data)++, &ring->syncp); 156 } 157 } 158 } 159 } 160 161 static void ena_dev_admin_queue_stats(struct ena_adapter *adapter, u64 **data) 162 { 163 const struct ena_stats *ena_stats; 164 u64 *ptr; 165 int i; 166 167 for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) { 168 ena_stats = &ena_stats_ena_com_strings[i]; 169 170 ptr = (u64 *)&adapter->ena_dev->admin_queue.stats + 171 ena_stats->stat_offset; 172 173 *(*data)++ = *ptr; 174 } 175 } 176 177 static void ena_get_stats(struct ena_adapter *adapter, 178 u64 *data, 179 bool eni_stats_needed) 180 { 181 const struct ena_stats *ena_stats; 182 u64 *ptr; 183 int i; 184 185 for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) { 186 ena_stats = &ena_stats_global_strings[i]; 187 188 ptr = (u64 *)&adapter->dev_stats + ena_stats->stat_offset; 189 190 ena_safe_update_stat(ptr, data++, &adapter->syncp); 191 } 192 193 if (eni_stats_needed) { 194 ena_update_hw_stats(adapter); 195 for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) { 196 ena_stats = &ena_stats_eni_strings[i]; 197 198 ptr = (u64 *)&adapter->eni_stats + 199 ena_stats->stat_offset; 200 201 ena_safe_update_stat(ptr, data++, &adapter->syncp); 202 } 203 } 204 205 ena_queue_stats(adapter, &data); 206 ena_dev_admin_queue_stats(adapter, &data); 207 } 208 209 static void ena_get_ethtool_stats(struct net_device *netdev, 210 struct ethtool_stats *stats, 211 u64 *data) 212 { 213 struct ena_adapter *adapter = netdev_priv(netdev); 214 215 ena_get_stats(adapter, data, adapter->eni_stats_supported); 216 } 217 218 static int ena_get_sw_stats_count(struct ena_adapter *adapter) 219 { 220 return adapter->num_io_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX) 221 + adapter->xdp_num_queues * ENA_STATS_ARRAY_TX 222 + ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM; 223 } 224 225 static int ena_get_hw_stats_count(struct ena_adapter *adapter) 226 { 227 return ENA_STATS_ARRAY_ENI(adapter); 228 } 229 230 int ena_get_sset_count(struct net_device *netdev, int sset) 231 { 232 struct ena_adapter *adapter = netdev_priv(netdev); 233 234 if (sset != ETH_SS_STATS) 235 return -EOPNOTSUPP; 236 237 return ena_get_sw_stats_count(adapter) + ena_get_hw_stats_count(adapter); 238 } 239 240 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data) 241 { 242 const struct ena_stats *ena_stats; 243 bool is_xdp; 244 int i, j; 245 246 for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) { 247 is_xdp = ENA_IS_XDP_INDEX(adapter, i); 248 /* Tx stats */ 249 for (j = 0; j < ENA_STATS_ARRAY_TX; j++) { 250 ena_stats = &ena_stats_tx_strings[j]; 251 252 snprintf(*data, ETH_GSTRING_LEN, 253 "queue_%u_%s_%s", i, 254 is_xdp ? "xdp_tx" : "tx", ena_stats->name); 255 (*data) += ETH_GSTRING_LEN; 256 } 257 258 if (!is_xdp) { 259 /* RX stats, in XDP there isn't a RX queue 260 * counterpart 261 */ 262 for (j = 0; j < ENA_STATS_ARRAY_RX; j++) { 263 ena_stats = &ena_stats_rx_strings[j]; 264 265 snprintf(*data, ETH_GSTRING_LEN, 266 "queue_%u_rx_%s", i, ena_stats->name); 267 (*data) += ETH_GSTRING_LEN; 268 } 269 } 270 } 271 } 272 273 static void ena_com_dev_strings(u8 **data) 274 { 275 const struct ena_stats *ena_stats; 276 int i; 277 278 for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) { 279 ena_stats = &ena_stats_ena_com_strings[i]; 280 281 snprintf(*data, ETH_GSTRING_LEN, 282 "ena_admin_q_%s", ena_stats->name); 283 (*data) += ETH_GSTRING_LEN; 284 } 285 } 286 287 static void ena_get_strings(struct ena_adapter *adapter, 288 u8 *data, 289 bool eni_stats_needed) 290 { 291 const struct ena_stats *ena_stats; 292 int i; 293 294 for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) { 295 ena_stats = &ena_stats_global_strings[i]; 296 memcpy(data, ena_stats->name, ETH_GSTRING_LEN); 297 data += ETH_GSTRING_LEN; 298 } 299 300 if (eni_stats_needed) { 301 for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) { 302 ena_stats = &ena_stats_eni_strings[i]; 303 memcpy(data, ena_stats->name, ETH_GSTRING_LEN); 304 data += ETH_GSTRING_LEN; 305 } 306 } 307 308 ena_queue_strings(adapter, &data); 309 ena_com_dev_strings(&data); 310 } 311 312 static void ena_get_ethtool_strings(struct net_device *netdev, 313 u32 sset, 314 u8 *data) 315 { 316 struct ena_adapter *adapter = netdev_priv(netdev); 317 318 if (sset != ETH_SS_STATS) 319 return; 320 321 ena_get_strings(adapter, data, adapter->eni_stats_supported); 322 } 323 324 static int ena_get_link_ksettings(struct net_device *netdev, 325 struct ethtool_link_ksettings *link_ksettings) 326 { 327 struct ena_adapter *adapter = netdev_priv(netdev); 328 struct ena_com_dev *ena_dev = adapter->ena_dev; 329 struct ena_admin_get_feature_link_desc *link; 330 struct ena_admin_get_feat_resp feat_resp; 331 int rc; 332 333 rc = ena_com_get_link_params(ena_dev, &feat_resp); 334 if (rc) 335 return rc; 336 337 link = &feat_resp.u.link; 338 link_ksettings->base.speed = link->speed; 339 340 if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) { 341 ethtool_link_ksettings_add_link_mode(link_ksettings, 342 supported, Autoneg); 343 ethtool_link_ksettings_add_link_mode(link_ksettings, 344 supported, Autoneg); 345 } 346 347 link_ksettings->base.autoneg = 348 (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ? 349 AUTONEG_ENABLE : AUTONEG_DISABLE; 350 351 link_ksettings->base.duplex = DUPLEX_FULL; 352 353 return 0; 354 } 355 356 static int ena_get_coalesce(struct net_device *net_dev, 357 struct ethtool_coalesce *coalesce) 358 { 359 struct ena_adapter *adapter = netdev_priv(net_dev); 360 struct ena_com_dev *ena_dev = adapter->ena_dev; 361 362 if (!ena_com_interrupt_moderation_supported(ena_dev)) 363 return -EOPNOTSUPP; 364 365 coalesce->tx_coalesce_usecs = 366 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) * 367 ena_dev->intr_delay_resolution; 368 369 coalesce->rx_coalesce_usecs = 370 ena_com_get_nonadaptive_moderation_interval_rx(ena_dev) 371 * ena_dev->intr_delay_resolution; 372 373 coalesce->use_adaptive_rx_coalesce = 374 ena_com_get_adaptive_moderation_enabled(ena_dev); 375 376 return 0; 377 } 378 379 static void ena_update_tx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter) 380 { 381 unsigned int val; 382 int i; 383 384 val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev); 385 386 for (i = 0; i < adapter->num_io_queues; i++) 387 adapter->tx_ring[i].smoothed_interval = val; 388 } 389 390 static void ena_update_rx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter) 391 { 392 unsigned int val; 393 int i; 394 395 val = ena_com_get_nonadaptive_moderation_interval_rx(adapter->ena_dev); 396 397 for (i = 0; i < adapter->num_io_queues; i++) 398 adapter->rx_ring[i].smoothed_interval = val; 399 } 400 401 static int ena_set_coalesce(struct net_device *net_dev, 402 struct ethtool_coalesce *coalesce) 403 { 404 struct ena_adapter *adapter = netdev_priv(net_dev); 405 struct ena_com_dev *ena_dev = adapter->ena_dev; 406 int rc; 407 408 if (!ena_com_interrupt_moderation_supported(ena_dev)) 409 return -EOPNOTSUPP; 410 411 rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev, 412 coalesce->tx_coalesce_usecs); 413 if (rc) 414 return rc; 415 416 ena_update_tx_rings_nonadaptive_intr_moderation(adapter); 417 418 rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev, 419 coalesce->rx_coalesce_usecs); 420 if (rc) 421 return rc; 422 423 ena_update_rx_rings_nonadaptive_intr_moderation(adapter); 424 425 if (coalesce->use_adaptive_rx_coalesce && 426 !ena_com_get_adaptive_moderation_enabled(ena_dev)) 427 ena_com_enable_adaptive_moderation(ena_dev); 428 429 if (!coalesce->use_adaptive_rx_coalesce && 430 ena_com_get_adaptive_moderation_enabled(ena_dev)) 431 ena_com_disable_adaptive_moderation(ena_dev); 432 433 return 0; 434 } 435 436 static u32 ena_get_msglevel(struct net_device *netdev) 437 { 438 struct ena_adapter *adapter = netdev_priv(netdev); 439 440 return adapter->msg_enable; 441 } 442 443 static void ena_set_msglevel(struct net_device *netdev, u32 value) 444 { 445 struct ena_adapter *adapter = netdev_priv(netdev); 446 447 adapter->msg_enable = value; 448 } 449 450 static void ena_get_drvinfo(struct net_device *dev, 451 struct ethtool_drvinfo *info) 452 { 453 struct ena_adapter *adapter = netdev_priv(dev); 454 455 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); 456 strlcpy(info->bus_info, pci_name(adapter->pdev), 457 sizeof(info->bus_info)); 458 } 459 460 static void ena_get_ringparam(struct net_device *netdev, 461 struct ethtool_ringparam *ring) 462 { 463 struct ena_adapter *adapter = netdev_priv(netdev); 464 465 ring->tx_max_pending = adapter->max_tx_ring_size; 466 ring->rx_max_pending = adapter->max_rx_ring_size; 467 ring->tx_pending = adapter->tx_ring[0].ring_size; 468 ring->rx_pending = adapter->rx_ring[0].ring_size; 469 } 470 471 static int ena_set_ringparam(struct net_device *netdev, 472 struct ethtool_ringparam *ring) 473 { 474 struct ena_adapter *adapter = netdev_priv(netdev); 475 u32 new_tx_size, new_rx_size; 476 477 new_tx_size = ring->tx_pending < ENA_MIN_RING_SIZE ? 478 ENA_MIN_RING_SIZE : ring->tx_pending; 479 new_tx_size = rounddown_pow_of_two(new_tx_size); 480 481 new_rx_size = ring->rx_pending < ENA_MIN_RING_SIZE ? 482 ENA_MIN_RING_SIZE : ring->rx_pending; 483 new_rx_size = rounddown_pow_of_two(new_rx_size); 484 485 if (new_tx_size == adapter->requested_tx_ring_size && 486 new_rx_size == adapter->requested_rx_ring_size) 487 return 0; 488 489 return ena_update_queue_sizes(adapter, new_tx_size, new_rx_size); 490 } 491 492 static u32 ena_flow_hash_to_flow_type(u16 hash_fields) 493 { 494 u32 data = 0; 495 496 if (hash_fields & ENA_ADMIN_RSS_L2_DA) 497 data |= RXH_L2DA; 498 499 if (hash_fields & ENA_ADMIN_RSS_L3_DA) 500 data |= RXH_IP_DST; 501 502 if (hash_fields & ENA_ADMIN_RSS_L3_SA) 503 data |= RXH_IP_SRC; 504 505 if (hash_fields & ENA_ADMIN_RSS_L4_DP) 506 data |= RXH_L4_B_2_3; 507 508 if (hash_fields & ENA_ADMIN_RSS_L4_SP) 509 data |= RXH_L4_B_0_1; 510 511 return data; 512 } 513 514 static u16 ena_flow_data_to_flow_hash(u32 hash_fields) 515 { 516 u16 data = 0; 517 518 if (hash_fields & RXH_L2DA) 519 data |= ENA_ADMIN_RSS_L2_DA; 520 521 if (hash_fields & RXH_IP_DST) 522 data |= ENA_ADMIN_RSS_L3_DA; 523 524 if (hash_fields & RXH_IP_SRC) 525 data |= ENA_ADMIN_RSS_L3_SA; 526 527 if (hash_fields & RXH_L4_B_2_3) 528 data |= ENA_ADMIN_RSS_L4_DP; 529 530 if (hash_fields & RXH_L4_B_0_1) 531 data |= ENA_ADMIN_RSS_L4_SP; 532 533 return data; 534 } 535 536 static int ena_get_rss_hash(struct ena_com_dev *ena_dev, 537 struct ethtool_rxnfc *cmd) 538 { 539 enum ena_admin_flow_hash_proto proto; 540 u16 hash_fields; 541 int rc; 542 543 cmd->data = 0; 544 545 switch (cmd->flow_type) { 546 case TCP_V4_FLOW: 547 proto = ENA_ADMIN_RSS_TCP4; 548 break; 549 case UDP_V4_FLOW: 550 proto = ENA_ADMIN_RSS_UDP4; 551 break; 552 case TCP_V6_FLOW: 553 proto = ENA_ADMIN_RSS_TCP6; 554 break; 555 case UDP_V6_FLOW: 556 proto = ENA_ADMIN_RSS_UDP6; 557 break; 558 case IPV4_FLOW: 559 proto = ENA_ADMIN_RSS_IP4; 560 break; 561 case IPV6_FLOW: 562 proto = ENA_ADMIN_RSS_IP6; 563 break; 564 case ETHER_FLOW: 565 proto = ENA_ADMIN_RSS_NOT_IP; 566 break; 567 case AH_V4_FLOW: 568 case ESP_V4_FLOW: 569 case AH_V6_FLOW: 570 case ESP_V6_FLOW: 571 case SCTP_V4_FLOW: 572 case AH_ESP_V4_FLOW: 573 return -EOPNOTSUPP; 574 default: 575 return -EINVAL; 576 } 577 578 rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields); 579 if (rc) 580 return rc; 581 582 cmd->data = ena_flow_hash_to_flow_type(hash_fields); 583 584 return 0; 585 } 586 587 static int ena_set_rss_hash(struct ena_com_dev *ena_dev, 588 struct ethtool_rxnfc *cmd) 589 { 590 enum ena_admin_flow_hash_proto proto; 591 u16 hash_fields; 592 593 switch (cmd->flow_type) { 594 case TCP_V4_FLOW: 595 proto = ENA_ADMIN_RSS_TCP4; 596 break; 597 case UDP_V4_FLOW: 598 proto = ENA_ADMIN_RSS_UDP4; 599 break; 600 case TCP_V6_FLOW: 601 proto = ENA_ADMIN_RSS_TCP6; 602 break; 603 case UDP_V6_FLOW: 604 proto = ENA_ADMIN_RSS_UDP6; 605 break; 606 case IPV4_FLOW: 607 proto = ENA_ADMIN_RSS_IP4; 608 break; 609 case IPV6_FLOW: 610 proto = ENA_ADMIN_RSS_IP6; 611 break; 612 case ETHER_FLOW: 613 proto = ENA_ADMIN_RSS_NOT_IP; 614 break; 615 case AH_V4_FLOW: 616 case ESP_V4_FLOW: 617 case AH_V6_FLOW: 618 case ESP_V6_FLOW: 619 case SCTP_V4_FLOW: 620 case AH_ESP_V4_FLOW: 621 return -EOPNOTSUPP; 622 default: 623 return -EINVAL; 624 } 625 626 hash_fields = ena_flow_data_to_flow_hash(cmd->data); 627 628 return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields); 629 } 630 631 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info) 632 { 633 struct ena_adapter *adapter = netdev_priv(netdev); 634 int rc = 0; 635 636 switch (info->cmd) { 637 case ETHTOOL_SRXFH: 638 rc = ena_set_rss_hash(adapter->ena_dev, info); 639 break; 640 case ETHTOOL_SRXCLSRLDEL: 641 case ETHTOOL_SRXCLSRLINS: 642 default: 643 netif_err(adapter, drv, netdev, 644 "Command parameter %d is not supported\n", info->cmd); 645 rc = -EOPNOTSUPP; 646 } 647 648 return rc; 649 } 650 651 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info, 652 u32 *rules) 653 { 654 struct ena_adapter *adapter = netdev_priv(netdev); 655 int rc = 0; 656 657 switch (info->cmd) { 658 case ETHTOOL_GRXRINGS: 659 info->data = adapter->num_io_queues; 660 rc = 0; 661 break; 662 case ETHTOOL_GRXFH: 663 rc = ena_get_rss_hash(adapter->ena_dev, info); 664 break; 665 case ETHTOOL_GRXCLSRLCNT: 666 case ETHTOOL_GRXCLSRULE: 667 case ETHTOOL_GRXCLSRLALL: 668 default: 669 netif_err(adapter, drv, netdev, 670 "Command parameter %d is not supported\n", info->cmd); 671 rc = -EOPNOTSUPP; 672 } 673 674 return rc; 675 } 676 677 static u32 ena_get_rxfh_indir_size(struct net_device *netdev) 678 { 679 return ENA_RX_RSS_TABLE_SIZE; 680 } 681 682 static u32 ena_get_rxfh_key_size(struct net_device *netdev) 683 { 684 return ENA_HASH_KEY_SIZE; 685 } 686 687 static int ena_indirection_table_set(struct ena_adapter *adapter, 688 const u32 *indir) 689 { 690 struct ena_com_dev *ena_dev = adapter->ena_dev; 691 int i, rc; 692 693 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) { 694 rc = ena_com_indirect_table_fill_entry(ena_dev, 695 i, 696 ENA_IO_RXQ_IDX(indir[i])); 697 if (unlikely(rc)) { 698 netif_err(adapter, drv, adapter->netdev, 699 "Cannot fill indirect table (index is too large)\n"); 700 return rc; 701 } 702 } 703 704 rc = ena_com_indirect_table_set(ena_dev); 705 if (rc) { 706 netif_err(adapter, drv, adapter->netdev, 707 "Cannot set indirect table\n"); 708 return rc == -EPERM ? -EOPNOTSUPP : rc; 709 } 710 return rc; 711 } 712 713 static int ena_indirection_table_get(struct ena_adapter *adapter, u32 *indir) 714 { 715 struct ena_com_dev *ena_dev = adapter->ena_dev; 716 int i, rc; 717 718 if (!indir) 719 return 0; 720 721 rc = ena_com_indirect_table_get(ena_dev, indir); 722 if (rc) 723 return rc; 724 725 /* Our internal representation of the indices is: even indices 726 * for Tx and uneven indices for Rx. We need to convert the Rx 727 * indices to be consecutive 728 */ 729 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) 730 indir[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(indir[i]); 731 732 return rc; 733 } 734 735 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, 736 u8 *hfunc) 737 { 738 struct ena_adapter *adapter = netdev_priv(netdev); 739 enum ena_admin_hash_functions ena_func; 740 u8 func; 741 int rc; 742 743 rc = ena_indirection_table_get(adapter, indir); 744 if (rc) 745 return rc; 746 747 /* We call this function in order to check if the device 748 * supports getting/setting the hash function. 749 */ 750 rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func); 751 if (rc) { 752 if (rc == -EOPNOTSUPP) 753 rc = 0; 754 755 return rc; 756 } 757 758 rc = ena_com_get_hash_key(adapter->ena_dev, key); 759 if (rc) 760 return rc; 761 762 switch (ena_func) { 763 case ENA_ADMIN_TOEPLITZ: 764 func = ETH_RSS_HASH_TOP; 765 break; 766 case ENA_ADMIN_CRC32: 767 func = ETH_RSS_HASH_CRC32; 768 break; 769 default: 770 netif_err(adapter, drv, netdev, 771 "Command parameter is not supported\n"); 772 return -EOPNOTSUPP; 773 } 774 775 if (hfunc) 776 *hfunc = func; 777 778 return 0; 779 } 780 781 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir, 782 const u8 *key, const u8 hfunc) 783 { 784 struct ena_adapter *adapter = netdev_priv(netdev); 785 struct ena_com_dev *ena_dev = adapter->ena_dev; 786 enum ena_admin_hash_functions func = 0; 787 int rc; 788 789 if (indir) { 790 rc = ena_indirection_table_set(adapter, indir); 791 if (rc) 792 return rc; 793 } 794 795 switch (hfunc) { 796 case ETH_RSS_HASH_NO_CHANGE: 797 func = ena_com_get_current_hash_function(ena_dev); 798 break; 799 case ETH_RSS_HASH_TOP: 800 func = ENA_ADMIN_TOEPLITZ; 801 break; 802 case ETH_RSS_HASH_CRC32: 803 func = ENA_ADMIN_CRC32; 804 break; 805 default: 806 netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n", 807 hfunc); 808 return -EOPNOTSUPP; 809 } 810 811 if (key || func) { 812 rc = ena_com_fill_hash_function(ena_dev, func, key, 813 ENA_HASH_KEY_SIZE, 814 0xFFFFFFFF); 815 if (unlikely(rc)) { 816 netif_err(adapter, drv, netdev, "Cannot fill key\n"); 817 return rc == -EPERM ? -EOPNOTSUPP : rc; 818 } 819 } 820 821 return 0; 822 } 823 824 static void ena_get_channels(struct net_device *netdev, 825 struct ethtool_channels *channels) 826 { 827 struct ena_adapter *adapter = netdev_priv(netdev); 828 829 channels->max_combined = adapter->max_num_io_queues; 830 channels->combined_count = adapter->num_io_queues; 831 } 832 833 static int ena_set_channels(struct net_device *netdev, 834 struct ethtool_channels *channels) 835 { 836 struct ena_adapter *adapter = netdev_priv(netdev); 837 u32 count = channels->combined_count; 838 /* The check for max value is already done in ethtool */ 839 if (count < ENA_MIN_NUM_IO_QUEUES || 840 (ena_xdp_present(adapter) && 841 !ena_xdp_legal_queue_count(adapter, channels->combined_count))) 842 return -EINVAL; 843 844 return ena_update_queue_count(adapter, count); 845 } 846 847 static int ena_get_tunable(struct net_device *netdev, 848 const struct ethtool_tunable *tuna, void *data) 849 { 850 struct ena_adapter *adapter = netdev_priv(netdev); 851 int ret = 0; 852 853 switch (tuna->id) { 854 case ETHTOOL_RX_COPYBREAK: 855 *(u32 *)data = adapter->rx_copybreak; 856 break; 857 default: 858 ret = -EINVAL; 859 break; 860 } 861 862 return ret; 863 } 864 865 static int ena_set_tunable(struct net_device *netdev, 866 const struct ethtool_tunable *tuna, 867 const void *data) 868 { 869 struct ena_adapter *adapter = netdev_priv(netdev); 870 int ret = 0; 871 u32 len; 872 873 switch (tuna->id) { 874 case ETHTOOL_RX_COPYBREAK: 875 len = *(u32 *)data; 876 if (len > adapter->netdev->mtu) { 877 ret = -EINVAL; 878 break; 879 } 880 adapter->rx_copybreak = len; 881 break; 882 default: 883 ret = -EINVAL; 884 break; 885 } 886 887 return ret; 888 } 889 890 static const struct ethtool_ops ena_ethtool_ops = { 891 .supported_coalesce_params = ETHTOOL_COALESCE_USECS | 892 ETHTOOL_COALESCE_USE_ADAPTIVE_RX, 893 .get_link_ksettings = ena_get_link_ksettings, 894 .get_drvinfo = ena_get_drvinfo, 895 .get_msglevel = ena_get_msglevel, 896 .set_msglevel = ena_set_msglevel, 897 .get_link = ethtool_op_get_link, 898 .get_coalesce = ena_get_coalesce, 899 .set_coalesce = ena_set_coalesce, 900 .get_ringparam = ena_get_ringparam, 901 .set_ringparam = ena_set_ringparam, 902 .get_sset_count = ena_get_sset_count, 903 .get_strings = ena_get_ethtool_strings, 904 .get_ethtool_stats = ena_get_ethtool_stats, 905 .get_rxnfc = ena_get_rxnfc, 906 .set_rxnfc = ena_set_rxnfc, 907 .get_rxfh_indir_size = ena_get_rxfh_indir_size, 908 .get_rxfh_key_size = ena_get_rxfh_key_size, 909 .get_rxfh = ena_get_rxfh, 910 .set_rxfh = ena_set_rxfh, 911 .get_channels = ena_get_channels, 912 .set_channels = ena_set_channels, 913 .get_tunable = ena_get_tunable, 914 .set_tunable = ena_set_tunable, 915 .get_ts_info = ethtool_op_get_ts_info, 916 }; 917 918 void ena_set_ethtool_ops(struct net_device *netdev) 919 { 920 netdev->ethtool_ops = &ena_ethtool_ops; 921 } 922 923 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf) 924 { 925 struct net_device *netdev = adapter->netdev; 926 u8 *strings_buf; 927 u64 *data_buf; 928 int strings_num; 929 int i, rc; 930 931 strings_num = ena_get_sw_stats_count(adapter); 932 if (strings_num <= 0) { 933 netif_err(adapter, drv, netdev, "Can't get stats num\n"); 934 return; 935 } 936 937 strings_buf = devm_kcalloc(&adapter->pdev->dev, 938 ETH_GSTRING_LEN, strings_num, 939 GFP_ATOMIC); 940 if (!strings_buf) { 941 netif_err(adapter, drv, netdev, 942 "Failed to allocate strings_buf\n"); 943 return; 944 } 945 946 data_buf = devm_kcalloc(&adapter->pdev->dev, 947 strings_num, sizeof(u64), 948 GFP_ATOMIC); 949 if (!data_buf) { 950 netif_err(adapter, drv, netdev, 951 "Failed to allocate data buf\n"); 952 devm_kfree(&adapter->pdev->dev, strings_buf); 953 return; 954 } 955 956 ena_get_strings(adapter, strings_buf, false); 957 ena_get_stats(adapter, data_buf, false); 958 959 /* If there is a buffer, dump stats, otherwise print them to dmesg */ 960 if (buf) 961 for (i = 0; i < strings_num; i++) { 962 rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64), 963 "%s %llu\n", 964 strings_buf + i * ETH_GSTRING_LEN, 965 data_buf[i]); 966 buf += rc; 967 } 968 else 969 for (i = 0; i < strings_num; i++) 970 netif_err(adapter, drv, netdev, "%s: %llu\n", 971 strings_buf + i * ETH_GSTRING_LEN, 972 data_buf[i]); 973 974 devm_kfree(&adapter->pdev->dev, strings_buf); 975 devm_kfree(&adapter->pdev->dev, data_buf); 976 } 977 978 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf) 979 { 980 if (!buf) 981 return; 982 983 ena_dump_stats_ex(adapter, buf); 984 } 985 986 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter) 987 { 988 ena_dump_stats_ex(adapter, NULL); 989 } 990