blob: 2199104ca7dfa15f943b24bb3dd52c4e181d33eb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vivien Didelotf2f23562017-09-19 11:57:00 -04002/*
3 * Handling of a master device, switching frames via its switch fabric CPU port
4 *
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelotf2f23562017-09-19 11:57:00 -04007 */
8
9#include "dsa_priv.h"
10
Vivien Didelot48e23312019-08-02 15:34:55 -040011static int dsa_master_get_regs_len(struct net_device *dev)
12{
13 struct dsa_port *cpu_dp = dev->dsa_ptr;
14 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
15 struct dsa_switch *ds = cpu_dp->ds;
16 int port = cpu_dp->index;
17 int ret = 0;
18 int len;
19
20 if (ops->get_regs_len) {
21 len = ops->get_regs_len(dev);
22 if (len < 0)
23 return len;
24 ret += len;
25 }
26
27 ret += sizeof(struct ethtool_drvinfo);
28 ret += sizeof(struct ethtool_regs);
29
30 if (ds->ops->get_regs_len) {
31 len = ds->ops->get_regs_len(ds, port);
32 if (len < 0)
33 return len;
34 ret += len;
35 }
36
37 return ret;
38}
39
40static void dsa_master_get_regs(struct net_device *dev,
41 struct ethtool_regs *regs, void *data)
42{
43 struct dsa_port *cpu_dp = dev->dsa_ptr;
44 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
45 struct dsa_switch *ds = cpu_dp->ds;
46 struct ethtool_drvinfo *cpu_info;
47 struct ethtool_regs *cpu_regs;
48 int port = cpu_dp->index;
49 int len;
50
51 if (ops->get_regs_len && ops->get_regs) {
52 len = ops->get_regs_len(dev);
53 if (len < 0)
54 return;
55 regs->len = len;
56 ops->get_regs(dev, regs, data);
57 data += regs->len;
58 }
59
60 cpu_info = (struct ethtool_drvinfo *)data;
61 strlcpy(cpu_info->driver, "dsa", sizeof(cpu_info->driver));
62 data += sizeof(*cpu_info);
63 cpu_regs = (struct ethtool_regs *)data;
64 data += sizeof(*cpu_regs);
65
66 if (ds->ops->get_regs_len && ds->ops->get_regs) {
67 len = ds->ops->get_regs_len(ds, port);
68 if (len < 0)
69 return;
70 cpu_regs->len = len;
71 ds->ops->get_regs(ds, port, cpu_regs, data);
72 }
73}
74
Vivien Didelotf2f23562017-09-19 11:57:00 -040075static void dsa_master_get_ethtool_stats(struct net_device *dev,
76 struct ethtool_stats *stats,
77 uint64_t *data)
78{
Vivien Didelot2f657a62017-09-29 17:19:20 -040079 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -040080 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
81 struct dsa_switch *ds = cpu_dp->ds;
82 int port = cpu_dp->index;
Vivien Didelotf2f23562017-09-19 11:57:00 -040083 int count = 0;
84
Florian Fainelli1d1e79f2018-04-25 12:12:49 -070085 if (ops->get_sset_count && ops->get_ethtool_stats) {
Vivien Didelotf2f23562017-09-19 11:57:00 -040086 count = ops->get_sset_count(dev, ETH_SS_STATS);
87 ops->get_ethtool_stats(dev, stats, data);
88 }
89
90 if (ds->ops->get_ethtool_stats)
Vivien Didelot7ec764e2017-09-29 17:19:16 -040091 ds->ops->get_ethtool_stats(ds, port, data + count);
Vivien Didelotf2f23562017-09-19 11:57:00 -040092}
93
Florian Fainellicf963572018-04-25 12:12:52 -070094static void dsa_master_get_ethtool_phy_stats(struct net_device *dev,
95 struct ethtool_stats *stats,
96 uint64_t *data)
97{
98 struct dsa_port *cpu_dp = dev->dsa_ptr;
99 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
100 struct dsa_switch *ds = cpu_dp->ds;
101 int port = cpu_dp->index;
102 int count = 0;
103
104 if (dev->phydev && !ops->get_ethtool_phy_stats) {
105 count = phy_ethtool_get_sset_count(dev->phydev);
106 if (count >= 0)
107 phy_ethtool_get_stats(dev->phydev, stats, data);
108 } else if (ops->get_sset_count && ops->get_ethtool_phy_stats) {
109 count = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
110 ops->get_ethtool_phy_stats(dev, stats, data);
111 }
112
113 if (count < 0)
114 count = 0;
115
116 if (ds->ops->get_ethtool_phy_stats)
117 ds->ops->get_ethtool_phy_stats(ds, port, data + count);
118}
119
Vivien Didelotf2f23562017-09-19 11:57:00 -0400120static int dsa_master_get_sset_count(struct net_device *dev, int sset)
121{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400122 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400123 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
124 struct dsa_switch *ds = cpu_dp->ds;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400125 int count = 0;
126
Florian Fainellicf963572018-04-25 12:12:52 -0700127 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
128 !ops->get_ethtool_phy_stats)
129 count = phy_ethtool_get_sset_count(dev->phydev);
130 else if (ops->get_sset_count)
Florian Fainelli89f09042018-04-25 12:12:50 -0700131 count = ops->get_sset_count(dev, sset);
Florian Fainellicf963572018-04-25 12:12:52 -0700132
133 if (count < 0)
134 count = 0;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400135
Florian Fainelli89f09042018-04-25 12:12:50 -0700136 if (ds->ops->get_sset_count)
137 count += ds->ops->get_sset_count(ds, cpu_dp->index, sset);
Vivien Didelotf2f23562017-09-19 11:57:00 -0400138
139 return count;
140}
141
142static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
143 uint8_t *data)
144{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400145 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400146 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
147 struct dsa_switch *ds = cpu_dp->ds;
148 int port = cpu_dp->index;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400149 int len = ETH_GSTRING_LEN;
Dan Carpentera2693332021-05-08 16:30:35 +0300150 int mcount = 0, count, i;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400151 uint8_t pfx[4];
152 uint8_t *ndata;
153
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400154 snprintf(pfx, sizeof(pfx), "p%.2d", port);
Vivien Didelotf2f23562017-09-19 11:57:00 -0400155 /* We do not want to be NULL-terminated, since this is a prefix */
156 pfx[sizeof(pfx) - 1] = '_';
157
Florian Fainellicf963572018-04-25 12:12:52 -0700158 if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
159 !ops->get_ethtool_phy_stats) {
160 mcount = phy_ethtool_get_sset_count(dev->phydev);
161 if (mcount < 0)
162 mcount = 0;
163 else
164 phy_ethtool_get_strings(dev->phydev, data);
165 } else if (ops->get_sset_count && ops->get_strings) {
Florian Fainelli89f09042018-04-25 12:12:50 -0700166 mcount = ops->get_sset_count(dev, stringset);
167 if (mcount < 0)
168 mcount = 0;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400169 ops->get_strings(dev, stringset, data);
170 }
171
Florian Fainelli89f09042018-04-25 12:12:50 -0700172 if (ds->ops->get_strings) {
Vivien Didelotf2f23562017-09-19 11:57:00 -0400173 ndata = data + mcount * len;
174 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
175 * the output after to prepend our CPU port prefix we
176 * constructed earlier
177 */
Florian Fainelli89f09042018-04-25 12:12:50 -0700178 ds->ops->get_strings(ds, port, stringset, ndata);
179 count = ds->ops->get_sset_count(ds, port, stringset);
Dan Carpentera2693332021-05-08 16:30:35 +0300180 if (count < 0)
181 return;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400182 for (i = 0; i < count; i++) {
183 memmove(ndata + (i * len + sizeof(pfx)),
184 ndata + i * len, len - sizeof(pfx));
185 memcpy(ndata + i * len, pfx, sizeof(pfx));
186 }
187 }
188}
189
Vladimir Olteanf685e602019-12-28 15:30:46 +0200190static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
191{
192 struct dsa_port *cpu_dp = dev->dsa_ptr;
193 struct dsa_switch *ds = cpu_dp->ds;
194 struct dsa_switch_tree *dst;
195 int err = -EOPNOTSUPP;
196 struct dsa_port *dp;
197
198 dst = ds->dst;
199
200 switch (cmd) {
201 case SIOCGHWTSTAMP:
202 case SIOCSHWTSTAMP:
203 /* Deny PTP operations on master if there is at least one
204 * switch in the tree that is PTP capable.
205 */
206 list_for_each_entry(dp, &dst->ports, list)
207 if (dp->ds->ops->port_hwtstamp_get ||
208 dp->ds->ops->port_hwtstamp_set)
209 return -EBUSY;
210 break;
211 }
212
Arnd Bergmanna7605372021-07-27 15:45:13 +0200213 if (dev->netdev_ops->ndo_eth_ioctl)
214 err = dev->netdev_ops->ndo_eth_ioctl(dev, ifr, cmd);
Vladimir Olteanf685e602019-12-28 15:30:46 +0200215
216 return err;
217}
218
Florian Fainelli9c0c7012020-07-19 20:49:54 -0700219static const struct dsa_netdevice_ops dsa_netdev_ops = {
Arnd Bergmanna7605372021-07-27 15:45:13 +0200220 .ndo_eth_ioctl = dsa_master_ioctl,
Florian Fainelli9c0c7012020-07-19 20:49:54 -0700221};
222
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500223static int dsa_master_ethtool_setup(struct net_device *dev)
Vivien Didelotf2f23562017-09-19 11:57:00 -0400224{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400225 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400226 struct dsa_switch *ds = cpu_dp->ds;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400227 struct ethtool_ops *ops;
228
229 ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
230 if (!ops)
231 return -ENOMEM;
232
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400233 cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
234 if (cpu_dp->orig_ethtool_ops)
235 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
Vivien Didelotf2f23562017-09-19 11:57:00 -0400236
Vivien Didelot48e23312019-08-02 15:34:55 -0400237 ops->get_regs_len = dsa_master_get_regs_len;
238 ops->get_regs = dsa_master_get_regs;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400239 ops->get_sset_count = dsa_master_get_sset_count;
240 ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
241 ops->get_strings = dsa_master_get_strings;
Florian Fainellicf963572018-04-25 12:12:52 -0700242 ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400243
244 dev->ethtool_ops = ops;
245
246 return 0;
247}
248
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500249static void dsa_master_ethtool_teardown(struct net_device *dev)
Vivien Didelotf2f23562017-09-19 11:57:00 -0400250{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400251 struct dsa_port *cpu_dp = dev->dsa_ptr;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400252
Vivien Didelot7ec764e2017-09-29 17:19:16 -0400253 dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
254 cpu_dp->orig_ethtool_ops = NULL;
Vivien Didelotf2f23562017-09-19 11:57:00 -0400255}
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500256
Florian Fainelli9c0c7012020-07-19 20:49:54 -0700257static void dsa_netdev_ops_set(struct net_device *dev,
258 const struct dsa_netdevice_ops *ops)
Florian Fainellida7b9e92019-01-15 14:43:04 -0800259{
Florian Fainelli9c0c7012020-07-19 20:49:54 -0700260 dev->dsa_ptr->netdev_ops = ops;
Florian Fainellida7b9e92019-01-15 14:43:04 -0800261}
262
Vladimir Olteanc3975402020-09-26 22:32:02 +0300263static void dsa_master_set_promiscuity(struct net_device *dev, int inc)
264{
265 const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops;
266
267 if (!ops->promisc_on_master)
268 return;
269
Vladimir Olteanc146f9b2022-01-06 01:11:15 +0200270 ASSERT_RTNL();
271
Vladimir Olteanc3975402020-09-26 22:32:02 +0300272 dev_set_promiscuity(dev, inc);
Vladimir Olteanc3975402020-09-26 22:32:02 +0300273}
274
Florian Fainellia3d7e012018-11-28 13:40:04 -0800275static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
276 char *buf)
277{
278 struct net_device *dev = to_net_dev(d);
279 struct dsa_port *cpu_dp = dev->dsa_ptr;
280
281 return sprintf(buf, "%s\n",
282 dsa_tag_protocol_to_str(cpu_dp->tag_ops));
283}
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200284
285static ssize_t tagging_store(struct device *d, struct device_attribute *attr,
286 const char *buf, size_t count)
287{
288 const struct dsa_device_ops *new_tag_ops, *old_tag_ops;
289 struct net_device *dev = to_net_dev(d);
290 struct dsa_port *cpu_dp = dev->dsa_ptr;
291 int err;
292
293 old_tag_ops = cpu_dp->tag_ops;
294 new_tag_ops = dsa_find_tagger_by_name(buf);
295 /* Bad tagger name, or module is not loaded? */
296 if (IS_ERR(new_tag_ops))
297 return PTR_ERR(new_tag_ops);
298
299 if (new_tag_ops == old_tag_ops)
300 /* Drop the temporarily held duplicate reference, since
301 * the DSA switch tree uses this tagger.
302 */
303 goto out;
304
305 err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, dev, new_tag_ops,
306 old_tag_ops);
307 if (err) {
308 /* On failure the old tagger is restored, so we don't need the
309 * driver for the new one.
310 */
311 dsa_tag_driver_put(new_tag_ops);
312 return err;
313 }
314
315 /* On success we no longer need the module for the old tagging protocol
316 */
317out:
318 dsa_tag_driver_put(old_tag_ops);
319 return count;
320}
321static DEVICE_ATTR_RW(tagging);
Florian Fainellia3d7e012018-11-28 13:40:04 -0800322
323static struct attribute *dsa_slave_attrs[] = {
324 &dev_attr_tagging.attr,
325 NULL
326};
327
328static const struct attribute_group dsa_group = {
329 .name = "dsa",
330 .attrs = dsa_slave_attrs,
331};
332
Cong Wang845e0eb2020-06-08 14:53:01 -0700333static struct lock_class_key dsa_master_addr_list_lock_key;
334
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500335int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
336{
Vladimir Oltean07b90052021-01-12 01:09:43 +0200337 struct dsa_switch *ds = cpu_dp->ds;
338 struct device_link *consumer_link;
Vladimir Olteana1ff94c2022-01-06 01:11:14 +0200339 int ret;
Florian Fainellia3d7e012018-11-28 13:40:04 -0800340
Vladimir Oltean07b90052021-01-12 01:09:43 +0200341 /* The DSA master must use SET_NETDEV_DEV for this to work. */
342 consumer_link = device_link_add(ds->dev, dev->dev.parent,
343 DL_FLAG_AUTOREMOVE_CONSUMER);
344 if (!consumer_link)
345 netdev_err(dev,
346 "Failed to create a device link to DSA switch %s\n",
347 dev_name(ds->dev));
348
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500349 /* If we use a tagging format that doesn't have an ethertype
350 * field, make sure that all packets from this point on get
351 * sent to the tag format's receive function.
352 */
353 wmb();
354
355 dev->dsa_ptr = cpu_dp;
Cong Wang845e0eb2020-06-08 14:53:01 -0700356 lockdep_set_class(&dev->addr_list_lock,
357 &dsa_master_addr_list_lock_key);
Vladimir Olteanc3975402020-09-26 22:32:02 +0300358
359 dsa_master_set_promiscuity(dev, 1);
360
Florian Fainellia3d7e012018-11-28 13:40:04 -0800361 ret = dsa_master_ethtool_setup(dev);
362 if (ret)
Vladimir Olteanc3975402020-09-26 22:32:02 +0300363 goto out_err_reset_promisc;
Florian Fainellia3d7e012018-11-28 13:40:04 -0800364
Florian Fainelli9c0c7012020-07-19 20:49:54 -0700365 dsa_netdev_ops_set(dev, &dsa_netdev_ops);
Florian Fainellida7b9e92019-01-15 14:43:04 -0800366
Florian Fainellia3d7e012018-11-28 13:40:04 -0800367 ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
368 if (ret)
Florian Fainellida7b9e92019-01-15 14:43:04 -0800369 goto out_err_ndo_teardown;
Florian Fainellia3d7e012018-11-28 13:40:04 -0800370
371 return ret;
Florian Fainellida7b9e92019-01-15 14:43:04 -0800372
373out_err_ndo_teardown:
Florian Fainelli9c0c7012020-07-19 20:49:54 -0700374 dsa_netdev_ops_set(dev, NULL);
Florian Fainellida7b9e92019-01-15 14:43:04 -0800375 dsa_master_ethtool_teardown(dev);
Vladimir Olteanc3975402020-09-26 22:32:02 +0300376out_err_reset_promisc:
377 dsa_master_set_promiscuity(dev, -1);
Florian Fainellida7b9e92019-01-15 14:43:04 -0800378 return ret;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500379}
380
381void dsa_master_teardown(struct net_device *dev)
382{
Florian Fainellia3d7e012018-11-28 13:40:04 -0800383 sysfs_remove_group(&dev->dev.kobj, &dsa_group);
Florian Fainelli9c0c7012020-07-19 20:49:54 -0700384 dsa_netdev_ops_set(dev, NULL);
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500385 dsa_master_ethtool_teardown(dev);
Vladimir Olteanc3975402020-09-26 22:32:02 +0300386 dsa_master_set_promiscuity(dev, -1);
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500387
388 dev->dsa_ptr = NULL;
389
390 /* If we used a tagging format that doesn't have an ethertype
391 * field, make sure that all packets from this point get sent
392 * without the tag and go through the regular receive path.
393 */
394 wmb();
395}