blob: 7bc787b095c897922751c3eaa5c2b32d5bca95fa [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 Pirko8f24f302015-10-01 11:03:43 +0200245 const struct switchdev_obj_port_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;
Vivien Didelot11149532015-08-13 12:52:17 -0400250 int err;
251
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200252 if (switchdev_trans_ph_prepare(trans)) {
Vivien Didelot76e398a2015-11-01 12:33:55 -0500253 if (!ds->drv->port_vlan_prepare || !ds->drv->port_vlan_add)
Vivien Didelot11149532015-08-13 12:52:17 -0400254 return -EOPNOTSUPP;
255
256 /* If the requested port doesn't belong to the same bridge as
257 * the VLAN members, fallback to software VLAN (hopefully).
258 */
259 err = dsa_bridge_check_vlan_range(ds, p->bridge_dev,
260 vlan->vid_begin,
261 vlan->vid_end);
262 if (err)
263 return err;
Vivien Didelot76e398a2015-11-01 12:33:55 -0500264
265 err = ds->drv->port_vlan_prepare(ds, p->port, vlan, trans);
266 if (err)
267 return err;
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200268 } else {
Vivien Didelot76e398a2015-11-01 12:33:55 -0500269 err = ds->drv->port_vlan_add(ds, p->port, vlan, trans);
270 if (err)
271 return err;
Vivien Didelot11149532015-08-13 12:52:17 -0400272 }
273
274 return 0;
275}
276
277static int dsa_slave_port_vlan_del(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200278 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot11149532015-08-13 12:52:17 -0400279{
Vivien Didelot11149532015-08-13 12:52:17 -0400280 struct dsa_slave_priv *p = netdev_priv(dev);
281 struct dsa_switch *ds = p->parent;
Vivien Didelot11149532015-08-13 12:52:17 -0400282
283 if (!ds->drv->port_vlan_del)
284 return -EOPNOTSUPP;
285
Vivien Didelot76e398a2015-11-01 12:33:55 -0500286 return ds->drv->port_vlan_del(ds, p->port, vlan);
Vivien Didelot11149532015-08-13 12:52:17 -0400287}
288
289static int dsa_slave_port_vlan_dump(struct net_device *dev,
Jiri Pirko8f24f302015-10-01 11:03:43 +0200290 struct switchdev_obj_port_vlan *vlan,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200291 switchdev_obj_dump_cb_t *cb)
Vivien Didelot11149532015-08-13 12:52:17 -0400292{
Vivien Didelot11149532015-08-13 12:52:17 -0400293 struct dsa_slave_priv *p = netdev_priv(dev);
294 struct dsa_switch *ds = p->parent;
295 DECLARE_BITMAP(members, DSA_MAX_PORTS);
296 DECLARE_BITMAP(untagged, DSA_MAX_PORTS);
297 u16 pvid, vid = 0;
298 int err;
299
300 if (!ds->drv->vlan_getnext || !ds->drv->port_pvid_get)
301 return -EOPNOTSUPP;
302
303 err = ds->drv->port_pvid_get(ds, p->port, &pvid);
304 if (err)
305 return err;
306
307 for (;;) {
308 err = ds->drv->vlan_getnext(ds, &vid, members, untagged);
309 if (err)
310 break;
311
312 if (!test_bit(p->port, members))
313 continue;
314
315 memset(vlan, 0, sizeof(*vlan));
316 vlan->vid_begin = vlan->vid_end = vid;
317
318 if (vid == pvid)
319 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
320
321 if (test_bit(p->port, untagged))
322 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
323
Jiri Pirko648b4a92015-10-01 11:03:45 +0200324 err = cb(&vlan->obj);
Vivien Didelot11149532015-08-13 12:52:17 -0400325 if (err)
326 break;
327 }
328
329 return err == -ENOENT ? 0 : err;
330}
331
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400332static int dsa_slave_port_fdb_add(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200333 const struct switchdev_obj_port_fdb *fdb,
Jiri Pirkof8db8342015-09-24 10:02:42 +0200334 struct switchdev_trans *trans)
David S. Millercdf09692015-08-11 12:00:37 -0700335{
336 struct dsa_slave_priv *p = netdev_priv(dev);
337 struct dsa_switch *ds = p->parent;
Vivien Didelot146a3202015-10-08 11:35:12 -0400338 int ret;
339
340 if (!ds->drv->port_fdb_prepare || !ds->drv->port_fdb_add)
341 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700342
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200343 if (switchdev_trans_ph_prepare(trans))
Vivien Didelot146a3202015-10-08 11:35:12 -0400344 ret = ds->drv->port_fdb_prepare(ds, p->port, fdb, trans);
Jiri Pirko79a62eb2015-09-24 10:02:48 +0200345 else
Vivien Didelot1f36faf2015-10-08 11:35:13 -0400346 ret = ds->drv->port_fdb_add(ds, p->port, fdb, trans);
David S. Millercdf09692015-08-11 12:00:37 -0700347
348 return ret;
349}
350
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400351static int dsa_slave_port_fdb_del(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200352 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -0700353{
354 struct dsa_slave_priv *p = netdev_priv(dev);
355 struct dsa_switch *ds = p->parent;
356 int ret = -EOPNOTSUPP;
357
Vivien Didelot2a778e12015-08-10 09:09:49 -0400358 if (ds->drv->port_fdb_del)
Vivien Didelot8057b3e2015-10-08 11:35:14 -0400359 ret = ds->drv->port_fdb_del(ds, p->port, fdb);
David S. Millercdf09692015-08-11 12:00:37 -0700360
361 return ret;
362}
363
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400364static int dsa_slave_port_fdb_dump(struct net_device *dev,
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200365 struct switchdev_obj_port_fdb *fdb,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200366 switchdev_obj_dump_cb_t *cb)
David S. Millercdf09692015-08-11 12:00:37 -0700367{
368 struct dsa_slave_priv *p = netdev_priv(dev);
369 struct dsa_switch *ds = p->parent;
David S. Millercdf09692015-08-11 12:00:37 -0700370
Vivien Didelotea70ba92015-10-22 09:34:38 -0400371 if (ds->drv->port_fdb_dump)
372 return ds->drv->port_fdb_dump(ds, p->port, fdb, cb);
373
Vivien Didelot1a49a2f2015-10-22 09:34:43 -0400374 return -EOPNOTSUPP;
David S. Millercdf09692015-08-11 12:00:37 -0700375}
376
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000377static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
378{
379 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000380
381 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000382 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000383
384 return -EOPNOTSUPP;
385}
386
Florian Fainellib73adef2015-02-24 13:15:33 -0800387/* Return a bitmask of all ports being currently bridged within a given bridge
388 * device. Note that on leave, the mask will still return the bitmask of ports
389 * currently bridged, prior to port removal, and this is exactly what we want.
390 */
391static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
392 struct net_device *bridge)
393{
394 struct dsa_slave_priv *p;
395 unsigned int port;
396 u32 mask = 0;
397
398 for (port = 0; port < DSA_MAX_PORTS; port++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800399 if (!dsa_is_port_initialized(ds, port))
Florian Fainellib73adef2015-02-24 13:15:33 -0800400 continue;
401
402 p = netdev_priv(ds->ports[port]);
403
404 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
405 p->bridge_dev == bridge)
406 mask |= 1 << port;
407 }
408
409 return mask;
410}
411
412static int dsa_slave_stp_update(struct net_device *dev, u8 state)
413{
414 struct dsa_slave_priv *p = netdev_priv(dev);
415 struct dsa_switch *ds = p->parent;
416 int ret = -EOPNOTSUPP;
417
418 if (ds->drv->port_stp_update)
419 ret = ds->drv->port_stp_update(ds, p->port, state);
420
421 return ret;
422}
423
Scott Feldman35636062015-05-10 09:47:51 -0700424static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200425 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200426 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700427{
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400428 struct dsa_slave_priv *p = netdev_priv(dev);
429 struct dsa_switch *ds = p->parent;
430 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700431
432 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200433 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400434 if (switchdev_trans_ph_prepare(trans))
435 ret = ds->drv->port_stp_update ? 0 : -EOPNOTSUPP;
436 else
437 ret = ds->drv->port_stp_update(ds, p->port,
438 attr->u.stp_state);
Scott Feldman35636062015-05-10 09:47:51 -0700439 break;
440 default:
441 ret = -EOPNOTSUPP;
442 break;
443 }
444
445 return ret;
446}
447
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400448static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200449 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200450 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400451{
452 int err;
453
454 /* For the prepare phase, ensure the full set of changes is feasable in
455 * one go in order to signal a failure properly. If an operation is not
456 * supported, return -EOPNOTSUPP.
457 */
458
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200459 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200460 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200461 err = dsa_slave_port_fdb_add(dev,
462 SWITCHDEV_OBJ_PORT_FDB(obj),
463 trans);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400464 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200465 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200466 err = dsa_slave_port_vlan_add(dev,
467 SWITCHDEV_OBJ_PORT_VLAN(obj),
468 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400469 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400470 default:
471 err = -EOPNOTSUPP;
472 break;
473 }
474
475 return err;
476}
477
478static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200479 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400480{
481 int err;
482
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200483 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200484 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200485 err = dsa_slave_port_fdb_del(dev,
486 SWITCHDEV_OBJ_PORT_FDB(obj));
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400487 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200488 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200489 err = dsa_slave_port_vlan_del(dev,
490 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400491 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400492 default:
493 err = -EOPNOTSUPP;
494 break;
495 }
496
497 return err;
498}
499
500static int dsa_slave_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200501 struct switchdev_obj *obj,
502 switchdev_obj_dump_cb_t *cb)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400503{
504 int err;
505
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200506 switch (obj->id) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200507 case SWITCHDEV_OBJ_ID_PORT_FDB:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200508 err = dsa_slave_port_fdb_dump(dev,
509 SWITCHDEV_OBJ_PORT_FDB(obj),
510 cb);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400511 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200512 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Jiri Pirko648b4a92015-10-01 11:03:45 +0200513 err = dsa_slave_port_vlan_dump(dev,
514 SWITCHDEV_OBJ_PORT_VLAN(obj),
515 cb);
Vivien Didelot11149532015-08-13 12:52:17 -0400516 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400517 default:
518 err = -EOPNOTSUPP;
519 break;
520 }
521
522 return err;
523}
524
Florian Fainellib73adef2015-02-24 13:15:33 -0800525static int dsa_slave_bridge_port_join(struct net_device *dev,
526 struct net_device *br)
527{
528 struct dsa_slave_priv *p = netdev_priv(dev);
529 struct dsa_switch *ds = p->parent;
530 int ret = -EOPNOTSUPP;
531
532 p->bridge_dev = br;
533
534 if (ds->drv->port_join_bridge)
535 ret = ds->drv->port_join_bridge(ds, p->port,
536 dsa_slave_br_port_mask(ds, br));
537
538 return ret;
539}
540
541static int dsa_slave_bridge_port_leave(struct net_device *dev)
542{
543 struct dsa_slave_priv *p = netdev_priv(dev);
544 struct dsa_switch *ds = p->parent;
545 int ret = -EOPNOTSUPP;
546
547
548 if (ds->drv->port_leave_bridge)
549 ret = ds->drv->port_leave_bridge(ds, p->port,
550 dsa_slave_br_port_mask(ds, p->bridge_dev));
551
552 p->bridge_dev = NULL;
553
554 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
555 * so allow it to be in BR_STATE_FORWARDING to be kept functional
556 */
557 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
558
559 return ret;
560}
561
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700562static int dsa_slave_port_attr_get(struct net_device *dev,
563 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800564{
565 struct dsa_slave_priv *p = netdev_priv(dev);
566 struct dsa_switch *ds = p->parent;
567
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700568 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200569 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -0700570 attr->u.ppid.id_len = sizeof(ds->index);
571 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700572 break;
573 default:
574 return -EOPNOTSUPP;
575 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800576
577 return 0;
578}
579
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700580static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
581 struct sk_buff *skb)
582{
583#ifdef CONFIG_NET_POLL_CONTROLLER
584 if (p->netpoll)
585 netpoll_send_skb(p->netpoll, skb);
586#else
587 BUG();
588#endif
589 return NETDEV_TX_OK;
590}
591
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700592static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
593{
594 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700595 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700596
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700597 dev->stats.tx_packets++;
598 dev->stats.tx_bytes += skb->len;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700599
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700600 /* Transmit function may have to reallocate the original SKB */
601 nskb = p->xmit(skb, dev);
602 if (!nskb)
603 return NETDEV_TX_OK;
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700604
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700605 /* SKB for netpoll still need to be mangled with the protocol-specific
606 * tag to be successfully transmitted
607 */
608 if (unlikely(netpoll_tx_running(dev)))
609 return dsa_netpoll_send_skb(p, nskb);
610
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700611 /* Queue the SKB for transmission on the parent interface, but
612 * do not modify its EtherType
613 */
614 nskb->dev = p->parent->dst->master_netdev;
615 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700616
617 return NETDEV_TX_OK;
618}
619
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700620static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
621 struct net_device *dev)
622{
623 /* Just return the original SKB */
624 return skb;
625}
626
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000627
628/* ethtool operations *******************************************************/
629static int
630dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
631{
632 struct dsa_slave_priv *p = netdev_priv(dev);
633 int err;
634
635 err = -EOPNOTSUPP;
636 if (p->phy != NULL) {
637 err = phy_read_status(p->phy);
638 if (err == 0)
639 err = phy_ethtool_gset(p->phy, cmd);
640 }
641
642 return err;
643}
644
645static int
646dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
647{
648 struct dsa_slave_priv *p = netdev_priv(dev);
649
650 if (p->phy != NULL)
651 return phy_ethtool_sset(p->phy, cmd);
652
653 return -EOPNOTSUPP;
654}
655
656static void dsa_slave_get_drvinfo(struct net_device *dev,
657 struct ethtool_drvinfo *drvinfo)
658{
Jiri Pirko7826d432013-01-06 00:44:26 +0000659 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
660 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
661 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
662 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000663}
664
Guenter Roeck3d762a02014-10-29 10:45:04 -0700665static int dsa_slave_get_regs_len(struct net_device *dev)
666{
667 struct dsa_slave_priv *p = netdev_priv(dev);
668 struct dsa_switch *ds = p->parent;
669
670 if (ds->drv->get_regs_len)
671 return ds->drv->get_regs_len(ds, p->port);
672
673 return -EOPNOTSUPP;
674}
675
676static void
677dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
678{
679 struct dsa_slave_priv *p = netdev_priv(dev);
680 struct dsa_switch *ds = p->parent;
681
682 if (ds->drv->get_regs)
683 ds->drv->get_regs(ds, p->port, regs, _p);
684}
685
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000686static int dsa_slave_nway_reset(struct net_device *dev)
687{
688 struct dsa_slave_priv *p = netdev_priv(dev);
689
690 if (p->phy != NULL)
691 return genphy_restart_aneg(p->phy);
692
693 return -EOPNOTSUPP;
694}
695
696static u32 dsa_slave_get_link(struct net_device *dev)
697{
698 struct dsa_slave_priv *p = netdev_priv(dev);
699
700 if (p->phy != NULL) {
701 genphy_update_link(p->phy);
702 return p->phy->link;
703 }
704
705 return -EOPNOTSUPP;
706}
707
Guenter Roeck6793abb2014-10-29 10:45:01 -0700708static int dsa_slave_get_eeprom_len(struct net_device *dev)
709{
710 struct dsa_slave_priv *p = netdev_priv(dev);
711 struct dsa_switch *ds = p->parent;
712
713 if (ds->pd->eeprom_len)
714 return ds->pd->eeprom_len;
715
716 if (ds->drv->get_eeprom_len)
717 return ds->drv->get_eeprom_len(ds);
718
719 return 0;
720}
721
722static int dsa_slave_get_eeprom(struct net_device *dev,
723 struct ethtool_eeprom *eeprom, u8 *data)
724{
725 struct dsa_slave_priv *p = netdev_priv(dev);
726 struct dsa_switch *ds = p->parent;
727
728 if (ds->drv->get_eeprom)
729 return ds->drv->get_eeprom(ds, eeprom, data);
730
731 return -EOPNOTSUPP;
732}
733
734static int dsa_slave_set_eeprom(struct net_device *dev,
735 struct ethtool_eeprom *eeprom, u8 *data)
736{
737 struct dsa_slave_priv *p = netdev_priv(dev);
738 struct dsa_switch *ds = p->parent;
739
740 if (ds->drv->set_eeprom)
741 return ds->drv->set_eeprom(ds, eeprom, data);
742
743 return -EOPNOTSUPP;
744}
745
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000746static void dsa_slave_get_strings(struct net_device *dev,
747 uint32_t stringset, uint8_t *data)
748{
749 struct dsa_slave_priv *p = netdev_priv(dev);
750 struct dsa_switch *ds = p->parent;
751
752 if (stringset == ETH_SS_STATS) {
753 int len = ETH_GSTRING_LEN;
754
755 strncpy(data, "tx_packets", len);
756 strncpy(data + len, "tx_bytes", len);
757 strncpy(data + 2 * len, "rx_packets", len);
758 strncpy(data + 3 * len, "rx_bytes", len);
759 if (ds->drv->get_strings != NULL)
760 ds->drv->get_strings(ds, p->port, data + 4 * len);
761 }
762}
763
764static void dsa_slave_get_ethtool_stats(struct net_device *dev,
765 struct ethtool_stats *stats,
766 uint64_t *data)
767{
768 struct dsa_slave_priv *p = netdev_priv(dev);
769 struct dsa_switch *ds = p->parent;
770
771 data[0] = p->dev->stats.tx_packets;
772 data[1] = p->dev->stats.tx_bytes;
773 data[2] = p->dev->stats.rx_packets;
774 data[3] = p->dev->stats.rx_bytes;
775 if (ds->drv->get_ethtool_stats != NULL)
776 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
777}
778
779static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
780{
781 struct dsa_slave_priv *p = netdev_priv(dev);
782 struct dsa_switch *ds = p->parent;
783
784 if (sset == ETH_SS_STATS) {
785 int count;
786
787 count = 4;
788 if (ds->drv->get_sset_count != NULL)
789 count += ds->drv->get_sset_count(ds);
790
791 return count;
792 }
793
794 return -EOPNOTSUPP;
795}
796
Florian Fainelli19e57c42014-09-18 17:31:24 -0700797static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
798{
799 struct dsa_slave_priv *p = netdev_priv(dev);
800 struct dsa_switch *ds = p->parent;
801
802 if (ds->drv->get_wol)
803 ds->drv->get_wol(ds, p->port, w);
804}
805
806static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
807{
808 struct dsa_slave_priv *p = netdev_priv(dev);
809 struct dsa_switch *ds = p->parent;
810 int ret = -EOPNOTSUPP;
811
812 if (ds->drv->set_wol)
813 ret = ds->drv->set_wol(ds, p->port, w);
814
815 return ret;
816}
817
Florian Fainelli79052882014-09-24 17:05:21 -0700818static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
819{
820 struct dsa_slave_priv *p = netdev_priv(dev);
821 struct dsa_switch *ds = p->parent;
822 int ret;
823
824 if (!ds->drv->set_eee)
825 return -EOPNOTSUPP;
826
827 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
828 if (ret)
829 return ret;
830
831 if (p->phy)
832 ret = phy_ethtool_set_eee(p->phy, e);
833
834 return ret;
835}
836
837static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
838{
839 struct dsa_slave_priv *p = netdev_priv(dev);
840 struct dsa_switch *ds = p->parent;
841 int ret;
842
843 if (!ds->drv->get_eee)
844 return -EOPNOTSUPP;
845
846 ret = ds->drv->get_eee(ds, p->port, e);
847 if (ret)
848 return ret;
849
850 if (p->phy)
851 ret = phy_ethtool_get_eee(p->phy, e);
852
853 return ret;
854}
855
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700856#ifdef CONFIG_NET_POLL_CONTROLLER
857static int dsa_slave_netpoll_setup(struct net_device *dev,
858 struct netpoll_info *ni)
859{
860 struct dsa_slave_priv *p = netdev_priv(dev);
861 struct dsa_switch *ds = p->parent;
862 struct net_device *master = ds->dst->master_netdev;
863 struct netpoll *netpoll;
864 int err = 0;
865
866 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
867 if (!netpoll)
868 return -ENOMEM;
869
870 err = __netpoll_setup(netpoll, master);
871 if (err) {
872 kfree(netpoll);
873 goto out;
874 }
875
876 p->netpoll = netpoll;
877out:
878 return err;
879}
880
881static void dsa_slave_netpoll_cleanup(struct net_device *dev)
882{
883 struct dsa_slave_priv *p = netdev_priv(dev);
884 struct netpoll *netpoll = p->netpoll;
885
886 if (!netpoll)
887 return;
888
889 p->netpoll = NULL;
890
891 __netpoll_free_async(netpoll);
892}
893
894static void dsa_slave_poll_controller(struct net_device *dev)
895{
896}
897#endif
898
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000899static const struct ethtool_ops dsa_slave_ethtool_ops = {
900 .get_settings = dsa_slave_get_settings,
901 .set_settings = dsa_slave_set_settings,
902 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700903 .get_regs_len = dsa_slave_get_regs_len,
904 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000905 .nway_reset = dsa_slave_nway_reset,
906 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700907 .get_eeprom_len = dsa_slave_get_eeprom_len,
908 .get_eeprom = dsa_slave_get_eeprom,
909 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000910 .get_strings = dsa_slave_get_strings,
911 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
912 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700913 .set_wol = dsa_slave_set_wol,
914 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700915 .set_eee = dsa_slave_set_eee,
916 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000917};
918
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700919static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800920 .ndo_open = dsa_slave_open,
921 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700922 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800923 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
924 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800925 .ndo_set_mac_address = dsa_slave_set_mac_address,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400926 .ndo_fdb_add = switchdev_port_fdb_add,
927 .ndo_fdb_del = switchdev_port_fdb_del,
928 .ndo_fdb_dump = switchdev_port_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800929 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +0200930 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700931#ifdef CONFIG_NET_POLL_CONTROLLER
932 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
933 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
934 .ndo_poll_controller = dsa_slave_poll_controller,
935#endif
Vivien Didelot11149532015-08-13 12:52:17 -0400936 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
937 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
938 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Scott Feldman98237d42015-03-15 21:07:15 -0700939};
940
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700941static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700942 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -0700943 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400944 .switchdev_port_obj_add = dsa_slave_port_obj_add,
945 .switchdev_port_obj_del = dsa_slave_port_obj_del,
946 .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800947};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000948
Florian Fainellif37db852015-09-23 18:19:58 -0700949static struct device_type dsa_type = {
950 .name = "dsa",
951};
952
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700953static void dsa_slave_adjust_link(struct net_device *dev)
954{
955 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700956 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700957 unsigned int status_changed = 0;
958
959 if (p->old_link != p->phy->link) {
960 status_changed = 1;
961 p->old_link = p->phy->link;
962 }
963
964 if (p->old_duplex != p->phy->duplex) {
965 status_changed = 1;
966 p->old_duplex = p->phy->duplex;
967 }
968
969 if (p->old_pause != p->phy->pause) {
970 status_changed = 1;
971 p->old_pause = p->phy->pause;
972 }
973
Florian Fainelliec9436b2014-08-27 17:04:53 -0700974 if (ds->drv->adjust_link && status_changed)
975 ds->drv->adjust_link(ds, p->port, p->phy);
976
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700977 if (status_changed)
978 phy_print_status(p->phy);
979}
980
Florian Fainellice31b312014-08-27 17:04:54 -0700981static int dsa_slave_fixed_link_update(struct net_device *dev,
982 struct fixed_phy_status *status)
983{
984 struct dsa_slave_priv *p = netdev_priv(dev);
985 struct dsa_switch *ds = p->parent;
986
987 if (ds->drv->fixed_link_update)
988 ds->drv->fixed_link_update(ds, p->port, status);
989
990 return 0;
991}
992
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000993/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -0700994static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700995 struct net_device *slave_dev,
996 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -0700997{
998 struct dsa_switch *ds = p->parent;
999
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001000 p->phy = ds->slave_mii_bus->phy_map[addr];
Russell Kingd25b8e72015-10-03 18:09:07 +01001001 if (!p->phy) {
1002 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001003 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001004 }
Florian Fainellic305c162015-03-10 16:57:12 -07001005
1006 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001007 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1008 p->phy_interface = p->phy->interface;
Florian Fainellic305c162015-03-10 16:57:12 -07001009 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1010 p->phy_interface);
1011
1012 return 0;
1013}
1014
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001015static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001016 struct net_device *slave_dev)
1017{
1018 struct dsa_switch *ds = p->parent;
1019 struct dsa_chip_data *cd = ds->pd;
1020 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001021 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001022 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001023 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001024
1025 port_dn = cd->port_dn[p->port];
Guenter Roeck19334922015-02-16 21:23:51 -08001026 mode = of_get_phy_mode(port_dn);
1027 if (mode < 0)
1028 mode = PHY_INTERFACE_MODE_NA;
1029 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001030
1031 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
1032 if (of_phy_is_fixed_link(port_dn)) {
1033 /* In the case of a fixed PHY, the DT node associated
1034 * to the fixed PHY is the Port DT node
1035 */
1036 ret = of_phy_register_fixed_link(port_dn);
1037 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001038 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001039 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001040 }
Florian Fainellice31b312014-08-27 17:04:54 -07001041 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001042 phy_dn = port_dn;
1043 }
1044
Florian Fainelli68195632014-09-19 13:07:54 -07001045 if (ds->drv->get_phy_flags)
1046 phy_flags = ds->drv->get_phy_flags(ds, p->port);
1047
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001048 if (phy_dn) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001049 int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1050
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001051 /* If this PHY address is part of phys_mii_mask, which means
1052 * that we need to divert reads and writes to/from it, then we
1053 * want to bind this device using the slave MII bus created by
1054 * DSA to make that happen.
1055 */
Russell Kingd25b8e72015-10-03 18:09:07 +01001056 if (!phy_is_fixed && phy_id >= 0 &&
1057 (ds->phys_mii_mask & (1 << phy_id))) {
1058 ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1059 if (ret) {
1060 netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001061 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001062 }
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001063 } else {
1064 p->phy = of_phy_connect(slave_dev, phy_dn,
1065 dsa_slave_adjust_link,
1066 phy_flags,
1067 p->phy_interface);
1068 }
1069 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001070
Florian Fainellice31b312014-08-27 17:04:54 -07001071 if (p->phy && phy_is_fixed)
1072 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1073
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001074 /* We could not connect to a designated PHY, so use the switch internal
1075 * MDIO bus instead
1076 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001077 if (!p->phy) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001078 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
Russell Kingd25b8e72015-10-03 18:09:07 +01001079 if (ret) {
1080 netdev_err(slave_dev, "failed to connect to port %d: %d\n", p->port, ret);
Florian Fainellic305c162015-03-10 16:57:12 -07001081 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001082 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001083 } else {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001084 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
1085 p->phy->addr, p->phy->drv->name);
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001086 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001087
1088 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001089}
1090
Andrew Lunn448b4482015-05-06 01:09:56 +02001091static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1092static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1093 struct netdev_queue *txq,
1094 void *_unused)
1095{
1096 lockdep_set_class(&txq->_xmit_lock,
1097 &dsa_slave_netdev_xmit_lock_key);
1098}
1099
Florian Fainelli24462542014-09-18 17:31:22 -07001100int dsa_slave_suspend(struct net_device *slave_dev)
1101{
1102 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1103
Florian Fainelli24462542014-09-18 17:31:22 -07001104 if (p->phy) {
1105 phy_stop(p->phy);
1106 p->old_pause = -1;
1107 p->old_link = -1;
1108 p->old_duplex = -1;
1109 phy_suspend(p->phy);
1110 }
1111
1112 return 0;
1113}
1114
1115int dsa_slave_resume(struct net_device *slave_dev)
1116{
1117 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1118
1119 netif_device_attach(slave_dev);
1120
1121 if (p->phy) {
1122 phy_resume(p->phy);
1123 phy_start(p->phy);
1124 }
1125
1126 return 0;
1127}
1128
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001129int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1130 int port, char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001131{
Lennert Buytenheke84665c2009-03-20 09:52:09 +00001132 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001133 struct net_device *slave_dev;
1134 struct dsa_slave_priv *p;
1135 int ret;
1136
Tom Gundersenc835a672014-07-14 16:37:24 +02001137 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1138 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001139 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001140 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001141
1142 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001143 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001144 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001145 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001146 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001147 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Florian Fainellif37db852015-09-23 18:19:58 -07001148 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001149
Andrew Lunn448b4482015-05-06 01:09:56 +02001150 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1151 NULL);
1152
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001153 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -07001154 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001155 slave_dev->vlan_features = master->vlan_features;
1156
1157 p = netdev_priv(slave_dev);
1158 p->dev = slave_dev;
1159 p->parent = ds;
1160 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001161
Alexander Duyck50753142014-09-15 13:00:19 -04001162 switch (ds->dst->tag_protocol) {
1163#ifdef CONFIG_NET_DSA_TAG_DSA
1164 case DSA_TAG_PROTO_DSA:
1165 p->xmit = dsa_netdev_ops.xmit;
1166 break;
1167#endif
1168#ifdef CONFIG_NET_DSA_TAG_EDSA
1169 case DSA_TAG_PROTO_EDSA:
1170 p->xmit = edsa_netdev_ops.xmit;
1171 break;
1172#endif
1173#ifdef CONFIG_NET_DSA_TAG_TRAILER
1174 case DSA_TAG_PROTO_TRAILER:
1175 p->xmit = trailer_netdev_ops.xmit;
1176 break;
1177#endif
1178#ifdef CONFIG_NET_DSA_TAG_BRCM
1179 case DSA_TAG_PROTO_BRCM:
1180 p->xmit = brcm_netdev_ops.xmit;
1181 break;
1182#endif
1183 default:
1184 p->xmit = dsa_slave_notag_xmit;
1185 break;
1186 }
1187
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001188 p->old_pause = -1;
1189 p->old_link = -1;
1190 p->old_duplex = -1;
1191
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001192 ret = dsa_slave_phy_setup(p, slave_dev);
1193 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001194 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001195 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001196 return ret;
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001197 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001198
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001199 ds->ports[port] = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001200 ret = register_netdev(slave_dev);
1201 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -08001202 netdev_err(master, "error %d registering interface %s\n",
1203 ret, slave_dev->name);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001204 phy_disconnect(p->phy);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001205 ds->ports[port] = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001206 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001207 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001208 }
1209
1210 netif_carrier_off(slave_dev);
1211
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001212 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001213}
Florian Fainellib73adef2015-02-24 13:15:33 -08001214
1215static bool dsa_slave_dev_check(struct net_device *dev)
1216{
1217 return dev->netdev_ops == &dsa_slave_netdev_ops;
1218}
1219
1220static int dsa_slave_master_changed(struct net_device *dev)
1221{
1222 struct net_device *master = netdev_master_upper_dev_get(dev);
Guenter Roeckb06b1072015-03-25 08:08:37 -07001223 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellib73adef2015-02-24 13:15:33 -08001224 int err = 0;
1225
1226 if (master && master->rtnl_link_ops &&
1227 !strcmp(master->rtnl_link_ops->kind, "bridge"))
1228 err = dsa_slave_bridge_port_join(dev, master);
Guenter Roeckb06b1072015-03-25 08:08:37 -07001229 else if (dsa_port_is_bridged(p))
Florian Fainellib73adef2015-02-24 13:15:33 -08001230 err = dsa_slave_bridge_port_leave(dev);
1231
1232 return err;
1233}
1234
1235int dsa_slave_netdevice_event(struct notifier_block *unused,
1236 unsigned long event, void *ptr)
1237{
1238 struct net_device *dev;
1239 int err = 0;
1240
1241 switch (event) {
1242 case NETDEV_CHANGEUPPER:
1243 dev = netdev_notifier_info_to_dev(ptr);
1244 if (!dsa_slave_dev_check(dev))
1245 goto out;
1246
1247 err = dsa_slave_master_changed(dev);
Vivien Didelotefd29b32015-10-11 18:08:37 -04001248 if (err && err != -EOPNOTSUPP)
Florian Fainellib73adef2015-02-24 13:15:33 -08001249 netdev_warn(dev, "failed to reflect master change\n");
1250
1251 break;
1252 }
1253
1254out:
1255 return NOTIFY_DONE;
1256}