Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
0002 /*
0003  * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
0004  */
0005 
0006 #include <linux/ethtool.h>
0007 #include <linux/pci.h>
0008 
0009 #include "ena_netdev.h"
0010 
0011 struct ena_stats {
0012     char name[ETH_GSTRING_LEN];
0013     int stat_offset;
0014 };
0015 
0016 #define ENA_STAT_ENA_COM_ENTRY(stat) { \
0017     .name = #stat, \
0018     .stat_offset = offsetof(struct ena_com_stats_admin, stat) / sizeof(u64) \
0019 }
0020 
0021 #define ENA_STAT_ENTRY(stat, stat_type) { \
0022     .name = #stat, \
0023     .stat_offset = offsetof(struct ena_stats_##stat_type, stat) / sizeof(u64) \
0024 }
0025 
0026 #define ENA_STAT_HW_ENTRY(stat, stat_type) { \
0027     .name = #stat, \
0028     .stat_offset = offsetof(struct ena_admin_##stat_type, stat) / sizeof(u64) \
0029 }
0030 
0031 #define ENA_STAT_RX_ENTRY(stat) \
0032     ENA_STAT_ENTRY(stat, rx)
0033 
0034 #define ENA_STAT_TX_ENTRY(stat) \
0035     ENA_STAT_ENTRY(stat, tx)
0036 
0037 #define ENA_STAT_GLOBAL_ENTRY(stat) \
0038     ENA_STAT_ENTRY(stat, dev)
0039 
0040 #define ENA_STAT_ENI_ENTRY(stat) \
0041     ENA_STAT_HW_ENTRY(stat, eni_stats)
0042 
0043 static const struct ena_stats ena_stats_global_strings[] = {
0044     ENA_STAT_GLOBAL_ENTRY(tx_timeout),
0045     ENA_STAT_GLOBAL_ENTRY(suspend),
0046     ENA_STAT_GLOBAL_ENTRY(resume),
0047     ENA_STAT_GLOBAL_ENTRY(wd_expired),
0048     ENA_STAT_GLOBAL_ENTRY(interface_up),
0049     ENA_STAT_GLOBAL_ENTRY(interface_down),
0050     ENA_STAT_GLOBAL_ENTRY(admin_q_pause),
0051 };
0052 
0053 static const struct ena_stats ena_stats_eni_strings[] = {
0054     ENA_STAT_ENI_ENTRY(bw_in_allowance_exceeded),
0055     ENA_STAT_ENI_ENTRY(bw_out_allowance_exceeded),
0056     ENA_STAT_ENI_ENTRY(pps_allowance_exceeded),
0057     ENA_STAT_ENI_ENTRY(conntrack_allowance_exceeded),
0058     ENA_STAT_ENI_ENTRY(linklocal_allowance_exceeded),
0059 };
0060 
0061 static const struct ena_stats ena_stats_tx_strings[] = {
0062     ENA_STAT_TX_ENTRY(cnt),
0063     ENA_STAT_TX_ENTRY(bytes),
0064     ENA_STAT_TX_ENTRY(queue_stop),
0065     ENA_STAT_TX_ENTRY(queue_wakeup),
0066     ENA_STAT_TX_ENTRY(dma_mapping_err),
0067     ENA_STAT_TX_ENTRY(linearize),
0068     ENA_STAT_TX_ENTRY(linearize_failed),
0069     ENA_STAT_TX_ENTRY(napi_comp),
0070     ENA_STAT_TX_ENTRY(tx_poll),
0071     ENA_STAT_TX_ENTRY(doorbells),
0072     ENA_STAT_TX_ENTRY(prepare_ctx_err),
0073     ENA_STAT_TX_ENTRY(bad_req_id),
0074     ENA_STAT_TX_ENTRY(llq_buffer_copy),
0075     ENA_STAT_TX_ENTRY(missed_tx),
0076     ENA_STAT_TX_ENTRY(unmask_interrupt),
0077 };
0078 
0079 static const struct ena_stats ena_stats_rx_strings[] = {
0080     ENA_STAT_RX_ENTRY(cnt),
0081     ENA_STAT_RX_ENTRY(bytes),
0082     ENA_STAT_RX_ENTRY(rx_copybreak_pkt),
0083     ENA_STAT_RX_ENTRY(csum_good),
0084     ENA_STAT_RX_ENTRY(refil_partial),
0085     ENA_STAT_RX_ENTRY(csum_bad),
0086     ENA_STAT_RX_ENTRY(page_alloc_fail),
0087     ENA_STAT_RX_ENTRY(skb_alloc_fail),
0088     ENA_STAT_RX_ENTRY(dma_mapping_err),
0089     ENA_STAT_RX_ENTRY(bad_desc_num),
0090     ENA_STAT_RX_ENTRY(bad_req_id),
0091     ENA_STAT_RX_ENTRY(empty_rx_ring),
0092     ENA_STAT_RX_ENTRY(csum_unchecked),
0093     ENA_STAT_RX_ENTRY(xdp_aborted),
0094     ENA_STAT_RX_ENTRY(xdp_drop),
0095     ENA_STAT_RX_ENTRY(xdp_pass),
0096     ENA_STAT_RX_ENTRY(xdp_tx),
0097     ENA_STAT_RX_ENTRY(xdp_invalid),
0098     ENA_STAT_RX_ENTRY(xdp_redirect),
0099 };
0100 
0101 static const struct ena_stats ena_stats_ena_com_strings[] = {
0102     ENA_STAT_ENA_COM_ENTRY(aborted_cmd),
0103     ENA_STAT_ENA_COM_ENTRY(submitted_cmd),
0104     ENA_STAT_ENA_COM_ENTRY(completed_cmd),
0105     ENA_STAT_ENA_COM_ENTRY(out_of_space),
0106     ENA_STAT_ENA_COM_ENTRY(no_completion),
0107 };
0108 
0109 #define ENA_STATS_ARRAY_GLOBAL      ARRAY_SIZE(ena_stats_global_strings)
0110 #define ENA_STATS_ARRAY_TX      ARRAY_SIZE(ena_stats_tx_strings)
0111 #define ENA_STATS_ARRAY_RX      ARRAY_SIZE(ena_stats_rx_strings)
0112 #define ENA_STATS_ARRAY_ENA_COM     ARRAY_SIZE(ena_stats_ena_com_strings)
0113 #define ENA_STATS_ARRAY_ENI(adapter)    ARRAY_SIZE(ena_stats_eni_strings)
0114 
0115 static void ena_safe_update_stat(u64 *src, u64 *dst,
0116                  struct u64_stats_sync *syncp)
0117 {
0118     unsigned int start;
0119 
0120     do {
0121         start = u64_stats_fetch_begin_irq(syncp);
0122         *(dst) = *src;
0123     } while (u64_stats_fetch_retry_irq(syncp, start));
0124 }
0125 
0126 static void ena_queue_stats(struct ena_adapter *adapter, u64 **data)
0127 {
0128     const struct ena_stats *ena_stats;
0129     struct ena_ring *ring;
0130 
0131     u64 *ptr;
0132     int i, j;
0133 
0134     for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
0135         /* Tx stats */
0136         ring = &adapter->tx_ring[i];
0137 
0138         for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
0139             ena_stats = &ena_stats_tx_strings[j];
0140 
0141             ptr = (u64 *)&ring->tx_stats + ena_stats->stat_offset;
0142 
0143             ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
0144         }
0145         /* XDP TX queues don't have a RX queue counterpart */
0146         if (!ENA_IS_XDP_INDEX(adapter, i)) {
0147             /* Rx stats */
0148             ring = &adapter->rx_ring[i];
0149 
0150             for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
0151                 ena_stats = &ena_stats_rx_strings[j];
0152 
0153                 ptr = (u64 *)&ring->rx_stats +
0154                     ena_stats->stat_offset;
0155 
0156                 ena_safe_update_stat(ptr, (*data)++, &ring->syncp);
0157             }
0158         }
0159     }
0160 }
0161 
0162 static void ena_dev_admin_queue_stats(struct ena_adapter *adapter, u64 **data)
0163 {
0164     const struct ena_stats *ena_stats;
0165     u64 *ptr;
0166     int i;
0167 
0168     for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
0169         ena_stats = &ena_stats_ena_com_strings[i];
0170 
0171         ptr = (u64 *)&adapter->ena_dev->admin_queue.stats +
0172             ena_stats->stat_offset;
0173 
0174         *(*data)++ = *ptr;
0175     }
0176 }
0177 
0178 static void ena_get_stats(struct ena_adapter *adapter,
0179               u64 *data,
0180               bool eni_stats_needed)
0181 {
0182     const struct ena_stats *ena_stats;
0183     u64 *ptr;
0184     int i;
0185 
0186     for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
0187         ena_stats = &ena_stats_global_strings[i];
0188 
0189         ptr = (u64 *)&adapter->dev_stats + ena_stats->stat_offset;
0190 
0191         ena_safe_update_stat(ptr, data++, &adapter->syncp);
0192     }
0193 
0194     if (eni_stats_needed) {
0195         ena_update_hw_stats(adapter);
0196         for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) {
0197             ena_stats = &ena_stats_eni_strings[i];
0198 
0199             ptr = (u64 *)&adapter->eni_stats +
0200                 ena_stats->stat_offset;
0201 
0202             ena_safe_update_stat(ptr, data++, &adapter->syncp);
0203         }
0204     }
0205 
0206     ena_queue_stats(adapter, &data);
0207     ena_dev_admin_queue_stats(adapter, &data);
0208 }
0209 
0210 static void ena_get_ethtool_stats(struct net_device *netdev,
0211                   struct ethtool_stats *stats,
0212                   u64 *data)
0213 {
0214     struct ena_adapter *adapter = netdev_priv(netdev);
0215     struct ena_com_dev *dev = adapter->ena_dev;
0216 
0217     ena_get_stats(adapter, data, ena_com_get_cap(dev, ENA_ADMIN_ENI_STATS));
0218 }
0219 
0220 static int ena_get_sw_stats_count(struct ena_adapter *adapter)
0221 {
0222     return adapter->num_io_queues * (ENA_STATS_ARRAY_TX + ENA_STATS_ARRAY_RX)
0223         + adapter->xdp_num_queues * ENA_STATS_ARRAY_TX
0224         + ENA_STATS_ARRAY_GLOBAL + ENA_STATS_ARRAY_ENA_COM;
0225 }
0226 
0227 static int ena_get_hw_stats_count(struct ena_adapter *adapter)
0228 {
0229     bool supported = ena_com_get_cap(adapter->ena_dev, ENA_ADMIN_ENI_STATS);
0230 
0231     return ENA_STATS_ARRAY_ENI(adapter) * supported;
0232 }
0233 
0234 int ena_get_sset_count(struct net_device *netdev, int sset)
0235 {
0236     struct ena_adapter *adapter = netdev_priv(netdev);
0237 
0238     switch (sset) {
0239     case ETH_SS_STATS:
0240         return ena_get_sw_stats_count(adapter) +
0241                ena_get_hw_stats_count(adapter);
0242     }
0243 
0244     return -EOPNOTSUPP;
0245 }
0246 
0247 static void ena_queue_strings(struct ena_adapter *adapter, u8 **data)
0248 {
0249     const struct ena_stats *ena_stats;
0250     bool is_xdp;
0251     int i, j;
0252 
0253     for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
0254         is_xdp = ENA_IS_XDP_INDEX(adapter, i);
0255         /* Tx stats */
0256         for (j = 0; j < ENA_STATS_ARRAY_TX; j++) {
0257             ena_stats = &ena_stats_tx_strings[j];
0258 
0259             ethtool_sprintf(data,
0260                     "queue_%u_%s_%s", i,
0261                     is_xdp ? "xdp_tx" : "tx",
0262                     ena_stats->name);
0263         }
0264 
0265         if (!is_xdp) {
0266             /* RX stats, in XDP there isn't a RX queue
0267              * counterpart
0268              */
0269             for (j = 0; j < ENA_STATS_ARRAY_RX; j++) {
0270                 ena_stats = &ena_stats_rx_strings[j];
0271 
0272                 ethtool_sprintf(data,
0273                         "queue_%u_rx_%s", i,
0274                         ena_stats->name);
0275             }
0276         }
0277     }
0278 }
0279 
0280 static void ena_com_dev_strings(u8 **data)
0281 {
0282     const struct ena_stats *ena_stats;
0283     int i;
0284 
0285     for (i = 0; i < ENA_STATS_ARRAY_ENA_COM; i++) {
0286         ena_stats = &ena_stats_ena_com_strings[i];
0287 
0288         ethtool_sprintf(data,
0289                 "ena_admin_q_%s", ena_stats->name);
0290     }
0291 }
0292 
0293 static void ena_get_strings(struct ena_adapter *adapter,
0294                 u8 *data,
0295                 bool eni_stats_needed)
0296 {
0297     const struct ena_stats *ena_stats;
0298     int i;
0299 
0300     for (i = 0; i < ENA_STATS_ARRAY_GLOBAL; i++) {
0301         ena_stats = &ena_stats_global_strings[i];
0302         ethtool_sprintf(&data, ena_stats->name);
0303     }
0304 
0305     if (eni_stats_needed) {
0306         for (i = 0; i < ENA_STATS_ARRAY_ENI(adapter); i++) {
0307             ena_stats = &ena_stats_eni_strings[i];
0308             ethtool_sprintf(&data, ena_stats->name);
0309         }
0310     }
0311 
0312     ena_queue_strings(adapter, &data);
0313     ena_com_dev_strings(&data);
0314 }
0315 
0316 static void ena_get_ethtool_strings(struct net_device *netdev,
0317                     u32 sset,
0318                     u8 *data)
0319 {
0320     struct ena_adapter *adapter = netdev_priv(netdev);
0321     struct ena_com_dev *dev = adapter->ena_dev;
0322 
0323     switch (sset) {
0324     case ETH_SS_STATS:
0325         ena_get_strings(adapter, data, ena_com_get_cap(dev, ENA_ADMIN_ENI_STATS));
0326         break;
0327     }
0328 }
0329 
0330 static int ena_get_link_ksettings(struct net_device *netdev,
0331                   struct ethtool_link_ksettings *link_ksettings)
0332 {
0333     struct ena_adapter *adapter = netdev_priv(netdev);
0334     struct ena_com_dev *ena_dev = adapter->ena_dev;
0335     struct ena_admin_get_feature_link_desc *link;
0336     struct ena_admin_get_feat_resp feat_resp;
0337     int rc;
0338 
0339     rc = ena_com_get_link_params(ena_dev, &feat_resp);
0340     if (rc)
0341         return rc;
0342 
0343     link = &feat_resp.u.link;
0344     link_ksettings->base.speed = link->speed;
0345 
0346     if (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) {
0347         ethtool_link_ksettings_add_link_mode(link_ksettings,
0348                              supported, Autoneg);
0349         ethtool_link_ksettings_add_link_mode(link_ksettings,
0350                              supported, Autoneg);
0351     }
0352 
0353     link_ksettings->base.autoneg =
0354         (link->flags & ENA_ADMIN_GET_FEATURE_LINK_DESC_AUTONEG_MASK) ?
0355         AUTONEG_ENABLE : AUTONEG_DISABLE;
0356 
0357     link_ksettings->base.duplex = DUPLEX_FULL;
0358 
0359     return 0;
0360 }
0361 
0362 static int ena_get_coalesce(struct net_device *net_dev,
0363                 struct ethtool_coalesce *coalesce,
0364                 struct kernel_ethtool_coalesce *kernel_coal,
0365                 struct netlink_ext_ack *extack)
0366 {
0367     struct ena_adapter *adapter = netdev_priv(net_dev);
0368     struct ena_com_dev *ena_dev = adapter->ena_dev;
0369 
0370     if (!ena_com_interrupt_moderation_supported(ena_dev))
0371         return -EOPNOTSUPP;
0372 
0373     coalesce->tx_coalesce_usecs =
0374         ena_com_get_nonadaptive_moderation_interval_tx(ena_dev) *
0375             ena_dev->intr_delay_resolution;
0376 
0377     coalesce->rx_coalesce_usecs =
0378         ena_com_get_nonadaptive_moderation_interval_rx(ena_dev)
0379         * ena_dev->intr_delay_resolution;
0380 
0381     coalesce->use_adaptive_rx_coalesce =
0382         ena_com_get_adaptive_moderation_enabled(ena_dev);
0383 
0384     return 0;
0385 }
0386 
0387 static void ena_update_tx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter)
0388 {
0389     unsigned int val;
0390     int i;
0391 
0392     val = ena_com_get_nonadaptive_moderation_interval_tx(adapter->ena_dev);
0393 
0394     for (i = 0; i < adapter->num_io_queues; i++)
0395         adapter->tx_ring[i].smoothed_interval = val;
0396 }
0397 
0398 static void ena_update_rx_rings_nonadaptive_intr_moderation(struct ena_adapter *adapter)
0399 {
0400     unsigned int val;
0401     int i;
0402 
0403     val = ena_com_get_nonadaptive_moderation_interval_rx(adapter->ena_dev);
0404 
0405     for (i = 0; i < adapter->num_io_queues; i++)
0406         adapter->rx_ring[i].smoothed_interval = val;
0407 }
0408 
0409 static int ena_set_coalesce(struct net_device *net_dev,
0410                 struct ethtool_coalesce *coalesce,
0411                 struct kernel_ethtool_coalesce *kernel_coal,
0412                 struct netlink_ext_ack *extack)
0413 {
0414     struct ena_adapter *adapter = netdev_priv(net_dev);
0415     struct ena_com_dev *ena_dev = adapter->ena_dev;
0416     int rc;
0417 
0418     if (!ena_com_interrupt_moderation_supported(ena_dev))
0419         return -EOPNOTSUPP;
0420 
0421     rc = ena_com_update_nonadaptive_moderation_interval_tx(ena_dev,
0422                                    coalesce->tx_coalesce_usecs);
0423     if (rc)
0424         return rc;
0425 
0426     ena_update_tx_rings_nonadaptive_intr_moderation(adapter);
0427 
0428     rc = ena_com_update_nonadaptive_moderation_interval_rx(ena_dev,
0429                                    coalesce->rx_coalesce_usecs);
0430     if (rc)
0431         return rc;
0432 
0433     ena_update_rx_rings_nonadaptive_intr_moderation(adapter);
0434 
0435     if (coalesce->use_adaptive_rx_coalesce &&
0436         !ena_com_get_adaptive_moderation_enabled(ena_dev))
0437         ena_com_enable_adaptive_moderation(ena_dev);
0438 
0439     if (!coalesce->use_adaptive_rx_coalesce &&
0440         ena_com_get_adaptive_moderation_enabled(ena_dev))
0441         ena_com_disable_adaptive_moderation(ena_dev);
0442 
0443     return 0;
0444 }
0445 
0446 static u32 ena_get_msglevel(struct net_device *netdev)
0447 {
0448     struct ena_adapter *adapter = netdev_priv(netdev);
0449 
0450     return adapter->msg_enable;
0451 }
0452 
0453 static void ena_set_msglevel(struct net_device *netdev, u32 value)
0454 {
0455     struct ena_adapter *adapter = netdev_priv(netdev);
0456 
0457     adapter->msg_enable = value;
0458 }
0459 
0460 static void ena_get_drvinfo(struct net_device *dev,
0461                 struct ethtool_drvinfo *info)
0462 {
0463     struct ena_adapter *adapter = netdev_priv(dev);
0464 
0465     strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
0466     strlcpy(info->bus_info, pci_name(adapter->pdev),
0467         sizeof(info->bus_info));
0468 }
0469 
0470 static void ena_get_ringparam(struct net_device *netdev,
0471                   struct ethtool_ringparam *ring,
0472                   struct kernel_ethtool_ringparam *kernel_ring,
0473                   struct netlink_ext_ack *extack)
0474 {
0475     struct ena_adapter *adapter = netdev_priv(netdev);
0476 
0477     ring->tx_max_pending = adapter->max_tx_ring_size;
0478     ring->rx_max_pending = adapter->max_rx_ring_size;
0479     ring->tx_pending = adapter->tx_ring[0].ring_size;
0480     ring->rx_pending = adapter->rx_ring[0].ring_size;
0481 }
0482 
0483 static int ena_set_ringparam(struct net_device *netdev,
0484                  struct ethtool_ringparam *ring,
0485                  struct kernel_ethtool_ringparam *kernel_ring,
0486                  struct netlink_ext_ack *extack)
0487 {
0488     struct ena_adapter *adapter = netdev_priv(netdev);
0489     u32 new_tx_size, new_rx_size;
0490 
0491     new_tx_size = ring->tx_pending < ENA_MIN_RING_SIZE ?
0492             ENA_MIN_RING_SIZE : ring->tx_pending;
0493     new_tx_size = rounddown_pow_of_two(new_tx_size);
0494 
0495     new_rx_size = ring->rx_pending < ENA_MIN_RING_SIZE ?
0496             ENA_MIN_RING_SIZE : ring->rx_pending;
0497     new_rx_size = rounddown_pow_of_two(new_rx_size);
0498 
0499     if (new_tx_size == adapter->requested_tx_ring_size &&
0500         new_rx_size == adapter->requested_rx_ring_size)
0501         return 0;
0502 
0503     return ena_update_queue_sizes(adapter, new_tx_size, new_rx_size);
0504 }
0505 
0506 static u32 ena_flow_hash_to_flow_type(u16 hash_fields)
0507 {
0508     u32 data = 0;
0509 
0510     if (hash_fields & ENA_ADMIN_RSS_L2_DA)
0511         data |= RXH_L2DA;
0512 
0513     if (hash_fields & ENA_ADMIN_RSS_L3_DA)
0514         data |= RXH_IP_DST;
0515 
0516     if (hash_fields & ENA_ADMIN_RSS_L3_SA)
0517         data |= RXH_IP_SRC;
0518 
0519     if (hash_fields & ENA_ADMIN_RSS_L4_DP)
0520         data |= RXH_L4_B_2_3;
0521 
0522     if (hash_fields & ENA_ADMIN_RSS_L4_SP)
0523         data |= RXH_L4_B_0_1;
0524 
0525     return data;
0526 }
0527 
0528 static u16 ena_flow_data_to_flow_hash(u32 hash_fields)
0529 {
0530     u16 data = 0;
0531 
0532     if (hash_fields & RXH_L2DA)
0533         data |= ENA_ADMIN_RSS_L2_DA;
0534 
0535     if (hash_fields & RXH_IP_DST)
0536         data |= ENA_ADMIN_RSS_L3_DA;
0537 
0538     if (hash_fields & RXH_IP_SRC)
0539         data |= ENA_ADMIN_RSS_L3_SA;
0540 
0541     if (hash_fields & RXH_L4_B_2_3)
0542         data |= ENA_ADMIN_RSS_L4_DP;
0543 
0544     if (hash_fields & RXH_L4_B_0_1)
0545         data |= ENA_ADMIN_RSS_L4_SP;
0546 
0547     return data;
0548 }
0549 
0550 static int ena_get_rss_hash(struct ena_com_dev *ena_dev,
0551                 struct ethtool_rxnfc *cmd)
0552 {
0553     enum ena_admin_flow_hash_proto proto;
0554     u16 hash_fields;
0555     int rc;
0556 
0557     cmd->data = 0;
0558 
0559     switch (cmd->flow_type) {
0560     case TCP_V4_FLOW:
0561         proto = ENA_ADMIN_RSS_TCP4;
0562         break;
0563     case UDP_V4_FLOW:
0564         proto = ENA_ADMIN_RSS_UDP4;
0565         break;
0566     case TCP_V6_FLOW:
0567         proto = ENA_ADMIN_RSS_TCP6;
0568         break;
0569     case UDP_V6_FLOW:
0570         proto = ENA_ADMIN_RSS_UDP6;
0571         break;
0572     case IPV4_FLOW:
0573         proto = ENA_ADMIN_RSS_IP4;
0574         break;
0575     case IPV6_FLOW:
0576         proto = ENA_ADMIN_RSS_IP6;
0577         break;
0578     case ETHER_FLOW:
0579         proto = ENA_ADMIN_RSS_NOT_IP;
0580         break;
0581     case AH_V4_FLOW:
0582     case ESP_V4_FLOW:
0583     case AH_V6_FLOW:
0584     case ESP_V6_FLOW:
0585     case SCTP_V4_FLOW:
0586     case AH_ESP_V4_FLOW:
0587         return -EOPNOTSUPP;
0588     default:
0589         return -EINVAL;
0590     }
0591 
0592     rc = ena_com_get_hash_ctrl(ena_dev, proto, &hash_fields);
0593     if (rc)
0594         return rc;
0595 
0596     cmd->data = ena_flow_hash_to_flow_type(hash_fields);
0597 
0598     return 0;
0599 }
0600 
0601 static int ena_set_rss_hash(struct ena_com_dev *ena_dev,
0602                 struct ethtool_rxnfc *cmd)
0603 {
0604     enum ena_admin_flow_hash_proto proto;
0605     u16 hash_fields;
0606 
0607     switch (cmd->flow_type) {
0608     case TCP_V4_FLOW:
0609         proto = ENA_ADMIN_RSS_TCP4;
0610         break;
0611     case UDP_V4_FLOW:
0612         proto = ENA_ADMIN_RSS_UDP4;
0613         break;
0614     case TCP_V6_FLOW:
0615         proto = ENA_ADMIN_RSS_TCP6;
0616         break;
0617     case UDP_V6_FLOW:
0618         proto = ENA_ADMIN_RSS_UDP6;
0619         break;
0620     case IPV4_FLOW:
0621         proto = ENA_ADMIN_RSS_IP4;
0622         break;
0623     case IPV6_FLOW:
0624         proto = ENA_ADMIN_RSS_IP6;
0625         break;
0626     case ETHER_FLOW:
0627         proto = ENA_ADMIN_RSS_NOT_IP;
0628         break;
0629     case AH_V4_FLOW:
0630     case ESP_V4_FLOW:
0631     case AH_V6_FLOW:
0632     case ESP_V6_FLOW:
0633     case SCTP_V4_FLOW:
0634     case AH_ESP_V4_FLOW:
0635         return -EOPNOTSUPP;
0636     default:
0637         return -EINVAL;
0638     }
0639 
0640     hash_fields = ena_flow_data_to_flow_hash(cmd->data);
0641 
0642     return ena_com_fill_hash_ctrl(ena_dev, proto, hash_fields);
0643 }
0644 
0645 static int ena_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
0646 {
0647     struct ena_adapter *adapter = netdev_priv(netdev);
0648     int rc = 0;
0649 
0650     switch (info->cmd) {
0651     case ETHTOOL_SRXFH:
0652         rc = ena_set_rss_hash(adapter->ena_dev, info);
0653         break;
0654     case ETHTOOL_SRXCLSRLDEL:
0655     case ETHTOOL_SRXCLSRLINS:
0656     default:
0657         netif_err(adapter, drv, netdev,
0658               "Command parameter %d is not supported\n", info->cmd);
0659         rc = -EOPNOTSUPP;
0660     }
0661 
0662     return rc;
0663 }
0664 
0665 static int ena_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
0666              u32 *rules)
0667 {
0668     struct ena_adapter *adapter = netdev_priv(netdev);
0669     int rc = 0;
0670 
0671     switch (info->cmd) {
0672     case ETHTOOL_GRXRINGS:
0673         info->data = adapter->num_io_queues;
0674         rc = 0;
0675         break;
0676     case ETHTOOL_GRXFH:
0677         rc = ena_get_rss_hash(adapter->ena_dev, info);
0678         break;
0679     case ETHTOOL_GRXCLSRLCNT:
0680     case ETHTOOL_GRXCLSRULE:
0681     case ETHTOOL_GRXCLSRLALL:
0682     default:
0683         netif_err(adapter, drv, netdev,
0684               "Command parameter %d is not supported\n", info->cmd);
0685         rc = -EOPNOTSUPP;
0686     }
0687 
0688     return rc;
0689 }
0690 
0691 static u32 ena_get_rxfh_indir_size(struct net_device *netdev)
0692 {
0693     return ENA_RX_RSS_TABLE_SIZE;
0694 }
0695 
0696 static u32 ena_get_rxfh_key_size(struct net_device *netdev)
0697 {
0698     return ENA_HASH_KEY_SIZE;
0699 }
0700 
0701 static int ena_indirection_table_set(struct ena_adapter *adapter,
0702                      const u32 *indir)
0703 {
0704     struct ena_com_dev *ena_dev = adapter->ena_dev;
0705     int i, rc;
0706 
0707     for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
0708         rc = ena_com_indirect_table_fill_entry(ena_dev,
0709                                i,
0710                                ENA_IO_RXQ_IDX(indir[i]));
0711         if (unlikely(rc)) {
0712             netif_err(adapter, drv, adapter->netdev,
0713                   "Cannot fill indirect table (index is too large)\n");
0714             return rc;
0715         }
0716     }
0717 
0718     rc = ena_com_indirect_table_set(ena_dev);
0719     if (rc) {
0720         netif_err(adapter, drv, adapter->netdev,
0721               "Cannot set indirect table\n");
0722         return rc == -EPERM ? -EOPNOTSUPP : rc;
0723     }
0724     return rc;
0725 }
0726 
0727 static int ena_indirection_table_get(struct ena_adapter *adapter, u32 *indir)
0728 {
0729     struct ena_com_dev *ena_dev = adapter->ena_dev;
0730     int i, rc;
0731 
0732     if (!indir)
0733         return 0;
0734 
0735     rc = ena_com_indirect_table_get(ena_dev, indir);
0736     if (rc)
0737         return rc;
0738 
0739     /* Our internal representation of the indices is: even indices
0740      * for Tx and uneven indices for Rx. We need to convert the Rx
0741      * indices to be consecutive
0742      */
0743     for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++)
0744         indir[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(indir[i]);
0745 
0746     return rc;
0747 }
0748 
0749 static int ena_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
0750             u8 *hfunc)
0751 {
0752     struct ena_adapter *adapter = netdev_priv(netdev);
0753     enum ena_admin_hash_functions ena_func;
0754     u8 func;
0755     int rc;
0756 
0757     rc = ena_indirection_table_get(adapter, indir);
0758     if (rc)
0759         return rc;
0760 
0761     /* We call this function in order to check if the device
0762      * supports getting/setting the hash function.
0763      */
0764     rc = ena_com_get_hash_function(adapter->ena_dev, &ena_func);
0765     if (rc) {
0766         if (rc == -EOPNOTSUPP)
0767             rc = 0;
0768 
0769         return rc;
0770     }
0771 
0772     rc = ena_com_get_hash_key(adapter->ena_dev, key);
0773     if (rc)
0774         return rc;
0775 
0776     switch (ena_func) {
0777     case ENA_ADMIN_TOEPLITZ:
0778         func = ETH_RSS_HASH_TOP;
0779         break;
0780     case ENA_ADMIN_CRC32:
0781         func = ETH_RSS_HASH_CRC32;
0782         break;
0783     default:
0784         netif_err(adapter, drv, netdev,
0785               "Command parameter is not supported\n");
0786         return -EOPNOTSUPP;
0787     }
0788 
0789     if (hfunc)
0790         *hfunc = func;
0791 
0792     return 0;
0793 }
0794 
0795 static int ena_set_rxfh(struct net_device *netdev, const u32 *indir,
0796             const u8 *key, const u8 hfunc)
0797 {
0798     struct ena_adapter *adapter = netdev_priv(netdev);
0799     struct ena_com_dev *ena_dev = adapter->ena_dev;
0800     enum ena_admin_hash_functions func = 0;
0801     int rc;
0802 
0803     if (indir) {
0804         rc = ena_indirection_table_set(adapter, indir);
0805         if (rc)
0806             return rc;
0807     }
0808 
0809     switch (hfunc) {
0810     case ETH_RSS_HASH_NO_CHANGE:
0811         func = ena_com_get_current_hash_function(ena_dev);
0812         break;
0813     case ETH_RSS_HASH_TOP:
0814         func = ENA_ADMIN_TOEPLITZ;
0815         break;
0816     case ETH_RSS_HASH_CRC32:
0817         func = ENA_ADMIN_CRC32;
0818         break;
0819     default:
0820         netif_err(adapter, drv, netdev, "Unsupported hfunc %d\n",
0821               hfunc);
0822         return -EOPNOTSUPP;
0823     }
0824 
0825     if (key || func) {
0826         rc = ena_com_fill_hash_function(ena_dev, func, key,
0827                         ENA_HASH_KEY_SIZE,
0828                         0xFFFFFFFF);
0829         if (unlikely(rc)) {
0830             netif_err(adapter, drv, netdev, "Cannot fill key\n");
0831             return rc == -EPERM ? -EOPNOTSUPP : rc;
0832         }
0833     }
0834 
0835     return 0;
0836 }
0837 
0838 static void ena_get_channels(struct net_device *netdev,
0839                  struct ethtool_channels *channels)
0840 {
0841     struct ena_adapter *adapter = netdev_priv(netdev);
0842 
0843     channels->max_combined = adapter->max_num_io_queues;
0844     channels->combined_count = adapter->num_io_queues;
0845 }
0846 
0847 static int ena_set_channels(struct net_device *netdev,
0848                 struct ethtool_channels *channels)
0849 {
0850     struct ena_adapter *adapter = netdev_priv(netdev);
0851     u32 count = channels->combined_count;
0852     /* The check for max value is already done in ethtool */
0853     if (count < ENA_MIN_NUM_IO_QUEUES ||
0854         (ena_xdp_present(adapter) &&
0855         !ena_xdp_legal_queue_count(adapter, count)))
0856         return -EINVAL;
0857 
0858     return ena_update_queue_count(adapter, count);
0859 }
0860 
0861 static int ena_get_tunable(struct net_device *netdev,
0862                const struct ethtool_tunable *tuna, void *data)
0863 {
0864     struct ena_adapter *adapter = netdev_priv(netdev);
0865     int ret = 0;
0866 
0867     switch (tuna->id) {
0868     case ETHTOOL_RX_COPYBREAK:
0869         *(u32 *)data = adapter->rx_copybreak;
0870         break;
0871     default:
0872         ret = -EINVAL;
0873         break;
0874     }
0875 
0876     return ret;
0877 }
0878 
0879 static int ena_set_tunable(struct net_device *netdev,
0880                const struct ethtool_tunable *tuna,
0881                const void *data)
0882 {
0883     struct ena_adapter *adapter = netdev_priv(netdev);
0884     int ret = 0;
0885     u32 len;
0886 
0887     switch (tuna->id) {
0888     case ETHTOOL_RX_COPYBREAK:
0889         len = *(u32 *)data;
0890         if (len > adapter->netdev->mtu) {
0891             ret = -EINVAL;
0892             break;
0893         }
0894         adapter->rx_copybreak = len;
0895         break;
0896     default:
0897         ret = -EINVAL;
0898         break;
0899     }
0900 
0901     return ret;
0902 }
0903 
0904 static const struct ethtool_ops ena_ethtool_ops = {
0905     .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
0906                      ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
0907     .get_link_ksettings = ena_get_link_ksettings,
0908     .get_drvinfo        = ena_get_drvinfo,
0909     .get_msglevel       = ena_get_msglevel,
0910     .set_msglevel       = ena_set_msglevel,
0911     .get_link       = ethtool_op_get_link,
0912     .get_coalesce       = ena_get_coalesce,
0913     .set_coalesce       = ena_set_coalesce,
0914     .get_ringparam      = ena_get_ringparam,
0915     .set_ringparam      = ena_set_ringparam,
0916     .get_sset_count         = ena_get_sset_count,
0917     .get_strings        = ena_get_ethtool_strings,
0918     .get_ethtool_stats      = ena_get_ethtool_stats,
0919     .get_rxnfc      = ena_get_rxnfc,
0920     .set_rxnfc      = ena_set_rxnfc,
0921     .get_rxfh_indir_size    = ena_get_rxfh_indir_size,
0922     .get_rxfh_key_size  = ena_get_rxfh_key_size,
0923     .get_rxfh       = ena_get_rxfh,
0924     .set_rxfh       = ena_set_rxfh,
0925     .get_channels       = ena_get_channels,
0926     .set_channels       = ena_set_channels,
0927     .get_tunable        = ena_get_tunable,
0928     .set_tunable        = ena_set_tunable,
0929     .get_ts_info            = ethtool_op_get_ts_info,
0930 };
0931 
0932 void ena_set_ethtool_ops(struct net_device *netdev)
0933 {
0934     netdev->ethtool_ops = &ena_ethtool_ops;
0935 }
0936 
0937 static void ena_dump_stats_ex(struct ena_adapter *adapter, u8 *buf)
0938 {
0939     struct net_device *netdev = adapter->netdev;
0940     u8 *strings_buf;
0941     u64 *data_buf;
0942     int strings_num;
0943     int i, rc;
0944 
0945     strings_num = ena_get_sw_stats_count(adapter);
0946     if (strings_num <= 0) {
0947         netif_err(adapter, drv, netdev, "Can't get stats num\n");
0948         return;
0949     }
0950 
0951     strings_buf = devm_kcalloc(&adapter->pdev->dev,
0952                    ETH_GSTRING_LEN, strings_num,
0953                    GFP_ATOMIC);
0954     if (!strings_buf) {
0955         netif_err(adapter, drv, netdev,
0956               "Failed to allocate strings_buf\n");
0957         return;
0958     }
0959 
0960     data_buf = devm_kcalloc(&adapter->pdev->dev,
0961                 strings_num, sizeof(u64),
0962                 GFP_ATOMIC);
0963     if (!data_buf) {
0964         netif_err(adapter, drv, netdev,
0965               "Failed to allocate data buf\n");
0966         devm_kfree(&adapter->pdev->dev, strings_buf);
0967         return;
0968     }
0969 
0970     ena_get_strings(adapter, strings_buf, false);
0971     ena_get_stats(adapter, data_buf, false);
0972 
0973     /* If there is a buffer, dump stats, otherwise print them to dmesg */
0974     if (buf)
0975         for (i = 0; i < strings_num; i++) {
0976             rc = snprintf(buf, ETH_GSTRING_LEN + sizeof(u64),
0977                       "%s %llu\n",
0978                       strings_buf + i * ETH_GSTRING_LEN,
0979                       data_buf[i]);
0980             buf += rc;
0981         }
0982     else
0983         for (i = 0; i < strings_num; i++)
0984             netif_err(adapter, drv, netdev, "%s: %llu\n",
0985                   strings_buf + i * ETH_GSTRING_LEN,
0986                   data_buf[i]);
0987 
0988     devm_kfree(&adapter->pdev->dev, strings_buf);
0989     devm_kfree(&adapter->pdev->dev, data_buf);
0990 }
0991 
0992 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf)
0993 {
0994     if (!buf)
0995         return;
0996 
0997     ena_dump_stats_ex(adapter, buf);
0998 }
0999 
1000 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter)
1001 {
1002     ena_dump_stats_ex(adapter, NULL);
1003 }