1e5b845dcSCatherine Sullivan // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2e5b845dcSCatherine Sullivan /* Google virtual Ethernet (gve) driver 3e5b845dcSCatherine Sullivan * 41f6228e4SBailey Forrest * Copyright (C) 2015-2021 Google, Inc. 5e5b845dcSCatherine Sullivan */ 6e5b845dcSCatherine Sullivan 7cc69837fSJakub Kicinski #include <linux/ethtool.h> 8e5b845dcSCatherine Sullivan #include <linux/rtnetlink.h> 9e5b845dcSCatherine Sullivan #include "gve.h" 1024aeb56fSKuo Zhao #include "gve_adminq.h" 116081ac20STao Liu #include "gve_dqo.h" 12e5b845dcSCatherine Sullivan 13e5b845dcSCatherine Sullivan static void gve_get_drvinfo(struct net_device *netdev, 14e5b845dcSCatherine Sullivan struct ethtool_drvinfo *info) 15e5b845dcSCatherine Sullivan { 16e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 17e5b845dcSCatherine Sullivan 18c32773c9SDaode Huang strscpy(info->driver, "gve", sizeof(info->driver)); 19c32773c9SDaode Huang strscpy(info->version, gve_version_str, sizeof(info->version)); 20c32773c9SDaode Huang strscpy(info->bus_info, pci_name(priv->pdev), sizeof(info->bus_info)); 21e5b845dcSCatherine Sullivan } 22e5b845dcSCatherine Sullivan 23e5b845dcSCatherine Sullivan static void gve_set_msglevel(struct net_device *netdev, u32 value) 24e5b845dcSCatherine Sullivan { 25e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 26e5b845dcSCatherine Sullivan 27e5b845dcSCatherine Sullivan priv->msg_enable = value; 28e5b845dcSCatherine Sullivan } 29e5b845dcSCatherine Sullivan 30e5b845dcSCatherine Sullivan static u32 gve_get_msglevel(struct net_device *netdev) 31e5b845dcSCatherine Sullivan { 32e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 33e5b845dcSCatherine Sullivan 34e5b845dcSCatherine Sullivan return priv->msg_enable; 35e5b845dcSCatherine Sullivan } 36e5b845dcSCatherine Sullivan 37e5b845dcSCatherine Sullivan static const char gve_gstrings_main_stats[][ETH_GSTRING_LEN] = { 38e5b845dcSCatherine Sullivan "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", 39e5b845dcSCatherine Sullivan "rx_dropped", "tx_dropped", "tx_timeouts", 40433e274bSKuo Zhao "rx_skb_alloc_fail", "rx_buf_alloc_fail", "rx_desc_err_dropped_pkt", 41433e274bSKuo Zhao "interface_up_cnt", "interface_down_cnt", "reset_cnt", 4224aeb56fSKuo Zhao "page_alloc_fail", "dma_mapping_error", "stats_report_trigger_cnt", 43433e274bSKuo Zhao }; 44433e274bSKuo Zhao 45433e274bSKuo Zhao static const char gve_gstrings_rx_stats[][ETH_GSTRING_LEN] = { 462c919835SJordan Kim "rx_posted_desc[%u]", "rx_completed_desc[%u]", "rx_consumed_desc[%u]", "rx_bytes[%u]", 4737149e93SDavid Awogbemila "rx_cont_packet_cnt[%u]", "rx_frag_flip_cnt[%u]", "rx_frag_copy_cnt[%u]", 48*82fd151dSShailend Chand "rx_frag_alloc_cnt[%u]", 49433e274bSKuo Zhao "rx_dropped_pkt[%u]", "rx_copybreak_pkt[%u]", "rx_copied_pkt[%u]", 502f523dc3SDavid Awogbemila "rx_queue_drop_cnt[%u]", "rx_no_buffers_posted[%u]", 512f523dc3SDavid Awogbemila "rx_drops_packet_over_mru[%u]", "rx_drops_invalid_checksum[%u]", 52433e274bSKuo Zhao }; 53433e274bSKuo Zhao 54433e274bSKuo Zhao static const char gve_gstrings_tx_stats[][ETH_GSTRING_LEN] = { 552c919835SJordan Kim "tx_posted_desc[%u]", "tx_completed_desc[%u]", "tx_consumed_desc[%u]", "tx_bytes[%u]", 56433e274bSKuo Zhao "tx_wake[%u]", "tx_stop[%u]", "tx_event_counter[%u]", 576f007c64SCatherine Sullivan "tx_dma_mapping_error[%u]", 58433e274bSKuo Zhao }; 59433e274bSKuo Zhao 60433e274bSKuo Zhao static const char gve_gstrings_adminq_stats[][ETH_GSTRING_LEN] = { 61433e274bSKuo Zhao "adminq_prod_cnt", "adminq_cmd_fail", "adminq_timeouts", 62433e274bSKuo Zhao "adminq_describe_device_cnt", "adminq_cfg_device_resources_cnt", 63433e274bSKuo Zhao "adminq_register_page_list_cnt", "adminq_unregister_page_list_cnt", 64433e274bSKuo Zhao "adminq_create_tx_queue_cnt", "adminq_create_rx_queue_cnt", 65433e274bSKuo Zhao "adminq_destroy_tx_queue_cnt", "adminq_destroy_rx_queue_cnt", 66433e274bSKuo Zhao "adminq_dcfg_device_resources_cnt", "adminq_set_driver_parameter_cnt", 677e074d5aSDavid Awogbemila "adminq_report_stats_cnt", "adminq_report_link_speed_cnt" 6824aeb56fSKuo Zhao }; 6924aeb56fSKuo Zhao 7024aeb56fSKuo Zhao static const char gve_gstrings_priv_flags[][ETH_GSTRING_LEN] = { 7124aeb56fSKuo Zhao "report-stats", 72e5b845dcSCatherine Sullivan }; 73e5b845dcSCatherine Sullivan 74e5b845dcSCatherine Sullivan #define GVE_MAIN_STATS_LEN ARRAY_SIZE(gve_gstrings_main_stats) 75433e274bSKuo Zhao #define GVE_ADMINQ_STATS_LEN ARRAY_SIZE(gve_gstrings_adminq_stats) 76433e274bSKuo Zhao #define NUM_GVE_TX_CNTS ARRAY_SIZE(gve_gstrings_tx_stats) 77433e274bSKuo Zhao #define NUM_GVE_RX_CNTS ARRAY_SIZE(gve_gstrings_rx_stats) 7824aeb56fSKuo Zhao #define GVE_PRIV_FLAGS_STR_LEN ARRAY_SIZE(gve_gstrings_priv_flags) 79e5b845dcSCatherine Sullivan 80e5b845dcSCatherine Sullivan static void gve_get_strings(struct net_device *netdev, u32 stringset, u8 *data) 81e5b845dcSCatherine Sullivan { 82e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 83e5b845dcSCatherine Sullivan char *s = (char *)data; 84433e274bSKuo Zhao int i, j; 85e5b845dcSCatherine Sullivan 8624aeb56fSKuo Zhao switch (stringset) { 8724aeb56fSKuo Zhao case ETH_SS_STATS: 88e5b845dcSCatherine Sullivan memcpy(s, *gve_gstrings_main_stats, 89e5b845dcSCatherine Sullivan sizeof(gve_gstrings_main_stats)); 90e5b845dcSCatherine Sullivan s += sizeof(gve_gstrings_main_stats); 9124aeb56fSKuo Zhao 92e5b845dcSCatherine Sullivan for (i = 0; i < priv->rx_cfg.num_queues; i++) { 93433e274bSKuo Zhao for (j = 0; j < NUM_GVE_RX_CNTS; j++) { 9424aeb56fSKuo Zhao snprintf(s, ETH_GSTRING_LEN, 9524aeb56fSKuo Zhao gve_gstrings_rx_stats[j], i); 96e5b845dcSCatherine Sullivan s += ETH_GSTRING_LEN; 97433e274bSKuo Zhao } 98e5b845dcSCatherine Sullivan } 9924aeb56fSKuo Zhao 100e5b845dcSCatherine Sullivan for (i = 0; i < priv->tx_cfg.num_queues; i++) { 101433e274bSKuo Zhao for (j = 0; j < NUM_GVE_TX_CNTS; j++) { 10224aeb56fSKuo Zhao snprintf(s, ETH_GSTRING_LEN, 10324aeb56fSKuo Zhao gve_gstrings_tx_stats[j], i); 104e5b845dcSCatherine Sullivan s += ETH_GSTRING_LEN; 105e5b845dcSCatherine Sullivan } 106e5b845dcSCatherine Sullivan } 107e5b845dcSCatherine Sullivan 108433e274bSKuo Zhao memcpy(s, *gve_gstrings_adminq_stats, 109433e274bSKuo Zhao sizeof(gve_gstrings_adminq_stats)); 110433e274bSKuo Zhao s += sizeof(gve_gstrings_adminq_stats); 11124aeb56fSKuo Zhao break; 11224aeb56fSKuo Zhao 11324aeb56fSKuo Zhao case ETH_SS_PRIV_FLAGS: 11424aeb56fSKuo Zhao memcpy(s, *gve_gstrings_priv_flags, 11524aeb56fSKuo Zhao sizeof(gve_gstrings_priv_flags)); 11624aeb56fSKuo Zhao s += sizeof(gve_gstrings_priv_flags); 11724aeb56fSKuo Zhao break; 11824aeb56fSKuo Zhao 11924aeb56fSKuo Zhao default: 12024aeb56fSKuo Zhao break; 12124aeb56fSKuo Zhao } 122433e274bSKuo Zhao } 123433e274bSKuo Zhao 124e5b845dcSCatherine Sullivan static int gve_get_sset_count(struct net_device *netdev, int sset) 125e5b845dcSCatherine Sullivan { 126e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 127e5b845dcSCatherine Sullivan 128e5b845dcSCatherine Sullivan switch (sset) { 129e5b845dcSCatherine Sullivan case ETH_SS_STATS: 130433e274bSKuo Zhao return GVE_MAIN_STATS_LEN + GVE_ADMINQ_STATS_LEN + 131e5b845dcSCatherine Sullivan (priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS) + 132e5b845dcSCatherine Sullivan (priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS); 13324aeb56fSKuo Zhao case ETH_SS_PRIV_FLAGS: 13424aeb56fSKuo Zhao return GVE_PRIV_FLAGS_STR_LEN; 135e5b845dcSCatherine Sullivan default: 136e5b845dcSCatherine Sullivan return -EOPNOTSUPP; 137e5b845dcSCatherine Sullivan } 138e5b845dcSCatherine Sullivan } 139e5b845dcSCatherine Sullivan 140e5b845dcSCatherine Sullivan static void 141e5b845dcSCatherine Sullivan gve_get_ethtool_stats(struct net_device *netdev, 142e5b845dcSCatherine Sullivan struct ethtool_stats *stats, u64 *data) 143e5b845dcSCatherine Sullivan { 1442c919835SJordan Kim u64 tmp_rx_pkts, tmp_rx_bytes, tmp_rx_skb_alloc_fail, 1452c919835SJordan Kim tmp_rx_buf_alloc_fail, tmp_rx_desc_err_dropped_pkt, 1462c919835SJordan Kim tmp_tx_pkts, tmp_tx_bytes; 147433e274bSKuo Zhao u64 rx_buf_alloc_fail, rx_desc_err_dropped_pkt, rx_pkts, 1482c919835SJordan Kim rx_skb_alloc_fail, rx_bytes, tx_pkts, tx_bytes, tx_dropped; 1492f523dc3SDavid Awogbemila int stats_idx, base_stats_idx, max_stats_idx; 1502f523dc3SDavid Awogbemila struct stats *report_stats; 1512f523dc3SDavid Awogbemila int *rx_qid_to_stats_idx; 1522f523dc3SDavid Awogbemila int *tx_qid_to_stats_idx; 153433e274bSKuo Zhao struct gve_priv *priv; 1542f523dc3SDavid Awogbemila bool skip_nic_stats; 155e5b845dcSCatherine Sullivan unsigned int start; 156e5b845dcSCatherine Sullivan int ring; 1572f523dc3SDavid Awogbemila int i, j; 158e5b845dcSCatherine Sullivan 159e5b845dcSCatherine Sullivan ASSERT_RTNL(); 160e5b845dcSCatherine Sullivan 161433e274bSKuo Zhao priv = netdev_priv(netdev); 1622f523dc3SDavid Awogbemila report_stats = priv->stats_report->stats; 1632f523dc3SDavid Awogbemila rx_qid_to_stats_idx = kmalloc_array(priv->rx_cfg.num_queues, 1642f523dc3SDavid Awogbemila sizeof(int), GFP_KERNEL); 1652f523dc3SDavid Awogbemila if (!rx_qid_to_stats_idx) 1662f523dc3SDavid Awogbemila return; 1672f523dc3SDavid Awogbemila tx_qid_to_stats_idx = kmalloc_array(priv->tx_cfg.num_queues, 1682f523dc3SDavid Awogbemila sizeof(int), GFP_KERNEL); 1692f523dc3SDavid Awogbemila if (!tx_qid_to_stats_idx) { 1702f523dc3SDavid Awogbemila kfree(rx_qid_to_stats_idx); 1712f523dc3SDavid Awogbemila return; 1722f523dc3SDavid Awogbemila } 173433e274bSKuo Zhao for (rx_pkts = 0, rx_bytes = 0, rx_skb_alloc_fail = 0, 174433e274bSKuo Zhao rx_buf_alloc_fail = 0, rx_desc_err_dropped_pkt = 0, ring = 0; 175e5b845dcSCatherine Sullivan ring < priv->rx_cfg.num_queues; ring++) { 176e5b845dcSCatherine Sullivan if (priv->rx) { 177e5b845dcSCatherine Sullivan do { 178433e274bSKuo Zhao struct gve_rx_ring *rx = &priv->rx[ring]; 179433e274bSKuo Zhao 1803c13ce74SCatherine Sullivan start = 181068c38adSThomas Gleixner u64_stats_fetch_begin(&priv->rx[ring].statss); 182433e274bSKuo Zhao tmp_rx_pkts = rx->rpackets; 183433e274bSKuo Zhao tmp_rx_bytes = rx->rbytes; 184433e274bSKuo Zhao tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail; 185433e274bSKuo Zhao tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail; 186433e274bSKuo Zhao tmp_rx_desc_err_dropped_pkt = 187433e274bSKuo Zhao rx->rx_desc_err_dropped_pkt; 188068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&priv->rx[ring].statss, 189e5b845dcSCatherine Sullivan start)); 190433e274bSKuo Zhao rx_pkts += tmp_rx_pkts; 191433e274bSKuo Zhao rx_bytes += tmp_rx_bytes; 192433e274bSKuo Zhao rx_skb_alloc_fail += tmp_rx_skb_alloc_fail; 193433e274bSKuo Zhao rx_buf_alloc_fail += tmp_rx_buf_alloc_fail; 194433e274bSKuo Zhao rx_desc_err_dropped_pkt += tmp_rx_desc_err_dropped_pkt; 195e5b845dcSCatherine Sullivan } 196e5b845dcSCatherine Sullivan } 1972c919835SJordan Kim for (tx_pkts = 0, tx_bytes = 0, tx_dropped = 0, ring = 0; 198e5b845dcSCatherine Sullivan ring < priv->tx_cfg.num_queues; ring++) { 199e5b845dcSCatherine Sullivan if (priv->tx) { 200e5b845dcSCatherine Sullivan do { 2013c13ce74SCatherine Sullivan start = 202068c38adSThomas Gleixner u64_stats_fetch_begin(&priv->tx[ring].statss); 203433e274bSKuo Zhao tmp_tx_pkts = priv->tx[ring].pkt_done; 204433e274bSKuo Zhao tmp_tx_bytes = priv->tx[ring].bytes_done; 205068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&priv->tx[ring].statss, 206e5b845dcSCatherine Sullivan start)); 207433e274bSKuo Zhao tx_pkts += tmp_tx_pkts; 208433e274bSKuo Zhao tx_bytes += tmp_tx_bytes; 2092c919835SJordan Kim tx_dropped += priv->tx[ring].dropped_pkt; 210e5b845dcSCatherine Sullivan } 211e5b845dcSCatherine Sullivan } 212e5b845dcSCatherine Sullivan 213e5b845dcSCatherine Sullivan i = 0; 214e5b845dcSCatherine Sullivan data[i++] = rx_pkts; 215e5b845dcSCatherine Sullivan data[i++] = tx_pkts; 216e5b845dcSCatherine Sullivan data[i++] = rx_bytes; 217e5b845dcSCatherine Sullivan data[i++] = tx_bytes; 218433e274bSKuo Zhao /* total rx dropped packets */ 219433e274bSKuo Zhao data[i++] = rx_skb_alloc_fail + rx_buf_alloc_fail + 220433e274bSKuo Zhao rx_desc_err_dropped_pkt; 2212c919835SJordan Kim data[i++] = tx_dropped; 222e5b845dcSCatherine Sullivan data[i++] = priv->tx_timeo_cnt; 223433e274bSKuo Zhao data[i++] = rx_skb_alloc_fail; 224433e274bSKuo Zhao data[i++] = rx_buf_alloc_fail; 225433e274bSKuo Zhao data[i++] = rx_desc_err_dropped_pkt; 226433e274bSKuo Zhao data[i++] = priv->interface_up_cnt; 227433e274bSKuo Zhao data[i++] = priv->interface_down_cnt; 228433e274bSKuo Zhao data[i++] = priv->reset_cnt; 229433e274bSKuo Zhao data[i++] = priv->page_alloc_fail; 230433e274bSKuo Zhao data[i++] = priv->dma_mapping_error; 23124aeb56fSKuo Zhao data[i++] = priv->stats_report_trigger_cnt; 232e5b845dcSCatherine Sullivan i = GVE_MAIN_STATS_LEN; 233e5b845dcSCatherine Sullivan 2342f523dc3SDavid Awogbemila /* For rx cross-reporting stats, start from nic rx stats in report */ 2352f523dc3SDavid Awogbemila base_stats_idx = GVE_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues + 2362f523dc3SDavid Awogbemila GVE_RX_STATS_REPORT_NUM * priv->rx_cfg.num_queues; 2372f523dc3SDavid Awogbemila max_stats_idx = NIC_RX_STATS_REPORT_NUM * priv->rx_cfg.num_queues + 2382f523dc3SDavid Awogbemila base_stats_idx; 2392f523dc3SDavid Awogbemila /* Preprocess the stats report for rx, map queue id to start index */ 2402f523dc3SDavid Awogbemila skip_nic_stats = false; 2412f523dc3SDavid Awogbemila for (stats_idx = base_stats_idx; stats_idx < max_stats_idx; 2422f523dc3SDavid Awogbemila stats_idx += NIC_RX_STATS_REPORT_NUM) { 2432f523dc3SDavid Awogbemila u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name); 2442f523dc3SDavid Awogbemila u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id); 2452f523dc3SDavid Awogbemila 2462f523dc3SDavid Awogbemila if (stat_name == 0) { 2472f523dc3SDavid Awogbemila /* no stats written by NIC yet */ 2482f523dc3SDavid Awogbemila skip_nic_stats = true; 2492f523dc3SDavid Awogbemila break; 2502f523dc3SDavid Awogbemila } 2512f523dc3SDavid Awogbemila rx_qid_to_stats_idx[queue_id] = stats_idx; 2522f523dc3SDavid Awogbemila } 253e5b845dcSCatherine Sullivan /* walk RX rings */ 254e5b845dcSCatherine Sullivan if (priv->rx) { 255e5b845dcSCatherine Sullivan for (ring = 0; ring < priv->rx_cfg.num_queues; ring++) { 256e5b845dcSCatherine Sullivan struct gve_rx_ring *rx = &priv->rx[ring]; 257e5b845dcSCatherine Sullivan 258438b43bdSCatherine Sullivan data[i++] = rx->fill_cnt; 259433e274bSKuo Zhao data[i++] = rx->cnt; 2602c919835SJordan Kim data[i++] = rx->fill_cnt - rx->cnt; 261433e274bSKuo Zhao do { 262433e274bSKuo Zhao start = 263068c38adSThomas Gleixner u64_stats_fetch_begin(&priv->rx[ring].statss); 264433e274bSKuo Zhao tmp_rx_bytes = rx->rbytes; 265433e274bSKuo Zhao tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail; 266433e274bSKuo Zhao tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail; 267433e274bSKuo Zhao tmp_rx_desc_err_dropped_pkt = 268433e274bSKuo Zhao rx->rx_desc_err_dropped_pkt; 269068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&priv->rx[ring].statss, 270433e274bSKuo Zhao start)); 271433e274bSKuo Zhao data[i++] = tmp_rx_bytes; 27237149e93SDavid Awogbemila data[i++] = rx->rx_cont_packet_cnt; 27337149e93SDavid Awogbemila data[i++] = rx->rx_frag_flip_cnt; 27437149e93SDavid Awogbemila data[i++] = rx->rx_frag_copy_cnt; 275*82fd151dSShailend Chand data[i++] = rx->rx_frag_alloc_cnt; 276433e274bSKuo Zhao /* rx dropped packets */ 277433e274bSKuo Zhao data[i++] = tmp_rx_skb_alloc_fail + 278433e274bSKuo Zhao tmp_rx_buf_alloc_fail + 279433e274bSKuo Zhao tmp_rx_desc_err_dropped_pkt; 280433e274bSKuo Zhao data[i++] = rx->rx_copybreak_pkt; 281433e274bSKuo Zhao data[i++] = rx->rx_copied_pkt; 2822f523dc3SDavid Awogbemila /* stats from NIC */ 2832f523dc3SDavid Awogbemila if (skip_nic_stats) { 2842f523dc3SDavid Awogbemila /* skip NIC rx stats */ 2852f523dc3SDavid Awogbemila i += NIC_RX_STATS_REPORT_NUM; 2862f523dc3SDavid Awogbemila continue; 2872f523dc3SDavid Awogbemila } 2882f523dc3SDavid Awogbemila for (j = 0; j < NIC_RX_STATS_REPORT_NUM; j++) { 2892f523dc3SDavid Awogbemila u64 value = 2902f523dc3SDavid Awogbemila be64_to_cpu(report_stats[rx_qid_to_stats_idx[ring] + j].value); 2912f523dc3SDavid Awogbemila 2922f523dc3SDavid Awogbemila data[i++] = value; 2932f523dc3SDavid Awogbemila } 294e5b845dcSCatherine Sullivan } 295e5b845dcSCatherine Sullivan } else { 296e5b845dcSCatherine Sullivan i += priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS; 297e5b845dcSCatherine Sullivan } 2982f523dc3SDavid Awogbemila 2992f523dc3SDavid Awogbemila /* For tx cross-reporting stats, start from nic tx stats in report */ 3002f523dc3SDavid Awogbemila base_stats_idx = max_stats_idx; 3012f523dc3SDavid Awogbemila max_stats_idx = NIC_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues + 3022f523dc3SDavid Awogbemila max_stats_idx; 3032f523dc3SDavid Awogbemila /* Preprocess the stats report for tx, map queue id to start index */ 3042f523dc3SDavid Awogbemila skip_nic_stats = false; 3052f523dc3SDavid Awogbemila for (stats_idx = base_stats_idx; stats_idx < max_stats_idx; 3062f523dc3SDavid Awogbemila stats_idx += NIC_TX_STATS_REPORT_NUM) { 3072f523dc3SDavid Awogbemila u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name); 3082f523dc3SDavid Awogbemila u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id); 3092f523dc3SDavid Awogbemila 3102f523dc3SDavid Awogbemila if (stat_name == 0) { 3112f523dc3SDavid Awogbemila /* no stats written by NIC yet */ 3122f523dc3SDavid Awogbemila skip_nic_stats = true; 3132f523dc3SDavid Awogbemila break; 3142f523dc3SDavid Awogbemila } 3152f523dc3SDavid Awogbemila tx_qid_to_stats_idx[queue_id] = stats_idx; 3162f523dc3SDavid Awogbemila } 317e5b845dcSCatherine Sullivan /* walk TX rings */ 318e5b845dcSCatherine Sullivan if (priv->tx) { 319e5b845dcSCatherine Sullivan for (ring = 0; ring < priv->tx_cfg.num_queues; ring++) { 320e5b845dcSCatherine Sullivan struct gve_tx_ring *tx = &priv->tx[ring]; 321e5b845dcSCatherine Sullivan 3225e8c5adfSBailey Forrest if (gve_is_gqi(priv)) { 323e5b845dcSCatherine Sullivan data[i++] = tx->req; 324e5b845dcSCatherine Sullivan data[i++] = tx->done; 3252c919835SJordan Kim data[i++] = tx->req - tx->done; 3265e8c5adfSBailey Forrest } else { 3275e8c5adfSBailey Forrest /* DQO doesn't currently support 3285e8c5adfSBailey Forrest * posted/completed descriptor counts; 3295e8c5adfSBailey Forrest */ 3305e8c5adfSBailey Forrest data[i++] = 0; 3315e8c5adfSBailey Forrest data[i++] = 0; 3322c919835SJordan Kim data[i++] = tx->dqo_tx.tail - tx->dqo_tx.head; 3335e8c5adfSBailey Forrest } 334433e274bSKuo Zhao do { 335433e274bSKuo Zhao start = 336068c38adSThomas Gleixner u64_stats_fetch_begin(&priv->tx[ring].statss); 337433e274bSKuo Zhao tmp_tx_bytes = tx->bytes_done; 338068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&priv->tx[ring].statss, 339433e274bSKuo Zhao start)); 340433e274bSKuo Zhao data[i++] = tmp_tx_bytes; 341e5b845dcSCatherine Sullivan data[i++] = tx->wake_queue; 342e5b845dcSCatherine Sullivan data[i++] = tx->stop_queue; 34361d72c7eSTao Liu data[i++] = gve_tx_load_event_counter(priv, tx); 3446f007c64SCatherine Sullivan data[i++] = tx->dma_mapping_error; 3452f523dc3SDavid Awogbemila /* stats from NIC */ 3462f523dc3SDavid Awogbemila if (skip_nic_stats) { 3472f523dc3SDavid Awogbemila /* skip NIC tx stats */ 3482f523dc3SDavid Awogbemila i += NIC_TX_STATS_REPORT_NUM; 3492f523dc3SDavid Awogbemila continue; 3502f523dc3SDavid Awogbemila } 3512f523dc3SDavid Awogbemila for (j = 0; j < NIC_TX_STATS_REPORT_NUM; j++) { 3522f523dc3SDavid Awogbemila u64 value = 3532f523dc3SDavid Awogbemila be64_to_cpu(report_stats[tx_qid_to_stats_idx[ring] + j].value); 3542f523dc3SDavid Awogbemila data[i++] = value; 3552f523dc3SDavid Awogbemila } 356e5b845dcSCatherine Sullivan } 357e5b845dcSCatherine Sullivan } else { 358e5b845dcSCatherine Sullivan i += priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS; 359e5b845dcSCatherine Sullivan } 3602f523dc3SDavid Awogbemila 3612f523dc3SDavid Awogbemila kfree(rx_qid_to_stats_idx); 3622f523dc3SDavid Awogbemila kfree(tx_qid_to_stats_idx); 363433e274bSKuo Zhao /* AQ Stats */ 364433e274bSKuo Zhao data[i++] = priv->adminq_prod_cnt; 365433e274bSKuo Zhao data[i++] = priv->adminq_cmd_fail; 366433e274bSKuo Zhao data[i++] = priv->adminq_timeouts; 367433e274bSKuo Zhao data[i++] = priv->adminq_describe_device_cnt; 368433e274bSKuo Zhao data[i++] = priv->adminq_cfg_device_resources_cnt; 369433e274bSKuo Zhao data[i++] = priv->adminq_register_page_list_cnt; 370433e274bSKuo Zhao data[i++] = priv->adminq_unregister_page_list_cnt; 371433e274bSKuo Zhao data[i++] = priv->adminq_create_tx_queue_cnt; 372433e274bSKuo Zhao data[i++] = priv->adminq_create_rx_queue_cnt; 373433e274bSKuo Zhao data[i++] = priv->adminq_destroy_tx_queue_cnt; 374433e274bSKuo Zhao data[i++] = priv->adminq_destroy_rx_queue_cnt; 375433e274bSKuo Zhao data[i++] = priv->adminq_dcfg_device_resources_cnt; 376433e274bSKuo Zhao data[i++] = priv->adminq_set_driver_parameter_cnt; 37724aeb56fSKuo Zhao data[i++] = priv->adminq_report_stats_cnt; 3787e074d5aSDavid Awogbemila data[i++] = priv->adminq_report_link_speed_cnt; 379e5b845dcSCatherine Sullivan } 380e5b845dcSCatherine Sullivan 381e5b845dcSCatherine Sullivan static void gve_get_channels(struct net_device *netdev, 382e5b845dcSCatherine Sullivan struct ethtool_channels *cmd) 383e5b845dcSCatherine Sullivan { 384e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 385e5b845dcSCatherine Sullivan 386e5b845dcSCatherine Sullivan cmd->max_rx = priv->rx_cfg.max_queues; 387e5b845dcSCatherine Sullivan cmd->max_tx = priv->tx_cfg.max_queues; 388e5b845dcSCatherine Sullivan cmd->max_other = 0; 389e5b845dcSCatherine Sullivan cmd->max_combined = 0; 390e5b845dcSCatherine Sullivan cmd->rx_count = priv->rx_cfg.num_queues; 391e5b845dcSCatherine Sullivan cmd->tx_count = priv->tx_cfg.num_queues; 392e5b845dcSCatherine Sullivan cmd->other_count = 0; 393e5b845dcSCatherine Sullivan cmd->combined_count = 0; 394e5b845dcSCatherine Sullivan } 395e5b845dcSCatherine Sullivan 396e5b845dcSCatherine Sullivan static int gve_set_channels(struct net_device *netdev, 397e5b845dcSCatherine Sullivan struct ethtool_channels *cmd) 398e5b845dcSCatherine Sullivan { 399e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 400e5b845dcSCatherine Sullivan struct gve_queue_config new_tx_cfg = priv->tx_cfg; 401e5b845dcSCatherine Sullivan struct gve_queue_config new_rx_cfg = priv->rx_cfg; 402e5b845dcSCatherine Sullivan struct ethtool_channels old_settings; 403e5b845dcSCatherine Sullivan int new_tx = cmd->tx_count; 404e5b845dcSCatherine Sullivan int new_rx = cmd->rx_count; 405e5b845dcSCatherine Sullivan 406e5b845dcSCatherine Sullivan gve_get_channels(netdev, &old_settings); 407e5b845dcSCatherine Sullivan 408f67435b5SDaode Huang /* Changing combined is not allowed */ 409e5b845dcSCatherine Sullivan if (cmd->combined_count != old_settings.combined_count) 410e5b845dcSCatherine Sullivan return -EINVAL; 411e5b845dcSCatherine Sullivan 412e5b845dcSCatherine Sullivan if (!new_rx || !new_tx) 413e5b845dcSCatherine Sullivan return -EINVAL; 414e5b845dcSCatherine Sullivan 415e5b845dcSCatherine Sullivan if (!netif_carrier_ok(netdev)) { 416e5b845dcSCatherine Sullivan priv->tx_cfg.num_queues = new_tx; 417e5b845dcSCatherine Sullivan priv->rx_cfg.num_queues = new_rx; 418e5b845dcSCatherine Sullivan return 0; 419e5b845dcSCatherine Sullivan } 420e5b845dcSCatherine Sullivan 421e5b845dcSCatherine Sullivan new_tx_cfg.num_queues = new_tx; 422e5b845dcSCatherine Sullivan new_rx_cfg.num_queues = new_rx; 423e5b845dcSCatherine Sullivan 424e5b845dcSCatherine Sullivan return gve_adjust_queues(priv, new_rx_cfg, new_tx_cfg); 425e5b845dcSCatherine Sullivan } 426e5b845dcSCatherine Sullivan 427e5b845dcSCatherine Sullivan static void gve_get_ringparam(struct net_device *netdev, 42874624944SHao Chen struct ethtool_ringparam *cmd, 42974624944SHao Chen struct kernel_ethtool_ringparam *kernel_cmd, 43074624944SHao Chen struct netlink_ext_ack *extack) 431e5b845dcSCatherine Sullivan { 432e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 433e5b845dcSCatherine Sullivan 434e5b845dcSCatherine Sullivan cmd->rx_max_pending = priv->rx_desc_cnt; 435e5b845dcSCatherine Sullivan cmd->tx_max_pending = priv->tx_desc_cnt; 436e5b845dcSCatherine Sullivan cmd->rx_pending = priv->rx_desc_cnt; 437e5b845dcSCatherine Sullivan cmd->tx_pending = priv->tx_desc_cnt; 438e5b845dcSCatherine Sullivan } 439e5b845dcSCatherine Sullivan 440e5b845dcSCatherine Sullivan static int gve_user_reset(struct net_device *netdev, u32 *flags) 441e5b845dcSCatherine Sullivan { 442e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 443e5b845dcSCatherine Sullivan 444e5b845dcSCatherine Sullivan if (*flags == ETH_RESET_ALL) { 445e5b845dcSCatherine Sullivan *flags = 0; 446e5b845dcSCatherine Sullivan return gve_reset(priv, true); 447e5b845dcSCatherine Sullivan } 448e5b845dcSCatherine Sullivan 449e5b845dcSCatherine Sullivan return -EOPNOTSUPP; 450e5b845dcSCatherine Sullivan } 451e5b845dcSCatherine Sullivan 452d5f7543cSKuo Zhao static int gve_get_tunable(struct net_device *netdev, 453d5f7543cSKuo Zhao const struct ethtool_tunable *etuna, void *value) 454d5f7543cSKuo Zhao { 455d5f7543cSKuo Zhao struct gve_priv *priv = netdev_priv(netdev); 456d5f7543cSKuo Zhao 457d5f7543cSKuo Zhao switch (etuna->id) { 458d5f7543cSKuo Zhao case ETHTOOL_RX_COPYBREAK: 459d5f7543cSKuo Zhao *(u32 *)value = priv->rx_copybreak; 460d5f7543cSKuo Zhao return 0; 461d5f7543cSKuo Zhao default: 462d5f7543cSKuo Zhao return -EOPNOTSUPP; 463d5f7543cSKuo Zhao } 464d5f7543cSKuo Zhao } 465d5f7543cSKuo Zhao 466d5f7543cSKuo Zhao static int gve_set_tunable(struct net_device *netdev, 467433e274bSKuo Zhao const struct ethtool_tunable *etuna, 468433e274bSKuo Zhao const void *value) 469d5f7543cSKuo Zhao { 470d5f7543cSKuo Zhao struct gve_priv *priv = netdev_priv(netdev); 471d5f7543cSKuo Zhao u32 len; 472d5f7543cSKuo Zhao 473d5f7543cSKuo Zhao switch (etuna->id) { 474d5f7543cSKuo Zhao case ETHTOOL_RX_COPYBREAK: 4751f6228e4SBailey Forrest { 4761f6228e4SBailey Forrest u32 max_copybreak = gve_is_gqi(priv) ? 4771f6228e4SBailey Forrest (PAGE_SIZE / 2) : priv->data_buffer_size_dqo; 4781f6228e4SBailey Forrest 479d5f7543cSKuo Zhao len = *(u32 *)value; 4801f6228e4SBailey Forrest if (len > max_copybreak) 481d5f7543cSKuo Zhao return -EINVAL; 482d5f7543cSKuo Zhao priv->rx_copybreak = len; 483d5f7543cSKuo Zhao return 0; 4841f6228e4SBailey Forrest } 485d5f7543cSKuo Zhao default: 486d5f7543cSKuo Zhao return -EOPNOTSUPP; 487d5f7543cSKuo Zhao } 488d5f7543cSKuo Zhao } 489d5f7543cSKuo Zhao 49024aeb56fSKuo Zhao static u32 gve_get_priv_flags(struct net_device *netdev) 49124aeb56fSKuo Zhao { 49224aeb56fSKuo Zhao struct gve_priv *priv = netdev_priv(netdev); 49324aeb56fSKuo Zhao u32 ret_flags = 0; 49424aeb56fSKuo Zhao 49524aeb56fSKuo Zhao /* Only 1 flag exists currently: report-stats (BIT(O)), so set that flag. */ 49624aeb56fSKuo Zhao if (priv->ethtool_flags & BIT(0)) 49724aeb56fSKuo Zhao ret_flags |= BIT(0); 49824aeb56fSKuo Zhao return ret_flags; 49924aeb56fSKuo Zhao } 50024aeb56fSKuo Zhao 50124aeb56fSKuo Zhao static int gve_set_priv_flags(struct net_device *netdev, u32 flags) 50224aeb56fSKuo Zhao { 50324aeb56fSKuo Zhao struct gve_priv *priv = netdev_priv(netdev); 50424aeb56fSKuo Zhao u64 ori_flags, new_flags; 50524aeb56fSKuo Zhao 50624aeb56fSKuo Zhao ori_flags = READ_ONCE(priv->ethtool_flags); 50724aeb56fSKuo Zhao new_flags = ori_flags; 50824aeb56fSKuo Zhao 50924aeb56fSKuo Zhao /* Only one priv flag exists: report-stats (BIT(0))*/ 51024aeb56fSKuo Zhao if (flags & BIT(0)) 51124aeb56fSKuo Zhao new_flags |= BIT(0); 51224aeb56fSKuo Zhao else 51324aeb56fSKuo Zhao new_flags &= ~(BIT(0)); 51424aeb56fSKuo Zhao priv->ethtool_flags = new_flags; 51524aeb56fSKuo Zhao /* start report-stats timer when user turns report stats on. */ 51624aeb56fSKuo Zhao if (flags & BIT(0)) { 51724aeb56fSKuo Zhao mod_timer(&priv->stats_report_timer, 51824aeb56fSKuo Zhao round_jiffies(jiffies + 51924aeb56fSKuo Zhao msecs_to_jiffies(priv->stats_report_timer_period))); 52024aeb56fSKuo Zhao } 52124aeb56fSKuo Zhao /* Zero off gve stats when report-stats turned off and */ 52224aeb56fSKuo Zhao /* delete report stats timer. */ 52324aeb56fSKuo Zhao if (!(flags & BIT(0)) && (ori_flags & BIT(0))) { 52424aeb56fSKuo Zhao int tx_stats_num = GVE_TX_STATS_REPORT_NUM * 52524aeb56fSKuo Zhao priv->tx_cfg.num_queues; 52624aeb56fSKuo Zhao int rx_stats_num = GVE_RX_STATS_REPORT_NUM * 52724aeb56fSKuo Zhao priv->rx_cfg.num_queues; 52824aeb56fSKuo Zhao 52924aeb56fSKuo Zhao memset(priv->stats_report->stats, 0, (tx_stats_num + rx_stats_num) * 53024aeb56fSKuo Zhao sizeof(struct stats)); 53124aeb56fSKuo Zhao del_timer_sync(&priv->stats_report_timer); 53224aeb56fSKuo Zhao } 53324aeb56fSKuo Zhao return 0; 53424aeb56fSKuo Zhao } 53524aeb56fSKuo Zhao 5367e074d5aSDavid Awogbemila static int gve_get_link_ksettings(struct net_device *netdev, 5377e074d5aSDavid Awogbemila struct ethtool_link_ksettings *cmd) 5387e074d5aSDavid Awogbemila { 5397e074d5aSDavid Awogbemila struct gve_priv *priv = netdev_priv(netdev); 5407e074d5aSDavid Awogbemila int err = gve_adminq_report_link_speed(priv); 5417e074d5aSDavid Awogbemila 5427e074d5aSDavid Awogbemila cmd->base.speed = priv->link_speed; 5437e074d5aSDavid Awogbemila return err; 5447e074d5aSDavid Awogbemila } 5457e074d5aSDavid Awogbemila 5466081ac20STao Liu static int gve_get_coalesce(struct net_device *netdev, 5476081ac20STao Liu struct ethtool_coalesce *ec, 5486081ac20STao Liu struct kernel_ethtool_coalesce *kernel_ec, 5496081ac20STao Liu struct netlink_ext_ack *extack) 5506081ac20STao Liu { 5516081ac20STao Liu struct gve_priv *priv = netdev_priv(netdev); 5526081ac20STao Liu 5536081ac20STao Liu if (gve_is_gqi(priv)) 5546081ac20STao Liu return -EOPNOTSUPP; 5556081ac20STao Liu ec->tx_coalesce_usecs = priv->tx_coalesce_usecs; 5566081ac20STao Liu ec->rx_coalesce_usecs = priv->rx_coalesce_usecs; 5576081ac20STao Liu 5586081ac20STao Liu return 0; 5596081ac20STao Liu } 5606081ac20STao Liu 5616081ac20STao Liu static int gve_set_coalesce(struct net_device *netdev, 5626081ac20STao Liu struct ethtool_coalesce *ec, 5636081ac20STao Liu struct kernel_ethtool_coalesce *kernel_ec, 5646081ac20STao Liu struct netlink_ext_ack *extack) 5656081ac20STao Liu { 5666081ac20STao Liu struct gve_priv *priv = netdev_priv(netdev); 5676081ac20STao Liu u32 tx_usecs_orig = priv->tx_coalesce_usecs; 5686081ac20STao Liu u32 rx_usecs_orig = priv->rx_coalesce_usecs; 5696081ac20STao Liu int idx; 5706081ac20STao Liu 5716081ac20STao Liu if (gve_is_gqi(priv)) 5726081ac20STao Liu return -EOPNOTSUPP; 5736081ac20STao Liu 5746081ac20STao Liu if (ec->tx_coalesce_usecs > GVE_MAX_ITR_INTERVAL_DQO || 5756081ac20STao Liu ec->rx_coalesce_usecs > GVE_MAX_ITR_INTERVAL_DQO) 5766081ac20STao Liu return -EINVAL; 5776081ac20STao Liu priv->tx_coalesce_usecs = ec->tx_coalesce_usecs; 5786081ac20STao Liu priv->rx_coalesce_usecs = ec->rx_coalesce_usecs; 5796081ac20STao Liu 5806081ac20STao Liu if (tx_usecs_orig != priv->tx_coalesce_usecs) { 5816081ac20STao Liu for (idx = 0; idx < priv->tx_cfg.num_queues; idx++) { 5826081ac20STao Liu int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx); 5836081ac20STao Liu struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx]; 5846081ac20STao Liu 5856081ac20STao Liu gve_set_itr_coalesce_usecs_dqo(priv, block, 5866081ac20STao Liu priv->tx_coalesce_usecs); 5876081ac20STao Liu } 5886081ac20STao Liu } 5896081ac20STao Liu 5906081ac20STao Liu if (rx_usecs_orig != priv->rx_coalesce_usecs) { 5916081ac20STao Liu for (idx = 0; idx < priv->rx_cfg.num_queues; idx++) { 5926081ac20STao Liu int ntfy_idx = gve_rx_idx_to_ntfy(priv, idx); 5936081ac20STao Liu struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx]; 5946081ac20STao Liu 5956081ac20STao Liu gve_set_itr_coalesce_usecs_dqo(priv, block, 5966081ac20STao Liu priv->rx_coalesce_usecs); 5976081ac20STao Liu } 5986081ac20STao Liu } 5996081ac20STao Liu 6006081ac20STao Liu return 0; 6016081ac20STao Liu } 6026081ac20STao Liu 603e5b845dcSCatherine Sullivan const struct ethtool_ops gve_ethtool_ops = { 6046081ac20STao Liu .supported_coalesce_params = ETHTOOL_COALESCE_USECS, 605e5b845dcSCatherine Sullivan .get_drvinfo = gve_get_drvinfo, 606e5b845dcSCatherine Sullivan .get_strings = gve_get_strings, 607e5b845dcSCatherine Sullivan .get_sset_count = gve_get_sset_count, 608e5b845dcSCatherine Sullivan .get_ethtool_stats = gve_get_ethtool_stats, 609e5b845dcSCatherine Sullivan .set_msglevel = gve_set_msglevel, 610e5b845dcSCatherine Sullivan .get_msglevel = gve_get_msglevel, 611e5b845dcSCatherine Sullivan .set_channels = gve_set_channels, 612e5b845dcSCatherine Sullivan .get_channels = gve_get_channels, 613e5b845dcSCatherine Sullivan .get_link = ethtool_op_get_link, 6146081ac20STao Liu .get_coalesce = gve_get_coalesce, 6156081ac20STao Liu .set_coalesce = gve_set_coalesce, 616e5b845dcSCatherine Sullivan .get_ringparam = gve_get_ringparam, 617e5b845dcSCatherine Sullivan .reset = gve_user_reset, 618d5f7543cSKuo Zhao .get_tunable = gve_get_tunable, 619d5f7543cSKuo Zhao .set_tunable = gve_set_tunable, 62024aeb56fSKuo Zhao .get_priv_flags = gve_get_priv_flags, 62124aeb56fSKuo Zhao .set_priv_flags = gve_set_priv_flags, 6227e074d5aSDavid Awogbemila .get_link_ksettings = gve_get_link_ksettings 623e5b845dcSCatherine Sullivan }; 624