blob: 3597724ec3d82399fd40b515d27ebada8c6a4cfa [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>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000021#include "dsa_priv.h"
22
23/* slave mii_bus handling ***************************************************/
24static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
25{
26 struct dsa_switch *ds = bus->priv;
27
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070028 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029 return ds->drv->phy_read(ds, addr, reg);
30
31 return 0xffff;
32}
33
34static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
35{
36 struct dsa_switch *ds = bus->priv;
37
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070038 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000039 return ds->drv->phy_write(ds, addr, reg, val);
40
41 return 0;
42}
43
44void dsa_slave_mii_bus_init(struct dsa_switch *ds)
45{
46 ds->slave_mii_bus->priv = (void *)ds;
47 ds->slave_mii_bus->name = "dsa slave smi";
48 ds->slave_mii_bus->read = dsa_slave_phy_read;
49 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainellif490be02013-01-21 09:58:50 +000050 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
51 ds->index, ds->pd->sw_addr);
Alexander Duyckb4d23942014-09-15 13:00:27 -040052 ds->slave_mii_bus->parent = ds->master_dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050053 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000054}
55
56
57/* slave device handling ****************************************************/
Lennert Buytenhekc0840802009-03-20 09:49:49 +000058static int dsa_slave_init(struct net_device *dev)
59{
60 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000061
Lennert Buytenheke84665c2009-03-20 09:52:09 +000062 dev->iflink = p->parent->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000063
64 return 0;
65}
66
Florian Fainellib73adef2015-02-24 13:15:33 -080067static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
68{
69 return !!p->bridge_dev;
70}
71
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072static int dsa_slave_open(struct net_device *dev)
73{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080074 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +000075 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -070076 struct dsa_switch *ds = p->parent;
Florian Fainellib73adef2015-02-24 13:15:33 -080077 u8 stp_state = dsa_port_is_bridged(p) ?
78 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080079 int err;
80
81 if (!(master->flags & IFF_UP))
82 return -ENETDOWN;
83
Joe Perches8feedbb2012-05-08 18:56:57 +000084 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000085 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080086 if (err < 0)
87 goto out;
88 }
89
90 if (dev->flags & IFF_ALLMULTI) {
91 err = dev_set_allmulti(master, 1);
92 if (err < 0)
93 goto del_unicast;
94 }
95 if (dev->flags & IFF_PROMISC) {
96 err = dev_set_promiscuity(master, 1);
97 if (err < 0)
98 goto clear_allmulti;
99 }
100
Florian Fainellib2f2af22014-09-24 17:05:18 -0700101 if (ds->drv->port_enable) {
102 err = ds->drv->port_enable(ds, p->port, p->phy);
103 if (err)
104 goto clear_promisc;
105 }
106
Florian Fainellib73adef2015-02-24 13:15:33 -0800107 if (ds->drv->port_stp_update)
108 ds->drv->port_stp_update(ds, p->port, stp_state);
109
Florian Fainellif7f1de52014-09-24 17:05:17 -0700110 if (p->phy)
111 phy_start(p->phy);
112
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000113 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800114
Florian Fainellib2f2af22014-09-24 17:05:18 -0700115clear_promisc:
116 if (dev->flags & IFF_PROMISC)
117 dev_set_promiscuity(master, 0);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800118clear_allmulti:
119 if (dev->flags & IFF_ALLMULTI)
120 dev_set_allmulti(master, -1);
121del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000122 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000123 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800124out:
125 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000126}
127
128static int dsa_slave_close(struct net_device *dev)
129{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800130 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000131 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -0700132 struct dsa_switch *ds = p->parent;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800133
Florian Fainellif7f1de52014-09-24 17:05:17 -0700134 if (p->phy)
135 phy_stop(p->phy);
136
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800137 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000138 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800139 if (dev->flags & IFF_ALLMULTI)
140 dev_set_allmulti(master, -1);
141 if (dev->flags & IFF_PROMISC)
142 dev_set_promiscuity(master, -1);
143
Joe Perches8feedbb2012-05-08 18:56:57 +0000144 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000145 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800146
Florian Fainellib2f2af22014-09-24 17:05:18 -0700147 if (ds->drv->port_disable)
148 ds->drv->port_disable(ds, p->port, p->phy);
149
Florian Fainellib73adef2015-02-24 13:15:33 -0800150 if (ds->drv->port_stp_update)
151 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
152
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000153 return 0;
154}
155
156static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
157{
158 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000159 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000160
161 if (change & IFF_ALLMULTI)
162 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
163 if (change & IFF_PROMISC)
164 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
165}
166
167static void dsa_slave_set_rx_mode(struct net_device *dev)
168{
169 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000170 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000171
172 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000173 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000174}
175
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800176static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000177{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800178 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000179 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800180 struct sockaddr *addr = a;
181 int err;
182
183 if (!is_valid_ether_addr(addr->sa_data))
184 return -EADDRNOTAVAIL;
185
186 if (!(dev->flags & IFF_UP))
187 goto out;
188
Joe Perches8feedbb2012-05-08 18:56:57 +0000189 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000190 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800191 if (err < 0)
192 return err;
193 }
194
Joe Perches8feedbb2012-05-08 18:56:57 +0000195 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000196 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800197
198out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800199 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000200
201 return 0;
202}
203
Guenter Roeck339d8262015-03-26 18:36:37 -0700204static int dsa_slave_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
205 struct net_device *dev,
206 const unsigned char *addr, u16 vid, u16 nlm_flags)
207{
208 struct dsa_slave_priv *p = netdev_priv(dev);
209 struct dsa_switch *ds = p->parent;
210 int ret = -EOPNOTSUPP;
211
212 if (ds->drv->fdb_add)
213 ret = ds->drv->fdb_add(ds, p->port, addr, vid);
214
215 return ret;
216}
217
218static int dsa_slave_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
219 struct net_device *dev,
220 const unsigned char *addr, u16 vid)
221{
222 struct dsa_slave_priv *p = netdev_priv(dev);
223 struct dsa_switch *ds = p->parent;
224 int ret = -EOPNOTSUPP;
225
226 if (ds->drv->fdb_del)
227 ret = ds->drv->fdb_del(ds, p->port, addr, vid);
228
229 return ret;
230}
231
232static int dsa_slave_fill_info(struct net_device *dev, struct sk_buff *skb,
233 const unsigned char *addr, u16 vid,
234 bool is_static,
235 u32 portid, u32 seq, int type,
236 unsigned int flags)
237{
238 struct nlmsghdr *nlh;
239 struct ndmsg *ndm;
240
241 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
242 if (!nlh)
243 return -EMSGSIZE;
244
245 ndm = nlmsg_data(nlh);
246 ndm->ndm_family = AF_BRIDGE;
247 ndm->ndm_pad1 = 0;
248 ndm->ndm_pad2 = 0;
249 ndm->ndm_flags = NTF_EXT_LEARNED;
250 ndm->ndm_type = 0;
251 ndm->ndm_ifindex = dev->ifindex;
252 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
253
254 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
255 goto nla_put_failure;
256
257 if (vid && nla_put_u16(skb, NDA_VLAN, vid))
258 goto nla_put_failure;
259
260 nlmsg_end(skb, nlh);
261 return 0;
262
263nla_put_failure:
264 nlmsg_cancel(skb, nlh);
265 return -EMSGSIZE;
266}
267
268/* Dump information about entries, in response to GETNEIGH */
269static int dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
270 struct net_device *dev,
271 struct net_device *filter_dev, int idx)
272{
273 struct dsa_slave_priv *p = netdev_priv(dev);
274 struct dsa_switch *ds = p->parent;
275 unsigned char addr[ETH_ALEN] = { 0 };
276 int ret;
277
278 if (!ds->drv->fdb_getnext)
279 return -EOPNOTSUPP;
280
281 for (; ; idx++) {
282 bool is_static;
283
284 ret = ds->drv->fdb_getnext(ds, p->port, addr, &is_static);
285 if (ret < 0)
286 break;
287
288 if (idx < cb->args[0])
289 continue;
290
291 ret = dsa_slave_fill_info(dev, skb, addr, 0,
292 is_static,
293 NETLINK_CB(cb->skb).portid,
294 cb->nlh->nlmsg_seq,
295 RTM_NEWNEIGH, NLM_F_MULTI);
296 if (ret < 0)
297 break;
298 }
299
300 return idx;
301}
302
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000303static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
304{
305 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000306
307 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000308 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000309
310 return -EOPNOTSUPP;
311}
312
Florian Fainellib73adef2015-02-24 13:15:33 -0800313/* Return a bitmask of all ports being currently bridged within a given bridge
314 * device. Note that on leave, the mask will still return the bitmask of ports
315 * currently bridged, prior to port removal, and this is exactly what we want.
316 */
317static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
318 struct net_device *bridge)
319{
320 struct dsa_slave_priv *p;
321 unsigned int port;
322 u32 mask = 0;
323
324 for (port = 0; port < DSA_MAX_PORTS; port++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800325 if (!dsa_is_port_initialized(ds, port))
Florian Fainellib73adef2015-02-24 13:15:33 -0800326 continue;
327
328 p = netdev_priv(ds->ports[port]);
329
330 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
331 p->bridge_dev == bridge)
332 mask |= 1 << port;
333 }
334
335 return mask;
336}
337
338static int dsa_slave_stp_update(struct net_device *dev, u8 state)
339{
340 struct dsa_slave_priv *p = netdev_priv(dev);
341 struct dsa_switch *ds = p->parent;
342 int ret = -EOPNOTSUPP;
343
344 if (ds->drv->port_stp_update)
345 ret = ds->drv->port_stp_update(ds, p->port, state);
346
347 return ret;
348}
349
350static int dsa_slave_bridge_port_join(struct net_device *dev,
351 struct net_device *br)
352{
353 struct dsa_slave_priv *p = netdev_priv(dev);
354 struct dsa_switch *ds = p->parent;
355 int ret = -EOPNOTSUPP;
356
357 p->bridge_dev = br;
358
359 if (ds->drv->port_join_bridge)
360 ret = ds->drv->port_join_bridge(ds, p->port,
361 dsa_slave_br_port_mask(ds, br));
362
363 return ret;
364}
365
366static int dsa_slave_bridge_port_leave(struct net_device *dev)
367{
368 struct dsa_slave_priv *p = netdev_priv(dev);
369 struct dsa_switch *ds = p->parent;
370 int ret = -EOPNOTSUPP;
371
372
373 if (ds->drv->port_leave_bridge)
374 ret = ds->drv->port_leave_bridge(ds, p->port,
375 dsa_slave_br_port_mask(ds, p->bridge_dev));
376
377 p->bridge_dev = NULL;
378
379 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
380 * so allow it to be in BR_STATE_FORWARDING to be kept functional
381 */
382 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
383
384 return ret;
385}
386
387static int dsa_slave_parent_id_get(struct net_device *dev,
388 struct netdev_phys_item_id *psid)
389{
390 struct dsa_slave_priv *p = netdev_priv(dev);
391 struct dsa_switch *ds = p->parent;
392
393 psid->id_len = sizeof(ds->index);
394 memcpy(&psid->id, &ds->index, psid->id_len);
395
396 return 0;
397}
398
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700399static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
400{
401 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700402
Alexander Duyck50753142014-09-15 13:00:19 -0400403 return p->xmit(skb, dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700404}
405
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700406static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
407 struct net_device *dev)
408{
409 struct dsa_slave_priv *p = netdev_priv(dev);
410
411 skb->dev = p->parent->dst->master_netdev;
412 dev_queue_xmit(skb);
413
414 return NETDEV_TX_OK;
415}
416
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000417
418/* ethtool operations *******************************************************/
419static int
420dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
421{
422 struct dsa_slave_priv *p = netdev_priv(dev);
423 int err;
424
425 err = -EOPNOTSUPP;
426 if (p->phy != NULL) {
427 err = phy_read_status(p->phy);
428 if (err == 0)
429 err = phy_ethtool_gset(p->phy, cmd);
430 }
431
432 return err;
433}
434
435static int
436dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
437{
438 struct dsa_slave_priv *p = netdev_priv(dev);
439
440 if (p->phy != NULL)
441 return phy_ethtool_sset(p->phy, cmd);
442
443 return -EOPNOTSUPP;
444}
445
446static void dsa_slave_get_drvinfo(struct net_device *dev,
447 struct ethtool_drvinfo *drvinfo)
448{
Jiri Pirko7826d432013-01-06 00:44:26 +0000449 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
450 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
451 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
452 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000453}
454
Guenter Roeck3d762a02014-10-29 10:45:04 -0700455static int dsa_slave_get_regs_len(struct net_device *dev)
456{
457 struct dsa_slave_priv *p = netdev_priv(dev);
458 struct dsa_switch *ds = p->parent;
459
460 if (ds->drv->get_regs_len)
461 return ds->drv->get_regs_len(ds, p->port);
462
463 return -EOPNOTSUPP;
464}
465
466static void
467dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
468{
469 struct dsa_slave_priv *p = netdev_priv(dev);
470 struct dsa_switch *ds = p->parent;
471
472 if (ds->drv->get_regs)
473 ds->drv->get_regs(ds, p->port, regs, _p);
474}
475
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000476static int dsa_slave_nway_reset(struct net_device *dev)
477{
478 struct dsa_slave_priv *p = netdev_priv(dev);
479
480 if (p->phy != NULL)
481 return genphy_restart_aneg(p->phy);
482
483 return -EOPNOTSUPP;
484}
485
486static u32 dsa_slave_get_link(struct net_device *dev)
487{
488 struct dsa_slave_priv *p = netdev_priv(dev);
489
490 if (p->phy != NULL) {
491 genphy_update_link(p->phy);
492 return p->phy->link;
493 }
494
495 return -EOPNOTSUPP;
496}
497
Guenter Roeck6793abb2014-10-29 10:45:01 -0700498static int dsa_slave_get_eeprom_len(struct net_device *dev)
499{
500 struct dsa_slave_priv *p = netdev_priv(dev);
501 struct dsa_switch *ds = p->parent;
502
503 if (ds->pd->eeprom_len)
504 return ds->pd->eeprom_len;
505
506 if (ds->drv->get_eeprom_len)
507 return ds->drv->get_eeprom_len(ds);
508
509 return 0;
510}
511
512static int dsa_slave_get_eeprom(struct net_device *dev,
513 struct ethtool_eeprom *eeprom, u8 *data)
514{
515 struct dsa_slave_priv *p = netdev_priv(dev);
516 struct dsa_switch *ds = p->parent;
517
518 if (ds->drv->get_eeprom)
519 return ds->drv->get_eeprom(ds, eeprom, data);
520
521 return -EOPNOTSUPP;
522}
523
524static int dsa_slave_set_eeprom(struct net_device *dev,
525 struct ethtool_eeprom *eeprom, u8 *data)
526{
527 struct dsa_slave_priv *p = netdev_priv(dev);
528 struct dsa_switch *ds = p->parent;
529
530 if (ds->drv->set_eeprom)
531 return ds->drv->set_eeprom(ds, eeprom, data);
532
533 return -EOPNOTSUPP;
534}
535
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000536static void dsa_slave_get_strings(struct net_device *dev,
537 uint32_t stringset, uint8_t *data)
538{
539 struct dsa_slave_priv *p = netdev_priv(dev);
540 struct dsa_switch *ds = p->parent;
541
542 if (stringset == ETH_SS_STATS) {
543 int len = ETH_GSTRING_LEN;
544
545 strncpy(data, "tx_packets", len);
546 strncpy(data + len, "tx_bytes", len);
547 strncpy(data + 2 * len, "rx_packets", len);
548 strncpy(data + 3 * len, "rx_bytes", len);
549 if (ds->drv->get_strings != NULL)
550 ds->drv->get_strings(ds, p->port, data + 4 * len);
551 }
552}
553
554static void dsa_slave_get_ethtool_stats(struct net_device *dev,
555 struct ethtool_stats *stats,
556 uint64_t *data)
557{
558 struct dsa_slave_priv *p = netdev_priv(dev);
559 struct dsa_switch *ds = p->parent;
560
561 data[0] = p->dev->stats.tx_packets;
562 data[1] = p->dev->stats.tx_bytes;
563 data[2] = p->dev->stats.rx_packets;
564 data[3] = p->dev->stats.rx_bytes;
565 if (ds->drv->get_ethtool_stats != NULL)
566 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
567}
568
569static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
570{
571 struct dsa_slave_priv *p = netdev_priv(dev);
572 struct dsa_switch *ds = p->parent;
573
574 if (sset == ETH_SS_STATS) {
575 int count;
576
577 count = 4;
578 if (ds->drv->get_sset_count != NULL)
579 count += ds->drv->get_sset_count(ds);
580
581 return count;
582 }
583
584 return -EOPNOTSUPP;
585}
586
Florian Fainelli19e57c42014-09-18 17:31:24 -0700587static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
588{
589 struct dsa_slave_priv *p = netdev_priv(dev);
590 struct dsa_switch *ds = p->parent;
591
592 if (ds->drv->get_wol)
593 ds->drv->get_wol(ds, p->port, w);
594}
595
596static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
597{
598 struct dsa_slave_priv *p = netdev_priv(dev);
599 struct dsa_switch *ds = p->parent;
600 int ret = -EOPNOTSUPP;
601
602 if (ds->drv->set_wol)
603 ret = ds->drv->set_wol(ds, p->port, w);
604
605 return ret;
606}
607
Florian Fainelli79052882014-09-24 17:05:21 -0700608static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
609{
610 struct dsa_slave_priv *p = netdev_priv(dev);
611 struct dsa_switch *ds = p->parent;
612 int ret;
613
614 if (!ds->drv->set_eee)
615 return -EOPNOTSUPP;
616
617 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
618 if (ret)
619 return ret;
620
621 if (p->phy)
622 ret = phy_ethtool_set_eee(p->phy, e);
623
624 return ret;
625}
626
627static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
628{
629 struct dsa_slave_priv *p = netdev_priv(dev);
630 struct dsa_switch *ds = p->parent;
631 int ret;
632
633 if (!ds->drv->get_eee)
634 return -EOPNOTSUPP;
635
636 ret = ds->drv->get_eee(ds, p->port, e);
637 if (ret)
638 return ret;
639
640 if (p->phy)
641 ret = phy_ethtool_get_eee(p->phy, e);
642
643 return ret;
644}
645
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000646static const struct ethtool_ops dsa_slave_ethtool_ops = {
647 .get_settings = dsa_slave_get_settings,
648 .set_settings = dsa_slave_set_settings,
649 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700650 .get_regs_len = dsa_slave_get_regs_len,
651 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000652 .nway_reset = dsa_slave_nway_reset,
653 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700654 .get_eeprom_len = dsa_slave_get_eeprom_len,
655 .get_eeprom = dsa_slave_get_eeprom,
656 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000657 .get_strings = dsa_slave_get_strings,
658 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
659 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700660 .set_wol = dsa_slave_set_wol,
661 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700662 .set_eee = dsa_slave_set_eee,
663 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000664};
665
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700666static const struct net_device_ops dsa_slave_netdev_ops = {
Lennert Buytenhekc0840802009-03-20 09:49:49 +0000667 .ndo_init = dsa_slave_init,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800668 .ndo_open = dsa_slave_open,
669 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700670 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800671 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
672 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800673 .ndo_set_mac_address = dsa_slave_set_mac_address,
Guenter Roeck339d8262015-03-26 18:36:37 -0700674 .ndo_fdb_add = dsa_slave_fdb_add,
675 .ndo_fdb_del = dsa_slave_fdb_del,
676 .ndo_fdb_dump = dsa_slave_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800677 .ndo_do_ioctl = dsa_slave_ioctl,
Scott Feldman98237d42015-03-15 21:07:15 -0700678};
679
680static const struct swdev_ops dsa_slave_swdev_ops = {
681 .swdev_parent_id_get = dsa_slave_parent_id_get,
682 .swdev_port_stp_update = dsa_slave_stp_update,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800683};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000684
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700685static void dsa_slave_adjust_link(struct net_device *dev)
686{
687 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700688 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700689 unsigned int status_changed = 0;
690
691 if (p->old_link != p->phy->link) {
692 status_changed = 1;
693 p->old_link = p->phy->link;
694 }
695
696 if (p->old_duplex != p->phy->duplex) {
697 status_changed = 1;
698 p->old_duplex = p->phy->duplex;
699 }
700
701 if (p->old_pause != p->phy->pause) {
702 status_changed = 1;
703 p->old_pause = p->phy->pause;
704 }
705
Florian Fainelliec9436b2014-08-27 17:04:53 -0700706 if (ds->drv->adjust_link && status_changed)
707 ds->drv->adjust_link(ds, p->port, p->phy);
708
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700709 if (status_changed)
710 phy_print_status(p->phy);
711}
712
Florian Fainellice31b312014-08-27 17:04:54 -0700713static int dsa_slave_fixed_link_update(struct net_device *dev,
714 struct fixed_phy_status *status)
715{
716 struct dsa_slave_priv *p = netdev_priv(dev);
717 struct dsa_switch *ds = p->parent;
718
719 if (ds->drv->fixed_link_update)
720 ds->drv->fixed_link_update(ds, p->port, status);
721
722 return 0;
723}
724
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000725/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -0700726static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700727 struct net_device *slave_dev,
728 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -0700729{
730 struct dsa_switch *ds = p->parent;
731
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700732 p->phy = ds->slave_mii_bus->phy_map[addr];
Florian Fainellic305c162015-03-10 16:57:12 -0700733 if (!p->phy)
734 return -ENODEV;
735
736 /* Use already configured phy mode */
737 p->phy_interface = p->phy->interface;
738 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
739 p->phy_interface);
740
741 return 0;
742}
743
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800744static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700745 struct net_device *slave_dev)
746{
747 struct dsa_switch *ds = p->parent;
748 struct dsa_chip_data *cd = ds->pd;
749 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -0700750 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -0700751 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -0800752 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700753
754 port_dn = cd->port_dn[p->port];
Guenter Roeck19334922015-02-16 21:23:51 -0800755 mode = of_get_phy_mode(port_dn);
756 if (mode < 0)
757 mode = PHY_INTERFACE_MODE_NA;
758 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700759
760 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
761 if (of_phy_is_fixed_link(port_dn)) {
762 /* In the case of a fixed PHY, the DT node associated
763 * to the fixed PHY is the Port DT node
764 */
765 ret = of_phy_register_fixed_link(port_dn);
766 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800767 netdev_err(slave_dev, "failed to register fixed PHY\n");
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800768 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700769 }
Florian Fainellice31b312014-08-27 17:04:54 -0700770 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700771 phy_dn = port_dn;
772 }
773
Florian Fainelli68195632014-09-19 13:07:54 -0700774 if (ds->drv->get_phy_flags)
775 phy_flags = ds->drv->get_phy_flags(ds, p->port);
776
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700777 if (phy_dn) {
778 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
779 /* If this PHY address is part of phys_mii_mask, which means
780 * that we need to divert reads and writes to/from it, then we
781 * want to bind this device using the slave MII bus created by
782 * DSA to make that happen.
783 */
Florian Fainelli96026d02015-03-14 13:21:59 -0700784 if (!phy_is_fixed && ret >= 0 &&
785 (ds->phys_mii_mask & (1 << ret))) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700786 ret = dsa_slave_phy_connect(p, slave_dev, ret);
787 if (ret)
788 return ret;
789 } else {
790 p->phy = of_phy_connect(slave_dev, phy_dn,
791 dsa_slave_adjust_link,
792 phy_flags,
793 p->phy_interface);
794 }
795 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700796
Florian Fainellice31b312014-08-27 17:04:54 -0700797 if (p->phy && phy_is_fixed)
798 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
799
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700800 /* We could not connect to a designated PHY, so use the switch internal
801 * MDIO bus instead
802 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100803 if (!p->phy) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700804 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
Florian Fainellic305c162015-03-10 16:57:12 -0700805 if (ret)
806 return ret;
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100807 } else {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800808 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
809 p->phy->addr, p->phy->drv->name);
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100810 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800811
812 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700813}
814
Florian Fainelli24462542014-09-18 17:31:22 -0700815int dsa_slave_suspend(struct net_device *slave_dev)
816{
817 struct dsa_slave_priv *p = netdev_priv(slave_dev);
818
819 netif_device_detach(slave_dev);
820
821 if (p->phy) {
822 phy_stop(p->phy);
823 p->old_pause = -1;
824 p->old_link = -1;
825 p->old_duplex = -1;
826 phy_suspend(p->phy);
827 }
828
829 return 0;
830}
831
832int dsa_slave_resume(struct net_device *slave_dev)
833{
834 struct dsa_slave_priv *p = netdev_priv(slave_dev);
835
836 netif_device_attach(slave_dev);
837
838 if (p->phy) {
839 phy_resume(p->phy);
840 phy_start(p->phy);
841 }
842
843 return 0;
844}
845
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800846int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
847 int port, char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000848{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000849 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000850 struct net_device *slave_dev;
851 struct dsa_slave_priv *p;
852 int ret;
853
Tom Gundersenc835a672014-07-14 16:37:24 +0200854 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
855 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000856 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800857 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000858
859 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000860 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +0200861 eth_hw_addr_inherit(slave_dev, master);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000862 slave_dev->tx_queue_len = 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700863 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Scott Feldman98237d42015-03-15 21:07:15 -0700864 slave_dev->swdev_ops = &dsa_slave_swdev_ops;
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800865
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000866 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -0700867 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000868 slave_dev->vlan_features = master->vlan_features;
869
870 p = netdev_priv(slave_dev);
871 p->dev = slave_dev;
872 p->parent = ds;
873 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700874
Alexander Duyck50753142014-09-15 13:00:19 -0400875 switch (ds->dst->tag_protocol) {
876#ifdef CONFIG_NET_DSA_TAG_DSA
877 case DSA_TAG_PROTO_DSA:
878 p->xmit = dsa_netdev_ops.xmit;
879 break;
880#endif
881#ifdef CONFIG_NET_DSA_TAG_EDSA
882 case DSA_TAG_PROTO_EDSA:
883 p->xmit = edsa_netdev_ops.xmit;
884 break;
885#endif
886#ifdef CONFIG_NET_DSA_TAG_TRAILER
887 case DSA_TAG_PROTO_TRAILER:
888 p->xmit = trailer_netdev_ops.xmit;
889 break;
890#endif
891#ifdef CONFIG_NET_DSA_TAG_BRCM
892 case DSA_TAG_PROTO_BRCM:
893 p->xmit = brcm_netdev_ops.xmit;
894 break;
895#endif
896 default:
897 p->xmit = dsa_slave_notag_xmit;
898 break;
899 }
900
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700901 p->old_pause = -1;
902 p->old_link = -1;
903 p->old_duplex = -1;
904
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800905 ret = dsa_slave_phy_setup(p, slave_dev);
906 if (ret) {
907 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800908 return ret;
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800909 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000910
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800911 ds->ports[port] = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000912 ret = register_netdev(slave_dev);
913 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800914 netdev_err(master, "error %d registering interface %s\n",
915 ret, slave_dev->name);
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800916 phy_disconnect(p->phy);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800917 ds->ports[port] = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000918 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800919 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000920 }
921
922 netif_carrier_off(slave_dev);
923
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800924 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000925}
Florian Fainellib73adef2015-02-24 13:15:33 -0800926
927static bool dsa_slave_dev_check(struct net_device *dev)
928{
929 return dev->netdev_ops == &dsa_slave_netdev_ops;
930}
931
932static int dsa_slave_master_changed(struct net_device *dev)
933{
934 struct net_device *master = netdev_master_upper_dev_get(dev);
Guenter Roeckb06b1072015-03-25 08:08:37 -0700935 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellib73adef2015-02-24 13:15:33 -0800936 int err = 0;
937
938 if (master && master->rtnl_link_ops &&
939 !strcmp(master->rtnl_link_ops->kind, "bridge"))
940 err = dsa_slave_bridge_port_join(dev, master);
Guenter Roeckb06b1072015-03-25 08:08:37 -0700941 else if (dsa_port_is_bridged(p))
Florian Fainellib73adef2015-02-24 13:15:33 -0800942 err = dsa_slave_bridge_port_leave(dev);
943
944 return err;
945}
946
947int dsa_slave_netdevice_event(struct notifier_block *unused,
948 unsigned long event, void *ptr)
949{
950 struct net_device *dev;
951 int err = 0;
952
953 switch (event) {
954 case NETDEV_CHANGEUPPER:
955 dev = netdev_notifier_info_to_dev(ptr);
956 if (!dsa_slave_dev_check(dev))
957 goto out;
958
959 err = dsa_slave_master_changed(dev);
960 if (err)
961 netdev_warn(dev, "failed to reflect master change\n");
962
963 break;
964 }
965
966out:
967 return NOTIFY_DONE;
968}