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