net: dsa: change scope of MDB handlers
[sfrench/cifs-2.6.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 <linux/list.h>
20 #include <net/rtnetlink.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_mirred.h>
23 #include <linux/if_bridge.h>
24 #include <linux/netpoll.h>
25
26 #include "dsa_priv.h"
27
28 static bool dsa_slave_dev_check(struct net_device *dev);
29
30 static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v)
31 {
32         struct raw_notifier_head *nh = &dp->ds->dst->nh;
33         int err;
34
35         err = raw_notifier_call_chain(nh, e, v);
36
37         return notifier_to_errno(err);
38 }
39
40 /* slave mii_bus handling ***************************************************/
41 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
42 {
43         struct dsa_switch *ds = bus->priv;
44
45         if (ds->phys_mii_mask & (1 << addr))
46                 return ds->ops->phy_read(ds, addr, reg);
47
48         return 0xffff;
49 }
50
51 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
52 {
53         struct dsa_switch *ds = bus->priv;
54
55         if (ds->phys_mii_mask & (1 << addr))
56                 return ds->ops->phy_write(ds, addr, reg, val);
57
58         return 0;
59 }
60
61 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
62 {
63         ds->slave_mii_bus->priv = (void *)ds;
64         ds->slave_mii_bus->name = "dsa slave smi";
65         ds->slave_mii_bus->read = dsa_slave_phy_read;
66         ds->slave_mii_bus->write = dsa_slave_phy_write;
67         snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
68                  ds->dst->tree, ds->index);
69         ds->slave_mii_bus->parent = ds->dev;
70         ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
71 }
72
73
74 /* slave device handling ****************************************************/
75 static int dsa_slave_get_iflink(const struct net_device *dev)
76 {
77         struct dsa_slave_priv *p = netdev_priv(dev);
78
79         return p->dp->ds->dst->master_netdev->ifindex;
80 }
81
82 static inline bool dsa_port_is_bridged(struct dsa_port *dp)
83 {
84         return !!dp->bridge_dev;
85 }
86
87 static int dsa_port_set_state(struct dsa_port *dp, u8 state,
88                               struct switchdev_trans *trans)
89 {
90         struct dsa_switch *ds = dp->ds;
91         int port = dp->index;
92
93         if (switchdev_trans_ph_prepare(trans))
94                 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
95
96         if (ds->ops->port_stp_state_set)
97                 ds->ops->port_stp_state_set(ds, port, state);
98
99         if (ds->ops->port_fast_age) {
100                 /* Fast age FDB entries or flush appropriate forwarding database
101                  * for the given port, if we are moving it from Learning or
102                  * Forwarding state, to Disabled or Blocking or Listening state.
103                  */
104
105                 if ((dp->stp_state == BR_STATE_LEARNING ||
106                      dp->stp_state == BR_STATE_FORWARDING) &&
107                     (state == BR_STATE_DISABLED ||
108                      state == BR_STATE_BLOCKING ||
109                      state == BR_STATE_LISTENING))
110                         ds->ops->port_fast_age(ds, port);
111         }
112
113         dp->stp_state = state;
114
115         return 0;
116 }
117
118 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
119 {
120         int err;
121
122         err = dsa_port_set_state(dp, state, NULL);
123         if (err)
124                 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
125 }
126
127 static int dsa_slave_open(struct net_device *dev)
128 {
129         struct dsa_slave_priv *p = netdev_priv(dev);
130         struct net_device *master = p->dp->ds->dst->master_netdev;
131         struct dsa_switch *ds = p->dp->ds;
132         u8 stp_state = dsa_port_is_bridged(p->dp) ?
133                         BR_STATE_BLOCKING : BR_STATE_FORWARDING;
134         int err;
135
136         if (!(master->flags & IFF_UP))
137                 return -ENETDOWN;
138
139         if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
140                 err = dev_uc_add(master, dev->dev_addr);
141                 if (err < 0)
142                         goto out;
143         }
144
145         if (dev->flags & IFF_ALLMULTI) {
146                 err = dev_set_allmulti(master, 1);
147                 if (err < 0)
148                         goto del_unicast;
149         }
150         if (dev->flags & IFF_PROMISC) {
151                 err = dev_set_promiscuity(master, 1);
152                 if (err < 0)
153                         goto clear_allmulti;
154         }
155
156         if (ds->ops->port_enable) {
157                 err = ds->ops->port_enable(ds, p->dp->index, p->phy);
158                 if (err)
159                         goto clear_promisc;
160         }
161
162         dsa_port_set_state_now(p->dp, stp_state);
163
164         if (p->phy)
165                 phy_start(p->phy);
166
167         return 0;
168
169 clear_promisc:
170         if (dev->flags & IFF_PROMISC)
171                 dev_set_promiscuity(master, -1);
172 clear_allmulti:
173         if (dev->flags & IFF_ALLMULTI)
174                 dev_set_allmulti(master, -1);
175 del_unicast:
176         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
177                 dev_uc_del(master, dev->dev_addr);
178 out:
179         return err;
180 }
181
182 static int dsa_slave_close(struct net_device *dev)
183 {
184         struct dsa_slave_priv *p = netdev_priv(dev);
185         struct net_device *master = p->dp->ds->dst->master_netdev;
186         struct dsa_switch *ds = p->dp->ds;
187
188         if (p->phy)
189                 phy_stop(p->phy);
190
191         dev_mc_unsync(master, dev);
192         dev_uc_unsync(master, dev);
193         if (dev->flags & IFF_ALLMULTI)
194                 dev_set_allmulti(master, -1);
195         if (dev->flags & IFF_PROMISC)
196                 dev_set_promiscuity(master, -1);
197
198         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
199                 dev_uc_del(master, dev->dev_addr);
200
201         if (ds->ops->port_disable)
202                 ds->ops->port_disable(ds, p->dp->index, p->phy);
203
204         dsa_port_set_state_now(p->dp, BR_STATE_DISABLED);
205
206         return 0;
207 }
208
209 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
210 {
211         struct dsa_slave_priv *p = netdev_priv(dev);
212         struct net_device *master = p->dp->ds->dst->master_netdev;
213
214         if (change & IFF_ALLMULTI)
215                 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
216         if (change & IFF_PROMISC)
217                 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
218 }
219
220 static void dsa_slave_set_rx_mode(struct net_device *dev)
221 {
222         struct dsa_slave_priv *p = netdev_priv(dev);
223         struct net_device *master = p->dp->ds->dst->master_netdev;
224
225         dev_mc_sync(master, dev);
226         dev_uc_sync(master, dev);
227 }
228
229 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
230 {
231         struct dsa_slave_priv *p = netdev_priv(dev);
232         struct net_device *master = p->dp->ds->dst->master_netdev;
233         struct sockaddr *addr = a;
234         int err;
235
236         if (!is_valid_ether_addr(addr->sa_data))
237                 return -EADDRNOTAVAIL;
238
239         if (!(dev->flags & IFF_UP))
240                 goto out;
241
242         if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
243                 err = dev_uc_add(master, addr->sa_data);
244                 if (err < 0)
245                         return err;
246         }
247
248         if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
249                 dev_uc_del(master, dev->dev_addr);
250
251 out:
252         ether_addr_copy(dev->dev_addr, addr->sa_data);
253
254         return 0;
255 }
256
257 static int dsa_slave_port_vlan_add(struct net_device *dev,
258                                    const struct switchdev_obj_port_vlan *vlan,
259                                    struct switchdev_trans *trans)
260 {
261         struct dsa_slave_priv *p = netdev_priv(dev);
262         struct dsa_port *dp = p->dp;
263         struct dsa_switch *ds = dp->ds;
264
265         if (switchdev_trans_ph_prepare(trans)) {
266                 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
267                         return -EOPNOTSUPP;
268
269                 return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
270         }
271
272         ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
273
274         return 0;
275 }
276
277 static int dsa_slave_port_vlan_del(struct net_device *dev,
278                                    const struct switchdev_obj_port_vlan *vlan)
279 {
280         struct dsa_slave_priv *p = netdev_priv(dev);
281         struct dsa_switch *ds = p->dp->ds;
282
283         if (!ds->ops->port_vlan_del)
284                 return -EOPNOTSUPP;
285
286         return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
287 }
288
289 static int dsa_slave_port_vlan_dump(struct net_device *dev,
290                                     struct switchdev_obj_port_vlan *vlan,
291                                     switchdev_obj_dump_cb_t *cb)
292 {
293         struct dsa_slave_priv *p = netdev_priv(dev);
294         struct dsa_switch *ds = p->dp->ds;
295
296         if (ds->ops->port_vlan_dump)
297                 return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
298
299         return -EOPNOTSUPP;
300 }
301
302 static int dsa_port_fdb_add(struct dsa_port *dp,
303                             const struct switchdev_obj_port_fdb *fdb,
304                             struct switchdev_trans *trans)
305 {
306         struct dsa_switch *ds = dp->ds;
307
308         if (switchdev_trans_ph_prepare(trans)) {
309                 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
310                         return -EOPNOTSUPP;
311
312                 return ds->ops->port_fdb_prepare(ds, dp->index, fdb, trans);
313         }
314
315         ds->ops->port_fdb_add(ds, dp->index, fdb, trans);
316
317         return 0;
318 }
319
320 static int dsa_port_fdb_del(struct dsa_port *dp,
321                             const struct switchdev_obj_port_fdb *fdb)
322 {
323         struct dsa_switch *ds = dp->ds;
324         int ret = -EOPNOTSUPP;
325
326         if (ds->ops->port_fdb_del)
327                 ret = ds->ops->port_fdb_del(ds, dp->index, fdb);
328
329         return ret;
330 }
331
332 static int dsa_port_fdb_dump(struct dsa_port *dp,
333                              struct switchdev_obj_port_fdb *fdb,
334                              switchdev_obj_dump_cb_t *cb)
335 {
336         struct dsa_switch *ds = dp->ds;
337
338         if (ds->ops->port_fdb_dump)
339                 return ds->ops->port_fdb_dump(ds, dp->index, fdb, cb);
340
341         return -EOPNOTSUPP;
342 }
343
344 static int dsa_port_mdb_add(struct dsa_port *dp,
345                             const struct switchdev_obj_port_mdb *mdb,
346                             struct switchdev_trans *trans)
347 {
348         struct dsa_switch *ds = dp->ds;
349
350         if (switchdev_trans_ph_prepare(trans)) {
351                 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
352                         return -EOPNOTSUPP;
353
354                 return ds->ops->port_mdb_prepare(ds, dp->index, mdb, trans);
355         }
356
357         ds->ops->port_mdb_add(ds, dp->index, mdb, trans);
358
359         return 0;
360 }
361
362 static int dsa_port_mdb_del(struct dsa_port *dp,
363                             const struct switchdev_obj_port_mdb *mdb)
364 {
365         struct dsa_switch *ds = dp->ds;
366
367         if (ds->ops->port_mdb_del)
368                 return ds->ops->port_mdb_del(ds, dp->index, mdb);
369
370         return -EOPNOTSUPP;
371 }
372
373 static int dsa_port_mdb_dump(struct dsa_port *dp,
374                              struct switchdev_obj_port_mdb *mdb,
375                              switchdev_obj_dump_cb_t *cb)
376 {
377         struct dsa_switch *ds = dp->ds;
378
379         if (ds->ops->port_mdb_dump)
380                 return ds->ops->port_mdb_dump(ds, dp->index, mdb, cb);
381
382         return -EOPNOTSUPP;
383 }
384
385 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
386 {
387         struct dsa_slave_priv *p = netdev_priv(dev);
388
389         if (p->phy != NULL)
390                 return phy_mii_ioctl(p->phy, ifr, cmd);
391
392         return -EOPNOTSUPP;
393 }
394
395 static int dsa_slave_vlan_filtering(struct net_device *dev,
396                                     const struct switchdev_attr *attr,
397                                     struct switchdev_trans *trans)
398 {
399         struct dsa_slave_priv *p = netdev_priv(dev);
400         struct dsa_switch *ds = p->dp->ds;
401
402         /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
403         if (switchdev_trans_ph_prepare(trans))
404                 return 0;
405
406         if (ds->ops->port_vlan_filtering)
407                 return ds->ops->port_vlan_filtering(ds, p->dp->index,
408                                                     attr->u.vlan_filtering);
409
410         return 0;
411 }
412
413 static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
414                                             unsigned int ageing_time)
415 {
416         int i;
417
418         for (i = 0; i < ds->num_ports; ++i) {
419                 struct dsa_port *dp = &ds->ports[i];
420
421                 if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
422                         ageing_time = dp->ageing_time;
423         }
424
425         return ageing_time;
426 }
427
428 static int dsa_slave_ageing_time(struct net_device *dev,
429                                  const struct switchdev_attr *attr,
430                                  struct switchdev_trans *trans)
431 {
432         struct dsa_slave_priv *p = netdev_priv(dev);
433         struct dsa_switch *ds = p->dp->ds;
434         unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
435         unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
436
437         if (switchdev_trans_ph_prepare(trans)) {
438                 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
439                         return -ERANGE;
440                 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
441                         return -ERANGE;
442                 return 0;
443         }
444
445         /* Keep the fastest ageing time in case of multiple bridges */
446         p->dp->ageing_time = ageing_time;
447         ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
448
449         if (ds->ops->set_ageing_time)
450                 return ds->ops->set_ageing_time(ds, ageing_time);
451
452         return 0;
453 }
454
455 static int dsa_slave_port_attr_set(struct net_device *dev,
456                                    const struct switchdev_attr *attr,
457                                    struct switchdev_trans *trans)
458 {
459         struct dsa_slave_priv *p = netdev_priv(dev);
460         struct dsa_port *dp = p->dp;
461         int ret;
462
463         switch (attr->id) {
464         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
465                 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
466                 break;
467         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
468                 ret = dsa_slave_vlan_filtering(dev, attr, trans);
469                 break;
470         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
471                 ret = dsa_slave_ageing_time(dev, attr, trans);
472                 break;
473         default:
474                 ret = -EOPNOTSUPP;
475                 break;
476         }
477
478         return ret;
479 }
480
481 static int dsa_slave_port_obj_add(struct net_device *dev,
482                                   const struct switchdev_obj *obj,
483                                   struct switchdev_trans *trans)
484 {
485         struct dsa_slave_priv *p = netdev_priv(dev);
486         struct dsa_port *dp = p->dp;
487         int err;
488
489         /* For the prepare phase, ensure the full set of changes is feasable in
490          * one go in order to signal a failure properly. If an operation is not
491          * supported, return -EOPNOTSUPP.
492          */
493
494         switch (obj->id) {
495         case SWITCHDEV_OBJ_ID_PORT_FDB:
496                 err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
497                 break;
498         case SWITCHDEV_OBJ_ID_PORT_MDB:
499                 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
500                 break;
501         case SWITCHDEV_OBJ_ID_PORT_VLAN:
502                 err = dsa_slave_port_vlan_add(dev,
503                                               SWITCHDEV_OBJ_PORT_VLAN(obj),
504                                               trans);
505                 break;
506         default:
507                 err = -EOPNOTSUPP;
508                 break;
509         }
510
511         return err;
512 }
513
514 static int dsa_slave_port_obj_del(struct net_device *dev,
515                                   const struct switchdev_obj *obj)
516 {
517         struct dsa_slave_priv *p = netdev_priv(dev);
518         struct dsa_port *dp = p->dp;
519         int err;
520
521         switch (obj->id) {
522         case SWITCHDEV_OBJ_ID_PORT_FDB:
523                 err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
524                 break;
525         case SWITCHDEV_OBJ_ID_PORT_MDB:
526                 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
527                 break;
528         case SWITCHDEV_OBJ_ID_PORT_VLAN:
529                 err = dsa_slave_port_vlan_del(dev,
530                                               SWITCHDEV_OBJ_PORT_VLAN(obj));
531                 break;
532         default:
533                 err = -EOPNOTSUPP;
534                 break;
535         }
536
537         return err;
538 }
539
540 static int dsa_slave_port_obj_dump(struct net_device *dev,
541                                    struct switchdev_obj *obj,
542                                    switchdev_obj_dump_cb_t *cb)
543 {
544         struct dsa_slave_priv *p = netdev_priv(dev);
545         struct dsa_port *dp = p->dp;
546         int err;
547
548         switch (obj->id) {
549         case SWITCHDEV_OBJ_ID_PORT_FDB:
550                 err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
551                 break;
552         case SWITCHDEV_OBJ_ID_PORT_MDB:
553                 err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb);
554                 break;
555         case SWITCHDEV_OBJ_ID_PORT_VLAN:
556                 err = dsa_slave_port_vlan_dump(dev,
557                                                SWITCHDEV_OBJ_PORT_VLAN(obj),
558                                                cb);
559                 break;
560         default:
561                 err = -EOPNOTSUPP;
562                 break;
563         }
564
565         return err;
566 }
567
568 static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
569 {
570         struct dsa_notifier_bridge_info info = {
571                 .sw_index = dp->ds->index,
572                 .port = dp->index,
573                 .br = br,
574         };
575         int err;
576
577         /* Here the port is already bridged. Reflect the current configuration
578          * so that drivers can program their chips accordingly.
579          */
580         dp->bridge_dev = br;
581
582         err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
583
584         /* The bridging is rolled back on error */
585         if (err)
586                 dp->bridge_dev = NULL;
587
588         return err;
589 }
590
591 static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
592 {
593         struct dsa_notifier_bridge_info info = {
594                 .sw_index = dp->ds->index,
595                 .port = dp->index,
596                 .br = br,
597         };
598         int err;
599
600         /* Here the port is already unbridged. Reflect the current configuration
601          * so that drivers can program their chips accordingly.
602          */
603         dp->bridge_dev = NULL;
604
605         err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
606         if (err)
607                 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
608
609         /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
610          * so allow it to be in BR_STATE_FORWARDING to be kept functional
611          */
612         dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
613 }
614
615 static int dsa_slave_port_attr_get(struct net_device *dev,
616                                    struct switchdev_attr *attr)
617 {
618         struct dsa_slave_priv *p = netdev_priv(dev);
619         struct dsa_switch *ds = p->dp->ds;
620
621         switch (attr->id) {
622         case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
623                 attr->u.ppid.id_len = sizeof(ds->index);
624                 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
625                 break;
626         default:
627                 return -EOPNOTSUPP;
628         }
629
630         return 0;
631 }
632
633 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
634                                                struct sk_buff *skb)
635 {
636 #ifdef CONFIG_NET_POLL_CONTROLLER
637         if (p->netpoll)
638                 netpoll_send_skb(p->netpoll, skb);
639 #else
640         BUG();
641 #endif
642         return NETDEV_TX_OK;
643 }
644
645 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
646 {
647         struct dsa_slave_priv *p = netdev_priv(dev);
648         struct sk_buff *nskb;
649
650         dev->stats.tx_packets++;
651         dev->stats.tx_bytes += skb->len;
652
653         /* Transmit function may have to reallocate the original SKB */
654         nskb = p->xmit(skb, dev);
655         if (!nskb)
656                 return NETDEV_TX_OK;
657
658         /* SKB for netpoll still need to be mangled with the protocol-specific
659          * tag to be successfully transmitted
660          */
661         if (unlikely(netpoll_tx_running(dev)))
662                 return dsa_netpoll_send_skb(p, nskb);
663
664         /* Queue the SKB for transmission on the parent interface, but
665          * do not modify its EtherType
666          */
667         nskb->dev = p->dp->ds->dst->master_netdev;
668         dev_queue_xmit(nskb);
669
670         return NETDEV_TX_OK;
671 }
672
673 /* ethtool operations *******************************************************/
674 static int
675 dsa_slave_get_link_ksettings(struct net_device *dev,
676                              struct ethtool_link_ksettings *cmd)
677 {
678         struct dsa_slave_priv *p = netdev_priv(dev);
679         int err = -EOPNOTSUPP;
680
681         if (p->phy != NULL)
682                 err = phy_ethtool_ksettings_get(p->phy, cmd);
683
684         return err;
685 }
686
687 static int
688 dsa_slave_set_link_ksettings(struct net_device *dev,
689                              const struct ethtool_link_ksettings *cmd)
690 {
691         struct dsa_slave_priv *p = netdev_priv(dev);
692
693         if (p->phy != NULL)
694                 return phy_ethtool_ksettings_set(p->phy, cmd);
695
696         return -EOPNOTSUPP;
697 }
698
699 static void dsa_slave_get_drvinfo(struct net_device *dev,
700                                   struct ethtool_drvinfo *drvinfo)
701 {
702         strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
703         strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
704         strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
705 }
706
707 static int dsa_slave_get_regs_len(struct net_device *dev)
708 {
709         struct dsa_slave_priv *p = netdev_priv(dev);
710         struct dsa_switch *ds = p->dp->ds;
711
712         if (ds->ops->get_regs_len)
713                 return ds->ops->get_regs_len(ds, p->dp->index);
714
715         return -EOPNOTSUPP;
716 }
717
718 static void
719 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
720 {
721         struct dsa_slave_priv *p = netdev_priv(dev);
722         struct dsa_switch *ds = p->dp->ds;
723
724         if (ds->ops->get_regs)
725                 ds->ops->get_regs(ds, p->dp->index, regs, _p);
726 }
727
728 static int dsa_slave_nway_reset(struct net_device *dev)
729 {
730         struct dsa_slave_priv *p = netdev_priv(dev);
731
732         if (p->phy != NULL)
733                 return genphy_restart_aneg(p->phy);
734
735         return -EOPNOTSUPP;
736 }
737
738 static u32 dsa_slave_get_link(struct net_device *dev)
739 {
740         struct dsa_slave_priv *p = netdev_priv(dev);
741
742         if (p->phy != NULL) {
743                 genphy_update_link(p->phy);
744                 return p->phy->link;
745         }
746
747         return -EOPNOTSUPP;
748 }
749
750 static int dsa_slave_get_eeprom_len(struct net_device *dev)
751 {
752         struct dsa_slave_priv *p = netdev_priv(dev);
753         struct dsa_switch *ds = p->dp->ds;
754
755         if (ds->cd && ds->cd->eeprom_len)
756                 return ds->cd->eeprom_len;
757
758         if (ds->ops->get_eeprom_len)
759                 return ds->ops->get_eeprom_len(ds);
760
761         return 0;
762 }
763
764 static int dsa_slave_get_eeprom(struct net_device *dev,
765                                 struct ethtool_eeprom *eeprom, u8 *data)
766 {
767         struct dsa_slave_priv *p = netdev_priv(dev);
768         struct dsa_switch *ds = p->dp->ds;
769
770         if (ds->ops->get_eeprom)
771                 return ds->ops->get_eeprom(ds, eeprom, data);
772
773         return -EOPNOTSUPP;
774 }
775
776 static int dsa_slave_set_eeprom(struct net_device *dev,
777                                 struct ethtool_eeprom *eeprom, u8 *data)
778 {
779         struct dsa_slave_priv *p = netdev_priv(dev);
780         struct dsa_switch *ds = p->dp->ds;
781
782         if (ds->ops->set_eeprom)
783                 return ds->ops->set_eeprom(ds, eeprom, data);
784
785         return -EOPNOTSUPP;
786 }
787
788 static void dsa_slave_get_strings(struct net_device *dev,
789                                   uint32_t stringset, uint8_t *data)
790 {
791         struct dsa_slave_priv *p = netdev_priv(dev);
792         struct dsa_switch *ds = p->dp->ds;
793
794         if (stringset == ETH_SS_STATS) {
795                 int len = ETH_GSTRING_LEN;
796
797                 strncpy(data, "tx_packets", len);
798                 strncpy(data + len, "tx_bytes", len);
799                 strncpy(data + 2 * len, "rx_packets", len);
800                 strncpy(data + 3 * len, "rx_bytes", len);
801                 if (ds->ops->get_strings)
802                         ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
803         }
804 }
805
806 static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
807                                            struct ethtool_stats *stats,
808                                            uint64_t *data)
809 {
810         struct dsa_switch_tree *dst = dev->dsa_ptr;
811         struct dsa_switch *ds = dst->cpu_dp->ds;
812         s8 cpu_port = dst->cpu_dp->index;
813         int count = 0;
814
815         if (dst->master_ethtool_ops.get_sset_count) {
816                 count = dst->master_ethtool_ops.get_sset_count(dev,
817                                                                ETH_SS_STATS);
818                 dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
819         }
820
821         if (ds->ops->get_ethtool_stats)
822                 ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
823 }
824
825 static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
826 {
827         struct dsa_switch_tree *dst = dev->dsa_ptr;
828         struct dsa_switch *ds = dst->cpu_dp->ds;
829         int count = 0;
830
831         if (dst->master_ethtool_ops.get_sset_count)
832                 count += dst->master_ethtool_ops.get_sset_count(dev, sset);
833
834         if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
835                 count += ds->ops->get_sset_count(ds);
836
837         return count;
838 }
839
840 static void dsa_cpu_port_get_strings(struct net_device *dev,
841                                      uint32_t stringset, uint8_t *data)
842 {
843         struct dsa_switch_tree *dst = dev->dsa_ptr;
844         struct dsa_switch *ds = dst->cpu_dp->ds;
845         s8 cpu_port = dst->cpu_dp->index;
846         int len = ETH_GSTRING_LEN;
847         int mcount = 0, count;
848         unsigned int i;
849         uint8_t pfx[4];
850         uint8_t *ndata;
851
852         snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
853         /* We do not want to be NULL-terminated, since this is a prefix */
854         pfx[sizeof(pfx) - 1] = '_';
855
856         if (dst->master_ethtool_ops.get_sset_count) {
857                 mcount = dst->master_ethtool_ops.get_sset_count(dev,
858                                                                 ETH_SS_STATS);
859                 dst->master_ethtool_ops.get_strings(dev, stringset, data);
860         }
861
862         if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
863                 ndata = data + mcount * len;
864                 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
865                  * the output after to prepend our CPU port prefix we
866                  * constructed earlier
867                  */
868                 ds->ops->get_strings(ds, cpu_port, ndata);
869                 count = ds->ops->get_sset_count(ds);
870                 for (i = 0; i < count; i++) {
871                         memmove(ndata + (i * len + sizeof(pfx)),
872                                 ndata + i * len, len - sizeof(pfx));
873                         memcpy(ndata + i * len, pfx, sizeof(pfx));
874                 }
875         }
876 }
877
878 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
879                                         struct ethtool_stats *stats,
880                                         uint64_t *data)
881 {
882         struct dsa_slave_priv *p = netdev_priv(dev);
883         struct dsa_switch *ds = p->dp->ds;
884
885         data[0] = dev->stats.tx_packets;
886         data[1] = dev->stats.tx_bytes;
887         data[2] = dev->stats.rx_packets;
888         data[3] = dev->stats.rx_bytes;
889         if (ds->ops->get_ethtool_stats)
890                 ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
891 }
892
893 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
894 {
895         struct dsa_slave_priv *p = netdev_priv(dev);
896         struct dsa_switch *ds = p->dp->ds;
897
898         if (sset == ETH_SS_STATS) {
899                 int count;
900
901                 count = 4;
902                 if (ds->ops->get_sset_count)
903                         count += ds->ops->get_sset_count(ds);
904
905                 return count;
906         }
907
908         return -EOPNOTSUPP;
909 }
910
911 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
912 {
913         struct dsa_slave_priv *p = netdev_priv(dev);
914         struct dsa_switch *ds = p->dp->ds;
915
916         if (ds->ops->get_wol)
917                 ds->ops->get_wol(ds, p->dp->index, w);
918 }
919
920 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
921 {
922         struct dsa_slave_priv *p = netdev_priv(dev);
923         struct dsa_switch *ds = p->dp->ds;
924         int ret = -EOPNOTSUPP;
925
926         if (ds->ops->set_wol)
927                 ret = ds->ops->set_wol(ds, p->dp->index, w);
928
929         return ret;
930 }
931
932 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
933 {
934         struct dsa_slave_priv *p = netdev_priv(dev);
935         struct dsa_switch *ds = p->dp->ds;
936         int ret;
937
938         if (!ds->ops->set_eee)
939                 return -EOPNOTSUPP;
940
941         ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
942         if (ret)
943                 return ret;
944
945         if (p->phy)
946                 ret = phy_ethtool_set_eee(p->phy, e);
947
948         return ret;
949 }
950
951 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
952 {
953         struct dsa_slave_priv *p = netdev_priv(dev);
954         struct dsa_switch *ds = p->dp->ds;
955         int ret;
956
957         if (!ds->ops->get_eee)
958                 return -EOPNOTSUPP;
959
960         ret = ds->ops->get_eee(ds, p->dp->index, e);
961         if (ret)
962                 return ret;
963
964         if (p->phy)
965                 ret = phy_ethtool_get_eee(p->phy, e);
966
967         return ret;
968 }
969
970 #ifdef CONFIG_NET_POLL_CONTROLLER
971 static int dsa_slave_netpoll_setup(struct net_device *dev,
972                                    struct netpoll_info *ni)
973 {
974         struct dsa_slave_priv *p = netdev_priv(dev);
975         struct dsa_switch *ds = p->dp->ds;
976         struct net_device *master = ds->dst->master_netdev;
977         struct netpoll *netpoll;
978         int err = 0;
979
980         netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
981         if (!netpoll)
982                 return -ENOMEM;
983
984         err = __netpoll_setup(netpoll, master);
985         if (err) {
986                 kfree(netpoll);
987                 goto out;
988         }
989
990         p->netpoll = netpoll;
991 out:
992         return err;
993 }
994
995 static void dsa_slave_netpoll_cleanup(struct net_device *dev)
996 {
997         struct dsa_slave_priv *p = netdev_priv(dev);
998         struct netpoll *netpoll = p->netpoll;
999
1000         if (!netpoll)
1001                 return;
1002
1003         p->netpoll = NULL;
1004
1005         __netpoll_free_async(netpoll);
1006 }
1007
1008 static void dsa_slave_poll_controller(struct net_device *dev)
1009 {
1010 }
1011 #endif
1012
1013 static int dsa_slave_get_phys_port_name(struct net_device *dev,
1014                                         char *name, size_t len)
1015 {
1016         struct dsa_slave_priv *p = netdev_priv(dev);
1017
1018         if (snprintf(name, len, "p%d", p->dp->index) >= len)
1019                 return -EINVAL;
1020
1021         return 0;
1022 }
1023
1024 static struct dsa_mall_tc_entry *
1025 dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1026                              unsigned long cookie)
1027 {
1028         struct dsa_mall_tc_entry *mall_tc_entry;
1029
1030         list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1031                 if (mall_tc_entry->cookie == cookie)
1032                         return mall_tc_entry;
1033
1034         return NULL;
1035 }
1036
1037 static int dsa_slave_add_cls_matchall(struct net_device *dev,
1038                                       __be16 protocol,
1039                                       struct tc_cls_matchall_offload *cls,
1040                                       bool ingress)
1041 {
1042         struct dsa_slave_priv *p = netdev_priv(dev);
1043         struct dsa_mall_tc_entry *mall_tc_entry;
1044         struct dsa_switch *ds = p->dp->ds;
1045         struct net *net = dev_net(dev);
1046         struct dsa_slave_priv *to_p;
1047         struct net_device *to_dev;
1048         const struct tc_action *a;
1049         int err = -EOPNOTSUPP;
1050         LIST_HEAD(actions);
1051         int ifindex;
1052
1053         if (!ds->ops->port_mirror_add)
1054                 return err;
1055
1056         if (!tc_single_action(cls->exts))
1057                 return err;
1058
1059         tcf_exts_to_list(cls->exts, &actions);
1060         a = list_first_entry(&actions, struct tc_action, list);
1061
1062         if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1063                 struct dsa_mall_mirror_tc_entry *mirror;
1064
1065                 ifindex = tcf_mirred_ifindex(a);
1066                 to_dev = __dev_get_by_index(net, ifindex);
1067                 if (!to_dev)
1068                         return -EINVAL;
1069
1070                 if (!dsa_slave_dev_check(to_dev))
1071                         return -EOPNOTSUPP;
1072
1073                 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1074                 if (!mall_tc_entry)
1075                         return -ENOMEM;
1076
1077                 mall_tc_entry->cookie = cls->cookie;
1078                 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1079                 mirror = &mall_tc_entry->mirror;
1080
1081                 to_p = netdev_priv(to_dev);
1082
1083                 mirror->to_local_port = to_p->dp->index;
1084                 mirror->ingress = ingress;
1085
1086                 err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1087                                                ingress);
1088                 if (err) {
1089                         kfree(mall_tc_entry);
1090                         return err;
1091                 }
1092
1093                 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1094         }
1095
1096         return 0;
1097 }
1098
1099 static void dsa_slave_del_cls_matchall(struct net_device *dev,
1100                                        struct tc_cls_matchall_offload *cls)
1101 {
1102         struct dsa_slave_priv *p = netdev_priv(dev);
1103         struct dsa_mall_tc_entry *mall_tc_entry;
1104         struct dsa_switch *ds = p->dp->ds;
1105
1106         if (!ds->ops->port_mirror_del)
1107                 return;
1108
1109         mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1110         if (!mall_tc_entry)
1111                 return;
1112
1113         list_del(&mall_tc_entry->list);
1114
1115         switch (mall_tc_entry->type) {
1116         case DSA_PORT_MALL_MIRROR:
1117                 ds->ops->port_mirror_del(ds, p->dp->index,
1118                                          &mall_tc_entry->mirror);
1119                 break;
1120         default:
1121                 WARN_ON(1);
1122         }
1123
1124         kfree(mall_tc_entry);
1125 }
1126
1127 static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1128                               __be16 protocol, struct tc_to_netdev *tc)
1129 {
1130         bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1131         int ret = -EOPNOTSUPP;
1132
1133         switch (tc->type) {
1134         case TC_SETUP_MATCHALL:
1135                 switch (tc->cls_mall->command) {
1136                 case TC_CLSMATCHALL_REPLACE:
1137                         return dsa_slave_add_cls_matchall(dev, protocol,
1138                                                           tc->cls_mall,
1139                                                           ingress);
1140                 case TC_CLSMATCHALL_DESTROY:
1141                         dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1142                         return 0;
1143                 }
1144         default:
1145                 break;
1146         }
1147
1148         return ret;
1149 }
1150
1151 void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1152 {
1153         ops->get_sset_count = dsa_cpu_port_get_sset_count;
1154         ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1155         ops->get_strings = dsa_cpu_port_get_strings;
1156 }
1157
1158 static int dsa_slave_get_rxnfc(struct net_device *dev,
1159                                struct ethtool_rxnfc *nfc, u32 *rule_locs)
1160 {
1161         struct dsa_slave_priv *p = netdev_priv(dev);
1162         struct dsa_switch *ds = p->dp->ds;
1163
1164         if (!ds->ops->get_rxnfc)
1165                 return -EOPNOTSUPP;
1166
1167         return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1168 }
1169
1170 static int dsa_slave_set_rxnfc(struct net_device *dev,
1171                                struct ethtool_rxnfc *nfc)
1172 {
1173         struct dsa_slave_priv *p = netdev_priv(dev);
1174         struct dsa_switch *ds = p->dp->ds;
1175
1176         if (!ds->ops->set_rxnfc)
1177                 return -EOPNOTSUPP;
1178
1179         return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1180 }
1181
1182 static const struct ethtool_ops dsa_slave_ethtool_ops = {
1183         .get_drvinfo            = dsa_slave_get_drvinfo,
1184         .get_regs_len           = dsa_slave_get_regs_len,
1185         .get_regs               = dsa_slave_get_regs,
1186         .nway_reset             = dsa_slave_nway_reset,
1187         .get_link               = dsa_slave_get_link,
1188         .get_eeprom_len         = dsa_slave_get_eeprom_len,
1189         .get_eeprom             = dsa_slave_get_eeprom,
1190         .set_eeprom             = dsa_slave_set_eeprom,
1191         .get_strings            = dsa_slave_get_strings,
1192         .get_ethtool_stats      = dsa_slave_get_ethtool_stats,
1193         .get_sset_count         = dsa_slave_get_sset_count,
1194         .set_wol                = dsa_slave_set_wol,
1195         .get_wol                = dsa_slave_get_wol,
1196         .set_eee                = dsa_slave_set_eee,
1197         .get_eee                = dsa_slave_get_eee,
1198         .get_link_ksettings     = dsa_slave_get_link_ksettings,
1199         .set_link_ksettings     = dsa_slave_set_link_ksettings,
1200         .get_rxnfc              = dsa_slave_get_rxnfc,
1201         .set_rxnfc              = dsa_slave_set_rxnfc,
1202 };
1203
1204 static const struct net_device_ops dsa_slave_netdev_ops = {
1205         .ndo_open               = dsa_slave_open,
1206         .ndo_stop               = dsa_slave_close,
1207         .ndo_start_xmit         = dsa_slave_xmit,
1208         .ndo_change_rx_flags    = dsa_slave_change_rx_flags,
1209         .ndo_set_rx_mode        = dsa_slave_set_rx_mode,
1210         .ndo_set_mac_address    = dsa_slave_set_mac_address,
1211         .ndo_fdb_add            = switchdev_port_fdb_add,
1212         .ndo_fdb_del            = switchdev_port_fdb_del,
1213         .ndo_fdb_dump           = switchdev_port_fdb_dump,
1214         .ndo_do_ioctl           = dsa_slave_ioctl,
1215         .ndo_get_iflink         = dsa_slave_get_iflink,
1216 #ifdef CONFIG_NET_POLL_CONTROLLER
1217         .ndo_netpoll_setup      = dsa_slave_netpoll_setup,
1218         .ndo_netpoll_cleanup    = dsa_slave_netpoll_cleanup,
1219         .ndo_poll_controller    = dsa_slave_poll_controller,
1220 #endif
1221         .ndo_bridge_getlink     = switchdev_port_bridge_getlink,
1222         .ndo_bridge_setlink     = switchdev_port_bridge_setlink,
1223         .ndo_bridge_dellink     = switchdev_port_bridge_dellink,
1224         .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
1225         .ndo_setup_tc           = dsa_slave_setup_tc,
1226 };
1227
1228 static const struct switchdev_ops dsa_slave_switchdev_ops = {
1229         .switchdev_port_attr_get        = dsa_slave_port_attr_get,
1230         .switchdev_port_attr_set        = dsa_slave_port_attr_set,
1231         .switchdev_port_obj_add         = dsa_slave_port_obj_add,
1232         .switchdev_port_obj_del         = dsa_slave_port_obj_del,
1233         .switchdev_port_obj_dump        = dsa_slave_port_obj_dump,
1234 };
1235
1236 static struct device_type dsa_type = {
1237         .name   = "dsa",
1238 };
1239
1240 static void dsa_slave_adjust_link(struct net_device *dev)
1241 {
1242         struct dsa_slave_priv *p = netdev_priv(dev);
1243         struct dsa_switch *ds = p->dp->ds;
1244         unsigned int status_changed = 0;
1245
1246         if (p->old_link != p->phy->link) {
1247                 status_changed = 1;
1248                 p->old_link = p->phy->link;
1249         }
1250
1251         if (p->old_duplex != p->phy->duplex) {
1252                 status_changed = 1;
1253                 p->old_duplex = p->phy->duplex;
1254         }
1255
1256         if (p->old_pause != p->phy->pause) {
1257                 status_changed = 1;
1258                 p->old_pause = p->phy->pause;
1259         }
1260
1261         if (ds->ops->adjust_link && status_changed)
1262                 ds->ops->adjust_link(ds, p->dp->index, p->phy);
1263
1264         if (status_changed)
1265                 phy_print_status(p->phy);
1266 }
1267
1268 static int dsa_slave_fixed_link_update(struct net_device *dev,
1269                                        struct fixed_phy_status *status)
1270 {
1271         struct dsa_slave_priv *p;
1272         struct dsa_switch *ds;
1273
1274         if (dev) {
1275                 p = netdev_priv(dev);
1276                 ds = p->dp->ds;
1277                 if (ds->ops->fixed_link_update)
1278                         ds->ops->fixed_link_update(ds, p->dp->index, status);
1279         }
1280
1281         return 0;
1282 }
1283
1284 /* slave device setup *******************************************************/
1285 static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1286                                  struct net_device *slave_dev,
1287                                  int addr)
1288 {
1289         struct dsa_switch *ds = p->dp->ds;
1290
1291         p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1292         if (!p->phy) {
1293                 netdev_err(slave_dev, "no phy at %d\n", addr);
1294                 return -ENODEV;
1295         }
1296
1297         /* Use already configured phy mode */
1298         if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1299                 p->phy_interface = p->phy->interface;
1300         return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1301                                   p->phy_interface);
1302 }
1303
1304 static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
1305                                 struct net_device *slave_dev)
1306 {
1307         struct dsa_switch *ds = p->dp->ds;
1308         struct device_node *phy_dn, *port_dn;
1309         bool phy_is_fixed = false;
1310         u32 phy_flags = 0;
1311         int mode, ret;
1312
1313         port_dn = p->dp->dn;
1314         mode = of_get_phy_mode(port_dn);
1315         if (mode < 0)
1316                 mode = PHY_INTERFACE_MODE_NA;
1317         p->phy_interface = mode;
1318
1319         phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1320         if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
1321                 /* In the case of a fixed PHY, the DT node associated
1322                  * to the fixed PHY is the Port DT node
1323                  */
1324                 ret = of_phy_register_fixed_link(port_dn);
1325                 if (ret) {
1326                         netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1327                         return ret;
1328                 }
1329                 phy_is_fixed = true;
1330                 phy_dn = of_node_get(port_dn);
1331         }
1332
1333         if (ds->ops->get_phy_flags)
1334                 phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
1335
1336         if (phy_dn) {
1337                 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1338
1339                 /* If this PHY address is part of phys_mii_mask, which means
1340                  * that we need to divert reads and writes to/from it, then we
1341                  * want to bind this device using the slave MII bus created by
1342                  * DSA to make that happen.
1343                  */
1344                 if (!phy_is_fixed && phy_id >= 0 &&
1345                     (ds->phys_mii_mask & (1 << phy_id))) {
1346                         ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1347                         if (ret) {
1348                                 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
1349                                 of_node_put(phy_dn);
1350                                 return ret;
1351                         }
1352                 } else {
1353                         p->phy = of_phy_connect(slave_dev, phy_dn,
1354                                                 dsa_slave_adjust_link,
1355                                                 phy_flags,
1356                                                 p->phy_interface);
1357                 }
1358
1359                 of_node_put(phy_dn);
1360         }
1361
1362         if (p->phy && phy_is_fixed)
1363                 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1364
1365         /* We could not connect to a designated PHY, so use the switch internal
1366          * MDIO bus instead
1367          */
1368         if (!p->phy) {
1369                 ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1370                 if (ret) {
1371                         netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1372                                    p->dp->index, ret);
1373                         if (phy_is_fixed)
1374                                 of_phy_deregister_fixed_link(port_dn);
1375                         return ret;
1376                 }
1377         }
1378
1379         phy_attached_info(p->phy);
1380
1381         return 0;
1382 }
1383
1384 static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1385 static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1386                                             struct netdev_queue *txq,
1387                                             void *_unused)
1388 {
1389         lockdep_set_class(&txq->_xmit_lock,
1390                           &dsa_slave_netdev_xmit_lock_key);
1391 }
1392
1393 int dsa_slave_suspend(struct net_device *slave_dev)
1394 {
1395         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1396
1397         netif_device_detach(slave_dev);
1398
1399         if (p->phy) {
1400                 phy_stop(p->phy);
1401                 p->old_pause = -1;
1402                 p->old_link = -1;
1403                 p->old_duplex = -1;
1404                 phy_suspend(p->phy);
1405         }
1406
1407         return 0;
1408 }
1409
1410 int dsa_slave_resume(struct net_device *slave_dev)
1411 {
1412         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1413
1414         netif_device_attach(slave_dev);
1415
1416         if (p->phy) {
1417                 phy_resume(p->phy);
1418                 phy_start(p->phy);
1419         }
1420
1421         return 0;
1422 }
1423
1424 int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1425                      int port, const char *name)
1426 {
1427         struct dsa_switch_tree *dst = ds->dst;
1428         struct net_device *master;
1429         struct net_device *slave_dev;
1430         struct dsa_slave_priv *p;
1431         int ret;
1432
1433         master = ds->dst->master_netdev;
1434         if (ds->master_netdev)
1435                 master = ds->master_netdev;
1436
1437         slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1438                                  NET_NAME_UNKNOWN, ether_setup);
1439         if (slave_dev == NULL)
1440                 return -ENOMEM;
1441
1442         slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1443         slave_dev->hw_features |= NETIF_F_HW_TC;
1444         slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1445         eth_hw_addr_inherit(slave_dev, master);
1446         slave_dev->priv_flags |= IFF_NO_QUEUE;
1447         slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1448         slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1449         slave_dev->min_mtu = 0;
1450         slave_dev->max_mtu = ETH_MAX_MTU;
1451         SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1452
1453         netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1454                                  NULL);
1455
1456         SET_NETDEV_DEV(slave_dev, parent);
1457         slave_dev->dev.of_node = ds->ports[port].dn;
1458         slave_dev->vlan_features = master->vlan_features;
1459
1460         p = netdev_priv(slave_dev);
1461         p->dp = &ds->ports[port];
1462         INIT_LIST_HEAD(&p->mall_tc_list);
1463         p->xmit = dst->tag_ops->xmit;
1464
1465         p->old_pause = -1;
1466         p->old_link = -1;
1467         p->old_duplex = -1;
1468
1469         ds->ports[port].netdev = slave_dev;
1470         ret = register_netdev(slave_dev);
1471         if (ret) {
1472                 netdev_err(master, "error %d registering interface %s\n",
1473                            ret, slave_dev->name);
1474                 ds->ports[port].netdev = NULL;
1475                 free_netdev(slave_dev);
1476                 return ret;
1477         }
1478
1479         netif_carrier_off(slave_dev);
1480
1481         ret = dsa_slave_phy_setup(p, slave_dev);
1482         if (ret) {
1483                 netdev_err(master, "error %d setting up slave phy\n", ret);
1484                 unregister_netdev(slave_dev);
1485                 free_netdev(slave_dev);
1486                 return ret;
1487         }
1488
1489         return 0;
1490 }
1491
1492 void dsa_slave_destroy(struct net_device *slave_dev)
1493 {
1494         struct dsa_slave_priv *p = netdev_priv(slave_dev);
1495         struct device_node *port_dn;
1496
1497         port_dn = p->dp->dn;
1498
1499         netif_carrier_off(slave_dev);
1500         if (p->phy) {
1501                 phy_disconnect(p->phy);
1502
1503                 if (of_phy_is_fixed_link(port_dn))
1504                         of_phy_deregister_fixed_link(port_dn);
1505         }
1506         unregister_netdev(slave_dev);
1507         free_netdev(slave_dev);
1508 }
1509
1510 static bool dsa_slave_dev_check(struct net_device *dev)
1511 {
1512         return dev->netdev_ops == &dsa_slave_netdev_ops;
1513 }
1514
1515 static int dsa_slave_changeupper(struct net_device *dev,
1516                                  struct netdev_notifier_changeupper_info *info)
1517 {
1518         struct dsa_slave_priv *p = netdev_priv(dev);
1519         struct dsa_port *dp = p->dp;
1520         int err = NOTIFY_DONE;
1521
1522         if (netif_is_bridge_master(info->upper_dev)) {
1523                 if (info->linking) {
1524                         err = dsa_port_bridge_join(dp, info->upper_dev);
1525                         err = notifier_from_errno(err);
1526                 } else {
1527                         dsa_port_bridge_leave(dp, info->upper_dev);
1528                         err = NOTIFY_OK;
1529                 }
1530         }
1531
1532         return err;
1533 }
1534
1535 static int dsa_slave_netdevice_event(struct notifier_block *nb,
1536                                      unsigned long event, void *ptr)
1537 {
1538         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1539
1540         if (dev->netdev_ops != &dsa_slave_netdev_ops)
1541                 return NOTIFY_DONE;
1542
1543         if (event == NETDEV_CHANGEUPPER)
1544                 return dsa_slave_changeupper(dev, ptr);
1545
1546         return NOTIFY_DONE;
1547 }
1548
1549 static struct notifier_block dsa_slave_nb __read_mostly = {
1550         .notifier_call  = dsa_slave_netdevice_event,
1551 };
1552
1553 int dsa_slave_register_notifier(void)
1554 {
1555         return register_netdevice_notifier(&dsa_slave_nb);
1556 }
1557
1558 void dsa_slave_unregister_notifier(void)
1559 {
1560         int err;
1561
1562         err = unregister_netdevice_notifier(&dsa_slave_nb);
1563         if (err)
1564                 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1565 }