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 * 4;
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);
51 for (i = 0; i < num_queues; i++) {
52 sprintf(p, "tx_%d_packets", i);
54 sprintf(p, "tx_%d_bytes", i);
59 static void mana_get_ethtool_stats(struct net_device *ndev,
60 struct ethtool_stats *e_stats, u64 *data)
62 struct mana_port_context *apc = netdev_priv(ndev);
63 unsigned int num_queues = apc->num_queues;
64 void *eth_stats = &apc->eth_stats;
65 struct mana_stats *stats;
73 for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++)
74 data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset);
76 for (q = 0; q < num_queues; q++) {
77 stats = &apc->rxqs[q]->stats;
80 start = u64_stats_fetch_begin_irq(&stats->syncp);
81 packets = stats->packets;
83 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
89 for (q = 0; q < num_queues; q++) {
90 stats = &apc->tx_qp[q].txq.stats;
93 start = u64_stats_fetch_begin_irq(&stats->syncp);
94 packets = stats->packets;
96 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
103 static int mana_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *cmd,
106 struct mana_port_context *apc = netdev_priv(ndev);
109 case ETHTOOL_GRXRINGS:
110 cmd->data = apc->num_queues;
117 static u32 mana_get_rxfh_key_size(struct net_device *ndev)
119 return MANA_HASH_KEY_SIZE;
122 static u32 mana_rss_indir_size(struct net_device *ndev)
124 return MANA_INDIRECT_TABLE_SIZE;
127 static int mana_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
130 struct mana_port_context *apc = netdev_priv(ndev);
134 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
137 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
138 indir[i] = apc->indir_table[i];
142 memcpy(key, apc->hashkey, MANA_HASH_KEY_SIZE);
147 static int mana_set_rxfh(struct net_device *ndev, const u32 *indir,
148 const u8 *key, const u8 hfunc)
150 struct mana_port_context *apc = netdev_priv(ndev);
151 bool update_hash = false, update_table = false;
152 u32 save_table[MANA_INDIRECT_TABLE_SIZE];
153 u8 save_key[MANA_HASH_KEY_SIZE];
156 if (!apc->port_is_up)
159 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
163 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
164 if (indir[i] >= apc->num_queues)
168 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
169 save_table[i] = apc->indir_table[i];
170 apc->indir_table[i] = indir[i];
176 memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE);
177 memcpy(apc->hashkey, key, MANA_HASH_KEY_SIZE);
180 err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
182 if (err) { /* recover to original values */
184 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
185 apc->indir_table[i] = save_table[i];
189 memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE);
191 mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
197 static void mana_get_channels(struct net_device *ndev,
198 struct ethtool_channels *channel)
200 struct mana_port_context *apc = netdev_priv(ndev);
202 channel->max_combined = apc->max_queues;
203 channel->combined_count = apc->num_queues;
206 static int mana_set_channels(struct net_device *ndev,
207 struct ethtool_channels *channels)
209 struct mana_port_context *apc = netdev_priv(ndev);
210 unsigned int new_count = channels->combined_count;
211 unsigned int old_count = apc->num_queues;
214 if (!apc->port_is_up)
217 err = mana_detach(ndev, false);
219 netdev_err(ndev, "mana_detach failed: %d\n", err);
223 apc->num_queues = new_count;
224 err = mana_attach(ndev);
228 netdev_err(ndev, "mana_attach failed: %d\n", err);
230 /* Try to roll it back to the old configuration. */
231 apc->num_queues = old_count;
232 err2 = mana_attach(ndev);
234 netdev_err(ndev, "mana re-attach failed: %d\n", err2);
239 const struct ethtool_ops mana_ethtool_ops = {
240 .get_ethtool_stats = mana_get_ethtool_stats,
241 .get_sset_count = mana_get_sset_count,
242 .get_strings = mana_get_strings,
243 .get_rxnfc = mana_get_rxnfc,
244 .get_rxfh_key_size = mana_get_rxfh_key_size,
245 .get_rxfh_indir_size = mana_rss_indir_size,
246 .get_rxfh = mana_get_rxfh,
247 .set_rxfh = mana_set_rxfh,
248 .get_channels = mana_get_channels,
249 .set_channels = mana_set_channels,