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 <net/mana/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 {"tx_cqes", offsetof(struct mana_ethtool_stats, tx_cqes)}, 17 {"tx_cq_err", offsetof(struct mana_ethtool_stats, tx_cqe_err)}, 18 {"tx_cqe_unknown_type", offsetof(struct mana_ethtool_stats, 19 tx_cqe_unknown_type)}, 20 {"rx_cqes", offsetof(struct mana_ethtool_stats, rx_cqes)}, 21 {"rx_coalesced_err", offsetof(struct mana_ethtool_stats, 22 rx_coalesced_err)}, 23 {"rx_cqe_unknown_type", offsetof(struct mana_ethtool_stats, 24 rx_cqe_unknown_type)}, 25 }; 26 27 static int mana_get_sset_count(struct net_device *ndev, int stringset) 28 { 29 struct mana_port_context *apc = netdev_priv(ndev); 30 unsigned int num_queues = apc->num_queues; 31 32 if (stringset != ETH_SS_STATS) 33 return -EINVAL; 34 35 return ARRAY_SIZE(mana_eth_stats) + num_queues * 36 (MANA_STATS_RX_COUNT + MANA_STATS_TX_COUNT); 37 } 38 39 static void mana_get_strings(struct net_device *ndev, u32 stringset, u8 *data) 40 { 41 struct mana_port_context *apc = netdev_priv(ndev); 42 unsigned int num_queues = apc->num_queues; 43 u8 *p = data; 44 int i; 45 46 if (stringset != ETH_SS_STATS) 47 return; 48 49 for (i = 0; i < ARRAY_SIZE(mana_eth_stats); i++) { 50 memcpy(p, mana_eth_stats[i].name, ETH_GSTRING_LEN); 51 p += ETH_GSTRING_LEN; 52 } 53 54 for (i = 0; i < num_queues; i++) { 55 sprintf(p, "rx_%d_packets", i); 56 p += ETH_GSTRING_LEN; 57 sprintf(p, "rx_%d_bytes", i); 58 p += ETH_GSTRING_LEN; 59 sprintf(p, "rx_%d_xdp_drop", i); 60 p += ETH_GSTRING_LEN; 61 sprintf(p, "rx_%d_xdp_tx", i); 62 p += ETH_GSTRING_LEN; 63 sprintf(p, "rx_%d_xdp_redirect", i); 64 p += ETH_GSTRING_LEN; 65 } 66 67 for (i = 0; i < num_queues; i++) { 68 sprintf(p, "tx_%d_packets", i); 69 p += ETH_GSTRING_LEN; 70 sprintf(p, "tx_%d_bytes", i); 71 p += ETH_GSTRING_LEN; 72 sprintf(p, "tx_%d_xdp_xmit", i); 73 p += ETH_GSTRING_LEN; 74 sprintf(p, "tx_%d_tso_packets", i); 75 p += ETH_GSTRING_LEN; 76 sprintf(p, "tx_%d_tso_bytes", i); 77 p += ETH_GSTRING_LEN; 78 sprintf(p, "tx_%d_tso_inner_packets", i); 79 p += ETH_GSTRING_LEN; 80 sprintf(p, "tx_%d_tso_inner_bytes", i); 81 p += ETH_GSTRING_LEN; 82 sprintf(p, "tx_%d_long_pkt_fmt", i); 83 p += ETH_GSTRING_LEN; 84 sprintf(p, "tx_%d_short_pkt_fmt", i); 85 p += ETH_GSTRING_LEN; 86 sprintf(p, "tx_%d_csum_partial", i); 87 p += ETH_GSTRING_LEN; 88 sprintf(p, "tx_%d_mana_map_err", i); 89 p += ETH_GSTRING_LEN; 90 } 91 } 92 93 static void mana_get_ethtool_stats(struct net_device *ndev, 94 struct ethtool_stats *e_stats, u64 *data) 95 { 96 struct mana_port_context *apc = netdev_priv(ndev); 97 unsigned int num_queues = apc->num_queues; 98 void *eth_stats = &apc->eth_stats; 99 struct mana_stats_rx *rx_stats; 100 struct mana_stats_tx *tx_stats; 101 unsigned int start; 102 u64 packets, bytes; 103 u64 xdp_redirect; 104 u64 xdp_xmit; 105 u64 xdp_drop; 106 u64 xdp_tx; 107 u64 tso_packets; 108 u64 tso_bytes; 109 u64 tso_inner_packets; 110 u64 tso_inner_bytes; 111 u64 long_pkt_fmt; 112 u64 short_pkt_fmt; 113 u64 csum_partial; 114 u64 mana_map_err; 115 int q, i = 0; 116 117 if (!apc->port_is_up) 118 return; 119 120 for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++) 121 data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset); 122 123 for (q = 0; q < num_queues; q++) { 124 rx_stats = &apc->rxqs[q]->stats; 125 126 do { 127 start = u64_stats_fetch_begin(&rx_stats->syncp); 128 packets = rx_stats->packets; 129 bytes = rx_stats->bytes; 130 xdp_drop = rx_stats->xdp_drop; 131 xdp_tx = rx_stats->xdp_tx; 132 xdp_redirect = rx_stats->xdp_redirect; 133 } while (u64_stats_fetch_retry(&rx_stats->syncp, start)); 134 135 data[i++] = packets; 136 data[i++] = bytes; 137 data[i++] = xdp_drop; 138 data[i++] = xdp_tx; 139 data[i++] = xdp_redirect; 140 } 141 142 for (q = 0; q < num_queues; q++) { 143 tx_stats = &apc->tx_qp[q].txq.stats; 144 145 do { 146 start = u64_stats_fetch_begin(&tx_stats->syncp); 147 packets = tx_stats->packets; 148 bytes = tx_stats->bytes; 149 xdp_xmit = tx_stats->xdp_xmit; 150 tso_packets = tx_stats->tso_packets; 151 tso_bytes = tx_stats->tso_bytes; 152 tso_inner_packets = tx_stats->tso_inner_packets; 153 tso_inner_bytes = tx_stats->tso_inner_bytes; 154 long_pkt_fmt = tx_stats->long_pkt_fmt; 155 short_pkt_fmt = tx_stats->short_pkt_fmt; 156 csum_partial = tx_stats->csum_partial; 157 mana_map_err = tx_stats->mana_map_err; 158 } while (u64_stats_fetch_retry(&tx_stats->syncp, start)); 159 160 data[i++] = packets; 161 data[i++] = bytes; 162 data[i++] = xdp_xmit; 163 data[i++] = tso_packets; 164 data[i++] = tso_bytes; 165 data[i++] = tso_inner_packets; 166 data[i++] = tso_inner_bytes; 167 data[i++] = long_pkt_fmt; 168 data[i++] = short_pkt_fmt; 169 data[i++] = csum_partial; 170 data[i++] = mana_map_err; 171 } 172 } 173 174 static int mana_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *cmd, 175 u32 *rules) 176 { 177 struct mana_port_context *apc = netdev_priv(ndev); 178 179 switch (cmd->cmd) { 180 case ETHTOOL_GRXRINGS: 181 cmd->data = apc->num_queues; 182 return 0; 183 } 184 185 return -EOPNOTSUPP; 186 } 187 188 static u32 mana_get_rxfh_key_size(struct net_device *ndev) 189 { 190 return MANA_HASH_KEY_SIZE; 191 } 192 193 static u32 mana_rss_indir_size(struct net_device *ndev) 194 { 195 return MANA_INDIRECT_TABLE_SIZE; 196 } 197 198 static int mana_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key, 199 u8 *hfunc) 200 { 201 struct mana_port_context *apc = netdev_priv(ndev); 202 int i; 203 204 if (hfunc) 205 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ 206 207 if (indir) { 208 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) 209 indir[i] = apc->indir_table[i]; 210 } 211 212 if (key) 213 memcpy(key, apc->hashkey, MANA_HASH_KEY_SIZE); 214 215 return 0; 216 } 217 218 static int mana_set_rxfh(struct net_device *ndev, const u32 *indir, 219 const u8 *key, const u8 hfunc) 220 { 221 struct mana_port_context *apc = netdev_priv(ndev); 222 bool update_hash = false, update_table = false; 223 u32 save_table[MANA_INDIRECT_TABLE_SIZE]; 224 u8 save_key[MANA_HASH_KEY_SIZE]; 225 int i, err; 226 227 if (!apc->port_is_up) 228 return -EOPNOTSUPP; 229 230 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) 231 return -EOPNOTSUPP; 232 233 if (indir) { 234 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) 235 if (indir[i] >= apc->num_queues) 236 return -EINVAL; 237 238 update_table = true; 239 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) { 240 save_table[i] = apc->indir_table[i]; 241 apc->indir_table[i] = indir[i]; 242 } 243 } 244 245 if (key) { 246 update_hash = true; 247 memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE); 248 memcpy(apc->hashkey, key, MANA_HASH_KEY_SIZE); 249 } 250 251 err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table); 252 253 if (err) { /* recover to original values */ 254 if (update_table) { 255 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) 256 apc->indir_table[i] = save_table[i]; 257 } 258 259 if (update_hash) 260 memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE); 261 262 mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table); 263 } 264 265 return err; 266 } 267 268 static void mana_get_channels(struct net_device *ndev, 269 struct ethtool_channels *channel) 270 { 271 struct mana_port_context *apc = netdev_priv(ndev); 272 273 channel->max_combined = apc->max_queues; 274 channel->combined_count = apc->num_queues; 275 } 276 277 static int mana_set_channels(struct net_device *ndev, 278 struct ethtool_channels *channels) 279 { 280 struct mana_port_context *apc = netdev_priv(ndev); 281 unsigned int new_count = channels->combined_count; 282 unsigned int old_count = apc->num_queues; 283 int err, err2; 284 285 err = mana_detach(ndev, false); 286 if (err) { 287 netdev_err(ndev, "mana_detach failed: %d\n", err); 288 return err; 289 } 290 291 apc->num_queues = new_count; 292 err = mana_attach(ndev); 293 if (!err) 294 return 0; 295 296 netdev_err(ndev, "mana_attach failed: %d\n", err); 297 298 /* Try to roll it back to the old configuration. */ 299 apc->num_queues = old_count; 300 err2 = mana_attach(ndev); 301 if (err2) 302 netdev_err(ndev, "mana re-attach failed: %d\n", err2); 303 304 return err; 305 } 306 307 const struct ethtool_ops mana_ethtool_ops = { 308 .get_ethtool_stats = mana_get_ethtool_stats, 309 .get_sset_count = mana_get_sset_count, 310 .get_strings = mana_get_strings, 311 .get_rxnfc = mana_get_rxnfc, 312 .get_rxfh_key_size = mana_get_rxfh_key_size, 313 .get_rxfh_indir_size = mana_rss_indir_size, 314 .get_rxfh = mana_get_rxfh, 315 .set_rxfh = mana_set_rxfh, 316 .get_channels = mana_get_channels, 317 .set_channels = mana_set_channels, 318 }; 319