GNU Linux-libre 5.4.207-gnu1
[releases.git] / net / dsa / master.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
7  */
8
9 #include "dsa_priv.h"
10
11 static 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
40 static 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
75 static void dsa_master_get_ethtool_stats(struct net_device *dev,
76                                          struct ethtool_stats *stats,
77                                          uint64_t *data)
78 {
79         struct dsa_port *cpu_dp = dev->dsa_ptr;
80         const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
81         struct dsa_switch *ds = cpu_dp->ds;
82         int port = cpu_dp->index;
83         int count = 0;
84
85         if (ops->get_sset_count && ops->get_ethtool_stats) {
86                 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)
91                 ds->ops->get_ethtool_stats(ds, port, data + count);
92 }
93
94 static 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
120 static int dsa_master_get_sset_count(struct net_device *dev, int sset)
121 {
122         struct dsa_port *cpu_dp = dev->dsa_ptr;
123         const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
124         struct dsa_switch *ds = cpu_dp->ds;
125         int count = 0;
126
127         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)
131                 count = ops->get_sset_count(dev, sset);
132
133         if (count < 0)
134                 count = 0;
135
136         if (ds->ops->get_sset_count)
137                 count += ds->ops->get_sset_count(ds, cpu_dp->index, sset);
138
139         return count;
140 }
141
142 static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
143                                    uint8_t *data)
144 {
145         struct dsa_port *cpu_dp = dev->dsa_ptr;
146         const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
147         struct dsa_switch *ds = cpu_dp->ds;
148         int port = cpu_dp->index;
149         int len = ETH_GSTRING_LEN;
150         int mcount = 0, count, i;
151         uint8_t pfx[4];
152         uint8_t *ndata;
153
154         snprintf(pfx, sizeof(pfx), "p%.2d", port);
155         /* We do not want to be NULL-terminated, since this is a prefix */
156         pfx[sizeof(pfx) - 1] = '_';
157
158         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) {
166                 mcount = ops->get_sset_count(dev, stringset);
167                 if (mcount < 0)
168                         mcount = 0;
169                 ops->get_strings(dev, stringset, data);
170         }
171
172         if (ds->ops->get_strings) {
173                 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                  */
178                 ds->ops->get_strings(ds, port, stringset, ndata);
179                 count = ds->ops->get_sset_count(ds, port, stringset);
180                 if (count < 0)
181                         return;
182                 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
190 static int dsa_master_get_phys_port_name(struct net_device *dev,
191                                          char *name, size_t len)
192 {
193         struct dsa_port *cpu_dp = dev->dsa_ptr;
194
195         if (snprintf(name, len, "p%d", cpu_dp->index) >= len)
196                 return -EINVAL;
197
198         return 0;
199 }
200
201 static int dsa_master_ethtool_setup(struct net_device *dev)
202 {
203         struct dsa_port *cpu_dp = dev->dsa_ptr;
204         struct dsa_switch *ds = cpu_dp->ds;
205         struct ethtool_ops *ops;
206
207         ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
208         if (!ops)
209                 return -ENOMEM;
210
211         cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
212         if (cpu_dp->orig_ethtool_ops)
213                 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
214
215         ops->get_regs_len = dsa_master_get_regs_len;
216         ops->get_regs = dsa_master_get_regs;
217         ops->get_sset_count = dsa_master_get_sset_count;
218         ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
219         ops->get_strings = dsa_master_get_strings;
220         ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats;
221
222         dev->ethtool_ops = ops;
223
224         return 0;
225 }
226
227 static void dsa_master_ethtool_teardown(struct net_device *dev)
228 {
229         struct dsa_port *cpu_dp = dev->dsa_ptr;
230
231         dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
232         cpu_dp->orig_ethtool_ops = NULL;
233 }
234
235 static int dsa_master_ndo_setup(struct net_device *dev)
236 {
237         struct dsa_port *cpu_dp = dev->dsa_ptr;
238         struct dsa_switch *ds = cpu_dp->ds;
239         struct net_device_ops *ops;
240
241         if (dev->netdev_ops->ndo_get_phys_port_name)
242                 return 0;
243
244         ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
245         if (!ops)
246                 return -ENOMEM;
247
248         cpu_dp->orig_ndo_ops = dev->netdev_ops;
249         if (cpu_dp->orig_ndo_ops)
250                 memcpy(ops, cpu_dp->orig_ndo_ops, sizeof(*ops));
251
252         ops->ndo_get_phys_port_name = dsa_master_get_phys_port_name;
253
254         dev->netdev_ops  = ops;
255
256         return 0;
257 }
258
259 static void dsa_master_ndo_teardown(struct net_device *dev)
260 {
261         struct dsa_port *cpu_dp = dev->dsa_ptr;
262
263         if (cpu_dp->orig_ndo_ops)
264                 dev->netdev_ops = cpu_dp->orig_ndo_ops;
265         cpu_dp->orig_ndo_ops = NULL;
266 }
267
268 static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
269                             char *buf)
270 {
271         struct net_device *dev = to_net_dev(d);
272         struct dsa_port *cpu_dp = dev->dsa_ptr;
273
274         return sprintf(buf, "%s\n",
275                        dsa_tag_protocol_to_str(cpu_dp->tag_ops));
276 }
277 static DEVICE_ATTR_RO(tagging);
278
279 static struct attribute *dsa_slave_attrs[] = {
280         &dev_attr_tagging.attr,
281         NULL
282 };
283
284 static const struct attribute_group dsa_group = {
285         .name   = "dsa",
286         .attrs  = dsa_slave_attrs,
287 };
288
289 static void dsa_master_set_mtu(struct net_device *dev, struct dsa_port *cpu_dp)
290 {
291         unsigned int mtu = ETH_DATA_LEN + cpu_dp->tag_ops->overhead;
292         int err;
293
294         rtnl_lock();
295         if (mtu <= dev->max_mtu) {
296                 err = dev_set_mtu(dev, mtu);
297                 if (err)
298                         netdev_dbg(dev, "Unable to set MTU to include for DSA overheads\n");
299         }
300         rtnl_unlock();
301 }
302
303 static void dsa_master_reset_mtu(struct net_device *dev)
304 {
305         int err;
306
307         rtnl_lock();
308         err = dev_set_mtu(dev, ETH_DATA_LEN);
309         if (err)
310                 netdev_dbg(dev,
311                            "Unable to reset MTU to exclude DSA overheads\n");
312         rtnl_unlock();
313 }
314
315 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
316 {
317         int ret;
318
319         dsa_master_set_mtu(dev,  cpu_dp);
320
321         /* If we use a tagging format that doesn't have an ethertype
322          * field, make sure that all packets from this point on get
323          * sent to the tag format's receive function.
324          */
325         wmb();
326
327         dev->dsa_ptr = cpu_dp;
328         ret = dsa_master_ethtool_setup(dev);
329         if (ret)
330                 return ret;
331
332         ret = dsa_master_ndo_setup(dev);
333         if (ret)
334                 goto out_err_ethtool_teardown;
335
336         ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
337         if (ret)
338                 goto out_err_ndo_teardown;
339
340         return ret;
341
342 out_err_ndo_teardown:
343         dsa_master_ndo_teardown(dev);
344 out_err_ethtool_teardown:
345         dsa_master_ethtool_teardown(dev);
346         return ret;
347 }
348
349 void dsa_master_teardown(struct net_device *dev)
350 {
351         sysfs_remove_group(&dev->dev.kobj, &dsa_group);
352         dsa_master_ndo_teardown(dev);
353         dsa_master_ethtool_teardown(dev);
354         dsa_master_reset_mtu(dev);
355
356         dev->dsa_ptr = NULL;
357
358         /* If we used a tagging format that doesn't have an ethertype
359          * field, make sure that all packets from this point get sent
360          * without the tag and go through the regular receive path.
361          */
362         wmb();
363 }