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