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_port *cpu_dp = dev->dsa_ptr;
20 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
21 struct dsa_switch *ds = cpu_dp->ds;
22 int port = cpu_dp->index;
25 if (ops->get_sset_count && ops->get_ethtool_stats) {
26 count = ops->get_sset_count(dev, ETH_SS_STATS);
27 ops->get_ethtool_stats(dev, stats, data);
30 if (ds->ops->get_ethtool_stats)
31 ds->ops->get_ethtool_stats(ds, port, data + count);
34 static void dsa_master_get_ethtool_phy_stats(struct net_device *dev,
35 struct ethtool_stats *stats,
38 struct dsa_port *cpu_dp = dev->dsa_ptr;
39 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
40 struct dsa_switch *ds = cpu_dp->ds;
41 int port = cpu_dp->index;
44 if (dev->phydev && !ops->get_ethtool_phy_stats) {
45 count = phy_ethtool_get_sset_count(dev->phydev);
47 phy_ethtool_get_stats(dev->phydev, stats, data);
48 } else if (ops->get_sset_count && ops->get_ethtool_phy_stats) {
49 count = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
50 ops->get_ethtool_phy_stats(dev, stats, data);
56 if (ds->ops->get_ethtool_phy_stats)
57 ds->ops->get_ethtool_phy_stats(ds, port, data + count);
60 static int dsa_master_get_sset_count(struct net_device *dev, int sset)
62 struct dsa_port *cpu_dp = dev->dsa_ptr;
63 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
64 struct dsa_switch *ds = cpu_dp->ds;
67 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
68 !ops->get_ethtool_phy_stats)
69 count = phy_ethtool_get_sset_count(dev->phydev);
70 else if (ops->get_sset_count)
71 count = ops->get_sset_count(dev, sset);
76 if (ds->ops->get_sset_count)
77 count += ds->ops->get_sset_count(ds, cpu_dp->index, sset);
82 static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
85 struct dsa_port *cpu_dp = dev->dsa_ptr;
86 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
87 struct dsa_switch *ds = cpu_dp->ds;
88 int port = cpu_dp->index;
89 int len = ETH_GSTRING_LEN;
90 int mcount = 0, count, i;
94 snprintf(pfx, sizeof(pfx), "p%.2d", port);
95 /* We do not want to be NULL-terminated, since this is a prefix */
96 pfx[sizeof(pfx) - 1] = '_';
98 if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
99 !ops->get_ethtool_phy_stats) {
100 mcount = phy_ethtool_get_sset_count(dev->phydev);
104 phy_ethtool_get_strings(dev->phydev, data);
105 } else if (ops->get_sset_count && ops->get_strings) {
106 mcount = ops->get_sset_count(dev, stringset);
109 ops->get_strings(dev, stringset, data);
112 if (ds->ops->get_strings) {
113 ndata = data + mcount * len;
114 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
115 * the output after to prepend our CPU port prefix we
116 * constructed earlier
118 ds->ops->get_strings(ds, port, stringset, ndata);
119 count = ds->ops->get_sset_count(ds, port, stringset);
122 for (i = 0; i < count; i++) {
123 memmove(ndata + (i * len + sizeof(pfx)),
124 ndata + i * len, len - sizeof(pfx));
125 memcpy(ndata + i * len, pfx, sizeof(pfx));
130 static int dsa_master_ethtool_setup(struct net_device *dev)
132 struct dsa_port *cpu_dp = dev->dsa_ptr;
133 struct dsa_switch *ds = cpu_dp->ds;
134 struct ethtool_ops *ops;
136 ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
140 cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
141 if (cpu_dp->orig_ethtool_ops)
142 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
144 ops->get_sset_count = dsa_master_get_sset_count;
145 ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
146 ops->get_strings = dsa_master_get_strings;
147 ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats;
149 dev->ethtool_ops = ops;
154 static void dsa_master_ethtool_teardown(struct net_device *dev)
156 struct dsa_port *cpu_dp = dev->dsa_ptr;
158 dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
159 cpu_dp->orig_ethtool_ops = NULL;
162 static struct lock_class_key dsa_master_addr_list_lock_key;
164 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
166 /* If we use a tagging format that doesn't have an ethertype
167 * field, make sure that all packets from this point on get
168 * sent to the tag format's receive function.
172 dev->dsa_ptr = cpu_dp;
173 lockdep_set_class(&dev->addr_list_lock,
174 &dsa_master_addr_list_lock_key);
176 return dsa_master_ethtool_setup(dev);
179 void dsa_master_teardown(struct net_device *dev)
181 dsa_master_ethtool_teardown(dev);
185 /* If we used a tagging format that doesn't have an ethertype
186 * field, make sure that all packets from this point get sent
187 * without the tag and go through the regular receive path.