Merge tag 'for_v4.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[linux-2.6-block.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>
aab9c406 16#include <linux/phylink.h>
0d8bcdd3
FF
17#include <linux/of_net.h>
18#include <linux/of_mdio.h>
7f854420 19#include <linux/mdio.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>
90af1059 25#include <linux/ptp_classify.h>
ea5dd34b 26
91da11f8
LB
27#include "dsa_priv.h"
28
f50f2127
FF
29static bool dsa_slave_dev_check(struct net_device *dev);
30
91da11f8
LB
31/* slave mii_bus handling ***************************************************/
32static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
33{
34 struct dsa_switch *ds = bus->priv;
35
0d8bcdd3 36 if (ds->phys_mii_mask & (1 << addr))
9d490b4e 37 return ds->ops->phy_read(ds, addr, reg);
91da11f8
LB
38
39 return 0xffff;
40}
41
42static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
43{
44 struct dsa_switch *ds = bus->priv;
45
0d8bcdd3 46 if (ds->phys_mii_mask & (1 << addr))
9d490b4e 47 return ds->ops->phy_write(ds, addr, reg, val);
91da11f8
LB
48
49 return 0;
50}
51
52void dsa_slave_mii_bus_init(struct dsa_switch *ds)
53{
54 ds->slave_mii_bus->priv = (void *)ds;
55 ds->slave_mii_bus->name = "dsa slave smi";
56 ds->slave_mii_bus->read = dsa_slave_phy_read;
57 ds->slave_mii_bus->write = dsa_slave_phy_write;
0b7b498d 58 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
49463b7f 59 ds->dst->index, ds->index);
c33063d6 60 ds->slave_mii_bus->parent = ds->dev;
24df8986 61 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
91da11f8
LB
62}
63
64
65/* slave device handling ****************************************************/
abd2be00 66static int dsa_slave_get_iflink(const struct net_device *dev)
c0840801 67{
d0006b00 68 return dsa_slave_to_master(dev)->ifindex;
c0840801
LB
69}
70
91da11f8
LB
71static int dsa_slave_open(struct net_device *dev)
72{
d0006b00 73 struct net_device *master = dsa_slave_to_master(dev);
d945097b 74 struct dsa_port *dp = dsa_slave_to_port(dev);
df02c6ff
LB
75 int err;
76
77 if (!(master->flags & IFF_UP))
78 return -ENETDOWN;
79
8feedbb4 80 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
a748ee24 81 err = dev_uc_add(master, dev->dev_addr);
df02c6ff
LB
82 if (err < 0)
83 goto out;
84 }
85
86 if (dev->flags & IFF_ALLMULTI) {
87 err = dev_set_allmulti(master, 1);
88 if (err < 0)
89 goto del_unicast;
90 }
91 if (dev->flags & IFF_PROMISC) {
92 err = dev_set_promiscuity(master, 1);
93 if (err < 0)
94 goto clear_allmulti;
95 }
96
0115dcd1 97 err = dsa_port_enable(dp, dev->phydev);
fb8a6a2b
VD
98 if (err)
99 goto clear_promisc;
b73adef6 100
aab9c406 101 phylink_start(dp->pl);
f7f1de51 102
91da11f8 103 return 0;
df02c6ff 104
b2f2af21
FF
105clear_promisc:
106 if (dev->flags & IFF_PROMISC)
4fdeddfe 107 dev_set_promiscuity(master, -1);
df02c6ff
LB
108clear_allmulti:
109 if (dev->flags & IFF_ALLMULTI)
110 dev_set_allmulti(master, -1);
111del_unicast:
8feedbb4 112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 113 dev_uc_del(master, dev->dev_addr);
df02c6ff
LB
114out:
115 return err;
91da11f8
LB
116}
117
118static int dsa_slave_close(struct net_device *dev)
119{
d0006b00 120 struct net_device *master = dsa_slave_to_master(dev);
d945097b 121 struct dsa_port *dp = dsa_slave_to_port(dev);
df02c6ff 122
aab9c406 123 phylink_stop(dp->pl);
f7f1de51 124
0115dcd1 125 dsa_port_disable(dp, dev->phydev);
6457edfe 126
df02c6ff 127 dev_mc_unsync(master, dev);
a748ee24 128 dev_uc_unsync(master, dev);
df02c6ff
LB
129 if (dev->flags & IFF_ALLMULTI)
130 dev_set_allmulti(master, -1);
131 if (dev->flags & IFF_PROMISC)
132 dev_set_promiscuity(master, -1);
133
8feedbb4 134 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 135 dev_uc_del(master, dev->dev_addr);
df02c6ff 136
91da11f8
LB
137 return 0;
138}
139
140static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
141{
d0006b00 142 struct net_device *master = dsa_slave_to_master(dev);
91da11f8
LB
143
144 if (change & IFF_ALLMULTI)
145 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
146 if (change & IFF_PROMISC)
147 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
148}
149
150static void dsa_slave_set_rx_mode(struct net_device *dev)
151{
d0006b00 152 struct net_device *master = dsa_slave_to_master(dev);
91da11f8
LB
153
154 dev_mc_sync(master, dev);
a748ee24 155 dev_uc_sync(master, dev);
91da11f8
LB
156}
157
df02c6ff 158static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
91da11f8 159{
d0006b00 160 struct net_device *master = dsa_slave_to_master(dev);
df02c6ff
LB
161 struct sockaddr *addr = a;
162 int err;
163
164 if (!is_valid_ether_addr(addr->sa_data))
165 return -EADDRNOTAVAIL;
166
167 if (!(dev->flags & IFF_UP))
168 goto out;
169
8feedbb4 170 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
a748ee24 171 err = dev_uc_add(master, addr->sa_data);
df02c6ff
LB
172 if (err < 0)
173 return err;
174 }
175
8feedbb4 176 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 177 dev_uc_del(master, dev->dev_addr);
df02c6ff
LB
178
179out:
d08f161a 180 ether_addr_copy(dev->dev_addr, addr->sa_data);
91da11f8
LB
181
182 return 0;
183}
184
2bedde1a
AS
185struct dsa_slave_dump_ctx {
186 struct net_device *dev;
187 struct sk_buff *skb;
188 struct netlink_callback *cb;
189 int idx;
190};
191
192static int
193dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
194 bool is_static, void *data)
195{
196 struct dsa_slave_dump_ctx *dump = data;
197 u32 portid = NETLINK_CB(dump->cb->skb).portid;
198 u32 seq = dump->cb->nlh->nlmsg_seq;
199 struct nlmsghdr *nlh;
200 struct ndmsg *ndm;
201
202 if (dump->idx < dump->cb->args[2])
203 goto skip;
204
205 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
206 sizeof(*ndm), NLM_F_MULTI);
207 if (!nlh)
208 return -EMSGSIZE;
209
210 ndm = nlmsg_data(nlh);
211 ndm->ndm_family = AF_BRIDGE;
212 ndm->ndm_pad1 = 0;
213 ndm->ndm_pad2 = 0;
214 ndm->ndm_flags = NTF_SELF;
215 ndm->ndm_type = 0;
216 ndm->ndm_ifindex = dump->dev->ifindex;
217 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
218
219 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
220 goto nla_put_failure;
221
222 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
223 goto nla_put_failure;
224
225 nlmsg_end(dump->skb, nlh);
226
227skip:
228 dump->idx++;
229 return 0;
230
231nla_put_failure:
232 nlmsg_cancel(dump->skb, nlh);
233 return -EMSGSIZE;
234}
235
236static int
237dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
238 struct net_device *dev, struct net_device *filter_dev,
239 int *idx)
240{
d945097b 241 struct dsa_port *dp = dsa_slave_to_port(dev);
2bedde1a
AS
242 struct dsa_slave_dump_ctx dump = {
243 .dev = dev,
244 .skb = skb,
245 .cb = cb,
246 .idx = *idx,
247 };
2bedde1a
AS
248 int err;
249
de40fc5d 250 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2bedde1a 251 *idx = dump.idx;
de40fc5d 252
2bedde1a
AS
253 return err;
254}
255
91da11f8
LB
256static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
257{
0336369d
BS
258 struct dsa_slave_priv *p = netdev_priv(dev);
259 struct dsa_switch *ds = p->dp->ds;
260 int port = p->dp->index;
261
262 /* Pass through to switch driver if it supports timestamping */
263 switch (cmd) {
264 case SIOCGHWTSTAMP:
265 if (ds->ops->port_hwtstamp_get)
266 return ds->ops->port_hwtstamp_get(ds, port, ifr);
267 break;
268 case SIOCSHWTSTAMP:
269 if (ds->ops->port_hwtstamp_set)
270 return ds->ops->port_hwtstamp_set(ds, port, ifr);
271 break;
272 }
273
aab9c406 274 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
91da11f8
LB
275}
276
35636062 277static int dsa_slave_port_attr_set(struct net_device *dev,
f7fadf30 278 const struct switchdev_attr *attr,
7ea6eb3f 279 struct switchdev_trans *trans)
35636062 280{
d945097b 281 struct dsa_port *dp = dsa_slave_to_port(dev);
b8d866ac 282 int ret;
35636062
SF
283
284 switch (attr->id) {
1f868398 285 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
fd364541 286 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
35636062 287 break;
fb2dabad 288 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
c02c4175
VD
289 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
290 trans);
fb2dabad 291 break;
34a79f63 292 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
072bb190 293 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
34a79f63 294 break;
35636062
SF
295 default:
296 ret = -EOPNOTSUPP;
297 break;
298 }
299
300 return ret;
301}
302
ba14d9eb 303static int dsa_slave_port_obj_add(struct net_device *dev,
648b4a99 304 const struct switchdev_obj *obj,
7ea6eb3f 305 struct switchdev_trans *trans)
ba14d9eb 306{
d945097b 307 struct dsa_port *dp = dsa_slave_to_port(dev);
ba14d9eb
VD
308 int err;
309
310 /* For the prepare phase, ensure the full set of changes is feasable in
311 * one go in order to signal a failure properly. If an operation is not
312 * supported, return -EOPNOTSUPP.
313 */
314
9e8f4a54 315 switch (obj->id) {
8df30255 316 case SWITCHDEV_OBJ_ID_PORT_MDB:
bcebb976 317 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
8df30255 318 break;
5f4dbc50
AL
319 case SWITCHDEV_OBJ_ID_HOST_MDB:
320 /* DSA can directly translate this to a normal MDB add,
321 * but on the CPU port.
322 */
323 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
324 trans);
325 break;
57d80838 326 case SWITCHDEV_OBJ_ID_PORT_VLAN:
01676d12
VD
327 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
328 trans);
11149536 329 break;
ba14d9eb
VD
330 default:
331 err = -EOPNOTSUPP;
332 break;
333 }
334
335 return err;
336}
337
338static int dsa_slave_port_obj_del(struct net_device *dev,
648b4a99 339 const struct switchdev_obj *obj)
ba14d9eb 340{
d945097b 341 struct dsa_port *dp = dsa_slave_to_port(dev);
ba14d9eb
VD
342 int err;
343
9e8f4a54 344 switch (obj->id) {
8df30255 345 case SWITCHDEV_OBJ_ID_PORT_MDB:
bcebb976 346 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
8df30255 347 break;
5f4dbc50
AL
348 case SWITCHDEV_OBJ_ID_HOST_MDB:
349 /* DSA can directly translate this to a normal MDB add,
350 * but on the CPU port.
351 */
352 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
353 break;
57d80838 354 case SWITCHDEV_OBJ_ID_PORT_VLAN:
01676d12 355 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
11149536 356 break;
ba14d9eb
VD
357 default:
358 err = -EOPNOTSUPP;
359 break;
360 }
361
362 return err;
363}
364
f8e20a9f
SF
365static int dsa_slave_port_attr_get(struct net_device *dev,
366 struct switchdev_attr *attr)
b73adef6 367{
d945097b
VD
368 struct dsa_port *dp = dsa_slave_to_port(dev);
369 struct dsa_switch *ds = dp->ds;
a42c8e33 370 struct dsa_switch_tree *dst = ds->dst;
b73adef6 371
f8e20a9f 372 switch (attr->id) {
1f868398 373 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
a42c8e33
AL
374 attr->u.ppid.id_len = sizeof(dst->index);
375 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
f8e20a9f 376 break;
c9e2105e
AS
377 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
378 attr->u.brport_flags_support = 0;
379 break;
f8e20a9f
SF
380 default:
381 return -EOPNOTSUPP;
382 }
b73adef6
FF
383
384 return 0;
385}
386
4fa7b718
VD
387static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
388 struct sk_buff *skb)
04ff53f9
FF
389{
390#ifdef CONFIG_NET_POLL_CONTROLLER
4fa7b718
VD
391 struct dsa_slave_priv *p = netdev_priv(dev);
392
04ff53f9
FF
393 if (p->netpoll)
394 netpoll_send_skb(p->netpoll, skb);
395#else
396 BUG();
397#endif
398 return NETDEV_TX_OK;
399}
400
90af1059
BS
401static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
402 struct sk_buff *skb)
403{
404 struct dsa_switch *ds = p->dp->ds;
405 struct sk_buff *clone;
406 unsigned int type;
407
408 type = ptp_classify_raw(skb);
409 if (type == PTP_CLASS_NONE)
410 return;
411
412 if (!ds->ops->port_txtstamp)
413 return;
414
415 clone = skb_clone_sk(skb);
416 if (!clone)
417 return;
418
419 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
420 return;
421
422 kfree_skb(clone);
423}
424
3e8a72d1
FF
425static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
426{
427 struct dsa_slave_priv *p = netdev_priv(dev);
5f6b4e14 428 struct pcpu_sw_netstats *s;
4ed70ce9 429 struct sk_buff *nskb;
3e8a72d1 430
5f6b4e14
FF
431 s = this_cpu_ptr(p->stats64);
432 u64_stats_update_begin(&s->syncp);
433 s->tx_packets++;
434 s->tx_bytes += skb->len;
435 u64_stats_update_end(&s->syncp);
3e8a72d1 436
90af1059
BS
437 /* Identify PTP protocol packets, clone them, and pass them to the
438 * switch driver
439 */
440 dsa_skb_tx_timestamp(p, skb);
441
fe47d563
VD
442 /* Transmit function may have to reallocate the original SKB,
443 * in which case it must have freed it. Only free it here on error.
444 */
4ed70ce9 445 nskb = p->xmit(skb, dev);
fe47d563
VD
446 if (!nskb) {
447 kfree_skb(skb);
4ed70ce9 448 return NETDEV_TX_OK;
fe47d563 449 }
5aed85ce 450
04ff53f9
FF
451 /* SKB for netpoll still need to be mangled with the protocol-specific
452 * tag to be successfully transmitted
453 */
454 if (unlikely(netpoll_tx_running(dev)))
4fa7b718 455 return dsa_slave_netpoll_send_skb(dev, nskb);
04ff53f9 456
4ed70ce9
FF
457 /* Queue the SKB for transmission on the parent interface, but
458 * do not modify its EtherType
459 */
d0006b00 460 nskb->dev = dsa_slave_to_master(dev);
4ed70ce9 461 dev_queue_xmit(nskb);
5aed85ce
FF
462
463 return NETDEV_TX_OK;
464}
465
91da11f8 466/* ethtool operations *******************************************************/
91da11f8 467
91da11f8
LB
468static void dsa_slave_get_drvinfo(struct net_device *dev,
469 struct ethtool_drvinfo *drvinfo)
470{
7826d43f 471 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
7826d43f
JP
472 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
473 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
91da11f8
LB
474}
475
3d762a0f
GR
476static int dsa_slave_get_regs_len(struct net_device *dev)
477{
d945097b
VD
478 struct dsa_port *dp = dsa_slave_to_port(dev);
479 struct dsa_switch *ds = dp->ds;
3d762a0f 480
9d490b4e 481 if (ds->ops->get_regs_len)
d945097b 482 return ds->ops->get_regs_len(ds, dp->index);
3d762a0f
GR
483
484 return -EOPNOTSUPP;
485}
486
487static void
488dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
489{
d945097b
VD
490 struct dsa_port *dp = dsa_slave_to_port(dev);
491 struct dsa_switch *ds = dp->ds;
3d762a0f 492
9d490b4e 493 if (ds->ops->get_regs)
d945097b 494 ds->ops->get_regs(ds, dp->index, regs, _p);
3d762a0f
GR
495}
496
aab9c406
FF
497static int dsa_slave_nway_reset(struct net_device *dev)
498{
499 struct dsa_port *dp = dsa_slave_to_port(dev);
500
501 return phylink_ethtool_nway_reset(dp->pl);
502}
503
6793abb4
GR
504static int dsa_slave_get_eeprom_len(struct net_device *dev)
505{
d945097b
VD
506 struct dsa_port *dp = dsa_slave_to_port(dev);
507 struct dsa_switch *ds = dp->ds;
6793abb4 508
0e576044 509 if (ds->cd && ds->cd->eeprom_len)
ff04955c 510 return ds->cd->eeprom_len;
6793abb4 511
9d490b4e
VD
512 if (ds->ops->get_eeprom_len)
513 return ds->ops->get_eeprom_len(ds);
6793abb4
GR
514
515 return 0;
516}
517
518static int dsa_slave_get_eeprom(struct net_device *dev,
519 struct ethtool_eeprom *eeprom, u8 *data)
520{
d945097b
VD
521 struct dsa_port *dp = dsa_slave_to_port(dev);
522 struct dsa_switch *ds = dp->ds;
6793abb4 523
9d490b4e
VD
524 if (ds->ops->get_eeprom)
525 return ds->ops->get_eeprom(ds, eeprom, data);
6793abb4
GR
526
527 return -EOPNOTSUPP;
528}
529
530static int dsa_slave_set_eeprom(struct net_device *dev,
531 struct ethtool_eeprom *eeprom, u8 *data)
532{
d945097b
VD
533 struct dsa_port *dp = dsa_slave_to_port(dev);
534 struct dsa_switch *ds = dp->ds;
6793abb4 535
9d490b4e
VD
536 if (ds->ops->set_eeprom)
537 return ds->ops->set_eeprom(ds, eeprom, data);
6793abb4
GR
538
539 return -EOPNOTSUPP;
540}
541
91da11f8
LB
542static void dsa_slave_get_strings(struct net_device *dev,
543 uint32_t stringset, uint8_t *data)
544{
d945097b
VD
545 struct dsa_port *dp = dsa_slave_to_port(dev);
546 struct dsa_switch *ds = dp->ds;
91da11f8
LB
547
548 if (stringset == ETH_SS_STATS) {
549 int len = ETH_GSTRING_LEN;
550
551 strncpy(data, "tx_packets", len);
552 strncpy(data + len, "tx_bytes", len);
553 strncpy(data + 2 * len, "rx_packets", len);
554 strncpy(data + 3 * len, "rx_bytes", len);
9d490b4e 555 if (ds->ops->get_strings)
89f09048
FF
556 ds->ops->get_strings(ds, dp->index, stringset,
557 data + 4 * len);
91da11f8
LB
558 }
559}
560
561static void dsa_slave_get_ethtool_stats(struct net_device *dev,
562 struct ethtool_stats *stats,
563 uint64_t *data)
564{
d945097b 565 struct dsa_port *dp = dsa_slave_to_port(dev);
91da11f8 566 struct dsa_slave_priv *p = netdev_priv(dev);
d945097b 567 struct dsa_switch *ds = dp->ds;
5f6b4e14 568 struct pcpu_sw_netstats *s;
f613ed66 569 unsigned int start;
5f6b4e14
FF
570 int i;
571
572 for_each_possible_cpu(i) {
573 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
574
575 s = per_cpu_ptr(p->stats64, i);
576 do {
577 start = u64_stats_fetch_begin_irq(&s->syncp);
578 tx_packets = s->tx_packets;
579 tx_bytes = s->tx_bytes;
580 rx_packets = s->rx_packets;
581 rx_bytes = s->rx_bytes;
582 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
583 data[0] += tx_packets;
584 data[1] += tx_bytes;
585 data[2] += rx_packets;
586 data[3] += rx_bytes;
587 }
9d490b4e 588 if (ds->ops->get_ethtool_stats)
d945097b 589 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
91da11f8
LB
590}
591
592static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
593{
d945097b
VD
594 struct dsa_port *dp = dsa_slave_to_port(dev);
595 struct dsa_switch *ds = dp->ds;
91da11f8
LB
596
597 if (sset == ETH_SS_STATS) {
598 int count;
599
600 count = 4;
9d490b4e 601 if (ds->ops->get_sset_count)
89f09048 602 count += ds->ops->get_sset_count(ds, dp->index, sset);
91da11f8
LB
603
604 return count;
605 }
606
607 return -EOPNOTSUPP;
608}
609
19e57c4e
FF
610static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
611{
d945097b
VD
612 struct dsa_port *dp = dsa_slave_to_port(dev);
613 struct dsa_switch *ds = dp->ds;
19e57c4e 614
aab9c406
FF
615 phylink_ethtool_get_wol(dp->pl, w);
616
9d490b4e 617 if (ds->ops->get_wol)
d945097b 618 ds->ops->get_wol(ds, dp->index, w);
19e57c4e
FF
619}
620
621static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
622{
d945097b
VD
623 struct dsa_port *dp = dsa_slave_to_port(dev);
624 struct dsa_switch *ds = dp->ds;
19e57c4e
FF
625 int ret = -EOPNOTSUPP;
626
aab9c406
FF
627 phylink_ethtool_set_wol(dp->pl, w);
628
9d490b4e 629 if (ds->ops->set_wol)
d945097b 630 ret = ds->ops->set_wol(ds, dp->index, w);
19e57c4e
FF
631
632 return ret;
633}
634
7905288f
FF
635static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
636{
d945097b
VD
637 struct dsa_port *dp = dsa_slave_to_port(dev);
638 struct dsa_switch *ds = dp->ds;
7905288f
FF
639 int ret;
640
7b9cc738 641 /* Port's PHY and MAC both need to be EEE capable */
1be52e97 642 if (!dev->phydev && !dp->pl)
7b9cc738
VD
643 return -ENODEV;
644
08f50061 645 if (!ds->ops->set_mac_eee)
7905288f
FF
646 return -EOPNOTSUPP;
647
d945097b 648 ret = ds->ops->set_mac_eee(ds, dp->index, e);
7905288f
FF
649 if (ret)
650 return ret;
651
aab9c406 652 return phylink_ethtool_set_eee(dp->pl, e);
7905288f
FF
653}
654
655static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
656{
d945097b
VD
657 struct dsa_port *dp = dsa_slave_to_port(dev);
658 struct dsa_switch *ds = dp->ds;
7905288f
FF
659 int ret;
660
7b9cc738 661 /* Port's PHY and MAC both need to be EEE capable */
1be52e97 662 if (!dev->phydev && !dp->pl)
7b9cc738
VD
663 return -ENODEV;
664
08f50061 665 if (!ds->ops->get_mac_eee)
7905288f
FF
666 return -EOPNOTSUPP;
667
d945097b 668 ret = ds->ops->get_mac_eee(ds, dp->index, e);
7905288f
FF
669 if (ret)
670 return ret;
671
aab9c406
FF
672 return phylink_ethtool_get_eee(dp->pl, e);
673}
674
675static int dsa_slave_get_link_ksettings(struct net_device *dev,
676 struct ethtool_link_ksettings *cmd)
677{
678 struct dsa_port *dp = dsa_slave_to_port(dev);
679
680 return phylink_ethtool_ksettings_get(dp->pl, cmd);
681}
682
683static int dsa_slave_set_link_ksettings(struct net_device *dev,
684 const struct ethtool_link_ksettings *cmd)
685{
686 struct dsa_port *dp = dsa_slave_to_port(dev);
687
688 return phylink_ethtool_ksettings_set(dp->pl, cmd);
7905288f
FF
689}
690
04ff53f9
FF
691#ifdef CONFIG_NET_POLL_CONTROLLER
692static int dsa_slave_netpoll_setup(struct net_device *dev,
693 struct netpoll_info *ni)
694{
d0006b00 695 struct net_device *master = dsa_slave_to_master(dev);
04ff53f9 696 struct dsa_slave_priv *p = netdev_priv(dev);
04ff53f9
FF
697 struct netpoll *netpoll;
698 int err = 0;
699
700 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
701 if (!netpoll)
702 return -ENOMEM;
703
704 err = __netpoll_setup(netpoll, master);
705 if (err) {
706 kfree(netpoll);
707 goto out;
708 }
709
710 p->netpoll = netpoll;
711out:
712 return err;
713}
714
715static void dsa_slave_netpoll_cleanup(struct net_device *dev)
716{
717 struct dsa_slave_priv *p = netdev_priv(dev);
718 struct netpoll *netpoll = p->netpoll;
719
720 if (!netpoll)
721 return;
722
723 p->netpoll = NULL;
724
c9fbd71f 725 __netpoll_free(netpoll);
04ff53f9
FF
726}
727
728static void dsa_slave_poll_controller(struct net_device *dev)
729{
730}
731#endif
732
44bb765c
FF
733static int dsa_slave_get_phys_port_name(struct net_device *dev,
734 char *name, size_t len)
735{
d945097b 736 struct dsa_port *dp = dsa_slave_to_port(dev);
44bb765c 737
d945097b 738 if (snprintf(name, len, "p%d", dp->index) >= len)
44bb765c 739 return -EINVAL;
3a543ef4
FF
740
741 return 0;
742}
743
f50f2127 744static struct dsa_mall_tc_entry *
4fa7b718 745dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
f50f2127 746{
4fa7b718 747 struct dsa_slave_priv *p = netdev_priv(dev);
f50f2127
FF
748 struct dsa_mall_tc_entry *mall_tc_entry;
749
750 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
751 if (mall_tc_entry->cookie == cookie)
752 return mall_tc_entry;
753
754 return NULL;
755}
756
757static int dsa_slave_add_cls_matchall(struct net_device *dev,
f50f2127
FF
758 struct tc_cls_matchall_offload *cls,
759 bool ingress)
760{
d945097b 761 struct dsa_port *dp = dsa_slave_to_port(dev);
f50f2127
FF
762 struct dsa_slave_priv *p = netdev_priv(dev);
763 struct dsa_mall_tc_entry *mall_tc_entry;
5fd9fc4e 764 __be16 protocol = cls->common.protocol;
d945097b 765 struct dsa_switch *ds = dp->ds;
f50f2127
FF
766 struct net_device *to_dev;
767 const struct tc_action *a;
d945097b 768 struct dsa_port *to_dp;
f50f2127 769 int err = -EOPNOTSUPP;
f50f2127
FF
770
771 if (!ds->ops->port_mirror_add)
772 return err;
773
3bcc0cec 774 if (!tcf_exts_has_one_action(cls->exts))
f50f2127
FF
775 return err;
776
244cd96a 777 a = tcf_exts_first_action(cls->exts);
f50f2127
FF
778
779 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
780 struct dsa_mall_mirror_tc_entry *mirror;
781
9f8a739e 782 to_dev = tcf_mirred_dev(a);
f50f2127
FF
783 if (!to_dev)
784 return -EINVAL;
785
786 if (!dsa_slave_dev_check(to_dev))
787 return -EOPNOTSUPP;
788
789 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
790 if (!mall_tc_entry)
791 return -ENOMEM;
792
793 mall_tc_entry->cookie = cls->cookie;
794 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
795 mirror = &mall_tc_entry->mirror;
796
d945097b 797 to_dp = dsa_slave_to_port(to_dev);
f50f2127 798
d945097b 799 mirror->to_local_port = to_dp->index;
f50f2127
FF
800 mirror->ingress = ingress;
801
d945097b 802 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
f50f2127
FF
803 if (err) {
804 kfree(mall_tc_entry);
805 return err;
806 }
807
808 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
809 }
810
811 return 0;
812}
813
814static void dsa_slave_del_cls_matchall(struct net_device *dev,
815 struct tc_cls_matchall_offload *cls)
816{
d945097b 817 struct dsa_port *dp = dsa_slave_to_port(dev);
f50f2127 818 struct dsa_mall_tc_entry *mall_tc_entry;
d945097b 819 struct dsa_switch *ds = dp->ds;
f50f2127
FF
820
821 if (!ds->ops->port_mirror_del)
822 return;
823
4fa7b718 824 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
f50f2127
FF
825 if (!mall_tc_entry)
826 return;
827
828 list_del(&mall_tc_entry->list);
829
830 switch (mall_tc_entry->type) {
831 case DSA_PORT_MALL_MIRROR:
d945097b 832 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
f50f2127
FF
833 break;
834 default:
835 WARN_ON(1);
836 }
837
838 kfree(mall_tc_entry);
839}
840
3fbae382 841static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
6b3eb752
JP
842 struct tc_cls_matchall_offload *cls,
843 bool ingress)
f50f2127 844{
5fd9fc4e 845 if (cls->common.chain_index)
a5fcf8a6 846 return -EOPNOTSUPP;
f50f2127 847
3fbae382
JP
848 switch (cls->command) {
849 case TC_CLSMATCHALL_REPLACE:
5fd9fc4e 850 return dsa_slave_add_cls_matchall(dev, cls, ingress);
3fbae382
JP
851 case TC_CLSMATCHALL_DESTROY:
852 dsa_slave_del_cls_matchall(dev, cls);
853 return 0;
854 default:
855 return -EOPNOTSUPP;
856 }
857}
858
6b3eb752
JP
859static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
860 void *cb_priv, bool ingress)
861{
862 struct net_device *dev = cb_priv;
863
44ae12a7
JP
864 if (!tc_can_offload(dev))
865 return -EOPNOTSUPP;
866
6b3eb752
JP
867 switch (type) {
868 case TC_SETUP_CLSMATCHALL:
869 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
870 default:
871 return -EOPNOTSUPP;
872 }
873}
874
875static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
876 void *type_data, void *cb_priv)
877{
878 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
879}
880
881static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
882 void *type_data, void *cb_priv)
883{
884 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
885}
886
887static int dsa_slave_setup_tc_block(struct net_device *dev,
888 struct tc_block_offload *f)
889{
890 tc_setup_cb_t *cb;
891
892 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
893 cb = dsa_slave_setup_tc_block_cb_ig;
894 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
895 cb = dsa_slave_setup_tc_block_cb_eg;
896 else
897 return -EOPNOTSUPP;
898
899 switch (f->command) {
900 case TC_BLOCK_BIND:
60513bd8 901 return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
6b3eb752
JP
902 case TC_BLOCK_UNBIND:
903 tcf_block_cb_unregister(f->block, cb, dev);
904 return 0;
905 default:
906 return -EOPNOTSUPP;
907 }
908}
909
3fbae382 910static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
de4784ca 911 void *type_data)
3fbae382 912{
2572ac53 913 switch (type) {
6b3eb752
JP
914 case TC_SETUP_BLOCK:
915 return dsa_slave_setup_tc_block(dev, type_data);
f50f2127 916 default:
a5fcf8a6 917 return -EOPNOTSUPP;
f50f2127 918 }
f50f2127
FF
919}
920
f613ed66
FF
921static void dsa_slave_get_stats64(struct net_device *dev,
922 struct rtnl_link_stats64 *stats)
923{
924 struct dsa_slave_priv *p = netdev_priv(dev);
5f6b4e14 925 struct pcpu_sw_netstats *s;
f613ed66 926 unsigned int start;
5f6b4e14 927 int i;
f613ed66
FF
928
929 netdev_stats_to_stats64(stats, &dev->stats);
5f6b4e14
FF
930 for_each_possible_cpu(i) {
931 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
932
933 s = per_cpu_ptr(p->stats64, i);
934 do {
935 start = u64_stats_fetch_begin_irq(&s->syncp);
936 tx_packets = s->tx_packets;
937 tx_bytes = s->tx_bytes;
938 rx_packets = s->rx_packets;
939 rx_bytes = s->rx_bytes;
940 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
941
942 stats->tx_packets += tx_packets;
943 stats->tx_bytes += tx_bytes;
944 stats->rx_packets += rx_packets;
945 stats->rx_bytes += rx_bytes;
946 }
f613ed66
FF
947}
948
bf9f2648
FF
949static int dsa_slave_get_rxnfc(struct net_device *dev,
950 struct ethtool_rxnfc *nfc, u32 *rule_locs)
951{
d945097b
VD
952 struct dsa_port *dp = dsa_slave_to_port(dev);
953 struct dsa_switch *ds = dp->ds;
bf9f2648
FF
954
955 if (!ds->ops->get_rxnfc)
956 return -EOPNOTSUPP;
957
d945097b 958 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
bf9f2648
FF
959}
960
961static int dsa_slave_set_rxnfc(struct net_device *dev,
962 struct ethtool_rxnfc *nfc)
963{
d945097b
VD
964 struct dsa_port *dp = dsa_slave_to_port(dev);
965 struct dsa_switch *ds = dp->ds;
bf9f2648
FF
966
967 if (!ds->ops->set_rxnfc)
968 return -EOPNOTSUPP;
969
d945097b 970 return ds->ops->set_rxnfc(ds, dp->index, nfc);
bf9f2648
FF
971}
972
0336369d
BS
973static int dsa_slave_get_ts_info(struct net_device *dev,
974 struct ethtool_ts_info *ts)
975{
976 struct dsa_slave_priv *p = netdev_priv(dev);
977 struct dsa_switch *ds = p->dp->ds;
978
979 if (!ds->ops->get_ts_info)
980 return -EOPNOTSUPP;
981
982 return ds->ops->get_ts_info(ds, p->dp->index, ts);
983}
984
91da11f8 985static const struct ethtool_ops dsa_slave_ethtool_ops = {
91da11f8 986 .get_drvinfo = dsa_slave_get_drvinfo,
3d762a0f
GR
987 .get_regs_len = dsa_slave_get_regs_len,
988 .get_regs = dsa_slave_get_regs,
aab9c406 989 .nway_reset = dsa_slave_nway_reset,
c4aef9fc 990 .get_link = ethtool_op_get_link,
6793abb4
GR
991 .get_eeprom_len = dsa_slave_get_eeprom_len,
992 .get_eeprom = dsa_slave_get_eeprom,
993 .set_eeprom = dsa_slave_set_eeprom,
91da11f8
LB
994 .get_strings = dsa_slave_get_strings,
995 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
996 .get_sset_count = dsa_slave_get_sset_count,
19e57c4e
FF
997 .set_wol = dsa_slave_set_wol,
998 .get_wol = dsa_slave_get_wol,
7905288f
FF
999 .set_eee = dsa_slave_set_eee,
1000 .get_eee = dsa_slave_get_eee,
aab9c406
FF
1001 .get_link_ksettings = dsa_slave_get_link_ksettings,
1002 .set_link_ksettings = dsa_slave_set_link_ksettings,
bf9f2648
FF
1003 .get_rxnfc = dsa_slave_get_rxnfc,
1004 .set_rxnfc = dsa_slave_set_rxnfc,
0336369d 1005 .get_ts_info = dsa_slave_get_ts_info,
91da11f8
LB
1006};
1007
2a93c1a3
FF
1008/* legacy way, bypassing the bridge *****************************************/
1009int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1010 struct net_device *dev,
1011 const unsigned char *addr, u16 vid,
1012 u16 flags)
1013{
1014 struct dsa_port *dp = dsa_slave_to_port(dev);
1015
1016 return dsa_port_fdb_add(dp, addr, vid);
1017}
1018
1019int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1020 struct net_device *dev,
1021 const unsigned char *addr, u16 vid)
1022{
1023 struct dsa_port *dp = dsa_slave_to_port(dev);
1024
1025 return dsa_port_fdb_del(dp, addr, vid);
1026}
1027
3e8a72d1 1028static const struct net_device_ops dsa_slave_netdev_ops = {
d442ad4a
SH
1029 .ndo_open = dsa_slave_open,
1030 .ndo_stop = dsa_slave_close,
3e8a72d1 1031 .ndo_start_xmit = dsa_slave_xmit,
d442ad4a
SH
1032 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1033 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
d442ad4a 1034 .ndo_set_mac_address = dsa_slave_set_mac_address,
37b8da1a
AS
1035 .ndo_fdb_add = dsa_legacy_fdb_add,
1036 .ndo_fdb_del = dsa_legacy_fdb_del,
2bedde1a 1037 .ndo_fdb_dump = dsa_slave_fdb_dump,
d442ad4a 1038 .ndo_do_ioctl = dsa_slave_ioctl,
abd2be00 1039 .ndo_get_iflink = dsa_slave_get_iflink,
04ff53f9
FF
1040#ifdef CONFIG_NET_POLL_CONTROLLER
1041 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1042 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1043 .ndo_poll_controller = dsa_slave_poll_controller,
1044#endif
44bb765c 1045 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
f50f2127 1046 .ndo_setup_tc = dsa_slave_setup_tc,
f613ed66 1047 .ndo_get_stats64 = dsa_slave_get_stats64,
98237d43
SF
1048};
1049
9d47c0a2 1050static const struct switchdev_ops dsa_slave_switchdev_ops = {
f8e20a9f 1051 .switchdev_port_attr_get = dsa_slave_port_attr_get,
35636062 1052 .switchdev_port_attr_set = dsa_slave_port_attr_set,
ba14d9eb
VD
1053 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1054 .switchdev_port_obj_del = dsa_slave_port_obj_del,
d442ad4a 1055};
91da11f8 1056
f37db85d
FF
1057static struct device_type dsa_type = {
1058 .name = "dsa",
1059};
1060
98cdb480
FF
1061static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
1062 char *buf)
1063{
1064 struct net_device *dev = to_net_dev(d);
1065 struct dsa_port *dp = dsa_slave_to_port(dev);
1066
1067 return sprintf(buf, "%s\n",
1068 dsa_tag_protocol_to_str(dp->cpu_dp->tag_ops));
1069}
1070static DEVICE_ATTR_RO(tagging);
1071
1072static struct attribute *dsa_slave_attrs[] = {
1073 &dev_attr_tagging.attr,
1074 NULL
1075};
1076
1077static const struct attribute_group dsa_group = {
1078 .name = "dsa",
1079 .attrs = dsa_slave_attrs,
1080};
1081
aab9c406
FF
1082static void dsa_slave_phylink_validate(struct net_device *dev,
1083 unsigned long *supported,
1084 struct phylink_link_state *state)
0d8bcdd3 1085{
d945097b 1086 struct dsa_port *dp = dsa_slave_to_port(dev);
d945097b 1087 struct dsa_switch *ds = dp->ds;
0d8bcdd3 1088
aab9c406
FF
1089 if (!ds->ops->phylink_validate)
1090 return;
0d8bcdd3 1091
aab9c406
FF
1092 ds->ops->phylink_validate(ds, dp->index, supported, state);
1093}
0d8bcdd3 1094
aab9c406
FF
1095static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1096 struct phylink_link_state *state)
1097{
1098 struct dsa_port *dp = dsa_slave_to_port(dev);
1099 struct dsa_switch *ds = dp->ds;
0d8bcdd3 1100
aab9c406
FF
1101 /* Only called for SGMII and 802.3z */
1102 if (!ds->ops->phylink_mac_link_state)
1103 return -EOPNOTSUPP;
ec9436ba 1104
aab9c406 1105 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
0d8bcdd3
FF
1106}
1107
aab9c406
FF
1108static void dsa_slave_phylink_mac_config(struct net_device *dev,
1109 unsigned int mode,
1110 const struct phylink_link_state *state)
ce31b31c 1111{
aab9c406
FF
1112 struct dsa_port *dp = dsa_slave_to_port(dev);
1113 struct dsa_switch *ds = dp->ds;
1114
1115 if (!ds->ops->phylink_mac_config)
1116 return;
b71be352 1117
aab9c406
FF
1118 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1119}
1120
1121static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1122{
1123 struct dsa_port *dp = dsa_slave_to_port(dev);
1124 struct dsa_switch *ds = dp->ds;
1125
1126 if (!ds->ops->phylink_mac_an_restart)
1127 return;
1128
1129 ds->ops->phylink_mac_an_restart(ds, dp->index);
1130}
1131
1132static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1133 unsigned int mode,
1134 phy_interface_t interface)
1135{
1136 struct dsa_port *dp = dsa_slave_to_port(dev);
1137 struct dsa_switch *ds = dp->ds;
1138
1139 if (!ds->ops->phylink_mac_link_down) {
1140 if (ds->ops->adjust_link && dev->phydev)
1141 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1142 return;
b71be352 1143 }
ce31b31c 1144
aab9c406
FF
1145 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1146}
1147
1148static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1149 unsigned int mode,
1150 phy_interface_t interface,
1151 struct phy_device *phydev)
1152{
1153 struct dsa_port *dp = dsa_slave_to_port(dev);
1154 struct dsa_switch *ds = dp->ds;
1155
1156 if (!ds->ops->phylink_mac_link_up) {
1157 if (ds->ops->adjust_link && dev->phydev)
1158 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1159 return;
1160 }
1161
1162 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1163}
1164
1165static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1166 .validate = dsa_slave_phylink_validate,
1167 .mac_link_state = dsa_slave_phylink_mac_link_state,
1168 .mac_config = dsa_slave_phylink_mac_config,
1169 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1170 .mac_link_down = dsa_slave_phylink_mac_link_down,
1171 .mac_link_up = dsa_slave_phylink_mac_link_up,
1172};
1173
1174void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1175{
1176 const struct dsa_port *dp = dsa_to_port(ds, port);
1177
1178 phylink_mac_change(dp->pl, up);
1179}
1180EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1181
1182static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1183 struct phylink_link_state *state)
1184{
1185 struct dsa_port *dp = dsa_slave_to_port(dev);
1186 struct dsa_switch *ds = dp->ds;
1187
1188 /* No need to check that this operation is valid, the callback would
1189 * not be called if it was not.
1190 */
1191 ds->ops->phylink_fixed_state(ds, dp->index, state);
ce31b31c
FF
1192}
1193
91da11f8 1194/* slave device setup *******************************************************/
4fa7b718 1195static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
c305c165 1196{
d945097b 1197 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
d945097b 1198 struct dsa_switch *ds = dp->ds;
c305c165 1199
0115dcd1
VD
1200 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1201 if (!slave_dev->phydev) {
d25b8e74 1202 netdev_err(slave_dev, "no phy at %d\n", addr);
c305c165 1203 return -ENODEV;
d25b8e74 1204 }
c305c165 1205
aab9c406 1206 return phylink_connect_phy(dp->pl, slave_dev->phydev);
11d8f3dd 1207}
11d8f3dd 1208
4fa7b718 1209static int dsa_slave_phy_setup(struct net_device *slave_dev)
0d8bcdd3 1210{
d945097b 1211 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
d945097b
VD
1212 struct device_node *port_dn = dp->dn;
1213 struct dsa_switch *ds = dp->ds;
6819563e 1214 u32 phy_flags = 0;
19334920 1215 int mode, ret;
0d8bcdd3 1216
19334920
GR
1217 mode = of_get_phy_mode(port_dn);
1218 if (mode < 0)
1219 mode = PHY_INTERFACE_MODE_NA;
0d8bcdd3 1220
aab9c406
FF
1221 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1222 &dsa_slave_phylink_mac_ops);
1223 if (IS_ERR(dp->pl)) {
1224 netdev_err(slave_dev,
1225 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1226 return PTR_ERR(dp->pl);
0d8bcdd3
FF
1227 }
1228
aab9c406
FF
1229 /* Register only if the switch provides such a callback, since this
1230 * callback takes precedence over polling the link GPIO in PHYLINK
1231 * (see phylink_get_fixed_state).
1232 */
1233 if (ds->ops->phylink_fixed_state)
1234 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1235
9d490b4e 1236 if (ds->ops->get_phy_flags)
d945097b 1237 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
6819563e 1238
aab9c406
FF
1239 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1240 if (ret == -ENODEV) {
1241 /* We could not connect to a designated PHY or SFP, so use the
1242 * switch internal MDIO bus instead
1243 */
d945097b 1244 ret = dsa_slave_phy_connect(slave_dev, dp->index);
d25b8e74 1245 if (ret) {
aab9c406
FF
1246 netdev_err(slave_dev,
1247 "failed to connect to port %d: %d\n",
d945097b 1248 dp->index, ret);
aab9c406 1249 phylink_destroy(dp->pl);
c305c165 1250 return ret;
d25b8e74 1251 }
b31f65fb 1252 }
9697f1cd
FF
1253
1254 return 0;
0d8bcdd3
FF
1255}
1256
448b4482
AL
1257static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1258static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1259 struct netdev_queue *txq,
1260 void *_unused)
1261{
1262 lockdep_set_class(&txq->_xmit_lock,
1263 &dsa_slave_netdev_xmit_lock_key);
1264}
1265
24462549
FF
1266int dsa_slave_suspend(struct net_device *slave_dev)
1267{
aab9c406 1268 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
24462549 1269
a94c689e
FF
1270 if (!netif_running(slave_dev))
1271 return 0;
1272
f154be24
FF
1273 netif_device_detach(slave_dev);
1274
aab9c406
FF
1275 rtnl_lock();
1276 phylink_stop(dp->pl);
1277 rtnl_unlock();
24462549
FF
1278
1279 return 0;
1280}
1281
1282int dsa_slave_resume(struct net_device *slave_dev)
1283{
aab9c406
FF
1284 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1285
a94c689e
FF
1286 if (!netif_running(slave_dev))
1287 return 0;
1288
24462549
FF
1289 netif_device_attach(slave_dev);
1290
aab9c406
FF
1291 rtnl_lock();
1292 phylink_start(dp->pl);
1293 rtnl_unlock();
24462549
FF
1294
1295 return 0;
1296}
1297
6158eaa7
VD
1298static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1299{
d0006b00 1300 struct net_device *master = dsa_slave_to_master(dev);
d945097b 1301 struct dsa_port *dp = dsa_slave_to_port(dev);
6158eaa7
VD
1302 struct dsa_notifier_register_info rinfo = {
1303 .switch_number = dp->ds->index,
1304 .port_number = dp->index,
1305 .master = master,
1306 .info.dev = dev,
1307 };
1308
1309 call_dsa_notifiers(val, dev, &rinfo.info);
1310}
1311
951259aa 1312int dsa_slave_create(struct dsa_port *port)
91da11f8 1313{
24a9332a 1314 const struct dsa_port *cpu_dp = port->cpu_dp;
f8b8b1cd 1315 struct net_device *master = cpu_dp->master;
4cfbf09c 1316 struct dsa_switch *ds = port->ds;
951259aa 1317 const char *name = port->name;
91da11f8
LB
1318 struct net_device *slave_dev;
1319 struct dsa_slave_priv *p;
1320 int ret;
1321
55199df6
FF
1322 if (!ds->num_tx_queues)
1323 ds->num_tx_queues = 1;
1324
1325 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1326 NET_NAME_UNKNOWN, ether_setup,
1327 ds->num_tx_queues, 1);
91da11f8 1328 if (slave_dev == NULL)
d87d6f44 1329 return -ENOMEM;
91da11f8 1330
f50f2127
FF
1331 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1332 slave_dev->hw_features |= NETIF_F_HW_TC;
7ad24ea4 1333 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2fcc8005 1334 eth_hw_addr_inherit(slave_dev, master);
0a5f107b 1335 slave_dev->priv_flags |= IFF_NO_QUEUE;
3e8a72d1 1336 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
9d47c0a2 1337 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
8b1efc0f
JW
1338 slave_dev->min_mtu = 0;
1339 slave_dev->max_mtu = ETH_MAX_MTU;
f37db85d 1340 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
d442ad4a 1341
448b4482
AL
1342 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1343 NULL);
1344
4cfbf09c
VD
1345 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1346 slave_dev->dev.of_node = port->dn;
5075314e
AD
1347 slave_dev->vlan_features = master->vlan_features;
1348
1349 p = netdev_priv(slave_dev);
5f6b4e14
FF
1350 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1351 if (!p->stats64) {
1352 free_netdev(slave_dev);
1353 return -ENOMEM;
1354 }
4cfbf09c 1355 p->dp = port;
f50f2127 1356 INIT_LIST_HEAD(&p->mall_tc_list);
15240248 1357 p->xmit = cpu_dp->tag_ops->xmit;
f8b8b1cd 1358 port->slave = slave_dev;
91da11f8
LB
1359
1360 netif_carrier_off(slave_dev);
1361
4fa7b718 1362 ret = dsa_slave_phy_setup(slave_dev);
0071f56e
AL
1363 if (ret) {
1364 netdev_err(master, "error %d setting up slave phy\n", ret);
e804441c
FF
1365 goto out_free;
1366 }
1367
6158eaa7 1368 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
60724d4b 1369
e804441c
FF
1370 ret = register_netdev(slave_dev);
1371 if (ret) {
1372 netdev_err(master, "error %d registering interface %s\n",
1373 ret, slave_dev->name);
1374 goto out_phy;
0071f56e
AL
1375 }
1376
98cdb480
FF
1377 ret = sysfs_create_group(&slave_dev->dev.kobj, &dsa_group);
1378 if (ret)
1379 goto out_unreg;
1380
d87d6f44 1381 return 0;
e804441c 1382
98cdb480
FF
1383out_unreg:
1384 unregister_netdev(slave_dev);
e804441c 1385out_phy:
aab9c406
FF
1386 rtnl_lock();
1387 phylink_disconnect_phy(p->dp->pl);
1388 rtnl_unlock();
1389 phylink_destroy(p->dp->pl);
e804441c
FF
1390out_free:
1391 free_percpu(p->stats64);
1392 free_netdev(slave_dev);
f8b8b1cd 1393 port->slave = NULL;
e804441c 1394 return ret;
91da11f8 1395}
b73adef6 1396
cda5c15b
NA
1397void dsa_slave_destroy(struct net_device *slave_dev)
1398{
d945097b 1399 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
cda5c15b
NA
1400 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1401
1402 netif_carrier_off(slave_dev);
aab9c406
FF
1403 rtnl_lock();
1404 phylink_disconnect_phy(dp->pl);
1405 rtnl_unlock();
881eadab 1406
6158eaa7 1407 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
98cdb480 1408 sysfs_remove_group(&slave_dev->dev.kobj, &dsa_group);
cda5c15b 1409 unregister_netdev(slave_dev);
aab9c406 1410 phylink_destroy(dp->pl);
5f6b4e14 1411 free_percpu(p->stats64);
cda5c15b
NA
1412 free_netdev(slave_dev);
1413}
1414
b73adef6
FF
1415static bool dsa_slave_dev_check(struct net_device *dev)
1416{
1417 return dev->netdev_ops == &dsa_slave_netdev_ops;
1418}
1419
8e92ab3a
VD
1420static int dsa_slave_changeupper(struct net_device *dev,
1421 struct netdev_notifier_changeupper_info *info)
b73adef6 1422{
d945097b 1423 struct dsa_port *dp = dsa_slave_to_port(dev);
8e92ab3a 1424 int err = NOTIFY_DONE;
b73adef6 1425
8e92ab3a
VD
1426 if (netif_is_bridge_master(info->upper_dev)) {
1427 if (info->linking) {
17d7802b 1428 err = dsa_port_bridge_join(dp, info->upper_dev);
8e92ab3a
VD
1429 err = notifier_from_errno(err);
1430 } else {
17d7802b 1431 dsa_port_bridge_leave(dp, info->upper_dev);
8e92ab3a 1432 err = NOTIFY_OK;
6debb68a 1433 }
6debb68a 1434 }
b73adef6 1435
8e92ab3a 1436 return err;
6debb68a 1437}
b73adef6 1438
88e4f0ca
VD
1439static int dsa_slave_netdevice_event(struct notifier_block *nb,
1440 unsigned long event, void *ptr)
6debb68a
VD
1441{
1442 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1443
53bade8a 1444 if (!dsa_slave_dev_check(dev))
8e92ab3a
VD
1445 return NOTIFY_DONE;
1446
1447 if (event == NETDEV_CHANGEUPPER)
1448 return dsa_slave_changeupper(dev, ptr);
b73adef6 1449
b73adef6
FF
1450 return NOTIFY_DONE;
1451}
88e4f0ca 1452
c9eb3e0f
AS
1453struct dsa_switchdev_event_work {
1454 struct work_struct work;
1455 struct switchdev_notifier_fdb_info fdb_info;
1456 struct net_device *dev;
1457 unsigned long event;
1458};
1459
1460static void dsa_slave_switchdev_event_work(struct work_struct *work)
1461{
1462 struct dsa_switchdev_event_work *switchdev_work =
1463 container_of(work, struct dsa_switchdev_event_work, work);
1464 struct net_device *dev = switchdev_work->dev;
1465 struct switchdev_notifier_fdb_info *fdb_info;
d945097b 1466 struct dsa_port *dp = dsa_slave_to_port(dev);
c9eb3e0f
AS
1467 int err;
1468
1469 rtnl_lock();
1470 switch (switchdev_work->event) {
1471 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1472 fdb_info = &switchdev_work->fdb_info;
a37fb855
VD
1473 if (!fdb_info->added_by_user)
1474 break;
1475
d945097b 1476 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
c9eb3e0f
AS
1477 if (err) {
1478 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1479 break;
1480 }
e9ba0fbc 1481 fdb_info->offloaded = true;
c9eb3e0f
AS
1482 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1483 &fdb_info->info);
1484 break;
1485
1486 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1487 fdb_info = &switchdev_work->fdb_info;
a37fb855
VD
1488 if (!fdb_info->added_by_user)
1489 break;
1490
d945097b 1491 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
c9eb3e0f
AS
1492 if (err) {
1493 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1494 dev_close(dev);
1495 }
1496 break;
1497 }
1498 rtnl_unlock();
1499
1500 kfree(switchdev_work->fdb_info.addr);
1501 kfree(switchdev_work);
1502 dev_put(dev);
1503}
1504
1505static int
1506dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1507 switchdev_work,
1508 const struct switchdev_notifier_fdb_info *
1509 fdb_info)
1510{
1511 memcpy(&switchdev_work->fdb_info, fdb_info,
1512 sizeof(switchdev_work->fdb_info));
1513 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1514 if (!switchdev_work->fdb_info.addr)
1515 return -ENOMEM;
1516 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1517 fdb_info->addr);
1518 return 0;
1519}
1520
1521/* Called under rcu_read_lock() */
1522static int dsa_slave_switchdev_event(struct notifier_block *unused,
1523 unsigned long event, void *ptr)
1524{
1525 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1526 struct dsa_switchdev_event_work *switchdev_work;
1527
1528 if (!dsa_slave_dev_check(dev))
1529 return NOTIFY_DONE;
1530
1531 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1532 if (!switchdev_work)
1533 return NOTIFY_BAD;
1534
1535 INIT_WORK(&switchdev_work->work,
1536 dsa_slave_switchdev_event_work);
1537 switchdev_work->dev = dev;
1538 switchdev_work->event = event;
1539
1540 switch (event) {
1541 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1542 case SWITCHDEV_FDB_DEL_TO_DEVICE:
a37fb855 1543 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
c9eb3e0f
AS
1544 goto err_fdb_work_init;
1545 dev_hold(dev);
1546 break;
1547 default:
1548 kfree(switchdev_work);
1549 return NOTIFY_DONE;
1550 }
1551
1552 dsa_schedule_work(&switchdev_work->work);
1553 return NOTIFY_OK;
1554
1555err_fdb_work_init:
1556 kfree(switchdev_work);
1557 return NOTIFY_BAD;
1558}
1559
88e4f0ca 1560static struct notifier_block dsa_slave_nb __read_mostly = {
c9eb3e0f
AS
1561 .notifier_call = dsa_slave_netdevice_event,
1562};
1563
1564static struct notifier_block dsa_slave_switchdev_notifier = {
1565 .notifier_call = dsa_slave_switchdev_event,
88e4f0ca
VD
1566};
1567
1568int dsa_slave_register_notifier(void)
1569{
c9eb3e0f
AS
1570 int err;
1571
1572 err = register_netdevice_notifier(&dsa_slave_nb);
1573 if (err)
1574 return err;
1575
1576 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1577 if (err)
1578 goto err_switchdev_nb;
1579
1580 return 0;
1581
1582err_switchdev_nb:
1583 unregister_netdevice_notifier(&dsa_slave_nb);
1584 return err;
88e4f0ca
VD
1585}
1586
1587void dsa_slave_unregister_notifier(void)
1588{
1589 int err;
1590
c9eb3e0f
AS
1591 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1592 if (err)
1593 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1594
88e4f0ca
VD
1595 err = unregister_netdevice_notifier(&dsa_slave_nb);
1596 if (err)
1597 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1598}