blob: ff2fbe79bc57475d89383de271db3155dc1d3075 [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>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000013#include <linux/phy.h>
Florian Fainellia2820542014-10-17 16:02:13 -070014#include <linux/phy_fixed.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070015#include <linux/of_net.h>
16#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000017#include "dsa_priv.h"
18
19/* slave mii_bus handling ***************************************************/
20static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
21{
22 struct dsa_switch *ds = bus->priv;
23
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070024 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000025 return ds->drv->phy_read(ds, addr, reg);
26
27 return 0xffff;
28}
29
30static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
31{
32 struct dsa_switch *ds = bus->priv;
33
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070034 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000035 return ds->drv->phy_write(ds, addr, reg, val);
36
37 return 0;
38}
39
40void dsa_slave_mii_bus_init(struct dsa_switch *ds)
41{
42 ds->slave_mii_bus->priv = (void *)ds;
43 ds->slave_mii_bus->name = "dsa slave smi";
44 ds->slave_mii_bus->read = dsa_slave_phy_read;
45 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainellif490be02013-01-21 09:58:50 +000046 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
47 ds->index, ds->pd->sw_addr);
Alexander Duyckb4d23942014-09-15 13:00:27 -040048 ds->slave_mii_bus->parent = ds->master_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000049}
50
51
52/* slave device handling ****************************************************/
Lennert Buytenhekc0840802009-03-20 09:49:49 +000053static int dsa_slave_init(struct net_device *dev)
54{
55 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000056
Lennert Buytenheke84665c2009-03-20 09:52:09 +000057 dev->iflink = p->parent->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000058
59 return 0;
60}
61
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000062static int dsa_slave_open(struct net_device *dev)
63{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080064 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +000065 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -070066 struct dsa_switch *ds = p->parent;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080067 int err;
68
69 if (!(master->flags & IFF_UP))
70 return -ENETDOWN;
71
Joe Perches8feedbb2012-05-08 18:56:57 +000072 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000073 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080074 if (err < 0)
75 goto out;
76 }
77
78 if (dev->flags & IFF_ALLMULTI) {
79 err = dev_set_allmulti(master, 1);
80 if (err < 0)
81 goto del_unicast;
82 }
83 if (dev->flags & IFF_PROMISC) {
84 err = dev_set_promiscuity(master, 1);
85 if (err < 0)
86 goto clear_allmulti;
87 }
88
Florian Fainellib2f2af22014-09-24 17:05:18 -070089 if (ds->drv->port_enable) {
90 err = ds->drv->port_enable(ds, p->port, p->phy);
91 if (err)
92 goto clear_promisc;
93 }
94
Florian Fainellif7f1de52014-09-24 17:05:17 -070095 if (p->phy)
96 phy_start(p->phy);
97
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000098 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080099
Florian Fainellib2f2af22014-09-24 17:05:18 -0700100clear_promisc:
101 if (dev->flags & IFF_PROMISC)
102 dev_set_promiscuity(master, 0);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800103clear_allmulti:
104 if (dev->flags & IFF_ALLMULTI)
105 dev_set_allmulti(master, -1);
106del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000107 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000108 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800109out:
110 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000111}
112
113static int dsa_slave_close(struct net_device *dev)
114{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800115 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000116 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -0700117 struct dsa_switch *ds = p->parent;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800118
Florian Fainellif7f1de52014-09-24 17:05:17 -0700119 if (p->phy)
120 phy_stop(p->phy);
121
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800122 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000123 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800124 if (dev->flags & IFF_ALLMULTI)
125 dev_set_allmulti(master, -1);
126 if (dev->flags & IFF_PROMISC)
127 dev_set_promiscuity(master, -1);
128
Joe Perches8feedbb2012-05-08 18:56:57 +0000129 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000130 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800131
Florian Fainellib2f2af22014-09-24 17:05:18 -0700132 if (ds->drv->port_disable)
133 ds->drv->port_disable(ds, p->port, p->phy);
134
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000135 return 0;
136}
137
138static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
139{
140 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000141 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000142
143 if (change & IFF_ALLMULTI)
144 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
145 if (change & IFF_PROMISC)
146 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
147}
148
149static void dsa_slave_set_rx_mode(struct net_device *dev)
150{
151 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000152 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000153
154 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000155 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000156}
157
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800158static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000159{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800160 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000161 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800162 struct sockaddr *addr = a;
163 int err;
164
165 if (!is_valid_ether_addr(addr->sa_data))
166 return -EADDRNOTAVAIL;
167
168 if (!(dev->flags & IFF_UP))
169 goto out;
170
Joe Perches8feedbb2012-05-08 18:56:57 +0000171 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000172 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800173 if (err < 0)
174 return err;
175 }
176
Joe Perches8feedbb2012-05-08 18:56:57 +0000177 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000178 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800179
180out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800181 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000182
183 return 0;
184}
185
186static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
187{
188 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000189
190 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000191 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000192
193 return -EOPNOTSUPP;
194}
195
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700196static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
197{
198 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700199
Alexander Duyck50753142014-09-15 13:00:19 -0400200 return p->xmit(skb, dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700201}
202
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700203static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
204 struct net_device *dev)
205{
206 struct dsa_slave_priv *p = netdev_priv(dev);
207
208 skb->dev = p->parent->dst->master_netdev;
209 dev_queue_xmit(skb);
210
211 return NETDEV_TX_OK;
212}
213
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000214
215/* ethtool operations *******************************************************/
216static int
217dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
218{
219 struct dsa_slave_priv *p = netdev_priv(dev);
220 int err;
221
222 err = -EOPNOTSUPP;
223 if (p->phy != NULL) {
224 err = phy_read_status(p->phy);
225 if (err == 0)
226 err = phy_ethtool_gset(p->phy, cmd);
227 }
228
229 return err;
230}
231
232static int
233dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
234{
235 struct dsa_slave_priv *p = netdev_priv(dev);
236
237 if (p->phy != NULL)
238 return phy_ethtool_sset(p->phy, cmd);
239
240 return -EOPNOTSUPP;
241}
242
243static void dsa_slave_get_drvinfo(struct net_device *dev,
244 struct ethtool_drvinfo *drvinfo)
245{
Jiri Pirko7826d432013-01-06 00:44:26 +0000246 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
247 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
248 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
249 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000250}
251
252static int dsa_slave_nway_reset(struct net_device *dev)
253{
254 struct dsa_slave_priv *p = netdev_priv(dev);
255
256 if (p->phy != NULL)
257 return genphy_restart_aneg(p->phy);
258
259 return -EOPNOTSUPP;
260}
261
262static u32 dsa_slave_get_link(struct net_device *dev)
263{
264 struct dsa_slave_priv *p = netdev_priv(dev);
265
266 if (p->phy != NULL) {
267 genphy_update_link(p->phy);
268 return p->phy->link;
269 }
270
271 return -EOPNOTSUPP;
272}
273
Guenter Roeck6793abb2014-10-29 10:45:01 -0700274static int dsa_slave_get_eeprom_len(struct net_device *dev)
275{
276 struct dsa_slave_priv *p = netdev_priv(dev);
277 struct dsa_switch *ds = p->parent;
278
279 if (ds->pd->eeprom_len)
280 return ds->pd->eeprom_len;
281
282 if (ds->drv->get_eeprom_len)
283 return ds->drv->get_eeprom_len(ds);
284
285 return 0;
286}
287
288static int dsa_slave_get_eeprom(struct net_device *dev,
289 struct ethtool_eeprom *eeprom, u8 *data)
290{
291 struct dsa_slave_priv *p = netdev_priv(dev);
292 struct dsa_switch *ds = p->parent;
293
294 if (ds->drv->get_eeprom)
295 return ds->drv->get_eeprom(ds, eeprom, data);
296
297 return -EOPNOTSUPP;
298}
299
300static int dsa_slave_set_eeprom(struct net_device *dev,
301 struct ethtool_eeprom *eeprom, u8 *data)
302{
303 struct dsa_slave_priv *p = netdev_priv(dev);
304 struct dsa_switch *ds = p->parent;
305
306 if (ds->drv->set_eeprom)
307 return ds->drv->set_eeprom(ds, eeprom, data);
308
309 return -EOPNOTSUPP;
310}
311
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000312static void dsa_slave_get_strings(struct net_device *dev,
313 uint32_t stringset, uint8_t *data)
314{
315 struct dsa_slave_priv *p = netdev_priv(dev);
316 struct dsa_switch *ds = p->parent;
317
318 if (stringset == ETH_SS_STATS) {
319 int len = ETH_GSTRING_LEN;
320
321 strncpy(data, "tx_packets", len);
322 strncpy(data + len, "tx_bytes", len);
323 strncpy(data + 2 * len, "rx_packets", len);
324 strncpy(data + 3 * len, "rx_bytes", len);
325 if (ds->drv->get_strings != NULL)
326 ds->drv->get_strings(ds, p->port, data + 4 * len);
327 }
328}
329
330static void dsa_slave_get_ethtool_stats(struct net_device *dev,
331 struct ethtool_stats *stats,
332 uint64_t *data)
333{
334 struct dsa_slave_priv *p = netdev_priv(dev);
335 struct dsa_switch *ds = p->parent;
336
337 data[0] = p->dev->stats.tx_packets;
338 data[1] = p->dev->stats.tx_bytes;
339 data[2] = p->dev->stats.rx_packets;
340 data[3] = p->dev->stats.rx_bytes;
341 if (ds->drv->get_ethtool_stats != NULL)
342 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
343}
344
345static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
346{
347 struct dsa_slave_priv *p = netdev_priv(dev);
348 struct dsa_switch *ds = p->parent;
349
350 if (sset == ETH_SS_STATS) {
351 int count;
352
353 count = 4;
354 if (ds->drv->get_sset_count != NULL)
355 count += ds->drv->get_sset_count(ds);
356
357 return count;
358 }
359
360 return -EOPNOTSUPP;
361}
362
Florian Fainelli19e57c42014-09-18 17:31:24 -0700363static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
364{
365 struct dsa_slave_priv *p = netdev_priv(dev);
366 struct dsa_switch *ds = p->parent;
367
368 if (ds->drv->get_wol)
369 ds->drv->get_wol(ds, p->port, w);
370}
371
372static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
373{
374 struct dsa_slave_priv *p = netdev_priv(dev);
375 struct dsa_switch *ds = p->parent;
376 int ret = -EOPNOTSUPP;
377
378 if (ds->drv->set_wol)
379 ret = ds->drv->set_wol(ds, p->port, w);
380
381 return ret;
382}
383
Florian Fainelli79052882014-09-24 17:05:21 -0700384static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
385{
386 struct dsa_slave_priv *p = netdev_priv(dev);
387 struct dsa_switch *ds = p->parent;
388 int ret;
389
390 if (!ds->drv->set_eee)
391 return -EOPNOTSUPP;
392
393 ret = ds->drv->set_eee(ds, p->port, p->phy, e);
394 if (ret)
395 return ret;
396
397 if (p->phy)
398 ret = phy_ethtool_set_eee(p->phy, e);
399
400 return ret;
401}
402
403static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
404{
405 struct dsa_slave_priv *p = netdev_priv(dev);
406 struct dsa_switch *ds = p->parent;
407 int ret;
408
409 if (!ds->drv->get_eee)
410 return -EOPNOTSUPP;
411
412 ret = ds->drv->get_eee(ds, p->port, e);
413 if (ret)
414 return ret;
415
416 if (p->phy)
417 ret = phy_ethtool_get_eee(p->phy, e);
418
419 return ret;
420}
421
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000422static const struct ethtool_ops dsa_slave_ethtool_ops = {
423 .get_settings = dsa_slave_get_settings,
424 .set_settings = dsa_slave_set_settings,
425 .get_drvinfo = dsa_slave_get_drvinfo,
426 .nway_reset = dsa_slave_nway_reset,
427 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700428 .get_eeprom_len = dsa_slave_get_eeprom_len,
429 .get_eeprom = dsa_slave_get_eeprom,
430 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000431 .get_strings = dsa_slave_get_strings,
432 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
433 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700434 .set_wol = dsa_slave_set_wol,
435 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700436 .set_eee = dsa_slave_set_eee,
437 .get_eee = dsa_slave_get_eee,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000438};
439
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700440static const struct net_device_ops dsa_slave_netdev_ops = {
Lennert Buytenhekc0840802009-03-20 09:49:49 +0000441 .ndo_init = dsa_slave_init,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800442 .ndo_open = dsa_slave_open,
443 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700444 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800445 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
446 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800447 .ndo_set_mac_address = dsa_slave_set_mac_address,
448 .ndo_do_ioctl = dsa_slave_ioctl,
449};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000450
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700451static void dsa_slave_adjust_link(struct net_device *dev)
452{
453 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700454 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700455 unsigned int status_changed = 0;
456
457 if (p->old_link != p->phy->link) {
458 status_changed = 1;
459 p->old_link = p->phy->link;
460 }
461
462 if (p->old_duplex != p->phy->duplex) {
463 status_changed = 1;
464 p->old_duplex = p->phy->duplex;
465 }
466
467 if (p->old_pause != p->phy->pause) {
468 status_changed = 1;
469 p->old_pause = p->phy->pause;
470 }
471
Florian Fainelliec9436b2014-08-27 17:04:53 -0700472 if (ds->drv->adjust_link && status_changed)
473 ds->drv->adjust_link(ds, p->port, p->phy);
474
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700475 if (status_changed)
476 phy_print_status(p->phy);
477}
478
Florian Fainellice31b312014-08-27 17:04:54 -0700479static int dsa_slave_fixed_link_update(struct net_device *dev,
480 struct fixed_phy_status *status)
481{
482 struct dsa_slave_priv *p = netdev_priv(dev);
483 struct dsa_switch *ds = p->parent;
484
485 if (ds->drv->fixed_link_update)
486 ds->drv->fixed_link_update(ds, p->port, status);
487
488 return 0;
489}
490
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000491/* slave device setup *******************************************************/
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700492static void dsa_slave_phy_setup(struct dsa_slave_priv *p,
493 struct net_device *slave_dev)
494{
495 struct dsa_switch *ds = p->parent;
496 struct dsa_chip_data *cd = ds->pd;
497 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -0700498 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -0700499 u32 phy_flags = 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700500 int ret;
501
502 port_dn = cd->port_dn[p->port];
503 p->phy_interface = of_get_phy_mode(port_dn);
504
505 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
506 if (of_phy_is_fixed_link(port_dn)) {
507 /* In the case of a fixed PHY, the DT node associated
508 * to the fixed PHY is the Port DT node
509 */
510 ret = of_phy_register_fixed_link(port_dn);
511 if (ret) {
512 pr_err("failed to register fixed PHY\n");
513 return;
514 }
Florian Fainellice31b312014-08-27 17:04:54 -0700515 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700516 phy_dn = port_dn;
517 }
518
Florian Fainelli68195632014-09-19 13:07:54 -0700519 if (ds->drv->get_phy_flags)
520 phy_flags = ds->drv->get_phy_flags(ds, p->port);
521
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700522 if (phy_dn)
523 p->phy = of_phy_connect(slave_dev, phy_dn,
Florian Fainelli68195632014-09-19 13:07:54 -0700524 dsa_slave_adjust_link, phy_flags,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700525 p->phy_interface);
526
Florian Fainellice31b312014-08-27 17:04:54 -0700527 if (p->phy && phy_is_fixed)
528 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
529
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700530 /* We could not connect to a designated PHY, so use the switch internal
531 * MDIO bus instead
532 */
533 if (!p->phy)
534 p->phy = ds->slave_mii_bus->phy_map[p->port];
535 else
536 pr_info("attached PHY at address %d [%s]\n",
537 p->phy->addr, p->phy->drv->name);
538}
539
Florian Fainelli24462542014-09-18 17:31:22 -0700540int dsa_slave_suspend(struct net_device *slave_dev)
541{
542 struct dsa_slave_priv *p = netdev_priv(slave_dev);
543
544 netif_device_detach(slave_dev);
545
546 if (p->phy) {
547 phy_stop(p->phy);
548 p->old_pause = -1;
549 p->old_link = -1;
550 p->old_duplex = -1;
551 phy_suspend(p->phy);
552 }
553
554 return 0;
555}
556
557int dsa_slave_resume(struct net_device *slave_dev)
558{
559 struct dsa_slave_priv *p = netdev_priv(slave_dev);
560
561 netif_device_attach(slave_dev);
562
563 if (p->phy) {
564 phy_resume(p->phy);
565 phy_start(p->phy);
566 }
567
568 return 0;
569}
570
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000571struct net_device *
572dsa_slave_create(struct dsa_switch *ds, struct device *parent,
573 int port, char *name)
574{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000575 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000576 struct net_device *slave_dev;
577 struct dsa_slave_priv *p;
578 int ret;
579
Tom Gundersenc835a672014-07-14 16:37:24 +0200580 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
581 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000582 if (slave_dev == NULL)
583 return slave_dev;
584
585 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000586 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +0200587 eth_hw_addr_inherit(slave_dev, master);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000588 slave_dev->tx_queue_len = 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700589 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800590
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000591 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -0700592 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000593 slave_dev->vlan_features = master->vlan_features;
594
595 p = netdev_priv(slave_dev);
596 p->dev = slave_dev;
597 p->parent = ds;
598 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700599
Alexander Duyck50753142014-09-15 13:00:19 -0400600 switch (ds->dst->tag_protocol) {
601#ifdef CONFIG_NET_DSA_TAG_DSA
602 case DSA_TAG_PROTO_DSA:
603 p->xmit = dsa_netdev_ops.xmit;
604 break;
605#endif
606#ifdef CONFIG_NET_DSA_TAG_EDSA
607 case DSA_TAG_PROTO_EDSA:
608 p->xmit = edsa_netdev_ops.xmit;
609 break;
610#endif
611#ifdef CONFIG_NET_DSA_TAG_TRAILER
612 case DSA_TAG_PROTO_TRAILER:
613 p->xmit = trailer_netdev_ops.xmit;
614 break;
615#endif
616#ifdef CONFIG_NET_DSA_TAG_BRCM
617 case DSA_TAG_PROTO_BRCM:
618 p->xmit = brcm_netdev_ops.xmit;
619 break;
620#endif
621 default:
622 p->xmit = dsa_slave_notag_xmit;
623 break;
624 }
625
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700626 p->old_pause = -1;
627 p->old_link = -1;
628 p->old_duplex = -1;
629
630 dsa_slave_phy_setup(p, slave_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000631
632 ret = register_netdev(slave_dev);
633 if (ret) {
634 printk(KERN_ERR "%s: error %d registering interface %s\n",
635 master->name, ret, slave_dev->name);
636 free_netdev(slave_dev);
637 return NULL;
638 }
639
640 netif_carrier_off(slave_dev);
641
642 if (p->phy != NULL) {
Andrew Lunn228b16c2014-10-19 16:41:47 +0200643 if (ds->drv->get_phy_flags)
Florian Fainelli68195632014-09-19 13:07:54 -0700644 p->phy->dev_flags |= ds->drv->get_phy_flags(ds, port);
645
Kay Sieversfb28ad32008-11-10 13:55:14 -0800646 phy_attach(slave_dev, dev_name(&p->phy->dev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000647 PHY_INTERFACE_MODE_GMII);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000648
649 p->phy->autoneg = AUTONEG_ENABLE;
650 p->phy->speed = 0;
651 p->phy->duplex = 0;
652 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000653 }
654
655 return slave_dev;
656}