blob: c3b868c3373bfb15cd83e5e5462a781d1ca8b11b [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,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200245 struct switchdev_obj *obj,
246 struct switchdev_trans *trans)
Vivien Didelot11149532015-08-13 12:52:17 -0400247{
248 struct switchdev_obj_vlan *vlan = &obj->u.vlan;
249 struct dsa_slave_priv *p = netdev_priv(dev);
250 struct dsa_switch *ds = p->parent;
251 u16 vid;
252 int err;
253
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200254 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot11149532015-08-13 12:52:17 -0400255 if (!ds->drv->port_vlan_add || !ds->drv->port_pvid_set)
256 return -EOPNOTSUPP;
257
258 /* If the requested port doesn't belong to the same bridge as
259 * the VLAN members, fallback to software VLAN (hopefully).
260 */
261 err = dsa_bridge_check_vlan_range(ds, p->bridge_dev,
262 vlan->vid_begin,
263 vlan->vid_end);
264 if (err)
265 return err;
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200266 } else {
Vivien Didelot11149532015-08-13 12:52:17 -0400267 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
268 err = ds->drv->port_vlan_add(ds, p->port, vid,
269 vlan->flags &
270 BRIDGE_VLAN_INFO_UNTAGGED);
271 if (!err && vlan->flags & BRIDGE_VLAN_INFO_PVID)
272 err = ds->drv->port_pvid_set(ds, p->port, vid);
273 if (err)
274 return err;
275 }
Vivien Didelot11149532015-08-13 12:52:17 -0400276 }
277
278 return 0;
279}
280
281static int dsa_slave_port_vlan_del(struct net_device *dev,
282 struct switchdev_obj *obj)
283{
284 struct switchdev_obj_vlan *vlan = &obj->u.vlan;
285 struct dsa_slave_priv *p = netdev_priv(dev);
286 struct dsa_switch *ds = p->parent;
287 u16 vid;
288 int err;
289
290 if (!ds->drv->port_vlan_del)
291 return -EOPNOTSUPP;
292
293 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
294 err = ds->drv->port_vlan_del(ds, p->port, vid);
295 if (err)
296 return err;
297 }
298
299 return 0;
300}
301
302static int dsa_slave_port_vlan_dump(struct net_device *dev,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400303 struct switchdev_obj_vlan *vlan,
304 int (*cb)(void *obj))
Vivien Didelot11149532015-08-13 12:52:17 -0400305{
Vivien Didelot11149532015-08-13 12:52:17 -0400306 struct dsa_slave_priv *p = netdev_priv(dev);
307 struct dsa_switch *ds = p->parent;
308 DECLARE_BITMAP(members, DSA_MAX_PORTS);
309 DECLARE_BITMAP(untagged, DSA_MAX_PORTS);
310 u16 pvid, vid = 0;
311 int err;
312
313 if (!ds->drv->vlan_getnext || !ds->drv->port_pvid_get)
314 return -EOPNOTSUPP;
315
316 err = ds->drv->port_pvid_get(ds, p->port, &pvid);
317 if (err)
318 return err;
319
320 for (;;) {
321 err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
322 if (err)
323 break;
324
325 if (!test_bit(p->port, members))
326 continue;
327
328 memset(vlan, 0, sizeof(*vlan));
329 vlan->vid_begin = vlan->vid_end = vid;
330
331 if (vid == pvid)
332 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
333
334 if (test_bit(p->port, untagged))
335 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
336
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400337 err = cb(vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400338 if (err)
339 break;
340 }
341
342 return err == -ENOENT ? 0 : err;
343}
344
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400345static int dsa_slave_port_fdb_add(struct net_device *dev,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200346 struct switchdev_obj *obj,
347 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700348{
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400349 struct switchdev_obj_fdb *fdb = &obj->u.fdb;
David S. Millercdf09692015-08-11 12:00:37 -0700350 struct dsa_slave_priv *p = netdev_priv(dev);
351 struct dsa_switch *ds = p->parent;
352 int ret = -EOPNOTSUPP;
353
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200354 if (switchdev_trans_ph_prepare(trans))
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400355 ret = ds->drv->port_fdb_add ? 0 : -EOPNOTSUPP;
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200356 else
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400357 ret = ds->drv->port_fdb_add(ds, p->port, fdb->addr, fdb->vid);
David S. Millercdf09692015-08-11 12:00:37 -0700358
359 return ret;
360}
361
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400362static int dsa_slave_port_fdb_del(struct net_device *dev,
363 struct switchdev_obj *obj)
David S. Millercdf09692015-08-11 12:00:37 -0700364{
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400365 struct switchdev_obj_fdb *fdb = &obj->u.fdb;
David S. Millercdf09692015-08-11 12:00:37 -0700366 struct dsa_slave_priv *p = netdev_priv(dev);
367 struct dsa_switch *ds = p->parent;
368 int ret = -EOPNOTSUPP;
369
Vivien Didelot2a778e12015-08-10 09:09:49 -0400370 if (ds->drv->port_fdb_del)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400371 ret = ds->drv->port_fdb_del(ds, p->port, fdb->addr, fdb->vid);
David S. Millercdf09692015-08-11 12:00:37 -0700372
373 return ret;
374}
375
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400376static int dsa_slave_port_fdb_dump(struct net_device *dev,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400377 struct switchdev_obj_fdb *fdb,
378 int (*cb)(void *obj))
David S. Millercdf09692015-08-11 12:00:37 -0700379{
380 struct dsa_slave_priv *p = netdev_priv(dev);
381 struct dsa_switch *ds = p->parent;
382 unsigned char addr[ETH_ALEN] = { 0 };
Vivien Didelot2a778e12015-08-10 09:09:49 -0400383 u16 vid = 0;
David S. Millercdf09692015-08-11 12:00:37 -0700384 int ret;
385
Vivien Didelot2a778e12015-08-10 09:09:49 -0400386 if (!ds->drv->port_fdb_getnext)
David S. Millercdf09692015-08-11 12:00:37 -0700387 return -EOPNOTSUPP;
388
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400389 for (;;) {
David S. Millercdf09692015-08-11 12:00:37 -0700390 bool is_static;
391
Vivien Didelot2a778e12015-08-10 09:09:49 -0400392 ret = ds->drv->port_fdb_getnext(ds, p->port, addr, &vid,
393 &is_static);
David S. Millercdf09692015-08-11 12:00:37 -0700394 if (ret < 0)
395 break;
396
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400397 fdb->addr = addr;
398 fdb->vid = vid;
399 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
David S. Millercdf09692015-08-11 12:00:37 -0700400
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400401 ret = cb(fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700402 if (ret < 0)
403 break;
404 }
405
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400406 return ret == -ENOENT ? 0 : ret;
David S. Millercdf09692015-08-11 12:00:37 -0700407}
408
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000409static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
410{
411 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000412
413 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000414 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000415
416 return -EOPNOTSUPP;
417}
418
Florian Fainellib73adef2015-02-24 13:15:33 -0800419/* Return a bitmask of all ports being currently bridged within a given bridge
420 * device. Note that on leave, the mask will still return the bitmask of ports
421 * currently bridged, prior to port removal, and this is exactly what we want.
422 */
423static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
424 struct net_device *bridge)
425{
426 struct dsa_slave_priv *p;
427 unsigned int port;
428 u32 mask = 0;
429
430 for (port = 0; port < DSA_MAX_PORTS; port++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800431 if (!dsa_is_port_initialized(ds, port))
Florian Fainellib73adef2015-02-24 13:15:33 -0800432 continue;
433
434 p = netdev_priv(ds->ports[port]);
435
436 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
437 p->bridge_dev == bridge)
438 mask |= 1 << port;
439 }
440
441 return mask;
442}
443
444static int dsa_slave_stp_update(struct net_device *dev, u8 state)
445{
446 struct dsa_slave_priv *p = netdev_priv(dev);
447 struct dsa_switch *ds = p->parent;
448 int ret = -EOPNOTSUPP;
449
450 if (ds->drv->port_stp_update)
451 ret = ds->drv->port_stp_update(ds, p->port, state);
452
453 return ret;
454}
455
Scott Feldman35636062015-05-10 09:47:51 -0700456static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200457 struct switchdev_attr *attr,
458 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700459{
460 int ret = 0;
461
462 switch (attr->id) {
463 case SWITCHDEV_ATTR_PORT_STP_STATE:
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200464 if (switchdev_trans_ph_commit(trans))
Scott Feldman42275bd2015-05-13 11:16:50 -0700465 ret = dsa_slave_stp_update(dev, attr->u.stp_state);
Scott Feldman35636062015-05-10 09:47:51 -0700466 break;
467 default:
468 ret = -EOPNOTSUPP;
469 break;
470 }
471
472 return ret;
473}
474
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400475static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200476 struct switchdev_obj *obj,
477 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400478{
479 int err;
480
481 /* For the prepare phase, ensure the full set of changes is feasable in
482 * one go in order to signal a failure properly. If an operation is not
483 * supported, return -EOPNOTSUPP.
484 */
485
486 switch (obj->id) {
487 case SWITCHDEV_OBJ_PORT_FDB:
Jiri Pirkof8db8342015-09-24 10:02:42 +0200488 err = dsa_slave_port_fdb_add(dev, obj, trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400489 break;
Vivien Didelot11149532015-08-13 12:52:17 -0400490 case SWITCHDEV_OBJ_PORT_VLAN:
Jiri Pirkof8db8342015-09-24 10:02:42 +0200491 err = dsa_slave_port_vlan_add(dev, obj, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400492 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400493 default:
494 err = -EOPNOTSUPP;
495 break;
496 }
497
498 return err;
499}
500
501static int dsa_slave_port_obj_del(struct net_device *dev,
502 struct switchdev_obj *obj)
503{
504 int err;
505
506 switch (obj->id) {
507 case SWITCHDEV_OBJ_PORT_FDB:
508 err = dsa_slave_port_fdb_del(dev, obj);
509 break;
Vivien Didelot11149532015-08-13 12:52:17 -0400510 case SWITCHDEV_OBJ_PORT_VLAN:
511 err = dsa_slave_port_vlan_del(dev, obj);
512 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400513 default:
514 err = -EOPNOTSUPP;
515 break;
516 }
517
518 return err;
519}
520
521static int dsa_slave_port_obj_dump(struct net_device *dev,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400522 enum switchdev_obj_id id, void *obj,
523 int (*cb)(void *obj))
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400524{
525 int err;
526
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400527 switch (id) {
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400528 case SWITCHDEV_OBJ_PORT_FDB:
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400529 err = dsa_slave_port_fdb_dump(dev, obj, cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400530 break;
Vivien Didelot11149532015-08-13 12:52:17 -0400531 case SWITCHDEV_OBJ_PORT_VLAN:
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400532 err = dsa_slave_port_vlan_dump(dev, obj, cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400533 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400534 default:
535 err = -EOPNOTSUPP;
536 break;
537 }
538
539 return err;
540}
541
Florian Fainellib73adef2015-02-24 13:15:33 -0800542static int dsa_slave_bridge_port_join(struct net_device *dev,
543 struct net_device *br)
544{
545 struct dsa_slave_priv *p = netdev_priv(dev);
546 struct dsa_switch *ds = p->parent;
547 int ret = -EOPNOTSUPP;
548
549 p->bridge_dev = br;
550
551 if (ds->drv->port_join_bridge)
552 ret = ds->drv->port_join_bridge(ds, p->port,
553 dsa_slave_br_port_mask(ds, br));
554
555 return ret;
556}
557
558static int dsa_slave_bridge_port_leave(struct net_device *dev)
559{
560 struct dsa_slave_priv *p = netdev_priv(dev);
561 struct dsa_switch *ds = p->parent;
562 int ret = -EOPNOTSUPP;
563
564
565 if (ds->drv->port_leave_bridge)
566 ret = ds->drv->port_leave_bridge(ds, p->port,
567 dsa_slave_br_port_mask(ds, p->bridge_dev));
568
569 p->bridge_dev = NULL;
570
571 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
572 * so allow it to be in BR_STATE_FORWARDING to be kept functional
573 */
574 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
575
576 return ret;
577}
578
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700579static int dsa_slave_port_attr_get(struct net_device *dev,
580 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800581{
582 struct dsa_slave_priv *p = netdev_priv(dev);
583 struct dsa_switch *ds = p->parent;
584
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700585 switch (attr->id) {
586 case SWITCHDEV_ATTR_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700587 attr->u.ppid.id_len = sizeof(ds->index);
588 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700589 break;
590 default:
591 return -EOPNOTSUPP;
592 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800593
594 return 0;
595}
596
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700597static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
598 struct sk_buff *skb)
599{
600#ifdef CONFIG_NET_POLL_CONTROLLER
601 if (p->netpoll)
602 netpoll_send_skb(p->netpoll, skb);
603#else
604 BUG();
605#endif
606 return NETDEV_TX_OK;
607}
608
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700609static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
610{
611 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700612 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700613
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700614 dev->stats.tx_packets++;
615 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700616
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700617 /* Transmit function may have to reallocate the original SKB */
618 nskb = p->xmit(skb, dev);
619 if (!nskb)
620 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700621
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700622 /* SKB for netpoll still need to be mangled with the protocol-specific
623 * tag to be successfully transmitted
624 */
625 if (unlikely(netpoll_tx_running(dev)))
626 return dsa_netpoll_send_skb(p, nskb);
627
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700628 /* Queue the SKB for transmission on the parent interface, but
629 * do not modify its EtherType
630 */
631 nskb->dev = p->parent->dst->master_netdev;
632 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700633
634 return NETDEV_TX_OK;
635}
636
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700637static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
638 struct net_device *dev)
639{
640 /* Just return the original SKB */
641 return skb;
642}
643
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000644
645/* ethtool operations *******************************************************/
646static int
647dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
648{
649 struct dsa_slave_priv *p = netdev_priv(dev);
650 int err;
651
652 err = -EOPNOTSUPP;
653 if (p->phy != NULL) {
654 err = phy_read_status(p->phy);
655 if (err == 0)
656 err = phy_ethtool_gset(p->phy, cmd);
657 }
658
659 return err;
660}
661
662static int
663dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
664{
665 struct dsa_slave_priv *p = netdev_priv(dev);
666
667 if (p->phy != NULL)
668 return phy_ethtool_sset(p->phy, cmd);
669
670 return -EOPNOTSUPP;
671}
672
673static void dsa_slave_get_drvinfo(struct net_device *dev,
674 struct ethtool_drvinfo *drvinfo)
675{
Jiri Pirko7826d432013-01-06 00:44:26 +0000676 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
677 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
678 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
679 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000680}
681
Guenter Roeck3d762a02014-10-29 10:45:04 -0700682static int dsa_slave_get_regs_len(struct net_device *dev)
683{
684 struct dsa_slave_priv *p = netdev_priv(dev);
685 struct dsa_switch *ds = p->parent;
686
687 if (ds->drv->get_regs_len)
688 return ds->drv->get_regs_len(ds, p->port);
689
690 return -EOPNOTSUPP;
691}
692
693static void
694dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
695{
696 struct dsa_slave_priv *p = netdev_priv(dev);
697 struct dsa_switch *ds = p->parent;
698
699 if (ds->drv->get_regs)
700 ds->drv->get_regs(ds, p->port, regs, _p);
701}
702
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000703static int dsa_slave_nway_reset(struct net_device *dev)
704{
705 struct dsa_slave_priv *p = netdev_priv(dev);
706
707 if (p->phy != NULL)
708 return genphy_restart_aneg(p->phy);
709
710 return -EOPNOTSUPP;
711}
712
713static u32 dsa_slave_get_link(struct net_device *dev)
714{
715 struct dsa_slave_priv *p = netdev_priv(dev);
716
717 if (p->phy != NULL) {
718 genphy_update_link(p->phy);
719 return p->phy->link;
720 }
721
722 return -EOPNOTSUPP;
723}
724
Guenter Roeck6793abb2014-10-29 10:45:01 -0700725static int dsa_slave_get_eeprom_len(struct net_device *dev)
726{
727 struct dsa_slave_priv *p = netdev_priv(dev);
728 struct dsa_switch *ds = p->parent;
729
730 if (ds->pd->eeprom_len)
731 return ds->pd->eeprom_len;
732
733 if (ds->drv->get_eeprom_len)
734 return ds->drv->get_eeprom_len(ds);
735
736 return 0;
737}
738
739static int dsa_slave_get_eeprom(struct net_device *dev,
740 struct ethtool_eeprom *eeprom, u8 *data)
741{
742 struct dsa_slave_priv *p = netdev_priv(dev);
743 struct dsa_switch *ds = p->parent;
744
745 if (ds->drv->get_eeprom)
746 return ds->drv->get_eeprom(ds, eeprom, data);
747
748 return -EOPNOTSUPP;
749}
750
751static int dsa_slave_set_eeprom(struct net_device *dev,
752 struct ethtool_eeprom *eeprom, u8 *data)
753{
754 struct dsa_slave_priv *p = netdev_priv(dev);
755 struct dsa_switch *ds = p->parent;
756
757 if (ds->drv->set_eeprom)
758 return ds->drv->set_eeprom(ds, eeprom, data);
759
760 return -EOPNOTSUPP;
761}
762
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000763static void dsa_slave_get_strings(struct net_device *dev,
764 uint32_t stringset, uint8_t *data)
765{
766 struct dsa_slave_priv *p = netdev_priv(dev);
767 struct dsa_switch *ds = p->parent;
768
769 if (stringset == ETH_SS_STATS) {
770 int len = ETH_GSTRING_LEN;
771
772 strncpy(data, "tx_packets", len);
773 strncpy(data + len, "tx_bytes", len);
774 strncpy(data + 2 * len, "rx_packets", len);
775 strncpy(data + 3 * len, "rx_bytes", len);
776 if (ds->drv->get_strings != NULL)
777 ds->drv->get_strings(ds, p->port, data + 4 * len);
778 }
779}
780
781static void dsa_slave_get_ethtool_stats(struct net_device *dev,
782 struct ethtool_stats *stats,
783 uint64_t *data)
784{
785 struct dsa_slave_priv *p = netdev_priv(dev);
786 struct dsa_switch *ds = p->parent;
787
788 data[0] = p->dev->stats.tx_packets;
789 data[1] = p->dev->stats.tx_bytes;
790 data[2] = p->dev->stats.rx_packets;
791 data[3] = p->dev->stats.rx_bytes;
792 if (ds->drv->get_ethtool_stats != NULL)
793 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
794}
795
796static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
797{
798 struct dsa_slave_priv *p = netdev_priv(dev);
799 struct dsa_switch *ds = p->parent;
800
801 if (sset == ETH_SS_STATS) {
802 int count;
803
804 count = 4;
805 if (ds->drv->get_sset_count != NULL)
806 count += ds->drv->get_sset_count(ds);
807
808 return count;
809 }
810
811 return -EOPNOTSUPP;
812}
813
Florian Fainelli19e57c42014-09-18 17:31:24 -0700814static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
815{
816 struct dsa_slave_priv *p = netdev_priv(dev);
817 struct dsa_switch *ds = p->parent;
818
819 if (ds->drv->get_wol)
820 ds->drv->get_wol(ds, p->port, w);
821}
822
823static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
824{
825 struct dsa_slave_priv *p = netdev_priv(dev);
826 struct dsa_switch *ds = p->parent;
827 int ret = -EOPNOTSUPP;
828
829 if (ds->drv->set_wol)
830 ret = ds->drv->set_wol(ds, p->port, w);
831
832 return ret;
833}
834
Florian Fainelli79052882014-09-24 17:05:21 -0700835static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
836{
837 struct dsa_slave_priv *p = netdev_priv(dev);
838 struct dsa_switch *ds = p->parent;
839 int ret;
840
841 if (!ds->drv->set_eee)
842 return -EOPNOTSUPP;
843
844 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
845 if (ret)
846 return ret;
847
848 if (p->phy)
849 ret = phy_ethtool_set_eee(p->phy, e);
850
851 return ret;
852}
853
854static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
855{
856 struct dsa_slave_priv *p = netdev_priv(dev);
857 struct dsa_switch *ds = p->parent;
858 int ret;
859
860 if (!ds->drv->get_eee)
861 return -EOPNOTSUPP;
862
863 ret = ds->drv->get_eee(ds, p->port, e);
864 if (ret)
865 return ret;
866
867 if (p->phy)
868 ret = phy_ethtool_get_eee(p->phy, e);
869
870 return ret;
871}
872
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700873#ifdef CONFIG_NET_POLL_CONTROLLER
874static int dsa_slave_netpoll_setup(struct net_device *dev,
875 struct netpoll_info *ni)
876{
877 struct dsa_slave_priv *p = netdev_priv(dev);
878 struct dsa_switch *ds = p->parent;
879 struct net_device *master = ds->dst->master_netdev;
880 struct netpoll *netpoll;
881 int err = 0;
882
883 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
884 if (!netpoll)
885 return -ENOMEM;
886
887 err = __netpoll_setup(netpoll, master);
888 if (err) {
889 kfree(netpoll);
890 goto out;
891 }
892
893 p->netpoll = netpoll;
894out:
895 return err;
896}
897
898static void dsa_slave_netpoll_cleanup(struct net_device *dev)
899{
900 struct dsa_slave_priv *p = netdev_priv(dev);
901 struct netpoll *netpoll = p->netpoll;
902
903 if (!netpoll)
904 return;
905
906 p->netpoll = NULL;
907
908 __netpoll_free_async(netpoll);
909}
910
911static void dsa_slave_poll_controller(struct net_device *dev)
912{
913}
914#endif
915
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000916static const struct ethtool_ops dsa_slave_ethtool_ops = {
917 .get_settings = dsa_slave_get_settings,
918 .set_settings = dsa_slave_set_settings,
919 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700920 .get_regs_len = dsa_slave_get_regs_len,
921 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000922 .nway_reset = dsa_slave_nway_reset,
923 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700924 .get_eeprom_len = dsa_slave_get_eeprom_len,
925 .get_eeprom = dsa_slave_get_eeprom,
926 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000927 .get_strings = dsa_slave_get_strings,
928 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
929 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700930 .set_wol = dsa_slave_set_wol,
931 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700932 .set_eee = dsa_slave_set_eee,
933 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000934};
935
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700936static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800937 .ndo_open = dsa_slave_open,
938 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700939 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800940 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
941 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800942 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400943 .ndo_fdb_add = switchdev_port_fdb_add,
944 .ndo_fdb_del = switchdev_port_fdb_del,
945 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800946 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +0200947 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700948#ifdef CONFIG_NET_POLL_CONTROLLER
949 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
950 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
951 .ndo_poll_controller = dsa_slave_poll_controller,
952#endif
Vivien Didelot11149532015-08-13 12:52:17 -0400953 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
954 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
955 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Scott Feldman98237d42015-03-15 21:07:15 -0700956};
957
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700958static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700959 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -0700960 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400961 .switchdev_port_obj_add = dsa_slave_port_obj_add,
962 .switchdev_port_obj_del = dsa_slave_port_obj_del,
963 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800964};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000965
Florian Fainellif37db852015-09-23 18:19:58 -0700966static struct device_type dsa_type = {
967 .name = "dsa",
968};
969
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700970static void dsa_slave_adjust_link(struct net_device *dev)
971{
972 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700973 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700974 unsigned int status_changed = 0;
975
976 if (p->old_link != p->phy->link) {
977 status_changed = 1;
978 p->old_link = p->phy->link;
979 }
980
981 if (p->old_duplex != p->phy->duplex) {
982 status_changed = 1;
983 p->old_duplex = p->phy->duplex;
984 }
985
986 if (p->old_pause != p->phy->pause) {
987 status_changed = 1;
988 p->old_pause = p->phy->pause;
989 }
990
Florian Fainelliec9436b2014-08-27 17:04:53 -0700991 if (ds->drv->adjust_link && status_changed)
992 ds->drv->adjust_link(ds, p->port, p->phy);
993
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700994 if (status_changed)
995 phy_print_status(p->phy);
996}
997
Florian Fainellice31b312014-08-27 17:04:54 -0700998static int dsa_slave_fixed_link_update(struct net_device *dev,
999 struct fixed_phy_status *status)
1000{
1001 struct dsa_slave_priv *p = netdev_priv(dev);
1002 struct dsa_switch *ds = p->parent;
1003
1004 if (ds->drv->fixed_link_update)
1005 ds->drv->fixed_link_update(ds, p->port, status);
1006
1007 return 0;
1008}
1009
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001010/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -07001011static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001012 struct net_device *slave_dev,
1013 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001014{
1015 struct dsa_switch *ds = p->parent;
1016
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001017 p->phy = ds->slave_mii_bus->phy_map[addr];
Florian Fainellic305c162015-03-10 16:57:12 -07001018 if (!p->phy)
1019 return -ENODEV;
1020
1021 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001022 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1023 p->phy_interface = p->phy->interface;
Florian Fainellic305c162015-03-10 16:57:12 -07001024 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1025 p->phy_interface);
1026
1027 return 0;
1028}
1029
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001030static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001031 struct net_device *slave_dev)
1032{
1033 struct dsa_switch *ds = p->parent;
1034 struct dsa_chip_data *cd = ds->pd;
1035 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001036 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001037 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001038 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001039
1040 port_dn = cd->port_dn[p->port];
Guenter Roeck19334922015-02-16 21:23:51 -08001041 mode = of_get_phy_mode(port_dn);
1042 if (mode < 0)
1043 mode = PHY_INTERFACE_MODE_NA;
1044 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001045
1046 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1047 if (of_phy_is_fixed_link(port_dn)) {
1048 /* In the case of a fixed PHY, the DT node associated
1049 * to the fixed PHY is the Port DT node
1050 */
1051 ret = of_phy_register_fixed_link(port_dn);
1052 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001053 netdev_err(slave_dev, "failed to register fixed PHY\n");
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001054 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001055 }
Florian Fainellice31b312014-08-27 17:04:54 -07001056 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001057 phy_dn = port_dn;
1058 }
1059
Florian Fainelli68195632014-09-19 13:07:54 -07001060 if (ds->drv->get_phy_flags)
1061 phy_flags = ds->drv->get_phy_flags(ds, p->port);
1062
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001063 if (phy_dn) {
1064 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1065 /* If this PHY address is part of phys_mii_mask, which means
1066 * that we need to divert reads and writes to/from it, then we
1067 * want to bind this device using the slave MII bus created by
1068 * DSA to make that happen.
1069 */
Florian Fainelli96026d02015-03-14 13:21:59 -07001070 if (!phy_is_fixed && ret >= 0 &&
1071 (ds->phys_mii_mask & (1 << ret))) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001072 ret = dsa_slave_phy_connect(p, slave_dev, ret);
1073 if (ret)
1074 return ret;
1075 } else {
1076 p->phy = of_phy_connect(slave_dev, phy_dn,
1077 dsa_slave_adjust_link,
1078 phy_flags,
1079 p->phy_interface);
1080 }
1081 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001082
Florian Fainellice31b312014-08-27 17:04:54 -07001083 if (p->phy && phy_is_fixed)
1084 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1085
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001086 /* We could not connect to a designated PHY, so use the switch internal
1087 * MDIO bus instead
1088 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001089 if (!p->phy) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001090 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
Florian Fainellic305c162015-03-10 16:57:12 -07001091 if (ret)
1092 return ret;
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001093 } else {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001094 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
1095 p->phy->addr, p->phy->drv->name);
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001096 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001097
1098 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001099}
1100
Andrew Lunn448b4482015-05-06 01:09:56 +02001101static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1102static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1103 struct netdev_queue *txq,
1104 void *_unused)
1105{
1106 lockdep_set_class(&txq->_xmit_lock,
1107 &dsa_slave_netdev_xmit_lock_key);
1108}
1109
Florian Fainelli24462542014-09-18 17:31:22 -07001110int dsa_slave_suspend(struct net_device *slave_dev)
1111{
1112 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1113
Florian Fainelli24462542014-09-18 17:31:22 -07001114 if (p->phy) {
1115 phy_stop(p->phy);
1116 p->old_pause = -1;
1117 p->old_link = -1;
1118 p->old_duplex = -1;
1119 phy_suspend(p->phy);
1120 }
1121
1122 return 0;
1123}
1124
1125int dsa_slave_resume(struct net_device *slave_dev)
1126{
1127 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1128
1129 netif_device_attach(slave_dev);
1130
1131 if (p->phy) {
1132 phy_resume(p->phy);
1133 phy_start(p->phy);
1134 }
1135
1136 return 0;
1137}
1138
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001139int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1140 int port, char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001141{
Lennert Buytenheke84665c2009-03-20 09:52:09 +00001142 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001143 struct net_device *slave_dev;
1144 struct dsa_slave_priv *p;
1145 int ret;
1146
Tom Gundersenc835a672014-07-14 16:37:24 +02001147 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1148 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001149 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001150 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001151
1152 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001153 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001154 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001155 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001156 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001157 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Florian Fainellif37db852015-09-23 18:19:58 -07001158 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001159
Andrew Lunn448b4482015-05-06 01:09:56 +02001160 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1161 NULL);
1162
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001163 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -07001164 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001165 slave_dev->vlan_features = master->vlan_features;
1166
1167 p = netdev_priv(slave_dev);
1168 p->dev = slave_dev;
1169 p->parent = ds;
1170 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001171
Alexander Duyck50753142014-09-15 13:00:19 -04001172 switch (ds->dst->tag_protocol) {
1173#ifdef CONFIG_NET_DSA_TAG_DSA
1174 case DSA_TAG_PROTO_DSA:
1175 p->xmit = dsa_netdev_ops.xmit;
1176 break;
1177#endif
1178#ifdef CONFIG_NET_DSA_TAG_EDSA
1179 case DSA_TAG_PROTO_EDSA:
1180 p->xmit = edsa_netdev_ops.xmit;
1181 break;
1182#endif
1183#ifdef CONFIG_NET_DSA_TAG_TRAILER
1184 case DSA_TAG_PROTO_TRAILER:
1185 p->xmit = trailer_netdev_ops.xmit;
1186 break;
1187#endif
1188#ifdef CONFIG_NET_DSA_TAG_BRCM
1189 case DSA_TAG_PROTO_BRCM:
1190 p->xmit = brcm_netdev_ops.xmit;
1191 break;
1192#endif
1193 default:
1194 p->xmit = dsa_slave_notag_xmit;
1195 break;
1196 }
1197
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001198 p->old_pause = -1;
1199 p->old_link = -1;
1200 p->old_duplex = -1;
1201
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001202 ret = dsa_slave_phy_setup(p, slave_dev);
1203 if (ret) {
1204 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001205 return ret;
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001206 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001207
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001208 ds->ports[port] = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001209 ret = register_netdev(slave_dev);
1210 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001211 netdev_err(master, "error %d registering interface %s\n",
1212 ret, slave_dev->name);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001213 phy_disconnect(p->phy);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001214 ds->ports[port] = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001215 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001216 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001217 }
1218
1219 netif_carrier_off(slave_dev);
1220
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001221 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001222}
Florian Fainellib73adef2015-02-24 13:15:33 -08001223
1224static bool dsa_slave_dev_check(struct net_device *dev)
1225{
1226 return dev->netdev_ops == &dsa_slave_netdev_ops;
1227}
1228
1229static int dsa_slave_master_changed(struct net_device *dev)
1230{
1231 struct net_device *master = netdev_master_upper_dev_get(dev);
Guenter Roeckb06b1072015-03-25 08:08:37 -07001232 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellib73adef2015-02-24 13:15:33 -08001233 int err = 0;
1234
1235 if (master && master->rtnl_link_ops &&
1236 !strcmp(master->rtnl_link_ops->kind, "bridge"))
1237 err = dsa_slave_bridge_port_join(dev, master);
Guenter Roeckb06b1072015-03-25 08:08:37 -07001238 else if (dsa_port_is_bridged(p))
Florian Fainellib73adef2015-02-24 13:15:33 -08001239 err = dsa_slave_bridge_port_leave(dev);
1240
1241 return err;
1242}
1243
1244int dsa_slave_netdevice_event(struct notifier_block *unused,
1245 unsigned long event, void *ptr)
1246{
1247 struct net_device *dev;
1248 int err = 0;
1249
1250 switch (event) {
1251 case NETDEV_CHANGEUPPER:
1252 dev = netdev_notifier_info_to_dev(ptr);
1253 if (!dsa_slave_dev_check(dev))
1254 goto out;
1255
1256 err = dsa_slave_master_changed(dev);
1257 if (err)
1258 netdev_warn(dev, "failed to reflect master change\n");
1259
1260 break;
1261 }
1262
1263out:
1264 return NOTIFY_DONE;
1265}