xref: /openbmc/linux/net/dsa/master.c (revision 0184c07a11a243569cb90104980237c8e101f363)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f2f23566SVivien Didelot /*
3f2f23566SVivien Didelot  * Handling of a master device, switching frames via its switch fabric CPU port
4f2f23566SVivien Didelot  *
5f2f23566SVivien Didelot  * Copyright (c) 2017 Savoir-faire Linux Inc.
6f2f23566SVivien Didelot  *	Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7f2f23566SVivien Didelot  */
8f2f23566SVivien Didelot 
9f2f23566SVivien Didelot #include "dsa_priv.h"
10f2f23566SVivien Didelot 
1148e23311SVivien Didelot static int dsa_master_get_regs_len(struct net_device *dev)
1248e23311SVivien Didelot {
1348e23311SVivien Didelot 	struct dsa_port *cpu_dp = dev->dsa_ptr;
1448e23311SVivien Didelot 	const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
1548e23311SVivien Didelot 	struct dsa_switch *ds = cpu_dp->ds;
1648e23311SVivien Didelot 	int port = cpu_dp->index;
1748e23311SVivien Didelot 	int ret = 0;
1848e23311SVivien Didelot 	int len;
1948e23311SVivien Didelot 
2048e23311SVivien Didelot 	if (ops->get_regs_len) {
2148e23311SVivien Didelot 		len = ops->get_regs_len(dev);
2248e23311SVivien Didelot 		if (len < 0)
2348e23311SVivien Didelot 			return len;
2448e23311SVivien Didelot 		ret += len;
2548e23311SVivien Didelot 	}
2648e23311SVivien Didelot 
2748e23311SVivien Didelot 	ret += sizeof(struct ethtool_drvinfo);
2848e23311SVivien Didelot 	ret += sizeof(struct ethtool_regs);
2948e23311SVivien Didelot 
3048e23311SVivien Didelot 	if (ds->ops->get_regs_len) {
3148e23311SVivien Didelot 		len = ds->ops->get_regs_len(ds, port);
3248e23311SVivien Didelot 		if (len < 0)
3348e23311SVivien Didelot 			return len;
3448e23311SVivien Didelot 		ret += len;
3548e23311SVivien Didelot 	}
3648e23311SVivien Didelot 
3748e23311SVivien Didelot 	return ret;
3848e23311SVivien Didelot }
3948e23311SVivien Didelot 
4048e23311SVivien Didelot static void dsa_master_get_regs(struct net_device *dev,
4148e23311SVivien Didelot 				struct ethtool_regs *regs, void *data)
4248e23311SVivien Didelot {
4348e23311SVivien Didelot 	struct dsa_port *cpu_dp = dev->dsa_ptr;
4448e23311SVivien Didelot 	const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
4548e23311SVivien Didelot 	struct dsa_switch *ds = cpu_dp->ds;
4648e23311SVivien Didelot 	struct ethtool_drvinfo *cpu_info;
4748e23311SVivien Didelot 	struct ethtool_regs *cpu_regs;
4848e23311SVivien Didelot 	int port = cpu_dp->index;
4948e23311SVivien Didelot 	int len;
5048e23311SVivien Didelot 
5148e23311SVivien Didelot 	if (ops->get_regs_len && ops->get_regs) {
5248e23311SVivien Didelot 		len = ops->get_regs_len(dev);
5348e23311SVivien Didelot 		if (len < 0)
5448e23311SVivien Didelot 			return;
5548e23311SVivien Didelot 		regs->len = len;
5648e23311SVivien Didelot 		ops->get_regs(dev, regs, data);
5748e23311SVivien Didelot 		data += regs->len;
5848e23311SVivien Didelot 	}
5948e23311SVivien Didelot 
6048e23311SVivien Didelot 	cpu_info = (struct ethtool_drvinfo *)data;
61e4d44b3dSWolfram Sang 	strscpy(cpu_info->driver, "dsa", sizeof(cpu_info->driver));
6248e23311SVivien Didelot 	data += sizeof(*cpu_info);
6348e23311SVivien Didelot 	cpu_regs = (struct ethtool_regs *)data;
6448e23311SVivien Didelot 	data += sizeof(*cpu_regs);
6548e23311SVivien Didelot 
6648e23311SVivien Didelot 	if (ds->ops->get_regs_len && ds->ops->get_regs) {
6748e23311SVivien Didelot 		len = ds->ops->get_regs_len(ds, port);
6848e23311SVivien Didelot 		if (len < 0)
6948e23311SVivien Didelot 			return;
7048e23311SVivien Didelot 		cpu_regs->len = len;
7148e23311SVivien Didelot 		ds->ops->get_regs(ds, port, cpu_regs, data);
7248e23311SVivien Didelot 	}
7348e23311SVivien Didelot }
7448e23311SVivien Didelot 
75f2f23566SVivien Didelot static void dsa_master_get_ethtool_stats(struct net_device *dev,
76f2f23566SVivien Didelot 					 struct ethtool_stats *stats,
77f2f23566SVivien Didelot 					 uint64_t *data)
78f2f23566SVivien Didelot {
792f657a60SVivien Didelot 	struct dsa_port *cpu_dp = dev->dsa_ptr;
807ec764eeSVivien Didelot 	const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
817ec764eeSVivien Didelot 	struct dsa_switch *ds = cpu_dp->ds;
827ec764eeSVivien Didelot 	int port = cpu_dp->index;
83f2f23566SVivien Didelot 	int count = 0;
84f2f23566SVivien Didelot 
851d1e79f1SFlorian Fainelli 	if (ops->get_sset_count && ops->get_ethtool_stats) {
86f2f23566SVivien Didelot 		count = ops->get_sset_count(dev, ETH_SS_STATS);
87f2f23566SVivien Didelot 		ops->get_ethtool_stats(dev, stats, data);
88f2f23566SVivien Didelot 	}
89f2f23566SVivien Didelot 
90f2f23566SVivien Didelot 	if (ds->ops->get_ethtool_stats)
917ec764eeSVivien Didelot 		ds->ops->get_ethtool_stats(ds, port, data + count);
92f2f23566SVivien Didelot }
93f2f23566SVivien Didelot 
94cf963573SFlorian Fainelli static void dsa_master_get_ethtool_phy_stats(struct net_device *dev,
95cf963573SFlorian Fainelli 					     struct ethtool_stats *stats,
96cf963573SFlorian Fainelli 					     uint64_t *data)
97cf963573SFlorian Fainelli {
98cf963573SFlorian Fainelli 	struct dsa_port *cpu_dp = dev->dsa_ptr;
99cf963573SFlorian Fainelli 	const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
100cf963573SFlorian Fainelli 	struct dsa_switch *ds = cpu_dp->ds;
101cf963573SFlorian Fainelli 	int port = cpu_dp->index;
102cf963573SFlorian Fainelli 	int count = 0;
103cf963573SFlorian Fainelli 
104cf963573SFlorian Fainelli 	if (dev->phydev && !ops->get_ethtool_phy_stats) {
105cf963573SFlorian Fainelli 		count = phy_ethtool_get_sset_count(dev->phydev);
106cf963573SFlorian Fainelli 		if (count >= 0)
107cf963573SFlorian Fainelli 			phy_ethtool_get_stats(dev->phydev, stats, data);
108cf963573SFlorian Fainelli 	} else if (ops->get_sset_count && ops->get_ethtool_phy_stats) {
109cf963573SFlorian Fainelli 		count = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
110cf963573SFlorian Fainelli 		ops->get_ethtool_phy_stats(dev, stats, data);
111cf963573SFlorian Fainelli 	}
112cf963573SFlorian Fainelli 
113cf963573SFlorian Fainelli 	if (count < 0)
114cf963573SFlorian Fainelli 		count = 0;
115cf963573SFlorian Fainelli 
116cf963573SFlorian Fainelli 	if (ds->ops->get_ethtool_phy_stats)
117cf963573SFlorian Fainelli 		ds->ops->get_ethtool_phy_stats(ds, port, data + count);
118cf963573SFlorian Fainelli }
119cf963573SFlorian Fainelli 
120f2f23566SVivien Didelot static int dsa_master_get_sset_count(struct net_device *dev, int sset)
121f2f23566SVivien Didelot {
1222f657a60SVivien Didelot 	struct dsa_port *cpu_dp = dev->dsa_ptr;
1237ec764eeSVivien Didelot 	const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
1247ec764eeSVivien Didelot 	struct dsa_switch *ds = cpu_dp->ds;
125f2f23566SVivien Didelot 	int count = 0;
126f2f23566SVivien Didelot 
127cf963573SFlorian Fainelli 	if (sset == ETH_SS_PHY_STATS && dev->phydev &&
128cf963573SFlorian Fainelli 	    !ops->get_ethtool_phy_stats)
129cf963573SFlorian Fainelli 		count = phy_ethtool_get_sset_count(dev->phydev);
130cf963573SFlorian Fainelli 	else if (ops->get_sset_count)
13189f09048SFlorian Fainelli 		count = ops->get_sset_count(dev, sset);
132cf963573SFlorian Fainelli 
13389f09048SFlorian Fainelli 	if (count < 0)
13489f09048SFlorian Fainelli 		count = 0;
135f2f23566SVivien Didelot 
13689f09048SFlorian Fainelli 	if (ds->ops->get_sset_count)
13789f09048SFlorian Fainelli 		count += ds->ops->get_sset_count(ds, cpu_dp->index, sset);
138f2f23566SVivien Didelot 
139f2f23566SVivien Didelot 	return count;
140f2f23566SVivien Didelot }
141f2f23566SVivien Didelot 
142f2f23566SVivien Didelot static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
143f2f23566SVivien Didelot 				   uint8_t *data)
144f2f23566SVivien Didelot {
1452f657a60SVivien Didelot 	struct dsa_port *cpu_dp = dev->dsa_ptr;
1467ec764eeSVivien Didelot 	const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
1477ec764eeSVivien Didelot 	struct dsa_switch *ds = cpu_dp->ds;
1487ec764eeSVivien Didelot 	int port = cpu_dp->index;
149f2f23566SVivien Didelot 	int len = ETH_GSTRING_LEN;
150a269333fSDan Carpenter 	int mcount = 0, count, i;
151f2f23566SVivien Didelot 	uint8_t pfx[4];
152f2f23566SVivien Didelot 	uint8_t *ndata;
153f2f23566SVivien Didelot 
1547ec764eeSVivien Didelot 	snprintf(pfx, sizeof(pfx), "p%.2d", port);
155f2f23566SVivien Didelot 	/* We do not want to be NULL-terminated, since this is a prefix */
156f2f23566SVivien Didelot 	pfx[sizeof(pfx) - 1] = '_';
157f2f23566SVivien Didelot 
158cf963573SFlorian Fainelli 	if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
159cf963573SFlorian Fainelli 	    !ops->get_ethtool_phy_stats) {
160cf963573SFlorian Fainelli 		mcount = phy_ethtool_get_sset_count(dev->phydev);
161cf963573SFlorian Fainelli 		if (mcount < 0)
162cf963573SFlorian Fainelli 			mcount = 0;
163cf963573SFlorian Fainelli 		else
164cf963573SFlorian Fainelli 			phy_ethtool_get_strings(dev->phydev, data);
165cf963573SFlorian Fainelli 	} else if (ops->get_sset_count && ops->get_strings) {
16689f09048SFlorian Fainelli 		mcount = ops->get_sset_count(dev, stringset);
16789f09048SFlorian Fainelli 		if (mcount < 0)
16889f09048SFlorian Fainelli 			mcount = 0;
169f2f23566SVivien Didelot 		ops->get_strings(dev, stringset, data);
170f2f23566SVivien Didelot 	}
171f2f23566SVivien Didelot 
17289f09048SFlorian Fainelli 	if (ds->ops->get_strings) {
173f2f23566SVivien Didelot 		ndata = data + mcount * len;
174f2f23566SVivien Didelot 		/* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
175f2f23566SVivien Didelot 		 * the output after to prepend our CPU port prefix we
176f2f23566SVivien Didelot 		 * constructed earlier
177f2f23566SVivien Didelot 		 */
17889f09048SFlorian Fainelli 		ds->ops->get_strings(ds, port, stringset, ndata);
17989f09048SFlorian Fainelli 		count = ds->ops->get_sset_count(ds, port, stringset);
180a269333fSDan Carpenter 		if (count < 0)
181a269333fSDan Carpenter 			return;
182f2f23566SVivien Didelot 		for (i = 0; i < count; i++) {
183f2f23566SVivien Didelot 			memmove(ndata + (i * len + sizeof(pfx)),
184f2f23566SVivien Didelot 				ndata + i * len, len - sizeof(pfx));
185f2f23566SVivien Didelot 			memcpy(ndata + i * len, pfx, sizeof(pfx));
186f2f23566SVivien Didelot 		}
187f2f23566SVivien Didelot 	}
188f2f23566SVivien Didelot }
189f2f23566SVivien Didelot 
190f685e609SVladimir Oltean static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
191f685e609SVladimir Oltean {
192f685e609SVladimir Oltean 	struct dsa_port *cpu_dp = dev->dsa_ptr;
193f685e609SVladimir Oltean 	struct dsa_switch *ds = cpu_dp->ds;
194f685e609SVladimir Oltean 	struct dsa_switch_tree *dst;
195f685e609SVladimir Oltean 	int err = -EOPNOTSUPP;
196f685e609SVladimir Oltean 	struct dsa_port *dp;
197f685e609SVladimir Oltean 
198f685e609SVladimir Oltean 	dst = ds->dst;
199f685e609SVladimir Oltean 
200f685e609SVladimir Oltean 	switch (cmd) {
201f685e609SVladimir Oltean 	case SIOCGHWTSTAMP:
202f685e609SVladimir Oltean 	case SIOCSHWTSTAMP:
203f685e609SVladimir Oltean 		/* Deny PTP operations on master if there is at least one
204f685e609SVladimir Oltean 		 * switch in the tree that is PTP capable.
205f685e609SVladimir Oltean 		 */
206f685e609SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list)
207ed1fe1beSVladimir Oltean 			if (dsa_port_supports_hwtstamp(dp, ifr))
208f685e609SVladimir Oltean 				return -EBUSY;
209f685e609SVladimir Oltean 		break;
210f685e609SVladimir Oltean 	}
211f685e609SVladimir Oltean 
212a7605370SArnd Bergmann 	if (dev->netdev_ops->ndo_eth_ioctl)
213a7605370SArnd Bergmann 		err = dev->netdev_ops->ndo_eth_ioctl(dev, ifr, cmd);
214f685e609SVladimir Oltean 
215f685e609SVladimir Oltean 	return err;
216f685e609SVladimir Oltean }
217f685e609SVladimir Oltean 
2189c0c7014SFlorian Fainelli static const struct dsa_netdevice_ops dsa_netdev_ops = {
219a7605370SArnd Bergmann 	.ndo_eth_ioctl = dsa_master_ioctl,
2209c0c7014SFlorian Fainelli };
2219c0c7014SFlorian Fainelli 
22217a22fcfSVivien Didelot static int dsa_master_ethtool_setup(struct net_device *dev)
223f2f23566SVivien Didelot {
2242f657a60SVivien Didelot 	struct dsa_port *cpu_dp = dev->dsa_ptr;
2257ec764eeSVivien Didelot 	struct dsa_switch *ds = cpu_dp->ds;
226f2f23566SVivien Didelot 	struct ethtool_ops *ops;
227f2f23566SVivien Didelot 
228cfeb84a5SVladimir Oltean 	if (netif_is_lag_master(dev))
229cfeb84a5SVladimir Oltean 		return 0;
230cfeb84a5SVladimir Oltean 
231f2f23566SVivien Didelot 	ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
232f2f23566SVivien Didelot 	if (!ops)
233f2f23566SVivien Didelot 		return -ENOMEM;
234f2f23566SVivien Didelot 
2357ec764eeSVivien Didelot 	cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
2367ec764eeSVivien Didelot 	if (cpu_dp->orig_ethtool_ops)
2377ec764eeSVivien Didelot 		memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
238f2f23566SVivien Didelot 
23948e23311SVivien Didelot 	ops->get_regs_len = dsa_master_get_regs_len;
24048e23311SVivien Didelot 	ops->get_regs = dsa_master_get_regs;
241f2f23566SVivien Didelot 	ops->get_sset_count = dsa_master_get_sset_count;
242f2f23566SVivien Didelot 	ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
243f2f23566SVivien Didelot 	ops->get_strings = dsa_master_get_strings;
244cf963573SFlorian Fainelli 	ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats;
245f2f23566SVivien Didelot 
246f2f23566SVivien Didelot 	dev->ethtool_ops = ops;
247f2f23566SVivien Didelot 
248f2f23566SVivien Didelot 	return 0;
249f2f23566SVivien Didelot }
250f2f23566SVivien Didelot 
25117a22fcfSVivien Didelot static void dsa_master_ethtool_teardown(struct net_device *dev)
252f2f23566SVivien Didelot {
2532f657a60SVivien Didelot 	struct dsa_port *cpu_dp = dev->dsa_ptr;
254f2f23566SVivien Didelot 
255cfeb84a5SVladimir Oltean 	if (netif_is_lag_master(dev))
256cfeb84a5SVladimir Oltean 		return;
257cfeb84a5SVladimir Oltean 
2587ec764eeSVivien Didelot 	dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
2597ec764eeSVivien Didelot 	cpu_dp->orig_ethtool_ops = NULL;
260f2f23566SVivien Didelot }
26117a22fcfSVivien Didelot 
2629c0c7014SFlorian Fainelli static void dsa_netdev_ops_set(struct net_device *dev,
2639c0c7014SFlorian Fainelli 			       const struct dsa_netdevice_ops *ops)
264da7b9e9bSFlorian Fainelli {
265cfeb84a5SVladimir Oltean 	if (netif_is_lag_master(dev))
266cfeb84a5SVladimir Oltean 		return;
267cfeb84a5SVladimir Oltean 
2689c0c7014SFlorian Fainelli 	dev->dsa_ptr->netdev_ops = ops;
269da7b9e9bSFlorian Fainelli }
270da7b9e9bSFlorian Fainelli 
2718940e6b6SVladimir Oltean /* Keep the master always promiscuous if the tagging protocol requires that
2728940e6b6SVladimir Oltean  * (garbles MAC DA) or if it doesn't support unicast filtering, case in which
2738940e6b6SVladimir Oltean  * it would revert to promiscuous mode as soon as we call dev_uc_add() on it
2748940e6b6SVladimir Oltean  * anyway.
2758940e6b6SVladimir Oltean  */
276c3975400SVladimir Oltean static void dsa_master_set_promiscuity(struct net_device *dev, int inc)
277c3975400SVladimir Oltean {
278c3975400SVladimir Oltean 	const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops;
279c3975400SVladimir Oltean 
2808940e6b6SVladimir Oltean 	if ((dev->priv_flags & IFF_UNICAST_FLT) && !ops->promisc_on_master)
281c3975400SVladimir Oltean 		return;
282c3975400SVladimir Oltean 
283c146f9bcSVladimir Oltean 	ASSERT_RTNL();
284c146f9bcSVladimir Oltean 
285c3975400SVladimir Oltean 	dev_set_promiscuity(dev, inc);
286c3975400SVladimir Oltean }
287c3975400SVladimir Oltean 
288a3d7e01dSFlorian Fainelli static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
289a3d7e01dSFlorian Fainelli 			    char *buf)
290a3d7e01dSFlorian Fainelli {
291a3d7e01dSFlorian Fainelli 	struct net_device *dev = to_net_dev(d);
292a3d7e01dSFlorian Fainelli 	struct dsa_port *cpu_dp = dev->dsa_ptr;
293a3d7e01dSFlorian Fainelli 
294a3d7e01dSFlorian Fainelli 	return sprintf(buf, "%s\n",
295a3d7e01dSFlorian Fainelli 		       dsa_tag_protocol_to_str(cpu_dp->tag_ops));
296a3d7e01dSFlorian Fainelli }
29753da0ebaSVladimir Oltean 
29853da0ebaSVladimir Oltean static ssize_t tagging_store(struct device *d, struct device_attribute *attr,
29953da0ebaSVladimir Oltean 			     const char *buf, size_t count)
30053da0ebaSVladimir Oltean {
30153da0ebaSVladimir Oltean 	const struct dsa_device_ops *new_tag_ops, *old_tag_ops;
302e8666130SVladimir Oltean 	const char *end = strchrnul(buf, '\n'), *name;
30353da0ebaSVladimir Oltean 	struct net_device *dev = to_net_dev(d);
30453da0ebaSVladimir Oltean 	struct dsa_port *cpu_dp = dev->dsa_ptr;
305e8666130SVladimir Oltean 	size_t len = end - buf;
30653da0ebaSVladimir Oltean 	int err;
30753da0ebaSVladimir Oltean 
308e8666130SVladimir Oltean 	/* Empty string passed */
309e8666130SVladimir Oltean 	if (!len)
310e8666130SVladimir Oltean 		return -ENOPROTOOPT;
311e8666130SVladimir Oltean 
312e8666130SVladimir Oltean 	name = kstrndup(buf, len, GFP_KERNEL);
313e8666130SVladimir Oltean 	if (!name)
314e8666130SVladimir Oltean 		return -ENOMEM;
315e8666130SVladimir Oltean 
31653da0ebaSVladimir Oltean 	old_tag_ops = cpu_dp->tag_ops;
317*0184c07aSVladimir Oltean 	new_tag_ops = dsa_tag_driver_get_by_name(name);
318e8666130SVladimir Oltean 	kfree(name);
319*0184c07aSVladimir Oltean 	/* Bad tagger name? */
32053da0ebaSVladimir Oltean 	if (IS_ERR(new_tag_ops))
32153da0ebaSVladimir Oltean 		return PTR_ERR(new_tag_ops);
32253da0ebaSVladimir Oltean 
32353da0ebaSVladimir Oltean 	if (new_tag_ops == old_tag_ops)
32453da0ebaSVladimir Oltean 		/* Drop the temporarily held duplicate reference, since
32553da0ebaSVladimir Oltean 		 * the DSA switch tree uses this tagger.
32653da0ebaSVladimir Oltean 		 */
32753da0ebaSVladimir Oltean 		goto out;
32853da0ebaSVladimir Oltean 
329f41ec1fdSVladimir Oltean 	err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, new_tag_ops,
33053da0ebaSVladimir Oltean 					old_tag_ops);
33153da0ebaSVladimir Oltean 	if (err) {
33253da0ebaSVladimir Oltean 		/* On failure the old tagger is restored, so we don't need the
33353da0ebaSVladimir Oltean 		 * driver for the new one.
33453da0ebaSVladimir Oltean 		 */
33553da0ebaSVladimir Oltean 		dsa_tag_driver_put(new_tag_ops);
33653da0ebaSVladimir Oltean 		return err;
33753da0ebaSVladimir Oltean 	}
33853da0ebaSVladimir Oltean 
33953da0ebaSVladimir Oltean 	/* On success we no longer need the module for the old tagging protocol
34053da0ebaSVladimir Oltean 	 */
34153da0ebaSVladimir Oltean out:
34253da0ebaSVladimir Oltean 	dsa_tag_driver_put(old_tag_ops);
34353da0ebaSVladimir Oltean 	return count;
34453da0ebaSVladimir Oltean }
34553da0ebaSVladimir Oltean static DEVICE_ATTR_RW(tagging);
346a3d7e01dSFlorian Fainelli 
347a3d7e01dSFlorian Fainelli static struct attribute *dsa_slave_attrs[] = {
348a3d7e01dSFlorian Fainelli 	&dev_attr_tagging.attr,
349a3d7e01dSFlorian Fainelli 	NULL
350a3d7e01dSFlorian Fainelli };
351a3d7e01dSFlorian Fainelli 
352a3d7e01dSFlorian Fainelli static const struct attribute_group dsa_group = {
353a3d7e01dSFlorian Fainelli 	.name	= "dsa",
354a3d7e01dSFlorian Fainelli 	.attrs	= dsa_slave_attrs,
355a3d7e01dSFlorian Fainelli };
356a3d7e01dSFlorian Fainelli 
357066dfc42SVladimir Oltean static void dsa_master_reset_mtu(struct net_device *dev)
358066dfc42SVladimir Oltean {
359066dfc42SVladimir Oltean 	int err;
360066dfc42SVladimir Oltean 
361066dfc42SVladimir Oltean 	err = dev_set_mtu(dev, ETH_DATA_LEN);
362066dfc42SVladimir Oltean 	if (err)
363066dfc42SVladimir Oltean 		netdev_dbg(dev,
364066dfc42SVladimir Oltean 			   "Unable to reset MTU to exclude DSA overheads\n");
365066dfc42SVladimir Oltean }
366066dfc42SVladimir Oltean 
36717a22fcfSVivien Didelot int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
36817a22fcfSVivien Didelot {
369066dfc42SVladimir Oltean 	const struct dsa_device_ops *tag_ops = cpu_dp->tag_ops;
37007b90056SVladimir Oltean 	struct dsa_switch *ds = cpu_dp->ds;
37107b90056SVladimir Oltean 	struct device_link *consumer_link;
372066dfc42SVladimir Oltean 	int mtu, ret;
373066dfc42SVladimir Oltean 
374066dfc42SVladimir Oltean 	mtu = ETH_DATA_LEN + dsa_tag_protocol_overhead(tag_ops);
375a3d7e01dSFlorian Fainelli 
37607b90056SVladimir Oltean 	/* The DSA master must use SET_NETDEV_DEV for this to work. */
37713eccc1bSVladimir Oltean 	if (!netif_is_lag_master(dev)) {
37807b90056SVladimir Oltean 		consumer_link = device_link_add(ds->dev, dev->dev.parent,
37907b90056SVladimir Oltean 						DL_FLAG_AUTOREMOVE_CONSUMER);
38007b90056SVladimir Oltean 		if (!consumer_link)
38107b90056SVladimir Oltean 			netdev_err(dev,
38207b90056SVladimir Oltean 				   "Failed to create a device link to DSA switch %s\n",
38307b90056SVladimir Oltean 				   dev_name(ds->dev));
38413eccc1bSVladimir Oltean 	}
38507b90056SVladimir Oltean 
386066dfc42SVladimir Oltean 	/* The switch driver may not implement ->port_change_mtu(), case in
387066dfc42SVladimir Oltean 	 * which dsa_slave_change_mtu() will not update the master MTU either,
388066dfc42SVladimir Oltean 	 * so we need to do that here.
389066dfc42SVladimir Oltean 	 */
390066dfc42SVladimir Oltean 	ret = dev_set_mtu(dev, mtu);
391066dfc42SVladimir Oltean 	if (ret)
392066dfc42SVladimir Oltean 		netdev_warn(dev, "error %d setting MTU to %d to include DSA overhead\n",
393066dfc42SVladimir Oltean 			    ret, mtu);
394066dfc42SVladimir Oltean 
39517a22fcfSVivien Didelot 	/* If we use a tagging format that doesn't have an ethertype
39617a22fcfSVivien Didelot 	 * field, make sure that all packets from this point on get
39717a22fcfSVivien Didelot 	 * sent to the tag format's receive function.
39817a22fcfSVivien Didelot 	 */
39917a22fcfSVivien Didelot 	wmb();
40017a22fcfSVivien Didelot 
40117a22fcfSVivien Didelot 	dev->dsa_ptr = cpu_dp;
402c3975400SVladimir Oltean 
403c3975400SVladimir Oltean 	dsa_master_set_promiscuity(dev, 1);
404c3975400SVladimir Oltean 
405a3d7e01dSFlorian Fainelli 	ret = dsa_master_ethtool_setup(dev);
406a3d7e01dSFlorian Fainelli 	if (ret)
407c3975400SVladimir Oltean 		goto out_err_reset_promisc;
408a3d7e01dSFlorian Fainelli 
4099c0c7014SFlorian Fainelli 	dsa_netdev_ops_set(dev, &dsa_netdev_ops);
410da7b9e9bSFlorian Fainelli 
411a3d7e01dSFlorian Fainelli 	ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
412a3d7e01dSFlorian Fainelli 	if (ret)
413da7b9e9bSFlorian Fainelli 		goto out_err_ndo_teardown;
414a3d7e01dSFlorian Fainelli 
415a3d7e01dSFlorian Fainelli 	return ret;
416da7b9e9bSFlorian Fainelli 
417da7b9e9bSFlorian Fainelli out_err_ndo_teardown:
4189c0c7014SFlorian Fainelli 	dsa_netdev_ops_set(dev, NULL);
419da7b9e9bSFlorian Fainelli 	dsa_master_ethtool_teardown(dev);
420c3975400SVladimir Oltean out_err_reset_promisc:
421c3975400SVladimir Oltean 	dsa_master_set_promiscuity(dev, -1);
422da7b9e9bSFlorian Fainelli 	return ret;
42317a22fcfSVivien Didelot }
42417a22fcfSVivien Didelot 
42517a22fcfSVivien Didelot void dsa_master_teardown(struct net_device *dev)
42617a22fcfSVivien Didelot {
427a3d7e01dSFlorian Fainelli 	sysfs_remove_group(&dev->dev.kobj, &dsa_group);
4289c0c7014SFlorian Fainelli 	dsa_netdev_ops_set(dev, NULL);
42917a22fcfSVivien Didelot 	dsa_master_ethtool_teardown(dev);
430066dfc42SVladimir Oltean 	dsa_master_reset_mtu(dev);
431c3975400SVladimir Oltean 	dsa_master_set_promiscuity(dev, -1);
43217a22fcfSVivien Didelot 
43317a22fcfSVivien Didelot 	dev->dsa_ptr = NULL;
43417a22fcfSVivien Didelot 
43517a22fcfSVivien Didelot 	/* If we used a tagging format that doesn't have an ethertype
43617a22fcfSVivien Didelot 	 * field, make sure that all packets from this point get sent
43717a22fcfSVivien Didelot 	 * without the tag and go through the regular receive path.
43817a22fcfSVivien Didelot 	 */
43917a22fcfSVivien Didelot 	wmb();
44017a22fcfSVivien Didelot }
441acc43b7bSVladimir Oltean 
442acc43b7bSVladimir Oltean int dsa_master_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp,
443acc43b7bSVladimir Oltean 			 struct netdev_lag_upper_info *uinfo,
444acc43b7bSVladimir Oltean 			 struct netlink_ext_ack *extack)
445acc43b7bSVladimir Oltean {
446acc43b7bSVladimir Oltean 	bool master_setup = false;
447acc43b7bSVladimir Oltean 	int err;
448acc43b7bSVladimir Oltean 
449acc43b7bSVladimir Oltean 	if (!netdev_uses_dsa(lag_dev)) {
450acc43b7bSVladimir Oltean 		err = dsa_master_setup(lag_dev, cpu_dp);
451acc43b7bSVladimir Oltean 		if (err)
452acc43b7bSVladimir Oltean 			return err;
453acc43b7bSVladimir Oltean 
454acc43b7bSVladimir Oltean 		master_setup = true;
455acc43b7bSVladimir Oltean 	}
456acc43b7bSVladimir Oltean 
457acc43b7bSVladimir Oltean 	err = dsa_port_lag_join(cpu_dp, lag_dev, uinfo, extack);
458acc43b7bSVladimir Oltean 	if (err) {
459acc43b7bSVladimir Oltean 		if (extack && !extack->_msg)
460acc43b7bSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
461acc43b7bSVladimir Oltean 					   "CPU port failed to join LAG");
462acc43b7bSVladimir Oltean 		goto out_master_teardown;
463acc43b7bSVladimir Oltean 	}
464acc43b7bSVladimir Oltean 
465acc43b7bSVladimir Oltean 	return 0;
466acc43b7bSVladimir Oltean 
467acc43b7bSVladimir Oltean out_master_teardown:
468acc43b7bSVladimir Oltean 	if (master_setup)
469acc43b7bSVladimir Oltean 		dsa_master_teardown(lag_dev);
470acc43b7bSVladimir Oltean 	return err;
471acc43b7bSVladimir Oltean }
472acc43b7bSVladimir Oltean 
473acc43b7bSVladimir Oltean /* Tear down a master if there isn't any other user port on it,
474acc43b7bSVladimir Oltean  * optionally also destroying LAG information.
475acc43b7bSVladimir Oltean  */
476acc43b7bSVladimir Oltean void dsa_master_lag_teardown(struct net_device *lag_dev,
477acc43b7bSVladimir Oltean 			     struct dsa_port *cpu_dp)
478acc43b7bSVladimir Oltean {
479acc43b7bSVladimir Oltean 	struct net_device *upper;
480acc43b7bSVladimir Oltean 	struct list_head *iter;
481acc43b7bSVladimir Oltean 
482acc43b7bSVladimir Oltean 	dsa_port_lag_leave(cpu_dp, lag_dev);
483acc43b7bSVladimir Oltean 
484acc43b7bSVladimir Oltean 	netdev_for_each_upper_dev_rcu(lag_dev, upper, iter)
485acc43b7bSVladimir Oltean 		if (dsa_slave_dev_check(upper))
486acc43b7bSVladimir Oltean 			return;
487acc43b7bSVladimir Oltean 
488acc43b7bSVladimir Oltean 	dsa_master_teardown(lag_dev);
489acc43b7bSVladimir Oltean }
490