xref: /openbmc/linux/net/dsa/slave.c (revision 3db55767)
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 <net/dcbnl.h>
23 #include <linux/netpoll.h>
24 
25 #include "dsa.h"
26 #include "port.h"
27 #include "master.h"
28 #include "netlink.h"
29 #include "slave.h"
30 #include "tag.h"
31 
32 struct dsa_switchdev_event_work {
33 	struct net_device *dev;
34 	struct net_device *orig_dev;
35 	struct work_struct work;
36 	unsigned long event;
37 	/* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
38 	 * SWITCHDEV_FDB_DEL_TO_DEVICE
39 	 */
40 	unsigned char addr[ETH_ALEN];
41 	u16 vid;
42 	bool host_addr;
43 };
44 
45 enum dsa_standalone_event {
46 	DSA_UC_ADD,
47 	DSA_UC_DEL,
48 	DSA_MC_ADD,
49 	DSA_MC_DEL,
50 };
51 
52 struct dsa_standalone_event_work {
53 	struct work_struct work;
54 	struct net_device *dev;
55 	enum dsa_standalone_event event;
56 	unsigned char addr[ETH_ALEN];
57 	u16 vid;
58 };
59 
60 struct dsa_host_vlan_rx_filtering_ctx {
61 	struct net_device *dev;
62 	const unsigned char *addr;
63 	enum dsa_standalone_event event;
64 };
65 
66 static bool dsa_switch_supports_uc_filtering(struct dsa_switch *ds)
67 {
68 	return ds->ops->port_fdb_add && ds->ops->port_fdb_del &&
69 	       ds->fdb_isolation && !ds->vlan_filtering_is_global &&
70 	       !ds->needs_standalone_vlan_filtering;
71 }
72 
73 static bool dsa_switch_supports_mc_filtering(struct dsa_switch *ds)
74 {
75 	return ds->ops->port_mdb_add && ds->ops->port_mdb_del &&
76 	       ds->fdb_isolation && !ds->vlan_filtering_is_global &&
77 	       !ds->needs_standalone_vlan_filtering;
78 }
79 
80 static void dsa_slave_standalone_event_work(struct work_struct *work)
81 {
82 	struct dsa_standalone_event_work *standalone_work =
83 		container_of(work, struct dsa_standalone_event_work, work);
84 	const unsigned char *addr = standalone_work->addr;
85 	struct net_device *dev = standalone_work->dev;
86 	struct dsa_port *dp = dsa_slave_to_port(dev);
87 	struct switchdev_obj_port_mdb mdb;
88 	struct dsa_switch *ds = dp->ds;
89 	u16 vid = standalone_work->vid;
90 	int err;
91 
92 	switch (standalone_work->event) {
93 	case DSA_UC_ADD:
94 		err = dsa_port_standalone_host_fdb_add(dp, addr, vid);
95 		if (err) {
96 			dev_err(ds->dev,
97 				"port %d failed to add %pM vid %d to fdb: %d\n",
98 				dp->index, addr, vid, err);
99 			break;
100 		}
101 		break;
102 
103 	case DSA_UC_DEL:
104 		err = dsa_port_standalone_host_fdb_del(dp, addr, vid);
105 		if (err) {
106 			dev_err(ds->dev,
107 				"port %d failed to delete %pM vid %d from fdb: %d\n",
108 				dp->index, addr, vid, err);
109 		}
110 
111 		break;
112 	case DSA_MC_ADD:
113 		ether_addr_copy(mdb.addr, addr);
114 		mdb.vid = vid;
115 
116 		err = dsa_port_standalone_host_mdb_add(dp, &mdb);
117 		if (err) {
118 			dev_err(ds->dev,
119 				"port %d failed to add %pM vid %d to mdb: %d\n",
120 				dp->index, addr, vid, err);
121 			break;
122 		}
123 		break;
124 	case DSA_MC_DEL:
125 		ether_addr_copy(mdb.addr, addr);
126 		mdb.vid = vid;
127 
128 		err = dsa_port_standalone_host_mdb_del(dp, &mdb);
129 		if (err) {
130 			dev_err(ds->dev,
131 				"port %d failed to delete %pM vid %d from mdb: %d\n",
132 				dp->index, addr, vid, err);
133 		}
134 
135 		break;
136 	}
137 
138 	kfree(standalone_work);
139 }
140 
141 static int dsa_slave_schedule_standalone_work(struct net_device *dev,
142 					      enum dsa_standalone_event event,
143 					      const unsigned char *addr,
144 					      u16 vid)
145 {
146 	struct dsa_standalone_event_work *standalone_work;
147 
148 	standalone_work = kzalloc(sizeof(*standalone_work), GFP_ATOMIC);
149 	if (!standalone_work)
150 		return -ENOMEM;
151 
152 	INIT_WORK(&standalone_work->work, dsa_slave_standalone_event_work);
153 	standalone_work->event = event;
154 	standalone_work->dev = dev;
155 
156 	ether_addr_copy(standalone_work->addr, addr);
157 	standalone_work->vid = vid;
158 
159 	dsa_schedule_work(&standalone_work->work);
160 
161 	return 0;
162 }
163 
164 static int dsa_slave_host_vlan_rx_filtering(struct net_device *vdev, int vid,
165 					    void *arg)
166 {
167 	struct dsa_host_vlan_rx_filtering_ctx *ctx = arg;
168 
169 	return dsa_slave_schedule_standalone_work(ctx->dev, ctx->event,
170 						  ctx->addr, vid);
171 }
172 
173 static int dsa_slave_sync_uc(struct net_device *dev,
174 			     const unsigned char *addr)
175 {
176 	struct net_device *master = dsa_slave_to_master(dev);
177 	struct dsa_port *dp = dsa_slave_to_port(dev);
178 	struct dsa_host_vlan_rx_filtering_ctx ctx = {
179 		.dev = dev,
180 		.addr = addr,
181 		.event = DSA_UC_ADD,
182 	};
183 	int err;
184 
185 	dev_uc_add(master, addr);
186 
187 	if (!dsa_switch_supports_uc_filtering(dp->ds))
188 		return 0;
189 
190 	err = dsa_slave_schedule_standalone_work(dev, DSA_UC_ADD, addr, 0);
191 	if (err)
192 		return err;
193 
194 	return vlan_for_each(dev, dsa_slave_host_vlan_rx_filtering, &ctx);
195 }
196 
197 static int dsa_slave_unsync_uc(struct net_device *dev,
198 			       const unsigned char *addr)
199 {
200 	struct net_device *master = dsa_slave_to_master(dev);
201 	struct dsa_port *dp = dsa_slave_to_port(dev);
202 	struct dsa_host_vlan_rx_filtering_ctx ctx = {
203 		.dev = dev,
204 		.addr = addr,
205 		.event = DSA_UC_DEL,
206 	};
207 	int err;
208 
209 	dev_uc_del(master, addr);
210 
211 	if (!dsa_switch_supports_uc_filtering(dp->ds))
212 		return 0;
213 
214 	err = dsa_slave_schedule_standalone_work(dev, DSA_UC_DEL, addr, 0);
215 	if (err)
216 		return err;
217 
218 	return vlan_for_each(dev, dsa_slave_host_vlan_rx_filtering, &ctx);
219 }
220 
221 static int dsa_slave_sync_mc(struct net_device *dev,
222 			     const unsigned char *addr)
223 {
224 	struct net_device *master = dsa_slave_to_master(dev);
225 	struct dsa_port *dp = dsa_slave_to_port(dev);
226 	struct dsa_host_vlan_rx_filtering_ctx ctx = {
227 		.dev = dev,
228 		.addr = addr,
229 		.event = DSA_MC_ADD,
230 	};
231 	int err;
232 
233 	dev_mc_add(master, addr);
234 
235 	if (!dsa_switch_supports_mc_filtering(dp->ds))
236 		return 0;
237 
238 	err = dsa_slave_schedule_standalone_work(dev, DSA_MC_ADD, addr, 0);
239 	if (err)
240 		return err;
241 
242 	return vlan_for_each(dev, dsa_slave_host_vlan_rx_filtering, &ctx);
243 }
244 
245 static int dsa_slave_unsync_mc(struct net_device *dev,
246 			       const unsigned char *addr)
247 {
248 	struct net_device *master = dsa_slave_to_master(dev);
249 	struct dsa_port *dp = dsa_slave_to_port(dev);
250 	struct dsa_host_vlan_rx_filtering_ctx ctx = {
251 		.dev = dev,
252 		.addr = addr,
253 		.event = DSA_MC_DEL,
254 	};
255 	int err;
256 
257 	dev_mc_del(master, addr);
258 
259 	if (!dsa_switch_supports_mc_filtering(dp->ds))
260 		return 0;
261 
262 	err = dsa_slave_schedule_standalone_work(dev, DSA_MC_DEL, addr, 0);
263 	if (err)
264 		return err;
265 
266 	return vlan_for_each(dev, dsa_slave_host_vlan_rx_filtering, &ctx);
267 }
268 
269 void dsa_slave_sync_ha(struct net_device *dev)
270 {
271 	struct dsa_port *dp = dsa_slave_to_port(dev);
272 	struct dsa_switch *ds = dp->ds;
273 	struct netdev_hw_addr *ha;
274 
275 	netif_addr_lock_bh(dev);
276 
277 	netdev_for_each_synced_mc_addr(ha, dev)
278 		dsa_slave_sync_mc(dev, ha->addr);
279 
280 	netdev_for_each_synced_uc_addr(ha, dev)
281 		dsa_slave_sync_uc(dev, ha->addr);
282 
283 	netif_addr_unlock_bh(dev);
284 
285 	if (dsa_switch_supports_uc_filtering(ds) ||
286 	    dsa_switch_supports_mc_filtering(ds))
287 		dsa_flush_workqueue();
288 }
289 
290 void dsa_slave_unsync_ha(struct net_device *dev)
291 {
292 	struct dsa_port *dp = dsa_slave_to_port(dev);
293 	struct dsa_switch *ds = dp->ds;
294 	struct netdev_hw_addr *ha;
295 
296 	netif_addr_lock_bh(dev);
297 
298 	netdev_for_each_synced_uc_addr(ha, dev)
299 		dsa_slave_unsync_uc(dev, ha->addr);
300 
301 	netdev_for_each_synced_mc_addr(ha, dev)
302 		dsa_slave_unsync_mc(dev, ha->addr);
303 
304 	netif_addr_unlock_bh(dev);
305 
306 	if (dsa_switch_supports_uc_filtering(ds) ||
307 	    dsa_switch_supports_mc_filtering(ds))
308 		dsa_flush_workqueue();
309 }
310 
311 /* slave mii_bus handling ***************************************************/
312 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
313 {
314 	struct dsa_switch *ds = bus->priv;
315 
316 	if (ds->phys_mii_mask & (1 << addr))
317 		return ds->ops->phy_read(ds, addr, reg);
318 
319 	return 0xffff;
320 }
321 
322 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
323 {
324 	struct dsa_switch *ds = bus->priv;
325 
326 	if (ds->phys_mii_mask & (1 << addr))
327 		return ds->ops->phy_write(ds, addr, reg, val);
328 
329 	return 0;
330 }
331 
332 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
333 {
334 	ds->slave_mii_bus->priv = (void *)ds;
335 	ds->slave_mii_bus->name = "dsa slave smi";
336 	ds->slave_mii_bus->read = dsa_slave_phy_read;
337 	ds->slave_mii_bus->write = dsa_slave_phy_write;
338 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
339 		 ds->dst->index, ds->index);
340 	ds->slave_mii_bus->parent = ds->dev;
341 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
342 }
343 
344 
345 /* slave device handling ****************************************************/
346 static int dsa_slave_get_iflink(const struct net_device *dev)
347 {
348 	return dsa_slave_to_master(dev)->ifindex;
349 }
350 
351 static int dsa_slave_open(struct net_device *dev)
352 {
353 	struct net_device *master = dsa_slave_to_master(dev);
354 	struct dsa_port *dp = dsa_slave_to_port(dev);
355 	struct dsa_switch *ds = dp->ds;
356 	int err;
357 
358 	err = dev_open(master, NULL);
359 	if (err < 0) {
360 		netdev_err(dev, "failed to open master %s\n", master->name);
361 		goto out;
362 	}
363 
364 	if (dsa_switch_supports_uc_filtering(ds)) {
365 		err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0);
366 		if (err)
367 			goto out;
368 	}
369 
370 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
371 		err = dev_uc_add(master, dev->dev_addr);
372 		if (err < 0)
373 			goto del_host_addr;
374 	}
375 
376 	err = dsa_port_enable_rt(dp, dev->phydev);
377 	if (err)
378 		goto del_unicast;
379 
380 	return 0;
381 
382 del_unicast:
383 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
384 		dev_uc_del(master, dev->dev_addr);
385 del_host_addr:
386 	if (dsa_switch_supports_uc_filtering(ds))
387 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
388 out:
389 	return err;
390 }
391 
392 static int dsa_slave_close(struct net_device *dev)
393 {
394 	struct net_device *master = dsa_slave_to_master(dev);
395 	struct dsa_port *dp = dsa_slave_to_port(dev);
396 	struct dsa_switch *ds = dp->ds;
397 
398 	dsa_port_disable_rt(dp);
399 
400 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
401 		dev_uc_del(master, dev->dev_addr);
402 
403 	if (dsa_switch_supports_uc_filtering(ds))
404 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
405 
406 	return 0;
407 }
408 
409 static void dsa_slave_manage_host_flood(struct net_device *dev)
410 {
411 	bool mc = dev->flags & (IFF_PROMISC | IFF_ALLMULTI);
412 	struct dsa_port *dp = dsa_slave_to_port(dev);
413 	bool uc = dev->flags & IFF_PROMISC;
414 
415 	dsa_port_set_host_flood(dp, uc, mc);
416 }
417 
418 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
419 {
420 	struct net_device *master = dsa_slave_to_master(dev);
421 	struct dsa_port *dp = dsa_slave_to_port(dev);
422 	struct dsa_switch *ds = dp->ds;
423 
424 	if (change & IFF_ALLMULTI)
425 		dev_set_allmulti(master,
426 				 dev->flags & IFF_ALLMULTI ? 1 : -1);
427 	if (change & IFF_PROMISC)
428 		dev_set_promiscuity(master,
429 				    dev->flags & IFF_PROMISC ? 1 : -1);
430 
431 	if (dsa_switch_supports_uc_filtering(ds) &&
432 	    dsa_switch_supports_mc_filtering(ds))
433 		dsa_slave_manage_host_flood(dev);
434 }
435 
436 static void dsa_slave_set_rx_mode(struct net_device *dev)
437 {
438 	__dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc);
439 	__dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc);
440 }
441 
442 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
443 {
444 	struct net_device *master = dsa_slave_to_master(dev);
445 	struct dsa_port *dp = dsa_slave_to_port(dev);
446 	struct dsa_switch *ds = dp->ds;
447 	struct sockaddr *addr = a;
448 	int err;
449 
450 	if (!is_valid_ether_addr(addr->sa_data))
451 		return -EADDRNOTAVAIL;
452 
453 	/* If the port is down, the address isn't synced yet to hardware or
454 	 * to the DSA master, so there is nothing to change.
455 	 */
456 	if (!(dev->flags & IFF_UP))
457 		goto out_change_dev_addr;
458 
459 	if (dsa_switch_supports_uc_filtering(ds)) {
460 		err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0);
461 		if (err)
462 			return err;
463 	}
464 
465 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
466 		err = dev_uc_add(master, addr->sa_data);
467 		if (err < 0)
468 			goto del_unicast;
469 	}
470 
471 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
472 		dev_uc_del(master, dev->dev_addr);
473 
474 	if (dsa_switch_supports_uc_filtering(ds))
475 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
476 
477 out_change_dev_addr:
478 	eth_hw_addr_set(dev, addr->sa_data);
479 
480 	return 0;
481 
482 del_unicast:
483 	if (dsa_switch_supports_uc_filtering(ds))
484 		dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0);
485 
486 	return err;
487 }
488 
489 struct dsa_slave_dump_ctx {
490 	struct net_device *dev;
491 	struct sk_buff *skb;
492 	struct netlink_callback *cb;
493 	int idx;
494 };
495 
496 static int
497 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
498 			   bool is_static, void *data)
499 {
500 	struct dsa_slave_dump_ctx *dump = data;
501 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
502 	u32 seq = dump->cb->nlh->nlmsg_seq;
503 	struct nlmsghdr *nlh;
504 	struct ndmsg *ndm;
505 
506 	if (dump->idx < dump->cb->args[2])
507 		goto skip;
508 
509 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
510 			sizeof(*ndm), NLM_F_MULTI);
511 	if (!nlh)
512 		return -EMSGSIZE;
513 
514 	ndm = nlmsg_data(nlh);
515 	ndm->ndm_family  = AF_BRIDGE;
516 	ndm->ndm_pad1    = 0;
517 	ndm->ndm_pad2    = 0;
518 	ndm->ndm_flags   = NTF_SELF;
519 	ndm->ndm_type    = 0;
520 	ndm->ndm_ifindex = dump->dev->ifindex;
521 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
522 
523 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
524 		goto nla_put_failure;
525 
526 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
527 		goto nla_put_failure;
528 
529 	nlmsg_end(dump->skb, nlh);
530 
531 skip:
532 	dump->idx++;
533 	return 0;
534 
535 nla_put_failure:
536 	nlmsg_cancel(dump->skb, nlh);
537 	return -EMSGSIZE;
538 }
539 
540 static int
541 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
542 		   struct net_device *dev, struct net_device *filter_dev,
543 		   int *idx)
544 {
545 	struct dsa_port *dp = dsa_slave_to_port(dev);
546 	struct dsa_slave_dump_ctx dump = {
547 		.dev = dev,
548 		.skb = skb,
549 		.cb = cb,
550 		.idx = *idx,
551 	};
552 	int err;
553 
554 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
555 	*idx = dump.idx;
556 
557 	return err;
558 }
559 
560 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
561 {
562 	struct dsa_slave_priv *p = netdev_priv(dev);
563 	struct dsa_switch *ds = p->dp->ds;
564 	int port = p->dp->index;
565 
566 	/* Pass through to switch driver if it supports timestamping */
567 	switch (cmd) {
568 	case SIOCGHWTSTAMP:
569 		if (ds->ops->port_hwtstamp_get)
570 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
571 		break;
572 	case SIOCSHWTSTAMP:
573 		if (ds->ops->port_hwtstamp_set)
574 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
575 		break;
576 	}
577 
578 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
579 }
580 
581 static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
582 				   const struct switchdev_attr *attr,
583 				   struct netlink_ext_ack *extack)
584 {
585 	struct dsa_port *dp = dsa_slave_to_port(dev);
586 	int ret;
587 
588 	if (ctx && ctx != dp)
589 		return 0;
590 
591 	switch (attr->id) {
592 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
593 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
594 			return -EOPNOTSUPP;
595 
596 		ret = dsa_port_set_state(dp, attr->u.stp_state, true);
597 		break;
598 	case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
599 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
600 			return -EOPNOTSUPP;
601 
602 		ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
603 		break;
604 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
605 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
606 			return -EOPNOTSUPP;
607 
608 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
609 					      extack);
610 		break;
611 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
612 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
613 			return -EOPNOTSUPP;
614 
615 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
616 		break;
617 	case SWITCHDEV_ATTR_ID_BRIDGE_MST:
618 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
619 			return -EOPNOTSUPP;
620 
621 		ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
622 		break;
623 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
624 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
625 			return -EOPNOTSUPP;
626 
627 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
628 						extack);
629 		break;
630 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
631 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
632 			return -EOPNOTSUPP;
633 
634 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
635 		break;
636 	case SWITCHDEV_ATTR_ID_VLAN_MSTI:
637 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
638 			return -EOPNOTSUPP;
639 
640 		ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
641 		break;
642 	default:
643 		ret = -EOPNOTSUPP;
644 		break;
645 	}
646 
647 	return ret;
648 }
649 
650 /* Must be called under rcu_read_lock() */
651 static int
652 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
653 				      const struct switchdev_obj_port_vlan *vlan)
654 {
655 	struct net_device *upper_dev;
656 	struct list_head *iter;
657 
658 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
659 		u16 vid;
660 
661 		if (!is_vlan_dev(upper_dev))
662 			continue;
663 
664 		vid = vlan_dev_vlan_id(upper_dev);
665 		if (vid == vlan->vid)
666 			return -EBUSY;
667 	}
668 
669 	return 0;
670 }
671 
672 static int dsa_slave_vlan_add(struct net_device *dev,
673 			      const struct switchdev_obj *obj,
674 			      struct netlink_ext_ack *extack)
675 {
676 	struct dsa_port *dp = dsa_slave_to_port(dev);
677 	struct switchdev_obj_port_vlan *vlan;
678 	int err;
679 
680 	if (dsa_port_skip_vlan_configuration(dp)) {
681 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
682 		return 0;
683 	}
684 
685 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
686 
687 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
688 	 * the same VID.
689 	 */
690 	if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
691 		rcu_read_lock();
692 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan);
693 		rcu_read_unlock();
694 		if (err) {
695 			NL_SET_ERR_MSG_MOD(extack,
696 					   "Port already has a VLAN upper with this VID");
697 			return err;
698 		}
699 	}
700 
701 	return dsa_port_vlan_add(dp, vlan, extack);
702 }
703 
704 /* Offload a VLAN installed on the bridge or on a foreign interface by
705  * installing it as a VLAN towards the CPU port.
706  */
707 static int dsa_slave_host_vlan_add(struct net_device *dev,
708 				   const struct switchdev_obj *obj,
709 				   struct netlink_ext_ack *extack)
710 {
711 	struct dsa_port *dp = dsa_slave_to_port(dev);
712 	struct switchdev_obj_port_vlan vlan;
713 
714 	/* Do nothing if this is a software bridge */
715 	if (!dp->bridge)
716 		return -EOPNOTSUPP;
717 
718 	if (dsa_port_skip_vlan_configuration(dp)) {
719 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
720 		return 0;
721 	}
722 
723 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
724 
725 	/* Even though drivers often handle CPU membership in special ways,
726 	 * it doesn't make sense to program a PVID, so clear this flag.
727 	 */
728 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
729 
730 	return dsa_port_host_vlan_add(dp, &vlan, extack);
731 }
732 
733 static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
734 				  const struct switchdev_obj *obj,
735 				  struct netlink_ext_ack *extack)
736 {
737 	struct dsa_port *dp = dsa_slave_to_port(dev);
738 	int err;
739 
740 	if (ctx && ctx != dp)
741 		return 0;
742 
743 	switch (obj->id) {
744 	case SWITCHDEV_OBJ_ID_PORT_MDB:
745 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
746 			return -EOPNOTSUPP;
747 
748 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
749 		break;
750 	case SWITCHDEV_OBJ_ID_HOST_MDB:
751 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
752 			return -EOPNOTSUPP;
753 
754 		err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
755 		break;
756 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
757 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
758 			err = dsa_slave_vlan_add(dev, obj, extack);
759 		else
760 			err = dsa_slave_host_vlan_add(dev, obj, extack);
761 		break;
762 	case SWITCHDEV_OBJ_ID_MRP:
763 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
764 			return -EOPNOTSUPP;
765 
766 		err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
767 		break;
768 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
769 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
770 			return -EOPNOTSUPP;
771 
772 		err = dsa_port_mrp_add_ring_role(dp,
773 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
774 		break;
775 	default:
776 		err = -EOPNOTSUPP;
777 		break;
778 	}
779 
780 	return err;
781 }
782 
783 static int dsa_slave_vlan_del(struct net_device *dev,
784 			      const struct switchdev_obj *obj)
785 {
786 	struct dsa_port *dp = dsa_slave_to_port(dev);
787 	struct switchdev_obj_port_vlan *vlan;
788 
789 	if (dsa_port_skip_vlan_configuration(dp))
790 		return 0;
791 
792 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
793 
794 	return dsa_port_vlan_del(dp, vlan);
795 }
796 
797 static int dsa_slave_host_vlan_del(struct net_device *dev,
798 				   const struct switchdev_obj *obj)
799 {
800 	struct dsa_port *dp = dsa_slave_to_port(dev);
801 	struct switchdev_obj_port_vlan *vlan;
802 
803 	/* Do nothing if this is a software bridge */
804 	if (!dp->bridge)
805 		return -EOPNOTSUPP;
806 
807 	if (dsa_port_skip_vlan_configuration(dp))
808 		return 0;
809 
810 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
811 
812 	return dsa_port_host_vlan_del(dp, vlan);
813 }
814 
815 static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
816 				  const struct switchdev_obj *obj)
817 {
818 	struct dsa_port *dp = dsa_slave_to_port(dev);
819 	int err;
820 
821 	if (ctx && ctx != dp)
822 		return 0;
823 
824 	switch (obj->id) {
825 	case SWITCHDEV_OBJ_ID_PORT_MDB:
826 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
827 			return -EOPNOTSUPP;
828 
829 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
830 		break;
831 	case SWITCHDEV_OBJ_ID_HOST_MDB:
832 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
833 			return -EOPNOTSUPP;
834 
835 		err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
836 		break;
837 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
838 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
839 			err = dsa_slave_vlan_del(dev, obj);
840 		else
841 			err = dsa_slave_host_vlan_del(dev, obj);
842 		break;
843 	case SWITCHDEV_OBJ_ID_MRP:
844 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
845 			return -EOPNOTSUPP;
846 
847 		err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
848 		break;
849 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
850 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
851 			return -EOPNOTSUPP;
852 
853 		err = dsa_port_mrp_del_ring_role(dp,
854 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
855 		break;
856 	default:
857 		err = -EOPNOTSUPP;
858 		break;
859 	}
860 
861 	return err;
862 }
863 
864 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
865 						     struct sk_buff *skb)
866 {
867 #ifdef CONFIG_NET_POLL_CONTROLLER
868 	struct dsa_slave_priv *p = netdev_priv(dev);
869 
870 	return netpoll_send_skb(p->netpoll, skb);
871 #else
872 	BUG();
873 	return NETDEV_TX_OK;
874 #endif
875 }
876 
877 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
878 				 struct sk_buff *skb)
879 {
880 	struct dsa_switch *ds = p->dp->ds;
881 
882 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
883 		return;
884 
885 	if (!ds->ops->port_txtstamp)
886 		return;
887 
888 	ds->ops->port_txtstamp(ds, p->dp->index, skb);
889 }
890 
891 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
892 {
893 	/* SKB for netpoll still need to be mangled with the protocol-specific
894 	 * tag to be successfully transmitted
895 	 */
896 	if (unlikely(netpoll_tx_running(dev)))
897 		return dsa_slave_netpoll_send_skb(dev, skb);
898 
899 	/* Queue the SKB for transmission on the parent interface, but
900 	 * do not modify its EtherType
901 	 */
902 	skb->dev = dsa_slave_to_master(dev);
903 	dev_queue_xmit(skb);
904 
905 	return NETDEV_TX_OK;
906 }
907 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
908 
909 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
910 {
911 	int needed_headroom = dev->needed_headroom;
912 	int needed_tailroom = dev->needed_tailroom;
913 
914 	/* For tail taggers, we need to pad short frames ourselves, to ensure
915 	 * that the tail tag does not fail at its role of being at the end of
916 	 * the packet, once the master interface pads the frame. Account for
917 	 * that pad length here, and pad later.
918 	 */
919 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
920 		needed_tailroom += ETH_ZLEN - skb->len;
921 	/* skb_headroom() returns unsigned int... */
922 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
923 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
924 
925 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
926 		/* No reallocation needed, yay! */
927 		return 0;
928 
929 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
930 				GFP_ATOMIC);
931 }
932 
933 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
934 {
935 	struct dsa_slave_priv *p = netdev_priv(dev);
936 	struct sk_buff *nskb;
937 
938 	dev_sw_netstats_tx_add(dev, 1, skb->len);
939 
940 	memset(skb->cb, 0, sizeof(skb->cb));
941 
942 	/* Handle tx timestamp if any */
943 	dsa_skb_tx_timestamp(p, skb);
944 
945 	if (dsa_realloc_skb(skb, dev)) {
946 		dev_kfree_skb_any(skb);
947 		return NETDEV_TX_OK;
948 	}
949 
950 	/* needed_tailroom should still be 'warm' in the cache line from
951 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
952 	 */
953 	if (dev->needed_tailroom)
954 		eth_skb_pad(skb);
955 
956 	/* Transmit function may have to reallocate the original SKB,
957 	 * in which case it must have freed it. Only free it here on error.
958 	 */
959 	nskb = p->xmit(skb, dev);
960 	if (!nskb) {
961 		kfree_skb(skb);
962 		return NETDEV_TX_OK;
963 	}
964 
965 	return dsa_enqueue_skb(nskb, dev);
966 }
967 
968 /* ethtool operations *******************************************************/
969 
970 static void dsa_slave_get_drvinfo(struct net_device *dev,
971 				  struct ethtool_drvinfo *drvinfo)
972 {
973 	strscpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
974 	strscpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
975 	strscpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
976 }
977 
978 static int dsa_slave_get_regs_len(struct net_device *dev)
979 {
980 	struct dsa_port *dp = dsa_slave_to_port(dev);
981 	struct dsa_switch *ds = dp->ds;
982 
983 	if (ds->ops->get_regs_len)
984 		return ds->ops->get_regs_len(ds, dp->index);
985 
986 	return -EOPNOTSUPP;
987 }
988 
989 static void
990 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
991 {
992 	struct dsa_port *dp = dsa_slave_to_port(dev);
993 	struct dsa_switch *ds = dp->ds;
994 
995 	if (ds->ops->get_regs)
996 		ds->ops->get_regs(ds, dp->index, regs, _p);
997 }
998 
999 static int dsa_slave_nway_reset(struct net_device *dev)
1000 {
1001 	struct dsa_port *dp = dsa_slave_to_port(dev);
1002 
1003 	return phylink_ethtool_nway_reset(dp->pl);
1004 }
1005 
1006 static int dsa_slave_get_eeprom_len(struct net_device *dev)
1007 {
1008 	struct dsa_port *dp = dsa_slave_to_port(dev);
1009 	struct dsa_switch *ds = dp->ds;
1010 
1011 	if (ds->cd && ds->cd->eeprom_len)
1012 		return ds->cd->eeprom_len;
1013 
1014 	if (ds->ops->get_eeprom_len)
1015 		return ds->ops->get_eeprom_len(ds);
1016 
1017 	return 0;
1018 }
1019 
1020 static int dsa_slave_get_eeprom(struct net_device *dev,
1021 				struct ethtool_eeprom *eeprom, u8 *data)
1022 {
1023 	struct dsa_port *dp = dsa_slave_to_port(dev);
1024 	struct dsa_switch *ds = dp->ds;
1025 
1026 	if (ds->ops->get_eeprom)
1027 		return ds->ops->get_eeprom(ds, eeprom, data);
1028 
1029 	return -EOPNOTSUPP;
1030 }
1031 
1032 static int dsa_slave_set_eeprom(struct net_device *dev,
1033 				struct ethtool_eeprom *eeprom, u8 *data)
1034 {
1035 	struct dsa_port *dp = dsa_slave_to_port(dev);
1036 	struct dsa_switch *ds = dp->ds;
1037 
1038 	if (ds->ops->set_eeprom)
1039 		return ds->ops->set_eeprom(ds, eeprom, data);
1040 
1041 	return -EOPNOTSUPP;
1042 }
1043 
1044 static void dsa_slave_get_strings(struct net_device *dev,
1045 				  uint32_t stringset, uint8_t *data)
1046 {
1047 	struct dsa_port *dp = dsa_slave_to_port(dev);
1048 	struct dsa_switch *ds = dp->ds;
1049 
1050 	if (stringset == ETH_SS_STATS) {
1051 		int len = ETH_GSTRING_LEN;
1052 
1053 		strncpy(data, "tx_packets", len);
1054 		strncpy(data + len, "tx_bytes", len);
1055 		strncpy(data + 2 * len, "rx_packets", len);
1056 		strncpy(data + 3 * len, "rx_bytes", len);
1057 		if (ds->ops->get_strings)
1058 			ds->ops->get_strings(ds, dp->index, stringset,
1059 					     data + 4 * len);
1060 	} else if (stringset ==  ETH_SS_TEST) {
1061 		net_selftest_get_strings(data);
1062 	}
1063 
1064 }
1065 
1066 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
1067 					struct ethtool_stats *stats,
1068 					uint64_t *data)
1069 {
1070 	struct dsa_port *dp = dsa_slave_to_port(dev);
1071 	struct dsa_switch *ds = dp->ds;
1072 	struct pcpu_sw_netstats *s;
1073 	unsigned int start;
1074 	int i;
1075 
1076 	for_each_possible_cpu(i) {
1077 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1078 
1079 		s = per_cpu_ptr(dev->tstats, i);
1080 		do {
1081 			start = u64_stats_fetch_begin(&s->syncp);
1082 			tx_packets = u64_stats_read(&s->tx_packets);
1083 			tx_bytes = u64_stats_read(&s->tx_bytes);
1084 			rx_packets = u64_stats_read(&s->rx_packets);
1085 			rx_bytes = u64_stats_read(&s->rx_bytes);
1086 		} while (u64_stats_fetch_retry(&s->syncp, start));
1087 		data[0] += tx_packets;
1088 		data[1] += tx_bytes;
1089 		data[2] += rx_packets;
1090 		data[3] += rx_bytes;
1091 	}
1092 	if (ds->ops->get_ethtool_stats)
1093 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
1094 }
1095 
1096 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
1097 {
1098 	struct dsa_port *dp = dsa_slave_to_port(dev);
1099 	struct dsa_switch *ds = dp->ds;
1100 
1101 	if (sset == ETH_SS_STATS) {
1102 		int count = 0;
1103 
1104 		if (ds->ops->get_sset_count) {
1105 			count = ds->ops->get_sset_count(ds, dp->index, sset);
1106 			if (count < 0)
1107 				return count;
1108 		}
1109 
1110 		return count + 4;
1111 	} else if (sset ==  ETH_SS_TEST) {
1112 		return net_selftest_get_count();
1113 	}
1114 
1115 	return -EOPNOTSUPP;
1116 }
1117 
1118 static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
1119 					struct ethtool_eth_phy_stats *phy_stats)
1120 {
1121 	struct dsa_port *dp = dsa_slave_to_port(dev);
1122 	struct dsa_switch *ds = dp->ds;
1123 
1124 	if (ds->ops->get_eth_phy_stats)
1125 		ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
1126 }
1127 
1128 static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
1129 					struct ethtool_eth_mac_stats *mac_stats)
1130 {
1131 	struct dsa_port *dp = dsa_slave_to_port(dev);
1132 	struct dsa_switch *ds = dp->ds;
1133 
1134 	if (ds->ops->get_eth_mac_stats)
1135 		ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
1136 }
1137 
1138 static void
1139 dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
1140 			     struct ethtool_eth_ctrl_stats *ctrl_stats)
1141 {
1142 	struct dsa_port *dp = dsa_slave_to_port(dev);
1143 	struct dsa_switch *ds = dp->ds;
1144 
1145 	if (ds->ops->get_eth_ctrl_stats)
1146 		ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1147 }
1148 
1149 static void
1150 dsa_slave_get_rmon_stats(struct net_device *dev,
1151 			 struct ethtool_rmon_stats *rmon_stats,
1152 			 const struct ethtool_rmon_hist_range **ranges)
1153 {
1154 	struct dsa_port *dp = dsa_slave_to_port(dev);
1155 	struct dsa_switch *ds = dp->ds;
1156 
1157 	if (ds->ops->get_rmon_stats)
1158 		ds->ops->get_rmon_stats(ds, dp->index, rmon_stats, ranges);
1159 }
1160 
1161 static void dsa_slave_net_selftest(struct net_device *ndev,
1162 				   struct ethtool_test *etest, u64 *buf)
1163 {
1164 	struct dsa_port *dp = dsa_slave_to_port(ndev);
1165 	struct dsa_switch *ds = dp->ds;
1166 
1167 	if (ds->ops->self_test) {
1168 		ds->ops->self_test(ds, dp->index, etest, buf);
1169 		return;
1170 	}
1171 
1172 	net_selftest(ndev, etest, buf);
1173 }
1174 
1175 static int dsa_slave_get_mm(struct net_device *dev,
1176 			    struct ethtool_mm_state *state)
1177 {
1178 	struct dsa_port *dp = dsa_slave_to_port(dev);
1179 	struct dsa_switch *ds = dp->ds;
1180 
1181 	if (!ds->ops->get_mm)
1182 		return -EOPNOTSUPP;
1183 
1184 	return ds->ops->get_mm(ds, dp->index, state);
1185 }
1186 
1187 static int dsa_slave_set_mm(struct net_device *dev, struct ethtool_mm_cfg *cfg,
1188 			    struct netlink_ext_ack *extack)
1189 {
1190 	struct dsa_port *dp = dsa_slave_to_port(dev);
1191 	struct dsa_switch *ds = dp->ds;
1192 
1193 	if (!ds->ops->set_mm)
1194 		return -EOPNOTSUPP;
1195 
1196 	return ds->ops->set_mm(ds, dp->index, cfg, extack);
1197 }
1198 
1199 static void dsa_slave_get_mm_stats(struct net_device *dev,
1200 				   struct ethtool_mm_stats *stats)
1201 {
1202 	struct dsa_port *dp = dsa_slave_to_port(dev);
1203 	struct dsa_switch *ds = dp->ds;
1204 
1205 	if (ds->ops->get_mm_stats)
1206 		ds->ops->get_mm_stats(ds, dp->index, stats);
1207 }
1208 
1209 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1210 {
1211 	struct dsa_port *dp = dsa_slave_to_port(dev);
1212 	struct dsa_switch *ds = dp->ds;
1213 
1214 	phylink_ethtool_get_wol(dp->pl, w);
1215 
1216 	if (ds->ops->get_wol)
1217 		ds->ops->get_wol(ds, dp->index, w);
1218 }
1219 
1220 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1221 {
1222 	struct dsa_port *dp = dsa_slave_to_port(dev);
1223 	struct dsa_switch *ds = dp->ds;
1224 	int ret = -EOPNOTSUPP;
1225 
1226 	phylink_ethtool_set_wol(dp->pl, w);
1227 
1228 	if (ds->ops->set_wol)
1229 		ret = ds->ops->set_wol(ds, dp->index, w);
1230 
1231 	return ret;
1232 }
1233 
1234 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
1235 {
1236 	struct dsa_port *dp = dsa_slave_to_port(dev);
1237 	struct dsa_switch *ds = dp->ds;
1238 	int ret;
1239 
1240 	/* Port's PHY and MAC both need to be EEE capable */
1241 	if (!dev->phydev || !dp->pl)
1242 		return -ENODEV;
1243 
1244 	if (!ds->ops->set_mac_eee)
1245 		return -EOPNOTSUPP;
1246 
1247 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
1248 	if (ret)
1249 		return ret;
1250 
1251 	return phylink_ethtool_set_eee(dp->pl, e);
1252 }
1253 
1254 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
1255 {
1256 	struct dsa_port *dp = dsa_slave_to_port(dev);
1257 	struct dsa_switch *ds = dp->ds;
1258 	int ret;
1259 
1260 	/* Port's PHY and MAC both need to be EEE capable */
1261 	if (!dev->phydev || !dp->pl)
1262 		return -ENODEV;
1263 
1264 	if (!ds->ops->get_mac_eee)
1265 		return -EOPNOTSUPP;
1266 
1267 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
1268 	if (ret)
1269 		return ret;
1270 
1271 	return phylink_ethtool_get_eee(dp->pl, e);
1272 }
1273 
1274 static int dsa_slave_get_link_ksettings(struct net_device *dev,
1275 					struct ethtool_link_ksettings *cmd)
1276 {
1277 	struct dsa_port *dp = dsa_slave_to_port(dev);
1278 
1279 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
1280 }
1281 
1282 static int dsa_slave_set_link_ksettings(struct net_device *dev,
1283 					const struct ethtool_link_ksettings *cmd)
1284 {
1285 	struct dsa_port *dp = dsa_slave_to_port(dev);
1286 
1287 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
1288 }
1289 
1290 static void dsa_slave_get_pause_stats(struct net_device *dev,
1291 				  struct ethtool_pause_stats *pause_stats)
1292 {
1293 	struct dsa_port *dp = dsa_slave_to_port(dev);
1294 	struct dsa_switch *ds = dp->ds;
1295 
1296 	if (ds->ops->get_pause_stats)
1297 		ds->ops->get_pause_stats(ds, dp->index, pause_stats);
1298 }
1299 
1300 static void dsa_slave_get_pauseparam(struct net_device *dev,
1301 				     struct ethtool_pauseparam *pause)
1302 {
1303 	struct dsa_port *dp = dsa_slave_to_port(dev);
1304 
1305 	phylink_ethtool_get_pauseparam(dp->pl, pause);
1306 }
1307 
1308 static int dsa_slave_set_pauseparam(struct net_device *dev,
1309 				    struct ethtool_pauseparam *pause)
1310 {
1311 	struct dsa_port *dp = dsa_slave_to_port(dev);
1312 
1313 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
1314 }
1315 
1316 #ifdef CONFIG_NET_POLL_CONTROLLER
1317 static int dsa_slave_netpoll_setup(struct net_device *dev,
1318 				   struct netpoll_info *ni)
1319 {
1320 	struct net_device *master = dsa_slave_to_master(dev);
1321 	struct dsa_slave_priv *p = netdev_priv(dev);
1322 	struct netpoll *netpoll;
1323 	int err = 0;
1324 
1325 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1326 	if (!netpoll)
1327 		return -ENOMEM;
1328 
1329 	err = __netpoll_setup(netpoll, master);
1330 	if (err) {
1331 		kfree(netpoll);
1332 		goto out;
1333 	}
1334 
1335 	p->netpoll = netpoll;
1336 out:
1337 	return err;
1338 }
1339 
1340 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
1341 {
1342 	struct dsa_slave_priv *p = netdev_priv(dev);
1343 	struct netpoll *netpoll = p->netpoll;
1344 
1345 	if (!netpoll)
1346 		return;
1347 
1348 	p->netpoll = NULL;
1349 
1350 	__netpoll_free(netpoll);
1351 }
1352 
1353 static void dsa_slave_poll_controller(struct net_device *dev)
1354 {
1355 }
1356 #endif
1357 
1358 static struct dsa_mall_tc_entry *
1359 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1360 {
1361 	struct dsa_slave_priv *p = netdev_priv(dev);
1362 	struct dsa_mall_tc_entry *mall_tc_entry;
1363 
1364 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1365 		if (mall_tc_entry->cookie == cookie)
1366 			return mall_tc_entry;
1367 
1368 	return NULL;
1369 }
1370 
1371 static int
1372 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1373 				  struct tc_cls_matchall_offload *cls,
1374 				  bool ingress)
1375 {
1376 	struct netlink_ext_ack *extack = cls->common.extack;
1377 	struct dsa_port *dp = dsa_slave_to_port(dev);
1378 	struct dsa_slave_priv *p = netdev_priv(dev);
1379 	struct dsa_mall_mirror_tc_entry *mirror;
1380 	struct dsa_mall_tc_entry *mall_tc_entry;
1381 	struct dsa_switch *ds = dp->ds;
1382 	struct flow_action_entry *act;
1383 	struct dsa_port *to_dp;
1384 	int err;
1385 
1386 	if (!ds->ops->port_mirror_add)
1387 		return -EOPNOTSUPP;
1388 
1389 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1390 					      cls->common.extack))
1391 		return -EOPNOTSUPP;
1392 
1393 	act = &cls->rule->action.entries[0];
1394 
1395 	if (!act->dev)
1396 		return -EINVAL;
1397 
1398 	if (!dsa_slave_dev_check(act->dev))
1399 		return -EOPNOTSUPP;
1400 
1401 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1402 	if (!mall_tc_entry)
1403 		return -ENOMEM;
1404 
1405 	mall_tc_entry->cookie = cls->cookie;
1406 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1407 	mirror = &mall_tc_entry->mirror;
1408 
1409 	to_dp = dsa_slave_to_port(act->dev);
1410 
1411 	mirror->to_local_port = to_dp->index;
1412 	mirror->ingress = ingress;
1413 
1414 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1415 	if (err) {
1416 		kfree(mall_tc_entry);
1417 		return err;
1418 	}
1419 
1420 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1421 
1422 	return err;
1423 }
1424 
1425 static int
1426 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1427 				  struct tc_cls_matchall_offload *cls,
1428 				  bool ingress)
1429 {
1430 	struct netlink_ext_ack *extack = cls->common.extack;
1431 	struct dsa_port *dp = dsa_slave_to_port(dev);
1432 	struct dsa_slave_priv *p = netdev_priv(dev);
1433 	struct dsa_mall_policer_tc_entry *policer;
1434 	struct dsa_mall_tc_entry *mall_tc_entry;
1435 	struct dsa_switch *ds = dp->ds;
1436 	struct flow_action_entry *act;
1437 	int err;
1438 
1439 	if (!ds->ops->port_policer_add) {
1440 		NL_SET_ERR_MSG_MOD(extack,
1441 				   "Policing offload not implemented");
1442 		return -EOPNOTSUPP;
1443 	}
1444 
1445 	if (!ingress) {
1446 		NL_SET_ERR_MSG_MOD(extack,
1447 				   "Only supported on ingress qdisc");
1448 		return -EOPNOTSUPP;
1449 	}
1450 
1451 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1452 					      cls->common.extack))
1453 		return -EOPNOTSUPP;
1454 
1455 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1456 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1457 			NL_SET_ERR_MSG_MOD(extack,
1458 					   "Only one port policer allowed");
1459 			return -EEXIST;
1460 		}
1461 	}
1462 
1463 	act = &cls->rule->action.entries[0];
1464 
1465 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1466 	if (!mall_tc_entry)
1467 		return -ENOMEM;
1468 
1469 	mall_tc_entry->cookie = cls->cookie;
1470 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1471 	policer = &mall_tc_entry->policer;
1472 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1473 	policer->burst = act->police.burst;
1474 
1475 	err = ds->ops->port_policer_add(ds, dp->index, policer);
1476 	if (err) {
1477 		kfree(mall_tc_entry);
1478 		return err;
1479 	}
1480 
1481 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1482 
1483 	return err;
1484 }
1485 
1486 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1487 				      struct tc_cls_matchall_offload *cls,
1488 				      bool ingress)
1489 {
1490 	int err = -EOPNOTSUPP;
1491 
1492 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1493 	    flow_offload_has_one_action(&cls->rule->action) &&
1494 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1495 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1496 	else if (flow_offload_has_one_action(&cls->rule->action) &&
1497 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1498 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1499 
1500 	return err;
1501 }
1502 
1503 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1504 				       struct tc_cls_matchall_offload *cls)
1505 {
1506 	struct dsa_port *dp = dsa_slave_to_port(dev);
1507 	struct dsa_mall_tc_entry *mall_tc_entry;
1508 	struct dsa_switch *ds = dp->ds;
1509 
1510 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1511 	if (!mall_tc_entry)
1512 		return;
1513 
1514 	list_del(&mall_tc_entry->list);
1515 
1516 	switch (mall_tc_entry->type) {
1517 	case DSA_PORT_MALL_MIRROR:
1518 		if (ds->ops->port_mirror_del)
1519 			ds->ops->port_mirror_del(ds, dp->index,
1520 						 &mall_tc_entry->mirror);
1521 		break;
1522 	case DSA_PORT_MALL_POLICER:
1523 		if (ds->ops->port_policer_del)
1524 			ds->ops->port_policer_del(ds, dp->index);
1525 		break;
1526 	default:
1527 		WARN_ON(1);
1528 	}
1529 
1530 	kfree(mall_tc_entry);
1531 }
1532 
1533 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1534 					   struct tc_cls_matchall_offload *cls,
1535 					   bool ingress)
1536 {
1537 	if (cls->common.chain_index)
1538 		return -EOPNOTSUPP;
1539 
1540 	switch (cls->command) {
1541 	case TC_CLSMATCHALL_REPLACE:
1542 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
1543 	case TC_CLSMATCHALL_DESTROY:
1544 		dsa_slave_del_cls_matchall(dev, cls);
1545 		return 0;
1546 	default:
1547 		return -EOPNOTSUPP;
1548 	}
1549 }
1550 
1551 static int dsa_slave_add_cls_flower(struct net_device *dev,
1552 				    struct flow_cls_offload *cls,
1553 				    bool ingress)
1554 {
1555 	struct dsa_port *dp = dsa_slave_to_port(dev);
1556 	struct dsa_switch *ds = dp->ds;
1557 	int port = dp->index;
1558 
1559 	if (!ds->ops->cls_flower_add)
1560 		return -EOPNOTSUPP;
1561 
1562 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1563 }
1564 
1565 static int dsa_slave_del_cls_flower(struct net_device *dev,
1566 				    struct flow_cls_offload *cls,
1567 				    bool ingress)
1568 {
1569 	struct dsa_port *dp = dsa_slave_to_port(dev);
1570 	struct dsa_switch *ds = dp->ds;
1571 	int port = dp->index;
1572 
1573 	if (!ds->ops->cls_flower_del)
1574 		return -EOPNOTSUPP;
1575 
1576 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1577 }
1578 
1579 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1580 				      struct flow_cls_offload *cls,
1581 				      bool ingress)
1582 {
1583 	struct dsa_port *dp = dsa_slave_to_port(dev);
1584 	struct dsa_switch *ds = dp->ds;
1585 	int port = dp->index;
1586 
1587 	if (!ds->ops->cls_flower_stats)
1588 		return -EOPNOTSUPP;
1589 
1590 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1591 }
1592 
1593 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1594 					 struct flow_cls_offload *cls,
1595 					 bool ingress)
1596 {
1597 	switch (cls->command) {
1598 	case FLOW_CLS_REPLACE:
1599 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1600 	case FLOW_CLS_DESTROY:
1601 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1602 	case FLOW_CLS_STATS:
1603 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1604 	default:
1605 		return -EOPNOTSUPP;
1606 	}
1607 }
1608 
1609 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1610 				       void *cb_priv, bool ingress)
1611 {
1612 	struct net_device *dev = cb_priv;
1613 
1614 	if (!tc_can_offload(dev))
1615 		return -EOPNOTSUPP;
1616 
1617 	switch (type) {
1618 	case TC_SETUP_CLSMATCHALL:
1619 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1620 	case TC_SETUP_CLSFLOWER:
1621 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1622 	default:
1623 		return -EOPNOTSUPP;
1624 	}
1625 }
1626 
1627 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1628 					  void *type_data, void *cb_priv)
1629 {
1630 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1631 }
1632 
1633 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1634 					  void *type_data, void *cb_priv)
1635 {
1636 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1637 }
1638 
1639 static LIST_HEAD(dsa_slave_block_cb_list);
1640 
1641 static int dsa_slave_setup_tc_block(struct net_device *dev,
1642 				    struct flow_block_offload *f)
1643 {
1644 	struct flow_block_cb *block_cb;
1645 	flow_setup_cb_t *cb;
1646 
1647 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1648 		cb = dsa_slave_setup_tc_block_cb_ig;
1649 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1650 		cb = dsa_slave_setup_tc_block_cb_eg;
1651 	else
1652 		return -EOPNOTSUPP;
1653 
1654 	f->driver_block_list = &dsa_slave_block_cb_list;
1655 
1656 	switch (f->command) {
1657 	case FLOW_BLOCK_BIND:
1658 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1659 			return -EBUSY;
1660 
1661 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1662 		if (IS_ERR(block_cb))
1663 			return PTR_ERR(block_cb);
1664 
1665 		flow_block_cb_add(block_cb, f);
1666 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1667 		return 0;
1668 	case FLOW_BLOCK_UNBIND:
1669 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1670 		if (!block_cb)
1671 			return -ENOENT;
1672 
1673 		flow_block_cb_remove(block_cb, f);
1674 		list_del(&block_cb->driver_list);
1675 		return 0;
1676 	default:
1677 		return -EOPNOTSUPP;
1678 	}
1679 }
1680 
1681 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1682 				    void *type_data)
1683 {
1684 	struct net_device *master = dsa_port_to_master(dsa_to_port(ds, port));
1685 
1686 	if (!master->netdev_ops->ndo_setup_tc)
1687 		return -EOPNOTSUPP;
1688 
1689 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1690 }
1691 
1692 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1693 			      void *type_data)
1694 {
1695 	struct dsa_port *dp = dsa_slave_to_port(dev);
1696 	struct dsa_switch *ds = dp->ds;
1697 
1698 	switch (type) {
1699 	case TC_SETUP_BLOCK:
1700 		return dsa_slave_setup_tc_block(dev, type_data);
1701 	case TC_SETUP_FT:
1702 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1703 	default:
1704 		break;
1705 	}
1706 
1707 	if (!ds->ops->port_setup_tc)
1708 		return -EOPNOTSUPP;
1709 
1710 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1711 }
1712 
1713 static int dsa_slave_get_rxnfc(struct net_device *dev,
1714 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1715 {
1716 	struct dsa_port *dp = dsa_slave_to_port(dev);
1717 	struct dsa_switch *ds = dp->ds;
1718 
1719 	if (!ds->ops->get_rxnfc)
1720 		return -EOPNOTSUPP;
1721 
1722 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1723 }
1724 
1725 static int dsa_slave_set_rxnfc(struct net_device *dev,
1726 			       struct ethtool_rxnfc *nfc)
1727 {
1728 	struct dsa_port *dp = dsa_slave_to_port(dev);
1729 	struct dsa_switch *ds = dp->ds;
1730 
1731 	if (!ds->ops->set_rxnfc)
1732 		return -EOPNOTSUPP;
1733 
1734 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1735 }
1736 
1737 static int dsa_slave_get_ts_info(struct net_device *dev,
1738 				 struct ethtool_ts_info *ts)
1739 {
1740 	struct dsa_slave_priv *p = netdev_priv(dev);
1741 	struct dsa_switch *ds = p->dp->ds;
1742 
1743 	if (!ds->ops->get_ts_info)
1744 		return -EOPNOTSUPP;
1745 
1746 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
1747 }
1748 
1749 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1750 				     u16 vid)
1751 {
1752 	struct dsa_port *dp = dsa_slave_to_port(dev);
1753 	struct switchdev_obj_port_vlan vlan = {
1754 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1755 		.vid = vid,
1756 		/* This API only allows programming tagged, non-PVID VIDs */
1757 		.flags = 0,
1758 	};
1759 	struct netlink_ext_ack extack = {0};
1760 	struct dsa_switch *ds = dp->ds;
1761 	struct netdev_hw_addr *ha;
1762 	int ret;
1763 
1764 	/* User port... */
1765 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
1766 	if (ret) {
1767 		if (extack._msg)
1768 			netdev_err(dev, "%s\n", extack._msg);
1769 		return ret;
1770 	}
1771 
1772 	/* And CPU port... */
1773 	ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
1774 	if (ret) {
1775 		if (extack._msg)
1776 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1777 				   extack._msg);
1778 		return ret;
1779 	}
1780 
1781 	if (!dsa_switch_supports_uc_filtering(ds) &&
1782 	    !dsa_switch_supports_mc_filtering(ds))
1783 		return 0;
1784 
1785 	netif_addr_lock_bh(dev);
1786 
1787 	if (dsa_switch_supports_mc_filtering(ds)) {
1788 		netdev_for_each_synced_mc_addr(ha, dev) {
1789 			dsa_slave_schedule_standalone_work(dev, DSA_MC_ADD,
1790 							   ha->addr, vid);
1791 		}
1792 	}
1793 
1794 	if (dsa_switch_supports_uc_filtering(ds)) {
1795 		netdev_for_each_synced_uc_addr(ha, dev) {
1796 			dsa_slave_schedule_standalone_work(dev, DSA_UC_ADD,
1797 							   ha->addr, vid);
1798 		}
1799 	}
1800 
1801 	netif_addr_unlock_bh(dev);
1802 
1803 	dsa_flush_workqueue();
1804 
1805 	return 0;
1806 }
1807 
1808 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1809 				      u16 vid)
1810 {
1811 	struct dsa_port *dp = dsa_slave_to_port(dev);
1812 	struct switchdev_obj_port_vlan vlan = {
1813 		.vid = vid,
1814 		/* This API only allows programming tagged, non-PVID VIDs */
1815 		.flags = 0,
1816 	};
1817 	struct dsa_switch *ds = dp->ds;
1818 	struct netdev_hw_addr *ha;
1819 	int err;
1820 
1821 	err = dsa_port_vlan_del(dp, &vlan);
1822 	if (err)
1823 		return err;
1824 
1825 	err = dsa_port_host_vlan_del(dp, &vlan);
1826 	if (err)
1827 		return err;
1828 
1829 	if (!dsa_switch_supports_uc_filtering(ds) &&
1830 	    !dsa_switch_supports_mc_filtering(ds))
1831 		return 0;
1832 
1833 	netif_addr_lock_bh(dev);
1834 
1835 	if (dsa_switch_supports_mc_filtering(ds)) {
1836 		netdev_for_each_synced_mc_addr(ha, dev) {
1837 			dsa_slave_schedule_standalone_work(dev, DSA_MC_DEL,
1838 							   ha->addr, vid);
1839 		}
1840 	}
1841 
1842 	if (dsa_switch_supports_uc_filtering(ds)) {
1843 		netdev_for_each_synced_uc_addr(ha, dev) {
1844 			dsa_slave_schedule_standalone_work(dev, DSA_UC_DEL,
1845 							   ha->addr, vid);
1846 		}
1847 	}
1848 
1849 	netif_addr_unlock_bh(dev);
1850 
1851 	dsa_flush_workqueue();
1852 
1853 	return 0;
1854 }
1855 
1856 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
1857 {
1858 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1859 
1860 	return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
1861 }
1862 
1863 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
1864 {
1865 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1866 
1867 	return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
1868 }
1869 
1870 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1871  * filtering is enabled. The baseline is that only ports that offload a
1872  * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1873  * but there are exceptions for quirky hardware.
1874  *
1875  * If ds->vlan_filtering_is_global = true, then standalone ports which share
1876  * the same switch with other ports that offload a VLAN-aware bridge are also
1877  * inevitably VLAN-aware.
1878  *
1879  * To summarize, a DSA switch port offloads:
1880  *
1881  * - If standalone (this includes software bridge, software LAG):
1882  *     - if ds->needs_standalone_vlan_filtering = true, OR if
1883  *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
1884  *       this switch chip which have vlan_filtering=1)
1885  *         - the 8021q upper VLANs
1886  *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
1887  *       global, or it is, but no port is under a VLAN-aware bridge):
1888  *         - no VLAN (any 8021q upper is a software VLAN)
1889  *
1890  * - If under a vlan_filtering=0 bridge which it offload:
1891  *     - if ds->configure_vlan_while_not_filtering = true (default):
1892  *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
1893  *     - else (deprecated):
1894  *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1895  *           enabled, so this behavior is broken and discouraged.
1896  *
1897  * - If under a vlan_filtering=1 bridge which it offload:
1898  *     - the bridge VLANs
1899  *     - the 8021q upper VLANs
1900  */
1901 int dsa_slave_manage_vlan_filtering(struct net_device *slave,
1902 				    bool vlan_filtering)
1903 {
1904 	int err;
1905 
1906 	if (vlan_filtering) {
1907 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1908 
1909 		err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
1910 		if (err) {
1911 			vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1912 			slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1913 			return err;
1914 		}
1915 	} else {
1916 		err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1917 		if (err)
1918 			return err;
1919 
1920 		slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1921 	}
1922 
1923 	return 0;
1924 }
1925 
1926 struct dsa_hw_port {
1927 	struct list_head list;
1928 	struct net_device *dev;
1929 	int old_mtu;
1930 };
1931 
1932 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1933 {
1934 	const struct dsa_hw_port *p;
1935 	int err;
1936 
1937 	list_for_each_entry(p, hw_port_list, list) {
1938 		if (p->dev->mtu == mtu)
1939 			continue;
1940 
1941 		err = dev_set_mtu(p->dev, mtu);
1942 		if (err)
1943 			goto rollback;
1944 	}
1945 
1946 	return 0;
1947 
1948 rollback:
1949 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1950 		if (p->dev->mtu == p->old_mtu)
1951 			continue;
1952 
1953 		if (dev_set_mtu(p->dev, p->old_mtu))
1954 			netdev_err(p->dev, "Failed to restore MTU\n");
1955 	}
1956 
1957 	return err;
1958 }
1959 
1960 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1961 {
1962 	struct dsa_hw_port *p, *n;
1963 
1964 	list_for_each_entry_safe(p, n, hw_port_list, list)
1965 		kfree(p);
1966 }
1967 
1968 /* Make the hardware datapath to/from @dev limited to a common MTU */
1969 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1970 {
1971 	struct list_head hw_port_list;
1972 	struct dsa_switch_tree *dst;
1973 	int min_mtu = ETH_MAX_MTU;
1974 	struct dsa_port *other_dp;
1975 	int err;
1976 
1977 	if (!dp->ds->mtu_enforcement_ingress)
1978 		return;
1979 
1980 	if (!dp->bridge)
1981 		return;
1982 
1983 	INIT_LIST_HEAD(&hw_port_list);
1984 
1985 	/* Populate the list of ports that are part of the same bridge
1986 	 * as the newly added/modified port
1987 	 */
1988 	list_for_each_entry(dst, &dsa_tree_list, list) {
1989 		list_for_each_entry(other_dp, &dst->ports, list) {
1990 			struct dsa_hw_port *hw_port;
1991 			struct net_device *slave;
1992 
1993 			if (other_dp->type != DSA_PORT_TYPE_USER)
1994 				continue;
1995 
1996 			if (!dsa_port_bridge_same(dp, other_dp))
1997 				continue;
1998 
1999 			if (!other_dp->ds->mtu_enforcement_ingress)
2000 				continue;
2001 
2002 			slave = other_dp->slave;
2003 
2004 			if (min_mtu > slave->mtu)
2005 				min_mtu = slave->mtu;
2006 
2007 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
2008 			if (!hw_port)
2009 				goto out;
2010 
2011 			hw_port->dev = slave;
2012 			hw_port->old_mtu = slave->mtu;
2013 
2014 			list_add(&hw_port->list, &hw_port_list);
2015 		}
2016 	}
2017 
2018 	/* Attempt to configure the entire hardware bridge to the newly added
2019 	 * interface's MTU first, regardless of whether the intention of the
2020 	 * user was to raise or lower it.
2021 	 */
2022 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
2023 	if (!err)
2024 		goto out;
2025 
2026 	/* Clearly that didn't work out so well, so just set the minimum MTU on
2027 	 * all hardware bridge ports now. If this fails too, then all ports will
2028 	 * still have their old MTU rolled back anyway.
2029 	 */
2030 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
2031 
2032 out:
2033 	dsa_hw_port_list_free(&hw_port_list);
2034 }
2035 
2036 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
2037 {
2038 	struct net_device *master = dsa_slave_to_master(dev);
2039 	struct dsa_port *dp = dsa_slave_to_port(dev);
2040 	struct dsa_port *cpu_dp = dp->cpu_dp;
2041 	struct dsa_switch *ds = dp->ds;
2042 	struct dsa_port *other_dp;
2043 	int largest_mtu = 0;
2044 	int new_master_mtu;
2045 	int old_master_mtu;
2046 	int mtu_limit;
2047 	int overhead;
2048 	int cpu_mtu;
2049 	int err;
2050 
2051 	if (!ds->ops->port_change_mtu)
2052 		return -EOPNOTSUPP;
2053 
2054 	dsa_tree_for_each_user_port(other_dp, ds->dst) {
2055 		int slave_mtu;
2056 
2057 		/* During probe, this function will be called for each slave
2058 		 * device, while not all of them have been allocated. That's
2059 		 * ok, it doesn't change what the maximum is, so ignore it.
2060 		 */
2061 		if (!other_dp->slave)
2062 			continue;
2063 
2064 		/* Pretend that we already applied the setting, which we
2065 		 * actually haven't (still haven't done all integrity checks)
2066 		 */
2067 		if (dp == other_dp)
2068 			slave_mtu = new_mtu;
2069 		else
2070 			slave_mtu = other_dp->slave->mtu;
2071 
2072 		if (largest_mtu < slave_mtu)
2073 			largest_mtu = slave_mtu;
2074 	}
2075 
2076 	overhead = dsa_tag_protocol_overhead(cpu_dp->tag_ops);
2077 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu + overhead);
2078 	old_master_mtu = master->mtu;
2079 	new_master_mtu = largest_mtu + overhead;
2080 	if (new_master_mtu > mtu_limit)
2081 		return -ERANGE;
2082 
2083 	/* If the master MTU isn't over limit, there's no need to check the CPU
2084 	 * MTU, since that surely isn't either.
2085 	 */
2086 	cpu_mtu = largest_mtu;
2087 
2088 	/* Start applying stuff */
2089 	if (new_master_mtu != old_master_mtu) {
2090 		err = dev_set_mtu(master, new_master_mtu);
2091 		if (err < 0)
2092 			goto out_master_failed;
2093 
2094 		/* We only need to propagate the MTU of the CPU port to
2095 		 * upstream switches, so emit a notifier which updates them.
2096 		 */
2097 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu);
2098 		if (err)
2099 			goto out_cpu_failed;
2100 	}
2101 
2102 	err = ds->ops->port_change_mtu(ds, dp->index, new_mtu);
2103 	if (err)
2104 		goto out_port_failed;
2105 
2106 	dev->mtu = new_mtu;
2107 
2108 	dsa_bridge_mtu_normalization(dp);
2109 
2110 	return 0;
2111 
2112 out_port_failed:
2113 	if (new_master_mtu != old_master_mtu)
2114 		dsa_port_mtu_change(cpu_dp, old_master_mtu - overhead);
2115 out_cpu_failed:
2116 	if (new_master_mtu != old_master_mtu)
2117 		dev_set_mtu(master, old_master_mtu);
2118 out_master_failed:
2119 	return err;
2120 }
2121 
2122 static int __maybe_unused
2123 dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
2124 {
2125 	struct dsa_port *dp = dsa_slave_to_port(dev);
2126 	struct dsa_switch *ds = dp->ds;
2127 	unsigned long mask, new_prio;
2128 	int err, port = dp->index;
2129 
2130 	if (!ds->ops->port_set_default_prio)
2131 		return -EOPNOTSUPP;
2132 
2133 	err = dcb_ieee_setapp(dev, app);
2134 	if (err)
2135 		return err;
2136 
2137 	mask = dcb_ieee_getapp_mask(dev, app);
2138 	new_prio = __fls(mask);
2139 
2140 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
2141 	if (err) {
2142 		dcb_ieee_delapp(dev, app);
2143 		return err;
2144 	}
2145 
2146 	return 0;
2147 }
2148 
2149 static int __maybe_unused
2150 dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
2151 {
2152 	struct dsa_port *dp = dsa_slave_to_port(dev);
2153 	struct dsa_switch *ds = dp->ds;
2154 	unsigned long mask, new_prio;
2155 	int err, port = dp->index;
2156 	u8 dscp = app->protocol;
2157 
2158 	if (!ds->ops->port_add_dscp_prio)
2159 		return -EOPNOTSUPP;
2160 
2161 	if (dscp >= 64) {
2162 		netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
2163 			   dscp);
2164 		return -EINVAL;
2165 	}
2166 
2167 	err = dcb_ieee_setapp(dev, app);
2168 	if (err)
2169 		return err;
2170 
2171 	mask = dcb_ieee_getapp_mask(dev, app);
2172 	new_prio = __fls(mask);
2173 
2174 	err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
2175 	if (err) {
2176 		dcb_ieee_delapp(dev, app);
2177 		return err;
2178 	}
2179 
2180 	return 0;
2181 }
2182 
2183 static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev,
2184 						      struct dcb_app *app)
2185 {
2186 	switch (app->selector) {
2187 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2188 		switch (app->protocol) {
2189 		case 0:
2190 			return dsa_slave_dcbnl_set_default_prio(dev, app);
2191 		default:
2192 			return -EOPNOTSUPP;
2193 		}
2194 		break;
2195 	case IEEE_8021QAZ_APP_SEL_DSCP:
2196 		return dsa_slave_dcbnl_add_dscp_prio(dev, app);
2197 	default:
2198 		return -EOPNOTSUPP;
2199 	}
2200 }
2201 
2202 static int __maybe_unused
2203 dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
2204 {
2205 	struct dsa_port *dp = dsa_slave_to_port(dev);
2206 	struct dsa_switch *ds = dp->ds;
2207 	unsigned long mask, new_prio;
2208 	int err, port = dp->index;
2209 
2210 	if (!ds->ops->port_set_default_prio)
2211 		return -EOPNOTSUPP;
2212 
2213 	err = dcb_ieee_delapp(dev, app);
2214 	if (err)
2215 		return err;
2216 
2217 	mask = dcb_ieee_getapp_mask(dev, app);
2218 	new_prio = mask ? __fls(mask) : 0;
2219 
2220 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
2221 	if (err) {
2222 		dcb_ieee_setapp(dev, app);
2223 		return err;
2224 	}
2225 
2226 	return 0;
2227 }
2228 
2229 static int __maybe_unused
2230 dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
2231 {
2232 	struct dsa_port *dp = dsa_slave_to_port(dev);
2233 	struct dsa_switch *ds = dp->ds;
2234 	int err, port = dp->index;
2235 	u8 dscp = app->protocol;
2236 
2237 	if (!ds->ops->port_del_dscp_prio)
2238 		return -EOPNOTSUPP;
2239 
2240 	err = dcb_ieee_delapp(dev, app);
2241 	if (err)
2242 		return err;
2243 
2244 	err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
2245 	if (err) {
2246 		dcb_ieee_setapp(dev, app);
2247 		return err;
2248 	}
2249 
2250 	return 0;
2251 }
2252 
2253 static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev,
2254 						      struct dcb_app *app)
2255 {
2256 	switch (app->selector) {
2257 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2258 		switch (app->protocol) {
2259 		case 0:
2260 			return dsa_slave_dcbnl_del_default_prio(dev, app);
2261 		default:
2262 			return -EOPNOTSUPP;
2263 		}
2264 		break;
2265 	case IEEE_8021QAZ_APP_SEL_DSCP:
2266 		return dsa_slave_dcbnl_del_dscp_prio(dev, app);
2267 	default:
2268 		return -EOPNOTSUPP;
2269 	}
2270 }
2271 
2272 /* Pre-populate the DCB application priority table with the priorities
2273  * configured during switch setup, which we read from hardware here.
2274  */
2275 static int dsa_slave_dcbnl_init(struct net_device *dev)
2276 {
2277 	struct dsa_port *dp = dsa_slave_to_port(dev);
2278 	struct dsa_switch *ds = dp->ds;
2279 	int port = dp->index;
2280 	int err;
2281 
2282 	if (ds->ops->port_get_default_prio) {
2283 		int prio = ds->ops->port_get_default_prio(ds, port);
2284 		struct dcb_app app = {
2285 			.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2286 			.protocol = 0,
2287 			.priority = prio,
2288 		};
2289 
2290 		if (prio < 0)
2291 			return prio;
2292 
2293 		err = dcb_ieee_setapp(dev, &app);
2294 		if (err)
2295 			return err;
2296 	}
2297 
2298 	if (ds->ops->port_get_dscp_prio) {
2299 		int protocol;
2300 
2301 		for (protocol = 0; protocol < 64; protocol++) {
2302 			struct dcb_app app = {
2303 				.selector = IEEE_8021QAZ_APP_SEL_DSCP,
2304 				.protocol = protocol,
2305 			};
2306 			int prio;
2307 
2308 			prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
2309 			if (prio == -EOPNOTSUPP)
2310 				continue;
2311 			if (prio < 0)
2312 				return prio;
2313 
2314 			app.priority = prio;
2315 
2316 			err = dcb_ieee_setapp(dev, &app);
2317 			if (err)
2318 				return err;
2319 		}
2320 	}
2321 
2322 	return 0;
2323 }
2324 
2325 static const struct ethtool_ops dsa_slave_ethtool_ops = {
2326 	.get_drvinfo		= dsa_slave_get_drvinfo,
2327 	.get_regs_len		= dsa_slave_get_regs_len,
2328 	.get_regs		= dsa_slave_get_regs,
2329 	.nway_reset		= dsa_slave_nway_reset,
2330 	.get_link		= ethtool_op_get_link,
2331 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
2332 	.get_eeprom		= dsa_slave_get_eeprom,
2333 	.set_eeprom		= dsa_slave_set_eeprom,
2334 	.get_strings		= dsa_slave_get_strings,
2335 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
2336 	.get_sset_count		= dsa_slave_get_sset_count,
2337 	.get_eth_phy_stats	= dsa_slave_get_eth_phy_stats,
2338 	.get_eth_mac_stats	= dsa_slave_get_eth_mac_stats,
2339 	.get_eth_ctrl_stats	= dsa_slave_get_eth_ctrl_stats,
2340 	.get_rmon_stats		= dsa_slave_get_rmon_stats,
2341 	.set_wol		= dsa_slave_set_wol,
2342 	.get_wol		= dsa_slave_get_wol,
2343 	.set_eee		= dsa_slave_set_eee,
2344 	.get_eee		= dsa_slave_get_eee,
2345 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
2346 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
2347 	.get_pause_stats	= dsa_slave_get_pause_stats,
2348 	.get_pauseparam		= dsa_slave_get_pauseparam,
2349 	.set_pauseparam		= dsa_slave_set_pauseparam,
2350 	.get_rxnfc		= dsa_slave_get_rxnfc,
2351 	.set_rxnfc		= dsa_slave_set_rxnfc,
2352 	.get_ts_info		= dsa_slave_get_ts_info,
2353 	.self_test		= dsa_slave_net_selftest,
2354 	.get_mm			= dsa_slave_get_mm,
2355 	.set_mm			= dsa_slave_set_mm,
2356 	.get_mm_stats		= dsa_slave_get_mm_stats,
2357 };
2358 
2359 static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = {
2360 	.ieee_setapp		= dsa_slave_dcbnl_ieee_setapp,
2361 	.ieee_delapp		= dsa_slave_dcbnl_ieee_delapp,
2362 };
2363 
2364 static void dsa_slave_get_stats64(struct net_device *dev,
2365 				  struct rtnl_link_stats64 *s)
2366 {
2367 	struct dsa_port *dp = dsa_slave_to_port(dev);
2368 	struct dsa_switch *ds = dp->ds;
2369 
2370 	if (ds->ops->get_stats64)
2371 		ds->ops->get_stats64(ds, dp->index, s);
2372 	else
2373 		dev_get_tstats64(dev, s);
2374 }
2375 
2376 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
2377 				       struct net_device_path *path)
2378 {
2379 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
2380 	struct net_device *master = dsa_port_to_master(dp);
2381 	struct dsa_port *cpu_dp = dp->cpu_dp;
2382 
2383 	path->dev = ctx->dev;
2384 	path->type = DEV_PATH_DSA;
2385 	path->dsa.proto = cpu_dp->tag_ops->proto;
2386 	path->dsa.port = dp->index;
2387 	ctx->dev = master;
2388 
2389 	return 0;
2390 }
2391 
2392 static const struct net_device_ops dsa_slave_netdev_ops = {
2393 	.ndo_open	 	= dsa_slave_open,
2394 	.ndo_stop		= dsa_slave_close,
2395 	.ndo_start_xmit		= dsa_slave_xmit,
2396 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
2397 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
2398 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
2399 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
2400 	.ndo_eth_ioctl		= dsa_slave_ioctl,
2401 	.ndo_get_iflink		= dsa_slave_get_iflink,
2402 #ifdef CONFIG_NET_POLL_CONTROLLER
2403 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
2404 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
2405 	.ndo_poll_controller	= dsa_slave_poll_controller,
2406 #endif
2407 	.ndo_setup_tc		= dsa_slave_setup_tc,
2408 	.ndo_get_stats64	= dsa_slave_get_stats64,
2409 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
2410 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
2411 	.ndo_change_mtu		= dsa_slave_change_mtu,
2412 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
2413 };
2414 
2415 static struct device_type dsa_type = {
2416 	.name	= "dsa",
2417 };
2418 
2419 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2420 {
2421 	const struct dsa_port *dp = dsa_to_port(ds, port);
2422 
2423 	if (dp->pl)
2424 		phylink_mac_change(dp->pl, up);
2425 }
2426 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2427 
2428 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
2429 					  struct phylink_link_state *state)
2430 {
2431 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
2432 	struct dsa_switch *ds = dp->ds;
2433 
2434 	/* No need to check that this operation is valid, the callback would
2435 	 * not be called if it was not.
2436 	 */
2437 	ds->ops->phylink_fixed_state(ds, dp->index, state);
2438 }
2439 
2440 /* slave device setup *******************************************************/
2441 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
2442 				 u32 flags)
2443 {
2444 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2445 	struct dsa_switch *ds = dp->ds;
2446 
2447 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
2448 	if (!slave_dev->phydev) {
2449 		netdev_err(slave_dev, "no phy at %d\n", addr);
2450 		return -ENODEV;
2451 	}
2452 
2453 	slave_dev->phydev->dev_flags |= flags;
2454 
2455 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
2456 }
2457 
2458 static int dsa_slave_phy_setup(struct net_device *slave_dev)
2459 {
2460 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2461 	struct device_node *port_dn = dp->dn;
2462 	struct dsa_switch *ds = dp->ds;
2463 	u32 phy_flags = 0;
2464 	int ret;
2465 
2466 	dp->pl_config.dev = &slave_dev->dev;
2467 	dp->pl_config.type = PHYLINK_NETDEV;
2468 
2469 	/* The get_fixed_state callback takes precedence over polling the
2470 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
2471 	 * this if the switch provides such a callback.
2472 	 */
2473 	if (ds->ops->phylink_fixed_state) {
2474 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
2475 		dp->pl_config.poll_fixed_state = true;
2476 	}
2477 
2478 	ret = dsa_port_phylink_create(dp);
2479 	if (ret)
2480 		return ret;
2481 
2482 	if (ds->ops->get_phy_flags)
2483 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
2484 
2485 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
2486 	if (ret == -ENODEV && ds->slave_mii_bus) {
2487 		/* We could not connect to a designated PHY or SFP, so try to
2488 		 * use the switch internal MDIO bus instead
2489 		 */
2490 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
2491 	}
2492 	if (ret) {
2493 		netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
2494 			   ERR_PTR(ret));
2495 		dsa_port_phylink_destroy(dp);
2496 	}
2497 
2498 	return ret;
2499 }
2500 
2501 void dsa_slave_setup_tagger(struct net_device *slave)
2502 {
2503 	struct dsa_port *dp = dsa_slave_to_port(slave);
2504 	struct net_device *master = dsa_port_to_master(dp);
2505 	struct dsa_slave_priv *p = netdev_priv(slave);
2506 	const struct dsa_port *cpu_dp = dp->cpu_dp;
2507 	const struct dsa_switch *ds = dp->ds;
2508 
2509 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
2510 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
2511 	/* Try to save one extra realloc later in the TX path (in the master)
2512 	 * by also inheriting the master's needed headroom and tailroom.
2513 	 * The 8021q driver also does this.
2514 	 */
2515 	slave->needed_headroom += master->needed_headroom;
2516 	slave->needed_tailroom += master->needed_tailroom;
2517 
2518 	p->xmit = cpu_dp->tag_ops->xmit;
2519 
2520 	slave->features = master->vlan_features | NETIF_F_HW_TC;
2521 	slave->hw_features |= NETIF_F_HW_TC;
2522 	slave->features |= NETIF_F_LLTX;
2523 	if (slave->needed_tailroom)
2524 		slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
2525 	if (ds->needs_standalone_vlan_filtering)
2526 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2527 }
2528 
2529 int dsa_slave_suspend(struct net_device *slave_dev)
2530 {
2531 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2532 
2533 	if (!netif_running(slave_dev))
2534 		return 0;
2535 
2536 	netif_device_detach(slave_dev);
2537 
2538 	rtnl_lock();
2539 	phylink_stop(dp->pl);
2540 	rtnl_unlock();
2541 
2542 	return 0;
2543 }
2544 
2545 int dsa_slave_resume(struct net_device *slave_dev)
2546 {
2547 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2548 
2549 	if (!netif_running(slave_dev))
2550 		return 0;
2551 
2552 	netif_device_attach(slave_dev);
2553 
2554 	rtnl_lock();
2555 	phylink_start(dp->pl);
2556 	rtnl_unlock();
2557 
2558 	return 0;
2559 }
2560 
2561 int dsa_slave_create(struct dsa_port *port)
2562 {
2563 	struct net_device *master = dsa_port_to_master(port);
2564 	struct dsa_switch *ds = port->ds;
2565 	struct net_device *slave_dev;
2566 	struct dsa_slave_priv *p;
2567 	const char *name;
2568 	int assign_type;
2569 	int ret;
2570 
2571 	if (!ds->num_tx_queues)
2572 		ds->num_tx_queues = 1;
2573 
2574 	if (port->name) {
2575 		name = port->name;
2576 		assign_type = NET_NAME_PREDICTABLE;
2577 	} else {
2578 		name = "eth%d";
2579 		assign_type = NET_NAME_ENUM;
2580 	}
2581 
2582 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
2583 				     assign_type, ether_setup,
2584 				     ds->num_tx_queues, 1);
2585 	if (slave_dev == NULL)
2586 		return -ENOMEM;
2587 
2588 	slave_dev->rtnl_link_ops = &dsa_link_ops;
2589 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2590 #if IS_ENABLED(CONFIG_DCB)
2591 	slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops;
2592 #endif
2593 	if (!is_zero_ether_addr(port->mac))
2594 		eth_hw_addr_set(slave_dev, port->mac);
2595 	else
2596 		eth_hw_addr_inherit(slave_dev, master);
2597 	slave_dev->priv_flags |= IFF_NO_QUEUE;
2598 	if (dsa_switch_supports_uc_filtering(ds))
2599 		slave_dev->priv_flags |= IFF_UNICAST_FLT;
2600 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
2601 	if (ds->ops->port_max_mtu)
2602 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2603 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
2604 
2605 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
2606 	SET_NETDEV_DEVLINK_PORT(slave_dev, &port->devlink_port);
2607 	slave_dev->dev.of_node = port->dn;
2608 	slave_dev->vlan_features = master->vlan_features;
2609 
2610 	p = netdev_priv(slave_dev);
2611 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2612 	if (!slave_dev->tstats) {
2613 		free_netdev(slave_dev);
2614 		return -ENOMEM;
2615 	}
2616 
2617 	ret = gro_cells_init(&p->gcells, slave_dev);
2618 	if (ret)
2619 		goto out_free;
2620 
2621 	p->dp = port;
2622 	INIT_LIST_HEAD(&p->mall_tc_list);
2623 	port->slave = slave_dev;
2624 	dsa_slave_setup_tagger(slave_dev);
2625 
2626 	netif_carrier_off(slave_dev);
2627 
2628 	ret = dsa_slave_phy_setup(slave_dev);
2629 	if (ret) {
2630 		netdev_err(slave_dev,
2631 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2632 			   ret, ds->dst->index, ds->index, port->index);
2633 		goto out_gcells;
2634 	}
2635 
2636 	rtnl_lock();
2637 
2638 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2639 	if (ret && ret != -EOPNOTSUPP)
2640 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2641 			 ret, ETH_DATA_LEN, port->index);
2642 
2643 	ret = register_netdevice(slave_dev);
2644 	if (ret) {
2645 		netdev_err(master, "error %d registering interface %s\n",
2646 			   ret, slave_dev->name);
2647 		rtnl_unlock();
2648 		goto out_phy;
2649 	}
2650 
2651 	if (IS_ENABLED(CONFIG_DCB)) {
2652 		ret = dsa_slave_dcbnl_init(slave_dev);
2653 		if (ret) {
2654 			netdev_err(slave_dev,
2655 				   "failed to initialize DCB: %pe\n",
2656 				   ERR_PTR(ret));
2657 			rtnl_unlock();
2658 			goto out_unregister;
2659 		}
2660 	}
2661 
2662 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
2663 
2664 	rtnl_unlock();
2665 
2666 	if (ret)
2667 		goto out_unregister;
2668 
2669 	return 0;
2670 
2671 out_unregister:
2672 	unregister_netdev(slave_dev);
2673 out_phy:
2674 	rtnl_lock();
2675 	phylink_disconnect_phy(p->dp->pl);
2676 	rtnl_unlock();
2677 	dsa_port_phylink_destroy(p->dp);
2678 out_gcells:
2679 	gro_cells_destroy(&p->gcells);
2680 out_free:
2681 	free_percpu(slave_dev->tstats);
2682 	free_netdev(slave_dev);
2683 	port->slave = NULL;
2684 	return ret;
2685 }
2686 
2687 void dsa_slave_destroy(struct net_device *slave_dev)
2688 {
2689 	struct net_device *master = dsa_slave_to_master(slave_dev);
2690 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2691 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2692 
2693 	netif_carrier_off(slave_dev);
2694 	rtnl_lock();
2695 	netdev_upper_dev_unlink(master, slave_dev);
2696 	unregister_netdevice(slave_dev);
2697 	phylink_disconnect_phy(dp->pl);
2698 	rtnl_unlock();
2699 
2700 	dsa_port_phylink_destroy(dp);
2701 	gro_cells_destroy(&p->gcells);
2702 	free_percpu(slave_dev->tstats);
2703 	free_netdev(slave_dev);
2704 }
2705 
2706 int dsa_slave_change_master(struct net_device *dev, struct net_device *master,
2707 			    struct netlink_ext_ack *extack)
2708 {
2709 	struct net_device *old_master = dsa_slave_to_master(dev);
2710 	struct dsa_port *dp = dsa_slave_to_port(dev);
2711 	struct dsa_switch *ds = dp->ds;
2712 	struct net_device *upper;
2713 	struct list_head *iter;
2714 	int err;
2715 
2716 	if (master == old_master)
2717 		return 0;
2718 
2719 	if (!ds->ops->port_change_master) {
2720 		NL_SET_ERR_MSG_MOD(extack,
2721 				   "Driver does not support changing DSA master");
2722 		return -EOPNOTSUPP;
2723 	}
2724 
2725 	if (!netdev_uses_dsa(master)) {
2726 		NL_SET_ERR_MSG_MOD(extack,
2727 				   "Interface not eligible as DSA master");
2728 		return -EOPNOTSUPP;
2729 	}
2730 
2731 	netdev_for_each_upper_dev_rcu(master, upper, iter) {
2732 		if (dsa_slave_dev_check(upper))
2733 			continue;
2734 		if (netif_is_bridge_master(upper))
2735 			continue;
2736 		NL_SET_ERR_MSG_MOD(extack, "Cannot join master with unknown uppers");
2737 		return -EOPNOTSUPP;
2738 	}
2739 
2740 	/* Since we allow live-changing the DSA master, plus we auto-open the
2741 	 * DSA master when the user port opens => we need to ensure that the
2742 	 * new DSA master is open too.
2743 	 */
2744 	if (dev->flags & IFF_UP) {
2745 		err = dev_open(master, extack);
2746 		if (err)
2747 			return err;
2748 	}
2749 
2750 	netdev_upper_dev_unlink(old_master, dev);
2751 
2752 	err = netdev_upper_dev_link(master, dev, extack);
2753 	if (err)
2754 		goto out_revert_old_master_unlink;
2755 
2756 	err = dsa_port_change_master(dp, master, extack);
2757 	if (err)
2758 		goto out_revert_master_link;
2759 
2760 	/* Update the MTU of the new CPU port through cross-chip notifiers */
2761 	err = dsa_slave_change_mtu(dev, dev->mtu);
2762 	if (err && err != -EOPNOTSUPP) {
2763 		netdev_warn(dev,
2764 			    "nonfatal error updating MTU with new master: %pe\n",
2765 			    ERR_PTR(err));
2766 	}
2767 
2768 	/* If the port doesn't have its own MAC address and relies on the DSA
2769 	 * master's one, inherit it again from the new DSA master.
2770 	 */
2771 	if (is_zero_ether_addr(dp->mac))
2772 		eth_hw_addr_inherit(dev, master);
2773 
2774 	return 0;
2775 
2776 out_revert_master_link:
2777 	netdev_upper_dev_unlink(master, dev);
2778 out_revert_old_master_unlink:
2779 	netdev_upper_dev_link(old_master, dev, NULL);
2780 	return err;
2781 }
2782 
2783 bool dsa_slave_dev_check(const struct net_device *dev)
2784 {
2785 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2786 }
2787 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2788 
2789 static int dsa_slave_changeupper(struct net_device *dev,
2790 				 struct netdev_notifier_changeupper_info *info)
2791 {
2792 	struct dsa_port *dp = dsa_slave_to_port(dev);
2793 	struct netlink_ext_ack *extack;
2794 	int err = NOTIFY_DONE;
2795 
2796 	if (!dsa_slave_dev_check(dev))
2797 		return err;
2798 
2799 	extack = netdev_notifier_info_to_extack(&info->info);
2800 
2801 	if (netif_is_bridge_master(info->upper_dev)) {
2802 		if (info->linking) {
2803 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2804 			if (!err)
2805 				dsa_bridge_mtu_normalization(dp);
2806 			if (err == -EOPNOTSUPP) {
2807 				NL_SET_ERR_MSG_WEAK_MOD(extack,
2808 							"Offloading not supported");
2809 				err = 0;
2810 			}
2811 			err = notifier_from_errno(err);
2812 		} else {
2813 			dsa_port_bridge_leave(dp, info->upper_dev);
2814 			err = NOTIFY_OK;
2815 		}
2816 	} else if (netif_is_lag_master(info->upper_dev)) {
2817 		if (info->linking) {
2818 			err = dsa_port_lag_join(dp, info->upper_dev,
2819 						info->upper_info, extack);
2820 			if (err == -EOPNOTSUPP) {
2821 				NL_SET_ERR_MSG_WEAK_MOD(extack,
2822 							"Offloading not supported");
2823 				err = 0;
2824 			}
2825 			err = notifier_from_errno(err);
2826 		} else {
2827 			dsa_port_lag_leave(dp, info->upper_dev);
2828 			err = NOTIFY_OK;
2829 		}
2830 	} else if (is_hsr_master(info->upper_dev)) {
2831 		if (info->linking) {
2832 			err = dsa_port_hsr_join(dp, info->upper_dev);
2833 			if (err == -EOPNOTSUPP) {
2834 				NL_SET_ERR_MSG_WEAK_MOD(extack,
2835 							"Offloading not supported");
2836 				err = 0;
2837 			}
2838 			err = notifier_from_errno(err);
2839 		} else {
2840 			dsa_port_hsr_leave(dp, info->upper_dev);
2841 			err = NOTIFY_OK;
2842 		}
2843 	}
2844 
2845 	return err;
2846 }
2847 
2848 static int dsa_slave_prechangeupper(struct net_device *dev,
2849 				    struct netdev_notifier_changeupper_info *info)
2850 {
2851 	struct dsa_port *dp = dsa_slave_to_port(dev);
2852 
2853 	if (!dsa_slave_dev_check(dev))
2854 		return NOTIFY_DONE;
2855 
2856 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2857 		dsa_port_pre_bridge_leave(dp, info->upper_dev);
2858 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2859 		dsa_port_pre_lag_leave(dp, info->upper_dev);
2860 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2861 	 * meaningfully enslaved to a bridge yet
2862 	 */
2863 
2864 	return NOTIFY_DONE;
2865 }
2866 
2867 static int
2868 dsa_slave_lag_changeupper(struct net_device *dev,
2869 			  struct netdev_notifier_changeupper_info *info)
2870 {
2871 	struct net_device *lower;
2872 	struct list_head *iter;
2873 	int err = NOTIFY_DONE;
2874 	struct dsa_port *dp;
2875 
2876 	if (!netif_is_lag_master(dev))
2877 		return err;
2878 
2879 	netdev_for_each_lower_dev(dev, lower, iter) {
2880 		if (!dsa_slave_dev_check(lower))
2881 			continue;
2882 
2883 		dp = dsa_slave_to_port(lower);
2884 		if (!dp->lag)
2885 			/* Software LAG */
2886 			continue;
2887 
2888 		err = dsa_slave_changeupper(lower, info);
2889 		if (notifier_to_errno(err))
2890 			break;
2891 	}
2892 
2893 	return err;
2894 }
2895 
2896 /* Same as dsa_slave_lag_changeupper() except that it calls
2897  * dsa_slave_prechangeupper()
2898  */
2899 static int
2900 dsa_slave_lag_prechangeupper(struct net_device *dev,
2901 			     struct netdev_notifier_changeupper_info *info)
2902 {
2903 	struct net_device *lower;
2904 	struct list_head *iter;
2905 	int err = NOTIFY_DONE;
2906 	struct dsa_port *dp;
2907 
2908 	if (!netif_is_lag_master(dev))
2909 		return err;
2910 
2911 	netdev_for_each_lower_dev(dev, lower, iter) {
2912 		if (!dsa_slave_dev_check(lower))
2913 			continue;
2914 
2915 		dp = dsa_slave_to_port(lower);
2916 		if (!dp->lag)
2917 			/* Software LAG */
2918 			continue;
2919 
2920 		err = dsa_slave_prechangeupper(lower, info);
2921 		if (notifier_to_errno(err))
2922 			break;
2923 	}
2924 
2925 	return err;
2926 }
2927 
2928 static int
2929 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2930 				 struct netdev_notifier_changeupper_info *info)
2931 {
2932 	struct netlink_ext_ack *ext_ack;
2933 	struct net_device *slave, *br;
2934 	struct dsa_port *dp;
2935 
2936 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2937 
2938 	if (!is_vlan_dev(dev))
2939 		return NOTIFY_DONE;
2940 
2941 	slave = vlan_dev_real_dev(dev);
2942 	if (!dsa_slave_dev_check(slave))
2943 		return NOTIFY_DONE;
2944 
2945 	dp = dsa_slave_to_port(slave);
2946 	br = dsa_port_bridge_dev_get(dp);
2947 	if (!br)
2948 		return NOTIFY_DONE;
2949 
2950 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2951 	if (br_vlan_enabled(br) &&
2952 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2953 		NL_SET_ERR_MSG_MOD(ext_ack,
2954 				   "Cannot enslave VLAN device into VLAN aware bridge");
2955 		return notifier_from_errno(-EINVAL);
2956 	}
2957 
2958 	return NOTIFY_DONE;
2959 }
2960 
2961 static int
2962 dsa_slave_check_8021q_upper(struct net_device *dev,
2963 			    struct netdev_notifier_changeupper_info *info)
2964 {
2965 	struct dsa_port *dp = dsa_slave_to_port(dev);
2966 	struct net_device *br = dsa_port_bridge_dev_get(dp);
2967 	struct bridge_vlan_info br_info;
2968 	struct netlink_ext_ack *extack;
2969 	int err = NOTIFY_DONE;
2970 	u16 vid;
2971 
2972 	if (!br || !br_vlan_enabled(br))
2973 		return NOTIFY_DONE;
2974 
2975 	extack = netdev_notifier_info_to_extack(&info->info);
2976 	vid = vlan_dev_vlan_id(info->upper_dev);
2977 
2978 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2979 	 * device, respectively the VID is not found, returning
2980 	 * 0 means success, which is a failure for us here.
2981 	 */
2982 	err = br_vlan_get_info(br, vid, &br_info);
2983 	if (err == 0) {
2984 		NL_SET_ERR_MSG_MOD(extack,
2985 				   "This VLAN is already configured by the bridge");
2986 		return notifier_from_errno(-EBUSY);
2987 	}
2988 
2989 	return NOTIFY_DONE;
2990 }
2991 
2992 static int
2993 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2994 				      struct netdev_notifier_changeupper_info *info)
2995 {
2996 	struct dsa_switch *ds;
2997 	struct dsa_port *dp;
2998 	int err;
2999 
3000 	if (!dsa_slave_dev_check(dev))
3001 		return dsa_prevent_bridging_8021q_upper(dev, info);
3002 
3003 	dp = dsa_slave_to_port(dev);
3004 	ds = dp->ds;
3005 
3006 	if (ds->ops->port_prechangeupper) {
3007 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
3008 		if (err)
3009 			return notifier_from_errno(err);
3010 	}
3011 
3012 	if (is_vlan_dev(info->upper_dev))
3013 		return dsa_slave_check_8021q_upper(dev, info);
3014 
3015 	return NOTIFY_DONE;
3016 }
3017 
3018 /* To be eligible as a DSA master, a LAG must have all lower interfaces be
3019  * eligible DSA masters. Additionally, all LAG slaves must be DSA masters of
3020  * switches in the same switch tree.
3021  */
3022 static int dsa_lag_master_validate(struct net_device *lag_dev,
3023 				   struct netlink_ext_ack *extack)
3024 {
3025 	struct net_device *lower1, *lower2;
3026 	struct list_head *iter1, *iter2;
3027 
3028 	netdev_for_each_lower_dev(lag_dev, lower1, iter1) {
3029 		netdev_for_each_lower_dev(lag_dev, lower2, iter2) {
3030 			if (!netdev_uses_dsa(lower1) ||
3031 			    !netdev_uses_dsa(lower2)) {
3032 				NL_SET_ERR_MSG_MOD(extack,
3033 						   "All LAG ports must be eligible as DSA masters");
3034 				return notifier_from_errno(-EINVAL);
3035 			}
3036 
3037 			if (lower1 == lower2)
3038 				continue;
3039 
3040 			if (!dsa_port_tree_same(lower1->dsa_ptr,
3041 						lower2->dsa_ptr)) {
3042 				NL_SET_ERR_MSG_MOD(extack,
3043 						   "LAG contains DSA masters of disjoint switch trees");
3044 				return notifier_from_errno(-EINVAL);
3045 			}
3046 		}
3047 	}
3048 
3049 	return NOTIFY_DONE;
3050 }
3051 
3052 static int
3053 dsa_master_prechangeupper_sanity_check(struct net_device *master,
3054 				       struct netdev_notifier_changeupper_info *info)
3055 {
3056 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
3057 
3058 	if (!netdev_uses_dsa(master))
3059 		return NOTIFY_DONE;
3060 
3061 	if (!info->linking)
3062 		return NOTIFY_DONE;
3063 
3064 	/* Allow DSA switch uppers */
3065 	if (dsa_slave_dev_check(info->upper_dev))
3066 		return NOTIFY_DONE;
3067 
3068 	/* Allow bridge uppers of DSA masters, subject to further
3069 	 * restrictions in dsa_bridge_prechangelower_sanity_check()
3070 	 */
3071 	if (netif_is_bridge_master(info->upper_dev))
3072 		return NOTIFY_DONE;
3073 
3074 	/* Allow LAG uppers, subject to further restrictions in
3075 	 * dsa_lag_master_prechangelower_sanity_check()
3076 	 */
3077 	if (netif_is_lag_master(info->upper_dev))
3078 		return dsa_lag_master_validate(info->upper_dev, extack);
3079 
3080 	NL_SET_ERR_MSG_MOD(extack,
3081 			   "DSA master cannot join unknown upper interfaces");
3082 	return notifier_from_errno(-EBUSY);
3083 }
3084 
3085 static int
3086 dsa_lag_master_prechangelower_sanity_check(struct net_device *dev,
3087 					   struct netdev_notifier_changeupper_info *info)
3088 {
3089 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
3090 	struct net_device *lag_dev = info->upper_dev;
3091 	struct net_device *lower;
3092 	struct list_head *iter;
3093 
3094 	if (!netdev_uses_dsa(lag_dev) || !netif_is_lag_master(lag_dev))
3095 		return NOTIFY_DONE;
3096 
3097 	if (!info->linking)
3098 		return NOTIFY_DONE;
3099 
3100 	if (!netdev_uses_dsa(dev)) {
3101 		NL_SET_ERR_MSG(extack,
3102 			       "Only DSA masters can join a LAG DSA master");
3103 		return notifier_from_errno(-EINVAL);
3104 	}
3105 
3106 	netdev_for_each_lower_dev(lag_dev, lower, iter) {
3107 		if (!dsa_port_tree_same(dev->dsa_ptr, lower->dsa_ptr)) {
3108 			NL_SET_ERR_MSG(extack,
3109 				       "Interface is DSA master for a different switch tree than this LAG");
3110 			return notifier_from_errno(-EINVAL);
3111 		}
3112 
3113 		break;
3114 	}
3115 
3116 	return NOTIFY_DONE;
3117 }
3118 
3119 /* Don't allow bridging of DSA masters, since the bridge layer rx_handler
3120  * prevents the DSA fake ethertype handler to be invoked, so we don't get the
3121  * chance to strip off and parse the DSA switch tag protocol header (the bridge
3122  * layer just returns RX_HANDLER_CONSUMED, stopping RX processing for these
3123  * frames).
3124  * The only case where that would not be an issue is when bridging can already
3125  * be offloaded, such as when the DSA master is itself a DSA or plain switchdev
3126  * port, and is bridged only with other ports from the same hardware device.
3127  */
3128 static int
3129 dsa_bridge_prechangelower_sanity_check(struct net_device *new_lower,
3130 				       struct netdev_notifier_changeupper_info *info)
3131 {
3132 	struct net_device *br = info->upper_dev;
3133 	struct netlink_ext_ack *extack;
3134 	struct net_device *lower;
3135 	struct list_head *iter;
3136 
3137 	if (!netif_is_bridge_master(br))
3138 		return NOTIFY_DONE;
3139 
3140 	if (!info->linking)
3141 		return NOTIFY_DONE;
3142 
3143 	extack = netdev_notifier_info_to_extack(&info->info);
3144 
3145 	netdev_for_each_lower_dev(br, lower, iter) {
3146 		if (!netdev_uses_dsa(new_lower) && !netdev_uses_dsa(lower))
3147 			continue;
3148 
3149 		if (!netdev_port_same_parent_id(lower, new_lower)) {
3150 			NL_SET_ERR_MSG(extack,
3151 				       "Cannot do software bridging with a DSA master");
3152 			return notifier_from_errno(-EINVAL);
3153 		}
3154 	}
3155 
3156 	return NOTIFY_DONE;
3157 }
3158 
3159 static void dsa_tree_migrate_ports_from_lag_master(struct dsa_switch_tree *dst,
3160 						   struct net_device *lag_dev)
3161 {
3162 	struct net_device *new_master = dsa_tree_find_first_master(dst);
3163 	struct dsa_port *dp;
3164 	int err;
3165 
3166 	dsa_tree_for_each_user_port(dp, dst) {
3167 		if (dsa_port_to_master(dp) != lag_dev)
3168 			continue;
3169 
3170 		err = dsa_slave_change_master(dp->slave, new_master, NULL);
3171 		if (err) {
3172 			netdev_err(dp->slave,
3173 				   "failed to restore master to %s: %pe\n",
3174 				   new_master->name, ERR_PTR(err));
3175 		}
3176 	}
3177 }
3178 
3179 static int dsa_master_lag_join(struct net_device *master,
3180 			       struct net_device *lag_dev,
3181 			       struct netdev_lag_upper_info *uinfo,
3182 			       struct netlink_ext_ack *extack)
3183 {
3184 	struct dsa_port *cpu_dp = master->dsa_ptr;
3185 	struct dsa_switch_tree *dst = cpu_dp->dst;
3186 	struct dsa_port *dp;
3187 	int err;
3188 
3189 	err = dsa_master_lag_setup(lag_dev, cpu_dp, uinfo, extack);
3190 	if (err)
3191 		return err;
3192 
3193 	dsa_tree_for_each_user_port(dp, dst) {
3194 		if (dsa_port_to_master(dp) != master)
3195 			continue;
3196 
3197 		err = dsa_slave_change_master(dp->slave, lag_dev, extack);
3198 		if (err)
3199 			goto restore;
3200 	}
3201 
3202 	return 0;
3203 
3204 restore:
3205 	dsa_tree_for_each_user_port_continue_reverse(dp, dst) {
3206 		if (dsa_port_to_master(dp) != lag_dev)
3207 			continue;
3208 
3209 		err = dsa_slave_change_master(dp->slave, master, NULL);
3210 		if (err) {
3211 			netdev_err(dp->slave,
3212 				   "failed to restore master to %s: %pe\n",
3213 				   master->name, ERR_PTR(err));
3214 		}
3215 	}
3216 
3217 	dsa_master_lag_teardown(lag_dev, master->dsa_ptr);
3218 
3219 	return err;
3220 }
3221 
3222 static void dsa_master_lag_leave(struct net_device *master,
3223 				 struct net_device *lag_dev)
3224 {
3225 	struct dsa_port *dp, *cpu_dp = lag_dev->dsa_ptr;
3226 	struct dsa_switch_tree *dst = cpu_dp->dst;
3227 	struct dsa_port *new_cpu_dp = NULL;
3228 	struct net_device *lower;
3229 	struct list_head *iter;
3230 
3231 	netdev_for_each_lower_dev(lag_dev, lower, iter) {
3232 		if (netdev_uses_dsa(lower)) {
3233 			new_cpu_dp = lower->dsa_ptr;
3234 			break;
3235 		}
3236 	}
3237 
3238 	if (new_cpu_dp) {
3239 		/* Update the CPU port of the user ports still under the LAG
3240 		 * so that dsa_port_to_master() continues to work properly
3241 		 */
3242 		dsa_tree_for_each_user_port(dp, dst)
3243 			if (dsa_port_to_master(dp) == lag_dev)
3244 				dp->cpu_dp = new_cpu_dp;
3245 
3246 		/* Update the index of the virtual CPU port to match the lowest
3247 		 * physical CPU port
3248 		 */
3249 		lag_dev->dsa_ptr = new_cpu_dp;
3250 		wmb();
3251 	} else {
3252 		/* If the LAG DSA master has no ports left, migrate back all
3253 		 * user ports to the first physical CPU port
3254 		 */
3255 		dsa_tree_migrate_ports_from_lag_master(dst, lag_dev);
3256 	}
3257 
3258 	/* This DSA master has left its LAG in any case, so let
3259 	 * the CPU port leave the hardware LAG as well
3260 	 */
3261 	dsa_master_lag_teardown(lag_dev, master->dsa_ptr);
3262 }
3263 
3264 static int dsa_master_changeupper(struct net_device *dev,
3265 				  struct netdev_notifier_changeupper_info *info)
3266 {
3267 	struct netlink_ext_ack *extack;
3268 	int err = NOTIFY_DONE;
3269 
3270 	if (!netdev_uses_dsa(dev))
3271 		return err;
3272 
3273 	extack = netdev_notifier_info_to_extack(&info->info);
3274 
3275 	if (netif_is_lag_master(info->upper_dev)) {
3276 		if (info->linking) {
3277 			err = dsa_master_lag_join(dev, info->upper_dev,
3278 						  info->upper_info, extack);
3279 			err = notifier_from_errno(err);
3280 		} else {
3281 			dsa_master_lag_leave(dev, info->upper_dev);
3282 			err = NOTIFY_OK;
3283 		}
3284 	}
3285 
3286 	return err;
3287 }
3288 
3289 static int dsa_slave_netdevice_event(struct notifier_block *nb,
3290 				     unsigned long event, void *ptr)
3291 {
3292 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3293 
3294 	switch (event) {
3295 	case NETDEV_PRECHANGEUPPER: {
3296 		struct netdev_notifier_changeupper_info *info = ptr;
3297 		int err;
3298 
3299 		err = dsa_slave_prechangeupper_sanity_check(dev, info);
3300 		if (notifier_to_errno(err))
3301 			return err;
3302 
3303 		err = dsa_master_prechangeupper_sanity_check(dev, info);
3304 		if (notifier_to_errno(err))
3305 			return err;
3306 
3307 		err = dsa_lag_master_prechangelower_sanity_check(dev, info);
3308 		if (notifier_to_errno(err))
3309 			return err;
3310 
3311 		err = dsa_bridge_prechangelower_sanity_check(dev, info);
3312 		if (notifier_to_errno(err))
3313 			return err;
3314 
3315 		err = dsa_slave_prechangeupper(dev, ptr);
3316 		if (notifier_to_errno(err))
3317 			return err;
3318 
3319 		err = dsa_slave_lag_prechangeupper(dev, ptr);
3320 		if (notifier_to_errno(err))
3321 			return err;
3322 
3323 		break;
3324 	}
3325 	case NETDEV_CHANGEUPPER: {
3326 		int err;
3327 
3328 		err = dsa_slave_changeupper(dev, ptr);
3329 		if (notifier_to_errno(err))
3330 			return err;
3331 
3332 		err = dsa_slave_lag_changeupper(dev, ptr);
3333 		if (notifier_to_errno(err))
3334 			return err;
3335 
3336 		err = dsa_master_changeupper(dev, ptr);
3337 		if (notifier_to_errno(err))
3338 			return err;
3339 
3340 		break;
3341 	}
3342 	case NETDEV_CHANGELOWERSTATE: {
3343 		struct netdev_notifier_changelowerstate_info *info = ptr;
3344 		struct dsa_port *dp;
3345 		int err = 0;
3346 
3347 		if (dsa_slave_dev_check(dev)) {
3348 			dp = dsa_slave_to_port(dev);
3349 
3350 			err = dsa_port_lag_change(dp, info->lower_state_info);
3351 		}
3352 
3353 		/* Mirror LAG port events on DSA masters that are in
3354 		 * a LAG towards their respective switch CPU ports
3355 		 */
3356 		if (netdev_uses_dsa(dev)) {
3357 			dp = dev->dsa_ptr;
3358 
3359 			err = dsa_port_lag_change(dp, info->lower_state_info);
3360 		}
3361 
3362 		return notifier_from_errno(err);
3363 	}
3364 	case NETDEV_CHANGE:
3365 	case NETDEV_UP: {
3366 		/* Track state of master port.
3367 		 * DSA driver may require the master port (and indirectly
3368 		 * the tagger) to be available for some special operation.
3369 		 */
3370 		if (netdev_uses_dsa(dev)) {
3371 			struct dsa_port *cpu_dp = dev->dsa_ptr;
3372 			struct dsa_switch_tree *dst = cpu_dp->ds->dst;
3373 
3374 			/* Track when the master port is UP */
3375 			dsa_tree_master_oper_state_change(dst, dev,
3376 							  netif_oper_up(dev));
3377 
3378 			/* Track when the master port is ready and can accept
3379 			 * packet.
3380 			 * NETDEV_UP event is not enough to flag a port as ready.
3381 			 * We also have to wait for linkwatch_do_dev to dev_activate
3382 			 * and emit a NETDEV_CHANGE event.
3383 			 * We check if a master port is ready by checking if the dev
3384 			 * have a qdisc assigned and is not noop.
3385 			 */
3386 			dsa_tree_master_admin_state_change(dst, dev,
3387 							   !qdisc_tx_is_noop(dev));
3388 
3389 			return NOTIFY_OK;
3390 		}
3391 
3392 		return NOTIFY_DONE;
3393 	}
3394 	case NETDEV_GOING_DOWN: {
3395 		struct dsa_port *dp, *cpu_dp;
3396 		struct dsa_switch_tree *dst;
3397 		LIST_HEAD(close_list);
3398 
3399 		if (!netdev_uses_dsa(dev))
3400 			return NOTIFY_DONE;
3401 
3402 		cpu_dp = dev->dsa_ptr;
3403 		dst = cpu_dp->ds->dst;
3404 
3405 		dsa_tree_master_admin_state_change(dst, dev, false);
3406 
3407 		list_for_each_entry(dp, &dst->ports, list) {
3408 			if (!dsa_port_is_user(dp))
3409 				continue;
3410 
3411 			if (dp->cpu_dp != cpu_dp)
3412 				continue;
3413 
3414 			list_add(&dp->slave->close_list, &close_list);
3415 		}
3416 
3417 		dev_close_many(&close_list, true);
3418 
3419 		return NOTIFY_OK;
3420 	}
3421 	default:
3422 		break;
3423 	}
3424 
3425 	return NOTIFY_DONE;
3426 }
3427 
3428 static void
3429 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
3430 {
3431 	struct switchdev_notifier_fdb_info info = {};
3432 
3433 	info.addr = switchdev_work->addr;
3434 	info.vid = switchdev_work->vid;
3435 	info.offloaded = true;
3436 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
3437 				 switchdev_work->orig_dev, &info.info, NULL);
3438 }
3439 
3440 static void dsa_slave_switchdev_event_work(struct work_struct *work)
3441 {
3442 	struct dsa_switchdev_event_work *switchdev_work =
3443 		container_of(work, struct dsa_switchdev_event_work, work);
3444 	const unsigned char *addr = switchdev_work->addr;
3445 	struct net_device *dev = switchdev_work->dev;
3446 	u16 vid = switchdev_work->vid;
3447 	struct dsa_switch *ds;
3448 	struct dsa_port *dp;
3449 	int err;
3450 
3451 	dp = dsa_slave_to_port(dev);
3452 	ds = dp->ds;
3453 
3454 	switch (switchdev_work->event) {
3455 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3456 		if (switchdev_work->host_addr)
3457 			err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
3458 		else if (dp->lag)
3459 			err = dsa_port_lag_fdb_add(dp, addr, vid);
3460 		else
3461 			err = dsa_port_fdb_add(dp, addr, vid);
3462 		if (err) {
3463 			dev_err(ds->dev,
3464 				"port %d failed to add %pM vid %d to fdb: %d\n",
3465 				dp->index, addr, vid, err);
3466 			break;
3467 		}
3468 		dsa_fdb_offload_notify(switchdev_work);
3469 		break;
3470 
3471 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3472 		if (switchdev_work->host_addr)
3473 			err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
3474 		else if (dp->lag)
3475 			err = dsa_port_lag_fdb_del(dp, addr, vid);
3476 		else
3477 			err = dsa_port_fdb_del(dp, addr, vid);
3478 		if (err) {
3479 			dev_err(ds->dev,
3480 				"port %d failed to delete %pM vid %d from fdb: %d\n",
3481 				dp->index, addr, vid, err);
3482 		}
3483 
3484 		break;
3485 	}
3486 
3487 	kfree(switchdev_work);
3488 }
3489 
3490 static bool dsa_foreign_dev_check(const struct net_device *dev,
3491 				  const struct net_device *foreign_dev)
3492 {
3493 	const struct dsa_port *dp = dsa_slave_to_port(dev);
3494 	struct dsa_switch_tree *dst = dp->ds->dst;
3495 
3496 	if (netif_is_bridge_master(foreign_dev))
3497 		return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
3498 
3499 	if (netif_is_bridge_port(foreign_dev))
3500 		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
3501 
3502 	/* Everything else is foreign */
3503 	return true;
3504 }
3505 
3506 static int dsa_slave_fdb_event(struct net_device *dev,
3507 			       struct net_device *orig_dev,
3508 			       unsigned long event, const void *ctx,
3509 			       const struct switchdev_notifier_fdb_info *fdb_info)
3510 {
3511 	struct dsa_switchdev_event_work *switchdev_work;
3512 	struct dsa_port *dp = dsa_slave_to_port(dev);
3513 	bool host_addr = fdb_info->is_local;
3514 	struct dsa_switch *ds = dp->ds;
3515 
3516 	if (ctx && ctx != dp)
3517 		return 0;
3518 
3519 	if (!dp->bridge)
3520 		return 0;
3521 
3522 	if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
3523 		if (dsa_port_offloads_bridge_port(dp, orig_dev))
3524 			return 0;
3525 
3526 		/* FDB entries learned by the software bridge or by foreign
3527 		 * bridge ports should be installed as host addresses only if
3528 		 * the driver requests assisted learning.
3529 		 */
3530 		if (!ds->assisted_learning_on_cpu_port)
3531 			return 0;
3532 	}
3533 
3534 	/* Also treat FDB entries on foreign interfaces bridged with us as host
3535 	 * addresses.
3536 	 */
3537 	if (dsa_foreign_dev_check(dev, orig_dev))
3538 		host_addr = true;
3539 
3540 	/* Check early that we're not doing work in vain.
3541 	 * Host addresses on LAG ports still require regular FDB ops,
3542 	 * since the CPU port isn't in a LAG.
3543 	 */
3544 	if (dp->lag && !host_addr) {
3545 		if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
3546 			return -EOPNOTSUPP;
3547 	} else {
3548 		if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
3549 			return -EOPNOTSUPP;
3550 	}
3551 
3552 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3553 	if (!switchdev_work)
3554 		return -ENOMEM;
3555 
3556 	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
3557 		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
3558 		   orig_dev->name, fdb_info->addr, fdb_info->vid,
3559 		   host_addr ? " as host address" : "");
3560 
3561 	INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
3562 	switchdev_work->event = event;
3563 	switchdev_work->dev = dev;
3564 	switchdev_work->orig_dev = orig_dev;
3565 
3566 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
3567 	switchdev_work->vid = fdb_info->vid;
3568 	switchdev_work->host_addr = host_addr;
3569 
3570 	dsa_schedule_work(&switchdev_work->work);
3571 
3572 	return 0;
3573 }
3574 
3575 /* Called under rcu_read_lock() */
3576 static int dsa_slave_switchdev_event(struct notifier_block *unused,
3577 				     unsigned long event, void *ptr)
3578 {
3579 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3580 	int err;
3581 
3582 	switch (event) {
3583 	case SWITCHDEV_PORT_ATTR_SET:
3584 		err = switchdev_handle_port_attr_set(dev, ptr,
3585 						     dsa_slave_dev_check,
3586 						     dsa_slave_port_attr_set);
3587 		return notifier_from_errno(err);
3588 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3589 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3590 		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
3591 							   dsa_slave_dev_check,
3592 							   dsa_foreign_dev_check,
3593 							   dsa_slave_fdb_event);
3594 		return notifier_from_errno(err);
3595 	default:
3596 		return NOTIFY_DONE;
3597 	}
3598 
3599 	return NOTIFY_OK;
3600 }
3601 
3602 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
3603 					      unsigned long event, void *ptr)
3604 {
3605 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3606 	int err;
3607 
3608 	switch (event) {
3609 	case SWITCHDEV_PORT_OBJ_ADD:
3610 		err = switchdev_handle_port_obj_add_foreign(dev, ptr,
3611 							    dsa_slave_dev_check,
3612 							    dsa_foreign_dev_check,
3613 							    dsa_slave_port_obj_add);
3614 		return notifier_from_errno(err);
3615 	case SWITCHDEV_PORT_OBJ_DEL:
3616 		err = switchdev_handle_port_obj_del_foreign(dev, ptr,
3617 							    dsa_slave_dev_check,
3618 							    dsa_foreign_dev_check,
3619 							    dsa_slave_port_obj_del);
3620 		return notifier_from_errno(err);
3621 	case SWITCHDEV_PORT_ATTR_SET:
3622 		err = switchdev_handle_port_attr_set(dev, ptr,
3623 						     dsa_slave_dev_check,
3624 						     dsa_slave_port_attr_set);
3625 		return notifier_from_errno(err);
3626 	}
3627 
3628 	return NOTIFY_DONE;
3629 }
3630 
3631 static struct notifier_block dsa_slave_nb __read_mostly = {
3632 	.notifier_call  = dsa_slave_netdevice_event,
3633 };
3634 
3635 struct notifier_block dsa_slave_switchdev_notifier = {
3636 	.notifier_call = dsa_slave_switchdev_event,
3637 };
3638 
3639 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
3640 	.notifier_call = dsa_slave_switchdev_blocking_event,
3641 };
3642 
3643 int dsa_slave_register_notifier(void)
3644 {
3645 	struct notifier_block *nb;
3646 	int err;
3647 
3648 	err = register_netdevice_notifier(&dsa_slave_nb);
3649 	if (err)
3650 		return err;
3651 
3652 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
3653 	if (err)
3654 		goto err_switchdev_nb;
3655 
3656 	nb = &dsa_slave_switchdev_blocking_notifier;
3657 	err = register_switchdev_blocking_notifier(nb);
3658 	if (err)
3659 		goto err_switchdev_blocking_nb;
3660 
3661 	return 0;
3662 
3663 err_switchdev_blocking_nb:
3664 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3665 err_switchdev_nb:
3666 	unregister_netdevice_notifier(&dsa_slave_nb);
3667 	return err;
3668 }
3669 
3670 void dsa_slave_unregister_notifier(void)
3671 {
3672 	struct notifier_block *nb;
3673 	int err;
3674 
3675 	nb = &dsa_slave_switchdev_blocking_notifier;
3676 	err = unregister_switchdev_blocking_notifier(nb);
3677 	if (err)
3678 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
3679 
3680 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3681 	if (err)
3682 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3683 
3684 	err = unregister_netdevice_notifier(&dsa_slave_nb);
3685 	if (err)
3686 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
3687 }
3688