blob: b5a4d8974b76e68b380e04011c41b40d7848b654 [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>
19#include <linux/if_bridge.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000020#include "dsa_priv.h"
21
22/* slave mii_bus handling ***************************************************/
23static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
24{
25 struct dsa_switch *ds = bus->priv;
26
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070027 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000028 return ds->drv->phy_read(ds, addr, reg);
29
30 return 0xffff;
31}
32
33static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
34{
35 struct dsa_switch *ds = bus->priv;
36
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070037 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000038 return ds->drv->phy_write(ds, addr, reg, val);
39
40 return 0;
41}
42
43void dsa_slave_mii_bus_init(struct dsa_switch *ds)
44{
45 ds->slave_mii_bus->priv = (void *)ds;
46 ds->slave_mii_bus->name = "dsa slave smi";
47 ds->slave_mii_bus->read = dsa_slave_phy_read;
48 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainellif490be02013-01-21 09:58:50 +000049 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
50 ds->index, ds->pd->sw_addr);
Alexander Duyckb4d23942014-09-15 13:00:27 -040051 ds->slave_mii_bus->parent = ds->master_dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050052 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000053}
54
55
56/* slave device handling ****************************************************/
Lennert Buytenhekc0840802009-03-20 09:49:49 +000057static int dsa_slave_init(struct net_device *dev)
58{
59 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000060
Lennert Buytenheke84665c2009-03-20 09:52:09 +000061 dev->iflink = p->parent->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000062
63 return 0;
64}
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)
116 dev_set_promiscuity(master, 0);
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
203static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
204{
205 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000206
207 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000208 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000209
210 return -EOPNOTSUPP;
211}
212
Florian Fainellib73adef2015-02-24 13:15:33 -0800213/* Return a bitmask of all ports being currently bridged within a given bridge
214 * device. Note that on leave, the mask will still return the bitmask of ports
215 * currently bridged, prior to port removal, and this is exactly what we want.
216 */
217static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
218 struct net_device *bridge)
219{
220 struct dsa_slave_priv *p;
221 unsigned int port;
222 u32 mask = 0;
223
224 for (port = 0; port < DSA_MAX_PORTS; port++) {
225 if (!((1 << port) & ds->phys_port_mask))
226 continue;
227
228 if (!ds->ports[port])
229 continue;
230
231 p = netdev_priv(ds->ports[port]);
232
233 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
234 p->bridge_dev == bridge)
235 mask |= 1 << port;
236 }
237
238 return mask;
239}
240
241static int dsa_slave_stp_update(struct net_device *dev, u8 state)
242{
243 struct dsa_slave_priv *p = netdev_priv(dev);
244 struct dsa_switch *ds = p->parent;
245 int ret = -EOPNOTSUPP;
246
247 if (ds->drv->port_stp_update)
248 ret = ds->drv->port_stp_update(ds, p->port, state);
249
250 return ret;
251}
252
253static int dsa_slave_bridge_port_join(struct net_device *dev,
254 struct net_device *br)
255{
256 struct dsa_slave_priv *p = netdev_priv(dev);
257 struct dsa_switch *ds = p->parent;
258 int ret = -EOPNOTSUPP;
259
260 p->bridge_dev = br;
261
262 if (ds->drv->port_join_bridge)
263 ret = ds->drv->port_join_bridge(ds, p->port,
264 dsa_slave_br_port_mask(ds, br));
265
266 return ret;
267}
268
269static int dsa_slave_bridge_port_leave(struct net_device *dev)
270{
271 struct dsa_slave_priv *p = netdev_priv(dev);
272 struct dsa_switch *ds = p->parent;
273 int ret = -EOPNOTSUPP;
274
275
276 if (ds->drv->port_leave_bridge)
277 ret = ds->drv->port_leave_bridge(ds, p->port,
278 dsa_slave_br_port_mask(ds, p->bridge_dev));
279
280 p->bridge_dev = NULL;
281
282 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
283 * so allow it to be in BR_STATE_FORWARDING to be kept functional
284 */
285 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
286
287 return ret;
288}
289
290static int dsa_slave_parent_id_get(struct net_device *dev,
291 struct netdev_phys_item_id *psid)
292{
293 struct dsa_slave_priv *p = netdev_priv(dev);
294 struct dsa_switch *ds = p->parent;
295
296 psid->id_len = sizeof(ds->index);
297 memcpy(&psid->id, &ds->index, psid->id_len);
298
299 return 0;
300}
301
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700302static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
303{
304 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700305
Alexander Duyck50753142014-09-15 13:00:19 -0400306 return p->xmit(skb, dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700307}
308
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700309static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
310 struct net_device *dev)
311{
312 struct dsa_slave_priv *p = netdev_priv(dev);
313
314 skb->dev = p->parent->dst->master_netdev;
315 dev_queue_xmit(skb);
316
317 return NETDEV_TX_OK;
318}
319
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000320
321/* ethtool operations *******************************************************/
322static int
323dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
324{
325 struct dsa_slave_priv *p = netdev_priv(dev);
326 int err;
327
328 err = -EOPNOTSUPP;
329 if (p->phy != NULL) {
330 err = phy_read_status(p->phy);
331 if (err == 0)
332 err = phy_ethtool_gset(p->phy, cmd);
333 }
334
335 return err;
336}
337
338static int
339dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
340{
341 struct dsa_slave_priv *p = netdev_priv(dev);
342
343 if (p->phy != NULL)
344 return phy_ethtool_sset(p->phy, cmd);
345
346 return -EOPNOTSUPP;
347}
348
349static void dsa_slave_get_drvinfo(struct net_device *dev,
350 struct ethtool_drvinfo *drvinfo)
351{
Jiri Pirko7826d432013-01-06 00:44:26 +0000352 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
353 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
354 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
355 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000356}
357
Guenter Roeck3d762a02014-10-29 10:45:04 -0700358static int dsa_slave_get_regs_len(struct net_device *dev)
359{
360 struct dsa_slave_priv *p = netdev_priv(dev);
361 struct dsa_switch *ds = p->parent;
362
363 if (ds->drv->get_regs_len)
364 return ds->drv->get_regs_len(ds, p->port);
365
366 return -EOPNOTSUPP;
367}
368
369static void
370dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
371{
372 struct dsa_slave_priv *p = netdev_priv(dev);
373 struct dsa_switch *ds = p->parent;
374
375 if (ds->drv->get_regs)
376 ds->drv->get_regs(ds, p->port, regs, _p);
377}
378
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000379static int dsa_slave_nway_reset(struct net_device *dev)
380{
381 struct dsa_slave_priv *p = netdev_priv(dev);
382
383 if (p->phy != NULL)
384 return genphy_restart_aneg(p->phy);
385
386 return -EOPNOTSUPP;
387}
388
389static u32 dsa_slave_get_link(struct net_device *dev)
390{
391 struct dsa_slave_priv *p = netdev_priv(dev);
392
393 if (p->phy != NULL) {
394 genphy_update_link(p->phy);
395 return p->phy->link;
396 }
397
398 return -EOPNOTSUPP;
399}
400
Guenter Roeck6793abb2014-10-29 10:45:01 -0700401static int dsa_slave_get_eeprom_len(struct net_device *dev)
402{
403 struct dsa_slave_priv *p = netdev_priv(dev);
404 struct dsa_switch *ds = p->parent;
405
406 if (ds->pd->eeprom_len)
407 return ds->pd->eeprom_len;
408
409 if (ds->drv->get_eeprom_len)
410 return ds->drv->get_eeprom_len(ds);
411
412 return 0;
413}
414
415static int dsa_slave_get_eeprom(struct net_device *dev,
416 struct ethtool_eeprom *eeprom, u8 *data)
417{
418 struct dsa_slave_priv *p = netdev_priv(dev);
419 struct dsa_switch *ds = p->parent;
420
421 if (ds->drv->get_eeprom)
422 return ds->drv->get_eeprom(ds, eeprom, data);
423
424 return -EOPNOTSUPP;
425}
426
427static int dsa_slave_set_eeprom(struct net_device *dev,
428 struct ethtool_eeprom *eeprom, u8 *data)
429{
430 struct dsa_slave_priv *p = netdev_priv(dev);
431 struct dsa_switch *ds = p->parent;
432
433 if (ds->drv->set_eeprom)
434 return ds->drv->set_eeprom(ds, eeprom, data);
435
436 return -EOPNOTSUPP;
437}
438
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000439static void dsa_slave_get_strings(struct net_device *dev,
440 uint32_t stringset, uint8_t *data)
441{
442 struct dsa_slave_priv *p = netdev_priv(dev);
443 struct dsa_switch *ds = p->parent;
444
445 if (stringset == ETH_SS_STATS) {
446 int len = ETH_GSTRING_LEN;
447
448 strncpy(data, "tx_packets", len);
449 strncpy(data + len, "tx_bytes", len);
450 strncpy(data + 2 * len, "rx_packets", len);
451 strncpy(data + 3 * len, "rx_bytes", len);
452 if (ds->drv->get_strings != NULL)
453 ds->drv->get_strings(ds, p->port, data + 4 * len);
454 }
455}
456
457static void dsa_slave_get_ethtool_stats(struct net_device *dev,
458 struct ethtool_stats *stats,
459 uint64_t *data)
460{
461 struct dsa_slave_priv *p = netdev_priv(dev);
462 struct dsa_switch *ds = p->parent;
463
464 data[0] = p->dev->stats.tx_packets;
465 data[1] = p->dev->stats.tx_bytes;
466 data[2] = p->dev->stats.rx_packets;
467 data[3] = p->dev->stats.rx_bytes;
468 if (ds->drv->get_ethtool_stats != NULL)
469 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
470}
471
472static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
473{
474 struct dsa_slave_priv *p = netdev_priv(dev);
475 struct dsa_switch *ds = p->parent;
476
477 if (sset == ETH_SS_STATS) {
478 int count;
479
480 count = 4;
481 if (ds->drv->get_sset_count != NULL)
482 count += ds->drv->get_sset_count(ds);
483
484 return count;
485 }
486
487 return -EOPNOTSUPP;
488}
489
Florian Fainelli19e57c42014-09-18 17:31:24 -0700490static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
491{
492 struct dsa_slave_priv *p = netdev_priv(dev);
493 struct dsa_switch *ds = p->parent;
494
495 if (ds->drv->get_wol)
496 ds->drv->get_wol(ds, p->port, w);
497}
498
499static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
500{
501 struct dsa_slave_priv *p = netdev_priv(dev);
502 struct dsa_switch *ds = p->parent;
503 int ret = -EOPNOTSUPP;
504
505 if (ds->drv->set_wol)
506 ret = ds->drv->set_wol(ds, p->port, w);
507
508 return ret;
509}
510
Florian Fainelli79052882014-09-24 17:05:21 -0700511static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
512{
513 struct dsa_slave_priv *p = netdev_priv(dev);
514 struct dsa_switch *ds = p->parent;
515 int ret;
516
517 if (!ds->drv->set_eee)
518 return -EOPNOTSUPP;
519
520 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
521 if (ret)
522 return ret;
523
524 if (p->phy)
525 ret = phy_ethtool_set_eee(p->phy, e);
526
527 return ret;
528}
529
530static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
531{
532 struct dsa_slave_priv *p = netdev_priv(dev);
533 struct dsa_switch *ds = p->parent;
534 int ret;
535
536 if (!ds->drv->get_eee)
537 return -EOPNOTSUPP;
538
539 ret = ds->drv->get_eee(ds, p->port, e);
540 if (ret)
541 return ret;
542
543 if (p->phy)
544 ret = phy_ethtool_get_eee(p->phy, e);
545
546 return ret;
547}
548
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000549static const struct ethtool_ops dsa_slave_ethtool_ops = {
550 .get_settings = dsa_slave_get_settings,
551 .set_settings = dsa_slave_set_settings,
552 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700553 .get_regs_len = dsa_slave_get_regs_len,
554 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000555 .nway_reset = dsa_slave_nway_reset,
556 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700557 .get_eeprom_len = dsa_slave_get_eeprom_len,
558 .get_eeprom = dsa_slave_get_eeprom,
559 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000560 .get_strings = dsa_slave_get_strings,
561 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
562 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700563 .set_wol = dsa_slave_set_wol,
564 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700565 .set_eee = dsa_slave_set_eee,
566 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000567};
568
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700569static const struct net_device_ops dsa_slave_netdev_ops = {
Lennert Buytenhekc0840802009-03-20 09:49:49 +0000570 .ndo_init = dsa_slave_init,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800571 .ndo_open = dsa_slave_open,
572 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700573 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800574 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
575 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800576 .ndo_set_mac_address = dsa_slave_set_mac_address,
577 .ndo_do_ioctl = dsa_slave_ioctl,
Florian Fainellib73adef2015-02-24 13:15:33 -0800578 .ndo_switch_parent_id_get = dsa_slave_parent_id_get,
579 .ndo_switch_port_stp_update = dsa_slave_stp_update,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800580};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000581
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700582static void dsa_slave_adjust_link(struct net_device *dev)
583{
584 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700585 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700586 unsigned int status_changed = 0;
587
588 if (p->old_link != p->phy->link) {
589 status_changed = 1;
590 p->old_link = p->phy->link;
591 }
592
593 if (p->old_duplex != p->phy->duplex) {
594 status_changed = 1;
595 p->old_duplex = p->phy->duplex;
596 }
597
598 if (p->old_pause != p->phy->pause) {
599 status_changed = 1;
600 p->old_pause = p->phy->pause;
601 }
602
Florian Fainelliec9436b2014-08-27 17:04:53 -0700603 if (ds->drv->adjust_link && status_changed)
604 ds->drv->adjust_link(ds, p->port, p->phy);
605
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700606 if (status_changed)
607 phy_print_status(p->phy);
608}
609
Florian Fainellice31b312014-08-27 17:04:54 -0700610static int dsa_slave_fixed_link_update(struct net_device *dev,
611 struct fixed_phy_status *status)
612{
613 struct dsa_slave_priv *p = netdev_priv(dev);
614 struct dsa_switch *ds = p->parent;
615
616 if (ds->drv->fixed_link_update)
617 ds->drv->fixed_link_update(ds, p->port, status);
618
619 return 0;
620}
621
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000622/* slave device setup *******************************************************/
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800623static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700624 struct net_device *slave_dev)
625{
626 struct dsa_switch *ds = p->parent;
627 struct dsa_chip_data *cd = ds->pd;
628 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -0700629 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -0700630 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -0800631 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700632
633 port_dn = cd->port_dn[p->port];
Guenter Roeck19334922015-02-16 21:23:51 -0800634 mode = of_get_phy_mode(port_dn);
635 if (mode < 0)
636 mode = PHY_INTERFACE_MODE_NA;
637 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700638
639 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
640 if (of_phy_is_fixed_link(port_dn)) {
641 /* In the case of a fixed PHY, the DT node associated
642 * to the fixed PHY is the Port DT node
643 */
644 ret = of_phy_register_fixed_link(port_dn);
645 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800646 netdev_err(slave_dev, "failed to register fixed PHY\n");
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800647 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700648 }
Florian Fainellice31b312014-08-27 17:04:54 -0700649 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700650 phy_dn = port_dn;
651 }
652
Florian Fainelli68195632014-09-19 13:07:54 -0700653 if (ds->drv->get_phy_flags)
654 phy_flags = ds->drv->get_phy_flags(ds, p->port);
655
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700656 if (phy_dn)
657 p->phy = of_phy_connect(slave_dev, phy_dn,
Florian Fainelli68195632014-09-19 13:07:54 -0700658 dsa_slave_adjust_link, phy_flags,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700659 p->phy_interface);
660
Florian Fainellice31b312014-08-27 17:04:54 -0700661 if (p->phy && phy_is_fixed)
662 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
663
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700664 /* We could not connect to a designated PHY, so use the switch internal
665 * MDIO bus instead
666 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100667 if (!p->phy) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700668 p->phy = ds->slave_mii_bus->phy_map[p->port];
Florian Fainelli53013c72014-12-11 12:49:15 -0800669 if (!p->phy)
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800670 return -ENODEV;
Florian Fainelli53013c72014-12-11 12:49:15 -0800671
Guenter Roeck19334922015-02-16 21:23:51 -0800672 /* Use already configured phy mode */
673 p->phy_interface = p->phy->interface;
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100674 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
675 p->phy_interface);
676 } else {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800677 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
678 p->phy->addr, p->phy->drv->name);
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100679 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800680
681 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700682}
683
Florian Fainelli24462542014-09-18 17:31:22 -0700684int dsa_slave_suspend(struct net_device *slave_dev)
685{
686 struct dsa_slave_priv *p = netdev_priv(slave_dev);
687
688 netif_device_detach(slave_dev);
689
690 if (p->phy) {
691 phy_stop(p->phy);
692 p->old_pause = -1;
693 p->old_link = -1;
694 p->old_duplex = -1;
695 phy_suspend(p->phy);
696 }
697
698 return 0;
699}
700
701int dsa_slave_resume(struct net_device *slave_dev)
702{
703 struct dsa_slave_priv *p = netdev_priv(slave_dev);
704
705 netif_device_attach(slave_dev);
706
707 if (p->phy) {
708 phy_resume(p->phy);
709 phy_start(p->phy);
710 }
711
712 return 0;
713}
714
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800715int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
716 int port, char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000717{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000718 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000719 struct net_device *slave_dev;
720 struct dsa_slave_priv *p;
721 int ret;
722
Tom Gundersenc835a672014-07-14 16:37:24 +0200723 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
724 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000725 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800726 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000727
728 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000729 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +0200730 eth_hw_addr_inherit(slave_dev, master);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000731 slave_dev->tx_queue_len = 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700732 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800733
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000734 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -0700735 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000736 slave_dev->vlan_features = master->vlan_features;
737
738 p = netdev_priv(slave_dev);
739 p->dev = slave_dev;
740 p->parent = ds;
741 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700742
Alexander Duyck50753142014-09-15 13:00:19 -0400743 switch (ds->dst->tag_protocol) {
744#ifdef CONFIG_NET_DSA_TAG_DSA
745 case DSA_TAG_PROTO_DSA:
746 p->xmit = dsa_netdev_ops.xmit;
747 break;
748#endif
749#ifdef CONFIG_NET_DSA_TAG_EDSA
750 case DSA_TAG_PROTO_EDSA:
751 p->xmit = edsa_netdev_ops.xmit;
752 break;
753#endif
754#ifdef CONFIG_NET_DSA_TAG_TRAILER
755 case DSA_TAG_PROTO_TRAILER:
756 p->xmit = trailer_netdev_ops.xmit;
757 break;
758#endif
759#ifdef CONFIG_NET_DSA_TAG_BRCM
760 case DSA_TAG_PROTO_BRCM:
761 p->xmit = brcm_netdev_ops.xmit;
762 break;
763#endif
764 default:
765 p->xmit = dsa_slave_notag_xmit;
766 break;
767 }
768
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700769 p->old_pause = -1;
770 p->old_link = -1;
771 p->old_duplex = -1;
772
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800773 ret = dsa_slave_phy_setup(p, slave_dev);
774 if (ret) {
775 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800776 return ret;
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800777 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000778
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800779 ds->ports[port] = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000780 ret = register_netdev(slave_dev);
781 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800782 netdev_err(master, "error %d registering interface %s\n",
783 ret, slave_dev->name);
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800784 phy_disconnect(p->phy);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800785 ds->ports[port] = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000786 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800787 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000788 }
789
790 netif_carrier_off(slave_dev);
791
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800792 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000793}
Florian Fainellib73adef2015-02-24 13:15:33 -0800794
795static bool dsa_slave_dev_check(struct net_device *dev)
796{
797 return dev->netdev_ops == &dsa_slave_netdev_ops;
798}
799
800static int dsa_slave_master_changed(struct net_device *dev)
801{
802 struct net_device *master = netdev_master_upper_dev_get(dev);
803 int err = 0;
804
805 if (master && master->rtnl_link_ops &&
806 !strcmp(master->rtnl_link_ops->kind, "bridge"))
807 err = dsa_slave_bridge_port_join(dev, master);
808 else
809 err = dsa_slave_bridge_port_leave(dev);
810
811 return err;
812}
813
814int dsa_slave_netdevice_event(struct notifier_block *unused,
815 unsigned long event, void *ptr)
816{
817 struct net_device *dev;
818 int err = 0;
819
820 switch (event) {
821 case NETDEV_CHANGEUPPER:
822 dev = netdev_notifier_info_to_dev(ptr);
823 if (!dsa_slave_dev_check(dev))
824 goto out;
825
826 err = dsa_slave_master_changed(dev);
827 if (err)
828 netdev_warn(dev, "failed to reflect master change\n");
829
830 break;
831 }
832
833out:
834 return NOTIFY_DONE;
835}