1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2021, Microsoft Corporation. */
4 #include <linux/inetdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ethtool.h>
11 char name[ETH_GSTRING_LEN];
13 } mana_eth_stats[] = {
14 {"stop_queue", offsetof(struct mana_ethtool_stats, stop_queue)},
15 {"wake_queue", offsetof(struct mana_ethtool_stats, wake_queue)},
18 static int mana_get_sset_count(struct net_device *ndev, int stringset)
20 struct mana_port_context *apc = netdev_priv(ndev);
21 unsigned int num_queues = apc->num_queues;
23 if (stringset != ETH_SS_STATS)
26 return ARRAY_SIZE(mana_eth_stats) + num_queues * 6;
29 static void mana_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
31 struct mana_port_context *apc = netdev_priv(ndev);
32 unsigned int num_queues = apc->num_queues;
36 if (stringset != ETH_SS_STATS)
39 for (i = 0; i < ARRAY_SIZE(mana_eth_stats); i++) {
40 memcpy(p, mana_eth_stats[i].name, ETH_GSTRING_LEN);
44 for (i = 0; i < num_queues; i++) {
45 sprintf(p, "rx_%d_packets", i);
47 sprintf(p, "rx_%d_bytes", i);
49 sprintf(p, "rx_%d_xdp_drop", i);
51 sprintf(p, "rx_%d_xdp_tx", i);
55 for (i = 0; i < num_queues; i++) {
56 sprintf(p, "tx_%d_packets", i);
58 sprintf(p, "tx_%d_bytes", i);
63 static void mana_get_ethtool_stats(struct net_device *ndev,
64 struct ethtool_stats *e_stats, u64 *data)
66 struct mana_port_context *apc = netdev_priv(ndev);
67 unsigned int num_queues = apc->num_queues;
68 void *eth_stats = &apc->eth_stats;
69 struct mana_stats_rx *rx_stats;
70 struct mana_stats_tx *tx_stats;
80 for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++)
81 data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset);
83 for (q = 0; q < num_queues; q++) {
84 rx_stats = &apc->rxqs[q]->stats;
87 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
88 packets = rx_stats->packets;
89 bytes = rx_stats->bytes;
90 xdp_drop = rx_stats->xdp_drop;
91 xdp_tx = rx_stats->xdp_tx;
92 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
100 for (q = 0; q < num_queues; q++) {
101 tx_stats = &apc->tx_qp[q].txq.stats;
104 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
105 packets = tx_stats->packets;
106 bytes = tx_stats->bytes;
107 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
114 static int mana_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *cmd,
117 struct mana_port_context *apc = netdev_priv(ndev);
120 case ETHTOOL_GRXRINGS:
121 cmd->data = apc->num_queues;
128 static u32 mana_get_rxfh_key_size(struct net_device *ndev)
130 return MANA_HASH_KEY_SIZE;
133 static u32 mana_rss_indir_size(struct net_device *ndev)
135 return MANA_INDIRECT_TABLE_SIZE;
138 static int mana_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
141 struct mana_port_context *apc = netdev_priv(ndev);
145 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
148 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
149 indir[i] = apc->indir_table[i];
153 memcpy(key, apc->hashkey, MANA_HASH_KEY_SIZE);
158 static int mana_set_rxfh(struct net_device *ndev, const u32 *indir,
159 const u8 *key, const u8 hfunc)
161 struct mana_port_context *apc = netdev_priv(ndev);
162 bool update_hash = false, update_table = false;
163 u32 save_table[MANA_INDIRECT_TABLE_SIZE];
164 u8 save_key[MANA_HASH_KEY_SIZE];
167 if (!apc->port_is_up)
170 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
174 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
175 if (indir[i] >= apc->num_queues)
179 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
180 save_table[i] = apc->indir_table[i];
181 apc->indir_table[i] = indir[i];
187 memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE);
188 memcpy(apc->hashkey, key, MANA_HASH_KEY_SIZE);
191 err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
193 if (err) { /* recover to original values */
195 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
196 apc->indir_table[i] = save_table[i];
200 memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE);
202 mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
208 static void mana_get_channels(struct net_device *ndev,
209 struct ethtool_channels *channel)
211 struct mana_port_context *apc = netdev_priv(ndev);
213 channel->max_combined = apc->max_queues;
214 channel->combined_count = apc->num_queues;
217 static int mana_set_channels(struct net_device *ndev,
218 struct ethtool_channels *channels)
220 struct mana_port_context *apc = netdev_priv(ndev);
221 unsigned int new_count = channels->combined_count;
222 unsigned int old_count = apc->num_queues;
225 err = mana_detach(ndev, false);
227 netdev_err(ndev, "mana_detach failed: %d\n", err);
231 apc->num_queues = new_count;
232 err = mana_attach(ndev);
236 netdev_err(ndev, "mana_attach failed: %d\n", err);
238 /* Try to roll it back to the old configuration. */
239 apc->num_queues = old_count;
240 err2 = mana_attach(ndev);
242 netdev_err(ndev, "mana re-attach failed: %d\n", err2);
247 const struct ethtool_ops mana_ethtool_ops = {
248 .get_ethtool_stats = mana_get_ethtool_stats,
249 .get_sset_count = mana_get_sset_count,
250 .get_strings = mana_get_strings,
251 .get_rxnfc = mana_get_rxnfc,
252 .get_rxfh_key_size = mana_get_rxfh_key_size,
253 .get_rxfh_indir_size = mana_rss_indir_size,
254 .get_rxfh = mana_get_rxfh,
255 .set_rxfh = mana_set_rxfh,
256 .get_channels = mana_get_channels,
257 .set_channels = mana_set_channels,