blob: 182d30ae6818cfc392ab781aaa00011f1732d761 [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 Fainelli0d8bcdd2014-08-27 17:04:51 -070014#include <linux/of_net.h>
15#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000016#include "dsa_priv.h"
17
18/* slave mii_bus handling ***************************************************/
19static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
20{
21 struct dsa_switch *ds = bus->priv;
22
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070023 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000024 return ds->drv->phy_read(ds, addr, reg);
25
26 return 0xffff;
27}
28
29static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
30{
31 struct dsa_switch *ds = bus->priv;
32
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070033 if (ds->phys_mii_mask & (1 << addr))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000034 return ds->drv->phy_write(ds, addr, reg, val);
35
36 return 0;
37}
38
39void dsa_slave_mii_bus_init(struct dsa_switch *ds)
40{
41 ds->slave_mii_bus->priv = (void *)ds;
42 ds->slave_mii_bus->name = "dsa slave smi";
43 ds->slave_mii_bus->read = dsa_slave_phy_read;
44 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainellif490be02013-01-21 09:58:50 +000045 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
46 ds->index, ds->pd->sw_addr);
Alexander Duyckb4d23942014-09-15 13:00:27 -040047 ds->slave_mii_bus->parent = ds->master_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000048}
49
50
51/* slave device handling ****************************************************/
Lennert Buytenhekc0840802009-03-20 09:49:49 +000052static int dsa_slave_init(struct net_device *dev)
53{
54 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhekc0840802009-03-20 09:49:49 +000055
Lennert Buytenheke84665c2009-03-20 09:52:09 +000056 dev->iflink = p->parent->dst->master_netdev->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000057
58 return 0;
59}
60
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000061static int dsa_slave_open(struct net_device *dev)
62{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080063 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +000064 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -070065 struct dsa_switch *ds = p->parent;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080066 int err;
67
68 if (!(master->flags & IFF_UP))
69 return -ENETDOWN;
70
Joe Perches8feedbb2012-05-08 18:56:57 +000071 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000072 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080073 if (err < 0)
74 goto out;
75 }
76
77 if (dev->flags & IFF_ALLMULTI) {
78 err = dev_set_allmulti(master, 1);
79 if (err < 0)
80 goto del_unicast;
81 }
82 if (dev->flags & IFF_PROMISC) {
83 err = dev_set_promiscuity(master, 1);
84 if (err < 0)
85 goto clear_allmulti;
86 }
87
Florian Fainellib2f2af22014-09-24 17:05:18 -070088 if (ds->drv->port_enable) {
89 err = ds->drv->port_enable(ds, p->port, p->phy);
90 if (err)
91 goto clear_promisc;
92 }
93
Florian Fainellif7f1de52014-09-24 17:05:17 -070094 if (p->phy)
95 phy_start(p->phy);
96
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000097 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080098
Florian Fainellib2f2af22014-09-24 17:05:18 -070099clear_promisc:
100 if (dev->flags & IFF_PROMISC)
101 dev_set_promiscuity(master, 0);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800102clear_allmulti:
103 if (dev->flags & IFF_ALLMULTI)
104 dev_set_allmulti(master, -1);
105del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000106 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000107 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800108out:
109 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110}
111
112static int dsa_slave_close(struct net_device *dev)
113{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800114 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000115 struct net_device *master = p->parent->dst->master_netdev;
Florian Fainellib2f2af22014-09-24 17:05:18 -0700116 struct dsa_switch *ds = p->parent;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800117
Florian Fainellif7f1de52014-09-24 17:05:17 -0700118 if (p->phy)
119 phy_stop(p->phy);
120
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800121 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000122 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800123 if (dev->flags & IFF_ALLMULTI)
124 dev_set_allmulti(master, -1);
125 if (dev->flags & IFF_PROMISC)
126 dev_set_promiscuity(master, -1);
127
Joe Perches8feedbb2012-05-08 18:56:57 +0000128 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000129 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800130
Florian Fainellib2f2af22014-09-24 17:05:18 -0700131 if (ds->drv->port_disable)
132 ds->drv->port_disable(ds, p->port, p->phy);
133
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000134 return 0;
135}
136
137static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
138{
139 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000140 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000141
142 if (change & IFF_ALLMULTI)
143 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
144 if (change & IFF_PROMISC)
145 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
146}
147
148static void dsa_slave_set_rx_mode(struct net_device *dev)
149{
150 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000151 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152
153 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000154 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000155}
156
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800157static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000158{
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800159 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000160 struct net_device *master = p->parent->dst->master_netdev;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800161 struct sockaddr *addr = a;
162 int err;
163
164 if (!is_valid_ether_addr(addr->sa_data))
165 return -EADDRNOTAVAIL;
166
167 if (!(dev->flags & IFF_UP))
168 goto out;
169
Joe Perches8feedbb2012-05-08 18:56:57 +0000170 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000171 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800172 if (err < 0)
173 return err;
174 }
175
Joe Perches8feedbb2012-05-08 18:56:57 +0000176 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000177 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800178
179out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800180 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000181
182 return 0;
183}
184
185static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
186{
187 struct dsa_slave_priv *p = netdev_priv(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000188
189 if (p->phy != NULL)
Richard Cochran28b04112010-07-17 08:48:55 +0000190 return phy_mii_ioctl(p->phy, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000191
192 return -EOPNOTSUPP;
193}
194
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700195static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
196{
197 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700198
Alexander Duyck50753142014-09-15 13:00:19 -0400199 return p->xmit(skb, dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700200}
201
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700202static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
203 struct net_device *dev)
204{
205 struct dsa_slave_priv *p = netdev_priv(dev);
206
207 skb->dev = p->parent->dst->master_netdev;
208 dev_queue_xmit(skb);
209
210 return NETDEV_TX_OK;
211}
212
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213
214/* ethtool operations *******************************************************/
215static int
216dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
217{
218 struct dsa_slave_priv *p = netdev_priv(dev);
219 int err;
220
221 err = -EOPNOTSUPP;
222 if (p->phy != NULL) {
223 err = phy_read_status(p->phy);
224 if (err == 0)
225 err = phy_ethtool_gset(p->phy, cmd);
226 }
227
228 return err;
229}
230
231static int
232dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
233{
234 struct dsa_slave_priv *p = netdev_priv(dev);
235
236 if (p->phy != NULL)
237 return phy_ethtool_sset(p->phy, cmd);
238
239 return -EOPNOTSUPP;
240}
241
242static void dsa_slave_get_drvinfo(struct net_device *dev,
243 struct ethtool_drvinfo *drvinfo)
244{
Jiri Pirko7826d432013-01-06 00:44:26 +0000245 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
246 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
247 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
248 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000249}
250
251static int dsa_slave_nway_reset(struct net_device *dev)
252{
253 struct dsa_slave_priv *p = netdev_priv(dev);
254
255 if (p->phy != NULL)
256 return genphy_restart_aneg(p->phy);
257
258 return -EOPNOTSUPP;
259}
260
261static u32 dsa_slave_get_link(struct net_device *dev)
262{
263 struct dsa_slave_priv *p = netdev_priv(dev);
264
265 if (p->phy != NULL) {
266 genphy_update_link(p->phy);
267 return p->phy->link;
268 }
269
270 return -EOPNOTSUPP;
271}
272
273static void dsa_slave_get_strings(struct net_device *dev,
274 uint32_t stringset, uint8_t *data)
275{
276 struct dsa_slave_priv *p = netdev_priv(dev);
277 struct dsa_switch *ds = p->parent;
278
279 if (stringset == ETH_SS_STATS) {
280 int len = ETH_GSTRING_LEN;
281
282 strncpy(data, "tx_packets", len);
283 strncpy(data + len, "tx_bytes", len);
284 strncpy(data + 2 * len, "rx_packets", len);
285 strncpy(data + 3 * len, "rx_bytes", len);
286 if (ds->drv->get_strings != NULL)
287 ds->drv->get_strings(ds, p->port, data + 4 * len);
288 }
289}
290
291static void dsa_slave_get_ethtool_stats(struct net_device *dev,
292 struct ethtool_stats *stats,
293 uint64_t *data)
294{
295 struct dsa_slave_priv *p = netdev_priv(dev);
296 struct dsa_switch *ds = p->parent;
297
298 data[0] = p->dev->stats.tx_packets;
299 data[1] = p->dev->stats.tx_bytes;
300 data[2] = p->dev->stats.rx_packets;
301 data[3] = p->dev->stats.rx_bytes;
302 if (ds->drv->get_ethtool_stats != NULL)
303 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
304}
305
306static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
307{
308 struct dsa_slave_priv *p = netdev_priv(dev);
309 struct dsa_switch *ds = p->parent;
310
311 if (sset == ETH_SS_STATS) {
312 int count;
313
314 count = 4;
315 if (ds->drv->get_sset_count != NULL)
316 count += ds->drv->get_sset_count(ds);
317
318 return count;
319 }
320
321 return -EOPNOTSUPP;
322}
323
Florian Fainelli19e57c42014-09-18 17:31:24 -0700324static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
325{
326 struct dsa_slave_priv *p = netdev_priv(dev);
327 struct dsa_switch *ds = p->parent;
328
329 if (ds->drv->get_wol)
330 ds->drv->get_wol(ds, p->port, w);
331}
332
333static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
334{
335 struct dsa_slave_priv *p = netdev_priv(dev);
336 struct dsa_switch *ds = p->parent;
337 int ret = -EOPNOTSUPP;
338
339 if (ds->drv->set_wol)
340 ret = ds->drv->set_wol(ds, p->port, w);
341
342 return ret;
343}
344
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000345static const struct ethtool_ops dsa_slave_ethtool_ops = {
346 .get_settings = dsa_slave_get_settings,
347 .set_settings = dsa_slave_set_settings,
348 .get_drvinfo = dsa_slave_get_drvinfo,
349 .nway_reset = dsa_slave_nway_reset,
350 .get_link = dsa_slave_get_link,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000351 .get_strings = dsa_slave_get_strings,
352 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
353 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700354 .set_wol = dsa_slave_set_wol,
355 .get_wol = dsa_slave_get_wol,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000356};
357
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700358static const struct net_device_ops dsa_slave_netdev_ops = {
Lennert Buytenhekc0840802009-03-20 09:49:49 +0000359 .ndo_init = dsa_slave_init,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800360 .ndo_open = dsa_slave_open,
361 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700362 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800363 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
364 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800365 .ndo_set_mac_address = dsa_slave_set_mac_address,
366 .ndo_do_ioctl = dsa_slave_ioctl,
367};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000368
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700369static void dsa_slave_adjust_link(struct net_device *dev)
370{
371 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelliec9436b2014-08-27 17:04:53 -0700372 struct dsa_switch *ds = p->parent;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700373 unsigned int status_changed = 0;
374
375 if (p->old_link != p->phy->link) {
376 status_changed = 1;
377 p->old_link = p->phy->link;
378 }
379
380 if (p->old_duplex != p->phy->duplex) {
381 status_changed = 1;
382 p->old_duplex = p->phy->duplex;
383 }
384
385 if (p->old_pause != p->phy->pause) {
386 status_changed = 1;
387 p->old_pause = p->phy->pause;
388 }
389
Florian Fainelliec9436b2014-08-27 17:04:53 -0700390 if (ds->drv->adjust_link && status_changed)
391 ds->drv->adjust_link(ds, p->port, p->phy);
392
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700393 if (status_changed)
394 phy_print_status(p->phy);
395}
396
Florian Fainellice31b312014-08-27 17:04:54 -0700397static int dsa_slave_fixed_link_update(struct net_device *dev,
398 struct fixed_phy_status *status)
399{
400 struct dsa_slave_priv *p = netdev_priv(dev);
401 struct dsa_switch *ds = p->parent;
402
403 if (ds->drv->fixed_link_update)
404 ds->drv->fixed_link_update(ds, p->port, status);
405
406 return 0;
407}
408
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000409/* slave device setup *******************************************************/
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700410static void dsa_slave_phy_setup(struct dsa_slave_priv *p,
411 struct net_device *slave_dev)
412{
413 struct dsa_switch *ds = p->parent;
414 struct dsa_chip_data *cd = ds->pd;
415 struct device_node *phy_dn, *port_dn;
Florian Fainellice31b312014-08-27 17:04:54 -0700416 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -0700417 u32 phy_flags = 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700418 int ret;
419
420 port_dn = cd->port_dn[p->port];
421 p->phy_interface = of_get_phy_mode(port_dn);
422
423 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
424 if (of_phy_is_fixed_link(port_dn)) {
425 /* In the case of a fixed PHY, the DT node associated
426 * to the fixed PHY is the Port DT node
427 */
428 ret = of_phy_register_fixed_link(port_dn);
429 if (ret) {
430 pr_err("failed to register fixed PHY\n");
431 return;
432 }
Florian Fainellice31b312014-08-27 17:04:54 -0700433 phy_is_fixed = true;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700434 phy_dn = port_dn;
435 }
436
Florian Fainelli68195632014-09-19 13:07:54 -0700437 if (ds->drv->get_phy_flags)
438 phy_flags = ds->drv->get_phy_flags(ds, p->port);
439
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700440 if (phy_dn)
441 p->phy = of_phy_connect(slave_dev, phy_dn,
Florian Fainelli68195632014-09-19 13:07:54 -0700442 dsa_slave_adjust_link, phy_flags,
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700443 p->phy_interface);
444
Florian Fainellice31b312014-08-27 17:04:54 -0700445 if (p->phy && phy_is_fixed)
446 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
447
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700448 /* We could not connect to a designated PHY, so use the switch internal
449 * MDIO bus instead
450 */
451 if (!p->phy)
452 p->phy = ds->slave_mii_bus->phy_map[p->port];
453 else
454 pr_info("attached PHY at address %d [%s]\n",
455 p->phy->addr, p->phy->drv->name);
456}
457
Florian Fainelli24462542014-09-18 17:31:22 -0700458int dsa_slave_suspend(struct net_device *slave_dev)
459{
460 struct dsa_slave_priv *p = netdev_priv(slave_dev);
461
462 netif_device_detach(slave_dev);
463
464 if (p->phy) {
465 phy_stop(p->phy);
466 p->old_pause = -1;
467 p->old_link = -1;
468 p->old_duplex = -1;
469 phy_suspend(p->phy);
470 }
471
472 return 0;
473}
474
475int dsa_slave_resume(struct net_device *slave_dev)
476{
477 struct dsa_slave_priv *p = netdev_priv(slave_dev);
478
479 netif_device_attach(slave_dev);
480
481 if (p->phy) {
482 phy_resume(p->phy);
483 phy_start(p->phy);
484 }
485
486 return 0;
487}
488
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000489struct net_device *
490dsa_slave_create(struct dsa_switch *ds, struct device *parent,
491 int port, char *name)
492{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000493 struct net_device *master = ds->dst->master_netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000494 struct net_device *slave_dev;
495 struct dsa_slave_priv *p;
496 int ret;
497
Tom Gundersenc835a672014-07-14 16:37:24 +0200498 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
499 NET_NAME_UNKNOWN, ether_setup);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000500 if (slave_dev == NULL)
501 return slave_dev;
502
503 slave_dev->features = master->vlan_features;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000504 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +0200505 eth_hw_addr_inherit(slave_dev, master);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000506 slave_dev->tx_queue_len = 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700507 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800508
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000509 SET_NETDEV_DEV(slave_dev, parent);
Florian Fainellibd474972014-08-27 17:04:50 -0700510 slave_dev->dev.of_node = ds->pd->port_dn[port];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000511 slave_dev->vlan_features = master->vlan_features;
512
513 p = netdev_priv(slave_dev);
514 p->dev = slave_dev;
515 p->parent = ds;
516 p->port = port;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700517
Alexander Duyck50753142014-09-15 13:00:19 -0400518 switch (ds->dst->tag_protocol) {
519#ifdef CONFIG_NET_DSA_TAG_DSA
520 case DSA_TAG_PROTO_DSA:
521 p->xmit = dsa_netdev_ops.xmit;
522 break;
523#endif
524#ifdef CONFIG_NET_DSA_TAG_EDSA
525 case DSA_TAG_PROTO_EDSA:
526 p->xmit = edsa_netdev_ops.xmit;
527 break;
528#endif
529#ifdef CONFIG_NET_DSA_TAG_TRAILER
530 case DSA_TAG_PROTO_TRAILER:
531 p->xmit = trailer_netdev_ops.xmit;
532 break;
533#endif
534#ifdef CONFIG_NET_DSA_TAG_BRCM
535 case DSA_TAG_PROTO_BRCM:
536 p->xmit = brcm_netdev_ops.xmit;
537 break;
538#endif
539 default:
540 p->xmit = dsa_slave_notag_xmit;
541 break;
542 }
543
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700544 p->old_pause = -1;
545 p->old_link = -1;
546 p->old_duplex = -1;
547
548 dsa_slave_phy_setup(p, slave_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000549
550 ret = register_netdev(slave_dev);
551 if (ret) {
552 printk(KERN_ERR "%s: error %d registering interface %s\n",
553 master->name, ret, slave_dev->name);
554 free_netdev(slave_dev);
555 return NULL;
556 }
557
558 netif_carrier_off(slave_dev);
559
560 if (p->phy != NULL) {
Florian Fainelli68195632014-09-19 13:07:54 -0700561 if (ds->drv->get_phy_flags(ds, port))
562 p->phy->dev_flags |= ds->drv->get_phy_flags(ds, port);
563
Kay Sieversfb28ad32008-11-10 13:55:14 -0800564 phy_attach(slave_dev, dev_name(&p->phy->dev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000565 PHY_INTERFACE_MODE_GMII);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000566
567 p->phy->autoneg = AUTONEG_ENABLE;
568 p->phy->speed = 0;
569 p->phy->duplex = 0;
570 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
571 phy_start_aneg(p->phy);
572 }
573
574 return slave_dev;
575}