Merge tag 'drm/tegra/for-5.15-rc3' of ssh://git.freedesktop.org/git/tegra/linux into...
[platform/kernel/linux-rpi.git] / net / dsa / slave.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/dsa/slave.c - Slave device handling
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  */
6
7 #include <linux/list.h>
8 #include <linux/etherdevice.h>
9 #include <linux/netdevice.h>
10 #include <linux/phy.h>
11 #include <linux/phy_fixed.h>
12 #include <linux/phylink.h>
13 #include <linux/of_net.h>
14 #include <linux/of_mdio.h>
15 #include <linux/mdio.h>
16 #include <net/rtnetlink.h>
17 #include <net/pkt_cls.h>
18 #include <net/selftests.h>
19 #include <net/tc_act/tc_mirred.h>
20 #include <linux/if_bridge.h>
21 #include <linux/if_hsr.h>
22 #include <linux/netpoll.h>
23
24 #include "dsa_priv.h"
25
26 /* slave mii_bus handling ***************************************************/
27 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
28 {
29         struct dsa_switch *ds = bus->priv;
30
31         if (ds->phys_mii_mask & (1 << addr))
32                 return ds->ops->phy_read(ds, addr, reg);
33
34         return 0xffff;
35 }
36
37 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
38 {
39         struct dsa_switch *ds = bus->priv;
40
41         if (ds->phys_mii_mask & (1 << addr))
42                 return ds->ops->phy_write(ds, addr, reg, val);
43
44         return 0;
45 }
46
47 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
48 {
49         ds->slave_mii_bus->priv = (void *)ds;
50         ds->slave_mii_bus->name = "dsa slave smi";
51         ds->slave_mii_bus->read = dsa_slave_phy_read;
52         ds->slave_mii_bus->write = dsa_slave_phy_write;
53         snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
54                  ds->dst->index, ds->index);
55         ds->slave_mii_bus->parent = ds->dev;
56         ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
57 }
58
59
60 /* slave device handling ****************************************************/
61 static int dsa_slave_get_iflink(const struct net_device *dev)
62 {
63         return dsa_slave_to_master(dev)->ifindex;
64 }
65
66 static int dsa_slave_open(struct net_device *dev)
67 {
68         struct net_device *master = dsa_slave_to_master(dev);
69         struct dsa_port *dp = dsa_slave_to_port(dev);
70         int err;
71
72         err = dev_open(master, NULL);
73         if (err < 0) {
74                 netdev_err(dev, "failed to open master %s\n", master->name);
75                 goto out;
76         }
77
78         if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
79                 err = dev_uc_add(master, dev->dev_addr);
80                 if (err < 0)
81                         goto out;
82         }
83
84         if (dev->flags & IFF_ALLMULTI) {
85                 err = dev_set_allmulti(master, 1);
86                 if (err < 0)
87                         goto del_unicast;
88         }
89         if (dev->flags & IFF_PROMISC) {
90                 err = dev_set_promiscuity(master, 1);
91                 if (err < 0)
92                         goto clear_allmulti;
93         }
94
95         err = dsa_port_enable_rt(dp, dev->phydev);
96         if (err)
97                 goto clear_promisc;
98
99         return 0;
100
101 clear_promisc:
102         if (dev->flags & IFF_PROMISC)
103                 dev_set_promiscuity(master, -1);
104 clear_allmulti:
105         if (dev->flags & IFF_ALLMULTI)
106                 dev_set_allmulti(master, -1);
107 del_unicast:
108         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
109                 dev_uc_del(master, dev->dev_addr);
110 out:
111         return err;
112 }
113
114 static int dsa_slave_close(struct net_device *dev)
115 {
116         struct net_device *master = dsa_slave_to_master(dev);
117         struct dsa_port *dp = dsa_slave_to_port(dev);
118
119         dsa_port_disable_rt(dp);
120
121         dev_mc_unsync(master, dev);
122         dev_uc_unsync(master, dev);
123         if (dev->flags & IFF_ALLMULTI)
124                 dev_set_allmulti(master, -1);
125         if (dev->flags & IFF_PROMISC)
126                 dev_set_promiscuity(master, -1);
127
128         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
129                 dev_uc_del(master, dev->dev_addr);
130
131         return 0;
132 }
133
134 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
135 {
136         struct net_device *master = dsa_slave_to_master(dev);
137         if (dev->flags & IFF_UP) {
138                 if (change & IFF_ALLMULTI)
139                         dev_set_allmulti(master,
140                                          dev->flags & IFF_ALLMULTI ? 1 : -1);
141                 if (change & IFF_PROMISC)
142                         dev_set_promiscuity(master,
143                                             dev->flags & IFF_PROMISC ? 1 : -1);
144         }
145 }
146
147 static void dsa_slave_set_rx_mode(struct net_device *dev)
148 {
149         struct net_device *master = dsa_slave_to_master(dev);
150
151         dev_mc_sync(master, dev);
152         dev_uc_sync(master, dev);
153 }
154
155 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
156 {
157         struct net_device *master = dsa_slave_to_master(dev);
158         struct sockaddr *addr = a;
159         int err;
160
161         if (!is_valid_ether_addr(addr->sa_data))
162                 return -EADDRNOTAVAIL;
163
164         if (!(dev->flags & IFF_UP))
165                 goto out;
166
167         if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
168                 err = dev_uc_add(master, addr->sa_data);
169                 if (err < 0)
170                         return err;
171         }
172
173         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
174                 dev_uc_del(master, dev->dev_addr);
175
176 out:
177         ether_addr_copy(dev->dev_addr, addr->sa_data);
178
179         return 0;
180 }
181
182 struct dsa_slave_dump_ctx {
183         struct net_device *dev;
184         struct sk_buff *skb;
185         struct netlink_callback *cb;
186         int idx;
187 };
188
189 static int
190 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
191                            bool is_static, void *data)
192 {
193         struct dsa_slave_dump_ctx *dump = data;
194         u32 portid = NETLINK_CB(dump->cb->skb).portid;
195         u32 seq = dump->cb->nlh->nlmsg_seq;
196         struct nlmsghdr *nlh;
197         struct ndmsg *ndm;
198
199         if (dump->idx < dump->cb->args[2])
200                 goto skip;
201
202         nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
203                         sizeof(*ndm), NLM_F_MULTI);
204         if (!nlh)
205                 return -EMSGSIZE;
206
207         ndm = nlmsg_data(nlh);
208         ndm->ndm_family  = AF_BRIDGE;
209         ndm->ndm_pad1    = 0;
210         ndm->ndm_pad2    = 0;
211         ndm->ndm_flags   = NTF_SELF;
212         ndm->ndm_type    = 0;
213         ndm->ndm_ifindex = dump->dev->ifindex;
214         ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
215
216         if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
217                 goto nla_put_failure;
218
219         if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
220                 goto nla_put_failure;
221
222         nlmsg_end(dump->skb, nlh);
223
224 skip:
225         dump->idx++;
226         return 0;
227
228 nla_put_failure:
229         nlmsg_cancel(dump->skb, nlh);
230         return -EMSGSIZE;
231 }
232
233 static int
234 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
235                    struct net_device *dev, struct net_device *filter_dev,
236                    int *idx)
237 {
238         struct dsa_port *dp = dsa_slave_to_port(dev);
239         struct dsa_slave_dump_ctx dump = {
240                 .dev = dev,
241                 .skb = skb,
242                 .cb = cb,
243                 .idx = *idx,
244         };
245         int err;
246
247         err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
248         *idx = dump.idx;
249
250         return err;
251 }
252
253 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
254 {
255         struct dsa_slave_priv *p = netdev_priv(dev);
256         struct dsa_switch *ds = p->dp->ds;
257         int port = p->dp->index;
258
259         /* Pass through to switch driver if it supports timestamping */
260         switch (cmd) {
261         case SIOCGHWTSTAMP:
262                 if (ds->ops->port_hwtstamp_get)
263                         return ds->ops->port_hwtstamp_get(ds, port, ifr);
264                 break;
265         case SIOCSHWTSTAMP:
266                 if (ds->ops->port_hwtstamp_set)
267                         return ds->ops->port_hwtstamp_set(ds, port, ifr);
268                 break;
269         }
270
271         return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
272 }
273
274 static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
275                                    const struct switchdev_attr *attr,
276                                    struct netlink_ext_ack *extack)
277 {
278         struct dsa_port *dp = dsa_slave_to_port(dev);
279         int ret;
280
281         if (ctx && ctx != dp)
282                 return 0;
283
284         switch (attr->id) {
285         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
286                 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
287                         return -EOPNOTSUPP;
288
289                 ret = dsa_port_set_state(dp, attr->u.stp_state, true);
290                 break;
291         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
292                 if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
293                         return -EOPNOTSUPP;
294
295                 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
296                                               extack);
297                 break;
298         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
299                 if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
300                         return -EOPNOTSUPP;
301
302                 ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
303                 break;
304         case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
305                 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
306                         return -EOPNOTSUPP;
307
308                 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
309                                                 extack);
310                 break;
311         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
312                 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
313                         return -EOPNOTSUPP;
314
315                 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
316                 break;
317         default:
318                 ret = -EOPNOTSUPP;
319                 break;
320         }
321
322         return ret;
323 }
324
325 /* Must be called under rcu_read_lock() */
326 static int
327 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
328                                       const struct switchdev_obj_port_vlan *vlan)
329 {
330         struct net_device *upper_dev;
331         struct list_head *iter;
332
333         netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
334                 u16 vid;
335
336                 if (!is_vlan_dev(upper_dev))
337                         continue;
338
339                 vid = vlan_dev_vlan_id(upper_dev);
340                 if (vid == vlan->vid)
341                         return -EBUSY;
342         }
343
344         return 0;
345 }
346
347 static int dsa_slave_vlan_add(struct net_device *dev,
348                               const struct switchdev_obj *obj,
349                               struct netlink_ext_ack *extack)
350 {
351         struct net_device *master = dsa_slave_to_master(dev);
352         struct dsa_port *dp = dsa_slave_to_port(dev);
353         struct switchdev_obj_port_vlan vlan;
354         int err;
355
356         if (dsa_port_skip_vlan_configuration(dp)) {
357                 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
358                 return 0;
359         }
360
361         vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
362
363         /* Deny adding a bridge VLAN when there is already an 802.1Q upper with
364          * the same VID.
365          */
366         if (br_vlan_enabled(dp->bridge_dev)) {
367                 rcu_read_lock();
368                 err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
369                 rcu_read_unlock();
370                 if (err) {
371                         NL_SET_ERR_MSG_MOD(extack,
372                                            "Port already has a VLAN upper with this VID");
373                         return err;
374                 }
375         }
376
377         err = dsa_port_vlan_add(dp, &vlan, extack);
378         if (err)
379                 return err;
380
381         /* We need the dedicated CPU port to be a member of the VLAN as well.
382          * Even though drivers often handle CPU membership in special ways,
383          * it doesn't make sense to program a PVID, so clear this flag.
384          */
385         vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
386
387         err = dsa_port_vlan_add(dp->cpu_dp, &vlan, extack);
388         if (err)
389                 return err;
390
391         return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid);
392 }
393
394 static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
395                                   const struct switchdev_obj *obj,
396                                   struct netlink_ext_ack *extack)
397 {
398         struct dsa_port *dp = dsa_slave_to_port(dev);
399         int err;
400
401         if (ctx && ctx != dp)
402                 return 0;
403
404         switch (obj->id) {
405         case SWITCHDEV_OBJ_ID_PORT_MDB:
406                 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
407                         return -EOPNOTSUPP;
408
409                 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
410                 break;
411         case SWITCHDEV_OBJ_ID_HOST_MDB:
412                 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
413                         return -EOPNOTSUPP;
414
415                 err = dsa_port_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
416                 break;
417         case SWITCHDEV_OBJ_ID_PORT_VLAN:
418                 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
419                         return -EOPNOTSUPP;
420
421                 err = dsa_slave_vlan_add(dev, obj, extack);
422                 break;
423         case SWITCHDEV_OBJ_ID_MRP:
424                 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
425                         return -EOPNOTSUPP;
426
427                 err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
428                 break;
429         case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
430                 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
431                         return -EOPNOTSUPP;
432
433                 err = dsa_port_mrp_add_ring_role(dp,
434                                                  SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
435                 break;
436         default:
437                 err = -EOPNOTSUPP;
438                 break;
439         }
440
441         return err;
442 }
443
444 static int dsa_slave_vlan_del(struct net_device *dev,
445                               const struct switchdev_obj *obj)
446 {
447         struct net_device *master = dsa_slave_to_master(dev);
448         struct dsa_port *dp = dsa_slave_to_port(dev);
449         struct switchdev_obj_port_vlan *vlan;
450         int err;
451
452         if (dsa_port_skip_vlan_configuration(dp))
453                 return 0;
454
455         vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
456
457         /* Do not deprogram the CPU port as it may be shared with other user
458          * ports which can be members of this VLAN as well.
459          */
460         err = dsa_port_vlan_del(dp, vlan);
461         if (err)
462                 return err;
463
464         vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid);
465
466         return 0;
467 }
468
469 static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
470                                   const struct switchdev_obj *obj)
471 {
472         struct dsa_port *dp = dsa_slave_to_port(dev);
473         int err;
474
475         if (ctx && ctx != dp)
476                 return 0;
477
478         switch (obj->id) {
479         case SWITCHDEV_OBJ_ID_PORT_MDB:
480                 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
481                         return -EOPNOTSUPP;
482
483                 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
484                 break;
485         case SWITCHDEV_OBJ_ID_HOST_MDB:
486                 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
487                         return -EOPNOTSUPP;
488
489                 err = dsa_port_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
490                 break;
491         case SWITCHDEV_OBJ_ID_PORT_VLAN:
492                 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
493                         return -EOPNOTSUPP;
494
495                 err = dsa_slave_vlan_del(dev, obj);
496                 break;
497         case SWITCHDEV_OBJ_ID_MRP:
498                 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
499                         return -EOPNOTSUPP;
500
501                 err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
502                 break;
503         case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
504                 if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
505                         return -EOPNOTSUPP;
506
507                 err = dsa_port_mrp_del_ring_role(dp,
508                                                  SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
509                 break;
510         default:
511                 err = -EOPNOTSUPP;
512                 break;
513         }
514
515         return err;
516 }
517
518 static int dsa_slave_get_port_parent_id(struct net_device *dev,
519                                         struct netdev_phys_item_id *ppid)
520 {
521         struct dsa_port *dp = dsa_slave_to_port(dev);
522         struct dsa_switch *ds = dp->ds;
523         struct dsa_switch_tree *dst = ds->dst;
524
525         /* For non-legacy ports, devlink is used and it takes
526          * care of the name generation. This ndo implementation
527          * should be removed with legacy support.
528          */
529         if (dp->ds->devlink)
530                 return -EOPNOTSUPP;
531
532         ppid->id_len = sizeof(dst->index);
533         memcpy(&ppid->id, &dst->index, ppid->id_len);
534
535         return 0;
536 }
537
538 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
539                                                      struct sk_buff *skb)
540 {
541 #ifdef CONFIG_NET_POLL_CONTROLLER
542         struct dsa_slave_priv *p = netdev_priv(dev);
543
544         return netpoll_send_skb(p->netpoll, skb);
545 #else
546         BUG();
547         return NETDEV_TX_OK;
548 #endif
549 }
550
551 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
552                                  struct sk_buff *skb)
553 {
554         struct dsa_switch *ds = p->dp->ds;
555
556         if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
557                 return;
558
559         if (!ds->ops->port_txtstamp)
560                 return;
561
562         ds->ops->port_txtstamp(ds, p->dp->index, skb);
563 }
564
565 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
566 {
567         /* SKB for netpoll still need to be mangled with the protocol-specific
568          * tag to be successfully transmitted
569          */
570         if (unlikely(netpoll_tx_running(dev)))
571                 return dsa_slave_netpoll_send_skb(dev, skb);
572
573         /* Queue the SKB for transmission on the parent interface, but
574          * do not modify its EtherType
575          */
576         skb->dev = dsa_slave_to_master(dev);
577         dev_queue_xmit(skb);
578
579         return NETDEV_TX_OK;
580 }
581 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
582
583 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
584 {
585         int needed_headroom = dev->needed_headroom;
586         int needed_tailroom = dev->needed_tailroom;
587
588         /* For tail taggers, we need to pad short frames ourselves, to ensure
589          * that the tail tag does not fail at its role of being at the end of
590          * the packet, once the master interface pads the frame. Account for
591          * that pad length here, and pad later.
592          */
593         if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
594                 needed_tailroom += ETH_ZLEN - skb->len;
595         /* skb_headroom() returns unsigned int... */
596         needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
597         needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
598
599         if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
600                 /* No reallocation needed, yay! */
601                 return 0;
602
603         return pskb_expand_head(skb, needed_headroom, needed_tailroom,
604                                 GFP_ATOMIC);
605 }
606
607 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
608 {
609         struct dsa_slave_priv *p = netdev_priv(dev);
610         struct sk_buff *nskb;
611
612         dev_sw_netstats_tx_add(dev, 1, skb->len);
613
614         memset(skb->cb, 0, sizeof(skb->cb));
615
616         /* Handle tx timestamp if any */
617         dsa_skb_tx_timestamp(p, skb);
618
619         if (dsa_realloc_skb(skb, dev)) {
620                 dev_kfree_skb_any(skb);
621                 return NETDEV_TX_OK;
622         }
623
624         /* needed_tailroom should still be 'warm' in the cache line from
625          * dsa_realloc_skb(), which has also ensured that padding is safe.
626          */
627         if (dev->needed_tailroom)
628                 eth_skb_pad(skb);
629
630         /* Transmit function may have to reallocate the original SKB,
631          * in which case it must have freed it. Only free it here on error.
632          */
633         nskb = p->xmit(skb, dev);
634         if (!nskb) {
635                 kfree_skb(skb);
636                 return NETDEV_TX_OK;
637         }
638
639         return dsa_enqueue_skb(nskb, dev);
640 }
641
642 /* ethtool operations *******************************************************/
643
644 static void dsa_slave_get_drvinfo(struct net_device *dev,
645                                   struct ethtool_drvinfo *drvinfo)
646 {
647         strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
648         strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
649         strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
650 }
651
652 static int dsa_slave_get_regs_len(struct net_device *dev)
653 {
654         struct dsa_port *dp = dsa_slave_to_port(dev);
655         struct dsa_switch *ds = dp->ds;
656
657         if (ds->ops->get_regs_len)
658                 return ds->ops->get_regs_len(ds, dp->index);
659
660         return -EOPNOTSUPP;
661 }
662
663 static void
664 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
665 {
666         struct dsa_port *dp = dsa_slave_to_port(dev);
667         struct dsa_switch *ds = dp->ds;
668
669         if (ds->ops->get_regs)
670                 ds->ops->get_regs(ds, dp->index, regs, _p);
671 }
672
673 static int dsa_slave_nway_reset(struct net_device *dev)
674 {
675         struct dsa_port *dp = dsa_slave_to_port(dev);
676
677         return phylink_ethtool_nway_reset(dp->pl);
678 }
679
680 static int dsa_slave_get_eeprom_len(struct net_device *dev)
681 {
682         struct dsa_port *dp = dsa_slave_to_port(dev);
683         struct dsa_switch *ds = dp->ds;
684
685         if (ds->cd && ds->cd->eeprom_len)
686                 return ds->cd->eeprom_len;
687
688         if (ds->ops->get_eeprom_len)
689                 return ds->ops->get_eeprom_len(ds);
690
691         return 0;
692 }
693
694 static int dsa_slave_get_eeprom(struct net_device *dev,
695                                 struct ethtool_eeprom *eeprom, u8 *data)
696 {
697         struct dsa_port *dp = dsa_slave_to_port(dev);
698         struct dsa_switch *ds = dp->ds;
699
700         if (ds->ops->get_eeprom)
701                 return ds->ops->get_eeprom(ds, eeprom, data);
702
703         return -EOPNOTSUPP;
704 }
705
706 static int dsa_slave_set_eeprom(struct net_device *dev,
707                                 struct ethtool_eeprom *eeprom, u8 *data)
708 {
709         struct dsa_port *dp = dsa_slave_to_port(dev);
710         struct dsa_switch *ds = dp->ds;
711
712         if (ds->ops->set_eeprom)
713                 return ds->ops->set_eeprom(ds, eeprom, data);
714
715         return -EOPNOTSUPP;
716 }
717
718 static void dsa_slave_get_strings(struct net_device *dev,
719                                   uint32_t stringset, uint8_t *data)
720 {
721         struct dsa_port *dp = dsa_slave_to_port(dev);
722         struct dsa_switch *ds = dp->ds;
723
724         if (stringset == ETH_SS_STATS) {
725                 int len = ETH_GSTRING_LEN;
726
727                 strncpy(data, "tx_packets", len);
728                 strncpy(data + len, "tx_bytes", len);
729                 strncpy(data + 2 * len, "rx_packets", len);
730                 strncpy(data + 3 * len, "rx_bytes", len);
731                 if (ds->ops->get_strings)
732                         ds->ops->get_strings(ds, dp->index, stringset,
733                                              data + 4 * len);
734         } else if (stringset ==  ETH_SS_TEST) {
735                 net_selftest_get_strings(data);
736         }
737
738 }
739
740 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
741                                         struct ethtool_stats *stats,
742                                         uint64_t *data)
743 {
744         struct dsa_port *dp = dsa_slave_to_port(dev);
745         struct dsa_switch *ds = dp->ds;
746         struct pcpu_sw_netstats *s;
747         unsigned int start;
748         int i;
749
750         for_each_possible_cpu(i) {
751                 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
752
753                 s = per_cpu_ptr(dev->tstats, i);
754                 do {
755                         start = u64_stats_fetch_begin_irq(&s->syncp);
756                         tx_packets = s->tx_packets;
757                         tx_bytes = s->tx_bytes;
758                         rx_packets = s->rx_packets;
759                         rx_bytes = s->rx_bytes;
760                 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
761                 data[0] += tx_packets;
762                 data[1] += tx_bytes;
763                 data[2] += rx_packets;
764                 data[3] += rx_bytes;
765         }
766         if (ds->ops->get_ethtool_stats)
767                 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
768 }
769
770 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
771 {
772         struct dsa_port *dp = dsa_slave_to_port(dev);
773         struct dsa_switch *ds = dp->ds;
774
775         if (sset == ETH_SS_STATS) {
776                 int count = 0;
777
778                 if (ds->ops->get_sset_count) {
779                         count = ds->ops->get_sset_count(ds, dp->index, sset);
780                         if (count < 0)
781                                 return count;
782                 }
783
784                 return count + 4;
785         } else if (sset ==  ETH_SS_TEST) {
786                 return net_selftest_get_count();
787         }
788
789         return -EOPNOTSUPP;
790 }
791
792 static void dsa_slave_net_selftest(struct net_device *ndev,
793                                    struct ethtool_test *etest, u64 *buf)
794 {
795         struct dsa_port *dp = dsa_slave_to_port(ndev);
796         struct dsa_switch *ds = dp->ds;
797
798         if (ds->ops->self_test) {
799                 ds->ops->self_test(ds, dp->index, etest, buf);
800                 return;
801         }
802
803         net_selftest(ndev, etest, buf);
804 }
805
806 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
807 {
808         struct dsa_port *dp = dsa_slave_to_port(dev);
809         struct dsa_switch *ds = dp->ds;
810
811         phylink_ethtool_get_wol(dp->pl, w);
812
813         if (ds->ops->get_wol)
814                 ds->ops->get_wol(ds, dp->index, w);
815 }
816
817 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
818 {
819         struct dsa_port *dp = dsa_slave_to_port(dev);
820         struct dsa_switch *ds = dp->ds;
821         int ret = -EOPNOTSUPP;
822
823         phylink_ethtool_set_wol(dp->pl, w);
824
825         if (ds->ops->set_wol)
826                 ret = ds->ops->set_wol(ds, dp->index, w);
827
828         return ret;
829 }
830
831 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
832 {
833         struct dsa_port *dp = dsa_slave_to_port(dev);
834         struct dsa_switch *ds = dp->ds;
835         int ret;
836
837         /* Port's PHY and MAC both need to be EEE capable */
838         if (!dev->phydev || !dp->pl)
839                 return -ENODEV;
840
841         if (!ds->ops->set_mac_eee)
842                 return -EOPNOTSUPP;
843
844         ret = ds->ops->set_mac_eee(ds, dp->index, e);
845         if (ret)
846                 return ret;
847
848         return phylink_ethtool_set_eee(dp->pl, e);
849 }
850
851 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
852 {
853         struct dsa_port *dp = dsa_slave_to_port(dev);
854         struct dsa_switch *ds = dp->ds;
855         int ret;
856
857         /* Port's PHY and MAC both need to be EEE capable */
858         if (!dev->phydev || !dp->pl)
859                 return -ENODEV;
860
861         if (!ds->ops->get_mac_eee)
862                 return -EOPNOTSUPP;
863
864         ret = ds->ops->get_mac_eee(ds, dp->index, e);
865         if (ret)
866                 return ret;
867
868         return phylink_ethtool_get_eee(dp->pl, e);
869 }
870
871 static int dsa_slave_get_link_ksettings(struct net_device *dev,
872                                         struct ethtool_link_ksettings *cmd)
873 {
874         struct dsa_port *dp = dsa_slave_to_port(dev);
875
876         return phylink_ethtool_ksettings_get(dp->pl, cmd);
877 }
878
879 static int dsa_slave_set_link_ksettings(struct net_device *dev,
880                                         const struct ethtool_link_ksettings *cmd)
881 {
882         struct dsa_port *dp = dsa_slave_to_port(dev);
883
884         return phylink_ethtool_ksettings_set(dp->pl, cmd);
885 }
886
887 static void dsa_slave_get_pauseparam(struct net_device *dev,
888                                      struct ethtool_pauseparam *pause)
889 {
890         struct dsa_port *dp = dsa_slave_to_port(dev);
891
892         phylink_ethtool_get_pauseparam(dp->pl, pause);
893 }
894
895 static int dsa_slave_set_pauseparam(struct net_device *dev,
896                                     struct ethtool_pauseparam *pause)
897 {
898         struct dsa_port *dp = dsa_slave_to_port(dev);
899
900         return phylink_ethtool_set_pauseparam(dp->pl, pause);
901 }
902
903 #ifdef CONFIG_NET_POLL_CONTROLLER
904 static int dsa_slave_netpoll_setup(struct net_device *dev,
905                                    struct netpoll_info *ni)
906 {
907         struct net_device *master = dsa_slave_to_master(dev);
908         struct dsa_slave_priv *p = netdev_priv(dev);
909         struct netpoll *netpoll;
910         int err = 0;
911
912         netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
913         if (!netpoll)
914                 return -ENOMEM;
915
916         err = __netpoll_setup(netpoll, master);
917         if (err) {
918                 kfree(netpoll);
919                 goto out;
920         }
921
922         p->netpoll = netpoll;
923 out:
924         return err;
925 }
926
927 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
928 {
929         struct dsa_slave_priv *p = netdev_priv(dev);
930         struct netpoll *netpoll = p->netpoll;
931
932         if (!netpoll)
933                 return;
934
935         p->netpoll = NULL;
936
937         __netpoll_free(netpoll);
938 }
939
940 static void dsa_slave_poll_controller(struct net_device *dev)
941 {
942 }
943 #endif
944
945 static int dsa_slave_get_phys_port_name(struct net_device *dev,
946                                         char *name, size_t len)
947 {
948         struct dsa_port *dp = dsa_slave_to_port(dev);
949
950         /* For non-legacy ports, devlink is used and it takes
951          * care of the name generation. This ndo implementation
952          * should be removed with legacy support.
953          */
954         if (dp->ds->devlink)
955                 return -EOPNOTSUPP;
956
957         if (snprintf(name, len, "p%d", dp->index) >= len)
958                 return -EINVAL;
959
960         return 0;
961 }
962
963 static struct dsa_mall_tc_entry *
964 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
965 {
966         struct dsa_slave_priv *p = netdev_priv(dev);
967         struct dsa_mall_tc_entry *mall_tc_entry;
968
969         list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
970                 if (mall_tc_entry->cookie == cookie)
971                         return mall_tc_entry;
972
973         return NULL;
974 }
975
976 static int
977 dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
978                                   struct tc_cls_matchall_offload *cls,
979                                   bool ingress)
980 {
981         struct dsa_port *dp = dsa_slave_to_port(dev);
982         struct dsa_slave_priv *p = netdev_priv(dev);
983         struct dsa_mall_mirror_tc_entry *mirror;
984         struct dsa_mall_tc_entry *mall_tc_entry;
985         struct dsa_switch *ds = dp->ds;
986         struct flow_action_entry *act;
987         struct dsa_port *to_dp;
988         int err;
989
990         if (!ds->ops->port_mirror_add)
991                 return -EOPNOTSUPP;
992
993         if (!flow_action_basic_hw_stats_check(&cls->rule->action,
994                                               cls->common.extack))
995                 return -EOPNOTSUPP;
996
997         act = &cls->rule->action.entries[0];
998
999         if (!act->dev)
1000                 return -EINVAL;
1001
1002         if (!dsa_slave_dev_check(act->dev))
1003                 return -EOPNOTSUPP;
1004
1005         mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1006         if (!mall_tc_entry)
1007                 return -ENOMEM;
1008
1009         mall_tc_entry->cookie = cls->cookie;
1010         mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1011         mirror = &mall_tc_entry->mirror;
1012
1013         to_dp = dsa_slave_to_port(act->dev);
1014
1015         mirror->to_local_port = to_dp->index;
1016         mirror->ingress = ingress;
1017
1018         err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1019         if (err) {
1020                 kfree(mall_tc_entry);
1021                 return err;
1022         }
1023
1024         list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1025
1026         return err;
1027 }
1028
1029 static int
1030 dsa_slave_add_cls_matchall_police(struct net_device *dev,
1031                                   struct tc_cls_matchall_offload *cls,
1032                                   bool ingress)
1033 {
1034         struct netlink_ext_ack *extack = cls->common.extack;
1035         struct dsa_port *dp = dsa_slave_to_port(dev);
1036         struct dsa_slave_priv *p = netdev_priv(dev);
1037         struct dsa_mall_policer_tc_entry *policer;
1038         struct dsa_mall_tc_entry *mall_tc_entry;
1039         struct dsa_switch *ds = dp->ds;
1040         struct flow_action_entry *act;
1041         int err;
1042
1043         if (!ds->ops->port_policer_add) {
1044                 NL_SET_ERR_MSG_MOD(extack,
1045                                    "Policing offload not implemented");
1046                 return -EOPNOTSUPP;
1047         }
1048
1049         if (!ingress) {
1050                 NL_SET_ERR_MSG_MOD(extack,
1051                                    "Only supported on ingress qdisc");
1052                 return -EOPNOTSUPP;
1053         }
1054
1055         if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1056                                               cls->common.extack))
1057                 return -EOPNOTSUPP;
1058
1059         list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1060                 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1061                         NL_SET_ERR_MSG_MOD(extack,
1062                                            "Only one port policer allowed");
1063                         return -EEXIST;
1064                 }
1065         }
1066
1067         act = &cls->rule->action.entries[0];
1068
1069         mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1070         if (!mall_tc_entry)
1071                 return -ENOMEM;
1072
1073         mall_tc_entry->cookie = cls->cookie;
1074         mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1075         policer = &mall_tc_entry->policer;
1076         policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1077         policer->burst = act->police.burst;
1078
1079         err = ds->ops->port_policer_add(ds, dp->index, policer);
1080         if (err) {
1081                 kfree(mall_tc_entry);
1082                 return err;
1083         }
1084
1085         list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1086
1087         return err;
1088 }
1089
1090 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1091                                       struct tc_cls_matchall_offload *cls,
1092                                       bool ingress)
1093 {
1094         int err = -EOPNOTSUPP;
1095
1096         if (cls->common.protocol == htons(ETH_P_ALL) &&
1097             flow_offload_has_one_action(&cls->rule->action) &&
1098             cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1099                 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
1100         else if (flow_offload_has_one_action(&cls->rule->action) &&
1101                  cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1102                 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1103
1104         return err;
1105 }
1106
1107 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1108                                        struct tc_cls_matchall_offload *cls)
1109 {
1110         struct dsa_port *dp = dsa_slave_to_port(dev);
1111         struct dsa_mall_tc_entry *mall_tc_entry;
1112         struct dsa_switch *ds = dp->ds;
1113
1114         mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1115         if (!mall_tc_entry)
1116                 return;
1117
1118         list_del(&mall_tc_entry->list);
1119
1120         switch (mall_tc_entry->type) {
1121         case DSA_PORT_MALL_MIRROR:
1122                 if (ds->ops->port_mirror_del)
1123                         ds->ops->port_mirror_del(ds, dp->index,
1124                                                  &mall_tc_entry->mirror);
1125                 break;
1126         case DSA_PORT_MALL_POLICER:
1127                 if (ds->ops->port_policer_del)
1128                         ds->ops->port_policer_del(ds, dp->index);
1129                 break;
1130         default:
1131                 WARN_ON(1);
1132         }
1133
1134         kfree(mall_tc_entry);
1135 }
1136
1137 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
1138                                            struct tc_cls_matchall_offload *cls,
1139                                            bool ingress)
1140 {
1141         if (cls->common.chain_index)
1142                 return -EOPNOTSUPP;
1143
1144         switch (cls->command) {
1145         case TC_CLSMATCHALL_REPLACE:
1146                 return dsa_slave_add_cls_matchall(dev, cls, ingress);
1147         case TC_CLSMATCHALL_DESTROY:
1148                 dsa_slave_del_cls_matchall(dev, cls);
1149                 return 0;
1150         default:
1151                 return -EOPNOTSUPP;
1152         }
1153 }
1154
1155 static int dsa_slave_add_cls_flower(struct net_device *dev,
1156                                     struct flow_cls_offload *cls,
1157                                     bool ingress)
1158 {
1159         struct dsa_port *dp = dsa_slave_to_port(dev);
1160         struct dsa_switch *ds = dp->ds;
1161         int port = dp->index;
1162
1163         if (!ds->ops->cls_flower_add)
1164                 return -EOPNOTSUPP;
1165
1166         return ds->ops->cls_flower_add(ds, port, cls, ingress);
1167 }
1168
1169 static int dsa_slave_del_cls_flower(struct net_device *dev,
1170                                     struct flow_cls_offload *cls,
1171                                     bool ingress)
1172 {
1173         struct dsa_port *dp = dsa_slave_to_port(dev);
1174         struct dsa_switch *ds = dp->ds;
1175         int port = dp->index;
1176
1177         if (!ds->ops->cls_flower_del)
1178                 return -EOPNOTSUPP;
1179
1180         return ds->ops->cls_flower_del(ds, port, cls, ingress);
1181 }
1182
1183 static int dsa_slave_stats_cls_flower(struct net_device *dev,
1184                                       struct flow_cls_offload *cls,
1185                                       bool ingress)
1186 {
1187         struct dsa_port *dp = dsa_slave_to_port(dev);
1188         struct dsa_switch *ds = dp->ds;
1189         int port = dp->index;
1190
1191         if (!ds->ops->cls_flower_stats)
1192                 return -EOPNOTSUPP;
1193
1194         return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1195 }
1196
1197 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1198                                          struct flow_cls_offload *cls,
1199                                          bool ingress)
1200 {
1201         switch (cls->command) {
1202         case FLOW_CLS_REPLACE:
1203                 return dsa_slave_add_cls_flower(dev, cls, ingress);
1204         case FLOW_CLS_DESTROY:
1205                 return dsa_slave_del_cls_flower(dev, cls, ingress);
1206         case FLOW_CLS_STATS:
1207                 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1208         default:
1209                 return -EOPNOTSUPP;
1210         }
1211 }
1212
1213 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1214                                        void *cb_priv, bool ingress)
1215 {
1216         struct net_device *dev = cb_priv;
1217
1218         if (!tc_can_offload(dev))
1219                 return -EOPNOTSUPP;
1220
1221         switch (type) {
1222         case TC_SETUP_CLSMATCHALL:
1223                 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1224         case TC_SETUP_CLSFLOWER:
1225                 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
1226         default:
1227                 return -EOPNOTSUPP;
1228         }
1229 }
1230
1231 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1232                                           void *type_data, void *cb_priv)
1233 {
1234         return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1235 }
1236
1237 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1238                                           void *type_data, void *cb_priv)
1239 {
1240         return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1241 }
1242
1243 static LIST_HEAD(dsa_slave_block_cb_list);
1244
1245 static int dsa_slave_setup_tc_block(struct net_device *dev,
1246                                     struct flow_block_offload *f)
1247 {
1248         struct flow_block_cb *block_cb;
1249         flow_setup_cb_t *cb;
1250
1251         if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1252                 cb = dsa_slave_setup_tc_block_cb_ig;
1253         else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1254                 cb = dsa_slave_setup_tc_block_cb_eg;
1255         else
1256                 return -EOPNOTSUPP;
1257
1258         f->driver_block_list = &dsa_slave_block_cb_list;
1259
1260         switch (f->command) {
1261         case FLOW_BLOCK_BIND:
1262                 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1263                         return -EBUSY;
1264
1265                 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1266                 if (IS_ERR(block_cb))
1267                         return PTR_ERR(block_cb);
1268
1269                 flow_block_cb_add(block_cb, f);
1270                 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1271                 return 0;
1272         case FLOW_BLOCK_UNBIND:
1273                 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1274                 if (!block_cb)
1275                         return -ENOENT;
1276
1277                 flow_block_cb_remove(block_cb, f);
1278                 list_del(&block_cb->driver_list);
1279                 return 0;
1280         default:
1281                 return -EOPNOTSUPP;
1282         }
1283 }
1284
1285 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
1286                                     void *type_data)
1287 {
1288         struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1289         struct net_device *master = cpu_dp->master;
1290
1291         if (!master->netdev_ops->ndo_setup_tc)
1292                 return -EOPNOTSUPP;
1293
1294         return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
1295 }
1296
1297 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1298                               void *type_data)
1299 {
1300         struct dsa_port *dp = dsa_slave_to_port(dev);
1301         struct dsa_switch *ds = dp->ds;
1302
1303         switch (type) {
1304         case TC_SETUP_BLOCK:
1305                 return dsa_slave_setup_tc_block(dev, type_data);
1306         case TC_SETUP_FT:
1307                 return dsa_slave_setup_ft_block(ds, dp->index, type_data);
1308         default:
1309                 break;
1310         }
1311
1312         if (!ds->ops->port_setup_tc)
1313                 return -EOPNOTSUPP;
1314
1315         return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1316 }
1317
1318 static int dsa_slave_get_rxnfc(struct net_device *dev,
1319                                struct ethtool_rxnfc *nfc, u32 *rule_locs)
1320 {
1321         struct dsa_port *dp = dsa_slave_to_port(dev);
1322         struct dsa_switch *ds = dp->ds;
1323
1324         if (!ds->ops->get_rxnfc)
1325                 return -EOPNOTSUPP;
1326
1327         return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1328 }
1329
1330 static int dsa_slave_set_rxnfc(struct net_device *dev,
1331                                struct ethtool_rxnfc *nfc)
1332 {
1333         struct dsa_port *dp = dsa_slave_to_port(dev);
1334         struct dsa_switch *ds = dp->ds;
1335
1336         if (!ds->ops->set_rxnfc)
1337                 return -EOPNOTSUPP;
1338
1339         return ds->ops->set_rxnfc(ds, dp->index, nfc);
1340 }
1341
1342 static int dsa_slave_get_ts_info(struct net_device *dev,
1343                                  struct ethtool_ts_info *ts)
1344 {
1345         struct dsa_slave_priv *p = netdev_priv(dev);
1346         struct dsa_switch *ds = p->dp->ds;
1347
1348         if (!ds->ops->get_ts_info)
1349                 return -EOPNOTSUPP;
1350
1351         return ds->ops->get_ts_info(ds, p->dp->index, ts);
1352 }
1353
1354 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1355                                      u16 vid)
1356 {
1357         struct net_device *master = dsa_slave_to_master(dev);
1358         struct dsa_port *dp = dsa_slave_to_port(dev);
1359         struct switchdev_obj_port_vlan vlan = {
1360                 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1361                 .vid = vid,
1362                 /* This API only allows programming tagged, non-PVID VIDs */
1363                 .flags = 0,
1364         };
1365         struct netlink_ext_ack extack = {0};
1366         int ret;
1367
1368         /* User port... */
1369         ret = dsa_port_vlan_add(dp, &vlan, &extack);
1370         if (ret) {
1371                 if (extack._msg)
1372                         netdev_err(dev, "%s\n", extack._msg);
1373                 return ret;
1374         }
1375
1376         /* And CPU port... */
1377         ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
1378         if (ret) {
1379                 if (extack._msg)
1380                         netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1381                                    extack._msg);
1382                 return ret;
1383         }
1384
1385         return vlan_vid_add(master, proto, vid);
1386 }
1387
1388 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1389                                       u16 vid)
1390 {
1391         struct net_device *master = dsa_slave_to_master(dev);
1392         struct dsa_port *dp = dsa_slave_to_port(dev);
1393         struct switchdev_obj_port_vlan vlan = {
1394                 .vid = vid,
1395                 /* This API only allows programming tagged, non-PVID VIDs */
1396                 .flags = 0,
1397         };
1398         int err;
1399
1400         /* Do not deprogram the CPU port as it may be shared with other user
1401          * ports which can be members of this VLAN as well.
1402          */
1403         err = dsa_port_vlan_del(dp, &vlan);
1404         if (err)
1405                 return err;
1406
1407         vlan_vid_del(master, proto, vid);
1408
1409         return 0;
1410 }
1411
1412 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
1413 {
1414         __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1415
1416         return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
1417 }
1418
1419 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
1420 {
1421         __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1422
1423         return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
1424 }
1425
1426 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1427  * filtering is enabled. The baseline is that only ports that offload a
1428  * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1429  * but there are exceptions for quirky hardware.
1430  *
1431  * If ds->vlan_filtering_is_global = true, then standalone ports which share
1432  * the same switch with other ports that offload a VLAN-aware bridge are also
1433  * inevitably VLAN-aware.
1434  *
1435  * To summarize, a DSA switch port offloads:
1436  *
1437  * - If standalone (this includes software bridge, software LAG):
1438  *     - if ds->needs_standalone_vlan_filtering = true, OR if
1439  *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
1440  *       this switch chip which have vlan_filtering=1)
1441  *         - the 8021q upper VLANs
1442  *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
1443  *       global, or it is, but no port is under a VLAN-aware bridge):
1444  *         - no VLAN (any 8021q upper is a software VLAN)
1445  *
1446  * - If under a vlan_filtering=0 bridge which it offload:
1447  *     - if ds->configure_vlan_while_not_filtering = true (default):
1448  *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
1449  *     - else (deprecated):
1450  *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1451  *           enabled, so this behavior is broken and discouraged.
1452  *
1453  * - If under a vlan_filtering=1 bridge which it offload:
1454  *     - the bridge VLANs
1455  *     - the 8021q upper VLANs
1456  */
1457 int dsa_slave_manage_vlan_filtering(struct net_device *slave,
1458                                     bool vlan_filtering)
1459 {
1460         int err;
1461
1462         if (vlan_filtering) {
1463                 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1464
1465                 err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
1466                 if (err) {
1467                         vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1468                         slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1469                         return err;
1470                 }
1471         } else {
1472                 err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
1473                 if (err)
1474                         return err;
1475
1476                 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1477         }
1478
1479         return 0;
1480 }
1481
1482 struct dsa_hw_port {
1483         struct list_head list;
1484         struct net_device *dev;
1485         int old_mtu;
1486 };
1487
1488 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1489 {
1490         const struct dsa_hw_port *p;
1491         int err;
1492
1493         list_for_each_entry(p, hw_port_list, list) {
1494                 if (p->dev->mtu == mtu)
1495                         continue;
1496
1497                 err = dev_set_mtu(p->dev, mtu);
1498                 if (err)
1499                         goto rollback;
1500         }
1501
1502         return 0;
1503
1504 rollback:
1505         list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1506                 if (p->dev->mtu == p->old_mtu)
1507                         continue;
1508
1509                 if (dev_set_mtu(p->dev, p->old_mtu))
1510                         netdev_err(p->dev, "Failed to restore MTU\n");
1511         }
1512
1513         return err;
1514 }
1515
1516 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1517 {
1518         struct dsa_hw_port *p, *n;
1519
1520         list_for_each_entry_safe(p, n, hw_port_list, list)
1521                 kfree(p);
1522 }
1523
1524 /* Make the hardware datapath to/from @dev limited to a common MTU */
1525 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1526 {
1527         struct list_head hw_port_list;
1528         struct dsa_switch_tree *dst;
1529         int min_mtu = ETH_MAX_MTU;
1530         struct dsa_port *other_dp;
1531         int err;
1532
1533         if (!dp->ds->mtu_enforcement_ingress)
1534                 return;
1535
1536         if (!dp->bridge_dev)
1537                 return;
1538
1539         INIT_LIST_HEAD(&hw_port_list);
1540
1541         /* Populate the list of ports that are part of the same bridge
1542          * as the newly added/modified port
1543          */
1544         list_for_each_entry(dst, &dsa_tree_list, list) {
1545                 list_for_each_entry(other_dp, &dst->ports, list) {
1546                         struct dsa_hw_port *hw_port;
1547                         struct net_device *slave;
1548
1549                         if (other_dp->type != DSA_PORT_TYPE_USER)
1550                                 continue;
1551
1552                         if (other_dp->bridge_dev != dp->bridge_dev)
1553                                 continue;
1554
1555                         if (!other_dp->ds->mtu_enforcement_ingress)
1556                                 continue;
1557
1558                         slave = other_dp->slave;
1559
1560                         if (min_mtu > slave->mtu)
1561                                 min_mtu = slave->mtu;
1562
1563                         hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1564                         if (!hw_port)
1565                                 goto out;
1566
1567                         hw_port->dev = slave;
1568                         hw_port->old_mtu = slave->mtu;
1569
1570                         list_add(&hw_port->list, &hw_port_list);
1571                 }
1572         }
1573
1574         /* Attempt to configure the entire hardware bridge to the newly added
1575          * interface's MTU first, regardless of whether the intention of the
1576          * user was to raise or lower it.
1577          */
1578         err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1579         if (!err)
1580                 goto out;
1581
1582         /* Clearly that didn't work out so well, so just set the minimum MTU on
1583          * all hardware bridge ports now. If this fails too, then all ports will
1584          * still have their old MTU rolled back anyway.
1585          */
1586         dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1587
1588 out:
1589         dsa_hw_port_list_free(&hw_port_list);
1590 }
1591
1592 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1593 {
1594         struct net_device *master = dsa_slave_to_master(dev);
1595         struct dsa_port *dp = dsa_slave_to_port(dev);
1596         struct dsa_slave_priv *p = netdev_priv(dev);
1597         struct dsa_switch *ds = p->dp->ds;
1598         struct dsa_port *dp_iter;
1599         struct dsa_port *cpu_dp;
1600         int port = p->dp->index;
1601         int largest_mtu = 0;
1602         int new_master_mtu;
1603         int old_master_mtu;
1604         int mtu_limit;
1605         int cpu_mtu;
1606         int err;
1607
1608         if (!ds->ops->port_change_mtu)
1609                 return -EOPNOTSUPP;
1610
1611         list_for_each_entry(dp_iter, &ds->dst->ports, list) {
1612                 int slave_mtu;
1613
1614                 if (!dsa_port_is_user(dp_iter))
1615                         continue;
1616
1617                 /* During probe, this function will be called for each slave
1618                  * device, while not all of them have been allocated. That's
1619                  * ok, it doesn't change what the maximum is, so ignore it.
1620                  */
1621                 if (!dp_iter->slave)
1622                         continue;
1623
1624                 /* Pretend that we already applied the setting, which we
1625                  * actually haven't (still haven't done all integrity checks)
1626                  */
1627                 if (dp_iter == dp)
1628                         slave_mtu = new_mtu;
1629                 else
1630                         slave_mtu = dp_iter->slave->mtu;
1631
1632                 if (largest_mtu < slave_mtu)
1633                         largest_mtu = slave_mtu;
1634         }
1635
1636         cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1637
1638         mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1639         old_master_mtu = master->mtu;
1640         new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1641         if (new_master_mtu > mtu_limit)
1642                 return -ERANGE;
1643
1644         /* If the master MTU isn't over limit, there's no need to check the CPU
1645          * MTU, since that surely isn't either.
1646          */
1647         cpu_mtu = largest_mtu;
1648
1649         /* Start applying stuff */
1650         if (new_master_mtu != old_master_mtu) {
1651                 err = dev_set_mtu(master, new_master_mtu);
1652                 if (err < 0)
1653                         goto out_master_failed;
1654
1655                 /* We only need to propagate the MTU of the CPU port to
1656                  * upstream switches, so create a non-targeted notifier which
1657                  * updates all switches.
1658                  */
1659                 err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1660                 if (err)
1661                         goto out_cpu_failed;
1662         }
1663
1664         err = dsa_port_mtu_change(dp, new_mtu, true);
1665         if (err)
1666                 goto out_port_failed;
1667
1668         dev->mtu = new_mtu;
1669
1670         dsa_bridge_mtu_normalization(dp);
1671
1672         return 0;
1673
1674 out_port_failed:
1675         if (new_master_mtu != old_master_mtu)
1676                 dsa_port_mtu_change(cpu_dp, old_master_mtu -
1677                                     dsa_tag_protocol_overhead(cpu_dp->tag_ops),
1678                                     false);
1679 out_cpu_failed:
1680         if (new_master_mtu != old_master_mtu)
1681                 dev_set_mtu(master, old_master_mtu);
1682 out_master_failed:
1683         return err;
1684 }
1685
1686 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1687         .get_drvinfo            = dsa_slave_get_drvinfo,
1688         .get_regs_len           = dsa_slave_get_regs_len,
1689         .get_regs               = dsa_slave_get_regs,
1690         .nway_reset             = dsa_slave_nway_reset,
1691         .get_link               = ethtool_op_get_link,
1692         .get_eeprom_len         = dsa_slave_get_eeprom_len,
1693         .get_eeprom             = dsa_slave_get_eeprom,
1694         .set_eeprom             = dsa_slave_set_eeprom,
1695         .get_strings            = dsa_slave_get_strings,
1696         .get_ethtool_stats      = dsa_slave_get_ethtool_stats,
1697         .get_sset_count         = dsa_slave_get_sset_count,
1698         .set_wol                = dsa_slave_set_wol,
1699         .get_wol                = dsa_slave_get_wol,
1700         .set_eee                = dsa_slave_set_eee,
1701         .get_eee                = dsa_slave_get_eee,
1702         .get_link_ksettings     = dsa_slave_get_link_ksettings,
1703         .set_link_ksettings     = dsa_slave_set_link_ksettings,
1704         .get_pauseparam         = dsa_slave_get_pauseparam,
1705         .set_pauseparam         = dsa_slave_set_pauseparam,
1706         .get_rxnfc              = dsa_slave_get_rxnfc,
1707         .set_rxnfc              = dsa_slave_set_rxnfc,
1708         .get_ts_info            = dsa_slave_get_ts_info,
1709         .self_test              = dsa_slave_net_selftest,
1710 };
1711
1712 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1713 {
1714         struct dsa_port *dp = dsa_slave_to_port(dev);
1715
1716         return dp->ds->devlink ? &dp->devlink_port : NULL;
1717 }
1718
1719 static void dsa_slave_get_stats64(struct net_device *dev,
1720                                   struct rtnl_link_stats64 *s)
1721 {
1722         struct dsa_port *dp = dsa_slave_to_port(dev);
1723         struct dsa_switch *ds = dp->ds;
1724
1725         if (ds->ops->get_stats64)
1726                 ds->ops->get_stats64(ds, dp->index, s);
1727         else
1728                 dev_get_tstats64(dev, s);
1729 }
1730
1731 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
1732                                        struct net_device_path *path)
1733 {
1734         struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
1735         struct dsa_port *cpu_dp = dp->cpu_dp;
1736
1737         path->dev = ctx->dev;
1738         path->type = DEV_PATH_DSA;
1739         path->dsa.proto = cpu_dp->tag_ops->proto;
1740         path->dsa.port = dp->index;
1741         ctx->dev = cpu_dp->master;
1742
1743         return 0;
1744 }
1745
1746 static const struct net_device_ops dsa_slave_netdev_ops = {
1747         .ndo_open               = dsa_slave_open,
1748         .ndo_stop               = dsa_slave_close,
1749         .ndo_start_xmit         = dsa_slave_xmit,
1750         .ndo_change_rx_flags    = dsa_slave_change_rx_flags,
1751         .ndo_set_rx_mode        = dsa_slave_set_rx_mode,
1752         .ndo_set_mac_address    = dsa_slave_set_mac_address,
1753         .ndo_fdb_dump           = dsa_slave_fdb_dump,
1754         .ndo_eth_ioctl          = dsa_slave_ioctl,
1755         .ndo_get_iflink         = dsa_slave_get_iflink,
1756 #ifdef CONFIG_NET_POLL_CONTROLLER
1757         .ndo_netpoll_setup      = dsa_slave_netpoll_setup,
1758         .ndo_netpoll_cleanup    = dsa_slave_netpoll_cleanup,
1759         .ndo_poll_controller    = dsa_slave_poll_controller,
1760 #endif
1761         .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1762         .ndo_setup_tc           = dsa_slave_setup_tc,
1763         .ndo_get_stats64        = dsa_slave_get_stats64,
1764         .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1765         .ndo_vlan_rx_add_vid    = dsa_slave_vlan_rx_add_vid,
1766         .ndo_vlan_rx_kill_vid   = dsa_slave_vlan_rx_kill_vid,
1767         .ndo_get_devlink_port   = dsa_slave_get_devlink_port,
1768         .ndo_change_mtu         = dsa_slave_change_mtu,
1769         .ndo_fill_forward_path  = dsa_slave_fill_forward_path,
1770 };
1771
1772 static struct device_type dsa_type = {
1773         .name   = "dsa",
1774 };
1775
1776 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1777 {
1778         const struct dsa_port *dp = dsa_to_port(ds, port);
1779
1780         if (dp->pl)
1781                 phylink_mac_change(dp->pl, up);
1782 }
1783 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1784
1785 static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1786                                           struct phylink_link_state *state)
1787 {
1788         struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1789         struct dsa_switch *ds = dp->ds;
1790
1791         /* No need to check that this operation is valid, the callback would
1792          * not be called if it was not.
1793          */
1794         ds->ops->phylink_fixed_state(ds, dp->index, state);
1795 }
1796
1797 /* slave device setup *******************************************************/
1798 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
1799                                  u32 flags)
1800 {
1801         struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1802         struct dsa_switch *ds = dp->ds;
1803
1804         slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1805         if (!slave_dev->phydev) {
1806                 netdev_err(slave_dev, "no phy at %d\n", addr);
1807                 return -ENODEV;
1808         }
1809
1810         slave_dev->phydev->dev_flags |= flags;
1811
1812         return phylink_connect_phy(dp->pl, slave_dev->phydev);
1813 }
1814
1815 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1816 {
1817         struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1818         struct device_node *port_dn = dp->dn;
1819         struct dsa_switch *ds = dp->ds;
1820         phy_interface_t mode;
1821         u32 phy_flags = 0;
1822         int ret;
1823
1824         ret = of_get_phy_mode(port_dn, &mode);
1825         if (ret)
1826                 mode = PHY_INTERFACE_MODE_NA;
1827
1828         dp->pl_config.dev = &slave_dev->dev;
1829         dp->pl_config.type = PHYLINK_NETDEV;
1830
1831         /* The get_fixed_state callback takes precedence over polling the
1832          * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
1833          * this if the switch provides such a callback.
1834          */
1835         if (ds->ops->phylink_fixed_state) {
1836                 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
1837                 dp->pl_config.poll_fixed_state = true;
1838         }
1839
1840         dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1841                                 &dsa_port_phylink_mac_ops);
1842         if (IS_ERR(dp->pl)) {
1843                 netdev_err(slave_dev,
1844                            "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1845                 return PTR_ERR(dp->pl);
1846         }
1847
1848         if (ds->ops->get_phy_flags)
1849                 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1850
1851         ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1852         if (ret == -ENODEV && ds->slave_mii_bus) {
1853                 /* We could not connect to a designated PHY or SFP, so try to
1854                  * use the switch internal MDIO bus instead
1855                  */
1856                 ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
1857         }
1858         if (ret) {
1859                 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1860                            ERR_PTR(ret));
1861                 phylink_destroy(dp->pl);
1862         }
1863
1864         return ret;
1865 }
1866
1867 void dsa_slave_setup_tagger(struct net_device *slave)
1868 {
1869         struct dsa_port *dp = dsa_slave_to_port(slave);
1870         struct dsa_slave_priv *p = netdev_priv(slave);
1871         const struct dsa_port *cpu_dp = dp->cpu_dp;
1872         struct net_device *master = cpu_dp->master;
1873         const struct dsa_switch *ds = dp->ds;
1874
1875         slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
1876         slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
1877         /* Try to save one extra realloc later in the TX path (in the master)
1878          * by also inheriting the master's needed headroom and tailroom.
1879          * The 8021q driver also does this.
1880          */
1881         slave->needed_headroom += master->needed_headroom;
1882         slave->needed_tailroom += master->needed_tailroom;
1883
1884         p->xmit = cpu_dp->tag_ops->xmit;
1885
1886         slave->features = master->vlan_features | NETIF_F_HW_TC;
1887         slave->hw_features |= NETIF_F_HW_TC;
1888         slave->features |= NETIF_F_LLTX;
1889         if (slave->needed_tailroom)
1890                 slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
1891         if (ds->needs_standalone_vlan_filtering)
1892                 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1893 }
1894
1895 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1896 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1897                                             struct netdev_queue *txq,
1898                                             void *_unused)
1899 {
1900         lockdep_set_class(&txq->_xmit_lock,
1901                           &dsa_slave_netdev_xmit_lock_key);
1902 }
1903
1904 int dsa_slave_suspend(struct net_device *slave_dev)
1905 {
1906         struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1907
1908         if (!netif_running(slave_dev))
1909                 return 0;
1910
1911         netif_device_detach(slave_dev);
1912
1913         rtnl_lock();
1914         phylink_stop(dp->pl);
1915         rtnl_unlock();
1916
1917         return 0;
1918 }
1919
1920 int dsa_slave_resume(struct net_device *slave_dev)
1921 {
1922         struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1923
1924         if (!netif_running(slave_dev))
1925                 return 0;
1926
1927         netif_device_attach(slave_dev);
1928
1929         rtnl_lock();
1930         phylink_start(dp->pl);
1931         rtnl_unlock();
1932
1933         return 0;
1934 }
1935
1936 int dsa_slave_create(struct dsa_port *port)
1937 {
1938         const struct dsa_port *cpu_dp = port->cpu_dp;
1939         struct net_device *master = cpu_dp->master;
1940         struct dsa_switch *ds = port->ds;
1941         const char *name = port->name;
1942         struct net_device *slave_dev;
1943         struct dsa_slave_priv *p;
1944         int ret;
1945
1946         if (!ds->num_tx_queues)
1947                 ds->num_tx_queues = 1;
1948
1949         slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1950                                      NET_NAME_UNKNOWN, ether_setup,
1951                                      ds->num_tx_queues, 1);
1952         if (slave_dev == NULL)
1953                 return -ENOMEM;
1954
1955         slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1956         if (!is_zero_ether_addr(port->mac))
1957                 ether_addr_copy(slave_dev->dev_addr, port->mac);
1958         else
1959                 eth_hw_addr_inherit(slave_dev, master);
1960         slave_dev->priv_flags |= IFF_NO_QUEUE;
1961         slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1962         if (ds->ops->port_max_mtu)
1963                 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1964         SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1965
1966         netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1967                                  NULL);
1968
1969         SET_NETDEV_DEV(slave_dev, port->ds->dev);
1970         slave_dev->dev.of_node = port->dn;
1971         slave_dev->vlan_features = master->vlan_features;
1972
1973         p = netdev_priv(slave_dev);
1974         slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1975         if (!slave_dev->tstats) {
1976                 free_netdev(slave_dev);
1977                 return -ENOMEM;
1978         }
1979
1980         ret = gro_cells_init(&p->gcells, slave_dev);
1981         if (ret)
1982                 goto out_free;
1983
1984         p->dp = port;
1985         INIT_LIST_HEAD(&p->mall_tc_list);
1986         port->slave = slave_dev;
1987         dsa_slave_setup_tagger(slave_dev);
1988
1989         rtnl_lock();
1990         ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1991         rtnl_unlock();
1992         if (ret && ret != -EOPNOTSUPP)
1993                 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1994                          ret, ETH_DATA_LEN, port->index);
1995
1996         netif_carrier_off(slave_dev);
1997
1998         ret = dsa_slave_phy_setup(slave_dev);
1999         if (ret) {
2000                 netdev_err(slave_dev,
2001                            "error %d setting up PHY for tree %d, switch %d, port %d\n",
2002                            ret, ds->dst->index, ds->index, port->index);
2003                 goto out_gcells;
2004         }
2005
2006         rtnl_lock();
2007
2008         ret = register_netdevice(slave_dev);
2009         if (ret) {
2010                 netdev_err(master, "error %d registering interface %s\n",
2011                            ret, slave_dev->name);
2012                 rtnl_unlock();
2013                 goto out_phy;
2014         }
2015
2016         ret = netdev_upper_dev_link(master, slave_dev, NULL);
2017
2018         rtnl_unlock();
2019
2020         if (ret)
2021                 goto out_unregister;
2022
2023         return 0;
2024
2025 out_unregister:
2026         unregister_netdev(slave_dev);
2027 out_phy:
2028         rtnl_lock();
2029         phylink_disconnect_phy(p->dp->pl);
2030         rtnl_unlock();
2031         phylink_destroy(p->dp->pl);
2032 out_gcells:
2033         gro_cells_destroy(&p->gcells);
2034 out_free:
2035         free_percpu(slave_dev->tstats);
2036         free_netdev(slave_dev);
2037         port->slave = NULL;
2038         return ret;
2039 }
2040
2041 void dsa_slave_destroy(struct net_device *slave_dev)
2042 {
2043         struct net_device *master = dsa_slave_to_master(slave_dev);
2044         struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2045         struct dsa_slave_priv *p = netdev_priv(slave_dev);
2046
2047         netif_carrier_off(slave_dev);
2048         rtnl_lock();
2049         netdev_upper_dev_unlink(master, slave_dev);
2050         unregister_netdevice(slave_dev);
2051         phylink_disconnect_phy(dp->pl);
2052         rtnl_unlock();
2053
2054         phylink_destroy(dp->pl);
2055         gro_cells_destroy(&p->gcells);
2056         free_percpu(slave_dev->tstats);
2057         free_netdev(slave_dev);
2058 }
2059
2060 bool dsa_slave_dev_check(const struct net_device *dev)
2061 {
2062         return dev->netdev_ops == &dsa_slave_netdev_ops;
2063 }
2064 EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2065
2066 static int dsa_slave_changeupper(struct net_device *dev,
2067                                  struct netdev_notifier_changeupper_info *info)
2068 {
2069         struct dsa_port *dp = dsa_slave_to_port(dev);
2070         struct netlink_ext_ack *extack;
2071         int err = NOTIFY_DONE;
2072
2073         extack = netdev_notifier_info_to_extack(&info->info);
2074
2075         if (netif_is_bridge_master(info->upper_dev)) {
2076                 if (info->linking) {
2077                         err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2078                         if (!err)
2079                                 dsa_bridge_mtu_normalization(dp);
2080                         if (err == -EOPNOTSUPP) {
2081                                 NL_SET_ERR_MSG_MOD(extack,
2082                                                    "Offloading not supported");
2083                                 err = 0;
2084                         }
2085                         err = notifier_from_errno(err);
2086                 } else {
2087                         dsa_port_bridge_leave(dp, info->upper_dev);
2088                         err = NOTIFY_OK;
2089                 }
2090         } else if (netif_is_lag_master(info->upper_dev)) {
2091                 if (info->linking) {
2092                         err = dsa_port_lag_join(dp, info->upper_dev,
2093                                                 info->upper_info, extack);
2094                         if (err == -EOPNOTSUPP) {
2095                                 NL_SET_ERR_MSG_MOD(info->info.extack,
2096                                                    "Offloading not supported");
2097                                 err = 0;
2098                         }
2099                         err = notifier_from_errno(err);
2100                 } else {
2101                         dsa_port_lag_leave(dp, info->upper_dev);
2102                         err = NOTIFY_OK;
2103                 }
2104         } else if (is_hsr_master(info->upper_dev)) {
2105                 if (info->linking) {
2106                         err = dsa_port_hsr_join(dp, info->upper_dev);
2107                         if (err == -EOPNOTSUPP) {
2108                                 NL_SET_ERR_MSG_MOD(info->info.extack,
2109                                                    "Offloading not supported");
2110                                 err = 0;
2111                         }
2112                         err = notifier_from_errno(err);
2113                 } else {
2114                         dsa_port_hsr_leave(dp, info->upper_dev);
2115                         err = NOTIFY_OK;
2116                 }
2117         }
2118
2119         return err;
2120 }
2121
2122 static int dsa_slave_prechangeupper(struct net_device *dev,
2123                                     struct netdev_notifier_changeupper_info *info)
2124 {
2125         struct dsa_port *dp = dsa_slave_to_port(dev);
2126
2127         if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2128                 dsa_port_pre_bridge_leave(dp, info->upper_dev);
2129         else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2130                 dsa_port_pre_lag_leave(dp, info->upper_dev);
2131         /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2132          * meaningfully enslaved to a bridge yet
2133          */
2134
2135         return NOTIFY_DONE;
2136 }
2137
2138 static int
2139 dsa_slave_lag_changeupper(struct net_device *dev,
2140                           struct netdev_notifier_changeupper_info *info)
2141 {
2142         struct net_device *lower;
2143         struct list_head *iter;
2144         int err = NOTIFY_DONE;
2145         struct dsa_port *dp;
2146
2147         netdev_for_each_lower_dev(dev, lower, iter) {
2148                 if (!dsa_slave_dev_check(lower))
2149                         continue;
2150
2151                 dp = dsa_slave_to_port(lower);
2152                 if (!dp->lag_dev)
2153                         /* Software LAG */
2154                         continue;
2155
2156                 err = dsa_slave_changeupper(lower, info);
2157                 if (notifier_to_errno(err))
2158                         break;
2159         }
2160
2161         return err;
2162 }
2163
2164 /* Same as dsa_slave_lag_changeupper() except that it calls
2165  * dsa_slave_prechangeupper()
2166  */
2167 static int
2168 dsa_slave_lag_prechangeupper(struct net_device *dev,
2169                              struct netdev_notifier_changeupper_info *info)
2170 {
2171         struct net_device *lower;
2172         struct list_head *iter;
2173         int err = NOTIFY_DONE;
2174         struct dsa_port *dp;
2175
2176         netdev_for_each_lower_dev(dev, lower, iter) {
2177                 if (!dsa_slave_dev_check(lower))
2178                         continue;
2179
2180                 dp = dsa_slave_to_port(lower);
2181                 if (!dp->lag_dev)
2182                         /* Software LAG */
2183                         continue;
2184
2185                 err = dsa_slave_prechangeupper(lower, info);
2186                 if (notifier_to_errno(err))
2187                         break;
2188         }
2189
2190         return err;
2191 }
2192
2193 static int
2194 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2195                                  struct netdev_notifier_changeupper_info *info)
2196 {
2197         struct netlink_ext_ack *ext_ack;
2198         struct net_device *slave;
2199         struct dsa_port *dp;
2200
2201         ext_ack = netdev_notifier_info_to_extack(&info->info);
2202
2203         if (!is_vlan_dev(dev))
2204                 return NOTIFY_DONE;
2205
2206         slave = vlan_dev_real_dev(dev);
2207         if (!dsa_slave_dev_check(slave))
2208                 return NOTIFY_DONE;
2209
2210         dp = dsa_slave_to_port(slave);
2211         if (!dp->bridge_dev)
2212                 return NOTIFY_DONE;
2213
2214         /* Deny enslaving a VLAN device into a VLAN-aware bridge */
2215         if (br_vlan_enabled(dp->bridge_dev) &&
2216             netif_is_bridge_master(info->upper_dev) && info->linking) {
2217                 NL_SET_ERR_MSG_MOD(ext_ack,
2218                                    "Cannot enslave VLAN device into VLAN aware bridge");
2219                 return notifier_from_errno(-EINVAL);
2220         }
2221
2222         return NOTIFY_DONE;
2223 }
2224
2225 static int
2226 dsa_slave_check_8021q_upper(struct net_device *dev,
2227                             struct netdev_notifier_changeupper_info *info)
2228 {
2229         struct dsa_port *dp = dsa_slave_to_port(dev);
2230         struct net_device *br = dp->bridge_dev;
2231         struct bridge_vlan_info br_info;
2232         struct netlink_ext_ack *extack;
2233         int err = NOTIFY_DONE;
2234         u16 vid;
2235
2236         if (!br || !br_vlan_enabled(br))
2237                 return NOTIFY_DONE;
2238
2239         extack = netdev_notifier_info_to_extack(&info->info);
2240         vid = vlan_dev_vlan_id(info->upper_dev);
2241
2242         /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
2243          * device, respectively the VID is not found, returning
2244          * 0 means success, which is a failure for us here.
2245          */
2246         err = br_vlan_get_info(br, vid, &br_info);
2247         if (err == 0) {
2248                 NL_SET_ERR_MSG_MOD(extack,
2249                                    "This VLAN is already configured by the bridge");
2250                 return notifier_from_errno(-EBUSY);
2251         }
2252
2253         return NOTIFY_DONE;
2254 }
2255
2256 static int
2257 dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
2258                                       struct netdev_notifier_changeupper_info *info)
2259 {
2260         struct dsa_switch *ds;
2261         struct dsa_port *dp;
2262         int err;
2263
2264         if (!dsa_slave_dev_check(dev))
2265                 return dsa_prevent_bridging_8021q_upper(dev, info);
2266
2267         dp = dsa_slave_to_port(dev);
2268         ds = dp->ds;
2269
2270         if (ds->ops->port_prechangeupper) {
2271                 err = ds->ops->port_prechangeupper(ds, dp->index, info);
2272                 if (err)
2273                         return notifier_from_errno(err);
2274         }
2275
2276         if (is_vlan_dev(info->upper_dev))
2277                 return dsa_slave_check_8021q_upper(dev, info);
2278
2279         return NOTIFY_DONE;
2280 }
2281
2282 static int dsa_slave_netdevice_event(struct notifier_block *nb,
2283                                      unsigned long event, void *ptr)
2284 {
2285         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2286
2287         switch (event) {
2288         case NETDEV_PRECHANGEUPPER: {
2289                 struct netdev_notifier_changeupper_info *info = ptr;
2290                 int err;
2291
2292                 err = dsa_slave_prechangeupper_sanity_check(dev, info);
2293                 if (err != NOTIFY_DONE)
2294                         return err;
2295
2296                 if (dsa_slave_dev_check(dev))
2297                         return dsa_slave_prechangeupper(dev, ptr);
2298
2299                 if (netif_is_lag_master(dev))
2300                         return dsa_slave_lag_prechangeupper(dev, ptr);
2301
2302                 break;
2303         }
2304         case NETDEV_CHANGEUPPER:
2305                 if (dsa_slave_dev_check(dev))
2306                         return dsa_slave_changeupper(dev, ptr);
2307
2308                 if (netif_is_lag_master(dev))
2309                         return dsa_slave_lag_changeupper(dev, ptr);
2310
2311                 break;
2312         case NETDEV_CHANGELOWERSTATE: {
2313                 struct netdev_notifier_changelowerstate_info *info = ptr;
2314                 struct dsa_port *dp;
2315                 int err;
2316
2317                 if (!dsa_slave_dev_check(dev))
2318                         break;
2319
2320                 dp = dsa_slave_to_port(dev);
2321
2322                 err = dsa_port_lag_change(dp, info->lower_state_info);
2323                 return notifier_from_errno(err);
2324         }
2325         case NETDEV_GOING_DOWN: {
2326                 struct dsa_port *dp, *cpu_dp;
2327                 struct dsa_switch_tree *dst;
2328                 LIST_HEAD(close_list);
2329
2330                 if (!netdev_uses_dsa(dev))
2331                         return NOTIFY_DONE;
2332
2333                 cpu_dp = dev->dsa_ptr;
2334                 dst = cpu_dp->ds->dst;
2335
2336                 list_for_each_entry(dp, &dst->ports, list) {
2337                         if (!dsa_is_user_port(dp->ds, dp->index))
2338                                 continue;
2339
2340                         list_add(&dp->slave->close_list, &close_list);
2341                 }
2342
2343                 dev_close_many(&close_list, true);
2344
2345                 return NOTIFY_OK;
2346         }
2347         default:
2348                 break;
2349         }
2350
2351         return NOTIFY_DONE;
2352 }
2353
2354 static void
2355 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2356 {
2357         struct switchdev_notifier_fdb_info info = {};
2358         struct dsa_switch *ds = switchdev_work->ds;
2359         struct dsa_port *dp;
2360
2361         if (!dsa_is_user_port(ds, switchdev_work->port))
2362                 return;
2363
2364         info.addr = switchdev_work->addr;
2365         info.vid = switchdev_work->vid;
2366         info.offloaded = true;
2367         dp = dsa_to_port(ds, switchdev_work->port);
2368         call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2369                                  dp->slave, &info.info, NULL);
2370 }
2371
2372 static void dsa_slave_switchdev_event_work(struct work_struct *work)
2373 {
2374         struct dsa_switchdev_event_work *switchdev_work =
2375                 container_of(work, struct dsa_switchdev_event_work, work);
2376         struct dsa_switch *ds = switchdev_work->ds;
2377         struct dsa_port *dp;
2378         int err;
2379
2380         dp = dsa_to_port(ds, switchdev_work->port);
2381
2382         rtnl_lock();
2383         switch (switchdev_work->event) {
2384         case SWITCHDEV_FDB_ADD_TO_DEVICE:
2385                 if (switchdev_work->host_addr)
2386                         err = dsa_port_host_fdb_add(dp, switchdev_work->addr,
2387                                                     switchdev_work->vid);
2388                 else
2389                         err = dsa_port_fdb_add(dp, switchdev_work->addr,
2390                                                switchdev_work->vid);
2391                 if (err) {
2392                         dev_err(ds->dev,
2393                                 "port %d failed to add %pM vid %d to fdb: %d\n",
2394                                 dp->index, switchdev_work->addr,
2395                                 switchdev_work->vid, err);
2396                         break;
2397                 }
2398                 dsa_fdb_offload_notify(switchdev_work);
2399                 break;
2400
2401         case SWITCHDEV_FDB_DEL_TO_DEVICE:
2402                 if (switchdev_work->host_addr)
2403                         err = dsa_port_host_fdb_del(dp, switchdev_work->addr,
2404                                                     switchdev_work->vid);
2405                 else
2406                         err = dsa_port_fdb_del(dp, switchdev_work->addr,
2407                                                switchdev_work->vid);
2408                 if (err) {
2409                         dev_err(ds->dev,
2410                                 "port %d failed to delete %pM vid %d from fdb: %d\n",
2411                                 dp->index, switchdev_work->addr,
2412                                 switchdev_work->vid, err);
2413                 }
2414
2415                 break;
2416         }
2417         rtnl_unlock();
2418
2419         dev_put(switchdev_work->dev);
2420         kfree(switchdev_work);
2421 }
2422
2423 static bool dsa_foreign_dev_check(const struct net_device *dev,
2424                                   const struct net_device *foreign_dev)
2425 {
2426         const struct dsa_port *dp = dsa_slave_to_port(dev);
2427         struct dsa_switch_tree *dst = dp->ds->dst;
2428
2429         if (netif_is_bridge_master(foreign_dev))
2430                 return !dsa_tree_offloads_bridge(dst, foreign_dev);
2431
2432         if (netif_is_bridge_port(foreign_dev))
2433                 return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2434
2435         /* Everything else is foreign */
2436         return true;
2437 }
2438
2439 static int dsa_slave_fdb_event(struct net_device *dev,
2440                                const struct net_device *orig_dev,
2441                                const void *ctx,
2442                                const struct switchdev_notifier_fdb_info *fdb_info,
2443                                unsigned long event)
2444 {
2445         struct dsa_switchdev_event_work *switchdev_work;
2446         struct dsa_port *dp = dsa_slave_to_port(dev);
2447         bool host_addr = fdb_info->is_local;
2448         struct dsa_switch *ds = dp->ds;
2449
2450         if (ctx && ctx != dp)
2451                 return 0;
2452
2453         if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2454                 return -EOPNOTSUPP;
2455
2456         if (dsa_slave_dev_check(orig_dev) &&
2457             switchdev_fdb_is_dynamically_learned(fdb_info))
2458                 return 0;
2459
2460         /* FDB entries learned by the software bridge should be installed as
2461          * host addresses only if the driver requests assisted learning.
2462          */
2463         if (switchdev_fdb_is_dynamically_learned(fdb_info) &&
2464             !ds->assisted_learning_on_cpu_port)
2465                 return 0;
2466
2467         /* Also treat FDB entries on foreign interfaces bridged with us as host
2468          * addresses.
2469          */
2470         if (dsa_foreign_dev_check(dev, orig_dev))
2471                 host_addr = true;
2472
2473         switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2474         if (!switchdev_work)
2475                 return -ENOMEM;
2476
2477         netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2478                    event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2479                    orig_dev->name, fdb_info->addr, fdb_info->vid,
2480                    host_addr ? " as host address" : "");
2481
2482         INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2483         switchdev_work->ds = ds;
2484         switchdev_work->port = dp->index;
2485         switchdev_work->event = event;
2486         switchdev_work->dev = dev;
2487
2488         ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2489         switchdev_work->vid = fdb_info->vid;
2490         switchdev_work->host_addr = host_addr;
2491
2492         /* Hold a reference for dsa_fdb_offload_notify */
2493         dev_hold(dev);
2494         dsa_schedule_work(&switchdev_work->work);
2495
2496         return 0;
2497 }
2498
2499 static int
2500 dsa_slave_fdb_add_to_device(struct net_device *dev,
2501                             const struct net_device *orig_dev, const void *ctx,
2502                             const struct switchdev_notifier_fdb_info *fdb_info)
2503 {
2504         return dsa_slave_fdb_event(dev, orig_dev, ctx, fdb_info,
2505                                    SWITCHDEV_FDB_ADD_TO_DEVICE);
2506 }
2507
2508 static int
2509 dsa_slave_fdb_del_to_device(struct net_device *dev,
2510                             const struct net_device *orig_dev, const void *ctx,
2511                             const struct switchdev_notifier_fdb_info *fdb_info)
2512 {
2513         return dsa_slave_fdb_event(dev, orig_dev, ctx, fdb_info,
2514                                    SWITCHDEV_FDB_DEL_TO_DEVICE);
2515 }
2516
2517 /* Called under rcu_read_lock() */
2518 static int dsa_slave_switchdev_event(struct notifier_block *unused,
2519                                      unsigned long event, void *ptr)
2520 {
2521         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2522         int err;
2523
2524         switch (event) {
2525         case SWITCHDEV_PORT_ATTR_SET:
2526                 err = switchdev_handle_port_attr_set(dev, ptr,
2527                                                      dsa_slave_dev_check,
2528                                                      dsa_slave_port_attr_set);
2529                 return notifier_from_errno(err);
2530         case SWITCHDEV_FDB_ADD_TO_DEVICE:
2531                 err = switchdev_handle_fdb_add_to_device(dev, ptr,
2532                                                          dsa_slave_dev_check,
2533                                                          dsa_foreign_dev_check,
2534                                                          dsa_slave_fdb_add_to_device,
2535                                                          NULL);
2536                 return notifier_from_errno(err);
2537         case SWITCHDEV_FDB_DEL_TO_DEVICE:
2538                 err = switchdev_handle_fdb_del_to_device(dev, ptr,
2539                                                          dsa_slave_dev_check,
2540                                                          dsa_foreign_dev_check,
2541                                                          dsa_slave_fdb_del_to_device,
2542                                                          NULL);
2543                 return notifier_from_errno(err);
2544         default:
2545                 return NOTIFY_DONE;
2546         }
2547
2548         return NOTIFY_OK;
2549 }
2550
2551 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2552                                               unsigned long event, void *ptr)
2553 {
2554         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2555         int err;
2556
2557         switch (event) {
2558         case SWITCHDEV_PORT_OBJ_ADD:
2559                 err = switchdev_handle_port_obj_add(dev, ptr,
2560                                                     dsa_slave_dev_check,
2561                                                     dsa_slave_port_obj_add);
2562                 return notifier_from_errno(err);
2563         case SWITCHDEV_PORT_OBJ_DEL:
2564                 err = switchdev_handle_port_obj_del(dev, ptr,
2565                                                     dsa_slave_dev_check,
2566                                                     dsa_slave_port_obj_del);
2567                 return notifier_from_errno(err);
2568         case SWITCHDEV_PORT_ATTR_SET:
2569                 err = switchdev_handle_port_attr_set(dev, ptr,
2570                                                      dsa_slave_dev_check,
2571                                                      dsa_slave_port_attr_set);
2572                 return notifier_from_errno(err);
2573         }
2574
2575         return NOTIFY_DONE;
2576 }
2577
2578 static struct notifier_block dsa_slave_nb __read_mostly = {
2579         .notifier_call  = dsa_slave_netdevice_event,
2580 };
2581
2582 struct notifier_block dsa_slave_switchdev_notifier = {
2583         .notifier_call = dsa_slave_switchdev_event,
2584 };
2585
2586 struct notifier_block dsa_slave_switchdev_blocking_notifier = {
2587         .notifier_call = dsa_slave_switchdev_blocking_event,
2588 };
2589
2590 int dsa_slave_register_notifier(void)
2591 {
2592         struct notifier_block *nb;
2593         int err;
2594
2595         err = register_netdevice_notifier(&dsa_slave_nb);
2596         if (err)
2597                 return err;
2598
2599         err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2600         if (err)
2601                 goto err_switchdev_nb;
2602
2603         nb = &dsa_slave_switchdev_blocking_notifier;
2604         err = register_switchdev_blocking_notifier(nb);
2605         if (err)
2606                 goto err_switchdev_blocking_nb;
2607
2608         return 0;
2609
2610 err_switchdev_blocking_nb:
2611         unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2612 err_switchdev_nb:
2613         unregister_netdevice_notifier(&dsa_slave_nb);
2614         return err;
2615 }
2616
2617 void dsa_slave_unregister_notifier(void)
2618 {
2619         struct notifier_block *nb;
2620         int err;
2621
2622         nb = &dsa_slave_switchdev_blocking_notifier;
2623         err = unregister_switchdev_blocking_notifier(nb);
2624         if (err)
2625                 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
2626
2627         err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2628         if (err)
2629                 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2630
2631         err = unregister_netdevice_notifier(&dsa_slave_nb);
2632         if (err)
2633                 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
2634 }