x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / net / dsa / slave.c
blobb14d530a32b1cc236ed2d60d4d979b7b80d4032d
1 /*
2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
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 */
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>
26 #include "dsa_priv.h"
28 static bool dsa_slave_dev_check(struct net_device *dev);
30 /* slave mii_bus handling ***************************************************/
31 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
33 struct dsa_switch *ds = bus->priv;
35 if (ds->phys_mii_mask & (1 << addr))
36 return ds->ops->phy_read(ds, addr, reg);
38 return 0xffff;
41 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
43 struct dsa_switch *ds = bus->priv;
45 if (ds->phys_mii_mask & (1 << addr))
46 return ds->ops->phy_write(ds, addr, reg, val);
48 return 0;
51 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
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;
64 /* slave device handling ****************************************************/
65 static int dsa_slave_get_iflink(const struct net_device *dev)
67 struct dsa_slave_priv *p = netdev_priv(dev);
69 return dsa_master_netdev(p)->ifindex;
72 static int dsa_slave_open(struct net_device *dev)
74 struct dsa_slave_priv *p = netdev_priv(dev);
75 struct dsa_port *dp = p->dp;
76 struct dsa_switch *ds = dp->ds;
77 struct net_device *master = dsa_master_netdev(p);
78 u8 stp_state = dp->bridge_dev ? BR_STATE_BLOCKING : BR_STATE_FORWARDING;
79 int err;
81 if (!(master->flags & IFF_UP))
82 return -ENETDOWN;
84 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
85 err = dev_uc_add(master, dev->dev_addr);
86 if (err < 0)
87 goto out;
90 if (dev->flags & IFF_ALLMULTI) {
91 err = dev_set_allmulti(master, 1);
92 if (err < 0)
93 goto del_unicast;
95 if (dev->flags & IFF_PROMISC) {
96 err = dev_set_promiscuity(master, 1);
97 if (err < 0)
98 goto clear_allmulti;
101 if (ds->ops->port_enable) {
102 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
103 if (err)
104 goto clear_promisc;
107 dsa_port_set_state_now(p->dp, stp_state);
109 if (p->phy)
110 phy_start(p->phy);
112 return 0;
114 clear_promisc:
115 if (dev->flags & IFF_PROMISC)
116 dev_set_promiscuity(master, -1);
117 clear_allmulti:
118 if (dev->flags & IFF_ALLMULTI)
119 dev_set_allmulti(master, -1);
120 del_unicast:
121 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
122 dev_uc_del(master, dev->dev_addr);
123 out:
124 return err;
127 static int dsa_slave_close(struct net_device *dev)
129 struct dsa_slave_priv *p = netdev_priv(dev);
130 struct net_device *master = dsa_master_netdev(p);
131 struct dsa_switch *ds = p->dp->ds;
133 if (p->phy)
134 phy_stop(p->phy);
136 dev_mc_unsync(master, dev);
137 dev_uc_unsync(master, dev);
138 if (dev->flags & IFF_ALLMULTI)
139 dev_set_allmulti(master, -1);
140 if (dev->flags & IFF_PROMISC)
141 dev_set_promiscuity(master, -1);
143 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
144 dev_uc_del(master, dev->dev_addr);
146 if (ds->ops->port_disable)
147 ds->ops->port_disable(ds, p->dp->index, p->phy);
149 dsa_port_set_state_now(p->dp, BR_STATE_DISABLED);
151 return 0;
154 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
156 struct dsa_slave_priv *p = netdev_priv(dev);
157 struct net_device *master = dsa_master_netdev(p);
159 if (dev->flags & IFF_UP) {
160 if (change & IFF_ALLMULTI)
161 dev_set_allmulti(master,
162 dev->flags & IFF_ALLMULTI ? 1 : -1);
163 if (change & IFF_PROMISC)
164 dev_set_promiscuity(master,
165 dev->flags & IFF_PROMISC ? 1 : -1);
169 static void dsa_slave_set_rx_mode(struct net_device *dev)
171 struct dsa_slave_priv *p = netdev_priv(dev);
172 struct net_device *master = dsa_master_netdev(p);
174 dev_mc_sync(master, dev);
175 dev_uc_sync(master, dev);
178 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
180 struct dsa_slave_priv *p = netdev_priv(dev);
181 struct net_device *master = dsa_master_netdev(p);
182 struct sockaddr *addr = a;
183 int err;
185 if (!is_valid_ether_addr(addr->sa_data))
186 return -EADDRNOTAVAIL;
188 if (!(dev->flags & IFF_UP))
189 goto out;
191 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
192 err = dev_uc_add(master, addr->sa_data);
193 if (err < 0)
194 return err;
197 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
198 dev_uc_del(master, dev->dev_addr);
200 out:
201 ether_addr_copy(dev->dev_addr, addr->sa_data);
203 return 0;
206 struct dsa_slave_dump_ctx {
207 struct net_device *dev;
208 struct sk_buff *skb;
209 struct netlink_callback *cb;
210 int idx;
213 static int
214 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
215 bool is_static, void *data)
217 struct dsa_slave_dump_ctx *dump = data;
218 u32 portid = NETLINK_CB(dump->cb->skb).portid;
219 u32 seq = dump->cb->nlh->nlmsg_seq;
220 struct nlmsghdr *nlh;
221 struct ndmsg *ndm;
223 if (dump->idx < dump->cb->args[2])
224 goto skip;
226 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
227 sizeof(*ndm), NLM_F_MULTI);
228 if (!nlh)
229 return -EMSGSIZE;
231 ndm = nlmsg_data(nlh);
232 ndm->ndm_family = AF_BRIDGE;
233 ndm->ndm_pad1 = 0;
234 ndm->ndm_pad2 = 0;
235 ndm->ndm_flags = NTF_SELF;
236 ndm->ndm_type = 0;
237 ndm->ndm_ifindex = dump->dev->ifindex;
238 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
240 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
241 goto nla_put_failure;
243 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
244 goto nla_put_failure;
246 nlmsg_end(dump->skb, nlh);
248 skip:
249 dump->idx++;
250 return 0;
252 nla_put_failure:
253 nlmsg_cancel(dump->skb, nlh);
254 return -EMSGSIZE;
257 static int
258 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
259 struct net_device *dev, struct net_device *filter_dev,
260 int *idx)
262 struct dsa_slave_dump_ctx dump = {
263 .dev = dev,
264 .skb = skb,
265 .cb = cb,
266 .idx = *idx,
268 struct dsa_slave_priv *p = netdev_priv(dev);
269 struct dsa_port *dp = p->dp;
270 struct dsa_switch *ds = dp->ds;
271 int err;
273 if (!ds->ops->port_fdb_dump)
274 return -EOPNOTSUPP;
276 err = ds->ops->port_fdb_dump(ds, dp->index,
277 dsa_slave_port_fdb_do_dump,
278 &dump);
279 *idx = dump.idx;
280 return err;
283 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
285 struct dsa_slave_priv *p = netdev_priv(dev);
287 if (p->phy != NULL)
288 return phy_mii_ioctl(p->phy, ifr, cmd);
290 return -EOPNOTSUPP;
293 static int dsa_slave_port_attr_set(struct net_device *dev,
294 const struct switchdev_attr *attr,
295 struct switchdev_trans *trans)
297 struct dsa_slave_priv *p = netdev_priv(dev);
298 struct dsa_port *dp = p->dp;
299 int ret;
301 switch (attr->id) {
302 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
303 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
304 break;
305 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
306 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
307 trans);
308 break;
309 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
310 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
311 break;
312 default:
313 ret = -EOPNOTSUPP;
314 break;
317 return ret;
320 static int dsa_slave_port_obj_add(struct net_device *dev,
321 const struct switchdev_obj *obj,
322 struct switchdev_trans *trans)
324 struct dsa_slave_priv *p = netdev_priv(dev);
325 struct dsa_port *dp = p->dp;
326 int err;
328 /* For the prepare phase, ensure the full set of changes is feasable in
329 * one go in order to signal a failure properly. If an operation is not
330 * supported, return -EOPNOTSUPP.
333 switch (obj->id) {
334 case SWITCHDEV_OBJ_ID_PORT_MDB:
335 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
336 break;
337 case SWITCHDEV_OBJ_ID_PORT_VLAN:
338 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
339 trans);
340 break;
341 default:
342 err = -EOPNOTSUPP;
343 break;
346 return err;
349 static int dsa_slave_port_obj_del(struct net_device *dev,
350 const struct switchdev_obj *obj)
352 struct dsa_slave_priv *p = netdev_priv(dev);
353 struct dsa_port *dp = p->dp;
354 int err;
356 switch (obj->id) {
357 case SWITCHDEV_OBJ_ID_PORT_MDB:
358 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
359 break;
360 case SWITCHDEV_OBJ_ID_PORT_VLAN:
361 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
362 break;
363 default:
364 err = -EOPNOTSUPP;
365 break;
368 return err;
371 static int dsa_slave_port_attr_get(struct net_device *dev,
372 struct switchdev_attr *attr)
374 struct dsa_slave_priv *p = netdev_priv(dev);
375 struct dsa_switch *ds = p->dp->ds;
377 switch (attr->id) {
378 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
379 attr->u.ppid.id_len = sizeof(ds->index);
380 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
381 break;
382 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
383 attr->u.brport_flags_support = 0;
384 break;
385 default:
386 return -EOPNOTSUPP;
389 return 0;
392 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
393 struct sk_buff *skb)
395 #ifdef CONFIG_NET_POLL_CONTROLLER
396 if (p->netpoll)
397 netpoll_send_skb(p->netpoll, skb);
398 #else
399 BUG();
400 #endif
401 return NETDEV_TX_OK;
404 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
406 struct dsa_slave_priv *p = netdev_priv(dev);
407 struct pcpu_sw_netstats *s;
408 struct sk_buff *nskb;
410 s = this_cpu_ptr(p->stats64);
411 u64_stats_update_begin(&s->syncp);
412 s->tx_packets++;
413 s->tx_bytes += skb->len;
414 u64_stats_update_end(&s->syncp);
416 /* Transmit function may have to reallocate the original SKB,
417 * in which case it must have freed it. Only free it here on error.
419 nskb = p->xmit(skb, dev);
420 if (!nskb) {
421 kfree_skb(skb);
422 return NETDEV_TX_OK;
425 /* SKB for netpoll still need to be mangled with the protocol-specific
426 * tag to be successfully transmitted
428 if (unlikely(netpoll_tx_running(dev)))
429 return dsa_netpoll_send_skb(p, nskb);
431 /* Queue the SKB for transmission on the parent interface, but
432 * do not modify its EtherType
434 nskb->dev = dsa_master_netdev(p);
435 dev_queue_xmit(nskb);
437 return NETDEV_TX_OK;
440 /* ethtool operations *******************************************************/
441 static int
442 dsa_slave_get_link_ksettings(struct net_device *dev,
443 struct ethtool_link_ksettings *cmd)
445 struct dsa_slave_priv *p = netdev_priv(dev);
447 if (!p->phy)
448 return -EOPNOTSUPP;
450 phy_ethtool_ksettings_get(p->phy, cmd);
452 return 0;
455 static int
456 dsa_slave_set_link_ksettings(struct net_device *dev,
457 const struct ethtool_link_ksettings *cmd)
459 struct dsa_slave_priv *p = netdev_priv(dev);
461 if (p->phy != NULL)
462 return phy_ethtool_ksettings_set(p->phy, cmd);
464 return -EOPNOTSUPP;
467 static void dsa_slave_get_drvinfo(struct net_device *dev,
468 struct ethtool_drvinfo *drvinfo)
470 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
471 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
472 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
475 static int dsa_slave_get_regs_len(struct net_device *dev)
477 struct dsa_slave_priv *p = netdev_priv(dev);
478 struct dsa_switch *ds = p->dp->ds;
480 if (ds->ops->get_regs_len)
481 return ds->ops->get_regs_len(ds, p->dp->index);
483 return -EOPNOTSUPP;
486 static void
487 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
489 struct dsa_slave_priv *p = netdev_priv(dev);
490 struct dsa_switch *ds = p->dp->ds;
492 if (ds->ops->get_regs)
493 ds->ops->get_regs(ds, p->dp->index, regs, _p);
496 static int dsa_slave_nway_reset(struct net_device *dev)
498 struct dsa_slave_priv *p = netdev_priv(dev);
500 if (p->phy != NULL)
501 return genphy_restart_aneg(p->phy);
503 return -EOPNOTSUPP;
506 static u32 dsa_slave_get_link(struct net_device *dev)
508 struct dsa_slave_priv *p = netdev_priv(dev);
510 if (p->phy != NULL) {
511 genphy_update_link(p->phy);
512 return p->phy->link;
515 return -EOPNOTSUPP;
518 static int dsa_slave_get_eeprom_len(struct net_device *dev)
520 struct dsa_slave_priv *p = netdev_priv(dev);
521 struct dsa_switch *ds = p->dp->ds;
523 if (ds->cd && ds->cd->eeprom_len)
524 return ds->cd->eeprom_len;
526 if (ds->ops->get_eeprom_len)
527 return ds->ops->get_eeprom_len(ds);
529 return 0;
532 static int dsa_slave_get_eeprom(struct net_device *dev,
533 struct ethtool_eeprom *eeprom, u8 *data)
535 struct dsa_slave_priv *p = netdev_priv(dev);
536 struct dsa_switch *ds = p->dp->ds;
538 if (ds->ops->get_eeprom)
539 return ds->ops->get_eeprom(ds, eeprom, data);
541 return -EOPNOTSUPP;
544 static int dsa_slave_set_eeprom(struct net_device *dev,
545 struct ethtool_eeprom *eeprom, u8 *data)
547 struct dsa_slave_priv *p = netdev_priv(dev);
548 struct dsa_switch *ds = p->dp->ds;
550 if (ds->ops->set_eeprom)
551 return ds->ops->set_eeprom(ds, eeprom, data);
553 return -EOPNOTSUPP;
556 static void dsa_slave_get_strings(struct net_device *dev,
557 uint32_t stringset, uint8_t *data)
559 struct dsa_slave_priv *p = netdev_priv(dev);
560 struct dsa_switch *ds = p->dp->ds;
562 if (stringset == ETH_SS_STATS) {
563 int len = ETH_GSTRING_LEN;
565 strncpy(data, "tx_packets", len);
566 strncpy(data + len, "tx_bytes", len);
567 strncpy(data + 2 * len, "rx_packets", len);
568 strncpy(data + 3 * len, "rx_bytes", len);
569 if (ds->ops->get_strings)
570 ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
574 static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
575 struct ethtool_stats *stats,
576 uint64_t *data)
578 struct dsa_switch_tree *dst = dev->dsa_ptr;
579 struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
580 struct dsa_switch *ds = cpu_dp->ds;
581 s8 cpu_port = cpu_dp->index;
582 int count = 0;
584 if (cpu_dp->ethtool_ops.get_sset_count) {
585 count = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS);
586 cpu_dp->ethtool_ops.get_ethtool_stats(dev, stats, data);
589 if (ds->ops->get_ethtool_stats)
590 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
593 static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
595 struct dsa_switch_tree *dst = dev->dsa_ptr;
596 struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
597 struct dsa_switch *ds = cpu_dp->ds;
598 int count = 0;
600 if (cpu_dp->ethtool_ops.get_sset_count)
601 count += cpu_dp->ethtool_ops.get_sset_count(dev, sset);
603 if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
604 count += ds->ops->get_sset_count(ds);
606 return count;
609 static void dsa_cpu_port_get_strings(struct net_device *dev,
610 uint32_t stringset, uint8_t *data)
612 struct dsa_switch_tree *dst = dev->dsa_ptr;
613 struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
614 struct dsa_switch *ds = cpu_dp->ds;
615 s8 cpu_port = cpu_dp->index;
616 int len = ETH_GSTRING_LEN;
617 int mcount = 0, count;
618 unsigned int i;
619 uint8_t pfx[4];
620 uint8_t *ndata;
622 snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
623 /* We do not want to be NULL-terminated, since this is a prefix */
624 pfx[sizeof(pfx) - 1] = '_';
626 if (cpu_dp->ethtool_ops.get_sset_count) {
627 mcount = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS);
628 cpu_dp->ethtool_ops.get_strings(dev, stringset, data);
631 if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
632 ndata = data + mcount * len;
633 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
634 * the output after to prepend our CPU port prefix we
635 * constructed earlier
637 ds->ops->get_strings(ds, cpu_port, ndata);
638 count = ds->ops->get_sset_count(ds);
639 for (i = 0; i < count; i++) {
640 memmove(ndata + (i * len + sizeof(pfx)),
641 ndata + i * len, len - sizeof(pfx));
642 memcpy(ndata + i * len, pfx, sizeof(pfx));
647 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
648 struct ethtool_stats *stats,
649 uint64_t *data)
651 struct dsa_slave_priv *p = netdev_priv(dev);
652 struct dsa_switch *ds = p->dp->ds;
653 struct pcpu_sw_netstats *s;
654 unsigned int start;
655 int i;
657 for_each_possible_cpu(i) {
658 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
660 s = per_cpu_ptr(p->stats64, i);
661 do {
662 start = u64_stats_fetch_begin_irq(&s->syncp);
663 tx_packets = s->tx_packets;
664 tx_bytes = s->tx_bytes;
665 rx_packets = s->rx_packets;
666 rx_bytes = s->rx_bytes;
667 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
668 data[0] += tx_packets;
669 data[1] += tx_bytes;
670 data[2] += rx_packets;
671 data[3] += rx_bytes;
673 if (ds->ops->get_ethtool_stats)
674 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
677 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
679 struct dsa_slave_priv *p = netdev_priv(dev);
680 struct dsa_switch *ds = p->dp->ds;
682 if (sset == ETH_SS_STATS) {
683 int count;
685 count = 4;
686 if (ds->ops->get_sset_count)
687 count += ds->ops->get_sset_count(ds);
689 return count;
692 return -EOPNOTSUPP;
695 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
697 struct dsa_slave_priv *p = netdev_priv(dev);
698 struct dsa_switch *ds = p->dp->ds;
700 if (ds->ops->get_wol)
701 ds->ops->get_wol(ds, p->dp->index, w);
704 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
706 struct dsa_slave_priv *p = netdev_priv(dev);
707 struct dsa_switch *ds = p->dp->ds;
708 int ret = -EOPNOTSUPP;
710 if (ds->ops->set_wol)
711 ret = ds->ops->set_wol(ds, p->dp->index, w);
713 return ret;
716 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
718 struct dsa_slave_priv *p = netdev_priv(dev);
719 struct dsa_switch *ds = p->dp->ds;
720 int ret;
722 /* Port's PHY and MAC both need to be EEE capable */
723 if (!p->phy)
724 return -ENODEV;
726 if (!ds->ops->set_mac_eee)
727 return -EOPNOTSUPP;
729 ret = ds->ops->set_mac_eee(ds, p->dp->index, e);
730 if (ret)
731 return ret;
733 if (e->eee_enabled) {
734 ret = phy_init_eee(p->phy, 0);
735 if (ret)
736 return ret;
739 return phy_ethtool_set_eee(p->phy, e);
742 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
744 struct dsa_slave_priv *p = netdev_priv(dev);
745 struct dsa_switch *ds = p->dp->ds;
746 int ret;
748 /* Port's PHY and MAC both need to be EEE capable */
749 if (!p->phy)
750 return -ENODEV;
752 if (!ds->ops->get_mac_eee)
753 return -EOPNOTSUPP;
755 ret = ds->ops->get_mac_eee(ds, p->dp->index, e);
756 if (ret)
757 return ret;
759 return phy_ethtool_get_eee(p->phy, e);
762 #ifdef CONFIG_NET_POLL_CONTROLLER
763 static int dsa_slave_netpoll_setup(struct net_device *dev,
764 struct netpoll_info *ni)
766 struct dsa_slave_priv *p = netdev_priv(dev);
767 struct net_device *master = dsa_master_netdev(p);
768 struct netpoll *netpoll;
769 int err = 0;
771 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
772 if (!netpoll)
773 return -ENOMEM;
775 err = __netpoll_setup(netpoll, master);
776 if (err) {
777 kfree(netpoll);
778 goto out;
781 p->netpoll = netpoll;
782 out:
783 return err;
786 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
788 struct dsa_slave_priv *p = netdev_priv(dev);
789 struct netpoll *netpoll = p->netpoll;
791 if (!netpoll)
792 return;
794 p->netpoll = NULL;
796 __netpoll_free_async(netpoll);
799 static void dsa_slave_poll_controller(struct net_device *dev)
802 #endif
804 static int dsa_slave_get_phys_port_name(struct net_device *dev,
805 char *name, size_t len)
807 struct dsa_slave_priv *p = netdev_priv(dev);
809 if (snprintf(name, len, "p%d", p->dp->index) >= len)
810 return -EINVAL;
812 return 0;
815 static struct dsa_mall_tc_entry *
816 dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
817 unsigned long cookie)
819 struct dsa_mall_tc_entry *mall_tc_entry;
821 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
822 if (mall_tc_entry->cookie == cookie)
823 return mall_tc_entry;
825 return NULL;
828 static int dsa_slave_add_cls_matchall(struct net_device *dev,
829 struct tc_cls_matchall_offload *cls,
830 bool ingress)
832 struct dsa_slave_priv *p = netdev_priv(dev);
833 struct dsa_mall_tc_entry *mall_tc_entry;
834 __be16 protocol = cls->common.protocol;
835 struct dsa_switch *ds = p->dp->ds;
836 struct net *net = dev_net(dev);
837 struct dsa_slave_priv *to_p;
838 struct net_device *to_dev;
839 const struct tc_action *a;
840 int err = -EOPNOTSUPP;
841 LIST_HEAD(actions);
842 int ifindex;
844 if (!ds->ops->port_mirror_add)
845 return err;
847 if (!tcf_exts_has_one_action(cls->exts))
848 return err;
850 tcf_exts_to_list(cls->exts, &actions);
851 a = list_first_entry(&actions, struct tc_action, list);
853 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
854 struct dsa_mall_mirror_tc_entry *mirror;
856 ifindex = tcf_mirred_ifindex(a);
857 to_dev = __dev_get_by_index(net, ifindex);
858 if (!to_dev)
859 return -EINVAL;
861 if (!dsa_slave_dev_check(to_dev))
862 return -EOPNOTSUPP;
864 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
865 if (!mall_tc_entry)
866 return -ENOMEM;
868 mall_tc_entry->cookie = cls->cookie;
869 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
870 mirror = &mall_tc_entry->mirror;
872 to_p = netdev_priv(to_dev);
874 mirror->to_local_port = to_p->dp->index;
875 mirror->ingress = ingress;
877 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
878 ingress);
879 if (err) {
880 kfree(mall_tc_entry);
881 return err;
884 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
887 return 0;
890 static void dsa_slave_del_cls_matchall(struct net_device *dev,
891 struct tc_cls_matchall_offload *cls)
893 struct dsa_slave_priv *p = netdev_priv(dev);
894 struct dsa_mall_tc_entry *mall_tc_entry;
895 struct dsa_switch *ds = p->dp->ds;
897 if (!ds->ops->port_mirror_del)
898 return;
900 mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
901 if (!mall_tc_entry)
902 return;
904 list_del(&mall_tc_entry->list);
906 switch (mall_tc_entry->type) {
907 case DSA_PORT_MALL_MIRROR:
908 ds->ops->port_mirror_del(ds, p->dp->index,
909 &mall_tc_entry->mirror);
910 break;
911 default:
912 WARN_ON(1);
915 kfree(mall_tc_entry);
918 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
919 struct tc_cls_matchall_offload *cls)
921 bool ingress;
923 if (is_classid_clsact_ingress(cls->common.classid))
924 ingress = true;
925 else if (is_classid_clsact_egress(cls->common.classid))
926 ingress = false;
927 else
928 return -EOPNOTSUPP;
930 if (cls->common.chain_index)
931 return -EOPNOTSUPP;
933 switch (cls->command) {
934 case TC_CLSMATCHALL_REPLACE:
935 return dsa_slave_add_cls_matchall(dev, cls, ingress);
936 case TC_CLSMATCHALL_DESTROY:
937 dsa_slave_del_cls_matchall(dev, cls);
938 return 0;
939 default:
940 return -EOPNOTSUPP;
944 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
945 void *type_data)
947 switch (type) {
948 case TC_SETUP_CLSMATCHALL:
949 return dsa_slave_setup_tc_cls_matchall(dev, type_data);
950 default:
951 return -EOPNOTSUPP;
955 static void dsa_slave_get_stats64(struct net_device *dev,
956 struct rtnl_link_stats64 *stats)
958 struct dsa_slave_priv *p = netdev_priv(dev);
959 struct pcpu_sw_netstats *s;
960 unsigned int start;
961 int i;
963 netdev_stats_to_stats64(stats, &dev->stats);
964 for_each_possible_cpu(i) {
965 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
967 s = per_cpu_ptr(p->stats64, i);
968 do {
969 start = u64_stats_fetch_begin_irq(&s->syncp);
970 tx_packets = s->tx_packets;
971 tx_bytes = s->tx_bytes;
972 rx_packets = s->rx_packets;
973 rx_bytes = s->rx_bytes;
974 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
976 stats->tx_packets += tx_packets;
977 stats->tx_bytes += tx_bytes;
978 stats->rx_packets += rx_packets;
979 stats->rx_bytes += rx_bytes;
983 void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
985 ops->get_sset_count = dsa_cpu_port_get_sset_count;
986 ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
987 ops->get_strings = dsa_cpu_port_get_strings;
990 static int dsa_slave_get_rxnfc(struct net_device *dev,
991 struct ethtool_rxnfc *nfc, u32 *rule_locs)
993 struct dsa_slave_priv *p = netdev_priv(dev);
994 struct dsa_switch *ds = p->dp->ds;
996 if (!ds->ops->get_rxnfc)
997 return -EOPNOTSUPP;
999 return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1002 static int dsa_slave_set_rxnfc(struct net_device *dev,
1003 struct ethtool_rxnfc *nfc)
1005 struct dsa_slave_priv *p = netdev_priv(dev);
1006 struct dsa_switch *ds = p->dp->ds;
1008 if (!ds->ops->set_rxnfc)
1009 return -EOPNOTSUPP;
1011 return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1014 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1015 .get_drvinfo = dsa_slave_get_drvinfo,
1016 .get_regs_len = dsa_slave_get_regs_len,
1017 .get_regs = dsa_slave_get_regs,
1018 .nway_reset = dsa_slave_nway_reset,
1019 .get_link = dsa_slave_get_link,
1020 .get_eeprom_len = dsa_slave_get_eeprom_len,
1021 .get_eeprom = dsa_slave_get_eeprom,
1022 .set_eeprom = dsa_slave_set_eeprom,
1023 .get_strings = dsa_slave_get_strings,
1024 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1025 .get_sset_count = dsa_slave_get_sset_count,
1026 .set_wol = dsa_slave_set_wol,
1027 .get_wol = dsa_slave_get_wol,
1028 .set_eee = dsa_slave_set_eee,
1029 .get_eee = dsa_slave_get_eee,
1030 .get_link_ksettings = dsa_slave_get_link_ksettings,
1031 .set_link_ksettings = dsa_slave_set_link_ksettings,
1032 .get_rxnfc = dsa_slave_get_rxnfc,
1033 .set_rxnfc = dsa_slave_set_rxnfc,
1036 static const struct net_device_ops dsa_slave_netdev_ops = {
1037 .ndo_open = dsa_slave_open,
1038 .ndo_stop = dsa_slave_close,
1039 .ndo_start_xmit = dsa_slave_xmit,
1040 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1041 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1042 .ndo_set_mac_address = dsa_slave_set_mac_address,
1043 .ndo_fdb_add = dsa_legacy_fdb_add,
1044 .ndo_fdb_del = dsa_legacy_fdb_del,
1045 .ndo_fdb_dump = dsa_slave_fdb_dump,
1046 .ndo_do_ioctl = dsa_slave_ioctl,
1047 .ndo_get_iflink = dsa_slave_get_iflink,
1048 #ifdef CONFIG_NET_POLL_CONTROLLER
1049 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1050 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1051 .ndo_poll_controller = dsa_slave_poll_controller,
1052 #endif
1053 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1054 .ndo_setup_tc = dsa_slave_setup_tc,
1055 .ndo_get_stats64 = dsa_slave_get_stats64,
1058 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1059 .switchdev_port_attr_get = dsa_slave_port_attr_get,
1060 .switchdev_port_attr_set = dsa_slave_port_attr_set,
1061 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1062 .switchdev_port_obj_del = dsa_slave_port_obj_del,
1065 static struct device_type dsa_type = {
1066 .name = "dsa",
1069 static void dsa_slave_adjust_link(struct net_device *dev)
1071 struct dsa_slave_priv *p = netdev_priv(dev);
1072 struct dsa_switch *ds = p->dp->ds;
1073 unsigned int status_changed = 0;
1075 if (p->old_link != p->phy->link) {
1076 status_changed = 1;
1077 p->old_link = p->phy->link;
1080 if (p->old_duplex != p->phy->duplex) {
1081 status_changed = 1;
1082 p->old_duplex = p->phy->duplex;
1085 if (p->old_pause != p->phy->pause) {
1086 status_changed = 1;
1087 p->old_pause = p->phy->pause;
1090 if (ds->ops->adjust_link && status_changed)
1091 ds->ops->adjust_link(ds, p->dp->index, p->phy);
1093 if (status_changed)
1094 phy_print_status(p->phy);
1097 static int dsa_slave_fixed_link_update(struct net_device *dev,
1098 struct fixed_phy_status *status)
1100 struct dsa_slave_priv *p;
1101 struct dsa_switch *ds;
1103 if (dev) {
1104 p = netdev_priv(dev);
1105 ds = p->dp->ds;
1106 if (ds->ops->fixed_link_update)
1107 ds->ops->fixed_link_update(ds, p->dp->index, status);
1110 return 0;
1113 /* slave device setup *******************************************************/
1114 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1115 struct net_device *slave_dev,
1116 int addr)
1118 struct dsa_switch *ds = p->dp->ds;
1120 p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1121 if (!p->phy) {
1122 netdev_err(slave_dev, "no phy at %d\n", addr);
1123 return -ENODEV;
1126 /* Use already configured phy mode */
1127 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1128 p->phy_interface = p->phy->interface;
1129 return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1130 p->phy_interface);
1133 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1134 struct net_device *slave_dev)
1136 struct dsa_switch *ds = p->dp->ds;
1137 struct device_node *phy_dn, *port_dn;
1138 bool phy_is_fixed = false;
1139 u32 phy_flags = 0;
1140 int mode, ret;
1142 port_dn = p->dp->dn;
1143 mode = of_get_phy_mode(port_dn);
1144 if (mode < 0)
1145 mode = PHY_INTERFACE_MODE_NA;
1146 p->phy_interface = mode;
1148 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1149 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1150 /* In the case of a fixed PHY, the DT node associated
1151 * to the fixed PHY is the Port DT node
1153 ret = of_phy_register_fixed_link(port_dn);
1154 if (ret) {
1155 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1156 return ret;
1158 phy_is_fixed = true;
1159 phy_dn = of_node_get(port_dn);
1162 if (ds->ops->get_phy_flags)
1163 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
1165 if (phy_dn) {
1166 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1168 /* If this PHY address is part of phys_mii_mask, which means
1169 * that we need to divert reads and writes to/from it, then we
1170 * want to bind this device using the slave MII bus created by
1171 * DSA to make that happen.
1173 if (!phy_is_fixed && phy_id >= 0 &&
1174 (ds->phys_mii_mask & (1 << phy_id))) {
1175 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1176 if (ret) {
1177 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1178 of_node_put(phy_dn);
1179 return ret;
1181 } else {
1182 p->phy = of_phy_connect(slave_dev, phy_dn,
1183 dsa_slave_adjust_link,
1184 phy_flags,
1185 p->phy_interface);
1188 of_node_put(phy_dn);
1191 if (p->phy && phy_is_fixed)
1192 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1194 /* We could not connect to a designated PHY, so use the switch internal
1195 * MDIO bus instead
1197 if (!p->phy) {
1198 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1199 if (ret) {
1200 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1201 p->dp->index, ret);
1202 if (phy_is_fixed)
1203 of_phy_deregister_fixed_link(port_dn);
1204 return ret;
1208 phy_attached_info(p->phy);
1210 return 0;
1213 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1214 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1215 struct netdev_queue *txq,
1216 void *_unused)
1218 lockdep_set_class(&txq->_xmit_lock,
1219 &dsa_slave_netdev_xmit_lock_key);
1222 int dsa_slave_suspend(struct net_device *slave_dev)
1224 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1226 if (!netif_running(slave_dev))
1227 return 0;
1229 netif_device_detach(slave_dev);
1231 if (p->phy) {
1232 phy_stop(p->phy);
1233 p->old_pause = -1;
1234 p->old_link = -1;
1235 p->old_duplex = -1;
1236 phy_suspend(p->phy);
1239 return 0;
1242 int dsa_slave_resume(struct net_device *slave_dev)
1244 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1246 if (!netif_running(slave_dev))
1247 return 0;
1249 netif_device_attach(slave_dev);
1251 if (p->phy) {
1252 phy_resume(p->phy);
1253 phy_start(p->phy);
1256 return 0;
1259 int dsa_slave_create(struct dsa_port *port, const char *name)
1261 struct dsa_switch *ds = port->ds;
1262 struct dsa_switch_tree *dst = ds->dst;
1263 struct net_device *master;
1264 struct net_device *slave_dev;
1265 struct dsa_slave_priv *p;
1266 struct dsa_port *cpu_dp;
1267 int ret;
1269 cpu_dp = ds->dst->cpu_dp;
1270 master = cpu_dp->netdev;
1272 if (!ds->num_tx_queues)
1273 ds->num_tx_queues = 1;
1275 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1276 NET_NAME_UNKNOWN, ether_setup,
1277 ds->num_tx_queues, 1);
1278 if (slave_dev == NULL)
1279 return -ENOMEM;
1281 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1282 slave_dev->hw_features |= NETIF_F_HW_TC;
1283 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1284 eth_hw_addr_inherit(slave_dev, master);
1285 slave_dev->priv_flags |= IFF_NO_QUEUE;
1286 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1287 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1288 slave_dev->min_mtu = 0;
1289 slave_dev->max_mtu = ETH_MAX_MTU;
1290 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1292 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1293 NULL);
1295 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1296 slave_dev->dev.of_node = port->dn;
1297 slave_dev->vlan_features = master->vlan_features;
1299 p = netdev_priv(slave_dev);
1300 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1301 if (!p->stats64) {
1302 free_netdev(slave_dev);
1303 return -ENOMEM;
1305 p->dp = port;
1306 INIT_LIST_HEAD(&p->mall_tc_list);
1307 p->xmit = dst->tag_ops->xmit;
1309 p->old_pause = -1;
1310 p->old_link = -1;
1311 p->old_duplex = -1;
1313 port->netdev = slave_dev;
1315 netif_carrier_off(slave_dev);
1317 ret = dsa_slave_phy_setup(p, slave_dev);
1318 if (ret) {
1319 netdev_err(master, "error %d setting up slave phy\n", ret);
1320 goto out_free;
1323 ret = register_netdev(slave_dev);
1324 if (ret) {
1325 netdev_err(master, "error %d registering interface %s\n",
1326 ret, slave_dev->name);
1327 goto out_phy;
1330 return 0;
1332 out_phy:
1333 phy_disconnect(p->phy);
1334 if (of_phy_is_fixed_link(p->dp->dn))
1335 of_phy_deregister_fixed_link(p->dp->dn);
1336 out_free:
1337 free_percpu(p->stats64);
1338 free_netdev(slave_dev);
1339 port->netdev = NULL;
1340 return ret;
1343 void dsa_slave_destroy(struct net_device *slave_dev)
1345 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1346 struct device_node *port_dn;
1348 port_dn = p->dp->dn;
1350 netif_carrier_off(slave_dev);
1351 if (p->phy) {
1352 phy_disconnect(p->phy);
1354 if (of_phy_is_fixed_link(port_dn))
1355 of_phy_deregister_fixed_link(port_dn);
1357 unregister_netdev(slave_dev);
1358 free_percpu(p->stats64);
1359 free_netdev(slave_dev);
1362 static bool dsa_slave_dev_check(struct net_device *dev)
1364 return dev->netdev_ops == &dsa_slave_netdev_ops;
1367 static int dsa_slave_changeupper(struct net_device *dev,
1368 struct netdev_notifier_changeupper_info *info)
1370 struct dsa_slave_priv *p = netdev_priv(dev);
1371 struct dsa_port *dp = p->dp;
1372 int err = NOTIFY_DONE;
1374 if (netif_is_bridge_master(info->upper_dev)) {
1375 if (info->linking) {
1376 err = dsa_port_bridge_join(dp, info->upper_dev);
1377 err = notifier_from_errno(err);
1378 } else {
1379 dsa_port_bridge_leave(dp, info->upper_dev);
1380 err = NOTIFY_OK;
1384 return err;
1387 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1388 unsigned long event, void *ptr)
1390 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1392 if (dev->netdev_ops != &dsa_slave_netdev_ops)
1393 return NOTIFY_DONE;
1395 if (event == NETDEV_CHANGEUPPER)
1396 return dsa_slave_changeupper(dev, ptr);
1398 return NOTIFY_DONE;
1401 struct dsa_switchdev_event_work {
1402 struct work_struct work;
1403 struct switchdev_notifier_fdb_info fdb_info;
1404 struct net_device *dev;
1405 unsigned long event;
1408 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1410 struct dsa_switchdev_event_work *switchdev_work =
1411 container_of(work, struct dsa_switchdev_event_work, work);
1412 struct net_device *dev = switchdev_work->dev;
1413 struct switchdev_notifier_fdb_info *fdb_info;
1414 struct dsa_slave_priv *p = netdev_priv(dev);
1415 int err;
1417 rtnl_lock();
1418 switch (switchdev_work->event) {
1419 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1420 fdb_info = &switchdev_work->fdb_info;
1421 err = dsa_port_fdb_add(p->dp, fdb_info->addr, fdb_info->vid);
1422 if (err) {
1423 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1424 break;
1426 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1427 &fdb_info->info);
1428 break;
1430 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1431 fdb_info = &switchdev_work->fdb_info;
1432 err = dsa_port_fdb_del(p->dp, fdb_info->addr, fdb_info->vid);
1433 if (err) {
1434 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1435 dev_close(dev);
1437 break;
1439 rtnl_unlock();
1441 kfree(switchdev_work->fdb_info.addr);
1442 kfree(switchdev_work);
1443 dev_put(dev);
1446 static int
1447 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1448 switchdev_work,
1449 const struct switchdev_notifier_fdb_info *
1450 fdb_info)
1452 memcpy(&switchdev_work->fdb_info, fdb_info,
1453 sizeof(switchdev_work->fdb_info));
1454 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1455 if (!switchdev_work->fdb_info.addr)
1456 return -ENOMEM;
1457 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1458 fdb_info->addr);
1459 return 0;
1462 /* Called under rcu_read_lock() */
1463 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1464 unsigned long event, void *ptr)
1466 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1467 struct dsa_switchdev_event_work *switchdev_work;
1469 if (!dsa_slave_dev_check(dev))
1470 return NOTIFY_DONE;
1472 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1473 if (!switchdev_work)
1474 return NOTIFY_BAD;
1476 INIT_WORK(&switchdev_work->work,
1477 dsa_slave_switchdev_event_work);
1478 switchdev_work->dev = dev;
1479 switchdev_work->event = event;
1481 switch (event) {
1482 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1483 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1484 if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1485 ptr))
1486 goto err_fdb_work_init;
1487 dev_hold(dev);
1488 break;
1489 default:
1490 kfree(switchdev_work);
1491 return NOTIFY_DONE;
1494 dsa_schedule_work(&switchdev_work->work);
1495 return NOTIFY_OK;
1497 err_fdb_work_init:
1498 kfree(switchdev_work);
1499 return NOTIFY_BAD;
1502 static struct notifier_block dsa_slave_nb __read_mostly = {
1503 .notifier_call = dsa_slave_netdevice_event,
1506 static struct notifier_block dsa_slave_switchdev_notifier = {
1507 .notifier_call = dsa_slave_switchdev_event,
1510 int dsa_slave_register_notifier(void)
1512 int err;
1514 err = register_netdevice_notifier(&dsa_slave_nb);
1515 if (err)
1516 return err;
1518 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1519 if (err)
1520 goto err_switchdev_nb;
1522 return 0;
1524 err_switchdev_nb:
1525 unregister_netdevice_notifier(&dsa_slave_nb);
1526 return err;
1529 void dsa_slave_unregister_notifier(void)
1531 int err;
1533 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1534 if (err)
1535 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1537 err = unregister_netdevice_notifier(&dsa_slave_nb);
1538 if (err)
1539 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);