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