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