OSDN Git Service

switchdev: Add fdb.added_by_user to switchdev notifications
[uclinux-h8/linux.git] / net / dsa / slave.c
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 <net/rtnetlink.h>
20 #include <net/pkt_cls.h>
21 #include <net/tc_act/tc_mirred.h>
22 #include <linux/if_bridge.h>
23 #include <linux/netpoll.h>
24 #include <linux/ptp_classify.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->index, 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         struct dsa_slave_priv *p = netdev_priv(dev);
260         struct dsa_switch *ds = p->dp->ds;
261         int port = p->dp->index;
262
263         /* Pass through to switch driver if it supports timestamping */
264         switch (cmd) {
265         case SIOCGHWTSTAMP:
266                 if (ds->ops->port_hwtstamp_get)
267                         return ds->ops->port_hwtstamp_get(ds, port, ifr);
268                 break;
269         case SIOCSHWTSTAMP:
270                 if (ds->ops->port_hwtstamp_set)
271                         return ds->ops->port_hwtstamp_set(ds, port, ifr);
272                 break;
273         }
274
275         if (!dev->phydev)
276                 return -ENODEV;
277
278         return phy_mii_ioctl(dev->phydev, ifr, cmd);
279 }
280
281 static int dsa_slave_port_attr_set(struct net_device *dev,
282                                    const struct switchdev_attr *attr,
283                                    struct switchdev_trans *trans)
284 {
285         struct dsa_port *dp = dsa_slave_to_port(dev);
286         int ret;
287
288         switch (attr->id) {
289         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
290                 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
291                 break;
292         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
293                 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
294                                               trans);
295                 break;
296         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
297                 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
298                 break;
299         default:
300                 ret = -EOPNOTSUPP;
301                 break;
302         }
303
304         return ret;
305 }
306
307 static int dsa_slave_port_obj_add(struct net_device *dev,
308                                   const struct switchdev_obj *obj,
309                                   struct switchdev_trans *trans)
310 {
311         struct dsa_port *dp = dsa_slave_to_port(dev);
312         int err;
313
314         /* For the prepare phase, ensure the full set of changes is feasable in
315          * one go in order to signal a failure properly. If an operation is not
316          * supported, return -EOPNOTSUPP.
317          */
318
319         switch (obj->id) {
320         case SWITCHDEV_OBJ_ID_PORT_MDB:
321                 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
322                 break;
323         case SWITCHDEV_OBJ_ID_HOST_MDB:
324                 /* DSA can directly translate this to a normal MDB add,
325                  * but on the CPU port.
326                  */
327                 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
328                                        trans);
329                 break;
330         case SWITCHDEV_OBJ_ID_PORT_VLAN:
331                 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
332                                         trans);
333                 break;
334         default:
335                 err = -EOPNOTSUPP;
336                 break;
337         }
338
339         return err;
340 }
341
342 static int dsa_slave_port_obj_del(struct net_device *dev,
343                                   const struct switchdev_obj *obj)
344 {
345         struct dsa_port *dp = dsa_slave_to_port(dev);
346         int err;
347
348         switch (obj->id) {
349         case SWITCHDEV_OBJ_ID_PORT_MDB:
350                 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
351                 break;
352         case SWITCHDEV_OBJ_ID_HOST_MDB:
353                 /* DSA can directly translate this to a normal MDB add,
354                  * but on the CPU port.
355                  */
356                 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
357                 break;
358         case SWITCHDEV_OBJ_ID_PORT_VLAN:
359                 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
360                 break;
361         default:
362                 err = -EOPNOTSUPP;
363                 break;
364         }
365
366         return err;
367 }
368
369 static int dsa_slave_port_attr_get(struct net_device *dev,
370                                    struct switchdev_attr *attr)
371 {
372         struct dsa_port *dp = dsa_slave_to_port(dev);
373         struct dsa_switch *ds = dp->ds;
374         struct dsa_switch_tree *dst = ds->dst;
375
376         switch (attr->id) {
377         case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
378                 attr->u.ppid.id_len = sizeof(dst->index);
379                 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
380                 break;
381         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
382                 attr->u.brport_flags_support = 0;
383                 break;
384         default:
385                 return -EOPNOTSUPP;
386         }
387
388         return 0;
389 }
390
391 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
392                                                      struct sk_buff *skb)
393 {
394 #ifdef CONFIG_NET_POLL_CONTROLLER
395         struct dsa_slave_priv *p = netdev_priv(dev);
396
397         if (p->netpoll)
398                 netpoll_send_skb(p->netpoll, skb);
399 #else
400         BUG();
401 #endif
402         return NETDEV_TX_OK;
403 }
404
405 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
406                                  struct sk_buff *skb)
407 {
408         struct dsa_switch *ds = p->dp->ds;
409         struct sk_buff *clone;
410         unsigned int type;
411
412         type = ptp_classify_raw(skb);
413         if (type == PTP_CLASS_NONE)
414                 return;
415
416         if (!ds->ops->port_txtstamp)
417                 return;
418
419         clone = skb_clone_sk(skb);
420         if (!clone)
421                 return;
422
423         if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
424                 return;
425
426         kfree_skb(clone);
427 }
428
429 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
430 {
431         struct dsa_slave_priv *p = netdev_priv(dev);
432         struct pcpu_sw_netstats *s;
433         struct sk_buff *nskb;
434
435         s = this_cpu_ptr(p->stats64);
436         u64_stats_update_begin(&s->syncp);
437         s->tx_packets++;
438         s->tx_bytes += skb->len;
439         u64_stats_update_end(&s->syncp);
440
441         /* Identify PTP protocol packets, clone them, and pass them to the
442          * switch driver
443          */
444         dsa_skb_tx_timestamp(p, skb);
445
446         /* Transmit function may have to reallocate the original SKB,
447          * in which case it must have freed it. Only free it here on error.
448          */
449         nskb = p->xmit(skb, dev);
450         if (!nskb) {
451                 kfree_skb(skb);
452                 return NETDEV_TX_OK;
453         }
454
455         /* SKB for netpoll still need to be mangled with the protocol-specific
456          * tag to be successfully transmitted
457          */
458         if (unlikely(netpoll_tx_running(dev)))
459                 return dsa_slave_netpoll_send_skb(dev, nskb);
460
461         /* Queue the SKB for transmission on the parent interface, but
462          * do not modify its EtherType
463          */
464         nskb->dev = dsa_slave_to_master(dev);
465         dev_queue_xmit(nskb);
466
467         return NETDEV_TX_OK;
468 }
469
470 /* ethtool operations *******************************************************/
471
472 static void dsa_slave_get_drvinfo(struct net_device *dev,
473                                   struct ethtool_drvinfo *drvinfo)
474 {
475         strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
476         strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
477         strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
478 }
479
480 static int dsa_slave_get_regs_len(struct net_device *dev)
481 {
482         struct dsa_port *dp = dsa_slave_to_port(dev);
483         struct dsa_switch *ds = dp->ds;
484
485         if (ds->ops->get_regs_len)
486                 return ds->ops->get_regs_len(ds, dp->index);
487
488         return -EOPNOTSUPP;
489 }
490
491 static void
492 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
493 {
494         struct dsa_port *dp = dsa_slave_to_port(dev);
495         struct dsa_switch *ds = dp->ds;
496
497         if (ds->ops->get_regs)
498                 ds->ops->get_regs(ds, dp->index, regs, _p);
499 }
500
501 static u32 dsa_slave_get_link(struct net_device *dev)
502 {
503         if (!dev->phydev)
504                 return -ENODEV;
505
506         genphy_update_link(dev->phydev);
507
508         return dev->phydev->link;
509 }
510
511 static int dsa_slave_get_eeprom_len(struct net_device *dev)
512 {
513         struct dsa_port *dp = dsa_slave_to_port(dev);
514         struct dsa_switch *ds = dp->ds;
515
516         if (ds->cd && ds->cd->eeprom_len)
517                 return ds->cd->eeprom_len;
518
519         if (ds->ops->get_eeprom_len)
520                 return ds->ops->get_eeprom_len(ds);
521
522         return 0;
523 }
524
525 static int dsa_slave_get_eeprom(struct net_device *dev,
526                                 struct ethtool_eeprom *eeprom, u8 *data)
527 {
528         struct dsa_port *dp = dsa_slave_to_port(dev);
529         struct dsa_switch *ds = dp->ds;
530
531         if (ds->ops->get_eeprom)
532                 return ds->ops->get_eeprom(ds, eeprom, data);
533
534         return -EOPNOTSUPP;
535 }
536
537 static int dsa_slave_set_eeprom(struct net_device *dev,
538                                 struct ethtool_eeprom *eeprom, u8 *data)
539 {
540         struct dsa_port *dp = dsa_slave_to_port(dev);
541         struct dsa_switch *ds = dp->ds;
542
543         if (ds->ops->set_eeprom)
544                 return ds->ops->set_eeprom(ds, eeprom, data);
545
546         return -EOPNOTSUPP;
547 }
548
549 static void dsa_slave_get_strings(struct net_device *dev,
550                                   uint32_t stringset, uint8_t *data)
551 {
552         struct dsa_port *dp = dsa_slave_to_port(dev);
553         struct dsa_switch *ds = dp->ds;
554
555         if (stringset == ETH_SS_STATS) {
556                 int len = ETH_GSTRING_LEN;
557
558                 strncpy(data, "tx_packets", len);
559                 strncpy(data + len, "tx_bytes", len);
560                 strncpy(data + 2 * len, "rx_packets", len);
561                 strncpy(data + 3 * len, "rx_bytes", len);
562                 if (ds->ops->get_strings)
563                         ds->ops->get_strings(ds, dp->index, stringset,
564                                              data + 4 * len);
565         }
566 }
567
568 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
569                                         struct ethtool_stats *stats,
570                                         uint64_t *data)
571 {
572         struct dsa_port *dp = dsa_slave_to_port(dev);
573         struct dsa_slave_priv *p = netdev_priv(dev);
574         struct dsa_switch *ds = dp->ds;
575         struct pcpu_sw_netstats *s;
576         unsigned int start;
577         int i;
578
579         for_each_possible_cpu(i) {
580                 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
581
582                 s = per_cpu_ptr(p->stats64, i);
583                 do {
584                         start = u64_stats_fetch_begin_irq(&s->syncp);
585                         tx_packets = s->tx_packets;
586                         tx_bytes = s->tx_bytes;
587                         rx_packets = s->rx_packets;
588                         rx_bytes = s->rx_bytes;
589                 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
590                 data[0] += tx_packets;
591                 data[1] += tx_bytes;
592                 data[2] += rx_packets;
593                 data[3] += rx_bytes;
594         }
595         if (ds->ops->get_ethtool_stats)
596                 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
597 }
598
599 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
600 {
601         struct dsa_port *dp = dsa_slave_to_port(dev);
602         struct dsa_switch *ds = dp->ds;
603
604         if (sset == ETH_SS_STATS) {
605                 int count;
606
607                 count = 4;
608                 if (ds->ops->get_sset_count)
609                         count += ds->ops->get_sset_count(ds, dp->index, sset);
610
611                 return count;
612         }
613
614         return -EOPNOTSUPP;
615 }
616
617 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
618 {
619         struct dsa_port *dp = dsa_slave_to_port(dev);
620         struct dsa_switch *ds = dp->ds;
621
622         if (ds->ops->get_wol)
623                 ds->ops->get_wol(ds, dp->index, w);
624 }
625
626 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
627 {
628         struct dsa_port *dp = dsa_slave_to_port(dev);
629         struct dsa_switch *ds = dp->ds;
630         int ret = -EOPNOTSUPP;
631
632         if (ds->ops->set_wol)
633                 ret = ds->ops->set_wol(ds, dp->index, w);
634
635         return ret;
636 }
637
638 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
639 {
640         struct dsa_port *dp = dsa_slave_to_port(dev);
641         struct dsa_switch *ds = dp->ds;
642         int ret;
643
644         /* Port's PHY and MAC both need to be EEE capable */
645         if (!dev->phydev)
646                 return -ENODEV;
647
648         if (!ds->ops->set_mac_eee)
649                 return -EOPNOTSUPP;
650
651         ret = ds->ops->set_mac_eee(ds, dp->index, e);
652         if (ret)
653                 return ret;
654
655         if (e->eee_enabled) {
656                 ret = phy_init_eee(dev->phydev, 0);
657                 if (ret)
658                         return ret;
659         }
660
661         return phy_ethtool_set_eee(dev->phydev, e);
662 }
663
664 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
665 {
666         struct dsa_port *dp = dsa_slave_to_port(dev);
667         struct dsa_switch *ds = dp->ds;
668         int ret;
669
670         /* Port's PHY and MAC both need to be EEE capable */
671         if (!dev->phydev)
672                 return -ENODEV;
673
674         if (!ds->ops->get_mac_eee)
675                 return -EOPNOTSUPP;
676
677         ret = ds->ops->get_mac_eee(ds, dp->index, e);
678         if (ret)
679                 return ret;
680
681         return phy_ethtool_get_eee(dev->phydev, e);
682 }
683
684 #ifdef CONFIG_NET_POLL_CONTROLLER
685 static int dsa_slave_netpoll_setup(struct net_device *dev,
686                                    struct netpoll_info *ni)
687 {
688         struct net_device *master = dsa_slave_to_master(dev);
689         struct dsa_slave_priv *p = netdev_priv(dev);
690         struct netpoll *netpoll;
691         int err = 0;
692
693         netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
694         if (!netpoll)
695                 return -ENOMEM;
696
697         err = __netpoll_setup(netpoll, master);
698         if (err) {
699                 kfree(netpoll);
700                 goto out;
701         }
702
703         p->netpoll = netpoll;
704 out:
705         return err;
706 }
707
708 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
709 {
710         struct dsa_slave_priv *p = netdev_priv(dev);
711         struct netpoll *netpoll = p->netpoll;
712
713         if (!netpoll)
714                 return;
715
716         p->netpoll = NULL;
717
718         __netpoll_free_async(netpoll);
719 }
720
721 static void dsa_slave_poll_controller(struct net_device *dev)
722 {
723 }
724 #endif
725
726 static int dsa_slave_get_phys_port_name(struct net_device *dev,
727                                         char *name, size_t len)
728 {
729         struct dsa_port *dp = dsa_slave_to_port(dev);
730
731         if (snprintf(name, len, "p%d", dp->index) >= len)
732                 return -EINVAL;
733
734         return 0;
735 }
736
737 static struct dsa_mall_tc_entry *
738 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
739 {
740         struct dsa_slave_priv *p = netdev_priv(dev);
741         struct dsa_mall_tc_entry *mall_tc_entry;
742
743         list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
744                 if (mall_tc_entry->cookie == cookie)
745                         return mall_tc_entry;
746
747         return NULL;
748 }
749
750 static int dsa_slave_add_cls_matchall(struct net_device *dev,
751                                       struct tc_cls_matchall_offload *cls,
752                                       bool ingress)
753 {
754         struct dsa_port *dp = dsa_slave_to_port(dev);
755         struct dsa_slave_priv *p = netdev_priv(dev);
756         struct dsa_mall_tc_entry *mall_tc_entry;
757         __be16 protocol = cls->common.protocol;
758         struct dsa_switch *ds = dp->ds;
759         struct net_device *to_dev;
760         const struct tc_action *a;
761         struct dsa_port *to_dp;
762         int err = -EOPNOTSUPP;
763         LIST_HEAD(actions);
764
765         if (!ds->ops->port_mirror_add)
766                 return err;
767
768         if (!tcf_exts_has_one_action(cls->exts))
769                 return err;
770
771         tcf_exts_to_list(cls->exts, &actions);
772         a = list_first_entry(&actions, struct tc_action, list);
773
774         if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
775                 struct dsa_mall_mirror_tc_entry *mirror;
776
777                 to_dev = tcf_mirred_dev(a);
778                 if (!to_dev)
779                         return -EINVAL;
780
781                 if (!dsa_slave_dev_check(to_dev))
782                         return -EOPNOTSUPP;
783
784                 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
785                 if (!mall_tc_entry)
786                         return -ENOMEM;
787
788                 mall_tc_entry->cookie = cls->cookie;
789                 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
790                 mirror = &mall_tc_entry->mirror;
791
792                 to_dp = dsa_slave_to_port(to_dev);
793
794                 mirror->to_local_port = to_dp->index;
795                 mirror->ingress = ingress;
796
797                 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
798                 if (err) {
799                         kfree(mall_tc_entry);
800                         return err;
801                 }
802
803                 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
804         }
805
806         return 0;
807 }
808
809 static void dsa_slave_del_cls_matchall(struct net_device *dev,
810                                        struct tc_cls_matchall_offload *cls)
811 {
812         struct dsa_port *dp = dsa_slave_to_port(dev);
813         struct dsa_mall_tc_entry *mall_tc_entry;
814         struct dsa_switch *ds = dp->ds;
815
816         if (!ds->ops->port_mirror_del)
817                 return;
818
819         mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
820         if (!mall_tc_entry)
821                 return;
822
823         list_del(&mall_tc_entry->list);
824
825         switch (mall_tc_entry->type) {
826         case DSA_PORT_MALL_MIRROR:
827                 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
828                 break;
829         default:
830                 WARN_ON(1);
831         }
832
833         kfree(mall_tc_entry);
834 }
835
836 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
837                                            struct tc_cls_matchall_offload *cls,
838                                            bool ingress)
839 {
840         if (cls->common.chain_index)
841                 return -EOPNOTSUPP;
842
843         switch (cls->command) {
844         case TC_CLSMATCHALL_REPLACE:
845                 return dsa_slave_add_cls_matchall(dev, cls, ingress);
846         case TC_CLSMATCHALL_DESTROY:
847                 dsa_slave_del_cls_matchall(dev, cls);
848                 return 0;
849         default:
850                 return -EOPNOTSUPP;
851         }
852 }
853
854 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
855                                        void *cb_priv, bool ingress)
856 {
857         struct net_device *dev = cb_priv;
858
859         if (!tc_can_offload(dev))
860                 return -EOPNOTSUPP;
861
862         switch (type) {
863         case TC_SETUP_CLSMATCHALL:
864                 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
865         default:
866                 return -EOPNOTSUPP;
867         }
868 }
869
870 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
871                                           void *type_data, void *cb_priv)
872 {
873         return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
874 }
875
876 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
877                                           void *type_data, void *cb_priv)
878 {
879         return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
880 }
881
882 static int dsa_slave_setup_tc_block(struct net_device *dev,
883                                     struct tc_block_offload *f)
884 {
885         tc_setup_cb_t *cb;
886
887         if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
888                 cb = dsa_slave_setup_tc_block_cb_ig;
889         else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
890                 cb = dsa_slave_setup_tc_block_cb_eg;
891         else
892                 return -EOPNOTSUPP;
893
894         switch (f->command) {
895         case TC_BLOCK_BIND:
896                 return tcf_block_cb_register(f->block, cb, dev, dev);
897         case TC_BLOCK_UNBIND:
898                 tcf_block_cb_unregister(f->block, cb, dev);
899                 return 0;
900         default:
901                 return -EOPNOTSUPP;
902         }
903 }
904
905 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
906                               void *type_data)
907 {
908         switch (type) {
909         case TC_SETUP_BLOCK:
910                 return dsa_slave_setup_tc_block(dev, type_data);
911         default:
912                 return -EOPNOTSUPP;
913         }
914 }
915
916 static void dsa_slave_get_stats64(struct net_device *dev,
917                                   struct rtnl_link_stats64 *stats)
918 {
919         struct dsa_slave_priv *p = netdev_priv(dev);
920         struct pcpu_sw_netstats *s;
921         unsigned int start;
922         int i;
923
924         netdev_stats_to_stats64(stats, &dev->stats);
925         for_each_possible_cpu(i) {
926                 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
927
928                 s = per_cpu_ptr(p->stats64, i);
929                 do {
930                         start = u64_stats_fetch_begin_irq(&s->syncp);
931                         tx_packets = s->tx_packets;
932                         tx_bytes = s->tx_bytes;
933                         rx_packets = s->rx_packets;
934                         rx_bytes = s->rx_bytes;
935                 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
936
937                 stats->tx_packets += tx_packets;
938                 stats->tx_bytes += tx_bytes;
939                 stats->rx_packets += rx_packets;
940                 stats->rx_bytes += rx_bytes;
941         }
942 }
943
944 static int dsa_slave_get_rxnfc(struct net_device *dev,
945                                struct ethtool_rxnfc *nfc, u32 *rule_locs)
946 {
947         struct dsa_port *dp = dsa_slave_to_port(dev);
948         struct dsa_switch *ds = dp->ds;
949
950         if (!ds->ops->get_rxnfc)
951                 return -EOPNOTSUPP;
952
953         return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
954 }
955
956 static int dsa_slave_set_rxnfc(struct net_device *dev,
957                                struct ethtool_rxnfc *nfc)
958 {
959         struct dsa_port *dp = dsa_slave_to_port(dev);
960         struct dsa_switch *ds = dp->ds;
961
962         if (!ds->ops->set_rxnfc)
963                 return -EOPNOTSUPP;
964
965         return ds->ops->set_rxnfc(ds, dp->index, nfc);
966 }
967
968 static int dsa_slave_get_ts_info(struct net_device *dev,
969                                  struct ethtool_ts_info *ts)
970 {
971         struct dsa_slave_priv *p = netdev_priv(dev);
972         struct dsa_switch *ds = p->dp->ds;
973
974         if (!ds->ops->get_ts_info)
975                 return -EOPNOTSUPP;
976
977         return ds->ops->get_ts_info(ds, p->dp->index, ts);
978 }
979
980 static const struct ethtool_ops dsa_slave_ethtool_ops = {
981         .get_drvinfo            = dsa_slave_get_drvinfo,
982         .get_regs_len           = dsa_slave_get_regs_len,
983         .get_regs               = dsa_slave_get_regs,
984         .nway_reset             = phy_ethtool_nway_reset,
985         .get_link               = dsa_slave_get_link,
986         .get_eeprom_len         = dsa_slave_get_eeprom_len,
987         .get_eeprom             = dsa_slave_get_eeprom,
988         .set_eeprom             = dsa_slave_set_eeprom,
989         .get_strings            = dsa_slave_get_strings,
990         .get_ethtool_stats      = dsa_slave_get_ethtool_stats,
991         .get_sset_count         = dsa_slave_get_sset_count,
992         .set_wol                = dsa_slave_set_wol,
993         .get_wol                = dsa_slave_get_wol,
994         .set_eee                = dsa_slave_set_eee,
995         .get_eee                = dsa_slave_get_eee,
996         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
997         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
998         .get_rxnfc              = dsa_slave_get_rxnfc,
999         .set_rxnfc              = dsa_slave_set_rxnfc,
1000         .get_ts_info            = dsa_slave_get_ts_info,
1001 };
1002
1003 /* legacy way, bypassing the bridge *****************************************/
1004 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1005                        struct net_device *dev,
1006                        const unsigned char *addr, u16 vid,
1007                        u16 flags)
1008 {
1009         struct dsa_port *dp = dsa_slave_to_port(dev);
1010
1011         return dsa_port_fdb_add(dp, addr, vid);
1012 }
1013
1014 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1015                        struct net_device *dev,
1016                        const unsigned char *addr, u16 vid)
1017 {
1018         struct dsa_port *dp = dsa_slave_to_port(dev);
1019
1020         return dsa_port_fdb_del(dp, addr, vid);
1021 }
1022
1023 static const struct net_device_ops dsa_slave_netdev_ops = {
1024         .ndo_open               = dsa_slave_open,
1025         .ndo_stop               = dsa_slave_close,
1026         .ndo_start_xmit         = dsa_slave_xmit,
1027         .ndo_change_rx_flags    = dsa_slave_change_rx_flags,
1028         .ndo_set_rx_mode        = dsa_slave_set_rx_mode,
1029         .ndo_set_mac_address    = dsa_slave_set_mac_address,
1030         .ndo_fdb_add            = dsa_legacy_fdb_add,
1031         .ndo_fdb_del            = dsa_legacy_fdb_del,
1032         .ndo_fdb_dump           = dsa_slave_fdb_dump,
1033         .ndo_do_ioctl           = dsa_slave_ioctl,
1034         .ndo_get_iflink         = dsa_slave_get_iflink,
1035 #ifdef CONFIG_NET_POLL_CONTROLLER
1036         .ndo_netpoll_setup      = dsa_slave_netpoll_setup,
1037         .ndo_netpoll_cleanup    = dsa_slave_netpoll_cleanup,
1038         .ndo_poll_controller    = dsa_slave_poll_controller,
1039 #endif
1040         .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1041         .ndo_setup_tc           = dsa_slave_setup_tc,
1042         .ndo_get_stats64        = dsa_slave_get_stats64,
1043 };
1044
1045 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1046         .switchdev_port_attr_get        = dsa_slave_port_attr_get,
1047         .switchdev_port_attr_set        = dsa_slave_port_attr_set,
1048         .switchdev_port_obj_add         = dsa_slave_port_obj_add,
1049         .switchdev_port_obj_del         = dsa_slave_port_obj_del,
1050 };
1051
1052 static struct device_type dsa_type = {
1053         .name   = "dsa",
1054 };
1055
1056 static void dsa_slave_adjust_link(struct net_device *dev)
1057 {
1058         struct dsa_port *dp = dsa_slave_to_port(dev);
1059         struct dsa_slave_priv *p = netdev_priv(dev);
1060         struct dsa_switch *ds = dp->ds;
1061         unsigned int status_changed = 0;
1062
1063         if (p->old_link != dev->phydev->link) {
1064                 status_changed = 1;
1065                 p->old_link = dev->phydev->link;
1066         }
1067
1068         if (p->old_duplex != dev->phydev->duplex) {
1069                 status_changed = 1;
1070                 p->old_duplex = dev->phydev->duplex;
1071         }
1072
1073         if (p->old_pause != dev->phydev->pause) {
1074                 status_changed = 1;
1075                 p->old_pause = dev->phydev->pause;
1076         }
1077
1078         if (ds->ops->adjust_link && status_changed)
1079                 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1080
1081         if (status_changed)
1082                 phy_print_status(dev->phydev);
1083 }
1084
1085 static int dsa_slave_fixed_link_update(struct net_device *dev,
1086                                        struct fixed_phy_status *status)
1087 {
1088         struct dsa_switch *ds;
1089         struct dsa_port *dp;
1090
1091         if (dev) {
1092                 dp = dsa_slave_to_port(dev);
1093                 ds = dp->ds;
1094                 if (ds->ops->fixed_link_update)
1095                         ds->ops->fixed_link_update(ds, dp->index, status);
1096         }
1097
1098         return 0;
1099 }
1100
1101 /* slave device setup *******************************************************/
1102 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1103 {
1104         struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1105         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1106         struct dsa_switch *ds = dp->ds;
1107
1108         slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1109         if (!slave_dev->phydev) {
1110                 netdev_err(slave_dev, "no phy at %d\n", addr);
1111                 return -ENODEV;
1112         }
1113
1114         /* Use already configured phy mode */
1115         if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1116                 p->phy_interface = slave_dev->phydev->interface;
1117
1118         return phy_connect_direct(slave_dev, slave_dev->phydev,
1119                                   dsa_slave_adjust_link, p->phy_interface);
1120 }
1121
1122 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1123 {
1124         struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1125         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1126         struct device_node *port_dn = dp->dn;
1127         struct dsa_switch *ds = dp->ds;
1128         struct device_node *phy_dn;
1129         bool phy_is_fixed = false;
1130         u32 phy_flags = 0;
1131         int mode, ret;
1132
1133         mode = of_get_phy_mode(port_dn);
1134         if (mode < 0)
1135                 mode = PHY_INTERFACE_MODE_NA;
1136         p->phy_interface = mode;
1137
1138         phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1139         if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1140                 /* In the case of a fixed PHY, the DT node associated
1141                  * to the fixed PHY is the Port DT node
1142                  */
1143                 ret = of_phy_register_fixed_link(port_dn);
1144                 if (ret) {
1145                         netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1146                         return ret;
1147                 }
1148                 phy_is_fixed = true;
1149                 phy_dn = of_node_get(port_dn);
1150         }
1151
1152         if (ds->ops->get_phy_flags)
1153                 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1154
1155         if (phy_dn) {
1156                 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1157                                                    dsa_slave_adjust_link,
1158                                                    phy_flags,
1159                                                    p->phy_interface);
1160                 of_node_put(phy_dn);
1161         }
1162
1163         if (slave_dev->phydev && phy_is_fixed)
1164                 fixed_phy_set_link_update(slave_dev->phydev,
1165                                           dsa_slave_fixed_link_update);
1166
1167         /* We could not connect to a designated PHY, so use the switch internal
1168          * MDIO bus instead
1169          */
1170         if (!slave_dev->phydev) {
1171                 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1172                 if (ret) {
1173                         netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1174                                    dp->index, ret);
1175                         if (phy_is_fixed)
1176                                 of_phy_deregister_fixed_link(port_dn);
1177                         return ret;
1178                 }
1179         }
1180
1181         phy_attached_info(slave_dev->phydev);
1182
1183         return 0;
1184 }
1185
1186 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1187 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1188                                             struct netdev_queue *txq,
1189                                             void *_unused)
1190 {
1191         lockdep_set_class(&txq->_xmit_lock,
1192                           &dsa_slave_netdev_xmit_lock_key);
1193 }
1194
1195 int dsa_slave_suspend(struct net_device *slave_dev)
1196 {
1197         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1198
1199         netif_device_detach(slave_dev);
1200
1201         if (slave_dev->phydev) {
1202                 phy_stop(slave_dev->phydev);
1203                 p->old_pause = -1;
1204                 p->old_link = -1;
1205                 p->old_duplex = -1;
1206                 phy_suspend(slave_dev->phydev);
1207         }
1208
1209         return 0;
1210 }
1211
1212 int dsa_slave_resume(struct net_device *slave_dev)
1213 {
1214         netif_device_attach(slave_dev);
1215
1216         if (slave_dev->phydev) {
1217                 phy_resume(slave_dev->phydev);
1218                 phy_start(slave_dev->phydev);
1219         }
1220
1221         return 0;
1222 }
1223
1224 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1225 {
1226         struct net_device *master = dsa_slave_to_master(dev);
1227         struct dsa_port *dp = dsa_slave_to_port(dev);
1228         struct dsa_notifier_register_info rinfo = {
1229                 .switch_number = dp->ds->index,
1230                 .port_number = dp->index,
1231                 .master = master,
1232                 .info.dev = dev,
1233         };
1234
1235         call_dsa_notifiers(val, dev, &rinfo.info);
1236 }
1237
1238 int dsa_slave_create(struct dsa_port *port)
1239 {
1240         const struct dsa_port *cpu_dp = port->cpu_dp;
1241         struct net_device *master = cpu_dp->master;
1242         struct dsa_switch *ds = port->ds;
1243         const char *name = port->name;
1244         struct net_device *slave_dev;
1245         struct dsa_slave_priv *p;
1246         int ret;
1247
1248         if (!ds->num_tx_queues)
1249                 ds->num_tx_queues = 1;
1250
1251         slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1252                                      NET_NAME_UNKNOWN, ether_setup,
1253                                      ds->num_tx_queues, 1);
1254         if (slave_dev == NULL)
1255                 return -ENOMEM;
1256
1257         slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1258         slave_dev->hw_features |= NETIF_F_HW_TC;
1259         slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1260         eth_hw_addr_inherit(slave_dev, master);
1261         slave_dev->priv_flags |= IFF_NO_QUEUE;
1262         slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1263         slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1264         slave_dev->min_mtu = 0;
1265         slave_dev->max_mtu = ETH_MAX_MTU;
1266         SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1267
1268         netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1269                                  NULL);
1270
1271         SET_NETDEV_DEV(slave_dev, port->ds->dev);
1272         slave_dev->dev.of_node = port->dn;
1273         slave_dev->vlan_features = master->vlan_features;
1274
1275         p = netdev_priv(slave_dev);
1276         p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1277         if (!p->stats64) {
1278                 free_netdev(slave_dev);
1279                 return -ENOMEM;
1280         }
1281         p->dp = port;
1282         INIT_LIST_HEAD(&p->mall_tc_list);
1283         p->xmit = cpu_dp->tag_ops->xmit;
1284
1285         p->old_pause = -1;
1286         p->old_link = -1;
1287         p->old_duplex = -1;
1288
1289         port->slave = slave_dev;
1290
1291         netif_carrier_off(slave_dev);
1292
1293         ret = dsa_slave_phy_setup(slave_dev);
1294         if (ret) {
1295                 netdev_err(master, "error %d setting up slave phy\n", ret);
1296                 goto out_free;
1297         }
1298
1299         dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1300
1301         ret = register_netdev(slave_dev);
1302         if (ret) {
1303                 netdev_err(master, "error %d registering interface %s\n",
1304                            ret, slave_dev->name);
1305                 goto out_phy;
1306         }
1307
1308         return 0;
1309
1310 out_phy:
1311         phy_disconnect(slave_dev->phydev);
1312         if (of_phy_is_fixed_link(port->dn))
1313                 of_phy_deregister_fixed_link(port->dn);
1314 out_free:
1315         free_percpu(p->stats64);
1316         free_netdev(slave_dev);
1317         port->slave = NULL;
1318         return ret;
1319 }
1320
1321 void dsa_slave_destroy(struct net_device *slave_dev)
1322 {
1323         struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1324         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1325         struct device_node *port_dn = dp->dn;
1326
1327         netif_carrier_off(slave_dev);
1328         if (slave_dev->phydev) {
1329                 phy_disconnect(slave_dev->phydev);
1330
1331                 if (of_phy_is_fixed_link(port_dn))
1332                         of_phy_deregister_fixed_link(port_dn);
1333         }
1334         dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1335         unregister_netdev(slave_dev);
1336         free_percpu(p->stats64);
1337         free_netdev(slave_dev);
1338 }
1339
1340 static bool dsa_slave_dev_check(struct net_device *dev)
1341 {
1342         return dev->netdev_ops == &dsa_slave_netdev_ops;
1343 }
1344
1345 static int dsa_slave_changeupper(struct net_device *dev,
1346                                  struct netdev_notifier_changeupper_info *info)
1347 {
1348         struct dsa_port *dp = dsa_slave_to_port(dev);
1349         int err = NOTIFY_DONE;
1350
1351         if (netif_is_bridge_master(info->upper_dev)) {
1352                 if (info->linking) {
1353                         err = dsa_port_bridge_join(dp, info->upper_dev);
1354                         err = notifier_from_errno(err);
1355                 } else {
1356                         dsa_port_bridge_leave(dp, info->upper_dev);
1357                         err = NOTIFY_OK;
1358                 }
1359         }
1360
1361         return err;
1362 }
1363
1364 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1365                                      unsigned long event, void *ptr)
1366 {
1367         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1368
1369         if (!dsa_slave_dev_check(dev))
1370                 return NOTIFY_DONE;
1371
1372         if (event == NETDEV_CHANGEUPPER)
1373                 return dsa_slave_changeupper(dev, ptr);
1374
1375         return NOTIFY_DONE;
1376 }
1377
1378 struct dsa_switchdev_event_work {
1379         struct work_struct work;
1380         struct switchdev_notifier_fdb_info fdb_info;
1381         struct net_device *dev;
1382         unsigned long event;
1383 };
1384
1385 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1386 {
1387         struct dsa_switchdev_event_work *switchdev_work =
1388                 container_of(work, struct dsa_switchdev_event_work, work);
1389         struct net_device *dev = switchdev_work->dev;
1390         struct switchdev_notifier_fdb_info *fdb_info;
1391         struct dsa_port *dp = dsa_slave_to_port(dev);
1392         int err;
1393
1394         rtnl_lock();
1395         switch (switchdev_work->event) {
1396         case SWITCHDEV_FDB_ADD_TO_DEVICE:
1397                 fdb_info = &switchdev_work->fdb_info;
1398                 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1399                 if (err) {
1400                         netdev_dbg(dev, "fdb add failed err=%d\n", err);
1401                         break;
1402                 }
1403                 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1404                                          &fdb_info->info);
1405                 break;
1406
1407         case SWITCHDEV_FDB_DEL_TO_DEVICE:
1408                 fdb_info = &switchdev_work->fdb_info;
1409                 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1410                 if (err) {
1411                         netdev_dbg(dev, "fdb del failed err=%d\n", err);
1412                         dev_close(dev);
1413                 }
1414                 break;
1415         }
1416         rtnl_unlock();
1417
1418         kfree(switchdev_work->fdb_info.addr);
1419         kfree(switchdev_work);
1420         dev_put(dev);
1421 }
1422
1423 static int
1424 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1425                                   switchdev_work,
1426                                   const struct switchdev_notifier_fdb_info *
1427                                   fdb_info)
1428 {
1429         memcpy(&switchdev_work->fdb_info, fdb_info,
1430                sizeof(switchdev_work->fdb_info));
1431         switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1432         if (!switchdev_work->fdb_info.addr)
1433                 return -ENOMEM;
1434         ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1435                         fdb_info->addr);
1436         return 0;
1437 }
1438
1439 /* Called under rcu_read_lock() */
1440 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1441                                      unsigned long event, void *ptr)
1442 {
1443         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1444         struct switchdev_notifier_fdb_info *fdb_info = ptr;
1445         struct dsa_switchdev_event_work *switchdev_work;
1446
1447         if (!dsa_slave_dev_check(dev))
1448                 return NOTIFY_DONE;
1449
1450         switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1451         if (!switchdev_work)
1452                 return NOTIFY_BAD;
1453
1454         INIT_WORK(&switchdev_work->work,
1455                   dsa_slave_switchdev_event_work);
1456         switchdev_work->dev = dev;
1457         switchdev_work->event = event;
1458
1459         switch (event) {
1460         case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1461         case SWITCHDEV_FDB_DEL_TO_DEVICE:
1462                 if (!fdb_info->added_by_user)
1463                         break;
1464                 if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1465                                                       fdb_info))
1466                         goto err_fdb_work_init;
1467                 dev_hold(dev);
1468                 break;
1469         default:
1470                 kfree(switchdev_work);
1471                 return NOTIFY_DONE;
1472         }
1473
1474         dsa_schedule_work(&switchdev_work->work);
1475         return NOTIFY_OK;
1476
1477 err_fdb_work_init:
1478         kfree(switchdev_work);
1479         return NOTIFY_BAD;
1480 }
1481
1482 static struct notifier_block dsa_slave_nb __read_mostly = {
1483         .notifier_call  = dsa_slave_netdevice_event,
1484 };
1485
1486 static struct notifier_block dsa_slave_switchdev_notifier = {
1487         .notifier_call = dsa_slave_switchdev_event,
1488 };
1489
1490 int dsa_slave_register_notifier(void)
1491 {
1492         int err;
1493
1494         err = register_netdevice_notifier(&dsa_slave_nb);
1495         if (err)
1496                 return err;
1497
1498         err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1499         if (err)
1500                 goto err_switchdev_nb;
1501
1502         return 0;
1503
1504 err_switchdev_nb:
1505         unregister_netdevice_notifier(&dsa_slave_nb);
1506         return err;
1507 }
1508
1509 void dsa_slave_unregister_notifier(void)
1510 {
1511         int err;
1512
1513         err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1514         if (err)
1515                 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1516
1517         err = unregister_netdevice_notifier(&dsa_slave_nb);
1518         if (err)
1519                 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1520 }