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
dsa_master_get_regs_len(struct net_device * dev)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
dsa_master_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * data)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
dsa_master_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,uint64_t * data)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
dsa_master_get_ethtool_phy_stats(struct net_device * dev,struct ethtool_stats * stats,uint64_t * data)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
dsa_master_get_sset_count(struct net_device * dev,int sset)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
dsa_master_get_strings(struct net_device * dev,uint32_t stringset,uint8_t * data)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
dsa_master_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)190 static 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 (dsa_port_supports_hwtstamp(dp, ifr))
208 return -EBUSY;
209 break;
210 }
211
212 if (dev->netdev_ops->ndo_eth_ioctl)
213 err = dev->netdev_ops->ndo_eth_ioctl(dev, ifr, cmd);
214
215 return err;
216 }
217
218 static const struct dsa_netdevice_ops dsa_netdev_ops = {
219 .ndo_eth_ioctl = dsa_master_ioctl,
220 };
221
dsa_master_ethtool_setup(struct net_device * dev)222 static int dsa_master_ethtool_setup(struct net_device *dev)
223 {
224 struct dsa_port *cpu_dp = dev->dsa_ptr;
225 struct dsa_switch *ds = cpu_dp->ds;
226 struct ethtool_ops *ops;
227
228 ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
229 if (!ops)
230 return -ENOMEM;
231
232 cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
233 if (cpu_dp->orig_ethtool_ops)
234 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
235
236 ops->get_regs_len = dsa_master_get_regs_len;
237 ops->get_regs = dsa_master_get_regs;
238 ops->get_sset_count = dsa_master_get_sset_count;
239 ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
240 ops->get_strings = dsa_master_get_strings;
241 ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats;
242
243 dev->ethtool_ops = ops;
244
245 return 0;
246 }
247
dsa_master_ethtool_teardown(struct net_device * dev)248 static void dsa_master_ethtool_teardown(struct net_device *dev)
249 {
250 struct dsa_port *cpu_dp = dev->dsa_ptr;
251
252 dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
253 cpu_dp->orig_ethtool_ops = NULL;
254 }
255
dsa_netdev_ops_set(struct net_device * dev,const struct dsa_netdevice_ops * ops)256 static void dsa_netdev_ops_set(struct net_device *dev,
257 const struct dsa_netdevice_ops *ops)
258 {
259 dev->dsa_ptr->netdev_ops = ops;
260 }
261
dsa_master_set_promiscuity(struct net_device * dev,int inc)262 static void dsa_master_set_promiscuity(struct net_device *dev, int inc)
263 {
264 const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops;
265
266 if (!ops->promisc_on_master)
267 return;
268
269 rtnl_lock();
270 dev_set_promiscuity(dev, inc);
271 rtnl_unlock();
272 }
273
tagging_show(struct device * d,struct device_attribute * attr,char * buf)274 static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
275 char *buf)
276 {
277 struct net_device *dev = to_net_dev(d);
278 struct dsa_port *cpu_dp = dev->dsa_ptr;
279
280 return sprintf(buf, "%s\n",
281 dsa_tag_protocol_to_str(cpu_dp->tag_ops));
282 }
283
tagging_store(struct device * d,struct device_attribute * attr,const char * buf,size_t count)284 static ssize_t tagging_store(struct device *d, struct device_attribute *attr,
285 const char *buf, size_t count)
286 {
287 const struct dsa_device_ops *new_tag_ops, *old_tag_ops;
288 struct net_device *dev = to_net_dev(d);
289 struct dsa_port *cpu_dp = dev->dsa_ptr;
290 int err;
291
292 old_tag_ops = cpu_dp->tag_ops;
293 new_tag_ops = dsa_find_tagger_by_name(buf);
294 /* Bad tagger name, or module is not loaded? */
295 if (IS_ERR(new_tag_ops))
296 return PTR_ERR(new_tag_ops);
297
298 if (new_tag_ops == old_tag_ops)
299 /* Drop the temporarily held duplicate reference, since
300 * the DSA switch tree uses this tagger.
301 */
302 goto out;
303
304 err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, dev, new_tag_ops,
305 old_tag_ops);
306 if (err) {
307 /* On failure the old tagger is restored, so we don't need the
308 * driver for the new one.
309 */
310 dsa_tag_driver_put(new_tag_ops);
311 return err;
312 }
313
314 /* On success we no longer need the module for the old tagging protocol
315 */
316 out:
317 dsa_tag_driver_put(old_tag_ops);
318 return count;
319 }
320 static DEVICE_ATTR_RW(tagging);
321
322 static struct attribute *dsa_slave_attrs[] = {
323 &dev_attr_tagging.attr,
324 NULL
325 };
326
327 static const struct attribute_group dsa_group = {
328 .name = "dsa",
329 .attrs = dsa_slave_attrs,
330 };
331
dsa_master_reset_mtu(struct net_device * dev)332 static void dsa_master_reset_mtu(struct net_device *dev)
333 {
334 int err;
335
336 rtnl_lock();
337 err = dev_set_mtu(dev, ETH_DATA_LEN);
338 if (err)
339 netdev_dbg(dev,
340 "Unable to reset MTU to exclude DSA overheads\n");
341 rtnl_unlock();
342 }
343
344 static struct lock_class_key dsa_master_addr_list_lock_key;
345
dsa_master_setup(struct net_device * dev,struct dsa_port * cpu_dp)346 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
347 {
348 const struct dsa_device_ops *tag_ops = cpu_dp->tag_ops;
349 struct dsa_switch *ds = cpu_dp->ds;
350 struct device_link *consumer_link;
351 int mtu, ret;
352
353 mtu = ETH_DATA_LEN + dsa_tag_protocol_overhead(tag_ops);
354
355 /* The DSA master must use SET_NETDEV_DEV for this to work. */
356 consumer_link = device_link_add(ds->dev, dev->dev.parent,
357 DL_FLAG_AUTOREMOVE_CONSUMER);
358 if (!consumer_link)
359 netdev_err(dev,
360 "Failed to create a device link to DSA switch %s\n",
361 dev_name(ds->dev));
362
363 rtnl_lock();
364 ret = dev_set_mtu(dev, mtu);
365 rtnl_unlock();
366 if (ret)
367 netdev_warn(dev, "error %d setting MTU to %d to include DSA overhead\n",
368 ret, mtu);
369
370 /* If we use a tagging format that doesn't have an ethertype
371 * field, make sure that all packets from this point on get
372 * sent to the tag format's receive function.
373 */
374 wmb();
375
376 dev->dsa_ptr = cpu_dp;
377 lockdep_set_class(&dev->addr_list_lock,
378 &dsa_master_addr_list_lock_key);
379
380 dsa_master_set_promiscuity(dev, 1);
381
382 ret = dsa_master_ethtool_setup(dev);
383 if (ret)
384 goto out_err_reset_promisc;
385
386 dsa_netdev_ops_set(dev, &dsa_netdev_ops);
387
388 ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
389 if (ret)
390 goto out_err_ndo_teardown;
391
392 return ret;
393
394 out_err_ndo_teardown:
395 dsa_netdev_ops_set(dev, NULL);
396 dsa_master_ethtool_teardown(dev);
397 out_err_reset_promisc:
398 dsa_master_set_promiscuity(dev, -1);
399 return ret;
400 }
401
dsa_master_teardown(struct net_device * dev)402 void dsa_master_teardown(struct net_device *dev)
403 {
404 sysfs_remove_group(&dev->dev.kobj, &dsa_group);
405 dsa_netdev_ops_set(dev, NULL);
406 dsa_master_ethtool_teardown(dev);
407 dsa_master_reset_mtu(dev);
408 dsa_master_set_promiscuity(dev, -1);
409
410 dev->dsa_ptr = NULL;
411
412 /* If we used a tagging format that doesn't have an ethertype
413 * field, make sure that all packets from this point get sent
414 * without the tag and go through the regular receive path.
415 */
416 wmb();
417 }
418