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