net: dsa: allow for more complex PHY setups
[deliverable/linux.git] / net / dsa / slave.c
CommitLineData
91da11f8
LB
1/*
2 * net/dsa/slave.c - Slave device handling
e84665c9 3 * Copyright (c) 2008-2009 Marvell Semiconductor
91da11f8
LB
4 *
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>
12#include <linux/netdevice.h>
df02c6ff 13#include <linux/etherdevice.h>
91da11f8 14#include <linux/phy.h>
0d8bcdd3
FF
15#include <linux/of_net.h>
16#include <linux/of_mdio.h>
91da11f8
LB
17#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
0d8bcdd3 24 if (ds->phys_mii_mask & (1 << addr))
91da11f8
LB
25 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
0d8bcdd3 34 if (ds->phys_mii_mask & (1 << addr))
91da11f8
LB
35 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;
f490be04
FF
46 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
47 ds->index, ds->pd->sw_addr);
e84665c9 48 ds->slave_mii_bus->parent = &ds->master_mii_bus->dev;
91da11f8
LB
49}
50
51
52/* slave device handling ****************************************************/
c0840801
LB
53static int dsa_slave_init(struct net_device *dev)
54{
55 struct dsa_slave_priv *p = netdev_priv(dev);
c0840801 56
e84665c9 57 dev->iflink = p->parent->dst->master_netdev->ifindex;
c0840801
LB
58
59 return 0;
60}
61
91da11f8
LB
62static int dsa_slave_open(struct net_device *dev)
63{
df02c6ff 64 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 65 struct net_device *master = p->parent->dst->master_netdev;
df02c6ff
LB
66 int err;
67
68 if (!(master->flags & IFF_UP))
69 return -ENETDOWN;
70
8feedbb4 71 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
a748ee24 72 err = dev_uc_add(master, dev->dev_addr);
df02c6ff
LB
73 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
91da11f8 88 return 0;
df02c6ff
LB
89
90clear_allmulti:
91 if (dev->flags & IFF_ALLMULTI)
92 dev_set_allmulti(master, -1);
93del_unicast:
8feedbb4 94 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 95 dev_uc_del(master, dev->dev_addr);
df02c6ff
LB
96out:
97 return err;
91da11f8
LB
98}
99
100static int dsa_slave_close(struct net_device *dev)
101{
df02c6ff 102 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 103 struct net_device *master = p->parent->dst->master_netdev;
df02c6ff
LB
104
105 dev_mc_unsync(master, dev);
a748ee24 106 dev_uc_unsync(master, dev);
df02c6ff
LB
107 if (dev->flags & IFF_ALLMULTI)
108 dev_set_allmulti(master, -1);
109 if (dev->flags & IFF_PROMISC)
110 dev_set_promiscuity(master, -1);
111
8feedbb4 112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 113 dev_uc_del(master, dev->dev_addr);
df02c6ff 114
91da11f8
LB
115 return 0;
116}
117
118static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
119{
120 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 121 struct net_device *master = p->parent->dst->master_netdev;
91da11f8
LB
122
123 if (change & IFF_ALLMULTI)
124 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
125 if (change & IFF_PROMISC)
126 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
127}
128
129static void dsa_slave_set_rx_mode(struct net_device *dev)
130{
131 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 132 struct net_device *master = p->parent->dst->master_netdev;
91da11f8
LB
133
134 dev_mc_sync(master, dev);
a748ee24 135 dev_uc_sync(master, dev);
91da11f8
LB
136}
137
df02c6ff 138static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
91da11f8 139{
df02c6ff 140 struct dsa_slave_priv *p = netdev_priv(dev);
e84665c9 141 struct net_device *master = p->parent->dst->master_netdev;
df02c6ff
LB
142 struct sockaddr *addr = a;
143 int err;
144
145 if (!is_valid_ether_addr(addr->sa_data))
146 return -EADDRNOTAVAIL;
147
148 if (!(dev->flags & IFF_UP))
149 goto out;
150
8feedbb4 151 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
a748ee24 152 err = dev_uc_add(master, addr->sa_data);
df02c6ff
LB
153 if (err < 0)
154 return err;
155 }
156
8feedbb4 157 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
a748ee24 158 dev_uc_del(master, dev->dev_addr);
df02c6ff
LB
159
160out:
d08f161a 161 ether_addr_copy(dev->dev_addr, addr->sa_data);
91da11f8
LB
162
163 return 0;
164}
165
166static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
167{
168 struct dsa_slave_priv *p = netdev_priv(dev);
91da11f8
LB
169
170 if (p->phy != NULL)
28b04113 171 return phy_mii_ioctl(p->phy, ifr, cmd);
91da11f8
LB
172
173 return -EOPNOTSUPP;
174}
175
3e8a72d1
FF
176static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
177{
178 struct dsa_slave_priv *p = netdev_priv(dev);
179 struct dsa_switch_tree *dst = p->parent->dst;
180
181 return dst->ops->xmit(skb, dev);
182}
183
91da11f8
LB
184
185/* ethtool operations *******************************************************/
186static int
187dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
188{
189 struct dsa_slave_priv *p = netdev_priv(dev);
190 int err;
191
192 err = -EOPNOTSUPP;
193 if (p->phy != NULL) {
194 err = phy_read_status(p->phy);
195 if (err == 0)
196 err = phy_ethtool_gset(p->phy, cmd);
197 }
198
199 return err;
200}
201
202static int
203dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
204{
205 struct dsa_slave_priv *p = netdev_priv(dev);
206
207 if (p->phy != NULL)
208 return phy_ethtool_sset(p->phy, cmd);
209
210 return -EOPNOTSUPP;
211}
212
213static void dsa_slave_get_drvinfo(struct net_device *dev,
214 struct ethtool_drvinfo *drvinfo)
215{
7826d43f
JP
216 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
217 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
218 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
219 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
91da11f8
LB
220}
221
222static int dsa_slave_nway_reset(struct net_device *dev)
223{
224 struct dsa_slave_priv *p = netdev_priv(dev);
225
226 if (p->phy != NULL)
227 return genphy_restart_aneg(p->phy);
228
229 return -EOPNOTSUPP;
230}
231
232static u32 dsa_slave_get_link(struct net_device *dev)
233{
234 struct dsa_slave_priv *p = netdev_priv(dev);
235
236 if (p->phy != NULL) {
237 genphy_update_link(p->phy);
238 return p->phy->link;
239 }
240
241 return -EOPNOTSUPP;
242}
243
244static void dsa_slave_get_strings(struct net_device *dev,
245 uint32_t stringset, uint8_t *data)
246{
247 struct dsa_slave_priv *p = netdev_priv(dev);
248 struct dsa_switch *ds = p->parent;
249
250 if (stringset == ETH_SS_STATS) {
251 int len = ETH_GSTRING_LEN;
252
253 strncpy(data, "tx_packets", len);
254 strncpy(data + len, "tx_bytes", len);
255 strncpy(data + 2 * len, "rx_packets", len);
256 strncpy(data + 3 * len, "rx_bytes", len);
257 if (ds->drv->get_strings != NULL)
258 ds->drv->get_strings(ds, p->port, data + 4 * len);
259 }
260}
261
262static void dsa_slave_get_ethtool_stats(struct net_device *dev,
263 struct ethtool_stats *stats,
264 uint64_t *data)
265{
266 struct dsa_slave_priv *p = netdev_priv(dev);
267 struct dsa_switch *ds = p->parent;
268
269 data[0] = p->dev->stats.tx_packets;
270 data[1] = p->dev->stats.tx_bytes;
271 data[2] = p->dev->stats.rx_packets;
272 data[3] = p->dev->stats.rx_bytes;
273 if (ds->drv->get_ethtool_stats != NULL)
274 ds->drv->get_ethtool_stats(ds, p->port, data + 4);
275}
276
277static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
278{
279 struct dsa_slave_priv *p = netdev_priv(dev);
280 struct dsa_switch *ds = p->parent;
281
282 if (sset == ETH_SS_STATS) {
283 int count;
284
285 count = 4;
286 if (ds->drv->get_sset_count != NULL)
287 count += ds->drv->get_sset_count(ds);
288
289 return count;
290 }
291
292 return -EOPNOTSUPP;
293}
294
295static const struct ethtool_ops dsa_slave_ethtool_ops = {
296 .get_settings = dsa_slave_get_settings,
297 .set_settings = dsa_slave_set_settings,
298 .get_drvinfo = dsa_slave_get_drvinfo,
299 .nway_reset = dsa_slave_nway_reset,
300 .get_link = dsa_slave_get_link,
91da11f8
LB
301 .get_strings = dsa_slave_get_strings,
302 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
303 .get_sset_count = dsa_slave_get_sset_count,
304};
305
3e8a72d1 306static const struct net_device_ops dsa_slave_netdev_ops = {
c0840801 307 .ndo_init = dsa_slave_init,
d442ad4a
SH
308 .ndo_open = dsa_slave_open,
309 .ndo_stop = dsa_slave_close,
3e8a72d1 310 .ndo_start_xmit = dsa_slave_xmit,
d442ad4a
SH
311 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
312 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
d442ad4a
SH
313 .ndo_set_mac_address = dsa_slave_set_mac_address,
314 .ndo_do_ioctl = dsa_slave_ioctl,
315};
91da11f8 316
0d8bcdd3
FF
317static void dsa_slave_adjust_link(struct net_device *dev)
318{
319 struct dsa_slave_priv *p = netdev_priv(dev);
320 unsigned int status_changed = 0;
321
322 if (p->old_link != p->phy->link) {
323 status_changed = 1;
324 p->old_link = p->phy->link;
325 }
326
327 if (p->old_duplex != p->phy->duplex) {
328 status_changed = 1;
329 p->old_duplex = p->phy->duplex;
330 }
331
332 if (p->old_pause != p->phy->pause) {
333 status_changed = 1;
334 p->old_pause = p->phy->pause;
335 }
336
337 if (status_changed)
338 phy_print_status(p->phy);
339}
340
91da11f8 341/* slave device setup *******************************************************/
0d8bcdd3
FF
342static void dsa_slave_phy_setup(struct dsa_slave_priv *p,
343 struct net_device *slave_dev)
344{
345 struct dsa_switch *ds = p->parent;
346 struct dsa_chip_data *cd = ds->pd;
347 struct device_node *phy_dn, *port_dn;
348 int ret;
349
350 port_dn = cd->port_dn[p->port];
351 p->phy_interface = of_get_phy_mode(port_dn);
352
353 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
354 if (of_phy_is_fixed_link(port_dn)) {
355 /* In the case of a fixed PHY, the DT node associated
356 * to the fixed PHY is the Port DT node
357 */
358 ret = of_phy_register_fixed_link(port_dn);
359 if (ret) {
360 pr_err("failed to register fixed PHY\n");
361 return;
362 }
363 phy_dn = port_dn;
364 }
365
366 if (phy_dn)
367 p->phy = of_phy_connect(slave_dev, phy_dn,
368 dsa_slave_adjust_link, 0,
369 p->phy_interface);
370
371 /* We could not connect to a designated PHY, so use the switch internal
372 * MDIO bus instead
373 */
374 if (!p->phy)
375 p->phy = ds->slave_mii_bus->phy_map[p->port];
376 else
377 pr_info("attached PHY at address %d [%s]\n",
378 p->phy->addr, p->phy->drv->name);
379}
380
91da11f8
LB
381struct net_device *
382dsa_slave_create(struct dsa_switch *ds, struct device *parent,
383 int port, char *name)
384{
e84665c9 385 struct net_device *master = ds->dst->master_netdev;
91da11f8
LB
386 struct net_device *slave_dev;
387 struct dsa_slave_priv *p;
388 int ret;
389
c835a677
TG
390 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
391 NET_NAME_UNKNOWN, ether_setup);
91da11f8
LB
392 if (slave_dev == NULL)
393 return slave_dev;
394
395 slave_dev->features = master->vlan_features;
7ad24ea4 396 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2fcc8005 397 eth_hw_addr_inherit(slave_dev, master);
91da11f8 398 slave_dev->tx_queue_len = 0;
3e8a72d1 399 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
d442ad4a 400
e84665c9 401 switch (ds->dst->tag_protocol) {
cf85d08f
LB
402#ifdef CONFIG_NET_DSA_TAG_DSA
403 case htons(ETH_P_DSA):
3e8a72d1 404 ds->dst->ops = &dsa_netdev_ops;
cf85d08f
LB
405 break;
406#endif
91da11f8
LB
407#ifdef CONFIG_NET_DSA_TAG_EDSA
408 case htons(ETH_P_EDSA):
3e8a72d1 409 ds->dst->ops = &edsa_netdev_ops;
91da11f8 410 break;
396138f0
LB
411#endif
412#ifdef CONFIG_NET_DSA_TAG_TRAILER
413 case htons(ETH_P_TRAILER):
3e8a72d1 414 ds->dst->ops = &trailer_netdev_ops;
396138f0 415 break;
91da11f8
LB
416#endif
417 default:
418 BUG();
419 }
d442ad4a 420
91da11f8 421 SET_NETDEV_DEV(slave_dev, parent);
bd47497a 422 slave_dev->dev.of_node = ds->pd->port_dn[port];
91da11f8
LB
423 slave_dev->vlan_features = master->vlan_features;
424
425 p = netdev_priv(slave_dev);
426 p->dev = slave_dev;
427 p->parent = ds;
428 p->port = port;
0d8bcdd3
FF
429
430 p->old_pause = -1;
431 p->old_link = -1;
432 p->old_duplex = -1;
433
434 dsa_slave_phy_setup(p, slave_dev);
91da11f8
LB
435
436 ret = register_netdev(slave_dev);
437 if (ret) {
438 printk(KERN_ERR "%s: error %d registering interface %s\n",
439 master->name, ret, slave_dev->name);
440 free_netdev(slave_dev);
441 return NULL;
442 }
443
444 netif_carrier_off(slave_dev);
445
446 if (p->phy != NULL) {
fb28ad35 447 phy_attach(slave_dev, dev_name(&p->phy->dev),
f9a8f83b 448 PHY_INTERFACE_MODE_GMII);
91da11f8
LB
449
450 p->phy->autoneg = AUTONEG_ENABLE;
451 p->phy->speed = 0;
452 p->phy->duplex = 0;
453 p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
454 phy_start_aneg(p->phy);
455 }
456
457 return slave_dev;
458}
This page took 0.45036 seconds and 5 git commands to generate.