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