GNU Linux-libre 4.9.292-gnu1
[releases.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/switchdev.h>
21 #include <linux/if_bridge.h>
22 #include <linux/netpoll.h>
23 #include "dsa_priv.h"
24
25 /* slave mii_bus handling ***************************************************/
26 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
27 {
28         struct dsa_switch *ds = bus->priv;
29
30         if (ds->phys_mii_mask & (1 << addr))
31                 return ds->ops->phy_read(ds, addr, reg);
32
33         return 0xffff;
34 }
35
36 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
37 {
38         struct dsa_switch *ds = bus->priv;
39
40         if (ds->phys_mii_mask & (1 << addr))
41                 return ds->ops->phy_write(ds, addr, reg, val);
42
43         return 0;
44 }
45
46 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
47 {
48         ds->slave_mii_bus->priv = (void *)ds;
49         ds->slave_mii_bus->name = "dsa slave smi";
50         ds->slave_mii_bus->read = dsa_slave_phy_read;
51         ds->slave_mii_bus->write = dsa_slave_phy_write;
52         snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
53                  ds->dst->tree, ds->index);
54         ds->slave_mii_bus->parent = ds->dev;
55         ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
56 }
57
58
59 /* slave device handling ****************************************************/
60 static int dsa_slave_get_iflink(const struct net_device *dev)
61 {
62         struct dsa_slave_priv *p = netdev_priv(dev);
63
64         return p->parent->dst->master_netdev->ifindex;
65 }
66
67 static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
68 {
69         return !!p->bridge_dev;
70 }
71
72 static void dsa_port_set_stp_state(struct dsa_switch *ds, int port, u8 state)
73 {
74         struct dsa_port *dp = &ds->ports[port];
75
76         if (ds->ops->port_stp_state_set)
77                 ds->ops->port_stp_state_set(ds, port, state);
78
79         if (ds->ops->port_fast_age) {
80                 /* Fast age FDB entries or flush appropriate forwarding database
81                  * for the given port, if we are moving it from Learning or
82                  * Forwarding state, to Disabled or Blocking or Listening state.
83                  */
84
85                 if ((dp->stp_state == BR_STATE_LEARNING ||
86                      dp->stp_state == BR_STATE_FORWARDING) &&
87                     (state == BR_STATE_DISABLED ||
88                      state == BR_STATE_BLOCKING ||
89                      state == BR_STATE_LISTENING))
90                         ds->ops->port_fast_age(ds, port);
91         }
92
93         dp->stp_state = state;
94 }
95
96 static int dsa_slave_open(struct net_device *dev)
97 {
98         struct dsa_slave_priv *p = netdev_priv(dev);
99         struct net_device *master = p->parent->dst->master_netdev;
100         struct dsa_switch *ds = p->parent;
101         u8 stp_state = dsa_port_is_bridged(p) ?
102                         BR_STATE_BLOCKING : BR_STATE_FORWARDING;
103         int err;
104
105         if (!(master->flags & IFF_UP))
106                 return -ENETDOWN;
107
108         if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
109                 err = dev_uc_add(master, dev->dev_addr);
110                 if (err < 0)
111                         goto out;
112         }
113
114         if (dev->flags & IFF_ALLMULTI) {
115                 err = dev_set_allmulti(master, 1);
116                 if (err < 0)
117                         goto del_unicast;
118         }
119         if (dev->flags & IFF_PROMISC) {
120                 err = dev_set_promiscuity(master, 1);
121                 if (err < 0)
122                         goto clear_allmulti;
123         }
124
125         if (ds->ops->port_enable) {
126                 err = ds->ops->port_enable(ds, p->port, p->phy);
127                 if (err)
128                         goto clear_promisc;
129         }
130
131         dsa_port_set_stp_state(ds, p->port, stp_state);
132
133         if (p->phy)
134                 phy_start(p->phy);
135
136         return 0;
137
138 clear_promisc:
139         if (dev->flags & IFF_PROMISC)
140                 dev_set_promiscuity(master, -1);
141 clear_allmulti:
142         if (dev->flags & IFF_ALLMULTI)
143                 dev_set_allmulti(master, -1);
144 del_unicast:
145         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
146                 dev_uc_del(master, dev->dev_addr);
147 out:
148         return err;
149 }
150
151 static int dsa_slave_close(struct net_device *dev)
152 {
153         struct dsa_slave_priv *p = netdev_priv(dev);
154         struct net_device *master = p->parent->dst->master_netdev;
155         struct dsa_switch *ds = p->parent;
156
157         if (p->phy)
158                 phy_stop(p->phy);
159
160         dev_mc_unsync(master, dev);
161         dev_uc_unsync(master, dev);
162         if (dev->flags & IFF_ALLMULTI)
163                 dev_set_allmulti(master, -1);
164         if (dev->flags & IFF_PROMISC)
165                 dev_set_promiscuity(master, -1);
166
167         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
168                 dev_uc_del(master, dev->dev_addr);
169
170         if (ds->ops->port_disable)
171                 ds->ops->port_disable(ds, p->port, p->phy);
172
173         dsa_port_set_stp_state(ds, p->port, BR_STATE_DISABLED);
174
175         return 0;
176 }
177
178 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
179 {
180         struct dsa_slave_priv *p = netdev_priv(dev);
181         struct net_device *master = p->parent->dst->master_netdev;
182
183         if (dev->flags & IFF_UP) {
184                 if (change & IFF_ALLMULTI)
185                         dev_set_allmulti(master,
186                                          dev->flags & IFF_ALLMULTI ? 1 : -1);
187                 if (change & IFF_PROMISC)
188                         dev_set_promiscuity(master,
189                                             dev->flags & IFF_PROMISC ? 1 : -1);
190         }
191 }
192
193 static void dsa_slave_set_rx_mode(struct net_device *dev)
194 {
195         struct dsa_slave_priv *p = netdev_priv(dev);
196         struct net_device *master = p->parent->dst->master_netdev;
197
198         dev_mc_sync(master, dev);
199         dev_uc_sync(master, dev);
200 }
201
202 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
203 {
204         struct dsa_slave_priv *p = netdev_priv(dev);
205         struct net_device *master = p->parent->dst->master_netdev;
206         struct sockaddr *addr = a;
207         int err;
208
209         if (!is_valid_ether_addr(addr->sa_data))
210                 return -EADDRNOTAVAIL;
211
212         if (!(dev->flags & IFF_UP))
213                 goto out;
214
215         if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
216                 err = dev_uc_add(master, addr->sa_data);
217                 if (err < 0)
218                         return err;
219         }
220
221         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
222                 dev_uc_del(master, dev->dev_addr);
223
224 out:
225         ether_addr_copy(dev->dev_addr, addr->sa_data);
226
227         return 0;
228 }
229
230 static int dsa_slave_port_vlan_add(struct net_device *dev,
231                                    const struct switchdev_obj_port_vlan *vlan,
232                                    struct switchdev_trans *trans)
233 {
234         struct dsa_slave_priv *p = netdev_priv(dev);
235         struct dsa_switch *ds = p->parent;
236
237         if (switchdev_trans_ph_prepare(trans)) {
238                 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
239                         return -EOPNOTSUPP;
240
241                 return ds->ops->port_vlan_prepare(ds, p->port, vlan, trans);
242         }
243
244         ds->ops->port_vlan_add(ds, p->port, vlan, trans);
245
246         return 0;
247 }
248
249 static int dsa_slave_port_vlan_del(struct net_device *dev,
250                                    const struct switchdev_obj_port_vlan *vlan)
251 {
252         struct dsa_slave_priv *p = netdev_priv(dev);
253         struct dsa_switch *ds = p->parent;
254
255         if (!ds->ops->port_vlan_del)
256                 return -EOPNOTSUPP;
257
258         return ds->ops->port_vlan_del(ds, p->port, vlan);
259 }
260
261 static int dsa_slave_port_vlan_dump(struct net_device *dev,
262                                     struct switchdev_obj_port_vlan *vlan,
263                                     switchdev_obj_dump_cb_t *cb)
264 {
265         struct dsa_slave_priv *p = netdev_priv(dev);
266         struct dsa_switch *ds = p->parent;
267
268         if (ds->ops->port_vlan_dump)
269                 return ds->ops->port_vlan_dump(ds, p->port, vlan, cb);
270
271         return -EOPNOTSUPP;
272 }
273
274 static int dsa_slave_port_fdb_add(struct net_device *dev,
275                                   const struct switchdev_obj_port_fdb *fdb,
276                                   struct switchdev_trans *trans)
277 {
278         struct dsa_slave_priv *p = netdev_priv(dev);
279         struct dsa_switch *ds = p->parent;
280
281         if (switchdev_trans_ph_prepare(trans)) {
282                 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
283                         return -EOPNOTSUPP;
284
285                 return ds->ops->port_fdb_prepare(ds, p->port, fdb, trans);
286         }
287
288         ds->ops->port_fdb_add(ds, p->port, fdb, trans);
289
290         return 0;
291 }
292
293 static int dsa_slave_port_fdb_del(struct net_device *dev,
294                                   const struct switchdev_obj_port_fdb *fdb)
295 {
296         struct dsa_slave_priv *p = netdev_priv(dev);
297         struct dsa_switch *ds = p->parent;
298         int ret = -EOPNOTSUPP;
299
300         if (ds->ops->port_fdb_del)
301                 ret = ds->ops->port_fdb_del(ds, p->port, fdb);
302
303         return ret;
304 }
305
306 static int dsa_slave_port_fdb_dump(struct net_device *dev,
307                                    struct switchdev_obj_port_fdb *fdb,
308                                    switchdev_obj_dump_cb_t *cb)
309 {
310         struct dsa_slave_priv *p = netdev_priv(dev);
311         struct dsa_switch *ds = p->parent;
312
313         if (ds->ops->port_fdb_dump)
314                 return ds->ops->port_fdb_dump(ds, p->port, fdb, cb);
315
316         return -EOPNOTSUPP;
317 }
318
319 static int dsa_slave_port_mdb_add(struct net_device *dev,
320                                   const struct switchdev_obj_port_mdb *mdb,
321                                   struct switchdev_trans *trans)
322 {
323         struct dsa_slave_priv *p = netdev_priv(dev);
324         struct dsa_switch *ds = p->parent;
325
326         if (switchdev_trans_ph_prepare(trans)) {
327                 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
328                         return -EOPNOTSUPP;
329
330                 return ds->ops->port_mdb_prepare(ds, p->port, mdb, trans);
331         }
332
333         ds->ops->port_mdb_add(ds, p->port, mdb, trans);
334
335         return 0;
336 }
337
338 static int dsa_slave_port_mdb_del(struct net_device *dev,
339                                   const struct switchdev_obj_port_mdb *mdb)
340 {
341         struct dsa_slave_priv *p = netdev_priv(dev);
342         struct dsa_switch *ds = p->parent;
343
344         if (ds->ops->port_mdb_del)
345                 return ds->ops->port_mdb_del(ds, p->port, mdb);
346
347         return -EOPNOTSUPP;
348 }
349
350 static int dsa_slave_port_mdb_dump(struct net_device *dev,
351                                    struct switchdev_obj_port_mdb *mdb,
352                                    switchdev_obj_dump_cb_t *cb)
353 {
354         struct dsa_slave_priv *p = netdev_priv(dev);
355         struct dsa_switch *ds = p->parent;
356
357         if (ds->ops->port_mdb_dump)
358                 return ds->ops->port_mdb_dump(ds, p->port, mdb, cb);
359
360         return -EOPNOTSUPP;
361 }
362
363 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
364 {
365         struct dsa_slave_priv *p = netdev_priv(dev);
366
367         if (p->phy != NULL)
368                 return phy_mii_ioctl(p->phy, ifr, cmd);
369
370         return -EOPNOTSUPP;
371 }
372
373 static int dsa_slave_stp_state_set(struct net_device *dev,
374                                    const struct switchdev_attr *attr,
375                                    struct switchdev_trans *trans)
376 {
377         struct dsa_slave_priv *p = netdev_priv(dev);
378         struct dsa_switch *ds = p->parent;
379
380         if (switchdev_trans_ph_prepare(trans))
381                 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
382
383         dsa_port_set_stp_state(ds, p->port, attr->u.stp_state);
384
385         return 0;
386 }
387
388 static int dsa_slave_vlan_filtering(struct net_device *dev,
389                                     const struct switchdev_attr *attr,
390                                     struct switchdev_trans *trans)
391 {
392         struct dsa_slave_priv *p = netdev_priv(dev);
393         struct dsa_switch *ds = p->parent;
394
395         /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
396         if (switchdev_trans_ph_prepare(trans))
397                 return 0;
398
399         if (ds->ops->port_vlan_filtering)
400                 return ds->ops->port_vlan_filtering(ds, p->port,
401                                                     attr->u.vlan_filtering);
402
403         return 0;
404 }
405
406 static int dsa_fastest_ageing_time(struct dsa_switch *ds,
407                                    unsigned int ageing_time)
408 {
409         int i;
410
411         for (i = 0; i < DSA_MAX_PORTS; ++i) {
412                 struct dsa_port *dp = &ds->ports[i];
413
414                 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
415                         ageing_time = dp->ageing_time;
416         }
417
418         return ageing_time;
419 }
420
421 static int dsa_slave_ageing_time(struct net_device *dev,
422                                  const struct switchdev_attr *attr,
423                                  struct switchdev_trans *trans)
424 {
425         struct dsa_slave_priv *p = netdev_priv(dev);
426         struct dsa_switch *ds = p->parent;
427         unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
428         unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
429
430         /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
431         if (switchdev_trans_ph_prepare(trans))
432                 return 0;
433
434         /* Keep the fastest ageing time in case of multiple bridges */
435         ds->ports[p->port].ageing_time = ageing_time;
436         ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
437
438         if (ds->ops->set_ageing_time)
439                 return ds->ops->set_ageing_time(ds, ageing_time);
440
441         return 0;
442 }
443
444 static int dsa_slave_port_attr_set(struct net_device *dev,
445                                    const struct switchdev_attr *attr,
446                                    struct switchdev_trans *trans)
447 {
448         int ret;
449
450         switch (attr->id) {
451         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
452                 ret = dsa_slave_stp_state_set(dev, attr, trans);
453                 break;
454         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
455                 ret = dsa_slave_vlan_filtering(dev, attr, trans);
456                 break;
457         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
458                 ret = dsa_slave_ageing_time(dev, attr, trans);
459                 break;
460         default:
461                 ret = -EOPNOTSUPP;
462                 break;
463         }
464
465         return ret;
466 }
467
468 static int dsa_slave_port_obj_add(struct net_device *dev,
469                                   const struct switchdev_obj *obj,
470                                   struct switchdev_trans *trans)
471 {
472         int err;
473
474         /* For the prepare phase, ensure the full set of changes is feasable in
475          * one go in order to signal a failure properly. If an operation is not
476          * supported, return -EOPNOTSUPP.
477          */
478
479         switch (obj->id) {
480         case SWITCHDEV_OBJ_ID_PORT_FDB:
481                 err = dsa_slave_port_fdb_add(dev,
482                                              SWITCHDEV_OBJ_PORT_FDB(obj),
483                                              trans);
484                 break;
485         case SWITCHDEV_OBJ_ID_PORT_MDB:
486                 err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
487                                              trans);
488                 break;
489         case SWITCHDEV_OBJ_ID_PORT_VLAN:
490                 err = dsa_slave_port_vlan_add(dev,
491                                               SWITCHDEV_OBJ_PORT_VLAN(obj),
492                                               trans);
493                 break;
494         default:
495                 err = -EOPNOTSUPP;
496                 break;
497         }
498
499         return err;
500 }
501
502 static int dsa_slave_port_obj_del(struct net_device *dev,
503                                   const struct switchdev_obj *obj)
504 {
505         int err;
506
507         switch (obj->id) {
508         case SWITCHDEV_OBJ_ID_PORT_FDB:
509                 err = dsa_slave_port_fdb_del(dev,
510                                              SWITCHDEV_OBJ_PORT_FDB(obj));
511                 break;
512         case SWITCHDEV_OBJ_ID_PORT_MDB:
513                 err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
514                 break;
515         case SWITCHDEV_OBJ_ID_PORT_VLAN:
516                 err = dsa_slave_port_vlan_del(dev,
517                                               SWITCHDEV_OBJ_PORT_VLAN(obj));
518                 break;
519         default:
520                 err = -EOPNOTSUPP;
521                 break;
522         }
523
524         return err;
525 }
526
527 static int dsa_slave_port_obj_dump(struct net_device *dev,
528                                    struct switchdev_obj *obj,
529                                    switchdev_obj_dump_cb_t *cb)
530 {
531         int err;
532
533         switch (obj->id) {
534         case SWITCHDEV_OBJ_ID_PORT_FDB:
535                 err = dsa_slave_port_fdb_dump(dev,
536                                               SWITCHDEV_OBJ_PORT_FDB(obj),
537                                               cb);
538                 break;
539         case SWITCHDEV_OBJ_ID_PORT_MDB:
540                 err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
541                                               cb);
542                 break;
543         case SWITCHDEV_OBJ_ID_PORT_VLAN:
544                 err = dsa_slave_port_vlan_dump(dev,
545                                                SWITCHDEV_OBJ_PORT_VLAN(obj),
546                                                cb);
547                 break;
548         default:
549                 err = -EOPNOTSUPP;
550                 break;
551         }
552
553         return err;
554 }
555
556 static int dsa_slave_bridge_port_join(struct net_device *dev,
557                                       struct net_device *br)
558 {
559         struct dsa_slave_priv *p = netdev_priv(dev);
560         struct dsa_switch *ds = p->parent;
561         int ret = -EOPNOTSUPP;
562
563         p->bridge_dev = br;
564
565         if (ds->ops->port_bridge_join)
566                 ret = ds->ops->port_bridge_join(ds, p->port, br);
567
568         return ret == -EOPNOTSUPP ? 0 : ret;
569 }
570
571 static void dsa_slave_bridge_port_leave(struct net_device *dev)
572 {
573         struct dsa_slave_priv *p = netdev_priv(dev);
574         struct dsa_switch *ds = p->parent;
575
576
577         if (ds->ops->port_bridge_leave)
578                 ds->ops->port_bridge_leave(ds, p->port);
579
580         p->bridge_dev = NULL;
581
582         /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
583          * so allow it to be in BR_STATE_FORWARDING to be kept functional
584          */
585         dsa_port_set_stp_state(ds, p->port, BR_STATE_FORWARDING);
586 }
587
588 static int dsa_slave_port_attr_get(struct net_device *dev,
589                                    struct switchdev_attr *attr)
590 {
591         struct dsa_slave_priv *p = netdev_priv(dev);
592         struct dsa_switch *ds = p->parent;
593
594         switch (attr->id) {
595         case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
596                 attr->u.ppid.id_len = sizeof(ds->index);
597                 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
598                 break;
599         default:
600                 return -EOPNOTSUPP;
601         }
602
603         return 0;
604 }
605
606 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
607                                                struct sk_buff *skb)
608 {
609 #ifdef CONFIG_NET_POLL_CONTROLLER
610         if (p->netpoll)
611                 netpoll_send_skb(p->netpoll, skb);
612 #else
613         BUG();
614 #endif
615         return NETDEV_TX_OK;
616 }
617
618 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
619 {
620         struct dsa_slave_priv *p = netdev_priv(dev);
621         struct sk_buff *nskb;
622
623         dev->stats.tx_packets++;
624         dev->stats.tx_bytes += skb->len;
625
626         /* Transmit function may have to reallocate the original SKB */
627         nskb = p->xmit(skb, dev);
628         if (!nskb)
629                 return NETDEV_TX_OK;
630
631         /* SKB for netpoll still need to be mangled with the protocol-specific
632          * tag to be successfully transmitted
633          */
634         if (unlikely(netpoll_tx_running(dev)))
635                 return dsa_netpoll_send_skb(p, nskb);
636
637         /* Queue the SKB for transmission on the parent interface, but
638          * do not modify its EtherType
639          */
640         nskb->dev = p->parent->dst->master_netdev;
641         dev_queue_xmit(nskb);
642
643         return NETDEV_TX_OK;
644 }
645
646 /* ethtool operations *******************************************************/
647 static int
648 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
649 {
650         struct dsa_slave_priv *p = netdev_priv(dev);
651         int err;
652
653         err = -EOPNOTSUPP;
654         if (p->phy != NULL) {
655                 err = phy_read_status(p->phy);
656                 if (err == 0)
657                         err = phy_ethtool_gset(p->phy, cmd);
658         }
659
660         return err;
661 }
662
663 static int
664 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
665 {
666         struct dsa_slave_priv *p = netdev_priv(dev);
667
668         if (p->phy != NULL)
669                 return phy_ethtool_sset(p->phy, cmd);
670
671         return -EOPNOTSUPP;
672 }
673
674 static void dsa_slave_get_drvinfo(struct net_device *dev,
675                                   struct ethtool_drvinfo *drvinfo)
676 {
677         strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
678         strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
679         strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
680         strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
681 }
682
683 static int dsa_slave_get_regs_len(struct net_device *dev)
684 {
685         struct dsa_slave_priv *p = netdev_priv(dev);
686         struct dsa_switch *ds = p->parent;
687
688         if (ds->ops->get_regs_len)
689                 return ds->ops->get_regs_len(ds, p->port);
690
691         return -EOPNOTSUPP;
692 }
693
694 static void
695 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
696 {
697         struct dsa_slave_priv *p = netdev_priv(dev);
698         struct dsa_switch *ds = p->parent;
699
700         if (ds->ops->get_regs)
701                 ds->ops->get_regs(ds, p->port, regs, _p);
702 }
703
704 static int dsa_slave_nway_reset(struct net_device *dev)
705 {
706         struct dsa_slave_priv *p = netdev_priv(dev);
707
708         if (p->phy != NULL)
709                 return genphy_restart_aneg(p->phy);
710
711         return -EOPNOTSUPP;
712 }
713
714 static u32 dsa_slave_get_link(struct net_device *dev)
715 {
716         struct dsa_slave_priv *p = netdev_priv(dev);
717
718         if (p->phy != NULL) {
719                 genphy_update_link(p->phy);
720                 return p->phy->link;
721         }
722
723         return -EOPNOTSUPP;
724 }
725
726 static int dsa_slave_get_eeprom_len(struct net_device *dev)
727 {
728         struct dsa_slave_priv *p = netdev_priv(dev);
729         struct dsa_switch *ds = p->parent;
730
731         if (ds->cd && ds->cd->eeprom_len)
732                 return ds->cd->eeprom_len;
733
734         if (ds->ops->get_eeprom_len)
735                 return ds->ops->get_eeprom_len(ds);
736
737         return 0;
738 }
739
740 static int dsa_slave_get_eeprom(struct net_device *dev,
741                                 struct ethtool_eeprom *eeprom, u8 *data)
742 {
743         struct dsa_slave_priv *p = netdev_priv(dev);
744         struct dsa_switch *ds = p->parent;
745
746         if (ds->ops->get_eeprom)
747                 return ds->ops->get_eeprom(ds, eeprom, data);
748
749         return -EOPNOTSUPP;
750 }
751
752 static int dsa_slave_set_eeprom(struct net_device *dev,
753                                 struct ethtool_eeprom *eeprom, u8 *data)
754 {
755         struct dsa_slave_priv *p = netdev_priv(dev);
756         struct dsa_switch *ds = p->parent;
757
758         if (ds->ops->set_eeprom)
759                 return ds->ops->set_eeprom(ds, eeprom, data);
760
761         return -EOPNOTSUPP;
762 }
763
764 static void dsa_slave_get_strings(struct net_device *dev,
765                                   uint32_t stringset, uint8_t *data)
766 {
767         struct dsa_slave_priv *p = netdev_priv(dev);
768         struct dsa_switch *ds = p->parent;
769
770         if (stringset == ETH_SS_STATS) {
771                 int len = ETH_GSTRING_LEN;
772
773                 strncpy(data, "tx_packets", len);
774                 strncpy(data + len, "tx_bytes", len);
775                 strncpy(data + 2 * len, "rx_packets", len);
776                 strncpy(data + 3 * len, "rx_bytes", len);
777                 if (ds->ops->get_strings)
778                         ds->ops->get_strings(ds, p->port, data + 4 * len);
779         }
780 }
781
782 static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
783                                            struct ethtool_stats *stats,
784                                            uint64_t *data)
785 {
786         struct dsa_switch_tree *dst = dev->dsa_ptr;
787         struct dsa_switch *ds = dst->ds[0];
788         s8 cpu_port = dst->cpu_port;
789         int count = 0;
790
791         if (dst->master_ethtool_ops.get_sset_count) {
792                 count = dst->master_ethtool_ops.get_sset_count(dev,
793                                                                ETH_SS_STATS);
794                 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
795         }
796
797         if (ds->ops->get_ethtool_stats)
798                 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
799 }
800
801 static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
802 {
803         struct dsa_switch_tree *dst = dev->dsa_ptr;
804         struct dsa_switch *ds = dst->ds[0];
805         int count = 0;
806
807         if (dst->master_ethtool_ops.get_sset_count)
808                 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
809
810         if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
811                 count += ds->ops->get_sset_count(ds);
812
813         return count;
814 }
815
816 static void dsa_cpu_port_get_strings(struct net_device *dev,
817                                      uint32_t stringset, uint8_t *data)
818 {
819         struct dsa_switch_tree *dst = dev->dsa_ptr;
820         struct dsa_switch *ds = dst->ds[0];
821         s8 cpu_port = dst->cpu_port;
822         int len = ETH_GSTRING_LEN;
823         int mcount = 0, count;
824         unsigned int i;
825         uint8_t pfx[4];
826         uint8_t *ndata;
827
828         snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
829         /* We do not want to be NULL-terminated, since this is a prefix */
830         pfx[sizeof(pfx) - 1] = '_';
831
832         if (dst->master_ethtool_ops.get_sset_count) {
833                 mcount = dst->master_ethtool_ops.get_sset_count(dev,
834                                                                 ETH_SS_STATS);
835                 dst->master_ethtool_ops.get_strings(dev, stringset, data);
836         }
837
838         if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
839                 ndata = data + mcount * len;
840                 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
841                  * the output after to prepend our CPU port prefix we
842                  * constructed earlier
843                  */
844                 ds->ops->get_strings(ds, cpu_port, ndata);
845                 count = ds->ops->get_sset_count(ds);
846                 for (i = 0; i < count; i++) {
847                         memmove(ndata + (i * len + sizeof(pfx)),
848                                 ndata + i * len, len - sizeof(pfx));
849                         memcpy(ndata + i * len, pfx, sizeof(pfx));
850                 }
851         }
852 }
853
854 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
855                                         struct ethtool_stats *stats,
856                                         uint64_t *data)
857 {
858         struct dsa_slave_priv *p = netdev_priv(dev);
859         struct dsa_switch *ds = p->parent;
860
861         data[0] = dev->stats.tx_packets;
862         data[1] = dev->stats.tx_bytes;
863         data[2] = dev->stats.rx_packets;
864         data[3] = dev->stats.rx_bytes;
865         if (ds->ops->get_ethtool_stats)
866                 ds->ops->get_ethtool_stats(ds, p->port, data + 4);
867 }
868
869 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
870 {
871         struct dsa_slave_priv *p = netdev_priv(dev);
872         struct dsa_switch *ds = p->parent;
873
874         if (sset == ETH_SS_STATS) {
875                 int count;
876
877                 count = 4;
878                 if (ds->ops->get_sset_count)
879                         count += ds->ops->get_sset_count(ds);
880
881                 return count;
882         }
883
884         return -EOPNOTSUPP;
885 }
886
887 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
888 {
889         struct dsa_slave_priv *p = netdev_priv(dev);
890         struct dsa_switch *ds = p->parent;
891
892         if (ds->ops->get_wol)
893                 ds->ops->get_wol(ds, p->port, w);
894 }
895
896 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
897 {
898         struct dsa_slave_priv *p = netdev_priv(dev);
899         struct dsa_switch *ds = p->parent;
900         int ret = -EOPNOTSUPP;
901
902         if (ds->ops->set_wol)
903                 ret = ds->ops->set_wol(ds, p->port, w);
904
905         return ret;
906 }
907
908 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
909 {
910         struct dsa_slave_priv *p = netdev_priv(dev);
911         struct dsa_switch *ds = p->parent;
912         int ret;
913
914         if (!ds->ops->set_eee)
915                 return -EOPNOTSUPP;
916
917         ret = ds->ops->set_eee(ds, p->port, p->phy, e);
918         if (ret)
919                 return ret;
920
921         if (p->phy)
922                 ret = phy_ethtool_set_eee(p->phy, e);
923
924         return ret;
925 }
926
927 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
928 {
929         struct dsa_slave_priv *p = netdev_priv(dev);
930         struct dsa_switch *ds = p->parent;
931         int ret;
932
933         if (!ds->ops->get_eee)
934                 return -EOPNOTSUPP;
935
936         ret = ds->ops->get_eee(ds, p->port, e);
937         if (ret)
938                 return ret;
939
940         if (p->phy)
941                 ret = phy_ethtool_get_eee(p->phy, e);
942
943         return ret;
944 }
945
946 #ifdef CONFIG_NET_POLL_CONTROLLER
947 static int dsa_slave_netpoll_setup(struct net_device *dev,
948                                    struct netpoll_info *ni)
949 {
950         struct dsa_slave_priv *p = netdev_priv(dev);
951         struct dsa_switch *ds = p->parent;
952         struct net_device *master = ds->dst->master_netdev;
953         struct netpoll *netpoll;
954         int err = 0;
955
956         netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
957         if (!netpoll)
958                 return -ENOMEM;
959
960         err = __netpoll_setup(netpoll, master);
961         if (err) {
962                 kfree(netpoll);
963                 goto out;
964         }
965
966         p->netpoll = netpoll;
967 out:
968         return err;
969 }
970
971 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
972 {
973         struct dsa_slave_priv *p = netdev_priv(dev);
974         struct netpoll *netpoll = p->netpoll;
975
976         if (!netpoll)
977                 return;
978
979         p->netpoll = NULL;
980
981         __netpoll_free_async(netpoll);
982 }
983
984 static void dsa_slave_poll_controller(struct net_device *dev)
985 {
986 }
987 #endif
988
989 void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
990 {
991         ops->get_sset_count = dsa_cpu_port_get_sset_count;
992         ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
993         ops->get_strings = dsa_cpu_port_get_strings;
994 }
995
996 static const struct ethtool_ops dsa_slave_ethtool_ops = {
997         .get_settings           = dsa_slave_get_settings,
998         .set_settings           = dsa_slave_set_settings,
999         .get_drvinfo            = dsa_slave_get_drvinfo,
1000         .get_regs_len           = dsa_slave_get_regs_len,
1001         .get_regs               = dsa_slave_get_regs,
1002         .nway_reset             = dsa_slave_nway_reset,
1003         .get_link               = dsa_slave_get_link,
1004         .get_eeprom_len         = dsa_slave_get_eeprom_len,
1005         .get_eeprom             = dsa_slave_get_eeprom,
1006         .set_eeprom             = dsa_slave_set_eeprom,
1007         .get_strings            = dsa_slave_get_strings,
1008         .get_ethtool_stats      = dsa_slave_get_ethtool_stats,
1009         .get_sset_count         = dsa_slave_get_sset_count,
1010         .set_wol                = dsa_slave_set_wol,
1011         .get_wol                = dsa_slave_get_wol,
1012         .set_eee                = dsa_slave_set_eee,
1013         .get_eee                = dsa_slave_get_eee,
1014 };
1015
1016 static const struct net_device_ops dsa_slave_netdev_ops = {
1017         .ndo_open               = dsa_slave_open,
1018         .ndo_stop               = dsa_slave_close,
1019         .ndo_start_xmit         = dsa_slave_xmit,
1020         .ndo_change_rx_flags    = dsa_slave_change_rx_flags,
1021         .ndo_set_rx_mode        = dsa_slave_set_rx_mode,
1022         .ndo_set_mac_address    = dsa_slave_set_mac_address,
1023         .ndo_fdb_add            = switchdev_port_fdb_add,
1024         .ndo_fdb_del            = switchdev_port_fdb_del,
1025         .ndo_fdb_dump           = switchdev_port_fdb_dump,
1026         .ndo_do_ioctl           = dsa_slave_ioctl,
1027         .ndo_get_iflink         = dsa_slave_get_iflink,
1028 #ifdef CONFIG_NET_POLL_CONTROLLER
1029         .ndo_netpoll_setup      = dsa_slave_netpoll_setup,
1030         .ndo_netpoll_cleanup    = dsa_slave_netpoll_cleanup,
1031         .ndo_poll_controller    = dsa_slave_poll_controller,
1032 #endif
1033         .ndo_bridge_getlink     = switchdev_port_bridge_getlink,
1034         .ndo_bridge_setlink     = switchdev_port_bridge_setlink,
1035         .ndo_bridge_dellink     = switchdev_port_bridge_dellink,
1036 };
1037
1038 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1039         .switchdev_port_attr_get        = dsa_slave_port_attr_get,
1040         .switchdev_port_attr_set        = dsa_slave_port_attr_set,
1041         .switchdev_port_obj_add         = dsa_slave_port_obj_add,
1042         .switchdev_port_obj_del         = dsa_slave_port_obj_del,
1043         .switchdev_port_obj_dump        = dsa_slave_port_obj_dump,
1044 };
1045
1046 static struct device_type dsa_type = {
1047         .name   = "dsa",
1048 };
1049
1050 static void dsa_slave_adjust_link(struct net_device *dev)
1051 {
1052         struct dsa_slave_priv *p = netdev_priv(dev);
1053         struct dsa_switch *ds = p->parent;
1054         unsigned int status_changed = 0;
1055
1056         if (p->old_link != p->phy->link) {
1057                 status_changed = 1;
1058                 p->old_link = p->phy->link;
1059         }
1060
1061         if (p->old_duplex != p->phy->duplex) {
1062                 status_changed = 1;
1063                 p->old_duplex = p->phy->duplex;
1064         }
1065
1066         if (p->old_pause != p->phy->pause) {
1067                 status_changed = 1;
1068                 p->old_pause = p->phy->pause;
1069         }
1070
1071         if (ds->ops->adjust_link && status_changed)
1072                 ds->ops->adjust_link(ds, p->port, p->phy);
1073
1074         if (status_changed)
1075                 phy_print_status(p->phy);
1076 }
1077
1078 static int dsa_slave_fixed_link_update(struct net_device *dev,
1079                                        struct fixed_phy_status *status)
1080 {
1081         struct dsa_slave_priv *p;
1082         struct dsa_switch *ds;
1083
1084         if (dev) {
1085                 p = netdev_priv(dev);
1086                 ds = p->parent;
1087                 if (ds->ops->fixed_link_update)
1088                         ds->ops->fixed_link_update(ds, p->port, status);
1089         }
1090
1091         return 0;
1092 }
1093
1094 /* slave device setup *******************************************************/
1095 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1096                                  struct net_device *slave_dev,
1097                                  int addr)
1098 {
1099         struct dsa_switch *ds = p->parent;
1100
1101         p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1102         if (!p->phy) {
1103                 netdev_err(slave_dev, "no phy at %d\n", addr);
1104                 return -ENODEV;
1105         }
1106
1107         /* Use already configured phy mode */
1108         if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1109                 p->phy_interface = p->phy->interface;
1110         return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1111                                   p->phy_interface);
1112 }
1113
1114 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1115                                 struct net_device *slave_dev)
1116 {
1117         struct dsa_switch *ds = p->parent;
1118         struct device_node *phy_dn, *port_dn;
1119         bool phy_is_fixed = false;
1120         u32 phy_flags = 0;
1121         int mode, ret;
1122
1123         port_dn = ds->ports[p->port].dn;
1124         mode = of_get_phy_mode(port_dn);
1125         if (mode < 0)
1126                 mode = PHY_INTERFACE_MODE_NA;
1127         p->phy_interface = mode;
1128
1129         phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1130         if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1131                 /* In the case of a fixed PHY, the DT node associated
1132                  * to the fixed PHY is the Port DT node
1133                  */
1134                 ret = of_phy_register_fixed_link(port_dn);
1135                 if (ret) {
1136                         netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1137                         return ret;
1138                 }
1139                 phy_is_fixed = true;
1140                 phy_dn = of_node_get(port_dn);
1141         }
1142
1143         if (ds->ops->get_phy_flags)
1144                 phy_flags = ds->ops->get_phy_flags(ds, p->port);
1145
1146         if (phy_dn) {
1147                 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1148
1149                 /* If this PHY address is part of phys_mii_mask, which means
1150                  * that we need to divert reads and writes to/from it, then we
1151                  * want to bind this device using the slave MII bus created by
1152                  * DSA to make that happen.
1153                  */
1154                 if (!phy_is_fixed && phy_id >= 0 &&
1155                     (ds->phys_mii_mask & (1 << phy_id))) {
1156                         ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1157                         if (ret) {
1158                                 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1159                                 of_node_put(phy_dn);
1160                                 return ret;
1161                         }
1162                 } else {
1163                         p->phy = of_phy_connect(slave_dev, phy_dn,
1164                                                 dsa_slave_adjust_link,
1165                                                 phy_flags,
1166                                                 p->phy_interface);
1167                 }
1168
1169                 of_node_put(phy_dn);
1170         }
1171
1172         if (p->phy && phy_is_fixed)
1173                 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1174
1175         /* We could not connect to a designated PHY, so use the switch internal
1176          * MDIO bus instead
1177          */
1178         if (!p->phy) {
1179                 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
1180                 if (ret) {
1181                         netdev_err(slave_dev, "failed to connect to port %d: %d\n", p->port, ret);
1182                         if (phy_is_fixed)
1183                                 of_phy_deregister_fixed_link(port_dn);
1184                         return ret;
1185                 }
1186         }
1187
1188         phy_attached_info(p->phy);
1189
1190         return 0;
1191 }
1192
1193 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1194 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1195                                             struct netdev_queue *txq,
1196                                             void *_unused)
1197 {
1198         lockdep_set_class(&txq->_xmit_lock,
1199                           &dsa_slave_netdev_xmit_lock_key);
1200 }
1201
1202 int dsa_slave_suspend(struct net_device *slave_dev)
1203 {
1204         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1205
1206         if (!netif_running(slave_dev))
1207                 return 0;
1208
1209         netif_device_detach(slave_dev);
1210
1211         if (p->phy) {
1212                 phy_stop(p->phy);
1213                 p->old_pause = -1;
1214                 p->old_link = -1;
1215                 p->old_duplex = -1;
1216                 phy_suspend(p->phy);
1217         }
1218
1219         return 0;
1220 }
1221
1222 int dsa_slave_resume(struct net_device *slave_dev)
1223 {
1224         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1225
1226         if (!netif_running(slave_dev))
1227                 return 0;
1228
1229         netif_device_attach(slave_dev);
1230
1231         if (p->phy) {
1232                 phy_resume(p->phy);
1233                 phy_start(p->phy);
1234         }
1235
1236         return 0;
1237 }
1238
1239 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1240                      int port, const char *name)
1241 {
1242         struct dsa_switch_tree *dst = ds->dst;
1243         struct net_device *master;
1244         struct net_device *slave_dev;
1245         struct dsa_slave_priv *p;
1246         int ret;
1247
1248         master = ds->dst->master_netdev;
1249         if (ds->master_netdev)
1250                 master = ds->master_netdev;
1251
1252         slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1253                                  NET_NAME_UNKNOWN, ether_setup);
1254         if (slave_dev == NULL)
1255                 return -ENOMEM;
1256
1257         slave_dev->features = master->vlan_features;
1258         slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1259         eth_hw_addr_inherit(slave_dev, master);
1260         slave_dev->priv_flags |= IFF_NO_QUEUE;
1261         slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1262         slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1263         SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1264
1265         netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1266                                  NULL);
1267
1268         SET_NETDEV_DEV(slave_dev, parent);
1269         slave_dev->dev.of_node = ds->ports[port].dn;
1270         slave_dev->vlan_features = master->vlan_features;
1271
1272         p = netdev_priv(slave_dev);
1273         p->parent = ds;
1274         p->port = port;
1275         p->xmit = dst->tag_ops->xmit;
1276
1277         p->old_pause = -1;
1278         p->old_link = -1;
1279         p->old_duplex = -1;
1280
1281         ds->ports[port].netdev = slave_dev;
1282
1283         netif_carrier_off(slave_dev);
1284
1285         ret = dsa_slave_phy_setup(p, slave_dev);
1286         if (ret) {
1287                 netdev_err(master, "error %d setting up slave phy\n", ret);
1288                 goto out_free;
1289         }
1290
1291         ret = register_netdev(slave_dev);
1292         if (ret) {
1293                 netdev_err(master, "error %d registering interface %s\n",
1294                            ret, slave_dev->name);
1295                 goto out_phy;
1296         }
1297
1298         return 0;
1299
1300 out_phy:
1301         phy_disconnect(p->phy);
1302         if (of_phy_is_fixed_link(ds->ports[port].dn))
1303                 of_phy_deregister_fixed_link(ds->ports[port].dn);
1304 out_free:
1305         free_netdev(slave_dev);
1306         ds->ports[port].netdev = NULL;
1307         return ret;
1308 }
1309
1310 void dsa_slave_destroy(struct net_device *slave_dev)
1311 {
1312         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1313         struct dsa_switch *ds = p->parent;
1314         struct device_node *port_dn;
1315
1316         port_dn = ds->ports[p->port].dn;
1317
1318         netif_carrier_off(slave_dev);
1319         if (p->phy) {
1320                 phy_disconnect(p->phy);
1321
1322                 if (of_phy_is_fixed_link(port_dn))
1323                         of_phy_deregister_fixed_link(port_dn);
1324         }
1325         unregister_netdev(slave_dev);
1326         free_netdev(slave_dev);
1327 }
1328
1329 static bool dsa_slave_dev_check(struct net_device *dev)
1330 {
1331         return dev->netdev_ops == &dsa_slave_netdev_ops;
1332 }
1333
1334 static int dsa_slave_port_upper_event(struct net_device *dev,
1335                                       unsigned long event, void *ptr)
1336 {
1337         struct netdev_notifier_changeupper_info *info = ptr;
1338         struct net_device *upper = info->upper_dev;
1339         int err = 0;
1340
1341         switch (event) {
1342         case NETDEV_CHANGEUPPER:
1343                 if (netif_is_bridge_master(upper)) {
1344                         if (info->linking)
1345                                 err = dsa_slave_bridge_port_join(dev, upper);
1346                         else
1347                                 dsa_slave_bridge_port_leave(dev);
1348                 }
1349
1350                 break;
1351         }
1352
1353         return notifier_from_errno(err);
1354 }
1355
1356 static int dsa_slave_port_event(struct net_device *dev, unsigned long event,
1357                                 void *ptr)
1358 {
1359         switch (event) {
1360         case NETDEV_CHANGEUPPER:
1361                 return dsa_slave_port_upper_event(dev, event, ptr);
1362         }
1363
1364         return NOTIFY_DONE;
1365 }
1366
1367 int dsa_slave_netdevice_event(struct notifier_block *unused,
1368                               unsigned long event, void *ptr)
1369 {
1370         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1371
1372         if (dsa_slave_dev_check(dev))
1373                 return dsa_slave_port_event(dev, event, ptr);
1374
1375         return NOTIFY_DONE;
1376 }