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 { 192f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 207ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 217ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 227ec764eeSVivien Didelot int port = cpu_dp->index; 23f2f23566SVivien Didelot int count = 0; 24f2f23566SVivien Didelot 251d1e79f1SFlorian Fainelli if (ops->get_sset_count && ops->get_ethtool_stats) { 26f2f23566SVivien Didelot count = ops->get_sset_count(dev, ETH_SS_STATS); 27f2f23566SVivien Didelot ops->get_ethtool_stats(dev, stats, data); 28f2f23566SVivien Didelot } 29f2f23566SVivien Didelot 30f2f23566SVivien Didelot if (ds->ops->get_ethtool_stats) 317ec764eeSVivien Didelot ds->ops->get_ethtool_stats(ds, port, data + count); 32f2f23566SVivien Didelot } 33f2f23566SVivien Didelot 34f2f23566SVivien Didelot static int dsa_master_get_sset_count(struct net_device *dev, int sset) 35f2f23566SVivien Didelot { 362f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 377ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 387ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 39f2f23566SVivien Didelot int count = 0; 40f2f23566SVivien Didelot 41*89f09048SFlorian Fainelli if (ops->get_sset_count) { 42*89f09048SFlorian Fainelli count = ops->get_sset_count(dev, sset); 43*89f09048SFlorian Fainelli if (count < 0) 44*89f09048SFlorian Fainelli count = 0; 45*89f09048SFlorian Fainelli } 46f2f23566SVivien Didelot 47*89f09048SFlorian Fainelli if (ds->ops->get_sset_count) 48*89f09048SFlorian Fainelli count += ds->ops->get_sset_count(ds, cpu_dp->index, sset); 49f2f23566SVivien Didelot 50f2f23566SVivien Didelot return count; 51f2f23566SVivien Didelot } 52f2f23566SVivien Didelot 53f2f23566SVivien Didelot static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset, 54f2f23566SVivien Didelot uint8_t *data) 55f2f23566SVivien Didelot { 562f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 577ec764eeSVivien Didelot const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 587ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 597ec764eeSVivien 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 667ec764eeSVivien 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 701d1e79f1SFlorian Fainelli if (ops->get_sset_count && ops->get_strings) { 71*89f09048SFlorian Fainelli mcount = ops->get_sset_count(dev, stringset); 72*89f09048SFlorian Fainelli if (mcount < 0) 73*89f09048SFlorian Fainelli mcount = 0; 74f2f23566SVivien Didelot ops->get_strings(dev, stringset, data); 75f2f23566SVivien Didelot } 76f2f23566SVivien Didelot 77*89f09048SFlorian Fainelli if (ds->ops->get_strings) { 78f2f23566SVivien Didelot ndata = data + mcount * len; 79f2f23566SVivien Didelot /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle 80f2f23566SVivien Didelot * the output after to prepend our CPU port prefix we 81f2f23566SVivien Didelot * constructed earlier 82f2f23566SVivien Didelot */ 83*89f09048SFlorian Fainelli ds->ops->get_strings(ds, port, stringset, ndata); 84*89f09048SFlorian Fainelli count = ds->ops->get_sset_count(ds, port, stringset); 85f2f23566SVivien Didelot for (i = 0; i < count; i++) { 86f2f23566SVivien Didelot memmove(ndata + (i * len + sizeof(pfx)), 87f2f23566SVivien Didelot ndata + i * len, len - sizeof(pfx)); 88f2f23566SVivien Didelot memcpy(ndata + i * len, pfx, sizeof(pfx)); 89f2f23566SVivien Didelot } 90f2f23566SVivien Didelot } 91f2f23566SVivien Didelot } 92f2f23566SVivien Didelot 9317a22fcfSVivien Didelot static int dsa_master_ethtool_setup(struct net_device *dev) 94f2f23566SVivien Didelot { 952f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 967ec764eeSVivien Didelot struct dsa_switch *ds = cpu_dp->ds; 97f2f23566SVivien Didelot struct ethtool_ops *ops; 98f2f23566SVivien Didelot 99f2f23566SVivien Didelot ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL); 100f2f23566SVivien Didelot if (!ops) 101f2f23566SVivien Didelot return -ENOMEM; 102f2f23566SVivien Didelot 1037ec764eeSVivien Didelot cpu_dp->orig_ethtool_ops = dev->ethtool_ops; 1047ec764eeSVivien Didelot if (cpu_dp->orig_ethtool_ops) 1057ec764eeSVivien Didelot memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops)); 106f2f23566SVivien Didelot 107f2f23566SVivien Didelot ops->get_sset_count = dsa_master_get_sset_count; 108f2f23566SVivien Didelot ops->get_ethtool_stats = dsa_master_get_ethtool_stats; 109f2f23566SVivien Didelot ops->get_strings = dsa_master_get_strings; 110f2f23566SVivien Didelot 111f2f23566SVivien Didelot dev->ethtool_ops = ops; 112f2f23566SVivien Didelot 113f2f23566SVivien Didelot return 0; 114f2f23566SVivien Didelot } 115f2f23566SVivien Didelot 11617a22fcfSVivien Didelot static void dsa_master_ethtool_teardown(struct net_device *dev) 117f2f23566SVivien Didelot { 1182f657a60SVivien Didelot struct dsa_port *cpu_dp = dev->dsa_ptr; 119f2f23566SVivien Didelot 1207ec764eeSVivien Didelot dev->ethtool_ops = cpu_dp->orig_ethtool_ops; 1217ec764eeSVivien Didelot cpu_dp->orig_ethtool_ops = NULL; 122f2f23566SVivien Didelot } 12317a22fcfSVivien Didelot 12417a22fcfSVivien Didelot int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp) 12517a22fcfSVivien Didelot { 12617a22fcfSVivien Didelot /* If we use a tagging format that doesn't have an ethertype 12717a22fcfSVivien Didelot * field, make sure that all packets from this point on get 12817a22fcfSVivien Didelot * sent to the tag format's receive function. 12917a22fcfSVivien Didelot */ 13017a22fcfSVivien Didelot wmb(); 13117a22fcfSVivien Didelot 13217a22fcfSVivien Didelot dev->dsa_ptr = cpu_dp; 13317a22fcfSVivien Didelot 13417a22fcfSVivien Didelot return dsa_master_ethtool_setup(dev); 13517a22fcfSVivien Didelot } 13617a22fcfSVivien Didelot 13717a22fcfSVivien Didelot void dsa_master_teardown(struct net_device *dev) 13817a22fcfSVivien Didelot { 13917a22fcfSVivien Didelot dsa_master_ethtool_teardown(dev); 14017a22fcfSVivien Didelot 14117a22fcfSVivien Didelot dev->dsa_ptr = NULL; 14217a22fcfSVivien Didelot 14317a22fcfSVivien Didelot /* If we used a tagging format that doesn't have an ethertype 14417a22fcfSVivien Didelot * field, make sure that all packets from this point get sent 14517a22fcfSVivien Didelot * without the tag and go through the regular receive path. 14617a22fcfSVivien Didelot */ 14717a22fcfSVivien Didelot wmb(); 14817a22fcfSVivien Didelot } 149