Linux 4.1.18
[linux/fpc-iii.git] / net / dsa / slave.c
blob57978c5b2c91dbabd8df9f4743a80579c52f434c
1 /*
2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/of_net.h>
17 #include <linux/of_mdio.h>
18 #include <net/rtnetlink.h>
19 #include <net/switchdev.h>
20 #include <linux/if_bridge.h>
21 #include "dsa_priv.h"
23 /* slave mii_bus handling ***************************************************/
24 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
26 struct dsa_switch *ds = bus->priv;
28 if (ds->phys_mii_mask & (1 << addr))
29 return ds->drv->phy_read(ds, addr, reg);
31 return 0xffff;
34 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
36 struct dsa_switch *ds = bus->priv;
38 if (ds->phys_mii_mask & (1 << addr))
39 return ds->drv->phy_write(ds, addr, reg, val);
41 return 0;
44 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
46 ds->slave_mii_bus->priv = (void *)ds;
47 ds->slave_mii_bus->name = "dsa slave smi";
48 ds->slave_mii_bus->read = dsa_slave_phy_read;
49 ds->slave_mii_bus->write = dsa_slave_phy_write;
50 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
51 ds->index, ds->pd->sw_addr);
52 ds->slave_mii_bus->parent = ds->master_dev;
53 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
57 /* slave device handling ****************************************************/
58 static int dsa_slave_get_iflink(const struct net_device *dev)
60 struct dsa_slave_priv *p = netdev_priv(dev);
62 return p->parent->dst->master_netdev->ifindex;
65 static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
67 return !!p->bridge_dev;
70 static int dsa_slave_open(struct net_device *dev)
72 struct dsa_slave_priv *p = netdev_priv(dev);
73 struct net_device *master = p->parent->dst->master_netdev;
74 struct dsa_switch *ds = p->parent;
75 u8 stp_state = dsa_port_is_bridged(p) ?
76 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
77 int err;
79 if (!(master->flags & IFF_UP))
80 return -ENETDOWN;
82 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
83 err = dev_uc_add(master, dev->dev_addr);
84 if (err < 0)
85 goto out;
88 if (dev->flags & IFF_ALLMULTI) {
89 err = dev_set_allmulti(master, 1);
90 if (err < 0)
91 goto del_unicast;
93 if (dev->flags & IFF_PROMISC) {
94 err = dev_set_promiscuity(master, 1);
95 if (err < 0)
96 goto clear_allmulti;
99 if (ds->drv->port_enable) {
100 err = ds->drv->port_enable(ds, p->port, p->phy);
101 if (err)
102 goto clear_promisc;
105 if (ds->drv->port_stp_update)
106 ds->drv->port_stp_update(ds, p->port, stp_state);
108 if (p->phy)
109 phy_start(p->phy);
111 return 0;
113 clear_promisc:
114 if (dev->flags & IFF_PROMISC)
115 dev_set_promiscuity(master, 0);
116 clear_allmulti:
117 if (dev->flags & IFF_ALLMULTI)
118 dev_set_allmulti(master, -1);
119 del_unicast:
120 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
121 dev_uc_del(master, dev->dev_addr);
122 out:
123 return err;
126 static int dsa_slave_close(struct net_device *dev)
128 struct dsa_slave_priv *p = netdev_priv(dev);
129 struct net_device *master = p->parent->dst->master_netdev;
130 struct dsa_switch *ds = p->parent;
132 if (p->phy)
133 phy_stop(p->phy);
135 dev_mc_unsync(master, dev);
136 dev_uc_unsync(master, dev);
137 if (dev->flags & IFF_ALLMULTI)
138 dev_set_allmulti(master, -1);
139 if (dev->flags & IFF_PROMISC)
140 dev_set_promiscuity(master, -1);
142 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
143 dev_uc_del(master, dev->dev_addr);
145 if (ds->drv->port_disable)
146 ds->drv->port_disable(ds, p->port, p->phy);
148 if (ds->drv->port_stp_update)
149 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
151 return 0;
154 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
156 struct dsa_slave_priv *p = netdev_priv(dev);
157 struct net_device *master = p->parent->dst->master_netdev;
159 if (change & IFF_ALLMULTI)
160 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
161 if (change & IFF_PROMISC)
162 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
165 static void dsa_slave_set_rx_mode(struct net_device *dev)
167 struct dsa_slave_priv *p = netdev_priv(dev);
168 struct net_device *master = p->parent->dst->master_netdev;
170 dev_mc_sync(master, dev);
171 dev_uc_sync(master, dev);
174 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
176 struct dsa_slave_priv *p = netdev_priv(dev);
177 struct net_device *master = p->parent->dst->master_netdev;
178 struct sockaddr *addr = a;
179 int err;
181 if (!is_valid_ether_addr(addr->sa_data))
182 return -EADDRNOTAVAIL;
184 if (!(dev->flags & IFF_UP))
185 goto out;
187 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
188 err = dev_uc_add(master, addr->sa_data);
189 if (err < 0)
190 return err;
193 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
194 dev_uc_del(master, dev->dev_addr);
196 out:
197 ether_addr_copy(dev->dev_addr, addr->sa_data);
199 return 0;
202 static int dsa_slave_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
203 struct net_device *dev,
204 const unsigned char *addr, u16 vid, u16 nlm_flags)
206 struct dsa_slave_priv *p = netdev_priv(dev);
207 struct dsa_switch *ds = p->parent;
208 int ret = -EOPNOTSUPP;
210 if (ds->drv->fdb_add)
211 ret = ds->drv->fdb_add(ds, p->port, addr, vid);
213 return ret;
216 static int dsa_slave_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
217 struct net_device *dev,
218 const unsigned char *addr, u16 vid)
220 struct dsa_slave_priv *p = netdev_priv(dev);
221 struct dsa_switch *ds = p->parent;
222 int ret = -EOPNOTSUPP;
224 if (ds->drv->fdb_del)
225 ret = ds->drv->fdb_del(ds, p->port, addr, vid);
227 return ret;
230 static int dsa_slave_fill_info(struct net_device *dev, struct sk_buff *skb,
231 const unsigned char *addr, u16 vid,
232 bool is_static,
233 u32 portid, u32 seq, int type,
234 unsigned int flags)
236 struct nlmsghdr *nlh;
237 struct ndmsg *ndm;
239 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
240 if (!nlh)
241 return -EMSGSIZE;
243 ndm = nlmsg_data(nlh);
244 ndm->ndm_family = AF_BRIDGE;
245 ndm->ndm_pad1 = 0;
246 ndm->ndm_pad2 = 0;
247 ndm->ndm_flags = NTF_EXT_LEARNED;
248 ndm->ndm_type = 0;
249 ndm->ndm_ifindex = dev->ifindex;
250 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
252 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
253 goto nla_put_failure;
255 if (vid && nla_put_u16(skb, NDA_VLAN, vid))
256 goto nla_put_failure;
258 nlmsg_end(skb, nlh);
259 return 0;
261 nla_put_failure:
262 nlmsg_cancel(skb, nlh);
263 return -EMSGSIZE;
266 /* Dump information about entries, in response to GETNEIGH */
267 static int dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
268 struct net_device *dev,
269 struct net_device *filter_dev, int idx)
271 struct dsa_slave_priv *p = netdev_priv(dev);
272 struct dsa_switch *ds = p->parent;
273 unsigned char addr[ETH_ALEN] = { 0 };
274 int ret;
276 if (!ds->drv->fdb_getnext)
277 return -EOPNOTSUPP;
279 for (; ; idx++) {
280 bool is_static;
282 ret = ds->drv->fdb_getnext(ds, p->port, addr, &is_static);
283 if (ret < 0)
284 break;
286 if (idx < cb->args[0])
287 continue;
289 ret = dsa_slave_fill_info(dev, skb, addr, 0,
290 is_static,
291 NETLINK_CB(cb->skb).portid,
292 cb->nlh->nlmsg_seq,
293 RTM_NEWNEIGH, NLM_F_MULTI);
294 if (ret < 0)
295 break;
298 return idx;
301 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
303 struct dsa_slave_priv *p = netdev_priv(dev);
305 if (p->phy != NULL)
306 return phy_mii_ioctl(p->phy, ifr, cmd);
308 return -EOPNOTSUPP;
311 /* Return a bitmask of all ports being currently bridged within a given bridge
312 * device. Note that on leave, the mask will still return the bitmask of ports
313 * currently bridged, prior to port removal, and this is exactly what we want.
315 static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
316 struct net_device *bridge)
318 struct dsa_slave_priv *p;
319 unsigned int port;
320 u32 mask = 0;
322 for (port = 0; port < DSA_MAX_PORTS; port++) {
323 if (!dsa_is_port_initialized(ds, port))
324 continue;
326 p = netdev_priv(ds->ports[port]);
328 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
329 p->bridge_dev == bridge)
330 mask |= 1 << port;
333 return mask;
336 static int dsa_slave_stp_update(struct net_device *dev, u8 state)
338 struct dsa_slave_priv *p = netdev_priv(dev);
339 struct dsa_switch *ds = p->parent;
340 int ret = -EOPNOTSUPP;
342 if (ds->drv->port_stp_update)
343 ret = ds->drv->port_stp_update(ds, p->port, state);
345 return ret;
348 static int dsa_slave_bridge_port_join(struct net_device *dev,
349 struct net_device *br)
351 struct dsa_slave_priv *p = netdev_priv(dev);
352 struct dsa_switch *ds = p->parent;
353 int ret = -EOPNOTSUPP;
355 p->bridge_dev = br;
357 if (ds->drv->port_join_bridge)
358 ret = ds->drv->port_join_bridge(ds, p->port,
359 dsa_slave_br_port_mask(ds, br));
361 return ret;
364 static int dsa_slave_bridge_port_leave(struct net_device *dev)
366 struct dsa_slave_priv *p = netdev_priv(dev);
367 struct dsa_switch *ds = p->parent;
368 int ret = -EOPNOTSUPP;
371 if (ds->drv->port_leave_bridge)
372 ret = ds->drv->port_leave_bridge(ds, p->port,
373 dsa_slave_br_port_mask(ds, p->bridge_dev));
375 p->bridge_dev = NULL;
377 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
378 * so allow it to be in BR_STATE_FORWARDING to be kept functional
380 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
382 return ret;
385 static int dsa_slave_parent_id_get(struct net_device *dev,
386 struct netdev_phys_item_id *psid)
388 struct dsa_slave_priv *p = netdev_priv(dev);
389 struct dsa_switch *ds = p->parent;
391 psid->id_len = sizeof(ds->index);
392 memcpy(&psid->id, &ds->index, psid->id_len);
394 return 0;
397 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
399 struct dsa_slave_priv *p = netdev_priv(dev);
401 return p->xmit(skb, dev);
404 static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
405 struct net_device *dev)
407 struct dsa_slave_priv *p = netdev_priv(dev);
409 skb->dev = p->parent->dst->master_netdev;
410 dev_queue_xmit(skb);
412 return NETDEV_TX_OK;
416 /* ethtool operations *******************************************************/
417 static int
418 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
420 struct dsa_slave_priv *p = netdev_priv(dev);
421 int err;
423 err = -EOPNOTSUPP;
424 if (p->phy != NULL) {
425 err = phy_read_status(p->phy);
426 if (err == 0)
427 err = phy_ethtool_gset(p->phy, cmd);
430 return err;
433 static int
434 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
436 struct dsa_slave_priv *p = netdev_priv(dev);
438 if (p->phy != NULL)
439 return phy_ethtool_sset(p->phy, cmd);
441 return -EOPNOTSUPP;
444 static void dsa_slave_get_drvinfo(struct net_device *dev,
445 struct ethtool_drvinfo *drvinfo)
447 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
448 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
449 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
450 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
453 static int dsa_slave_get_regs_len(struct net_device *dev)
455 struct dsa_slave_priv *p = netdev_priv(dev);
456 struct dsa_switch *ds = p->parent;
458 if (ds->drv->get_regs_len)
459 return ds->drv->get_regs_len(ds, p->port);
461 return -EOPNOTSUPP;
464 static void
465 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
467 struct dsa_slave_priv *p = netdev_priv(dev);
468 struct dsa_switch *ds = p->parent;
470 if (ds->drv->get_regs)
471 ds->drv->get_regs(ds, p->port, regs, _p);
474 static int dsa_slave_nway_reset(struct net_device *dev)
476 struct dsa_slave_priv *p = netdev_priv(dev);
478 if (p->phy != NULL)
479 return genphy_restart_aneg(p->phy);
481 return -EOPNOTSUPP;
484 static u32 dsa_slave_get_link(struct net_device *dev)
486 struct dsa_slave_priv *p = netdev_priv(dev);
488 if (p->phy != NULL) {
489 genphy_update_link(p->phy);
490 return p->phy->link;
493 return -EOPNOTSUPP;
496 static int dsa_slave_get_eeprom_len(struct net_device *dev)
498 struct dsa_slave_priv *p = netdev_priv(dev);
499 struct dsa_switch *ds = p->parent;
501 if (ds->pd->eeprom_len)
502 return ds->pd->eeprom_len;
504 if (ds->drv->get_eeprom_len)
505 return ds->drv->get_eeprom_len(ds);
507 return 0;
510 static int dsa_slave_get_eeprom(struct net_device *dev,
511 struct ethtool_eeprom *eeprom, u8 *data)
513 struct dsa_slave_priv *p = netdev_priv(dev);
514 struct dsa_switch *ds = p->parent;
516 if (ds->drv->get_eeprom)
517 return ds->drv->get_eeprom(ds, eeprom, data);
519 return -EOPNOTSUPP;
522 static int dsa_slave_set_eeprom(struct net_device *dev,
523 struct ethtool_eeprom *eeprom, u8 *data)
525 struct dsa_slave_priv *p = netdev_priv(dev);
526 struct dsa_switch *ds = p->parent;
528 if (ds->drv->set_eeprom)
529 return ds->drv->set_eeprom(ds, eeprom, data);
531 return -EOPNOTSUPP;
534 static void dsa_slave_get_strings(struct net_device *dev,
535 uint32_t stringset, uint8_t *data)
537 struct dsa_slave_priv *p = netdev_priv(dev);
538 struct dsa_switch *ds = p->parent;
540 if (stringset == ETH_SS_STATS) {
541 int len = ETH_GSTRING_LEN;
543 strncpy(data, "tx_packets", len);
544 strncpy(data + len, "tx_bytes", len);
545 strncpy(data + 2 * len, "rx_packets", len);
546 strncpy(data + 3 * len, "rx_bytes", len);
547 if (ds->drv->get_strings != NULL)
548 ds->drv->get_strings(ds, p->port, data + 4 * len);
552 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
553 struct ethtool_stats *stats,
554 uint64_t *data)
556 struct dsa_slave_priv *p = netdev_priv(dev);
557 struct dsa_switch *ds = p->parent;
559 data[0] = p->dev->stats.tx_packets;
560 data[1] = p->dev->stats.tx_bytes;
561 data[2] = p->dev->stats.rx_packets;
562 data[3] = p->dev->stats.rx_bytes;
563 if (ds->drv->get_ethtool_stats != NULL)
564 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
567 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
569 struct dsa_slave_priv *p = netdev_priv(dev);
570 struct dsa_switch *ds = p->parent;
572 if (sset == ETH_SS_STATS) {
573 int count;
575 count = 4;
576 if (ds->drv->get_sset_count != NULL)
577 count += ds->drv->get_sset_count(ds);
579 return count;
582 return -EOPNOTSUPP;
585 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
587 struct dsa_slave_priv *p = netdev_priv(dev);
588 struct dsa_switch *ds = p->parent;
590 if (ds->drv->get_wol)
591 ds->drv->get_wol(ds, p->port, w);
594 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
596 struct dsa_slave_priv *p = netdev_priv(dev);
597 struct dsa_switch *ds = p->parent;
598 int ret = -EOPNOTSUPP;
600 if (ds->drv->set_wol)
601 ret = ds->drv->set_wol(ds, p->port, w);
603 return ret;
606 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
608 struct dsa_slave_priv *p = netdev_priv(dev);
609 struct dsa_switch *ds = p->parent;
610 int ret;
612 if (!ds->drv->set_eee)
613 return -EOPNOTSUPP;
615 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
616 if (ret)
617 return ret;
619 if (p->phy)
620 ret = phy_ethtool_set_eee(p->phy, e);
622 return ret;
625 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
627 struct dsa_slave_priv *p = netdev_priv(dev);
628 struct dsa_switch *ds = p->parent;
629 int ret;
631 if (!ds->drv->get_eee)
632 return -EOPNOTSUPP;
634 ret = ds->drv->get_eee(ds, p->port, e);
635 if (ret)
636 return ret;
638 if (p->phy)
639 ret = phy_ethtool_get_eee(p->phy, e);
641 return ret;
644 static const struct ethtool_ops dsa_slave_ethtool_ops = {
645 .get_settings = dsa_slave_get_settings,
646 .set_settings = dsa_slave_set_settings,
647 .get_drvinfo = dsa_slave_get_drvinfo,
648 .get_regs_len = dsa_slave_get_regs_len,
649 .get_regs = dsa_slave_get_regs,
650 .nway_reset = dsa_slave_nway_reset,
651 .get_link = dsa_slave_get_link,
652 .get_eeprom_len = dsa_slave_get_eeprom_len,
653 .get_eeprom = dsa_slave_get_eeprom,
654 .set_eeprom = dsa_slave_set_eeprom,
655 .get_strings = dsa_slave_get_strings,
656 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
657 .get_sset_count = dsa_slave_get_sset_count,
658 .set_wol = dsa_slave_set_wol,
659 .get_wol = dsa_slave_get_wol,
660 .set_eee = dsa_slave_set_eee,
661 .get_eee = dsa_slave_get_eee,
664 static const struct net_device_ops dsa_slave_netdev_ops = {
665 .ndo_open = dsa_slave_open,
666 .ndo_stop = dsa_slave_close,
667 .ndo_start_xmit = dsa_slave_xmit,
668 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
669 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
670 .ndo_set_mac_address = dsa_slave_set_mac_address,
671 .ndo_fdb_add = dsa_slave_fdb_add,
672 .ndo_fdb_del = dsa_slave_fdb_del,
673 .ndo_fdb_dump = dsa_slave_fdb_dump,
674 .ndo_do_ioctl = dsa_slave_ioctl,
675 .ndo_get_iflink = dsa_slave_get_iflink,
678 static const struct swdev_ops dsa_slave_swdev_ops = {
679 .swdev_parent_id_get = dsa_slave_parent_id_get,
680 .swdev_port_stp_update = dsa_slave_stp_update,
683 static void dsa_slave_adjust_link(struct net_device *dev)
685 struct dsa_slave_priv *p = netdev_priv(dev);
686 struct dsa_switch *ds = p->parent;
687 unsigned int status_changed = 0;
689 if (p->old_link != p->phy->link) {
690 status_changed = 1;
691 p->old_link = p->phy->link;
694 if (p->old_duplex != p->phy->duplex) {
695 status_changed = 1;
696 p->old_duplex = p->phy->duplex;
699 if (p->old_pause != p->phy->pause) {
700 status_changed = 1;
701 p->old_pause = p->phy->pause;
704 if (ds->drv->adjust_link && status_changed)
705 ds->drv->adjust_link(ds, p->port, p->phy);
707 if (status_changed)
708 phy_print_status(p->phy);
711 static int dsa_slave_fixed_link_update(struct net_device *dev,
712 struct fixed_phy_status *status)
714 struct dsa_slave_priv *p = netdev_priv(dev);
715 struct dsa_switch *ds = p->parent;
717 if (ds->drv->fixed_link_update)
718 ds->drv->fixed_link_update(ds, p->port, status);
720 return 0;
723 /* slave device setup *******************************************************/
724 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
725 struct net_device *slave_dev,
726 int addr)
728 struct dsa_switch *ds = p->parent;
730 p->phy = ds->slave_mii_bus->phy_map[addr];
731 if (!p->phy)
732 return -ENODEV;
734 /* Use already configured phy mode */
735 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
736 p->phy_interface = p->phy->interface;
737 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
738 p->phy_interface);
740 return 0;
743 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
744 struct net_device *slave_dev)
746 struct dsa_switch *ds = p->parent;
747 struct dsa_chip_data *cd = ds->pd;
748 struct device_node *phy_dn, *port_dn;
749 bool phy_is_fixed = false;
750 u32 phy_flags = 0;
751 int mode, ret;
753 port_dn = cd->port_dn[p->port];
754 mode = of_get_phy_mode(port_dn);
755 if (mode < 0)
756 mode = PHY_INTERFACE_MODE_NA;
757 p->phy_interface = mode;
759 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
760 if (of_phy_is_fixed_link(port_dn)) {
761 /* In the case of a fixed PHY, the DT node associated
762 * to the fixed PHY is the Port DT node
764 ret = of_phy_register_fixed_link(port_dn);
765 if (ret) {
766 netdev_err(slave_dev, "failed to register fixed PHY\n");
767 return ret;
769 phy_is_fixed = true;
770 phy_dn = port_dn;
773 if (ds->drv->get_phy_flags)
774 phy_flags = ds->drv->get_phy_flags(ds, p->port);
776 if (phy_dn) {
777 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
778 /* If this PHY address is part of phys_mii_mask, which means
779 * that we need to divert reads and writes to/from it, then we
780 * want to bind this device using the slave MII bus created by
781 * DSA to make that happen.
783 if (!phy_is_fixed && ret >= 0 &&
784 (ds->phys_mii_mask & (1 << ret))) {
785 ret = dsa_slave_phy_connect(p, slave_dev, ret);
786 if (ret)
787 return ret;
788 } else {
789 p->phy = of_phy_connect(slave_dev, phy_dn,
790 dsa_slave_adjust_link,
791 phy_flags,
792 p->phy_interface);
796 if (p->phy && phy_is_fixed)
797 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
799 /* We could not connect to a designated PHY, so use the switch internal
800 * MDIO bus instead
802 if (!p->phy) {
803 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
804 if (ret)
805 return ret;
806 } else {
807 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
808 p->phy->addr, p->phy->drv->name);
811 return 0;
814 int dsa_slave_suspend(struct net_device *slave_dev)
816 struct dsa_slave_priv *p = netdev_priv(slave_dev);
818 netif_device_detach(slave_dev);
820 if (p->phy) {
821 phy_stop(p->phy);
822 p->old_pause = -1;
823 p->old_link = -1;
824 p->old_duplex = -1;
825 phy_suspend(p->phy);
828 return 0;
831 int dsa_slave_resume(struct net_device *slave_dev)
833 struct dsa_slave_priv *p = netdev_priv(slave_dev);
835 netif_device_attach(slave_dev);
837 if (p->phy) {
838 phy_resume(p->phy);
839 phy_start(p->phy);
842 return 0;
845 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
846 int port, char *name)
848 struct net_device *master = ds->dst->master_netdev;
849 struct net_device *slave_dev;
850 struct dsa_slave_priv *p;
851 int ret;
853 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
854 NET_NAME_UNKNOWN, ether_setup);
855 if (slave_dev == NULL)
856 return -ENOMEM;
858 slave_dev->features = master->vlan_features;
859 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
860 eth_hw_addr_inherit(slave_dev, master);
861 slave_dev->tx_queue_len = 0;
862 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
863 slave_dev->swdev_ops = &dsa_slave_swdev_ops;
865 SET_NETDEV_DEV(slave_dev, parent);
866 slave_dev->dev.of_node = ds->pd->port_dn[port];
867 slave_dev->vlan_features = master->vlan_features;
869 p = netdev_priv(slave_dev);
870 p->dev = slave_dev;
871 p->parent = ds;
872 p->port = port;
874 switch (ds->dst->tag_protocol) {
875 #ifdef CONFIG_NET_DSA_TAG_DSA
876 case DSA_TAG_PROTO_DSA:
877 p->xmit = dsa_netdev_ops.xmit;
878 break;
879 #endif
880 #ifdef CONFIG_NET_DSA_TAG_EDSA
881 case DSA_TAG_PROTO_EDSA:
882 p->xmit = edsa_netdev_ops.xmit;
883 break;
884 #endif
885 #ifdef CONFIG_NET_DSA_TAG_TRAILER
886 case DSA_TAG_PROTO_TRAILER:
887 p->xmit = trailer_netdev_ops.xmit;
888 break;
889 #endif
890 #ifdef CONFIG_NET_DSA_TAG_BRCM
891 case DSA_TAG_PROTO_BRCM:
892 p->xmit = brcm_netdev_ops.xmit;
893 break;
894 #endif
895 default:
896 p->xmit = dsa_slave_notag_xmit;
897 break;
900 p->old_pause = -1;
901 p->old_link = -1;
902 p->old_duplex = -1;
904 ret = dsa_slave_phy_setup(p, slave_dev);
905 if (ret) {
906 free_netdev(slave_dev);
907 return ret;
910 ds->ports[port] = slave_dev;
911 ret = register_netdev(slave_dev);
912 if (ret) {
913 netdev_err(master, "error %d registering interface %s\n",
914 ret, slave_dev->name);
915 phy_disconnect(p->phy);
916 ds->ports[port] = NULL;
917 free_netdev(slave_dev);
918 return ret;
921 netif_carrier_off(slave_dev);
923 return 0;
926 static bool dsa_slave_dev_check(struct net_device *dev)
928 return dev->netdev_ops == &dsa_slave_netdev_ops;
931 static int dsa_slave_master_changed(struct net_device *dev)
933 struct net_device *master = netdev_master_upper_dev_get(dev);
934 struct dsa_slave_priv *p = netdev_priv(dev);
935 int err = 0;
937 if (master && master->rtnl_link_ops &&
938 !strcmp(master->rtnl_link_ops->kind, "bridge"))
939 err = dsa_slave_bridge_port_join(dev, master);
940 else if (dsa_port_is_bridged(p))
941 err = dsa_slave_bridge_port_leave(dev);
943 return err;
946 int dsa_slave_netdevice_event(struct notifier_block *unused,
947 unsigned long event, void *ptr)
949 struct net_device *dev;
950 int err = 0;
952 switch (event) {
953 case NETDEV_CHANGEUPPER:
954 dev = netdev_notifier_info_to_dev(ptr);
955 if (!dsa_slave_dev_check(dev))
956 goto out;
958 err = dsa_slave_master_changed(dev);
959 if (err)
960 netdev_warn(dev, "failed to reflect master change\n");
962 break;
965 out:
966 return NOTIFY_DONE;