xref: /openbmc/linux/net/dsa/slave.c (revision eb3fcf007fffe5830d815e713591f3e858f2a365)
1 /*
2  * net/dsa/slave.c - Slave device handling
3  * Copyright (c) 2008-2009 Marvell Semiconductor
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10 
11 #include <linux/list.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/of_net.h>
17 #include <linux/of_mdio.h>
18 #include <net/rtnetlink.h>
19 #include <net/switchdev.h>
20 #include <linux/if_bridge.h>
21 #include <linux/netpoll.h>
22 #include "dsa_priv.h"
23 
24 /* slave mii_bus handling ***************************************************/
25 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
26 {
27 	struct dsa_switch *ds = bus->priv;
28 
29 	if (ds->phys_mii_mask & (1 << addr))
30 		return ds->drv->phy_read(ds, addr, reg);
31 
32 	return 0xffff;
33 }
34 
35 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
36 {
37 	struct dsa_switch *ds = bus->priv;
38 
39 	if (ds->phys_mii_mask & (1 << addr))
40 		return ds->drv->phy_write(ds, addr, reg, val);
41 
42 	return 0;
43 }
44 
45 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
46 {
47 	ds->slave_mii_bus->priv = (void *)ds;
48 	ds->slave_mii_bus->name = "dsa slave smi";
49 	ds->slave_mii_bus->read = dsa_slave_phy_read;
50 	ds->slave_mii_bus->write = dsa_slave_phy_write;
51 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
52 			ds->index, ds->pd->sw_addr);
53 	ds->slave_mii_bus->parent = ds->master_dev;
54 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
55 }
56 
57 
58 /* slave device handling ****************************************************/
59 static int dsa_slave_get_iflink(const struct net_device *dev)
60 {
61 	struct dsa_slave_priv *p = netdev_priv(dev);
62 
63 	return p->parent->dst->master_netdev->ifindex;
64 }
65 
66 static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
67 {
68 	return !!p->bridge_dev;
69 }
70 
71 static int dsa_slave_open(struct net_device *dev)
72 {
73 	struct dsa_slave_priv *p = netdev_priv(dev);
74 	struct net_device *master = p->parent->dst->master_netdev;
75 	struct dsa_switch *ds = p->parent;
76 	u8 stp_state = dsa_port_is_bridged(p) ?
77 			BR_STATE_BLOCKING : BR_STATE_FORWARDING;
78 	int err;
79 
80 	if (!(master->flags & IFF_UP))
81 		return -ENETDOWN;
82 
83 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
84 		err = dev_uc_add(master, dev->dev_addr);
85 		if (err < 0)
86 			goto out;
87 	}
88 
89 	if (dev->flags & IFF_ALLMULTI) {
90 		err = dev_set_allmulti(master, 1);
91 		if (err < 0)
92 			goto del_unicast;
93 	}
94 	if (dev->flags & IFF_PROMISC) {
95 		err = dev_set_promiscuity(master, 1);
96 		if (err < 0)
97 			goto clear_allmulti;
98 	}
99 
100 	if (ds->drv->port_enable) {
101 		err = ds->drv->port_enable(ds, p->port, p->phy);
102 		if (err)
103 			goto clear_promisc;
104 	}
105 
106 	if (ds->drv->port_stp_update)
107 		ds->drv->port_stp_update(ds, p->port, stp_state);
108 
109 	if (p->phy)
110 		phy_start(p->phy);
111 
112 	return 0;
113 
114 clear_promisc:
115 	if (dev->flags & IFF_PROMISC)
116 		dev_set_promiscuity(master, -1);
117 clear_allmulti:
118 	if (dev->flags & IFF_ALLMULTI)
119 		dev_set_allmulti(master, -1);
120 del_unicast:
121 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
122 		dev_uc_del(master, dev->dev_addr);
123 out:
124 	return err;
125 }
126 
127 static int dsa_slave_close(struct net_device *dev)
128 {
129 	struct dsa_slave_priv *p = netdev_priv(dev);
130 	struct net_device *master = p->parent->dst->master_netdev;
131 	struct dsa_switch *ds = p->parent;
132 
133 	if (p->phy)
134 		phy_stop(p->phy);
135 
136 	dev_mc_unsync(master, dev);
137 	dev_uc_unsync(master, dev);
138 	if (dev->flags & IFF_ALLMULTI)
139 		dev_set_allmulti(master, -1);
140 	if (dev->flags & IFF_PROMISC)
141 		dev_set_promiscuity(master, -1);
142 
143 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
144 		dev_uc_del(master, dev->dev_addr);
145 
146 	if (ds->drv->port_disable)
147 		ds->drv->port_disable(ds, p->port, p->phy);
148 
149 	if (ds->drv->port_stp_update)
150 		ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
151 
152 	return 0;
153 }
154 
155 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
156 {
157 	struct dsa_slave_priv *p = netdev_priv(dev);
158 	struct net_device *master = p->parent->dst->master_netdev;
159 
160 	if (change & IFF_ALLMULTI)
161 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
162 	if (change & IFF_PROMISC)
163 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
164 }
165 
166 static void dsa_slave_set_rx_mode(struct net_device *dev)
167 {
168 	struct dsa_slave_priv *p = netdev_priv(dev);
169 	struct net_device *master = p->parent->dst->master_netdev;
170 
171 	dev_mc_sync(master, dev);
172 	dev_uc_sync(master, dev);
173 }
174 
175 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
176 {
177 	struct dsa_slave_priv *p = netdev_priv(dev);
178 	struct net_device *master = p->parent->dst->master_netdev;
179 	struct sockaddr *addr = a;
180 	int err;
181 
182 	if (!is_valid_ether_addr(addr->sa_data))
183 		return -EADDRNOTAVAIL;
184 
185 	if (!(dev->flags & IFF_UP))
186 		goto out;
187 
188 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
189 		err = dev_uc_add(master, addr->sa_data);
190 		if (err < 0)
191 			return err;
192 	}
193 
194 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
195 		dev_uc_del(master, dev->dev_addr);
196 
197 out:
198 	ether_addr_copy(dev->dev_addr, addr->sa_data);
199 
200 	return 0;
201 }
202 
203 static int dsa_bridge_check_vlan_range(struct dsa_switch *ds,
204 				       const struct net_device *bridge,
205 				       u16 vid_begin, u16 vid_end)
206 {
207 	struct dsa_slave_priv *p;
208 	struct net_device *dev, *vlan_br;
209 	DECLARE_BITMAP(members, DSA_MAX_PORTS);
210 	DECLARE_BITMAP(untagged, DSA_MAX_PORTS);
211 	u16 vid;
212 	int member, err;
213 
214 	if (!ds->drv->vlan_getnext || !vid_begin)
215 		return -EOPNOTSUPP;
216 
217 	vid = vid_begin - 1;
218 
219 	do {
220 		err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
221 		if (err)
222 			break;
223 
224 		if (vid > vid_end)
225 			break;
226 
227 		member = find_first_bit(members, DSA_MAX_PORTS);
228 		if (member == DSA_MAX_PORTS)
229 			continue;
230 
231 		dev = ds->ports[member];
232 		p = netdev_priv(dev);
233 		vlan_br = p->bridge_dev;
234 		if (vlan_br == bridge)
235 			continue;
236 
237 		netdev_dbg(vlan_br, "hardware VLAN %d already in use\n", vid);
238 		return -EOPNOTSUPP;
239 	} while (vid < vid_end);
240 
241 	return err == -ENOENT ? 0 : err;
242 }
243 
244 static int dsa_slave_port_vlan_add(struct net_device *dev,
245 				   struct switchdev_obj *obj)
246 {
247 	struct switchdev_obj_vlan *vlan = &obj->u.vlan;
248 	struct dsa_slave_priv *p = netdev_priv(dev);
249 	struct dsa_switch *ds = p->parent;
250 	u16 vid;
251 	int err;
252 
253 	switch (obj->trans) {
254 	case SWITCHDEV_TRANS_PREPARE:
255 		if (!ds->drv->port_vlan_add || !ds->drv->port_pvid_set)
256 			return -EOPNOTSUPP;
257 
258 		/* If the requested port doesn't belong to the same bridge as
259 		 * the VLAN members, fallback to software VLAN (hopefully).
260 		 */
261 		err = dsa_bridge_check_vlan_range(ds, p->bridge_dev,
262 						  vlan->vid_begin,
263 						  vlan->vid_end);
264 		if (err)
265 			return err;
266 		break;
267 	case SWITCHDEV_TRANS_COMMIT:
268 		for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
269 			err = ds->drv->port_vlan_add(ds, p->port, vid,
270 						     vlan->flags &
271 						     BRIDGE_VLAN_INFO_UNTAGGED);
272 			if (!err && vlan->flags & BRIDGE_VLAN_INFO_PVID)
273 				err = ds->drv->port_pvid_set(ds, p->port, vid);
274 			if (err)
275 				return err;
276 		}
277 		break;
278 	default:
279 		return -EOPNOTSUPP;
280 	}
281 
282 	return 0;
283 }
284 
285 static int dsa_slave_port_vlan_del(struct net_device *dev,
286 				   struct switchdev_obj *obj)
287 {
288 	struct switchdev_obj_vlan *vlan = &obj->u.vlan;
289 	struct dsa_slave_priv *p = netdev_priv(dev);
290 	struct dsa_switch *ds = p->parent;
291 	u16 vid;
292 	int err;
293 
294 	if (!ds->drv->port_vlan_del)
295 		return -EOPNOTSUPP;
296 
297 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
298 		err = ds->drv->port_vlan_del(ds, p->port, vid);
299 		if (err)
300 			return err;
301 	}
302 
303 	return 0;
304 }
305 
306 static int dsa_slave_port_vlan_dump(struct net_device *dev,
307 				    struct switchdev_obj *obj)
308 {
309 	struct switchdev_obj_vlan *vlan = &obj->u.vlan;
310 	struct dsa_slave_priv *p = netdev_priv(dev);
311 	struct dsa_switch *ds = p->parent;
312 	DECLARE_BITMAP(members, DSA_MAX_PORTS);
313 	DECLARE_BITMAP(untagged, DSA_MAX_PORTS);
314 	u16 pvid, vid = 0;
315 	int err;
316 
317 	if (!ds->drv->vlan_getnext || !ds->drv->port_pvid_get)
318 		return -EOPNOTSUPP;
319 
320 	err = ds->drv->port_pvid_get(ds, p->port, &pvid);
321 	if (err)
322 		return err;
323 
324 	for (;;) {
325 		err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
326 		if (err)
327 			break;
328 
329 		if (!test_bit(p->port, members))
330 			continue;
331 
332 		memset(vlan, 0, sizeof(*vlan));
333 		vlan->vid_begin = vlan->vid_end = vid;
334 
335 		if (vid == pvid)
336 			vlan->flags |= BRIDGE_VLAN_INFO_PVID;
337 
338 		if (test_bit(p->port, untagged))
339 			vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
340 
341 		err = obj->cb(dev, obj);
342 		if (err)
343 			break;
344 	}
345 
346 	return err == -ENOENT ? 0 : err;
347 }
348 
349 static int dsa_slave_port_fdb_add(struct net_device *dev,
350 				  struct switchdev_obj *obj)
351 {
352 	struct switchdev_obj_fdb *fdb = &obj->u.fdb;
353 	struct dsa_slave_priv *p = netdev_priv(dev);
354 	struct dsa_switch *ds = p->parent;
355 	int ret = -EOPNOTSUPP;
356 
357 	if (obj->trans == SWITCHDEV_TRANS_PREPARE)
358 		ret = ds->drv->port_fdb_add ? 0 : -EOPNOTSUPP;
359 	else if (obj->trans == SWITCHDEV_TRANS_COMMIT)
360 		ret = ds->drv->port_fdb_add(ds, p->port, fdb->addr, fdb->vid);
361 
362 	return ret;
363 }
364 
365 static int dsa_slave_port_fdb_del(struct net_device *dev,
366 				  struct switchdev_obj *obj)
367 {
368 	struct switchdev_obj_fdb *fdb = &obj->u.fdb;
369 	struct dsa_slave_priv *p = netdev_priv(dev);
370 	struct dsa_switch *ds = p->parent;
371 	int ret = -EOPNOTSUPP;
372 
373 	if (ds->drv->port_fdb_del)
374 		ret = ds->drv->port_fdb_del(ds, p->port, fdb->addr, fdb->vid);
375 
376 	return ret;
377 }
378 
379 static int dsa_slave_port_fdb_dump(struct net_device *dev,
380 				   struct switchdev_obj *obj)
381 {
382 	struct dsa_slave_priv *p = netdev_priv(dev);
383 	struct dsa_switch *ds = p->parent;
384 	unsigned char addr[ETH_ALEN] = { 0 };
385 	u16 vid = 0;
386 	int ret;
387 
388 	if (!ds->drv->port_fdb_getnext)
389 		return -EOPNOTSUPP;
390 
391 	for (;;) {
392 		bool is_static;
393 
394 		ret = ds->drv->port_fdb_getnext(ds, p->port, addr, &vid,
395 						&is_static);
396 		if (ret < 0)
397 			break;
398 
399 		obj->u.fdb.addr = addr;
400 		obj->u.fdb.vid = vid;
401 		obj->u.fdb.ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
402 
403 		ret = obj->cb(dev, obj);
404 		if (ret < 0)
405 			break;
406 	}
407 
408 	return ret == -ENOENT ? 0 : ret;
409 }
410 
411 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
412 {
413 	struct dsa_slave_priv *p = netdev_priv(dev);
414 
415 	if (p->phy != NULL)
416 		return phy_mii_ioctl(p->phy, ifr, cmd);
417 
418 	return -EOPNOTSUPP;
419 }
420 
421 /* Return a bitmask of all ports being currently bridged within a given bridge
422  * device. Note that on leave, the mask will still return the bitmask of ports
423  * currently bridged, prior to port removal, and this is exactly what we want.
424  */
425 static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
426 				  struct net_device *bridge)
427 {
428 	struct dsa_slave_priv *p;
429 	unsigned int port;
430 	u32 mask = 0;
431 
432 	for (port = 0; port < DSA_MAX_PORTS; port++) {
433 		if (!dsa_is_port_initialized(ds, port))
434 			continue;
435 
436 		p = netdev_priv(ds->ports[port]);
437 
438 		if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
439 		    p->bridge_dev == bridge)
440 			mask |= 1 << port;
441 	}
442 
443 	return mask;
444 }
445 
446 static int dsa_slave_stp_update(struct net_device *dev, u8 state)
447 {
448 	struct dsa_slave_priv *p = netdev_priv(dev);
449 	struct dsa_switch *ds = p->parent;
450 	int ret = -EOPNOTSUPP;
451 
452 	if (ds->drv->port_stp_update)
453 		ret = ds->drv->port_stp_update(ds, p->port, state);
454 
455 	return ret;
456 }
457 
458 static int dsa_slave_port_attr_set(struct net_device *dev,
459 				   struct switchdev_attr *attr)
460 {
461 	int ret = 0;
462 
463 	switch (attr->id) {
464 	case SWITCHDEV_ATTR_PORT_STP_STATE:
465 		if (attr->trans == SWITCHDEV_TRANS_COMMIT)
466 			ret = dsa_slave_stp_update(dev, attr->u.stp_state);
467 		break;
468 	default:
469 		ret = -EOPNOTSUPP;
470 		break;
471 	}
472 
473 	return ret;
474 }
475 
476 static int dsa_slave_port_obj_add(struct net_device *dev,
477 				  struct switchdev_obj *obj)
478 {
479 	int err;
480 
481 	/* For the prepare phase, ensure the full set of changes is feasable in
482 	 * one go in order to signal a failure properly. If an operation is not
483 	 * supported, return -EOPNOTSUPP.
484 	 */
485 
486 	switch (obj->id) {
487 	case SWITCHDEV_OBJ_PORT_FDB:
488 		err = dsa_slave_port_fdb_add(dev, obj);
489 		break;
490 	case SWITCHDEV_OBJ_PORT_VLAN:
491 		err = dsa_slave_port_vlan_add(dev, obj);
492 		break;
493 	default:
494 		err = -EOPNOTSUPP;
495 		break;
496 	}
497 
498 	return err;
499 }
500 
501 static int dsa_slave_port_obj_del(struct net_device *dev,
502 				  struct switchdev_obj *obj)
503 {
504 	int err;
505 
506 	switch (obj->id) {
507 	case SWITCHDEV_OBJ_PORT_FDB:
508 		err = dsa_slave_port_fdb_del(dev, obj);
509 		break;
510 	case SWITCHDEV_OBJ_PORT_VLAN:
511 		err = dsa_slave_port_vlan_del(dev, obj);
512 		break;
513 	default:
514 		err = -EOPNOTSUPP;
515 		break;
516 	}
517 
518 	return err;
519 }
520 
521 static int dsa_slave_port_obj_dump(struct net_device *dev,
522 				   struct switchdev_obj *obj)
523 {
524 	int err;
525 
526 	switch (obj->id) {
527 	case SWITCHDEV_OBJ_PORT_FDB:
528 		err = dsa_slave_port_fdb_dump(dev, obj);
529 		break;
530 	case SWITCHDEV_OBJ_PORT_VLAN:
531 		err = dsa_slave_port_vlan_dump(dev, obj);
532 		break;
533 	default:
534 		err = -EOPNOTSUPP;
535 		break;
536 	}
537 
538 	return err;
539 }
540 
541 static int dsa_slave_bridge_port_join(struct net_device *dev,
542 				      struct net_device *br)
543 {
544 	struct dsa_slave_priv *p = netdev_priv(dev);
545 	struct dsa_switch *ds = p->parent;
546 	int ret = -EOPNOTSUPP;
547 
548 	p->bridge_dev = br;
549 
550 	if (ds->drv->port_join_bridge)
551 		ret = ds->drv->port_join_bridge(ds, p->port,
552 						dsa_slave_br_port_mask(ds, br));
553 
554 	return ret;
555 }
556 
557 static int dsa_slave_bridge_port_leave(struct net_device *dev)
558 {
559 	struct dsa_slave_priv *p = netdev_priv(dev);
560 	struct dsa_switch *ds = p->parent;
561 	int ret = -EOPNOTSUPP;
562 
563 
564 	if (ds->drv->port_leave_bridge)
565 		ret = ds->drv->port_leave_bridge(ds, p->port,
566 						 dsa_slave_br_port_mask(ds, p->bridge_dev));
567 
568 	p->bridge_dev = NULL;
569 
570 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
571 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
572 	 */
573 	dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
574 
575 	return ret;
576 }
577 
578 static int dsa_slave_port_attr_get(struct net_device *dev,
579 				   struct switchdev_attr *attr)
580 {
581 	struct dsa_slave_priv *p = netdev_priv(dev);
582 	struct dsa_switch *ds = p->parent;
583 
584 	switch (attr->id) {
585 	case SWITCHDEV_ATTR_PORT_PARENT_ID:
586 		attr->u.ppid.id_len = sizeof(ds->index);
587 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
588 		break;
589 	default:
590 		return -EOPNOTSUPP;
591 	}
592 
593 	return 0;
594 }
595 
596 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
597 					       struct sk_buff *skb)
598 {
599 #ifdef CONFIG_NET_POLL_CONTROLLER
600 	if (p->netpoll)
601 		netpoll_send_skb(p->netpoll, skb);
602 #else
603 	BUG();
604 #endif
605 	return NETDEV_TX_OK;
606 }
607 
608 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
609 {
610 	struct dsa_slave_priv *p = netdev_priv(dev);
611 	struct sk_buff *nskb;
612 
613 	dev->stats.tx_packets++;
614 	dev->stats.tx_bytes += skb->len;
615 
616 	/* Transmit function may have to reallocate the original SKB */
617 	nskb = p->xmit(skb, dev);
618 	if (!nskb)
619 		return NETDEV_TX_OK;
620 
621 	/* SKB for netpoll still need to be mangled with the protocol-specific
622 	 * tag to be successfully transmitted
623 	 */
624 	if (unlikely(netpoll_tx_running(dev)))
625 		return dsa_netpoll_send_skb(p, nskb);
626 
627 	/* Queue the SKB for transmission on the parent interface, but
628 	 * do not modify its EtherType
629 	 */
630 	nskb->dev = p->parent->dst->master_netdev;
631 	dev_queue_xmit(nskb);
632 
633 	return NETDEV_TX_OK;
634 }
635 
636 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
637 					    struct net_device *dev)
638 {
639 	/* Just return the original SKB */
640 	return skb;
641 }
642 
643 
644 /* ethtool operations *******************************************************/
645 static int
646 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
647 {
648 	struct dsa_slave_priv *p = netdev_priv(dev);
649 	int err;
650 
651 	err = -EOPNOTSUPP;
652 	if (p->phy != NULL) {
653 		err = phy_read_status(p->phy);
654 		if (err == 0)
655 			err = phy_ethtool_gset(p->phy, cmd);
656 	}
657 
658 	return err;
659 }
660 
661 static int
662 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
663 {
664 	struct dsa_slave_priv *p = netdev_priv(dev);
665 
666 	if (p->phy != NULL)
667 		return phy_ethtool_sset(p->phy, cmd);
668 
669 	return -EOPNOTSUPP;
670 }
671 
672 static void dsa_slave_get_drvinfo(struct net_device *dev,
673 				  struct ethtool_drvinfo *drvinfo)
674 {
675 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
676 	strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
677 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
678 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
679 }
680 
681 static int dsa_slave_get_regs_len(struct net_device *dev)
682 {
683 	struct dsa_slave_priv *p = netdev_priv(dev);
684 	struct dsa_switch *ds = p->parent;
685 
686 	if (ds->drv->get_regs_len)
687 		return ds->drv->get_regs_len(ds, p->port);
688 
689 	return -EOPNOTSUPP;
690 }
691 
692 static void
693 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
694 {
695 	struct dsa_slave_priv *p = netdev_priv(dev);
696 	struct dsa_switch *ds = p->parent;
697 
698 	if (ds->drv->get_regs)
699 		ds->drv->get_regs(ds, p->port, regs, _p);
700 }
701 
702 static int dsa_slave_nway_reset(struct net_device *dev)
703 {
704 	struct dsa_slave_priv *p = netdev_priv(dev);
705 
706 	if (p->phy != NULL)
707 		return genphy_restart_aneg(p->phy);
708 
709 	return -EOPNOTSUPP;
710 }
711 
712 static u32 dsa_slave_get_link(struct net_device *dev)
713 {
714 	struct dsa_slave_priv *p = netdev_priv(dev);
715 
716 	if (p->phy != NULL) {
717 		genphy_update_link(p->phy);
718 		return p->phy->link;
719 	}
720 
721 	return -EOPNOTSUPP;
722 }
723 
724 static int dsa_slave_get_eeprom_len(struct net_device *dev)
725 {
726 	struct dsa_slave_priv *p = netdev_priv(dev);
727 	struct dsa_switch *ds = p->parent;
728 
729 	if (ds->pd->eeprom_len)
730 		return ds->pd->eeprom_len;
731 
732 	if (ds->drv->get_eeprom_len)
733 		return ds->drv->get_eeprom_len(ds);
734 
735 	return 0;
736 }
737 
738 static int dsa_slave_get_eeprom(struct net_device *dev,
739 				struct ethtool_eeprom *eeprom, u8 *data)
740 {
741 	struct dsa_slave_priv *p = netdev_priv(dev);
742 	struct dsa_switch *ds = p->parent;
743 
744 	if (ds->drv->get_eeprom)
745 		return ds->drv->get_eeprom(ds, eeprom, data);
746 
747 	return -EOPNOTSUPP;
748 }
749 
750 static int dsa_slave_set_eeprom(struct net_device *dev,
751 				struct ethtool_eeprom *eeprom, u8 *data)
752 {
753 	struct dsa_slave_priv *p = netdev_priv(dev);
754 	struct dsa_switch *ds = p->parent;
755 
756 	if (ds->drv->set_eeprom)
757 		return ds->drv->set_eeprom(ds, eeprom, data);
758 
759 	return -EOPNOTSUPP;
760 }
761 
762 static void dsa_slave_get_strings(struct net_device *dev,
763 				  uint32_t stringset, uint8_t *data)
764 {
765 	struct dsa_slave_priv *p = netdev_priv(dev);
766 	struct dsa_switch *ds = p->parent;
767 
768 	if (stringset == ETH_SS_STATS) {
769 		int len = ETH_GSTRING_LEN;
770 
771 		strncpy(data, "tx_packets", len);
772 		strncpy(data + len, "tx_bytes", len);
773 		strncpy(data + 2 * len, "rx_packets", len);
774 		strncpy(data + 3 * len, "rx_bytes", len);
775 		if (ds->drv->get_strings != NULL)
776 			ds->drv->get_strings(ds, p->port, data + 4 * len);
777 	}
778 }
779 
780 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
781 					struct ethtool_stats *stats,
782 					uint64_t *data)
783 {
784 	struct dsa_slave_priv *p = netdev_priv(dev);
785 	struct dsa_switch *ds = p->parent;
786 
787 	data[0] = p->dev->stats.tx_packets;
788 	data[1] = p->dev->stats.tx_bytes;
789 	data[2] = p->dev->stats.rx_packets;
790 	data[3] = p->dev->stats.rx_bytes;
791 	if (ds->drv->get_ethtool_stats != NULL)
792 		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
793 }
794 
795 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
796 {
797 	struct dsa_slave_priv *p = netdev_priv(dev);
798 	struct dsa_switch *ds = p->parent;
799 
800 	if (sset == ETH_SS_STATS) {
801 		int count;
802 
803 		count = 4;
804 		if (ds->drv->get_sset_count != NULL)
805 			count += ds->drv->get_sset_count(ds);
806 
807 		return count;
808 	}
809 
810 	return -EOPNOTSUPP;
811 }
812 
813 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
814 {
815 	struct dsa_slave_priv *p = netdev_priv(dev);
816 	struct dsa_switch *ds = p->parent;
817 
818 	if (ds->drv->get_wol)
819 		ds->drv->get_wol(ds, p->port, w);
820 }
821 
822 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
823 {
824 	struct dsa_slave_priv *p = netdev_priv(dev);
825 	struct dsa_switch *ds = p->parent;
826 	int ret = -EOPNOTSUPP;
827 
828 	if (ds->drv->set_wol)
829 		ret = ds->drv->set_wol(ds, p->port, w);
830 
831 	return ret;
832 }
833 
834 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
835 {
836 	struct dsa_slave_priv *p = netdev_priv(dev);
837 	struct dsa_switch *ds = p->parent;
838 	int ret;
839 
840 	if (!ds->drv->set_eee)
841 		return -EOPNOTSUPP;
842 
843 	ret = ds->drv->set_eee(ds, p->port, p->phy, e);
844 	if (ret)
845 		return ret;
846 
847 	if (p->phy)
848 		ret = phy_ethtool_set_eee(p->phy, e);
849 
850 	return ret;
851 }
852 
853 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
854 {
855 	struct dsa_slave_priv *p = netdev_priv(dev);
856 	struct dsa_switch *ds = p->parent;
857 	int ret;
858 
859 	if (!ds->drv->get_eee)
860 		return -EOPNOTSUPP;
861 
862 	ret = ds->drv->get_eee(ds, p->port, e);
863 	if (ret)
864 		return ret;
865 
866 	if (p->phy)
867 		ret = phy_ethtool_get_eee(p->phy, e);
868 
869 	return ret;
870 }
871 
872 #ifdef CONFIG_NET_POLL_CONTROLLER
873 static int dsa_slave_netpoll_setup(struct net_device *dev,
874 				   struct netpoll_info *ni)
875 {
876 	struct dsa_slave_priv *p = netdev_priv(dev);
877 	struct dsa_switch *ds = p->parent;
878 	struct net_device *master = ds->dst->master_netdev;
879 	struct netpoll *netpoll;
880 	int err = 0;
881 
882 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
883 	if (!netpoll)
884 		return -ENOMEM;
885 
886 	err = __netpoll_setup(netpoll, master);
887 	if (err) {
888 		kfree(netpoll);
889 		goto out;
890 	}
891 
892 	p->netpoll = netpoll;
893 out:
894 	return err;
895 }
896 
897 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
898 {
899 	struct dsa_slave_priv *p = netdev_priv(dev);
900 	struct netpoll *netpoll = p->netpoll;
901 
902 	if (!netpoll)
903 		return;
904 
905 	p->netpoll = NULL;
906 
907 	__netpoll_free_async(netpoll);
908 }
909 
910 static void dsa_slave_poll_controller(struct net_device *dev)
911 {
912 }
913 #endif
914 
915 static const struct ethtool_ops dsa_slave_ethtool_ops = {
916 	.get_settings		= dsa_slave_get_settings,
917 	.set_settings		= dsa_slave_set_settings,
918 	.get_drvinfo		= dsa_slave_get_drvinfo,
919 	.get_regs_len		= dsa_slave_get_regs_len,
920 	.get_regs		= dsa_slave_get_regs,
921 	.nway_reset		= dsa_slave_nway_reset,
922 	.get_link		= dsa_slave_get_link,
923 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
924 	.get_eeprom		= dsa_slave_get_eeprom,
925 	.set_eeprom		= dsa_slave_set_eeprom,
926 	.get_strings		= dsa_slave_get_strings,
927 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
928 	.get_sset_count		= dsa_slave_get_sset_count,
929 	.set_wol		= dsa_slave_set_wol,
930 	.get_wol		= dsa_slave_get_wol,
931 	.set_eee		= dsa_slave_set_eee,
932 	.get_eee		= dsa_slave_get_eee,
933 };
934 
935 static const struct net_device_ops dsa_slave_netdev_ops = {
936 	.ndo_open	 	= dsa_slave_open,
937 	.ndo_stop		= dsa_slave_close,
938 	.ndo_start_xmit		= dsa_slave_xmit,
939 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
940 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
941 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
942 	.ndo_fdb_add		= switchdev_port_fdb_add,
943 	.ndo_fdb_del		= switchdev_port_fdb_del,
944 	.ndo_fdb_dump		= switchdev_port_fdb_dump,
945 	.ndo_do_ioctl		= dsa_slave_ioctl,
946 	.ndo_get_iflink		= dsa_slave_get_iflink,
947 #ifdef CONFIG_NET_POLL_CONTROLLER
948 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
949 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
950 	.ndo_poll_controller	= dsa_slave_poll_controller,
951 #endif
952 	.ndo_bridge_getlink	= switchdev_port_bridge_getlink,
953 	.ndo_bridge_setlink	= switchdev_port_bridge_setlink,
954 	.ndo_bridge_dellink	= switchdev_port_bridge_dellink,
955 };
956 
957 static const struct switchdev_ops dsa_slave_switchdev_ops = {
958 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
959 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
960 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
961 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
962 	.switchdev_port_obj_dump	= dsa_slave_port_obj_dump,
963 };
964 
965 static void dsa_slave_adjust_link(struct net_device *dev)
966 {
967 	struct dsa_slave_priv *p = netdev_priv(dev);
968 	struct dsa_switch *ds = p->parent;
969 	unsigned int status_changed = 0;
970 
971 	if (p->old_link != p->phy->link) {
972 		status_changed = 1;
973 		p->old_link = p->phy->link;
974 	}
975 
976 	if (p->old_duplex != p->phy->duplex) {
977 		status_changed = 1;
978 		p->old_duplex = p->phy->duplex;
979 	}
980 
981 	if (p->old_pause != p->phy->pause) {
982 		status_changed = 1;
983 		p->old_pause = p->phy->pause;
984 	}
985 
986 	if (ds->drv->adjust_link && status_changed)
987 		ds->drv->adjust_link(ds, p->port, p->phy);
988 
989 	if (status_changed)
990 		phy_print_status(p->phy);
991 }
992 
993 static int dsa_slave_fixed_link_update(struct net_device *dev,
994 				       struct fixed_phy_status *status)
995 {
996 	struct dsa_slave_priv *p = netdev_priv(dev);
997 	struct dsa_switch *ds = p->parent;
998 
999 	if (ds->drv->fixed_link_update)
1000 		ds->drv->fixed_link_update(ds, p->port, status);
1001 
1002 	return 0;
1003 }
1004 
1005 /* slave device setup *******************************************************/
1006 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1007 				 struct net_device *slave_dev,
1008 				 int addr)
1009 {
1010 	struct dsa_switch *ds = p->parent;
1011 
1012 	p->phy = ds->slave_mii_bus->phy_map[addr];
1013 	if (!p->phy)
1014 		return -ENODEV;
1015 
1016 	/* Use already configured phy mode */
1017 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1018 		p->phy_interface = p->phy->interface;
1019 	phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1020 			   p->phy_interface);
1021 
1022 	return 0;
1023 }
1024 
1025 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1026 				struct net_device *slave_dev)
1027 {
1028 	struct dsa_switch *ds = p->parent;
1029 	struct dsa_chip_data *cd = ds->pd;
1030 	struct device_node *phy_dn, *port_dn;
1031 	bool phy_is_fixed = false;
1032 	u32 phy_flags = 0;
1033 	int mode, ret;
1034 
1035 	port_dn = cd->port_dn[p->port];
1036 	mode = of_get_phy_mode(port_dn);
1037 	if (mode < 0)
1038 		mode = PHY_INTERFACE_MODE_NA;
1039 	p->phy_interface = mode;
1040 
1041 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1042 	if (of_phy_is_fixed_link(port_dn)) {
1043 		/* In the case of a fixed PHY, the DT node associated
1044 		 * to the fixed PHY is the Port DT node
1045 		 */
1046 		ret = of_phy_register_fixed_link(port_dn);
1047 		if (ret) {
1048 			netdev_err(slave_dev, "failed to register fixed PHY\n");
1049 			return ret;
1050 		}
1051 		phy_is_fixed = true;
1052 		phy_dn = port_dn;
1053 	}
1054 
1055 	if (ds->drv->get_phy_flags)
1056 		phy_flags = ds->drv->get_phy_flags(ds, p->port);
1057 
1058 	if (phy_dn) {
1059 		ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1060 		/* If this PHY address is part of phys_mii_mask, which means
1061 		 * that we need to divert reads and writes to/from it, then we
1062 		 * want to bind this device using the slave MII bus created by
1063 		 * DSA to make that happen.
1064 		 */
1065 		if (!phy_is_fixed && ret >= 0 &&
1066 		    (ds->phys_mii_mask & (1 << ret))) {
1067 			ret = dsa_slave_phy_connect(p, slave_dev, ret);
1068 			if (ret)
1069 				return ret;
1070 		} else {
1071 			p->phy = of_phy_connect(slave_dev, phy_dn,
1072 						dsa_slave_adjust_link,
1073 						phy_flags,
1074 						p->phy_interface);
1075 		}
1076 	}
1077 
1078 	if (p->phy && phy_is_fixed)
1079 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1080 
1081 	/* We could not connect to a designated PHY, so use the switch internal
1082 	 * MDIO bus instead
1083 	 */
1084 	if (!p->phy) {
1085 		ret = dsa_slave_phy_connect(p, slave_dev, p->port);
1086 		if (ret)
1087 			return ret;
1088 	} else {
1089 		netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
1090 			    p->phy->addr, p->phy->drv->name);
1091 	}
1092 
1093 	return 0;
1094 }
1095 
1096 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1097 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1098 					    struct netdev_queue *txq,
1099 					    void *_unused)
1100 {
1101 	lockdep_set_class(&txq->_xmit_lock,
1102 			  &dsa_slave_netdev_xmit_lock_key);
1103 }
1104 
1105 int dsa_slave_suspend(struct net_device *slave_dev)
1106 {
1107 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1108 
1109 	if (p->phy) {
1110 		phy_stop(p->phy);
1111 		p->old_pause = -1;
1112 		p->old_link = -1;
1113 		p->old_duplex = -1;
1114 		phy_suspend(p->phy);
1115 	}
1116 
1117 	return 0;
1118 }
1119 
1120 int dsa_slave_resume(struct net_device *slave_dev)
1121 {
1122 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1123 
1124 	netif_device_attach(slave_dev);
1125 
1126 	if (p->phy) {
1127 		phy_resume(p->phy);
1128 		phy_start(p->phy);
1129 	}
1130 
1131 	return 0;
1132 }
1133 
1134 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1135 		     int port, char *name)
1136 {
1137 	struct net_device *master = ds->dst->master_netdev;
1138 	struct net_device *slave_dev;
1139 	struct dsa_slave_priv *p;
1140 	int ret;
1141 
1142 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1143 				 NET_NAME_UNKNOWN, ether_setup);
1144 	if (slave_dev == NULL)
1145 		return -ENOMEM;
1146 
1147 	slave_dev->features = master->vlan_features;
1148 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1149 	eth_hw_addr_inherit(slave_dev, master);
1150 	slave_dev->priv_flags |= IFF_NO_QUEUE;
1151 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1152 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1153 
1154 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1155 				 NULL);
1156 
1157 	SET_NETDEV_DEV(slave_dev, parent);
1158 	slave_dev->dev.of_node = ds->pd->port_dn[port];
1159 	slave_dev->vlan_features = master->vlan_features;
1160 
1161 	p = netdev_priv(slave_dev);
1162 	p->dev = slave_dev;
1163 	p->parent = ds;
1164 	p->port = port;
1165 
1166 	switch (ds->dst->tag_protocol) {
1167 #ifdef CONFIG_NET_DSA_TAG_DSA
1168 	case DSA_TAG_PROTO_DSA:
1169 		p->xmit = dsa_netdev_ops.xmit;
1170 		break;
1171 #endif
1172 #ifdef CONFIG_NET_DSA_TAG_EDSA
1173 	case DSA_TAG_PROTO_EDSA:
1174 		p->xmit = edsa_netdev_ops.xmit;
1175 		break;
1176 #endif
1177 #ifdef CONFIG_NET_DSA_TAG_TRAILER
1178 	case DSA_TAG_PROTO_TRAILER:
1179 		p->xmit = trailer_netdev_ops.xmit;
1180 		break;
1181 #endif
1182 #ifdef CONFIG_NET_DSA_TAG_BRCM
1183 	case DSA_TAG_PROTO_BRCM:
1184 		p->xmit = brcm_netdev_ops.xmit;
1185 		break;
1186 #endif
1187 	default:
1188 		p->xmit	= dsa_slave_notag_xmit;
1189 		break;
1190 	}
1191 
1192 	p->old_pause = -1;
1193 	p->old_link = -1;
1194 	p->old_duplex = -1;
1195 
1196 	ret = dsa_slave_phy_setup(p, slave_dev);
1197 	if (ret) {
1198 		free_netdev(slave_dev);
1199 		return ret;
1200 	}
1201 
1202 	ds->ports[port] = slave_dev;
1203 	ret = register_netdev(slave_dev);
1204 	if (ret) {
1205 		netdev_err(master, "error %d registering interface %s\n",
1206 			   ret, slave_dev->name);
1207 		phy_disconnect(p->phy);
1208 		ds->ports[port] = NULL;
1209 		free_netdev(slave_dev);
1210 		return ret;
1211 	}
1212 
1213 	netif_carrier_off(slave_dev);
1214 
1215 	return 0;
1216 }
1217 
1218 static bool dsa_slave_dev_check(struct net_device *dev)
1219 {
1220 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1221 }
1222 
1223 static int dsa_slave_master_changed(struct net_device *dev)
1224 {
1225 	struct net_device *master = netdev_master_upper_dev_get(dev);
1226 	struct dsa_slave_priv *p = netdev_priv(dev);
1227 	int err = 0;
1228 
1229 	if (master && master->rtnl_link_ops &&
1230 	    !strcmp(master->rtnl_link_ops->kind, "bridge"))
1231 		err = dsa_slave_bridge_port_join(dev, master);
1232 	else if (dsa_port_is_bridged(p))
1233 		err = dsa_slave_bridge_port_leave(dev);
1234 
1235 	return err;
1236 }
1237 
1238 int dsa_slave_netdevice_event(struct notifier_block *unused,
1239 			      unsigned long event, void *ptr)
1240 {
1241 	struct net_device *dev;
1242 	int err = 0;
1243 
1244 	switch (event) {
1245 	case NETDEV_CHANGEUPPER:
1246 		dev = netdev_notifier_info_to_dev(ptr);
1247 		if (!dsa_slave_dev_check(dev))
1248 			goto out;
1249 
1250 		err = dsa_slave_master_changed(dev);
1251 		if (err)
1252 			netdev_warn(dev, "failed to reflect master change\n");
1253 
1254 		break;
1255 	}
1256 
1257 out:
1258 	return NOTIFY_DONE;
1259 }
1260