xref: /openbmc/linux/net/dsa/slave.c (revision 13dd8710)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/dsa/slave.c - Slave device handling
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  */
6 
7 #include <linux/list.h>
8 #include <linux/etherdevice.h>
9 #include <linux/netdevice.h>
10 #include <linux/phy.h>
11 #include <linux/phy_fixed.h>
12 #include <linux/phylink.h>
13 #include <linux/of_net.h>
14 #include <linux/of_mdio.h>
15 #include <linux/mdio.h>
16 #include <net/rtnetlink.h>
17 #include <net/pkt_cls.h>
18 #include <net/tc_act/tc_mirred.h>
19 #include <linux/if_bridge.h>
20 #include <linux/netpoll.h>
21 #include <linux/ptp_classify.h>
22 
23 #include "dsa_priv.h"
24 
25 /* slave mii_bus handling ***************************************************/
26 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
27 {
28 	struct dsa_switch *ds = bus->priv;
29 
30 	if (ds->phys_mii_mask & (1 << addr))
31 		return ds->ops->phy_read(ds, addr, reg);
32 
33 	return 0xffff;
34 }
35 
36 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
37 {
38 	struct dsa_switch *ds = bus->priv;
39 
40 	if (ds->phys_mii_mask & (1 << addr))
41 		return ds->ops->phy_write(ds, addr, reg, val);
42 
43 	return 0;
44 }
45 
46 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
47 {
48 	ds->slave_mii_bus->priv = (void *)ds;
49 	ds->slave_mii_bus->name = "dsa slave smi";
50 	ds->slave_mii_bus->read = dsa_slave_phy_read;
51 	ds->slave_mii_bus->write = dsa_slave_phy_write;
52 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
53 		 ds->dst->index, ds->index);
54 	ds->slave_mii_bus->parent = ds->dev;
55 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
56 }
57 
58 
59 /* slave device handling ****************************************************/
60 static int dsa_slave_get_iflink(const struct net_device *dev)
61 {
62 	return dsa_slave_to_master(dev)->ifindex;
63 }
64 
65 static int dsa_slave_open(struct net_device *dev)
66 {
67 	struct net_device *master = dsa_slave_to_master(dev);
68 	struct dsa_port *dp = dsa_slave_to_port(dev);
69 	int err;
70 
71 	if (!(master->flags & IFF_UP))
72 		return -ENETDOWN;
73 
74 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
75 		err = dev_uc_add(master, dev->dev_addr);
76 		if (err < 0)
77 			goto out;
78 	}
79 
80 	if (dev->flags & IFF_ALLMULTI) {
81 		err = dev_set_allmulti(master, 1);
82 		if (err < 0)
83 			goto del_unicast;
84 	}
85 	if (dev->flags & IFF_PROMISC) {
86 		err = dev_set_promiscuity(master, 1);
87 		if (err < 0)
88 			goto clear_allmulti;
89 	}
90 
91 	err = dsa_port_enable_rt(dp, dev->phydev);
92 	if (err)
93 		goto clear_promisc;
94 
95 	return 0;
96 
97 clear_promisc:
98 	if (dev->flags & IFF_PROMISC)
99 		dev_set_promiscuity(master, -1);
100 clear_allmulti:
101 	if (dev->flags & IFF_ALLMULTI)
102 		dev_set_allmulti(master, -1);
103 del_unicast:
104 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
105 		dev_uc_del(master, dev->dev_addr);
106 out:
107 	return err;
108 }
109 
110 static int dsa_slave_close(struct net_device *dev)
111 {
112 	struct net_device *master = dsa_slave_to_master(dev);
113 	struct dsa_port *dp = dsa_slave_to_port(dev);
114 
115 	dsa_port_disable_rt(dp);
116 
117 	dev_mc_unsync(master, dev);
118 	dev_uc_unsync(master, dev);
119 	if (dev->flags & IFF_ALLMULTI)
120 		dev_set_allmulti(master, -1);
121 	if (dev->flags & IFF_PROMISC)
122 		dev_set_promiscuity(master, -1);
123 
124 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
125 		dev_uc_del(master, dev->dev_addr);
126 
127 	return 0;
128 }
129 
130 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
131 {
132 	struct net_device *master = dsa_slave_to_master(dev);
133 	if (dev->flags & IFF_UP) {
134 		if (change & IFF_ALLMULTI)
135 			dev_set_allmulti(master,
136 					 dev->flags & IFF_ALLMULTI ? 1 : -1);
137 		if (change & IFF_PROMISC)
138 			dev_set_promiscuity(master,
139 					    dev->flags & IFF_PROMISC ? 1 : -1);
140 	}
141 }
142 
143 static void dsa_slave_set_rx_mode(struct net_device *dev)
144 {
145 	struct net_device *master = dsa_slave_to_master(dev);
146 
147 	dev_mc_sync(master, dev);
148 	dev_uc_sync(master, dev);
149 }
150 
151 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
152 {
153 	struct net_device *master = dsa_slave_to_master(dev);
154 	struct sockaddr *addr = a;
155 	int err;
156 
157 	if (!is_valid_ether_addr(addr->sa_data))
158 		return -EADDRNOTAVAIL;
159 
160 	if (!(dev->flags & IFF_UP))
161 		goto out;
162 
163 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
164 		err = dev_uc_add(master, addr->sa_data);
165 		if (err < 0)
166 			return err;
167 	}
168 
169 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
170 		dev_uc_del(master, dev->dev_addr);
171 
172 out:
173 	ether_addr_copy(dev->dev_addr, addr->sa_data);
174 
175 	return 0;
176 }
177 
178 struct dsa_slave_dump_ctx {
179 	struct net_device *dev;
180 	struct sk_buff *skb;
181 	struct netlink_callback *cb;
182 	int idx;
183 };
184 
185 static int
186 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
187 			   bool is_static, void *data)
188 {
189 	struct dsa_slave_dump_ctx *dump = data;
190 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
191 	u32 seq = dump->cb->nlh->nlmsg_seq;
192 	struct nlmsghdr *nlh;
193 	struct ndmsg *ndm;
194 
195 	if (dump->idx < dump->cb->args[2])
196 		goto skip;
197 
198 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
199 			sizeof(*ndm), NLM_F_MULTI);
200 	if (!nlh)
201 		return -EMSGSIZE;
202 
203 	ndm = nlmsg_data(nlh);
204 	ndm->ndm_family  = AF_BRIDGE;
205 	ndm->ndm_pad1    = 0;
206 	ndm->ndm_pad2    = 0;
207 	ndm->ndm_flags   = NTF_SELF;
208 	ndm->ndm_type    = 0;
209 	ndm->ndm_ifindex = dump->dev->ifindex;
210 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
211 
212 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
213 		goto nla_put_failure;
214 
215 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
216 		goto nla_put_failure;
217 
218 	nlmsg_end(dump->skb, nlh);
219 
220 skip:
221 	dump->idx++;
222 	return 0;
223 
224 nla_put_failure:
225 	nlmsg_cancel(dump->skb, nlh);
226 	return -EMSGSIZE;
227 }
228 
229 static int
230 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
231 		   struct net_device *dev, struct net_device *filter_dev,
232 		   int *idx)
233 {
234 	struct dsa_port *dp = dsa_slave_to_port(dev);
235 	struct dsa_slave_dump_ctx dump = {
236 		.dev = dev,
237 		.skb = skb,
238 		.cb = cb,
239 		.idx = *idx,
240 	};
241 	int err;
242 
243 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
244 	*idx = dump.idx;
245 
246 	return err;
247 }
248 
249 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
250 {
251 	struct dsa_slave_priv *p = netdev_priv(dev);
252 	struct dsa_switch *ds = p->dp->ds;
253 	int port = p->dp->index;
254 
255 	/* Pass through to switch driver if it supports timestamping */
256 	switch (cmd) {
257 	case SIOCGHWTSTAMP:
258 		if (ds->ops->port_hwtstamp_get)
259 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
260 		break;
261 	case SIOCSHWTSTAMP:
262 		if (ds->ops->port_hwtstamp_set)
263 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
264 		break;
265 	}
266 
267 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
268 }
269 
270 static int dsa_slave_port_attr_set(struct net_device *dev,
271 				   const struct switchdev_attr *attr,
272 				   struct switchdev_trans *trans)
273 {
274 	struct dsa_port *dp = dsa_slave_to_port(dev);
275 	int ret;
276 
277 	switch (attr->id) {
278 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
279 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
280 		break;
281 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
282 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
283 					      trans);
284 		break;
285 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
286 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
287 		break;
288 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
289 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
290 						trans);
291 		break;
292 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
293 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
294 		break;
295 	case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
296 		ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans);
297 		break;
298 	default:
299 		ret = -EOPNOTSUPP;
300 		break;
301 	}
302 
303 	return ret;
304 }
305 
306 /* Must be called under rcu_read_lock() */
307 static int
308 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
309 				      const struct switchdev_obj_port_vlan *vlan)
310 {
311 	struct net_device *upper_dev;
312 	struct list_head *iter;
313 
314 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
315 		u16 vid;
316 
317 		if (!is_vlan_dev(upper_dev))
318 			continue;
319 
320 		vid = vlan_dev_vlan_id(upper_dev);
321 		if (vid >= vlan->vid_begin && vid <= vlan->vid_end)
322 			return -EBUSY;
323 	}
324 
325 	return 0;
326 }
327 
328 static int dsa_slave_vlan_add(struct net_device *dev,
329 			      const struct switchdev_obj *obj,
330 			      struct switchdev_trans *trans)
331 {
332 	struct net_device *master = dsa_slave_to_master(dev);
333 	struct dsa_port *dp = dsa_slave_to_port(dev);
334 	struct switchdev_obj_port_vlan vlan;
335 	int vid, err;
336 
337 	if (obj->orig_dev != dev)
338 		return -EOPNOTSUPP;
339 
340 	if (dsa_port_skip_vlan_configuration(dp))
341 		return 0;
342 
343 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
344 
345 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
346 	 * the same VID.
347 	 */
348 	if (trans->ph_prepare && br_vlan_enabled(dp->bridge_dev)) {
349 		rcu_read_lock();
350 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
351 		rcu_read_unlock();
352 		if (err)
353 			return err;
354 	}
355 
356 	err = dsa_port_vlan_add(dp, &vlan, trans);
357 	if (err)
358 		return err;
359 
360 	/* We need the dedicated CPU port to be a member of the VLAN as well.
361 	 * Even though drivers often handle CPU membership in special ways,
362 	 * it doesn't make sense to program a PVID, so clear this flag.
363 	 */
364 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
365 
366 	err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans);
367 	if (err)
368 		return err;
369 
370 	for (vid = vlan.vid_begin; vid <= vlan.vid_end; vid++) {
371 		err = vlan_vid_add(master, htons(ETH_P_8021Q), vid);
372 		if (err)
373 			return err;
374 	}
375 
376 	return 0;
377 }
378 
379 static int dsa_slave_port_obj_add(struct net_device *dev,
380 				  const struct switchdev_obj *obj,
381 				  struct switchdev_trans *trans,
382 				  struct netlink_ext_ack *extack)
383 {
384 	struct dsa_port *dp = dsa_slave_to_port(dev);
385 	int err;
386 
387 	/* For the prepare phase, ensure the full set of changes is feasable in
388 	 * one go in order to signal a failure properly. If an operation is not
389 	 * supported, return -EOPNOTSUPP.
390 	 */
391 
392 	switch (obj->id) {
393 	case SWITCHDEV_OBJ_ID_PORT_MDB:
394 		if (obj->orig_dev != dev)
395 			return -EOPNOTSUPP;
396 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
397 		break;
398 	case SWITCHDEV_OBJ_ID_HOST_MDB:
399 		/* DSA can directly translate this to a normal MDB add,
400 		 * but on the CPU port.
401 		 */
402 		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
403 				       trans);
404 		break;
405 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
406 		err = dsa_slave_vlan_add(dev, obj, trans);
407 		break;
408 	default:
409 		err = -EOPNOTSUPP;
410 		break;
411 	}
412 
413 	return err;
414 }
415 
416 static int dsa_slave_vlan_del(struct net_device *dev,
417 			      const struct switchdev_obj *obj)
418 {
419 	struct net_device *master = dsa_slave_to_master(dev);
420 	struct dsa_port *dp = dsa_slave_to_port(dev);
421 	struct switchdev_obj_port_vlan *vlan;
422 	int vid, err;
423 
424 	if (obj->orig_dev != dev)
425 		return -EOPNOTSUPP;
426 
427 	if (dsa_port_skip_vlan_configuration(dp))
428 		return 0;
429 
430 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
431 
432 	/* Do not deprogram the CPU port as it may be shared with other user
433 	 * ports which can be members of this VLAN as well.
434 	 */
435 	err = dsa_port_vlan_del(dp, vlan);
436 	if (err)
437 		return err;
438 
439 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
440 		vlan_vid_del(master, htons(ETH_P_8021Q), vid);
441 
442 	return 0;
443 }
444 
445 static int dsa_slave_port_obj_del(struct net_device *dev,
446 				  const struct switchdev_obj *obj)
447 {
448 	struct dsa_port *dp = dsa_slave_to_port(dev);
449 	int err;
450 
451 	switch (obj->id) {
452 	case SWITCHDEV_OBJ_ID_PORT_MDB:
453 		if (obj->orig_dev != dev)
454 			return -EOPNOTSUPP;
455 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
456 		break;
457 	case SWITCHDEV_OBJ_ID_HOST_MDB:
458 		/* DSA can directly translate this to a normal MDB add,
459 		 * but on the CPU port.
460 		 */
461 		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
462 		break;
463 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
464 		err = dsa_slave_vlan_del(dev, obj);
465 		break;
466 	default:
467 		err = -EOPNOTSUPP;
468 		break;
469 	}
470 
471 	return err;
472 }
473 
474 static int dsa_slave_get_port_parent_id(struct net_device *dev,
475 					struct netdev_phys_item_id *ppid)
476 {
477 	struct dsa_port *dp = dsa_slave_to_port(dev);
478 	struct dsa_switch *ds = dp->ds;
479 	struct dsa_switch_tree *dst = ds->dst;
480 
481 	/* For non-legacy ports, devlink is used and it takes
482 	 * care of the name generation. This ndo implementation
483 	 * should be removed with legacy support.
484 	 */
485 	if (dp->ds->devlink)
486 		return -EOPNOTSUPP;
487 
488 	ppid->id_len = sizeof(dst->index);
489 	memcpy(&ppid->id, &dst->index, ppid->id_len);
490 
491 	return 0;
492 }
493 
494 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
495 						     struct sk_buff *skb)
496 {
497 #ifdef CONFIG_NET_POLL_CONTROLLER
498 	struct dsa_slave_priv *p = netdev_priv(dev);
499 
500 	return netpoll_send_skb(p->netpoll, skb);
501 #else
502 	BUG();
503 	return NETDEV_TX_OK;
504 #endif
505 }
506 
507 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
508 				 struct sk_buff *skb)
509 {
510 	struct dsa_switch *ds = p->dp->ds;
511 	struct sk_buff *clone;
512 	unsigned int type;
513 
514 	type = ptp_classify_raw(skb);
515 	if (type == PTP_CLASS_NONE)
516 		return;
517 
518 	if (!ds->ops->port_txtstamp)
519 		return;
520 
521 	clone = skb_clone_sk(skb);
522 	if (!clone)
523 		return;
524 
525 	DSA_SKB_CB(skb)->clone = clone;
526 
527 	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
528 		return;
529 
530 	kfree_skb(clone);
531 }
532 
533 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
534 {
535 	/* SKB for netpoll still need to be mangled with the protocol-specific
536 	 * tag to be successfully transmitted
537 	 */
538 	if (unlikely(netpoll_tx_running(dev)))
539 		return dsa_slave_netpoll_send_skb(dev, skb);
540 
541 	/* Queue the SKB for transmission on the parent interface, but
542 	 * do not modify its EtherType
543 	 */
544 	skb->dev = dsa_slave_to_master(dev);
545 	dev_queue_xmit(skb);
546 
547 	return NETDEV_TX_OK;
548 }
549 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
550 
551 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
552 {
553 	struct dsa_slave_priv *p = netdev_priv(dev);
554 	struct pcpu_sw_netstats *s;
555 	struct sk_buff *nskb;
556 
557 	s = this_cpu_ptr(p->stats64);
558 	u64_stats_update_begin(&s->syncp);
559 	s->tx_packets++;
560 	s->tx_bytes += skb->len;
561 	u64_stats_update_end(&s->syncp);
562 
563 	DSA_SKB_CB(skb)->clone = NULL;
564 
565 	/* Identify PTP protocol packets, clone them, and pass them to the
566 	 * switch driver
567 	 */
568 	dsa_skb_tx_timestamp(p, skb);
569 
570 	/* Transmit function may have to reallocate the original SKB,
571 	 * in which case it must have freed it. Only free it here on error.
572 	 */
573 	nskb = p->xmit(skb, dev);
574 	if (!nskb) {
575 		kfree_skb(skb);
576 		return NETDEV_TX_OK;
577 	}
578 
579 	return dsa_enqueue_skb(nskb, dev);
580 }
581 
582 /* ethtool operations *******************************************************/
583 
584 static void dsa_slave_get_drvinfo(struct net_device *dev,
585 				  struct ethtool_drvinfo *drvinfo)
586 {
587 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
588 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
589 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
590 }
591 
592 static int dsa_slave_get_regs_len(struct net_device *dev)
593 {
594 	struct dsa_port *dp = dsa_slave_to_port(dev);
595 	struct dsa_switch *ds = dp->ds;
596 
597 	if (ds->ops->get_regs_len)
598 		return ds->ops->get_regs_len(ds, dp->index);
599 
600 	return -EOPNOTSUPP;
601 }
602 
603 static void
604 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
605 {
606 	struct dsa_port *dp = dsa_slave_to_port(dev);
607 	struct dsa_switch *ds = dp->ds;
608 
609 	if (ds->ops->get_regs)
610 		ds->ops->get_regs(ds, dp->index, regs, _p);
611 }
612 
613 static int dsa_slave_nway_reset(struct net_device *dev)
614 {
615 	struct dsa_port *dp = dsa_slave_to_port(dev);
616 
617 	return phylink_ethtool_nway_reset(dp->pl);
618 }
619 
620 static int dsa_slave_get_eeprom_len(struct net_device *dev)
621 {
622 	struct dsa_port *dp = dsa_slave_to_port(dev);
623 	struct dsa_switch *ds = dp->ds;
624 
625 	if (ds->cd && ds->cd->eeprom_len)
626 		return ds->cd->eeprom_len;
627 
628 	if (ds->ops->get_eeprom_len)
629 		return ds->ops->get_eeprom_len(ds);
630 
631 	return 0;
632 }
633 
634 static int dsa_slave_get_eeprom(struct net_device *dev,
635 				struct ethtool_eeprom *eeprom, u8 *data)
636 {
637 	struct dsa_port *dp = dsa_slave_to_port(dev);
638 	struct dsa_switch *ds = dp->ds;
639 
640 	if (ds->ops->get_eeprom)
641 		return ds->ops->get_eeprom(ds, eeprom, data);
642 
643 	return -EOPNOTSUPP;
644 }
645 
646 static int dsa_slave_set_eeprom(struct net_device *dev,
647 				struct ethtool_eeprom *eeprom, u8 *data)
648 {
649 	struct dsa_port *dp = dsa_slave_to_port(dev);
650 	struct dsa_switch *ds = dp->ds;
651 
652 	if (ds->ops->set_eeprom)
653 		return ds->ops->set_eeprom(ds, eeprom, data);
654 
655 	return -EOPNOTSUPP;
656 }
657 
658 static void dsa_slave_get_strings(struct net_device *dev,
659 				  uint32_t stringset, uint8_t *data)
660 {
661 	struct dsa_port *dp = dsa_slave_to_port(dev);
662 	struct dsa_switch *ds = dp->ds;
663 
664 	if (stringset == ETH_SS_STATS) {
665 		int len = ETH_GSTRING_LEN;
666 
667 		strncpy(data, "tx_packets", len);
668 		strncpy(data + len, "tx_bytes", len);
669 		strncpy(data + 2 * len, "rx_packets", len);
670 		strncpy(data + 3 * len, "rx_bytes", len);
671 		if (ds->ops->get_strings)
672 			ds->ops->get_strings(ds, dp->index, stringset,
673 					     data + 4 * len);
674 	}
675 }
676 
677 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
678 					struct ethtool_stats *stats,
679 					uint64_t *data)
680 {
681 	struct dsa_port *dp = dsa_slave_to_port(dev);
682 	struct dsa_slave_priv *p = netdev_priv(dev);
683 	struct dsa_switch *ds = dp->ds;
684 	struct pcpu_sw_netstats *s;
685 	unsigned int start;
686 	int i;
687 
688 	for_each_possible_cpu(i) {
689 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
690 
691 		s = per_cpu_ptr(p->stats64, i);
692 		do {
693 			start = u64_stats_fetch_begin_irq(&s->syncp);
694 			tx_packets = s->tx_packets;
695 			tx_bytes = s->tx_bytes;
696 			rx_packets = s->rx_packets;
697 			rx_bytes = s->rx_bytes;
698 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
699 		data[0] += tx_packets;
700 		data[1] += tx_bytes;
701 		data[2] += rx_packets;
702 		data[3] += rx_bytes;
703 	}
704 	if (ds->ops->get_ethtool_stats)
705 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
706 }
707 
708 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
709 {
710 	struct dsa_port *dp = dsa_slave_to_port(dev);
711 	struct dsa_switch *ds = dp->ds;
712 
713 	if (sset == ETH_SS_STATS) {
714 		int count;
715 
716 		count = 4;
717 		if (ds->ops->get_sset_count)
718 			count += ds->ops->get_sset_count(ds, dp->index, sset);
719 
720 		return count;
721 	}
722 
723 	return -EOPNOTSUPP;
724 }
725 
726 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
727 {
728 	struct dsa_port *dp = dsa_slave_to_port(dev);
729 	struct dsa_switch *ds = dp->ds;
730 
731 	phylink_ethtool_get_wol(dp->pl, w);
732 
733 	if (ds->ops->get_wol)
734 		ds->ops->get_wol(ds, dp->index, w);
735 }
736 
737 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
738 {
739 	struct dsa_port *dp = dsa_slave_to_port(dev);
740 	struct dsa_switch *ds = dp->ds;
741 	int ret = -EOPNOTSUPP;
742 
743 	phylink_ethtool_set_wol(dp->pl, w);
744 
745 	if (ds->ops->set_wol)
746 		ret = ds->ops->set_wol(ds, dp->index, w);
747 
748 	return ret;
749 }
750 
751 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
752 {
753 	struct dsa_port *dp = dsa_slave_to_port(dev);
754 	struct dsa_switch *ds = dp->ds;
755 	int ret;
756 
757 	/* Port's PHY and MAC both need to be EEE capable */
758 	if (!dev->phydev || !dp->pl)
759 		return -ENODEV;
760 
761 	if (!ds->ops->set_mac_eee)
762 		return -EOPNOTSUPP;
763 
764 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
765 	if (ret)
766 		return ret;
767 
768 	return phylink_ethtool_set_eee(dp->pl, e);
769 }
770 
771 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
772 {
773 	struct dsa_port *dp = dsa_slave_to_port(dev);
774 	struct dsa_switch *ds = dp->ds;
775 	int ret;
776 
777 	/* Port's PHY and MAC both need to be EEE capable */
778 	if (!dev->phydev || !dp->pl)
779 		return -ENODEV;
780 
781 	if (!ds->ops->get_mac_eee)
782 		return -EOPNOTSUPP;
783 
784 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
785 	if (ret)
786 		return ret;
787 
788 	return phylink_ethtool_get_eee(dp->pl, e);
789 }
790 
791 static int dsa_slave_get_link_ksettings(struct net_device *dev,
792 					struct ethtool_link_ksettings *cmd)
793 {
794 	struct dsa_port *dp = dsa_slave_to_port(dev);
795 
796 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
797 }
798 
799 static int dsa_slave_set_link_ksettings(struct net_device *dev,
800 					const struct ethtool_link_ksettings *cmd)
801 {
802 	struct dsa_port *dp = dsa_slave_to_port(dev);
803 
804 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
805 }
806 
807 static void dsa_slave_get_pauseparam(struct net_device *dev,
808 				     struct ethtool_pauseparam *pause)
809 {
810 	struct dsa_port *dp = dsa_slave_to_port(dev);
811 
812 	phylink_ethtool_get_pauseparam(dp->pl, pause);
813 }
814 
815 static int dsa_slave_set_pauseparam(struct net_device *dev,
816 				    struct ethtool_pauseparam *pause)
817 {
818 	struct dsa_port *dp = dsa_slave_to_port(dev);
819 
820 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
821 }
822 
823 #ifdef CONFIG_NET_POLL_CONTROLLER
824 static int dsa_slave_netpoll_setup(struct net_device *dev,
825 				   struct netpoll_info *ni)
826 {
827 	struct net_device *master = dsa_slave_to_master(dev);
828 	struct dsa_slave_priv *p = netdev_priv(dev);
829 	struct netpoll *netpoll;
830 	int err = 0;
831 
832 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
833 	if (!netpoll)
834 		return -ENOMEM;
835 
836 	err = __netpoll_setup(netpoll, master);
837 	if (err) {
838 		kfree(netpoll);
839 		goto out;
840 	}
841 
842 	p->netpoll = netpoll;
843 out:
844 	return err;
845 }
846 
847 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
848 {
849 	struct dsa_slave_priv *p = netdev_priv(dev);
850 	struct netpoll *netpoll = p->netpoll;
851 
852 	if (!netpoll)
853 		return;
854 
855 	p->netpoll = NULL;
856 
857 	__netpoll_free(netpoll);
858 }
859 
860 static void dsa_slave_poll_controller(struct net_device *dev)
861 {
862 }
863 #endif
864 
865 static int dsa_slave_get_phys_port_name(struct net_device *dev,
866 					char *name, size_t len)
867 {
868 	struct dsa_port *dp = dsa_slave_to_port(dev);
869 
870 	/* For non-legacy ports, devlink is used and it takes
871 	 * care of the name generation. This ndo implementation
872 	 * should be removed with legacy support.
873 	 */
874 	if (dp->ds->devlink)
875 		return -EOPNOTSUPP;
876 
877 	if (snprintf(name, len, "p%d", dp->index) >= len)
878 		return -EINVAL;
879 
880 	return 0;
881 }
882 
883 static struct dsa_mall_tc_entry *
884 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
885 {
886 	struct dsa_slave_priv *p = netdev_priv(dev);
887 	struct dsa_mall_tc_entry *mall_tc_entry;
888 
889 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
890 		if (mall_tc_entry->cookie == cookie)
891 			return mall_tc_entry;
892 
893 	return NULL;
894 }
895 
896 static int
897 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
898 				  struct tc_cls_matchall_offload *cls,
899 				  bool ingress)
900 {
901 	struct dsa_port *dp = dsa_slave_to_port(dev);
902 	struct dsa_slave_priv *p = netdev_priv(dev);
903 	struct dsa_mall_mirror_tc_entry *mirror;
904 	struct dsa_mall_tc_entry *mall_tc_entry;
905 	struct dsa_switch *ds = dp->ds;
906 	struct flow_action_entry *act;
907 	struct dsa_port *to_dp;
908 	int err;
909 
910 	if (!ds->ops->port_mirror_add)
911 		return -EOPNOTSUPP;
912 
913 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
914 					      cls->common.extack))
915 		return -EOPNOTSUPP;
916 
917 	act = &cls->rule->action.entries[0];
918 
919 	if (!act->dev)
920 		return -EINVAL;
921 
922 	if (!dsa_slave_dev_check(act->dev))
923 		return -EOPNOTSUPP;
924 
925 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
926 	if (!mall_tc_entry)
927 		return -ENOMEM;
928 
929 	mall_tc_entry->cookie = cls->cookie;
930 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
931 	mirror = &mall_tc_entry->mirror;
932 
933 	to_dp = dsa_slave_to_port(act->dev);
934 
935 	mirror->to_local_port = to_dp->index;
936 	mirror->ingress = ingress;
937 
938 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
939 	if (err) {
940 		kfree(mall_tc_entry);
941 		return err;
942 	}
943 
944 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
945 
946 	return err;
947 }
948 
949 static int
950 dsa_slave_add_cls_matchall_police(struct net_device *dev,
951 				  struct tc_cls_matchall_offload *cls,
952 				  bool ingress)
953 {
954 	struct netlink_ext_ack *extack = cls->common.extack;
955 	struct dsa_port *dp = dsa_slave_to_port(dev);
956 	struct dsa_slave_priv *p = netdev_priv(dev);
957 	struct dsa_mall_policer_tc_entry *policer;
958 	struct dsa_mall_tc_entry *mall_tc_entry;
959 	struct dsa_switch *ds = dp->ds;
960 	struct flow_action_entry *act;
961 	int err;
962 
963 	if (!ds->ops->port_policer_add) {
964 		NL_SET_ERR_MSG_MOD(extack,
965 				   "Policing offload not implemented");
966 		return -EOPNOTSUPP;
967 	}
968 
969 	if (!ingress) {
970 		NL_SET_ERR_MSG_MOD(extack,
971 				   "Only supported on ingress qdisc");
972 		return -EOPNOTSUPP;
973 	}
974 
975 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
976 					      cls->common.extack))
977 		return -EOPNOTSUPP;
978 
979 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
980 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
981 			NL_SET_ERR_MSG_MOD(extack,
982 					   "Only one port policer allowed");
983 			return -EEXIST;
984 		}
985 	}
986 
987 	act = &cls->rule->action.entries[0];
988 
989 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
990 	if (!mall_tc_entry)
991 		return -ENOMEM;
992 
993 	mall_tc_entry->cookie = cls->cookie;
994 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
995 	policer = &mall_tc_entry->policer;
996 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
997 	policer->burst = act->police.burst;
998 
999 	err = ds->ops->port_policer_add(ds, dp->index, policer);
1000 	if (err) {
1001 		kfree(mall_tc_entry);
1002 		return err;
1003 	}
1004 
1005 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1006 
1007 	return err;
1008 }
1009 
1010 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1011 				      struct tc_cls_matchall_offload *cls,
1012 				      bool ingress)
1013 {
1014 	int err = -EOPNOTSUPP;
1015 
1016 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1017 	    flow_offload_has_one_action(&cls->rule->action) &&
1018 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1019 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1020 	else if (flow_offload_has_one_action(&cls->rule->action) &&
1021 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1022 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1023 
1024 	return err;
1025 }
1026 
1027 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1028 				       struct tc_cls_matchall_offload *cls)
1029 {
1030 	struct dsa_port *dp = dsa_slave_to_port(dev);
1031 	struct dsa_mall_tc_entry *mall_tc_entry;
1032 	struct dsa_switch *ds = dp->ds;
1033 
1034 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1035 	if (!mall_tc_entry)
1036 		return;
1037 
1038 	list_del(&mall_tc_entry->list);
1039 
1040 	switch (mall_tc_entry->type) {
1041 	case DSA_PORT_MALL_MIRROR:
1042 		if (ds->ops->port_mirror_del)
1043 			ds->ops->port_mirror_del(ds, dp->index,
1044 						 &mall_tc_entry->mirror);
1045 		break;
1046 	case DSA_PORT_MALL_POLICER:
1047 		if (ds->ops->port_policer_del)
1048 			ds->ops->port_policer_del(ds, dp->index);
1049 		break;
1050 	default:
1051 		WARN_ON(1);
1052 	}
1053 
1054 	kfree(mall_tc_entry);
1055 }
1056 
1057 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1058 					   struct tc_cls_matchall_offload *cls,
1059 					   bool ingress)
1060 {
1061 	if (cls->common.chain_index)
1062 		return -EOPNOTSUPP;
1063 
1064 	switch (cls->command) {
1065 	case TC_CLSMATCHALL_REPLACE:
1066 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
1067 	case TC_CLSMATCHALL_DESTROY:
1068 		dsa_slave_del_cls_matchall(dev, cls);
1069 		return 0;
1070 	default:
1071 		return -EOPNOTSUPP;
1072 	}
1073 }
1074 
1075 static int dsa_slave_add_cls_flower(struct net_device *dev,
1076 				    struct flow_cls_offload *cls,
1077 				    bool ingress)
1078 {
1079 	struct dsa_port *dp = dsa_slave_to_port(dev);
1080 	struct dsa_switch *ds = dp->ds;
1081 	int port = dp->index;
1082 
1083 	if (!ds->ops->cls_flower_add)
1084 		return -EOPNOTSUPP;
1085 
1086 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1087 }
1088 
1089 static int dsa_slave_del_cls_flower(struct net_device *dev,
1090 				    struct flow_cls_offload *cls,
1091 				    bool ingress)
1092 {
1093 	struct dsa_port *dp = dsa_slave_to_port(dev);
1094 	struct dsa_switch *ds = dp->ds;
1095 	int port = dp->index;
1096 
1097 	if (!ds->ops->cls_flower_del)
1098 		return -EOPNOTSUPP;
1099 
1100 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1101 }
1102 
1103 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1104 				      struct flow_cls_offload *cls,
1105 				      bool ingress)
1106 {
1107 	struct dsa_port *dp = dsa_slave_to_port(dev);
1108 	struct dsa_switch *ds = dp->ds;
1109 	int port = dp->index;
1110 
1111 	if (!ds->ops->cls_flower_stats)
1112 		return -EOPNOTSUPP;
1113 
1114 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1115 }
1116 
1117 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1118 					 struct flow_cls_offload *cls,
1119 					 bool ingress)
1120 {
1121 	switch (cls->command) {
1122 	case FLOW_CLS_REPLACE:
1123 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1124 	case FLOW_CLS_DESTROY:
1125 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1126 	case FLOW_CLS_STATS:
1127 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1128 	default:
1129 		return -EOPNOTSUPP;
1130 	}
1131 }
1132 
1133 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1134 				       void *cb_priv, bool ingress)
1135 {
1136 	struct net_device *dev = cb_priv;
1137 
1138 	if (!tc_can_offload(dev))
1139 		return -EOPNOTSUPP;
1140 
1141 	switch (type) {
1142 	case TC_SETUP_CLSMATCHALL:
1143 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1144 	case TC_SETUP_CLSFLOWER:
1145 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1146 	default:
1147 		return -EOPNOTSUPP;
1148 	}
1149 }
1150 
1151 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1152 					  void *type_data, void *cb_priv)
1153 {
1154 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1155 }
1156 
1157 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1158 					  void *type_data, void *cb_priv)
1159 {
1160 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1161 }
1162 
1163 static LIST_HEAD(dsa_slave_block_cb_list);
1164 
1165 static int dsa_slave_setup_tc_block(struct net_device *dev,
1166 				    struct flow_block_offload *f)
1167 {
1168 	struct flow_block_cb *block_cb;
1169 	flow_setup_cb_t *cb;
1170 
1171 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1172 		cb = dsa_slave_setup_tc_block_cb_ig;
1173 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1174 		cb = dsa_slave_setup_tc_block_cb_eg;
1175 	else
1176 		return -EOPNOTSUPP;
1177 
1178 	f->driver_block_list = &dsa_slave_block_cb_list;
1179 
1180 	switch (f->command) {
1181 	case FLOW_BLOCK_BIND:
1182 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1183 			return -EBUSY;
1184 
1185 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1186 		if (IS_ERR(block_cb))
1187 			return PTR_ERR(block_cb);
1188 
1189 		flow_block_cb_add(block_cb, f);
1190 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1191 		return 0;
1192 	case FLOW_BLOCK_UNBIND:
1193 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1194 		if (!block_cb)
1195 			return -ENOENT;
1196 
1197 		flow_block_cb_remove(block_cb, f);
1198 		list_del(&block_cb->driver_list);
1199 		return 0;
1200 	default:
1201 		return -EOPNOTSUPP;
1202 	}
1203 }
1204 
1205 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1206 			      void *type_data)
1207 {
1208 	struct dsa_port *dp = dsa_slave_to_port(dev);
1209 	struct dsa_switch *ds = dp->ds;
1210 
1211 	if (type == TC_SETUP_BLOCK)
1212 		return dsa_slave_setup_tc_block(dev, type_data);
1213 
1214 	if (!ds->ops->port_setup_tc)
1215 		return -EOPNOTSUPP;
1216 
1217 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1218 }
1219 
1220 static void dsa_slave_get_stats64(struct net_device *dev,
1221 				  struct rtnl_link_stats64 *stats)
1222 {
1223 	struct dsa_slave_priv *p = netdev_priv(dev);
1224 
1225 	netdev_stats_to_stats64(stats, &dev->stats);
1226 	dev_fetch_sw_netstats(stats, p->stats64);
1227 }
1228 
1229 static int dsa_slave_get_rxnfc(struct net_device *dev,
1230 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1231 {
1232 	struct dsa_port *dp = dsa_slave_to_port(dev);
1233 	struct dsa_switch *ds = dp->ds;
1234 
1235 	if (!ds->ops->get_rxnfc)
1236 		return -EOPNOTSUPP;
1237 
1238 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1239 }
1240 
1241 static int dsa_slave_set_rxnfc(struct net_device *dev,
1242 			       struct ethtool_rxnfc *nfc)
1243 {
1244 	struct dsa_port *dp = dsa_slave_to_port(dev);
1245 	struct dsa_switch *ds = dp->ds;
1246 
1247 	if (!ds->ops->set_rxnfc)
1248 		return -EOPNOTSUPP;
1249 
1250 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1251 }
1252 
1253 static int dsa_slave_get_ts_info(struct net_device *dev,
1254 				 struct ethtool_ts_info *ts)
1255 {
1256 	struct dsa_slave_priv *p = netdev_priv(dev);
1257 	struct dsa_switch *ds = p->dp->ds;
1258 
1259 	if (!ds->ops->get_ts_info)
1260 		return -EOPNOTSUPP;
1261 
1262 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
1263 }
1264 
1265 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1266 				     u16 vid)
1267 {
1268 	struct net_device *master = dsa_slave_to_master(dev);
1269 	struct dsa_port *dp = dsa_slave_to_port(dev);
1270 	struct switchdev_obj_port_vlan vlan = {
1271 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1272 		.vid_begin = vid,
1273 		.vid_end = vid,
1274 		/* This API only allows programming tagged, non-PVID VIDs */
1275 		.flags = 0,
1276 	};
1277 	struct switchdev_trans trans;
1278 	int ret;
1279 
1280 	/* User port... */
1281 	trans.ph_prepare = true;
1282 	ret = dsa_port_vlan_add(dp, &vlan, &trans);
1283 	if (ret)
1284 		return ret;
1285 
1286 	trans.ph_prepare = false;
1287 	ret = dsa_port_vlan_add(dp, &vlan, &trans);
1288 	if (ret)
1289 		return ret;
1290 
1291 	/* And CPU port... */
1292 	trans.ph_prepare = true;
1293 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &trans);
1294 	if (ret)
1295 		return ret;
1296 
1297 	trans.ph_prepare = false;
1298 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &trans);
1299 	if (ret)
1300 		return ret;
1301 
1302 	return vlan_vid_add(master, proto, vid);
1303 }
1304 
1305 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1306 				      u16 vid)
1307 {
1308 	struct net_device *master = dsa_slave_to_master(dev);
1309 	struct dsa_port *dp = dsa_slave_to_port(dev);
1310 	struct switchdev_obj_port_vlan vlan = {
1311 		.vid_begin = vid,
1312 		.vid_end = vid,
1313 		/* This API only allows programming tagged, non-PVID VIDs */
1314 		.flags = 0,
1315 	};
1316 	int err;
1317 
1318 	/* Do not deprogram the CPU port as it may be shared with other user
1319 	 * ports which can be members of this VLAN as well.
1320 	 */
1321 	err = dsa_port_vlan_del(dp, &vlan);
1322 	if (err)
1323 		return err;
1324 
1325 	vlan_vid_del(master, proto, vid);
1326 
1327 	return 0;
1328 }
1329 
1330 struct dsa_hw_port {
1331 	struct list_head list;
1332 	struct net_device *dev;
1333 	int old_mtu;
1334 };
1335 
1336 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1337 {
1338 	const struct dsa_hw_port *p;
1339 	int err;
1340 
1341 	list_for_each_entry(p, hw_port_list, list) {
1342 		if (p->dev->mtu == mtu)
1343 			continue;
1344 
1345 		err = dev_set_mtu(p->dev, mtu);
1346 		if (err)
1347 			goto rollback;
1348 	}
1349 
1350 	return 0;
1351 
1352 rollback:
1353 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1354 		if (p->dev->mtu == p->old_mtu)
1355 			continue;
1356 
1357 		if (dev_set_mtu(p->dev, p->old_mtu))
1358 			netdev_err(p->dev, "Failed to restore MTU\n");
1359 	}
1360 
1361 	return err;
1362 }
1363 
1364 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1365 {
1366 	struct dsa_hw_port *p, *n;
1367 
1368 	list_for_each_entry_safe(p, n, hw_port_list, list)
1369 		kfree(p);
1370 }
1371 
1372 /* Make the hardware datapath to/from @dev limited to a common MTU */
1373 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1374 {
1375 	struct list_head hw_port_list;
1376 	struct dsa_switch_tree *dst;
1377 	int min_mtu = ETH_MAX_MTU;
1378 	struct dsa_port *other_dp;
1379 	int err;
1380 
1381 	if (!dp->ds->mtu_enforcement_ingress)
1382 		return;
1383 
1384 	if (!dp->bridge_dev)
1385 		return;
1386 
1387 	INIT_LIST_HEAD(&hw_port_list);
1388 
1389 	/* Populate the list of ports that are part of the same bridge
1390 	 * as the newly added/modified port
1391 	 */
1392 	list_for_each_entry(dst, &dsa_tree_list, list) {
1393 		list_for_each_entry(other_dp, &dst->ports, list) {
1394 			struct dsa_hw_port *hw_port;
1395 			struct net_device *slave;
1396 
1397 			if (other_dp->type != DSA_PORT_TYPE_USER)
1398 				continue;
1399 
1400 			if (other_dp->bridge_dev != dp->bridge_dev)
1401 				continue;
1402 
1403 			if (!other_dp->ds->mtu_enforcement_ingress)
1404 				continue;
1405 
1406 			slave = other_dp->slave;
1407 
1408 			if (min_mtu > slave->mtu)
1409 				min_mtu = slave->mtu;
1410 
1411 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1412 			if (!hw_port)
1413 				goto out;
1414 
1415 			hw_port->dev = slave;
1416 			hw_port->old_mtu = slave->mtu;
1417 
1418 			list_add(&hw_port->list, &hw_port_list);
1419 		}
1420 	}
1421 
1422 	/* Attempt to configure the entire hardware bridge to the newly added
1423 	 * interface's MTU first, regardless of whether the intention of the
1424 	 * user was to raise or lower it.
1425 	 */
1426 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1427 	if (!err)
1428 		goto out;
1429 
1430 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1431 	 * all hardware bridge ports now. If this fails too, then all ports will
1432 	 * still have their old MTU rolled back anyway.
1433 	 */
1434 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1435 
1436 out:
1437 	dsa_hw_port_list_free(&hw_port_list);
1438 }
1439 
1440 static int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1441 {
1442 	struct net_device *master = dsa_slave_to_master(dev);
1443 	struct dsa_port *dp = dsa_slave_to_port(dev);
1444 	struct dsa_slave_priv *p = netdev_priv(dev);
1445 	struct dsa_switch *ds = p->dp->ds;
1446 	struct dsa_port *cpu_dp;
1447 	int port = p->dp->index;
1448 	int largest_mtu = 0;
1449 	int new_master_mtu;
1450 	int old_master_mtu;
1451 	int mtu_limit;
1452 	int cpu_mtu;
1453 	int err, i;
1454 
1455 	if (!ds->ops->port_change_mtu)
1456 		return -EOPNOTSUPP;
1457 
1458 	for (i = 0; i < ds->num_ports; i++) {
1459 		int slave_mtu;
1460 
1461 		if (!dsa_is_user_port(ds, i))
1462 			continue;
1463 
1464 		/* During probe, this function will be called for each slave
1465 		 * device, while not all of them have been allocated. That's
1466 		 * ok, it doesn't change what the maximum is, so ignore it.
1467 		 */
1468 		if (!dsa_to_port(ds, i)->slave)
1469 			continue;
1470 
1471 		/* Pretend that we already applied the setting, which we
1472 		 * actually haven't (still haven't done all integrity checks)
1473 		 */
1474 		if (i == port)
1475 			slave_mtu = new_mtu;
1476 		else
1477 			slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1478 
1479 		if (largest_mtu < slave_mtu)
1480 			largest_mtu = slave_mtu;
1481 	}
1482 
1483 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1484 
1485 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1486 	old_master_mtu = master->mtu;
1487 	new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1488 	if (new_master_mtu > mtu_limit)
1489 		return -ERANGE;
1490 
1491 	/* If the master MTU isn't over limit, there's no need to check the CPU
1492 	 * MTU, since that surely isn't either.
1493 	 */
1494 	cpu_mtu = largest_mtu;
1495 
1496 	/* Start applying stuff */
1497 	if (new_master_mtu != old_master_mtu) {
1498 		err = dev_set_mtu(master, new_master_mtu);
1499 		if (err < 0)
1500 			goto out_master_failed;
1501 
1502 		/* We only need to propagate the MTU of the CPU port to
1503 		 * upstream switches.
1504 		 */
1505 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1506 		if (err)
1507 			goto out_cpu_failed;
1508 	}
1509 
1510 	err = dsa_port_mtu_change(dp, new_mtu, false);
1511 	if (err)
1512 		goto out_port_failed;
1513 
1514 	dev->mtu = new_mtu;
1515 
1516 	dsa_bridge_mtu_normalization(dp);
1517 
1518 	return 0;
1519 
1520 out_port_failed:
1521 	if (new_master_mtu != old_master_mtu)
1522 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1523 				    cpu_dp->tag_ops->overhead,
1524 				    true);
1525 out_cpu_failed:
1526 	if (new_master_mtu != old_master_mtu)
1527 		dev_set_mtu(master, old_master_mtu);
1528 out_master_failed:
1529 	return err;
1530 }
1531 
1532 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1533 	.get_drvinfo		= dsa_slave_get_drvinfo,
1534 	.get_regs_len		= dsa_slave_get_regs_len,
1535 	.get_regs		= dsa_slave_get_regs,
1536 	.nway_reset		= dsa_slave_nway_reset,
1537 	.get_link		= ethtool_op_get_link,
1538 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
1539 	.get_eeprom		= dsa_slave_get_eeprom,
1540 	.set_eeprom		= dsa_slave_set_eeprom,
1541 	.get_strings		= dsa_slave_get_strings,
1542 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
1543 	.get_sset_count		= dsa_slave_get_sset_count,
1544 	.set_wol		= dsa_slave_set_wol,
1545 	.get_wol		= dsa_slave_get_wol,
1546 	.set_eee		= dsa_slave_set_eee,
1547 	.get_eee		= dsa_slave_get_eee,
1548 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1549 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1550 	.get_pauseparam		= dsa_slave_get_pauseparam,
1551 	.set_pauseparam		= dsa_slave_set_pauseparam,
1552 	.get_rxnfc		= dsa_slave_get_rxnfc,
1553 	.set_rxnfc		= dsa_slave_set_rxnfc,
1554 	.get_ts_info		= dsa_slave_get_ts_info,
1555 };
1556 
1557 /* legacy way, bypassing the bridge *****************************************/
1558 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1559 		       struct net_device *dev,
1560 		       const unsigned char *addr, u16 vid,
1561 		       u16 flags,
1562 		       struct netlink_ext_ack *extack)
1563 {
1564 	struct dsa_port *dp = dsa_slave_to_port(dev);
1565 
1566 	return dsa_port_fdb_add(dp, addr, vid);
1567 }
1568 
1569 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1570 		       struct net_device *dev,
1571 		       const unsigned char *addr, u16 vid)
1572 {
1573 	struct dsa_port *dp = dsa_slave_to_port(dev);
1574 
1575 	return dsa_port_fdb_del(dp, addr, vid);
1576 }
1577 
1578 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1579 {
1580 	struct dsa_port *dp = dsa_slave_to_port(dev);
1581 
1582 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1583 }
1584 
1585 static const struct net_device_ops dsa_slave_netdev_ops = {
1586 	.ndo_open	 	= dsa_slave_open,
1587 	.ndo_stop		= dsa_slave_close,
1588 	.ndo_start_xmit		= dsa_slave_xmit,
1589 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1590 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1591 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
1592 	.ndo_fdb_add		= dsa_legacy_fdb_add,
1593 	.ndo_fdb_del		= dsa_legacy_fdb_del,
1594 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1595 	.ndo_do_ioctl		= dsa_slave_ioctl,
1596 	.ndo_get_iflink		= dsa_slave_get_iflink,
1597 #ifdef CONFIG_NET_POLL_CONTROLLER
1598 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
1599 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
1600 	.ndo_poll_controller	= dsa_slave_poll_controller,
1601 #endif
1602 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1603 	.ndo_setup_tc		= dsa_slave_setup_tc,
1604 	.ndo_get_stats64	= dsa_slave_get_stats64,
1605 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1606 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1607 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1608 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1609 	.ndo_change_mtu		= dsa_slave_change_mtu,
1610 };
1611 
1612 static struct device_type dsa_type = {
1613 	.name	= "dsa",
1614 };
1615 
1616 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1617 {
1618 	const struct dsa_port *dp = dsa_to_port(ds, port);
1619 
1620 	if (dp->pl)
1621 		phylink_mac_change(dp->pl, up);
1622 }
1623 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1624 
1625 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1626 					  struct phylink_link_state *state)
1627 {
1628 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1629 	struct dsa_switch *ds = dp->ds;
1630 
1631 	/* No need to check that this operation is valid, the callback would
1632 	 * not be called if it was not.
1633 	 */
1634 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1635 }
1636 
1637 /* slave device setup *******************************************************/
1638 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1639 {
1640 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1641 	struct dsa_switch *ds = dp->ds;
1642 
1643 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1644 	if (!slave_dev->phydev) {
1645 		netdev_err(slave_dev, "no phy at %d\n", addr);
1646 		return -ENODEV;
1647 	}
1648 
1649 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1650 }
1651 
1652 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1653 {
1654 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1655 	struct device_node *port_dn = dp->dn;
1656 	struct dsa_switch *ds = dp->ds;
1657 	phy_interface_t mode;
1658 	u32 phy_flags = 0;
1659 	int ret;
1660 
1661 	ret = of_get_phy_mode(port_dn, &mode);
1662 	if (ret)
1663 		mode = PHY_INTERFACE_MODE_NA;
1664 
1665 	dp->pl_config.dev = &slave_dev->dev;
1666 	dp->pl_config.type = PHYLINK_NETDEV;
1667 
1668 	/* The get_fixed_state callback takes precedence over polling the
1669 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
1670 	 * this if the switch provides such a callback.
1671 	 */
1672 	if (ds->ops->phylink_fixed_state) {
1673 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
1674 		dp->pl_config.poll_fixed_state = true;
1675 	}
1676 
1677 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1678 				&dsa_port_phylink_mac_ops);
1679 	if (IS_ERR(dp->pl)) {
1680 		netdev_err(slave_dev,
1681 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1682 		return PTR_ERR(dp->pl);
1683 	}
1684 
1685 	if (ds->ops->get_phy_flags)
1686 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1687 
1688 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1689 	if (ret == -ENODEV && ds->slave_mii_bus) {
1690 		/* We could not connect to a designated PHY or SFP, so try to
1691 		 * use the switch internal MDIO bus instead
1692 		 */
1693 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1694 		if (ret) {
1695 			netdev_err(slave_dev,
1696 				   "failed to connect to port %d: %d\n",
1697 				   dp->index, ret);
1698 			phylink_destroy(dp->pl);
1699 			return ret;
1700 		}
1701 	}
1702 
1703 	return ret;
1704 }
1705 
1706 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1707 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1708 					    struct netdev_queue *txq,
1709 					    void *_unused)
1710 {
1711 	lockdep_set_class(&txq->_xmit_lock,
1712 			  &dsa_slave_netdev_xmit_lock_key);
1713 }
1714 
1715 int dsa_slave_suspend(struct net_device *slave_dev)
1716 {
1717 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1718 
1719 	if (!netif_running(slave_dev))
1720 		return 0;
1721 
1722 	netif_device_detach(slave_dev);
1723 
1724 	rtnl_lock();
1725 	phylink_stop(dp->pl);
1726 	rtnl_unlock();
1727 
1728 	return 0;
1729 }
1730 
1731 int dsa_slave_resume(struct net_device *slave_dev)
1732 {
1733 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1734 
1735 	if (!netif_running(slave_dev))
1736 		return 0;
1737 
1738 	netif_device_attach(slave_dev);
1739 
1740 	rtnl_lock();
1741 	phylink_start(dp->pl);
1742 	rtnl_unlock();
1743 
1744 	return 0;
1745 }
1746 
1747 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1748 {
1749 	struct net_device *master = dsa_slave_to_master(dev);
1750 	struct dsa_port *dp = dsa_slave_to_port(dev);
1751 	struct dsa_notifier_register_info rinfo = {
1752 		.switch_number = dp->ds->index,
1753 		.port_number = dp->index,
1754 		.master = master,
1755 		.info.dev = dev,
1756 	};
1757 
1758 	call_dsa_notifiers(val, dev, &rinfo.info);
1759 }
1760 
1761 int dsa_slave_create(struct dsa_port *port)
1762 {
1763 	const struct dsa_port *cpu_dp = port->cpu_dp;
1764 	struct net_device *master = cpu_dp->master;
1765 	struct dsa_switch *ds = port->ds;
1766 	const char *name = port->name;
1767 	struct net_device *slave_dev;
1768 	struct dsa_slave_priv *p;
1769 	int ret;
1770 
1771 	if (!ds->num_tx_queues)
1772 		ds->num_tx_queues = 1;
1773 
1774 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1775 				     NET_NAME_UNKNOWN, ether_setup,
1776 				     ds->num_tx_queues, 1);
1777 	if (slave_dev == NULL)
1778 		return -ENOMEM;
1779 
1780 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1781 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
1782 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1783 	slave_dev->hw_features |= NETIF_F_HW_TC;
1784 	slave_dev->features |= NETIF_F_LLTX;
1785 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1786 	if (!IS_ERR_OR_NULL(port->mac))
1787 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1788 	else
1789 		eth_hw_addr_inherit(slave_dev, master);
1790 	slave_dev->priv_flags |= IFF_NO_QUEUE;
1791 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1792 	if (ds->ops->port_max_mtu)
1793 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1794 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1795 
1796 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1797 				 NULL);
1798 
1799 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
1800 	slave_dev->dev.of_node = port->dn;
1801 	slave_dev->vlan_features = master->vlan_features;
1802 
1803 	p = netdev_priv(slave_dev);
1804 	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1805 	if (!p->stats64) {
1806 		free_netdev(slave_dev);
1807 		return -ENOMEM;
1808 	}
1809 
1810 	ret = gro_cells_init(&p->gcells, slave_dev);
1811 	if (ret)
1812 		goto out_free;
1813 
1814 	p->dp = port;
1815 	INIT_LIST_HEAD(&p->mall_tc_list);
1816 	p->xmit = cpu_dp->tag_ops->xmit;
1817 	port->slave = slave_dev;
1818 
1819 	rtnl_lock();
1820 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1821 	rtnl_unlock();
1822 	if (ret && ret != -EOPNOTSUPP)
1823 		dev_warn(ds->dev, "nonfatal error %d setting MTU on port %d\n",
1824 			 ret, port->index);
1825 
1826 	netif_carrier_off(slave_dev);
1827 
1828 	ret = dsa_slave_phy_setup(slave_dev);
1829 	if (ret) {
1830 		netdev_err(slave_dev,
1831 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
1832 			   ret, ds->dst->index, ds->index, port->index);
1833 		goto out_gcells;
1834 	}
1835 
1836 	dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1837 
1838 	rtnl_lock();
1839 
1840 	ret = register_netdevice(slave_dev);
1841 	if (ret) {
1842 		netdev_err(master, "error %d registering interface %s\n",
1843 			   ret, slave_dev->name);
1844 		rtnl_unlock();
1845 		goto out_phy;
1846 	}
1847 
1848 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
1849 
1850 	rtnl_unlock();
1851 
1852 	if (ret)
1853 		goto out_unregister;
1854 
1855 	return 0;
1856 
1857 out_unregister:
1858 	unregister_netdev(slave_dev);
1859 out_phy:
1860 	rtnl_lock();
1861 	phylink_disconnect_phy(p->dp->pl);
1862 	rtnl_unlock();
1863 	phylink_destroy(p->dp->pl);
1864 out_gcells:
1865 	gro_cells_destroy(&p->gcells);
1866 out_free:
1867 	free_percpu(p->stats64);
1868 	free_netdev(slave_dev);
1869 	port->slave = NULL;
1870 	return ret;
1871 }
1872 
1873 void dsa_slave_destroy(struct net_device *slave_dev)
1874 {
1875 	struct net_device *master = dsa_slave_to_master(slave_dev);
1876 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1877 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1878 
1879 	netif_carrier_off(slave_dev);
1880 	rtnl_lock();
1881 	netdev_upper_dev_unlink(master, slave_dev);
1882 	unregister_netdevice(slave_dev);
1883 	phylink_disconnect_phy(dp->pl);
1884 	rtnl_unlock();
1885 
1886 	dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1887 	phylink_destroy(dp->pl);
1888 	gro_cells_destroy(&p->gcells);
1889 	free_percpu(p->stats64);
1890 	free_netdev(slave_dev);
1891 }
1892 
1893 bool dsa_slave_dev_check(const struct net_device *dev)
1894 {
1895 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1896 }
1897 
1898 static int dsa_slave_changeupper(struct net_device *dev,
1899 				 struct netdev_notifier_changeupper_info *info)
1900 {
1901 	struct dsa_port *dp = dsa_slave_to_port(dev);
1902 	int err = NOTIFY_DONE;
1903 
1904 	if (netif_is_bridge_master(info->upper_dev)) {
1905 		if (info->linking) {
1906 			err = dsa_port_bridge_join(dp, info->upper_dev);
1907 			if (!err)
1908 				dsa_bridge_mtu_normalization(dp);
1909 			err = notifier_from_errno(err);
1910 		} else {
1911 			dsa_port_bridge_leave(dp, info->upper_dev);
1912 			err = NOTIFY_OK;
1913 		}
1914 	}
1915 
1916 	return err;
1917 }
1918 
1919 static int
1920 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
1921 				 struct netdev_notifier_changeupper_info *info)
1922 {
1923 	struct netlink_ext_ack *ext_ack;
1924 	struct net_device *slave;
1925 	struct dsa_port *dp;
1926 
1927 	ext_ack = netdev_notifier_info_to_extack(&info->info);
1928 
1929 	if (!is_vlan_dev(dev))
1930 		return NOTIFY_DONE;
1931 
1932 	slave = vlan_dev_real_dev(dev);
1933 	if (!dsa_slave_dev_check(slave))
1934 		return NOTIFY_DONE;
1935 
1936 	dp = dsa_slave_to_port(slave);
1937 	if (!dp->bridge_dev)
1938 		return NOTIFY_DONE;
1939 
1940 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
1941 	if (br_vlan_enabled(dp->bridge_dev) &&
1942 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
1943 		NL_SET_ERR_MSG_MOD(ext_ack,
1944 				   "Cannot enslave VLAN device into VLAN aware bridge");
1945 		return notifier_from_errno(-EINVAL);
1946 	}
1947 
1948 	return NOTIFY_DONE;
1949 }
1950 
1951 static int
1952 dsa_slave_check_8021q_upper(struct net_device *dev,
1953 			    struct netdev_notifier_changeupper_info *info)
1954 {
1955 	struct dsa_port *dp = dsa_slave_to_port(dev);
1956 	struct net_device *br = dp->bridge_dev;
1957 	struct bridge_vlan_info br_info;
1958 	struct netlink_ext_ack *extack;
1959 	int err = NOTIFY_DONE;
1960 	u16 vid;
1961 
1962 	if (!br || !br_vlan_enabled(br))
1963 		return NOTIFY_DONE;
1964 
1965 	extack = netdev_notifier_info_to_extack(&info->info);
1966 	vid = vlan_dev_vlan_id(info->upper_dev);
1967 
1968 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1969 	 * device, respectively the VID is not found, returning
1970 	 * 0 means success, which is a failure for us here.
1971 	 */
1972 	err = br_vlan_get_info(br, vid, &br_info);
1973 	if (err == 0) {
1974 		NL_SET_ERR_MSG_MOD(extack,
1975 				   "This VLAN is already configured by the bridge");
1976 		return notifier_from_errno(-EBUSY);
1977 	}
1978 
1979 	return NOTIFY_DONE;
1980 }
1981 
1982 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1983 				     unsigned long event, void *ptr)
1984 {
1985 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1986 
1987 	switch (event) {
1988 	case NETDEV_PRECHANGEUPPER: {
1989 		struct netdev_notifier_changeupper_info *info = ptr;
1990 
1991 		if (!dsa_slave_dev_check(dev))
1992 			return dsa_prevent_bridging_8021q_upper(dev, ptr);
1993 
1994 		if (is_vlan_dev(info->upper_dev))
1995 			return dsa_slave_check_8021q_upper(dev, ptr);
1996 		break;
1997 	}
1998 	case NETDEV_CHANGEUPPER:
1999 		if (!dsa_slave_dev_check(dev))
2000 			return NOTIFY_DONE;
2001 
2002 		return dsa_slave_changeupper(dev, ptr);
2003 	}
2004 
2005 	return NOTIFY_DONE;
2006 }
2007 
2008 struct dsa_switchdev_event_work {
2009 	struct work_struct work;
2010 	struct switchdev_notifier_fdb_info fdb_info;
2011 	struct net_device *dev;
2012 	unsigned long event;
2013 };
2014 
2015 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2016 {
2017 	struct dsa_switchdev_event_work *switchdev_work =
2018 		container_of(work, struct dsa_switchdev_event_work, work);
2019 	struct net_device *dev = switchdev_work->dev;
2020 	struct switchdev_notifier_fdb_info *fdb_info;
2021 	struct dsa_port *dp = dsa_slave_to_port(dev);
2022 	int err;
2023 
2024 	rtnl_lock();
2025 	switch (switchdev_work->event) {
2026 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2027 		fdb_info = &switchdev_work->fdb_info;
2028 		if (!fdb_info->added_by_user)
2029 			break;
2030 
2031 		err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
2032 		if (err) {
2033 			netdev_dbg(dev, "fdb add failed err=%d\n", err);
2034 			break;
2035 		}
2036 		fdb_info->offloaded = true;
2037 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
2038 					 &fdb_info->info, NULL);
2039 		break;
2040 
2041 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2042 		fdb_info = &switchdev_work->fdb_info;
2043 		if (!fdb_info->added_by_user)
2044 			break;
2045 
2046 		err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
2047 		if (err) {
2048 			netdev_dbg(dev, "fdb del failed err=%d\n", err);
2049 			dev_close(dev);
2050 		}
2051 		break;
2052 	}
2053 	rtnl_unlock();
2054 
2055 	kfree(switchdev_work->fdb_info.addr);
2056 	kfree(switchdev_work);
2057 	dev_put(dev);
2058 }
2059 
2060 static int
2061 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
2062 				  switchdev_work,
2063 				  const struct switchdev_notifier_fdb_info *
2064 				  fdb_info)
2065 {
2066 	memcpy(&switchdev_work->fdb_info, fdb_info,
2067 	       sizeof(switchdev_work->fdb_info));
2068 	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2069 	if (!switchdev_work->fdb_info.addr)
2070 		return -ENOMEM;
2071 	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2072 			fdb_info->addr);
2073 	return 0;
2074 }
2075 
2076 /* Called under rcu_read_lock() */
2077 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2078 				     unsigned long event, void *ptr)
2079 {
2080 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2081 	struct dsa_switchdev_event_work *switchdev_work;
2082 	int err;
2083 
2084 	if (event == SWITCHDEV_PORT_ATTR_SET) {
2085 		err = switchdev_handle_port_attr_set(dev, ptr,
2086 						     dsa_slave_dev_check,
2087 						     dsa_slave_port_attr_set);
2088 		return notifier_from_errno(err);
2089 	}
2090 
2091 	if (!dsa_slave_dev_check(dev))
2092 		return NOTIFY_DONE;
2093 
2094 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2095 	if (!switchdev_work)
2096 		return NOTIFY_BAD;
2097 
2098 	INIT_WORK(&switchdev_work->work,
2099 		  dsa_slave_switchdev_event_work);
2100 	switchdev_work->dev = dev;
2101 	switchdev_work->event = event;
2102 
2103 	switch (event) {
2104 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2105 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2106 		if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
2107 			goto err_fdb_work_init;
2108 		dev_hold(dev);
2109 		break;
2110 	default:
2111 		kfree(switchdev_work);
2112 		return NOTIFY_DONE;
2113 	}
2114 
2115 	dsa_schedule_work(&switchdev_work->work);
2116 	return NOTIFY_OK;
2117 
2118 err_fdb_work_init:
2119 	kfree(switchdev_work);
2120 	return NOTIFY_BAD;
2121 }
2122 
2123 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2124 					      unsigned long event, void *ptr)
2125 {
2126 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2127 	int err;
2128 
2129 	switch (event) {
2130 	case SWITCHDEV_PORT_OBJ_ADD:
2131 		err = switchdev_handle_port_obj_add(dev, ptr,
2132 						    dsa_slave_dev_check,
2133 						    dsa_slave_port_obj_add);
2134 		return notifier_from_errno(err);
2135 	case SWITCHDEV_PORT_OBJ_DEL:
2136 		err = switchdev_handle_port_obj_del(dev, ptr,
2137 						    dsa_slave_dev_check,
2138 						    dsa_slave_port_obj_del);
2139 		return notifier_from_errno(err);
2140 	case SWITCHDEV_PORT_ATTR_SET:
2141 		err = switchdev_handle_port_attr_set(dev, ptr,
2142 						     dsa_slave_dev_check,
2143 						     dsa_slave_port_attr_set);
2144 		return notifier_from_errno(err);
2145 	}
2146 
2147 	return NOTIFY_DONE;
2148 }
2149 
2150 static struct notifier_block dsa_slave_nb __read_mostly = {
2151 	.notifier_call  = dsa_slave_netdevice_event,
2152 };
2153 
2154 static struct notifier_block dsa_slave_switchdev_notifier = {
2155 	.notifier_call = dsa_slave_switchdev_event,
2156 };
2157 
2158 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
2159 	.notifier_call = dsa_slave_switchdev_blocking_event,
2160 };
2161 
2162 int dsa_slave_register_notifier(void)
2163 {
2164 	struct notifier_block *nb;
2165 	int err;
2166 
2167 	err = register_netdevice_notifier(&dsa_slave_nb);
2168 	if (err)
2169 		return err;
2170 
2171 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2172 	if (err)
2173 		goto err_switchdev_nb;
2174 
2175 	nb = &dsa_slave_switchdev_blocking_notifier;
2176 	err = register_switchdev_blocking_notifier(nb);
2177 	if (err)
2178 		goto err_switchdev_blocking_nb;
2179 
2180 	return 0;
2181 
2182 err_switchdev_blocking_nb:
2183 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2184 err_switchdev_nb:
2185 	unregister_netdevice_notifier(&dsa_slave_nb);
2186 	return err;
2187 }
2188 
2189 void dsa_slave_unregister_notifier(void)
2190 {
2191 	struct notifier_block *nb;
2192 	int err;
2193 
2194 	nb = &dsa_slave_switchdev_blocking_notifier;
2195 	err = unregister_switchdev_blocking_notifier(nb);
2196 	if (err)
2197 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
2198 
2199 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2200 	if (err)
2201 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2202 
2203 	err = unregister_netdevice_notifier(&dsa_slave_nb);
2204 	if (err)
2205 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
2206 }
2207