1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright (c) 2021, Microsoft Corporation. */ 3 4 #include <linux/inetdevice.h> 5 #include <linux/etherdevice.h> 6 #include <linux/ethtool.h> 7 8 #include "mana.h" 9 10 static const struct { 11 char name[ETH_GSTRING_LEN]; 12 u16 offset; 13 } mana_eth_stats[] = { 14 {"stop_queue", offsetof(struct mana_ethtool_stats, stop_queue)}, 15 {"wake_queue", offsetof(struct mana_ethtool_stats, wake_queue)}, 16 }; 17 18 static int mana_get_sset_count(struct net_device *ndev, int stringset) 19 { 20 struct mana_port_context *apc = netdev_priv(ndev); 21 unsigned int num_queues = apc->num_queues; 22 23 if (stringset != ETH_SS_STATS) 24 return -EINVAL; 25 26 return ARRAY_SIZE(mana_eth_stats) + num_queues * 4; 27 } 28 29 static void mana_get_strings(struct net_device *ndev, u32 stringset, u8 *data) 30 { 31 struct mana_port_context *apc = netdev_priv(ndev); 32 unsigned int num_queues = apc->num_queues; 33 u8 *p = data; 34 int i; 35 36 if (stringset != ETH_SS_STATS) 37 return; 38 39 for (i = 0; i < ARRAY_SIZE(mana_eth_stats); i++) { 40 memcpy(p, mana_eth_stats[i].name, ETH_GSTRING_LEN); 41 p += ETH_GSTRING_LEN; 42 } 43 44 for (i = 0; i < num_queues; i++) { 45 sprintf(p, "rx_%d_packets", i); 46 p += ETH_GSTRING_LEN; 47 sprintf(p, "rx_%d_bytes", i); 48 p += ETH_GSTRING_LEN; 49 } 50 51 for (i = 0; i < num_queues; i++) { 52 sprintf(p, "tx_%d_packets", i); 53 p += ETH_GSTRING_LEN; 54 sprintf(p, "tx_%d_bytes", i); 55 p += ETH_GSTRING_LEN; 56 } 57 } 58 59 static void mana_get_ethtool_stats(struct net_device *ndev, 60 struct ethtool_stats *e_stats, u64 *data) 61 { 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; 66 unsigned int start; 67 u64 packets, bytes; 68 int q, i = 0; 69 70 if (!apc->port_is_up) 71 return; 72 73 for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++) 74 data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset); 75 76 for (q = 0; q < num_queues; q++) { 77 stats = &apc->rxqs[q]->stats; 78 79 do { 80 start = u64_stats_fetch_begin_irq(&stats->syncp); 81 packets = stats->packets; 82 bytes = stats->bytes; 83 } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); 84 85 data[i++] = packets; 86 data[i++] = bytes; 87 } 88 89 for (q = 0; q < num_queues; q++) { 90 stats = &apc->tx_qp[q].txq.stats; 91 92 do { 93 start = u64_stats_fetch_begin_irq(&stats->syncp); 94 packets = stats->packets; 95 bytes = stats->bytes; 96 } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); 97 98 data[i++] = packets; 99 data[i++] = bytes; 100 } 101 } 102 103 static int mana_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *cmd, 104 u32 *rules) 105 { 106 struct mana_port_context *apc = netdev_priv(ndev); 107 108 switch (cmd->cmd) { 109 case ETHTOOL_GRXRINGS: 110 cmd->data = apc->num_queues; 111 return 0; 112 } 113 114 return -EOPNOTSUPP; 115 } 116 117 static u32 mana_get_rxfh_key_size(struct net_device *ndev) 118 { 119 return MANA_HASH_KEY_SIZE; 120 } 121 122 static u32 mana_rss_indir_size(struct net_device *ndev) 123 { 124 return MANA_INDIRECT_TABLE_SIZE; 125 } 126 127 static int mana_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key, 128 u8 *hfunc) 129 { 130 struct mana_port_context *apc = netdev_priv(ndev); 131 int i; 132 133 if (hfunc) 134 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ 135 136 if (indir) { 137 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) 138 indir[i] = apc->indir_table[i]; 139 } 140 141 if (key) 142 memcpy(key, apc->hashkey, MANA_HASH_KEY_SIZE); 143 144 return 0; 145 } 146 147 static int mana_set_rxfh(struct net_device *ndev, const u32 *indir, 148 const u8 *key, const u8 hfunc) 149 { 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]; 154 int i, err; 155 156 if (!apc->port_is_up) 157 return -EOPNOTSUPP; 158 159 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) 160 return -EOPNOTSUPP; 161 162 if (indir) { 163 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) 164 if (indir[i] >= apc->num_queues) 165 return -EINVAL; 166 167 update_table = true; 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]; 171 } 172 } 173 174 if (key) { 175 update_hash = true; 176 memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE); 177 memcpy(apc->hashkey, key, MANA_HASH_KEY_SIZE); 178 } 179 180 err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table); 181 182 if (err) { /* recover to original values */ 183 if (update_table) { 184 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) 185 apc->indir_table[i] = save_table[i]; 186 } 187 188 if (update_hash) 189 memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE); 190 191 mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table); 192 } 193 194 return err; 195 } 196 197 static void mana_get_channels(struct net_device *ndev, 198 struct ethtool_channels *channel) 199 { 200 struct mana_port_context *apc = netdev_priv(ndev); 201 202 channel->max_combined = apc->max_queues; 203 channel->combined_count = apc->num_queues; 204 } 205 206 static int mana_set_channels(struct net_device *ndev, 207 struct ethtool_channels *channels) 208 { 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; 212 int err, err2; 213 214 err = mana_detach(ndev, false); 215 if (err) { 216 netdev_err(ndev, "mana_detach failed: %d\n", err); 217 return err; 218 } 219 220 apc->num_queues = new_count; 221 err = mana_attach(ndev); 222 if (!err) 223 return 0; 224 225 netdev_err(ndev, "mana_attach failed: %d\n", err); 226 227 /* Try to roll it back to the old configuration. */ 228 apc->num_queues = old_count; 229 err2 = mana_attach(ndev); 230 if (err2) 231 netdev_err(ndev, "mana re-attach failed: %d\n", err2); 232 233 return err; 234 } 235 236 const struct ethtool_ops mana_ethtool_ops = { 237 .get_ethtool_stats = mana_get_ethtool_stats, 238 .get_sset_count = mana_get_sset_count, 239 .get_strings = mana_get_strings, 240 .get_rxnfc = mana_get_rxnfc, 241 .get_rxfh_key_size = mana_get_rxfh_key_size, 242 .get_rxfh_indir_size = mana_rss_indir_size, 243 .get_rxfh = mana_get_rxfh, 244 .set_rxfh = mana_set_rxfh, 245 .get_channels = mana_get_channels, 246 .set_channels = mana_set_channels, 247 }; 248