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 * 8;
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);
53 sprintf(p, "rx_%d_xdp_redirect", i);
57 for (i = 0; i < num_queues; i++) {
58 sprintf(p, "tx_%d_packets", i);
60 sprintf(p, "tx_%d_bytes", i);
62 sprintf(p, "tx_%d_xdp_xmit", i);
67 static void mana_get_ethtool_stats(struct net_device *ndev,
68 struct ethtool_stats *e_stats, u64 *data)
70 struct mana_port_context *apc = netdev_priv(ndev);
71 unsigned int num_queues = apc->num_queues;
72 void *eth_stats = &apc->eth_stats;
73 struct mana_stats_rx *rx_stats;
74 struct mana_stats_tx *tx_stats;
86 for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++)
87 data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset);
89 for (q = 0; q < num_queues; q++) {
90 rx_stats = &apc->rxqs[q]->stats;
93 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
94 packets = rx_stats->packets;
95 bytes = rx_stats->bytes;
96 xdp_drop = rx_stats->xdp_drop;
97 xdp_tx = rx_stats->xdp_tx;
98 xdp_redirect = rx_stats->xdp_redirect;
99 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
103 data[i++] = xdp_drop;
105 data[i++] = xdp_redirect;
108 for (q = 0; q < num_queues; q++) {
109 tx_stats = &apc->tx_qp[q].txq.stats;
112 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
113 packets = tx_stats->packets;
114 bytes = tx_stats->bytes;
115 xdp_xmit = tx_stats->xdp_xmit;
116 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
120 data[i++] = xdp_xmit;
124 static int mana_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *cmd,
127 struct mana_port_context *apc = netdev_priv(ndev);
130 case ETHTOOL_GRXRINGS:
131 cmd->data = apc->num_queues;
138 static u32 mana_get_rxfh_key_size(struct net_device *ndev)
140 return MANA_HASH_KEY_SIZE;
143 static u32 mana_rss_indir_size(struct net_device *ndev)
145 return MANA_INDIRECT_TABLE_SIZE;
148 static int mana_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
151 struct mana_port_context *apc = netdev_priv(ndev);
155 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
158 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
159 indir[i] = apc->indir_table[i];
163 memcpy(key, apc->hashkey, MANA_HASH_KEY_SIZE);
168 static int mana_set_rxfh(struct net_device *ndev, const u32 *indir,
169 const u8 *key, const u8 hfunc)
171 struct mana_port_context *apc = netdev_priv(ndev);
172 bool update_hash = false, update_table = false;
173 u32 save_table[MANA_INDIRECT_TABLE_SIZE];
174 u8 save_key[MANA_HASH_KEY_SIZE];
177 if (!apc->port_is_up)
180 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
184 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
185 if (indir[i] >= apc->num_queues)
189 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
190 save_table[i] = apc->indir_table[i];
191 apc->indir_table[i] = indir[i];
197 memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE);
198 memcpy(apc->hashkey, key, MANA_HASH_KEY_SIZE);
201 err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
203 if (err) { /* recover to original values */
205 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
206 apc->indir_table[i] = save_table[i];
210 memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE);
212 mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
218 static void mana_get_channels(struct net_device *ndev,
219 struct ethtool_channels *channel)
221 struct mana_port_context *apc = netdev_priv(ndev);
223 channel->max_combined = apc->max_queues;
224 channel->combined_count = apc->num_queues;
227 static int mana_set_channels(struct net_device *ndev,
228 struct ethtool_channels *channels)
230 struct mana_port_context *apc = netdev_priv(ndev);
231 unsigned int new_count = channels->combined_count;
232 unsigned int old_count = apc->num_queues;
235 err = mana_detach(ndev, false);
237 netdev_err(ndev, "mana_detach failed: %d\n", err);
241 apc->num_queues = new_count;
242 err = mana_attach(ndev);
246 netdev_err(ndev, "mana_attach failed: %d\n", err);
248 /* Try to roll it back to the old configuration. */
249 apc->num_queues = old_count;
250 err2 = mana_attach(ndev);
252 netdev_err(ndev, "mana re-attach failed: %d\n", err2);
257 const struct ethtool_ops mana_ethtool_ops = {
258 .get_ethtool_stats = mana_get_ethtool_stats,
259 .get_sset_count = mana_get_sset_count,
260 .get_strings = mana_get_strings,
261 .get_rxnfc = mana_get_rxnfc,
262 .get_rxfh_key_size = mana_get_rxfh_key_size,
263 .get_rxfh_indir_size = mana_rss_indir_size,
264 .get_rxfh = mana_get_rxfh,
265 .set_rxfh = mana_set_rxfh,
266 .get_channels = mana_get_channels,
267 .set_channels = mana_set_channels,