1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handling of a master device, switching frames via its switch fabric CPU port
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
11 static int dsa_master_get_regs_len(struct net_device *dev)
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;
20 if (ops->get_regs_len) {
21 len = ops->get_regs_len(dev);
27 ret += sizeof(struct ethtool_drvinfo);
28 ret += sizeof(struct ethtool_regs);
30 if (ds->ops->get_regs_len) {
31 len = ds->ops->get_regs_len(ds, port);
40 static void dsa_master_get_regs(struct net_device *dev,
41 struct ethtool_regs *regs, void *data)
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;
51 if (ops->get_regs_len && ops->get_regs) {
52 len = ops->get_regs_len(dev);
56 ops->get_regs(dev, regs, data);
60 cpu_info = (struct ethtool_drvinfo *)data;
61 strscpy(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);
66 if (ds->ops->get_regs_len && ds->ops->get_regs) {
67 len = ds->ops->get_regs_len(ds, port);
71 ds->ops->get_regs(ds, port, cpu_regs, data);
75 static void dsa_master_get_ethtool_stats(struct net_device *dev,
76 struct ethtool_stats *stats,
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;
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);
90 if (ds->ops->get_ethtool_stats)
91 ds->ops->get_ethtool_stats(ds, port, data + count);
94 static void dsa_master_get_ethtool_phy_stats(struct net_device *dev,
95 struct ethtool_stats *stats,
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;
104 if (dev->phydev && !ops->get_ethtool_phy_stats) {
105 count = phy_ethtool_get_sset_count(dev->phydev);
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);
116 if (ds->ops->get_ethtool_phy_stats)
117 ds->ops->get_ethtool_phy_stats(ds, port, data + count);
120 static int dsa_master_get_sset_count(struct net_device *dev, int sset)
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;
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);
136 if (ds->ops->get_sset_count)
137 count += ds->ops->get_sset_count(ds, cpu_dp->index, sset);
142 static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
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;
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] = '_';
158 if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
159 !ops->get_ethtool_phy_stats) {
160 mcount = phy_ethtool_get_sset_count(dev->phydev);
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);
169 ops->get_strings(dev, stringset, data);
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
178 ds->ops->get_strings(ds, port, stringset, ndata);
179 count = ds->ops->get_sset_count(ds, port, stringset);
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));
190 static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
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;
203 /* Deny PTP operations on master if there is at least one
204 * switch in the tree that is PTP capable.
206 list_for_each_entry(dp, &dst->ports, list)
207 if (dp->ds->ops->port_hwtstamp_get ||
208 dp->ds->ops->port_hwtstamp_set)
213 if (dev->netdev_ops->ndo_eth_ioctl)
214 err = dev->netdev_ops->ndo_eth_ioctl(dev, ifr, cmd);
219 static const struct dsa_netdevice_ops dsa_netdev_ops = {
220 .ndo_eth_ioctl = dsa_master_ioctl,
223 static int dsa_master_ethtool_setup(struct net_device *dev)
225 struct dsa_port *cpu_dp = dev->dsa_ptr;
226 struct dsa_switch *ds = cpu_dp->ds;
227 struct ethtool_ops *ops;
229 if (netif_is_lag_master(dev))
232 ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
236 cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
237 if (cpu_dp->orig_ethtool_ops)
238 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
240 ops->get_regs_len = dsa_master_get_regs_len;
241 ops->get_regs = dsa_master_get_regs;
242 ops->get_sset_count = dsa_master_get_sset_count;
243 ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
244 ops->get_strings = dsa_master_get_strings;
245 ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats;
247 dev->ethtool_ops = ops;
252 static void dsa_master_ethtool_teardown(struct net_device *dev)
254 struct dsa_port *cpu_dp = dev->dsa_ptr;
256 if (netif_is_lag_master(dev))
259 dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
260 cpu_dp->orig_ethtool_ops = NULL;
263 static void dsa_netdev_ops_set(struct net_device *dev,
264 const struct dsa_netdevice_ops *ops)
266 if (netif_is_lag_master(dev))
269 dev->dsa_ptr->netdev_ops = ops;
272 /* Keep the master always promiscuous if the tagging protocol requires that
273 * (garbles MAC DA) or if it doesn't support unicast filtering, case in which
274 * it would revert to promiscuous mode as soon as we call dev_uc_add() on it
277 static void dsa_master_set_promiscuity(struct net_device *dev, int inc)
279 const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops;
281 if ((dev->priv_flags & IFF_UNICAST_FLT) && !ops->promisc_on_master)
286 dev_set_promiscuity(dev, inc);
289 static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
292 struct net_device *dev = to_net_dev(d);
293 struct dsa_port *cpu_dp = dev->dsa_ptr;
295 return sprintf(buf, "%s\n",
296 dsa_tag_protocol_to_str(cpu_dp->tag_ops));
299 static ssize_t tagging_store(struct device *d, struct device_attribute *attr,
300 const char *buf, size_t count)
302 const struct dsa_device_ops *new_tag_ops, *old_tag_ops;
303 struct net_device *dev = to_net_dev(d);
304 struct dsa_port *cpu_dp = dev->dsa_ptr;
307 old_tag_ops = cpu_dp->tag_ops;
308 new_tag_ops = dsa_find_tagger_by_name(buf);
309 /* Bad tagger name, or module is not loaded? */
310 if (IS_ERR(new_tag_ops))
311 return PTR_ERR(new_tag_ops);
313 if (new_tag_ops == old_tag_ops)
314 /* Drop the temporarily held duplicate reference, since
315 * the DSA switch tree uses this tagger.
319 err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, new_tag_ops,
322 /* On failure the old tagger is restored, so we don't need the
323 * driver for the new one.
325 dsa_tag_driver_put(new_tag_ops);
329 /* On success we no longer need the module for the old tagging protocol
332 dsa_tag_driver_put(old_tag_ops);
335 static DEVICE_ATTR_RW(tagging);
337 static struct attribute *dsa_slave_attrs[] = {
338 &dev_attr_tagging.attr,
342 static const struct attribute_group dsa_group = {
344 .attrs = dsa_slave_attrs,
347 static void dsa_master_reset_mtu(struct net_device *dev)
351 err = dev_set_mtu(dev, ETH_DATA_LEN);
354 "Unable to reset MTU to exclude DSA overheads\n");
357 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
359 const struct dsa_device_ops *tag_ops = cpu_dp->tag_ops;
360 struct dsa_switch *ds = cpu_dp->ds;
361 struct device_link *consumer_link;
364 mtu = ETH_DATA_LEN + dsa_tag_protocol_overhead(tag_ops);
366 /* The DSA master must use SET_NETDEV_DEV for this to work. */
367 if (!netif_is_lag_master(dev)) {
368 consumer_link = device_link_add(ds->dev, dev->dev.parent,
369 DL_FLAG_AUTOREMOVE_CONSUMER);
372 "Failed to create a device link to DSA switch %s\n",
376 /* The switch driver may not implement ->port_change_mtu(), case in
377 * which dsa_slave_change_mtu() will not update the master MTU either,
378 * so we need to do that here.
380 ret = dev_set_mtu(dev, mtu);
382 netdev_warn(dev, "error %d setting MTU to %d to include DSA overhead\n",
385 /* If we use a tagging format that doesn't have an ethertype
386 * field, make sure that all packets from this point on get
387 * sent to the tag format's receive function.
391 dev->dsa_ptr = cpu_dp;
393 dsa_master_set_promiscuity(dev, 1);
395 ret = dsa_master_ethtool_setup(dev);
397 goto out_err_reset_promisc;
399 dsa_netdev_ops_set(dev, &dsa_netdev_ops);
401 ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
403 goto out_err_ndo_teardown;
407 out_err_ndo_teardown:
408 dsa_netdev_ops_set(dev, NULL);
409 dsa_master_ethtool_teardown(dev);
410 out_err_reset_promisc:
411 dsa_master_set_promiscuity(dev, -1);
415 void dsa_master_teardown(struct net_device *dev)
417 sysfs_remove_group(&dev->dev.kobj, &dsa_group);
418 dsa_netdev_ops_set(dev, NULL);
419 dsa_master_ethtool_teardown(dev);
420 dsa_master_reset_mtu(dev);
421 dsa_master_set_promiscuity(dev, -1);
425 /* If we used a tagging format that doesn't have an ethertype
426 * field, make sure that all packets from this point get sent
427 * without the tag and go through the regular receive path.
432 int dsa_master_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp,
433 struct netdev_lag_upper_info *uinfo,
434 struct netlink_ext_ack *extack)
436 bool master_setup = false;
439 if (!netdev_uses_dsa(lag_dev)) {
440 err = dsa_master_setup(lag_dev, cpu_dp);
447 err = dsa_port_lag_join(cpu_dp, lag_dev, uinfo, extack);
449 if (extack && !extack->_msg)
450 NL_SET_ERR_MSG_MOD(extack,
451 "CPU port failed to join LAG");
452 goto out_master_teardown;
459 dsa_master_teardown(lag_dev);
463 /* Tear down a master if there isn't any other user port on it,
464 * optionally also destroying LAG information.
466 void dsa_master_lag_teardown(struct net_device *lag_dev,
467 struct dsa_port *cpu_dp)
469 struct net_device *upper;
470 struct list_head *iter;
472 dsa_port_lag_leave(cpu_dp, lag_dev);
474 netdev_for_each_upper_dev_rcu(lag_dev, upper, iter)
475 if (dsa_slave_dev_check(upper))
478 dsa_master_teardown(lag_dev);