blob: 0010c690cc6715838c76da2d42c93ea9dcc113fc [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/slave.c - Slave device handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 *
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>
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080012#include <linux/etherdevice.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080013#include <linux/netdevice.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/phy.h>
Florian Fainellia2820542014-10-17 16:02:13 -070015#include <linux/phy_fixed.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070016#include <linux/of_net.h>
17#include <linux/of_mdio.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080018#include <net/rtnetlink.h>
Scott Feldman98237d42015-03-15 21:07:15 -070019#include <net/switchdev.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080020#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070021#include <linux/netpoll.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include "dsa_priv.h"
23
24/* slave mii_bus handling ***************************************************/
25static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
26{
27 struct dsa_switch *ds = bus->priv;
28
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070029 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000030 return ds->drv->phy_read(ds, addr, reg);
31
32 return 0xffff;
33}
34
35static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
36{
37 struct dsa_switch *ds = bus->priv;
38
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070039 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000040 return ds->drv->phy_write(ds, addr, reg, val);
41
42 return 0;
43}
44
45void dsa_slave_mii_bus_init(struct dsa_switch *ds)
46{
47 ds->slave_mii_bus->priv = (void *)ds;
48 ds->slave_mii_bus->name = "dsa slave smi";
49 ds->slave_mii_bus->read = dsa_slave_phy_read;
50 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainellif490be02013-01-21 09:58:50 +000051 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
52 ds->index, ds->pd->sw_addr);
Alexander Duyckb4d23942014-09-15 13:00:27 -040053 ds->slave_mii_bus->parent = ds->master_dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050054 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000055}
56
57
58/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020059static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000060{
61 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000062
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020063 return p->parent->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000064}
65
Florian Fainellib73adef2015-02-24 13:15:33 -080066static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
67{
68 return !!p->bridge_dev;
69}
70
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071static int dsa_slave_open(struct net_device *dev)
72{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080073 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +000074 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -070075 struct dsa_switch *ds = p->parent;
Florian Fainellib73adef2015-02-24 13:15:33 -080076 u8 stp_state = dsa_port_is_bridged(p) ?
77 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080078 int err;
79
80 if (!(master->flags & IFF_UP))
81 return -ENETDOWN;
82
Joe Perches8feedbb2012-05-08 18:56:57 +000083 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000084 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080085 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
Florian Fainellib2f2af22014-09-24 17:05:18 -0700100 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
Florian Fainellib73adef2015-02-24 13:15:33 -0800106 if (ds->drv->port_stp_update)
107 ds->drv->port_stp_update(ds, p->port, stp_state);
108
Florian Fainellif7f1de52014-09-24 17:05:17 -0700109 if (p->phy)
110 phy_start(p->phy);
111
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800113
Florian Fainellib2f2af22014-09-24 17:05:18 -0700114clear_promisc:
115 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300116 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800117clear_allmulti:
118 if (dev->flags & IFF_ALLMULTI)
119 dev_set_allmulti(master, -1);
120del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000121 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000122 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800123out:
124 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000125}
126
127static int dsa_slave_close(struct net_device *dev)
128{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800129 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000130 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -0700131 struct dsa_switch *ds = p->parent;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800132
Florian Fainellif7f1de52014-09-24 17:05:17 -0700133 if (p->phy)
134 phy_stop(p->phy);
135
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800136 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000137 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800138 if (dev->flags & IFF_ALLMULTI)
139 dev_set_allmulti(master, -1);
140 if (dev->flags & IFF_PROMISC)
141 dev_set_promiscuity(master, -1);
142
Joe Perches8feedbb2012-05-08 18:56:57 +0000143 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000144 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800145
Florian Fainellib2f2af22014-09-24 17:05:18 -0700146 if (ds->drv->port_disable)
147 ds->drv->port_disable(ds, p->port, p->phy);
148
Florian Fainellib73adef2015-02-24 13:15:33 -0800149 if (ds->drv->port_stp_update)
150 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
151
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152 return 0;
153}
154
155static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
156{
157 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000158 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000159
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
166static void dsa_slave_set_rx_mode(struct net_device *dev)
167{
168 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000169 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000170
171 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000172 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000173}
174
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800175static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000176{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800177 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000178 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800179 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
Joe Perches8feedbb2012-05-08 18:56:57 +0000188 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000189 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800190 if (err < 0)
191 return err;
192 }
193
Joe Perches8feedbb2012-05-08 18:56:57 +0000194 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000195 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800196
197out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800198 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000199
200 return 0;
201}
202
Guenter Roeck339d8262015-03-26 18:36:37 -0700203static int dsa_slave_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
204 struct net_device *dev,
205 const unsigned char *addr, u16 vid, u16 nlm_flags)
206{
207 struct dsa_slave_priv *p = netdev_priv(dev);
208 struct dsa_switch *ds = p->parent;
209 int ret = -EOPNOTSUPP;
210
211 if (ds->drv->fdb_add)
212 ret = ds->drv->fdb_add(ds, p->port, addr, vid);
213
214 return ret;
215}
216
217static int dsa_slave_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
218 struct net_device *dev,
219 const unsigned char *addr, u16 vid)
220{
221 struct dsa_slave_priv *p = netdev_priv(dev);
222 struct dsa_switch *ds = p->parent;
223 int ret = -EOPNOTSUPP;
224
225 if (ds->drv->fdb_del)
226 ret = ds->drv->fdb_del(ds, p->port, addr, vid);
227
228 return ret;
229}
230
231static int dsa_slave_fill_info(struct net_device *dev, struct sk_buff *skb,
232 const unsigned char *addr, u16 vid,
233 bool is_static,
234 u32 portid, u32 seq, int type,
235 unsigned int flags)
236{
237 struct nlmsghdr *nlh;
238 struct ndmsg *ndm;
239
240 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
241 if (!nlh)
242 return -EMSGSIZE;
243
244 ndm = nlmsg_data(nlh);
245 ndm->ndm_family = AF_BRIDGE;
246 ndm->ndm_pad1 = 0;
247 ndm->ndm_pad2 = 0;
248 ndm->ndm_flags = NTF_EXT_LEARNED;
249 ndm->ndm_type = 0;
250 ndm->ndm_ifindex = dev->ifindex;
251 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
252
253 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
254 goto nla_put_failure;
255
256 if (vid && nla_put_u16(skb, NDA_VLAN, vid))
257 goto nla_put_failure;
258
259 nlmsg_end(skb, nlh);
260 return 0;
261
262nla_put_failure:
263 nlmsg_cancel(skb, nlh);
264 return -EMSGSIZE;
265}
266
267/* Dump information about entries, in response to GETNEIGH */
268static int dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
269 struct net_device *dev,
270 struct net_device *filter_dev, int idx)
271{
272 struct dsa_slave_priv *p = netdev_priv(dev);
273 struct dsa_switch *ds = p->parent;
274 unsigned char addr[ETH_ALEN] = { 0 };
275 int ret;
276
277 if (!ds->drv->fdb_getnext)
278 return -EOPNOTSUPP;
279
280 for (; ; idx++) {
281 bool is_static;
282
283 ret = ds->drv->fdb_getnext(ds, p->port, addr, &is_static);
284 if (ret < 0)
285 break;
286
287 if (idx < cb->args[0])
288 continue;
289
290 ret = dsa_slave_fill_info(dev, skb, addr, 0,
291 is_static,
292 NETLINK_CB(cb->skb).portid,
293 cb->nlh->nlmsg_seq,
294 RTM_NEWNEIGH, NLM_F_MULTI);
295 if (ret < 0)
296 break;
297 }
298
299 return idx;
300}
301
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000302static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
303{
304 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000305
306 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000307 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000308
309 return -EOPNOTSUPP;
310}
311
Florian Fainellib73adef2015-02-24 13:15:33 -0800312/* Return a bitmask of all ports being currently bridged within a given bridge
313 * device. Note that on leave, the mask will still return the bitmask of ports
314 * currently bridged, prior to port removal, and this is exactly what we want.
315 */
316static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
317 struct net_device *bridge)
318{
319 struct dsa_slave_priv *p;
320 unsigned int port;
321 u32 mask = 0;
322
323 for (port = 0; port < DSA_MAX_PORTS; port++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800324 if (!dsa_is_port_initialized(ds, port))
Florian Fainellib73adef2015-02-24 13:15:33 -0800325 continue;
326
327 p = netdev_priv(ds->ports[port]);
328
329 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
330 p->bridge_dev == bridge)
331 mask |= 1 << port;
332 }
333
334 return mask;
335}
336
337static int dsa_slave_stp_update(struct net_device *dev, u8 state)
338{
339 struct dsa_slave_priv *p = netdev_priv(dev);
340 struct dsa_switch *ds = p->parent;
341 int ret = -EOPNOTSUPP;
342
343 if (ds->drv->port_stp_update)
344 ret = ds->drv->port_stp_update(ds, p->port, state);
345
346 return ret;
347}
348
Scott Feldman35636062015-05-10 09:47:51 -0700349static int dsa_slave_port_attr_set(struct net_device *dev,
350 struct switchdev_attr *attr)
351{
352 int ret = 0;
353
354 switch (attr->id) {
355 case SWITCHDEV_ATTR_PORT_STP_STATE:
356 if (attr->trans == SWITCHDEV_TRANS_COMMIT)
Scott Feldman42275bd2015-05-13 11:16:50 -0700357 ret = dsa_slave_stp_update(dev, attr->u.stp_state);
Scott Feldman35636062015-05-10 09:47:51 -0700358 break;
359 default:
360 ret = -EOPNOTSUPP;
361 break;
362 }
363
364 return ret;
365}
366
Florian Fainellib73adef2015-02-24 13:15:33 -0800367static int dsa_slave_bridge_port_join(struct net_device *dev,
368 struct net_device *br)
369{
370 struct dsa_slave_priv *p = netdev_priv(dev);
371 struct dsa_switch *ds = p->parent;
372 int ret = -EOPNOTSUPP;
373
374 p->bridge_dev = br;
375
376 if (ds->drv->port_join_bridge)
377 ret = ds->drv->port_join_bridge(ds, p->port,
378 dsa_slave_br_port_mask(ds, br));
379
380 return ret;
381}
382
383static int dsa_slave_bridge_port_leave(struct net_device *dev)
384{
385 struct dsa_slave_priv *p = netdev_priv(dev);
386 struct dsa_switch *ds = p->parent;
387 int ret = -EOPNOTSUPP;
388
389
390 if (ds->drv->port_leave_bridge)
391 ret = ds->drv->port_leave_bridge(ds, p->port,
392 dsa_slave_br_port_mask(ds, p->bridge_dev));
393
394 p->bridge_dev = NULL;
395
396 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
397 * so allow it to be in BR_STATE_FORWARDING to be kept functional
398 */
399 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
400
401 return ret;
402}
403
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700404static int dsa_slave_port_attr_get(struct net_device *dev,
405 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800406{
407 struct dsa_slave_priv *p = netdev_priv(dev);
408 struct dsa_switch *ds = p->parent;
409
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700410 switch (attr->id) {
411 case SWITCHDEV_ATTR_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700412 attr->u.ppid.id_len = sizeof(ds->index);
413 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700414 break;
415 default:
416 return -EOPNOTSUPP;
417 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800418
419 return 0;
420}
421
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700422static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
423 struct sk_buff *skb)
424{
425#ifdef CONFIG_NET_POLL_CONTROLLER
426 if (p->netpoll)
427 netpoll_send_skb(p->netpoll, skb);
428#else
429 BUG();
430#endif
431 return NETDEV_TX_OK;
432}
433
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700434static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
435{
436 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700437 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700438
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700439 dev->stats.tx_packets++;
440 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700441
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700442 /* Transmit function may have to reallocate the original SKB */
443 nskb = p->xmit(skb, dev);
444 if (!nskb)
445 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700446
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700447 /* SKB for netpoll still need to be mangled with the protocol-specific
448 * tag to be successfully transmitted
449 */
450 if (unlikely(netpoll_tx_running(dev)))
451 return dsa_netpoll_send_skb(p, nskb);
452
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700453 /* Queue the SKB for transmission on the parent interface, but
454 * do not modify its EtherType
455 */
456 nskb->dev = p->parent->dst->master_netdev;
457 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700458
459 return NETDEV_TX_OK;
460}
461
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700462static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
463 struct net_device *dev)
464{
465 /* Just return the original SKB */
466 return skb;
467}
468
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000469
470/* ethtool operations *******************************************************/
471static int
472dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
473{
474 struct dsa_slave_priv *p = netdev_priv(dev);
475 int err;
476
477 err = -EOPNOTSUPP;
478 if (p->phy != NULL) {
479 err = phy_read_status(p->phy);
480 if (err == 0)
481 err = phy_ethtool_gset(p->phy, cmd);
482 }
483
484 return err;
485}
486
487static int
488dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
489{
490 struct dsa_slave_priv *p = netdev_priv(dev);
491
492 if (p->phy != NULL)
493 return phy_ethtool_sset(p->phy, cmd);
494
495 return -EOPNOTSUPP;
496}
497
498static void dsa_slave_get_drvinfo(struct net_device *dev,
499 struct ethtool_drvinfo *drvinfo)
500{
Jiri Pirko7826d432013-01-06 00:44:26 +0000501 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
502 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
503 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
504 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000505}
506
Guenter Roeck3d762a02014-10-29 10:45:04 -0700507static int dsa_slave_get_regs_len(struct net_device *dev)
508{
509 struct dsa_slave_priv *p = netdev_priv(dev);
510 struct dsa_switch *ds = p->parent;
511
512 if (ds->drv->get_regs_len)
513 return ds->drv->get_regs_len(ds, p->port);
514
515 return -EOPNOTSUPP;
516}
517
518static void
519dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
520{
521 struct dsa_slave_priv *p = netdev_priv(dev);
522 struct dsa_switch *ds = p->parent;
523
524 if (ds->drv->get_regs)
525 ds->drv->get_regs(ds, p->port, regs, _p);
526}
527
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000528static int dsa_slave_nway_reset(struct net_device *dev)
529{
530 struct dsa_slave_priv *p = netdev_priv(dev);
531
532 if (p->phy != NULL)
533 return genphy_restart_aneg(p->phy);
534
535 return -EOPNOTSUPP;
536}
537
538static u32 dsa_slave_get_link(struct net_device *dev)
539{
540 struct dsa_slave_priv *p = netdev_priv(dev);
541
542 if (p->phy != NULL) {
543 genphy_update_link(p->phy);
544 return p->phy->link;
545 }
546
547 return -EOPNOTSUPP;
548}
549
Guenter Roeck6793abb2014-10-29 10:45:01 -0700550static int dsa_slave_get_eeprom_len(struct net_device *dev)
551{
552 struct dsa_slave_priv *p = netdev_priv(dev);
553 struct dsa_switch *ds = p->parent;
554
555 if (ds->pd->eeprom_len)
556 return ds->pd->eeprom_len;
557
558 if (ds->drv->get_eeprom_len)
559 return ds->drv->get_eeprom_len(ds);
560
561 return 0;
562}
563
564static int dsa_slave_get_eeprom(struct net_device *dev,
565 struct ethtool_eeprom *eeprom, u8 *data)
566{
567 struct dsa_slave_priv *p = netdev_priv(dev);
568 struct dsa_switch *ds = p->parent;
569
570 if (ds->drv->get_eeprom)
571 return ds->drv->get_eeprom(ds, eeprom, data);
572
573 return -EOPNOTSUPP;
574}
575
576static int dsa_slave_set_eeprom(struct net_device *dev,
577 struct ethtool_eeprom *eeprom, u8 *data)
578{
579 struct dsa_slave_priv *p = netdev_priv(dev);
580 struct dsa_switch *ds = p->parent;
581
582 if (ds->drv->set_eeprom)
583 return ds->drv->set_eeprom(ds, eeprom, data);
584
585 return -EOPNOTSUPP;
586}
587
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000588static void dsa_slave_get_strings(struct net_device *dev,
589 uint32_t stringset, uint8_t *data)
590{
591 struct dsa_slave_priv *p = netdev_priv(dev);
592 struct dsa_switch *ds = p->parent;
593
594 if (stringset == ETH_SS_STATS) {
595 int len = ETH_GSTRING_LEN;
596
597 strncpy(data, "tx_packets", len);
598 strncpy(data + len, "tx_bytes", len);
599 strncpy(data + 2 * len, "rx_packets", len);
600 strncpy(data + 3 * len, "rx_bytes", len);
601 if (ds->drv->get_strings != NULL)
602 ds->drv->get_strings(ds, p->port, data + 4 * len);
603 }
604}
605
606static void dsa_slave_get_ethtool_stats(struct net_device *dev,
607 struct ethtool_stats *stats,
608 uint64_t *data)
609{
610 struct dsa_slave_priv *p = netdev_priv(dev);
611 struct dsa_switch *ds = p->parent;
612
613 data[0] = p->dev->stats.tx_packets;
614 data[1] = p->dev->stats.tx_bytes;
615 data[2] = p->dev->stats.rx_packets;
616 data[3] = p->dev->stats.rx_bytes;
617 if (ds->drv->get_ethtool_stats != NULL)
618 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
619}
620
621static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
622{
623 struct dsa_slave_priv *p = netdev_priv(dev);
624 struct dsa_switch *ds = p->parent;
625
626 if (sset == ETH_SS_STATS) {
627 int count;
628
629 count = 4;
630 if (ds->drv->get_sset_count != NULL)
631 count += ds->drv->get_sset_count(ds);
632
633 return count;
634 }
635
636 return -EOPNOTSUPP;
637}
638
Florian Fainelli19e57c42014-09-18 17:31:24 -0700639static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
640{
641 struct dsa_slave_priv *p = netdev_priv(dev);
642 struct dsa_switch *ds = p->parent;
643
644 if (ds->drv->get_wol)
645 ds->drv->get_wol(ds, p->port, w);
646}
647
648static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
649{
650 struct dsa_slave_priv *p = netdev_priv(dev);
651 struct dsa_switch *ds = p->parent;
652 int ret = -EOPNOTSUPP;
653
654 if (ds->drv->set_wol)
655 ret = ds->drv->set_wol(ds, p->port, w);
656
657 return ret;
658}
659
Florian Fainelli79052882014-09-24 17:05:21 -0700660static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
661{
662 struct dsa_slave_priv *p = netdev_priv(dev);
663 struct dsa_switch *ds = p->parent;
664 int ret;
665
666 if (!ds->drv->set_eee)
667 return -EOPNOTSUPP;
668
669 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
670 if (ret)
671 return ret;
672
673 if (p->phy)
674 ret = phy_ethtool_set_eee(p->phy, e);
675
676 return ret;
677}
678
679static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
680{
681 struct dsa_slave_priv *p = netdev_priv(dev);
682 struct dsa_switch *ds = p->parent;
683 int ret;
684
685 if (!ds->drv->get_eee)
686 return -EOPNOTSUPP;
687
688 ret = ds->drv->get_eee(ds, p->port, e);
689 if (ret)
690 return ret;
691
692 if (p->phy)
693 ret = phy_ethtool_get_eee(p->phy, e);
694
695 return ret;
696}
697
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700698#ifdef CONFIG_NET_POLL_CONTROLLER
699static int dsa_slave_netpoll_setup(struct net_device *dev,
700 struct netpoll_info *ni)
701{
702 struct dsa_slave_priv *p = netdev_priv(dev);
703 struct dsa_switch *ds = p->parent;
704 struct net_device *master = ds->dst->master_netdev;
705 struct netpoll *netpoll;
706 int err = 0;
707
708 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
709 if (!netpoll)
710 return -ENOMEM;
711
712 err = __netpoll_setup(netpoll, master);
713 if (err) {
714 kfree(netpoll);
715 goto out;
716 }
717
718 p->netpoll = netpoll;
719out:
720 return err;
721}
722
723static void dsa_slave_netpoll_cleanup(struct net_device *dev)
724{
725 struct dsa_slave_priv *p = netdev_priv(dev);
726 struct netpoll *netpoll = p->netpoll;
727
728 if (!netpoll)
729 return;
730
731 p->netpoll = NULL;
732
733 __netpoll_free_async(netpoll);
734}
735
736static void dsa_slave_poll_controller(struct net_device *dev)
737{
738}
739#endif
740
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000741static const struct ethtool_ops dsa_slave_ethtool_ops = {
742 .get_settings = dsa_slave_get_settings,
743 .set_settings = dsa_slave_set_settings,
744 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700745 .get_regs_len = dsa_slave_get_regs_len,
746 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000747 .nway_reset = dsa_slave_nway_reset,
748 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700749 .get_eeprom_len = dsa_slave_get_eeprom_len,
750 .get_eeprom = dsa_slave_get_eeprom,
751 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000752 .get_strings = dsa_slave_get_strings,
753 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
754 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700755 .set_wol = dsa_slave_set_wol,
756 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700757 .set_eee = dsa_slave_set_eee,
758 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000759};
760
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700761static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800762 .ndo_open = dsa_slave_open,
763 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700764 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800765 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
766 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800767 .ndo_set_mac_address = dsa_slave_set_mac_address,
Guenter Roeck339d8262015-03-26 18:36:37 -0700768 .ndo_fdb_add = dsa_slave_fdb_add,
769 .ndo_fdb_del = dsa_slave_fdb_del,
770 .ndo_fdb_dump = dsa_slave_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800771 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +0200772 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700773#ifdef CONFIG_NET_POLL_CONTROLLER
774 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
775 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
776 .ndo_poll_controller = dsa_slave_poll_controller,
777#endif
Scott Feldman98237d42015-03-15 21:07:15 -0700778};
779
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700780static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700781 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -0700782 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800783};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000784
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700785static void dsa_slave_adjust_link(struct net_device *dev)
786{
787 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700788 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700789 unsigned int status_changed = 0;
790
791 if (p->old_link != p->phy->link) {
792 status_changed = 1;
793 p->old_link = p->phy->link;
794 }
795
796 if (p->old_duplex != p->phy->duplex) {
797 status_changed = 1;
798 p->old_duplex = p->phy->duplex;
799 }
800
801 if (p->old_pause != p->phy->pause) {
802 status_changed = 1;
803 p->old_pause = p->phy->pause;
804 }
805
Florian Fainelliec9436b2014-08-27 17:04:53 -0700806 if (ds->drv->adjust_link && status_changed)
807 ds->drv->adjust_link(ds, p->port, p->phy);
808
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700809 if (status_changed)
810 phy_print_status(p->phy);
811}
812
Florian Fainellice31b312014-08-27 17:04:54 -0700813static int dsa_slave_fixed_link_update(struct net_device *dev,
814 struct fixed_phy_status *status)
815{
816 struct dsa_slave_priv *p = netdev_priv(dev);
817 struct dsa_switch *ds = p->parent;
818
819 if (ds->drv->fixed_link_update)
820 ds->drv->fixed_link_update(ds, p->port, status);
821
822 return 0;
823}
824
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000825/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -0700826static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700827 struct net_device *slave_dev,
828 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -0700829{
830 struct dsa_switch *ds = p->parent;
831
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700832 p->phy = ds->slave_mii_bus->phy_map[addr];
Florian Fainellic305c162015-03-10 16:57:12 -0700833 if (!p->phy)
834 return -ENODEV;
835
836 /* Use already configured phy mode */
837 p->phy_interface = p->phy->interface;
838 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
839 p->phy_interface);
840
841 return 0;
842}
843
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800844static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700845 struct net_device *slave_dev)
846{
847 struct dsa_switch *ds = p->parent;
848 struct dsa_chip_data *cd = ds->pd;
849 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -0700850 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -0700851 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -0800852 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700853
854 port_dn = cd->port_dn[p->port];
Guenter Roeck19334922015-02-16 21:23:51 -0800855 mode = of_get_phy_mode(port_dn);
856 if (mode < 0)
857 mode = PHY_INTERFACE_MODE_NA;
858 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700859
860 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
861 if (of_phy_is_fixed_link(port_dn)) {
862 /* In the case of a fixed PHY, the DT node associated
863 * to the fixed PHY is the Port DT node
864 */
865 ret = of_phy_register_fixed_link(port_dn);
866 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800867 netdev_err(slave_dev, "failed to register fixed PHY\n");
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800868 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700869 }
Florian Fainellice31b312014-08-27 17:04:54 -0700870 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700871 phy_dn = port_dn;
872 }
873
Florian Fainelli68195632014-09-19 13:07:54 -0700874 if (ds->drv->get_phy_flags)
875 phy_flags = ds->drv->get_phy_flags(ds, p->port);
876
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700877 if (phy_dn) {
878 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
879 /* If this PHY address is part of phys_mii_mask, which means
880 * that we need to divert reads and writes to/from it, then we
881 * want to bind this device using the slave MII bus created by
882 * DSA to make that happen.
883 */
Florian Fainelli96026d02015-03-14 13:21:59 -0700884 if (!phy_is_fixed && ret >= 0 &&
885 (ds->phys_mii_mask & (1 << ret))) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700886 ret = dsa_slave_phy_connect(p, slave_dev, ret);
887 if (ret)
888 return ret;
889 } else {
890 p->phy = of_phy_connect(slave_dev, phy_dn,
891 dsa_slave_adjust_link,
892 phy_flags,
893 p->phy_interface);
894 }
895 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700896
Florian Fainellice31b312014-08-27 17:04:54 -0700897 if (p->phy && phy_is_fixed)
898 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
899
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700900 /* We could not connect to a designated PHY, so use the switch internal
901 * MDIO bus instead
902 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100903 if (!p->phy) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700904 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
Florian Fainellic305c162015-03-10 16:57:12 -0700905 if (ret)
906 return ret;
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100907 } else {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800908 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
909 p->phy->addr, p->phy->drv->name);
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100910 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800911
912 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700913}
914
Andrew Lunn448b4482015-05-06 01:09:56 +0200915static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
916static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
917 struct netdev_queue *txq,
918 void *_unused)
919{
920 lockdep_set_class(&txq->_xmit_lock,
921 &dsa_slave_netdev_xmit_lock_key);
922}
923
Florian Fainelli24462542014-09-18 17:31:22 -0700924int dsa_slave_suspend(struct net_device *slave_dev)
925{
926 struct dsa_slave_priv *p = netdev_priv(slave_dev);
927
Florian Fainelli24462542014-09-18 17:31:22 -0700928 if (p->phy) {
929 phy_stop(p->phy);
930 p->old_pause = -1;
931 p->old_link = -1;
932 p->old_duplex = -1;
933 phy_suspend(p->phy);
934 }
935
936 return 0;
937}
938
939int dsa_slave_resume(struct net_device *slave_dev)
940{
941 struct dsa_slave_priv *p = netdev_priv(slave_dev);
942
943 netif_device_attach(slave_dev);
944
945 if (p->phy) {
946 phy_resume(p->phy);
947 phy_start(p->phy);
948 }
949
950 return 0;
951}
952
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800953int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
954 int port, char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000955{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000956 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000957 struct net_device *slave_dev;
958 struct dsa_slave_priv *p;
959 int ret;
960
Tom Gundersenc835a672014-07-14 16:37:24 +0200961 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
962 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000963 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800964 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000965
966 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000967 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +0200968 eth_hw_addr_inherit(slave_dev, master);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000969 slave_dev->tx_queue_len = 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700970 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700971 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800972
Andrew Lunn448b4482015-05-06 01:09:56 +0200973 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
974 NULL);
975
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000976 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -0700977 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000978 slave_dev->vlan_features = master->vlan_features;
979
980 p = netdev_priv(slave_dev);
981 p->dev = slave_dev;
982 p->parent = ds;
983 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700984
Alexander Duyck50753142014-09-15 13:00:19 -0400985 switch (ds->dst->tag_protocol) {
986#ifdef CONFIG_NET_DSA_TAG_DSA
987 case DSA_TAG_PROTO_DSA:
988 p->xmit = dsa_netdev_ops.xmit;
989 break;
990#endif
991#ifdef CONFIG_NET_DSA_TAG_EDSA
992 case DSA_TAG_PROTO_EDSA:
993 p->xmit = edsa_netdev_ops.xmit;
994 break;
995#endif
996#ifdef CONFIG_NET_DSA_TAG_TRAILER
997 case DSA_TAG_PROTO_TRAILER:
998 p->xmit = trailer_netdev_ops.xmit;
999 break;
1000#endif
1001#ifdef CONFIG_NET_DSA_TAG_BRCM
1002 case DSA_TAG_PROTO_BRCM:
1003 p->xmit = brcm_netdev_ops.xmit;
1004 break;
1005#endif
1006 default:
1007 p->xmit = dsa_slave_notag_xmit;
1008 break;
1009 }
1010
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001011 p->old_pause = -1;
1012 p->old_link = -1;
1013 p->old_duplex = -1;
1014
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001015 ret = dsa_slave_phy_setup(p, slave_dev);
1016 if (ret) {
1017 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001018 return ret;
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001019 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001020
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001021 ds->ports[port] = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001022 ret = register_netdev(slave_dev);
1023 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001024 netdev_err(master, "error %d registering interface %s\n",
1025 ret, slave_dev->name);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001026 phy_disconnect(p->phy);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001027 ds->ports[port] = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001028 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001029 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001030 }
1031
1032 netif_carrier_off(slave_dev);
1033
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001034 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001035}
Florian Fainellib73adef2015-02-24 13:15:33 -08001036
1037static bool dsa_slave_dev_check(struct net_device *dev)
1038{
1039 return dev->netdev_ops == &dsa_slave_netdev_ops;
1040}
1041
1042static int dsa_slave_master_changed(struct net_device *dev)
1043{
1044 struct net_device *master = netdev_master_upper_dev_get(dev);
Guenter Roeckb06b1072015-03-25 08:08:37 -07001045 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellib73adef2015-02-24 13:15:33 -08001046 int err = 0;
1047
1048 if (master && master->rtnl_link_ops &&
1049 !strcmp(master->rtnl_link_ops->kind, "bridge"))
1050 err = dsa_slave_bridge_port_join(dev, master);
Guenter Roeckb06b1072015-03-25 08:08:37 -07001051 else if (dsa_port_is_bridged(p))
Florian Fainellib73adef2015-02-24 13:15:33 -08001052 err = dsa_slave_bridge_port_leave(dev);
1053
1054 return err;
1055}
1056
1057int dsa_slave_netdevice_event(struct notifier_block *unused,
1058 unsigned long event, void *ptr)
1059{
1060 struct net_device *dev;
1061 int err = 0;
1062
1063 switch (event) {
1064 case NETDEV_CHANGEUPPER:
1065 dev = netdev_notifier_info_to_dev(ptr);
1066 if (!dsa_slave_dev_check(dev))
1067 goto out;
1068
1069 err = dsa_slave_master_changed(dev);
1070 if (err)
1071 netdev_warn(dev, "failed to reflect master change\n");
1072
1073 break;
1074 }
1075
1076out:
1077 return NOTIFY_DONE;
1078}