1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/dsa/slave.c - Slave device handling
4 * Copyright (c) 2008-2009 Marvell Semiconductor
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 /* slave mii_bus handling ***************************************************/
dsa_slave_phy_read(struct mii_bus * bus,int addr,int reg)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
dsa_slave_phy_write(struct mii_bus * bus,int addr,int reg,u16 val)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
dsa_slave_mii_bus_init(struct dsa_switch * ds)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 /* slave device handling ****************************************************/
dsa_slave_get_iflink(const struct net_device * dev)62 static int dsa_slave_get_iflink(const struct net_device *dev)
63 {
64 return dsa_slave_to_master(dev)->ifindex;
65 }
66
dsa_slave_open(struct net_device * dev)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
dsa_slave_close(struct net_device * dev)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
dsa_slave_change_rx_flags(struct net_device * dev,int change)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
dsa_slave_set_rx_mode(struct net_device * dev)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
dsa_slave_set_mac_address(struct net_device * dev,void * a)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
dsa_slave_port_fdb_do_dump(const unsigned char * addr,u16 vid,bool is_static,void * data)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
dsa_slave_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,struct net_device * filter_dev,int * idx)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
dsa_slave_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)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 /* Pass through to switch driver if it supports timestamping */
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
dsa_slave_port_attr_set(struct net_device * dev,const struct switchdev_attr * attr,struct switchdev_trans * trans)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
dsa_slave_vlan_add(struct net_device * dev,const struct switchdev_obj * obj,struct switchdev_trans * trans)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 /* We need the dedicated CPU port to be a member of the VLAN as well.
332 * Even though drivers often handle CPU membership in special ways,
333 * it doesn't make sense to program a PVID, so clear this flag.
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
dsa_slave_port_obj_add(struct net_device * dev,const struct switchdev_obj * obj,struct switchdev_trans * trans,struct netlink_ext_ack * extack)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 /* For the prepare phase, ensure the full set of changes is feasable in
353 * one go in order to signal a failure properly. If an operation is not
354 * supported, return -EOPNOTSUPP.
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 /* DSA can directly translate this to a normal MDB add,
365 * but on the CPU port.
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
dsa_slave_vlan_del(struct net_device * dev,const struct switchdev_obj * obj)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 /* Do not deprogram the CPU port as it may be shared with other user
393 * ports which can be members of this VLAN as well.
394 */
395 return dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
396 }
397
dsa_slave_port_obj_del(struct net_device * dev,const struct switchdev_obj * obj)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 /* DSA can directly translate this to a normal MDB add,
412 * but on the CPU port.
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
dsa_slave_get_port_parent_id(struct net_device * dev,struct netdev_phys_item_id * ppid)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 /* For non-legacy ports, devlink is used and it takes
435 * care of the name generation. This ndo implementation
436 * should be removed with legacy support.
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
dsa_slave_netpoll_send_skb(struct net_device * dev,struct sk_buff * skb)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
dsa_skb_tx_timestamp(struct dsa_slave_priv * p,struct sk_buff * skb)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
dsa_enqueue_skb(struct sk_buff * skb,struct net_device * dev)487 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
488 {
489 /* SKB for netpoll still need to be mangled with the protocol-specific
490 * tag to be successfully transmitted
491 */
492 if (unlikely(netpoll_tx_running(dev)))
493 return dsa_slave_netpoll_send_skb(dev, skb);
494
495 /* Queue the SKB for transmission on the parent interface, but
496 * do not modify its EtherType
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
dsa_slave_xmit(struct sk_buff * skb,struct net_device * dev)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 /* Identify PTP protocol packets, clone them, and pass them to the
521 * switch driver
522 */
523 dsa_skb_tx_timestamp(p, skb);
524
525 /* Transmit function may have to reallocate the original SKB,
526 * in which case it must have freed it. Only free it here on error.
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
dsa_defer_xmit(struct sk_buff * skb,struct net_device * dev)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
dsa_port_xmit_work(struct work_struct * work)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 /* ethtool operations *******************************************************/
564
dsa_slave_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)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
dsa_slave_get_regs_len(struct net_device * dev)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
dsa_slave_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * _p)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
dsa_slave_nway_reset(struct net_device * dev)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
dsa_slave_get_eeprom_len(struct net_device * dev)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
dsa_slave_get_eeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)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
dsa_slave_set_eeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)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
dsa_slave_get_strings(struct net_device * dev,uint32_t stringset,uint8_t * data)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
dsa_slave_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,uint64_t * data)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
dsa_slave_get_sset_count(struct net_device * dev,int sset)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 = 0;
696
697 if (ds->ops->get_sset_count) {
698 count = ds->ops->get_sset_count(ds, dp->index, sset);
699 if (count < 0)
700 return count;
701 }
702
703 return count + 4;
704 }
705
706 return -EOPNOTSUPP;
707 }
708
dsa_slave_get_wol(struct net_device * dev,struct ethtool_wolinfo * w)709 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
710 {
711 struct dsa_port *dp = dsa_slave_to_port(dev);
712 struct dsa_switch *ds = dp->ds;
713
714 phylink_ethtool_get_wol(dp->pl, w);
715
716 if (ds->ops->get_wol)
717 ds->ops->get_wol(ds, dp->index, w);
718 }
719
dsa_slave_set_wol(struct net_device * dev,struct ethtool_wolinfo * w)720 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
721 {
722 struct dsa_port *dp = dsa_slave_to_port(dev);
723 struct dsa_switch *ds = dp->ds;
724 int ret = -EOPNOTSUPP;
725
726 phylink_ethtool_set_wol(dp->pl, w);
727
728 if (ds->ops->set_wol)
729 ret = ds->ops->set_wol(ds, dp->index, w);
730
731 return ret;
732 }
733
dsa_slave_set_eee(struct net_device * dev,struct ethtool_eee * e)734 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
735 {
736 struct dsa_port *dp = dsa_slave_to_port(dev);
737 struct dsa_switch *ds = dp->ds;
738 int ret;
739
740 /* Port's PHY and MAC both need to be EEE capable */
741 if (!dev->phydev || !dp->pl)
742 return -ENODEV;
743
744 if (!ds->ops->set_mac_eee)
745 return -EOPNOTSUPP;
746
747 ret = ds->ops->set_mac_eee(ds, dp->index, e);
748 if (ret)
749 return ret;
750
751 return phylink_ethtool_set_eee(dp->pl, e);
752 }
753
dsa_slave_get_eee(struct net_device * dev,struct ethtool_eee * e)754 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
755 {
756 struct dsa_port *dp = dsa_slave_to_port(dev);
757 struct dsa_switch *ds = dp->ds;
758 int ret;
759
760 /* Port's PHY and MAC both need to be EEE capable */
761 if (!dev->phydev || !dp->pl)
762 return -ENODEV;
763
764 if (!ds->ops->get_mac_eee)
765 return -EOPNOTSUPP;
766
767 ret = ds->ops->get_mac_eee(ds, dp->index, e);
768 if (ret)
769 return ret;
770
771 return phylink_ethtool_get_eee(dp->pl, e);
772 }
773
dsa_slave_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)774 static int dsa_slave_get_link_ksettings(struct net_device *dev,
775 struct ethtool_link_ksettings *cmd)
776 {
777 struct dsa_port *dp = dsa_slave_to_port(dev);
778
779 return phylink_ethtool_ksettings_get(dp->pl, cmd);
780 }
781
dsa_slave_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)782 static int dsa_slave_set_link_ksettings(struct net_device *dev,
783 const struct ethtool_link_ksettings *cmd)
784 {
785 struct dsa_port *dp = dsa_slave_to_port(dev);
786
787 return phylink_ethtool_ksettings_set(dp->pl, cmd);
788 }
789
790 #ifdef CONFIG_NET_POLL_CONTROLLER
dsa_slave_netpoll_setup(struct net_device * dev,struct netpoll_info * ni)791 static int dsa_slave_netpoll_setup(struct net_device *dev,
792 struct netpoll_info *ni)
793 {
794 struct net_device *master = dsa_slave_to_master(dev);
795 struct dsa_slave_priv *p = netdev_priv(dev);
796 struct netpoll *netpoll;
797 int err = 0;
798
799 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
800 if (!netpoll)
801 return -ENOMEM;
802
803 err = __netpoll_setup(netpoll, master);
804 if (err) {
805 kfree(netpoll);
806 goto out;
807 }
808
809 p->netpoll = netpoll;
810 out:
811 return err;
812 }
813
dsa_slave_netpoll_cleanup(struct net_device * dev)814 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
815 {
816 struct dsa_slave_priv *p = netdev_priv(dev);
817 struct netpoll *netpoll = p->netpoll;
818
819 if (!netpoll)
820 return;
821
822 p->netpoll = NULL;
823
824 __netpoll_free(netpoll);
825 }
826
dsa_slave_poll_controller(struct net_device * dev)827 static void dsa_slave_poll_controller(struct net_device *dev)
828 {
829 }
830 #endif
831
dsa_slave_get_phys_port_name(struct net_device * dev,char * name,size_t len)832 static int dsa_slave_get_phys_port_name(struct net_device *dev,
833 char *name, size_t len)
834 {
835 struct dsa_port *dp = dsa_slave_to_port(dev);
836
837 /* For non-legacy ports, devlink is used and it takes
838 * care of the name generation. This ndo implementation
839 * should be removed with legacy support.
840 */
841 if (dp->ds->devlink)
842 return -EOPNOTSUPP;
843
844 if (snprintf(name, len, "p%d", dp->index) >= len)
845 return -EINVAL;
846
847 return 0;
848 }
849
850 static struct dsa_mall_tc_entry *
dsa_slave_mall_tc_entry_find(struct net_device * dev,unsigned long cookie)851 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
852 {
853 struct dsa_slave_priv *p = netdev_priv(dev);
854 struct dsa_mall_tc_entry *mall_tc_entry;
855
856 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
857 if (mall_tc_entry->cookie == cookie)
858 return mall_tc_entry;
859
860 return NULL;
861 }
862
dsa_slave_add_cls_matchall(struct net_device * dev,struct tc_cls_matchall_offload * cls,bool ingress)863 static int dsa_slave_add_cls_matchall(struct net_device *dev,
864 struct tc_cls_matchall_offload *cls,
865 bool ingress)
866 {
867 struct dsa_port *dp = dsa_slave_to_port(dev);
868 struct dsa_slave_priv *p = netdev_priv(dev);
869 struct dsa_mall_tc_entry *mall_tc_entry;
870 __be16 protocol = cls->common.protocol;
871 struct dsa_switch *ds = dp->ds;
872 struct flow_action_entry *act;
873 struct dsa_port *to_dp;
874 int err = -EOPNOTSUPP;
875
876 if (!ds->ops->port_mirror_add)
877 return err;
878
879 if (!flow_offload_has_one_action(&cls->rule->action))
880 return err;
881
882 act = &cls->rule->action.entries[0];
883
884 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
885 struct dsa_mall_mirror_tc_entry *mirror;
886
887 if (!act->dev)
888 return -EINVAL;
889
890 if (!dsa_slave_dev_check(act->dev))
891 return -EOPNOTSUPP;
892
893 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
894 if (!mall_tc_entry)
895 return -ENOMEM;
896
897 mall_tc_entry->cookie = cls->cookie;
898 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
899 mirror = &mall_tc_entry->mirror;
900
901 to_dp = dsa_slave_to_port(act->dev);
902
903 mirror->to_local_port = to_dp->index;
904 mirror->ingress = ingress;
905
906 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
907 if (err) {
908 kfree(mall_tc_entry);
909 return err;
910 }
911
912 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
913 }
914
915 return 0;
916 }
917
dsa_slave_del_cls_matchall(struct net_device * dev,struct tc_cls_matchall_offload * cls)918 static void dsa_slave_del_cls_matchall(struct net_device *dev,
919 struct tc_cls_matchall_offload *cls)
920 {
921 struct dsa_port *dp = dsa_slave_to_port(dev);
922 struct dsa_mall_tc_entry *mall_tc_entry;
923 struct dsa_switch *ds = dp->ds;
924
925 if (!ds->ops->port_mirror_del)
926 return;
927
928 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
929 if (!mall_tc_entry)
930 return;
931
932 list_del(&mall_tc_entry->list);
933
934 switch (mall_tc_entry->type) {
935 case DSA_PORT_MALL_MIRROR:
936 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
937 break;
938 default:
939 WARN_ON(1);
940 }
941
942 kfree(mall_tc_entry);
943 }
944
dsa_slave_setup_tc_cls_matchall(struct net_device * dev,struct tc_cls_matchall_offload * cls,bool ingress)945 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
946 struct tc_cls_matchall_offload *cls,
947 bool ingress)
948 {
949 if (cls->common.chain_index)
950 return -EOPNOTSUPP;
951
952 switch (cls->command) {
953 case TC_CLSMATCHALL_REPLACE:
954 return dsa_slave_add_cls_matchall(dev, cls, ingress);
955 case TC_CLSMATCHALL_DESTROY:
956 dsa_slave_del_cls_matchall(dev, cls);
957 return 0;
958 default:
959 return -EOPNOTSUPP;
960 }
961 }
962
dsa_slave_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv,bool ingress)963 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
964 void *cb_priv, bool ingress)
965 {
966 struct net_device *dev = cb_priv;
967
968 if (!tc_can_offload(dev))
969 return -EOPNOTSUPP;
970
971 switch (type) {
972 case TC_SETUP_CLSMATCHALL:
973 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
974 default:
975 return -EOPNOTSUPP;
976 }
977 }
978
dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,void * type_data,void * cb_priv)979 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
980 void *type_data, void *cb_priv)
981 {
982 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
983 }
984
dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,void * type_data,void * cb_priv)985 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
986 void *type_data, void *cb_priv)
987 {
988 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
989 }
990
991 static LIST_HEAD(dsa_slave_block_cb_list);
992
dsa_slave_setup_tc_block(struct net_device * dev,struct flow_block_offload * f)993 static int dsa_slave_setup_tc_block(struct net_device *dev,
994 struct flow_block_offload *f)
995 {
996 struct flow_block_cb *block_cb;
997 flow_setup_cb_t *cb;
998
999 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1000 cb = dsa_slave_setup_tc_block_cb_ig;
1001 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1002 cb = dsa_slave_setup_tc_block_cb_eg;
1003 else
1004 return -EOPNOTSUPP;
1005
1006 f->driver_block_list = &dsa_slave_block_cb_list;
1007
1008 switch (f->command) {
1009 case FLOW_BLOCK_BIND:
1010 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1011 return -EBUSY;
1012
1013 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1014 if (IS_ERR(block_cb))
1015 return PTR_ERR(block_cb);
1016
1017 flow_block_cb_add(block_cb, f);
1018 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1019 return 0;
1020 case FLOW_BLOCK_UNBIND:
1021 block_cb = flow_block_cb_lookup(f->block, cb, dev);
1022 if (!block_cb)
1023 return -ENOENT;
1024
1025 flow_block_cb_remove(block_cb, f);
1026 list_del(&block_cb->driver_list);
1027 return 0;
1028 default:
1029 return -EOPNOTSUPP;
1030 }
1031 }
1032
dsa_slave_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)1033 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1034 void *type_data)
1035 {
1036 struct dsa_port *dp = dsa_slave_to_port(dev);
1037 struct dsa_switch *ds = dp->ds;
1038
1039 if (type == TC_SETUP_BLOCK)
1040 return dsa_slave_setup_tc_block(dev, type_data);
1041
1042 if (!ds->ops->port_setup_tc)
1043 return -EOPNOTSUPP;
1044
1045 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1046 }
1047
dsa_slave_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)1048 static void dsa_slave_get_stats64(struct net_device *dev,
1049 struct rtnl_link_stats64 *stats)
1050 {
1051 struct dsa_slave_priv *p = netdev_priv(dev);
1052 struct pcpu_sw_netstats *s;
1053 unsigned int start;
1054 int i;
1055
1056 netdev_stats_to_stats64(stats, &dev->stats);
1057 for_each_possible_cpu(i) {
1058 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1059
1060 s = per_cpu_ptr(p->stats64, i);
1061 do {
1062 start = u64_stats_fetch_begin_irq(&s->syncp);
1063 tx_packets = s->tx_packets;
1064 tx_bytes = s->tx_bytes;
1065 rx_packets = s->rx_packets;
1066 rx_bytes = s->rx_bytes;
1067 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
1068
1069 stats->tx_packets += tx_packets;
1070 stats->tx_bytes += tx_bytes;
1071 stats->rx_packets += rx_packets;
1072 stats->rx_bytes += rx_bytes;
1073 }
1074 }
1075
dsa_slave_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * nfc,u32 * rule_locs)1076 static int dsa_slave_get_rxnfc(struct net_device *dev,
1077 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1078 {
1079 struct dsa_port *dp = dsa_slave_to_port(dev);
1080 struct dsa_switch *ds = dp->ds;
1081
1082 if (!ds->ops->get_rxnfc)
1083 return -EOPNOTSUPP;
1084
1085 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1086 }
1087
dsa_slave_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * nfc)1088 static int dsa_slave_set_rxnfc(struct net_device *dev,
1089 struct ethtool_rxnfc *nfc)
1090 {
1091 struct dsa_port *dp = dsa_slave_to_port(dev);
1092 struct dsa_switch *ds = dp->ds;
1093
1094 if (!ds->ops->set_rxnfc)
1095 return -EOPNOTSUPP;
1096
1097 return ds->ops->set_rxnfc(ds, dp->index, nfc);
1098 }
1099
dsa_slave_get_ts_info(struct net_device * dev,struct ethtool_ts_info * ts)1100 static int dsa_slave_get_ts_info(struct net_device *dev,
1101 struct ethtool_ts_info *ts)
1102 {
1103 struct dsa_slave_priv *p = netdev_priv(dev);
1104 struct dsa_switch *ds = p->dp->ds;
1105
1106 if (!ds->ops->get_ts_info)
1107 return -EOPNOTSUPP;
1108
1109 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1110 }
1111
dsa_slave_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)1112 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1113 u16 vid)
1114 {
1115 struct dsa_port *dp = dsa_slave_to_port(dev);
1116 struct bridge_vlan_info info;
1117 int ret;
1118
1119 /* Check for a possible bridge VLAN entry now since there is no
1120 * need to emulate the switchdev prepare + commit phase.
1121 */
1122 if (dp->bridge_dev) {
1123 if (!br_vlan_enabled(dp->bridge_dev))
1124 return 0;
1125
1126 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1127 * device, respectively the VID is not found, returning
1128 * 0 means success, which is a failure for us here.
1129 */
1130 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1131 if (ret == 0)
1132 return -EBUSY;
1133 }
1134
1135 ret = dsa_port_vid_add(dp, vid, 0);
1136 if (ret)
1137 return ret;
1138
1139 ret = dsa_port_vid_add(dp->cpu_dp, vid, 0);
1140 if (ret)
1141 return ret;
1142
1143 return 0;
1144 }
1145
dsa_slave_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)1146 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1147 u16 vid)
1148 {
1149 struct dsa_port *dp = dsa_slave_to_port(dev);
1150 struct bridge_vlan_info info;
1151 int ret;
1152
1153 /* Check for a possible bridge VLAN entry now since there is no
1154 * need to emulate the switchdev prepare + commit phase.
1155 */
1156 if (dp->bridge_dev) {
1157 if (!br_vlan_enabled(dp->bridge_dev))
1158 return 0;
1159
1160 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1161 * device, respectively the VID is not found, returning
1162 * 0 means success, which is a failure for us here.
1163 */
1164 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1165 if (ret == 0)
1166 return -EBUSY;
1167 }
1168
1169 /* Do not deprogram the CPU port as it may be shared with other user
1170 * ports which can be members of this VLAN as well.
1171 */
1172 return dsa_port_vid_del(dp, vid);
1173 }
1174
1175 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1176 .get_drvinfo = dsa_slave_get_drvinfo,
1177 .get_regs_len = dsa_slave_get_regs_len,
1178 .get_regs = dsa_slave_get_regs,
1179 .nway_reset = dsa_slave_nway_reset,
1180 .get_link = ethtool_op_get_link,
1181 .get_eeprom_len = dsa_slave_get_eeprom_len,
1182 .get_eeprom = dsa_slave_get_eeprom,
1183 .set_eeprom = dsa_slave_set_eeprom,
1184 .get_strings = dsa_slave_get_strings,
1185 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1186 .get_sset_count = dsa_slave_get_sset_count,
1187 .set_wol = dsa_slave_set_wol,
1188 .get_wol = dsa_slave_get_wol,
1189 .set_eee = dsa_slave_set_eee,
1190 .get_eee = dsa_slave_get_eee,
1191 .get_link_ksettings = dsa_slave_get_link_ksettings,
1192 .set_link_ksettings = dsa_slave_set_link_ksettings,
1193 .get_rxnfc = dsa_slave_get_rxnfc,
1194 .set_rxnfc = dsa_slave_set_rxnfc,
1195 .get_ts_info = dsa_slave_get_ts_info,
1196 };
1197
1198 /* 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,struct netlink_ext_ack * extack)1199 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1200 struct net_device *dev,
1201 const unsigned char *addr, u16 vid,
1202 u16 flags,
1203 struct netlink_ext_ack *extack)
1204 {
1205 struct dsa_port *dp = dsa_slave_to_port(dev);
1206
1207 return dsa_port_fdb_add(dp, addr, vid);
1208 }
1209
dsa_legacy_fdb_del(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid)1210 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1211 struct net_device *dev,
1212 const unsigned char *addr, u16 vid)
1213 {
1214 struct dsa_port *dp = dsa_slave_to_port(dev);
1215
1216 return dsa_port_fdb_del(dp, addr, vid);
1217 }
1218
dsa_slave_get_devlink_port(struct net_device * dev)1219 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1220 {
1221 struct dsa_port *dp = dsa_slave_to_port(dev);
1222
1223 return dp->ds->devlink ? &dp->devlink_port : NULL;
1224 }
1225
1226 static const struct net_device_ops dsa_slave_netdev_ops = {
1227 .ndo_open = dsa_slave_open,
1228 .ndo_stop = dsa_slave_close,
1229 .ndo_start_xmit = dsa_slave_xmit,
1230 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1231 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
1232 .ndo_set_mac_address = dsa_slave_set_mac_address,
1233 .ndo_fdb_add = dsa_legacy_fdb_add,
1234 .ndo_fdb_del = dsa_legacy_fdb_del,
1235 .ndo_fdb_dump = dsa_slave_fdb_dump,
1236 .ndo_do_ioctl = dsa_slave_ioctl,
1237 .ndo_get_iflink = dsa_slave_get_iflink,
1238 #ifdef CONFIG_NET_POLL_CONTROLLER
1239 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1240 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1241 .ndo_poll_controller = dsa_slave_poll_controller,
1242 #endif
1243 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1244 .ndo_setup_tc = dsa_slave_setup_tc,
1245 .ndo_get_stats64 = dsa_slave_get_stats64,
1246 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
1247 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1248 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
1249 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
1250 };
1251
1252 static struct device_type dsa_type = {
1253 .name = "dsa",
1254 };
1255
dsa_port_phylink_mac_change(struct dsa_switch * ds,int port,bool up)1256 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1257 {
1258 const struct dsa_port *dp = dsa_to_port(ds, port);
1259
1260 phylink_mac_change(dp->pl, up);
1261 }
1262 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1263
dsa_slave_phylink_fixed_state(struct net_device * dev,struct phylink_link_state * state)1264 static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1265 struct phylink_link_state *state)
1266 {
1267 struct dsa_port *dp = dsa_slave_to_port(dev);
1268 struct dsa_switch *ds = dp->ds;
1269
1270 /* No need to check that this operation is valid, the callback would
1271 * not be called if it was not.
1272 */
1273 ds->ops->phylink_fixed_state(ds, dp->index, state);
1274 }
1275
1276 /* slave device setup *******************************************************/
dsa_slave_phy_connect(struct net_device * slave_dev,int addr)1277 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1278 {
1279 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1280 struct dsa_switch *ds = dp->ds;
1281
1282 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1283 if (!slave_dev->phydev) {
1284 netdev_err(slave_dev, "no phy at %d\n", addr);
1285 return -ENODEV;
1286 }
1287
1288 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1289 }
1290
dsa_slave_phy_setup(struct net_device * slave_dev)1291 static int dsa_slave_phy_setup(struct net_device *slave_dev)
1292 {
1293 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1294 struct device_node *port_dn = dp->dn;
1295 struct dsa_switch *ds = dp->ds;
1296 u32 phy_flags = 0;
1297 int mode, ret;
1298
1299 mode = of_get_phy_mode(port_dn);
1300 if (mode < 0)
1301 mode = PHY_INTERFACE_MODE_NA;
1302
1303 dp->pl_config.dev = &slave_dev->dev;
1304 dp->pl_config.type = PHYLINK_NETDEV;
1305
1306 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1307 &dsa_port_phylink_mac_ops);
1308 if (IS_ERR(dp->pl)) {
1309 netdev_err(slave_dev,
1310 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1311 return PTR_ERR(dp->pl);
1312 }
1313
1314 /* Register only if the switch provides such a callback, since this
1315 * callback takes precedence over polling the link GPIO in PHYLINK
1316 * (see phylink_get_fixed_state).
1317 */
1318 if (ds->ops->phylink_fixed_state)
1319 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1320
1321 if (ds->ops->get_phy_flags)
1322 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1323
1324 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1325 if (ret == -ENODEV && ds->slave_mii_bus) {
1326 /* We could not connect to a designated PHY or SFP, so try to
1327 * use the switch internal MDIO bus instead
1328 */
1329 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1330 }
1331 if (ret) {
1332 netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1333 ERR_PTR(ret));
1334 phylink_destroy(dp->pl);
1335 }
1336
1337 return ret;
1338 }
1339
dsa_slave_suspend(struct net_device * slave_dev)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
dsa_slave_resume(struct net_device * slave_dev)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
dsa_slave_notify(struct net_device * dev,unsigned long val)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
dsa_slave_create(struct dsa_port * port)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
1435 ret = gro_cells_init(&p->gcells, slave_dev);
1436 if (ret)
1437 goto out_free;
1438
1439 p->dp = port;
1440 INIT_LIST_HEAD(&p->mall_tc_list);
1441 INIT_WORK(&port->xmit_work, dsa_port_xmit_work);
1442 skb_queue_head_init(&port->xmit_queue);
1443 p->xmit = cpu_dp->tag_ops->xmit;
1444 port->slave = slave_dev;
1445
1446 netif_carrier_off(slave_dev);
1447
1448 ret = dsa_slave_phy_setup(slave_dev);
1449 if (ret) {
1450 netdev_err(master, "error %d setting up slave phy\n", ret);
1451 goto out_gcells;
1452 }
1453
1454 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
1455
1456 ret = register_netdev(slave_dev);
1457 if (ret) {
1458 netdev_err(master, "error %d registering interface %s\n",
1459 ret, slave_dev->name);
1460 goto out_phy;
1461 }
1462
1463 return 0;
1464
1465 out_phy:
1466 rtnl_lock();
1467 phylink_disconnect_phy(p->dp->pl);
1468 rtnl_unlock();
1469 phylink_destroy(p->dp->pl);
1470 out_gcells:
1471 gro_cells_destroy(&p->gcells);
1472 out_free:
1473 free_percpu(p->stats64);
1474 free_netdev(slave_dev);
1475 port->slave = NULL;
1476 return ret;
1477 }
1478
dsa_slave_destroy(struct net_device * slave_dev)1479 void dsa_slave_destroy(struct net_device *slave_dev)
1480 {
1481 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1482 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1483
1484 netif_carrier_off(slave_dev);
1485 rtnl_lock();
1486 phylink_disconnect_phy(dp->pl);
1487 rtnl_unlock();
1488
1489 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1490 unregister_netdev(slave_dev);
1491 phylink_destroy(dp->pl);
1492 gro_cells_destroy(&p->gcells);
1493 free_percpu(p->stats64);
1494 free_netdev(slave_dev);
1495 }
1496
dsa_slave_dev_check(const struct net_device * dev)1497 static bool dsa_slave_dev_check(const struct net_device *dev)
1498 {
1499 return dev->netdev_ops == &dsa_slave_netdev_ops;
1500 }
1501
dsa_slave_changeupper(struct net_device * dev,struct netdev_notifier_changeupper_info * info)1502 static int dsa_slave_changeupper(struct net_device *dev,
1503 struct netdev_notifier_changeupper_info *info)
1504 {
1505 struct dsa_port *dp = dsa_slave_to_port(dev);
1506 int err = NOTIFY_DONE;
1507
1508 if (netif_is_bridge_master(info->upper_dev)) {
1509 if (info->linking) {
1510 err = dsa_port_bridge_join(dp, info->upper_dev);
1511 err = notifier_from_errno(err);
1512 } else {
1513 dsa_port_bridge_leave(dp, info->upper_dev);
1514 err = NOTIFY_OK;
1515 }
1516 }
1517
1518 return err;
1519 }
1520
dsa_slave_upper_vlan_check(struct net_device * dev,struct netdev_notifier_changeupper_info * info)1521 static int dsa_slave_upper_vlan_check(struct net_device *dev,
1522 struct netdev_notifier_changeupper_info *
1523 info)
1524 {
1525 struct netlink_ext_ack *ext_ack;
1526 struct net_device *slave;
1527 struct dsa_port *dp;
1528
1529 ext_ack = netdev_notifier_info_to_extack(&info->info);
1530
1531 if (!is_vlan_dev(dev))
1532 return NOTIFY_DONE;
1533
1534 slave = vlan_dev_real_dev(dev);
1535 if (!dsa_slave_dev_check(slave))
1536 return NOTIFY_DONE;
1537
1538 dp = dsa_slave_to_port(slave);
1539 if (!dp->bridge_dev)
1540 return NOTIFY_DONE;
1541
1542 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1543 if (br_vlan_enabled(dp->bridge_dev) &&
1544 netif_is_bridge_master(info->upper_dev) && info->linking) {
1545 NL_SET_ERR_MSG_MOD(ext_ack,
1546 "Cannot enslave VLAN device into VLAN aware bridge");
1547 return notifier_from_errno(-EINVAL);
1548 }
1549
1550 return NOTIFY_DONE;
1551 }
1552
dsa_slave_netdevice_event(struct notifier_block * nb,unsigned long event,void * ptr)1553 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1554 unsigned long event, void *ptr)
1555 {
1556 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1557
1558 if (event == NETDEV_CHANGEUPPER) {
1559 if (!dsa_slave_dev_check(dev))
1560 return dsa_slave_upper_vlan_check(dev, ptr);
1561
1562 return dsa_slave_changeupper(dev, ptr);
1563 }
1564
1565 return NOTIFY_DONE;
1566 }
1567
1568 struct dsa_switchdev_event_work {
1569 struct work_struct work;
1570 struct switchdev_notifier_fdb_info fdb_info;
1571 struct net_device *dev;
1572 unsigned long event;
1573 };
1574
dsa_slave_switchdev_event_work(struct work_struct * work)1575 static void dsa_slave_switchdev_event_work(struct work_struct *work)
1576 {
1577 struct dsa_switchdev_event_work *switchdev_work =
1578 container_of(work, struct dsa_switchdev_event_work, work);
1579 struct net_device *dev = switchdev_work->dev;
1580 struct switchdev_notifier_fdb_info *fdb_info;
1581 struct dsa_port *dp = dsa_slave_to_port(dev);
1582 int err;
1583
1584 rtnl_lock();
1585 switch (switchdev_work->event) {
1586 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1587 fdb_info = &switchdev_work->fdb_info;
1588 if (!fdb_info->added_by_user)
1589 break;
1590
1591 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1592 if (err) {
1593 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1594 break;
1595 }
1596 fdb_info->offloaded = true;
1597 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1598 &fdb_info->info, NULL);
1599 break;
1600
1601 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1602 fdb_info = &switchdev_work->fdb_info;
1603 if (!fdb_info->added_by_user)
1604 break;
1605
1606 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1607 if (err) {
1608 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1609 dev_close(dev);
1610 }
1611 break;
1612 }
1613 rtnl_unlock();
1614
1615 kfree(switchdev_work->fdb_info.addr);
1616 kfree(switchdev_work);
1617 dev_put(dev);
1618 }
1619
1620 static int
dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work * switchdev_work,const struct switchdev_notifier_fdb_info * fdb_info)1621 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1622 switchdev_work,
1623 const struct switchdev_notifier_fdb_info *
1624 fdb_info)
1625 {
1626 memcpy(&switchdev_work->fdb_info, fdb_info,
1627 sizeof(switchdev_work->fdb_info));
1628 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1629 if (!switchdev_work->fdb_info.addr)
1630 return -ENOMEM;
1631 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1632 fdb_info->addr);
1633 return 0;
1634 }
1635
1636 /* Called under rcu_read_lock() */
dsa_slave_switchdev_event(struct notifier_block * unused,unsigned long event,void * ptr)1637 static int dsa_slave_switchdev_event(struct notifier_block *unused,
1638 unsigned long event, void *ptr)
1639 {
1640 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1641 struct dsa_switchdev_event_work *switchdev_work;
1642 int err;
1643
1644 if (event == SWITCHDEV_PORT_ATTR_SET) {
1645 err = switchdev_handle_port_attr_set(dev, ptr,
1646 dsa_slave_dev_check,
1647 dsa_slave_port_attr_set);
1648 return notifier_from_errno(err);
1649 }
1650
1651 if (!dsa_slave_dev_check(dev))
1652 return NOTIFY_DONE;
1653
1654 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1655 if (!switchdev_work)
1656 return NOTIFY_BAD;
1657
1658 INIT_WORK(&switchdev_work->work,
1659 dsa_slave_switchdev_event_work);
1660 switchdev_work->dev = dev;
1661 switchdev_work->event = event;
1662
1663 switch (event) {
1664 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1665 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1666 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1667 goto err_fdb_work_init;
1668 dev_hold(dev);
1669 break;
1670 default:
1671 kfree(switchdev_work);
1672 return NOTIFY_DONE;
1673 }
1674
1675 dsa_schedule_work(&switchdev_work->work);
1676 return NOTIFY_OK;
1677
1678 err_fdb_work_init:
1679 kfree(switchdev_work);
1680 return NOTIFY_BAD;
1681 }
1682
dsa_slave_switchdev_blocking_event(struct notifier_block * unused,unsigned long event,void * ptr)1683 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1684 unsigned long event, void *ptr)
1685 {
1686 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1687 int err;
1688
1689 switch (event) {
1690 case SWITCHDEV_PORT_OBJ_ADD:
1691 err = switchdev_handle_port_obj_add(dev, ptr,
1692 dsa_slave_dev_check,
1693 dsa_slave_port_obj_add);
1694 return notifier_from_errno(err);
1695 case SWITCHDEV_PORT_OBJ_DEL:
1696 err = switchdev_handle_port_obj_del(dev, ptr,
1697 dsa_slave_dev_check,
1698 dsa_slave_port_obj_del);
1699 return notifier_from_errno(err);
1700 case SWITCHDEV_PORT_ATTR_SET:
1701 err = switchdev_handle_port_attr_set(dev, ptr,
1702 dsa_slave_dev_check,
1703 dsa_slave_port_attr_set);
1704 return notifier_from_errno(err);
1705 }
1706
1707 return NOTIFY_DONE;
1708 }
1709
1710 static struct notifier_block dsa_slave_nb __read_mostly = {
1711 .notifier_call = dsa_slave_netdevice_event,
1712 };
1713
1714 static struct notifier_block dsa_slave_switchdev_notifier = {
1715 .notifier_call = dsa_slave_switchdev_event,
1716 };
1717
1718 static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1719 .notifier_call = dsa_slave_switchdev_blocking_event,
1720 };
1721
dsa_slave_register_notifier(void)1722 int dsa_slave_register_notifier(void)
1723 {
1724 struct notifier_block *nb;
1725 int err;
1726
1727 err = register_netdevice_notifier(&dsa_slave_nb);
1728 if (err)
1729 return err;
1730
1731 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1732 if (err)
1733 goto err_switchdev_nb;
1734
1735 nb = &dsa_slave_switchdev_blocking_notifier;
1736 err = register_switchdev_blocking_notifier(nb);
1737 if (err)
1738 goto err_switchdev_blocking_nb;
1739
1740 return 0;
1741
1742 err_switchdev_blocking_nb:
1743 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1744 err_switchdev_nb:
1745 unregister_netdevice_notifier(&dsa_slave_nb);
1746 return err;
1747 }
1748
dsa_slave_unregister_notifier(void)1749 void dsa_slave_unregister_notifier(void)
1750 {
1751 struct notifier_block *nb;
1752 int err;
1753
1754 nb = &dsa_slave_switchdev_blocking_notifier;
1755 err = unregister_switchdev_blocking_notifier(nb);
1756 if (err)
1757 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1758
1759 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1760 if (err)
1761 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1762
1763 err = unregister_netdevice_notifier(&dsa_slave_nb);
1764 if (err)
1765 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1766 }
1767