Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux/fpc-iii.git] / net / dsa / slave.c
blobd6e7a642493b03223ab9890247983d9d1499cea0
1 /*
2 * net/dsa/slave.c - Slave device handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/of_net.h>
17 #include <linux/of_mdio.h>
18 #include <linux/mdio.h>
19 #include <linux/list.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
26 #include "dsa_priv.h"
28 static bool dsa_slave_dev_check(struct net_device *dev);
30 /* slave mii_bus handling ***************************************************/
31 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
33 struct dsa_switch *ds = bus->priv;
35 if (ds->phys_mii_mask & (1 << addr))
36 return ds->ops->phy_read(ds, addr, reg);
38 return 0xffff;
41 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
43 struct dsa_switch *ds = bus->priv;
45 if (ds->phys_mii_mask & (1 << addr))
46 return ds->ops->phy_write(ds, addr, reg, val);
48 return 0;
51 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
53 ds->slave_mii_bus->priv = (void *)ds;
54 ds->slave_mii_bus->name = "dsa slave smi";
55 ds->slave_mii_bus->read = dsa_slave_phy_read;
56 ds->slave_mii_bus->write = dsa_slave_phy_write;
57 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
58 ds->dst->index, ds->index);
59 ds->slave_mii_bus->parent = ds->dev;
60 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
64 /* slave device handling ****************************************************/
65 static int dsa_slave_get_iflink(const struct net_device *dev)
67 return dsa_slave_to_master(dev)->ifindex;
70 static int dsa_slave_open(struct net_device *dev)
72 struct net_device *master = dsa_slave_to_master(dev);
73 struct dsa_port *dp = dsa_slave_to_port(dev);
74 int err;
76 if (!(master->flags & IFF_UP))
77 return -ENETDOWN;
79 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
80 err = dev_uc_add(master, dev->dev_addr);
81 if (err < 0)
82 goto out;
85 if (dev->flags & IFF_ALLMULTI) {
86 err = dev_set_allmulti(master, 1);
87 if (err < 0)
88 goto del_unicast;
90 if (dev->flags & IFF_PROMISC) {
91 err = dev_set_promiscuity(master, 1);
92 if (err < 0)
93 goto clear_allmulti;
96 err = dsa_port_enable(dp, dev->phydev);
97 if (err)
98 goto clear_promisc;
100 if (dev->phydev)
101 phy_start(dev->phydev);
103 return 0;
105 clear_promisc:
106 if (dev->flags & IFF_PROMISC)
107 dev_set_promiscuity(master, -1);
108 clear_allmulti:
109 if (dev->flags & IFF_ALLMULTI)
110 dev_set_allmulti(master, -1);
111 del_unicast:
112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
113 dev_uc_del(master, dev->dev_addr);
114 out:
115 return err;
118 static int dsa_slave_close(struct net_device *dev)
120 struct net_device *master = dsa_slave_to_master(dev);
121 struct dsa_port *dp = dsa_slave_to_port(dev);
123 if (dev->phydev)
124 phy_stop(dev->phydev);
126 dsa_port_disable(dp, dev->phydev);
128 dev_mc_unsync(master, dev);
129 dev_uc_unsync(master, dev);
130 if (dev->flags & IFF_ALLMULTI)
131 dev_set_allmulti(master, -1);
132 if (dev->flags & IFF_PROMISC)
133 dev_set_promiscuity(master, -1);
135 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
136 dev_uc_del(master, dev->dev_addr);
138 return 0;
141 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
143 struct net_device *master = dsa_slave_to_master(dev);
145 if (change & IFF_ALLMULTI)
146 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
147 if (change & IFF_PROMISC)
148 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
151 static void dsa_slave_set_rx_mode(struct net_device *dev)
153 struct net_device *master = dsa_slave_to_master(dev);
155 dev_mc_sync(master, dev);
156 dev_uc_sync(master, dev);
159 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
161 struct net_device *master = dsa_slave_to_master(dev);
162 struct sockaddr *addr = a;
163 int err;
165 if (!is_valid_ether_addr(addr->sa_data))
166 return -EADDRNOTAVAIL;
168 if (!(dev->flags & IFF_UP))
169 goto out;
171 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
172 err = dev_uc_add(master, addr->sa_data);
173 if (err < 0)
174 return err;
177 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
178 dev_uc_del(master, dev->dev_addr);
180 out:
181 ether_addr_copy(dev->dev_addr, addr->sa_data);
183 return 0;
186 struct dsa_slave_dump_ctx {
187 struct net_device *dev;
188 struct sk_buff *skb;
189 struct netlink_callback *cb;
190 int idx;
193 static int
194 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
195 bool is_static, void *data)
197 struct dsa_slave_dump_ctx *dump = data;
198 u32 portid = NETLINK_CB(dump->cb->skb).portid;
199 u32 seq = dump->cb->nlh->nlmsg_seq;
200 struct nlmsghdr *nlh;
201 struct ndmsg *ndm;
203 if (dump->idx < dump->cb->args[2])
204 goto skip;
206 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
207 sizeof(*ndm), NLM_F_MULTI);
208 if (!nlh)
209 return -EMSGSIZE;
211 ndm = nlmsg_data(nlh);
212 ndm->ndm_family = AF_BRIDGE;
213 ndm->ndm_pad1 = 0;
214 ndm->ndm_pad2 = 0;
215 ndm->ndm_flags = NTF_SELF;
216 ndm->ndm_type = 0;
217 ndm->ndm_ifindex = dump->dev->ifindex;
218 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
220 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
221 goto nla_put_failure;
223 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
224 goto nla_put_failure;
226 nlmsg_end(dump->skb, nlh);
228 skip:
229 dump->idx++;
230 return 0;
232 nla_put_failure:
233 nlmsg_cancel(dump->skb, nlh);
234 return -EMSGSIZE;
237 static int
238 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
239 struct net_device *dev, struct net_device *filter_dev,
240 int *idx)
242 struct dsa_port *dp = dsa_slave_to_port(dev);
243 struct dsa_slave_dump_ctx dump = {
244 .dev = dev,
245 .skb = skb,
246 .cb = cb,
247 .idx = *idx,
249 int err;
251 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
252 *idx = dump.idx;
254 return err;
257 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
259 if (!dev->phydev)
260 return -ENODEV;
262 return phy_mii_ioctl(dev->phydev, ifr, cmd);
265 static int dsa_slave_port_attr_set(struct net_device *dev,
266 const struct switchdev_attr *attr,
267 struct switchdev_trans *trans)
269 struct dsa_port *dp = dsa_slave_to_port(dev);
270 int ret;
272 switch (attr->id) {
273 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
274 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
275 break;
276 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
277 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
278 trans);
279 break;
280 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
281 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
282 break;
283 default:
284 ret = -EOPNOTSUPP;
285 break;
288 return ret;
291 static int dsa_slave_port_obj_add(struct net_device *dev,
292 const struct switchdev_obj *obj,
293 struct switchdev_trans *trans)
295 struct dsa_port *dp = dsa_slave_to_port(dev);
296 int err;
298 /* For the prepare phase, ensure the full set of changes is feasable in
299 * one go in order to signal a failure properly. If an operation is not
300 * supported, return -EOPNOTSUPP.
303 switch (obj->id) {
304 case SWITCHDEV_OBJ_ID_PORT_MDB:
305 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
306 break;
307 case SWITCHDEV_OBJ_ID_HOST_MDB:
308 /* DSA can directly translate this to a normal MDB add,
309 * but on the CPU port.
311 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
312 trans);
313 break;
314 case SWITCHDEV_OBJ_ID_PORT_VLAN:
315 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
316 trans);
317 break;
318 default:
319 err = -EOPNOTSUPP;
320 break;
323 return err;
326 static int dsa_slave_port_obj_del(struct net_device *dev,
327 const struct switchdev_obj *obj)
329 struct dsa_port *dp = dsa_slave_to_port(dev);
330 int err;
332 switch (obj->id) {
333 case SWITCHDEV_OBJ_ID_PORT_MDB:
334 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
335 break;
336 case SWITCHDEV_OBJ_ID_HOST_MDB:
337 /* DSA can directly translate this to a normal MDB add,
338 * but on the CPU port.
340 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
341 break;
342 case SWITCHDEV_OBJ_ID_PORT_VLAN:
343 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
344 break;
345 default:
346 err = -EOPNOTSUPP;
347 break;
350 return err;
353 static int dsa_slave_port_attr_get(struct net_device *dev,
354 struct switchdev_attr *attr)
356 struct dsa_port *dp = dsa_slave_to_port(dev);
357 struct dsa_switch *ds = dp->ds;
358 struct dsa_switch_tree *dst = ds->dst;
360 switch (attr->id) {
361 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
362 attr->u.ppid.id_len = sizeof(dst->index);
363 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
364 break;
365 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
366 attr->u.brport_flags_support = 0;
367 break;
368 default:
369 return -EOPNOTSUPP;
372 return 0;
375 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
376 struct sk_buff *skb)
378 #ifdef CONFIG_NET_POLL_CONTROLLER
379 struct dsa_slave_priv *p = netdev_priv(dev);
381 if (p->netpoll)
382 netpoll_send_skb(p->netpoll, skb);
383 #else
384 BUG();
385 #endif
386 return NETDEV_TX_OK;
389 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
391 struct dsa_slave_priv *p = netdev_priv(dev);
392 struct pcpu_sw_netstats *s;
393 struct sk_buff *nskb;
395 s = this_cpu_ptr(p->stats64);
396 u64_stats_update_begin(&s->syncp);
397 s->tx_packets++;
398 s->tx_bytes += skb->len;
399 u64_stats_update_end(&s->syncp);
401 /* Transmit function may have to reallocate the original SKB,
402 * in which case it must have freed it. Only free it here on error.
404 nskb = p->xmit(skb, dev);
405 if (!nskb) {
406 kfree_skb(skb);
407 return NETDEV_TX_OK;
410 /* SKB for netpoll still need to be mangled with the protocol-specific
411 * tag to be successfully transmitted
413 if (unlikely(netpoll_tx_running(dev)))
414 return dsa_slave_netpoll_send_skb(dev, nskb);
416 /* Queue the SKB for transmission on the parent interface, but
417 * do not modify its EtherType
419 nskb->dev = dsa_slave_to_master(dev);
420 dev_queue_xmit(nskb);
422 return NETDEV_TX_OK;
425 /* ethtool operations *******************************************************/
427 static void dsa_slave_get_drvinfo(struct net_device *dev,
428 struct ethtool_drvinfo *drvinfo)
430 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
431 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
432 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
435 static int dsa_slave_get_regs_len(struct net_device *dev)
437 struct dsa_port *dp = dsa_slave_to_port(dev);
438 struct dsa_switch *ds = dp->ds;
440 if (ds->ops->get_regs_len)
441 return ds->ops->get_regs_len(ds, dp->index);
443 return -EOPNOTSUPP;
446 static void
447 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
449 struct dsa_port *dp = dsa_slave_to_port(dev);
450 struct dsa_switch *ds = dp->ds;
452 if (ds->ops->get_regs)
453 ds->ops->get_regs(ds, dp->index, regs, _p);
456 static u32 dsa_slave_get_link(struct net_device *dev)
458 if (!dev->phydev)
459 return -ENODEV;
461 genphy_update_link(dev->phydev);
463 return dev->phydev->link;
466 static int dsa_slave_get_eeprom_len(struct net_device *dev)
468 struct dsa_port *dp = dsa_slave_to_port(dev);
469 struct dsa_switch *ds = dp->ds;
471 if (ds->cd && ds->cd->eeprom_len)
472 return ds->cd->eeprom_len;
474 if (ds->ops->get_eeprom_len)
475 return ds->ops->get_eeprom_len(ds);
477 return 0;
480 static int dsa_slave_get_eeprom(struct net_device *dev,
481 struct ethtool_eeprom *eeprom, u8 *data)
483 struct dsa_port *dp = dsa_slave_to_port(dev);
484 struct dsa_switch *ds = dp->ds;
486 if (ds->ops->get_eeprom)
487 return ds->ops->get_eeprom(ds, eeprom, data);
489 return -EOPNOTSUPP;
492 static int dsa_slave_set_eeprom(struct net_device *dev,
493 struct ethtool_eeprom *eeprom, u8 *data)
495 struct dsa_port *dp = dsa_slave_to_port(dev);
496 struct dsa_switch *ds = dp->ds;
498 if (ds->ops->set_eeprom)
499 return ds->ops->set_eeprom(ds, eeprom, data);
501 return -EOPNOTSUPP;
504 static void dsa_slave_get_strings(struct net_device *dev,
505 uint32_t stringset, uint8_t *data)
507 struct dsa_port *dp = dsa_slave_to_port(dev);
508 struct dsa_switch *ds = dp->ds;
510 if (stringset == ETH_SS_STATS) {
511 int len = ETH_GSTRING_LEN;
513 strncpy(data, "tx_packets", len);
514 strncpy(data + len, "tx_bytes", len);
515 strncpy(data + 2 * len, "rx_packets", len);
516 strncpy(data + 3 * len, "rx_bytes", len);
517 if (ds->ops->get_strings)
518 ds->ops->get_strings(ds, dp->index, data + 4 * len);
522 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
523 struct ethtool_stats *stats,
524 uint64_t *data)
526 struct dsa_port *dp = dsa_slave_to_port(dev);
527 struct dsa_slave_priv *p = netdev_priv(dev);
528 struct dsa_switch *ds = dp->ds;
529 struct pcpu_sw_netstats *s;
530 unsigned int start;
531 int i;
533 for_each_possible_cpu(i) {
534 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
536 s = per_cpu_ptr(p->stats64, i);
537 do {
538 start = u64_stats_fetch_begin_irq(&s->syncp);
539 tx_packets = s->tx_packets;
540 tx_bytes = s->tx_bytes;
541 rx_packets = s->rx_packets;
542 rx_bytes = s->rx_bytes;
543 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
544 data[0] += tx_packets;
545 data[1] += tx_bytes;
546 data[2] += rx_packets;
547 data[3] += rx_bytes;
549 if (ds->ops->get_ethtool_stats)
550 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
553 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
555 struct dsa_port *dp = dsa_slave_to_port(dev);
556 struct dsa_switch *ds = dp->ds;
558 if (sset == ETH_SS_STATS) {
559 int count;
561 count = 4;
562 if (ds->ops->get_sset_count)
563 count += ds->ops->get_sset_count(ds);
565 return count;
568 return -EOPNOTSUPP;
571 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
573 struct dsa_port *dp = dsa_slave_to_port(dev);
574 struct dsa_switch *ds = dp->ds;
576 if (ds->ops->get_wol)
577 ds->ops->get_wol(ds, dp->index, w);
580 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
582 struct dsa_port *dp = dsa_slave_to_port(dev);
583 struct dsa_switch *ds = dp->ds;
584 int ret = -EOPNOTSUPP;
586 if (ds->ops->set_wol)
587 ret = ds->ops->set_wol(ds, dp->index, w);
589 return ret;
592 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
594 struct dsa_port *dp = dsa_slave_to_port(dev);
595 struct dsa_switch *ds = dp->ds;
596 int ret;
598 /* Port's PHY and MAC both need to be EEE capable */
599 if (!dev->phydev)
600 return -ENODEV;
602 if (!ds->ops->set_mac_eee)
603 return -EOPNOTSUPP;
605 ret = ds->ops->set_mac_eee(ds, dp->index, e);
606 if (ret)
607 return ret;
609 if (e->eee_enabled) {
610 ret = phy_init_eee(dev->phydev, 0);
611 if (ret)
612 return ret;
615 return phy_ethtool_set_eee(dev->phydev, e);
618 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
620 struct dsa_port *dp = dsa_slave_to_port(dev);
621 struct dsa_switch *ds = dp->ds;
622 int ret;
624 /* Port's PHY and MAC both need to be EEE capable */
625 if (!dev->phydev)
626 return -ENODEV;
628 if (!ds->ops->get_mac_eee)
629 return -EOPNOTSUPP;
631 ret = ds->ops->get_mac_eee(ds, dp->index, e);
632 if (ret)
633 return ret;
635 return phy_ethtool_get_eee(dev->phydev, e);
638 #ifdef CONFIG_NET_POLL_CONTROLLER
639 static int dsa_slave_netpoll_setup(struct net_device *dev,
640 struct netpoll_info *ni)
642 struct net_device *master = dsa_slave_to_master(dev);
643 struct dsa_slave_priv *p = netdev_priv(dev);
644 struct netpoll *netpoll;
645 int err = 0;
647 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
648 if (!netpoll)
649 return -ENOMEM;
651 err = __netpoll_setup(netpoll, master);
652 if (err) {
653 kfree(netpoll);
654 goto out;
657 p->netpoll = netpoll;
658 out:
659 return err;
662 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
664 struct dsa_slave_priv *p = netdev_priv(dev);
665 struct netpoll *netpoll = p->netpoll;
667 if (!netpoll)
668 return;
670 p->netpoll = NULL;
672 __netpoll_free_async(netpoll);
675 static void dsa_slave_poll_controller(struct net_device *dev)
678 #endif
680 static int dsa_slave_get_phys_port_name(struct net_device *dev,
681 char *name, size_t len)
683 struct dsa_port *dp = dsa_slave_to_port(dev);
685 if (snprintf(name, len, "p%d", dp->index) >= len)
686 return -EINVAL;
688 return 0;
691 static struct dsa_mall_tc_entry *
692 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
694 struct dsa_slave_priv *p = netdev_priv(dev);
695 struct dsa_mall_tc_entry *mall_tc_entry;
697 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
698 if (mall_tc_entry->cookie == cookie)
699 return mall_tc_entry;
701 return NULL;
704 static int dsa_slave_add_cls_matchall(struct net_device *dev,
705 struct tc_cls_matchall_offload *cls,
706 bool ingress)
708 struct dsa_port *dp = dsa_slave_to_port(dev);
709 struct dsa_slave_priv *p = netdev_priv(dev);
710 struct dsa_mall_tc_entry *mall_tc_entry;
711 __be16 protocol = cls->common.protocol;
712 struct net *net = dev_net(dev);
713 struct dsa_switch *ds = dp->ds;
714 struct net_device *to_dev;
715 const struct tc_action *a;
716 struct dsa_port *to_dp;
717 int err = -EOPNOTSUPP;
718 LIST_HEAD(actions);
719 int ifindex;
721 if (!ds->ops->port_mirror_add)
722 return err;
724 if (!tcf_exts_has_one_action(cls->exts))
725 return err;
727 tcf_exts_to_list(cls->exts, &actions);
728 a = list_first_entry(&actions, struct tc_action, list);
730 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
731 struct dsa_mall_mirror_tc_entry *mirror;
733 ifindex = tcf_mirred_ifindex(a);
734 to_dev = __dev_get_by_index(net, ifindex);
735 if (!to_dev)
736 return -EINVAL;
738 if (!dsa_slave_dev_check(to_dev))
739 return -EOPNOTSUPP;
741 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
742 if (!mall_tc_entry)
743 return -ENOMEM;
745 mall_tc_entry->cookie = cls->cookie;
746 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
747 mirror = &mall_tc_entry->mirror;
749 to_dp = dsa_slave_to_port(to_dev);
751 mirror->to_local_port = to_dp->index;
752 mirror->ingress = ingress;
754 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
755 if (err) {
756 kfree(mall_tc_entry);
757 return err;
760 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
763 return 0;
766 static void dsa_slave_del_cls_matchall(struct net_device *dev,
767 struct tc_cls_matchall_offload *cls)
769 struct dsa_port *dp = dsa_slave_to_port(dev);
770 struct dsa_mall_tc_entry *mall_tc_entry;
771 struct dsa_switch *ds = dp->ds;
773 if (!ds->ops->port_mirror_del)
774 return;
776 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
777 if (!mall_tc_entry)
778 return;
780 list_del(&mall_tc_entry->list);
782 switch (mall_tc_entry->type) {
783 case DSA_PORT_MALL_MIRROR:
784 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
785 break;
786 default:
787 WARN_ON(1);
790 kfree(mall_tc_entry);
793 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
794 struct tc_cls_matchall_offload *cls,
795 bool ingress)
797 if (cls->common.chain_index)
798 return -EOPNOTSUPP;
800 switch (cls->command) {
801 case TC_CLSMATCHALL_REPLACE:
802 return dsa_slave_add_cls_matchall(dev, cls, ingress);
803 case TC_CLSMATCHALL_DESTROY:
804 dsa_slave_del_cls_matchall(dev, cls);
805 return 0;
806 default:
807 return -EOPNOTSUPP;
811 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
812 void *cb_priv, bool ingress)
814 struct net_device *dev = cb_priv;
816 if (!tc_can_offload(dev))
817 return -EOPNOTSUPP;
819 switch (type) {
820 case TC_SETUP_CLSMATCHALL:
821 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
822 default:
823 return -EOPNOTSUPP;
827 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
828 void *type_data, void *cb_priv)
830 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
833 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
834 void *type_data, void *cb_priv)
836 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
839 static int dsa_slave_setup_tc_block(struct net_device *dev,
840 struct tc_block_offload *f)
842 tc_setup_cb_t *cb;
844 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
845 cb = dsa_slave_setup_tc_block_cb_ig;
846 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
847 cb = dsa_slave_setup_tc_block_cb_eg;
848 else
849 return -EOPNOTSUPP;
851 switch (f->command) {
852 case TC_BLOCK_BIND:
853 return tcf_block_cb_register(f->block, cb, dev, dev);
854 case TC_BLOCK_UNBIND:
855 tcf_block_cb_unregister(f->block, cb, dev);
856 return 0;
857 default:
858 return -EOPNOTSUPP;
862 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
863 void *type_data)
865 switch (type) {
866 case TC_SETUP_BLOCK:
867 return dsa_slave_setup_tc_block(dev, type_data);
868 default:
869 return -EOPNOTSUPP;
873 static void dsa_slave_get_stats64(struct net_device *dev,
874 struct rtnl_link_stats64 *stats)
876 struct dsa_slave_priv *p = netdev_priv(dev);
877 struct pcpu_sw_netstats *s;
878 unsigned int start;
879 int i;
881 netdev_stats_to_stats64(stats, &dev->stats);
882 for_each_possible_cpu(i) {
883 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
885 s = per_cpu_ptr(p->stats64, i);
886 do {
887 start = u64_stats_fetch_begin_irq(&s->syncp);
888 tx_packets = s->tx_packets;
889 tx_bytes = s->tx_bytes;
890 rx_packets = s->rx_packets;
891 rx_bytes = s->rx_bytes;
892 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
894 stats->tx_packets += tx_packets;
895 stats->tx_bytes += tx_bytes;
896 stats->rx_packets += rx_packets;
897 stats->rx_bytes += rx_bytes;
901 static int dsa_slave_get_rxnfc(struct net_device *dev,
902 struct ethtool_rxnfc *nfc, u32 *rule_locs)
904 struct dsa_port *dp = dsa_slave_to_port(dev);
905 struct dsa_switch *ds = dp->ds;
907 if (!ds->ops->get_rxnfc)
908 return -EOPNOTSUPP;
910 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
913 static int dsa_slave_set_rxnfc(struct net_device *dev,
914 struct ethtool_rxnfc *nfc)
916 struct dsa_port *dp = dsa_slave_to_port(dev);
917 struct dsa_switch *ds = dp->ds;
919 if (!ds->ops->set_rxnfc)
920 return -EOPNOTSUPP;
922 return ds->ops->set_rxnfc(ds, dp->index, nfc);
925 static const struct ethtool_ops dsa_slave_ethtool_ops = {
926 .get_drvinfo = dsa_slave_get_drvinfo,
927 .get_regs_len = dsa_slave_get_regs_len,
928 .get_regs = dsa_slave_get_regs,
929 .nway_reset = phy_ethtool_nway_reset,
930 .get_link = dsa_slave_get_link,
931 .get_eeprom_len = dsa_slave_get_eeprom_len,
932 .get_eeprom = dsa_slave_get_eeprom,
933 .set_eeprom = dsa_slave_set_eeprom,
934 .get_strings = dsa_slave_get_strings,
935 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
936 .get_sset_count = dsa_slave_get_sset_count,
937 .set_wol = dsa_slave_set_wol,
938 .get_wol = dsa_slave_get_wol,
939 .set_eee = dsa_slave_set_eee,
940 .get_eee = dsa_slave_get_eee,
941 .get_link_ksettings = phy_ethtool_get_link_ksettings,
942 .set_link_ksettings = phy_ethtool_set_link_ksettings,
943 .get_rxnfc = dsa_slave_get_rxnfc,
944 .set_rxnfc = dsa_slave_set_rxnfc,
947 static const struct net_device_ops dsa_slave_netdev_ops = {
948 .ndo_open = dsa_slave_open,
949 .ndo_stop = dsa_slave_close,
950 .ndo_start_xmit = dsa_slave_xmit,
951 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
952 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
953 .ndo_set_mac_address = dsa_slave_set_mac_address,
954 .ndo_fdb_add = dsa_legacy_fdb_add,
955 .ndo_fdb_del = dsa_legacy_fdb_del,
956 .ndo_fdb_dump = dsa_slave_fdb_dump,
957 .ndo_do_ioctl = dsa_slave_ioctl,
958 .ndo_get_iflink = dsa_slave_get_iflink,
959 #ifdef CONFIG_NET_POLL_CONTROLLER
960 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
961 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
962 .ndo_poll_controller = dsa_slave_poll_controller,
963 #endif
964 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
965 .ndo_setup_tc = dsa_slave_setup_tc,
966 .ndo_get_stats64 = dsa_slave_get_stats64,
969 static const struct switchdev_ops dsa_slave_switchdev_ops = {
970 .switchdev_port_attr_get = dsa_slave_port_attr_get,
971 .switchdev_port_attr_set = dsa_slave_port_attr_set,
972 .switchdev_port_obj_add = dsa_slave_port_obj_add,
973 .switchdev_port_obj_del = dsa_slave_port_obj_del,
976 static struct device_type dsa_type = {
977 .name = "dsa",
980 static void dsa_slave_adjust_link(struct net_device *dev)
982 struct dsa_port *dp = dsa_slave_to_port(dev);
983 struct dsa_slave_priv *p = netdev_priv(dev);
984 struct dsa_switch *ds = dp->ds;
985 unsigned int status_changed = 0;
987 if (p->old_link != dev->phydev->link) {
988 status_changed = 1;
989 p->old_link = dev->phydev->link;
992 if (p->old_duplex != dev->phydev->duplex) {
993 status_changed = 1;
994 p->old_duplex = dev->phydev->duplex;
997 if (p->old_pause != dev->phydev->pause) {
998 status_changed = 1;
999 p->old_pause = dev->phydev->pause;
1002 if (ds->ops->adjust_link && status_changed)
1003 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1005 if (status_changed)
1006 phy_print_status(dev->phydev);
1009 static int dsa_slave_fixed_link_update(struct net_device *dev,
1010 struct fixed_phy_status *status)
1012 struct dsa_switch *ds;
1013 struct dsa_port *dp;
1015 if (dev) {
1016 dp = dsa_slave_to_port(dev);
1017 ds = dp->ds;
1018 if (ds->ops->fixed_link_update)
1019 ds->ops->fixed_link_update(ds, dp->index, status);
1022 return 0;
1025 /* slave device setup *******************************************************/
1026 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1028 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1029 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1030 struct dsa_switch *ds = dp->ds;
1032 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1033 if (!slave_dev->phydev) {
1034 netdev_err(slave_dev, "no phy at %d\n", addr);
1035 return -ENODEV;
1038 /* Use already configured phy mode */
1039 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1040 p->phy_interface = slave_dev->phydev->interface;
1042 return phy_connect_direct(slave_dev, slave_dev->phydev,
1043 dsa_slave_adjust_link, p->phy_interface);
1046 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1048 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1049 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1050 struct device_node *port_dn = dp->dn;
1051 struct dsa_switch *ds = dp->ds;
1052 struct device_node *phy_dn;
1053 bool phy_is_fixed = false;
1054 u32 phy_flags = 0;
1055 int mode, ret;
1057 mode = of_get_phy_mode(port_dn);
1058 if (mode < 0)
1059 mode = PHY_INTERFACE_MODE_NA;
1060 p->phy_interface = mode;
1062 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1063 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1064 /* In the case of a fixed PHY, the DT node associated
1065 * to the fixed PHY is the Port DT node
1067 ret = of_phy_register_fixed_link(port_dn);
1068 if (ret) {
1069 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1070 return ret;
1072 phy_is_fixed = true;
1073 phy_dn = of_node_get(port_dn);
1076 if (ds->ops->get_phy_flags)
1077 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1079 if (phy_dn) {
1080 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1081 dsa_slave_adjust_link,
1082 phy_flags,
1083 p->phy_interface);
1084 of_node_put(phy_dn);
1087 if (slave_dev->phydev && phy_is_fixed)
1088 fixed_phy_set_link_update(slave_dev->phydev,
1089 dsa_slave_fixed_link_update);
1091 /* We could not connect to a designated PHY, so use the switch internal
1092 * MDIO bus instead
1094 if (!slave_dev->phydev) {
1095 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1096 if (ret) {
1097 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1098 dp->index, ret);
1099 if (phy_is_fixed)
1100 of_phy_deregister_fixed_link(port_dn);
1101 return ret;
1105 phy_attached_info(slave_dev->phydev);
1107 return 0;
1110 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1111 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1112 struct netdev_queue *txq,
1113 void *_unused)
1115 lockdep_set_class(&txq->_xmit_lock,
1116 &dsa_slave_netdev_xmit_lock_key);
1119 int dsa_slave_suspend(struct net_device *slave_dev)
1121 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1123 netif_device_detach(slave_dev);
1125 if (slave_dev->phydev) {
1126 phy_stop(slave_dev->phydev);
1127 p->old_pause = -1;
1128 p->old_link = -1;
1129 p->old_duplex = -1;
1130 phy_suspend(slave_dev->phydev);
1133 return 0;
1136 int dsa_slave_resume(struct net_device *slave_dev)
1138 netif_device_attach(slave_dev);
1140 if (slave_dev->phydev) {
1141 phy_resume(slave_dev->phydev);
1142 phy_start(slave_dev->phydev);
1145 return 0;
1148 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1150 struct net_device *master = dsa_slave_to_master(dev);
1151 struct dsa_port *dp = dsa_slave_to_port(dev);
1152 struct dsa_notifier_register_info rinfo = {
1153 .switch_number = dp->ds->index,
1154 .port_number = dp->index,
1155 .master = master,
1156 .info.dev = dev,
1159 call_dsa_notifiers(val, dev, &rinfo.info);
1162 int dsa_slave_create(struct dsa_port *port)
1164 const struct dsa_port *cpu_dp = port->cpu_dp;
1165 struct net_device *master = cpu_dp->master;
1166 struct dsa_switch *ds = port->ds;
1167 const char *name = port->name;
1168 struct net_device *slave_dev;
1169 struct dsa_slave_priv *p;
1170 int ret;
1172 if (!ds->num_tx_queues)
1173 ds->num_tx_queues = 1;
1175 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1176 NET_NAME_UNKNOWN, ether_setup,
1177 ds->num_tx_queues, 1);
1178 if (slave_dev == NULL)
1179 return -ENOMEM;
1181 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1182 slave_dev->hw_features |= NETIF_F_HW_TC;
1183 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1184 eth_hw_addr_inherit(slave_dev, master);
1185 slave_dev->priv_flags |= IFF_NO_QUEUE;
1186 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1187 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1188 slave_dev->min_mtu = 0;
1189 slave_dev->max_mtu = ETH_MAX_MTU;
1190 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1192 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1193 NULL);
1195 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1196 slave_dev->dev.of_node = port->dn;
1197 slave_dev->vlan_features = master->vlan_features;
1199 p = netdev_priv(slave_dev);
1200 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1201 if (!p->stats64) {
1202 free_netdev(slave_dev);
1203 return -ENOMEM;
1205 p->dp = port;
1206 INIT_LIST_HEAD(&p->mall_tc_list);
1207 p->xmit = cpu_dp->tag_ops->xmit;
1209 p->old_pause = -1;
1210 p->old_link = -1;
1211 p->old_duplex = -1;
1213 port->slave = slave_dev;
1215 netif_carrier_off(slave_dev);
1217 ret = dsa_slave_phy_setup(slave_dev);
1218 if (ret) {
1219 netdev_err(master, "error %d setting up slave phy\n", ret);
1220 goto out_free;
1223 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1225 ret = register_netdev(slave_dev);
1226 if (ret) {
1227 netdev_err(master, "error %d registering interface %s\n",
1228 ret, slave_dev->name);
1229 goto out_phy;
1232 return 0;
1234 out_phy:
1235 phy_disconnect(slave_dev->phydev);
1236 if (of_phy_is_fixed_link(port->dn))
1237 of_phy_deregister_fixed_link(port->dn);
1238 out_free:
1239 free_percpu(p->stats64);
1240 free_netdev(slave_dev);
1241 port->slave = NULL;
1242 return ret;
1245 void dsa_slave_destroy(struct net_device *slave_dev)
1247 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1248 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1249 struct device_node *port_dn = dp->dn;
1251 netif_carrier_off(slave_dev);
1252 if (slave_dev->phydev) {
1253 phy_disconnect(slave_dev->phydev);
1255 if (of_phy_is_fixed_link(port_dn))
1256 of_phy_deregister_fixed_link(port_dn);
1258 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1259 unregister_netdev(slave_dev);
1260 free_percpu(p->stats64);
1261 free_netdev(slave_dev);
1264 static bool dsa_slave_dev_check(struct net_device *dev)
1266 return dev->netdev_ops == &dsa_slave_netdev_ops;
1269 static int dsa_slave_changeupper(struct net_device *dev,
1270 struct netdev_notifier_changeupper_info *info)
1272 struct dsa_port *dp = dsa_slave_to_port(dev);
1273 int err = NOTIFY_DONE;
1275 if (netif_is_bridge_master(info->upper_dev)) {
1276 if (info->linking) {
1277 err = dsa_port_bridge_join(dp, info->upper_dev);
1278 err = notifier_from_errno(err);
1279 } else {
1280 dsa_port_bridge_leave(dp, info->upper_dev);
1281 err = NOTIFY_OK;
1285 return err;
1288 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1289 unsigned long event, void *ptr)
1291 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1293 if (!dsa_slave_dev_check(dev))
1294 return NOTIFY_DONE;
1296 if (event == NETDEV_CHANGEUPPER)
1297 return dsa_slave_changeupper(dev, ptr);
1299 return NOTIFY_DONE;
1302 struct dsa_switchdev_event_work {
1303 struct work_struct work;
1304 struct switchdev_notifier_fdb_info fdb_info;
1305 struct net_device *dev;
1306 unsigned long event;
1309 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1311 struct dsa_switchdev_event_work *switchdev_work =
1312 container_of(work, struct dsa_switchdev_event_work, work);
1313 struct net_device *dev = switchdev_work->dev;
1314 struct switchdev_notifier_fdb_info *fdb_info;
1315 struct dsa_port *dp = dsa_slave_to_port(dev);
1316 int err;
1318 rtnl_lock();
1319 switch (switchdev_work->event) {
1320 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1321 fdb_info = &switchdev_work->fdb_info;
1322 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1323 if (err) {
1324 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1325 break;
1327 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1328 &fdb_info->info);
1329 break;
1331 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1332 fdb_info = &switchdev_work->fdb_info;
1333 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1334 if (err) {
1335 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1336 dev_close(dev);
1338 break;
1340 rtnl_unlock();
1342 kfree(switchdev_work->fdb_info.addr);
1343 kfree(switchdev_work);
1344 dev_put(dev);
1347 static int
1348 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1349 switchdev_work,
1350 const struct switchdev_notifier_fdb_info *
1351 fdb_info)
1353 memcpy(&switchdev_work->fdb_info, fdb_info,
1354 sizeof(switchdev_work->fdb_info));
1355 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1356 if (!switchdev_work->fdb_info.addr)
1357 return -ENOMEM;
1358 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1359 fdb_info->addr);
1360 return 0;
1363 /* Called under rcu_read_lock() */
1364 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1365 unsigned long event, void *ptr)
1367 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1368 struct dsa_switchdev_event_work *switchdev_work;
1370 if (!dsa_slave_dev_check(dev))
1371 return NOTIFY_DONE;
1373 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1374 if (!switchdev_work)
1375 return NOTIFY_BAD;
1377 INIT_WORK(&switchdev_work->work,
1378 dsa_slave_switchdev_event_work);
1379 switchdev_work->dev = dev;
1380 switchdev_work->event = event;
1382 switch (event) {
1383 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1384 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1385 if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1386 ptr))
1387 goto err_fdb_work_init;
1388 dev_hold(dev);
1389 break;
1390 default:
1391 kfree(switchdev_work);
1392 return NOTIFY_DONE;
1395 dsa_schedule_work(&switchdev_work->work);
1396 return NOTIFY_OK;
1398 err_fdb_work_init:
1399 kfree(switchdev_work);
1400 return NOTIFY_BAD;
1403 static struct notifier_block dsa_slave_nb __read_mostly = {
1404 .notifier_call = dsa_slave_netdevice_event,
1407 static struct notifier_block dsa_slave_switchdev_notifier = {
1408 .notifier_call = dsa_slave_switchdev_event,
1411 int dsa_slave_register_notifier(void)
1413 int err;
1415 err = register_netdevice_notifier(&dsa_slave_nb);
1416 if (err)
1417 return err;
1419 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1420 if (err)
1421 goto err_switchdev_nb;
1423 return 0;
1425 err_switchdev_nb:
1426 unregister_netdevice_notifier(&dsa_slave_nb);
1427 return err;
1430 void dsa_slave_unregister_notifier(void)
1432 int err;
1434 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1435 if (err)
1436 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1438 err = unregister_netdevice_notifier(&dsa_slave_nb);
1439 if (err)
1440 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);