xref: /openbmc/linux/net/dsa/slave.c (revision 15a83487)
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/selftests.h>
19 #include <net/tc_act/tc_mirred.h>
20 #include <linux/if_bridge.h>
21 #include <linux/if_hsr.h>
22 #include <net/dcbnl.h>
23 #include <linux/netpoll.h>
24 
25 #include "dsa_priv.h"
26 
27 static void dsa_slave_standalone_event_work(struct work_struct *work)
28 {
29 	struct dsa_standalone_event_work *standalone_work =
30 		container_of(work, struct dsa_standalone_event_work, work);
31 	const unsigned char *addr = standalone_work->addr;
32 	struct net_device *dev = standalone_work->dev;
33 	struct dsa_port *dp = dsa_slave_to_port(dev);
34 	struct switchdev_obj_port_mdb mdb;
35 	struct dsa_switch *ds = dp->ds;
36 	u16 vid = standalone_work->vid;
37 	int err;
38 
39 	switch (standalone_work->event) {
40 	case DSA_UC_ADD:
41 		err = dsa_port_standalone_host_fdb_add(dp, addr, vid);
42 		if (err) {
43 			dev_err(ds->dev,
44 				"port %d failed to add %pM vid %d to fdb: %d\n",
45 				dp->index, addr, vid, err);
46 			break;
47 		}
48 		break;
49 
50 	case DSA_UC_DEL:
51 		err = dsa_port_standalone_host_fdb_del(dp, addr, vid);
52 		if (err) {
53 			dev_err(ds->dev,
54 				"port %d failed to delete %pM vid %d from fdb: %d\n",
55 				dp->index, addr, vid, err);
56 		}
57 
58 		break;
59 	case DSA_MC_ADD:
60 		ether_addr_copy(mdb.addr, addr);
61 		mdb.vid = vid;
62 
63 		err = dsa_port_standalone_host_mdb_add(dp, &mdb);
64 		if (err) {
65 			dev_err(ds->dev,
66 				"port %d failed to add %pM vid %d to mdb: %d\n",
67 				dp->index, addr, vid, err);
68 			break;
69 		}
70 		break;
71 	case DSA_MC_DEL:
72 		ether_addr_copy(mdb.addr, addr);
73 		mdb.vid = vid;
74 
75 		err = dsa_port_standalone_host_mdb_del(dp, &mdb);
76 		if (err) {
77 			dev_err(ds->dev,
78 				"port %d failed to delete %pM vid %d from mdb: %d\n",
79 				dp->index, addr, vid, err);
80 		}
81 
82 		break;
83 	}
84 
85 	kfree(standalone_work);
86 }
87 
88 static int dsa_slave_schedule_standalone_work(struct net_device *dev,
89 					      enum dsa_standalone_event event,
90 					      const unsigned char *addr,
91 					      u16 vid)
92 {
93 	struct dsa_standalone_event_work *standalone_work;
94 
95 	standalone_work = kzalloc(sizeof(*standalone_work), GFP_ATOMIC);
96 	if (!standalone_work)
97 		return -ENOMEM;
98 
99 	INIT_WORK(&standalone_work->work, dsa_slave_standalone_event_work);
100 	standalone_work->event = event;
101 	standalone_work->dev = dev;
102 
103 	ether_addr_copy(standalone_work->addr, addr);
104 	standalone_work->vid = vid;
105 
106 	dsa_schedule_work(&standalone_work->work);
107 
108 	return 0;
109 }
110 
111 static int dsa_slave_sync_uc(struct net_device *dev,
112 			     const unsigned char *addr)
113 {
114 	struct net_device *master = dsa_slave_to_master(dev);
115 	struct dsa_port *dp = dsa_slave_to_port(dev);
116 
117 	dev_uc_add(master, addr);
118 
119 	if (!dsa_switch_supports_uc_filtering(dp->ds))
120 		return 0;
121 
122 	return dsa_slave_schedule_standalone_work(dev, DSA_UC_ADD, addr, 0);
123 }
124 
125 static int dsa_slave_unsync_uc(struct net_device *dev,
126 			       const unsigned char *addr)
127 {
128 	struct net_device *master = dsa_slave_to_master(dev);
129 	struct dsa_port *dp = dsa_slave_to_port(dev);
130 
131 	dev_uc_del(master, addr);
132 
133 	if (!dsa_switch_supports_uc_filtering(dp->ds))
134 		return 0;
135 
136 	return dsa_slave_schedule_standalone_work(dev, DSA_UC_DEL, addr, 0);
137 }
138 
139 static int dsa_slave_sync_mc(struct net_device *dev,
140 			     const unsigned char *addr)
141 {
142 	struct net_device *master = dsa_slave_to_master(dev);
143 	struct dsa_port *dp = dsa_slave_to_port(dev);
144 
145 	dev_mc_add(master, addr);
146 
147 	if (!dsa_switch_supports_mc_filtering(dp->ds))
148 		return 0;
149 
150 	return dsa_slave_schedule_standalone_work(dev, DSA_MC_ADD, addr, 0);
151 }
152 
153 static int dsa_slave_unsync_mc(struct net_device *dev,
154 			       const unsigned char *addr)
155 {
156 	struct net_device *master = dsa_slave_to_master(dev);
157 	struct dsa_port *dp = dsa_slave_to_port(dev);
158 
159 	dev_mc_del(master, addr);
160 
161 	if (!dsa_switch_supports_mc_filtering(dp->ds))
162 		return 0;
163 
164 	return dsa_slave_schedule_standalone_work(dev, DSA_MC_DEL, addr, 0);
165 }
166 
167 void dsa_slave_sync_ha(struct net_device *dev)
168 {
169 	struct dsa_port *dp = dsa_slave_to_port(dev);
170 	struct dsa_switch *ds = dp->ds;
171 	struct netdev_hw_addr *ha;
172 
173 	netif_addr_lock_bh(dev);
174 
175 	netdev_for_each_synced_mc_addr(ha, dev)
176 		dsa_slave_sync_mc(dev, ha->addr);
177 
178 	netdev_for_each_synced_uc_addr(ha, dev)
179 		dsa_slave_sync_uc(dev, ha->addr);
180 
181 	netif_addr_unlock_bh(dev);
182 
183 	if (dsa_switch_supports_uc_filtering(ds) ||
184 	    dsa_switch_supports_mc_filtering(ds))
185 		dsa_flush_workqueue();
186 }
187 
188 void dsa_slave_unsync_ha(struct net_device *dev)
189 {
190 	struct dsa_port *dp = dsa_slave_to_port(dev);
191 	struct dsa_switch *ds = dp->ds;
192 	struct netdev_hw_addr *ha;
193 
194 	netif_addr_lock_bh(dev);
195 
196 	netdev_for_each_synced_uc_addr(ha, dev)
197 		dsa_slave_unsync_uc(dev, ha->addr);
198 
199 	netdev_for_each_synced_mc_addr(ha, dev)
200 		dsa_slave_unsync_mc(dev, ha->addr);
201 
202 	netif_addr_unlock_bh(dev);
203 
204 	if (dsa_switch_supports_uc_filtering(ds) ||
205 	    dsa_switch_supports_mc_filtering(ds))
206 		dsa_flush_workqueue();
207 }
208 
209 /* slave mii_bus handling ***************************************************/
210 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
211 {
212 	struct dsa_switch *ds = bus->priv;
213 
214 	if (ds->phys_mii_mask & (1 << addr))
215 		return ds->ops->phy_read(ds, addr, reg);
216 
217 	return 0xffff;
218 }
219 
220 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
221 {
222 	struct dsa_switch *ds = bus->priv;
223 
224 	if (ds->phys_mii_mask & (1 << addr))
225 		return ds->ops->phy_write(ds, addr, reg, val);
226 
227 	return 0;
228 }
229 
230 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
231 {
232 	ds->slave_mii_bus->priv = (void *)ds;
233 	ds->slave_mii_bus->name = "dsa slave smi";
234 	ds->slave_mii_bus->read = dsa_slave_phy_read;
235 	ds->slave_mii_bus->write = dsa_slave_phy_write;
236 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
237 		 ds->dst->index, ds->index);
238 	ds->slave_mii_bus->parent = ds->dev;
239 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
240 }
241 
242 
243 /* slave device handling ****************************************************/
244 static int dsa_slave_get_iflink(const struct net_device *dev)
245 {
246 	return dsa_slave_to_master(dev)->ifindex;
247 }
248 
249 static int dsa_slave_open(struct net_device *dev)
250 {
251 	struct net_device *master = dsa_slave_to_master(dev);
252 	struct dsa_port *dp = dsa_slave_to_port(dev);
253 	struct dsa_switch *ds = dp->ds;
254 	int err;
255 
256 	err = dev_open(master, NULL);
257 	if (err < 0) {
258 		netdev_err(dev, "failed to open master %s\n", master->name);
259 		goto out;
260 	}
261 
262 	if (dsa_switch_supports_uc_filtering(ds)) {
263 		err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0);
264 		if (err)
265 			goto out;
266 	}
267 
268 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
269 		err = dev_uc_add(master, dev->dev_addr);
270 		if (err < 0)
271 			goto del_host_addr;
272 	}
273 
274 	err = dsa_port_enable_rt(dp, dev->phydev);
275 	if (err)
276 		goto del_unicast;
277 
278 	return 0;
279 
280 del_unicast:
281 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
282 		dev_uc_del(master, dev->dev_addr);
283 del_host_addr:
284 	if (dsa_switch_supports_uc_filtering(ds))
285 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
286 out:
287 	return err;
288 }
289 
290 static int dsa_slave_close(struct net_device *dev)
291 {
292 	struct net_device *master = dsa_slave_to_master(dev);
293 	struct dsa_port *dp = dsa_slave_to_port(dev);
294 	struct dsa_switch *ds = dp->ds;
295 
296 	dsa_port_disable_rt(dp);
297 
298 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
299 		dev_uc_del(master, dev->dev_addr);
300 
301 	if (dsa_switch_supports_uc_filtering(ds))
302 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
303 
304 	return 0;
305 }
306 
307 static void dsa_slave_manage_host_flood(struct net_device *dev)
308 {
309 	bool mc = dev->flags & (IFF_PROMISC | IFF_ALLMULTI);
310 	struct dsa_port *dp = dsa_slave_to_port(dev);
311 	bool uc = dev->flags & IFF_PROMISC;
312 
313 	dsa_port_set_host_flood(dp, uc, mc);
314 }
315 
316 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
317 {
318 	struct net_device *master = dsa_slave_to_master(dev);
319 	struct dsa_port *dp = dsa_slave_to_port(dev);
320 	struct dsa_switch *ds = dp->ds;
321 
322 	if (change & IFF_ALLMULTI)
323 		dev_set_allmulti(master,
324 				 dev->flags & IFF_ALLMULTI ? 1 : -1);
325 	if (change & IFF_PROMISC)
326 		dev_set_promiscuity(master,
327 				    dev->flags & IFF_PROMISC ? 1 : -1);
328 
329 	if (dsa_switch_supports_uc_filtering(ds) &&
330 	    dsa_switch_supports_mc_filtering(ds))
331 		dsa_slave_manage_host_flood(dev);
332 }
333 
334 static void dsa_slave_set_rx_mode(struct net_device *dev)
335 {
336 	__dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc);
337 	__dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc);
338 }
339 
340 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
341 {
342 	struct net_device *master = dsa_slave_to_master(dev);
343 	struct dsa_port *dp = dsa_slave_to_port(dev);
344 	struct dsa_switch *ds = dp->ds;
345 	struct sockaddr *addr = a;
346 	int err;
347 
348 	if (!is_valid_ether_addr(addr->sa_data))
349 		return -EADDRNOTAVAIL;
350 
351 	/* If the port is down, the address isn't synced yet to hardware or
352 	 * to the DSA master, so there is nothing to change.
353 	 */
354 	if (!(dev->flags & IFF_UP))
355 		goto out_change_dev_addr;
356 
357 	if (dsa_switch_supports_uc_filtering(ds)) {
358 		err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0);
359 		if (err)
360 			return err;
361 	}
362 
363 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
364 		err = dev_uc_add(master, addr->sa_data);
365 		if (err < 0)
366 			goto del_unicast;
367 	}
368 
369 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
370 		dev_uc_del(master, dev->dev_addr);
371 
372 	if (dsa_switch_supports_uc_filtering(ds))
373 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
374 
375 out_change_dev_addr:
376 	eth_hw_addr_set(dev, addr->sa_data);
377 
378 	return 0;
379 
380 del_unicast:
381 	if (dsa_switch_supports_uc_filtering(ds))
382 		dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0);
383 
384 	return err;
385 }
386 
387 struct dsa_slave_dump_ctx {
388 	struct net_device *dev;
389 	struct sk_buff *skb;
390 	struct netlink_callback *cb;
391 	int idx;
392 };
393 
394 static int
395 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
396 			   bool is_static, void *data)
397 {
398 	struct dsa_slave_dump_ctx *dump = data;
399 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
400 	u32 seq = dump->cb->nlh->nlmsg_seq;
401 	struct nlmsghdr *nlh;
402 	struct ndmsg *ndm;
403 
404 	if (dump->idx < dump->cb->args[2])
405 		goto skip;
406 
407 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
408 			sizeof(*ndm), NLM_F_MULTI);
409 	if (!nlh)
410 		return -EMSGSIZE;
411 
412 	ndm = nlmsg_data(nlh);
413 	ndm->ndm_family  = AF_BRIDGE;
414 	ndm->ndm_pad1    = 0;
415 	ndm->ndm_pad2    = 0;
416 	ndm->ndm_flags   = NTF_SELF;
417 	ndm->ndm_type    = 0;
418 	ndm->ndm_ifindex = dump->dev->ifindex;
419 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
420 
421 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
422 		goto nla_put_failure;
423 
424 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
425 		goto nla_put_failure;
426 
427 	nlmsg_end(dump->skb, nlh);
428 
429 skip:
430 	dump->idx++;
431 	return 0;
432 
433 nla_put_failure:
434 	nlmsg_cancel(dump->skb, nlh);
435 	return -EMSGSIZE;
436 }
437 
438 static int
439 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
440 		   struct net_device *dev, struct net_device *filter_dev,
441 		   int *idx)
442 {
443 	struct dsa_port *dp = dsa_slave_to_port(dev);
444 	struct dsa_slave_dump_ctx dump = {
445 		.dev = dev,
446 		.skb = skb,
447 		.cb = cb,
448 		.idx = *idx,
449 	};
450 	int err;
451 
452 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
453 	*idx = dump.idx;
454 
455 	return err;
456 }
457 
458 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
459 {
460 	struct dsa_slave_priv *p = netdev_priv(dev);
461 	struct dsa_switch *ds = p->dp->ds;
462 	int port = p->dp->index;
463 
464 	/* Pass through to switch driver if it supports timestamping */
465 	switch (cmd) {
466 	case SIOCGHWTSTAMP:
467 		if (ds->ops->port_hwtstamp_get)
468 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
469 		break;
470 	case SIOCSHWTSTAMP:
471 		if (ds->ops->port_hwtstamp_set)
472 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
473 		break;
474 	}
475 
476 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
477 }
478 
479 static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
480 				   const struct switchdev_attr *attr,
481 				   struct netlink_ext_ack *extack)
482 {
483 	struct dsa_port *dp = dsa_slave_to_port(dev);
484 	int ret;
485 
486 	if (ctx && ctx != dp)
487 		return 0;
488 
489 	switch (attr->id) {
490 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
491 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
492 			return -EOPNOTSUPP;
493 
494 		ret = dsa_port_set_state(dp, attr->u.stp_state, true);
495 		break;
496 	case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
497 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
498 			return -EOPNOTSUPP;
499 
500 		ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
501 		break;
502 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
503 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
504 			return -EOPNOTSUPP;
505 
506 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
507 					      extack);
508 		break;
509 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
510 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
511 			return -EOPNOTSUPP;
512 
513 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
514 		break;
515 	case SWITCHDEV_ATTR_ID_BRIDGE_MST:
516 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
517 			return -EOPNOTSUPP;
518 
519 		ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
520 		break;
521 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
522 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
523 			return -EOPNOTSUPP;
524 
525 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
526 						extack);
527 		break;
528 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
529 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
530 			return -EOPNOTSUPP;
531 
532 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
533 		break;
534 	case SWITCHDEV_ATTR_ID_VLAN_MSTI:
535 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
536 			return -EOPNOTSUPP;
537 
538 		ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
539 		break;
540 	default:
541 		ret = -EOPNOTSUPP;
542 		break;
543 	}
544 
545 	return ret;
546 }
547 
548 /* Must be called under rcu_read_lock() */
549 static int
550 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
551 				      const struct switchdev_obj_port_vlan *vlan)
552 {
553 	struct net_device *upper_dev;
554 	struct list_head *iter;
555 
556 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
557 		u16 vid;
558 
559 		if (!is_vlan_dev(upper_dev))
560 			continue;
561 
562 		vid = vlan_dev_vlan_id(upper_dev);
563 		if (vid == vlan->vid)
564 			return -EBUSY;
565 	}
566 
567 	return 0;
568 }
569 
570 static int dsa_slave_vlan_add(struct net_device *dev,
571 			      const struct switchdev_obj *obj,
572 			      struct netlink_ext_ack *extack)
573 {
574 	struct dsa_port *dp = dsa_slave_to_port(dev);
575 	struct switchdev_obj_port_vlan *vlan;
576 	int err;
577 
578 	if (dsa_port_skip_vlan_configuration(dp)) {
579 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
580 		return 0;
581 	}
582 
583 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
584 
585 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
586 	 * the same VID.
587 	 */
588 	if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
589 		rcu_read_lock();
590 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan);
591 		rcu_read_unlock();
592 		if (err) {
593 			NL_SET_ERR_MSG_MOD(extack,
594 					   "Port already has a VLAN upper with this VID");
595 			return err;
596 		}
597 	}
598 
599 	return dsa_port_vlan_add(dp, vlan, extack);
600 }
601 
602 /* Offload a VLAN installed on the bridge or on a foreign interface by
603  * installing it as a VLAN towards the CPU port.
604  */
605 static int dsa_slave_host_vlan_add(struct net_device *dev,
606 				   const struct switchdev_obj *obj,
607 				   struct netlink_ext_ack *extack)
608 {
609 	struct dsa_port *dp = dsa_slave_to_port(dev);
610 	struct switchdev_obj_port_vlan vlan;
611 
612 	/* Do nothing if this is a software bridge */
613 	if (!dp->bridge)
614 		return -EOPNOTSUPP;
615 
616 	if (dsa_port_skip_vlan_configuration(dp)) {
617 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
618 		return 0;
619 	}
620 
621 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
622 
623 	/* Even though drivers often handle CPU membership in special ways,
624 	 * it doesn't make sense to program a PVID, so clear this flag.
625 	 */
626 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
627 
628 	return dsa_port_host_vlan_add(dp, &vlan, extack);
629 }
630 
631 static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
632 				  const struct switchdev_obj *obj,
633 				  struct netlink_ext_ack *extack)
634 {
635 	struct dsa_port *dp = dsa_slave_to_port(dev);
636 	int err;
637 
638 	if (ctx && ctx != dp)
639 		return 0;
640 
641 	switch (obj->id) {
642 	case SWITCHDEV_OBJ_ID_PORT_MDB:
643 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
644 			return -EOPNOTSUPP;
645 
646 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
647 		break;
648 	case SWITCHDEV_OBJ_ID_HOST_MDB:
649 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
650 			return -EOPNOTSUPP;
651 
652 		err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
653 		break;
654 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
655 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
656 			err = dsa_slave_vlan_add(dev, obj, extack);
657 		else
658 			err = dsa_slave_host_vlan_add(dev, obj, extack);
659 		break;
660 	case SWITCHDEV_OBJ_ID_MRP:
661 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
662 			return -EOPNOTSUPP;
663 
664 		err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
665 		break;
666 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
667 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
668 			return -EOPNOTSUPP;
669 
670 		err = dsa_port_mrp_add_ring_role(dp,
671 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
672 		break;
673 	default:
674 		err = -EOPNOTSUPP;
675 		break;
676 	}
677 
678 	return err;
679 }
680 
681 static int dsa_slave_vlan_del(struct net_device *dev,
682 			      const struct switchdev_obj *obj)
683 {
684 	struct dsa_port *dp = dsa_slave_to_port(dev);
685 	struct switchdev_obj_port_vlan *vlan;
686 
687 	if (dsa_port_skip_vlan_configuration(dp))
688 		return 0;
689 
690 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
691 
692 	return dsa_port_vlan_del(dp, vlan);
693 }
694 
695 static int dsa_slave_host_vlan_del(struct net_device *dev,
696 				   const struct switchdev_obj *obj)
697 {
698 	struct dsa_port *dp = dsa_slave_to_port(dev);
699 	struct switchdev_obj_port_vlan *vlan;
700 
701 	/* Do nothing if this is a software bridge */
702 	if (!dp->bridge)
703 		return -EOPNOTSUPP;
704 
705 	if (dsa_port_skip_vlan_configuration(dp))
706 		return 0;
707 
708 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
709 
710 	return dsa_port_host_vlan_del(dp, vlan);
711 }
712 
713 static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
714 				  const struct switchdev_obj *obj)
715 {
716 	struct dsa_port *dp = dsa_slave_to_port(dev);
717 	int err;
718 
719 	if (ctx && ctx != dp)
720 		return 0;
721 
722 	switch (obj->id) {
723 	case SWITCHDEV_OBJ_ID_PORT_MDB:
724 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
725 			return -EOPNOTSUPP;
726 
727 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
728 		break;
729 	case SWITCHDEV_OBJ_ID_HOST_MDB:
730 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
731 			return -EOPNOTSUPP;
732 
733 		err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
734 		break;
735 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
736 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
737 			err = dsa_slave_vlan_del(dev, obj);
738 		else
739 			err = dsa_slave_host_vlan_del(dev, obj);
740 		break;
741 	case SWITCHDEV_OBJ_ID_MRP:
742 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
743 			return -EOPNOTSUPP;
744 
745 		err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
746 		break;
747 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
748 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
749 			return -EOPNOTSUPP;
750 
751 		err = dsa_port_mrp_del_ring_role(dp,
752 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
753 		break;
754 	default:
755 		err = -EOPNOTSUPP;
756 		break;
757 	}
758 
759 	return err;
760 }
761 
762 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
763 						     struct sk_buff *skb)
764 {
765 #ifdef CONFIG_NET_POLL_CONTROLLER
766 	struct dsa_slave_priv *p = netdev_priv(dev);
767 
768 	return netpoll_send_skb(p->netpoll, skb);
769 #else
770 	BUG();
771 	return NETDEV_TX_OK;
772 #endif
773 }
774 
775 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
776 				 struct sk_buff *skb)
777 {
778 	struct dsa_switch *ds = p->dp->ds;
779 
780 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
781 		return;
782 
783 	if (!ds->ops->port_txtstamp)
784 		return;
785 
786 	ds->ops->port_txtstamp(ds, p->dp->index, skb);
787 }
788 
789 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
790 {
791 	/* SKB for netpoll still need to be mangled with the protocol-specific
792 	 * tag to be successfully transmitted
793 	 */
794 	if (unlikely(netpoll_tx_running(dev)))
795 		return dsa_slave_netpoll_send_skb(dev, skb);
796 
797 	/* Queue the SKB for transmission on the parent interface, but
798 	 * do not modify its EtherType
799 	 */
800 	skb->dev = dsa_slave_to_master(dev);
801 	dev_queue_xmit(skb);
802 
803 	return NETDEV_TX_OK;
804 }
805 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
806 
807 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
808 {
809 	int needed_headroom = dev->needed_headroom;
810 	int needed_tailroom = dev->needed_tailroom;
811 
812 	/* For tail taggers, we need to pad short frames ourselves, to ensure
813 	 * that the tail tag does not fail at its role of being at the end of
814 	 * the packet, once the master interface pads the frame. Account for
815 	 * that pad length here, and pad later.
816 	 */
817 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
818 		needed_tailroom += ETH_ZLEN - skb->len;
819 	/* skb_headroom() returns unsigned int... */
820 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
821 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
822 
823 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
824 		/* No reallocation needed, yay! */
825 		return 0;
826 
827 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
828 				GFP_ATOMIC);
829 }
830 
831 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
832 {
833 	struct dsa_slave_priv *p = netdev_priv(dev);
834 	struct sk_buff *nskb;
835 
836 	dev_sw_netstats_tx_add(dev, 1, skb->len);
837 
838 	memset(skb->cb, 0, sizeof(skb->cb));
839 
840 	/* Handle tx timestamp if any */
841 	dsa_skb_tx_timestamp(p, skb);
842 
843 	if (dsa_realloc_skb(skb, dev)) {
844 		dev_kfree_skb_any(skb);
845 		return NETDEV_TX_OK;
846 	}
847 
848 	/* needed_tailroom should still be 'warm' in the cache line from
849 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
850 	 */
851 	if (dev->needed_tailroom)
852 		eth_skb_pad(skb);
853 
854 	/* Transmit function may have to reallocate the original SKB,
855 	 * in which case it must have freed it. Only free it here on error.
856 	 */
857 	nskb = p->xmit(skb, dev);
858 	if (!nskb) {
859 		kfree_skb(skb);
860 		return NETDEV_TX_OK;
861 	}
862 
863 	return dsa_enqueue_skb(nskb, dev);
864 }
865 
866 /* ethtool operations *******************************************************/
867 
868 static void dsa_slave_get_drvinfo(struct net_device *dev,
869 				  struct ethtool_drvinfo *drvinfo)
870 {
871 	strscpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
872 	strscpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
873 	strscpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
874 }
875 
876 static int dsa_slave_get_regs_len(struct net_device *dev)
877 {
878 	struct dsa_port *dp = dsa_slave_to_port(dev);
879 	struct dsa_switch *ds = dp->ds;
880 
881 	if (ds->ops->get_regs_len)
882 		return ds->ops->get_regs_len(ds, dp->index);
883 
884 	return -EOPNOTSUPP;
885 }
886 
887 static void
888 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
889 {
890 	struct dsa_port *dp = dsa_slave_to_port(dev);
891 	struct dsa_switch *ds = dp->ds;
892 
893 	if (ds->ops->get_regs)
894 		ds->ops->get_regs(ds, dp->index, regs, _p);
895 }
896 
897 static int dsa_slave_nway_reset(struct net_device *dev)
898 {
899 	struct dsa_port *dp = dsa_slave_to_port(dev);
900 
901 	return phylink_ethtool_nway_reset(dp->pl);
902 }
903 
904 static int dsa_slave_get_eeprom_len(struct net_device *dev)
905 {
906 	struct dsa_port *dp = dsa_slave_to_port(dev);
907 	struct dsa_switch *ds = dp->ds;
908 
909 	if (ds->cd && ds->cd->eeprom_len)
910 		return ds->cd->eeprom_len;
911 
912 	if (ds->ops->get_eeprom_len)
913 		return ds->ops->get_eeprom_len(ds);
914 
915 	return 0;
916 }
917 
918 static int dsa_slave_get_eeprom(struct net_device *dev,
919 				struct ethtool_eeprom *eeprom, u8 *data)
920 {
921 	struct dsa_port *dp = dsa_slave_to_port(dev);
922 	struct dsa_switch *ds = dp->ds;
923 
924 	if (ds->ops->get_eeprom)
925 		return ds->ops->get_eeprom(ds, eeprom, data);
926 
927 	return -EOPNOTSUPP;
928 }
929 
930 static int dsa_slave_set_eeprom(struct net_device *dev,
931 				struct ethtool_eeprom *eeprom, u8 *data)
932 {
933 	struct dsa_port *dp = dsa_slave_to_port(dev);
934 	struct dsa_switch *ds = dp->ds;
935 
936 	if (ds->ops->set_eeprom)
937 		return ds->ops->set_eeprom(ds, eeprom, data);
938 
939 	return -EOPNOTSUPP;
940 }
941 
942 static void dsa_slave_get_strings(struct net_device *dev,
943 				  uint32_t stringset, uint8_t *data)
944 {
945 	struct dsa_port *dp = dsa_slave_to_port(dev);
946 	struct dsa_switch *ds = dp->ds;
947 
948 	if (stringset == ETH_SS_STATS) {
949 		int len = ETH_GSTRING_LEN;
950 
951 		strncpy(data, "tx_packets", len);
952 		strncpy(data + len, "tx_bytes", len);
953 		strncpy(data + 2 * len, "rx_packets", len);
954 		strncpy(data + 3 * len, "rx_bytes", len);
955 		if (ds->ops->get_strings)
956 			ds->ops->get_strings(ds, dp->index, stringset,
957 					     data + 4 * len);
958 	} else if (stringset ==  ETH_SS_TEST) {
959 		net_selftest_get_strings(data);
960 	}
961 
962 }
963 
964 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
965 					struct ethtool_stats *stats,
966 					uint64_t *data)
967 {
968 	struct dsa_port *dp = dsa_slave_to_port(dev);
969 	struct dsa_switch *ds = dp->ds;
970 	struct pcpu_sw_netstats *s;
971 	unsigned int start;
972 	int i;
973 
974 	for_each_possible_cpu(i) {
975 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
976 
977 		s = per_cpu_ptr(dev->tstats, i);
978 		do {
979 			start = u64_stats_fetch_begin_irq(&s->syncp);
980 			tx_packets = u64_stats_read(&s->tx_packets);
981 			tx_bytes = u64_stats_read(&s->tx_bytes);
982 			rx_packets = u64_stats_read(&s->rx_packets);
983 			rx_bytes = u64_stats_read(&s->rx_bytes);
984 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
985 		data[0] += tx_packets;
986 		data[1] += tx_bytes;
987 		data[2] += rx_packets;
988 		data[3] += rx_bytes;
989 	}
990 	if (ds->ops->get_ethtool_stats)
991 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
992 }
993 
994 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
995 {
996 	struct dsa_port *dp = dsa_slave_to_port(dev);
997 	struct dsa_switch *ds = dp->ds;
998 
999 	if (sset == ETH_SS_STATS) {
1000 		int count = 0;
1001 
1002 		if (ds->ops->get_sset_count) {
1003 			count = ds->ops->get_sset_count(ds, dp->index, sset);
1004 			if (count < 0)
1005 				return count;
1006 		}
1007 
1008 		return count + 4;
1009 	} else if (sset ==  ETH_SS_TEST) {
1010 		return net_selftest_get_count();
1011 	}
1012 
1013 	return -EOPNOTSUPP;
1014 }
1015 
1016 static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
1017 					struct ethtool_eth_phy_stats *phy_stats)
1018 {
1019 	struct dsa_port *dp = dsa_slave_to_port(dev);
1020 	struct dsa_switch *ds = dp->ds;
1021 
1022 	if (ds->ops->get_eth_phy_stats)
1023 		ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
1024 }
1025 
1026 static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
1027 					struct ethtool_eth_mac_stats *mac_stats)
1028 {
1029 	struct dsa_port *dp = dsa_slave_to_port(dev);
1030 	struct dsa_switch *ds = dp->ds;
1031 
1032 	if (ds->ops->get_eth_mac_stats)
1033 		ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
1034 }
1035 
1036 static void
1037 dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
1038 			     struct ethtool_eth_ctrl_stats *ctrl_stats)
1039 {
1040 	struct dsa_port *dp = dsa_slave_to_port(dev);
1041 	struct dsa_switch *ds = dp->ds;
1042 
1043 	if (ds->ops->get_eth_ctrl_stats)
1044 		ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1045 }
1046 
1047 static void
1048 dsa_slave_get_rmon_stats(struct net_device *dev,
1049 			 struct ethtool_rmon_stats *rmon_stats,
1050 			 const struct ethtool_rmon_hist_range **ranges)
1051 {
1052 	struct dsa_port *dp = dsa_slave_to_port(dev);
1053 	struct dsa_switch *ds = dp->ds;
1054 
1055 	if (ds->ops->get_rmon_stats)
1056 		ds->ops->get_rmon_stats(ds, dp->index, rmon_stats, ranges);
1057 }
1058 
1059 static void dsa_slave_net_selftest(struct net_device *ndev,
1060 				   struct ethtool_test *etest, u64 *buf)
1061 {
1062 	struct dsa_port *dp = dsa_slave_to_port(ndev);
1063 	struct dsa_switch *ds = dp->ds;
1064 
1065 	if (ds->ops->self_test) {
1066 		ds->ops->self_test(ds, dp->index, etest, buf);
1067 		return;
1068 	}
1069 
1070 	net_selftest(ndev, etest, buf);
1071 }
1072 
1073 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1074 {
1075 	struct dsa_port *dp = dsa_slave_to_port(dev);
1076 	struct dsa_switch *ds = dp->ds;
1077 
1078 	phylink_ethtool_get_wol(dp->pl, w);
1079 
1080 	if (ds->ops->get_wol)
1081 		ds->ops->get_wol(ds, dp->index, w);
1082 }
1083 
1084 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1085 {
1086 	struct dsa_port *dp = dsa_slave_to_port(dev);
1087 	struct dsa_switch *ds = dp->ds;
1088 	int ret = -EOPNOTSUPP;
1089 
1090 	phylink_ethtool_set_wol(dp->pl, w);
1091 
1092 	if (ds->ops->set_wol)
1093 		ret = ds->ops->set_wol(ds, dp->index, w);
1094 
1095 	return ret;
1096 }
1097 
1098 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
1099 {
1100 	struct dsa_port *dp = dsa_slave_to_port(dev);
1101 	struct dsa_switch *ds = dp->ds;
1102 	int ret;
1103 
1104 	/* Port's PHY and MAC both need to be EEE capable */
1105 	if (!dev->phydev || !dp->pl)
1106 		return -ENODEV;
1107 
1108 	if (!ds->ops->set_mac_eee)
1109 		return -EOPNOTSUPP;
1110 
1111 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
1112 	if (ret)
1113 		return ret;
1114 
1115 	return phylink_ethtool_set_eee(dp->pl, e);
1116 }
1117 
1118 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
1119 {
1120 	struct dsa_port *dp = dsa_slave_to_port(dev);
1121 	struct dsa_switch *ds = dp->ds;
1122 	int ret;
1123 
1124 	/* Port's PHY and MAC both need to be EEE capable */
1125 	if (!dev->phydev || !dp->pl)
1126 		return -ENODEV;
1127 
1128 	if (!ds->ops->get_mac_eee)
1129 		return -EOPNOTSUPP;
1130 
1131 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
1132 	if (ret)
1133 		return ret;
1134 
1135 	return phylink_ethtool_get_eee(dp->pl, e);
1136 }
1137 
1138 static int dsa_slave_get_link_ksettings(struct net_device *dev,
1139 					struct ethtool_link_ksettings *cmd)
1140 {
1141 	struct dsa_port *dp = dsa_slave_to_port(dev);
1142 
1143 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
1144 }
1145 
1146 static int dsa_slave_set_link_ksettings(struct net_device *dev,
1147 					const struct ethtool_link_ksettings *cmd)
1148 {
1149 	struct dsa_port *dp = dsa_slave_to_port(dev);
1150 
1151 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
1152 }
1153 
1154 static void dsa_slave_get_pause_stats(struct net_device *dev,
1155 				  struct ethtool_pause_stats *pause_stats)
1156 {
1157 	struct dsa_port *dp = dsa_slave_to_port(dev);
1158 	struct dsa_switch *ds = dp->ds;
1159 
1160 	if (ds->ops->get_pause_stats)
1161 		ds->ops->get_pause_stats(ds, dp->index, pause_stats);
1162 }
1163 
1164 static void dsa_slave_get_pauseparam(struct net_device *dev,
1165 				     struct ethtool_pauseparam *pause)
1166 {
1167 	struct dsa_port *dp = dsa_slave_to_port(dev);
1168 
1169 	phylink_ethtool_get_pauseparam(dp->pl, pause);
1170 }
1171 
1172 static int dsa_slave_set_pauseparam(struct net_device *dev,
1173 				    struct ethtool_pauseparam *pause)
1174 {
1175 	struct dsa_port *dp = dsa_slave_to_port(dev);
1176 
1177 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
1178 }
1179 
1180 #ifdef CONFIG_NET_POLL_CONTROLLER
1181 static int dsa_slave_netpoll_setup(struct net_device *dev,
1182 				   struct netpoll_info *ni)
1183 {
1184 	struct net_device *master = dsa_slave_to_master(dev);
1185 	struct dsa_slave_priv *p = netdev_priv(dev);
1186 	struct netpoll *netpoll;
1187 	int err = 0;
1188 
1189 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1190 	if (!netpoll)
1191 		return -ENOMEM;
1192 
1193 	err = __netpoll_setup(netpoll, master);
1194 	if (err) {
1195 		kfree(netpoll);
1196 		goto out;
1197 	}
1198 
1199 	p->netpoll = netpoll;
1200 out:
1201 	return err;
1202 }
1203 
1204 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1205 {
1206 	struct dsa_slave_priv *p = netdev_priv(dev);
1207 	struct netpoll *netpoll = p->netpoll;
1208 
1209 	if (!netpoll)
1210 		return;
1211 
1212 	p->netpoll = NULL;
1213 
1214 	__netpoll_free(netpoll);
1215 }
1216 
1217 static void dsa_slave_poll_controller(struct net_device *dev)
1218 {
1219 }
1220 #endif
1221 
1222 static struct dsa_mall_tc_entry *
1223 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1224 {
1225 	struct dsa_slave_priv *p = netdev_priv(dev);
1226 	struct dsa_mall_tc_entry *mall_tc_entry;
1227 
1228 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1229 		if (mall_tc_entry->cookie == cookie)
1230 			return mall_tc_entry;
1231 
1232 	return NULL;
1233 }
1234 
1235 static int
1236 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1237 				  struct tc_cls_matchall_offload *cls,
1238 				  bool ingress)
1239 {
1240 	struct netlink_ext_ack *extack = cls->common.extack;
1241 	struct dsa_port *dp = dsa_slave_to_port(dev);
1242 	struct dsa_slave_priv *p = netdev_priv(dev);
1243 	struct dsa_mall_mirror_tc_entry *mirror;
1244 	struct dsa_mall_tc_entry *mall_tc_entry;
1245 	struct dsa_switch *ds = dp->ds;
1246 	struct flow_action_entry *act;
1247 	struct dsa_port *to_dp;
1248 	int err;
1249 
1250 	if (!ds->ops->port_mirror_add)
1251 		return -EOPNOTSUPP;
1252 
1253 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1254 					      cls->common.extack))
1255 		return -EOPNOTSUPP;
1256 
1257 	act = &cls->rule->action.entries[0];
1258 
1259 	if (!act->dev)
1260 		return -EINVAL;
1261 
1262 	if (!dsa_slave_dev_check(act->dev))
1263 		return -EOPNOTSUPP;
1264 
1265 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1266 	if (!mall_tc_entry)
1267 		return -ENOMEM;
1268 
1269 	mall_tc_entry->cookie = cls->cookie;
1270 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1271 	mirror = &mall_tc_entry->mirror;
1272 
1273 	to_dp = dsa_slave_to_port(act->dev);
1274 
1275 	mirror->to_local_port = to_dp->index;
1276 	mirror->ingress = ingress;
1277 
1278 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1279 	if (err) {
1280 		kfree(mall_tc_entry);
1281 		return err;
1282 	}
1283 
1284 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1285 
1286 	return err;
1287 }
1288 
1289 static int
1290 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1291 				  struct tc_cls_matchall_offload *cls,
1292 				  bool ingress)
1293 {
1294 	struct netlink_ext_ack *extack = cls->common.extack;
1295 	struct dsa_port *dp = dsa_slave_to_port(dev);
1296 	struct dsa_slave_priv *p = netdev_priv(dev);
1297 	struct dsa_mall_policer_tc_entry *policer;
1298 	struct dsa_mall_tc_entry *mall_tc_entry;
1299 	struct dsa_switch *ds = dp->ds;
1300 	struct flow_action_entry *act;
1301 	int err;
1302 
1303 	if (!ds->ops->port_policer_add) {
1304 		NL_SET_ERR_MSG_MOD(extack,
1305 				   "Policing offload not implemented");
1306 		return -EOPNOTSUPP;
1307 	}
1308 
1309 	if (!ingress) {
1310 		NL_SET_ERR_MSG_MOD(extack,
1311 				   "Only supported on ingress qdisc");
1312 		return -EOPNOTSUPP;
1313 	}
1314 
1315 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1316 					      cls->common.extack))
1317 		return -EOPNOTSUPP;
1318 
1319 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1320 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1321 			NL_SET_ERR_MSG_MOD(extack,
1322 					   "Only one port policer allowed");
1323 			return -EEXIST;
1324 		}
1325 	}
1326 
1327 	act = &cls->rule->action.entries[0];
1328 
1329 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1330 	if (!mall_tc_entry)
1331 		return -ENOMEM;
1332 
1333 	mall_tc_entry->cookie = cls->cookie;
1334 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1335 	policer = &mall_tc_entry->policer;
1336 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1337 	policer->burst = act->police.burst;
1338 
1339 	err = ds->ops->port_policer_add(ds, dp->index, policer);
1340 	if (err) {
1341 		kfree(mall_tc_entry);
1342 		return err;
1343 	}
1344 
1345 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1346 
1347 	return err;
1348 }
1349 
1350 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1351 				      struct tc_cls_matchall_offload *cls,
1352 				      bool ingress)
1353 {
1354 	int err = -EOPNOTSUPP;
1355 
1356 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1357 	    flow_offload_has_one_action(&cls->rule->action) &&
1358 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1359 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1360 	else if (flow_offload_has_one_action(&cls->rule->action) &&
1361 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1362 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1363 
1364 	return err;
1365 }
1366 
1367 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1368 				       struct tc_cls_matchall_offload *cls)
1369 {
1370 	struct dsa_port *dp = dsa_slave_to_port(dev);
1371 	struct dsa_mall_tc_entry *mall_tc_entry;
1372 	struct dsa_switch *ds = dp->ds;
1373 
1374 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1375 	if (!mall_tc_entry)
1376 		return;
1377 
1378 	list_del(&mall_tc_entry->list);
1379 
1380 	switch (mall_tc_entry->type) {
1381 	case DSA_PORT_MALL_MIRROR:
1382 		if (ds->ops->port_mirror_del)
1383 			ds->ops->port_mirror_del(ds, dp->index,
1384 						 &mall_tc_entry->mirror);
1385 		break;
1386 	case DSA_PORT_MALL_POLICER:
1387 		if (ds->ops->port_policer_del)
1388 			ds->ops->port_policer_del(ds, dp->index);
1389 		break;
1390 	default:
1391 		WARN_ON(1);
1392 	}
1393 
1394 	kfree(mall_tc_entry);
1395 }
1396 
1397 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1398 					   struct tc_cls_matchall_offload *cls,
1399 					   bool ingress)
1400 {
1401 	if (cls->common.chain_index)
1402 		return -EOPNOTSUPP;
1403 
1404 	switch (cls->command) {
1405 	case TC_CLSMATCHALL_REPLACE:
1406 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
1407 	case TC_CLSMATCHALL_DESTROY:
1408 		dsa_slave_del_cls_matchall(dev, cls);
1409 		return 0;
1410 	default:
1411 		return -EOPNOTSUPP;
1412 	}
1413 }
1414 
1415 static int dsa_slave_add_cls_flower(struct net_device *dev,
1416 				    struct flow_cls_offload *cls,
1417 				    bool ingress)
1418 {
1419 	struct dsa_port *dp = dsa_slave_to_port(dev);
1420 	struct dsa_switch *ds = dp->ds;
1421 	int port = dp->index;
1422 
1423 	if (!ds->ops->cls_flower_add)
1424 		return -EOPNOTSUPP;
1425 
1426 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1427 }
1428 
1429 static int dsa_slave_del_cls_flower(struct net_device *dev,
1430 				    struct flow_cls_offload *cls,
1431 				    bool ingress)
1432 {
1433 	struct dsa_port *dp = dsa_slave_to_port(dev);
1434 	struct dsa_switch *ds = dp->ds;
1435 	int port = dp->index;
1436 
1437 	if (!ds->ops->cls_flower_del)
1438 		return -EOPNOTSUPP;
1439 
1440 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1441 }
1442 
1443 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1444 				      struct flow_cls_offload *cls,
1445 				      bool ingress)
1446 {
1447 	struct dsa_port *dp = dsa_slave_to_port(dev);
1448 	struct dsa_switch *ds = dp->ds;
1449 	int port = dp->index;
1450 
1451 	if (!ds->ops->cls_flower_stats)
1452 		return -EOPNOTSUPP;
1453 
1454 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1455 }
1456 
1457 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1458 					 struct flow_cls_offload *cls,
1459 					 bool ingress)
1460 {
1461 	switch (cls->command) {
1462 	case FLOW_CLS_REPLACE:
1463 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1464 	case FLOW_CLS_DESTROY:
1465 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1466 	case FLOW_CLS_STATS:
1467 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1468 	default:
1469 		return -EOPNOTSUPP;
1470 	}
1471 }
1472 
1473 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1474 				       void *cb_priv, bool ingress)
1475 {
1476 	struct net_device *dev = cb_priv;
1477 
1478 	if (!tc_can_offload(dev))
1479 		return -EOPNOTSUPP;
1480 
1481 	switch (type) {
1482 	case TC_SETUP_CLSMATCHALL:
1483 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1484 	case TC_SETUP_CLSFLOWER:
1485 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1486 	default:
1487 		return -EOPNOTSUPP;
1488 	}
1489 }
1490 
1491 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1492 					  void *type_data, void *cb_priv)
1493 {
1494 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1495 }
1496 
1497 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1498 					  void *type_data, void *cb_priv)
1499 {
1500 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1501 }
1502 
1503 static LIST_HEAD(dsa_slave_block_cb_list);
1504 
1505 static int dsa_slave_setup_tc_block(struct net_device *dev,
1506 				    struct flow_block_offload *f)
1507 {
1508 	struct flow_block_cb *block_cb;
1509 	flow_setup_cb_t *cb;
1510 
1511 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1512 		cb = dsa_slave_setup_tc_block_cb_ig;
1513 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1514 		cb = dsa_slave_setup_tc_block_cb_eg;
1515 	else
1516 		return -EOPNOTSUPP;
1517 
1518 	f->driver_block_list = &dsa_slave_block_cb_list;
1519 
1520 	switch (f->command) {
1521 	case FLOW_BLOCK_BIND:
1522 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1523 			return -EBUSY;
1524 
1525 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1526 		if (IS_ERR(block_cb))
1527 			return PTR_ERR(block_cb);
1528 
1529 		flow_block_cb_add(block_cb, f);
1530 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1531 		return 0;
1532 	case FLOW_BLOCK_UNBIND:
1533 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1534 		if (!block_cb)
1535 			return -ENOENT;
1536 
1537 		flow_block_cb_remove(block_cb, f);
1538 		list_del(&block_cb->driver_list);
1539 		return 0;
1540 	default:
1541 		return -EOPNOTSUPP;
1542 	}
1543 }
1544 
1545 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1546 				    void *type_data)
1547 {
1548 	struct net_device *master = dsa_port_to_master(dsa_to_port(ds, port));
1549 
1550 	if (!master->netdev_ops->ndo_setup_tc)
1551 		return -EOPNOTSUPP;
1552 
1553 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1554 }
1555 
1556 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1557 			      void *type_data)
1558 {
1559 	struct dsa_port *dp = dsa_slave_to_port(dev);
1560 	struct dsa_switch *ds = dp->ds;
1561 
1562 	switch (type) {
1563 	case TC_SETUP_BLOCK:
1564 		return dsa_slave_setup_tc_block(dev, type_data);
1565 	case TC_SETUP_FT:
1566 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1567 	default:
1568 		break;
1569 	}
1570 
1571 	if (!ds->ops->port_setup_tc)
1572 		return -EOPNOTSUPP;
1573 
1574 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1575 }
1576 
1577 static int dsa_slave_get_rxnfc(struct net_device *dev,
1578 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1579 {
1580 	struct dsa_port *dp = dsa_slave_to_port(dev);
1581 	struct dsa_switch *ds = dp->ds;
1582 
1583 	if (!ds->ops->get_rxnfc)
1584 		return -EOPNOTSUPP;
1585 
1586 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1587 }
1588 
1589 static int dsa_slave_set_rxnfc(struct net_device *dev,
1590 			       struct ethtool_rxnfc *nfc)
1591 {
1592 	struct dsa_port *dp = dsa_slave_to_port(dev);
1593 	struct dsa_switch *ds = dp->ds;
1594 
1595 	if (!ds->ops->set_rxnfc)
1596 		return -EOPNOTSUPP;
1597 
1598 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1599 }
1600 
1601 static int dsa_slave_get_ts_info(struct net_device *dev,
1602 				 struct ethtool_ts_info *ts)
1603 {
1604 	struct dsa_slave_priv *p = netdev_priv(dev);
1605 	struct dsa_switch *ds = p->dp->ds;
1606 
1607 	if (!ds->ops->get_ts_info)
1608 		return -EOPNOTSUPP;
1609 
1610 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
1611 }
1612 
1613 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1614 				     u16 vid)
1615 {
1616 	struct dsa_port *dp = dsa_slave_to_port(dev);
1617 	struct switchdev_obj_port_vlan vlan = {
1618 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1619 		.vid = vid,
1620 		/* This API only allows programming tagged, non-PVID VIDs */
1621 		.flags = 0,
1622 	};
1623 	struct netlink_ext_ack extack = {0};
1624 	int ret;
1625 
1626 	/* User port... */
1627 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
1628 	if (ret) {
1629 		if (extack._msg)
1630 			netdev_err(dev, "%s\n", extack._msg);
1631 		return ret;
1632 	}
1633 
1634 	/* And CPU port... */
1635 	ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
1636 	if (ret) {
1637 		if (extack._msg)
1638 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1639 				   extack._msg);
1640 		return ret;
1641 	}
1642 
1643 	return 0;
1644 }
1645 
1646 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1647 				      u16 vid)
1648 {
1649 	struct dsa_port *dp = dsa_slave_to_port(dev);
1650 	struct switchdev_obj_port_vlan vlan = {
1651 		.vid = vid,
1652 		/* This API only allows programming tagged, non-PVID VIDs */
1653 		.flags = 0,
1654 	};
1655 	int err;
1656 
1657 	err = dsa_port_vlan_del(dp, &vlan);
1658 	if (err)
1659 		return err;
1660 
1661 	return dsa_port_host_vlan_del(dp, &vlan);
1662 }
1663 
1664 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
1665 {
1666 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1667 
1668 	return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
1669 }
1670 
1671 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
1672 {
1673 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1674 
1675 	return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
1676 }
1677 
1678 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1679  * filtering is enabled. The baseline is that only ports that offload a
1680  * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1681  * but there are exceptions for quirky hardware.
1682  *
1683  * If ds->vlan_filtering_is_global = true, then standalone ports which share
1684  * the same switch with other ports that offload a VLAN-aware bridge are also
1685  * inevitably VLAN-aware.
1686  *
1687  * To summarize, a DSA switch port offloads:
1688  *
1689  * - If standalone (this includes software bridge, software LAG):
1690  *     - if ds->needs_standalone_vlan_filtering = true, OR if
1691  *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
1692  *       this switch chip which have vlan_filtering=1)
1693  *         - the 8021q upper VLANs
1694  *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
1695  *       global, or it is, but no port is under a VLAN-aware bridge):
1696  *         - no VLAN (any 8021q upper is a software VLAN)
1697  *
1698  * - If under a vlan_filtering=0 bridge which it offload:
1699  *     - if ds->configure_vlan_while_not_filtering = true (default):
1700  *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
1701  *     - else (deprecated):
1702  *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1703  *           enabled, so this behavior is broken and discouraged.
1704  *
1705  * - If under a vlan_filtering=1 bridge which it offload:
1706  *     - the bridge VLANs
1707  *     - the 8021q upper VLANs
1708  */
1709 int dsa_slave_manage_vlan_filtering(struct net_device *slave,
1710 				    bool vlan_filtering)
1711 {
1712 	int err;
1713 
1714 	if (vlan_filtering) {
1715 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1716 
1717 		err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
1718 		if (err) {
1719 			vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1720 			slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1721 			return err;
1722 		}
1723 	} else {
1724 		err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1725 		if (err)
1726 			return err;
1727 
1728 		slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1729 	}
1730 
1731 	return 0;
1732 }
1733 
1734 struct dsa_hw_port {
1735 	struct list_head list;
1736 	struct net_device *dev;
1737 	int old_mtu;
1738 };
1739 
1740 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1741 {
1742 	const struct dsa_hw_port *p;
1743 	int err;
1744 
1745 	list_for_each_entry(p, hw_port_list, list) {
1746 		if (p->dev->mtu == mtu)
1747 			continue;
1748 
1749 		err = dev_set_mtu(p->dev, mtu);
1750 		if (err)
1751 			goto rollback;
1752 	}
1753 
1754 	return 0;
1755 
1756 rollback:
1757 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1758 		if (p->dev->mtu == p->old_mtu)
1759 			continue;
1760 
1761 		if (dev_set_mtu(p->dev, p->old_mtu))
1762 			netdev_err(p->dev, "Failed to restore MTU\n");
1763 	}
1764 
1765 	return err;
1766 }
1767 
1768 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1769 {
1770 	struct dsa_hw_port *p, *n;
1771 
1772 	list_for_each_entry_safe(p, n, hw_port_list, list)
1773 		kfree(p);
1774 }
1775 
1776 /* Make the hardware datapath to/from @dev limited to a common MTU */
1777 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1778 {
1779 	struct list_head hw_port_list;
1780 	struct dsa_switch_tree *dst;
1781 	int min_mtu = ETH_MAX_MTU;
1782 	struct dsa_port *other_dp;
1783 	int err;
1784 
1785 	if (!dp->ds->mtu_enforcement_ingress)
1786 		return;
1787 
1788 	if (!dp->bridge)
1789 		return;
1790 
1791 	INIT_LIST_HEAD(&hw_port_list);
1792 
1793 	/* Populate the list of ports that are part of the same bridge
1794 	 * as the newly added/modified port
1795 	 */
1796 	list_for_each_entry(dst, &dsa_tree_list, list) {
1797 		list_for_each_entry(other_dp, &dst->ports, list) {
1798 			struct dsa_hw_port *hw_port;
1799 			struct net_device *slave;
1800 
1801 			if (other_dp->type != DSA_PORT_TYPE_USER)
1802 				continue;
1803 
1804 			if (!dsa_port_bridge_same(dp, other_dp))
1805 				continue;
1806 
1807 			if (!other_dp->ds->mtu_enforcement_ingress)
1808 				continue;
1809 
1810 			slave = other_dp->slave;
1811 
1812 			if (min_mtu > slave->mtu)
1813 				min_mtu = slave->mtu;
1814 
1815 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1816 			if (!hw_port)
1817 				goto out;
1818 
1819 			hw_port->dev = slave;
1820 			hw_port->old_mtu = slave->mtu;
1821 
1822 			list_add(&hw_port->list, &hw_port_list);
1823 		}
1824 	}
1825 
1826 	/* Attempt to configure the entire hardware bridge to the newly added
1827 	 * interface's MTU first, regardless of whether the intention of the
1828 	 * user was to raise or lower it.
1829 	 */
1830 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1831 	if (!err)
1832 		goto out;
1833 
1834 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1835 	 * all hardware bridge ports now. If this fails too, then all ports will
1836 	 * still have their old MTU rolled back anyway.
1837 	 */
1838 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1839 
1840 out:
1841 	dsa_hw_port_list_free(&hw_port_list);
1842 }
1843 
1844 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1845 {
1846 	struct net_device *master = dsa_slave_to_master(dev);
1847 	struct dsa_port *dp = dsa_slave_to_port(dev);
1848 	struct dsa_port *cpu_dp = dp->cpu_dp;
1849 	struct dsa_switch *ds = dp->ds;
1850 	struct dsa_port *other_dp;
1851 	int largest_mtu = 0;
1852 	int new_master_mtu;
1853 	int old_master_mtu;
1854 	int mtu_limit;
1855 	int cpu_mtu;
1856 	int err;
1857 
1858 	if (!ds->ops->port_change_mtu)
1859 		return -EOPNOTSUPP;
1860 
1861 	dsa_tree_for_each_user_port(other_dp, ds->dst) {
1862 		int slave_mtu;
1863 
1864 		/* During probe, this function will be called for each slave
1865 		 * device, while not all of them have been allocated. That's
1866 		 * ok, it doesn't change what the maximum is, so ignore it.
1867 		 */
1868 		if (!other_dp->slave)
1869 			continue;
1870 
1871 		/* Pretend that we already applied the setting, which we
1872 		 * actually haven't (still haven't done all integrity checks)
1873 		 */
1874 		if (dp == other_dp)
1875 			slave_mtu = new_mtu;
1876 		else
1877 			slave_mtu = other_dp->slave->mtu;
1878 
1879 		if (largest_mtu < slave_mtu)
1880 			largest_mtu = slave_mtu;
1881 	}
1882 
1883 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1884 	old_master_mtu = master->mtu;
1885 	new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1886 	if (new_master_mtu > mtu_limit)
1887 		return -ERANGE;
1888 
1889 	/* If the master MTU isn't over limit, there's no need to check the CPU
1890 	 * MTU, since that surely isn't either.
1891 	 */
1892 	cpu_mtu = largest_mtu;
1893 
1894 	/* Start applying stuff */
1895 	if (new_master_mtu != old_master_mtu) {
1896 		err = dev_set_mtu(master, new_master_mtu);
1897 		if (err < 0)
1898 			goto out_master_failed;
1899 
1900 		/* We only need to propagate the MTU of the CPU port to
1901 		 * upstream switches, so emit a notifier which updates them.
1902 		 */
1903 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu);
1904 		if (err)
1905 			goto out_cpu_failed;
1906 	}
1907 
1908 	err = ds->ops->port_change_mtu(ds, dp->index, new_mtu);
1909 	if (err)
1910 		goto out_port_failed;
1911 
1912 	dev->mtu = new_mtu;
1913 
1914 	dsa_bridge_mtu_normalization(dp);
1915 
1916 	return 0;
1917 
1918 out_port_failed:
1919 	if (new_master_mtu != old_master_mtu)
1920 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1921 				    dsa_tag_protocol_overhead(cpu_dp->tag_ops));
1922 out_cpu_failed:
1923 	if (new_master_mtu != old_master_mtu)
1924 		dev_set_mtu(master, old_master_mtu);
1925 out_master_failed:
1926 	return err;
1927 }
1928 
1929 static int __maybe_unused
1930 dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
1931 {
1932 	struct dsa_port *dp = dsa_slave_to_port(dev);
1933 	struct dsa_switch *ds = dp->ds;
1934 	unsigned long mask, new_prio;
1935 	int err, port = dp->index;
1936 
1937 	if (!ds->ops->port_set_default_prio)
1938 		return -EOPNOTSUPP;
1939 
1940 	err = dcb_ieee_setapp(dev, app);
1941 	if (err)
1942 		return err;
1943 
1944 	mask = dcb_ieee_getapp_mask(dev, app);
1945 	new_prio = __fls(mask);
1946 
1947 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
1948 	if (err) {
1949 		dcb_ieee_delapp(dev, app);
1950 		return err;
1951 	}
1952 
1953 	return 0;
1954 }
1955 
1956 static int __maybe_unused
1957 dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
1958 {
1959 	struct dsa_port *dp = dsa_slave_to_port(dev);
1960 	struct dsa_switch *ds = dp->ds;
1961 	unsigned long mask, new_prio;
1962 	int err, port = dp->index;
1963 	u8 dscp = app->protocol;
1964 
1965 	if (!ds->ops->port_add_dscp_prio)
1966 		return -EOPNOTSUPP;
1967 
1968 	if (dscp >= 64) {
1969 		netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
1970 			   dscp);
1971 		return -EINVAL;
1972 	}
1973 
1974 	err = dcb_ieee_setapp(dev, app);
1975 	if (err)
1976 		return err;
1977 
1978 	mask = dcb_ieee_getapp_mask(dev, app);
1979 	new_prio = __fls(mask);
1980 
1981 	err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
1982 	if (err) {
1983 		dcb_ieee_delapp(dev, app);
1984 		return err;
1985 	}
1986 
1987 	return 0;
1988 }
1989 
1990 static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev,
1991 						      struct dcb_app *app)
1992 {
1993 	switch (app->selector) {
1994 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
1995 		switch (app->protocol) {
1996 		case 0:
1997 			return dsa_slave_dcbnl_set_default_prio(dev, app);
1998 		default:
1999 			return -EOPNOTSUPP;
2000 		}
2001 		break;
2002 	case IEEE_8021QAZ_APP_SEL_DSCP:
2003 		return dsa_slave_dcbnl_add_dscp_prio(dev, app);
2004 	default:
2005 		return -EOPNOTSUPP;
2006 	}
2007 }
2008 
2009 static int __maybe_unused
2010 dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
2011 {
2012 	struct dsa_port *dp = dsa_slave_to_port(dev);
2013 	struct dsa_switch *ds = dp->ds;
2014 	unsigned long mask, new_prio;
2015 	int err, port = dp->index;
2016 
2017 	if (!ds->ops->port_set_default_prio)
2018 		return -EOPNOTSUPP;
2019 
2020 	err = dcb_ieee_delapp(dev, app);
2021 	if (err)
2022 		return err;
2023 
2024 	mask = dcb_ieee_getapp_mask(dev, app);
2025 	new_prio = mask ? __fls(mask) : 0;
2026 
2027 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
2028 	if (err) {
2029 		dcb_ieee_setapp(dev, app);
2030 		return err;
2031 	}
2032 
2033 	return 0;
2034 }
2035 
2036 static int __maybe_unused
2037 dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
2038 {
2039 	struct dsa_port *dp = dsa_slave_to_port(dev);
2040 	struct dsa_switch *ds = dp->ds;
2041 	int err, port = dp->index;
2042 	u8 dscp = app->protocol;
2043 
2044 	if (!ds->ops->port_del_dscp_prio)
2045 		return -EOPNOTSUPP;
2046 
2047 	err = dcb_ieee_delapp(dev, app);
2048 	if (err)
2049 		return err;
2050 
2051 	err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
2052 	if (err) {
2053 		dcb_ieee_setapp(dev, app);
2054 		return err;
2055 	}
2056 
2057 	return 0;
2058 }
2059 
2060 static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev,
2061 						      struct dcb_app *app)
2062 {
2063 	switch (app->selector) {
2064 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2065 		switch (app->protocol) {
2066 		case 0:
2067 			return dsa_slave_dcbnl_del_default_prio(dev, app);
2068 		default:
2069 			return -EOPNOTSUPP;
2070 		}
2071 		break;
2072 	case IEEE_8021QAZ_APP_SEL_DSCP:
2073 		return dsa_slave_dcbnl_del_dscp_prio(dev, app);
2074 	default:
2075 		return -EOPNOTSUPP;
2076 	}
2077 }
2078 
2079 /* Pre-populate the DCB application priority table with the priorities
2080  * configured during switch setup, which we read from hardware here.
2081  */
2082 static int dsa_slave_dcbnl_init(struct net_device *dev)
2083 {
2084 	struct dsa_port *dp = dsa_slave_to_port(dev);
2085 	struct dsa_switch *ds = dp->ds;
2086 	int port = dp->index;
2087 	int err;
2088 
2089 	if (ds->ops->port_get_default_prio) {
2090 		int prio = ds->ops->port_get_default_prio(ds, port);
2091 		struct dcb_app app = {
2092 			.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2093 			.protocol = 0,
2094 			.priority = prio,
2095 		};
2096 
2097 		if (prio < 0)
2098 			return prio;
2099 
2100 		err = dcb_ieee_setapp(dev, &app);
2101 		if (err)
2102 			return err;
2103 	}
2104 
2105 	if (ds->ops->port_get_dscp_prio) {
2106 		int protocol;
2107 
2108 		for (protocol = 0; protocol < 64; protocol++) {
2109 			struct dcb_app app = {
2110 				.selector = IEEE_8021QAZ_APP_SEL_DSCP,
2111 				.protocol = protocol,
2112 			};
2113 			int prio;
2114 
2115 			prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
2116 			if (prio == -EOPNOTSUPP)
2117 				continue;
2118 			if (prio < 0)
2119 				return prio;
2120 
2121 			app.priority = prio;
2122 
2123 			err = dcb_ieee_setapp(dev, &app);
2124 			if (err)
2125 				return err;
2126 		}
2127 	}
2128 
2129 	return 0;
2130 }
2131 
2132 static const struct ethtool_ops dsa_slave_ethtool_ops = {
2133 	.get_drvinfo		= dsa_slave_get_drvinfo,
2134 	.get_regs_len		= dsa_slave_get_regs_len,
2135 	.get_regs		= dsa_slave_get_regs,
2136 	.nway_reset		= dsa_slave_nway_reset,
2137 	.get_link		= ethtool_op_get_link,
2138 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
2139 	.get_eeprom		= dsa_slave_get_eeprom,
2140 	.set_eeprom		= dsa_slave_set_eeprom,
2141 	.get_strings		= dsa_slave_get_strings,
2142 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
2143 	.get_sset_count		= dsa_slave_get_sset_count,
2144 	.get_eth_phy_stats	= dsa_slave_get_eth_phy_stats,
2145 	.get_eth_mac_stats	= dsa_slave_get_eth_mac_stats,
2146 	.get_eth_ctrl_stats	= dsa_slave_get_eth_ctrl_stats,
2147 	.get_rmon_stats		= dsa_slave_get_rmon_stats,
2148 	.set_wol		= dsa_slave_set_wol,
2149 	.get_wol		= dsa_slave_get_wol,
2150 	.set_eee		= dsa_slave_set_eee,
2151 	.get_eee		= dsa_slave_get_eee,
2152 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
2153 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
2154 	.get_pause_stats	= dsa_slave_get_pause_stats,
2155 	.get_pauseparam		= dsa_slave_get_pauseparam,
2156 	.set_pauseparam		= dsa_slave_set_pauseparam,
2157 	.get_rxnfc		= dsa_slave_get_rxnfc,
2158 	.set_rxnfc		= dsa_slave_set_rxnfc,
2159 	.get_ts_info		= dsa_slave_get_ts_info,
2160 	.self_test		= dsa_slave_net_selftest,
2161 };
2162 
2163 static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = {
2164 	.ieee_setapp		= dsa_slave_dcbnl_ieee_setapp,
2165 	.ieee_delapp		= dsa_slave_dcbnl_ieee_delapp,
2166 };
2167 
2168 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
2169 {
2170 	struct dsa_port *dp = dsa_slave_to_port(dev);
2171 
2172 	return &dp->devlink_port;
2173 }
2174 
2175 static void dsa_slave_get_stats64(struct net_device *dev,
2176 				  struct rtnl_link_stats64 *s)
2177 {
2178 	struct dsa_port *dp = dsa_slave_to_port(dev);
2179 	struct dsa_switch *ds = dp->ds;
2180 
2181 	if (ds->ops->get_stats64)
2182 		ds->ops->get_stats64(ds, dp->index, s);
2183 	else
2184 		dev_get_tstats64(dev, s);
2185 }
2186 
2187 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
2188 				       struct net_device_path *path)
2189 {
2190 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
2191 	struct net_device *master = dsa_port_to_master(dp);
2192 	struct dsa_port *cpu_dp = dp->cpu_dp;
2193 
2194 	path->dev = ctx->dev;
2195 	path->type = DEV_PATH_DSA;
2196 	path->dsa.proto = cpu_dp->tag_ops->proto;
2197 	path->dsa.port = dp->index;
2198 	ctx->dev = master;
2199 
2200 	return 0;
2201 }
2202 
2203 static const struct net_device_ops dsa_slave_netdev_ops = {
2204 	.ndo_open	 	= dsa_slave_open,
2205 	.ndo_stop		= dsa_slave_close,
2206 	.ndo_start_xmit		= dsa_slave_xmit,
2207 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
2208 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
2209 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
2210 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
2211 	.ndo_eth_ioctl		= dsa_slave_ioctl,
2212 	.ndo_get_iflink		= dsa_slave_get_iflink,
2213 #ifdef CONFIG_NET_POLL_CONTROLLER
2214 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
2215 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
2216 	.ndo_poll_controller	= dsa_slave_poll_controller,
2217 #endif
2218 	.ndo_setup_tc		= dsa_slave_setup_tc,
2219 	.ndo_get_stats64	= dsa_slave_get_stats64,
2220 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
2221 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
2222 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
2223 	.ndo_change_mtu		= dsa_slave_change_mtu,
2224 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
2225 };
2226 
2227 static struct device_type dsa_type = {
2228 	.name	= "dsa",
2229 };
2230 
2231 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2232 {
2233 	const struct dsa_port *dp = dsa_to_port(ds, port);
2234 
2235 	if (dp->pl)
2236 		phylink_mac_change(dp->pl, up);
2237 }
2238 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2239 
2240 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
2241 					  struct phylink_link_state *state)
2242 {
2243 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
2244 	struct dsa_switch *ds = dp->ds;
2245 
2246 	/* No need to check that this operation is valid, the callback would
2247 	 * not be called if it was not.
2248 	 */
2249 	ds->ops->phylink_fixed_state(ds, dp->index, state);
2250 }
2251 
2252 /* slave device setup *******************************************************/
2253 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
2254 				 u32 flags)
2255 {
2256 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2257 	struct dsa_switch *ds = dp->ds;
2258 
2259 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
2260 	if (!slave_dev->phydev) {
2261 		netdev_err(slave_dev, "no phy at %d\n", addr);
2262 		return -ENODEV;
2263 	}
2264 
2265 	slave_dev->phydev->dev_flags |= flags;
2266 
2267 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
2268 }
2269 
2270 static int dsa_slave_phy_setup(struct net_device *slave_dev)
2271 {
2272 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2273 	struct device_node *port_dn = dp->dn;
2274 	struct dsa_switch *ds = dp->ds;
2275 	u32 phy_flags = 0;
2276 	int ret;
2277 
2278 	dp->pl_config.dev = &slave_dev->dev;
2279 	dp->pl_config.type = PHYLINK_NETDEV;
2280 
2281 	/* The get_fixed_state callback takes precedence over polling the
2282 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
2283 	 * this if the switch provides such a callback.
2284 	 */
2285 	if (ds->ops->phylink_fixed_state) {
2286 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
2287 		dp->pl_config.poll_fixed_state = true;
2288 	}
2289 
2290 	ret = dsa_port_phylink_create(dp);
2291 	if (ret)
2292 		return ret;
2293 
2294 	if (ds->ops->get_phy_flags)
2295 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
2296 
2297 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
2298 	if (ret == -ENODEV && ds->slave_mii_bus) {
2299 		/* We could not connect to a designated PHY or SFP, so try to
2300 		 * use the switch internal MDIO bus instead
2301 		 */
2302 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
2303 	}
2304 	if (ret) {
2305 		netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
2306 			   ERR_PTR(ret));
2307 		dsa_port_phylink_destroy(dp);
2308 	}
2309 
2310 	return ret;
2311 }
2312 
2313 void dsa_slave_setup_tagger(struct net_device *slave)
2314 {
2315 	struct dsa_port *dp = dsa_slave_to_port(slave);
2316 	struct net_device *master = dsa_port_to_master(dp);
2317 	struct dsa_slave_priv *p = netdev_priv(slave);
2318 	const struct dsa_port *cpu_dp = dp->cpu_dp;
2319 	const struct dsa_switch *ds = dp->ds;
2320 
2321 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
2322 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
2323 	/* Try to save one extra realloc later in the TX path (in the master)
2324 	 * by also inheriting the master's needed headroom and tailroom.
2325 	 * The 8021q driver also does this.
2326 	 */
2327 	slave->needed_headroom += master->needed_headroom;
2328 	slave->needed_tailroom += master->needed_tailroom;
2329 
2330 	p->xmit = cpu_dp->tag_ops->xmit;
2331 
2332 	slave->features = master->vlan_features | NETIF_F_HW_TC;
2333 	slave->hw_features |= NETIF_F_HW_TC;
2334 	slave->features |= NETIF_F_LLTX;
2335 	if (slave->needed_tailroom)
2336 		slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
2337 	if (ds->needs_standalone_vlan_filtering)
2338 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2339 }
2340 
2341 int dsa_slave_suspend(struct net_device *slave_dev)
2342 {
2343 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2344 
2345 	if (!netif_running(slave_dev))
2346 		return 0;
2347 
2348 	netif_device_detach(slave_dev);
2349 
2350 	rtnl_lock();
2351 	phylink_stop(dp->pl);
2352 	rtnl_unlock();
2353 
2354 	return 0;
2355 }
2356 
2357 int dsa_slave_resume(struct net_device *slave_dev)
2358 {
2359 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2360 
2361 	if (!netif_running(slave_dev))
2362 		return 0;
2363 
2364 	netif_device_attach(slave_dev);
2365 
2366 	rtnl_lock();
2367 	phylink_start(dp->pl);
2368 	rtnl_unlock();
2369 
2370 	return 0;
2371 }
2372 
2373 int dsa_slave_create(struct dsa_port *port)
2374 {
2375 	struct net_device *master = dsa_port_to_master(port);
2376 	struct dsa_switch *ds = port->ds;
2377 	const char *name = port->name;
2378 	struct net_device *slave_dev;
2379 	struct dsa_slave_priv *p;
2380 	int ret;
2381 
2382 	if (!ds->num_tx_queues)
2383 		ds->num_tx_queues = 1;
2384 
2385 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
2386 				     NET_NAME_UNKNOWN, ether_setup,
2387 				     ds->num_tx_queues, 1);
2388 	if (slave_dev == NULL)
2389 		return -ENOMEM;
2390 
2391 	slave_dev->rtnl_link_ops = &dsa_link_ops;
2392 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2393 #if IS_ENABLED(CONFIG_DCB)
2394 	slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops;
2395 #endif
2396 	if (!is_zero_ether_addr(port->mac))
2397 		eth_hw_addr_set(slave_dev, port->mac);
2398 	else
2399 		eth_hw_addr_inherit(slave_dev, master);
2400 	slave_dev->priv_flags |= IFF_NO_QUEUE;
2401 	if (dsa_switch_supports_uc_filtering(ds))
2402 		slave_dev->priv_flags |= IFF_UNICAST_FLT;
2403 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
2404 	if (ds->ops->port_max_mtu)
2405 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2406 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
2407 
2408 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
2409 	slave_dev->dev.of_node = port->dn;
2410 	slave_dev->vlan_features = master->vlan_features;
2411 
2412 	p = netdev_priv(slave_dev);
2413 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2414 	if (!slave_dev->tstats) {
2415 		free_netdev(slave_dev);
2416 		return -ENOMEM;
2417 	}
2418 
2419 	ret = gro_cells_init(&p->gcells, slave_dev);
2420 	if (ret)
2421 		goto out_free;
2422 
2423 	p->dp = port;
2424 	INIT_LIST_HEAD(&p->mall_tc_list);
2425 	port->slave = slave_dev;
2426 	dsa_slave_setup_tagger(slave_dev);
2427 
2428 	netif_carrier_off(slave_dev);
2429 
2430 	ret = dsa_slave_phy_setup(slave_dev);
2431 	if (ret) {
2432 		netdev_err(slave_dev,
2433 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2434 			   ret, ds->dst->index, ds->index, port->index);
2435 		goto out_gcells;
2436 	}
2437 
2438 	rtnl_lock();
2439 
2440 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2441 	if (ret && ret != -EOPNOTSUPP)
2442 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2443 			 ret, ETH_DATA_LEN, port->index);
2444 
2445 	ret = register_netdevice(slave_dev);
2446 	if (ret) {
2447 		netdev_err(master, "error %d registering interface %s\n",
2448 			   ret, slave_dev->name);
2449 		rtnl_unlock();
2450 		goto out_phy;
2451 	}
2452 
2453 	if (IS_ENABLED(CONFIG_DCB)) {
2454 		ret = dsa_slave_dcbnl_init(slave_dev);
2455 		if (ret) {
2456 			netdev_err(slave_dev,
2457 				   "failed to initialize DCB: %pe\n",
2458 				   ERR_PTR(ret));
2459 			rtnl_unlock();
2460 			goto out_unregister;
2461 		}
2462 	}
2463 
2464 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
2465 
2466 	rtnl_unlock();
2467 
2468 	if (ret)
2469 		goto out_unregister;
2470 
2471 	return 0;
2472 
2473 out_unregister:
2474 	unregister_netdev(slave_dev);
2475 out_phy:
2476 	rtnl_lock();
2477 	phylink_disconnect_phy(p->dp->pl);
2478 	rtnl_unlock();
2479 	dsa_port_phylink_destroy(p->dp);
2480 out_gcells:
2481 	gro_cells_destroy(&p->gcells);
2482 out_free:
2483 	free_percpu(slave_dev->tstats);
2484 	free_netdev(slave_dev);
2485 	port->slave = NULL;
2486 	return ret;
2487 }
2488 
2489 void dsa_slave_destroy(struct net_device *slave_dev)
2490 {
2491 	struct net_device *master = dsa_slave_to_master(slave_dev);
2492 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2493 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2494 
2495 	netif_carrier_off(slave_dev);
2496 	rtnl_lock();
2497 	netdev_upper_dev_unlink(master, slave_dev);
2498 	unregister_netdevice(slave_dev);
2499 	phylink_disconnect_phy(dp->pl);
2500 	rtnl_unlock();
2501 
2502 	dsa_port_phylink_destroy(dp);
2503 	gro_cells_destroy(&p->gcells);
2504 	free_percpu(slave_dev->tstats);
2505 	free_netdev(slave_dev);
2506 }
2507 
2508 int dsa_slave_change_master(struct net_device *dev, struct net_device *master,
2509 			    struct netlink_ext_ack *extack)
2510 {
2511 	struct net_device *old_master = dsa_slave_to_master(dev);
2512 	struct dsa_port *dp = dsa_slave_to_port(dev);
2513 	struct dsa_switch *ds = dp->ds;
2514 	struct net_device *upper;
2515 	struct list_head *iter;
2516 	int err;
2517 
2518 	if (master == old_master)
2519 		return 0;
2520 
2521 	if (!ds->ops->port_change_master) {
2522 		NL_SET_ERR_MSG_MOD(extack,
2523 				   "Driver does not support changing DSA master");
2524 		return -EOPNOTSUPP;
2525 	}
2526 
2527 	if (!netdev_uses_dsa(master)) {
2528 		NL_SET_ERR_MSG_MOD(extack,
2529 				   "Interface not eligible as DSA master");
2530 		return -EOPNOTSUPP;
2531 	}
2532 
2533 	netdev_for_each_upper_dev_rcu(master, upper, iter) {
2534 		if (dsa_slave_dev_check(upper))
2535 			continue;
2536 		if (netif_is_bridge_master(upper))
2537 			continue;
2538 		NL_SET_ERR_MSG_MOD(extack, "Cannot join master with unknown uppers");
2539 		return -EOPNOTSUPP;
2540 	}
2541 
2542 	/* Since we allow live-changing the DSA master, plus we auto-open the
2543 	 * DSA master when the user port opens => we need to ensure that the
2544 	 * new DSA master is open too.
2545 	 */
2546 	if (dev->flags & IFF_UP) {
2547 		err = dev_open(master, extack);
2548 		if (err)
2549 			return err;
2550 	}
2551 
2552 	netdev_upper_dev_unlink(old_master, dev);
2553 
2554 	err = netdev_upper_dev_link(master, dev, extack);
2555 	if (err)
2556 		goto out_revert_old_master_unlink;
2557 
2558 	err = dsa_port_change_master(dp, master, extack);
2559 	if (err)
2560 		goto out_revert_master_link;
2561 
2562 	/* Update the MTU of the new CPU port through cross-chip notifiers */
2563 	err = dsa_slave_change_mtu(dev, dev->mtu);
2564 	if (err && err != -EOPNOTSUPP) {
2565 		netdev_warn(dev,
2566 			    "nonfatal error updating MTU with new master: %pe\n",
2567 			    ERR_PTR(err));
2568 	}
2569 
2570 	/* If the port doesn't have its own MAC address and relies on the DSA
2571 	 * master's one, inherit it again from the new DSA master.
2572 	 */
2573 	if (is_zero_ether_addr(dp->mac))
2574 		eth_hw_addr_inherit(dev, master);
2575 
2576 	return 0;
2577 
2578 out_revert_master_link:
2579 	netdev_upper_dev_unlink(master, dev);
2580 out_revert_old_master_unlink:
2581 	netdev_upper_dev_link(old_master, dev, NULL);
2582 	return err;
2583 }
2584 
2585 bool dsa_slave_dev_check(const struct net_device *dev)
2586 {
2587 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2588 }
2589 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2590 
2591 static int dsa_slave_changeupper(struct net_device *dev,
2592 				 struct netdev_notifier_changeupper_info *info)
2593 {
2594 	struct dsa_port *dp = dsa_slave_to_port(dev);
2595 	struct netlink_ext_ack *extack;
2596 	int err = NOTIFY_DONE;
2597 
2598 	if (!dsa_slave_dev_check(dev))
2599 		return err;
2600 
2601 	extack = netdev_notifier_info_to_extack(&info->info);
2602 
2603 	if (netif_is_bridge_master(info->upper_dev)) {
2604 		if (info->linking) {
2605 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2606 			if (!err)
2607 				dsa_bridge_mtu_normalization(dp);
2608 			if (err == -EOPNOTSUPP) {
2609 				if (extack && !extack->_msg)
2610 					NL_SET_ERR_MSG_MOD(extack,
2611 							   "Offloading not supported");
2612 				err = 0;
2613 			}
2614 			err = notifier_from_errno(err);
2615 		} else {
2616 			dsa_port_bridge_leave(dp, info->upper_dev);
2617 			err = NOTIFY_OK;
2618 		}
2619 	} else if (netif_is_lag_master(info->upper_dev)) {
2620 		if (info->linking) {
2621 			err = dsa_port_lag_join(dp, info->upper_dev,
2622 						info->upper_info, extack);
2623 			if (err == -EOPNOTSUPP) {
2624 				NL_SET_ERR_MSG_MOD(info->info.extack,
2625 						   "Offloading not supported");
2626 				err = 0;
2627 			}
2628 			err = notifier_from_errno(err);
2629 		} else {
2630 			dsa_port_lag_leave(dp, info->upper_dev);
2631 			err = NOTIFY_OK;
2632 		}
2633 	} else if (is_hsr_master(info->upper_dev)) {
2634 		if (info->linking) {
2635 			err = dsa_port_hsr_join(dp, info->upper_dev);
2636 			if (err == -EOPNOTSUPP) {
2637 				NL_SET_ERR_MSG_MOD(info->info.extack,
2638 						   "Offloading not supported");
2639 				err = 0;
2640 			}
2641 			err = notifier_from_errno(err);
2642 		} else {
2643 			dsa_port_hsr_leave(dp, info->upper_dev);
2644 			err = NOTIFY_OK;
2645 		}
2646 	}
2647 
2648 	return err;
2649 }
2650 
2651 static int dsa_slave_prechangeupper(struct net_device *dev,
2652 				    struct netdev_notifier_changeupper_info *info)
2653 {
2654 	struct dsa_port *dp = dsa_slave_to_port(dev);
2655 
2656 	if (!dsa_slave_dev_check(dev))
2657 		return NOTIFY_DONE;
2658 
2659 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2660 		dsa_port_pre_bridge_leave(dp, info->upper_dev);
2661 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2662 		dsa_port_pre_lag_leave(dp, info->upper_dev);
2663 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2664 	 * meaningfully enslaved to a bridge yet
2665 	 */
2666 
2667 	return NOTIFY_DONE;
2668 }
2669 
2670 static int
2671 dsa_slave_lag_changeupper(struct net_device *dev,
2672 			  struct netdev_notifier_changeupper_info *info)
2673 {
2674 	struct net_device *lower;
2675 	struct list_head *iter;
2676 	int err = NOTIFY_DONE;
2677 	struct dsa_port *dp;
2678 
2679 	if (!netif_is_lag_master(dev))
2680 		return err;
2681 
2682 	netdev_for_each_lower_dev(dev, lower, iter) {
2683 		if (!dsa_slave_dev_check(lower))
2684 			continue;
2685 
2686 		dp = dsa_slave_to_port(lower);
2687 		if (!dp->lag)
2688 			/* Software LAG */
2689 			continue;
2690 
2691 		err = dsa_slave_changeupper(lower, info);
2692 		if (notifier_to_errno(err))
2693 			break;
2694 	}
2695 
2696 	return err;
2697 }
2698 
2699 /* Same as dsa_slave_lag_changeupper() except that it calls
2700  * dsa_slave_prechangeupper()
2701  */
2702 static int
2703 dsa_slave_lag_prechangeupper(struct net_device *dev,
2704 			     struct netdev_notifier_changeupper_info *info)
2705 {
2706 	struct net_device *lower;
2707 	struct list_head *iter;
2708 	int err = NOTIFY_DONE;
2709 	struct dsa_port *dp;
2710 
2711 	if (!netif_is_lag_master(dev))
2712 		return err;
2713 
2714 	netdev_for_each_lower_dev(dev, lower, iter) {
2715 		if (!dsa_slave_dev_check(lower))
2716 			continue;
2717 
2718 		dp = dsa_slave_to_port(lower);
2719 		if (!dp->lag)
2720 			/* Software LAG */
2721 			continue;
2722 
2723 		err = dsa_slave_prechangeupper(lower, info);
2724 		if (notifier_to_errno(err))
2725 			break;
2726 	}
2727 
2728 	return err;
2729 }
2730 
2731 static int
2732 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2733 				 struct netdev_notifier_changeupper_info *info)
2734 {
2735 	struct netlink_ext_ack *ext_ack;
2736 	struct net_device *slave, *br;
2737 	struct dsa_port *dp;
2738 
2739 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2740 
2741 	if (!is_vlan_dev(dev))
2742 		return NOTIFY_DONE;
2743 
2744 	slave = vlan_dev_real_dev(dev);
2745 	if (!dsa_slave_dev_check(slave))
2746 		return NOTIFY_DONE;
2747 
2748 	dp = dsa_slave_to_port(slave);
2749 	br = dsa_port_bridge_dev_get(dp);
2750 	if (!br)
2751 		return NOTIFY_DONE;
2752 
2753 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2754 	if (br_vlan_enabled(br) &&
2755 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2756 		NL_SET_ERR_MSG_MOD(ext_ack,
2757 				   "Cannot enslave VLAN device into VLAN aware bridge");
2758 		return notifier_from_errno(-EINVAL);
2759 	}
2760 
2761 	return NOTIFY_DONE;
2762 }
2763 
2764 static int
2765 dsa_slave_check_8021q_upper(struct net_device *dev,
2766 			    struct netdev_notifier_changeupper_info *info)
2767 {
2768 	struct dsa_port *dp = dsa_slave_to_port(dev);
2769 	struct net_device *br = dsa_port_bridge_dev_get(dp);
2770 	struct bridge_vlan_info br_info;
2771 	struct netlink_ext_ack *extack;
2772 	int err = NOTIFY_DONE;
2773 	u16 vid;
2774 
2775 	if (!br || !br_vlan_enabled(br))
2776 		return NOTIFY_DONE;
2777 
2778 	extack = netdev_notifier_info_to_extack(&info->info);
2779 	vid = vlan_dev_vlan_id(info->upper_dev);
2780 
2781 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2782 	 * device, respectively the VID is not found, returning
2783 	 * 0 means success, which is a failure for us here.
2784 	 */
2785 	err = br_vlan_get_info(br, vid, &br_info);
2786 	if (err == 0) {
2787 		NL_SET_ERR_MSG_MOD(extack,
2788 				   "This VLAN is already configured by the bridge");
2789 		return notifier_from_errno(-EBUSY);
2790 	}
2791 
2792 	return NOTIFY_DONE;
2793 }
2794 
2795 static int
2796 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2797 				      struct netdev_notifier_changeupper_info *info)
2798 {
2799 	struct dsa_switch *ds;
2800 	struct dsa_port *dp;
2801 	int err;
2802 
2803 	if (!dsa_slave_dev_check(dev))
2804 		return dsa_prevent_bridging_8021q_upper(dev, info);
2805 
2806 	dp = dsa_slave_to_port(dev);
2807 	ds = dp->ds;
2808 
2809 	if (ds->ops->port_prechangeupper) {
2810 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
2811 		if (err)
2812 			return notifier_from_errno(err);
2813 	}
2814 
2815 	if (is_vlan_dev(info->upper_dev))
2816 		return dsa_slave_check_8021q_upper(dev, info);
2817 
2818 	return NOTIFY_DONE;
2819 }
2820 
2821 /* To be eligible as a DSA master, a LAG must have all lower interfaces be
2822  * eligible DSA masters. Additionally, all LAG slaves must be DSA masters of
2823  * switches in the same switch tree.
2824  */
2825 static int dsa_lag_master_validate(struct net_device *lag_dev,
2826 				   struct netlink_ext_ack *extack)
2827 {
2828 	struct net_device *lower1, *lower2;
2829 	struct list_head *iter1, *iter2;
2830 
2831 	netdev_for_each_lower_dev(lag_dev, lower1, iter1) {
2832 		netdev_for_each_lower_dev(lag_dev, lower2, iter2) {
2833 			if (!netdev_uses_dsa(lower1) ||
2834 			    !netdev_uses_dsa(lower2)) {
2835 				NL_SET_ERR_MSG_MOD(extack,
2836 						   "All LAG ports must be eligible as DSA masters");
2837 				return notifier_from_errno(-EINVAL);
2838 			}
2839 
2840 			if (lower1 == lower2)
2841 				continue;
2842 
2843 			if (!dsa_port_tree_same(lower1->dsa_ptr,
2844 						lower2->dsa_ptr)) {
2845 				NL_SET_ERR_MSG_MOD(extack,
2846 						   "LAG contains DSA masters of disjoint switch trees");
2847 				return notifier_from_errno(-EINVAL);
2848 			}
2849 		}
2850 	}
2851 
2852 	return NOTIFY_DONE;
2853 }
2854 
2855 static int
2856 dsa_master_prechangeupper_sanity_check(struct net_device *master,
2857 				       struct netdev_notifier_changeupper_info *info)
2858 {
2859 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
2860 
2861 	if (!netdev_uses_dsa(master))
2862 		return NOTIFY_DONE;
2863 
2864 	if (!info->linking)
2865 		return NOTIFY_DONE;
2866 
2867 	/* Allow DSA switch uppers */
2868 	if (dsa_slave_dev_check(info->upper_dev))
2869 		return NOTIFY_DONE;
2870 
2871 	/* Allow bridge uppers of DSA masters, subject to further
2872 	 * restrictions in dsa_bridge_prechangelower_sanity_check()
2873 	 */
2874 	if (netif_is_bridge_master(info->upper_dev))
2875 		return NOTIFY_DONE;
2876 
2877 	/* Allow LAG uppers, subject to further restrictions in
2878 	 * dsa_lag_master_prechangelower_sanity_check()
2879 	 */
2880 	if (netif_is_lag_master(info->upper_dev))
2881 		return dsa_lag_master_validate(info->upper_dev, extack);
2882 
2883 	NL_SET_ERR_MSG_MOD(extack,
2884 			   "DSA master cannot join unknown upper interfaces");
2885 	return notifier_from_errno(-EBUSY);
2886 }
2887 
2888 static int
2889 dsa_lag_master_prechangelower_sanity_check(struct net_device *dev,
2890 					   struct netdev_notifier_changeupper_info *info)
2891 {
2892 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
2893 	struct net_device *lag_dev = info->upper_dev;
2894 	struct net_device *lower;
2895 	struct list_head *iter;
2896 
2897 	if (!netdev_uses_dsa(lag_dev) || !netif_is_lag_master(lag_dev))
2898 		return NOTIFY_DONE;
2899 
2900 	if (!info->linking)
2901 		return NOTIFY_DONE;
2902 
2903 	if (!netdev_uses_dsa(dev)) {
2904 		NL_SET_ERR_MSG(extack,
2905 			       "Only DSA masters can join a LAG DSA master");
2906 		return notifier_from_errno(-EINVAL);
2907 	}
2908 
2909 	netdev_for_each_lower_dev(lag_dev, lower, iter) {
2910 		if (!dsa_port_tree_same(dev->dsa_ptr, lower->dsa_ptr)) {
2911 			NL_SET_ERR_MSG(extack,
2912 				       "Interface is DSA master for a different switch tree than this LAG");
2913 			return notifier_from_errno(-EINVAL);
2914 		}
2915 
2916 		break;
2917 	}
2918 
2919 	return NOTIFY_DONE;
2920 }
2921 
2922 /* Don't allow bridging of DSA masters, since the bridge layer rx_handler
2923  * prevents the DSA fake ethertype handler to be invoked, so we don't get the
2924  * chance to strip off and parse the DSA switch tag protocol header (the bridge
2925  * layer just returns RX_HANDLER_CONSUMED, stopping RX processing for these
2926  * frames).
2927  * The only case where that would not be an issue is when bridging can already
2928  * be offloaded, such as when the DSA master is itself a DSA or plain switchdev
2929  * port, and is bridged only with other ports from the same hardware device.
2930  */
2931 static int
2932 dsa_bridge_prechangelower_sanity_check(struct net_device *new_lower,
2933 				       struct netdev_notifier_changeupper_info *info)
2934 {
2935 	struct net_device *br = info->upper_dev;
2936 	struct netlink_ext_ack *extack;
2937 	struct net_device *lower;
2938 	struct list_head *iter;
2939 
2940 	if (!netif_is_bridge_master(br))
2941 		return NOTIFY_DONE;
2942 
2943 	if (!info->linking)
2944 		return NOTIFY_DONE;
2945 
2946 	extack = netdev_notifier_info_to_extack(&info->info);
2947 
2948 	netdev_for_each_lower_dev(br, lower, iter) {
2949 		if (!netdev_uses_dsa(new_lower) && !netdev_uses_dsa(lower))
2950 			continue;
2951 
2952 		if (!netdev_port_same_parent_id(lower, new_lower)) {
2953 			NL_SET_ERR_MSG(extack,
2954 				       "Cannot do software bridging with a DSA master");
2955 			return notifier_from_errno(-EINVAL);
2956 		}
2957 	}
2958 
2959 	return NOTIFY_DONE;
2960 }
2961 
2962 static void dsa_tree_migrate_ports_from_lag_master(struct dsa_switch_tree *dst,
2963 						   struct net_device *lag_dev)
2964 {
2965 	struct net_device *new_master = dsa_tree_find_first_master(dst);
2966 	struct dsa_port *dp;
2967 	int err;
2968 
2969 	dsa_tree_for_each_user_port(dp, dst) {
2970 		if (dsa_port_to_master(dp) != lag_dev)
2971 			continue;
2972 
2973 		err = dsa_slave_change_master(dp->slave, new_master, NULL);
2974 		if (err) {
2975 			netdev_err(dp->slave,
2976 				   "failed to restore master to %s: %pe\n",
2977 				   new_master->name, ERR_PTR(err));
2978 		}
2979 	}
2980 }
2981 
2982 static int dsa_master_lag_join(struct net_device *master,
2983 			       struct net_device *lag_dev,
2984 			       struct netdev_lag_upper_info *uinfo,
2985 			       struct netlink_ext_ack *extack)
2986 {
2987 	struct dsa_port *cpu_dp = master->dsa_ptr;
2988 	struct dsa_switch_tree *dst = cpu_dp->dst;
2989 	struct dsa_port *dp;
2990 	int err;
2991 
2992 	err = dsa_master_lag_setup(lag_dev, cpu_dp, uinfo, extack);
2993 	if (err)
2994 		return err;
2995 
2996 	dsa_tree_for_each_user_port(dp, dst) {
2997 		if (dsa_port_to_master(dp) != master)
2998 			continue;
2999 
3000 		err = dsa_slave_change_master(dp->slave, lag_dev, extack);
3001 		if (err)
3002 			goto restore;
3003 	}
3004 
3005 	return 0;
3006 
3007 restore:
3008 	dsa_tree_for_each_user_port_continue_reverse(dp, dst) {
3009 		if (dsa_port_to_master(dp) != lag_dev)
3010 			continue;
3011 
3012 		err = dsa_slave_change_master(dp->slave, master, NULL);
3013 		if (err) {
3014 			netdev_err(dp->slave,
3015 				   "failed to restore master to %s: %pe\n",
3016 				   master->name, ERR_PTR(err));
3017 		}
3018 	}
3019 
3020 	dsa_master_lag_teardown(lag_dev, master->dsa_ptr);
3021 
3022 	return err;
3023 }
3024 
3025 static void dsa_master_lag_leave(struct net_device *master,
3026 				 struct net_device *lag_dev)
3027 {
3028 	struct dsa_port *dp, *cpu_dp = lag_dev->dsa_ptr;
3029 	struct dsa_switch_tree *dst = cpu_dp->dst;
3030 	struct dsa_port *new_cpu_dp = NULL;
3031 	struct net_device *lower;
3032 	struct list_head *iter;
3033 
3034 	netdev_for_each_lower_dev(lag_dev, lower, iter) {
3035 		if (netdev_uses_dsa(lower)) {
3036 			new_cpu_dp = lower->dsa_ptr;
3037 			break;
3038 		}
3039 	}
3040 
3041 	if (new_cpu_dp) {
3042 		/* Update the CPU port of the user ports still under the LAG
3043 		 * so that dsa_port_to_master() continues to work properly
3044 		 */
3045 		dsa_tree_for_each_user_port(dp, dst)
3046 			if (dsa_port_to_master(dp) == lag_dev)
3047 				dp->cpu_dp = new_cpu_dp;
3048 
3049 		/* Update the index of the virtual CPU port to match the lowest
3050 		 * physical CPU port
3051 		 */
3052 		lag_dev->dsa_ptr = new_cpu_dp;
3053 		wmb();
3054 	} else {
3055 		/* If the LAG DSA master has no ports left, migrate back all
3056 		 * user ports to the first physical CPU port
3057 		 */
3058 		dsa_tree_migrate_ports_from_lag_master(dst, lag_dev);
3059 	}
3060 
3061 	/* This DSA master has left its LAG in any case, so let
3062 	 * the CPU port leave the hardware LAG as well
3063 	 */
3064 	dsa_master_lag_teardown(lag_dev, master->dsa_ptr);
3065 }
3066 
3067 static int dsa_master_changeupper(struct net_device *dev,
3068 				  struct netdev_notifier_changeupper_info *info)
3069 {
3070 	struct netlink_ext_ack *extack;
3071 	int err = NOTIFY_DONE;
3072 
3073 	if (!netdev_uses_dsa(dev))
3074 		return err;
3075 
3076 	extack = netdev_notifier_info_to_extack(&info->info);
3077 
3078 	if (netif_is_lag_master(info->upper_dev)) {
3079 		if (info->linking) {
3080 			err = dsa_master_lag_join(dev, info->upper_dev,
3081 						  info->upper_info, extack);
3082 			err = notifier_from_errno(err);
3083 		} else {
3084 			dsa_master_lag_leave(dev, info->upper_dev);
3085 			err = NOTIFY_OK;
3086 		}
3087 	}
3088 
3089 	return err;
3090 }
3091 
3092 static int dsa_slave_netdevice_event(struct notifier_block *nb,
3093 				     unsigned long event, void *ptr)
3094 {
3095 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3096 
3097 	switch (event) {
3098 	case NETDEV_PRECHANGEUPPER: {
3099 		struct netdev_notifier_changeupper_info *info = ptr;
3100 		int err;
3101 
3102 		err = dsa_slave_prechangeupper_sanity_check(dev, info);
3103 		if (notifier_to_errno(err))
3104 			return err;
3105 
3106 		err = dsa_master_prechangeupper_sanity_check(dev, info);
3107 		if (notifier_to_errno(err))
3108 			return err;
3109 
3110 		err = dsa_lag_master_prechangelower_sanity_check(dev, info);
3111 		if (notifier_to_errno(err))
3112 			return err;
3113 
3114 		err = dsa_bridge_prechangelower_sanity_check(dev, info);
3115 		if (notifier_to_errno(err))
3116 			return err;
3117 
3118 		err = dsa_slave_prechangeupper(dev, ptr);
3119 		if (notifier_to_errno(err))
3120 			return err;
3121 
3122 		err = dsa_slave_lag_prechangeupper(dev, ptr);
3123 		if (notifier_to_errno(err))
3124 			return err;
3125 
3126 		break;
3127 	}
3128 	case NETDEV_CHANGEUPPER: {
3129 		int err;
3130 
3131 		err = dsa_slave_changeupper(dev, ptr);
3132 		if (notifier_to_errno(err))
3133 			return err;
3134 
3135 		err = dsa_slave_lag_changeupper(dev, ptr);
3136 		if (notifier_to_errno(err))
3137 			return err;
3138 
3139 		err = dsa_master_changeupper(dev, ptr);
3140 		if (notifier_to_errno(err))
3141 			return err;
3142 
3143 		break;
3144 	}
3145 	case NETDEV_CHANGELOWERSTATE: {
3146 		struct netdev_notifier_changelowerstate_info *info = ptr;
3147 		struct dsa_port *dp;
3148 		int err = 0;
3149 
3150 		if (dsa_slave_dev_check(dev)) {
3151 			dp = dsa_slave_to_port(dev);
3152 
3153 			err = dsa_port_lag_change(dp, info->lower_state_info);
3154 		}
3155 
3156 		/* Mirror LAG port events on DSA masters that are in
3157 		 * a LAG towards their respective switch CPU ports
3158 		 */
3159 		if (netdev_uses_dsa(dev)) {
3160 			dp = dev->dsa_ptr;
3161 
3162 			err = dsa_port_lag_change(dp, info->lower_state_info);
3163 		}
3164 
3165 		return notifier_from_errno(err);
3166 	}
3167 	case NETDEV_CHANGE:
3168 	case NETDEV_UP: {
3169 		/* Track state of master port.
3170 		 * DSA driver may require the master port (and indirectly
3171 		 * the tagger) to be available for some special operation.
3172 		 */
3173 		if (netdev_uses_dsa(dev)) {
3174 			struct dsa_port *cpu_dp = dev->dsa_ptr;
3175 			struct dsa_switch_tree *dst = cpu_dp->ds->dst;
3176 
3177 			/* Track when the master port is UP */
3178 			dsa_tree_master_oper_state_change(dst, dev,
3179 							  netif_oper_up(dev));
3180 
3181 			/* Track when the master port is ready and can accept
3182 			 * packet.
3183 			 * NETDEV_UP event is not enough to flag a port as ready.
3184 			 * We also have to wait for linkwatch_do_dev to dev_activate
3185 			 * and emit a NETDEV_CHANGE event.
3186 			 * We check if a master port is ready by checking if the dev
3187 			 * have a qdisc assigned and is not noop.
3188 			 */
3189 			dsa_tree_master_admin_state_change(dst, dev,
3190 							   !qdisc_tx_is_noop(dev));
3191 
3192 			return NOTIFY_OK;
3193 		}
3194 
3195 		return NOTIFY_DONE;
3196 	}
3197 	case NETDEV_GOING_DOWN: {
3198 		struct dsa_port *dp, *cpu_dp;
3199 		struct dsa_switch_tree *dst;
3200 		LIST_HEAD(close_list);
3201 
3202 		if (!netdev_uses_dsa(dev))
3203 			return NOTIFY_DONE;
3204 
3205 		cpu_dp = dev->dsa_ptr;
3206 		dst = cpu_dp->ds->dst;
3207 
3208 		dsa_tree_master_admin_state_change(dst, dev, false);
3209 
3210 		list_for_each_entry(dp, &dst->ports, list) {
3211 			if (!dsa_port_is_user(dp))
3212 				continue;
3213 
3214 			if (dp->cpu_dp != cpu_dp)
3215 				continue;
3216 
3217 			list_add(&dp->slave->close_list, &close_list);
3218 		}
3219 
3220 		dev_close_many(&close_list, true);
3221 
3222 		return NOTIFY_OK;
3223 	}
3224 	default:
3225 		break;
3226 	}
3227 
3228 	return NOTIFY_DONE;
3229 }
3230 
3231 static void
3232 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
3233 {
3234 	struct switchdev_notifier_fdb_info info = {};
3235 
3236 	info.addr = switchdev_work->addr;
3237 	info.vid = switchdev_work->vid;
3238 	info.offloaded = true;
3239 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
3240 				 switchdev_work->orig_dev, &info.info, NULL);
3241 }
3242 
3243 static void dsa_slave_switchdev_event_work(struct work_struct *work)
3244 {
3245 	struct dsa_switchdev_event_work *switchdev_work =
3246 		container_of(work, struct dsa_switchdev_event_work, work);
3247 	const unsigned char *addr = switchdev_work->addr;
3248 	struct net_device *dev = switchdev_work->dev;
3249 	u16 vid = switchdev_work->vid;
3250 	struct dsa_switch *ds;
3251 	struct dsa_port *dp;
3252 	int err;
3253 
3254 	dp = dsa_slave_to_port(dev);
3255 	ds = dp->ds;
3256 
3257 	switch (switchdev_work->event) {
3258 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3259 		if (switchdev_work->host_addr)
3260 			err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
3261 		else if (dp->lag)
3262 			err = dsa_port_lag_fdb_add(dp, addr, vid);
3263 		else
3264 			err = dsa_port_fdb_add(dp, addr, vid);
3265 		if (err) {
3266 			dev_err(ds->dev,
3267 				"port %d failed to add %pM vid %d to fdb: %d\n",
3268 				dp->index, addr, vid, err);
3269 			break;
3270 		}
3271 		dsa_fdb_offload_notify(switchdev_work);
3272 		break;
3273 
3274 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3275 		if (switchdev_work->host_addr)
3276 			err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
3277 		else if (dp->lag)
3278 			err = dsa_port_lag_fdb_del(dp, addr, vid);
3279 		else
3280 			err = dsa_port_fdb_del(dp, addr, vid);
3281 		if (err) {
3282 			dev_err(ds->dev,
3283 				"port %d failed to delete %pM vid %d from fdb: %d\n",
3284 				dp->index, addr, vid, err);
3285 		}
3286 
3287 		break;
3288 	}
3289 
3290 	kfree(switchdev_work);
3291 }
3292 
3293 static bool dsa_foreign_dev_check(const struct net_device *dev,
3294 				  const struct net_device *foreign_dev)
3295 {
3296 	const struct dsa_port *dp = dsa_slave_to_port(dev);
3297 	struct dsa_switch_tree *dst = dp->ds->dst;
3298 
3299 	if (netif_is_bridge_master(foreign_dev))
3300 		return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
3301 
3302 	if (netif_is_bridge_port(foreign_dev))
3303 		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
3304 
3305 	/* Everything else is foreign */
3306 	return true;
3307 }
3308 
3309 static int dsa_slave_fdb_event(struct net_device *dev,
3310 			       struct net_device *orig_dev,
3311 			       unsigned long event, const void *ctx,
3312 			       const struct switchdev_notifier_fdb_info *fdb_info)
3313 {
3314 	struct dsa_switchdev_event_work *switchdev_work;
3315 	struct dsa_port *dp = dsa_slave_to_port(dev);
3316 	bool host_addr = fdb_info->is_local;
3317 	struct dsa_switch *ds = dp->ds;
3318 
3319 	if (ctx && ctx != dp)
3320 		return 0;
3321 
3322 	if (!dp->bridge)
3323 		return 0;
3324 
3325 	if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
3326 		if (dsa_port_offloads_bridge_port(dp, orig_dev))
3327 			return 0;
3328 
3329 		/* FDB entries learned by the software bridge or by foreign
3330 		 * bridge ports should be installed as host addresses only if
3331 		 * the driver requests assisted learning.
3332 		 */
3333 		if (!ds->assisted_learning_on_cpu_port)
3334 			return 0;
3335 	}
3336 
3337 	/* Also treat FDB entries on foreign interfaces bridged with us as host
3338 	 * addresses.
3339 	 */
3340 	if (dsa_foreign_dev_check(dev, orig_dev))
3341 		host_addr = true;
3342 
3343 	/* Check early that we're not doing work in vain.
3344 	 * Host addresses on LAG ports still require regular FDB ops,
3345 	 * since the CPU port isn't in a LAG.
3346 	 */
3347 	if (dp->lag && !host_addr) {
3348 		if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
3349 			return -EOPNOTSUPP;
3350 	} else {
3351 		if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
3352 			return -EOPNOTSUPP;
3353 	}
3354 
3355 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3356 	if (!switchdev_work)
3357 		return -ENOMEM;
3358 
3359 	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
3360 		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
3361 		   orig_dev->name, fdb_info->addr, fdb_info->vid,
3362 		   host_addr ? " as host address" : "");
3363 
3364 	INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
3365 	switchdev_work->event = event;
3366 	switchdev_work->dev = dev;
3367 	switchdev_work->orig_dev = orig_dev;
3368 
3369 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
3370 	switchdev_work->vid = fdb_info->vid;
3371 	switchdev_work->host_addr = host_addr;
3372 
3373 	dsa_schedule_work(&switchdev_work->work);
3374 
3375 	return 0;
3376 }
3377 
3378 /* Called under rcu_read_lock() */
3379 static int dsa_slave_switchdev_event(struct notifier_block *unused,
3380 				     unsigned long event, void *ptr)
3381 {
3382 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3383 	int err;
3384 
3385 	switch (event) {
3386 	case SWITCHDEV_PORT_ATTR_SET:
3387 		err = switchdev_handle_port_attr_set(dev, ptr,
3388 						     dsa_slave_dev_check,
3389 						     dsa_slave_port_attr_set);
3390 		return notifier_from_errno(err);
3391 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3392 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3393 		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
3394 							   dsa_slave_dev_check,
3395 							   dsa_foreign_dev_check,
3396 							   dsa_slave_fdb_event);
3397 		return notifier_from_errno(err);
3398 	default:
3399 		return NOTIFY_DONE;
3400 	}
3401 
3402 	return NOTIFY_OK;
3403 }
3404 
3405 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
3406 					      unsigned long event, void *ptr)
3407 {
3408 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3409 	int err;
3410 
3411 	switch (event) {
3412 	case SWITCHDEV_PORT_OBJ_ADD:
3413 		err = switchdev_handle_port_obj_add_foreign(dev, ptr,
3414 							    dsa_slave_dev_check,
3415 							    dsa_foreign_dev_check,
3416 							    dsa_slave_port_obj_add);
3417 		return notifier_from_errno(err);
3418 	case SWITCHDEV_PORT_OBJ_DEL:
3419 		err = switchdev_handle_port_obj_del_foreign(dev, ptr,
3420 							    dsa_slave_dev_check,
3421 							    dsa_foreign_dev_check,
3422 							    dsa_slave_port_obj_del);
3423 		return notifier_from_errno(err);
3424 	case SWITCHDEV_PORT_ATTR_SET:
3425 		err = switchdev_handle_port_attr_set(dev, ptr,
3426 						     dsa_slave_dev_check,
3427 						     dsa_slave_port_attr_set);
3428 		return notifier_from_errno(err);
3429 	}
3430 
3431 	return NOTIFY_DONE;
3432 }
3433 
3434 static struct notifier_block dsa_slave_nb __read_mostly = {
3435 	.notifier_call  = dsa_slave_netdevice_event,
3436 };
3437 
3438 struct notifier_block dsa_slave_switchdev_notifier = {
3439 	.notifier_call = dsa_slave_switchdev_event,
3440 };
3441 
3442 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
3443 	.notifier_call = dsa_slave_switchdev_blocking_event,
3444 };
3445 
3446 int dsa_slave_register_notifier(void)
3447 {
3448 	struct notifier_block *nb;
3449 	int err;
3450 
3451 	err = register_netdevice_notifier(&dsa_slave_nb);
3452 	if (err)
3453 		return err;
3454 
3455 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
3456 	if (err)
3457 		goto err_switchdev_nb;
3458 
3459 	nb = &dsa_slave_switchdev_blocking_notifier;
3460 	err = register_switchdev_blocking_notifier(nb);
3461 	if (err)
3462 		goto err_switchdev_blocking_nb;
3463 
3464 	return 0;
3465 
3466 err_switchdev_blocking_nb:
3467 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3468 err_switchdev_nb:
3469 	unregister_netdevice_notifier(&dsa_slave_nb);
3470 	return err;
3471 }
3472 
3473 void dsa_slave_unregister_notifier(void)
3474 {
3475 	struct notifier_block *nb;
3476 	int err;
3477 
3478 	nb = &dsa_slave_switchdev_blocking_notifier;
3479 	err = unregister_switchdev_blocking_notifier(nb);
3480 	if (err)
3481 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
3482 
3483 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3484 	if (err)
3485 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3486 
3487 	err = unregister_netdevice_notifier(&dsa_slave_nb);
3488 	if (err)
3489 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
3490 }
3491