~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/dsa/slave.c

Version: ~ [ linux-5.11 ] ~ [ linux-5.10.17 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.99 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.176 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.221 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.257 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.257 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp