fm10k: add helper functions to set strings and data for ethtool stats
authorJacob Keller <jacob.e.keller@intel.com>
Fri, 5 Feb 2016 18:43:08 +0000 (10:43 -0800)
committerJeff Kirsher <jeffrey.t.kirsher@intel.com>
Tue, 5 Apr 2016 19:49:27 +0000 (12:49 -0700)
Reduce duplicate code and the amount of indentation by adding
fm10k_add_stat_strings and fm10k_add_ethtool_stats functions which help
add fm10k_stat structures to the ethtool stats callbacks. This helps
increase ease of use for future stat additions, and increases code
readability. Skip handling of the per-queue stats as these will be
reworked in a following patch.

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Tested-by: Krishneil Singh <Krishneil.k.singh@intel.com>
Signed-off-by: Jeff Kirsher <jeffrey.t.kirsher@intel.com>
drivers/net/ethernet/intel/fm10k/fm10k_ethtool.c

index 28837ae099dffed68276754917f61fbad5b71852..c67121cc7b235a215f2b068d81aea08014ce558e 100644 (file)
@@ -153,57 +153,51 @@ static const char fm10k_prv_flags[FM10K_PRV_FLAG_LEN][ETH_GSTRING_LEN] = {
        "debug-statistics",
 };
 
+static void fm10k_add_stat_strings(char **p, const char *prefix,
+                                  const struct fm10k_stats stats[],
+                                  const unsigned int size)
+{
+       unsigned int i;
+
+       for (i = 0; i < size; i++) {
+               snprintf(*p, ETH_GSTRING_LEN, "%s%s",
+                        prefix, stats[i].stat_string);
+               *p += ETH_GSTRING_LEN;
+       }
+}
+
 static void fm10k_get_stat_strings(struct net_device *dev, u8 *data)
 {
        struct fm10k_intfc *interface = netdev_priv(dev);
        struct fm10k_iov_data *iov_data = interface->iov_data;
        char *p = (char *)data;
        unsigned int i;
-       unsigned int j;
 
-       for (i = 0; i < FM10K_NETDEV_STATS_LEN; i++) {
-               memcpy(p, fm10k_gstrings_net_stats[i].stat_string,
-                      ETH_GSTRING_LEN);
-               p += ETH_GSTRING_LEN;
-       }
+       fm10k_add_stat_strings(&p, "", fm10k_gstrings_net_stats,
+                              FM10K_NETDEV_STATS_LEN);
 
-       for (i = 0; i < FM10K_GLOBAL_STATS_LEN; i++) {
-               memcpy(p, fm10k_gstrings_global_stats[i].stat_string,
-                      ETH_GSTRING_LEN);
-               p += ETH_GSTRING_LEN;
-       }
+       fm10k_add_stat_strings(&p, "", fm10k_gstrings_global_stats,
+                              FM10K_GLOBAL_STATS_LEN);
 
-       if (interface->flags & FM10K_FLAG_DEBUG_STATS) {
-               for (i = 0; i < FM10K_DEBUG_STATS_LEN; i++) {
-                       memcpy(p, fm10k_gstrings_debug_stats[i].stat_string,
-                              ETH_GSTRING_LEN);
-                       p += ETH_GSTRING_LEN;
-               }
-       }
+       if (interface->flags & FM10K_FLAG_DEBUG_STATS)
+               fm10k_add_stat_strings(&p, "", fm10k_gstrings_debug_stats,
+                                      FM10K_DEBUG_STATS_LEN);
 
-       for (i = 0; i < FM10K_MBX_STATS_LEN; i++) {
-               memcpy(p, fm10k_gstrings_mbx_stats[i].stat_string,
-                      ETH_GSTRING_LEN);
-               p += ETH_GSTRING_LEN;
-       }
+       fm10k_add_stat_strings(&p, "", fm10k_gstrings_mbx_stats,
+                              FM10K_MBX_STATS_LEN);
 
-       if (interface->hw.mac.type != fm10k_mac_vf) {
-               for (i = 0; i < FM10K_PF_STATS_LEN; i++) {
-                       memcpy(p, fm10k_gstrings_pf_stats[i].stat_string,
-                              ETH_GSTRING_LEN);
-                       p += ETH_GSTRING_LEN;
-               }
-       }
+       if (interface->hw.mac.type != fm10k_mac_vf)
+               fm10k_add_stat_strings(&p, "", fm10k_gstrings_pf_stats,
+                                      FM10K_PF_STATS_LEN);
 
        if ((interface->flags & FM10K_FLAG_DEBUG_STATS) && iov_data) {
                for (i = 0; i < iov_data->num_vfs; i++) {
-                       for (j = 0; j < FM10K_MBX_STATS_LEN; j++) {
-                               snprintf(p,
-                                        ETH_GSTRING_LEN,
-                                        "vf_%u_%s", i,
-                                        fm10k_gstrings_mbx_stats[j].stat_string);
-                               p += ETH_GSTRING_LEN;
-                       }
+                       char prefix[ETH_GSTRING_LEN];
+
+                       snprintf(prefix, ETH_GSTRING_LEN, "vf_%u_", i);
+                       fm10k_add_stat_strings(&p, prefix,
+                                              fm10k_gstrings_mbx_stats,
+                                              FM10K_MBX_STATS_LEN);
                }
        }
 
@@ -271,6 +265,41 @@ static int fm10k_get_sset_count(struct net_device *dev, int sset)
        }
 }
 
+static void fm10k_add_ethtool_stats(u64 **data, void *pointer,
+                                   const struct fm10k_stats stats[],
+                                   const unsigned int size)
+{
+       unsigned int i;
+       char *p;
+
+       /* simply skip forward if we were not given a valid pointer */
+       if (!pointer) {
+               *data += size;
+               return;
+       }
+
+       for (i = 0; i < size; i++) {
+               p = (char *)pointer + stats[i].stat_offset;
+
+               switch (stats[i].sizeof_stat) {
+               case sizeof(u64):
+                       *((*data)++) = *(u64 *)p;
+                       break;
+               case sizeof(u32):
+                       *((*data)++) = *(u32 *)p;
+                       break;
+               case sizeof(u16):
+                       *((*data)++) = *(u16 *)p;
+                       break;
+               case sizeof(u8):
+                       *((*data)++) = *(u8 *)p;
+                       break;
+               default:
+                       *((*data)++) = 0;
+               }
+       }
+}
+
 static void fm10k_get_ethtool_stats(struct net_device *netdev,
                                    struct ethtool_stats __always_unused *stats,
                                    u64 *data)
@@ -279,47 +308,29 @@ static void fm10k_get_ethtool_stats(struct net_device *netdev,
        struct fm10k_intfc *interface = netdev_priv(netdev);
        struct fm10k_iov_data *iov_data = interface->iov_data;
        struct net_device_stats *net_stats = &netdev->stats;
-       char *p;
        int i, j;
 
        fm10k_update_stats(interface);
 
-       for (i = 0; i < FM10K_NETDEV_STATS_LEN; i++) {
-               p = (char *)net_stats + fm10k_gstrings_net_stats[i].stat_offset;
-               *(data++) = (fm10k_gstrings_net_stats[i].sizeof_stat ==
-                       sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
-       }
+       fm10k_add_ethtool_stats(&data, net_stats, fm10k_gstrings_net_stats,
+                               FM10K_NETDEV_STATS_LEN);
 
-       for (i = 0; i < FM10K_GLOBAL_STATS_LEN; i++) {
-               p = (char *)interface +
-                   fm10k_gstrings_global_stats[i].stat_offset;
-               *(data++) = (fm10k_gstrings_global_stats[i].sizeof_stat ==
-                       sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
-       }
+       fm10k_add_ethtool_stats(&data, interface, fm10k_gstrings_global_stats,
+                               FM10K_GLOBAL_STATS_LEN);
 
-       if (interface->flags & FM10K_FLAG_DEBUG_STATS) {
-               for (i = 0; i < FM10K_DEBUG_STATS_LEN; i++) {
-                       p = (char *)interface +
-                               fm10k_gstrings_debug_stats[i].stat_offset;
-                       *(data++) = (fm10k_gstrings_debug_stats[i].sizeof_stat ==
-                                    sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
-               }
-       }
+       if (interface->flags & FM10K_FLAG_DEBUG_STATS)
+               fm10k_add_ethtool_stats(&data, interface,
+                                       fm10k_gstrings_debug_stats,
+                                       FM10K_DEBUG_STATS_LEN);
 
-       for (i = 0; i < FM10K_MBX_STATS_LEN; i++) {
-               p = (char *)&interface->hw.mbx +
-                       fm10k_gstrings_mbx_stats[i].stat_offset;
-               *(data++) = (fm10k_gstrings_mbx_stats[i].sizeof_stat ==
-                       sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
-       }
+       fm10k_add_ethtool_stats(&data, &interface->hw.mbx,
+                               fm10k_gstrings_mbx_stats,
+                               FM10K_MBX_STATS_LEN);
 
        if (interface->hw.mac.type != fm10k_mac_vf) {
-               for (i = 0; i < FM10K_PF_STATS_LEN; i++) {
-                       p = (char *)interface +
-                           fm10k_gstrings_pf_stats[i].stat_offset;
-                       *(data++) = (fm10k_gstrings_pf_stats[i].sizeof_stat ==
-                                    sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
-               }
+               fm10k_add_ethtool_stats(&data, interface,
+                                       fm10k_gstrings_pf_stats,
+                                       FM10K_PF_STATS_LEN);
        }
 
        if ((interface->flags & FM10K_FLAG_DEBUG_STATS) && iov_data) {
@@ -328,18 +339,9 @@ static void fm10k_get_ethtool_stats(struct net_device *netdev,
 
                        vf_info = &iov_data->vf_info[i];
 
-                       /* skip stats if we don't have a vf info */
-                       if (!vf_info) {
-                               data += FM10K_MBX_STATS_LEN;
-                               continue;
-                       }
-
-                       for (j = 0; j < FM10K_MBX_STATS_LEN; j++) {
-                               p = (char *)&vf_info->mbx +
-                                       fm10k_gstrings_mbx_stats[j].stat_offset;
-                               *(data++) = (fm10k_gstrings_mbx_stats[j].sizeof_stat ==
-                                            sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
-                       }
+                       fm10k_add_ethtool_stats(&data, &vf_info->mbx,
+                                               fm10k_gstrings_mbx_stats,
+                                               FM10K_MBX_STATS_LEN);
                }
        }