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" 11*6081ac20STao 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]", 48433e274bSKuo Zhao "rx_dropped_pkt[%u]", "rx_copybreak_pkt[%u]", "rx_copied_pkt[%u]", 492f523dc3SDavid Awogbemila "rx_queue_drop_cnt[%u]", "rx_no_buffers_posted[%u]", 502f523dc3SDavid Awogbemila "rx_drops_packet_over_mru[%u]", "rx_drops_invalid_checksum[%u]", 51433e274bSKuo Zhao }; 52433e274bSKuo Zhao 53433e274bSKuo Zhao static const char gve_gstrings_tx_stats[][ETH_GSTRING_LEN] = { 542c919835SJordan Kim "tx_posted_desc[%u]", "tx_completed_desc[%u]", "tx_consumed_desc[%u]", "tx_bytes[%u]", 55433e274bSKuo Zhao "tx_wake[%u]", "tx_stop[%u]", "tx_event_counter[%u]", 566f007c64SCatherine Sullivan "tx_dma_mapping_error[%u]", 57433e274bSKuo Zhao }; 58433e274bSKuo Zhao 59433e274bSKuo Zhao static const char gve_gstrings_adminq_stats[][ETH_GSTRING_LEN] = { 60433e274bSKuo Zhao "adminq_prod_cnt", "adminq_cmd_fail", "adminq_timeouts", 61433e274bSKuo Zhao "adminq_describe_device_cnt", "adminq_cfg_device_resources_cnt", 62433e274bSKuo Zhao "adminq_register_page_list_cnt", "adminq_unregister_page_list_cnt", 63433e274bSKuo Zhao "adminq_create_tx_queue_cnt", "adminq_create_rx_queue_cnt", 64433e274bSKuo Zhao "adminq_destroy_tx_queue_cnt", "adminq_destroy_rx_queue_cnt", 65433e274bSKuo Zhao "adminq_dcfg_device_resources_cnt", "adminq_set_driver_parameter_cnt", 667e074d5aSDavid Awogbemila "adminq_report_stats_cnt", "adminq_report_link_speed_cnt" 6724aeb56fSKuo Zhao }; 6824aeb56fSKuo Zhao 6924aeb56fSKuo Zhao static const char gve_gstrings_priv_flags[][ETH_GSTRING_LEN] = { 7024aeb56fSKuo Zhao "report-stats", 71e5b845dcSCatherine Sullivan }; 72e5b845dcSCatherine Sullivan 73e5b845dcSCatherine Sullivan #define GVE_MAIN_STATS_LEN ARRAY_SIZE(gve_gstrings_main_stats) 74433e274bSKuo Zhao #define GVE_ADMINQ_STATS_LEN ARRAY_SIZE(gve_gstrings_adminq_stats) 75433e274bSKuo Zhao #define NUM_GVE_TX_CNTS ARRAY_SIZE(gve_gstrings_tx_stats) 76433e274bSKuo Zhao #define NUM_GVE_RX_CNTS ARRAY_SIZE(gve_gstrings_rx_stats) 7724aeb56fSKuo Zhao #define GVE_PRIV_FLAGS_STR_LEN ARRAY_SIZE(gve_gstrings_priv_flags) 78e5b845dcSCatherine Sullivan 79e5b845dcSCatherine Sullivan static void gve_get_strings(struct net_device *netdev, u32 stringset, u8 *data) 80e5b845dcSCatherine Sullivan { 81e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 82e5b845dcSCatherine Sullivan char *s = (char *)data; 83433e274bSKuo Zhao int i, j; 84e5b845dcSCatherine Sullivan 8524aeb56fSKuo Zhao switch (stringset) { 8624aeb56fSKuo Zhao case ETH_SS_STATS: 87e5b845dcSCatherine Sullivan memcpy(s, *gve_gstrings_main_stats, 88e5b845dcSCatherine Sullivan sizeof(gve_gstrings_main_stats)); 89e5b845dcSCatherine Sullivan s += sizeof(gve_gstrings_main_stats); 9024aeb56fSKuo Zhao 91e5b845dcSCatherine Sullivan for (i = 0; i < priv->rx_cfg.num_queues; i++) { 92433e274bSKuo Zhao for (j = 0; j < NUM_GVE_RX_CNTS; j++) { 9324aeb56fSKuo Zhao snprintf(s, ETH_GSTRING_LEN, 9424aeb56fSKuo Zhao gve_gstrings_rx_stats[j], i); 95e5b845dcSCatherine Sullivan s += ETH_GSTRING_LEN; 96433e274bSKuo Zhao } 97e5b845dcSCatherine Sullivan } 9824aeb56fSKuo Zhao 99e5b845dcSCatherine Sullivan for (i = 0; i < priv->tx_cfg.num_queues; i++) { 100433e274bSKuo Zhao for (j = 0; j < NUM_GVE_TX_CNTS; j++) { 10124aeb56fSKuo Zhao snprintf(s, ETH_GSTRING_LEN, 10224aeb56fSKuo Zhao gve_gstrings_tx_stats[j], i); 103e5b845dcSCatherine Sullivan s += ETH_GSTRING_LEN; 104e5b845dcSCatherine Sullivan } 105e5b845dcSCatherine Sullivan } 106e5b845dcSCatherine Sullivan 107433e274bSKuo Zhao memcpy(s, *gve_gstrings_adminq_stats, 108433e274bSKuo Zhao sizeof(gve_gstrings_adminq_stats)); 109433e274bSKuo Zhao s += sizeof(gve_gstrings_adminq_stats); 11024aeb56fSKuo Zhao break; 11124aeb56fSKuo Zhao 11224aeb56fSKuo Zhao case ETH_SS_PRIV_FLAGS: 11324aeb56fSKuo Zhao memcpy(s, *gve_gstrings_priv_flags, 11424aeb56fSKuo Zhao sizeof(gve_gstrings_priv_flags)); 11524aeb56fSKuo Zhao s += sizeof(gve_gstrings_priv_flags); 11624aeb56fSKuo Zhao break; 11724aeb56fSKuo Zhao 11824aeb56fSKuo Zhao default: 11924aeb56fSKuo Zhao break; 12024aeb56fSKuo Zhao } 121433e274bSKuo Zhao } 122433e274bSKuo Zhao 123e5b845dcSCatherine Sullivan static int gve_get_sset_count(struct net_device *netdev, int sset) 124e5b845dcSCatherine Sullivan { 125e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 126e5b845dcSCatherine Sullivan 127e5b845dcSCatherine Sullivan switch (sset) { 128e5b845dcSCatherine Sullivan case ETH_SS_STATS: 129433e274bSKuo Zhao return GVE_MAIN_STATS_LEN + GVE_ADMINQ_STATS_LEN + 130e5b845dcSCatherine Sullivan (priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS) + 131e5b845dcSCatherine Sullivan (priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS); 13224aeb56fSKuo Zhao case ETH_SS_PRIV_FLAGS: 13324aeb56fSKuo Zhao return GVE_PRIV_FLAGS_STR_LEN; 134e5b845dcSCatherine Sullivan default: 135e5b845dcSCatherine Sullivan return -EOPNOTSUPP; 136e5b845dcSCatherine Sullivan } 137e5b845dcSCatherine Sullivan } 138e5b845dcSCatherine Sullivan 139e5b845dcSCatherine Sullivan static void 140e5b845dcSCatherine Sullivan gve_get_ethtool_stats(struct net_device *netdev, 141e5b845dcSCatherine Sullivan struct ethtool_stats *stats, u64 *data) 142e5b845dcSCatherine Sullivan { 1432c919835SJordan Kim u64 tmp_rx_pkts, tmp_rx_bytes, tmp_rx_skb_alloc_fail, 1442c919835SJordan Kim tmp_rx_buf_alloc_fail, tmp_rx_desc_err_dropped_pkt, 1452c919835SJordan Kim tmp_tx_pkts, tmp_tx_bytes; 146433e274bSKuo Zhao u64 rx_buf_alloc_fail, rx_desc_err_dropped_pkt, rx_pkts, 1472c919835SJordan Kim rx_skb_alloc_fail, rx_bytes, tx_pkts, tx_bytes, tx_dropped; 1482f523dc3SDavid Awogbemila int stats_idx, base_stats_idx, max_stats_idx; 1492f523dc3SDavid Awogbemila struct stats *report_stats; 1502f523dc3SDavid Awogbemila int *rx_qid_to_stats_idx; 1512f523dc3SDavid Awogbemila int *tx_qid_to_stats_idx; 152433e274bSKuo Zhao struct gve_priv *priv; 1532f523dc3SDavid Awogbemila bool skip_nic_stats; 154e5b845dcSCatherine Sullivan unsigned int start; 155e5b845dcSCatherine Sullivan int ring; 1562f523dc3SDavid Awogbemila int i, j; 157e5b845dcSCatherine Sullivan 158e5b845dcSCatherine Sullivan ASSERT_RTNL(); 159e5b845dcSCatherine Sullivan 160433e274bSKuo Zhao priv = netdev_priv(netdev); 1612f523dc3SDavid Awogbemila report_stats = priv->stats_report->stats; 1622f523dc3SDavid Awogbemila rx_qid_to_stats_idx = kmalloc_array(priv->rx_cfg.num_queues, 1632f523dc3SDavid Awogbemila sizeof(int), GFP_KERNEL); 1642f523dc3SDavid Awogbemila if (!rx_qid_to_stats_idx) 1652f523dc3SDavid Awogbemila return; 1662f523dc3SDavid Awogbemila tx_qid_to_stats_idx = kmalloc_array(priv->tx_cfg.num_queues, 1672f523dc3SDavid Awogbemila sizeof(int), GFP_KERNEL); 1682f523dc3SDavid Awogbemila if (!tx_qid_to_stats_idx) { 1692f523dc3SDavid Awogbemila kfree(rx_qid_to_stats_idx); 1702f523dc3SDavid Awogbemila return; 1712f523dc3SDavid Awogbemila } 172433e274bSKuo Zhao for (rx_pkts = 0, rx_bytes = 0, rx_skb_alloc_fail = 0, 173433e274bSKuo Zhao rx_buf_alloc_fail = 0, rx_desc_err_dropped_pkt = 0, ring = 0; 174e5b845dcSCatherine Sullivan ring < priv->rx_cfg.num_queues; ring++) { 175e5b845dcSCatherine Sullivan if (priv->rx) { 176e5b845dcSCatherine Sullivan do { 177433e274bSKuo Zhao struct gve_rx_ring *rx = &priv->rx[ring]; 178433e274bSKuo Zhao 1793c13ce74SCatherine Sullivan start = 180e5b845dcSCatherine Sullivan u64_stats_fetch_begin(&priv->rx[ring].statss); 181433e274bSKuo Zhao tmp_rx_pkts = rx->rpackets; 182433e274bSKuo Zhao tmp_rx_bytes = rx->rbytes; 183433e274bSKuo Zhao tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail; 184433e274bSKuo Zhao tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail; 185433e274bSKuo Zhao tmp_rx_desc_err_dropped_pkt = 186433e274bSKuo Zhao rx->rx_desc_err_dropped_pkt; 187e5b845dcSCatherine Sullivan } while (u64_stats_fetch_retry(&priv->rx[ring].statss, 188e5b845dcSCatherine Sullivan start)); 189433e274bSKuo Zhao rx_pkts += tmp_rx_pkts; 190433e274bSKuo Zhao rx_bytes += tmp_rx_bytes; 191433e274bSKuo Zhao rx_skb_alloc_fail += tmp_rx_skb_alloc_fail; 192433e274bSKuo Zhao rx_buf_alloc_fail += tmp_rx_buf_alloc_fail; 193433e274bSKuo Zhao rx_desc_err_dropped_pkt += tmp_rx_desc_err_dropped_pkt; 194e5b845dcSCatherine Sullivan } 195e5b845dcSCatherine Sullivan } 1962c919835SJordan Kim for (tx_pkts = 0, tx_bytes = 0, tx_dropped = 0, ring = 0; 197e5b845dcSCatherine Sullivan ring < priv->tx_cfg.num_queues; ring++) { 198e5b845dcSCatherine Sullivan if (priv->tx) { 199e5b845dcSCatherine Sullivan do { 2003c13ce74SCatherine Sullivan start = 201e5b845dcSCatherine Sullivan u64_stats_fetch_begin(&priv->tx[ring].statss); 202433e274bSKuo Zhao tmp_tx_pkts = priv->tx[ring].pkt_done; 203433e274bSKuo Zhao tmp_tx_bytes = priv->tx[ring].bytes_done; 204e5b845dcSCatherine Sullivan } while (u64_stats_fetch_retry(&priv->tx[ring].statss, 205e5b845dcSCatherine Sullivan start)); 206433e274bSKuo Zhao tx_pkts += tmp_tx_pkts; 207433e274bSKuo Zhao tx_bytes += tmp_tx_bytes; 2082c919835SJordan Kim tx_dropped += priv->tx[ring].dropped_pkt; 209e5b845dcSCatherine Sullivan } 210e5b845dcSCatherine Sullivan } 211e5b845dcSCatherine Sullivan 212e5b845dcSCatherine Sullivan i = 0; 213e5b845dcSCatherine Sullivan data[i++] = rx_pkts; 214e5b845dcSCatherine Sullivan data[i++] = tx_pkts; 215e5b845dcSCatherine Sullivan data[i++] = rx_bytes; 216e5b845dcSCatherine Sullivan data[i++] = tx_bytes; 217433e274bSKuo Zhao /* total rx dropped packets */ 218433e274bSKuo Zhao data[i++] = rx_skb_alloc_fail + rx_buf_alloc_fail + 219433e274bSKuo Zhao rx_desc_err_dropped_pkt; 2202c919835SJordan Kim data[i++] = tx_dropped; 221e5b845dcSCatherine Sullivan data[i++] = priv->tx_timeo_cnt; 222433e274bSKuo Zhao data[i++] = rx_skb_alloc_fail; 223433e274bSKuo Zhao data[i++] = rx_buf_alloc_fail; 224433e274bSKuo Zhao data[i++] = rx_desc_err_dropped_pkt; 225433e274bSKuo Zhao data[i++] = priv->interface_up_cnt; 226433e274bSKuo Zhao data[i++] = priv->interface_down_cnt; 227433e274bSKuo Zhao data[i++] = priv->reset_cnt; 228433e274bSKuo Zhao data[i++] = priv->page_alloc_fail; 229433e274bSKuo Zhao data[i++] = priv->dma_mapping_error; 23024aeb56fSKuo Zhao data[i++] = priv->stats_report_trigger_cnt; 231e5b845dcSCatherine Sullivan i = GVE_MAIN_STATS_LEN; 232e5b845dcSCatherine Sullivan 2332f523dc3SDavid Awogbemila /* For rx cross-reporting stats, start from nic rx stats in report */ 2342f523dc3SDavid Awogbemila base_stats_idx = GVE_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues + 2352f523dc3SDavid Awogbemila GVE_RX_STATS_REPORT_NUM * priv->rx_cfg.num_queues; 2362f523dc3SDavid Awogbemila max_stats_idx = NIC_RX_STATS_REPORT_NUM * priv->rx_cfg.num_queues + 2372f523dc3SDavid Awogbemila base_stats_idx; 2382f523dc3SDavid Awogbemila /* Preprocess the stats report for rx, map queue id to start index */ 2392f523dc3SDavid Awogbemila skip_nic_stats = false; 2402f523dc3SDavid Awogbemila for (stats_idx = base_stats_idx; stats_idx < max_stats_idx; 2412f523dc3SDavid Awogbemila stats_idx += NIC_RX_STATS_REPORT_NUM) { 2422f523dc3SDavid Awogbemila u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name); 2432f523dc3SDavid Awogbemila u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id); 2442f523dc3SDavid Awogbemila 2452f523dc3SDavid Awogbemila if (stat_name == 0) { 2462f523dc3SDavid Awogbemila /* no stats written by NIC yet */ 2472f523dc3SDavid Awogbemila skip_nic_stats = true; 2482f523dc3SDavid Awogbemila break; 2492f523dc3SDavid Awogbemila } 2502f523dc3SDavid Awogbemila rx_qid_to_stats_idx[queue_id] = stats_idx; 2512f523dc3SDavid Awogbemila } 252e5b845dcSCatherine Sullivan /* walk RX rings */ 253e5b845dcSCatherine Sullivan if (priv->rx) { 254e5b845dcSCatherine Sullivan for (ring = 0; ring < priv->rx_cfg.num_queues; ring++) { 255e5b845dcSCatherine Sullivan struct gve_rx_ring *rx = &priv->rx[ring]; 256e5b845dcSCatherine Sullivan 257438b43bdSCatherine Sullivan data[i++] = rx->fill_cnt; 258433e274bSKuo Zhao data[i++] = rx->cnt; 2592c919835SJordan Kim data[i++] = rx->fill_cnt - rx->cnt; 260433e274bSKuo Zhao do { 261433e274bSKuo Zhao start = 262433e274bSKuo Zhao u64_stats_fetch_begin(&priv->rx[ring].statss); 263433e274bSKuo Zhao tmp_rx_bytes = rx->rbytes; 264433e274bSKuo Zhao tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail; 265433e274bSKuo Zhao tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail; 266433e274bSKuo Zhao tmp_rx_desc_err_dropped_pkt = 267433e274bSKuo Zhao rx->rx_desc_err_dropped_pkt; 268433e274bSKuo Zhao } while (u64_stats_fetch_retry(&priv->rx[ring].statss, 269433e274bSKuo Zhao start)); 270433e274bSKuo Zhao data[i++] = tmp_rx_bytes; 27137149e93SDavid Awogbemila data[i++] = rx->rx_cont_packet_cnt; 27237149e93SDavid Awogbemila data[i++] = rx->rx_frag_flip_cnt; 27337149e93SDavid Awogbemila data[i++] = rx->rx_frag_copy_cnt; 274433e274bSKuo Zhao /* rx dropped packets */ 275433e274bSKuo Zhao data[i++] = tmp_rx_skb_alloc_fail + 276433e274bSKuo Zhao tmp_rx_buf_alloc_fail + 277433e274bSKuo Zhao tmp_rx_desc_err_dropped_pkt; 278433e274bSKuo Zhao data[i++] = rx->rx_copybreak_pkt; 279433e274bSKuo Zhao data[i++] = rx->rx_copied_pkt; 2802f523dc3SDavid Awogbemila /* stats from NIC */ 2812f523dc3SDavid Awogbemila if (skip_nic_stats) { 2822f523dc3SDavid Awogbemila /* skip NIC rx stats */ 2832f523dc3SDavid Awogbemila i += NIC_RX_STATS_REPORT_NUM; 2842f523dc3SDavid Awogbemila continue; 2852f523dc3SDavid Awogbemila } 2862f523dc3SDavid Awogbemila for (j = 0; j < NIC_RX_STATS_REPORT_NUM; j++) { 2872f523dc3SDavid Awogbemila u64 value = 2882f523dc3SDavid Awogbemila be64_to_cpu(report_stats[rx_qid_to_stats_idx[ring] + j].value); 2892f523dc3SDavid Awogbemila 2902f523dc3SDavid Awogbemila data[i++] = value; 2912f523dc3SDavid Awogbemila } 292e5b845dcSCatherine Sullivan } 293e5b845dcSCatherine Sullivan } else { 294e5b845dcSCatherine Sullivan i += priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS; 295e5b845dcSCatherine Sullivan } 2962f523dc3SDavid Awogbemila 2972f523dc3SDavid Awogbemila /* For tx cross-reporting stats, start from nic tx stats in report */ 2982f523dc3SDavid Awogbemila base_stats_idx = max_stats_idx; 2992f523dc3SDavid Awogbemila max_stats_idx = NIC_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues + 3002f523dc3SDavid Awogbemila max_stats_idx; 3012f523dc3SDavid Awogbemila /* Preprocess the stats report for tx, map queue id to start index */ 3022f523dc3SDavid Awogbemila skip_nic_stats = false; 3032f523dc3SDavid Awogbemila for (stats_idx = base_stats_idx; stats_idx < max_stats_idx; 3042f523dc3SDavid Awogbemila stats_idx += NIC_TX_STATS_REPORT_NUM) { 3052f523dc3SDavid Awogbemila u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name); 3062f523dc3SDavid Awogbemila u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id); 3072f523dc3SDavid Awogbemila 3082f523dc3SDavid Awogbemila if (stat_name == 0) { 3092f523dc3SDavid Awogbemila /* no stats written by NIC yet */ 3102f523dc3SDavid Awogbemila skip_nic_stats = true; 3112f523dc3SDavid Awogbemila break; 3122f523dc3SDavid Awogbemila } 3132f523dc3SDavid Awogbemila tx_qid_to_stats_idx[queue_id] = stats_idx; 3142f523dc3SDavid Awogbemila } 315e5b845dcSCatherine Sullivan /* walk TX rings */ 316e5b845dcSCatherine Sullivan if (priv->tx) { 317e5b845dcSCatherine Sullivan for (ring = 0; ring < priv->tx_cfg.num_queues; ring++) { 318e5b845dcSCatherine Sullivan struct gve_tx_ring *tx = &priv->tx[ring]; 319e5b845dcSCatherine Sullivan 3205e8c5adfSBailey Forrest if (gve_is_gqi(priv)) { 321e5b845dcSCatherine Sullivan data[i++] = tx->req; 322e5b845dcSCatherine Sullivan data[i++] = tx->done; 3232c919835SJordan Kim data[i++] = tx->req - tx->done; 3245e8c5adfSBailey Forrest } else { 3255e8c5adfSBailey Forrest /* DQO doesn't currently support 3265e8c5adfSBailey Forrest * posted/completed descriptor counts; 3275e8c5adfSBailey Forrest */ 3285e8c5adfSBailey Forrest data[i++] = 0; 3295e8c5adfSBailey Forrest data[i++] = 0; 3302c919835SJordan Kim data[i++] = tx->dqo_tx.tail - tx->dqo_tx.head; 3315e8c5adfSBailey Forrest } 332433e274bSKuo Zhao do { 333433e274bSKuo Zhao start = 334433e274bSKuo Zhao u64_stats_fetch_begin(&priv->tx[ring].statss); 335433e274bSKuo Zhao tmp_tx_bytes = tx->bytes_done; 336433e274bSKuo Zhao } while (u64_stats_fetch_retry(&priv->tx[ring].statss, 337433e274bSKuo Zhao start)); 338433e274bSKuo Zhao data[i++] = tmp_tx_bytes; 339e5b845dcSCatherine Sullivan data[i++] = tx->wake_queue; 340e5b845dcSCatherine Sullivan data[i++] = tx->stop_queue; 34161d72c7eSTao Liu data[i++] = gve_tx_load_event_counter(priv, tx); 3426f007c64SCatherine Sullivan data[i++] = tx->dma_mapping_error; 3432f523dc3SDavid Awogbemila /* stats from NIC */ 3442f523dc3SDavid Awogbemila if (skip_nic_stats) { 3452f523dc3SDavid Awogbemila /* skip NIC tx stats */ 3462f523dc3SDavid Awogbemila i += NIC_TX_STATS_REPORT_NUM; 3472f523dc3SDavid Awogbemila continue; 3482f523dc3SDavid Awogbemila } 3492f523dc3SDavid Awogbemila for (j = 0; j < NIC_TX_STATS_REPORT_NUM; j++) { 3502f523dc3SDavid Awogbemila u64 value = 3512f523dc3SDavid Awogbemila be64_to_cpu(report_stats[tx_qid_to_stats_idx[ring] + j].value); 3522f523dc3SDavid Awogbemila data[i++] = value; 3532f523dc3SDavid Awogbemila } 354e5b845dcSCatherine Sullivan } 355e5b845dcSCatherine Sullivan } else { 356e5b845dcSCatherine Sullivan i += priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS; 357e5b845dcSCatherine Sullivan } 3582f523dc3SDavid Awogbemila 3592f523dc3SDavid Awogbemila kfree(rx_qid_to_stats_idx); 3602f523dc3SDavid Awogbemila kfree(tx_qid_to_stats_idx); 361433e274bSKuo Zhao /* AQ Stats */ 362433e274bSKuo Zhao data[i++] = priv->adminq_prod_cnt; 363433e274bSKuo Zhao data[i++] = priv->adminq_cmd_fail; 364433e274bSKuo Zhao data[i++] = priv->adminq_timeouts; 365433e274bSKuo Zhao data[i++] = priv->adminq_describe_device_cnt; 366433e274bSKuo Zhao data[i++] = priv->adminq_cfg_device_resources_cnt; 367433e274bSKuo Zhao data[i++] = priv->adminq_register_page_list_cnt; 368433e274bSKuo Zhao data[i++] = priv->adminq_unregister_page_list_cnt; 369433e274bSKuo Zhao data[i++] = priv->adminq_create_tx_queue_cnt; 370433e274bSKuo Zhao data[i++] = priv->adminq_create_rx_queue_cnt; 371433e274bSKuo Zhao data[i++] = priv->adminq_destroy_tx_queue_cnt; 372433e274bSKuo Zhao data[i++] = priv->adminq_destroy_rx_queue_cnt; 373433e274bSKuo Zhao data[i++] = priv->adminq_dcfg_device_resources_cnt; 374433e274bSKuo Zhao data[i++] = priv->adminq_set_driver_parameter_cnt; 37524aeb56fSKuo Zhao data[i++] = priv->adminq_report_stats_cnt; 3767e074d5aSDavid Awogbemila data[i++] = priv->adminq_report_link_speed_cnt; 377e5b845dcSCatherine Sullivan } 378e5b845dcSCatherine Sullivan 379e5b845dcSCatherine Sullivan static void gve_get_channels(struct net_device *netdev, 380e5b845dcSCatherine Sullivan struct ethtool_channels *cmd) 381e5b845dcSCatherine Sullivan { 382e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 383e5b845dcSCatherine Sullivan 384e5b845dcSCatherine Sullivan cmd->max_rx = priv->rx_cfg.max_queues; 385e5b845dcSCatherine Sullivan cmd->max_tx = priv->tx_cfg.max_queues; 386e5b845dcSCatherine Sullivan cmd->max_other = 0; 387e5b845dcSCatherine Sullivan cmd->max_combined = 0; 388e5b845dcSCatherine Sullivan cmd->rx_count = priv->rx_cfg.num_queues; 389e5b845dcSCatherine Sullivan cmd->tx_count = priv->tx_cfg.num_queues; 390e5b845dcSCatherine Sullivan cmd->other_count = 0; 391e5b845dcSCatherine Sullivan cmd->combined_count = 0; 392e5b845dcSCatherine Sullivan } 393e5b845dcSCatherine Sullivan 394e5b845dcSCatherine Sullivan static int gve_set_channels(struct net_device *netdev, 395e5b845dcSCatherine Sullivan struct ethtool_channels *cmd) 396e5b845dcSCatherine Sullivan { 397e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 398e5b845dcSCatherine Sullivan struct gve_queue_config new_tx_cfg = priv->tx_cfg; 399e5b845dcSCatherine Sullivan struct gve_queue_config new_rx_cfg = priv->rx_cfg; 400e5b845dcSCatherine Sullivan struct ethtool_channels old_settings; 401e5b845dcSCatherine Sullivan int new_tx = cmd->tx_count; 402e5b845dcSCatherine Sullivan int new_rx = cmd->rx_count; 403e5b845dcSCatherine Sullivan 404e5b845dcSCatherine Sullivan gve_get_channels(netdev, &old_settings); 405e5b845dcSCatherine Sullivan 406f67435b5SDaode Huang /* Changing combined is not allowed */ 407e5b845dcSCatherine Sullivan if (cmd->combined_count != old_settings.combined_count) 408e5b845dcSCatherine Sullivan return -EINVAL; 409e5b845dcSCatherine Sullivan 410e5b845dcSCatherine Sullivan if (!new_rx || !new_tx) 411e5b845dcSCatherine Sullivan return -EINVAL; 412e5b845dcSCatherine Sullivan 413e5b845dcSCatherine Sullivan if (!netif_carrier_ok(netdev)) { 414e5b845dcSCatherine Sullivan priv->tx_cfg.num_queues = new_tx; 415e5b845dcSCatherine Sullivan priv->rx_cfg.num_queues = new_rx; 416e5b845dcSCatherine Sullivan return 0; 417e5b845dcSCatherine Sullivan } 418e5b845dcSCatherine Sullivan 419e5b845dcSCatherine Sullivan new_tx_cfg.num_queues = new_tx; 420e5b845dcSCatherine Sullivan new_rx_cfg.num_queues = new_rx; 421e5b845dcSCatherine Sullivan 422e5b845dcSCatherine Sullivan return gve_adjust_queues(priv, new_rx_cfg, new_tx_cfg); 423e5b845dcSCatherine Sullivan } 424e5b845dcSCatherine Sullivan 425e5b845dcSCatherine Sullivan static void gve_get_ringparam(struct net_device *netdev, 42674624944SHao Chen struct ethtool_ringparam *cmd, 42774624944SHao Chen struct kernel_ethtool_ringparam *kernel_cmd, 42874624944SHao Chen struct netlink_ext_ack *extack) 429e5b845dcSCatherine Sullivan { 430e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 431e5b845dcSCatherine Sullivan 432e5b845dcSCatherine Sullivan cmd->rx_max_pending = priv->rx_desc_cnt; 433e5b845dcSCatherine Sullivan cmd->tx_max_pending = priv->tx_desc_cnt; 434e5b845dcSCatherine Sullivan cmd->rx_pending = priv->rx_desc_cnt; 435e5b845dcSCatherine Sullivan cmd->tx_pending = priv->tx_desc_cnt; 436e5b845dcSCatherine Sullivan } 437e5b845dcSCatherine Sullivan 438e5b845dcSCatherine Sullivan static int gve_user_reset(struct net_device *netdev, u32 *flags) 439e5b845dcSCatherine Sullivan { 440e5b845dcSCatherine Sullivan struct gve_priv *priv = netdev_priv(netdev); 441e5b845dcSCatherine Sullivan 442e5b845dcSCatherine Sullivan if (*flags == ETH_RESET_ALL) { 443e5b845dcSCatherine Sullivan *flags = 0; 444e5b845dcSCatherine Sullivan return gve_reset(priv, true); 445e5b845dcSCatherine Sullivan } 446e5b845dcSCatherine Sullivan 447e5b845dcSCatherine Sullivan return -EOPNOTSUPP; 448e5b845dcSCatherine Sullivan } 449e5b845dcSCatherine Sullivan 450d5f7543cSKuo Zhao static int gve_get_tunable(struct net_device *netdev, 451d5f7543cSKuo Zhao const struct ethtool_tunable *etuna, void *value) 452d5f7543cSKuo Zhao { 453d5f7543cSKuo Zhao struct gve_priv *priv = netdev_priv(netdev); 454d5f7543cSKuo Zhao 455d5f7543cSKuo Zhao switch (etuna->id) { 456d5f7543cSKuo Zhao case ETHTOOL_RX_COPYBREAK: 457d5f7543cSKuo Zhao *(u32 *)value = priv->rx_copybreak; 458d5f7543cSKuo Zhao return 0; 459d5f7543cSKuo Zhao default: 460d5f7543cSKuo Zhao return -EOPNOTSUPP; 461d5f7543cSKuo Zhao } 462d5f7543cSKuo Zhao } 463d5f7543cSKuo Zhao 464d5f7543cSKuo Zhao static int gve_set_tunable(struct net_device *netdev, 465433e274bSKuo Zhao const struct ethtool_tunable *etuna, 466433e274bSKuo Zhao const void *value) 467d5f7543cSKuo Zhao { 468d5f7543cSKuo Zhao struct gve_priv *priv = netdev_priv(netdev); 469d5f7543cSKuo Zhao u32 len; 470d5f7543cSKuo Zhao 471d5f7543cSKuo Zhao switch (etuna->id) { 472d5f7543cSKuo Zhao case ETHTOOL_RX_COPYBREAK: 4731f6228e4SBailey Forrest { 4741f6228e4SBailey Forrest u32 max_copybreak = gve_is_gqi(priv) ? 4751f6228e4SBailey Forrest (PAGE_SIZE / 2) : priv->data_buffer_size_dqo; 4761f6228e4SBailey Forrest 477d5f7543cSKuo Zhao len = *(u32 *)value; 4781f6228e4SBailey Forrest if (len > max_copybreak) 479d5f7543cSKuo Zhao return -EINVAL; 480d5f7543cSKuo Zhao priv->rx_copybreak = len; 481d5f7543cSKuo Zhao return 0; 4821f6228e4SBailey Forrest } 483d5f7543cSKuo Zhao default: 484d5f7543cSKuo Zhao return -EOPNOTSUPP; 485d5f7543cSKuo Zhao } 486d5f7543cSKuo Zhao } 487d5f7543cSKuo Zhao 48824aeb56fSKuo Zhao static u32 gve_get_priv_flags(struct net_device *netdev) 48924aeb56fSKuo Zhao { 49024aeb56fSKuo Zhao struct gve_priv *priv = netdev_priv(netdev); 49124aeb56fSKuo Zhao u32 ret_flags = 0; 49224aeb56fSKuo Zhao 49324aeb56fSKuo Zhao /* Only 1 flag exists currently: report-stats (BIT(O)), so set that flag. */ 49424aeb56fSKuo Zhao if (priv->ethtool_flags & BIT(0)) 49524aeb56fSKuo Zhao ret_flags |= BIT(0); 49624aeb56fSKuo Zhao return ret_flags; 49724aeb56fSKuo Zhao } 49824aeb56fSKuo Zhao 49924aeb56fSKuo Zhao static int gve_set_priv_flags(struct net_device *netdev, u32 flags) 50024aeb56fSKuo Zhao { 50124aeb56fSKuo Zhao struct gve_priv *priv = netdev_priv(netdev); 50224aeb56fSKuo Zhao u64 ori_flags, new_flags; 50324aeb56fSKuo Zhao 50424aeb56fSKuo Zhao ori_flags = READ_ONCE(priv->ethtool_flags); 50524aeb56fSKuo Zhao new_flags = ori_flags; 50624aeb56fSKuo Zhao 50724aeb56fSKuo Zhao /* Only one priv flag exists: report-stats (BIT(0))*/ 50824aeb56fSKuo Zhao if (flags & BIT(0)) 50924aeb56fSKuo Zhao new_flags |= BIT(0); 51024aeb56fSKuo Zhao else 51124aeb56fSKuo Zhao new_flags &= ~(BIT(0)); 51224aeb56fSKuo Zhao priv->ethtool_flags = new_flags; 51324aeb56fSKuo Zhao /* start report-stats timer when user turns report stats on. */ 51424aeb56fSKuo Zhao if (flags & BIT(0)) { 51524aeb56fSKuo Zhao mod_timer(&priv->stats_report_timer, 51624aeb56fSKuo Zhao round_jiffies(jiffies + 51724aeb56fSKuo Zhao msecs_to_jiffies(priv->stats_report_timer_period))); 51824aeb56fSKuo Zhao } 51924aeb56fSKuo Zhao /* Zero off gve stats when report-stats turned off and */ 52024aeb56fSKuo Zhao /* delete report stats timer. */ 52124aeb56fSKuo Zhao if (!(flags & BIT(0)) && (ori_flags & BIT(0))) { 52224aeb56fSKuo Zhao int tx_stats_num = GVE_TX_STATS_REPORT_NUM * 52324aeb56fSKuo Zhao priv->tx_cfg.num_queues; 52424aeb56fSKuo Zhao int rx_stats_num = GVE_RX_STATS_REPORT_NUM * 52524aeb56fSKuo Zhao priv->rx_cfg.num_queues; 52624aeb56fSKuo Zhao 52724aeb56fSKuo Zhao memset(priv->stats_report->stats, 0, (tx_stats_num + rx_stats_num) * 52824aeb56fSKuo Zhao sizeof(struct stats)); 52924aeb56fSKuo Zhao del_timer_sync(&priv->stats_report_timer); 53024aeb56fSKuo Zhao } 53124aeb56fSKuo Zhao return 0; 53224aeb56fSKuo Zhao } 53324aeb56fSKuo Zhao 5347e074d5aSDavid Awogbemila static int gve_get_link_ksettings(struct net_device *netdev, 5357e074d5aSDavid Awogbemila struct ethtool_link_ksettings *cmd) 5367e074d5aSDavid Awogbemila { 5377e074d5aSDavid Awogbemila struct gve_priv *priv = netdev_priv(netdev); 5387e074d5aSDavid Awogbemila int err = gve_adminq_report_link_speed(priv); 5397e074d5aSDavid Awogbemila 5407e074d5aSDavid Awogbemila cmd->base.speed = priv->link_speed; 5417e074d5aSDavid Awogbemila return err; 5427e074d5aSDavid Awogbemila } 5437e074d5aSDavid Awogbemila 544*6081ac20STao Liu static int gve_get_coalesce(struct net_device *netdev, 545*6081ac20STao Liu struct ethtool_coalesce *ec, 546*6081ac20STao Liu struct kernel_ethtool_coalesce *kernel_ec, 547*6081ac20STao Liu struct netlink_ext_ack *extack) 548*6081ac20STao Liu { 549*6081ac20STao Liu struct gve_priv *priv = netdev_priv(netdev); 550*6081ac20STao Liu 551*6081ac20STao Liu if (gve_is_gqi(priv)) 552*6081ac20STao Liu return -EOPNOTSUPP; 553*6081ac20STao Liu ec->tx_coalesce_usecs = priv->tx_coalesce_usecs; 554*6081ac20STao Liu ec->rx_coalesce_usecs = priv->rx_coalesce_usecs; 555*6081ac20STao Liu 556*6081ac20STao Liu return 0; 557*6081ac20STao Liu } 558*6081ac20STao Liu 559*6081ac20STao Liu static int gve_set_coalesce(struct net_device *netdev, 560*6081ac20STao Liu struct ethtool_coalesce *ec, 561*6081ac20STao Liu struct kernel_ethtool_coalesce *kernel_ec, 562*6081ac20STao Liu struct netlink_ext_ack *extack) 563*6081ac20STao Liu { 564*6081ac20STao Liu struct gve_priv *priv = netdev_priv(netdev); 565*6081ac20STao Liu u32 tx_usecs_orig = priv->tx_coalesce_usecs; 566*6081ac20STao Liu u32 rx_usecs_orig = priv->rx_coalesce_usecs; 567*6081ac20STao Liu int idx; 568*6081ac20STao Liu 569*6081ac20STao Liu if (gve_is_gqi(priv)) 570*6081ac20STao Liu return -EOPNOTSUPP; 571*6081ac20STao Liu 572*6081ac20STao Liu if (ec->tx_coalesce_usecs > GVE_MAX_ITR_INTERVAL_DQO || 573*6081ac20STao Liu ec->rx_coalesce_usecs > GVE_MAX_ITR_INTERVAL_DQO) 574*6081ac20STao Liu return -EINVAL; 575*6081ac20STao Liu priv->tx_coalesce_usecs = ec->tx_coalesce_usecs; 576*6081ac20STao Liu priv->rx_coalesce_usecs = ec->rx_coalesce_usecs; 577*6081ac20STao Liu 578*6081ac20STao Liu if (tx_usecs_orig != priv->tx_coalesce_usecs) { 579*6081ac20STao Liu for (idx = 0; idx < priv->tx_cfg.num_queues; idx++) { 580*6081ac20STao Liu int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx); 581*6081ac20STao Liu struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx]; 582*6081ac20STao Liu 583*6081ac20STao Liu gve_set_itr_coalesce_usecs_dqo(priv, block, 584*6081ac20STao Liu priv->tx_coalesce_usecs); 585*6081ac20STao Liu } 586*6081ac20STao Liu } 587*6081ac20STao Liu 588*6081ac20STao Liu if (rx_usecs_orig != priv->rx_coalesce_usecs) { 589*6081ac20STao Liu for (idx = 0; idx < priv->rx_cfg.num_queues; idx++) { 590*6081ac20STao Liu int ntfy_idx = gve_rx_idx_to_ntfy(priv, idx); 591*6081ac20STao Liu struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx]; 592*6081ac20STao Liu 593*6081ac20STao Liu gve_set_itr_coalesce_usecs_dqo(priv, block, 594*6081ac20STao Liu priv->rx_coalesce_usecs); 595*6081ac20STao Liu } 596*6081ac20STao Liu } 597*6081ac20STao Liu 598*6081ac20STao Liu return 0; 599*6081ac20STao Liu } 600*6081ac20STao Liu 601e5b845dcSCatherine Sullivan const struct ethtool_ops gve_ethtool_ops = { 602*6081ac20STao Liu .supported_coalesce_params = ETHTOOL_COALESCE_USECS, 603e5b845dcSCatherine Sullivan .get_drvinfo = gve_get_drvinfo, 604e5b845dcSCatherine Sullivan .get_strings = gve_get_strings, 605e5b845dcSCatherine Sullivan .get_sset_count = gve_get_sset_count, 606e5b845dcSCatherine Sullivan .get_ethtool_stats = gve_get_ethtool_stats, 607e5b845dcSCatherine Sullivan .set_msglevel = gve_set_msglevel, 608e5b845dcSCatherine Sullivan .get_msglevel = gve_get_msglevel, 609e5b845dcSCatherine Sullivan .set_channels = gve_set_channels, 610e5b845dcSCatherine Sullivan .get_channels = gve_get_channels, 611e5b845dcSCatherine Sullivan .get_link = ethtool_op_get_link, 612*6081ac20STao Liu .get_coalesce = gve_get_coalesce, 613*6081ac20STao Liu .set_coalesce = gve_set_coalesce, 614e5b845dcSCatherine Sullivan .get_ringparam = gve_get_ringparam, 615e5b845dcSCatherine Sullivan .reset = gve_user_reset, 616d5f7543cSKuo Zhao .get_tunable = gve_get_tunable, 617d5f7543cSKuo Zhao .set_tunable = gve_set_tunable, 61824aeb56fSKuo Zhao .get_priv_flags = gve_get_priv_flags, 61924aeb56fSKuo Zhao .set_priv_flags = gve_set_priv_flags, 6207e074d5aSDavid Awogbemila .get_link_ksettings = gve_get_link_ksettings 621e5b845dcSCatherine Sullivan }; 622