blob: ef15d35f1574bfd9bd72da4b5fb7191949752adf [file] [log] [blame]
Vivien Didelotf2f23562017-09-19 11:57:00 -04001/*
2 * Handling of a master device, switching frames via its switch fabric CPU port
3 *
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6 *
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.
11 */
12
13#include "dsa_priv.h"
14
15static void dsa_master_get_ethtool_stats(struct net_device *dev,
16 struct ethtool_stats *stats,
17 uint64_t *data)
18{
19 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040020 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;
Vivien Didelotf2f23562017-09-19 11:57:00 -040024 int count = 0;
25
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);
29 }
30
31 if (ds->ops->get_ethtool_stats)
Vivien Didelot7ec764e2017-09-29 17:19:16 -040032 ds->ops->get_ethtool_stats(ds, port, data + count);
Vivien Didelotf2f23562017-09-19 11:57:00 -040033}
34
35static int dsa_master_get_sset_count(struct net_device *dev, int sset)
36{
37 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040038 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;
Vivien Didelotf2f23562017-09-19 11:57:00 -040041 int count = 0;
42
43 if (ops && ops->get_sset_count)
44 count += ops->get_sset_count(dev, sset);
45
46 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
47 count += ds->ops->get_sset_count(ds);
48
49 return count;
50}
51
52static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
53 uint8_t *data)
54{
55 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040056 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;
Vivien Didelotf2f23562017-09-19 11:57:00 -040060 int len = ETH_GSTRING_LEN;
61 int mcount = 0, count;
62 unsigned int i;
63 uint8_t pfx[4];
64 uint8_t *ndata;
65
Vivien Didelot7ec764e2017-09-29 17:19:16 -040066 snprintf(pfx, sizeof(pfx), "p%.2d", port);
Vivien Didelotf2f23562017-09-19 11:57:00 -040067 /* We do not want to be NULL-terminated, since this is a prefix */
68 pfx[sizeof(pfx) - 1] = '_';
69
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);
73 }
74
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
79 * constructed earlier
80 */
Vivien Didelot7ec764e2017-09-29 17:19:16 -040081 ds->ops->get_strings(ds, port, ndata);
Vivien Didelotf2f23562017-09-19 11:57:00 -040082 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));
87 }
88 }
89}
90
91int dsa_master_ethtool_setup(struct net_device *dev)
92{
93 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040094 struct dsa_port *cpu_dp = dst->cpu_dp;
95 struct dsa_switch *ds = cpu_dp->ds;
Vivien Didelotf2f23562017-09-19 11:57:00 -040096 struct ethtool_ops *ops;
97
98 ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
99 if (!ops)
100 return -ENOMEM;
101
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400102 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));
Vivien Didelotf2f23562017-09-19 11:57:00 -0400105
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;
109
110 dev->ethtool_ops = ops;
111
112 return 0;
113}
114
115void dsa_master_ethtool_restore(struct net_device *dev)
116{
117 struct dsa_switch_tree *dst = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400118 struct dsa_port *cpu_dp = dst->cpu_dp;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400119
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400120 dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
121 cpu_dp->orig_ethtool_ops = NULL;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400122}