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