xref: /openbmc/linux/net/dsa/slave.c (revision f8b8b1cd5aadd221742b45eb0ee3c8a80abf036a)
1 /*
2  * net/dsa/slave.c - Slave device handling
3  * Copyright (c) 2008-2009 Marvell Semiconductor
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10 
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/of_net.h>
17 #include <linux/of_mdio.h>
18 #include <linux/mdio.h>
19 #include <linux/list.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
25 
26 #include "dsa_priv.h"
27 
28 static bool dsa_slave_dev_check(struct net_device *dev);
29 
30 /* slave mii_bus handling ***************************************************/
31 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
32 {
33 	struct dsa_switch *ds = bus->priv;
34 
35 	if (ds->phys_mii_mask & (1 << addr))
36 		return ds->ops->phy_read(ds, addr, reg);
37 
38 	return 0xffff;
39 }
40 
41 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
42 {
43 	struct dsa_switch *ds = bus->priv;
44 
45 	if (ds->phys_mii_mask & (1 << addr))
46 		return ds->ops->phy_write(ds, addr, reg, val);
47 
48 	return 0;
49 }
50 
51 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
52 {
53 	ds->slave_mii_bus->priv = (void *)ds;
54 	ds->slave_mii_bus->name = "dsa slave smi";
55 	ds->slave_mii_bus->read = dsa_slave_phy_read;
56 	ds->slave_mii_bus->write = dsa_slave_phy_write;
57 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
58 		 ds->dst->tree, ds->index);
59 	ds->slave_mii_bus->parent = ds->dev;
60 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
61 }
62 
63 
64 /* slave device handling ****************************************************/
65 static int dsa_slave_get_iflink(const struct net_device *dev)
66 {
67 	return dsa_slave_to_master(dev)->ifindex;
68 }
69 
70 static int dsa_slave_open(struct net_device *dev)
71 {
72 	struct net_device *master = dsa_slave_to_master(dev);
73 	struct dsa_port *dp = dsa_slave_to_port(dev);
74 	int err;
75 
76 	if (!(master->flags & IFF_UP))
77 		return -ENETDOWN;
78 
79 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
80 		err = dev_uc_add(master, dev->dev_addr);
81 		if (err < 0)
82 			goto out;
83 	}
84 
85 	if (dev->flags & IFF_ALLMULTI) {
86 		err = dev_set_allmulti(master, 1);
87 		if (err < 0)
88 			goto del_unicast;
89 	}
90 	if (dev->flags & IFF_PROMISC) {
91 		err = dev_set_promiscuity(master, 1);
92 		if (err < 0)
93 			goto clear_allmulti;
94 	}
95 
96 	err = dsa_port_enable(dp, dev->phydev);
97 	if (err)
98 		goto clear_promisc;
99 
100 	if (dev->phydev)
101 		phy_start(dev->phydev);
102 
103 	return 0;
104 
105 clear_promisc:
106 	if (dev->flags & IFF_PROMISC)
107 		dev_set_promiscuity(master, -1);
108 clear_allmulti:
109 	if (dev->flags & IFF_ALLMULTI)
110 		dev_set_allmulti(master, -1);
111 del_unicast:
112 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
113 		dev_uc_del(master, dev->dev_addr);
114 out:
115 	return err;
116 }
117 
118 static int dsa_slave_close(struct net_device *dev)
119 {
120 	struct net_device *master = dsa_slave_to_master(dev);
121 	struct dsa_port *dp = dsa_slave_to_port(dev);
122 
123 	if (dev->phydev)
124 		phy_stop(dev->phydev);
125 
126 	dsa_port_disable(dp, dev->phydev);
127 
128 	dev_mc_unsync(master, dev);
129 	dev_uc_unsync(master, dev);
130 	if (dev->flags & IFF_ALLMULTI)
131 		dev_set_allmulti(master, -1);
132 	if (dev->flags & IFF_PROMISC)
133 		dev_set_promiscuity(master, -1);
134 
135 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
136 		dev_uc_del(master, dev->dev_addr);
137 
138 	return 0;
139 }
140 
141 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
142 {
143 	struct net_device *master = dsa_slave_to_master(dev);
144 
145 	if (change & IFF_ALLMULTI)
146 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
147 	if (change & IFF_PROMISC)
148 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
149 }
150 
151 static void dsa_slave_set_rx_mode(struct net_device *dev)
152 {
153 	struct net_device *master = dsa_slave_to_master(dev);
154 
155 	dev_mc_sync(master, dev);
156 	dev_uc_sync(master, dev);
157 }
158 
159 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
160 {
161 	struct net_device *master = dsa_slave_to_master(dev);
162 	struct sockaddr *addr = a;
163 	int err;
164 
165 	if (!is_valid_ether_addr(addr->sa_data))
166 		return -EADDRNOTAVAIL;
167 
168 	if (!(dev->flags & IFF_UP))
169 		goto out;
170 
171 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
172 		err = dev_uc_add(master, addr->sa_data);
173 		if (err < 0)
174 			return err;
175 	}
176 
177 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
178 		dev_uc_del(master, dev->dev_addr);
179 
180 out:
181 	ether_addr_copy(dev->dev_addr, addr->sa_data);
182 
183 	return 0;
184 }
185 
186 struct dsa_slave_dump_ctx {
187 	struct net_device *dev;
188 	struct sk_buff *skb;
189 	struct netlink_callback *cb;
190 	int idx;
191 };
192 
193 static int
194 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
195 			   bool is_static, void *data)
196 {
197 	struct dsa_slave_dump_ctx *dump = data;
198 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
199 	u32 seq = dump->cb->nlh->nlmsg_seq;
200 	struct nlmsghdr *nlh;
201 	struct ndmsg *ndm;
202 
203 	if (dump->idx < dump->cb->args[2])
204 		goto skip;
205 
206 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
207 			sizeof(*ndm), NLM_F_MULTI);
208 	if (!nlh)
209 		return -EMSGSIZE;
210 
211 	ndm = nlmsg_data(nlh);
212 	ndm->ndm_family  = AF_BRIDGE;
213 	ndm->ndm_pad1    = 0;
214 	ndm->ndm_pad2    = 0;
215 	ndm->ndm_flags   = NTF_SELF;
216 	ndm->ndm_type    = 0;
217 	ndm->ndm_ifindex = dump->dev->ifindex;
218 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
219 
220 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
221 		goto nla_put_failure;
222 
223 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
224 		goto nla_put_failure;
225 
226 	nlmsg_end(dump->skb, nlh);
227 
228 skip:
229 	dump->idx++;
230 	return 0;
231 
232 nla_put_failure:
233 	nlmsg_cancel(dump->skb, nlh);
234 	return -EMSGSIZE;
235 }
236 
237 static int
238 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
239 		   struct net_device *dev, struct net_device *filter_dev,
240 		   int *idx)
241 {
242 	struct dsa_port *dp = dsa_slave_to_port(dev);
243 	struct dsa_slave_dump_ctx dump = {
244 		.dev = dev,
245 		.skb = skb,
246 		.cb = cb,
247 		.idx = *idx,
248 	};
249 	int err;
250 
251 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
252 	*idx = dump.idx;
253 
254 	return err;
255 }
256 
257 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
258 {
259 	if (!dev->phydev)
260 		return -ENODEV;
261 
262 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
263 }
264 
265 static int dsa_slave_port_attr_set(struct net_device *dev,
266 				   const struct switchdev_attr *attr,
267 				   struct switchdev_trans *trans)
268 {
269 	struct dsa_port *dp = dsa_slave_to_port(dev);
270 	int ret;
271 
272 	switch (attr->id) {
273 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
274 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
275 		break;
276 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
277 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
278 					      trans);
279 		break;
280 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
281 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
282 		break;
283 	default:
284 		ret = -EOPNOTSUPP;
285 		break;
286 	}
287 
288 	return ret;
289 }
290 
291 static int dsa_slave_port_obj_add(struct net_device *dev,
292 				  const struct switchdev_obj *obj,
293 				  struct switchdev_trans *trans)
294 {
295 	struct dsa_port *dp = dsa_slave_to_port(dev);
296 	int err;
297 
298 	/* For the prepare phase, ensure the full set of changes is feasable in
299 	 * one go in order to signal a failure properly. If an operation is not
300 	 * supported, return -EOPNOTSUPP.
301 	 */
302 
303 	switch (obj->id) {
304 	case SWITCHDEV_OBJ_ID_PORT_MDB:
305 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
306 		break;
307 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
308 		err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
309 					trans);
310 		break;
311 	default:
312 		err = -EOPNOTSUPP;
313 		break;
314 	}
315 
316 	return err;
317 }
318 
319 static int dsa_slave_port_obj_del(struct net_device *dev,
320 				  const struct switchdev_obj *obj)
321 {
322 	struct dsa_port *dp = dsa_slave_to_port(dev);
323 	int err;
324 
325 	switch (obj->id) {
326 	case SWITCHDEV_OBJ_ID_PORT_MDB:
327 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
328 		break;
329 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
330 		err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
331 		break;
332 	default:
333 		err = -EOPNOTSUPP;
334 		break;
335 	}
336 
337 	return err;
338 }
339 
340 static int dsa_slave_port_attr_get(struct net_device *dev,
341 				   struct switchdev_attr *attr)
342 {
343 	struct dsa_port *dp = dsa_slave_to_port(dev);
344 	struct dsa_switch *ds = dp->ds;
345 
346 	switch (attr->id) {
347 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
348 		attr->u.ppid.id_len = sizeof(ds->index);
349 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
350 		break;
351 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
352 		attr->u.brport_flags_support = 0;
353 		break;
354 	default:
355 		return -EOPNOTSUPP;
356 	}
357 
358 	return 0;
359 }
360 
361 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
362 						     struct sk_buff *skb)
363 {
364 #ifdef CONFIG_NET_POLL_CONTROLLER
365 	struct dsa_slave_priv *p = netdev_priv(dev);
366 
367 	if (p->netpoll)
368 		netpoll_send_skb(p->netpoll, skb);
369 #else
370 	BUG();
371 #endif
372 	return NETDEV_TX_OK;
373 }
374 
375 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
376 {
377 	struct dsa_slave_priv *p = netdev_priv(dev);
378 	struct pcpu_sw_netstats *s;
379 	struct sk_buff *nskb;
380 
381 	s = this_cpu_ptr(p->stats64);
382 	u64_stats_update_begin(&s->syncp);
383 	s->tx_packets++;
384 	s->tx_bytes += skb->len;
385 	u64_stats_update_end(&s->syncp);
386 
387 	/* Transmit function may have to reallocate the original SKB,
388 	 * in which case it must have freed it. Only free it here on error.
389 	 */
390 	nskb = p->xmit(skb, dev);
391 	if (!nskb) {
392 		kfree_skb(skb);
393 		return NETDEV_TX_OK;
394 	}
395 
396 	/* SKB for netpoll still need to be mangled with the protocol-specific
397 	 * tag to be successfully transmitted
398 	 */
399 	if (unlikely(netpoll_tx_running(dev)))
400 		return dsa_slave_netpoll_send_skb(dev, nskb);
401 
402 	/* Queue the SKB for transmission on the parent interface, but
403 	 * do not modify its EtherType
404 	 */
405 	nskb->dev = dsa_slave_to_master(dev);
406 	dev_queue_xmit(nskb);
407 
408 	return NETDEV_TX_OK;
409 }
410 
411 /* ethtool operations *******************************************************/
412 
413 static void dsa_slave_get_drvinfo(struct net_device *dev,
414 				  struct ethtool_drvinfo *drvinfo)
415 {
416 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
417 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
418 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
419 }
420 
421 static int dsa_slave_get_regs_len(struct net_device *dev)
422 {
423 	struct dsa_port *dp = dsa_slave_to_port(dev);
424 	struct dsa_switch *ds = dp->ds;
425 
426 	if (ds->ops->get_regs_len)
427 		return ds->ops->get_regs_len(ds, dp->index);
428 
429 	return -EOPNOTSUPP;
430 }
431 
432 static void
433 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
434 {
435 	struct dsa_port *dp = dsa_slave_to_port(dev);
436 	struct dsa_switch *ds = dp->ds;
437 
438 	if (ds->ops->get_regs)
439 		ds->ops->get_regs(ds, dp->index, regs, _p);
440 }
441 
442 static u32 dsa_slave_get_link(struct net_device *dev)
443 {
444 	if (!dev->phydev)
445 		return -ENODEV;
446 
447 	genphy_update_link(dev->phydev);
448 
449 	return dev->phydev->link;
450 }
451 
452 static int dsa_slave_get_eeprom_len(struct net_device *dev)
453 {
454 	struct dsa_port *dp = dsa_slave_to_port(dev);
455 	struct dsa_switch *ds = dp->ds;
456 
457 	if (ds->cd && ds->cd->eeprom_len)
458 		return ds->cd->eeprom_len;
459 
460 	if (ds->ops->get_eeprom_len)
461 		return ds->ops->get_eeprom_len(ds);
462 
463 	return 0;
464 }
465 
466 static int dsa_slave_get_eeprom(struct net_device *dev,
467 				struct ethtool_eeprom *eeprom, u8 *data)
468 {
469 	struct dsa_port *dp = dsa_slave_to_port(dev);
470 	struct dsa_switch *ds = dp->ds;
471 
472 	if (ds->ops->get_eeprom)
473 		return ds->ops->get_eeprom(ds, eeprom, data);
474 
475 	return -EOPNOTSUPP;
476 }
477 
478 static int dsa_slave_set_eeprom(struct net_device *dev,
479 				struct ethtool_eeprom *eeprom, u8 *data)
480 {
481 	struct dsa_port *dp = dsa_slave_to_port(dev);
482 	struct dsa_switch *ds = dp->ds;
483 
484 	if (ds->ops->set_eeprom)
485 		return ds->ops->set_eeprom(ds, eeprom, data);
486 
487 	return -EOPNOTSUPP;
488 }
489 
490 static void dsa_slave_get_strings(struct net_device *dev,
491 				  uint32_t stringset, uint8_t *data)
492 {
493 	struct dsa_port *dp = dsa_slave_to_port(dev);
494 	struct dsa_switch *ds = dp->ds;
495 
496 	if (stringset == ETH_SS_STATS) {
497 		int len = ETH_GSTRING_LEN;
498 
499 		strncpy(data, "tx_packets", len);
500 		strncpy(data + len, "tx_bytes", len);
501 		strncpy(data + 2 * len, "rx_packets", len);
502 		strncpy(data + 3 * len, "rx_bytes", len);
503 		if (ds->ops->get_strings)
504 			ds->ops->get_strings(ds, dp->index, data + 4 * len);
505 	}
506 }
507 
508 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
509 					struct ethtool_stats *stats,
510 					uint64_t *data)
511 {
512 	struct dsa_port *dp = dsa_slave_to_port(dev);
513 	struct dsa_slave_priv *p = netdev_priv(dev);
514 	struct dsa_switch *ds = dp->ds;
515 	struct pcpu_sw_netstats *s;
516 	unsigned int start;
517 	int i;
518 
519 	for_each_possible_cpu(i) {
520 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
521 
522 		s = per_cpu_ptr(p->stats64, i);
523 		do {
524 			start = u64_stats_fetch_begin_irq(&s->syncp);
525 			tx_packets = s->tx_packets;
526 			tx_bytes = s->tx_bytes;
527 			rx_packets = s->rx_packets;
528 			rx_bytes = s->rx_bytes;
529 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
530 		data[0] += tx_packets;
531 		data[1] += tx_bytes;
532 		data[2] += rx_packets;
533 		data[3] += rx_bytes;
534 	}
535 	if (ds->ops->get_ethtool_stats)
536 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
537 }
538 
539 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
540 {
541 	struct dsa_port *dp = dsa_slave_to_port(dev);
542 	struct dsa_switch *ds = dp->ds;
543 
544 	if (sset == ETH_SS_STATS) {
545 		int count;
546 
547 		count = 4;
548 		if (ds->ops->get_sset_count)
549 			count += ds->ops->get_sset_count(ds);
550 
551 		return count;
552 	}
553 
554 	return -EOPNOTSUPP;
555 }
556 
557 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
558 {
559 	struct dsa_port *dp = dsa_slave_to_port(dev);
560 	struct dsa_switch *ds = dp->ds;
561 
562 	if (ds->ops->get_wol)
563 		ds->ops->get_wol(ds, dp->index, w);
564 }
565 
566 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
567 {
568 	struct dsa_port *dp = dsa_slave_to_port(dev);
569 	struct dsa_switch *ds = dp->ds;
570 	int ret = -EOPNOTSUPP;
571 
572 	if (ds->ops->set_wol)
573 		ret = ds->ops->set_wol(ds, dp->index, w);
574 
575 	return ret;
576 }
577 
578 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
579 {
580 	struct dsa_port *dp = dsa_slave_to_port(dev);
581 	struct dsa_switch *ds = dp->ds;
582 	int ret;
583 
584 	/* Port's PHY and MAC both need to be EEE capable */
585 	if (!dev->phydev)
586 		return -ENODEV;
587 
588 	if (!ds->ops->set_mac_eee)
589 		return -EOPNOTSUPP;
590 
591 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
592 	if (ret)
593 		return ret;
594 
595 	if (e->eee_enabled) {
596 		ret = phy_init_eee(dev->phydev, 0);
597 		if (ret)
598 			return ret;
599 	}
600 
601 	return phy_ethtool_set_eee(dev->phydev, e);
602 }
603 
604 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
605 {
606 	struct dsa_port *dp = dsa_slave_to_port(dev);
607 	struct dsa_switch *ds = dp->ds;
608 	int ret;
609 
610 	/* Port's PHY and MAC both need to be EEE capable */
611 	if (!dev->phydev)
612 		return -ENODEV;
613 
614 	if (!ds->ops->get_mac_eee)
615 		return -EOPNOTSUPP;
616 
617 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
618 	if (ret)
619 		return ret;
620 
621 	return phy_ethtool_get_eee(dev->phydev, e);
622 }
623 
624 #ifdef CONFIG_NET_POLL_CONTROLLER
625 static int dsa_slave_netpoll_setup(struct net_device *dev,
626 				   struct netpoll_info *ni)
627 {
628 	struct net_device *master = dsa_slave_to_master(dev);
629 	struct dsa_slave_priv *p = netdev_priv(dev);
630 	struct netpoll *netpoll;
631 	int err = 0;
632 
633 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
634 	if (!netpoll)
635 		return -ENOMEM;
636 
637 	err = __netpoll_setup(netpoll, master);
638 	if (err) {
639 		kfree(netpoll);
640 		goto out;
641 	}
642 
643 	p->netpoll = netpoll;
644 out:
645 	return err;
646 }
647 
648 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
649 {
650 	struct dsa_slave_priv *p = netdev_priv(dev);
651 	struct netpoll *netpoll = p->netpoll;
652 
653 	if (!netpoll)
654 		return;
655 
656 	p->netpoll = NULL;
657 
658 	__netpoll_free_async(netpoll);
659 }
660 
661 static void dsa_slave_poll_controller(struct net_device *dev)
662 {
663 }
664 #endif
665 
666 static int dsa_slave_get_phys_port_name(struct net_device *dev,
667 					char *name, size_t len)
668 {
669 	struct dsa_port *dp = dsa_slave_to_port(dev);
670 
671 	if (snprintf(name, len, "p%d", dp->index) >= len)
672 		return -EINVAL;
673 
674 	return 0;
675 }
676 
677 static struct dsa_mall_tc_entry *
678 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
679 {
680 	struct dsa_slave_priv *p = netdev_priv(dev);
681 	struct dsa_mall_tc_entry *mall_tc_entry;
682 
683 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
684 		if (mall_tc_entry->cookie == cookie)
685 			return mall_tc_entry;
686 
687 	return NULL;
688 }
689 
690 static int dsa_slave_add_cls_matchall(struct net_device *dev,
691 				      struct tc_cls_matchall_offload *cls,
692 				      bool ingress)
693 {
694 	struct dsa_port *dp = dsa_slave_to_port(dev);
695 	struct dsa_slave_priv *p = netdev_priv(dev);
696 	struct dsa_mall_tc_entry *mall_tc_entry;
697 	__be16 protocol = cls->common.protocol;
698 	struct net *net = dev_net(dev);
699 	struct dsa_switch *ds = dp->ds;
700 	struct net_device *to_dev;
701 	const struct tc_action *a;
702 	struct dsa_port *to_dp;
703 	int err = -EOPNOTSUPP;
704 	LIST_HEAD(actions);
705 	int ifindex;
706 
707 	if (!ds->ops->port_mirror_add)
708 		return err;
709 
710 	if (!tcf_exts_has_one_action(cls->exts))
711 		return err;
712 
713 	tcf_exts_to_list(cls->exts, &actions);
714 	a = list_first_entry(&actions, struct tc_action, list);
715 
716 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
717 		struct dsa_mall_mirror_tc_entry *mirror;
718 
719 		ifindex = tcf_mirred_ifindex(a);
720 		to_dev = __dev_get_by_index(net, ifindex);
721 		if (!to_dev)
722 			return -EINVAL;
723 
724 		if (!dsa_slave_dev_check(to_dev))
725 			return -EOPNOTSUPP;
726 
727 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
728 		if (!mall_tc_entry)
729 			return -ENOMEM;
730 
731 		mall_tc_entry->cookie = cls->cookie;
732 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
733 		mirror = &mall_tc_entry->mirror;
734 
735 		to_dp = dsa_slave_to_port(to_dev);
736 
737 		mirror->to_local_port = to_dp->index;
738 		mirror->ingress = ingress;
739 
740 		err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
741 		if (err) {
742 			kfree(mall_tc_entry);
743 			return err;
744 		}
745 
746 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
747 	}
748 
749 	return 0;
750 }
751 
752 static void dsa_slave_del_cls_matchall(struct net_device *dev,
753 				       struct tc_cls_matchall_offload *cls)
754 {
755 	struct dsa_port *dp = dsa_slave_to_port(dev);
756 	struct dsa_mall_tc_entry *mall_tc_entry;
757 	struct dsa_switch *ds = dp->ds;
758 
759 	if (!ds->ops->port_mirror_del)
760 		return;
761 
762 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
763 	if (!mall_tc_entry)
764 		return;
765 
766 	list_del(&mall_tc_entry->list);
767 
768 	switch (mall_tc_entry->type) {
769 	case DSA_PORT_MALL_MIRROR:
770 		ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
771 		break;
772 	default:
773 		WARN_ON(1);
774 	}
775 
776 	kfree(mall_tc_entry);
777 }
778 
779 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
780 					   struct tc_cls_matchall_offload *cls)
781 {
782 	bool ingress;
783 
784 	if (is_classid_clsact_ingress(cls->common.classid))
785 		ingress = true;
786 	else if (is_classid_clsact_egress(cls->common.classid))
787 		ingress = false;
788 	else
789 		return -EOPNOTSUPP;
790 
791 	if (cls->common.chain_index)
792 		return -EOPNOTSUPP;
793 
794 	switch (cls->command) {
795 	case TC_CLSMATCHALL_REPLACE:
796 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
797 	case TC_CLSMATCHALL_DESTROY:
798 		dsa_slave_del_cls_matchall(dev, cls);
799 		return 0;
800 	default:
801 		return -EOPNOTSUPP;
802 	}
803 }
804 
805 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
806 			      void *type_data)
807 {
808 	switch (type) {
809 	case TC_SETUP_CLSMATCHALL:
810 		return dsa_slave_setup_tc_cls_matchall(dev, type_data);
811 	default:
812 		return -EOPNOTSUPP;
813 	}
814 }
815 
816 static void dsa_slave_get_stats64(struct net_device *dev,
817 				  struct rtnl_link_stats64 *stats)
818 {
819 	struct dsa_slave_priv *p = netdev_priv(dev);
820 	struct pcpu_sw_netstats *s;
821 	unsigned int start;
822 	int i;
823 
824 	netdev_stats_to_stats64(stats, &dev->stats);
825 	for_each_possible_cpu(i) {
826 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
827 
828 		s = per_cpu_ptr(p->stats64, i);
829 		do {
830 			start = u64_stats_fetch_begin_irq(&s->syncp);
831 			tx_packets = s->tx_packets;
832 			tx_bytes = s->tx_bytes;
833 			rx_packets = s->rx_packets;
834 			rx_bytes = s->rx_bytes;
835 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
836 
837 		stats->tx_packets += tx_packets;
838 		stats->tx_bytes += tx_bytes;
839 		stats->rx_packets += rx_packets;
840 		stats->rx_bytes += rx_bytes;
841 	}
842 }
843 
844 static int dsa_slave_get_rxnfc(struct net_device *dev,
845 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
846 {
847 	struct dsa_port *dp = dsa_slave_to_port(dev);
848 	struct dsa_switch *ds = dp->ds;
849 
850 	if (!ds->ops->get_rxnfc)
851 		return -EOPNOTSUPP;
852 
853 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
854 }
855 
856 static int dsa_slave_set_rxnfc(struct net_device *dev,
857 			       struct ethtool_rxnfc *nfc)
858 {
859 	struct dsa_port *dp = dsa_slave_to_port(dev);
860 	struct dsa_switch *ds = dp->ds;
861 
862 	if (!ds->ops->set_rxnfc)
863 		return -EOPNOTSUPP;
864 
865 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
866 }
867 
868 static const struct ethtool_ops dsa_slave_ethtool_ops = {
869 	.get_drvinfo		= dsa_slave_get_drvinfo,
870 	.get_regs_len		= dsa_slave_get_regs_len,
871 	.get_regs		= dsa_slave_get_regs,
872 	.nway_reset		= phy_ethtool_nway_reset,
873 	.get_link		= dsa_slave_get_link,
874 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
875 	.get_eeprom		= dsa_slave_get_eeprom,
876 	.set_eeprom		= dsa_slave_set_eeprom,
877 	.get_strings		= dsa_slave_get_strings,
878 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
879 	.get_sset_count		= dsa_slave_get_sset_count,
880 	.set_wol		= dsa_slave_set_wol,
881 	.get_wol		= dsa_slave_get_wol,
882 	.set_eee		= dsa_slave_set_eee,
883 	.get_eee		= dsa_slave_get_eee,
884 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
885 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
886 	.get_rxnfc		= dsa_slave_get_rxnfc,
887 	.set_rxnfc		= dsa_slave_set_rxnfc,
888 };
889 
890 static const struct net_device_ops dsa_slave_netdev_ops = {
891 	.ndo_open	 	= dsa_slave_open,
892 	.ndo_stop		= dsa_slave_close,
893 	.ndo_start_xmit		= dsa_slave_xmit,
894 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
895 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
896 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
897 	.ndo_fdb_add		= dsa_legacy_fdb_add,
898 	.ndo_fdb_del		= dsa_legacy_fdb_del,
899 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
900 	.ndo_do_ioctl		= dsa_slave_ioctl,
901 	.ndo_get_iflink		= dsa_slave_get_iflink,
902 #ifdef CONFIG_NET_POLL_CONTROLLER
903 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
904 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
905 	.ndo_poll_controller	= dsa_slave_poll_controller,
906 #endif
907 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
908 	.ndo_setup_tc		= dsa_slave_setup_tc,
909 	.ndo_get_stats64	= dsa_slave_get_stats64,
910 };
911 
912 static const struct switchdev_ops dsa_slave_switchdev_ops = {
913 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
914 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
915 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
916 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
917 };
918 
919 static struct device_type dsa_type = {
920 	.name	= "dsa",
921 };
922 
923 static void dsa_slave_adjust_link(struct net_device *dev)
924 {
925 	struct dsa_port *dp = dsa_slave_to_port(dev);
926 	struct dsa_slave_priv *p = netdev_priv(dev);
927 	struct dsa_switch *ds = dp->ds;
928 	unsigned int status_changed = 0;
929 
930 	if (p->old_link != dev->phydev->link) {
931 		status_changed = 1;
932 		p->old_link = dev->phydev->link;
933 	}
934 
935 	if (p->old_duplex != dev->phydev->duplex) {
936 		status_changed = 1;
937 		p->old_duplex = dev->phydev->duplex;
938 	}
939 
940 	if (p->old_pause != dev->phydev->pause) {
941 		status_changed = 1;
942 		p->old_pause = dev->phydev->pause;
943 	}
944 
945 	if (ds->ops->adjust_link && status_changed)
946 		ds->ops->adjust_link(ds, dp->index, dev->phydev);
947 
948 	if (status_changed)
949 		phy_print_status(dev->phydev);
950 }
951 
952 static int dsa_slave_fixed_link_update(struct net_device *dev,
953 				       struct fixed_phy_status *status)
954 {
955 	struct dsa_switch *ds;
956 	struct dsa_port *dp;
957 
958 	if (dev) {
959 		dp = dsa_slave_to_port(dev);
960 		ds = dp->ds;
961 		if (ds->ops->fixed_link_update)
962 			ds->ops->fixed_link_update(ds, dp->index, status);
963 	}
964 
965 	return 0;
966 }
967 
968 /* slave device setup *******************************************************/
969 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
970 {
971 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
972 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
973 	struct dsa_switch *ds = dp->ds;
974 
975 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
976 	if (!slave_dev->phydev) {
977 		netdev_err(slave_dev, "no phy at %d\n", addr);
978 		return -ENODEV;
979 	}
980 
981 	/* Use already configured phy mode */
982 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
983 		p->phy_interface = slave_dev->phydev->interface;
984 
985 	return phy_connect_direct(slave_dev, slave_dev->phydev,
986 				  dsa_slave_adjust_link, p->phy_interface);
987 }
988 
989 static int dsa_slave_phy_setup(struct net_device *slave_dev)
990 {
991 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
992 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
993 	struct device_node *port_dn = dp->dn;
994 	struct dsa_switch *ds = dp->ds;
995 	struct device_node *phy_dn;
996 	bool phy_is_fixed = false;
997 	u32 phy_flags = 0;
998 	int mode, ret;
999 
1000 	mode = of_get_phy_mode(port_dn);
1001 	if (mode < 0)
1002 		mode = PHY_INTERFACE_MODE_NA;
1003 	p->phy_interface = mode;
1004 
1005 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1006 	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1007 		/* In the case of a fixed PHY, the DT node associated
1008 		 * to the fixed PHY is the Port DT node
1009 		 */
1010 		ret = of_phy_register_fixed_link(port_dn);
1011 		if (ret) {
1012 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1013 			return ret;
1014 		}
1015 		phy_is_fixed = true;
1016 		phy_dn = of_node_get(port_dn);
1017 	}
1018 
1019 	if (ds->ops->get_phy_flags)
1020 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1021 
1022 	if (phy_dn) {
1023 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1024 
1025 		/* If this PHY address is part of phys_mii_mask, which means
1026 		 * that we need to divert reads and writes to/from it, then we
1027 		 * want to bind this device using the slave MII bus created by
1028 		 * DSA to make that happen.
1029 		 */
1030 		if (!phy_is_fixed && phy_id >= 0 &&
1031 		    (ds->phys_mii_mask & (1 << phy_id))) {
1032 			ret = dsa_slave_phy_connect(slave_dev, phy_id);
1033 			if (ret) {
1034 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1035 				of_node_put(phy_dn);
1036 				return ret;
1037 			}
1038 		} else {
1039 			slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1040 							   dsa_slave_adjust_link,
1041 							   phy_flags,
1042 							   p->phy_interface);
1043 		}
1044 
1045 		of_node_put(phy_dn);
1046 	}
1047 
1048 	if (slave_dev->phydev && phy_is_fixed)
1049 		fixed_phy_set_link_update(slave_dev->phydev,
1050 					  dsa_slave_fixed_link_update);
1051 
1052 	/* We could not connect to a designated PHY, so use the switch internal
1053 	 * MDIO bus instead
1054 	 */
1055 	if (!slave_dev->phydev) {
1056 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1057 		if (ret) {
1058 			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1059 				   dp->index, ret);
1060 			if (phy_is_fixed)
1061 				of_phy_deregister_fixed_link(port_dn);
1062 			return ret;
1063 		}
1064 	}
1065 
1066 	phy_attached_info(slave_dev->phydev);
1067 
1068 	return 0;
1069 }
1070 
1071 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1072 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1073 					    struct netdev_queue *txq,
1074 					    void *_unused)
1075 {
1076 	lockdep_set_class(&txq->_xmit_lock,
1077 			  &dsa_slave_netdev_xmit_lock_key);
1078 }
1079 
1080 int dsa_slave_suspend(struct net_device *slave_dev)
1081 {
1082 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1083 
1084 	netif_device_detach(slave_dev);
1085 
1086 	if (slave_dev->phydev) {
1087 		phy_stop(slave_dev->phydev);
1088 		p->old_pause = -1;
1089 		p->old_link = -1;
1090 		p->old_duplex = -1;
1091 		phy_suspend(slave_dev->phydev);
1092 	}
1093 
1094 	return 0;
1095 }
1096 
1097 int dsa_slave_resume(struct net_device *slave_dev)
1098 {
1099 	netif_device_attach(slave_dev);
1100 
1101 	if (slave_dev->phydev) {
1102 		phy_resume(slave_dev->phydev);
1103 		phy_start(slave_dev->phydev);
1104 	}
1105 
1106 	return 0;
1107 }
1108 
1109 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1110 {
1111 	struct net_device *master = dsa_slave_to_master(dev);
1112 	struct dsa_port *dp = dsa_slave_to_port(dev);
1113 	struct dsa_notifier_register_info rinfo = {
1114 		.switch_number = dp->ds->index,
1115 		.port_number = dp->index,
1116 		.master = master,
1117 		.info.dev = dev,
1118 	};
1119 
1120 	call_dsa_notifiers(val, dev, &rinfo.info);
1121 }
1122 
1123 int dsa_slave_create(struct dsa_port *port, const char *name)
1124 {
1125 	struct dsa_port *cpu_dp = port->cpu_dp;
1126 	struct net_device *master = cpu_dp->master;
1127 	struct dsa_switch *ds = port->ds;
1128 	struct net_device *slave_dev;
1129 	struct dsa_slave_priv *p;
1130 	int ret;
1131 
1132 	if (!ds->num_tx_queues)
1133 		ds->num_tx_queues = 1;
1134 
1135 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1136 				     NET_NAME_UNKNOWN, ether_setup,
1137 				     ds->num_tx_queues, 1);
1138 	if (slave_dev == NULL)
1139 		return -ENOMEM;
1140 
1141 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1142 	slave_dev->hw_features |= NETIF_F_HW_TC;
1143 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1144 	eth_hw_addr_inherit(slave_dev, master);
1145 	slave_dev->priv_flags |= IFF_NO_QUEUE;
1146 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1147 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1148 	slave_dev->min_mtu = 0;
1149 	slave_dev->max_mtu = ETH_MAX_MTU;
1150 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1151 
1152 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1153 				 NULL);
1154 
1155 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
1156 	slave_dev->dev.of_node = port->dn;
1157 	slave_dev->vlan_features = master->vlan_features;
1158 
1159 	p = netdev_priv(slave_dev);
1160 	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1161 	if (!p->stats64) {
1162 		free_netdev(slave_dev);
1163 		return -ENOMEM;
1164 	}
1165 	p->dp = port;
1166 	INIT_LIST_HEAD(&p->mall_tc_list);
1167 	p->xmit = cpu_dp->tag_ops->xmit;
1168 
1169 	p->old_pause = -1;
1170 	p->old_link = -1;
1171 	p->old_duplex = -1;
1172 
1173 	port->slave = slave_dev;
1174 
1175 	netif_carrier_off(slave_dev);
1176 
1177 	ret = dsa_slave_phy_setup(slave_dev);
1178 	if (ret) {
1179 		netdev_err(master, "error %d setting up slave phy\n", ret);
1180 		goto out_free;
1181 	}
1182 
1183 	dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1184 
1185 	ret = register_netdev(slave_dev);
1186 	if (ret) {
1187 		netdev_err(master, "error %d registering interface %s\n",
1188 			   ret, slave_dev->name);
1189 		goto out_phy;
1190 	}
1191 
1192 	return 0;
1193 
1194 out_phy:
1195 	phy_disconnect(slave_dev->phydev);
1196 	if (of_phy_is_fixed_link(port->dn))
1197 		of_phy_deregister_fixed_link(port->dn);
1198 out_free:
1199 	free_percpu(p->stats64);
1200 	free_netdev(slave_dev);
1201 	port->slave = NULL;
1202 	return ret;
1203 }
1204 
1205 void dsa_slave_destroy(struct net_device *slave_dev)
1206 {
1207 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1208 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1209 	struct device_node *port_dn = dp->dn;
1210 
1211 	netif_carrier_off(slave_dev);
1212 	if (slave_dev->phydev) {
1213 		phy_disconnect(slave_dev->phydev);
1214 
1215 		if (of_phy_is_fixed_link(port_dn))
1216 			of_phy_deregister_fixed_link(port_dn);
1217 	}
1218 	dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1219 	unregister_netdev(slave_dev);
1220 	free_percpu(p->stats64);
1221 	free_netdev(slave_dev);
1222 }
1223 
1224 static bool dsa_slave_dev_check(struct net_device *dev)
1225 {
1226 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1227 }
1228 
1229 static int dsa_slave_changeupper(struct net_device *dev,
1230 				 struct netdev_notifier_changeupper_info *info)
1231 {
1232 	struct dsa_port *dp = dsa_slave_to_port(dev);
1233 	int err = NOTIFY_DONE;
1234 
1235 	if (netif_is_bridge_master(info->upper_dev)) {
1236 		if (info->linking) {
1237 			err = dsa_port_bridge_join(dp, info->upper_dev);
1238 			err = notifier_from_errno(err);
1239 		} else {
1240 			dsa_port_bridge_leave(dp, info->upper_dev);
1241 			err = NOTIFY_OK;
1242 		}
1243 	}
1244 
1245 	return err;
1246 }
1247 
1248 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1249 				     unsigned long event, void *ptr)
1250 {
1251 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1252 
1253 	if (!dsa_slave_dev_check(dev))
1254 		return NOTIFY_DONE;
1255 
1256 	if (event == NETDEV_CHANGEUPPER)
1257 		return dsa_slave_changeupper(dev, ptr);
1258 
1259 	return NOTIFY_DONE;
1260 }
1261 
1262 struct dsa_switchdev_event_work {
1263 	struct work_struct work;
1264 	struct switchdev_notifier_fdb_info fdb_info;
1265 	struct net_device *dev;
1266 	unsigned long event;
1267 };
1268 
1269 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1270 {
1271 	struct dsa_switchdev_event_work *switchdev_work =
1272 		container_of(work, struct dsa_switchdev_event_work, work);
1273 	struct net_device *dev = switchdev_work->dev;
1274 	struct switchdev_notifier_fdb_info *fdb_info;
1275 	struct dsa_port *dp = dsa_slave_to_port(dev);
1276 	int err;
1277 
1278 	rtnl_lock();
1279 	switch (switchdev_work->event) {
1280 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1281 		fdb_info = &switchdev_work->fdb_info;
1282 		err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1283 		if (err) {
1284 			netdev_dbg(dev, "fdb add failed err=%d\n", err);
1285 			break;
1286 		}
1287 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1288 					 &fdb_info->info);
1289 		break;
1290 
1291 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1292 		fdb_info = &switchdev_work->fdb_info;
1293 		err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1294 		if (err) {
1295 			netdev_dbg(dev, "fdb del failed err=%d\n", err);
1296 			dev_close(dev);
1297 		}
1298 		break;
1299 	}
1300 	rtnl_unlock();
1301 
1302 	kfree(switchdev_work->fdb_info.addr);
1303 	kfree(switchdev_work);
1304 	dev_put(dev);
1305 }
1306 
1307 static int
1308 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1309 				  switchdev_work,
1310 				  const struct switchdev_notifier_fdb_info *
1311 				  fdb_info)
1312 {
1313 	memcpy(&switchdev_work->fdb_info, fdb_info,
1314 	       sizeof(switchdev_work->fdb_info));
1315 	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1316 	if (!switchdev_work->fdb_info.addr)
1317 		return -ENOMEM;
1318 	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1319 			fdb_info->addr);
1320 	return 0;
1321 }
1322 
1323 /* Called under rcu_read_lock() */
1324 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1325 				     unsigned long event, void *ptr)
1326 {
1327 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1328 	struct dsa_switchdev_event_work *switchdev_work;
1329 
1330 	if (!dsa_slave_dev_check(dev))
1331 		return NOTIFY_DONE;
1332 
1333 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1334 	if (!switchdev_work)
1335 		return NOTIFY_BAD;
1336 
1337 	INIT_WORK(&switchdev_work->work,
1338 		  dsa_slave_switchdev_event_work);
1339 	switchdev_work->dev = dev;
1340 	switchdev_work->event = event;
1341 
1342 	switch (event) {
1343 	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1344 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1345 		if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1346 						      ptr))
1347 			goto err_fdb_work_init;
1348 		dev_hold(dev);
1349 		break;
1350 	default:
1351 		kfree(switchdev_work);
1352 		return NOTIFY_DONE;
1353 	}
1354 
1355 	dsa_schedule_work(&switchdev_work->work);
1356 	return NOTIFY_OK;
1357 
1358 err_fdb_work_init:
1359 	kfree(switchdev_work);
1360 	return NOTIFY_BAD;
1361 }
1362 
1363 static struct notifier_block dsa_slave_nb __read_mostly = {
1364 	.notifier_call  = dsa_slave_netdevice_event,
1365 };
1366 
1367 static struct notifier_block dsa_slave_switchdev_notifier = {
1368 	.notifier_call = dsa_slave_switchdev_event,
1369 };
1370 
1371 int dsa_slave_register_notifier(void)
1372 {
1373 	int err;
1374 
1375 	err = register_netdevice_notifier(&dsa_slave_nb);
1376 	if (err)
1377 		return err;
1378 
1379 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1380 	if (err)
1381 		goto err_switchdev_nb;
1382 
1383 	return 0;
1384 
1385 err_switchdev_nb:
1386 	unregister_netdevice_notifier(&dsa_slave_nb);
1387 	return err;
1388 }
1389 
1390 void dsa_slave_unregister_notifier(void)
1391 {
1392 	int err;
1393 
1394 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1395 	if (err)
1396 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1397 
1398 	err = unregister_netdevice_notifier(&dsa_slave_nb);
1399 	if (err)
1400 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1401 }
1402