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"
11e5b845dcSCatherine Sullivan 
12e5b845dcSCatherine Sullivan static void gve_get_drvinfo(struct net_device *netdev,
13e5b845dcSCatherine Sullivan 			    struct ethtool_drvinfo *info)
14e5b845dcSCatherine Sullivan {
15e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
16e5b845dcSCatherine Sullivan 
17c32773c9SDaode Huang 	strscpy(info->driver, "gve", sizeof(info->driver));
18c32773c9SDaode Huang 	strscpy(info->version, gve_version_str, sizeof(info->version));
19c32773c9SDaode Huang 	strscpy(info->bus_info, pci_name(priv->pdev), sizeof(info->bus_info));
20e5b845dcSCatherine Sullivan }
21e5b845dcSCatherine Sullivan 
22e5b845dcSCatherine Sullivan static void gve_set_msglevel(struct net_device *netdev, u32 value)
23e5b845dcSCatherine Sullivan {
24e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
25e5b845dcSCatherine Sullivan 
26e5b845dcSCatherine Sullivan 	priv->msg_enable = value;
27e5b845dcSCatherine Sullivan }
28e5b845dcSCatherine Sullivan 
29e5b845dcSCatherine Sullivan static u32 gve_get_msglevel(struct net_device *netdev)
30e5b845dcSCatherine Sullivan {
31e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
32e5b845dcSCatherine Sullivan 
33e5b845dcSCatherine Sullivan 	return priv->msg_enable;
34e5b845dcSCatherine Sullivan }
35e5b845dcSCatherine Sullivan 
36e5b845dcSCatherine Sullivan static const char gve_gstrings_main_stats[][ETH_GSTRING_LEN] = {
37e5b845dcSCatherine Sullivan 	"rx_packets", "tx_packets", "rx_bytes", "tx_bytes",
38e5b845dcSCatherine Sullivan 	"rx_dropped", "tx_dropped", "tx_timeouts",
39433e274bSKuo Zhao 	"rx_skb_alloc_fail", "rx_buf_alloc_fail", "rx_desc_err_dropped_pkt",
40433e274bSKuo Zhao 	"interface_up_cnt", "interface_down_cnt", "reset_cnt",
4124aeb56fSKuo Zhao 	"page_alloc_fail", "dma_mapping_error", "stats_report_trigger_cnt",
42433e274bSKuo Zhao };
43433e274bSKuo Zhao 
44433e274bSKuo Zhao static const char gve_gstrings_rx_stats[][ETH_GSTRING_LEN] = {
45433e274bSKuo Zhao 	"rx_posted_desc[%u]", "rx_completed_desc[%u]", "rx_bytes[%u]",
46*37149e93SDavid Awogbemila 	"rx_cont_packet_cnt[%u]", "rx_frag_flip_cnt[%u]", "rx_frag_copy_cnt[%u]",
47433e274bSKuo Zhao 	"rx_dropped_pkt[%u]", "rx_copybreak_pkt[%u]", "rx_copied_pkt[%u]",
482f523dc3SDavid Awogbemila 	"rx_queue_drop_cnt[%u]", "rx_no_buffers_posted[%u]",
492f523dc3SDavid Awogbemila 	"rx_drops_packet_over_mru[%u]", "rx_drops_invalid_checksum[%u]",
50433e274bSKuo Zhao };
51433e274bSKuo Zhao 
52433e274bSKuo Zhao static const char gve_gstrings_tx_stats[][ETH_GSTRING_LEN] = {
53433e274bSKuo Zhao 	"tx_posted_desc[%u]", "tx_completed_desc[%u]", "tx_bytes[%u]",
54433e274bSKuo Zhao 	"tx_wake[%u]", "tx_stop[%u]", "tx_event_counter[%u]",
556f007c64SCatherine Sullivan 	"tx_dma_mapping_error[%u]",
56433e274bSKuo Zhao };
57433e274bSKuo Zhao 
58433e274bSKuo Zhao static const char gve_gstrings_adminq_stats[][ETH_GSTRING_LEN] = {
59433e274bSKuo Zhao 	"adminq_prod_cnt", "adminq_cmd_fail", "adminq_timeouts",
60433e274bSKuo Zhao 	"adminq_describe_device_cnt", "adminq_cfg_device_resources_cnt",
61433e274bSKuo Zhao 	"adminq_register_page_list_cnt", "adminq_unregister_page_list_cnt",
62433e274bSKuo Zhao 	"adminq_create_tx_queue_cnt", "adminq_create_rx_queue_cnt",
63433e274bSKuo Zhao 	"adminq_destroy_tx_queue_cnt", "adminq_destroy_rx_queue_cnt",
64433e274bSKuo Zhao 	"adminq_dcfg_device_resources_cnt", "adminq_set_driver_parameter_cnt",
657e074d5aSDavid Awogbemila 	"adminq_report_stats_cnt", "adminq_report_link_speed_cnt"
6624aeb56fSKuo Zhao };
6724aeb56fSKuo Zhao 
6824aeb56fSKuo Zhao static const char gve_gstrings_priv_flags[][ETH_GSTRING_LEN] = {
6924aeb56fSKuo Zhao 	"report-stats",
70e5b845dcSCatherine Sullivan };
71e5b845dcSCatherine Sullivan 
72e5b845dcSCatherine Sullivan #define GVE_MAIN_STATS_LEN  ARRAY_SIZE(gve_gstrings_main_stats)
73433e274bSKuo Zhao #define GVE_ADMINQ_STATS_LEN  ARRAY_SIZE(gve_gstrings_adminq_stats)
74433e274bSKuo Zhao #define NUM_GVE_TX_CNTS	ARRAY_SIZE(gve_gstrings_tx_stats)
75433e274bSKuo Zhao #define NUM_GVE_RX_CNTS	ARRAY_SIZE(gve_gstrings_rx_stats)
7624aeb56fSKuo Zhao #define GVE_PRIV_FLAGS_STR_LEN ARRAY_SIZE(gve_gstrings_priv_flags)
77e5b845dcSCatherine Sullivan 
78e5b845dcSCatherine Sullivan static void gve_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
79e5b845dcSCatherine Sullivan {
80e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
81e5b845dcSCatherine Sullivan 	char *s = (char *)data;
82433e274bSKuo Zhao 	int i, j;
83e5b845dcSCatherine Sullivan 
8424aeb56fSKuo Zhao 	switch (stringset) {
8524aeb56fSKuo Zhao 	case ETH_SS_STATS:
86e5b845dcSCatherine Sullivan 		memcpy(s, *gve_gstrings_main_stats,
87e5b845dcSCatherine Sullivan 		       sizeof(gve_gstrings_main_stats));
88e5b845dcSCatherine Sullivan 		s += sizeof(gve_gstrings_main_stats);
8924aeb56fSKuo Zhao 
90e5b845dcSCatherine Sullivan 		for (i = 0; i < priv->rx_cfg.num_queues; i++) {
91433e274bSKuo Zhao 			for (j = 0; j < NUM_GVE_RX_CNTS; j++) {
9224aeb56fSKuo Zhao 				snprintf(s, ETH_GSTRING_LEN,
9324aeb56fSKuo Zhao 					 gve_gstrings_rx_stats[j], i);
94e5b845dcSCatherine Sullivan 				s += ETH_GSTRING_LEN;
95433e274bSKuo Zhao 			}
96e5b845dcSCatherine Sullivan 		}
9724aeb56fSKuo Zhao 
98e5b845dcSCatherine Sullivan 		for (i = 0; i < priv->tx_cfg.num_queues; i++) {
99433e274bSKuo Zhao 			for (j = 0; j < NUM_GVE_TX_CNTS; j++) {
10024aeb56fSKuo Zhao 				snprintf(s, ETH_GSTRING_LEN,
10124aeb56fSKuo Zhao 					 gve_gstrings_tx_stats[j], i);
102e5b845dcSCatherine Sullivan 				s += ETH_GSTRING_LEN;
103e5b845dcSCatherine Sullivan 			}
104e5b845dcSCatherine Sullivan 		}
105e5b845dcSCatherine Sullivan 
106433e274bSKuo Zhao 		memcpy(s, *gve_gstrings_adminq_stats,
107433e274bSKuo Zhao 		       sizeof(gve_gstrings_adminq_stats));
108433e274bSKuo Zhao 		s += sizeof(gve_gstrings_adminq_stats);
10924aeb56fSKuo Zhao 		break;
11024aeb56fSKuo Zhao 
11124aeb56fSKuo Zhao 	case ETH_SS_PRIV_FLAGS:
11224aeb56fSKuo Zhao 		memcpy(s, *gve_gstrings_priv_flags,
11324aeb56fSKuo Zhao 		       sizeof(gve_gstrings_priv_flags));
11424aeb56fSKuo Zhao 		s += sizeof(gve_gstrings_priv_flags);
11524aeb56fSKuo Zhao 		break;
11624aeb56fSKuo Zhao 
11724aeb56fSKuo Zhao 	default:
11824aeb56fSKuo Zhao 		break;
11924aeb56fSKuo Zhao 	}
120433e274bSKuo Zhao }
121433e274bSKuo Zhao 
122e5b845dcSCatherine Sullivan static int gve_get_sset_count(struct net_device *netdev, int sset)
123e5b845dcSCatherine Sullivan {
124e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
125e5b845dcSCatherine Sullivan 
126e5b845dcSCatherine Sullivan 	switch (sset) {
127e5b845dcSCatherine Sullivan 	case ETH_SS_STATS:
128433e274bSKuo Zhao 		return GVE_MAIN_STATS_LEN + GVE_ADMINQ_STATS_LEN +
129e5b845dcSCatherine Sullivan 		       (priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS) +
130e5b845dcSCatherine Sullivan 		       (priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS);
13124aeb56fSKuo Zhao 	case ETH_SS_PRIV_FLAGS:
13224aeb56fSKuo Zhao 		return GVE_PRIV_FLAGS_STR_LEN;
133e5b845dcSCatherine Sullivan 	default:
134e5b845dcSCatherine Sullivan 		return -EOPNOTSUPP;
135e5b845dcSCatherine Sullivan 	}
136e5b845dcSCatherine Sullivan }
137e5b845dcSCatherine Sullivan 
138e5b845dcSCatherine Sullivan static void
139e5b845dcSCatherine Sullivan gve_get_ethtool_stats(struct net_device *netdev,
140e5b845dcSCatherine Sullivan 		      struct ethtool_stats *stats, u64 *data)
141e5b845dcSCatherine Sullivan {
142433e274bSKuo Zhao 	u64 tmp_rx_pkts, tmp_rx_bytes, tmp_rx_skb_alloc_fail,	tmp_rx_buf_alloc_fail,
143433e274bSKuo Zhao 		tmp_rx_desc_err_dropped_pkt, tmp_tx_pkts, tmp_tx_bytes;
144433e274bSKuo Zhao 	u64 rx_buf_alloc_fail, rx_desc_err_dropped_pkt, rx_pkts,
145433e274bSKuo Zhao 		rx_skb_alloc_fail, rx_bytes, tx_pkts, tx_bytes;
1462f523dc3SDavid Awogbemila 	int stats_idx, base_stats_idx, max_stats_idx;
1472f523dc3SDavid Awogbemila 	struct stats *report_stats;
1482f523dc3SDavid Awogbemila 	int *rx_qid_to_stats_idx;
1492f523dc3SDavid Awogbemila 	int *tx_qid_to_stats_idx;
150433e274bSKuo Zhao 	struct gve_priv *priv;
1512f523dc3SDavid Awogbemila 	bool skip_nic_stats;
152e5b845dcSCatherine Sullivan 	unsigned int start;
153e5b845dcSCatherine Sullivan 	int ring;
1542f523dc3SDavid Awogbemila 	int i, j;
155e5b845dcSCatherine Sullivan 
156e5b845dcSCatherine Sullivan 	ASSERT_RTNL();
157e5b845dcSCatherine Sullivan 
158433e274bSKuo Zhao 	priv = netdev_priv(netdev);
1592f523dc3SDavid Awogbemila 	report_stats = priv->stats_report->stats;
1602f523dc3SDavid Awogbemila 	rx_qid_to_stats_idx = kmalloc_array(priv->rx_cfg.num_queues,
1612f523dc3SDavid Awogbemila 					    sizeof(int), GFP_KERNEL);
1622f523dc3SDavid Awogbemila 	if (!rx_qid_to_stats_idx)
1632f523dc3SDavid Awogbemila 		return;
1642f523dc3SDavid Awogbemila 	tx_qid_to_stats_idx = kmalloc_array(priv->tx_cfg.num_queues,
1652f523dc3SDavid Awogbemila 					    sizeof(int), GFP_KERNEL);
1662f523dc3SDavid Awogbemila 	if (!tx_qid_to_stats_idx) {
1672f523dc3SDavid Awogbemila 		kfree(rx_qid_to_stats_idx);
1682f523dc3SDavid Awogbemila 		return;
1692f523dc3SDavid Awogbemila 	}
170433e274bSKuo Zhao 	for (rx_pkts = 0, rx_bytes = 0, rx_skb_alloc_fail = 0,
171433e274bSKuo Zhao 	     rx_buf_alloc_fail = 0, rx_desc_err_dropped_pkt = 0, ring = 0;
172e5b845dcSCatherine Sullivan 	     ring < priv->rx_cfg.num_queues; ring++) {
173e5b845dcSCatherine Sullivan 		if (priv->rx) {
174e5b845dcSCatherine Sullivan 			do {
175433e274bSKuo Zhao 				struct gve_rx_ring *rx = &priv->rx[ring];
176433e274bSKuo Zhao 
1773c13ce74SCatherine Sullivan 				start =
178e5b845dcSCatherine Sullivan 				  u64_stats_fetch_begin(&priv->rx[ring].statss);
179433e274bSKuo Zhao 				tmp_rx_pkts = rx->rpackets;
180433e274bSKuo Zhao 				tmp_rx_bytes = rx->rbytes;
181433e274bSKuo Zhao 				tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail;
182433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail;
183433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt =
184433e274bSKuo Zhao 					rx->rx_desc_err_dropped_pkt;
185e5b845dcSCatherine Sullivan 			} while (u64_stats_fetch_retry(&priv->rx[ring].statss,
186e5b845dcSCatherine Sullivan 						       start));
187433e274bSKuo Zhao 			rx_pkts += tmp_rx_pkts;
188433e274bSKuo Zhao 			rx_bytes += tmp_rx_bytes;
189433e274bSKuo Zhao 			rx_skb_alloc_fail += tmp_rx_skb_alloc_fail;
190433e274bSKuo Zhao 			rx_buf_alloc_fail += tmp_rx_buf_alloc_fail;
191433e274bSKuo Zhao 			rx_desc_err_dropped_pkt += tmp_rx_desc_err_dropped_pkt;
192e5b845dcSCatherine Sullivan 		}
193e5b845dcSCatherine Sullivan 	}
194e5b845dcSCatherine Sullivan 	for (tx_pkts = 0, tx_bytes = 0, ring = 0;
195e5b845dcSCatherine Sullivan 	     ring < priv->tx_cfg.num_queues; ring++) {
196e5b845dcSCatherine Sullivan 		if (priv->tx) {
197e5b845dcSCatherine Sullivan 			do {
1983c13ce74SCatherine Sullivan 				start =
199e5b845dcSCatherine Sullivan 				  u64_stats_fetch_begin(&priv->tx[ring].statss);
200433e274bSKuo Zhao 				tmp_tx_pkts = priv->tx[ring].pkt_done;
201433e274bSKuo Zhao 				tmp_tx_bytes = priv->tx[ring].bytes_done;
202e5b845dcSCatherine Sullivan 			} while (u64_stats_fetch_retry(&priv->tx[ring].statss,
203e5b845dcSCatherine Sullivan 						       start));
204433e274bSKuo Zhao 			tx_pkts += tmp_tx_pkts;
205433e274bSKuo Zhao 			tx_bytes += tmp_tx_bytes;
206e5b845dcSCatherine Sullivan 		}
207e5b845dcSCatherine Sullivan 	}
208e5b845dcSCatherine Sullivan 
209e5b845dcSCatherine Sullivan 	i = 0;
210e5b845dcSCatherine Sullivan 	data[i++] = rx_pkts;
211e5b845dcSCatherine Sullivan 	data[i++] = tx_pkts;
212e5b845dcSCatherine Sullivan 	data[i++] = rx_bytes;
213e5b845dcSCatherine Sullivan 	data[i++] = tx_bytes;
214433e274bSKuo Zhao 	/* total rx dropped packets */
215433e274bSKuo Zhao 	data[i++] = rx_skb_alloc_fail + rx_buf_alloc_fail +
216433e274bSKuo Zhao 		    rx_desc_err_dropped_pkt;
217433e274bSKuo Zhao 	/* Skip tx_dropped */
218433e274bSKuo Zhao 	i++;
219433e274bSKuo Zhao 
220e5b845dcSCatherine Sullivan 	data[i++] = priv->tx_timeo_cnt;
221433e274bSKuo Zhao 	data[i++] = rx_skb_alloc_fail;
222433e274bSKuo Zhao 	data[i++] = rx_buf_alloc_fail;
223433e274bSKuo Zhao 	data[i++] = rx_desc_err_dropped_pkt;
224433e274bSKuo Zhao 	data[i++] = priv->interface_up_cnt;
225433e274bSKuo Zhao 	data[i++] = priv->interface_down_cnt;
226433e274bSKuo Zhao 	data[i++] = priv->reset_cnt;
227433e274bSKuo Zhao 	data[i++] = priv->page_alloc_fail;
228433e274bSKuo Zhao 	data[i++] = priv->dma_mapping_error;
22924aeb56fSKuo Zhao 	data[i++] = priv->stats_report_trigger_cnt;
230e5b845dcSCatherine Sullivan 	i = GVE_MAIN_STATS_LEN;
231e5b845dcSCatherine Sullivan 
2322f523dc3SDavid Awogbemila 	/* For rx cross-reporting stats, start from nic rx stats in report */
2332f523dc3SDavid Awogbemila 	base_stats_idx = GVE_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues +
2342f523dc3SDavid Awogbemila 		GVE_RX_STATS_REPORT_NUM * priv->rx_cfg.num_queues;
2352f523dc3SDavid Awogbemila 	max_stats_idx = NIC_RX_STATS_REPORT_NUM * priv->rx_cfg.num_queues +
2362f523dc3SDavid Awogbemila 		base_stats_idx;
2372f523dc3SDavid Awogbemila 	/* Preprocess the stats report for rx, map queue id to start index */
2382f523dc3SDavid Awogbemila 	skip_nic_stats = false;
2392f523dc3SDavid Awogbemila 	for (stats_idx = base_stats_idx; stats_idx < max_stats_idx;
2402f523dc3SDavid Awogbemila 		stats_idx += NIC_RX_STATS_REPORT_NUM) {
2412f523dc3SDavid Awogbemila 		u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name);
2422f523dc3SDavid Awogbemila 		u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id);
2432f523dc3SDavid Awogbemila 
2442f523dc3SDavid Awogbemila 		if (stat_name == 0) {
2452f523dc3SDavid Awogbemila 			/* no stats written by NIC yet */
2462f523dc3SDavid Awogbemila 			skip_nic_stats = true;
2472f523dc3SDavid Awogbemila 			break;
2482f523dc3SDavid Awogbemila 		}
2492f523dc3SDavid Awogbemila 		rx_qid_to_stats_idx[queue_id] = stats_idx;
2502f523dc3SDavid Awogbemila 	}
251e5b845dcSCatherine Sullivan 	/* walk RX rings */
252e5b845dcSCatherine Sullivan 	if (priv->rx) {
253e5b845dcSCatherine Sullivan 		for (ring = 0; ring < priv->rx_cfg.num_queues; ring++) {
254e5b845dcSCatherine Sullivan 			struct gve_rx_ring *rx = &priv->rx[ring];
255e5b845dcSCatherine Sullivan 
256438b43bdSCatherine Sullivan 			data[i++] = rx->fill_cnt;
257433e274bSKuo Zhao 			data[i++] = rx->cnt;
258433e274bSKuo Zhao 			do {
259433e274bSKuo Zhao 				start =
260433e274bSKuo Zhao 				  u64_stats_fetch_begin(&priv->rx[ring].statss);
261433e274bSKuo Zhao 				tmp_rx_bytes = rx->rbytes;
262433e274bSKuo Zhao 				tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail;
263433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail;
264433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt =
265433e274bSKuo Zhao 					rx->rx_desc_err_dropped_pkt;
266433e274bSKuo Zhao 			} while (u64_stats_fetch_retry(&priv->rx[ring].statss,
267433e274bSKuo Zhao 						       start));
268433e274bSKuo Zhao 			data[i++] = tmp_rx_bytes;
269*37149e93SDavid Awogbemila 			data[i++] = rx->rx_cont_packet_cnt;
270*37149e93SDavid Awogbemila 			data[i++] = rx->rx_frag_flip_cnt;
271*37149e93SDavid Awogbemila 			data[i++] = rx->rx_frag_copy_cnt;
272433e274bSKuo Zhao 			/* rx dropped packets */
273433e274bSKuo Zhao 			data[i++] = tmp_rx_skb_alloc_fail +
274433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail +
275433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt;
276433e274bSKuo Zhao 			data[i++] = rx->rx_copybreak_pkt;
277433e274bSKuo Zhao 			data[i++] = rx->rx_copied_pkt;
2782f523dc3SDavid Awogbemila 			/* stats from NIC */
2792f523dc3SDavid Awogbemila 			if (skip_nic_stats) {
2802f523dc3SDavid Awogbemila 				/* skip NIC rx stats */
2812f523dc3SDavid Awogbemila 				i += NIC_RX_STATS_REPORT_NUM;
2822f523dc3SDavid Awogbemila 				continue;
2832f523dc3SDavid Awogbemila 			}
2842f523dc3SDavid Awogbemila 			for (j = 0; j < NIC_RX_STATS_REPORT_NUM; j++) {
2852f523dc3SDavid Awogbemila 				u64 value =
2862f523dc3SDavid Awogbemila 				be64_to_cpu(report_stats[rx_qid_to_stats_idx[ring] + j].value);
2872f523dc3SDavid Awogbemila 
2882f523dc3SDavid Awogbemila 				data[i++] = value;
2892f523dc3SDavid Awogbemila 			}
290e5b845dcSCatherine Sullivan 		}
291e5b845dcSCatherine Sullivan 	} else {
292e5b845dcSCatherine Sullivan 		i += priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS;
293e5b845dcSCatherine Sullivan 	}
2942f523dc3SDavid Awogbemila 
2952f523dc3SDavid Awogbemila 	/* For tx cross-reporting stats, start from nic tx stats in report */
2962f523dc3SDavid Awogbemila 	base_stats_idx = max_stats_idx;
2972f523dc3SDavid Awogbemila 	max_stats_idx = NIC_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues +
2982f523dc3SDavid Awogbemila 		max_stats_idx;
2992f523dc3SDavid Awogbemila 	/* Preprocess the stats report for tx, map queue id to start index */
3002f523dc3SDavid Awogbemila 	skip_nic_stats = false;
3012f523dc3SDavid Awogbemila 	for (stats_idx = base_stats_idx; stats_idx < max_stats_idx;
3022f523dc3SDavid Awogbemila 		stats_idx += NIC_TX_STATS_REPORT_NUM) {
3032f523dc3SDavid Awogbemila 		u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name);
3042f523dc3SDavid Awogbemila 		u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id);
3052f523dc3SDavid Awogbemila 
3062f523dc3SDavid Awogbemila 		if (stat_name == 0) {
3072f523dc3SDavid Awogbemila 			/* no stats written by NIC yet */
3082f523dc3SDavid Awogbemila 			skip_nic_stats = true;
3092f523dc3SDavid Awogbemila 			break;
3102f523dc3SDavid Awogbemila 		}
3112f523dc3SDavid Awogbemila 		tx_qid_to_stats_idx[queue_id] = stats_idx;
3122f523dc3SDavid Awogbemila 	}
313e5b845dcSCatherine Sullivan 	/* walk TX rings */
314e5b845dcSCatherine Sullivan 	if (priv->tx) {
315e5b845dcSCatherine Sullivan 		for (ring = 0; ring < priv->tx_cfg.num_queues; ring++) {
316e5b845dcSCatherine Sullivan 			struct gve_tx_ring *tx = &priv->tx[ring];
317e5b845dcSCatherine Sullivan 
3185e8c5adfSBailey Forrest 			if (gve_is_gqi(priv)) {
319e5b845dcSCatherine Sullivan 				data[i++] = tx->req;
320e5b845dcSCatherine Sullivan 				data[i++] = tx->done;
3215e8c5adfSBailey Forrest 			} else {
3225e8c5adfSBailey Forrest 				/* DQO doesn't currently support
3235e8c5adfSBailey Forrest 				 * posted/completed descriptor counts;
3245e8c5adfSBailey Forrest 				 */
3255e8c5adfSBailey Forrest 				data[i++] = 0;
3265e8c5adfSBailey Forrest 				data[i++] = 0;
3275e8c5adfSBailey Forrest 			}
328433e274bSKuo Zhao 			do {
329433e274bSKuo Zhao 				start =
330433e274bSKuo Zhao 				  u64_stats_fetch_begin(&priv->tx[ring].statss);
331433e274bSKuo Zhao 				tmp_tx_bytes = tx->bytes_done;
332433e274bSKuo Zhao 			} while (u64_stats_fetch_retry(&priv->tx[ring].statss,
333433e274bSKuo Zhao 						       start));
334433e274bSKuo Zhao 			data[i++] = tmp_tx_bytes;
335e5b845dcSCatherine Sullivan 			data[i++] = tx->wake_queue;
336e5b845dcSCatherine Sullivan 			data[i++] = tx->stop_queue;
33761d72c7eSTao Liu 			data[i++] = gve_tx_load_event_counter(priv, tx);
3386f007c64SCatherine Sullivan 			data[i++] = tx->dma_mapping_error;
3392f523dc3SDavid Awogbemila 			/* stats from NIC */
3402f523dc3SDavid Awogbemila 			if (skip_nic_stats) {
3412f523dc3SDavid Awogbemila 				/* skip NIC tx stats */
3422f523dc3SDavid Awogbemila 				i += NIC_TX_STATS_REPORT_NUM;
3432f523dc3SDavid Awogbemila 				continue;
3442f523dc3SDavid Awogbemila 			}
3452f523dc3SDavid Awogbemila 			for (j = 0; j < NIC_TX_STATS_REPORT_NUM; j++) {
3462f523dc3SDavid Awogbemila 				u64 value =
3472f523dc3SDavid Awogbemila 				be64_to_cpu(report_stats[tx_qid_to_stats_idx[ring] + j].value);
3482f523dc3SDavid Awogbemila 				data[i++] = value;
3492f523dc3SDavid Awogbemila 			}
350e5b845dcSCatherine Sullivan 		}
351e5b845dcSCatherine Sullivan 	} else {
352e5b845dcSCatherine Sullivan 		i += priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS;
353e5b845dcSCatherine Sullivan 	}
3542f523dc3SDavid Awogbemila 
3552f523dc3SDavid Awogbemila 	kfree(rx_qid_to_stats_idx);
3562f523dc3SDavid Awogbemila 	kfree(tx_qid_to_stats_idx);
357433e274bSKuo Zhao 	/* AQ Stats */
358433e274bSKuo Zhao 	data[i++] = priv->adminq_prod_cnt;
359433e274bSKuo Zhao 	data[i++] = priv->adminq_cmd_fail;
360433e274bSKuo Zhao 	data[i++] = priv->adminq_timeouts;
361433e274bSKuo Zhao 	data[i++] = priv->adminq_describe_device_cnt;
362433e274bSKuo Zhao 	data[i++] = priv->adminq_cfg_device_resources_cnt;
363433e274bSKuo Zhao 	data[i++] = priv->adminq_register_page_list_cnt;
364433e274bSKuo Zhao 	data[i++] = priv->adminq_unregister_page_list_cnt;
365433e274bSKuo Zhao 	data[i++] = priv->adminq_create_tx_queue_cnt;
366433e274bSKuo Zhao 	data[i++] = priv->adminq_create_rx_queue_cnt;
367433e274bSKuo Zhao 	data[i++] = priv->adminq_destroy_tx_queue_cnt;
368433e274bSKuo Zhao 	data[i++] = priv->adminq_destroy_rx_queue_cnt;
369433e274bSKuo Zhao 	data[i++] = priv->adminq_dcfg_device_resources_cnt;
370433e274bSKuo Zhao 	data[i++] = priv->adminq_set_driver_parameter_cnt;
37124aeb56fSKuo Zhao 	data[i++] = priv->adminq_report_stats_cnt;
3727e074d5aSDavid Awogbemila 	data[i++] = priv->adminq_report_link_speed_cnt;
373e5b845dcSCatherine Sullivan }
374e5b845dcSCatherine Sullivan 
375e5b845dcSCatherine Sullivan static void gve_get_channels(struct net_device *netdev,
376e5b845dcSCatherine Sullivan 			     struct ethtool_channels *cmd)
377e5b845dcSCatherine Sullivan {
378e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
379e5b845dcSCatherine Sullivan 
380e5b845dcSCatherine Sullivan 	cmd->max_rx = priv->rx_cfg.max_queues;
381e5b845dcSCatherine Sullivan 	cmd->max_tx = priv->tx_cfg.max_queues;
382e5b845dcSCatherine Sullivan 	cmd->max_other = 0;
383e5b845dcSCatherine Sullivan 	cmd->max_combined = 0;
384e5b845dcSCatherine Sullivan 	cmd->rx_count = priv->rx_cfg.num_queues;
385e5b845dcSCatherine Sullivan 	cmd->tx_count = priv->tx_cfg.num_queues;
386e5b845dcSCatherine Sullivan 	cmd->other_count = 0;
387e5b845dcSCatherine Sullivan 	cmd->combined_count = 0;
388e5b845dcSCatherine Sullivan }
389e5b845dcSCatherine Sullivan 
390e5b845dcSCatherine Sullivan static int gve_set_channels(struct net_device *netdev,
391e5b845dcSCatherine Sullivan 			    struct ethtool_channels *cmd)
392e5b845dcSCatherine Sullivan {
393e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
394e5b845dcSCatherine Sullivan 	struct gve_queue_config new_tx_cfg = priv->tx_cfg;
395e5b845dcSCatherine Sullivan 	struct gve_queue_config new_rx_cfg = priv->rx_cfg;
396e5b845dcSCatherine Sullivan 	struct ethtool_channels old_settings;
397e5b845dcSCatherine Sullivan 	int new_tx = cmd->tx_count;
398e5b845dcSCatherine Sullivan 	int new_rx = cmd->rx_count;
399e5b845dcSCatherine Sullivan 
400e5b845dcSCatherine Sullivan 	gve_get_channels(netdev, &old_settings);
401e5b845dcSCatherine Sullivan 
402f67435b5SDaode Huang 	/* Changing combined is not allowed */
403e5b845dcSCatherine Sullivan 	if (cmd->combined_count != old_settings.combined_count)
404e5b845dcSCatherine Sullivan 		return -EINVAL;
405e5b845dcSCatherine Sullivan 
406e5b845dcSCatherine Sullivan 	if (!new_rx || !new_tx)
407e5b845dcSCatherine Sullivan 		return -EINVAL;
408e5b845dcSCatherine Sullivan 
409e5b845dcSCatherine Sullivan 	if (!netif_carrier_ok(netdev)) {
410e5b845dcSCatherine Sullivan 		priv->tx_cfg.num_queues = new_tx;
411e5b845dcSCatherine Sullivan 		priv->rx_cfg.num_queues = new_rx;
412e5b845dcSCatherine Sullivan 		return 0;
413e5b845dcSCatherine Sullivan 	}
414e5b845dcSCatherine Sullivan 
415e5b845dcSCatherine Sullivan 	new_tx_cfg.num_queues = new_tx;
416e5b845dcSCatherine Sullivan 	new_rx_cfg.num_queues = new_rx;
417e5b845dcSCatherine Sullivan 
418e5b845dcSCatherine Sullivan 	return gve_adjust_queues(priv, new_rx_cfg, new_tx_cfg);
419e5b845dcSCatherine Sullivan }
420e5b845dcSCatherine Sullivan 
421e5b845dcSCatherine Sullivan static void gve_get_ringparam(struct net_device *netdev,
422e5b845dcSCatherine Sullivan 			      struct ethtool_ringparam *cmd)
423e5b845dcSCatherine Sullivan {
424e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
425e5b845dcSCatherine Sullivan 
426e5b845dcSCatherine Sullivan 	cmd->rx_max_pending = priv->rx_desc_cnt;
427e5b845dcSCatherine Sullivan 	cmd->tx_max_pending = priv->tx_desc_cnt;
428e5b845dcSCatherine Sullivan 	cmd->rx_pending = priv->rx_desc_cnt;
429e5b845dcSCatherine Sullivan 	cmd->tx_pending = priv->tx_desc_cnt;
430e5b845dcSCatherine Sullivan }
431e5b845dcSCatherine Sullivan 
432e5b845dcSCatherine Sullivan static int gve_user_reset(struct net_device *netdev, u32 *flags)
433e5b845dcSCatherine Sullivan {
434e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
435e5b845dcSCatherine Sullivan 
436e5b845dcSCatherine Sullivan 	if (*flags == ETH_RESET_ALL) {
437e5b845dcSCatherine Sullivan 		*flags = 0;
438e5b845dcSCatherine Sullivan 		return gve_reset(priv, true);
439e5b845dcSCatherine Sullivan 	}
440e5b845dcSCatherine Sullivan 
441e5b845dcSCatherine Sullivan 	return -EOPNOTSUPP;
442e5b845dcSCatherine Sullivan }
443e5b845dcSCatherine Sullivan 
444d5f7543cSKuo Zhao static int gve_get_tunable(struct net_device *netdev,
445d5f7543cSKuo Zhao 			   const struct ethtool_tunable *etuna, void *value)
446d5f7543cSKuo Zhao {
447d5f7543cSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
448d5f7543cSKuo Zhao 
449d5f7543cSKuo Zhao 	switch (etuna->id) {
450d5f7543cSKuo Zhao 	case ETHTOOL_RX_COPYBREAK:
451d5f7543cSKuo Zhao 		*(u32 *)value = priv->rx_copybreak;
452d5f7543cSKuo Zhao 		return 0;
453d5f7543cSKuo Zhao 	default:
454d5f7543cSKuo Zhao 		return -EOPNOTSUPP;
455d5f7543cSKuo Zhao 	}
456d5f7543cSKuo Zhao }
457d5f7543cSKuo Zhao 
458d5f7543cSKuo Zhao static int gve_set_tunable(struct net_device *netdev,
459433e274bSKuo Zhao 			   const struct ethtool_tunable *etuna,
460433e274bSKuo Zhao 			   const void *value)
461d5f7543cSKuo Zhao {
462d5f7543cSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
463d5f7543cSKuo Zhao 	u32 len;
464d5f7543cSKuo Zhao 
465d5f7543cSKuo Zhao 	switch (etuna->id) {
466d5f7543cSKuo Zhao 	case ETHTOOL_RX_COPYBREAK:
4671f6228e4SBailey Forrest 	{
4681f6228e4SBailey Forrest 		u32 max_copybreak = gve_is_gqi(priv) ?
4691f6228e4SBailey Forrest 			(PAGE_SIZE / 2) : priv->data_buffer_size_dqo;
4701f6228e4SBailey Forrest 
471d5f7543cSKuo Zhao 		len = *(u32 *)value;
4721f6228e4SBailey Forrest 		if (len > max_copybreak)
473d5f7543cSKuo Zhao 			return -EINVAL;
474d5f7543cSKuo Zhao 		priv->rx_copybreak = len;
475d5f7543cSKuo Zhao 		return 0;
4761f6228e4SBailey Forrest 	}
477d5f7543cSKuo Zhao 	default:
478d5f7543cSKuo Zhao 		return -EOPNOTSUPP;
479d5f7543cSKuo Zhao 	}
480d5f7543cSKuo Zhao }
481d5f7543cSKuo Zhao 
48224aeb56fSKuo Zhao static u32 gve_get_priv_flags(struct net_device *netdev)
48324aeb56fSKuo Zhao {
48424aeb56fSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
48524aeb56fSKuo Zhao 	u32 ret_flags = 0;
48624aeb56fSKuo Zhao 
48724aeb56fSKuo Zhao 	/* Only 1 flag exists currently: report-stats (BIT(O)), so set that flag. */
48824aeb56fSKuo Zhao 	if (priv->ethtool_flags & BIT(0))
48924aeb56fSKuo Zhao 		ret_flags |= BIT(0);
49024aeb56fSKuo Zhao 	return ret_flags;
49124aeb56fSKuo Zhao }
49224aeb56fSKuo Zhao 
49324aeb56fSKuo Zhao static int gve_set_priv_flags(struct net_device *netdev, u32 flags)
49424aeb56fSKuo Zhao {
49524aeb56fSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
49624aeb56fSKuo Zhao 	u64 ori_flags, new_flags;
49724aeb56fSKuo Zhao 
49824aeb56fSKuo Zhao 	ori_flags = READ_ONCE(priv->ethtool_flags);
49924aeb56fSKuo Zhao 	new_flags = ori_flags;
50024aeb56fSKuo Zhao 
50124aeb56fSKuo Zhao 	/* Only one priv flag exists: report-stats (BIT(0))*/
50224aeb56fSKuo Zhao 	if (flags & BIT(0))
50324aeb56fSKuo Zhao 		new_flags |= BIT(0);
50424aeb56fSKuo Zhao 	else
50524aeb56fSKuo Zhao 		new_flags &= ~(BIT(0));
50624aeb56fSKuo Zhao 	priv->ethtool_flags = new_flags;
50724aeb56fSKuo Zhao 	/* start report-stats timer when user turns report stats on. */
50824aeb56fSKuo Zhao 	if (flags & BIT(0)) {
50924aeb56fSKuo Zhao 		mod_timer(&priv->stats_report_timer,
51024aeb56fSKuo Zhao 			  round_jiffies(jiffies +
51124aeb56fSKuo Zhao 					msecs_to_jiffies(priv->stats_report_timer_period)));
51224aeb56fSKuo Zhao 	}
51324aeb56fSKuo Zhao 	/* Zero off gve stats when report-stats turned off and */
51424aeb56fSKuo Zhao 	/* delete report stats timer. */
51524aeb56fSKuo Zhao 	if (!(flags & BIT(0)) && (ori_flags & BIT(0))) {
51624aeb56fSKuo Zhao 		int tx_stats_num = GVE_TX_STATS_REPORT_NUM *
51724aeb56fSKuo Zhao 			priv->tx_cfg.num_queues;
51824aeb56fSKuo Zhao 		int rx_stats_num = GVE_RX_STATS_REPORT_NUM *
51924aeb56fSKuo Zhao 			priv->rx_cfg.num_queues;
52024aeb56fSKuo Zhao 
52124aeb56fSKuo Zhao 		memset(priv->stats_report->stats, 0, (tx_stats_num + rx_stats_num) *
52224aeb56fSKuo Zhao 				   sizeof(struct stats));
52324aeb56fSKuo Zhao 		del_timer_sync(&priv->stats_report_timer);
52424aeb56fSKuo Zhao 	}
52524aeb56fSKuo Zhao 	return 0;
52624aeb56fSKuo Zhao }
52724aeb56fSKuo Zhao 
5287e074d5aSDavid Awogbemila static int gve_get_link_ksettings(struct net_device *netdev,
5297e074d5aSDavid Awogbemila 				  struct ethtool_link_ksettings *cmd)
5307e074d5aSDavid Awogbemila {
5317e074d5aSDavid Awogbemila 	struct gve_priv *priv = netdev_priv(netdev);
5327e074d5aSDavid Awogbemila 	int err = gve_adminq_report_link_speed(priv);
5337e074d5aSDavid Awogbemila 
5347e074d5aSDavid Awogbemila 	cmd->base.speed = priv->link_speed;
5357e074d5aSDavid Awogbemila 	return err;
5367e074d5aSDavid Awogbemila }
5377e074d5aSDavid Awogbemila 
538e5b845dcSCatherine Sullivan const struct ethtool_ops gve_ethtool_ops = {
539e5b845dcSCatherine Sullivan 	.get_drvinfo = gve_get_drvinfo,
540e5b845dcSCatherine Sullivan 	.get_strings = gve_get_strings,
541e5b845dcSCatherine Sullivan 	.get_sset_count = gve_get_sset_count,
542e5b845dcSCatherine Sullivan 	.get_ethtool_stats = gve_get_ethtool_stats,
543e5b845dcSCatherine Sullivan 	.set_msglevel = gve_set_msglevel,
544e5b845dcSCatherine Sullivan 	.get_msglevel = gve_get_msglevel,
545e5b845dcSCatherine Sullivan 	.set_channels = gve_set_channels,
546e5b845dcSCatherine Sullivan 	.get_channels = gve_get_channels,
547e5b845dcSCatherine Sullivan 	.get_link = ethtool_op_get_link,
548e5b845dcSCatherine Sullivan 	.get_ringparam = gve_get_ringparam,
549e5b845dcSCatherine Sullivan 	.reset = gve_user_reset,
550d5f7543cSKuo Zhao 	.get_tunable = gve_get_tunable,
551d5f7543cSKuo Zhao 	.set_tunable = gve_set_tunable,
55224aeb56fSKuo Zhao 	.get_priv_flags = gve_get_priv_flags,
55324aeb56fSKuo Zhao 	.set_priv_flags = gve_set_priv_flags,
5547e074d5aSDavid Awogbemila 	.get_link_ksettings = gve_get_link_ksettings
555e5b845dcSCatherine Sullivan };
556