]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - net/dsa/slave.c
Merge tag 'kconfig-v5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy...
[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 DSA_SKB_CB(skb)->deferred_xmit = false;
467
468 /* Identify PTP protocol packets, clone them, and pass them to the
469 * switch driver
470 */
471 dsa_skb_tx_timestamp(p, skb);
472
473 /* Transmit function may have to reallocate the original SKB,
474 * in which case it must have freed it. Only free it here on error.
475 */
476 nskb = p->xmit(skb, dev);
477 if (!nskb) {
478 if (!DSA_SKB_CB(skb)->deferred_xmit)
479 kfree_skb(skb);
480 return NETDEV_TX_OK;
481 }
482
483 return dsa_enqueue_skb(nskb, dev);
484 }
485
486 void *dsa_defer_xmit(struct sk_buff *skb, struct net_device *dev)
487 {
488 struct dsa_port *dp = dsa_slave_to_port(dev);
489
490 DSA_SKB_CB(skb)->deferred_xmit = true;
491
492 skb_queue_tail(&dp->xmit_queue, skb);
493 schedule_work(&dp->xmit_work);
494 return NULL;
495 }
496 EXPORT_SYMBOL_GPL(dsa_defer_xmit);
497
498 static void dsa_port_xmit_work(struct work_struct *work)
499 {
500 struct dsa_port *dp = container_of(work, struct dsa_port, xmit_work);
501 struct dsa_switch *ds = dp->ds;
502 struct sk_buff *skb;
503
504 if (unlikely(!ds->ops->port_deferred_xmit))
505 return;
506
507 while ((skb = skb_dequeue(&dp->xmit_queue)) != NULL)
508 ds->ops->port_deferred_xmit(ds, dp->index, skb);
509 }
510
511 /* ethtool operations *******************************************************/
512
513 static void dsa_slave_get_drvinfo(struct net_device *dev,
514 struct ethtool_drvinfo *drvinfo)
515 {
516 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
517 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
518 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
519 }
520
521 static int dsa_slave_get_regs_len(struct net_device *dev)
522 {
523 struct dsa_port *dp = dsa_slave_to_port(dev);
524 struct dsa_switch *ds = dp->ds;
525
526 if (ds->ops->get_regs_len)
527 return ds->ops->get_regs_len(ds, dp->index);
528
529 return -EOPNOTSUPP;
530 }
531
532 static void
533 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
534 {
535 struct dsa_port *dp = dsa_slave_to_port(dev);
536 struct dsa_switch *ds = dp->ds;
537
538 if (ds->ops->get_regs)
539 ds->ops->get_regs(ds, dp->index, regs, _p);
540 }
541
542 static int dsa_slave_nway_reset(struct net_device *dev)
543 {
544 struct dsa_port *dp = dsa_slave_to_port(dev);
545
546 return phylink_ethtool_nway_reset(dp->pl);
547 }
548
549 static int dsa_slave_get_eeprom_len(struct net_device *dev)
550 {
551 struct dsa_port *dp = dsa_slave_to_port(dev);
552 struct dsa_switch *ds = dp->ds;
553
554 if (ds->cd && ds->cd->eeprom_len)
555 return ds->cd->eeprom_len;
556
557 if (ds->ops->get_eeprom_len)
558 return ds->ops->get_eeprom_len(ds);
559
560 return 0;
561 }
562
563 static int dsa_slave_get_eeprom(struct net_device *dev,
564 struct ethtool_eeprom *eeprom, u8 *data)
565 {
566 struct dsa_port *dp = dsa_slave_to_port(dev);
567 struct dsa_switch *ds = dp->ds;
568
569 if (ds->ops->get_eeprom)
570 return ds->ops->get_eeprom(ds, eeprom, data);
571
572 return -EOPNOTSUPP;
573 }
574
575 static int dsa_slave_set_eeprom(struct net_device *dev,
576 struct ethtool_eeprom *eeprom, u8 *data)
577 {
578 struct dsa_port *dp = dsa_slave_to_port(dev);
579 struct dsa_switch *ds = dp->ds;
580
581 if (ds->ops->set_eeprom)
582 return ds->ops->set_eeprom(ds, eeprom, data);
583
584 return -EOPNOTSUPP;
585 }
586
587 static void dsa_slave_get_strings(struct net_device *dev,
588 uint32_t stringset, uint8_t *data)
589 {
590 struct dsa_port *dp = dsa_slave_to_port(dev);
591 struct dsa_switch *ds = dp->ds;
592
593 if (stringset == ETH_SS_STATS) {
594 int len = ETH_GSTRING_LEN;
595
596 strncpy(data, "tx_packets", len);
597 strncpy(data + len, "tx_bytes", len);
598 strncpy(data + 2 * len, "rx_packets", len);
599 strncpy(data + 3 * len, "rx_bytes", len);
600 if (ds->ops->get_strings)
601 ds->ops->get_strings(ds, dp->index, stringset,
602 data + 4 * len);
603 }
604 }
605
606 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
607 struct ethtool_stats *stats,
608 uint64_t *data)
609 {
610 struct dsa_port *dp = dsa_slave_to_port(dev);
611 struct dsa_slave_priv *p = netdev_priv(dev);
612 struct dsa_switch *ds = dp->ds;
613 struct pcpu_sw_netstats *s;
614 unsigned int start;
615 int i;
616
617 for_each_possible_cpu(i) {
618 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
619
620 s = per_cpu_ptr(p->stats64, i);
621 do {
622 start = u64_stats_fetch_begin_irq(&s->syncp);
623 tx_packets = s->tx_packets;
624 tx_bytes = s->tx_bytes;
625 rx_packets = s->rx_packets;
626 rx_bytes = s->rx_bytes;
627 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
628 data[0] += tx_packets;
629 data[1] += tx_bytes;
630 data[2] += rx_packets;
631 data[3] += rx_bytes;
632 }
633 if (ds->ops->get_ethtool_stats)
634 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
635 }
636
637 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
638 {
639 struct dsa_port *dp = dsa_slave_to_port(dev);
640 struct dsa_switch *ds = dp->ds;
641
642 if (sset == ETH_SS_STATS) {
643 int count;
644
645 count = 4;
646 if (ds->ops->get_sset_count)
647 count += ds->ops->get_sset_count(ds, dp->index, sset);
648
649 return count;
650 }
651
652 return -EOPNOTSUPP;
653 }
654
655 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
656 {
657 struct dsa_port *dp = dsa_slave_to_port(dev);
658 struct dsa_switch *ds = dp->ds;
659
660 phylink_ethtool_get_wol(dp->pl, w);
661
662 if (ds->ops->get_wol)
663 ds->ops->get_wol(ds, dp->index, w);
664 }
665
666 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
667 {
668 struct dsa_port *dp = dsa_slave_to_port(dev);
669 struct dsa_switch *ds = dp->ds;
670 int ret = -EOPNOTSUPP;
671
672 phylink_ethtool_set_wol(dp->pl, w);
673
674 if (ds->ops->set_wol)
675 ret = ds->ops->set_wol(ds, dp->index, w);
676
677 return ret;
678 }
679
680 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
681 {
682 struct dsa_port *dp = dsa_slave_to_port(dev);
683 struct dsa_switch *ds = dp->ds;
684 int ret;
685
686 /* Port's PHY and MAC both need to be EEE capable */
687 if (!dev->phydev || !dp->pl)
688 return -ENODEV;
689
690 if (!ds->ops->set_mac_eee)
691 return -EOPNOTSUPP;
692
693 ret = ds->ops->set_mac_eee(ds, dp->index, e);
694 if (ret)
695 return ret;
696
697 return phylink_ethtool_set_eee(dp->pl, e);
698 }
699
700 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
701 {
702 struct dsa_port *dp = dsa_slave_to_port(dev);
703 struct dsa_switch *ds = dp->ds;
704 int ret;
705
706 /* Port's PHY and MAC both need to be EEE capable */
707 if (!dev->phydev || !dp->pl)
708 return -ENODEV;
709
710 if (!ds->ops->get_mac_eee)
711 return -EOPNOTSUPP;
712
713 ret = ds->ops->get_mac_eee(ds, dp->index, e);
714 if (ret)
715 return ret;
716
717 return phylink_ethtool_get_eee(dp->pl, e);
718 }
719
720 static int dsa_slave_get_link_ksettings(struct net_device *dev,
721 struct ethtool_link_ksettings *cmd)
722 {
723 struct dsa_port *dp = dsa_slave_to_port(dev);
724
725 return phylink_ethtool_ksettings_get(dp->pl, cmd);
726 }
727
728 static int dsa_slave_set_link_ksettings(struct net_device *dev,
729 const struct ethtool_link_ksettings *cmd)
730 {
731 struct dsa_port *dp = dsa_slave_to_port(dev);
732
733 return phylink_ethtool_ksettings_set(dp->pl, cmd);
734 }
735
736 #ifdef CONFIG_NET_POLL_CONTROLLER
737 static int dsa_slave_netpoll_setup(struct net_device *dev,
738 struct netpoll_info *ni)
739 {
740 struct net_device *master = dsa_slave_to_master(dev);
741 struct dsa_slave_priv *p = netdev_priv(dev);
742 struct netpoll *netpoll;
743 int err = 0;
744
745 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
746 if (!netpoll)
747 return -ENOMEM;
748
749 err = __netpoll_setup(netpoll, master);
750 if (err) {
751 kfree(netpoll);
752 goto out;
753 }
754
755 p->netpoll = netpoll;
756 out:
757 return err;
758 }
759
760 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
761 {
762 struct dsa_slave_priv *p = netdev_priv(dev);
763 struct netpoll *netpoll = p->netpoll;
764
765 if (!netpoll)
766 return;
767
768 p->netpoll = NULL;
769
770 __netpoll_free(netpoll);
771 }
772
773 static void dsa_slave_poll_controller(struct net_device *dev)
774 {
775 }
776 #endif
777
778 static int dsa_slave_get_phys_port_name(struct net_device *dev,
779 char *name, size_t len)
780 {
781 struct dsa_port *dp = dsa_slave_to_port(dev);
782
783 /* For non-legacy ports, devlink is used and it takes
784 * care of the name generation. This ndo implementation
785 * should be removed with legacy support.
786 */
787 if (dp->ds->devlink)
788 return -EOPNOTSUPP;
789
790 if (snprintf(name, len, "p%d", dp->index) >= len)
791 return -EINVAL;
792
793 return 0;
794 }
795
796 static struct dsa_mall_tc_entry *
797 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
798 {
799 struct dsa_slave_priv *p = netdev_priv(dev);
800 struct dsa_mall_tc_entry *mall_tc_entry;
801
802 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
803 if (mall_tc_entry->cookie == cookie)
804 return mall_tc_entry;
805
806 return NULL;
807 }
808
809 static int dsa_slave_add_cls_matchall(struct net_device *dev,
810 struct tc_cls_matchall_offload *cls,
811 bool ingress)
812 {
813 struct dsa_port *dp = dsa_slave_to_port(dev);
814 struct dsa_slave_priv *p = netdev_priv(dev);
815 struct dsa_mall_tc_entry *mall_tc_entry;
816 __be16 protocol = cls->common.protocol;
817 struct dsa_switch *ds = dp->ds;
818 struct flow_action_entry *act;
819 struct dsa_port *to_dp;
820 int err = -EOPNOTSUPP;
821
822 if (!ds->ops->port_mirror_add)
823 return err;
824
825 if (!flow_offload_has_one_action(&cls->rule->action))
826 return err;
827
828 act = &cls->rule->action.entries[0];
829
830 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
831 struct dsa_mall_mirror_tc_entry *mirror;
832
833 if (!act->dev)
834 return -EINVAL;
835
836 if (!dsa_slave_dev_check(act->dev))
837 return -EOPNOTSUPP;
838
839 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
840 if (!mall_tc_entry)
841 return -ENOMEM;
842
843 mall_tc_entry->cookie = cls->cookie;
844 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
845 mirror = &mall_tc_entry->mirror;
846
847 to_dp = dsa_slave_to_port(act->dev);
848
849 mirror->to_local_port = to_dp->index;
850 mirror->ingress = ingress;
851
852 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
853 if (err) {
854 kfree(mall_tc_entry);
855 return err;
856 }
857
858 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
859 }
860
861 return 0;
862 }
863
864 static void dsa_slave_del_cls_matchall(struct net_device *dev,
865 struct tc_cls_matchall_offload *cls)
866 {
867 struct dsa_port *dp = dsa_slave_to_port(dev);
868 struct dsa_mall_tc_entry *mall_tc_entry;
869 struct dsa_switch *ds = dp->ds;
870
871 if (!ds->ops->port_mirror_del)
872 return;
873
874 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
875 if (!mall_tc_entry)
876 return;
877
878 list_del(&mall_tc_entry->list);
879
880 switch (mall_tc_entry->type) {
881 case DSA_PORT_MALL_MIRROR:
882 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
883 break;
884 default:
885 WARN_ON(1);
886 }
887
888 kfree(mall_tc_entry);
889 }
890
891 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
892 struct tc_cls_matchall_offload *cls,
893 bool ingress)
894 {
895 if (cls->common.chain_index)
896 return -EOPNOTSUPP;
897
898 switch (cls->command) {
899 case TC_CLSMATCHALL_REPLACE:
900 return dsa_slave_add_cls_matchall(dev, cls, ingress);
901 case TC_CLSMATCHALL_DESTROY:
902 dsa_slave_del_cls_matchall(dev, cls);
903 return 0;
904 default:
905 return -EOPNOTSUPP;
906 }
907 }
908
909 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
910 void *cb_priv, bool ingress)
911 {
912 struct net_device *dev = cb_priv;
913
914 if (!tc_can_offload(dev))
915 return -EOPNOTSUPP;
916
917 switch (type) {
918 case TC_SETUP_CLSMATCHALL:
919 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
920 default:
921 return -EOPNOTSUPP;
922 }
923 }
924
925 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
926 void *type_data, void *cb_priv)
927 {
928 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
929 }
930
931 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
932 void *type_data, void *cb_priv)
933 {
934 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
935 }
936
937 static int dsa_slave_setup_tc_block(struct net_device *dev,
938 struct tc_block_offload *f)
939 {
940 tc_setup_cb_t *cb;
941
942 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
943 cb = dsa_slave_setup_tc_block_cb_ig;
944 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
945 cb = dsa_slave_setup_tc_block_cb_eg;
946 else
947 return -EOPNOTSUPP;
948
949 switch (f->command) {
950 case TC_BLOCK_BIND:
951 return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
952 case TC_BLOCK_UNBIND:
953 tcf_block_cb_unregister(f->block, cb, dev);
954 return 0;
955 default:
956 return -EOPNOTSUPP;
957 }
958 }
959
960 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
961 void *type_data)
962 {
963 switch (type) {
964 case TC_SETUP_BLOCK:
965 return dsa_slave_setup_tc_block(dev, type_data);
966 default:
967 return -EOPNOTSUPP;
968 }
969 }
970
971 static void dsa_slave_get_stats64(struct net_device *dev,
972 struct rtnl_link_stats64 *stats)
973 {
974 struct dsa_slave_priv *p = netdev_priv(dev);
975 struct pcpu_sw_netstats *s;
976 unsigned int start;
977 int i;
978
979 netdev_stats_to_stats64(stats, &dev->stats);
980 for_each_possible_cpu(i) {
981 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
982
983 s = per_cpu_ptr(p->stats64, i);
984 do {
985 start = u64_stats_fetch_begin_irq(&s->syncp);
986 tx_packets = s->tx_packets;
987 tx_bytes = s->tx_bytes;
988 rx_packets = s->rx_packets;
989 rx_bytes = s->rx_bytes;
990 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
991
992 stats->tx_packets += tx_packets;
993 stats->tx_bytes += tx_bytes;
994 stats->rx_packets += rx_packets;
995 stats->rx_bytes += rx_bytes;
996 }
997 }
998
999 static int dsa_slave_get_rxnfc(struct net_device *dev,
1000 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1001 {
1002 struct dsa_port *dp = dsa_slave_to_port(dev);
1003 struct dsa_switch *ds = dp->ds;
1004
1005 if (!ds->ops->get_rxnfc)
1006 return -EOPNOTSUPP;
1007
1008 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1009 }
1010
1011 static int dsa_slave_set_rxnfc(struct net_device *dev,
1012 struct ethtool_rxnfc *nfc)
1013 {
1014 struct dsa_port *dp = dsa_slave_to_port(dev);
1015 struct dsa_switch *ds = dp->ds;
1016
1017 if (!ds->ops->set_rxnfc)
1018 return -EOPNOTSUPP;
1019
1020 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1021 }
1022
1023 static int dsa_slave_get_ts_info(struct net_device *dev,
1024 struct ethtool_ts_info *ts)
1025 {
1026 struct dsa_slave_priv *p = netdev_priv(dev);
1027 struct dsa_switch *ds = p->dp->ds;
1028
1029 if (!ds->ops->get_ts_info)
1030 return -EOPNOTSUPP;
1031
1032 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1033 }
1034
1035 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1036 u16 vid)
1037 {
1038 struct dsa_port *dp = dsa_slave_to_port(dev);
1039 struct bridge_vlan_info info;
1040 int ret;
1041
1042 /* Check for a possible bridge VLAN entry now since there is no
1043 * need to emulate the switchdev prepare + commit phase.
1044 */
1045 if (dp->bridge_dev) {
1046 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1047 * device, respectively the VID is not found, returning
1048 * 0 means success, which is a failure for us here.
1049 */
1050 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1051 if (ret == 0)
1052 return -EBUSY;
1053 }
1054
1055 /* This API only allows programming tagged, non-PVID VIDs */
1056 return dsa_port_vid_add(dp, vid, 0);
1057 }
1058
1059 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1060 u16 vid)
1061 {
1062 struct dsa_port *dp = dsa_slave_to_port(dev);
1063 struct bridge_vlan_info info;
1064 int ret;
1065
1066 /* Check for a possible bridge VLAN entry now since there is no
1067 * need to emulate the switchdev prepare + commit phase.
1068 */
1069 if (dp->bridge_dev) {
1070 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1071 * device, respectively the VID is not found, returning
1072 * 0 means success, which is a failure for us here.
1073 */
1074 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1075 if (ret == 0)
1076 return -EBUSY;
1077 }
1078
1079 ret = dsa_port_vid_del(dp, vid);
1080 if (ret == -EOPNOTSUPP)
1081 ret = 0;
1082
1083 return ret;
1084 }
1085
1086 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1087 .get_drvinfo = dsa_slave_get_drvinfo,
1088 .get_regs_len = dsa_slave_get_regs_len,
1089 .get_regs = dsa_slave_get_regs,
1090 .nway_reset = dsa_slave_nway_reset,
1091 .get_link = ethtool_op_get_link,
1092 .get_eeprom_len = dsa_slave_get_eeprom_len,
1093 .get_eeprom = dsa_slave_get_eeprom,
1094 .set_eeprom = dsa_slave_set_eeprom,
1095 .get_strings = dsa_slave_get_strings,
1096 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1097 .get_sset_count = dsa_slave_get_sset_count,
1098 .set_wol = dsa_slave_set_wol,
1099 .get_wol = dsa_slave_get_wol,
1100 .set_eee = dsa_slave_set_eee,
1101 .get_eee = dsa_slave_get_eee,
1102 .get_link_ksettings = dsa_slave_get_link_ksettings,
1103 .set_link_ksettings = dsa_slave_set_link_ksettings,
1104 .get_rxnfc = dsa_slave_get_rxnfc,
1105 .set_rxnfc = dsa_slave_set_rxnfc,
1106 .get_ts_info = dsa_slave_get_ts_info,
1107 };
1108
1109 /* legacy way, bypassing the bridge *****************************************/
1110 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1111 struct net_device *dev,
1112 const unsigned char *addr, u16 vid,
1113 u16 flags,
1114 struct netlink_ext_ack *extack)
1115 {
1116 struct dsa_port *dp = dsa_slave_to_port(dev);
1117
1118 return dsa_port_fdb_add(dp, addr, vid);
1119 }
1120
1121 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1122 struct net_device *dev,
1123 const unsigned char *addr, u16 vid)
1124 {
1125 struct dsa_port *dp = dsa_slave_to_port(dev);
1126
1127 return dsa_port_fdb_del(dp, addr, vid);
1128 }
1129
1130 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1131 {
1132 struct dsa_port *dp = dsa_slave_to_port(dev);
1133
1134 return dp->ds->devlink ? &dp->devlink_port : NULL;
1135 }
1136
1137 static const struct net_device_ops dsa_slave_netdev_ops = {
1138 .ndo_open = dsa_slave_open,
1139 .ndo_stop = dsa_slave_close,
1140 .ndo_start_xmit = dsa_slave_xmit,
1141 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1142 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1143 .ndo_set_mac_address = dsa_slave_set_mac_address,
1144 .ndo_fdb_add = dsa_legacy_fdb_add,
1145 .ndo_fdb_del = dsa_legacy_fdb_del,
1146 .ndo_fdb_dump = dsa_slave_fdb_dump,
1147 .ndo_do_ioctl = dsa_slave_ioctl,
1148 .ndo_get_iflink = dsa_slave_get_iflink,
1149 #ifdef CONFIG_NET_POLL_CONTROLLER
1150 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1151 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1152 .ndo_poll_controller = dsa_slave_poll_controller,
1153 #endif
1154 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1155 .ndo_setup_tc = dsa_slave_setup_tc,
1156 .ndo_get_stats64 = dsa_slave_get_stats64,
1157 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1158 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1159 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1160 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1161 };
1162
1163 static struct device_type dsa_type = {
1164 .name = "dsa",
1165 };
1166
1167 static void dsa_slave_phylink_validate(struct net_device *dev,
1168 unsigned long *supported,
1169 struct phylink_link_state *state)
1170 {
1171 struct dsa_port *dp = dsa_slave_to_port(dev);
1172 struct dsa_switch *ds = dp->ds;
1173
1174 if (!ds->ops->phylink_validate)
1175 return;
1176
1177 ds->ops->phylink_validate(ds, dp->index, supported, state);
1178 }
1179
1180 static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1181 struct phylink_link_state *state)
1182 {
1183 struct dsa_port *dp = dsa_slave_to_port(dev);
1184 struct dsa_switch *ds = dp->ds;
1185
1186 /* Only called for SGMII and 802.3z */
1187 if (!ds->ops->phylink_mac_link_state)
1188 return -EOPNOTSUPP;
1189
1190 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1191 }
1192
1193 static void dsa_slave_phylink_mac_config(struct net_device *dev,
1194 unsigned int mode,
1195 const struct phylink_link_state *state)
1196 {
1197 struct dsa_port *dp = dsa_slave_to_port(dev);
1198 struct dsa_switch *ds = dp->ds;
1199
1200 if (!ds->ops->phylink_mac_config)
1201 return;
1202
1203 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1204 }
1205
1206 static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1207 {
1208 struct dsa_port *dp = dsa_slave_to_port(dev);
1209 struct dsa_switch *ds = dp->ds;
1210
1211 if (!ds->ops->phylink_mac_an_restart)
1212 return;
1213
1214 ds->ops->phylink_mac_an_restart(ds, dp->index);
1215 }
1216
1217 static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1218 unsigned int mode,
1219 phy_interface_t interface)
1220 {
1221 struct dsa_port *dp = dsa_slave_to_port(dev);
1222 struct dsa_switch *ds = dp->ds;
1223
1224 if (!ds->ops->phylink_mac_link_down) {
1225 if (ds->ops->adjust_link && dev->phydev)
1226 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1227 return;
1228 }
1229
1230 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1231 }
1232
1233 static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1234 unsigned int mode,
1235 phy_interface_t interface,
1236 struct phy_device *phydev)
1237 {
1238 struct dsa_port *dp = dsa_slave_to_port(dev);
1239 struct dsa_switch *ds = dp->ds;
1240
1241 if (!ds->ops->phylink_mac_link_up) {
1242 if (ds->ops->adjust_link && dev->phydev)
1243 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1244 return;
1245 }
1246
1247 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1248 }
1249
1250 static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1251 .validate = dsa_slave_phylink_validate,
1252 .mac_link_state = dsa_slave_phylink_mac_link_state,
1253 .mac_config = dsa_slave_phylink_mac_config,
1254 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1255 .mac_link_down = dsa_slave_phylink_mac_link_down,
1256 .mac_link_up = dsa_slave_phylink_mac_link_up,
1257 };
1258
1259 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1260 {
1261 const struct dsa_port *dp = dsa_to_port(ds, port);
1262
1263 phylink_mac_change(dp->pl, up);
1264 }
1265 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1266
1267 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1268 struct phylink_link_state *state)
1269 {
1270 struct dsa_port *dp = dsa_slave_to_port(dev);
1271 struct dsa_switch *ds = dp->ds;
1272
1273 /* No need to check that this operation is valid, the callback would
1274 * not be called if it was not.
1275 */
1276 ds->ops->phylink_fixed_state(ds, dp->index, state);
1277 }
1278
1279 /* slave device setup *******************************************************/
1280 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1281 {
1282 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1283 struct dsa_switch *ds = dp->ds;
1284
1285 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1286 if (!slave_dev->phydev) {
1287 netdev_err(slave_dev, "no phy at %d\n", addr);
1288 return -ENODEV;
1289 }
1290
1291 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1292 }
1293
1294 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1295 {
1296 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1297 struct device_node *port_dn = dp->dn;
1298 struct dsa_switch *ds = dp->ds;
1299 u32 phy_flags = 0;
1300 int mode, ret;
1301
1302 mode = of_get_phy_mode(port_dn);
1303 if (mode < 0)
1304 mode = PHY_INTERFACE_MODE_NA;
1305
1306 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1307 &dsa_slave_phylink_mac_ops);
1308 if (IS_ERR(dp->pl)) {
1309 netdev_err(slave_dev,
1310 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1311 return PTR_ERR(dp->pl);
1312 }
1313
1314 /* Register only if the switch provides such a callback, since this
1315 * callback takes precedence over polling the link GPIO in PHYLINK
1316 * (see phylink_get_fixed_state).
1317 */
1318 if (ds->ops->phylink_fixed_state)
1319 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1320
1321 if (ds->ops->get_phy_flags)
1322 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1323
1324 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1325 if (ret == -ENODEV && ds->slave_mii_bus) {
1326 /* We could not connect to a designated PHY or SFP, so try to
1327 * use the switch internal MDIO bus instead
1328 */
1329 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1330 if (ret) {
1331 netdev_err(slave_dev,
1332 "failed to connect to port %d: %d\n",
1333 dp->index, ret);
1334 phylink_destroy(dp->pl);
1335 return ret;
1336 }
1337 }
1338
1339 return ret;
1340 }
1341
1342 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1343 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1344 struct netdev_queue *txq,
1345 void *_unused)
1346 {
1347 lockdep_set_class(&txq->_xmit_lock,
1348 &dsa_slave_netdev_xmit_lock_key);
1349 }
1350
1351 int dsa_slave_suspend(struct net_device *slave_dev)
1352 {
1353 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1354
1355 if (!netif_running(slave_dev))
1356 return 0;
1357
1358 cancel_work_sync(&dp->xmit_work);
1359 skb_queue_purge(&dp->xmit_queue);
1360
1361 netif_device_detach(slave_dev);
1362
1363 rtnl_lock();
1364 phylink_stop(dp->pl);
1365 rtnl_unlock();
1366
1367 return 0;
1368 }
1369
1370 int dsa_slave_resume(struct net_device *slave_dev)
1371 {
1372 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1373
1374 if (!netif_running(slave_dev))
1375 return 0;
1376
1377 netif_device_attach(slave_dev);
1378
1379 rtnl_lock();
1380 phylink_start(dp->pl);
1381 rtnl_unlock();
1382
1383 return 0;
1384 }
1385
1386 static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1387 {
1388 struct net_device *master = dsa_slave_to_master(dev);
1389 struct dsa_port *dp = dsa_slave_to_port(dev);
1390 struct dsa_notifier_register_info rinfo = {
1391 .switch_number = dp->ds->index,
1392 .port_number = dp->index,
1393 .master = master,
1394 .info.dev = dev,
1395 };
1396
1397 call_dsa_notifiers(val, dev, &rinfo.info);
1398 }
1399
1400 int dsa_slave_create(struct dsa_port *port)
1401 {
1402 const struct dsa_port *cpu_dp = port->cpu_dp;
1403 struct net_device *master = cpu_dp->master;
1404 struct dsa_switch *ds = port->ds;
1405 const char *name = port->name;
1406 struct net_device *slave_dev;
1407 struct dsa_slave_priv *p;
1408 int ret;
1409
1410 if (!ds->num_tx_queues)
1411 ds->num_tx_queues = 1;
1412
1413 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1414 NET_NAME_UNKNOWN, ether_setup,
1415 ds->num_tx_queues, 1);
1416 if (slave_dev == NULL)
1417 return -ENOMEM;
1418
1419 slave_dev->features = master->vlan_features | NETIF_F_HW_TC |
1420 NETIF_F_HW_VLAN_CTAG_FILTER;
1421 slave_dev->hw_features |= NETIF_F_HW_TC;
1422 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1423 if (!IS_ERR_OR_NULL(port->mac))
1424 ether_addr_copy(slave_dev->dev_addr, port->mac);
1425 else
1426 eth_hw_addr_inherit(slave_dev, master);
1427 slave_dev->priv_flags |= IFF_NO_QUEUE;
1428 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1429 slave_dev->min_mtu = 0;
1430 slave_dev->max_mtu = ETH_MAX_MTU;
1431 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1432
1433 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1434 NULL);
1435
1436 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1437 slave_dev->dev.of_node = port->dn;
1438 slave_dev->vlan_features = master->vlan_features;
1439
1440 p = netdev_priv(slave_dev);
1441 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1442 if (!p->stats64) {
1443 free_netdev(slave_dev);
1444 return -ENOMEM;
1445 }
1446 p->dp = port;
1447 INIT_LIST_HEAD(&p->mall_tc_list);
1448 INIT_WORK(&port->xmit_work, dsa_port_xmit_work);
1449 skb_queue_head_init(&port->xmit_queue);
1450 p->xmit = cpu_dp->tag_ops->xmit;
1451 port->slave = slave_dev;
1452
1453 netif_carrier_off(slave_dev);
1454
1455 ret = dsa_slave_phy_setup(slave_dev);
1456 if (ret) {
1457 netdev_err(master, "error %d setting up slave phy\n", ret);
1458 goto out_free;
1459 }
1460
1461 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1462
1463 ret = register_netdev(slave_dev);
1464 if (ret) {
1465 netdev_err(master, "error %d registering interface %s\n",
1466 ret, slave_dev->name);
1467 goto out_phy;
1468 }
1469
1470 return 0;
1471
1472 out_phy:
1473 rtnl_lock();
1474 phylink_disconnect_phy(p->dp->pl);
1475 rtnl_unlock();
1476 phylink_destroy(p->dp->pl);
1477 out_free:
1478 free_percpu(p->stats64);
1479 free_netdev(slave_dev);
1480 port->slave = NULL;
1481 return ret;
1482 }
1483
1484 void dsa_slave_destroy(struct net_device *slave_dev)
1485 {
1486 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1487 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1488
1489 netif_carrier_off(slave_dev);
1490 rtnl_lock();
1491 phylink_disconnect_phy(dp->pl);
1492 rtnl_unlock();
1493
1494 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1495 unregister_netdev(slave_dev);
1496 phylink_destroy(dp->pl);
1497 free_percpu(p->stats64);
1498 free_netdev(slave_dev);
1499 }
1500
1501 static bool dsa_slave_dev_check(struct net_device *dev)
1502 {
1503 return dev->netdev_ops == &dsa_slave_netdev_ops;
1504 }
1505
1506 static int dsa_slave_changeupper(struct net_device *dev,
1507 struct netdev_notifier_changeupper_info *info)
1508 {
1509 struct dsa_port *dp = dsa_slave_to_port(dev);
1510 int err = NOTIFY_DONE;
1511
1512 if (netif_is_bridge_master(info->upper_dev)) {
1513 if (info->linking) {
1514 err = dsa_port_bridge_join(dp, info->upper_dev);
1515 err = notifier_from_errno(err);
1516 } else {
1517 dsa_port_bridge_leave(dp, info->upper_dev);
1518 err = NOTIFY_OK;
1519 }
1520 }
1521
1522 return err;
1523 }
1524
1525 static int dsa_slave_upper_vlan_check(struct net_device *dev,
1526 struct netdev_notifier_changeupper_info *
1527 info)
1528 {
1529 struct netlink_ext_ack *ext_ack;
1530 struct net_device *slave;
1531 struct dsa_port *dp;
1532
1533 ext_ack = netdev_notifier_info_to_extack(&info->info);
1534
1535 if (!is_vlan_dev(dev))
1536 return NOTIFY_DONE;
1537
1538 slave = vlan_dev_real_dev(dev);
1539 if (!dsa_slave_dev_check(slave))
1540 return NOTIFY_DONE;
1541
1542 dp = dsa_slave_to_port(slave);
1543 if (!dp->bridge_dev)
1544 return NOTIFY_DONE;
1545
1546 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1547 if (br_vlan_enabled(dp->bridge_dev) &&
1548 netif_is_bridge_master(info->upper_dev) && info->linking) {
1549 NL_SET_ERR_MSG_MOD(ext_ack,
1550 "Cannot enslave VLAN device into VLAN aware bridge");
1551 return notifier_from_errno(-EINVAL);
1552 }
1553
1554 return NOTIFY_DONE;
1555 }
1556
1557 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1558 unsigned long event, void *ptr)
1559 {
1560 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1561
1562 if (event == NETDEV_CHANGEUPPER) {
1563 if (!dsa_slave_dev_check(dev))
1564 return dsa_slave_upper_vlan_check(dev, ptr);
1565
1566 return dsa_slave_changeupper(dev, ptr);
1567 }
1568
1569 return NOTIFY_DONE;
1570 }
1571
1572 static int
1573 dsa_slave_switchdev_port_attr_set_event(struct net_device *netdev,
1574 struct switchdev_notifier_port_attr_info *port_attr_info)
1575 {
1576 int err;
1577
1578 err = dsa_slave_port_attr_set(netdev, port_attr_info->attr,
1579 port_attr_info->trans);
1580
1581 port_attr_info->handled = true;
1582 return notifier_from_errno(err);
1583 }
1584
1585 struct dsa_switchdev_event_work {
1586 struct work_struct work;
1587 struct switchdev_notifier_fdb_info fdb_info;
1588 struct net_device *dev;
1589 unsigned long event;
1590 };
1591
1592 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1593 {
1594 struct dsa_switchdev_event_work *switchdev_work =
1595 container_of(work, struct dsa_switchdev_event_work, work);
1596 struct net_device *dev = switchdev_work->dev;
1597 struct switchdev_notifier_fdb_info *fdb_info;
1598 struct dsa_port *dp = dsa_slave_to_port(dev);
1599 int err;
1600
1601 rtnl_lock();
1602 switch (switchdev_work->event) {
1603 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1604 fdb_info = &switchdev_work->fdb_info;
1605 if (!fdb_info->added_by_user)
1606 break;
1607
1608 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1609 if (err) {
1610 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1611 break;
1612 }
1613 fdb_info->offloaded = true;
1614 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1615 &fdb_info->info, NULL);
1616 break;
1617
1618 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1619 fdb_info = &switchdev_work->fdb_info;
1620 if (!fdb_info->added_by_user)
1621 break;
1622
1623 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1624 if (err) {
1625 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1626 dev_close(dev);
1627 }
1628 break;
1629 }
1630 rtnl_unlock();
1631
1632 kfree(switchdev_work->fdb_info.addr);
1633 kfree(switchdev_work);
1634 dev_put(dev);
1635 }
1636
1637 static int
1638 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1639 switchdev_work,
1640 const struct switchdev_notifier_fdb_info *
1641 fdb_info)
1642 {
1643 memcpy(&switchdev_work->fdb_info, fdb_info,
1644 sizeof(switchdev_work->fdb_info));
1645 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1646 if (!switchdev_work->fdb_info.addr)
1647 return -ENOMEM;
1648 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1649 fdb_info->addr);
1650 return 0;
1651 }
1652
1653 /* Called under rcu_read_lock() */
1654 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1655 unsigned long event, void *ptr)
1656 {
1657 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1658 struct dsa_switchdev_event_work *switchdev_work;
1659
1660 if (!dsa_slave_dev_check(dev))
1661 return NOTIFY_DONE;
1662
1663 if (event == SWITCHDEV_PORT_ATTR_SET)
1664 return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
1665
1666 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1667 if (!switchdev_work)
1668 return NOTIFY_BAD;
1669
1670 INIT_WORK(&switchdev_work->work,
1671 dsa_slave_switchdev_event_work);
1672 switchdev_work->dev = dev;
1673 switchdev_work->event = event;
1674
1675 switch (event) {
1676 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1677 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1678 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1679 goto err_fdb_work_init;
1680 dev_hold(dev);
1681 break;
1682 default:
1683 kfree(switchdev_work);
1684 return NOTIFY_DONE;
1685 }
1686
1687 dsa_schedule_work(&switchdev_work->work);
1688 return NOTIFY_OK;
1689
1690 err_fdb_work_init:
1691 kfree(switchdev_work);
1692 return NOTIFY_BAD;
1693 }
1694
1695 static int
1696 dsa_slave_switchdev_port_obj_event(unsigned long event,
1697 struct net_device *netdev,
1698 struct switchdev_notifier_port_obj_info *port_obj_info)
1699 {
1700 int err = -EOPNOTSUPP;
1701
1702 switch (event) {
1703 case SWITCHDEV_PORT_OBJ_ADD:
1704 err = dsa_slave_port_obj_add(netdev, port_obj_info->obj,
1705 port_obj_info->trans);
1706 break;
1707 case SWITCHDEV_PORT_OBJ_DEL:
1708 err = dsa_slave_port_obj_del(netdev, port_obj_info->obj);
1709 break;
1710 }
1711
1712 port_obj_info->handled = true;
1713 return notifier_from_errno(err);
1714 }
1715
1716 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1717 unsigned long event, void *ptr)
1718 {
1719 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1720
1721 if (!dsa_slave_dev_check(dev))
1722 return NOTIFY_DONE;
1723
1724 switch (event) {
1725 case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1726 case SWITCHDEV_PORT_OBJ_DEL:
1727 return dsa_slave_switchdev_port_obj_event(event, dev, ptr);
1728 case SWITCHDEV_PORT_ATTR_SET:
1729 return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
1730 }
1731
1732 return NOTIFY_DONE;
1733 }
1734
1735 static struct notifier_block dsa_slave_nb __read_mostly = {
1736 .notifier_call = dsa_slave_netdevice_event,
1737 };
1738
1739 static struct notifier_block dsa_slave_switchdev_notifier = {
1740 .notifier_call = dsa_slave_switchdev_event,
1741 };
1742
1743 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1744 .notifier_call = dsa_slave_switchdev_blocking_event,
1745 };
1746
1747 int dsa_slave_register_notifier(void)
1748 {
1749 struct notifier_block *nb;
1750 int err;
1751
1752 err = register_netdevice_notifier(&dsa_slave_nb);
1753 if (err)
1754 return err;
1755
1756 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1757 if (err)
1758 goto err_switchdev_nb;
1759
1760 nb = &dsa_slave_switchdev_blocking_notifier;
1761 err = register_switchdev_blocking_notifier(nb);
1762 if (err)
1763 goto err_switchdev_blocking_nb;
1764
1765 return 0;
1766
1767 err_switchdev_blocking_nb:
1768 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1769 err_switchdev_nb:
1770 unregister_netdevice_notifier(&dsa_slave_nb);
1771 return err;
1772 }
1773
1774 void dsa_slave_unregister_notifier(void)
1775 {
1776 struct notifier_block *nb;
1777 int err;
1778
1779 nb = &dsa_slave_switchdev_blocking_notifier;
1780 err = unregister_switchdev_blocking_notifier(nb);
1781 if (err)
1782 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1783
1784 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1785 if (err)
1786 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1787
1788 err = unregister_netdevice_notifier(&dsa_slave_nb);
1789 if (err)
1790 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1791 }