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