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