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