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