2 * Handling of a master device, switching frames via its switch fabric CPU port
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
15 static void dsa_master_get_ethtool_stats(struct net_device *dev,
16 struct ethtool_stats *stats,
19 struct dsa_switch_tree *dst = dev->dsa_ptr;
20 struct dsa_port *cpu_dp = dst->cpu_dp;
21 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
22 struct dsa_switch *ds = cpu_dp->ds;
23 int port = cpu_dp->index;
26 if (ops && ops->get_sset_count && ops->get_ethtool_stats) {
27 count = ops->get_sset_count(dev, ETH_SS_STATS);
28 ops->get_ethtool_stats(dev, stats, data);
31 if (ds->ops->get_ethtool_stats)
32 ds->ops->get_ethtool_stats(ds, port, data + count);
35 static int dsa_master_get_sset_count(struct net_device *dev, int sset)
37 struct dsa_switch_tree *dst = dev->dsa_ptr;
38 struct dsa_port *cpu_dp = dst->cpu_dp;
39 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
40 struct dsa_switch *ds = cpu_dp->ds;
43 if (ops && ops->get_sset_count)
44 count += ops->get_sset_count(dev, sset);
46 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
47 count += ds->ops->get_sset_count(ds);
52 static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
55 struct dsa_switch_tree *dst = dev->dsa_ptr;
56 struct dsa_port *cpu_dp = dst->cpu_dp;
57 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
58 struct dsa_switch *ds = cpu_dp->ds;
59 int port = cpu_dp->index;
60 int len = ETH_GSTRING_LEN;
61 int mcount = 0, count;
66 snprintf(pfx, sizeof(pfx), "p%.2d", port);
67 /* We do not want to be NULL-terminated, since this is a prefix */
68 pfx[sizeof(pfx) - 1] = '_';
70 if (ops && ops->get_sset_count && ops->get_strings) {
71 mcount = ops->get_sset_count(dev, ETH_SS_STATS);
72 ops->get_strings(dev, stringset, data);
75 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
76 ndata = data + mcount * len;
77 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
78 * the output after to prepend our CPU port prefix we
81 ds->ops->get_strings(ds, port, ndata);
82 count = ds->ops->get_sset_count(ds);
83 for (i = 0; i < count; i++) {
84 memmove(ndata + (i * len + sizeof(pfx)),
85 ndata + i * len, len - sizeof(pfx));
86 memcpy(ndata + i * len, pfx, sizeof(pfx));
91 int dsa_master_ethtool_setup(struct net_device *dev)
93 struct dsa_switch_tree *dst = dev->dsa_ptr;
94 struct dsa_port *cpu_dp = dst->cpu_dp;
95 struct dsa_switch *ds = cpu_dp->ds;
96 struct ethtool_ops *ops;
98 ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
102 cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
103 if (cpu_dp->orig_ethtool_ops)
104 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
106 ops->get_sset_count = dsa_master_get_sset_count;
107 ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
108 ops->get_strings = dsa_master_get_strings;
110 dev->ethtool_ops = ops;
115 void dsa_master_ethtool_restore(struct net_device *dev)
117 struct dsa_switch_tree *dst = dev->dsa_ptr;
118 struct dsa_port *cpu_dp = dst->cpu_dp;
120 dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
121 cpu_dp->orig_ethtool_ops = NULL;