1f2f23566SVivien Didelot /* 2f2f23566SVivien Didelot * Handling of a master device, switching frames via its switch fabric CPU port 3f2f23566SVivien Didelot * 4f2f23566SVivien Didelot * Copyright (c) 2017 Savoir-faire Linux Inc. 5f2f23566SVivien Didelot * Vivien Didelot <vivien.didelot@savoirfairelinux.com> 6f2f23566SVivien Didelot * 7f2f23566SVivien Didelot * This program is free software; you can redistribute it and/or modify 8f2f23566SVivien Didelot * it under the terms of the GNU General Public License as published by 9f2f23566SVivien Didelot * the Free Software Foundation; either version 2 of the License, or 10f2f23566SVivien Didelot * (at your option) any later version. 11f2f23566SVivien Didelot */ 12f2f23566SVivien Didelot 13f2f23566SVivien Didelot #include "dsa_priv.h" 14f2f23566SVivien Didelot 15f2f23566SVivien Didelot static void dsa_master_get_ethtool_stats(struct net_device *dev, 16f2f23566SVivien Didelot struct ethtool_stats *stats, 17f2f23566SVivien Didelot uint64_t *data) 18f2f23566SVivien Didelot { 19f2f23566SVivien Didelot struct dsa_switch_tree *dst = dev->dsa_ptr; 20*7ec764eeSVivien Didelot struct dsa_port *cpu_dp = dst->cpu_dp; 21*7ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 22*7ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 23*7ec764eeSVivien Didelot int port = cpu_dp->index; 24f2f23566SVivien Didelot int count = 0; 25f2f23566SVivien Didelot 26f2f23566SVivien Didelot if (ops && ops->get_sset_count && ops->get_ethtool_stats) { 27f2f23566SVivien Didelot count = ops->get_sset_count(dev, ETH_SS_STATS); 28f2f23566SVivien Didelot ops->get_ethtool_stats(dev, stats, data); 29f2f23566SVivien Didelot } 30f2f23566SVivien Didelot 31f2f23566SVivien Didelot if (ds->ops->get_ethtool_stats) 32*7ec764eeSVivien Didelot ds->ops->get_ethtool_stats(ds, port, data + count); 33f2f23566SVivien Didelot } 34f2f23566SVivien Didelot 35f2f23566SVivien Didelot static int dsa_master_get_sset_count(struct net_device *dev, int sset) 36f2f23566SVivien Didelot { 37f2f23566SVivien Didelot struct dsa_switch_tree *dst = dev->dsa_ptr; 38*7ec764eeSVivien Didelot struct dsa_port *cpu_dp = dst->cpu_dp; 39*7ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 40*7ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 41f2f23566SVivien Didelot int count = 0; 42f2f23566SVivien Didelot 43f2f23566SVivien Didelot if (ops && ops->get_sset_count) 44f2f23566SVivien Didelot count += ops->get_sset_count(dev, sset); 45f2f23566SVivien Didelot 46f2f23566SVivien Didelot if (sset == ETH_SS_STATS && ds->ops->get_sset_count) 47f2f23566SVivien Didelot count += ds->ops->get_sset_count(ds); 48f2f23566SVivien Didelot 49f2f23566SVivien Didelot return count; 50f2f23566SVivien Didelot } 51f2f23566SVivien Didelot 52f2f23566SVivien Didelot static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset, 53f2f23566SVivien Didelot uint8_t *data) 54f2f23566SVivien Didelot { 55f2f23566SVivien Didelot struct dsa_switch_tree *dst = dev->dsa_ptr; 56*7ec764eeSVivien Didelot struct dsa_port *cpu_dp = dst->cpu_dp; 57*7ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 58*7ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 59*7ec764eeSVivien Didelot int port = cpu_dp->index; 60f2f23566SVivien Didelot int len = ETH_GSTRING_LEN; 61f2f23566SVivien Didelot int mcount = 0, count; 62f2f23566SVivien Didelot unsigned int i; 63f2f23566SVivien Didelot uint8_t pfx[4]; 64f2f23566SVivien Didelot uint8_t *ndata; 65f2f23566SVivien Didelot 66*7ec764eeSVivien Didelot snprintf(pfx, sizeof(pfx), "p%.2d", port); 67f2f23566SVivien Didelot /* We do not want to be NULL-terminated, since this is a prefix */ 68f2f23566SVivien Didelot pfx[sizeof(pfx) - 1] = '_'; 69f2f23566SVivien Didelot 70f2f23566SVivien Didelot if (ops && ops->get_sset_count && ops->get_strings) { 71f2f23566SVivien Didelot mcount = ops->get_sset_count(dev, ETH_SS_STATS); 72f2f23566SVivien Didelot ops->get_strings(dev, stringset, data); 73f2f23566SVivien Didelot } 74f2f23566SVivien Didelot 75f2f23566SVivien Didelot if (stringset == ETH_SS_STATS && ds->ops->get_strings) { 76f2f23566SVivien Didelot ndata = data + mcount * len; 77f2f23566SVivien Didelot /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle 78f2f23566SVivien Didelot * the output after to prepend our CPU port prefix we 79f2f23566SVivien Didelot * constructed earlier 80f2f23566SVivien Didelot */ 81*7ec764eeSVivien Didelot ds->ops->get_strings(ds, port, ndata); 82f2f23566SVivien Didelot count = ds->ops->get_sset_count(ds); 83f2f23566SVivien Didelot for (i = 0; i < count; i++) { 84f2f23566SVivien Didelot memmove(ndata + (i * len + sizeof(pfx)), 85f2f23566SVivien Didelot ndata + i * len, len - sizeof(pfx)); 86f2f23566SVivien Didelot memcpy(ndata + i * len, pfx, sizeof(pfx)); 87f2f23566SVivien Didelot } 88f2f23566SVivien Didelot } 89f2f23566SVivien Didelot } 90f2f23566SVivien Didelot 91f2f23566SVivien Didelot int dsa_master_ethtool_setup(struct net_device *dev) 92f2f23566SVivien Didelot { 93f2f23566SVivien Didelot struct dsa_switch_tree *dst = dev->dsa_ptr; 94*7ec764eeSVivien Didelot struct dsa_port *cpu_dp = dst->cpu_dp; 95*7ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 96f2f23566SVivien Didelot struct ethtool_ops *ops; 97f2f23566SVivien Didelot 98f2f23566SVivien Didelot ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL); 99f2f23566SVivien Didelot if (!ops) 100f2f23566SVivien Didelot return -ENOMEM; 101f2f23566SVivien Didelot 102*7ec764eeSVivien Didelot cpu_dp->orig_ethtool_ops = dev->ethtool_ops; 103*7ec764eeSVivien Didelot if (cpu_dp->orig_ethtool_ops) 104*7ec764eeSVivien Didelot memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops)); 105f2f23566SVivien Didelot 106f2f23566SVivien Didelot ops->get_sset_count = dsa_master_get_sset_count; 107f2f23566SVivien Didelot ops->get_ethtool_stats = dsa_master_get_ethtool_stats; 108f2f23566SVivien Didelot ops->get_strings = dsa_master_get_strings; 109f2f23566SVivien Didelot 110f2f23566SVivien Didelot dev->ethtool_ops = ops; 111f2f23566SVivien Didelot 112f2f23566SVivien Didelot return 0; 113f2f23566SVivien Didelot } 114f2f23566SVivien Didelot 115f2f23566SVivien Didelot void dsa_master_ethtool_restore(struct net_device *dev) 116f2f23566SVivien Didelot { 117f2f23566SVivien Didelot struct dsa_switch_tree *dst = dev->dsa_ptr; 118*7ec764eeSVivien Didelot struct dsa_port *cpu_dp = dst->cpu_dp; 119f2f23566SVivien Didelot 120*7ec764eeSVivien Didelot dev->ethtool_ops = cpu_dp->orig_ethtool_ops; 121*7ec764eeSVivien Didelot cpu_dp->orig_ethtool_ops = NULL; 122f2f23566SVivien Didelot } 123