xref: /openbmc/linux/net/dsa/slave.c (revision 276e552e)
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;
780 
781 		count = 4;
782 		if (ds->ops->get_sset_count)
783 			count += ds->ops->get_sset_count(ds, dp->index, sset);
784 
785 		return count;
786 	} else if (sset ==  ETH_SS_TEST) {
787 		return net_selftest_get_count();
788 	}
789 
790 	return -EOPNOTSUPP;
791 }
792 
793 static void dsa_slave_net_selftest(struct net_device *ndev,
794 				   struct ethtool_test *etest, u64 *buf)
795 {
796 	struct dsa_port *dp = dsa_slave_to_port(ndev);
797 	struct dsa_switch *ds = dp->ds;
798 
799 	if (ds->ops->self_test) {
800 		ds->ops->self_test(ds, dp->index, etest, buf);
801 		return;
802 	}
803 
804 	net_selftest(ndev, etest, buf);
805 }
806 
807 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
808 {
809 	struct dsa_port *dp = dsa_slave_to_port(dev);
810 	struct dsa_switch *ds = dp->ds;
811 
812 	phylink_ethtool_get_wol(dp->pl, w);
813 
814 	if (ds->ops->get_wol)
815 		ds->ops->get_wol(ds, dp->index, w);
816 }
817 
818 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
819 {
820 	struct dsa_port *dp = dsa_slave_to_port(dev);
821 	struct dsa_switch *ds = dp->ds;
822 	int ret = -EOPNOTSUPP;
823 
824 	phylink_ethtool_set_wol(dp->pl, w);
825 
826 	if (ds->ops->set_wol)
827 		ret = ds->ops->set_wol(ds, dp->index, w);
828 
829 	return ret;
830 }
831 
832 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
833 {
834 	struct dsa_port *dp = dsa_slave_to_port(dev);
835 	struct dsa_switch *ds = dp->ds;
836 	int ret;
837 
838 	/* Port's PHY and MAC both need to be EEE capable */
839 	if (!dev->phydev || !dp->pl)
840 		return -ENODEV;
841 
842 	if (!ds->ops->set_mac_eee)
843 		return -EOPNOTSUPP;
844 
845 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
846 	if (ret)
847 		return ret;
848 
849 	return phylink_ethtool_set_eee(dp->pl, e);
850 }
851 
852 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
853 {
854 	struct dsa_port *dp = dsa_slave_to_port(dev);
855 	struct dsa_switch *ds = dp->ds;
856 	int ret;
857 
858 	/* Port's PHY and MAC both need to be EEE capable */
859 	if (!dev->phydev || !dp->pl)
860 		return -ENODEV;
861 
862 	if (!ds->ops->get_mac_eee)
863 		return -EOPNOTSUPP;
864 
865 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
866 	if (ret)
867 		return ret;
868 
869 	return phylink_ethtool_get_eee(dp->pl, e);
870 }
871 
872 static int dsa_slave_get_link_ksettings(struct net_device *dev,
873 					struct ethtool_link_ksettings *cmd)
874 {
875 	struct dsa_port *dp = dsa_slave_to_port(dev);
876 
877 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
878 }
879 
880 static int dsa_slave_set_link_ksettings(struct net_device *dev,
881 					const struct ethtool_link_ksettings *cmd)
882 {
883 	struct dsa_port *dp = dsa_slave_to_port(dev);
884 
885 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
886 }
887 
888 static void dsa_slave_get_pauseparam(struct net_device *dev,
889 				     struct ethtool_pauseparam *pause)
890 {
891 	struct dsa_port *dp = dsa_slave_to_port(dev);
892 
893 	phylink_ethtool_get_pauseparam(dp->pl, pause);
894 }
895 
896 static int dsa_slave_set_pauseparam(struct net_device *dev,
897 				    struct ethtool_pauseparam *pause)
898 {
899 	struct dsa_port *dp = dsa_slave_to_port(dev);
900 
901 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
902 }
903 
904 #ifdef CONFIG_NET_POLL_CONTROLLER
905 static int dsa_slave_netpoll_setup(struct net_device *dev,
906 				   struct netpoll_info *ni)
907 {
908 	struct net_device *master = dsa_slave_to_master(dev);
909 	struct dsa_slave_priv *p = netdev_priv(dev);
910 	struct netpoll *netpoll;
911 	int err = 0;
912 
913 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
914 	if (!netpoll)
915 		return -ENOMEM;
916 
917 	err = __netpoll_setup(netpoll, master);
918 	if (err) {
919 		kfree(netpoll);
920 		goto out;
921 	}
922 
923 	p->netpoll = netpoll;
924 out:
925 	return err;
926 }
927 
928 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
929 {
930 	struct dsa_slave_priv *p = netdev_priv(dev);
931 	struct netpoll *netpoll = p->netpoll;
932 
933 	if (!netpoll)
934 		return;
935 
936 	p->netpoll = NULL;
937 
938 	__netpoll_free(netpoll);
939 }
940 
941 static void dsa_slave_poll_controller(struct net_device *dev)
942 {
943 }
944 #endif
945 
946 static int dsa_slave_get_phys_port_name(struct net_device *dev,
947 					char *name, size_t len)
948 {
949 	struct dsa_port *dp = dsa_slave_to_port(dev);
950 
951 	/* For non-legacy ports, devlink is used and it takes
952 	 * care of the name generation. This ndo implementation
953 	 * should be removed with legacy support.
954 	 */
955 	if (dp->ds->devlink)
956 		return -EOPNOTSUPP;
957 
958 	if (snprintf(name, len, "p%d", dp->index) >= len)
959 		return -EINVAL;
960 
961 	return 0;
962 }
963 
964 static struct dsa_mall_tc_entry *
965 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
966 {
967 	struct dsa_slave_priv *p = netdev_priv(dev);
968 	struct dsa_mall_tc_entry *mall_tc_entry;
969 
970 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
971 		if (mall_tc_entry->cookie == cookie)
972 			return mall_tc_entry;
973 
974 	return NULL;
975 }
976 
977 static int
978 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
979 				  struct tc_cls_matchall_offload *cls,
980 				  bool ingress)
981 {
982 	struct dsa_port *dp = dsa_slave_to_port(dev);
983 	struct dsa_slave_priv *p = netdev_priv(dev);
984 	struct dsa_mall_mirror_tc_entry *mirror;
985 	struct dsa_mall_tc_entry *mall_tc_entry;
986 	struct dsa_switch *ds = dp->ds;
987 	struct flow_action_entry *act;
988 	struct dsa_port *to_dp;
989 	int err;
990 
991 	if (!ds->ops->port_mirror_add)
992 		return -EOPNOTSUPP;
993 
994 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
995 					      cls->common.extack))
996 		return -EOPNOTSUPP;
997 
998 	act = &cls->rule->action.entries[0];
999 
1000 	if (!act->dev)
1001 		return -EINVAL;
1002 
1003 	if (!dsa_slave_dev_check(act->dev))
1004 		return -EOPNOTSUPP;
1005 
1006 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1007 	if (!mall_tc_entry)
1008 		return -ENOMEM;
1009 
1010 	mall_tc_entry->cookie = cls->cookie;
1011 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1012 	mirror = &mall_tc_entry->mirror;
1013 
1014 	to_dp = dsa_slave_to_port(act->dev);
1015 
1016 	mirror->to_local_port = to_dp->index;
1017 	mirror->ingress = ingress;
1018 
1019 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1020 	if (err) {
1021 		kfree(mall_tc_entry);
1022 		return err;
1023 	}
1024 
1025 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1026 
1027 	return err;
1028 }
1029 
1030 static int
1031 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1032 				  struct tc_cls_matchall_offload *cls,
1033 				  bool ingress)
1034 {
1035 	struct netlink_ext_ack *extack = cls->common.extack;
1036 	struct dsa_port *dp = dsa_slave_to_port(dev);
1037 	struct dsa_slave_priv *p = netdev_priv(dev);
1038 	struct dsa_mall_policer_tc_entry *policer;
1039 	struct dsa_mall_tc_entry *mall_tc_entry;
1040 	struct dsa_switch *ds = dp->ds;
1041 	struct flow_action_entry *act;
1042 	int err;
1043 
1044 	if (!ds->ops->port_policer_add) {
1045 		NL_SET_ERR_MSG_MOD(extack,
1046 				   "Policing offload not implemented");
1047 		return -EOPNOTSUPP;
1048 	}
1049 
1050 	if (!ingress) {
1051 		NL_SET_ERR_MSG_MOD(extack,
1052 				   "Only supported on ingress qdisc");
1053 		return -EOPNOTSUPP;
1054 	}
1055 
1056 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1057 					      cls->common.extack))
1058 		return -EOPNOTSUPP;
1059 
1060 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1061 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1062 			NL_SET_ERR_MSG_MOD(extack,
1063 					   "Only one port policer allowed");
1064 			return -EEXIST;
1065 		}
1066 	}
1067 
1068 	act = &cls->rule->action.entries[0];
1069 
1070 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1071 	if (!mall_tc_entry)
1072 		return -ENOMEM;
1073 
1074 	mall_tc_entry->cookie = cls->cookie;
1075 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1076 	policer = &mall_tc_entry->policer;
1077 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1078 	policer->burst = act->police.burst;
1079 
1080 	err = ds->ops->port_policer_add(ds, dp->index, policer);
1081 	if (err) {
1082 		kfree(mall_tc_entry);
1083 		return err;
1084 	}
1085 
1086 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1087 
1088 	return err;
1089 }
1090 
1091 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1092 				      struct tc_cls_matchall_offload *cls,
1093 				      bool ingress)
1094 {
1095 	int err = -EOPNOTSUPP;
1096 
1097 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1098 	    flow_offload_has_one_action(&cls->rule->action) &&
1099 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1100 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1101 	else if (flow_offload_has_one_action(&cls->rule->action) &&
1102 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1103 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1104 
1105 	return err;
1106 }
1107 
1108 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1109 				       struct tc_cls_matchall_offload *cls)
1110 {
1111 	struct dsa_port *dp = dsa_slave_to_port(dev);
1112 	struct dsa_mall_tc_entry *mall_tc_entry;
1113 	struct dsa_switch *ds = dp->ds;
1114 
1115 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1116 	if (!mall_tc_entry)
1117 		return;
1118 
1119 	list_del(&mall_tc_entry->list);
1120 
1121 	switch (mall_tc_entry->type) {
1122 	case DSA_PORT_MALL_MIRROR:
1123 		if (ds->ops->port_mirror_del)
1124 			ds->ops->port_mirror_del(ds, dp->index,
1125 						 &mall_tc_entry->mirror);
1126 		break;
1127 	case DSA_PORT_MALL_POLICER:
1128 		if (ds->ops->port_policer_del)
1129 			ds->ops->port_policer_del(ds, dp->index);
1130 		break;
1131 	default:
1132 		WARN_ON(1);
1133 	}
1134 
1135 	kfree(mall_tc_entry);
1136 }
1137 
1138 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1139 					   struct tc_cls_matchall_offload *cls,
1140 					   bool ingress)
1141 {
1142 	if (cls->common.chain_index)
1143 		return -EOPNOTSUPP;
1144 
1145 	switch (cls->command) {
1146 	case TC_CLSMATCHALL_REPLACE:
1147 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
1148 	case TC_CLSMATCHALL_DESTROY:
1149 		dsa_slave_del_cls_matchall(dev, cls);
1150 		return 0;
1151 	default:
1152 		return -EOPNOTSUPP;
1153 	}
1154 }
1155 
1156 static int dsa_slave_add_cls_flower(struct net_device *dev,
1157 				    struct flow_cls_offload *cls,
1158 				    bool ingress)
1159 {
1160 	struct dsa_port *dp = dsa_slave_to_port(dev);
1161 	struct dsa_switch *ds = dp->ds;
1162 	int port = dp->index;
1163 
1164 	if (!ds->ops->cls_flower_add)
1165 		return -EOPNOTSUPP;
1166 
1167 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1168 }
1169 
1170 static int dsa_slave_del_cls_flower(struct net_device *dev,
1171 				    struct flow_cls_offload *cls,
1172 				    bool ingress)
1173 {
1174 	struct dsa_port *dp = dsa_slave_to_port(dev);
1175 	struct dsa_switch *ds = dp->ds;
1176 	int port = dp->index;
1177 
1178 	if (!ds->ops->cls_flower_del)
1179 		return -EOPNOTSUPP;
1180 
1181 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1182 }
1183 
1184 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1185 				      struct flow_cls_offload *cls,
1186 				      bool ingress)
1187 {
1188 	struct dsa_port *dp = dsa_slave_to_port(dev);
1189 	struct dsa_switch *ds = dp->ds;
1190 	int port = dp->index;
1191 
1192 	if (!ds->ops->cls_flower_stats)
1193 		return -EOPNOTSUPP;
1194 
1195 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1196 }
1197 
1198 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1199 					 struct flow_cls_offload *cls,
1200 					 bool ingress)
1201 {
1202 	switch (cls->command) {
1203 	case FLOW_CLS_REPLACE:
1204 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1205 	case FLOW_CLS_DESTROY:
1206 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1207 	case FLOW_CLS_STATS:
1208 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1209 	default:
1210 		return -EOPNOTSUPP;
1211 	}
1212 }
1213 
1214 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1215 				       void *cb_priv, bool ingress)
1216 {
1217 	struct net_device *dev = cb_priv;
1218 
1219 	if (!tc_can_offload(dev))
1220 		return -EOPNOTSUPP;
1221 
1222 	switch (type) {
1223 	case TC_SETUP_CLSMATCHALL:
1224 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1225 	case TC_SETUP_CLSFLOWER:
1226 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1227 	default:
1228 		return -EOPNOTSUPP;
1229 	}
1230 }
1231 
1232 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1233 					  void *type_data, void *cb_priv)
1234 {
1235 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1236 }
1237 
1238 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1239 					  void *type_data, void *cb_priv)
1240 {
1241 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1242 }
1243 
1244 static LIST_HEAD(dsa_slave_block_cb_list);
1245 
1246 static int dsa_slave_setup_tc_block(struct net_device *dev,
1247 				    struct flow_block_offload *f)
1248 {
1249 	struct flow_block_cb *block_cb;
1250 	flow_setup_cb_t *cb;
1251 
1252 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1253 		cb = dsa_slave_setup_tc_block_cb_ig;
1254 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1255 		cb = dsa_slave_setup_tc_block_cb_eg;
1256 	else
1257 		return -EOPNOTSUPP;
1258 
1259 	f->driver_block_list = &dsa_slave_block_cb_list;
1260 
1261 	switch (f->command) {
1262 	case FLOW_BLOCK_BIND:
1263 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1264 			return -EBUSY;
1265 
1266 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1267 		if (IS_ERR(block_cb))
1268 			return PTR_ERR(block_cb);
1269 
1270 		flow_block_cb_add(block_cb, f);
1271 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1272 		return 0;
1273 	case FLOW_BLOCK_UNBIND:
1274 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1275 		if (!block_cb)
1276 			return -ENOENT;
1277 
1278 		flow_block_cb_remove(block_cb, f);
1279 		list_del(&block_cb->driver_list);
1280 		return 0;
1281 	default:
1282 		return -EOPNOTSUPP;
1283 	}
1284 }
1285 
1286 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1287 				    void *type_data)
1288 {
1289 	struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1290 	struct net_device *master = cpu_dp->master;
1291 
1292 	if (!master->netdev_ops->ndo_setup_tc)
1293 		return -EOPNOTSUPP;
1294 
1295 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1296 }
1297 
1298 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1299 			      void *type_data)
1300 {
1301 	struct dsa_port *dp = dsa_slave_to_port(dev);
1302 	struct dsa_switch *ds = dp->ds;
1303 
1304 	switch (type) {
1305 	case TC_SETUP_BLOCK:
1306 		return dsa_slave_setup_tc_block(dev, type_data);
1307 	case TC_SETUP_FT:
1308 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1309 	default:
1310 		break;
1311 	}
1312 
1313 	if (!ds->ops->port_setup_tc)
1314 		return -EOPNOTSUPP;
1315 
1316 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1317 }
1318 
1319 static int dsa_slave_get_rxnfc(struct net_device *dev,
1320 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1321 {
1322 	struct dsa_port *dp = dsa_slave_to_port(dev);
1323 	struct dsa_switch *ds = dp->ds;
1324 
1325 	if (!ds->ops->get_rxnfc)
1326 		return -EOPNOTSUPP;
1327 
1328 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1329 }
1330 
1331 static int dsa_slave_set_rxnfc(struct net_device *dev,
1332 			       struct ethtool_rxnfc *nfc)
1333 {
1334 	struct dsa_port *dp = dsa_slave_to_port(dev);
1335 	struct dsa_switch *ds = dp->ds;
1336 
1337 	if (!ds->ops->set_rxnfc)
1338 		return -EOPNOTSUPP;
1339 
1340 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1341 }
1342 
1343 static int dsa_slave_get_ts_info(struct net_device *dev,
1344 				 struct ethtool_ts_info *ts)
1345 {
1346 	struct dsa_slave_priv *p = netdev_priv(dev);
1347 	struct dsa_switch *ds = p->dp->ds;
1348 
1349 	if (!ds->ops->get_ts_info)
1350 		return -EOPNOTSUPP;
1351 
1352 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
1353 }
1354 
1355 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1356 				     u16 vid)
1357 {
1358 	struct net_device *master = dsa_slave_to_master(dev);
1359 	struct dsa_port *dp = dsa_slave_to_port(dev);
1360 	struct switchdev_obj_port_vlan vlan = {
1361 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1362 		.vid = vid,
1363 		/* This API only allows programming tagged, non-PVID VIDs */
1364 		.flags = 0,
1365 	};
1366 	struct netlink_ext_ack extack = {0};
1367 	int ret;
1368 
1369 	/* User port... */
1370 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
1371 	if (ret) {
1372 		if (extack._msg)
1373 			netdev_err(dev, "%s\n", extack._msg);
1374 		return ret;
1375 	}
1376 
1377 	/* And CPU port... */
1378 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
1379 	if (ret) {
1380 		if (extack._msg)
1381 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1382 				   extack._msg);
1383 		return ret;
1384 	}
1385 
1386 	return vlan_vid_add(master, proto, vid);
1387 }
1388 
1389 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1390 				      u16 vid)
1391 {
1392 	struct net_device *master = dsa_slave_to_master(dev);
1393 	struct dsa_port *dp = dsa_slave_to_port(dev);
1394 	struct switchdev_obj_port_vlan vlan = {
1395 		.vid = vid,
1396 		/* This API only allows programming tagged, non-PVID VIDs */
1397 		.flags = 0,
1398 	};
1399 	int err;
1400 
1401 	/* Do not deprogram the CPU port as it may be shared with other user
1402 	 * ports which can be members of this VLAN as well.
1403 	 */
1404 	err = dsa_port_vlan_del(dp, &vlan);
1405 	if (err)
1406 		return err;
1407 
1408 	vlan_vid_del(master, proto, vid);
1409 
1410 	return 0;
1411 }
1412 
1413 struct dsa_hw_port {
1414 	struct list_head list;
1415 	struct net_device *dev;
1416 	int old_mtu;
1417 };
1418 
1419 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1420 {
1421 	const struct dsa_hw_port *p;
1422 	int err;
1423 
1424 	list_for_each_entry(p, hw_port_list, list) {
1425 		if (p->dev->mtu == mtu)
1426 			continue;
1427 
1428 		err = dev_set_mtu(p->dev, mtu);
1429 		if (err)
1430 			goto rollback;
1431 	}
1432 
1433 	return 0;
1434 
1435 rollback:
1436 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1437 		if (p->dev->mtu == p->old_mtu)
1438 			continue;
1439 
1440 		if (dev_set_mtu(p->dev, p->old_mtu))
1441 			netdev_err(p->dev, "Failed to restore MTU\n");
1442 	}
1443 
1444 	return err;
1445 }
1446 
1447 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1448 {
1449 	struct dsa_hw_port *p, *n;
1450 
1451 	list_for_each_entry_safe(p, n, hw_port_list, list)
1452 		kfree(p);
1453 }
1454 
1455 /* Make the hardware datapath to/from @dev limited to a common MTU */
1456 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1457 {
1458 	struct list_head hw_port_list;
1459 	struct dsa_switch_tree *dst;
1460 	int min_mtu = ETH_MAX_MTU;
1461 	struct dsa_port *other_dp;
1462 	int err;
1463 
1464 	if (!dp->ds->mtu_enforcement_ingress)
1465 		return;
1466 
1467 	if (!dp->bridge_dev)
1468 		return;
1469 
1470 	INIT_LIST_HEAD(&hw_port_list);
1471 
1472 	/* Populate the list of ports that are part of the same bridge
1473 	 * as the newly added/modified port
1474 	 */
1475 	list_for_each_entry(dst, &dsa_tree_list, list) {
1476 		list_for_each_entry(other_dp, &dst->ports, list) {
1477 			struct dsa_hw_port *hw_port;
1478 			struct net_device *slave;
1479 
1480 			if (other_dp->type != DSA_PORT_TYPE_USER)
1481 				continue;
1482 
1483 			if (other_dp->bridge_dev != dp->bridge_dev)
1484 				continue;
1485 
1486 			if (!other_dp->ds->mtu_enforcement_ingress)
1487 				continue;
1488 
1489 			slave = other_dp->slave;
1490 
1491 			if (min_mtu > slave->mtu)
1492 				min_mtu = slave->mtu;
1493 
1494 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1495 			if (!hw_port)
1496 				goto out;
1497 
1498 			hw_port->dev = slave;
1499 			hw_port->old_mtu = slave->mtu;
1500 
1501 			list_add(&hw_port->list, &hw_port_list);
1502 		}
1503 	}
1504 
1505 	/* Attempt to configure the entire hardware bridge to the newly added
1506 	 * interface's MTU first, regardless of whether the intention of the
1507 	 * user was to raise or lower it.
1508 	 */
1509 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1510 	if (!err)
1511 		goto out;
1512 
1513 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1514 	 * all hardware bridge ports now. If this fails too, then all ports will
1515 	 * still have their old MTU rolled back anyway.
1516 	 */
1517 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1518 
1519 out:
1520 	dsa_hw_port_list_free(&hw_port_list);
1521 }
1522 
1523 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1524 {
1525 	struct net_device *master = dsa_slave_to_master(dev);
1526 	struct dsa_port *dp = dsa_slave_to_port(dev);
1527 	struct dsa_slave_priv *p = netdev_priv(dev);
1528 	struct dsa_switch *ds = p->dp->ds;
1529 	struct dsa_port *cpu_dp;
1530 	int port = p->dp->index;
1531 	int largest_mtu = 0;
1532 	int new_master_mtu;
1533 	int old_master_mtu;
1534 	int mtu_limit;
1535 	int cpu_mtu;
1536 	int err, i;
1537 
1538 	if (!ds->ops->port_change_mtu)
1539 		return -EOPNOTSUPP;
1540 
1541 	for (i = 0; i < ds->num_ports; i++) {
1542 		int slave_mtu;
1543 
1544 		if (!dsa_is_user_port(ds, i))
1545 			continue;
1546 
1547 		/* During probe, this function will be called for each slave
1548 		 * device, while not all of them have been allocated. That's
1549 		 * ok, it doesn't change what the maximum is, so ignore it.
1550 		 */
1551 		if (!dsa_to_port(ds, i)->slave)
1552 			continue;
1553 
1554 		/* Pretend that we already applied the setting, which we
1555 		 * actually haven't (still haven't done all integrity checks)
1556 		 */
1557 		if (i == port)
1558 			slave_mtu = new_mtu;
1559 		else
1560 			slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1561 
1562 		if (largest_mtu < slave_mtu)
1563 			largest_mtu = slave_mtu;
1564 	}
1565 
1566 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1567 
1568 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1569 	old_master_mtu = master->mtu;
1570 	new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1571 	if (new_master_mtu > mtu_limit)
1572 		return -ERANGE;
1573 
1574 	/* If the master MTU isn't over limit, there's no need to check the CPU
1575 	 * MTU, since that surely isn't either.
1576 	 */
1577 	cpu_mtu = largest_mtu;
1578 
1579 	/* Start applying stuff */
1580 	if (new_master_mtu != old_master_mtu) {
1581 		err = dev_set_mtu(master, new_master_mtu);
1582 		if (err < 0)
1583 			goto out_master_failed;
1584 
1585 		/* We only need to propagate the MTU of the CPU port to
1586 		 * upstream switches.
1587 		 */
1588 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1589 		if (err)
1590 			goto out_cpu_failed;
1591 	}
1592 
1593 	err = dsa_port_mtu_change(dp, new_mtu, false);
1594 	if (err)
1595 		goto out_port_failed;
1596 
1597 	dev->mtu = new_mtu;
1598 
1599 	dsa_bridge_mtu_normalization(dp);
1600 
1601 	return 0;
1602 
1603 out_port_failed:
1604 	if (new_master_mtu != old_master_mtu)
1605 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1606 				    cpu_dp->tag_ops->overhead,
1607 				    true);
1608 out_cpu_failed:
1609 	if (new_master_mtu != old_master_mtu)
1610 		dev_set_mtu(master, old_master_mtu);
1611 out_master_failed:
1612 	return err;
1613 }
1614 
1615 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1616 	.get_drvinfo		= dsa_slave_get_drvinfo,
1617 	.get_regs_len		= dsa_slave_get_regs_len,
1618 	.get_regs		= dsa_slave_get_regs,
1619 	.nway_reset		= dsa_slave_nway_reset,
1620 	.get_link		= ethtool_op_get_link,
1621 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
1622 	.get_eeprom		= dsa_slave_get_eeprom,
1623 	.set_eeprom		= dsa_slave_set_eeprom,
1624 	.get_strings		= dsa_slave_get_strings,
1625 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
1626 	.get_sset_count		= dsa_slave_get_sset_count,
1627 	.set_wol		= dsa_slave_set_wol,
1628 	.get_wol		= dsa_slave_get_wol,
1629 	.set_eee		= dsa_slave_set_eee,
1630 	.get_eee		= dsa_slave_get_eee,
1631 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1632 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1633 	.get_pauseparam		= dsa_slave_get_pauseparam,
1634 	.set_pauseparam		= dsa_slave_set_pauseparam,
1635 	.get_rxnfc		= dsa_slave_get_rxnfc,
1636 	.set_rxnfc		= dsa_slave_set_rxnfc,
1637 	.get_ts_info		= dsa_slave_get_ts_info,
1638 	.self_test		= dsa_slave_net_selftest,
1639 };
1640 
1641 /* legacy way, bypassing the bridge *****************************************/
1642 static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1643 			      struct net_device *dev,
1644 			      const unsigned char *addr, u16 vid,
1645 			      u16 flags,
1646 			      struct netlink_ext_ack *extack)
1647 {
1648 	struct dsa_port *dp = dsa_slave_to_port(dev);
1649 
1650 	return dsa_port_fdb_add(dp, addr, vid);
1651 }
1652 
1653 static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1654 			      struct net_device *dev,
1655 			      const unsigned char *addr, u16 vid)
1656 {
1657 	struct dsa_port *dp = dsa_slave_to_port(dev);
1658 
1659 	return dsa_port_fdb_del(dp, addr, vid);
1660 }
1661 
1662 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1663 {
1664 	struct dsa_port *dp = dsa_slave_to_port(dev);
1665 
1666 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1667 }
1668 
1669 static void dsa_slave_get_stats64(struct net_device *dev,
1670 				  struct rtnl_link_stats64 *s)
1671 {
1672 	struct dsa_port *dp = dsa_slave_to_port(dev);
1673 	struct dsa_switch *ds = dp->ds;
1674 
1675 	if (ds->ops->get_stats64)
1676 		ds->ops->get_stats64(ds, dp->index, s);
1677 	else
1678 		dev_get_tstats64(dev, s);
1679 }
1680 
1681 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
1682 				       struct net_device_path *path)
1683 {
1684 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
1685 	struct dsa_port *cpu_dp = dp->cpu_dp;
1686 
1687 	path->dev = ctx->dev;
1688 	path->type = DEV_PATH_DSA;
1689 	path->dsa.proto = cpu_dp->tag_ops->proto;
1690 	path->dsa.port = dp->index;
1691 	ctx->dev = cpu_dp->master;
1692 
1693 	return 0;
1694 }
1695 
1696 static const struct net_device_ops dsa_slave_netdev_ops = {
1697 	.ndo_open	 	= dsa_slave_open,
1698 	.ndo_stop		= dsa_slave_close,
1699 	.ndo_start_xmit		= dsa_slave_xmit,
1700 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1701 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1702 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
1703 	.ndo_fdb_add		= dsa_legacy_fdb_add,
1704 	.ndo_fdb_del		= dsa_legacy_fdb_del,
1705 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1706 	.ndo_do_ioctl		= dsa_slave_ioctl,
1707 	.ndo_get_iflink		= dsa_slave_get_iflink,
1708 #ifdef CONFIG_NET_POLL_CONTROLLER
1709 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
1710 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
1711 	.ndo_poll_controller	= dsa_slave_poll_controller,
1712 #endif
1713 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1714 	.ndo_setup_tc		= dsa_slave_setup_tc,
1715 	.ndo_get_stats64	= dsa_slave_get_stats64,
1716 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1717 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1718 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1719 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1720 	.ndo_change_mtu		= dsa_slave_change_mtu,
1721 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
1722 };
1723 
1724 static struct device_type dsa_type = {
1725 	.name	= "dsa",
1726 };
1727 
1728 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1729 {
1730 	const struct dsa_port *dp = dsa_to_port(ds, port);
1731 
1732 	if (dp->pl)
1733 		phylink_mac_change(dp->pl, up);
1734 }
1735 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1736 
1737 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1738 					  struct phylink_link_state *state)
1739 {
1740 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1741 	struct dsa_switch *ds = dp->ds;
1742 
1743 	/* No need to check that this operation is valid, the callback would
1744 	 * not be called if it was not.
1745 	 */
1746 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1747 }
1748 
1749 /* slave device setup *******************************************************/
1750 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1751 {
1752 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1753 	struct dsa_switch *ds = dp->ds;
1754 
1755 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1756 	if (!slave_dev->phydev) {
1757 		netdev_err(slave_dev, "no phy at %d\n", addr);
1758 		return -ENODEV;
1759 	}
1760 
1761 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1762 }
1763 
1764 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1765 {
1766 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1767 	struct device_node *port_dn = dp->dn;
1768 	struct dsa_switch *ds = dp->ds;
1769 	phy_interface_t mode;
1770 	u32 phy_flags = 0;
1771 	int ret;
1772 
1773 	ret = of_get_phy_mode(port_dn, &mode);
1774 	if (ret)
1775 		mode = PHY_INTERFACE_MODE_NA;
1776 
1777 	dp->pl_config.dev = &slave_dev->dev;
1778 	dp->pl_config.type = PHYLINK_NETDEV;
1779 
1780 	/* The get_fixed_state callback takes precedence over polling the
1781 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
1782 	 * this if the switch provides such a callback.
1783 	 */
1784 	if (ds->ops->phylink_fixed_state) {
1785 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
1786 		dp->pl_config.poll_fixed_state = true;
1787 	}
1788 
1789 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1790 				&dsa_port_phylink_mac_ops);
1791 	if (IS_ERR(dp->pl)) {
1792 		netdev_err(slave_dev,
1793 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1794 		return PTR_ERR(dp->pl);
1795 	}
1796 
1797 	if (ds->ops->get_phy_flags)
1798 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1799 
1800 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1801 	if (ret == -ENODEV && ds->slave_mii_bus) {
1802 		/* We could not connect to a designated PHY or SFP, so try to
1803 		 * use the switch internal MDIO bus instead
1804 		 */
1805 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1806 		if (ret) {
1807 			netdev_err(slave_dev,
1808 				   "failed to connect to port %d: %d\n",
1809 				   dp->index, ret);
1810 			phylink_destroy(dp->pl);
1811 			return ret;
1812 		}
1813 	}
1814 
1815 	return ret;
1816 }
1817 
1818 void dsa_slave_setup_tagger(struct net_device *slave)
1819 {
1820 	struct dsa_port *dp = dsa_slave_to_port(slave);
1821 	struct dsa_slave_priv *p = netdev_priv(slave);
1822 	const struct dsa_port *cpu_dp = dp->cpu_dp;
1823 	struct net_device *master = cpu_dp->master;
1824 
1825 	if (cpu_dp->tag_ops->tail_tag)
1826 		slave->needed_tailroom = cpu_dp->tag_ops->overhead;
1827 	else
1828 		slave->needed_headroom = cpu_dp->tag_ops->overhead;
1829 	/* Try to save one extra realloc later in the TX path (in the master)
1830 	 * by also inheriting the master's needed headroom and tailroom.
1831 	 * The 8021q driver also does this.
1832 	 */
1833 	slave->needed_headroom += master->needed_headroom;
1834 	slave->needed_tailroom += master->needed_tailroom;
1835 
1836 	p->xmit = cpu_dp->tag_ops->xmit;
1837 }
1838 
1839 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1840 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1841 					    struct netdev_queue *txq,
1842 					    void *_unused)
1843 {
1844 	lockdep_set_class(&txq->_xmit_lock,
1845 			  &dsa_slave_netdev_xmit_lock_key);
1846 }
1847 
1848 int dsa_slave_suspend(struct net_device *slave_dev)
1849 {
1850 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1851 
1852 	if (!netif_running(slave_dev))
1853 		return 0;
1854 
1855 	netif_device_detach(slave_dev);
1856 
1857 	rtnl_lock();
1858 	phylink_stop(dp->pl);
1859 	rtnl_unlock();
1860 
1861 	return 0;
1862 }
1863 
1864 int dsa_slave_resume(struct net_device *slave_dev)
1865 {
1866 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1867 
1868 	if (!netif_running(slave_dev))
1869 		return 0;
1870 
1871 	netif_device_attach(slave_dev);
1872 
1873 	rtnl_lock();
1874 	phylink_start(dp->pl);
1875 	rtnl_unlock();
1876 
1877 	return 0;
1878 }
1879 
1880 int dsa_slave_create(struct dsa_port *port)
1881 {
1882 	const struct dsa_port *cpu_dp = port->cpu_dp;
1883 	struct net_device *master = cpu_dp->master;
1884 	struct dsa_switch *ds = port->ds;
1885 	const char *name = port->name;
1886 	struct net_device *slave_dev;
1887 	struct dsa_slave_priv *p;
1888 	int ret;
1889 
1890 	if (!ds->num_tx_queues)
1891 		ds->num_tx_queues = 1;
1892 
1893 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1894 				     NET_NAME_UNKNOWN, ether_setup,
1895 				     ds->num_tx_queues, 1);
1896 	if (slave_dev == NULL)
1897 		return -ENOMEM;
1898 
1899 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1900 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
1901 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1902 	slave_dev->hw_features |= NETIF_F_HW_TC;
1903 	slave_dev->features |= NETIF_F_LLTX;
1904 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1905 	if (!is_zero_ether_addr(port->mac))
1906 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1907 	else
1908 		eth_hw_addr_inherit(slave_dev, master);
1909 	slave_dev->priv_flags |= IFF_NO_QUEUE;
1910 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1911 	if (ds->ops->port_max_mtu)
1912 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1913 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1914 
1915 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1916 				 NULL);
1917 
1918 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
1919 	slave_dev->dev.of_node = port->dn;
1920 	slave_dev->vlan_features = master->vlan_features;
1921 
1922 	p = netdev_priv(slave_dev);
1923 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1924 	if (!slave_dev->tstats) {
1925 		free_netdev(slave_dev);
1926 		return -ENOMEM;
1927 	}
1928 
1929 	ret = gro_cells_init(&p->gcells, slave_dev);
1930 	if (ret)
1931 		goto out_free;
1932 
1933 	p->dp = port;
1934 	INIT_LIST_HEAD(&p->mall_tc_list);
1935 	port->slave = slave_dev;
1936 	dsa_slave_setup_tagger(slave_dev);
1937 
1938 	rtnl_lock();
1939 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1940 	rtnl_unlock();
1941 	if (ret && ret != -EOPNOTSUPP)
1942 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1943 			 ret, ETH_DATA_LEN, port->index);
1944 
1945 	netif_carrier_off(slave_dev);
1946 
1947 	ret = dsa_slave_phy_setup(slave_dev);
1948 	if (ret) {
1949 		netdev_err(slave_dev,
1950 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
1951 			   ret, ds->dst->index, ds->index, port->index);
1952 		goto out_gcells;
1953 	}
1954 
1955 	rtnl_lock();
1956 
1957 	ret = register_netdevice(slave_dev);
1958 	if (ret) {
1959 		netdev_err(master, "error %d registering interface %s\n",
1960 			   ret, slave_dev->name);
1961 		rtnl_unlock();
1962 		goto out_phy;
1963 	}
1964 
1965 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
1966 
1967 	rtnl_unlock();
1968 
1969 	if (ret)
1970 		goto out_unregister;
1971 
1972 	return 0;
1973 
1974 out_unregister:
1975 	unregister_netdev(slave_dev);
1976 out_phy:
1977 	rtnl_lock();
1978 	phylink_disconnect_phy(p->dp->pl);
1979 	rtnl_unlock();
1980 	phylink_destroy(p->dp->pl);
1981 out_gcells:
1982 	gro_cells_destroy(&p->gcells);
1983 out_free:
1984 	free_percpu(slave_dev->tstats);
1985 	free_netdev(slave_dev);
1986 	port->slave = NULL;
1987 	return ret;
1988 }
1989 
1990 void dsa_slave_destroy(struct net_device *slave_dev)
1991 {
1992 	struct net_device *master = dsa_slave_to_master(slave_dev);
1993 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1994 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1995 
1996 	netif_carrier_off(slave_dev);
1997 	rtnl_lock();
1998 	netdev_upper_dev_unlink(master, slave_dev);
1999 	unregister_netdevice(slave_dev);
2000 	phylink_disconnect_phy(dp->pl);
2001 	rtnl_unlock();
2002 
2003 	phylink_destroy(dp->pl);
2004 	gro_cells_destroy(&p->gcells);
2005 	free_percpu(slave_dev->tstats);
2006 	free_netdev(slave_dev);
2007 }
2008 
2009 bool dsa_slave_dev_check(const struct net_device *dev)
2010 {
2011 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2012 }
2013 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2014 
2015 static int dsa_slave_changeupper(struct net_device *dev,
2016 				 struct netdev_notifier_changeupper_info *info)
2017 {
2018 	struct dsa_port *dp = dsa_slave_to_port(dev);
2019 	struct netlink_ext_ack *extack;
2020 	int err = NOTIFY_DONE;
2021 
2022 	extack = netdev_notifier_info_to_extack(&info->info);
2023 
2024 	if (netif_is_bridge_master(info->upper_dev)) {
2025 		if (info->linking) {
2026 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2027 			if (!err)
2028 				dsa_bridge_mtu_normalization(dp);
2029 			err = notifier_from_errno(err);
2030 		} else {
2031 			dsa_port_bridge_leave(dp, info->upper_dev);
2032 			err = NOTIFY_OK;
2033 		}
2034 	} else if (netif_is_lag_master(info->upper_dev)) {
2035 		if (info->linking) {
2036 			err = dsa_port_lag_join(dp, info->upper_dev,
2037 						info->upper_info, extack);
2038 			if (err == -EOPNOTSUPP) {
2039 				NL_SET_ERR_MSG_MOD(info->info.extack,
2040 						   "Offloading not supported");
2041 				err = 0;
2042 			}
2043 			err = notifier_from_errno(err);
2044 		} else {
2045 			dsa_port_lag_leave(dp, info->upper_dev);
2046 			err = NOTIFY_OK;
2047 		}
2048 	} else if (is_hsr_master(info->upper_dev)) {
2049 		if (info->linking) {
2050 			err = dsa_port_hsr_join(dp, info->upper_dev);
2051 			if (err == -EOPNOTSUPP) {
2052 				NL_SET_ERR_MSG_MOD(info->info.extack,
2053 						   "Offloading not supported");
2054 				err = 0;
2055 			}
2056 			err = notifier_from_errno(err);
2057 		} else {
2058 			dsa_port_hsr_leave(dp, info->upper_dev);
2059 			err = NOTIFY_OK;
2060 		}
2061 	}
2062 
2063 	return err;
2064 }
2065 
2066 static int
2067 dsa_slave_lag_changeupper(struct net_device *dev,
2068 			  struct netdev_notifier_changeupper_info *info)
2069 {
2070 	struct net_device *lower;
2071 	struct list_head *iter;
2072 	int err = NOTIFY_DONE;
2073 	struct dsa_port *dp;
2074 
2075 	netdev_for_each_lower_dev(dev, lower, iter) {
2076 		if (!dsa_slave_dev_check(lower))
2077 			continue;
2078 
2079 		dp = dsa_slave_to_port(lower);
2080 		if (!dp->lag_dev)
2081 			/* Software LAG */
2082 			continue;
2083 
2084 		err = dsa_slave_changeupper(lower, info);
2085 		if (notifier_to_errno(err))
2086 			break;
2087 	}
2088 
2089 	return err;
2090 }
2091 
2092 static int
2093 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2094 				 struct netdev_notifier_changeupper_info *info)
2095 {
2096 	struct netlink_ext_ack *ext_ack;
2097 	struct net_device *slave;
2098 	struct dsa_port *dp;
2099 
2100 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2101 
2102 	if (!is_vlan_dev(dev))
2103 		return NOTIFY_DONE;
2104 
2105 	slave = vlan_dev_real_dev(dev);
2106 	if (!dsa_slave_dev_check(slave))
2107 		return NOTIFY_DONE;
2108 
2109 	dp = dsa_slave_to_port(slave);
2110 	if (!dp->bridge_dev)
2111 		return NOTIFY_DONE;
2112 
2113 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2114 	if (br_vlan_enabled(dp->bridge_dev) &&
2115 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2116 		NL_SET_ERR_MSG_MOD(ext_ack,
2117 				   "Cannot enslave VLAN device into VLAN aware bridge");
2118 		return notifier_from_errno(-EINVAL);
2119 	}
2120 
2121 	return NOTIFY_DONE;
2122 }
2123 
2124 static int
2125 dsa_slave_check_8021q_upper(struct net_device *dev,
2126 			    struct netdev_notifier_changeupper_info *info)
2127 {
2128 	struct dsa_port *dp = dsa_slave_to_port(dev);
2129 	struct net_device *br = dp->bridge_dev;
2130 	struct bridge_vlan_info br_info;
2131 	struct netlink_ext_ack *extack;
2132 	int err = NOTIFY_DONE;
2133 	u16 vid;
2134 
2135 	if (!br || !br_vlan_enabled(br))
2136 		return NOTIFY_DONE;
2137 
2138 	extack = netdev_notifier_info_to_extack(&info->info);
2139 	vid = vlan_dev_vlan_id(info->upper_dev);
2140 
2141 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2142 	 * device, respectively the VID is not found, returning
2143 	 * 0 means success, which is a failure for us here.
2144 	 */
2145 	err = br_vlan_get_info(br, vid, &br_info);
2146 	if (err == 0) {
2147 		NL_SET_ERR_MSG_MOD(extack,
2148 				   "This VLAN is already configured by the bridge");
2149 		return notifier_from_errno(-EBUSY);
2150 	}
2151 
2152 	return NOTIFY_DONE;
2153 }
2154 
2155 static int dsa_slave_netdevice_event(struct notifier_block *nb,
2156 				     unsigned long event, void *ptr)
2157 {
2158 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2159 
2160 	switch (event) {
2161 	case NETDEV_PRECHANGEUPPER: {
2162 		struct netdev_notifier_changeupper_info *info = ptr;
2163 		struct dsa_switch *ds;
2164 		struct dsa_port *dp;
2165 		int err;
2166 
2167 		if (!dsa_slave_dev_check(dev))
2168 			return dsa_prevent_bridging_8021q_upper(dev, ptr);
2169 
2170 		dp = dsa_slave_to_port(dev);
2171 		ds = dp->ds;
2172 
2173 		if (ds->ops->port_prechangeupper) {
2174 			err = ds->ops->port_prechangeupper(ds, dp->index, info);
2175 			if (err)
2176 				return notifier_from_errno(err);
2177 		}
2178 
2179 		if (is_vlan_dev(info->upper_dev))
2180 			return dsa_slave_check_8021q_upper(dev, ptr);
2181 		break;
2182 	}
2183 	case NETDEV_CHANGEUPPER:
2184 		if (dsa_slave_dev_check(dev))
2185 			return dsa_slave_changeupper(dev, ptr);
2186 
2187 		if (netif_is_lag_master(dev))
2188 			return dsa_slave_lag_changeupper(dev, ptr);
2189 
2190 		break;
2191 	case NETDEV_CHANGELOWERSTATE: {
2192 		struct netdev_notifier_changelowerstate_info *info = ptr;
2193 		struct dsa_port *dp;
2194 		int err;
2195 
2196 		if (!dsa_slave_dev_check(dev))
2197 			break;
2198 
2199 		dp = dsa_slave_to_port(dev);
2200 
2201 		err = dsa_port_lag_change(dp, info->lower_state_info);
2202 		return notifier_from_errno(err);
2203 	}
2204 	case NETDEV_GOING_DOWN: {
2205 		struct dsa_port *dp, *cpu_dp;
2206 		struct dsa_switch_tree *dst;
2207 		LIST_HEAD(close_list);
2208 
2209 		if (!netdev_uses_dsa(dev))
2210 			return NOTIFY_DONE;
2211 
2212 		cpu_dp = dev->dsa_ptr;
2213 		dst = cpu_dp->ds->dst;
2214 
2215 		list_for_each_entry(dp, &dst->ports, list) {
2216 			if (!dsa_is_user_port(dp->ds, dp->index))
2217 				continue;
2218 
2219 			list_add(&dp->slave->close_list, &close_list);
2220 		}
2221 
2222 		dev_close_many(&close_list, true);
2223 
2224 		return NOTIFY_OK;
2225 	}
2226 	default:
2227 		break;
2228 	}
2229 
2230 	return NOTIFY_DONE;
2231 }
2232 
2233 static void
2234 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2235 {
2236 	struct dsa_switch *ds = switchdev_work->ds;
2237 	struct switchdev_notifier_fdb_info info;
2238 	struct dsa_port *dp;
2239 
2240 	if (!dsa_is_user_port(ds, switchdev_work->port))
2241 		return;
2242 
2243 	info.addr = switchdev_work->addr;
2244 	info.vid = switchdev_work->vid;
2245 	info.offloaded = true;
2246 	dp = dsa_to_port(ds, switchdev_work->port);
2247 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2248 				 dp->slave, &info.info, NULL);
2249 }
2250 
2251 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2252 {
2253 	struct dsa_switchdev_event_work *switchdev_work =
2254 		container_of(work, struct dsa_switchdev_event_work, work);
2255 	struct dsa_switch *ds = switchdev_work->ds;
2256 	struct dsa_port *dp;
2257 	int err;
2258 
2259 	dp = dsa_to_port(ds, switchdev_work->port);
2260 
2261 	rtnl_lock();
2262 	switch (switchdev_work->event) {
2263 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2264 		err = dsa_port_fdb_add(dp, switchdev_work->addr,
2265 				       switchdev_work->vid);
2266 		if (err) {
2267 			dev_err(ds->dev,
2268 				"port %d failed to add %pM vid %d to fdb: %d\n",
2269 				dp->index, switchdev_work->addr,
2270 				switchdev_work->vid, err);
2271 			break;
2272 		}
2273 		dsa_fdb_offload_notify(switchdev_work);
2274 		break;
2275 
2276 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2277 		err = dsa_port_fdb_del(dp, switchdev_work->addr,
2278 				       switchdev_work->vid);
2279 		if (err) {
2280 			dev_err(ds->dev,
2281 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2282 				dp->index, switchdev_work->addr,
2283 				switchdev_work->vid, err);
2284 		}
2285 
2286 		break;
2287 	}
2288 	rtnl_unlock();
2289 
2290 	kfree(switchdev_work);
2291 	if (dsa_is_user_port(ds, dp->index))
2292 		dev_put(dp->slave);
2293 }
2294 
2295 static int dsa_lower_dev_walk(struct net_device *lower_dev,
2296 			      struct netdev_nested_priv *priv)
2297 {
2298 	if (dsa_slave_dev_check(lower_dev)) {
2299 		priv->data = (void *)netdev_priv(lower_dev);
2300 		return 1;
2301 	}
2302 
2303 	return 0;
2304 }
2305 
2306 static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev)
2307 {
2308 	struct netdev_nested_priv priv = {
2309 		.data = NULL,
2310 	};
2311 
2312 	netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv);
2313 
2314 	return (struct dsa_slave_priv *)priv.data;
2315 }
2316 
2317 /* Called under rcu_read_lock() */
2318 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2319 				     unsigned long event, void *ptr)
2320 {
2321 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2322 	const struct switchdev_notifier_fdb_info *fdb_info;
2323 	struct dsa_switchdev_event_work *switchdev_work;
2324 	struct dsa_port *dp;
2325 	int err;
2326 
2327 	switch (event) {
2328 	case SWITCHDEV_PORT_ATTR_SET:
2329 		err = switchdev_handle_port_attr_set(dev, ptr,
2330 						     dsa_slave_dev_check,
2331 						     dsa_slave_port_attr_set);
2332 		return notifier_from_errno(err);
2333 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2334 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2335 		fdb_info = ptr;
2336 
2337 		if (dsa_slave_dev_check(dev)) {
2338 			if (!fdb_info->added_by_user || fdb_info->is_local)
2339 				return NOTIFY_OK;
2340 
2341 			dp = dsa_slave_to_port(dev);
2342 		} else {
2343 			/* Snoop addresses learnt on foreign interfaces
2344 			 * bridged with us, for switches that don't
2345 			 * automatically learn SA from CPU-injected traffic
2346 			 */
2347 			struct net_device *br_dev;
2348 			struct dsa_slave_priv *p;
2349 
2350 			br_dev = netdev_master_upper_dev_get_rcu(dev);
2351 			if (!br_dev)
2352 				return NOTIFY_DONE;
2353 
2354 			if (!netif_is_bridge_master(br_dev))
2355 				return NOTIFY_DONE;
2356 
2357 			p = dsa_slave_dev_lower_find(br_dev);
2358 			if (!p)
2359 				return NOTIFY_DONE;
2360 
2361 			dp = p->dp->cpu_dp;
2362 
2363 			if (!dp->ds->assisted_learning_on_cpu_port)
2364 				return NOTIFY_DONE;
2365 
2366 			/* When the bridge learns an address on an offloaded
2367 			 * LAG we don't want to send traffic to the CPU, the
2368 			 * other ports bridged with the LAG should be able to
2369 			 * autonomously forward towards it.
2370 			 */
2371 			if (dsa_tree_offloads_bridge_port(dp->ds->dst, dev))
2372 				return NOTIFY_DONE;
2373 		}
2374 
2375 		if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del)
2376 			return NOTIFY_DONE;
2377 
2378 		switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2379 		if (!switchdev_work)
2380 			return NOTIFY_BAD;
2381 
2382 		INIT_WORK(&switchdev_work->work,
2383 			  dsa_slave_switchdev_event_work);
2384 		switchdev_work->ds = dp->ds;
2385 		switchdev_work->port = dp->index;
2386 		switchdev_work->event = event;
2387 
2388 		ether_addr_copy(switchdev_work->addr,
2389 				fdb_info->addr);
2390 		switchdev_work->vid = fdb_info->vid;
2391 
2392 		/* Hold a reference on the slave for dsa_fdb_offload_notify */
2393 		if (dsa_is_user_port(dp->ds, dp->index))
2394 			dev_hold(dev);
2395 		dsa_schedule_work(&switchdev_work->work);
2396 		break;
2397 	default:
2398 		return NOTIFY_DONE;
2399 	}
2400 
2401 	return NOTIFY_OK;
2402 }
2403 
2404 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2405 					      unsigned long event, void *ptr)
2406 {
2407 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2408 	int err;
2409 
2410 	switch (event) {
2411 	case SWITCHDEV_PORT_OBJ_ADD:
2412 		err = switchdev_handle_port_obj_add(dev, ptr,
2413 						    dsa_slave_dev_check,
2414 						    dsa_slave_port_obj_add);
2415 		return notifier_from_errno(err);
2416 	case SWITCHDEV_PORT_OBJ_DEL:
2417 		err = switchdev_handle_port_obj_del(dev, ptr,
2418 						    dsa_slave_dev_check,
2419 						    dsa_slave_port_obj_del);
2420 		return notifier_from_errno(err);
2421 	case SWITCHDEV_PORT_ATTR_SET:
2422 		err = switchdev_handle_port_attr_set(dev, ptr,
2423 						     dsa_slave_dev_check,
2424 						     dsa_slave_port_attr_set);
2425 		return notifier_from_errno(err);
2426 	}
2427 
2428 	return NOTIFY_DONE;
2429 }
2430 
2431 static struct notifier_block dsa_slave_nb __read_mostly = {
2432 	.notifier_call  = dsa_slave_netdevice_event,
2433 };
2434 
2435 struct notifier_block dsa_slave_switchdev_notifier = {
2436 	.notifier_call = dsa_slave_switchdev_event,
2437 };
2438 
2439 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
2440 	.notifier_call = dsa_slave_switchdev_blocking_event,
2441 };
2442 
2443 int dsa_slave_register_notifier(void)
2444 {
2445 	struct notifier_block *nb;
2446 	int err;
2447 
2448 	err = register_netdevice_notifier(&dsa_slave_nb);
2449 	if (err)
2450 		return err;
2451 
2452 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2453 	if (err)
2454 		goto err_switchdev_nb;
2455 
2456 	nb = &dsa_slave_switchdev_blocking_notifier;
2457 	err = register_switchdev_blocking_notifier(nb);
2458 	if (err)
2459 		goto err_switchdev_blocking_nb;
2460 
2461 	return 0;
2462 
2463 err_switchdev_blocking_nb:
2464 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2465 err_switchdev_nb:
2466 	unregister_netdevice_notifier(&dsa_slave_nb);
2467 	return err;
2468 }
2469 
2470 void dsa_slave_unregister_notifier(void)
2471 {
2472 	struct notifier_block *nb;
2473 	int err;
2474 
2475 	nb = &dsa_slave_switchdev_blocking_notifier;
2476 	err = unregister_switchdev_blocking_notifier(nb);
2477 	if (err)
2478 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
2479 
2480 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2481 	if (err)
2482 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2483 
2484 	err = unregister_netdevice_notifier(&dsa_slave_nb);
2485 	if (err)
2486 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
2487 }
2488