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