blob: 8d27687fd0ca78b6171dfcbd1df2e27ff0a78efe [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{
Vivien Didelot2f657a62017-09-29 17:19:20 -040019 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040020 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
21 struct dsa_switch *ds = cpu_dp->ds;
22 int port = cpu_dp->index;
Vivien Didelotf2f23562017-09-19 11:57:00 -040023 int count = 0;
24
Florian Fainelli1d1e79f2018-04-25 12:12:49 -070025 if (ops->get_sset_count && ops->get_ethtool_stats) {
Vivien Didelotf2f23562017-09-19 11:57:00 -040026 count = ops->get_sset_count(dev, ETH_SS_STATS);
27 ops->get_ethtool_stats(dev, stats, data);
28 }
29
30 if (ds->ops->get_ethtool_stats)
Vivien Didelot7ec764e2017-09-29 17:19:16 -040031 ds->ops->get_ethtool_stats(ds, port, data + count);
Vivien Didelotf2f23562017-09-19 11:57:00 -040032}
33
34static int dsa_master_get_sset_count(struct net_device *dev, int sset)
35{
Vivien Didelot2f657a62017-09-29 17:19:20 -040036 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040037 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
38 struct dsa_switch *ds = cpu_dp->ds;
Vivien Didelotf2f23562017-09-19 11:57:00 -040039 int count = 0;
40
Florian Fainelli89f09042018-04-25 12:12:50 -070041 if (ops->get_sset_count) {
42 count = ops->get_sset_count(dev, sset);
43 if (count < 0)
44 count = 0;
45 }
Vivien Didelotf2f23562017-09-19 11:57:00 -040046
Florian Fainelli89f09042018-04-25 12:12:50 -070047 if (ds->ops->get_sset_count)
48 count += ds->ops->get_sset_count(ds, cpu_dp->index, sset);
Vivien Didelotf2f23562017-09-19 11:57:00 -040049
50 return count;
51}
52
53static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
54 uint8_t *data)
55{
Vivien Didelot2f657a62017-09-29 17:19:20 -040056 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040057 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
Florian Fainelli1d1e79f2018-04-25 12:12:49 -070070 if (ops->get_sset_count && ops->get_strings) {
Florian Fainelli89f09042018-04-25 12:12:50 -070071 mcount = ops->get_sset_count(dev, stringset);
72 if (mcount < 0)
73 mcount = 0;
Vivien Didelotf2f23562017-09-19 11:57:00 -040074 ops->get_strings(dev, stringset, data);
75 }
76
Florian Fainelli89f09042018-04-25 12:12:50 -070077 if (ds->ops->get_strings) {
Vivien Didelotf2f23562017-09-19 11:57:00 -040078 ndata = data + mcount * len;
79 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
80 * the output after to prepend our CPU port prefix we
81 * constructed earlier
82 */
Florian Fainelli89f09042018-04-25 12:12:50 -070083 ds->ops->get_strings(ds, port, stringset, ndata);
84 count = ds->ops->get_sset_count(ds, port, stringset);
Vivien Didelotf2f23562017-09-19 11:57:00 -040085 for (i = 0; i < count; i++) {
86 memmove(ndata + (i * len + sizeof(pfx)),
87 ndata + i * len, len - sizeof(pfx));
88 memcpy(ndata + i * len, pfx, sizeof(pfx));
89 }
90 }
91}
92
Vivien Didelot17a22fc2017-11-06 16:11:45 -050093static int dsa_master_ethtool_setup(struct net_device *dev)
Vivien Didelotf2f23562017-09-19 11:57:00 -040094{
Vivien Didelot2f657a62017-09-29 17:19:20 -040095 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040096 struct dsa_switch *ds = cpu_dp->ds;
Vivien Didelotf2f23562017-09-19 11:57:00 -040097 struct ethtool_ops *ops;
98
99 ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
100 if (!ops)
101 return -ENOMEM;
102
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400103 cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
104 if (cpu_dp->orig_ethtool_ops)
105 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
Vivien Didelotf2f23562017-09-19 11:57:00 -0400106
107 ops->get_sset_count = dsa_master_get_sset_count;
108 ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
109 ops->get_strings = dsa_master_get_strings;
110
111 dev->ethtool_ops = ops;
112
113 return 0;
114}
115
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500116static void dsa_master_ethtool_teardown(struct net_device *dev)
Vivien Didelotf2f23562017-09-19 11:57:00 -0400117{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400118 struct dsa_port *cpu_dp = dev->dsa_ptr;
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}
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500123
124int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
125{
126 /* If we use a tagging format that doesn't have an ethertype
127 * field, make sure that all packets from this point on get
128 * sent to the tag format's receive function.
129 */
130 wmb();
131
132 dev->dsa_ptr = cpu_dp;
133
134 return dsa_master_ethtool_setup(dev);
135}
136
137void dsa_master_teardown(struct net_device *dev)
138{
139 dsa_master_ethtool_teardown(dev);
140
141 dev->dsa_ptr = NULL;
142
143 /* If we used a tagging format that doesn't have an ethertype
144 * field, make sure that all packets from this point get sent
145 * without the tag and go through the regular receive path.
146 */
147 wmb();
148}