xref: /openbmc/linux/net/dsa/slave.c (revision 9f99d983)
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_dev(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_dev(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(dsa_port_bridge_dev_get(dp))) {
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_dev(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_dev(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_dev(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_dev(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_dev(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_dev(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)
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 (!dsa_port_bridge_same(dp, other_dp))
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 	u32 phy_flags = 0;
1855 	int ret;
1856 
1857 	dp->pl_config.dev = &slave_dev->dev;
1858 	dp->pl_config.type = PHYLINK_NETDEV;
1859 
1860 	/* The get_fixed_state callback takes precedence over polling the
1861 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
1862 	 * this if the switch provides such a callback.
1863 	 */
1864 	if (ds->ops->phylink_fixed_state) {
1865 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
1866 		dp->pl_config.poll_fixed_state = true;
1867 	}
1868 
1869 	ret = dsa_port_phylink_create(dp);
1870 	if (ret)
1871 		return ret;
1872 
1873 	if (ds->ops->get_phy_flags)
1874 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1875 
1876 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1877 	if (ret == -ENODEV && ds->slave_mii_bus) {
1878 		/* We could not connect to a designated PHY or SFP, so try to
1879 		 * use the switch internal MDIO bus instead
1880 		 */
1881 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
1882 	}
1883 	if (ret) {
1884 		netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1885 			   ERR_PTR(ret));
1886 		phylink_destroy(dp->pl);
1887 	}
1888 
1889 	return ret;
1890 }
1891 
1892 void dsa_slave_setup_tagger(struct net_device *slave)
1893 {
1894 	struct dsa_port *dp = dsa_slave_to_port(slave);
1895 	struct dsa_slave_priv *p = netdev_priv(slave);
1896 	const struct dsa_port *cpu_dp = dp->cpu_dp;
1897 	struct net_device *master = cpu_dp->master;
1898 	const struct dsa_switch *ds = dp->ds;
1899 
1900 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
1901 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
1902 	/* Try to save one extra realloc later in the TX path (in the master)
1903 	 * by also inheriting the master's needed headroom and tailroom.
1904 	 * The 8021q driver also does this.
1905 	 */
1906 	slave->needed_headroom += master->needed_headroom;
1907 	slave->needed_tailroom += master->needed_tailroom;
1908 
1909 	p->xmit = cpu_dp->tag_ops->xmit;
1910 
1911 	slave->features = master->vlan_features | NETIF_F_HW_TC;
1912 	slave->hw_features |= NETIF_F_HW_TC;
1913 	slave->features |= NETIF_F_LLTX;
1914 	if (slave->needed_tailroom)
1915 		slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
1916 	if (ds->needs_standalone_vlan_filtering)
1917 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1918 }
1919 
1920 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1921 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1922 					    struct netdev_queue *txq,
1923 					    void *_unused)
1924 {
1925 	lockdep_set_class(&txq->_xmit_lock,
1926 			  &dsa_slave_netdev_xmit_lock_key);
1927 }
1928 
1929 int dsa_slave_suspend(struct net_device *slave_dev)
1930 {
1931 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1932 
1933 	if (!netif_running(slave_dev))
1934 		return 0;
1935 
1936 	netif_device_detach(slave_dev);
1937 
1938 	rtnl_lock();
1939 	phylink_stop(dp->pl);
1940 	rtnl_unlock();
1941 
1942 	return 0;
1943 }
1944 
1945 int dsa_slave_resume(struct net_device *slave_dev)
1946 {
1947 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1948 
1949 	if (!netif_running(slave_dev))
1950 		return 0;
1951 
1952 	netif_device_attach(slave_dev);
1953 
1954 	rtnl_lock();
1955 	phylink_start(dp->pl);
1956 	rtnl_unlock();
1957 
1958 	return 0;
1959 }
1960 
1961 int dsa_slave_create(struct dsa_port *port)
1962 {
1963 	const struct dsa_port *cpu_dp = port->cpu_dp;
1964 	struct net_device *master = cpu_dp->master;
1965 	struct dsa_switch *ds = port->ds;
1966 	const char *name = port->name;
1967 	struct net_device *slave_dev;
1968 	struct dsa_slave_priv *p;
1969 	int ret;
1970 
1971 	if (!ds->num_tx_queues)
1972 		ds->num_tx_queues = 1;
1973 
1974 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1975 				     NET_NAME_UNKNOWN, ether_setup,
1976 				     ds->num_tx_queues, 1);
1977 	if (slave_dev == NULL)
1978 		return -ENOMEM;
1979 
1980 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1981 	if (!is_zero_ether_addr(port->mac))
1982 		eth_hw_addr_set(slave_dev, port->mac);
1983 	else
1984 		eth_hw_addr_inherit(slave_dev, master);
1985 	slave_dev->priv_flags |= IFF_NO_QUEUE;
1986 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1987 	if (ds->ops->port_max_mtu)
1988 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1989 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1990 
1991 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1992 				 NULL);
1993 
1994 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
1995 	slave_dev->dev.of_node = port->dn;
1996 	slave_dev->vlan_features = master->vlan_features;
1997 
1998 	p = netdev_priv(slave_dev);
1999 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2000 	if (!slave_dev->tstats) {
2001 		free_netdev(slave_dev);
2002 		return -ENOMEM;
2003 	}
2004 
2005 	ret = gro_cells_init(&p->gcells, slave_dev);
2006 	if (ret)
2007 		goto out_free;
2008 
2009 	p->dp = port;
2010 	INIT_LIST_HEAD(&p->mall_tc_list);
2011 	port->slave = slave_dev;
2012 	dsa_slave_setup_tagger(slave_dev);
2013 
2014 	netif_carrier_off(slave_dev);
2015 
2016 	ret = dsa_slave_phy_setup(slave_dev);
2017 	if (ret) {
2018 		netdev_err(slave_dev,
2019 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2020 			   ret, ds->dst->index, ds->index, port->index);
2021 		goto out_gcells;
2022 	}
2023 
2024 	rtnl_lock();
2025 
2026 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2027 	if (ret && ret != -EOPNOTSUPP)
2028 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2029 			 ret, ETH_DATA_LEN, port->index);
2030 
2031 	ret = register_netdevice(slave_dev);
2032 	if (ret) {
2033 		netdev_err(master, "error %d registering interface %s\n",
2034 			   ret, slave_dev->name);
2035 		rtnl_unlock();
2036 		goto out_phy;
2037 	}
2038 
2039 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
2040 
2041 	rtnl_unlock();
2042 
2043 	if (ret)
2044 		goto out_unregister;
2045 
2046 	return 0;
2047 
2048 out_unregister:
2049 	unregister_netdev(slave_dev);
2050 out_phy:
2051 	rtnl_lock();
2052 	phylink_disconnect_phy(p->dp->pl);
2053 	rtnl_unlock();
2054 	phylink_destroy(p->dp->pl);
2055 out_gcells:
2056 	gro_cells_destroy(&p->gcells);
2057 out_free:
2058 	free_percpu(slave_dev->tstats);
2059 	free_netdev(slave_dev);
2060 	port->slave = NULL;
2061 	return ret;
2062 }
2063 
2064 void dsa_slave_destroy(struct net_device *slave_dev)
2065 {
2066 	struct net_device *master = dsa_slave_to_master(slave_dev);
2067 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2068 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2069 
2070 	netif_carrier_off(slave_dev);
2071 	rtnl_lock();
2072 	netdev_upper_dev_unlink(master, slave_dev);
2073 	unregister_netdevice(slave_dev);
2074 	phylink_disconnect_phy(dp->pl);
2075 	rtnl_unlock();
2076 
2077 	phylink_destroy(dp->pl);
2078 	gro_cells_destroy(&p->gcells);
2079 	free_percpu(slave_dev->tstats);
2080 	free_netdev(slave_dev);
2081 }
2082 
2083 bool dsa_slave_dev_check(const struct net_device *dev)
2084 {
2085 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2086 }
2087 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2088 
2089 static int dsa_slave_changeupper(struct net_device *dev,
2090 				 struct netdev_notifier_changeupper_info *info)
2091 {
2092 	struct dsa_port *dp = dsa_slave_to_port(dev);
2093 	struct netlink_ext_ack *extack;
2094 	int err = NOTIFY_DONE;
2095 
2096 	extack = netdev_notifier_info_to_extack(&info->info);
2097 
2098 	if (netif_is_bridge_master(info->upper_dev)) {
2099 		if (info->linking) {
2100 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2101 			if (!err)
2102 				dsa_bridge_mtu_normalization(dp);
2103 			if (err == -EOPNOTSUPP) {
2104 				NL_SET_ERR_MSG_MOD(extack,
2105 						   "Offloading not supported");
2106 				err = 0;
2107 			}
2108 			err = notifier_from_errno(err);
2109 		} else {
2110 			dsa_port_bridge_leave(dp, info->upper_dev);
2111 			err = NOTIFY_OK;
2112 		}
2113 	} else if (netif_is_lag_master(info->upper_dev)) {
2114 		if (info->linking) {
2115 			err = dsa_port_lag_join(dp, info->upper_dev,
2116 						info->upper_info, extack);
2117 			if (err == -EOPNOTSUPP) {
2118 				NL_SET_ERR_MSG_MOD(info->info.extack,
2119 						   "Offloading not supported");
2120 				err = 0;
2121 			}
2122 			err = notifier_from_errno(err);
2123 		} else {
2124 			dsa_port_lag_leave(dp, info->upper_dev);
2125 			err = NOTIFY_OK;
2126 		}
2127 	} else if (is_hsr_master(info->upper_dev)) {
2128 		if (info->linking) {
2129 			err = dsa_port_hsr_join(dp, info->upper_dev);
2130 			if (err == -EOPNOTSUPP) {
2131 				NL_SET_ERR_MSG_MOD(info->info.extack,
2132 						   "Offloading not supported");
2133 				err = 0;
2134 			}
2135 			err = notifier_from_errno(err);
2136 		} else {
2137 			dsa_port_hsr_leave(dp, info->upper_dev);
2138 			err = NOTIFY_OK;
2139 		}
2140 	}
2141 
2142 	return err;
2143 }
2144 
2145 static int dsa_slave_prechangeupper(struct net_device *dev,
2146 				    struct netdev_notifier_changeupper_info *info)
2147 {
2148 	struct dsa_port *dp = dsa_slave_to_port(dev);
2149 
2150 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2151 		dsa_port_pre_bridge_leave(dp, info->upper_dev);
2152 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2153 		dsa_port_pre_lag_leave(dp, info->upper_dev);
2154 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2155 	 * meaningfully enslaved to a bridge yet
2156 	 */
2157 
2158 	return NOTIFY_DONE;
2159 }
2160 
2161 static int
2162 dsa_slave_lag_changeupper(struct net_device *dev,
2163 			  struct netdev_notifier_changeupper_info *info)
2164 {
2165 	struct net_device *lower;
2166 	struct list_head *iter;
2167 	int err = NOTIFY_DONE;
2168 	struct dsa_port *dp;
2169 
2170 	netdev_for_each_lower_dev(dev, lower, iter) {
2171 		if (!dsa_slave_dev_check(lower))
2172 			continue;
2173 
2174 		dp = dsa_slave_to_port(lower);
2175 		if (!dp->lag_dev)
2176 			/* Software LAG */
2177 			continue;
2178 
2179 		err = dsa_slave_changeupper(lower, info);
2180 		if (notifier_to_errno(err))
2181 			break;
2182 	}
2183 
2184 	return err;
2185 }
2186 
2187 /* Same as dsa_slave_lag_changeupper() except that it calls
2188  * dsa_slave_prechangeupper()
2189  */
2190 static int
2191 dsa_slave_lag_prechangeupper(struct net_device *dev,
2192 			     struct netdev_notifier_changeupper_info *info)
2193 {
2194 	struct net_device *lower;
2195 	struct list_head *iter;
2196 	int err = NOTIFY_DONE;
2197 	struct dsa_port *dp;
2198 
2199 	netdev_for_each_lower_dev(dev, lower, iter) {
2200 		if (!dsa_slave_dev_check(lower))
2201 			continue;
2202 
2203 		dp = dsa_slave_to_port(lower);
2204 		if (!dp->lag_dev)
2205 			/* Software LAG */
2206 			continue;
2207 
2208 		err = dsa_slave_prechangeupper(lower, info);
2209 		if (notifier_to_errno(err))
2210 			break;
2211 	}
2212 
2213 	return err;
2214 }
2215 
2216 static int
2217 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2218 				 struct netdev_notifier_changeupper_info *info)
2219 {
2220 	struct netlink_ext_ack *ext_ack;
2221 	struct net_device *slave, *br;
2222 	struct dsa_port *dp;
2223 
2224 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2225 
2226 	if (!is_vlan_dev(dev))
2227 		return NOTIFY_DONE;
2228 
2229 	slave = vlan_dev_real_dev(dev);
2230 	if (!dsa_slave_dev_check(slave))
2231 		return NOTIFY_DONE;
2232 
2233 	dp = dsa_slave_to_port(slave);
2234 	br = dsa_port_bridge_dev_get(dp);
2235 	if (!br)
2236 		return NOTIFY_DONE;
2237 
2238 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2239 	if (br_vlan_enabled(br) &&
2240 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2241 		NL_SET_ERR_MSG_MOD(ext_ack,
2242 				   "Cannot enslave VLAN device into VLAN aware bridge");
2243 		return notifier_from_errno(-EINVAL);
2244 	}
2245 
2246 	return NOTIFY_DONE;
2247 }
2248 
2249 static int
2250 dsa_slave_check_8021q_upper(struct net_device *dev,
2251 			    struct netdev_notifier_changeupper_info *info)
2252 {
2253 	struct dsa_port *dp = dsa_slave_to_port(dev);
2254 	struct net_device *br = dsa_port_bridge_dev_get(dp);
2255 	struct bridge_vlan_info br_info;
2256 	struct netlink_ext_ack *extack;
2257 	int err = NOTIFY_DONE;
2258 	u16 vid;
2259 
2260 	if (!br || !br_vlan_enabled(br))
2261 		return NOTIFY_DONE;
2262 
2263 	extack = netdev_notifier_info_to_extack(&info->info);
2264 	vid = vlan_dev_vlan_id(info->upper_dev);
2265 
2266 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2267 	 * device, respectively the VID is not found, returning
2268 	 * 0 means success, which is a failure for us here.
2269 	 */
2270 	err = br_vlan_get_info(br, vid, &br_info);
2271 	if (err == 0) {
2272 		NL_SET_ERR_MSG_MOD(extack,
2273 				   "This VLAN is already configured by the bridge");
2274 		return notifier_from_errno(-EBUSY);
2275 	}
2276 
2277 	return NOTIFY_DONE;
2278 }
2279 
2280 static int
2281 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2282 				      struct netdev_notifier_changeupper_info *info)
2283 {
2284 	struct dsa_switch *ds;
2285 	struct dsa_port *dp;
2286 	int err;
2287 
2288 	if (!dsa_slave_dev_check(dev))
2289 		return dsa_prevent_bridging_8021q_upper(dev, info);
2290 
2291 	dp = dsa_slave_to_port(dev);
2292 	ds = dp->ds;
2293 
2294 	if (ds->ops->port_prechangeupper) {
2295 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
2296 		if (err)
2297 			return notifier_from_errno(err);
2298 	}
2299 
2300 	if (is_vlan_dev(info->upper_dev))
2301 		return dsa_slave_check_8021q_upper(dev, info);
2302 
2303 	return NOTIFY_DONE;
2304 }
2305 
2306 static int dsa_slave_netdevice_event(struct notifier_block *nb,
2307 				     unsigned long event, void *ptr)
2308 {
2309 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2310 
2311 	switch (event) {
2312 	case NETDEV_PRECHANGEUPPER: {
2313 		struct netdev_notifier_changeupper_info *info = ptr;
2314 		int err;
2315 
2316 		err = dsa_slave_prechangeupper_sanity_check(dev, info);
2317 		if (err != NOTIFY_DONE)
2318 			return err;
2319 
2320 		if (dsa_slave_dev_check(dev))
2321 			return dsa_slave_prechangeupper(dev, ptr);
2322 
2323 		if (netif_is_lag_master(dev))
2324 			return dsa_slave_lag_prechangeupper(dev, ptr);
2325 
2326 		break;
2327 	}
2328 	case NETDEV_CHANGEUPPER:
2329 		if (dsa_slave_dev_check(dev))
2330 			return dsa_slave_changeupper(dev, ptr);
2331 
2332 		if (netif_is_lag_master(dev))
2333 			return dsa_slave_lag_changeupper(dev, ptr);
2334 
2335 		break;
2336 	case NETDEV_CHANGELOWERSTATE: {
2337 		struct netdev_notifier_changelowerstate_info *info = ptr;
2338 		struct dsa_port *dp;
2339 		int err;
2340 
2341 		if (!dsa_slave_dev_check(dev))
2342 			break;
2343 
2344 		dp = dsa_slave_to_port(dev);
2345 
2346 		err = dsa_port_lag_change(dp, info->lower_state_info);
2347 		return notifier_from_errno(err);
2348 	}
2349 	case NETDEV_GOING_DOWN: {
2350 		struct dsa_port *dp, *cpu_dp;
2351 		struct dsa_switch_tree *dst;
2352 		LIST_HEAD(close_list);
2353 
2354 		if (!netdev_uses_dsa(dev))
2355 			return NOTIFY_DONE;
2356 
2357 		cpu_dp = dev->dsa_ptr;
2358 		dst = cpu_dp->ds->dst;
2359 
2360 		list_for_each_entry(dp, &dst->ports, list) {
2361 			if (!dsa_port_is_user(dp))
2362 				continue;
2363 
2364 			list_add(&dp->slave->close_list, &close_list);
2365 		}
2366 
2367 		dev_close_many(&close_list, true);
2368 
2369 		return NOTIFY_OK;
2370 	}
2371 	default:
2372 		break;
2373 	}
2374 
2375 	return NOTIFY_DONE;
2376 }
2377 
2378 static void
2379 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2380 {
2381 	struct switchdev_notifier_fdb_info info = {};
2382 	struct dsa_switch *ds = switchdev_work->ds;
2383 	struct dsa_port *dp;
2384 
2385 	if (!dsa_is_user_port(ds, switchdev_work->port))
2386 		return;
2387 
2388 	info.addr = switchdev_work->addr;
2389 	info.vid = switchdev_work->vid;
2390 	info.offloaded = true;
2391 	dp = dsa_to_port(ds, switchdev_work->port);
2392 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2393 				 dp->slave, &info.info, NULL);
2394 }
2395 
2396 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2397 {
2398 	struct dsa_switchdev_event_work *switchdev_work =
2399 		container_of(work, struct dsa_switchdev_event_work, work);
2400 	struct dsa_switch *ds = switchdev_work->ds;
2401 	struct dsa_port *dp;
2402 	int err;
2403 
2404 	dp = dsa_to_port(ds, switchdev_work->port);
2405 
2406 	switch (switchdev_work->event) {
2407 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2408 		if (switchdev_work->host_addr)
2409 			err = dsa_port_host_fdb_add(dp, switchdev_work->addr,
2410 						    switchdev_work->vid);
2411 		else
2412 			err = dsa_port_fdb_add(dp, switchdev_work->addr,
2413 					       switchdev_work->vid);
2414 		if (err) {
2415 			dev_err(ds->dev,
2416 				"port %d failed to add %pM vid %d to fdb: %d\n",
2417 				dp->index, switchdev_work->addr,
2418 				switchdev_work->vid, err);
2419 			break;
2420 		}
2421 		dsa_fdb_offload_notify(switchdev_work);
2422 		break;
2423 
2424 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2425 		if (switchdev_work->host_addr)
2426 			err = dsa_port_host_fdb_del(dp, switchdev_work->addr,
2427 						    switchdev_work->vid);
2428 		else
2429 			err = dsa_port_fdb_del(dp, switchdev_work->addr,
2430 					       switchdev_work->vid);
2431 		if (err) {
2432 			dev_err(ds->dev,
2433 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2434 				dp->index, switchdev_work->addr,
2435 				switchdev_work->vid, err);
2436 		}
2437 
2438 		break;
2439 	}
2440 
2441 	kfree(switchdev_work);
2442 }
2443 
2444 static bool dsa_foreign_dev_check(const struct net_device *dev,
2445 				  const struct net_device *foreign_dev)
2446 {
2447 	const struct dsa_port *dp = dsa_slave_to_port(dev);
2448 	struct dsa_switch_tree *dst = dp->ds->dst;
2449 
2450 	if (netif_is_bridge_master(foreign_dev))
2451 		return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
2452 
2453 	if (netif_is_bridge_port(foreign_dev))
2454 		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2455 
2456 	/* Everything else is foreign */
2457 	return true;
2458 }
2459 
2460 static int dsa_slave_fdb_event(struct net_device *dev,
2461 			       struct net_device *orig_dev,
2462 			       unsigned long event, const void *ctx,
2463 			       const struct switchdev_notifier_fdb_info *fdb_info)
2464 {
2465 	struct dsa_switchdev_event_work *switchdev_work;
2466 	struct dsa_port *dp = dsa_slave_to_port(dev);
2467 	bool host_addr = fdb_info->is_local;
2468 	struct dsa_switch *ds = dp->ds;
2469 
2470 	if (ctx && ctx != dp)
2471 		return 0;
2472 
2473 	if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2474 		return -EOPNOTSUPP;
2475 
2476 	if (dsa_slave_dev_check(orig_dev) &&
2477 	    switchdev_fdb_is_dynamically_learned(fdb_info))
2478 		return 0;
2479 
2480 	/* FDB entries learned by the software bridge should be installed as
2481 	 * host addresses only if the driver requests assisted learning.
2482 	 */
2483 	if (switchdev_fdb_is_dynamically_learned(fdb_info) &&
2484 	    !ds->assisted_learning_on_cpu_port)
2485 		return 0;
2486 
2487 	/* Also treat FDB entries on foreign interfaces bridged with us as host
2488 	 * addresses.
2489 	 */
2490 	if (dsa_foreign_dev_check(dev, orig_dev))
2491 		host_addr = true;
2492 
2493 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2494 	if (!switchdev_work)
2495 		return -ENOMEM;
2496 
2497 	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2498 		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2499 		   orig_dev->name, fdb_info->addr, fdb_info->vid,
2500 		   host_addr ? " as host address" : "");
2501 
2502 	INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2503 	switchdev_work->ds = ds;
2504 	switchdev_work->port = dp->index;
2505 	switchdev_work->event = event;
2506 	switchdev_work->dev = dev;
2507 
2508 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2509 	switchdev_work->vid = fdb_info->vid;
2510 	switchdev_work->host_addr = host_addr;
2511 
2512 	dsa_schedule_work(&switchdev_work->work);
2513 
2514 	return 0;
2515 }
2516 
2517 /* Called under rcu_read_lock() */
2518 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2519 				     unsigned long event, void *ptr)
2520 {
2521 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2522 	int err;
2523 
2524 	switch (event) {
2525 	case SWITCHDEV_PORT_ATTR_SET:
2526 		err = switchdev_handle_port_attr_set(dev, ptr,
2527 						     dsa_slave_dev_check,
2528 						     dsa_slave_port_attr_set);
2529 		return notifier_from_errno(err);
2530 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2531 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2532 		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
2533 							   dsa_slave_dev_check,
2534 							   dsa_foreign_dev_check,
2535 							   dsa_slave_fdb_event,
2536 							   NULL);
2537 		return notifier_from_errno(err);
2538 	default:
2539 		return NOTIFY_DONE;
2540 	}
2541 
2542 	return NOTIFY_OK;
2543 }
2544 
2545 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2546 					      unsigned long event, void *ptr)
2547 {
2548 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2549 	int err;
2550 
2551 	switch (event) {
2552 	case SWITCHDEV_PORT_OBJ_ADD:
2553 		err = switchdev_handle_port_obj_add(dev, ptr,
2554 						    dsa_slave_dev_check,
2555 						    dsa_slave_port_obj_add);
2556 		return notifier_from_errno(err);
2557 	case SWITCHDEV_PORT_OBJ_DEL:
2558 		err = switchdev_handle_port_obj_del(dev, ptr,
2559 						    dsa_slave_dev_check,
2560 						    dsa_slave_port_obj_del);
2561 		return notifier_from_errno(err);
2562 	case SWITCHDEV_PORT_ATTR_SET:
2563 		err = switchdev_handle_port_attr_set(dev, ptr,
2564 						     dsa_slave_dev_check,
2565 						     dsa_slave_port_attr_set);
2566 		return notifier_from_errno(err);
2567 	}
2568 
2569 	return NOTIFY_DONE;
2570 }
2571 
2572 static struct notifier_block dsa_slave_nb __read_mostly = {
2573 	.notifier_call  = dsa_slave_netdevice_event,
2574 };
2575 
2576 struct notifier_block dsa_slave_switchdev_notifier = {
2577 	.notifier_call = dsa_slave_switchdev_event,
2578 };
2579 
2580 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
2581 	.notifier_call = dsa_slave_switchdev_blocking_event,
2582 };
2583 
2584 int dsa_slave_register_notifier(void)
2585 {
2586 	struct notifier_block *nb;
2587 	int err;
2588 
2589 	err = register_netdevice_notifier(&dsa_slave_nb);
2590 	if (err)
2591 		return err;
2592 
2593 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2594 	if (err)
2595 		goto err_switchdev_nb;
2596 
2597 	nb = &dsa_slave_switchdev_blocking_notifier;
2598 	err = register_switchdev_blocking_notifier(nb);
2599 	if (err)
2600 		goto err_switchdev_blocking_nb;
2601 
2602 	return 0;
2603 
2604 err_switchdev_blocking_nb:
2605 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2606 err_switchdev_nb:
2607 	unregister_netdevice_notifier(&dsa_slave_nb);
2608 	return err;
2609 }
2610 
2611 void dsa_slave_unregister_notifier(void)
2612 {
2613 	struct notifier_block *nb;
2614 	int err;
2615 
2616 	nb = &dsa_slave_switchdev_blocking_notifier;
2617 	err = unregister_switchdev_blocking_notifier(nb);
2618 	if (err)
2619 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
2620 
2621 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2622 	if (err)
2623 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2624 
2625 	err = unregister_netdevice_notifier(&dsa_slave_nb);
2626 	if (err)
2627 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
2628 }
2629