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.
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>
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
);
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
);
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
;
79 if (!(master
->flags
& IFF_UP
))
82 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
)) {
83 err
= dev_uc_add(master
, dev
->dev_addr
);
88 if (dev
->flags
& IFF_ALLMULTI
) {
89 err
= dev_set_allmulti(master
, 1);
93 if (dev
->flags
& IFF_PROMISC
) {
94 err
= dev_set_promiscuity(master
, 1);
99 if (ds
->drv
->port_enable
) {
100 err
= ds
->drv
->port_enable(ds
, p
->port
, p
->phy
);
105 if (ds
->drv
->port_stp_update
)
106 ds
->drv
->port_stp_update(ds
, p
->port
, stp_state
);
114 if (dev
->flags
& IFF_PROMISC
)
115 dev_set_promiscuity(master
, -1);
117 if (dev
->flags
& IFF_ALLMULTI
)
118 dev_set_allmulti(master
, -1);
120 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
121 dev_uc_del(master
, dev
->dev_addr
);
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
;
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
);
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
;
181 if (!is_valid_ether_addr(addr
->sa_data
))
182 return -EADDRNOTAVAIL
;
184 if (!(dev
->flags
& IFF_UP
))
187 if (!ether_addr_equal(addr
->sa_data
, master
->dev_addr
)) {
188 err
= dev_uc_add(master
, addr
->sa_data
);
193 if (!ether_addr_equal(dev
->dev_addr
, master
->dev_addr
))
194 dev_uc_del(master
, dev
->dev_addr
);
197 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
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
);
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
);
230 static int dsa_slave_fill_info(struct net_device
*dev
, struct sk_buff
*skb
,
231 const unsigned char *addr
, u16 vid
,
233 u32 portid
, u32 seq
, int type
,
236 struct nlmsghdr
*nlh
;
239 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
243 ndm
= nlmsg_data(nlh
);
244 ndm
->ndm_family
= AF_BRIDGE
;
247 ndm
->ndm_flags
= NTF_EXT_LEARNED
;
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
;
262 nlmsg_cancel(skb
, nlh
);
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 };
276 if (!ds
->drv
->fdb_getnext
)
282 ret
= ds
->drv
->fdb_getnext(ds
, p
->port
, addr
, &is_static
);
286 if (idx
< cb
->args
[0])
289 ret
= dsa_slave_fill_info(dev
, skb
, addr
, 0,
291 NETLINK_CB(cb
->skb
).portid
,
293 RTM_NEWNEIGH
, NLM_F_MULTI
);
301 static int dsa_slave_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
303 struct dsa_slave_priv
*p
= netdev_priv(dev
);
306 return phy_mii_ioctl(p
->phy
, ifr
, cmd
);
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
;
322 for (port
= 0; port
< DSA_MAX_PORTS
; port
++) {
323 if (!dsa_is_port_initialized(ds
, port
))
326 p
= netdev_priv(ds
->ports
[port
]);
328 if (ds
->ports
[port
]->priv_flags
& IFF_BRIDGE_PORT
&&
329 p
->bridge_dev
== bridge
)
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
);
348 static int dsa_slave_port_attr_set(struct net_device
*dev
,
349 struct switchdev_attr
*attr
)
354 case SWITCHDEV_ATTR_PORT_STP_STATE
:
355 if (attr
->trans
== SWITCHDEV_TRANS_COMMIT
)
356 ret
= dsa_slave_stp_update(dev
, attr
->u
.stp_state
);
366 static int dsa_slave_bridge_port_join(struct net_device
*dev
,
367 struct net_device
*br
)
369 struct dsa_slave_priv
*p
= netdev_priv(dev
);
370 struct dsa_switch
*ds
= p
->parent
;
371 int ret
= -EOPNOTSUPP
;
375 if (ds
->drv
->port_join_bridge
)
376 ret
= ds
->drv
->port_join_bridge(ds
, p
->port
,
377 dsa_slave_br_port_mask(ds
, br
));
382 static int dsa_slave_bridge_port_leave(struct net_device
*dev
)
384 struct dsa_slave_priv
*p
= netdev_priv(dev
);
385 struct dsa_switch
*ds
= p
->parent
;
386 int ret
= -EOPNOTSUPP
;
389 if (ds
->drv
->port_leave_bridge
)
390 ret
= ds
->drv
->port_leave_bridge(ds
, p
->port
,
391 dsa_slave_br_port_mask(ds
, p
->bridge_dev
));
393 p
->bridge_dev
= NULL
;
395 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
396 * so allow it to be in BR_STATE_FORWARDING to be kept functional
398 dsa_slave_stp_update(dev
, BR_STATE_FORWARDING
);
403 static int dsa_slave_port_attr_get(struct net_device
*dev
,
404 struct switchdev_attr
*attr
)
406 struct dsa_slave_priv
*p
= netdev_priv(dev
);
407 struct dsa_switch
*ds
= p
->parent
;
410 case SWITCHDEV_ATTR_PORT_PARENT_ID
:
411 attr
->u
.ppid
.id_len
= sizeof(ds
->index
);
412 memcpy(&attr
->u
.ppid
.id
, &ds
->index
, attr
->u
.ppid
.id_len
);
421 static netdev_tx_t
dsa_slave_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
423 struct dsa_slave_priv
*p
= netdev_priv(dev
);
425 return p
->xmit(skb
, dev
);
428 static netdev_tx_t
dsa_slave_notag_xmit(struct sk_buff
*skb
,
429 struct net_device
*dev
)
431 struct dsa_slave_priv
*p
= netdev_priv(dev
);
433 skb
->dev
= p
->parent
->dst
->master_netdev
;
440 /* ethtool operations *******************************************************/
442 dsa_slave_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
444 struct dsa_slave_priv
*p
= netdev_priv(dev
);
448 if (p
->phy
!= NULL
) {
449 err
= phy_read_status(p
->phy
);
451 err
= phy_ethtool_gset(p
->phy
, cmd
);
458 dsa_slave_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
460 struct dsa_slave_priv
*p
= netdev_priv(dev
);
463 return phy_ethtool_sset(p
->phy
, cmd
);
468 static void dsa_slave_get_drvinfo(struct net_device
*dev
,
469 struct ethtool_drvinfo
*drvinfo
)
471 strlcpy(drvinfo
->driver
, "dsa", sizeof(drvinfo
->driver
));
472 strlcpy(drvinfo
->version
, dsa_driver_version
, sizeof(drvinfo
->version
));
473 strlcpy(drvinfo
->fw_version
, "N/A", sizeof(drvinfo
->fw_version
));
474 strlcpy(drvinfo
->bus_info
, "platform", sizeof(drvinfo
->bus_info
));
477 static int dsa_slave_get_regs_len(struct net_device
*dev
)
479 struct dsa_slave_priv
*p
= netdev_priv(dev
);
480 struct dsa_switch
*ds
= p
->parent
;
482 if (ds
->drv
->get_regs_len
)
483 return ds
->drv
->get_regs_len(ds
, p
->port
);
489 dsa_slave_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *_p
)
491 struct dsa_slave_priv
*p
= netdev_priv(dev
);
492 struct dsa_switch
*ds
= p
->parent
;
494 if (ds
->drv
->get_regs
)
495 ds
->drv
->get_regs(ds
, p
->port
, regs
, _p
);
498 static int dsa_slave_nway_reset(struct net_device
*dev
)
500 struct dsa_slave_priv
*p
= netdev_priv(dev
);
503 return genphy_restart_aneg(p
->phy
);
508 static u32
dsa_slave_get_link(struct net_device
*dev
)
510 struct dsa_slave_priv
*p
= netdev_priv(dev
);
512 if (p
->phy
!= NULL
) {
513 genphy_update_link(p
->phy
);
520 static int dsa_slave_get_eeprom_len(struct net_device
*dev
)
522 struct dsa_slave_priv
*p
= netdev_priv(dev
);
523 struct dsa_switch
*ds
= p
->parent
;
525 if (ds
->pd
->eeprom_len
)
526 return ds
->pd
->eeprom_len
;
528 if (ds
->drv
->get_eeprom_len
)
529 return ds
->drv
->get_eeprom_len(ds
);
534 static int dsa_slave_get_eeprom(struct net_device
*dev
,
535 struct ethtool_eeprom
*eeprom
, u8
*data
)
537 struct dsa_slave_priv
*p
= netdev_priv(dev
);
538 struct dsa_switch
*ds
= p
->parent
;
540 if (ds
->drv
->get_eeprom
)
541 return ds
->drv
->get_eeprom(ds
, eeprom
, data
);
546 static int dsa_slave_set_eeprom(struct net_device
*dev
,
547 struct ethtool_eeprom
*eeprom
, u8
*data
)
549 struct dsa_slave_priv
*p
= netdev_priv(dev
);
550 struct dsa_switch
*ds
= p
->parent
;
552 if (ds
->drv
->set_eeprom
)
553 return ds
->drv
->set_eeprom(ds
, eeprom
, data
);
558 static void dsa_slave_get_strings(struct net_device
*dev
,
559 uint32_t stringset
, uint8_t *data
)
561 struct dsa_slave_priv
*p
= netdev_priv(dev
);
562 struct dsa_switch
*ds
= p
->parent
;
564 if (stringset
== ETH_SS_STATS
) {
565 int len
= ETH_GSTRING_LEN
;
567 strncpy(data
, "tx_packets", len
);
568 strncpy(data
+ len
, "tx_bytes", len
);
569 strncpy(data
+ 2 * len
, "rx_packets", len
);
570 strncpy(data
+ 3 * len
, "rx_bytes", len
);
571 if (ds
->drv
->get_strings
!= NULL
)
572 ds
->drv
->get_strings(ds
, p
->port
, data
+ 4 * len
);
576 static void dsa_slave_get_ethtool_stats(struct net_device
*dev
,
577 struct ethtool_stats
*stats
,
580 struct dsa_slave_priv
*p
= netdev_priv(dev
);
581 struct dsa_switch
*ds
= p
->parent
;
583 data
[0] = p
->dev
->stats
.tx_packets
;
584 data
[1] = p
->dev
->stats
.tx_bytes
;
585 data
[2] = p
->dev
->stats
.rx_packets
;
586 data
[3] = p
->dev
->stats
.rx_bytes
;
587 if (ds
->drv
->get_ethtool_stats
!= NULL
)
588 ds
->drv
->get_ethtool_stats(ds
, p
->port
, data
+ 4);
591 static int dsa_slave_get_sset_count(struct net_device
*dev
, int sset
)
593 struct dsa_slave_priv
*p
= netdev_priv(dev
);
594 struct dsa_switch
*ds
= p
->parent
;
596 if (sset
== ETH_SS_STATS
) {
600 if (ds
->drv
->get_sset_count
!= NULL
)
601 count
+= ds
->drv
->get_sset_count(ds
);
609 static void dsa_slave_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
611 struct dsa_slave_priv
*p
= netdev_priv(dev
);
612 struct dsa_switch
*ds
= p
->parent
;
614 if (ds
->drv
->get_wol
)
615 ds
->drv
->get_wol(ds
, p
->port
, w
);
618 static int dsa_slave_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*w
)
620 struct dsa_slave_priv
*p
= netdev_priv(dev
);
621 struct dsa_switch
*ds
= p
->parent
;
622 int ret
= -EOPNOTSUPP
;
624 if (ds
->drv
->set_wol
)
625 ret
= ds
->drv
->set_wol(ds
, p
->port
, w
);
630 static int dsa_slave_set_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
632 struct dsa_slave_priv
*p
= netdev_priv(dev
);
633 struct dsa_switch
*ds
= p
->parent
;
636 if (!ds
->drv
->set_eee
)
639 ret
= ds
->drv
->set_eee(ds
, p
->port
, p
->phy
, e
);
644 ret
= phy_ethtool_set_eee(p
->phy
, e
);
649 static int dsa_slave_get_eee(struct net_device
*dev
, struct ethtool_eee
*e
)
651 struct dsa_slave_priv
*p
= netdev_priv(dev
);
652 struct dsa_switch
*ds
= p
->parent
;
655 if (!ds
->drv
->get_eee
)
658 ret
= ds
->drv
->get_eee(ds
, p
->port
, e
);
663 ret
= phy_ethtool_get_eee(p
->phy
, e
);
668 static const struct ethtool_ops dsa_slave_ethtool_ops
= {
669 .get_settings
= dsa_slave_get_settings
,
670 .set_settings
= dsa_slave_set_settings
,
671 .get_drvinfo
= dsa_slave_get_drvinfo
,
672 .get_regs_len
= dsa_slave_get_regs_len
,
673 .get_regs
= dsa_slave_get_regs
,
674 .nway_reset
= dsa_slave_nway_reset
,
675 .get_link
= dsa_slave_get_link
,
676 .get_eeprom_len
= dsa_slave_get_eeprom_len
,
677 .get_eeprom
= dsa_slave_get_eeprom
,
678 .set_eeprom
= dsa_slave_set_eeprom
,
679 .get_strings
= dsa_slave_get_strings
,
680 .get_ethtool_stats
= dsa_slave_get_ethtool_stats
,
681 .get_sset_count
= dsa_slave_get_sset_count
,
682 .set_wol
= dsa_slave_set_wol
,
683 .get_wol
= dsa_slave_get_wol
,
684 .set_eee
= dsa_slave_set_eee
,
685 .get_eee
= dsa_slave_get_eee
,
688 static const struct net_device_ops dsa_slave_netdev_ops
= {
689 .ndo_open
= dsa_slave_open
,
690 .ndo_stop
= dsa_slave_close
,
691 .ndo_start_xmit
= dsa_slave_xmit
,
692 .ndo_change_rx_flags
= dsa_slave_change_rx_flags
,
693 .ndo_set_rx_mode
= dsa_slave_set_rx_mode
,
694 .ndo_set_mac_address
= dsa_slave_set_mac_address
,
695 .ndo_fdb_add
= dsa_slave_fdb_add
,
696 .ndo_fdb_del
= dsa_slave_fdb_del
,
697 .ndo_fdb_dump
= dsa_slave_fdb_dump
,
698 .ndo_do_ioctl
= dsa_slave_ioctl
,
699 .ndo_get_iflink
= dsa_slave_get_iflink
,
702 static const struct switchdev_ops dsa_slave_switchdev_ops
= {
703 .switchdev_port_attr_get
= dsa_slave_port_attr_get
,
704 .switchdev_port_attr_set
= dsa_slave_port_attr_set
,
707 static void dsa_slave_adjust_link(struct net_device
*dev
)
709 struct dsa_slave_priv
*p
= netdev_priv(dev
);
710 struct dsa_switch
*ds
= p
->parent
;
711 unsigned int status_changed
= 0;
713 if (p
->old_link
!= p
->phy
->link
) {
715 p
->old_link
= p
->phy
->link
;
718 if (p
->old_duplex
!= p
->phy
->duplex
) {
720 p
->old_duplex
= p
->phy
->duplex
;
723 if (p
->old_pause
!= p
->phy
->pause
) {
725 p
->old_pause
= p
->phy
->pause
;
728 if (ds
->drv
->adjust_link
&& status_changed
)
729 ds
->drv
->adjust_link(ds
, p
->port
, p
->phy
);
732 phy_print_status(p
->phy
);
735 static int dsa_slave_fixed_link_update(struct net_device
*dev
,
736 struct fixed_phy_status
*status
)
738 struct dsa_slave_priv
*p
= netdev_priv(dev
);
739 struct dsa_switch
*ds
= p
->parent
;
741 if (ds
->drv
->fixed_link_update
)
742 ds
->drv
->fixed_link_update(ds
, p
->port
, status
);
747 /* slave device setup *******************************************************/
748 static int dsa_slave_phy_connect(struct dsa_slave_priv
*p
,
749 struct net_device
*slave_dev
,
752 struct dsa_switch
*ds
= p
->parent
;
754 p
->phy
= ds
->slave_mii_bus
->phy_map
[addr
];
758 /* Use already configured phy mode */
759 p
->phy_interface
= p
->phy
->interface
;
760 phy_connect_direct(slave_dev
, p
->phy
, dsa_slave_adjust_link
,
766 static int dsa_slave_phy_setup(struct dsa_slave_priv
*p
,
767 struct net_device
*slave_dev
)
769 struct dsa_switch
*ds
= p
->parent
;
770 struct dsa_chip_data
*cd
= ds
->pd
;
771 struct device_node
*phy_dn
, *port_dn
;
772 bool phy_is_fixed
= false;
776 port_dn
= cd
->port_dn
[p
->port
];
777 mode
= of_get_phy_mode(port_dn
);
779 mode
= PHY_INTERFACE_MODE_NA
;
780 p
->phy_interface
= mode
;
782 phy_dn
= of_parse_phandle(port_dn
, "phy-handle", 0);
783 if (of_phy_is_fixed_link(port_dn
)) {
784 /* In the case of a fixed PHY, the DT node associated
785 * to the fixed PHY is the Port DT node
787 ret
= of_phy_register_fixed_link(port_dn
);
789 netdev_err(slave_dev
, "failed to register fixed PHY\n");
796 if (ds
->drv
->get_phy_flags
)
797 phy_flags
= ds
->drv
->get_phy_flags(ds
, p
->port
);
800 ret
= of_mdio_parse_addr(&slave_dev
->dev
, phy_dn
);
801 /* If this PHY address is part of phys_mii_mask, which means
802 * that we need to divert reads and writes to/from it, then we
803 * want to bind this device using the slave MII bus created by
804 * DSA to make that happen.
806 if (!phy_is_fixed
&& ret
>= 0 &&
807 (ds
->phys_mii_mask
& (1 << ret
))) {
808 ret
= dsa_slave_phy_connect(p
, slave_dev
, ret
);
812 p
->phy
= of_phy_connect(slave_dev
, phy_dn
,
813 dsa_slave_adjust_link
,
819 if (p
->phy
&& phy_is_fixed
)
820 fixed_phy_set_link_update(p
->phy
, dsa_slave_fixed_link_update
);
822 /* We could not connect to a designated PHY, so use the switch internal
826 ret
= dsa_slave_phy_connect(p
, slave_dev
, p
->port
);
830 netdev_info(slave_dev
, "attached PHY at address %d [%s]\n",
831 p
->phy
->addr
, p
->phy
->drv
->name
);
837 static struct lock_class_key dsa_slave_netdev_xmit_lock_key
;
838 static void dsa_slave_set_lockdep_class_one(struct net_device
*dev
,
839 struct netdev_queue
*txq
,
842 lockdep_set_class(&txq
->_xmit_lock
,
843 &dsa_slave_netdev_xmit_lock_key
);
846 int dsa_slave_suspend(struct net_device
*slave_dev
)
848 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
861 int dsa_slave_resume(struct net_device
*slave_dev
)
863 struct dsa_slave_priv
*p
= netdev_priv(slave_dev
);
865 netif_device_attach(slave_dev
);
875 int dsa_slave_create(struct dsa_switch
*ds
, struct device
*parent
,
876 int port
, char *name
)
878 struct net_device
*master
= ds
->dst
->master_netdev
;
879 struct net_device
*slave_dev
;
880 struct dsa_slave_priv
*p
;
883 slave_dev
= alloc_netdev(sizeof(struct dsa_slave_priv
), name
,
884 NET_NAME_UNKNOWN
, ether_setup
);
885 if (slave_dev
== NULL
)
888 slave_dev
->features
= master
->vlan_features
;
889 slave_dev
->ethtool_ops
= &dsa_slave_ethtool_ops
;
890 eth_hw_addr_inherit(slave_dev
, master
);
891 slave_dev
->tx_queue_len
= 0;
892 slave_dev
->netdev_ops
= &dsa_slave_netdev_ops
;
893 slave_dev
->switchdev_ops
= &dsa_slave_switchdev_ops
;
895 netdev_for_each_tx_queue(slave_dev
, dsa_slave_set_lockdep_class_one
,
898 SET_NETDEV_DEV(slave_dev
, parent
);
899 slave_dev
->dev
.of_node
= ds
->pd
->port_dn
[port
];
900 slave_dev
->vlan_features
= master
->vlan_features
;
902 p
= netdev_priv(slave_dev
);
907 switch (ds
->dst
->tag_protocol
) {
908 #ifdef CONFIG_NET_DSA_TAG_DSA
909 case DSA_TAG_PROTO_DSA
:
910 p
->xmit
= dsa_netdev_ops
.xmit
;
913 #ifdef CONFIG_NET_DSA_TAG_EDSA
914 case DSA_TAG_PROTO_EDSA
:
915 p
->xmit
= edsa_netdev_ops
.xmit
;
918 #ifdef CONFIG_NET_DSA_TAG_TRAILER
919 case DSA_TAG_PROTO_TRAILER
:
920 p
->xmit
= trailer_netdev_ops
.xmit
;
923 #ifdef CONFIG_NET_DSA_TAG_BRCM
924 case DSA_TAG_PROTO_BRCM
:
925 p
->xmit
= brcm_netdev_ops
.xmit
;
929 p
->xmit
= dsa_slave_notag_xmit
;
937 ret
= dsa_slave_phy_setup(p
, slave_dev
);
939 free_netdev(slave_dev
);
943 ds
->ports
[port
] = slave_dev
;
944 ret
= register_netdev(slave_dev
);
946 netdev_err(master
, "error %d registering interface %s\n",
947 ret
, slave_dev
->name
);
948 phy_disconnect(p
->phy
);
949 ds
->ports
[port
] = NULL
;
950 free_netdev(slave_dev
);
954 netif_carrier_off(slave_dev
);
959 static bool dsa_slave_dev_check(struct net_device
*dev
)
961 return dev
->netdev_ops
== &dsa_slave_netdev_ops
;
964 static int dsa_slave_master_changed(struct net_device
*dev
)
966 struct net_device
*master
= netdev_master_upper_dev_get(dev
);
967 struct dsa_slave_priv
*p
= netdev_priv(dev
);
970 if (master
&& master
->rtnl_link_ops
&&
971 !strcmp(master
->rtnl_link_ops
->kind
, "bridge"))
972 err
= dsa_slave_bridge_port_join(dev
, master
);
973 else if (dsa_port_is_bridged(p
))
974 err
= dsa_slave_bridge_port_leave(dev
);
979 int dsa_slave_netdevice_event(struct notifier_block
*unused
,
980 unsigned long event
, void *ptr
)
982 struct net_device
*dev
;
986 case NETDEV_CHANGEUPPER
:
987 dev
= netdev_notifier_info_to_dev(ptr
);
988 if (!dsa_slave_dev_check(dev
))
991 err
= dsa_slave_master_changed(dev
);
993 netdev_warn(dev
, "failed to reflect master change\n");