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