blob: 7b1d9ec74e09289eee75340d1638f595170a42cb [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/slave.c - Slave device handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 *
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>
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080012#include <linux/etherdevice.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080013#include <linux/netdevice.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/phy.h>
Florian Fainellia2820542014-10-17 16:02:13 -070015#include <linux/phy_fixed.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070016#include <linux/of_net.h>
17#include <linux/of_mdio.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080018#include <net/rtnetlink.h>
Scott Feldman98237d42015-03-15 21:07:15 -070019#include <net/switchdev.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080020#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070021#include <linux/netpoll.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include "dsa_priv.h"
23
24/* slave mii_bus handling ***************************************************/
25static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
26{
27 struct dsa_switch *ds = bus->priv;
28
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070029 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000030 return ds->drv->phy_read(ds, addr, reg);
31
32 return 0xffff;
33}
34
35static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
36{
37 struct dsa_switch *ds = bus->priv;
38
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070039 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000040 return ds->drv->phy_write(ds, addr, reg, val);
41
42 return 0;
43}
44
45void dsa_slave_mii_bus_init(struct dsa_switch *ds)
46{
47 ds->slave_mii_bus->priv = (void *)ds;
48 ds->slave_mii_bus->name = "dsa slave smi";
49 ds->slave_mii_bus->read = dsa_slave_phy_read;
50 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainellif490be02013-01-21 09:58:50 +000051 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
52 ds->index, ds->pd->sw_addr);
Alexander Duyckb4d23942014-09-15 13:00:27 -040053 ds->slave_mii_bus->parent = ds->master_dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050054 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000055}
56
57
58/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020059static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000060{
61 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000062
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020063 return p->parent->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000064}
65
Florian Fainellib73adef2015-02-24 13:15:33 -080066static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
67{
68 return !!p->bridge_dev;
69}
70
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071static int dsa_slave_open(struct net_device *dev)
72{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080073 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +000074 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -070075 struct dsa_switch *ds = p->parent;
Florian Fainellib73adef2015-02-24 13:15:33 -080076 u8 stp_state = dsa_port_is_bridged(p) ?
77 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080078 int err;
79
80 if (!(master->flags & IFF_UP))
81 return -ENETDOWN;
82
Joe Perches8feedbb2012-05-08 18:56:57 +000083 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000084 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080085 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
Florian Fainellib2f2af22014-09-24 17:05:18 -0700100 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
Florian Fainellib73adef2015-02-24 13:15:33 -0800106 if (ds->drv->port_stp_update)
107 ds->drv->port_stp_update(ds, p->port, stp_state);
108
Florian Fainellif7f1de52014-09-24 17:05:17 -0700109 if (p->phy)
110 phy_start(p->phy);
111
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800113
Florian Fainellib2f2af22014-09-24 17:05:18 -0700114clear_promisc:
115 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300116 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800117clear_allmulti:
118 if (dev->flags & IFF_ALLMULTI)
119 dev_set_allmulti(master, -1);
120del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000121 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000122 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800123out:
124 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000125}
126
127static int dsa_slave_close(struct net_device *dev)
128{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800129 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000130 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -0700131 struct dsa_switch *ds = p->parent;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800132
Florian Fainellif7f1de52014-09-24 17:05:17 -0700133 if (p->phy)
134 phy_stop(p->phy);
135
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800136 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000137 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800138 if (dev->flags & IFF_ALLMULTI)
139 dev_set_allmulti(master, -1);
140 if (dev->flags & IFF_PROMISC)
141 dev_set_promiscuity(master, -1);
142
Joe Perches8feedbb2012-05-08 18:56:57 +0000143 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000144 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800145
Florian Fainellib2f2af22014-09-24 17:05:18 -0700146 if (ds->drv->port_disable)
147 ds->drv->port_disable(ds, p->port, p->phy);
148
Florian Fainellib73adef2015-02-24 13:15:33 -0800149 if (ds->drv->port_stp_update)
150 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
151
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152 return 0;
153}
154
155static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
156{
157 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000158 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000159
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
166static void dsa_slave_set_rx_mode(struct net_device *dev)
167{
168 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000169 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000170
171 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000172 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000173}
174
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800175static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000176{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800177 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000178 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800179 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
Joe Perches8feedbb2012-05-08 18:56:57 +0000188 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000189 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800190 if (err < 0)
191 return err;
192 }
193
Joe Perches8feedbb2012-05-08 18:56:57 +0000194 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000195 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800196
197out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800198 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000199
200 return 0;
201}
202
Vivien Didelot11149532015-08-13 12:52:17 -0400203static int dsa_bridge_check_vlan_range(struct dsa_switch *ds,
204 const struct net_device *bridge,
205 u16 vid_begin, u16 vid_end)
206{
207 struct dsa_slave_priv *p;
208 struct net_device *dev, *vlan_br;
209 DECLARE_BITMAP(members, DSA_MAX_PORTS);
210 DECLARE_BITMAP(untagged, DSA_MAX_PORTS);
211 u16 vid;
212 int member, err;
213
214 if (!ds->drv->vlan_getnext || !vid_begin)
215 return -EOPNOTSUPP;
216
217 vid = vid_begin - 1;
218
219 do {
220 err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
221 if (err)
222 break;
223
224 if (vid > vid_end)
225 break;
226
227 member = find_first_bit(members, DSA_MAX_PORTS);
228 if (member == DSA_MAX_PORTS)
229 continue;
230
231 dev = ds->ports[member];
232 p = netdev_priv(dev);
233 vlan_br = p->bridge_dev;
234 if (vlan_br == bridge)
235 continue;
236
237 netdev_dbg(vlan_br, "hardware VLAN %d already in use\n", vid);
238 return -EOPNOTSUPP;
239 } while (vid < vid_end);
240
241 return err == -ENOENT ? 0 : err;
242}
243
244static int dsa_slave_port_vlan_add(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400245 const struct switchdev_obj_vlan *vlan,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200246 struct switchdev_trans *trans)
Vivien Didelot11149532015-08-13 12:52:17 -0400247{
Vivien Didelot11149532015-08-13 12:52:17 -0400248 struct dsa_slave_priv *p = netdev_priv(dev);
249 struct dsa_switch *ds = p->parent;
250 u16 vid;
251 int err;
252
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200253 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot11149532015-08-13 12:52:17 -0400254 if (!ds->drv->port_vlan_add || !ds->drv->port_pvid_set)
255 return -EOPNOTSUPP;
256
257 /* If the requested port doesn't belong to the same bridge as
258 * the VLAN members, fallback to software VLAN (hopefully).
259 */
260 err = dsa_bridge_check_vlan_range(ds, p->bridge_dev,
261 vlan->vid_begin,
262 vlan->vid_end);
263 if (err)
264 return err;
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200265 } else {
Vivien Didelot11149532015-08-13 12:52:17 -0400266 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
267 err = ds->drv->port_vlan_add(ds, p->port, vid,
268 vlan->flags &
269 BRIDGE_VLAN_INFO_UNTAGGED);
270 if (!err && vlan->flags & BRIDGE_VLAN_INFO_PVID)
271 err = ds->drv->port_pvid_set(ds, p->port, vid);
272 if (err)
273 return err;
274 }
Vivien Didelot11149532015-08-13 12:52:17 -0400275 }
276
277 return 0;
278}
279
280static int dsa_slave_port_vlan_del(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400281 const struct switchdev_obj_vlan *vlan)
Vivien Didelot11149532015-08-13 12:52:17 -0400282{
Vivien Didelot11149532015-08-13 12:52:17 -0400283 struct dsa_slave_priv *p = netdev_priv(dev);
284 struct dsa_switch *ds = p->parent;
285 u16 vid;
286 int err;
287
288 if (!ds->drv->port_vlan_del)
289 return -EOPNOTSUPP;
290
291 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
292 err = ds->drv->port_vlan_del(ds, p->port, vid);
293 if (err)
294 return err;
295 }
296
297 return 0;
298}
299
300static int dsa_slave_port_vlan_dump(struct net_device *dev,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400301 struct switchdev_obj_vlan *vlan,
302 int (*cb)(void *obj))
Vivien Didelot11149532015-08-13 12:52:17 -0400303{
Vivien Didelot11149532015-08-13 12:52:17 -0400304 struct dsa_slave_priv *p = netdev_priv(dev);
305 struct dsa_switch *ds = p->parent;
306 DECLARE_BITMAP(members, DSA_MAX_PORTS);
307 DECLARE_BITMAP(untagged, DSA_MAX_PORTS);
308 u16 pvid, vid = 0;
309 int err;
310
311 if (!ds->drv->vlan_getnext || !ds->drv->port_pvid_get)
312 return -EOPNOTSUPP;
313
314 err = ds->drv->port_pvid_get(ds, p->port, &pvid);
315 if (err)
316 return err;
317
318 for (;;) {
319 err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
320 if (err)
321 break;
322
323 if (!test_bit(p->port, members))
324 continue;
325
326 memset(vlan, 0, sizeof(*vlan));
327 vlan->vid_begin = vlan->vid_end = vid;
328
329 if (vid == pvid)
330 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
331
332 if (test_bit(p->port, untagged))
333 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
334
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400335 err = cb(vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400336 if (err)
337 break;
338 }
339
340 return err == -ENOENT ? 0 : err;
341}
342
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400343static int dsa_slave_port_fdb_add(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400344 const struct switchdev_obj_fdb *fdb,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200345 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700346{
347 struct dsa_slave_priv *p = netdev_priv(dev);
348 struct dsa_switch *ds = p->parent;
349 int ret = -EOPNOTSUPP;
350
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200351 if (switchdev_trans_ph_prepare(trans))
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400352 ret = ds->drv->port_fdb_add ? 0 : -EOPNOTSUPP;
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200353 else
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400354 ret = ds->drv->port_fdb_add(ds, p->port, fdb->addr, fdb->vid);
David S. Millercdf09692015-08-11 12:00:37 -0700355
356 return ret;
357}
358
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400359static int dsa_slave_port_fdb_del(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400360 const struct switchdev_obj_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700361{
362 struct dsa_slave_priv *p = netdev_priv(dev);
363 struct dsa_switch *ds = p->parent;
364 int ret = -EOPNOTSUPP;
365
Vivien Didelot2a778e12015-08-10 09:09:49 -0400366 if (ds->drv->port_fdb_del)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400367 ret = ds->drv->port_fdb_del(ds, p->port, fdb->addr, fdb->vid);
David S. Millercdf09692015-08-11 12:00:37 -0700368
369 return ret;
370}
371
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400372static int dsa_slave_port_fdb_dump(struct net_device *dev,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400373 struct switchdev_obj_fdb *fdb,
374 int (*cb)(void *obj))
David S. Millercdf09692015-08-11 12:00:37 -0700375{
376 struct dsa_slave_priv *p = netdev_priv(dev);
377 struct dsa_switch *ds = p->parent;
378 unsigned char addr[ETH_ALEN] = { 0 };
Vivien Didelot2a778e12015-08-10 09:09:49 -0400379 u16 vid = 0;
David S. Millercdf09692015-08-11 12:00:37 -0700380 int ret;
381
Vivien Didelot2a778e12015-08-10 09:09:49 -0400382 if (!ds->drv->port_fdb_getnext)
David S. Millercdf09692015-08-11 12:00:37 -0700383 return -EOPNOTSUPP;
384
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400385 for (;;) {
David S. Millercdf09692015-08-11 12:00:37 -0700386 bool is_static;
387
Vivien Didelot2a778e12015-08-10 09:09:49 -0400388 ret = ds->drv->port_fdb_getnext(ds, p->port, addr, &vid,
389 &is_static);
David S. Millercdf09692015-08-11 12:00:37 -0700390 if (ret < 0)
391 break;
392
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400393 fdb->addr = addr;
394 fdb->vid = vid;
395 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
David S. Millercdf09692015-08-11 12:00:37 -0700396
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400397 ret = cb(fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700398 if (ret < 0)
399 break;
400 }
401
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400402 return ret == -ENOENT ? 0 : ret;
David S. Millercdf09692015-08-11 12:00:37 -0700403}
404
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000405static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
406{
407 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000408
409 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000410 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000411
412 return -EOPNOTSUPP;
413}
414
Florian Fainellib73adef2015-02-24 13:15:33 -0800415/* Return a bitmask of all ports being currently bridged within a given bridge
416 * device. Note that on leave, the mask will still return the bitmask of ports
417 * currently bridged, prior to port removal, and this is exactly what we want.
418 */
419static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
420 struct net_device *bridge)
421{
422 struct dsa_slave_priv *p;
423 unsigned int port;
424 u32 mask = 0;
425
426 for (port = 0; port < DSA_MAX_PORTS; port++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800427 if (!dsa_is_port_initialized(ds, port))
Florian Fainellib73adef2015-02-24 13:15:33 -0800428 continue;
429
430 p = netdev_priv(ds->ports[port]);
431
432 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
433 p->bridge_dev == bridge)
434 mask |= 1 << port;
435 }
436
437 return mask;
438}
439
440static int dsa_slave_stp_update(struct net_device *dev, u8 state)
441{
442 struct dsa_slave_priv *p = netdev_priv(dev);
443 struct dsa_switch *ds = p->parent;
444 int ret = -EOPNOTSUPP;
445
446 if (ds->drv->port_stp_update)
447 ret = ds->drv->port_stp_update(ds, p->port, state);
448
449 return ret;
450}
451
Scott Feldman35636062015-05-10 09:47:51 -0700452static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200453 struct switchdev_attr *attr,
454 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700455{
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400456 struct dsa_slave_priv *p = netdev_priv(dev);
457 struct dsa_switch *ds = p->parent;
458 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700459
460 switch (attr->id) {
461 case SWITCHDEV_ATTR_PORT_STP_STATE:
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400462 if (switchdev_trans_ph_prepare(trans))
463 ret = ds->drv->port_stp_update ? 0 : -EOPNOTSUPP;
464 else
465 ret = ds->drv->port_stp_update(ds, p->port,
466 attr->u.stp_state);
Scott Feldman35636062015-05-10 09:47:51 -0700467 break;
468 default:
469 ret = -EOPNOTSUPP;
470 break;
471 }
472
473 return ret;
474}
475
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400476static int dsa_slave_port_obj_add(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400477 enum switchdev_obj_id id, const void *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200478 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400479{
480 int err;
481
482 /* For the prepare phase, ensure the full set of changes is feasable in
483 * one go in order to signal a failure properly. If an operation is not
484 * supported, return -EOPNOTSUPP.
485 */
486
Vivien Didelotab069002015-09-29 12:07:17 -0400487 switch (id) {
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400488 case SWITCHDEV_OBJ_PORT_FDB:
Jiri Pirkof8db8342015-09-24 10:02:42 +0200489 err = dsa_slave_port_fdb_add(dev, obj, trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400490 break;
Vivien Didelot11149532015-08-13 12:52:17 -0400491 case SWITCHDEV_OBJ_PORT_VLAN:
Jiri Pirkof8db8342015-09-24 10:02:42 +0200492 err = dsa_slave_port_vlan_add(dev, obj, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400493 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400494 default:
495 err = -EOPNOTSUPP;
496 break;
497 }
498
499 return err;
500}
501
502static int dsa_slave_port_obj_del(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400503 enum switchdev_obj_id id, const void *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400504{
505 int err;
506
Vivien Didelotab069002015-09-29 12:07:17 -0400507 switch (id) {
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400508 case SWITCHDEV_OBJ_PORT_FDB:
509 err = dsa_slave_port_fdb_del(dev, obj);
510 break;
Vivien Didelot11149532015-08-13 12:52:17 -0400511 case SWITCHDEV_OBJ_PORT_VLAN:
512 err = dsa_slave_port_vlan_del(dev, obj);
513 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400514 default:
515 err = -EOPNOTSUPP;
516 break;
517 }
518
519 return err;
520}
521
522static int dsa_slave_port_obj_dump(struct net_device *dev,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400523 enum switchdev_obj_id id, void *obj,
524 int (*cb)(void *obj))
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400525{
526 int err;
527
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400528 switch (id) {
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400529 case SWITCHDEV_OBJ_PORT_FDB:
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400530 err = dsa_slave_port_fdb_dump(dev, obj, cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400531 break;
Vivien Didelot11149532015-08-13 12:52:17 -0400532 case SWITCHDEV_OBJ_PORT_VLAN:
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400533 err = dsa_slave_port_vlan_dump(dev, obj, cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400534 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400535 default:
536 err = -EOPNOTSUPP;
537 break;
538 }
539
540 return err;
541}
542
Florian Fainellib73adef2015-02-24 13:15:33 -0800543static int dsa_slave_bridge_port_join(struct net_device *dev,
544 struct net_device *br)
545{
546 struct dsa_slave_priv *p = netdev_priv(dev);
547 struct dsa_switch *ds = p->parent;
548 int ret = -EOPNOTSUPP;
549
550 p->bridge_dev = br;
551
552 if (ds->drv->port_join_bridge)
553 ret = ds->drv->port_join_bridge(ds, p->port,
554 dsa_slave_br_port_mask(ds, br));
555
556 return ret;
557}
558
559static int dsa_slave_bridge_port_leave(struct net_device *dev)
560{
561 struct dsa_slave_priv *p = netdev_priv(dev);
562 struct dsa_switch *ds = p->parent;
563 int ret = -EOPNOTSUPP;
564
565
566 if (ds->drv->port_leave_bridge)
567 ret = ds->drv->port_leave_bridge(ds, p->port,
568 dsa_slave_br_port_mask(ds, p->bridge_dev));
569
570 p->bridge_dev = NULL;
571
572 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
573 * so allow it to be in BR_STATE_FORWARDING to be kept functional
574 */
575 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
576
577 return ret;
578}
579
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700580static int dsa_slave_port_attr_get(struct net_device *dev,
581 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800582{
583 struct dsa_slave_priv *p = netdev_priv(dev);
584 struct dsa_switch *ds = p->parent;
585
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700586 switch (attr->id) {
587 case SWITCHDEV_ATTR_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700588 attr->u.ppid.id_len = sizeof(ds->index);
589 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700590 break;
591 default:
592 return -EOPNOTSUPP;
593 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800594
595 return 0;
596}
597
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700598static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
599 struct sk_buff *skb)
600{
601#ifdef CONFIG_NET_POLL_CONTROLLER
602 if (p->netpoll)
603 netpoll_send_skb(p->netpoll, skb);
604#else
605 BUG();
606#endif
607 return NETDEV_TX_OK;
608}
609
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700610static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
611{
612 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700613 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700614
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700615 dev->stats.tx_packets++;
616 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700617
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700618 /* Transmit function may have to reallocate the original SKB */
619 nskb = p->xmit(skb, dev);
620 if (!nskb)
621 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700622
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700623 /* SKB for netpoll still need to be mangled with the protocol-specific
624 * tag to be successfully transmitted
625 */
626 if (unlikely(netpoll_tx_running(dev)))
627 return dsa_netpoll_send_skb(p, nskb);
628
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700629 /* Queue the SKB for transmission on the parent interface, but
630 * do not modify its EtherType
631 */
632 nskb->dev = p->parent->dst->master_netdev;
633 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700634
635 return NETDEV_TX_OK;
636}
637
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700638static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
639 struct net_device *dev)
640{
641 /* Just return the original SKB */
642 return skb;
643}
644
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000645
646/* ethtool operations *******************************************************/
647static int
648dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
649{
650 struct dsa_slave_priv *p = netdev_priv(dev);
651 int err;
652
653 err = -EOPNOTSUPP;
654 if (p->phy != NULL) {
655 err = phy_read_status(p->phy);
656 if (err == 0)
657 err = phy_ethtool_gset(p->phy, cmd);
658 }
659
660 return err;
661}
662
663static int
664dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
665{
666 struct dsa_slave_priv *p = netdev_priv(dev);
667
668 if (p->phy != NULL)
669 return phy_ethtool_sset(p->phy, cmd);
670
671 return -EOPNOTSUPP;
672}
673
674static void dsa_slave_get_drvinfo(struct net_device *dev,
675 struct ethtool_drvinfo *drvinfo)
676{
Jiri Pirko7826d432013-01-06 00:44:26 +0000677 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
678 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
679 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
680 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000681}
682
Guenter Roeck3d762a02014-10-29 10:45:04 -0700683static int dsa_slave_get_regs_len(struct net_device *dev)
684{
685 struct dsa_slave_priv *p = netdev_priv(dev);
686 struct dsa_switch *ds = p->parent;
687
688 if (ds->drv->get_regs_len)
689 return ds->drv->get_regs_len(ds, p->port);
690
691 return -EOPNOTSUPP;
692}
693
694static void
695dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
696{
697 struct dsa_slave_priv *p = netdev_priv(dev);
698 struct dsa_switch *ds = p->parent;
699
700 if (ds->drv->get_regs)
701 ds->drv->get_regs(ds, p->port, regs, _p);
702}
703
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000704static int dsa_slave_nway_reset(struct net_device *dev)
705{
706 struct dsa_slave_priv *p = netdev_priv(dev);
707
708 if (p->phy != NULL)
709 return genphy_restart_aneg(p->phy);
710
711 return -EOPNOTSUPP;
712}
713
714static u32 dsa_slave_get_link(struct net_device *dev)
715{
716 struct dsa_slave_priv *p = netdev_priv(dev);
717
718 if (p->phy != NULL) {
719 genphy_update_link(p->phy);
720 return p->phy->link;
721 }
722
723 return -EOPNOTSUPP;
724}
725
Guenter Roeck6793abb2014-10-29 10:45:01 -0700726static int dsa_slave_get_eeprom_len(struct net_device *dev)
727{
728 struct dsa_slave_priv *p = netdev_priv(dev);
729 struct dsa_switch *ds = p->parent;
730
731 if (ds->pd->eeprom_len)
732 return ds->pd->eeprom_len;
733
734 if (ds->drv->get_eeprom_len)
735 return ds->drv->get_eeprom_len(ds);
736
737 return 0;
738}
739
740static int dsa_slave_get_eeprom(struct net_device *dev,
741 struct ethtool_eeprom *eeprom, u8 *data)
742{
743 struct dsa_slave_priv *p = netdev_priv(dev);
744 struct dsa_switch *ds = p->parent;
745
746 if (ds->drv->get_eeprom)
747 return ds->drv->get_eeprom(ds, eeprom, data);
748
749 return -EOPNOTSUPP;
750}
751
752static int dsa_slave_set_eeprom(struct net_device *dev,
753 struct ethtool_eeprom *eeprom, u8 *data)
754{
755 struct dsa_slave_priv *p = netdev_priv(dev);
756 struct dsa_switch *ds = p->parent;
757
758 if (ds->drv->set_eeprom)
759 return ds->drv->set_eeprom(ds, eeprom, data);
760
761 return -EOPNOTSUPP;
762}
763
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000764static void dsa_slave_get_strings(struct net_device *dev,
765 uint32_t stringset, uint8_t *data)
766{
767 struct dsa_slave_priv *p = netdev_priv(dev);
768 struct dsa_switch *ds = p->parent;
769
770 if (stringset == ETH_SS_STATS) {
771 int len = ETH_GSTRING_LEN;
772
773 strncpy(data, "tx_packets", len);
774 strncpy(data + len, "tx_bytes", len);
775 strncpy(data + 2 * len, "rx_packets", len);
776 strncpy(data + 3 * len, "rx_bytes", len);
777 if (ds->drv->get_strings != NULL)
778 ds->drv->get_strings(ds, p->port, data + 4 * len);
779 }
780}
781
782static void dsa_slave_get_ethtool_stats(struct net_device *dev,
783 struct ethtool_stats *stats,
784 uint64_t *data)
785{
786 struct dsa_slave_priv *p = netdev_priv(dev);
787 struct dsa_switch *ds = p->parent;
788
789 data[0] = p->dev->stats.tx_packets;
790 data[1] = p->dev->stats.tx_bytes;
791 data[2] = p->dev->stats.rx_packets;
792 data[3] = p->dev->stats.rx_bytes;
793 if (ds->drv->get_ethtool_stats != NULL)
794 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
795}
796
797static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
798{
799 struct dsa_slave_priv *p = netdev_priv(dev);
800 struct dsa_switch *ds = p->parent;
801
802 if (sset == ETH_SS_STATS) {
803 int count;
804
805 count = 4;
806 if (ds->drv->get_sset_count != NULL)
807 count += ds->drv->get_sset_count(ds);
808
809 return count;
810 }
811
812 return -EOPNOTSUPP;
813}
814
Florian Fainelli19e57c42014-09-18 17:31:24 -0700815static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
816{
817 struct dsa_slave_priv *p = netdev_priv(dev);
818 struct dsa_switch *ds = p->parent;
819
820 if (ds->drv->get_wol)
821 ds->drv->get_wol(ds, p->port, w);
822}
823
824static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
825{
826 struct dsa_slave_priv *p = netdev_priv(dev);
827 struct dsa_switch *ds = p->parent;
828 int ret = -EOPNOTSUPP;
829
830 if (ds->drv->set_wol)
831 ret = ds->drv->set_wol(ds, p->port, w);
832
833 return ret;
834}
835
Florian Fainelli79052882014-09-24 17:05:21 -0700836static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
837{
838 struct dsa_slave_priv *p = netdev_priv(dev);
839 struct dsa_switch *ds = p->parent;
840 int ret;
841
842 if (!ds->drv->set_eee)
843 return -EOPNOTSUPP;
844
845 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
846 if (ret)
847 return ret;
848
849 if (p->phy)
850 ret = phy_ethtool_set_eee(p->phy, e);
851
852 return ret;
853}
854
855static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
856{
857 struct dsa_slave_priv *p = netdev_priv(dev);
858 struct dsa_switch *ds = p->parent;
859 int ret;
860
861 if (!ds->drv->get_eee)
862 return -EOPNOTSUPP;
863
864 ret = ds->drv->get_eee(ds, p->port, e);
865 if (ret)
866 return ret;
867
868 if (p->phy)
869 ret = phy_ethtool_get_eee(p->phy, e);
870
871 return ret;
872}
873
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700874#ifdef CONFIG_NET_POLL_CONTROLLER
875static int dsa_slave_netpoll_setup(struct net_device *dev,
876 struct netpoll_info *ni)
877{
878 struct dsa_slave_priv *p = netdev_priv(dev);
879 struct dsa_switch *ds = p->parent;
880 struct net_device *master = ds->dst->master_netdev;
881 struct netpoll *netpoll;
882 int err = 0;
883
884 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
885 if (!netpoll)
886 return -ENOMEM;
887
888 err = __netpoll_setup(netpoll, master);
889 if (err) {
890 kfree(netpoll);
891 goto out;
892 }
893
894 p->netpoll = netpoll;
895out:
896 return err;
897}
898
899static void dsa_slave_netpoll_cleanup(struct net_device *dev)
900{
901 struct dsa_slave_priv *p = netdev_priv(dev);
902 struct netpoll *netpoll = p->netpoll;
903
904 if (!netpoll)
905 return;
906
907 p->netpoll = NULL;
908
909 __netpoll_free_async(netpoll);
910}
911
912static void dsa_slave_poll_controller(struct net_device *dev)
913{
914}
915#endif
916
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000917static const struct ethtool_ops dsa_slave_ethtool_ops = {
918 .get_settings = dsa_slave_get_settings,
919 .set_settings = dsa_slave_set_settings,
920 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700921 .get_regs_len = dsa_slave_get_regs_len,
922 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000923 .nway_reset = dsa_slave_nway_reset,
924 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700925 .get_eeprom_len = dsa_slave_get_eeprom_len,
926 .get_eeprom = dsa_slave_get_eeprom,
927 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000928 .get_strings = dsa_slave_get_strings,
929 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
930 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700931 .set_wol = dsa_slave_set_wol,
932 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700933 .set_eee = dsa_slave_set_eee,
934 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000935};
936
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700937static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800938 .ndo_open = dsa_slave_open,
939 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700940 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800941 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
942 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800943 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400944 .ndo_fdb_add = switchdev_port_fdb_add,
945 .ndo_fdb_del = switchdev_port_fdb_del,
946 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800947 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +0200948 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700949#ifdef CONFIG_NET_POLL_CONTROLLER
950 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
951 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
952 .ndo_poll_controller = dsa_slave_poll_controller,
953#endif
Vivien Didelot11149532015-08-13 12:52:17 -0400954 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
955 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
956 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Scott Feldman98237d42015-03-15 21:07:15 -0700957};
958
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700959static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700960 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -0700961 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400962 .switchdev_port_obj_add = dsa_slave_port_obj_add,
963 .switchdev_port_obj_del = dsa_slave_port_obj_del,
964 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800965};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000966
Florian Fainellif37db852015-09-23 18:19:58 -0700967static struct device_type dsa_type = {
968 .name = "dsa",
969};
970
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700971static void dsa_slave_adjust_link(struct net_device *dev)
972{
973 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700974 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700975 unsigned int status_changed = 0;
976
977 if (p->old_link != p->phy->link) {
978 status_changed = 1;
979 p->old_link = p->phy->link;
980 }
981
982 if (p->old_duplex != p->phy->duplex) {
983 status_changed = 1;
984 p->old_duplex = p->phy->duplex;
985 }
986
987 if (p->old_pause != p->phy->pause) {
988 status_changed = 1;
989 p->old_pause = p->phy->pause;
990 }
991
Florian Fainelliec9436b2014-08-27 17:04:53 -0700992 if (ds->drv->adjust_link && status_changed)
993 ds->drv->adjust_link(ds, p->port, p->phy);
994
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700995 if (status_changed)
996 phy_print_status(p->phy);
997}
998
Florian Fainellice31b312014-08-27 17:04:54 -0700999static int dsa_slave_fixed_link_update(struct net_device *dev,
1000 struct fixed_phy_status *status)
1001{
1002 struct dsa_slave_priv *p = netdev_priv(dev);
1003 struct dsa_switch *ds = p->parent;
1004
1005 if (ds->drv->fixed_link_update)
1006 ds->drv->fixed_link_update(ds, p->port, status);
1007
1008 return 0;
1009}
1010
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001011/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001012static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001013 struct net_device *slave_dev,
1014 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001015{
1016 struct dsa_switch *ds = p->parent;
1017
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001018 p->phy = ds->slave_mii_bus->phy_map[addr];
Florian Fainellic305c162015-03-10 16:57:12 -07001019 if (!p->phy)
1020 return -ENODEV;
1021
1022 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001023 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1024 p->phy_interface = p->phy->interface;
Florian Fainellic305c162015-03-10 16:57:12 -07001025 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1026 p->phy_interface);
1027
1028 return 0;
1029}
1030
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001031static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001032 struct net_device *slave_dev)
1033{
1034 struct dsa_switch *ds = p->parent;
1035 struct dsa_chip_data *cd = ds->pd;
1036 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001037 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001038 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001039 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001040
1041 port_dn = cd->port_dn[p->port];
Guenter Roeck19334922015-02-16 21:23:51 -08001042 mode = of_get_phy_mode(port_dn);
1043 if (mode < 0)
1044 mode = PHY_INTERFACE_MODE_NA;
1045 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001046
1047 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1048 if (of_phy_is_fixed_link(port_dn)) {
1049 /* In the case of a fixed PHY, the DT node associated
1050 * to the fixed PHY is the Port DT node
1051 */
1052 ret = of_phy_register_fixed_link(port_dn);
1053 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001054 netdev_err(slave_dev, "failed to register fixed PHY\n");
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001055 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001056 }
Florian Fainellice31b312014-08-27 17:04:54 -07001057 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001058 phy_dn = port_dn;
1059 }
1060
Florian Fainelli68195632014-09-19 13:07:54 -07001061 if (ds->drv->get_phy_flags)
1062 phy_flags = ds->drv->get_phy_flags(ds, p->port);
1063
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001064 if (phy_dn) {
1065 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1066 /* If this PHY address is part of phys_mii_mask, which means
1067 * that we need to divert reads and writes to/from it, then we
1068 * want to bind this device using the slave MII bus created by
1069 * DSA to make that happen.
1070 */
Florian Fainelli96026d02015-03-14 13:21:59 -07001071 if (!phy_is_fixed && ret >= 0 &&
1072 (ds->phys_mii_mask & (1 << ret))) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001073 ret = dsa_slave_phy_connect(p, slave_dev, ret);
1074 if (ret)
1075 return ret;
1076 } else {
1077 p->phy = of_phy_connect(slave_dev, phy_dn,
1078 dsa_slave_adjust_link,
1079 phy_flags,
1080 p->phy_interface);
1081 }
1082 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001083
Florian Fainellice31b312014-08-27 17:04:54 -07001084 if (p->phy && phy_is_fixed)
1085 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1086
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001087 /* We could not connect to a designated PHY, so use the switch internal
1088 * MDIO bus instead
1089 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001090 if (!p->phy) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001091 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
Florian Fainellic305c162015-03-10 16:57:12 -07001092 if (ret)
1093 return ret;
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001094 } else {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001095 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
1096 p->phy->addr, p->phy->drv->name);
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001097 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001098
1099 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001100}
1101
Andrew Lunn448b4482015-05-06 01:09:56 +02001102static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1103static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1104 struct netdev_queue *txq,
1105 void *_unused)
1106{
1107 lockdep_set_class(&txq->_xmit_lock,
1108 &dsa_slave_netdev_xmit_lock_key);
1109}
1110
Florian Fainelli24462542014-09-18 17:31:22 -07001111int dsa_slave_suspend(struct net_device *slave_dev)
1112{
1113 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1114
Florian Fainelli24462542014-09-18 17:31:22 -07001115 if (p->phy) {
1116 phy_stop(p->phy);
1117 p->old_pause = -1;
1118 p->old_link = -1;
1119 p->old_duplex = -1;
1120 phy_suspend(p->phy);
1121 }
1122
1123 return 0;
1124}
1125
1126int dsa_slave_resume(struct net_device *slave_dev)
1127{
1128 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1129
1130 netif_device_attach(slave_dev);
1131
1132 if (p->phy) {
1133 phy_resume(p->phy);
1134 phy_start(p->phy);
1135 }
1136
1137 return 0;
1138}
1139
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001140int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1141 int port, char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001142{
Lennert Buytenheke84665c2009-03-20 09:52:09 +00001143 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001144 struct net_device *slave_dev;
1145 struct dsa_slave_priv *p;
1146 int ret;
1147
Tom Gundersenc835a672014-07-14 16:37:24 +02001148 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1149 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001150 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001151 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001152
1153 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001154 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001155 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001156 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001157 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001158 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Florian Fainellif37db852015-09-23 18:19:58 -07001159 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001160
Andrew Lunn448b4482015-05-06 01:09:56 +02001161 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1162 NULL);
1163
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001164 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -07001165 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001166 slave_dev->vlan_features = master->vlan_features;
1167
1168 p = netdev_priv(slave_dev);
1169 p->dev = slave_dev;
1170 p->parent = ds;
1171 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001172
Alexander Duyck50753142014-09-15 13:00:19 -04001173 switch (ds->dst->tag_protocol) {
1174#ifdef CONFIG_NET_DSA_TAG_DSA
1175 case DSA_TAG_PROTO_DSA:
1176 p->xmit = dsa_netdev_ops.xmit;
1177 break;
1178#endif
1179#ifdef CONFIG_NET_DSA_TAG_EDSA
1180 case DSA_TAG_PROTO_EDSA:
1181 p->xmit = edsa_netdev_ops.xmit;
1182 break;
1183#endif
1184#ifdef CONFIG_NET_DSA_TAG_TRAILER
1185 case DSA_TAG_PROTO_TRAILER:
1186 p->xmit = trailer_netdev_ops.xmit;
1187 break;
1188#endif
1189#ifdef CONFIG_NET_DSA_TAG_BRCM
1190 case DSA_TAG_PROTO_BRCM:
1191 p->xmit = brcm_netdev_ops.xmit;
1192 break;
1193#endif
1194 default:
1195 p->xmit = dsa_slave_notag_xmit;
1196 break;
1197 }
1198
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001199 p->old_pause = -1;
1200 p->old_link = -1;
1201 p->old_duplex = -1;
1202
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001203 ret = dsa_slave_phy_setup(p, slave_dev);
1204 if (ret) {
1205 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001206 return ret;
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001207 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001208
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001209 ds->ports[port] = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001210 ret = register_netdev(slave_dev);
1211 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001212 netdev_err(master, "error %d registering interface %s\n",
1213 ret, slave_dev->name);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001214 phy_disconnect(p->phy);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001215 ds->ports[port] = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001216 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001217 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001218 }
1219
1220 netif_carrier_off(slave_dev);
1221
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001222 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001223}
Florian Fainellib73adef2015-02-24 13:15:33 -08001224
1225static bool dsa_slave_dev_check(struct net_device *dev)
1226{
1227 return dev->netdev_ops == &dsa_slave_netdev_ops;
1228}
1229
1230static int dsa_slave_master_changed(struct net_device *dev)
1231{
1232 struct net_device *master = netdev_master_upper_dev_get(dev);
Guenter Roeckb06b1072015-03-25 08:08:37 -07001233 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellib73adef2015-02-24 13:15:33 -08001234 int err = 0;
1235
1236 if (master && master->rtnl_link_ops &&
1237 !strcmp(master->rtnl_link_ops->kind, "bridge"))
1238 err = dsa_slave_bridge_port_join(dev, master);
Guenter Roeckb06b1072015-03-25 08:08:37 -07001239 else if (dsa_port_is_bridged(p))
Florian Fainellib73adef2015-02-24 13:15:33 -08001240 err = dsa_slave_bridge_port_leave(dev);
1241
1242 return err;
1243}
1244
1245int dsa_slave_netdevice_event(struct notifier_block *unused,
1246 unsigned long event, void *ptr)
1247{
1248 struct net_device *dev;
1249 int err = 0;
1250
1251 switch (event) {
1252 case NETDEV_CHANGEUPPER:
1253 dev = netdev_notifier_info_to_dev(ptr);
1254 if (!dsa_slave_dev_check(dev))
1255 goto out;
1256
1257 err = dsa_slave_master_changed(dev);
1258 if (err)
1259 netdev_warn(dev, "failed to reflect master change\n");
1260
1261 break;
1262 }
1263
1264out:
1265 return NOTIFY_DONE;
1266}