]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/dsa/slave.c
mpls: In mpls_egress verify the packet length.
[karo-tx-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 <net/rtnetlink.h>
19 #include <linux/if_bridge.h>
20 #include "dsa_priv.h"
21
22 /* slave mii_bus handling ***************************************************/
23 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
24 {
25         struct dsa_switch *ds = bus->priv;
26
27         if (ds->phys_mii_mask & (1 << addr))
28                 return ds->drv->phy_read(ds, addr, reg);
29
30         return 0xffff;
31 }
32
33 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
34 {
35         struct dsa_switch *ds = bus->priv;
36
37         if (ds->phys_mii_mask & (1 << addr))
38                 return ds->drv->phy_write(ds, addr, reg, val);
39
40         return 0;
41 }
42
43 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
44 {
45         ds->slave_mii_bus->priv = (void *)ds;
46         ds->slave_mii_bus->name = "dsa slave smi";
47         ds->slave_mii_bus->read = dsa_slave_phy_read;
48         ds->slave_mii_bus->write = dsa_slave_phy_write;
49         snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
50                         ds->index, ds->pd->sw_addr);
51         ds->slave_mii_bus->parent = ds->master_dev;
52         ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
53 }
54
55
56 /* slave device handling ****************************************************/
57 static int dsa_slave_init(struct net_device *dev)
58 {
59         struct dsa_slave_priv *p = netdev_priv(dev);
60
61         dev->iflink = p->parent->dst->master_netdev->ifindex;
62
63         return 0;
64 }
65
66 static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
67 {
68         return !!p->bridge_dev;
69 }
70
71 static int dsa_slave_open(struct net_device *dev)
72 {
73         struct dsa_slave_priv *p = netdev_priv(dev);
74         struct net_device *master = p->parent->dst->master_netdev;
75         struct dsa_switch *ds = p->parent;
76         u8 stp_state = dsa_port_is_bridged(p) ?
77                         BR_STATE_BLOCKING : BR_STATE_FORWARDING;
78         int err;
79
80         if (!(master->flags & IFF_UP))
81                 return -ENETDOWN;
82
83         if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
84                 err = dev_uc_add(master, dev->dev_addr);
85                 if (err < 0)
86                         goto out;
87         }
88
89         if (dev->flags & IFF_ALLMULTI) {
90                 err = dev_set_allmulti(master, 1);
91                 if (err < 0)
92                         goto del_unicast;
93         }
94         if (dev->flags & IFF_PROMISC) {
95                 err = dev_set_promiscuity(master, 1);
96                 if (err < 0)
97                         goto clear_allmulti;
98         }
99
100         if (ds->drv->port_enable) {
101                 err = ds->drv->port_enable(ds, p->port, p->phy);
102                 if (err)
103                         goto clear_promisc;
104         }
105
106         if (ds->drv->port_stp_update)
107                 ds->drv->port_stp_update(ds, p->port, stp_state);
108
109         if (p->phy)
110                 phy_start(p->phy);
111
112         return 0;
113
114 clear_promisc:
115         if (dev->flags & IFF_PROMISC)
116                 dev_set_promiscuity(master, 0);
117 clear_allmulti:
118         if (dev->flags & IFF_ALLMULTI)
119                 dev_set_allmulti(master, -1);
120 del_unicast:
121         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
122                 dev_uc_del(master, dev->dev_addr);
123 out:
124         return err;
125 }
126
127 static int dsa_slave_close(struct net_device *dev)
128 {
129         struct dsa_slave_priv *p = netdev_priv(dev);
130         struct net_device *master = p->parent->dst->master_netdev;
131         struct dsa_switch *ds = p->parent;
132
133         if (p->phy)
134                 phy_stop(p->phy);
135
136         dev_mc_unsync(master, dev);
137         dev_uc_unsync(master, dev);
138         if (dev->flags & IFF_ALLMULTI)
139                 dev_set_allmulti(master, -1);
140         if (dev->flags & IFF_PROMISC)
141                 dev_set_promiscuity(master, -1);
142
143         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
144                 dev_uc_del(master, dev->dev_addr);
145
146         if (ds->drv->port_disable)
147                 ds->drv->port_disable(ds, p->port, p->phy);
148
149         if (ds->drv->port_stp_update)
150                 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
151
152         return 0;
153 }
154
155 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
156 {
157         struct dsa_slave_priv *p = netdev_priv(dev);
158         struct net_device *master = p->parent->dst->master_netdev;
159
160         if (change & IFF_ALLMULTI)
161                 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
162         if (change & IFF_PROMISC)
163                 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
164 }
165
166 static void dsa_slave_set_rx_mode(struct net_device *dev)
167 {
168         struct dsa_slave_priv *p = netdev_priv(dev);
169         struct net_device *master = p->parent->dst->master_netdev;
170
171         dev_mc_sync(master, dev);
172         dev_uc_sync(master, dev);
173 }
174
175 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
176 {
177         struct dsa_slave_priv *p = netdev_priv(dev);
178         struct net_device *master = p->parent->dst->master_netdev;
179         struct sockaddr *addr = a;
180         int err;
181
182         if (!is_valid_ether_addr(addr->sa_data))
183                 return -EADDRNOTAVAIL;
184
185         if (!(dev->flags & IFF_UP))
186                 goto out;
187
188         if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
189                 err = dev_uc_add(master, addr->sa_data);
190                 if (err < 0)
191                         return err;
192         }
193
194         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
195                 dev_uc_del(master, dev->dev_addr);
196
197 out:
198         ether_addr_copy(dev->dev_addr, addr->sa_data);
199
200         return 0;
201 }
202
203 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
204 {
205         struct dsa_slave_priv *p = netdev_priv(dev);
206
207         if (p->phy != NULL)
208                 return phy_mii_ioctl(p->phy, ifr, cmd);
209
210         return -EOPNOTSUPP;
211 }
212
213 /* Return a bitmask of all ports being currently bridged within a given bridge
214  * device. Note that on leave, the mask will still return the bitmask of ports
215  * currently bridged, prior to port removal, and this is exactly what we want.
216  */
217 static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
218                                   struct net_device *bridge)
219 {
220         struct dsa_slave_priv *p;
221         unsigned int port;
222         u32 mask = 0;
223
224         for (port = 0; port < DSA_MAX_PORTS; port++) {
225                 if (!dsa_is_port_initialized(ds, port))
226                         continue;
227
228                 p = netdev_priv(ds->ports[port]);
229
230                 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
231                     p->bridge_dev == bridge)
232                         mask |= 1 << port;
233         }
234
235         return mask;
236 }
237
238 static int dsa_slave_stp_update(struct net_device *dev, u8 state)
239 {
240         struct dsa_slave_priv *p = netdev_priv(dev);
241         struct dsa_switch *ds = p->parent;
242         int ret = -EOPNOTSUPP;
243
244         if (ds->drv->port_stp_update)
245                 ret = ds->drv->port_stp_update(ds, p->port, state);
246
247         return ret;
248 }
249
250 static int dsa_slave_bridge_port_join(struct net_device *dev,
251                                       struct net_device *br)
252 {
253         struct dsa_slave_priv *p = netdev_priv(dev);
254         struct dsa_switch *ds = p->parent;
255         int ret = -EOPNOTSUPP;
256
257         p->bridge_dev = br;
258
259         if (ds->drv->port_join_bridge)
260                 ret = ds->drv->port_join_bridge(ds, p->port,
261                                                 dsa_slave_br_port_mask(ds, br));
262
263         return ret;
264 }
265
266 static int dsa_slave_bridge_port_leave(struct net_device *dev)
267 {
268         struct dsa_slave_priv *p = netdev_priv(dev);
269         struct dsa_switch *ds = p->parent;
270         int ret = -EOPNOTSUPP;
271
272
273         if (ds->drv->port_leave_bridge)
274                 ret = ds->drv->port_leave_bridge(ds, p->port,
275                                                  dsa_slave_br_port_mask(ds, p->bridge_dev));
276
277         p->bridge_dev = NULL;
278
279         /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
280          * so allow it to be in BR_STATE_FORWARDING to be kept functional
281          */
282         dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
283
284         return ret;
285 }
286
287 static int dsa_slave_parent_id_get(struct net_device *dev,
288                                    struct netdev_phys_item_id *psid)
289 {
290         struct dsa_slave_priv *p = netdev_priv(dev);
291         struct dsa_switch *ds = p->parent;
292
293         psid->id_len = sizeof(ds->index);
294         memcpy(&psid->id, &ds->index, psid->id_len);
295
296         return 0;
297 }
298
299 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
300 {
301         struct dsa_slave_priv *p = netdev_priv(dev);
302
303         return p->xmit(skb, dev);
304 }
305
306 static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
307                                         struct net_device *dev)
308 {
309         struct dsa_slave_priv *p = netdev_priv(dev);
310
311         skb->dev = p->parent->dst->master_netdev;
312         dev_queue_xmit(skb);
313
314         return NETDEV_TX_OK;
315 }
316
317
318 /* ethtool operations *******************************************************/
319 static int
320 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
321 {
322         struct dsa_slave_priv *p = netdev_priv(dev);
323         int err;
324
325         err = -EOPNOTSUPP;
326         if (p->phy != NULL) {
327                 err = phy_read_status(p->phy);
328                 if (err == 0)
329                         err = phy_ethtool_gset(p->phy, cmd);
330         }
331
332         return err;
333 }
334
335 static int
336 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
337 {
338         struct dsa_slave_priv *p = netdev_priv(dev);
339
340         if (p->phy != NULL)
341                 return phy_ethtool_sset(p->phy, cmd);
342
343         return -EOPNOTSUPP;
344 }
345
346 static void dsa_slave_get_drvinfo(struct net_device *dev,
347                                   struct ethtool_drvinfo *drvinfo)
348 {
349         strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
350         strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
351         strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
352         strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
353 }
354
355 static int dsa_slave_get_regs_len(struct net_device *dev)
356 {
357         struct dsa_slave_priv *p = netdev_priv(dev);
358         struct dsa_switch *ds = p->parent;
359
360         if (ds->drv->get_regs_len)
361                 return ds->drv->get_regs_len(ds, p->port);
362
363         return -EOPNOTSUPP;
364 }
365
366 static void
367 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
368 {
369         struct dsa_slave_priv *p = netdev_priv(dev);
370         struct dsa_switch *ds = p->parent;
371
372         if (ds->drv->get_regs)
373                 ds->drv->get_regs(ds, p->port, regs, _p);
374 }
375
376 static int dsa_slave_nway_reset(struct net_device *dev)
377 {
378         struct dsa_slave_priv *p = netdev_priv(dev);
379
380         if (p->phy != NULL)
381                 return genphy_restart_aneg(p->phy);
382
383         return -EOPNOTSUPP;
384 }
385
386 static u32 dsa_slave_get_link(struct net_device *dev)
387 {
388         struct dsa_slave_priv *p = netdev_priv(dev);
389
390         if (p->phy != NULL) {
391                 genphy_update_link(p->phy);
392                 return p->phy->link;
393         }
394
395         return -EOPNOTSUPP;
396 }
397
398 static int dsa_slave_get_eeprom_len(struct net_device *dev)
399 {
400         struct dsa_slave_priv *p = netdev_priv(dev);
401         struct dsa_switch *ds = p->parent;
402
403         if (ds->pd->eeprom_len)
404                 return ds->pd->eeprom_len;
405
406         if (ds->drv->get_eeprom_len)
407                 return ds->drv->get_eeprom_len(ds);
408
409         return 0;
410 }
411
412 static int dsa_slave_get_eeprom(struct net_device *dev,
413                                 struct ethtool_eeprom *eeprom, u8 *data)
414 {
415         struct dsa_slave_priv *p = netdev_priv(dev);
416         struct dsa_switch *ds = p->parent;
417
418         if (ds->drv->get_eeprom)
419                 return ds->drv->get_eeprom(ds, eeprom, data);
420
421         return -EOPNOTSUPP;
422 }
423
424 static int dsa_slave_set_eeprom(struct net_device *dev,
425                                 struct ethtool_eeprom *eeprom, u8 *data)
426 {
427         struct dsa_slave_priv *p = netdev_priv(dev);
428         struct dsa_switch *ds = p->parent;
429
430         if (ds->drv->set_eeprom)
431                 return ds->drv->set_eeprom(ds, eeprom, data);
432
433         return -EOPNOTSUPP;
434 }
435
436 static void dsa_slave_get_strings(struct net_device *dev,
437                                   uint32_t stringset, uint8_t *data)
438 {
439         struct dsa_slave_priv *p = netdev_priv(dev);
440         struct dsa_switch *ds = p->parent;
441
442         if (stringset == ETH_SS_STATS) {
443                 int len = ETH_GSTRING_LEN;
444
445                 strncpy(data, "tx_packets", len);
446                 strncpy(data + len, "tx_bytes", len);
447                 strncpy(data + 2 * len, "rx_packets", len);
448                 strncpy(data + 3 * len, "rx_bytes", len);
449                 if (ds->drv->get_strings != NULL)
450                         ds->drv->get_strings(ds, p->port, data + 4 * len);
451         }
452 }
453
454 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
455                                         struct ethtool_stats *stats,
456                                         uint64_t *data)
457 {
458         struct dsa_slave_priv *p = netdev_priv(dev);
459         struct dsa_switch *ds = p->parent;
460
461         data[0] = p->dev->stats.tx_packets;
462         data[1] = p->dev->stats.tx_bytes;
463         data[2] = p->dev->stats.rx_packets;
464         data[3] = p->dev->stats.rx_bytes;
465         if (ds->drv->get_ethtool_stats != NULL)
466                 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
467 }
468
469 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
470 {
471         struct dsa_slave_priv *p = netdev_priv(dev);
472         struct dsa_switch *ds = p->parent;
473
474         if (sset == ETH_SS_STATS) {
475                 int count;
476
477                 count = 4;
478                 if (ds->drv->get_sset_count != NULL)
479                         count += ds->drv->get_sset_count(ds);
480
481                 return count;
482         }
483
484         return -EOPNOTSUPP;
485 }
486
487 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
488 {
489         struct dsa_slave_priv *p = netdev_priv(dev);
490         struct dsa_switch *ds = p->parent;
491
492         if (ds->drv->get_wol)
493                 ds->drv->get_wol(ds, p->port, w);
494 }
495
496 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
497 {
498         struct dsa_slave_priv *p = netdev_priv(dev);
499         struct dsa_switch *ds = p->parent;
500         int ret = -EOPNOTSUPP;
501
502         if (ds->drv->set_wol)
503                 ret = ds->drv->set_wol(ds, p->port, w);
504
505         return ret;
506 }
507
508 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
509 {
510         struct dsa_slave_priv *p = netdev_priv(dev);
511         struct dsa_switch *ds = p->parent;
512         int ret;
513
514         if (!ds->drv->set_eee)
515                 return -EOPNOTSUPP;
516
517         ret = ds->drv->set_eee(ds, p->port, p->phy, e);
518         if (ret)
519                 return ret;
520
521         if (p->phy)
522                 ret = phy_ethtool_set_eee(p->phy, e);
523
524         return ret;
525 }
526
527 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
528 {
529         struct dsa_slave_priv *p = netdev_priv(dev);
530         struct dsa_switch *ds = p->parent;
531         int ret;
532
533         if (!ds->drv->get_eee)
534                 return -EOPNOTSUPP;
535
536         ret = ds->drv->get_eee(ds, p->port, e);
537         if (ret)
538                 return ret;
539
540         if (p->phy)
541                 ret = phy_ethtool_get_eee(p->phy, e);
542
543         return ret;
544 }
545
546 static const struct ethtool_ops dsa_slave_ethtool_ops = {
547         .get_settings           = dsa_slave_get_settings,
548         .set_settings           = dsa_slave_set_settings,
549         .get_drvinfo            = dsa_slave_get_drvinfo,
550         .get_regs_len           = dsa_slave_get_regs_len,
551         .get_regs               = dsa_slave_get_regs,
552         .nway_reset             = dsa_slave_nway_reset,
553         .get_link               = dsa_slave_get_link,
554         .get_eeprom_len         = dsa_slave_get_eeprom_len,
555         .get_eeprom             = dsa_slave_get_eeprom,
556         .set_eeprom             = dsa_slave_set_eeprom,
557         .get_strings            = dsa_slave_get_strings,
558         .get_ethtool_stats      = dsa_slave_get_ethtool_stats,
559         .get_sset_count         = dsa_slave_get_sset_count,
560         .set_wol                = dsa_slave_set_wol,
561         .get_wol                = dsa_slave_get_wol,
562         .set_eee                = dsa_slave_set_eee,
563         .get_eee                = dsa_slave_get_eee,
564 };
565
566 static const struct net_device_ops dsa_slave_netdev_ops = {
567         .ndo_init               = dsa_slave_init,
568         .ndo_open               = dsa_slave_open,
569         .ndo_stop               = dsa_slave_close,
570         .ndo_start_xmit         = dsa_slave_xmit,
571         .ndo_change_rx_flags    = dsa_slave_change_rx_flags,
572         .ndo_set_rx_mode        = dsa_slave_set_rx_mode,
573         .ndo_set_mac_address    = dsa_slave_set_mac_address,
574         .ndo_do_ioctl           = dsa_slave_ioctl,
575         .ndo_switch_parent_id_get = dsa_slave_parent_id_get,
576         .ndo_switch_port_stp_update = dsa_slave_stp_update,
577 };
578
579 static void dsa_slave_adjust_link(struct net_device *dev)
580 {
581         struct dsa_slave_priv *p = netdev_priv(dev);
582         struct dsa_switch *ds = p->parent;
583         unsigned int status_changed = 0;
584
585         if (p->old_link != p->phy->link) {
586                 status_changed = 1;
587                 p->old_link = p->phy->link;
588         }
589
590         if (p->old_duplex != p->phy->duplex) {
591                 status_changed = 1;
592                 p->old_duplex = p->phy->duplex;
593         }
594
595         if (p->old_pause != p->phy->pause) {
596                 status_changed = 1;
597                 p->old_pause = p->phy->pause;
598         }
599
600         if (ds->drv->adjust_link && status_changed)
601                 ds->drv->adjust_link(ds, p->port, p->phy);
602
603         if (status_changed)
604                 phy_print_status(p->phy);
605 }
606
607 static int dsa_slave_fixed_link_update(struct net_device *dev,
608                                        struct fixed_phy_status *status)
609 {
610         struct dsa_slave_priv *p = netdev_priv(dev);
611         struct dsa_switch *ds = p->parent;
612
613         if (ds->drv->fixed_link_update)
614                 ds->drv->fixed_link_update(ds, p->port, status);
615
616         return 0;
617 }
618
619 /* slave device setup *******************************************************/
620 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
621                                  struct net_device *slave_dev,
622                                  int addr)
623 {
624         struct dsa_switch *ds = p->parent;
625
626         p->phy = ds->slave_mii_bus->phy_map[addr];
627         if (!p->phy)
628                 return -ENODEV;
629
630         /* Use already configured phy mode */
631         p->phy_interface = p->phy->interface;
632         phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
633                            p->phy_interface);
634
635         return 0;
636 }
637
638 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
639                                 struct net_device *slave_dev)
640 {
641         struct dsa_switch *ds = p->parent;
642         struct dsa_chip_data *cd = ds->pd;
643         struct device_node *phy_dn, *port_dn;
644         bool phy_is_fixed = false;
645         u32 phy_flags = 0;
646         int mode, ret;
647
648         port_dn = cd->port_dn[p->port];
649         mode = of_get_phy_mode(port_dn);
650         if (mode < 0)
651                 mode = PHY_INTERFACE_MODE_NA;
652         p->phy_interface = mode;
653
654         phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
655         if (of_phy_is_fixed_link(port_dn)) {
656                 /* In the case of a fixed PHY, the DT node associated
657                  * to the fixed PHY is the Port DT node
658                  */
659                 ret = of_phy_register_fixed_link(port_dn);
660                 if (ret) {
661                         netdev_err(slave_dev, "failed to register fixed PHY\n");
662                         return ret;
663                 }
664                 phy_is_fixed = true;
665                 phy_dn = port_dn;
666         }
667
668         if (ds->drv->get_phy_flags)
669                 phy_flags = ds->drv->get_phy_flags(ds, p->port);
670
671         if (phy_dn) {
672                 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
673                 /* If this PHY address is part of phys_mii_mask, which means
674                  * that we need to divert reads and writes to/from it, then we
675                  * want to bind this device using the slave MII bus created by
676                  * DSA to make that happen.
677                  */
678                 if (ret >= 0 && (ds->phys_mii_mask & (1 << ret))) {
679                         ret = dsa_slave_phy_connect(p, slave_dev, ret);
680                         if (ret)
681                                 return ret;
682                 } else {
683                         p->phy = of_phy_connect(slave_dev, phy_dn,
684                                                 dsa_slave_adjust_link,
685                                                 phy_flags,
686                                                 p->phy_interface);
687                 }
688         }
689
690         if (p->phy && phy_is_fixed)
691                 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
692
693         /* We could not connect to a designated PHY, so use the switch internal
694          * MDIO bus instead
695          */
696         if (!p->phy) {
697                 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
698                 if (ret)
699                         return ret;
700         } else {
701                 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
702                             p->phy->addr, p->phy->drv->name);
703         }
704
705         return 0;
706 }
707
708 int dsa_slave_suspend(struct net_device *slave_dev)
709 {
710         struct dsa_slave_priv *p = netdev_priv(slave_dev);
711
712         netif_device_detach(slave_dev);
713
714         if (p->phy) {
715                 phy_stop(p->phy);
716                 p->old_pause = -1;
717                 p->old_link = -1;
718                 p->old_duplex = -1;
719                 phy_suspend(p->phy);
720         }
721
722         return 0;
723 }
724
725 int dsa_slave_resume(struct net_device *slave_dev)
726 {
727         struct dsa_slave_priv *p = netdev_priv(slave_dev);
728
729         netif_device_attach(slave_dev);
730
731         if (p->phy) {
732                 phy_resume(p->phy);
733                 phy_start(p->phy);
734         }
735
736         return 0;
737 }
738
739 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
740                      int port, char *name)
741 {
742         struct net_device *master = ds->dst->master_netdev;
743         struct net_device *slave_dev;
744         struct dsa_slave_priv *p;
745         int ret;
746
747         slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
748                                  NET_NAME_UNKNOWN, ether_setup);
749         if (slave_dev == NULL)
750                 return -ENOMEM;
751
752         slave_dev->features = master->vlan_features;
753         slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
754         eth_hw_addr_inherit(slave_dev, master);
755         slave_dev->tx_queue_len = 0;
756         slave_dev->netdev_ops = &dsa_slave_netdev_ops;
757
758         SET_NETDEV_DEV(slave_dev, parent);
759         slave_dev->dev.of_node = ds->pd->port_dn[port];
760         slave_dev->vlan_features = master->vlan_features;
761
762         p = netdev_priv(slave_dev);
763         p->dev = slave_dev;
764         p->parent = ds;
765         p->port = port;
766
767         switch (ds->dst->tag_protocol) {
768 #ifdef CONFIG_NET_DSA_TAG_DSA
769         case DSA_TAG_PROTO_DSA:
770                 p->xmit = dsa_netdev_ops.xmit;
771                 break;
772 #endif
773 #ifdef CONFIG_NET_DSA_TAG_EDSA
774         case DSA_TAG_PROTO_EDSA:
775                 p->xmit = edsa_netdev_ops.xmit;
776                 break;
777 #endif
778 #ifdef CONFIG_NET_DSA_TAG_TRAILER
779         case DSA_TAG_PROTO_TRAILER:
780                 p->xmit = trailer_netdev_ops.xmit;
781                 break;
782 #endif
783 #ifdef CONFIG_NET_DSA_TAG_BRCM
784         case DSA_TAG_PROTO_BRCM:
785                 p->xmit = brcm_netdev_ops.xmit;
786                 break;
787 #endif
788         default:
789                 p->xmit = dsa_slave_notag_xmit;
790                 break;
791         }
792
793         p->old_pause = -1;
794         p->old_link = -1;
795         p->old_duplex = -1;
796
797         ret = dsa_slave_phy_setup(p, slave_dev);
798         if (ret) {
799                 free_netdev(slave_dev);
800                 return ret;
801         }
802
803         ds->ports[port] = slave_dev;
804         ret = register_netdev(slave_dev);
805         if (ret) {
806                 netdev_err(master, "error %d registering interface %s\n",
807                            ret, slave_dev->name);
808                 phy_disconnect(p->phy);
809                 ds->ports[port] = NULL;
810                 free_netdev(slave_dev);
811                 return ret;
812         }
813
814         netif_carrier_off(slave_dev);
815
816         return 0;
817 }
818
819 static bool dsa_slave_dev_check(struct net_device *dev)
820 {
821         return dev->netdev_ops == &dsa_slave_netdev_ops;
822 }
823
824 static int dsa_slave_master_changed(struct net_device *dev)
825 {
826         struct net_device *master = netdev_master_upper_dev_get(dev);
827         int err = 0;
828
829         if (master && master->rtnl_link_ops &&
830             !strcmp(master->rtnl_link_ops->kind, "bridge"))
831                 err = dsa_slave_bridge_port_join(dev, master);
832         else
833                 err = dsa_slave_bridge_port_leave(dev);
834
835         return err;
836 }
837
838 int dsa_slave_netdevice_event(struct notifier_block *unused,
839                               unsigned long event, void *ptr)
840 {
841         struct net_device *dev;
842         int err = 0;
843
844         switch (event) {
845         case NETDEV_CHANGEUPPER:
846                 dev = netdev_notifier_info_to_dev(ptr);
847                 if (!dsa_slave_dev_check(dev))
848                         goto out;
849
850                 err = dsa_slave_master_changed(dev);
851                 if (err)
852                         netdev_warn(dev, "failed to reflect master change\n");
853
854                 break;
855         }
856
857 out:
858         return NOTIFY_DONE;
859 }