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