blob: 6511552039d6dde3e7343ff3a3f811cd2246c5ae [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++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800225 if (!dsa_is_port_initialized(ds, port))
Florian Fainellib73adef2015-02-24 13:15:33 -0800226 continue;
227
228 p = netdev_priv(ds->ports[port]);
229
230 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
231 p->bridge_dev == bridge)
232 mask |= 1 << port;
233 }
234
235 return mask;
236}
237
238static int dsa_slave_stp_update(struct net_device *dev, u8 state)
239{
240 struct dsa_slave_priv *p = netdev_priv(dev);
241 struct dsa_switch *ds = p->parent;
242 int ret = -EOPNOTSUPP;
243
244 if (ds->drv->port_stp_update)
245 ret = ds->drv->port_stp_update(ds, p->port, state);
246
247 return ret;
248}
249
250static int dsa_slave_bridge_port_join(struct net_device *dev,
251 struct net_device *br)
252{
253 struct dsa_slave_priv *p = netdev_priv(dev);
254 struct dsa_switch *ds = p->parent;
255 int ret = -EOPNOTSUPP;
256
257 p->bridge_dev = br;
258
259 if (ds->drv->port_join_bridge)
260 ret = ds->drv->port_join_bridge(ds, p->port,
261 dsa_slave_br_port_mask(ds, br));
262
263 return ret;
264}
265
266static int dsa_slave_bridge_port_leave(struct net_device *dev)
267{
268 struct dsa_slave_priv *p = netdev_priv(dev);
269 struct dsa_switch *ds = p->parent;
270 int ret = -EOPNOTSUPP;
271
272
273 if (ds->drv->port_leave_bridge)
274 ret = ds->drv->port_leave_bridge(ds, p->port,
275 dsa_slave_br_port_mask(ds, p->bridge_dev));
276
277 p->bridge_dev = NULL;
278
279 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
280 * so allow it to be in BR_STATE_FORWARDING to be kept functional
281 */
282 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
283
284 return ret;
285}
286
287static int dsa_slave_parent_id_get(struct net_device *dev,
288 struct netdev_phys_item_id *psid)
289{
290 struct dsa_slave_priv *p = netdev_priv(dev);
291 struct dsa_switch *ds = p->parent;
292
293 psid->id_len = sizeof(ds->index);
294 memcpy(&psid->id, &ds->index, psid->id_len);
295
296 return 0;
297}
298
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700299static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
300{
301 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700302
Alexander Duyck50753142014-09-15 13:00:19 -0400303 return p->xmit(skb, dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700304}
305
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700306static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
307 struct net_device *dev)
308{
309 struct dsa_slave_priv *p = netdev_priv(dev);
310
311 skb->dev = p->parent->dst->master_netdev;
312 dev_queue_xmit(skb);
313
314 return NETDEV_TX_OK;
315}
316
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000317
318/* ethtool operations *******************************************************/
319static int
320dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
321{
322 struct dsa_slave_priv *p = netdev_priv(dev);
323 int err;
324
325 err = -EOPNOTSUPP;
326 if (p->phy != NULL) {
327 err = phy_read_status(p->phy);
328 if (err == 0)
329 err = phy_ethtool_gset(p->phy, cmd);
330 }
331
332 return err;
333}
334
335static int
336dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
337{
338 struct dsa_slave_priv *p = netdev_priv(dev);
339
340 if (p->phy != NULL)
341 return phy_ethtool_sset(p->phy, cmd);
342
343 return -EOPNOTSUPP;
344}
345
346static void dsa_slave_get_drvinfo(struct net_device *dev,
347 struct ethtool_drvinfo *drvinfo)
348{
Jiri Pirko7826d432013-01-06 00:44:26 +0000349 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
350 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
351 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
352 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000353}
354
Guenter Roeck3d762a02014-10-29 10:45:04 -0700355static int dsa_slave_get_regs_len(struct net_device *dev)
356{
357 struct dsa_slave_priv *p = netdev_priv(dev);
358 struct dsa_switch *ds = p->parent;
359
360 if (ds->drv->get_regs_len)
361 return ds->drv->get_regs_len(ds, p->port);
362
363 return -EOPNOTSUPP;
364}
365
366static void
367dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
368{
369 struct dsa_slave_priv *p = netdev_priv(dev);
370 struct dsa_switch *ds = p->parent;
371
372 if (ds->drv->get_regs)
373 ds->drv->get_regs(ds, p->port, regs, _p);
374}
375
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000376static int dsa_slave_nway_reset(struct net_device *dev)
377{
378 struct dsa_slave_priv *p = netdev_priv(dev);
379
380 if (p->phy != NULL)
381 return genphy_restart_aneg(p->phy);
382
383 return -EOPNOTSUPP;
384}
385
386static u32 dsa_slave_get_link(struct net_device *dev)
387{
388 struct dsa_slave_priv *p = netdev_priv(dev);
389
390 if (p->phy != NULL) {
391 genphy_update_link(p->phy);
392 return p->phy->link;
393 }
394
395 return -EOPNOTSUPP;
396}
397
Guenter Roeck6793abb2014-10-29 10:45:01 -0700398static int dsa_slave_get_eeprom_len(struct net_device *dev)
399{
400 struct dsa_slave_priv *p = netdev_priv(dev);
401 struct dsa_switch *ds = p->parent;
402
403 if (ds->pd->eeprom_len)
404 return ds->pd->eeprom_len;
405
406 if (ds->drv->get_eeprom_len)
407 return ds->drv->get_eeprom_len(ds);
408
409 return 0;
410}
411
412static int dsa_slave_get_eeprom(struct net_device *dev,
413 struct ethtool_eeprom *eeprom, u8 *data)
414{
415 struct dsa_slave_priv *p = netdev_priv(dev);
416 struct dsa_switch *ds = p->parent;
417
418 if (ds->drv->get_eeprom)
419 return ds->drv->get_eeprom(ds, eeprom, data);
420
421 return -EOPNOTSUPP;
422}
423
424static int dsa_slave_set_eeprom(struct net_device *dev,
425 struct ethtool_eeprom *eeprom, u8 *data)
426{
427 struct dsa_slave_priv *p = netdev_priv(dev);
428 struct dsa_switch *ds = p->parent;
429
430 if (ds->drv->set_eeprom)
431 return ds->drv->set_eeprom(ds, eeprom, data);
432
433 return -EOPNOTSUPP;
434}
435
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000436static void dsa_slave_get_strings(struct net_device *dev,
437 uint32_t stringset, uint8_t *data)
438{
439 struct dsa_slave_priv *p = netdev_priv(dev);
440 struct dsa_switch *ds = p->parent;
441
442 if (stringset == ETH_SS_STATS) {
443 int len = ETH_GSTRING_LEN;
444
445 strncpy(data, "tx_packets", len);
446 strncpy(data + len, "tx_bytes", len);
447 strncpy(data + 2 * len, "rx_packets", len);
448 strncpy(data + 3 * len, "rx_bytes", len);
449 if (ds->drv->get_strings != NULL)
450 ds->drv->get_strings(ds, p->port, data + 4 * len);
451 }
452}
453
454static void dsa_slave_get_ethtool_stats(struct net_device *dev,
455 struct ethtool_stats *stats,
456 uint64_t *data)
457{
458 struct dsa_slave_priv *p = netdev_priv(dev);
459 struct dsa_switch *ds = p->parent;
460
461 data[0] = p->dev->stats.tx_packets;
462 data[1] = p->dev->stats.tx_bytes;
463 data[2] = p->dev->stats.rx_packets;
464 data[3] = p->dev->stats.rx_bytes;
465 if (ds->drv->get_ethtool_stats != NULL)
466 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
467}
468
469static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
470{
471 struct dsa_slave_priv *p = netdev_priv(dev);
472 struct dsa_switch *ds = p->parent;
473
474 if (sset == ETH_SS_STATS) {
475 int count;
476
477 count = 4;
478 if (ds->drv->get_sset_count != NULL)
479 count += ds->drv->get_sset_count(ds);
480
481 return count;
482 }
483
484 return -EOPNOTSUPP;
485}
486
Florian Fainelli19e57c42014-09-18 17:31:24 -0700487static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
488{
489 struct dsa_slave_priv *p = netdev_priv(dev);
490 struct dsa_switch *ds = p->parent;
491
492 if (ds->drv->get_wol)
493 ds->drv->get_wol(ds, p->port, w);
494}
495
496static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
497{
498 struct dsa_slave_priv *p = netdev_priv(dev);
499 struct dsa_switch *ds = p->parent;
500 int ret = -EOPNOTSUPP;
501
502 if (ds->drv->set_wol)
503 ret = ds->drv->set_wol(ds, p->port, w);
504
505 return ret;
506}
507
Florian Fainelli79052882014-09-24 17:05:21 -0700508static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
509{
510 struct dsa_slave_priv *p = netdev_priv(dev);
511 struct dsa_switch *ds = p->parent;
512 int ret;
513
514 if (!ds->drv->set_eee)
515 return -EOPNOTSUPP;
516
517 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
518 if (ret)
519 return ret;
520
521 if (p->phy)
522 ret = phy_ethtool_set_eee(p->phy, e);
523
524 return ret;
525}
526
527static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
528{
529 struct dsa_slave_priv *p = netdev_priv(dev);
530 struct dsa_switch *ds = p->parent;
531 int ret;
532
533 if (!ds->drv->get_eee)
534 return -EOPNOTSUPP;
535
536 ret = ds->drv->get_eee(ds, p->port, e);
537 if (ret)
538 return ret;
539
540 if (p->phy)
541 ret = phy_ethtool_get_eee(p->phy, e);
542
543 return ret;
544}
545
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000546static const struct ethtool_ops dsa_slave_ethtool_ops = {
547 .get_settings = dsa_slave_get_settings,
548 .set_settings = dsa_slave_set_settings,
549 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700550 .get_regs_len = dsa_slave_get_regs_len,
551 .get_regs = dsa_slave_get_regs,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000552 .nway_reset = dsa_slave_nway_reset,
553 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700554 .get_eeprom_len = dsa_slave_get_eeprom_len,
555 .get_eeprom = dsa_slave_get_eeprom,
556 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000557 .get_strings = dsa_slave_get_strings,
558 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
559 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700560 .set_wol = dsa_slave_set_wol,
561 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700562 .set_eee = dsa_slave_set_eee,
563 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000564};
565
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700566static const struct net_device_ops dsa_slave_netdev_ops = {
Lennert Buytenhekc0840802009-03-20 09:49:49 +0000567 .ndo_init = dsa_slave_init,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800568 .ndo_open = dsa_slave_open,
569 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700570 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800571 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
572 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800573 .ndo_set_mac_address = dsa_slave_set_mac_address,
574 .ndo_do_ioctl = dsa_slave_ioctl,
Florian Fainellib73adef2015-02-24 13:15:33 -0800575 .ndo_switch_parent_id_get = dsa_slave_parent_id_get,
576 .ndo_switch_port_stp_update = dsa_slave_stp_update,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800577};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000578
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700579static void dsa_slave_adjust_link(struct net_device *dev)
580{
581 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700582 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700583 unsigned int status_changed = 0;
584
585 if (p->old_link != p->phy->link) {
586 status_changed = 1;
587 p->old_link = p->phy->link;
588 }
589
590 if (p->old_duplex != p->phy->duplex) {
591 status_changed = 1;
592 p->old_duplex = p->phy->duplex;
593 }
594
595 if (p->old_pause != p->phy->pause) {
596 status_changed = 1;
597 p->old_pause = p->phy->pause;
598 }
599
Florian Fainelliec9436b2014-08-27 17:04:53 -0700600 if (ds->drv->adjust_link && status_changed)
601 ds->drv->adjust_link(ds, p->port, p->phy);
602
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700603 if (status_changed)
604 phy_print_status(p->phy);
605}
606
Florian Fainellice31b312014-08-27 17:04:54 -0700607static int dsa_slave_fixed_link_update(struct net_device *dev,
608 struct fixed_phy_status *status)
609{
610 struct dsa_slave_priv *p = netdev_priv(dev);
611 struct dsa_switch *ds = p->parent;
612
613 if (ds->drv->fixed_link_update)
614 ds->drv->fixed_link_update(ds, p->port, status);
615
616 return 0;
617}
618
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000619/* slave device setup *******************************************************/
Florian Fainellic305c162015-03-10 16:57:12 -0700620static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700621 struct net_device *slave_dev,
622 int addr)
Florian Fainellic305c162015-03-10 16:57:12 -0700623{
624 struct dsa_switch *ds = p->parent;
625
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700626 p->phy = ds->slave_mii_bus->phy_map[addr];
Florian Fainellic305c162015-03-10 16:57:12 -0700627 if (!p->phy)
628 return -ENODEV;
629
630 /* Use already configured phy mode */
631 p->phy_interface = p->phy->interface;
632 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
633 p->phy_interface);
634
635 return 0;
636}
637
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800638static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700639 struct net_device *slave_dev)
640{
641 struct dsa_switch *ds = p->parent;
642 struct dsa_chip_data *cd = ds->pd;
643 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -0700644 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -0700645 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -0800646 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700647
648 port_dn = cd->port_dn[p->port];
Guenter Roeck19334922015-02-16 21:23:51 -0800649 mode = of_get_phy_mode(port_dn);
650 if (mode < 0)
651 mode = PHY_INTERFACE_MODE_NA;
652 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700653
654 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
655 if (of_phy_is_fixed_link(port_dn)) {
656 /* In the case of a fixed PHY, the DT node associated
657 * to the fixed PHY is the Port DT node
658 */
659 ret = of_phy_register_fixed_link(port_dn);
660 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800661 netdev_err(slave_dev, "failed to register fixed PHY\n");
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800662 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700663 }
Florian Fainellice31b312014-08-27 17:04:54 -0700664 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700665 phy_dn = port_dn;
666 }
667
Florian Fainelli68195632014-09-19 13:07:54 -0700668 if (ds->drv->get_phy_flags)
669 phy_flags = ds->drv->get_phy_flags(ds, p->port);
670
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700671 if (phy_dn) {
672 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
673 /* If this PHY address is part of phys_mii_mask, which means
674 * that we need to divert reads and writes to/from it, then we
675 * want to bind this device using the slave MII bus created by
676 * DSA to make that happen.
677 */
Florian Fainelli96026d02015-03-14 13:21:59 -0700678 if (!phy_is_fixed && ret >= 0 &&
679 (ds->phys_mii_mask & (1 << ret))) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700680 ret = dsa_slave_phy_connect(p, slave_dev, ret);
681 if (ret)
682 return ret;
683 } else {
684 p->phy = of_phy_connect(slave_dev, phy_dn,
685 dsa_slave_adjust_link,
686 phy_flags,
687 p->phy_interface);
688 }
689 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700690
Florian Fainellice31b312014-08-27 17:04:54 -0700691 if (p->phy && phy_is_fixed)
692 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
693
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700694 /* We could not connect to a designated PHY, so use the switch internal
695 * MDIO bus instead
696 */
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100697 if (!p->phy) {
Florian Fainellicd28a1a2015-03-10 16:57:13 -0700698 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
Florian Fainellic305c162015-03-10 16:57:12 -0700699 if (ret)
700 return ret;
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100701 } else {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800702 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
703 p->phy->addr, p->phy->drv->name);
Andrew Lunnb31f65f2014-11-05 19:47:28 +0100704 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800705
706 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700707}
708
Florian Fainelli24462542014-09-18 17:31:22 -0700709int dsa_slave_suspend(struct net_device *slave_dev)
710{
711 struct dsa_slave_priv *p = netdev_priv(slave_dev);
712
713 netif_device_detach(slave_dev);
714
715 if (p->phy) {
716 phy_stop(p->phy);
717 p->old_pause = -1;
718 p->old_link = -1;
719 p->old_duplex = -1;
720 phy_suspend(p->phy);
721 }
722
723 return 0;
724}
725
726int dsa_slave_resume(struct net_device *slave_dev)
727{
728 struct dsa_slave_priv *p = netdev_priv(slave_dev);
729
730 netif_device_attach(slave_dev);
731
732 if (p->phy) {
733 phy_resume(p->phy);
734 phy_start(p->phy);
735 }
736
737 return 0;
738}
739
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800740int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
741 int port, char *name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000742{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000743 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000744 struct net_device *slave_dev;
745 struct dsa_slave_priv *p;
746 int ret;
747
Tom Gundersenc835a672014-07-14 16:37:24 +0200748 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
749 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000750 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800751 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000752
753 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000754 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +0200755 eth_hw_addr_inherit(slave_dev, master);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000756 slave_dev->tx_queue_len = 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700757 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800758
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000759 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -0700760 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000761 slave_dev->vlan_features = master->vlan_features;
762
763 p = netdev_priv(slave_dev);
764 p->dev = slave_dev;
765 p->parent = ds;
766 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700767
Alexander Duyck50753142014-09-15 13:00:19 -0400768 switch (ds->dst->tag_protocol) {
769#ifdef CONFIG_NET_DSA_TAG_DSA
770 case DSA_TAG_PROTO_DSA:
771 p->xmit = dsa_netdev_ops.xmit;
772 break;
773#endif
774#ifdef CONFIG_NET_DSA_TAG_EDSA
775 case DSA_TAG_PROTO_EDSA:
776 p->xmit = edsa_netdev_ops.xmit;
777 break;
778#endif
779#ifdef CONFIG_NET_DSA_TAG_TRAILER
780 case DSA_TAG_PROTO_TRAILER:
781 p->xmit = trailer_netdev_ops.xmit;
782 break;
783#endif
784#ifdef CONFIG_NET_DSA_TAG_BRCM
785 case DSA_TAG_PROTO_BRCM:
786 p->xmit = brcm_netdev_ops.xmit;
787 break;
788#endif
789 default:
790 p->xmit = dsa_slave_notag_xmit;
791 break;
792 }
793
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700794 p->old_pause = -1;
795 p->old_link = -1;
796 p->old_duplex = -1;
797
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800798 ret = dsa_slave_phy_setup(p, slave_dev);
799 if (ret) {
800 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800801 return ret;
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800802 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000803
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800804 ds->ports[port] = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000805 ret = register_netdev(slave_dev);
806 if (ret) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800807 netdev_err(master, "error %d registering interface %s\n",
808 ret, slave_dev->name);
Florian Fainelli9697f1c2014-12-11 12:49:16 -0800809 phy_disconnect(p->phy);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800810 ds->ports[port] = NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000811 free_netdev(slave_dev);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800812 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000813 }
814
815 netif_carrier_off(slave_dev);
816
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800817 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000818}
Florian Fainellib73adef2015-02-24 13:15:33 -0800819
820static bool dsa_slave_dev_check(struct net_device *dev)
821{
822 return dev->netdev_ops == &dsa_slave_netdev_ops;
823}
824
825static int dsa_slave_master_changed(struct net_device *dev)
826{
827 struct net_device *master = netdev_master_upper_dev_get(dev);
828 int err = 0;
829
830 if (master && master->rtnl_link_ops &&
831 !strcmp(master->rtnl_link_ops->kind, "bridge"))
832 err = dsa_slave_bridge_port_join(dev, master);
833 else
834 err = dsa_slave_bridge_port_leave(dev);
835
836 return err;
837}
838
839int dsa_slave_netdevice_event(struct notifier_block *unused,
840 unsigned long event, void *ptr)
841{
842 struct net_device *dev;
843 int err = 0;
844
845 switch (event) {
846 case NETDEV_CHANGEUPPER:
847 dev = netdev_notifier_info_to_dev(ptr);
848 if (!dsa_slave_dev_check(dev))
849 goto out;
850
851 err = dsa_slave_master_changed(dev);
852 if (err)
853 netdev_warn(dev, "failed to reflect master change\n");
854
855 break;
856 }
857
858out:
859 return NOTIFY_DONE;
860}