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