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