1e5b845dcSCatherine Sullivan // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2e5b845dcSCatherine Sullivan /* Google virtual Ethernet (gve) driver
3e5b845dcSCatherine Sullivan  *
4e5b845dcSCatherine Sullivan  * Copyright (C) 2015-2019 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 
17*c32773c9SDaode Huang 	strscpy(info->driver, "gve", sizeof(info->driver));
18*c32773c9SDaode Huang 	strscpy(info->version, gve_version_str, sizeof(info->version));
19*c32773c9SDaode 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]",
46433e274bSKuo Zhao 	"rx_dropped_pkt[%u]", "rx_copybreak_pkt[%u]", "rx_copied_pkt[%u]",
472f523dc3SDavid Awogbemila 	"rx_queue_drop_cnt[%u]", "rx_no_buffers_posted[%u]",
482f523dc3SDavid Awogbemila 	"rx_drops_packet_over_mru[%u]", "rx_drops_invalid_checksum[%u]",
49433e274bSKuo Zhao };
50433e274bSKuo Zhao 
51433e274bSKuo Zhao static const char gve_gstrings_tx_stats[][ETH_GSTRING_LEN] = {
52433e274bSKuo Zhao 	"tx_posted_desc[%u]", "tx_completed_desc[%u]", "tx_bytes[%u]",
53433e274bSKuo Zhao 	"tx_wake[%u]", "tx_stop[%u]", "tx_event_counter[%u]",
546f007c64SCatherine Sullivan 	"tx_dma_mapping_error[%u]",
55433e274bSKuo Zhao };
56433e274bSKuo Zhao 
57433e274bSKuo Zhao static const char gve_gstrings_adminq_stats[][ETH_GSTRING_LEN] = {
58433e274bSKuo Zhao 	"adminq_prod_cnt", "adminq_cmd_fail", "adminq_timeouts",
59433e274bSKuo Zhao 	"adminq_describe_device_cnt", "adminq_cfg_device_resources_cnt",
60433e274bSKuo Zhao 	"adminq_register_page_list_cnt", "adminq_unregister_page_list_cnt",
61433e274bSKuo Zhao 	"adminq_create_tx_queue_cnt", "adminq_create_rx_queue_cnt",
62433e274bSKuo Zhao 	"adminq_destroy_tx_queue_cnt", "adminq_destroy_rx_queue_cnt",
63433e274bSKuo Zhao 	"adminq_dcfg_device_resources_cnt", "adminq_set_driver_parameter_cnt",
647e074d5aSDavid Awogbemila 	"adminq_report_stats_cnt", "adminq_report_link_speed_cnt"
6524aeb56fSKuo Zhao };
6624aeb56fSKuo Zhao 
6724aeb56fSKuo Zhao static const char gve_gstrings_priv_flags[][ETH_GSTRING_LEN] = {
6824aeb56fSKuo Zhao 	"report-stats",
69e5b845dcSCatherine Sullivan };
70e5b845dcSCatherine Sullivan 
71e5b845dcSCatherine Sullivan #define GVE_MAIN_STATS_LEN  ARRAY_SIZE(gve_gstrings_main_stats)
72433e274bSKuo Zhao #define GVE_ADMINQ_STATS_LEN  ARRAY_SIZE(gve_gstrings_adminq_stats)
73433e274bSKuo Zhao #define NUM_GVE_TX_CNTS	ARRAY_SIZE(gve_gstrings_tx_stats)
74433e274bSKuo Zhao #define NUM_GVE_RX_CNTS	ARRAY_SIZE(gve_gstrings_rx_stats)
7524aeb56fSKuo Zhao #define GVE_PRIV_FLAGS_STR_LEN ARRAY_SIZE(gve_gstrings_priv_flags)
76e5b845dcSCatherine Sullivan 
77e5b845dcSCatherine Sullivan static void gve_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
78e5b845dcSCatherine Sullivan {
79e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
80e5b845dcSCatherine Sullivan 	char *s = (char *)data;
81433e274bSKuo Zhao 	int i, j;
82e5b845dcSCatherine Sullivan 
8324aeb56fSKuo Zhao 	switch (stringset) {
8424aeb56fSKuo Zhao 	case ETH_SS_STATS:
85e5b845dcSCatherine Sullivan 		memcpy(s, *gve_gstrings_main_stats,
86e5b845dcSCatherine Sullivan 		       sizeof(gve_gstrings_main_stats));
87e5b845dcSCatherine Sullivan 		s += sizeof(gve_gstrings_main_stats);
8824aeb56fSKuo Zhao 
89e5b845dcSCatherine Sullivan 		for (i = 0; i < priv->rx_cfg.num_queues; i++) {
90433e274bSKuo Zhao 			for (j = 0; j < NUM_GVE_RX_CNTS; j++) {
9124aeb56fSKuo Zhao 				snprintf(s, ETH_GSTRING_LEN,
9224aeb56fSKuo Zhao 					 gve_gstrings_rx_stats[j], i);
93e5b845dcSCatherine Sullivan 				s += ETH_GSTRING_LEN;
94433e274bSKuo Zhao 			}
95e5b845dcSCatherine Sullivan 		}
9624aeb56fSKuo Zhao 
97e5b845dcSCatherine Sullivan 		for (i = 0; i < priv->tx_cfg.num_queues; i++) {
98433e274bSKuo Zhao 			for (j = 0; j < NUM_GVE_TX_CNTS; j++) {
9924aeb56fSKuo Zhao 				snprintf(s, ETH_GSTRING_LEN,
10024aeb56fSKuo Zhao 					 gve_gstrings_tx_stats[j], i);
101e5b845dcSCatherine Sullivan 				s += ETH_GSTRING_LEN;
102e5b845dcSCatherine Sullivan 			}
103e5b845dcSCatherine Sullivan 		}
104e5b845dcSCatherine Sullivan 
105433e274bSKuo Zhao 		memcpy(s, *gve_gstrings_adminq_stats,
106433e274bSKuo Zhao 		       sizeof(gve_gstrings_adminq_stats));
107433e274bSKuo Zhao 		s += sizeof(gve_gstrings_adminq_stats);
10824aeb56fSKuo Zhao 		break;
10924aeb56fSKuo Zhao 
11024aeb56fSKuo Zhao 	case ETH_SS_PRIV_FLAGS:
11124aeb56fSKuo Zhao 		memcpy(s, *gve_gstrings_priv_flags,
11224aeb56fSKuo Zhao 		       sizeof(gve_gstrings_priv_flags));
11324aeb56fSKuo Zhao 		s += sizeof(gve_gstrings_priv_flags);
11424aeb56fSKuo Zhao 		break;
11524aeb56fSKuo Zhao 
11624aeb56fSKuo Zhao 	default:
11724aeb56fSKuo Zhao 		break;
11824aeb56fSKuo Zhao 	}
119433e274bSKuo Zhao }
120433e274bSKuo Zhao 
121e5b845dcSCatherine Sullivan static int gve_get_sset_count(struct net_device *netdev, int sset)
122e5b845dcSCatherine Sullivan {
123e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
124e5b845dcSCatherine Sullivan 
125e5b845dcSCatherine Sullivan 	switch (sset) {
126e5b845dcSCatherine Sullivan 	case ETH_SS_STATS:
127433e274bSKuo Zhao 		return GVE_MAIN_STATS_LEN + GVE_ADMINQ_STATS_LEN +
128e5b845dcSCatherine Sullivan 		       (priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS) +
129e5b845dcSCatherine Sullivan 		       (priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS);
13024aeb56fSKuo Zhao 	case ETH_SS_PRIV_FLAGS:
13124aeb56fSKuo Zhao 		return GVE_PRIV_FLAGS_STR_LEN;
132e5b845dcSCatherine Sullivan 	default:
133e5b845dcSCatherine Sullivan 		return -EOPNOTSUPP;
134e5b845dcSCatherine Sullivan 	}
135e5b845dcSCatherine Sullivan }
136e5b845dcSCatherine Sullivan 
137e5b845dcSCatherine Sullivan static void
138e5b845dcSCatherine Sullivan gve_get_ethtool_stats(struct net_device *netdev,
139e5b845dcSCatherine Sullivan 		      struct ethtool_stats *stats, u64 *data)
140e5b845dcSCatherine Sullivan {
141433e274bSKuo Zhao 	u64 tmp_rx_pkts, tmp_rx_bytes, tmp_rx_skb_alloc_fail,	tmp_rx_buf_alloc_fail,
142433e274bSKuo Zhao 		tmp_rx_desc_err_dropped_pkt, tmp_tx_pkts, tmp_tx_bytes;
143433e274bSKuo Zhao 	u64 rx_buf_alloc_fail, rx_desc_err_dropped_pkt, rx_pkts,
144433e274bSKuo Zhao 		rx_skb_alloc_fail, rx_bytes, tx_pkts, tx_bytes;
1452f523dc3SDavid Awogbemila 	int stats_idx, base_stats_idx, max_stats_idx;
1462f523dc3SDavid Awogbemila 	struct stats *report_stats;
1472f523dc3SDavid Awogbemila 	int *rx_qid_to_stats_idx;
1482f523dc3SDavid Awogbemila 	int *tx_qid_to_stats_idx;
149433e274bSKuo Zhao 	struct gve_priv *priv;
1502f523dc3SDavid Awogbemila 	bool skip_nic_stats;
151e5b845dcSCatherine Sullivan 	unsigned int start;
152e5b845dcSCatherine Sullivan 	int ring;
1532f523dc3SDavid Awogbemila 	int i, j;
154e5b845dcSCatherine Sullivan 
155e5b845dcSCatherine Sullivan 	ASSERT_RTNL();
156e5b845dcSCatherine Sullivan 
157433e274bSKuo Zhao 	priv = netdev_priv(netdev);
1582f523dc3SDavid Awogbemila 	report_stats = priv->stats_report->stats;
1592f523dc3SDavid Awogbemila 	rx_qid_to_stats_idx = kmalloc_array(priv->rx_cfg.num_queues,
1602f523dc3SDavid Awogbemila 					    sizeof(int), GFP_KERNEL);
1612f523dc3SDavid Awogbemila 	if (!rx_qid_to_stats_idx)
1622f523dc3SDavid Awogbemila 		return;
1632f523dc3SDavid Awogbemila 	tx_qid_to_stats_idx = kmalloc_array(priv->tx_cfg.num_queues,
1642f523dc3SDavid Awogbemila 					    sizeof(int), GFP_KERNEL);
1652f523dc3SDavid Awogbemila 	if (!tx_qid_to_stats_idx) {
1662f523dc3SDavid Awogbemila 		kfree(rx_qid_to_stats_idx);
1672f523dc3SDavid Awogbemila 		return;
1682f523dc3SDavid Awogbemila 	}
169433e274bSKuo Zhao 	for (rx_pkts = 0, rx_bytes = 0, rx_skb_alloc_fail = 0,
170433e274bSKuo Zhao 	     rx_buf_alloc_fail = 0, rx_desc_err_dropped_pkt = 0, ring = 0;
171e5b845dcSCatherine Sullivan 	     ring < priv->rx_cfg.num_queues; ring++) {
172e5b845dcSCatherine Sullivan 		if (priv->rx) {
173e5b845dcSCatherine Sullivan 			do {
174433e274bSKuo Zhao 				struct gve_rx_ring *rx = &priv->rx[ring];
175433e274bSKuo Zhao 
1763c13ce74SCatherine Sullivan 				start =
177e5b845dcSCatherine Sullivan 				  u64_stats_fetch_begin(&priv->rx[ring].statss);
178433e274bSKuo Zhao 				tmp_rx_pkts = rx->rpackets;
179433e274bSKuo Zhao 				tmp_rx_bytes = rx->rbytes;
180433e274bSKuo Zhao 				tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail;
181433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail;
182433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt =
183433e274bSKuo Zhao 					rx->rx_desc_err_dropped_pkt;
184e5b845dcSCatherine Sullivan 			} while (u64_stats_fetch_retry(&priv->rx[ring].statss,
185e5b845dcSCatherine Sullivan 						       start));
186433e274bSKuo Zhao 			rx_pkts += tmp_rx_pkts;
187433e274bSKuo Zhao 			rx_bytes += tmp_rx_bytes;
188433e274bSKuo Zhao 			rx_skb_alloc_fail += tmp_rx_skb_alloc_fail;
189433e274bSKuo Zhao 			rx_buf_alloc_fail += tmp_rx_buf_alloc_fail;
190433e274bSKuo Zhao 			rx_desc_err_dropped_pkt += tmp_rx_desc_err_dropped_pkt;
191e5b845dcSCatherine Sullivan 		}
192e5b845dcSCatherine Sullivan 	}
193e5b845dcSCatherine Sullivan 	for (tx_pkts = 0, tx_bytes = 0, ring = 0;
194e5b845dcSCatherine Sullivan 	     ring < priv->tx_cfg.num_queues; ring++) {
195e5b845dcSCatherine Sullivan 		if (priv->tx) {
196e5b845dcSCatherine Sullivan 			do {
1973c13ce74SCatherine Sullivan 				start =
198e5b845dcSCatherine Sullivan 				  u64_stats_fetch_begin(&priv->tx[ring].statss);
199433e274bSKuo Zhao 				tmp_tx_pkts = priv->tx[ring].pkt_done;
200433e274bSKuo Zhao 				tmp_tx_bytes = priv->tx[ring].bytes_done;
201e5b845dcSCatherine Sullivan 			} while (u64_stats_fetch_retry(&priv->tx[ring].statss,
202e5b845dcSCatherine Sullivan 						       start));
203433e274bSKuo Zhao 			tx_pkts += tmp_tx_pkts;
204433e274bSKuo Zhao 			tx_bytes += tmp_tx_bytes;
205e5b845dcSCatherine Sullivan 		}
206e5b845dcSCatherine Sullivan 	}
207e5b845dcSCatherine Sullivan 
208e5b845dcSCatherine Sullivan 	i = 0;
209e5b845dcSCatherine Sullivan 	data[i++] = rx_pkts;
210e5b845dcSCatherine Sullivan 	data[i++] = tx_pkts;
211e5b845dcSCatherine Sullivan 	data[i++] = rx_bytes;
212e5b845dcSCatherine Sullivan 	data[i++] = tx_bytes;
213433e274bSKuo Zhao 	/* total rx dropped packets */
214433e274bSKuo Zhao 	data[i++] = rx_skb_alloc_fail + rx_buf_alloc_fail +
215433e274bSKuo Zhao 		    rx_desc_err_dropped_pkt;
216433e274bSKuo Zhao 	/* Skip tx_dropped */
217433e274bSKuo Zhao 	i++;
218433e274bSKuo Zhao 
219e5b845dcSCatherine Sullivan 	data[i++] = priv->tx_timeo_cnt;
220433e274bSKuo Zhao 	data[i++] = rx_skb_alloc_fail;
221433e274bSKuo Zhao 	data[i++] = rx_buf_alloc_fail;
222433e274bSKuo Zhao 	data[i++] = rx_desc_err_dropped_pkt;
223433e274bSKuo Zhao 	data[i++] = priv->interface_up_cnt;
224433e274bSKuo Zhao 	data[i++] = priv->interface_down_cnt;
225433e274bSKuo Zhao 	data[i++] = priv->reset_cnt;
226433e274bSKuo Zhao 	data[i++] = priv->page_alloc_fail;
227433e274bSKuo Zhao 	data[i++] = priv->dma_mapping_error;
22824aeb56fSKuo Zhao 	data[i++] = priv->stats_report_trigger_cnt;
229e5b845dcSCatherine Sullivan 	i = GVE_MAIN_STATS_LEN;
230e5b845dcSCatherine Sullivan 
2312f523dc3SDavid Awogbemila 	/* For rx cross-reporting stats, start from nic rx stats in report */
2322f523dc3SDavid Awogbemila 	base_stats_idx = GVE_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues +
2332f523dc3SDavid Awogbemila 		GVE_RX_STATS_REPORT_NUM * priv->rx_cfg.num_queues;
2342f523dc3SDavid Awogbemila 	max_stats_idx = NIC_RX_STATS_REPORT_NUM * priv->rx_cfg.num_queues +
2352f523dc3SDavid Awogbemila 		base_stats_idx;
2362f523dc3SDavid Awogbemila 	/* Preprocess the stats report for rx, map queue id to start index */
2372f523dc3SDavid Awogbemila 	skip_nic_stats = false;
2382f523dc3SDavid Awogbemila 	for (stats_idx = base_stats_idx; stats_idx < max_stats_idx;
2392f523dc3SDavid Awogbemila 		stats_idx += NIC_RX_STATS_REPORT_NUM) {
2402f523dc3SDavid Awogbemila 		u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name);
2412f523dc3SDavid Awogbemila 		u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id);
2422f523dc3SDavid Awogbemila 
2432f523dc3SDavid Awogbemila 		if (stat_name == 0) {
2442f523dc3SDavid Awogbemila 			/* no stats written by NIC yet */
2452f523dc3SDavid Awogbemila 			skip_nic_stats = true;
2462f523dc3SDavid Awogbemila 			break;
2472f523dc3SDavid Awogbemila 		}
2482f523dc3SDavid Awogbemila 		rx_qid_to_stats_idx[queue_id] = stats_idx;
2492f523dc3SDavid Awogbemila 	}
250e5b845dcSCatherine Sullivan 	/* walk RX rings */
251e5b845dcSCatherine Sullivan 	if (priv->rx) {
252e5b845dcSCatherine Sullivan 		for (ring = 0; ring < priv->rx_cfg.num_queues; ring++) {
253e5b845dcSCatherine Sullivan 			struct gve_rx_ring *rx = &priv->rx[ring];
254e5b845dcSCatherine Sullivan 
255438b43bdSCatherine Sullivan 			data[i++] = rx->fill_cnt;
256433e274bSKuo Zhao 			data[i++] = rx->cnt;
257433e274bSKuo Zhao 			do {
258433e274bSKuo Zhao 				start =
259433e274bSKuo Zhao 				  u64_stats_fetch_begin(&priv->rx[ring].statss);
260433e274bSKuo Zhao 				tmp_rx_bytes = rx->rbytes;
261433e274bSKuo Zhao 				tmp_rx_skb_alloc_fail = rx->rx_skb_alloc_fail;
262433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail = rx->rx_buf_alloc_fail;
263433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt =
264433e274bSKuo Zhao 					rx->rx_desc_err_dropped_pkt;
265433e274bSKuo Zhao 			} while (u64_stats_fetch_retry(&priv->rx[ring].statss,
266433e274bSKuo Zhao 						       start));
267433e274bSKuo Zhao 			data[i++] = tmp_rx_bytes;
268433e274bSKuo Zhao 			/* rx dropped packets */
269433e274bSKuo Zhao 			data[i++] = tmp_rx_skb_alloc_fail +
270433e274bSKuo Zhao 				tmp_rx_buf_alloc_fail +
271433e274bSKuo Zhao 				tmp_rx_desc_err_dropped_pkt;
272433e274bSKuo Zhao 			data[i++] = rx->rx_copybreak_pkt;
273433e274bSKuo Zhao 			data[i++] = rx->rx_copied_pkt;
2742f523dc3SDavid Awogbemila 			/* stats from NIC */
2752f523dc3SDavid Awogbemila 			if (skip_nic_stats) {
2762f523dc3SDavid Awogbemila 				/* skip NIC rx stats */
2772f523dc3SDavid Awogbemila 				i += NIC_RX_STATS_REPORT_NUM;
2782f523dc3SDavid Awogbemila 				continue;
2792f523dc3SDavid Awogbemila 			}
2802f523dc3SDavid Awogbemila 			for (j = 0; j < NIC_RX_STATS_REPORT_NUM; j++) {
2812f523dc3SDavid Awogbemila 				u64 value =
2822f523dc3SDavid Awogbemila 				be64_to_cpu(report_stats[rx_qid_to_stats_idx[ring] + j].value);
2832f523dc3SDavid Awogbemila 
2842f523dc3SDavid Awogbemila 				data[i++] = value;
2852f523dc3SDavid Awogbemila 			}
286e5b845dcSCatherine Sullivan 		}
287e5b845dcSCatherine Sullivan 	} else {
288e5b845dcSCatherine Sullivan 		i += priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS;
289e5b845dcSCatherine Sullivan 	}
2902f523dc3SDavid Awogbemila 
2912f523dc3SDavid Awogbemila 	/* For tx cross-reporting stats, start from nic tx stats in report */
2922f523dc3SDavid Awogbemila 	base_stats_idx = max_stats_idx;
2932f523dc3SDavid Awogbemila 	max_stats_idx = NIC_TX_STATS_REPORT_NUM * priv->tx_cfg.num_queues +
2942f523dc3SDavid Awogbemila 		max_stats_idx;
2952f523dc3SDavid Awogbemila 	/* Preprocess the stats report for tx, map queue id to start index */
2962f523dc3SDavid Awogbemila 	skip_nic_stats = false;
2972f523dc3SDavid Awogbemila 	for (stats_idx = base_stats_idx; stats_idx < max_stats_idx;
2982f523dc3SDavid Awogbemila 		stats_idx += NIC_TX_STATS_REPORT_NUM) {
2992f523dc3SDavid Awogbemila 		u32 stat_name = be32_to_cpu(report_stats[stats_idx].stat_name);
3002f523dc3SDavid Awogbemila 		u32 queue_id = be32_to_cpu(report_stats[stats_idx].queue_id);
3012f523dc3SDavid Awogbemila 
3022f523dc3SDavid Awogbemila 		if (stat_name == 0) {
3032f523dc3SDavid Awogbemila 			/* no stats written by NIC yet */
3042f523dc3SDavid Awogbemila 			skip_nic_stats = true;
3052f523dc3SDavid Awogbemila 			break;
3062f523dc3SDavid Awogbemila 		}
3072f523dc3SDavid Awogbemila 		tx_qid_to_stats_idx[queue_id] = stats_idx;
3082f523dc3SDavid Awogbemila 	}
309e5b845dcSCatherine Sullivan 	/* walk TX rings */
310e5b845dcSCatherine Sullivan 	if (priv->tx) {
311e5b845dcSCatherine Sullivan 		for (ring = 0; ring < priv->tx_cfg.num_queues; ring++) {
312e5b845dcSCatherine Sullivan 			struct gve_tx_ring *tx = &priv->tx[ring];
313e5b845dcSCatherine Sullivan 
314e5b845dcSCatherine Sullivan 			data[i++] = tx->req;
315e5b845dcSCatherine Sullivan 			data[i++] = tx->done;
316433e274bSKuo Zhao 			do {
317433e274bSKuo Zhao 				start =
318433e274bSKuo Zhao 				  u64_stats_fetch_begin(&priv->tx[ring].statss);
319433e274bSKuo Zhao 				tmp_tx_bytes = tx->bytes_done;
320433e274bSKuo Zhao 			} while (u64_stats_fetch_retry(&priv->tx[ring].statss,
321433e274bSKuo Zhao 						       start));
322433e274bSKuo Zhao 			data[i++] = tmp_tx_bytes;
323e5b845dcSCatherine Sullivan 			data[i++] = tx->wake_queue;
324e5b845dcSCatherine Sullivan 			data[i++] = tx->stop_queue;
325e5b845dcSCatherine Sullivan 			data[i++] = be32_to_cpu(gve_tx_load_event_counter(priv,
326e5b845dcSCatherine Sullivan 									  tx));
3276f007c64SCatherine Sullivan 			data[i++] = tx->dma_mapping_error;
3282f523dc3SDavid Awogbemila 			/* stats from NIC */
3292f523dc3SDavid Awogbemila 			if (skip_nic_stats) {
3302f523dc3SDavid Awogbemila 				/* skip NIC tx stats */
3312f523dc3SDavid Awogbemila 				i += NIC_TX_STATS_REPORT_NUM;
3322f523dc3SDavid Awogbemila 				continue;
3332f523dc3SDavid Awogbemila 			}
3342f523dc3SDavid Awogbemila 			for (j = 0; j < NIC_TX_STATS_REPORT_NUM; j++) {
3352f523dc3SDavid Awogbemila 				u64 value =
3362f523dc3SDavid Awogbemila 				be64_to_cpu(report_stats[tx_qid_to_stats_idx[ring] + j].value);
3372f523dc3SDavid Awogbemila 				data[i++] = value;
3382f523dc3SDavid Awogbemila 			}
339e5b845dcSCatherine Sullivan 		}
340e5b845dcSCatherine Sullivan 	} else {
341e5b845dcSCatherine Sullivan 		i += priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS;
342e5b845dcSCatherine Sullivan 	}
3432f523dc3SDavid Awogbemila 
3442f523dc3SDavid Awogbemila 	kfree(rx_qid_to_stats_idx);
3452f523dc3SDavid Awogbemila 	kfree(tx_qid_to_stats_idx);
346433e274bSKuo Zhao 	/* AQ Stats */
347433e274bSKuo Zhao 	data[i++] = priv->adminq_prod_cnt;
348433e274bSKuo Zhao 	data[i++] = priv->adminq_cmd_fail;
349433e274bSKuo Zhao 	data[i++] = priv->adminq_timeouts;
350433e274bSKuo Zhao 	data[i++] = priv->adminq_describe_device_cnt;
351433e274bSKuo Zhao 	data[i++] = priv->adminq_cfg_device_resources_cnt;
352433e274bSKuo Zhao 	data[i++] = priv->adminq_register_page_list_cnt;
353433e274bSKuo Zhao 	data[i++] = priv->adminq_unregister_page_list_cnt;
354433e274bSKuo Zhao 	data[i++] = priv->adminq_create_tx_queue_cnt;
355433e274bSKuo Zhao 	data[i++] = priv->adminq_create_rx_queue_cnt;
356433e274bSKuo Zhao 	data[i++] = priv->adminq_destroy_tx_queue_cnt;
357433e274bSKuo Zhao 	data[i++] = priv->adminq_destroy_rx_queue_cnt;
358433e274bSKuo Zhao 	data[i++] = priv->adminq_dcfg_device_resources_cnt;
359433e274bSKuo Zhao 	data[i++] = priv->adminq_set_driver_parameter_cnt;
36024aeb56fSKuo Zhao 	data[i++] = priv->adminq_report_stats_cnt;
3617e074d5aSDavid Awogbemila 	data[i++] = priv->adminq_report_link_speed_cnt;
362e5b845dcSCatherine Sullivan }
363e5b845dcSCatherine Sullivan 
364e5b845dcSCatherine Sullivan static void gve_get_channels(struct net_device *netdev,
365e5b845dcSCatherine Sullivan 			     struct ethtool_channels *cmd)
366e5b845dcSCatherine Sullivan {
367e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
368e5b845dcSCatherine Sullivan 
369e5b845dcSCatherine Sullivan 	cmd->max_rx = priv->rx_cfg.max_queues;
370e5b845dcSCatherine Sullivan 	cmd->max_tx = priv->tx_cfg.max_queues;
371e5b845dcSCatherine Sullivan 	cmd->max_other = 0;
372e5b845dcSCatherine Sullivan 	cmd->max_combined = 0;
373e5b845dcSCatherine Sullivan 	cmd->rx_count = priv->rx_cfg.num_queues;
374e5b845dcSCatherine Sullivan 	cmd->tx_count = priv->tx_cfg.num_queues;
375e5b845dcSCatherine Sullivan 	cmd->other_count = 0;
376e5b845dcSCatherine Sullivan 	cmd->combined_count = 0;
377e5b845dcSCatherine Sullivan }
378e5b845dcSCatherine Sullivan 
379e5b845dcSCatherine Sullivan static int gve_set_channels(struct net_device *netdev,
380e5b845dcSCatherine Sullivan 			    struct ethtool_channels *cmd)
381e5b845dcSCatherine Sullivan {
382e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
383e5b845dcSCatherine Sullivan 	struct gve_queue_config new_tx_cfg = priv->tx_cfg;
384e5b845dcSCatherine Sullivan 	struct gve_queue_config new_rx_cfg = priv->rx_cfg;
385e5b845dcSCatherine Sullivan 	struct ethtool_channels old_settings;
386e5b845dcSCatherine Sullivan 	int new_tx = cmd->tx_count;
387e5b845dcSCatherine Sullivan 	int new_rx = cmd->rx_count;
388e5b845dcSCatherine Sullivan 
389e5b845dcSCatherine Sullivan 	gve_get_channels(netdev, &old_settings);
390e5b845dcSCatherine Sullivan 
391e5b845dcSCatherine Sullivan 	/* Changing combined is not allowed allowed */
392e5b845dcSCatherine Sullivan 	if (cmd->combined_count != old_settings.combined_count)
393e5b845dcSCatherine Sullivan 		return -EINVAL;
394e5b845dcSCatherine Sullivan 
395e5b845dcSCatherine Sullivan 	if (!new_rx || !new_tx)
396e5b845dcSCatherine Sullivan 		return -EINVAL;
397e5b845dcSCatherine Sullivan 
398e5b845dcSCatherine Sullivan 	if (!netif_carrier_ok(netdev)) {
399e5b845dcSCatherine Sullivan 		priv->tx_cfg.num_queues = new_tx;
400e5b845dcSCatherine Sullivan 		priv->rx_cfg.num_queues = new_rx;
401e5b845dcSCatherine Sullivan 		return 0;
402e5b845dcSCatherine Sullivan 	}
403e5b845dcSCatherine Sullivan 
404e5b845dcSCatherine Sullivan 	new_tx_cfg.num_queues = new_tx;
405e5b845dcSCatherine Sullivan 	new_rx_cfg.num_queues = new_rx;
406e5b845dcSCatherine Sullivan 
407e5b845dcSCatherine Sullivan 	return gve_adjust_queues(priv, new_rx_cfg, new_tx_cfg);
408e5b845dcSCatherine Sullivan }
409e5b845dcSCatherine Sullivan 
410e5b845dcSCatherine Sullivan static void gve_get_ringparam(struct net_device *netdev,
411e5b845dcSCatherine Sullivan 			      struct ethtool_ringparam *cmd)
412e5b845dcSCatherine Sullivan {
413e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
414e5b845dcSCatherine Sullivan 
415e5b845dcSCatherine Sullivan 	cmd->rx_max_pending = priv->rx_desc_cnt;
416e5b845dcSCatherine Sullivan 	cmd->tx_max_pending = priv->tx_desc_cnt;
417e5b845dcSCatherine Sullivan 	cmd->rx_pending = priv->rx_desc_cnt;
418e5b845dcSCatherine Sullivan 	cmd->tx_pending = priv->tx_desc_cnt;
419e5b845dcSCatherine Sullivan }
420e5b845dcSCatherine Sullivan 
421e5b845dcSCatherine Sullivan static int gve_user_reset(struct net_device *netdev, u32 *flags)
422e5b845dcSCatherine Sullivan {
423e5b845dcSCatherine Sullivan 	struct gve_priv *priv = netdev_priv(netdev);
424e5b845dcSCatherine Sullivan 
425e5b845dcSCatherine Sullivan 	if (*flags == ETH_RESET_ALL) {
426e5b845dcSCatherine Sullivan 		*flags = 0;
427e5b845dcSCatherine Sullivan 		return gve_reset(priv, true);
428e5b845dcSCatherine Sullivan 	}
429e5b845dcSCatherine Sullivan 
430e5b845dcSCatherine Sullivan 	return -EOPNOTSUPP;
431e5b845dcSCatherine Sullivan }
432e5b845dcSCatherine Sullivan 
433d5f7543cSKuo Zhao static int gve_get_tunable(struct net_device *netdev,
434d5f7543cSKuo Zhao 			   const struct ethtool_tunable *etuna, void *value)
435d5f7543cSKuo Zhao {
436d5f7543cSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
437d5f7543cSKuo Zhao 
438d5f7543cSKuo Zhao 	switch (etuna->id) {
439d5f7543cSKuo Zhao 	case ETHTOOL_RX_COPYBREAK:
440d5f7543cSKuo Zhao 		*(u32 *)value = priv->rx_copybreak;
441d5f7543cSKuo Zhao 		return 0;
442d5f7543cSKuo Zhao 	default:
443d5f7543cSKuo Zhao 		return -EOPNOTSUPP;
444d5f7543cSKuo Zhao 	}
445d5f7543cSKuo Zhao }
446d5f7543cSKuo Zhao 
447d5f7543cSKuo Zhao static int gve_set_tunable(struct net_device *netdev,
448433e274bSKuo Zhao 			   const struct ethtool_tunable *etuna,
449433e274bSKuo Zhao 			   const void *value)
450d5f7543cSKuo Zhao {
451d5f7543cSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
452d5f7543cSKuo Zhao 	u32 len;
453d5f7543cSKuo Zhao 
454d5f7543cSKuo Zhao 	switch (etuna->id) {
455d5f7543cSKuo Zhao 	case ETHTOOL_RX_COPYBREAK:
456d5f7543cSKuo Zhao 		len = *(u32 *)value;
457d5f7543cSKuo Zhao 		if (len > PAGE_SIZE / 2)
458d5f7543cSKuo Zhao 			return -EINVAL;
459d5f7543cSKuo Zhao 		priv->rx_copybreak = len;
460d5f7543cSKuo Zhao 		return 0;
461d5f7543cSKuo Zhao 	default:
462d5f7543cSKuo Zhao 		return -EOPNOTSUPP;
463d5f7543cSKuo Zhao 	}
464d5f7543cSKuo Zhao }
465d5f7543cSKuo Zhao 
46624aeb56fSKuo Zhao static u32 gve_get_priv_flags(struct net_device *netdev)
46724aeb56fSKuo Zhao {
46824aeb56fSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
46924aeb56fSKuo Zhao 	u32 ret_flags = 0;
47024aeb56fSKuo Zhao 
47124aeb56fSKuo Zhao 	/* Only 1 flag exists currently: report-stats (BIT(O)), so set that flag. */
47224aeb56fSKuo Zhao 	if (priv->ethtool_flags & BIT(0))
47324aeb56fSKuo Zhao 		ret_flags |= BIT(0);
47424aeb56fSKuo Zhao 	return ret_flags;
47524aeb56fSKuo Zhao }
47624aeb56fSKuo Zhao 
47724aeb56fSKuo Zhao static int gve_set_priv_flags(struct net_device *netdev, u32 flags)
47824aeb56fSKuo Zhao {
47924aeb56fSKuo Zhao 	struct gve_priv *priv = netdev_priv(netdev);
48024aeb56fSKuo Zhao 	u64 ori_flags, new_flags;
48124aeb56fSKuo Zhao 
48224aeb56fSKuo Zhao 	ori_flags = READ_ONCE(priv->ethtool_flags);
48324aeb56fSKuo Zhao 	new_flags = ori_flags;
48424aeb56fSKuo Zhao 
48524aeb56fSKuo Zhao 	/* Only one priv flag exists: report-stats (BIT(0))*/
48624aeb56fSKuo Zhao 	if (flags & BIT(0))
48724aeb56fSKuo Zhao 		new_flags |= BIT(0);
48824aeb56fSKuo Zhao 	else
48924aeb56fSKuo Zhao 		new_flags &= ~(BIT(0));
49024aeb56fSKuo Zhao 	priv->ethtool_flags = new_flags;
49124aeb56fSKuo Zhao 	/* start report-stats timer when user turns report stats on. */
49224aeb56fSKuo Zhao 	if (flags & BIT(0)) {
49324aeb56fSKuo Zhao 		mod_timer(&priv->stats_report_timer,
49424aeb56fSKuo Zhao 			  round_jiffies(jiffies +
49524aeb56fSKuo Zhao 					msecs_to_jiffies(priv->stats_report_timer_period)));
49624aeb56fSKuo Zhao 	}
49724aeb56fSKuo Zhao 	/* Zero off gve stats when report-stats turned off and */
49824aeb56fSKuo Zhao 	/* delete report stats timer. */
49924aeb56fSKuo Zhao 	if (!(flags & BIT(0)) && (ori_flags & BIT(0))) {
50024aeb56fSKuo Zhao 		int tx_stats_num = GVE_TX_STATS_REPORT_NUM *
50124aeb56fSKuo Zhao 			priv->tx_cfg.num_queues;
50224aeb56fSKuo Zhao 		int rx_stats_num = GVE_RX_STATS_REPORT_NUM *
50324aeb56fSKuo Zhao 			priv->rx_cfg.num_queues;
50424aeb56fSKuo Zhao 
50524aeb56fSKuo Zhao 		memset(priv->stats_report->stats, 0, (tx_stats_num + rx_stats_num) *
50624aeb56fSKuo Zhao 				   sizeof(struct stats));
50724aeb56fSKuo Zhao 		del_timer_sync(&priv->stats_report_timer);
50824aeb56fSKuo Zhao 	}
50924aeb56fSKuo Zhao 	return 0;
51024aeb56fSKuo Zhao }
51124aeb56fSKuo Zhao 
5127e074d5aSDavid Awogbemila static int gve_get_link_ksettings(struct net_device *netdev,
5137e074d5aSDavid Awogbemila 				  struct ethtool_link_ksettings *cmd)
5147e074d5aSDavid Awogbemila {
5157e074d5aSDavid Awogbemila 	struct gve_priv *priv = netdev_priv(netdev);
5167e074d5aSDavid Awogbemila 	int err = gve_adminq_report_link_speed(priv);
5177e074d5aSDavid Awogbemila 
5187e074d5aSDavid Awogbemila 	cmd->base.speed = priv->link_speed;
5197e074d5aSDavid Awogbemila 	return err;
5207e074d5aSDavid Awogbemila }
5217e074d5aSDavid Awogbemila 
522e5b845dcSCatherine Sullivan const struct ethtool_ops gve_ethtool_ops = {
523e5b845dcSCatherine Sullivan 	.get_drvinfo = gve_get_drvinfo,
524e5b845dcSCatherine Sullivan 	.get_strings = gve_get_strings,
525e5b845dcSCatherine Sullivan 	.get_sset_count = gve_get_sset_count,
526e5b845dcSCatherine Sullivan 	.get_ethtool_stats = gve_get_ethtool_stats,
527e5b845dcSCatherine Sullivan 	.set_msglevel = gve_set_msglevel,
528e5b845dcSCatherine Sullivan 	.get_msglevel = gve_get_msglevel,
529e5b845dcSCatherine Sullivan 	.set_channels = gve_set_channels,
530e5b845dcSCatherine Sullivan 	.get_channels = gve_get_channels,
531e5b845dcSCatherine Sullivan 	.get_link = ethtool_op_get_link,
532e5b845dcSCatherine Sullivan 	.get_ringparam = gve_get_ringparam,
533e5b845dcSCatherine Sullivan 	.reset = gve_user_reset,
534d5f7543cSKuo Zhao 	.get_tunable = gve_get_tunable,
535d5f7543cSKuo Zhao 	.set_tunable = gve_set_tunable,
53624aeb56fSKuo Zhao 	.get_priv_flags = gve_get_priv_flags,
53724aeb56fSKuo Zhao 	.set_priv_flags = gve_set_priv_flags,
5387e074d5aSDavid Awogbemila 	.get_link_ksettings = gve_get_link_ksettings
539e5b845dcSCatherine Sullivan };
540