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