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] = {
45*2c919835SJordan Kim 	"rx_posted_desc[%u]", "rx_completed_desc[%u]", "rx_consumed_desc[%u]", "rx_bytes[%u]",
4637149e93SDavid 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] = {
53*2c919835SJordan Kim 	"tx_posted_desc[%u]", "tx_completed_desc[%u]", "tx_consumed_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 {
142*2c919835SJordan Kim 	u64 tmp_rx_pkts, tmp_rx_bytes, tmp_rx_skb_alloc_fail,
143*2c919835SJordan Kim 		tmp_rx_buf_alloc_fail, tmp_rx_desc_err_dropped_pkt,
144*2c919835SJordan Kim 		tmp_tx_pkts, tmp_tx_bytes;
145433e274bSKuo Zhao 	u64 rx_buf_alloc_fail, rx_desc_err_dropped_pkt, rx_pkts,
146*2c919835SJordan Kim 		rx_skb_alloc_fail, rx_bytes, tx_pkts, tx_bytes, tx_dropped;
1472f523dc3SDavid Awogbemila 	int stats_idx, base_stats_idx, max_stats_idx;
1482f523dc3SDavid Awogbemila 	struct stats *report_stats;
1492f523dc3SDavid Awogbemila 	int *rx_qid_to_stats_idx;
1502f523dc3SDavid Awogbemila 	int *tx_qid_to_stats_idx;
151433e274bSKuo Zhao 	struct gve_priv *priv;
1522f523dc3SDavid Awogbemila 	bool skip_nic_stats;
153e5b845dcSCatherine Sullivan 	unsigned int start;
154e5b845dcSCatherine Sullivan 	int ring;
1552f523dc3SDavid Awogbemila 	int i, j;
156e5b845dcSCatherine Sullivan 
157e5b845dcSCatherine Sullivan 	ASSERT_RTNL();
158e5b845dcSCatherine Sullivan 
159433e274bSKuo Zhao 	priv = netdev_priv(netdev);
1602f523dc3SDavid Awogbemila 	report_stats = priv->stats_report->stats;
1612f523dc3SDavid Awogbemila 	rx_qid_to_stats_idx = kmalloc_array(priv->rx_cfg.num_queues,
1622f523dc3SDavid Awogbemila 					    sizeof(int), GFP_KERNEL);
1632f523dc3SDavid Awogbemila 	if (!rx_qid_to_stats_idx)
1642f523dc3SDavid Awogbemila 		return;
1652f523dc3SDavid Awogbemila 	tx_qid_to_stats_idx = kmalloc_array(priv->tx_cfg.num_queues,
1662f523dc3SDavid Awogbemila 					    sizeof(int), GFP_KERNEL);
1672f523dc3SDavid Awogbemila 	if (!tx_qid_to_stats_idx) {
1682f523dc3SDavid Awogbemila 		kfree(rx_qid_to_stats_idx);
1692f523dc3SDavid Awogbemila 		return;
1702f523dc3SDavid Awogbemila 	}
171433e274bSKuo Zhao 	for (rx_pkts = 0, rx_bytes = 0, rx_skb_alloc_fail = 0,
172433e274bSKuo Zhao 	     rx_buf_alloc_fail = 0, rx_desc_err_dropped_pkt = 0, ring = 0;
173e5b845dcSCatherine Sullivan 	     ring < priv->rx_cfg.num_queues; ring++) {
174e5b845dcSCatherine Sullivan 		if (priv->rx) {
175e5b845dcSCatherine Sullivan 			do {
176433e274bSKuo Zhao 				struct gve_rx_ring *rx = &priv->rx[ring];
177433e274bSKuo Zhao 
1783c13ce74SCatherine Sullivan 				start =
179e5b845dcSCatherine Sullivan 				  u64_stats_fetch_begin(&priv->rx[ring].statss);
180433e274bSKuo Zhao 				tmp_rx_pkts = rx->rpackets;
181433e274bSKuo Zhao 				tmp_rx_bytes = rx->rbytes;
182433e274bSKuo Zhao 				tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail;
183433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail;
184433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt =
185433e274bSKuo Zhao 					rx->rx_desc_err_dropped_pkt;
186e5b845dcSCatherine Sullivan 			} while (u64_stats_fetch_retry(&priv->rx[ring].statss,
187e5b845dcSCatherine Sullivan 						       start));
188433e274bSKuo Zhao 			rx_pkts += tmp_rx_pkts;
189433e274bSKuo Zhao 			rx_bytes += tmp_rx_bytes;
190433e274bSKuo Zhao 			rx_skb_alloc_fail += tmp_rx_skb_alloc_fail;
191433e274bSKuo Zhao 			rx_buf_alloc_fail += tmp_rx_buf_alloc_fail;
192433e274bSKuo Zhao 			rx_desc_err_dropped_pkt += tmp_rx_desc_err_dropped_pkt;
193e5b845dcSCatherine Sullivan 		}
194e5b845dcSCatherine Sullivan 	}
195*2c919835SJordan Kim 	for (tx_pkts = 0, tx_bytes = 0, tx_dropped = 0, ring = 0;
196e5b845dcSCatherine Sullivan 	     ring < priv->tx_cfg.num_queues; ring++) {
197e5b845dcSCatherine Sullivan 		if (priv->tx) {
198e5b845dcSCatherine Sullivan 			do {
1993c13ce74SCatherine Sullivan 				start =
200e5b845dcSCatherine Sullivan 				  u64_stats_fetch_begin(&priv->tx[ring].statss);
201433e274bSKuo Zhao 				tmp_tx_pkts = priv->tx[ring].pkt_done;
202433e274bSKuo Zhao 				tmp_tx_bytes = priv->tx[ring].bytes_done;
203e5b845dcSCatherine Sullivan 			} while (u64_stats_fetch_retry(&priv->tx[ring].statss,
204e5b845dcSCatherine Sullivan 						       start));
205433e274bSKuo Zhao 			tx_pkts += tmp_tx_pkts;
206433e274bSKuo Zhao 			tx_bytes += tmp_tx_bytes;
207*2c919835SJordan Kim 			tx_dropped += priv->tx[ring].dropped_pkt;
208e5b845dcSCatherine Sullivan 		}
209e5b845dcSCatherine Sullivan 	}
210e5b845dcSCatherine Sullivan 
211e5b845dcSCatherine Sullivan 	i = 0;
212e5b845dcSCatherine Sullivan 	data[i++] = rx_pkts;
213e5b845dcSCatherine Sullivan 	data[i++] = tx_pkts;
214e5b845dcSCatherine Sullivan 	data[i++] = rx_bytes;
215e5b845dcSCatherine Sullivan 	data[i++] = tx_bytes;
216433e274bSKuo Zhao 	/* total rx dropped packets */
217433e274bSKuo Zhao 	data[i++] = rx_skb_alloc_fail + rx_buf_alloc_fail +
218433e274bSKuo Zhao 		    rx_desc_err_dropped_pkt;
219*2c919835SJordan Kim 	data[i++] = tx_dropped;
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;
258*2c919835SJordan Kim 			data[i++] = rx->fill_cnt - rx->cnt;
259433e274bSKuo Zhao 			do {
260433e274bSKuo Zhao 				start =
261433e274bSKuo Zhao 				  u64_stats_fetch_begin(&priv->rx[ring].statss);
262433e274bSKuo Zhao 				tmp_rx_bytes = rx->rbytes;
263433e274bSKuo Zhao 				tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail;
264433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail;
265433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt =
266433e274bSKuo Zhao 					rx->rx_desc_err_dropped_pkt;
267433e274bSKuo Zhao 			} while (u64_stats_fetch_retry(&priv->rx[ring].statss,
268433e274bSKuo Zhao 						       start));
269433e274bSKuo Zhao 			data[i++] = tmp_rx_bytes;
27037149e93SDavid Awogbemila 			data[i++] = rx->rx_cont_packet_cnt;
27137149e93SDavid Awogbemila 			data[i++] = rx->rx_frag_flip_cnt;
27237149e93SDavid Awogbemila 			data[i++] = rx->rx_frag_copy_cnt;
273433e274bSKuo Zhao 			/* rx dropped packets */
274433e274bSKuo Zhao 			data[i++] = tmp_rx_skb_alloc_fail +
275433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail +
276433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt;
277433e274bSKuo Zhao 			data[i++] = rx->rx_copybreak_pkt;
278433e274bSKuo Zhao 			data[i++] = rx->rx_copied_pkt;
2792f523dc3SDavid Awogbemila 			/* stats from NIC */
2802f523dc3SDavid Awogbemila 			if (skip_nic_stats) {
2812f523dc3SDavid Awogbemila 				/* skip NIC rx stats */
2822f523dc3SDavid Awogbemila 				i += NIC_RX_STATS_REPORT_NUM;
2832f523dc3SDavid Awogbemila 				continue;
2842f523dc3SDavid Awogbemila 			}
2852f523dc3SDavid Awogbemila 			for (j = 0; j < NIC_RX_STATS_REPORT_NUM; j++) {
2862f523dc3SDavid Awogbemila 				u64 value =
2872f523dc3SDavid Awogbemila 				be64_to_cpu(report_stats[rx_qid_to_stats_idx[ring] + j].value);
2882f523dc3SDavid Awogbemila 
2892f523dc3SDavid Awogbemila 				data[i++] = value;
2902f523dc3SDavid Awogbemila 			}
291e5b845dcSCatherine Sullivan 		}
292e5b845dcSCatherine Sullivan 	} else {
293e5b845dcSCatherine Sullivan 		i += priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS;
294e5b845dcSCatherine Sullivan 	}
2952f523dc3SDavid Awogbemila 
2962f523dc3SDavid Awogbemila 	/* For tx cross-reporting stats, start from nic tx stats in report */
2972f523dc3SDavid Awogbemila 	base_stats_idx = max_stats_idx;
2982f523dc3SDavid Awogbemila 	max_stats_idx = NIC_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues +
2992f523dc3SDavid Awogbemila 		max_stats_idx;
3002f523dc3SDavid Awogbemila 	/* Preprocess the stats report for tx, map queue id to start index */
3012f523dc3SDavid Awogbemila 	skip_nic_stats = false;
3022f523dc3SDavid Awogbemila 	for (stats_idx = base_stats_idx; stats_idx < max_stats_idx;
3032f523dc3SDavid Awogbemila 		stats_idx += NIC_TX_STATS_REPORT_NUM) {
3042f523dc3SDavid Awogbemila 		u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name);
3052f523dc3SDavid Awogbemila 		u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id);
3062f523dc3SDavid Awogbemila 
3072f523dc3SDavid Awogbemila 		if (stat_name == 0) {
3082f523dc3SDavid Awogbemila 			/* no stats written by NIC yet */
3092f523dc3SDavid Awogbemila 			skip_nic_stats = true;
3102f523dc3SDavid Awogbemila 			break;
3112f523dc3SDavid Awogbemila 		}
3122f523dc3SDavid Awogbemila 		tx_qid_to_stats_idx[queue_id] = stats_idx;
3132f523dc3SDavid Awogbemila 	}
314e5b845dcSCatherine Sullivan 	/* walk TX rings */
315e5b845dcSCatherine Sullivan 	if (priv->tx) {
316e5b845dcSCatherine Sullivan 		for (ring = 0; ring < priv->tx_cfg.num_queues; ring++) {
317e5b845dcSCatherine Sullivan 			struct gve_tx_ring *tx = &priv->tx[ring];
318e5b845dcSCatherine Sullivan 
3195e8c5adfSBailey Forrest 			if (gve_is_gqi(priv)) {
320e5b845dcSCatherine Sullivan 				data[i++] = tx->req;
321e5b845dcSCatherine Sullivan 				data[i++] = tx->done;
322*2c919835SJordan Kim 				data[i++] = tx->req - tx->done;
3235e8c5adfSBailey Forrest 			} else {
3245e8c5adfSBailey Forrest 				/* DQO doesn't currently support
3255e8c5adfSBailey Forrest 				 * posted/completed descriptor counts;
3265e8c5adfSBailey Forrest 				 */
3275e8c5adfSBailey Forrest 				data[i++] = 0;
3285e8c5adfSBailey Forrest 				data[i++] = 0;
329*2c919835SJordan Kim 				data[i++] = tx->dqo_tx.tail - tx->dqo_tx.head;
3305e8c5adfSBailey Forrest 			}
331433e274bSKuo Zhao 			do {
332433e274bSKuo Zhao 				start =
333433e274bSKuo Zhao 				  u64_stats_fetch_begin(&priv->tx[ring].statss);
334433e274bSKuo Zhao 				tmp_tx_bytes = tx->bytes_done;
335433e274bSKuo Zhao 			} while (u64_stats_fetch_retry(&priv->tx[ring].statss,
336433e274bSKuo Zhao 						       start));
337433e274bSKuo Zhao 			data[i++] = tmp_tx_bytes;
338e5b845dcSCatherine Sullivan 			data[i++] = tx->wake_queue;
339e5b845dcSCatherine Sullivan 			data[i++] = tx->stop_queue;
34061d72c7eSTao Liu 			data[i++] = gve_tx_load_event_counter(priv, tx);
3416f007c64SCatherine Sullivan 			data[i++] = tx->dma_mapping_error;
3422f523dc3SDavid Awogbemila 			/* stats from NIC */
3432f523dc3SDavid Awogbemila 			if (skip_nic_stats) {
3442f523dc3SDavid Awogbemila 				/* skip NIC tx stats */
3452f523dc3SDavid Awogbemila 				i += NIC_TX_STATS_REPORT_NUM;
3462f523dc3SDavid Awogbemila 				continue;
3472f523dc3SDavid Awogbemila 			}
3482f523dc3SDavid Awogbemila 			for (j = 0; j < NIC_TX_STATS_REPORT_NUM; j++) {
3492f523dc3SDavid Awogbemila 				u64 value =
3502f523dc3SDavid Awogbemila 				be64_to_cpu(report_stats[tx_qid_to_stats_idx[ring] + j].value);
3512f523dc3SDavid Awogbemila 				data[i++] = value;
3522f523dc3SDavid Awogbemila 			}
353e5b845dcSCatherine Sullivan 		}
354e5b845dcSCatherine Sullivan 	} else {
355e5b845dcSCatherine Sullivan 		i += priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS;
356e5b845dcSCatherine Sullivan 	}
3572f523dc3SDavid Awogbemila 
3582f523dc3SDavid Awogbemila 	kfree(rx_qid_to_stats_idx);
3592f523dc3SDavid Awogbemila 	kfree(tx_qid_to_stats_idx);
360433e274bSKuo Zhao 	/* AQ Stats */
361433e274bSKuo Zhao 	data[i++] = priv->adminq_prod_cnt;
362433e274bSKuo Zhao 	data[i++] = priv->adminq_cmd_fail;
363433e274bSKuo Zhao 	data[i++] = priv->adminq_timeouts;
364433e274bSKuo Zhao 	data[i++] = priv->adminq_describe_device_cnt;
365433e274bSKuo Zhao 	data[i++] = priv->adminq_cfg_device_resources_cnt;
366433e274bSKuo Zhao 	data[i++] = priv->adminq_register_page_list_cnt;
367433e274bSKuo Zhao 	data[i++] = priv->adminq_unregister_page_list_cnt;
368433e274bSKuo Zhao 	data[i++] = priv->adminq_create_tx_queue_cnt;
369433e274bSKuo Zhao 	data[i++] = priv->adminq_create_rx_queue_cnt;
370433e274bSKuo Zhao 	data[i++] = priv->adminq_destroy_tx_queue_cnt;
371433e274bSKuo Zhao 	data[i++] = priv->adminq_destroy_rx_queue_cnt;
372433e274bSKuo Zhao 	data[i++] = priv->adminq_dcfg_device_resources_cnt;
373433e274bSKuo Zhao 	data[i++] = priv->adminq_set_driver_parameter_cnt;
37424aeb56fSKuo Zhao 	data[i++] = priv->adminq_report_stats_cnt;
3757e074d5aSDavid Awogbemila 	data[i++] = priv->adminq_report_link_speed_cnt;
376e5b845dcSCatherine Sullivan }
377e5b845dcSCatherine Sullivan 
378e5b845dcSCatherine Sullivan static void gve_get_channels(struct net_device *netdev,
379e5b845dcSCatherine Sullivan 			     struct ethtool_channels *cmd)
380e5b845dcSCatherine Sullivan {
381e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
382e5b845dcSCatherine Sullivan 
383e5b845dcSCatherine Sullivan 	cmd->max_rx = priv->rx_cfg.max_queues;
384e5b845dcSCatherine Sullivan 	cmd->max_tx = priv->tx_cfg.max_queues;
385e5b845dcSCatherine Sullivan 	cmd->max_other = 0;
386e5b845dcSCatherine Sullivan 	cmd->max_combined = 0;
387e5b845dcSCatherine Sullivan 	cmd->rx_count = priv->rx_cfg.num_queues;
388e5b845dcSCatherine Sullivan 	cmd->tx_count = priv->tx_cfg.num_queues;
389e5b845dcSCatherine Sullivan 	cmd->other_count = 0;
390e5b845dcSCatherine Sullivan 	cmd->combined_count = 0;
391e5b845dcSCatherine Sullivan }
392e5b845dcSCatherine Sullivan 
393e5b845dcSCatherine Sullivan static int gve_set_channels(struct net_device *netdev,
394e5b845dcSCatherine Sullivan 			    struct ethtool_channels *cmd)
395e5b845dcSCatherine Sullivan {
396e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
397e5b845dcSCatherine Sullivan 	struct gve_queue_config new_tx_cfg = priv->tx_cfg;
398e5b845dcSCatherine Sullivan 	struct gve_queue_config new_rx_cfg = priv->rx_cfg;
399e5b845dcSCatherine Sullivan 	struct ethtool_channels old_settings;
400e5b845dcSCatherine Sullivan 	int new_tx = cmd->tx_count;
401e5b845dcSCatherine Sullivan 	int new_rx = cmd->rx_count;
402e5b845dcSCatherine Sullivan 
403e5b845dcSCatherine Sullivan 	gve_get_channels(netdev, &old_settings);
404e5b845dcSCatherine Sullivan 
405f67435b5SDaode Huang 	/* Changing combined is not allowed */
406e5b845dcSCatherine Sullivan 	if (cmd->combined_count != old_settings.combined_count)
407e5b845dcSCatherine Sullivan 		return -EINVAL;
408e5b845dcSCatherine Sullivan 
409e5b845dcSCatherine Sullivan 	if (!new_rx || !new_tx)
410e5b845dcSCatherine Sullivan 		return -EINVAL;
411e5b845dcSCatherine Sullivan 
412e5b845dcSCatherine Sullivan 	if (!netif_carrier_ok(netdev)) {
413e5b845dcSCatherine Sullivan 		priv->tx_cfg.num_queues = new_tx;
414e5b845dcSCatherine Sullivan 		priv->rx_cfg.num_queues = new_rx;
415e5b845dcSCatherine Sullivan 		return 0;
416e5b845dcSCatherine Sullivan 	}
417e5b845dcSCatherine Sullivan 
418e5b845dcSCatherine Sullivan 	new_tx_cfg.num_queues = new_tx;
419e5b845dcSCatherine Sullivan 	new_rx_cfg.num_queues = new_rx;
420e5b845dcSCatherine Sullivan 
421e5b845dcSCatherine Sullivan 	return gve_adjust_queues(priv, new_rx_cfg, new_tx_cfg);
422e5b845dcSCatherine Sullivan }
423e5b845dcSCatherine Sullivan 
424e5b845dcSCatherine Sullivan static void gve_get_ringparam(struct net_device *netdev,
42574624944SHao Chen 			      struct ethtool_ringparam *cmd,
42674624944SHao Chen 			      struct kernel_ethtool_ringparam *kernel_cmd,
42774624944SHao Chen 			      struct netlink_ext_ack *extack)
428e5b845dcSCatherine Sullivan {
429e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
430e5b845dcSCatherine Sullivan 
431e5b845dcSCatherine Sullivan 	cmd->rx_max_pending = priv->rx_desc_cnt;
432e5b845dcSCatherine Sullivan 	cmd->tx_max_pending = priv->tx_desc_cnt;
433e5b845dcSCatherine Sullivan 	cmd->rx_pending = priv->rx_desc_cnt;
434e5b845dcSCatherine Sullivan 	cmd->tx_pending = priv->tx_desc_cnt;
435e5b845dcSCatherine Sullivan }
436e5b845dcSCatherine Sullivan 
437e5b845dcSCatherine Sullivan static int gve_user_reset(struct net_device *netdev, u32 *flags)
438e5b845dcSCatherine Sullivan {
439e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
440e5b845dcSCatherine Sullivan 
441e5b845dcSCatherine Sullivan 	if (*flags == ETH_RESET_ALL) {
442e5b845dcSCatherine Sullivan 		*flags = 0;
443e5b845dcSCatherine Sullivan 		return gve_reset(priv, true);
444e5b845dcSCatherine Sullivan 	}
445e5b845dcSCatherine Sullivan 
446e5b845dcSCatherine Sullivan 	return -EOPNOTSUPP;
447e5b845dcSCatherine Sullivan }
448e5b845dcSCatherine Sullivan 
449d5f7543cSKuo Zhao static int gve_get_tunable(struct net_device *netdev,
450d5f7543cSKuo Zhao 			   const struct ethtool_tunable *etuna, void *value)
451d5f7543cSKuo Zhao {
452d5f7543cSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
453d5f7543cSKuo Zhao 
454d5f7543cSKuo Zhao 	switch (etuna->id) {
455d5f7543cSKuo Zhao 	case ETHTOOL_RX_COPYBREAK:
456d5f7543cSKuo Zhao 		*(u32 *)value = priv->rx_copybreak;
457d5f7543cSKuo Zhao 		return 0;
458d5f7543cSKuo Zhao 	default:
459d5f7543cSKuo Zhao 		return -EOPNOTSUPP;
460d5f7543cSKuo Zhao 	}
461d5f7543cSKuo Zhao }
462d5f7543cSKuo Zhao 
463d5f7543cSKuo Zhao static int gve_set_tunable(struct net_device *netdev,
464433e274bSKuo Zhao 			   const struct ethtool_tunable *etuna,
465433e274bSKuo Zhao 			   const void *value)
466d5f7543cSKuo Zhao {
467d5f7543cSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
468d5f7543cSKuo Zhao 	u32 len;
469d5f7543cSKuo Zhao 
470d5f7543cSKuo Zhao 	switch (etuna->id) {
471d5f7543cSKuo Zhao 	case ETHTOOL_RX_COPYBREAK:
4721f6228e4SBailey Forrest 	{
4731f6228e4SBailey Forrest 		u32 max_copybreak = gve_is_gqi(priv) ?
4741f6228e4SBailey Forrest 			(PAGE_SIZE / 2) : priv->data_buffer_size_dqo;
4751f6228e4SBailey Forrest 
476d5f7543cSKuo Zhao 		len = *(u32 *)value;
4771f6228e4SBailey Forrest 		if (len > max_copybreak)
478d5f7543cSKuo Zhao 			return -EINVAL;
479d5f7543cSKuo Zhao 		priv->rx_copybreak = len;
480d5f7543cSKuo Zhao 		return 0;
4811f6228e4SBailey Forrest 	}
482d5f7543cSKuo Zhao 	default:
483d5f7543cSKuo Zhao 		return -EOPNOTSUPP;
484d5f7543cSKuo Zhao 	}
485d5f7543cSKuo Zhao }
486d5f7543cSKuo Zhao 
48724aeb56fSKuo Zhao static u32 gve_get_priv_flags(struct net_device *netdev)
48824aeb56fSKuo Zhao {
48924aeb56fSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
49024aeb56fSKuo Zhao 	u32 ret_flags = 0;
49124aeb56fSKuo Zhao 
49224aeb56fSKuo Zhao 	/* Only 1 flag exists currently: report-stats (BIT(O)), so set that flag. */
49324aeb56fSKuo Zhao 	if (priv->ethtool_flags & BIT(0))
49424aeb56fSKuo Zhao 		ret_flags |= BIT(0);
49524aeb56fSKuo Zhao 	return ret_flags;
49624aeb56fSKuo Zhao }
49724aeb56fSKuo Zhao 
49824aeb56fSKuo Zhao static int gve_set_priv_flags(struct net_device *netdev, u32 flags)
49924aeb56fSKuo Zhao {
50024aeb56fSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
50124aeb56fSKuo Zhao 	u64 ori_flags, new_flags;
50224aeb56fSKuo Zhao 
50324aeb56fSKuo Zhao 	ori_flags = READ_ONCE(priv->ethtool_flags);
50424aeb56fSKuo Zhao 	new_flags = ori_flags;
50524aeb56fSKuo Zhao 
50624aeb56fSKuo Zhao 	/* Only one priv flag exists: report-stats (BIT(0))*/
50724aeb56fSKuo Zhao 	if (flags & BIT(0))
50824aeb56fSKuo Zhao 		new_flags |= BIT(0);
50924aeb56fSKuo Zhao 	else
51024aeb56fSKuo Zhao 		new_flags &= ~(BIT(0));
51124aeb56fSKuo Zhao 	priv->ethtool_flags = new_flags;
51224aeb56fSKuo Zhao 	/* start report-stats timer when user turns report stats on. */
51324aeb56fSKuo Zhao 	if (flags & BIT(0)) {
51424aeb56fSKuo Zhao 		mod_timer(&priv->stats_report_timer,
51524aeb56fSKuo Zhao 			  round_jiffies(jiffies +
51624aeb56fSKuo Zhao 					msecs_to_jiffies(priv->stats_report_timer_period)));
51724aeb56fSKuo Zhao 	}
51824aeb56fSKuo Zhao 	/* Zero off gve stats when report-stats turned off and */
51924aeb56fSKuo Zhao 	/* delete report stats timer. */
52024aeb56fSKuo Zhao 	if (!(flags & BIT(0)) && (ori_flags & BIT(0))) {
52124aeb56fSKuo Zhao 		int tx_stats_num = GVE_TX_STATS_REPORT_NUM *
52224aeb56fSKuo Zhao 			priv->tx_cfg.num_queues;
52324aeb56fSKuo Zhao 		int rx_stats_num = GVE_RX_STATS_REPORT_NUM *
52424aeb56fSKuo Zhao 			priv->rx_cfg.num_queues;
52524aeb56fSKuo Zhao 
52624aeb56fSKuo Zhao 		memset(priv->stats_report->stats, 0, (tx_stats_num + rx_stats_num) *
52724aeb56fSKuo Zhao 				   sizeof(struct stats));
52824aeb56fSKuo Zhao 		del_timer_sync(&priv->stats_report_timer);
52924aeb56fSKuo Zhao 	}
53024aeb56fSKuo Zhao 	return 0;
53124aeb56fSKuo Zhao }
53224aeb56fSKuo Zhao 
5337e074d5aSDavid Awogbemila static int gve_get_link_ksettings(struct net_device *netdev,
5347e074d5aSDavid Awogbemila 				  struct ethtool_link_ksettings *cmd)
5357e074d5aSDavid Awogbemila {
5367e074d5aSDavid Awogbemila 	struct gve_priv *priv = netdev_priv(netdev);
5377e074d5aSDavid Awogbemila 	int err = gve_adminq_report_link_speed(priv);
5387e074d5aSDavid Awogbemila 
5397e074d5aSDavid Awogbemila 	cmd->base.speed = priv->link_speed;
5407e074d5aSDavid Awogbemila 	return err;
5417e074d5aSDavid Awogbemila }
5427e074d5aSDavid Awogbemila 
543e5b845dcSCatherine Sullivan const struct ethtool_ops gve_ethtool_ops = {
544e5b845dcSCatherine Sullivan 	.get_drvinfo = gve_get_drvinfo,
545e5b845dcSCatherine Sullivan 	.get_strings = gve_get_strings,
546e5b845dcSCatherine Sullivan 	.get_sset_count = gve_get_sset_count,
547e5b845dcSCatherine Sullivan 	.get_ethtool_stats = gve_get_ethtool_stats,
548e5b845dcSCatherine Sullivan 	.set_msglevel = gve_set_msglevel,
549e5b845dcSCatherine Sullivan 	.get_msglevel = gve_get_msglevel,
550e5b845dcSCatherine Sullivan 	.set_channels = gve_set_channels,
551e5b845dcSCatherine Sullivan 	.get_channels = gve_get_channels,
552e5b845dcSCatherine Sullivan 	.get_link = ethtool_op_get_link,
553e5b845dcSCatherine Sullivan 	.get_ringparam = gve_get_ringparam,
554e5b845dcSCatherine Sullivan 	.reset = gve_user_reset,
555d5f7543cSKuo Zhao 	.get_tunable = gve_get_tunable,
556d5f7543cSKuo Zhao 	.set_tunable = gve_set_tunable,
55724aeb56fSKuo Zhao 	.get_priv_flags = gve_get_priv_flags,
55824aeb56fSKuo Zhao 	.set_priv_flags = gve_set_priv_flags,
5597e074d5aSDavid Awogbemila 	.get_link_ksettings = gve_get_link_ksettings
560e5b845dcSCatherine Sullivan };
561