1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Google virtual Ethernet (gve) driver
4 * Copyright (C) 2015-2019 Google, Inc.
7 #include <linux/rtnetlink.h>
10 static void gve_get_drvinfo(struct net_device *netdev,
11 struct ethtool_drvinfo *info)
13 struct gve_priv *priv = netdev_priv(netdev);
15 strlcpy(info->driver, "gve", sizeof(info->driver));
16 strlcpy(info->version, gve_version_str, sizeof(info->version));
17 strlcpy(info->bus_info, pci_name(priv->pdev), sizeof(info->bus_info));
20 static void gve_set_msglevel(struct net_device *netdev, u32 value)
22 struct gve_priv *priv = netdev_priv(netdev);
24 priv->msg_enable = value;
27 static u32 gve_get_msglevel(struct net_device *netdev)
29 struct gve_priv *priv = netdev_priv(netdev);
31 return priv->msg_enable;
34 static const char gve_gstrings_main_stats[][ETH_GSTRING_LEN] = {
35 "rx_packets", "tx_packets", "rx_bytes", "tx_bytes",
36 "rx_dropped", "tx_dropped", "tx_timeouts",
39 #define GVE_MAIN_STATS_LEN ARRAY_SIZE(gve_gstrings_main_stats)
40 #define NUM_GVE_TX_CNTS 5
41 #define NUM_GVE_RX_CNTS 2
43 static void gve_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
45 struct gve_priv *priv = netdev_priv(netdev);
46 char *s = (char *)data;
49 if (stringset != ETH_SS_STATS)
52 memcpy(s, *gve_gstrings_main_stats,
53 sizeof(gve_gstrings_main_stats));
54 s += sizeof(gve_gstrings_main_stats);
55 for (i = 0; i < priv->rx_cfg.num_queues; i++) {
56 snprintf(s, ETH_GSTRING_LEN, "rx_desc_cnt[%u]", i);
58 snprintf(s, ETH_GSTRING_LEN, "rx_desc_fill_cnt[%u]", i);
61 for (i = 0; i < priv->tx_cfg.num_queues; i++) {
62 snprintf(s, ETH_GSTRING_LEN, "tx_req[%u]", i);
64 snprintf(s, ETH_GSTRING_LEN, "tx_done[%u]", i);
66 snprintf(s, ETH_GSTRING_LEN, "tx_wake[%u]", i);
68 snprintf(s, ETH_GSTRING_LEN, "tx_stop[%u]", i);
70 snprintf(s, ETH_GSTRING_LEN, "tx_event_counter[%u]", i);
75 static int gve_get_sset_count(struct net_device *netdev, int sset)
77 struct gve_priv *priv = netdev_priv(netdev);
81 return GVE_MAIN_STATS_LEN +
82 (priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS) +
83 (priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS);
90 gve_get_ethtool_stats(struct net_device *netdev,
91 struct ethtool_stats *stats, u64 *data)
93 struct gve_priv *priv = netdev_priv(netdev);
94 u64 rx_pkts, rx_bytes, tx_pkts, tx_bytes;
101 for (rx_pkts = 0, rx_bytes = 0, ring = 0;
102 ring < priv->rx_cfg.num_queues; ring++) {
106 u64_stats_fetch_begin(&priv->rx[ring].statss);
107 rx_pkts += priv->rx[ring].rpackets;
108 rx_bytes += priv->rx[ring].rbytes;
109 } while (u64_stats_fetch_retry(&priv->rx[ring].statss,
113 for (tx_pkts = 0, tx_bytes = 0, ring = 0;
114 ring < priv->tx_cfg.num_queues; ring++) {
118 u64_stats_fetch_begin(&priv->tx[ring].statss);
119 tx_pkts += priv->tx[ring].pkt_done;
120 tx_bytes += priv->tx[ring].bytes_done;
121 } while (u64_stats_fetch_retry(&priv->tx[ring].statss,
129 data[i++] = rx_bytes;
130 data[i++] = tx_bytes;
131 /* Skip rx_dropped and tx_dropped */
133 data[i++] = priv->tx_timeo_cnt;
134 i = GVE_MAIN_STATS_LEN;
138 for (ring = 0; ring < priv->rx_cfg.num_queues; ring++) {
139 struct gve_rx_ring *rx = &priv->rx[ring];
142 data[i++] = rx->fill_cnt;
145 i += priv->rx_cfg.num_queues * NUM_GVE_RX_CNTS;
149 for (ring = 0; ring < priv->tx_cfg.num_queues; ring++) {
150 struct gve_tx_ring *tx = &priv->tx[ring];
153 data[i++] = tx->done;
154 data[i++] = tx->wake_queue;
155 data[i++] = tx->stop_queue;
156 data[i++] = be32_to_cpu(gve_tx_load_event_counter(priv,
160 i += priv->tx_cfg.num_queues * NUM_GVE_TX_CNTS;
164 static void gve_get_channels(struct net_device *netdev,
165 struct ethtool_channels *cmd)
167 struct gve_priv *priv = netdev_priv(netdev);
169 cmd->max_rx = priv->rx_cfg.max_queues;
170 cmd->max_tx = priv->tx_cfg.max_queues;
172 cmd->max_combined = 0;
173 cmd->rx_count = priv->rx_cfg.num_queues;
174 cmd->tx_count = priv->tx_cfg.num_queues;
175 cmd->other_count = 0;
176 cmd->combined_count = 0;
179 static int gve_set_channels(struct net_device *netdev,
180 struct ethtool_channels *cmd)
182 struct gve_priv *priv = netdev_priv(netdev);
183 struct gve_queue_config new_tx_cfg = priv->tx_cfg;
184 struct gve_queue_config new_rx_cfg = priv->rx_cfg;
185 struct ethtool_channels old_settings;
186 int new_tx = cmd->tx_count;
187 int new_rx = cmd->rx_count;
189 gve_get_channels(netdev, &old_settings);
191 /* Changing combined is not allowed allowed */
192 if (cmd->combined_count != old_settings.combined_count)
195 if (!new_rx || !new_tx)
198 if (!netif_carrier_ok(netdev)) {
199 priv->tx_cfg.num_queues = new_tx;
200 priv->rx_cfg.num_queues = new_rx;
204 new_tx_cfg.num_queues = new_tx;
205 new_rx_cfg.num_queues = new_rx;
207 return gve_adjust_queues(priv, new_rx_cfg, new_tx_cfg);
210 static void gve_get_ringparam(struct net_device *netdev,
211 struct ethtool_ringparam *cmd)
213 struct gve_priv *priv = netdev_priv(netdev);
215 cmd->rx_max_pending = priv->rx_desc_cnt;
216 cmd->tx_max_pending = priv->tx_desc_cnt;
217 cmd->rx_pending = priv->rx_desc_cnt;
218 cmd->tx_pending = priv->tx_desc_cnt;
221 static int gve_user_reset(struct net_device *netdev, u32 *flags)
223 struct gve_priv *priv = netdev_priv(netdev);
225 if (*flags == ETH_RESET_ALL) {
227 return gve_reset(priv, true);
233 static int gve_get_tunable(struct net_device *netdev,
234 const struct ethtool_tunable *etuna, void *value)
236 struct gve_priv *priv = netdev_priv(netdev);
239 case ETHTOOL_RX_COPYBREAK:
240 *(u32 *)value = priv->rx_copybreak;
247 static int gve_set_tunable(struct net_device *netdev,
248 const struct ethtool_tunable *etuna, const void *value)
250 struct gve_priv *priv = netdev_priv(netdev);
254 case ETHTOOL_RX_COPYBREAK:
256 if (len > PAGE_SIZE / 2)
258 priv->rx_copybreak = len;
265 const struct ethtool_ops gve_ethtool_ops = {
266 .get_drvinfo = gve_get_drvinfo,
267 .get_strings = gve_get_strings,
268 .get_sset_count = gve_get_sset_count,
269 .get_ethtool_stats = gve_get_ethtool_stats,
270 .set_msglevel = gve_set_msglevel,
271 .get_msglevel = gve_get_msglevel,
272 .set_channels = gve_set_channels,
273 .get_channels = gve_get_channels,
274 .get_link = ethtool_op_get_link,
275 .get_ringparam = gve_get_ringparam,
276 .reset = gve_user_reset,
277 .get_tunable = gve_get_tunable,
278 .set_tunable = gve_set_tunable,