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> |
b73adef6 | 13 | #include <linux/netdevice.h> |
91da11f8 | 14 | #include <linux/phy.h> |
a2820543 | 15 | #include <linux/phy_fixed.h> |
0d8bcdd3 FF |
16 | #include <linux/of_net.h> |
17 | #include <linux/of_mdio.h> | |
7f854420 | 18 | #include <linux/mdio.h> |
f50f2127 | 19 | #include <linux/list.h> |
b73adef6 | 20 | #include <net/rtnetlink.h> |
f50f2127 FF |
21 | #include <net/pkt_cls.h> |
22 | #include <net/tc_act/tc_mirred.h> | |
b73adef6 | 23 | #include <linux/if_bridge.h> |
04ff53f9 | 24 | #include <linux/netpoll.h> |
ea5dd34b | 25 | |
91da11f8 LB |
26 | #include "dsa_priv.h" |
27 | ||
f50f2127 FF |
28 | static bool dsa_slave_dev_check(struct net_device *dev); |
29 | ||
91da11f8 LB |
30 | /* slave mii_bus handling ***************************************************/ |
31 | static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) | |
32 | { | |
33 | struct dsa_switch *ds = bus->priv; | |
34 | ||
0d8bcdd3 | 35 | if (ds->phys_mii_mask & (1 << addr)) |
9d490b4e | 36 | return ds->ops->phy_read(ds, addr, reg); |
91da11f8 LB |
37 | |
38 | return 0xffff; | |
39 | } | |
40 | ||
41 | static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) | |
42 | { | |
43 | struct dsa_switch *ds = bus->priv; | |
44 | ||
0d8bcdd3 | 45 | if (ds->phys_mii_mask & (1 << addr)) |
9d490b4e | 46 | return ds->ops->phy_write(ds, addr, reg, val); |
91da11f8 LB |
47 | |
48 | return 0; | |
49 | } | |
50 | ||
51 | void dsa_slave_mii_bus_init(struct dsa_switch *ds) | |
52 | { | |
53 | ds->slave_mii_bus->priv = (void *)ds; | |
54 | ds->slave_mii_bus->name = "dsa slave smi"; | |
55 | ds->slave_mii_bus->read = dsa_slave_phy_read; | |
56 | ds->slave_mii_bus->write = dsa_slave_phy_write; | |
0b7b498d FF |
57 | snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", |
58 | ds->dst->tree, ds->index); | |
c33063d6 | 59 | ds->slave_mii_bus->parent = ds->dev; |
24df8986 | 60 | ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; |
91da11f8 LB |
61 | } |
62 | ||
63 | ||
64 | /* slave device handling ****************************************************/ | |
abd2be00 | 65 | static int dsa_slave_get_iflink(const struct net_device *dev) |
c0840801 LB |
66 | { |
67 | struct dsa_slave_priv *p = netdev_priv(dev); | |
c0840801 | 68 | |
6d3c8c0d | 69 | return dsa_master_netdev(p)->ifindex; |
c0840801 LB |
70 | } |
71 | ||
91da11f8 LB |
72 | static int dsa_slave_open(struct net_device *dev) |
73 | { | |
df02c6ff | 74 | struct dsa_slave_priv *p = netdev_priv(dev); |
f3d736c4 VD |
75 | struct dsa_port *dp = p->dp; |
76 | struct dsa_switch *ds = dp->ds; | |
6d3c8c0d | 77 | struct net_device *master = dsa_master_netdev(p); |
f3d736c4 | 78 | u8 stp_state = dp->bridge_dev ? BR_STATE_BLOCKING : BR_STATE_FORWARDING; |
df02c6ff LB |
79 | int err; |
80 | ||
81 | if (!(master->flags & IFF_UP)) | |
82 | return -ENETDOWN; | |
83 | ||
8feedbb4 | 84 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { |
a748ee24 | 85 | err = dev_uc_add(master, dev->dev_addr); |
df02c6ff LB |
86 | if (err < 0) |
87 | goto out; | |
88 | } | |
89 | ||
90 | if (dev->flags & IFF_ALLMULTI) { | |
91 | err = dev_set_allmulti(master, 1); | |
92 | if (err < 0) | |
93 | goto del_unicast; | |
94 | } | |
95 | if (dev->flags & IFF_PROMISC) { | |
96 | err = dev_set_promiscuity(master, 1); | |
97 | if (err < 0) | |
98 | goto clear_allmulti; | |
99 | } | |
100 | ||
9d490b4e | 101 | if (ds->ops->port_enable) { |
afdcf151 | 102 | err = ds->ops->port_enable(ds, p->dp->index, p->phy); |
b2f2af21 FF |
103 | if (err) |
104 | goto clear_promisc; | |
105 | } | |
106 | ||
fd364541 | 107 | dsa_port_set_state_now(p->dp, stp_state); |
b73adef6 | 108 | |
f7f1de51 FF |
109 | if (p->phy) |
110 | phy_start(p->phy); | |
111 | ||
91da11f8 | 112 | return 0; |
df02c6ff | 113 | |
b2f2af21 FF |
114 | clear_promisc: |
115 | if (dev->flags & IFF_PROMISC) | |
4fdeddfe | 116 | dev_set_promiscuity(master, -1); |
df02c6ff LB |
117 | clear_allmulti: |
118 | if (dev->flags & IFF_ALLMULTI) | |
119 | dev_set_allmulti(master, -1); | |
120 | del_unicast: | |
8feedbb4 | 121 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
a748ee24 | 122 | dev_uc_del(master, dev->dev_addr); |
df02c6ff LB |
123 | out: |
124 | return err; | |
91da11f8 LB |
125 | } |
126 | ||
127 | static int dsa_slave_close(struct net_device *dev) | |
128 | { | |
df02c6ff | 129 | struct dsa_slave_priv *p = netdev_priv(dev); |
6d3c8c0d | 130 | struct net_device *master = dsa_master_netdev(p); |
afdcf151 | 131 | struct dsa_switch *ds = p->dp->ds; |
df02c6ff | 132 | |
f7f1de51 FF |
133 | if (p->phy) |
134 | phy_stop(p->phy); | |
135 | ||
df02c6ff | 136 | dev_mc_unsync(master, dev); |
a748ee24 | 137 | dev_uc_unsync(master, dev); |
df02c6ff LB |
138 | if (dev->flags & IFF_ALLMULTI) |
139 | dev_set_allmulti(master, -1); | |
140 | if (dev->flags & IFF_PROMISC) | |
141 | dev_set_promiscuity(master, -1); | |
142 | ||
8feedbb4 | 143 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
a748ee24 | 144 | dev_uc_del(master, dev->dev_addr); |
df02c6ff | 145 | |
9d490b4e | 146 | if (ds->ops->port_disable) |
afdcf151 | 147 | ds->ops->port_disable(ds, p->dp->index, p->phy); |
b2f2af21 | 148 | |
fd364541 | 149 | dsa_port_set_state_now(p->dp, BR_STATE_DISABLED); |
b73adef6 | 150 | |
91da11f8 LB |
151 | return 0; |
152 | } | |
153 | ||
154 | static void dsa_slave_change_rx_flags(struct net_device *dev, int change) | |
155 | { | |
156 | struct dsa_slave_priv *p = netdev_priv(dev); | |
6d3c8c0d | 157 | struct net_device *master = dsa_master_netdev(p); |
91da11f8 LB |
158 | |
159 | if (change & IFF_ALLMULTI) | |
160 | dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); | |
161 | if (change & IFF_PROMISC) | |
162 | dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); | |
163 | } | |
164 | ||
165 | static void dsa_slave_set_rx_mode(struct net_device *dev) | |
166 | { | |
167 | struct dsa_slave_priv *p = netdev_priv(dev); | |
6d3c8c0d | 168 | struct net_device *master = dsa_master_netdev(p); |
91da11f8 LB |
169 | |
170 | dev_mc_sync(master, dev); | |
a748ee24 | 171 | dev_uc_sync(master, dev); |
91da11f8 LB |
172 | } |
173 | ||
df02c6ff | 174 | static int dsa_slave_set_mac_address(struct net_device *dev, void *a) |
91da11f8 | 175 | { |
df02c6ff | 176 | struct dsa_slave_priv *p = netdev_priv(dev); |
6d3c8c0d | 177 | struct net_device *master = dsa_master_netdev(p); |
df02c6ff LB |
178 | struct sockaddr *addr = a; |
179 | int err; | |
180 | ||
181 | if (!is_valid_ether_addr(addr->sa_data)) | |
182 | return -EADDRNOTAVAIL; | |
183 | ||
184 | if (!(dev->flags & IFF_UP)) | |
185 | goto out; | |
186 | ||
8feedbb4 | 187 | if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { |
a748ee24 | 188 | err = dev_uc_add(master, addr->sa_data); |
df02c6ff LB |
189 | if (err < 0) |
190 | return err; | |
191 | } | |
192 | ||
8feedbb4 | 193 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
a748ee24 | 194 | dev_uc_del(master, dev->dev_addr); |
df02c6ff LB |
195 | |
196 | out: | |
d08f161a | 197 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
91da11f8 LB |
198 | |
199 | return 0; | |
200 | } | |
201 | ||
202 | static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) | |
203 | { | |
204 | struct dsa_slave_priv *p = netdev_priv(dev); | |
91da11f8 LB |
205 | |
206 | if (p->phy != NULL) | |
28b04113 | 207 | return phy_mii_ioctl(p->phy, ifr, cmd); |
91da11f8 LB |
208 | |
209 | return -EOPNOTSUPP; | |
210 | } | |
211 | ||
35636062 | 212 | static int dsa_slave_port_attr_set(struct net_device *dev, |
f7fadf30 | 213 | const struct switchdev_attr *attr, |
7ea6eb3f | 214 | struct switchdev_trans *trans) |
35636062 | 215 | { |
fd364541 VD |
216 | struct dsa_slave_priv *p = netdev_priv(dev); |
217 | struct dsa_port *dp = p->dp; | |
b8d866ac | 218 | int ret; |
35636062 SF |
219 | |
220 | switch (attr->id) { | |
1f868398 | 221 | case SWITCHDEV_ATTR_ID_PORT_STP_STATE: |
fd364541 | 222 | ret = dsa_port_set_state(dp, attr->u.stp_state, trans); |
35636062 | 223 | break; |
fb2dabad | 224 | case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: |
c02c4175 VD |
225 | ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, |
226 | trans); | |
fb2dabad | 227 | break; |
34a79f63 | 228 | case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: |
072bb190 | 229 | ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); |
34a79f63 | 230 | break; |
35636062 SF |
231 | default: |
232 | ret = -EOPNOTSUPP; | |
233 | break; | |
234 | } | |
235 | ||
236 | return ret; | |
237 | } | |
238 | ||
ba14d9eb | 239 | static int dsa_slave_port_obj_add(struct net_device *dev, |
648b4a99 | 240 | const struct switchdev_obj *obj, |
7ea6eb3f | 241 | struct switchdev_trans *trans) |
ba14d9eb | 242 | { |
3fdb023b VD |
243 | struct dsa_slave_priv *p = netdev_priv(dev); |
244 | struct dsa_port *dp = p->dp; | |
ba14d9eb VD |
245 | int err; |
246 | ||
247 | /* For the prepare phase, ensure the full set of changes is feasable in | |
248 | * one go in order to signal a failure properly. If an operation is not | |
249 | * supported, return -EOPNOTSUPP. | |
250 | */ | |
251 | ||
9e8f4a54 | 252 | switch (obj->id) { |
57d80838 | 253 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
3fdb023b | 254 | err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans); |
ba14d9eb | 255 | break; |
8df30255 | 256 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
bcebb976 | 257 | err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); |
8df30255 | 258 | break; |
57d80838 | 259 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
01676d12 VD |
260 | err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), |
261 | trans); | |
11149536 | 262 | break; |
ba14d9eb VD |
263 | default: |
264 | err = -EOPNOTSUPP; | |
265 | break; | |
266 | } | |
267 | ||
268 | return err; | |
269 | } | |
270 | ||
271 | static int dsa_slave_port_obj_del(struct net_device *dev, | |
648b4a99 | 272 | const struct switchdev_obj *obj) |
ba14d9eb | 273 | { |
3fdb023b VD |
274 | struct dsa_slave_priv *p = netdev_priv(dev); |
275 | struct dsa_port *dp = p->dp; | |
ba14d9eb VD |
276 | int err; |
277 | ||
9e8f4a54 | 278 | switch (obj->id) { |
57d80838 | 279 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
3fdb023b | 280 | err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj)); |
ba14d9eb | 281 | break; |
8df30255 | 282 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
bcebb976 | 283 | err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); |
8df30255 | 284 | break; |
57d80838 | 285 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
01676d12 | 286 | err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); |
11149536 | 287 | break; |
ba14d9eb VD |
288 | default: |
289 | err = -EOPNOTSUPP; | |
290 | break; | |
291 | } | |
292 | ||
293 | return err; | |
294 | } | |
295 | ||
296 | static int dsa_slave_port_obj_dump(struct net_device *dev, | |
648b4a99 JP |
297 | struct switchdev_obj *obj, |
298 | switchdev_obj_dump_cb_t *cb) | |
ba14d9eb | 299 | { |
3fdb023b VD |
300 | struct dsa_slave_priv *p = netdev_priv(dev); |
301 | struct dsa_port *dp = p->dp; | |
ba14d9eb VD |
302 | int err; |
303 | ||
9e8f4a54 | 304 | switch (obj->id) { |
57d80838 | 305 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
3fdb023b | 306 | err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb); |
ba14d9eb | 307 | break; |
8df30255 | 308 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
bcebb976 | 309 | err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb); |
8df30255 | 310 | break; |
57d80838 | 311 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
01676d12 | 312 | err = dsa_port_vlan_dump(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), cb); |
11149536 | 313 | break; |
ba14d9eb VD |
314 | default: |
315 | err = -EOPNOTSUPP; | |
316 | break; | |
317 | } | |
318 | ||
319 | return err; | |
320 | } | |
321 | ||
f8e20a9f SF |
322 | static int dsa_slave_port_attr_get(struct net_device *dev, |
323 | struct switchdev_attr *attr) | |
b73adef6 FF |
324 | { |
325 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 326 | struct dsa_switch *ds = p->dp->ds; |
b73adef6 | 327 | |
f8e20a9f | 328 | switch (attr->id) { |
1f868398 | 329 | case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: |
42275bd8 SF |
330 | attr->u.ppid.id_len = sizeof(ds->index); |
331 | memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); | |
f8e20a9f SF |
332 | break; |
333 | default: | |
334 | return -EOPNOTSUPP; | |
335 | } | |
b73adef6 FF |
336 | |
337 | return 0; | |
338 | } | |
339 | ||
04ff53f9 FF |
340 | static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p, |
341 | struct sk_buff *skb) | |
342 | { | |
343 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
344 | if (p->netpoll) | |
345 | netpoll_send_skb(p->netpoll, skb); | |
346 | #else | |
347 | BUG(); | |
348 | #endif | |
349 | return NETDEV_TX_OK; | |
350 | } | |
351 | ||
3e8a72d1 FF |
352 | static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) |
353 | { | |
354 | struct dsa_slave_priv *p = netdev_priv(dev); | |
4ed70ce9 | 355 | struct sk_buff *nskb; |
3e8a72d1 | 356 | |
f613ed66 FF |
357 | u64_stats_update_begin(&p->stats64.syncp); |
358 | p->stats64.tx_packets++; | |
359 | p->stats64.tx_bytes += skb->len; | |
360 | u64_stats_update_end(&p->stats64.syncp); | |
3e8a72d1 | 361 | |
fe47d563 VD |
362 | /* Transmit function may have to reallocate the original SKB, |
363 | * in which case it must have freed it. Only free it here on error. | |
364 | */ | |
4ed70ce9 | 365 | nskb = p->xmit(skb, dev); |
fe47d563 VD |
366 | if (!nskb) { |
367 | kfree_skb(skb); | |
4ed70ce9 | 368 | return NETDEV_TX_OK; |
fe47d563 | 369 | } |
5aed85ce | 370 | |
04ff53f9 FF |
371 | /* SKB for netpoll still need to be mangled with the protocol-specific |
372 | * tag to be successfully transmitted | |
373 | */ | |
374 | if (unlikely(netpoll_tx_running(dev))) | |
375 | return dsa_netpoll_send_skb(p, nskb); | |
376 | ||
4ed70ce9 FF |
377 | /* Queue the SKB for transmission on the parent interface, but |
378 | * do not modify its EtherType | |
379 | */ | |
6d3c8c0d | 380 | nskb->dev = dsa_master_netdev(p); |
4ed70ce9 | 381 | dev_queue_xmit(nskb); |
5aed85ce FF |
382 | |
383 | return NETDEV_TX_OK; | |
384 | } | |
385 | ||
91da11f8 LB |
386 | /* ethtool operations *******************************************************/ |
387 | static int | |
bb10bb3e PR |
388 | dsa_slave_get_link_ksettings(struct net_device *dev, |
389 | struct ethtool_link_ksettings *cmd) | |
91da11f8 LB |
390 | { |
391 | struct dsa_slave_priv *p = netdev_priv(dev); | |
91da11f8 | 392 | |
5514174f | 393 | if (!p->phy) |
394 | return -EOPNOTSUPP; | |
91da11f8 | 395 | |
5514174f | 396 | phy_ethtool_ksettings_get(p->phy, cmd); |
397 | ||
398 | return 0; | |
91da11f8 LB |
399 | } |
400 | ||
401 | static int | |
bb10bb3e PR |
402 | dsa_slave_set_link_ksettings(struct net_device *dev, |
403 | const struct ethtool_link_ksettings *cmd) | |
91da11f8 LB |
404 | { |
405 | struct dsa_slave_priv *p = netdev_priv(dev); | |
406 | ||
407 | if (p->phy != NULL) | |
bb10bb3e | 408 | return phy_ethtool_ksettings_set(p->phy, cmd); |
91da11f8 LB |
409 | |
410 | return -EOPNOTSUPP; | |
411 | } | |
412 | ||
413 | static void dsa_slave_get_drvinfo(struct net_device *dev, | |
414 | struct ethtool_drvinfo *drvinfo) | |
415 | { | |
7826d43f | 416 | strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); |
7826d43f JP |
417 | strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); |
418 | strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); | |
91da11f8 LB |
419 | } |
420 | ||
3d762a0f GR |
421 | static int dsa_slave_get_regs_len(struct net_device *dev) |
422 | { | |
423 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 424 | struct dsa_switch *ds = p->dp->ds; |
3d762a0f | 425 | |
9d490b4e | 426 | if (ds->ops->get_regs_len) |
afdcf151 | 427 | return ds->ops->get_regs_len(ds, p->dp->index); |
3d762a0f GR |
428 | |
429 | return -EOPNOTSUPP; | |
430 | } | |
431 | ||
432 | static void | |
433 | dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) | |
434 | { | |
435 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 436 | struct dsa_switch *ds = p->dp->ds; |
3d762a0f | 437 | |
9d490b4e | 438 | if (ds->ops->get_regs) |
afdcf151 | 439 | ds->ops->get_regs(ds, p->dp->index, regs, _p); |
3d762a0f GR |
440 | } |
441 | ||
91da11f8 LB |
442 | static int dsa_slave_nway_reset(struct net_device *dev) |
443 | { | |
444 | struct dsa_slave_priv *p = netdev_priv(dev); | |
445 | ||
446 | if (p->phy != NULL) | |
447 | return genphy_restart_aneg(p->phy); | |
448 | ||
449 | return -EOPNOTSUPP; | |
450 | } | |
451 | ||
452 | static u32 dsa_slave_get_link(struct net_device *dev) | |
453 | { | |
454 | struct dsa_slave_priv *p = netdev_priv(dev); | |
455 | ||
456 | if (p->phy != NULL) { | |
457 | genphy_update_link(p->phy); | |
458 | return p->phy->link; | |
459 | } | |
460 | ||
461 | return -EOPNOTSUPP; | |
462 | } | |
463 | ||
6793abb4 GR |
464 | static int dsa_slave_get_eeprom_len(struct net_device *dev) |
465 | { | |
466 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 467 | struct dsa_switch *ds = p->dp->ds; |
6793abb4 | 468 | |
0e576044 | 469 | if (ds->cd && ds->cd->eeprom_len) |
ff04955c | 470 | return ds->cd->eeprom_len; |
6793abb4 | 471 | |
9d490b4e VD |
472 | if (ds->ops->get_eeprom_len) |
473 | return ds->ops->get_eeprom_len(ds); | |
6793abb4 GR |
474 | |
475 | return 0; | |
476 | } | |
477 | ||
478 | static int dsa_slave_get_eeprom(struct net_device *dev, | |
479 | struct ethtool_eeprom *eeprom, u8 *data) | |
480 | { | |
481 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 482 | struct dsa_switch *ds = p->dp->ds; |
6793abb4 | 483 | |
9d490b4e VD |
484 | if (ds->ops->get_eeprom) |
485 | return ds->ops->get_eeprom(ds, eeprom, data); | |
6793abb4 GR |
486 | |
487 | return -EOPNOTSUPP; | |
488 | } | |
489 | ||
490 | static int dsa_slave_set_eeprom(struct net_device *dev, | |
491 | struct ethtool_eeprom *eeprom, u8 *data) | |
492 | { | |
493 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 494 | struct dsa_switch *ds = p->dp->ds; |
6793abb4 | 495 | |
9d490b4e VD |
496 | if (ds->ops->set_eeprom) |
497 | return ds->ops->set_eeprom(ds, eeprom, data); | |
6793abb4 GR |
498 | |
499 | return -EOPNOTSUPP; | |
500 | } | |
501 | ||
91da11f8 LB |
502 | static void dsa_slave_get_strings(struct net_device *dev, |
503 | uint32_t stringset, uint8_t *data) | |
504 | { | |
505 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 506 | struct dsa_switch *ds = p->dp->ds; |
91da11f8 LB |
507 | |
508 | if (stringset == ETH_SS_STATS) { | |
509 | int len = ETH_GSTRING_LEN; | |
510 | ||
511 | strncpy(data, "tx_packets", len); | |
512 | strncpy(data + len, "tx_bytes", len); | |
513 | strncpy(data + 2 * len, "rx_packets", len); | |
514 | strncpy(data + 3 * len, "rx_bytes", len); | |
9d490b4e | 515 | if (ds->ops->get_strings) |
afdcf151 | 516 | ds->ops->get_strings(ds, p->dp->index, data + 4 * len); |
91da11f8 LB |
517 | } |
518 | } | |
519 | ||
badf3ada FF |
520 | static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev, |
521 | struct ethtool_stats *stats, | |
522 | uint64_t *data) | |
523 | { | |
524 | struct dsa_switch_tree *dst = dev->dsa_ptr; | |
3cc9f257 FF |
525 | struct dsa_port *cpu_dp = dsa_get_cpu_port(dst); |
526 | struct dsa_switch *ds = cpu_dp->ds; | |
527 | s8 cpu_port = cpu_dp->index; | |
badf3ada FF |
528 | int count = 0; |
529 | ||
3cc9f257 FF |
530 | if (cpu_dp->ethtool_ops.get_sset_count) { |
531 | count = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS); | |
532 | cpu_dp->ethtool_ops.get_ethtool_stats(dev, stats, data); | |
badf3ada FF |
533 | } |
534 | ||
9d490b4e VD |
535 | if (ds->ops->get_ethtool_stats) |
536 | ds->ops->get_ethtool_stats(ds, cpu_port, data + count); | |
badf3ada FF |
537 | } |
538 | ||
539 | static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset) | |
540 | { | |
541 | struct dsa_switch_tree *dst = dev->dsa_ptr; | |
3cc9f257 FF |
542 | struct dsa_port *cpu_dp = dsa_get_cpu_port(dst); |
543 | struct dsa_switch *ds = cpu_dp->ds; | |
badf3ada FF |
544 | int count = 0; |
545 | ||
3cc9f257 FF |
546 | if (cpu_dp->ethtool_ops.get_sset_count) |
547 | count += cpu_dp->ethtool_ops.get_sset_count(dev, sset); | |
badf3ada | 548 | |
9d490b4e VD |
549 | if (sset == ETH_SS_STATS && ds->ops->get_sset_count) |
550 | count += ds->ops->get_sset_count(ds); | |
badf3ada FF |
551 | |
552 | return count; | |
553 | } | |
554 | ||
555 | static void dsa_cpu_port_get_strings(struct net_device *dev, | |
556 | uint32_t stringset, uint8_t *data) | |
557 | { | |
558 | struct dsa_switch_tree *dst = dev->dsa_ptr; | |
3cc9f257 FF |
559 | struct dsa_port *cpu_dp = dsa_get_cpu_port(dst); |
560 | struct dsa_switch *ds = cpu_dp->ds; | |
561 | s8 cpu_port = cpu_dp->index; | |
badf3ada FF |
562 | int len = ETH_GSTRING_LEN; |
563 | int mcount = 0, count; | |
564 | unsigned int i; | |
565 | uint8_t pfx[4]; | |
566 | uint8_t *ndata; | |
567 | ||
568 | snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port); | |
569 | /* We do not want to be NULL-terminated, since this is a prefix */ | |
570 | pfx[sizeof(pfx) - 1] = '_'; | |
571 | ||
3cc9f257 FF |
572 | if (cpu_dp->ethtool_ops.get_sset_count) { |
573 | mcount = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS); | |
574 | cpu_dp->ethtool_ops.get_strings(dev, stringset, data); | |
badf3ada FF |
575 | } |
576 | ||
9d490b4e | 577 | if (stringset == ETH_SS_STATS && ds->ops->get_strings) { |
badf3ada FF |
578 | ndata = data + mcount * len; |
579 | /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle | |
580 | * the output after to prepend our CPU port prefix we | |
581 | * constructed earlier | |
582 | */ | |
9d490b4e VD |
583 | ds->ops->get_strings(ds, cpu_port, ndata); |
584 | count = ds->ops->get_sset_count(ds); | |
badf3ada FF |
585 | for (i = 0; i < count; i++) { |
586 | memmove(ndata + (i * len + sizeof(pfx)), | |
587 | ndata + i * len, len - sizeof(pfx)); | |
588 | memcpy(ndata + i * len, pfx, sizeof(pfx)); | |
589 | } | |
590 | } | |
591 | } | |
592 | ||
91da11f8 LB |
593 | static void dsa_slave_get_ethtool_stats(struct net_device *dev, |
594 | struct ethtool_stats *stats, | |
595 | uint64_t *data) | |
596 | { | |
597 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 598 | struct dsa_switch *ds = p->dp->ds; |
f613ed66 FF |
599 | unsigned int start; |
600 | ||
601 | do { | |
602 | start = u64_stats_fetch_begin_irq(&p->stats64.syncp); | |
603 | data[0] = p->stats64.tx_packets; | |
604 | data[1] = p->stats64.tx_bytes; | |
605 | data[2] = p->stats64.rx_packets; | |
606 | data[3] = p->stats64.rx_bytes; | |
607 | } while (u64_stats_fetch_retry_irq(&p->stats64.syncp, start)); | |
9d490b4e | 608 | if (ds->ops->get_ethtool_stats) |
afdcf151 | 609 | ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4); |
91da11f8 LB |
610 | } |
611 | ||
612 | static int dsa_slave_get_sset_count(struct net_device *dev, int sset) | |
613 | { | |
614 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 615 | struct dsa_switch *ds = p->dp->ds; |
91da11f8 LB |
616 | |
617 | if (sset == ETH_SS_STATS) { | |
618 | int count; | |
619 | ||
620 | count = 4; | |
9d490b4e VD |
621 | if (ds->ops->get_sset_count) |
622 | count += ds->ops->get_sset_count(ds); | |
91da11f8 LB |
623 | |
624 | return count; | |
625 | } | |
626 | ||
627 | return -EOPNOTSUPP; | |
628 | } | |
629 | ||
19e57c4e FF |
630 | static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
631 | { | |
632 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 633 | struct dsa_switch *ds = p->dp->ds; |
19e57c4e | 634 | |
9d490b4e | 635 | if (ds->ops->get_wol) |
afdcf151 | 636 | ds->ops->get_wol(ds, p->dp->index, w); |
19e57c4e FF |
637 | } |
638 | ||
639 | static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) | |
640 | { | |
641 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 642 | struct dsa_switch *ds = p->dp->ds; |
19e57c4e FF |
643 | int ret = -EOPNOTSUPP; |
644 | ||
9d490b4e | 645 | if (ds->ops->set_wol) |
afdcf151 | 646 | ret = ds->ops->set_wol(ds, p->dp->index, w); |
19e57c4e FF |
647 | |
648 | return ret; | |
649 | } | |
650 | ||
7905288f FF |
651 | static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) |
652 | { | |
653 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 654 | struct dsa_switch *ds = p->dp->ds; |
7905288f FF |
655 | int ret; |
656 | ||
7b9cc738 VD |
657 | /* Port's PHY and MAC both need to be EEE capable */ |
658 | if (!p->phy) | |
659 | return -ENODEV; | |
660 | ||
08f50061 | 661 | if (!ds->ops->set_mac_eee) |
7905288f FF |
662 | return -EOPNOTSUPP; |
663 | ||
08f50061 | 664 | ret = ds->ops->set_mac_eee(ds, p->dp->index, e); |
7905288f FF |
665 | if (ret) |
666 | return ret; | |
667 | ||
c48f7eb3 VD |
668 | if (e->eee_enabled) { |
669 | ret = phy_init_eee(p->phy, 0); | |
670 | if (ret) | |
671 | return ret; | |
672 | } | |
673 | ||
7b9cc738 | 674 | return phy_ethtool_set_eee(p->phy, e); |
7905288f FF |
675 | } |
676 | ||
677 | static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) | |
678 | { | |
679 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 680 | struct dsa_switch *ds = p->dp->ds; |
7905288f FF |
681 | int ret; |
682 | ||
7b9cc738 VD |
683 | /* Port's PHY and MAC both need to be EEE capable */ |
684 | if (!p->phy) | |
685 | return -ENODEV; | |
686 | ||
08f50061 | 687 | if (!ds->ops->get_mac_eee) |
7905288f FF |
688 | return -EOPNOTSUPP; |
689 | ||
08f50061 | 690 | ret = ds->ops->get_mac_eee(ds, p->dp->index, e); |
7905288f FF |
691 | if (ret) |
692 | return ret; | |
693 | ||
7b9cc738 | 694 | return phy_ethtool_get_eee(p->phy, e); |
7905288f FF |
695 | } |
696 | ||
04ff53f9 FF |
697 | #ifdef CONFIG_NET_POLL_CONTROLLER |
698 | static int dsa_slave_netpoll_setup(struct net_device *dev, | |
699 | struct netpoll_info *ni) | |
700 | { | |
701 | struct dsa_slave_priv *p = netdev_priv(dev); | |
6d3c8c0d | 702 | struct net_device *master = dsa_master_netdev(p); |
04ff53f9 FF |
703 | struct netpoll *netpoll; |
704 | int err = 0; | |
705 | ||
706 | netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); | |
707 | if (!netpoll) | |
708 | return -ENOMEM; | |
709 | ||
710 | err = __netpoll_setup(netpoll, master); | |
711 | if (err) { | |
712 | kfree(netpoll); | |
713 | goto out; | |
714 | } | |
715 | ||
716 | p->netpoll = netpoll; | |
717 | out: | |
718 | return err; | |
719 | } | |
720 | ||
721 | static void dsa_slave_netpoll_cleanup(struct net_device *dev) | |
722 | { | |
723 | struct dsa_slave_priv *p = netdev_priv(dev); | |
724 | struct netpoll *netpoll = p->netpoll; | |
725 | ||
726 | if (!netpoll) | |
727 | return; | |
728 | ||
729 | p->netpoll = NULL; | |
730 | ||
731 | __netpoll_free_async(netpoll); | |
732 | } | |
733 | ||
734 | static void dsa_slave_poll_controller(struct net_device *dev) | |
735 | { | |
736 | } | |
737 | #endif | |
738 | ||
44bb765c FF |
739 | static int dsa_slave_get_phys_port_name(struct net_device *dev, |
740 | char *name, size_t len) | |
741 | { | |
742 | struct dsa_slave_priv *p = netdev_priv(dev); | |
743 | ||
afdcf151 | 744 | if (snprintf(name, len, "p%d", p->dp->index) >= len) |
44bb765c | 745 | return -EINVAL; |
3a543ef4 FF |
746 | |
747 | return 0; | |
748 | } | |
749 | ||
f50f2127 FF |
750 | static struct dsa_mall_tc_entry * |
751 | dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p, | |
752 | unsigned long cookie) | |
753 | { | |
754 | struct dsa_mall_tc_entry *mall_tc_entry; | |
755 | ||
756 | list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) | |
757 | if (mall_tc_entry->cookie == cookie) | |
758 | return mall_tc_entry; | |
759 | ||
760 | return NULL; | |
761 | } | |
762 | ||
763 | static int dsa_slave_add_cls_matchall(struct net_device *dev, | |
764 | __be16 protocol, | |
765 | struct tc_cls_matchall_offload *cls, | |
766 | bool ingress) | |
767 | { | |
768 | struct dsa_slave_priv *p = netdev_priv(dev); | |
769 | struct dsa_mall_tc_entry *mall_tc_entry; | |
770 | struct dsa_switch *ds = p->dp->ds; | |
771 | struct net *net = dev_net(dev); | |
772 | struct dsa_slave_priv *to_p; | |
773 | struct net_device *to_dev; | |
774 | const struct tc_action *a; | |
775 | int err = -EOPNOTSUPP; | |
776 | LIST_HEAD(actions); | |
777 | int ifindex; | |
778 | ||
779 | if (!ds->ops->port_mirror_add) | |
780 | return err; | |
781 | ||
782 | if (!tc_single_action(cls->exts)) | |
783 | return err; | |
784 | ||
785 | tcf_exts_to_list(cls->exts, &actions); | |
786 | a = list_first_entry(&actions, struct tc_action, list); | |
787 | ||
788 | if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) { | |
789 | struct dsa_mall_mirror_tc_entry *mirror; | |
790 | ||
791 | ifindex = tcf_mirred_ifindex(a); | |
792 | to_dev = __dev_get_by_index(net, ifindex); | |
793 | if (!to_dev) | |
794 | return -EINVAL; | |
795 | ||
796 | if (!dsa_slave_dev_check(to_dev)) | |
797 | return -EOPNOTSUPP; | |
798 | ||
799 | mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); | |
800 | if (!mall_tc_entry) | |
801 | return -ENOMEM; | |
802 | ||
803 | mall_tc_entry->cookie = cls->cookie; | |
804 | mall_tc_entry->type = DSA_PORT_MALL_MIRROR; | |
805 | mirror = &mall_tc_entry->mirror; | |
806 | ||
807 | to_p = netdev_priv(to_dev); | |
808 | ||
809 | mirror->to_local_port = to_p->dp->index; | |
810 | mirror->ingress = ingress; | |
811 | ||
812 | err = ds->ops->port_mirror_add(ds, p->dp->index, mirror, | |
813 | ingress); | |
814 | if (err) { | |
815 | kfree(mall_tc_entry); | |
816 | return err; | |
817 | } | |
818 | ||
819 | list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); | |
820 | } | |
821 | ||
822 | return 0; | |
823 | } | |
824 | ||
825 | static void dsa_slave_del_cls_matchall(struct net_device *dev, | |
826 | struct tc_cls_matchall_offload *cls) | |
827 | { | |
828 | struct dsa_slave_priv *p = netdev_priv(dev); | |
829 | struct dsa_mall_tc_entry *mall_tc_entry; | |
830 | struct dsa_switch *ds = p->dp->ds; | |
831 | ||
832 | if (!ds->ops->port_mirror_del) | |
833 | return; | |
834 | ||
835 | mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie); | |
836 | if (!mall_tc_entry) | |
837 | return; | |
838 | ||
839 | list_del(&mall_tc_entry->list); | |
840 | ||
841 | switch (mall_tc_entry->type) { | |
842 | case DSA_PORT_MALL_MIRROR: | |
843 | ds->ops->port_mirror_del(ds, p->dp->index, | |
844 | &mall_tc_entry->mirror); | |
845 | break; | |
846 | default: | |
847 | WARN_ON(1); | |
848 | } | |
849 | ||
850 | kfree(mall_tc_entry); | |
851 | } | |
852 | ||
853 | static int dsa_slave_setup_tc(struct net_device *dev, u32 handle, | |
a5fcf8a6 JP |
854 | u32 chain_index, __be16 protocol, |
855 | struct tc_to_netdev *tc) | |
f50f2127 FF |
856 | { |
857 | bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS); | |
a5fcf8a6 JP |
858 | |
859 | if (chain_index) | |
860 | return -EOPNOTSUPP; | |
f50f2127 FF |
861 | |
862 | switch (tc->type) { | |
863 | case TC_SETUP_MATCHALL: | |
864 | switch (tc->cls_mall->command) { | |
865 | case TC_CLSMATCHALL_REPLACE: | |
866 | return dsa_slave_add_cls_matchall(dev, protocol, | |
867 | tc->cls_mall, | |
868 | ingress); | |
869 | case TC_CLSMATCHALL_DESTROY: | |
870 | dsa_slave_del_cls_matchall(dev, tc->cls_mall); | |
871 | return 0; | |
872 | } | |
873 | default: | |
a5fcf8a6 | 874 | return -EOPNOTSUPP; |
f50f2127 | 875 | } |
f50f2127 FF |
876 | } |
877 | ||
f613ed66 FF |
878 | static void dsa_slave_get_stats64(struct net_device *dev, |
879 | struct rtnl_link_stats64 *stats) | |
880 | { | |
881 | struct dsa_slave_priv *p = netdev_priv(dev); | |
882 | unsigned int start; | |
883 | ||
884 | netdev_stats_to_stats64(stats, &dev->stats); | |
885 | do { | |
886 | start = u64_stats_fetch_begin_irq(&p->stats64.syncp); | |
887 | stats->tx_packets = p->stats64.tx_packets; | |
888 | stats->tx_bytes = p->stats64.tx_bytes; | |
889 | stats->rx_packets = p->stats64.rx_packets; | |
890 | stats->rx_bytes = p->stats64.rx_bytes; | |
891 | } while (u64_stats_fetch_retry_irq(&p->stats64.syncp, start)); | |
892 | } | |
893 | ||
af42192c FF |
894 | void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops) |
895 | { | |
896 | ops->get_sset_count = dsa_cpu_port_get_sset_count; | |
897 | ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats; | |
898 | ops->get_strings = dsa_cpu_port_get_strings; | |
899 | } | |
900 | ||
bf9f2648 FF |
901 | static int dsa_slave_get_rxnfc(struct net_device *dev, |
902 | struct ethtool_rxnfc *nfc, u32 *rule_locs) | |
903 | { | |
904 | struct dsa_slave_priv *p = netdev_priv(dev); | |
905 | struct dsa_switch *ds = p->dp->ds; | |
906 | ||
907 | if (!ds->ops->get_rxnfc) | |
908 | return -EOPNOTSUPP; | |
909 | ||
910 | return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs); | |
911 | } | |
912 | ||
913 | static int dsa_slave_set_rxnfc(struct net_device *dev, | |
914 | struct ethtool_rxnfc *nfc) | |
915 | { | |
916 | struct dsa_slave_priv *p = netdev_priv(dev); | |
917 | struct dsa_switch *ds = p->dp->ds; | |
918 | ||
919 | if (!ds->ops->set_rxnfc) | |
920 | return -EOPNOTSUPP; | |
921 | ||
922 | return ds->ops->set_rxnfc(ds, p->dp->index, nfc); | |
923 | } | |
924 | ||
91da11f8 | 925 | static const struct ethtool_ops dsa_slave_ethtool_ops = { |
91da11f8 | 926 | .get_drvinfo = dsa_slave_get_drvinfo, |
3d762a0f GR |
927 | .get_regs_len = dsa_slave_get_regs_len, |
928 | .get_regs = dsa_slave_get_regs, | |
91da11f8 LB |
929 | .nway_reset = dsa_slave_nway_reset, |
930 | .get_link = dsa_slave_get_link, | |
6793abb4 GR |
931 | .get_eeprom_len = dsa_slave_get_eeprom_len, |
932 | .get_eeprom = dsa_slave_get_eeprom, | |
933 | .set_eeprom = dsa_slave_set_eeprom, | |
91da11f8 LB |
934 | .get_strings = dsa_slave_get_strings, |
935 | .get_ethtool_stats = dsa_slave_get_ethtool_stats, | |
936 | .get_sset_count = dsa_slave_get_sset_count, | |
19e57c4e FF |
937 | .set_wol = dsa_slave_set_wol, |
938 | .get_wol = dsa_slave_get_wol, | |
7905288f FF |
939 | .set_eee = dsa_slave_set_eee, |
940 | .get_eee = dsa_slave_get_eee, | |
bb10bb3e PR |
941 | .get_link_ksettings = dsa_slave_get_link_ksettings, |
942 | .set_link_ksettings = dsa_slave_set_link_ksettings, | |
bf9f2648 FF |
943 | .get_rxnfc = dsa_slave_get_rxnfc, |
944 | .set_rxnfc = dsa_slave_set_rxnfc, | |
91da11f8 LB |
945 | }; |
946 | ||
3e8a72d1 | 947 | static const struct net_device_ops dsa_slave_netdev_ops = { |
d442ad4a SH |
948 | .ndo_open = dsa_slave_open, |
949 | .ndo_stop = dsa_slave_close, | |
3e8a72d1 | 950 | .ndo_start_xmit = dsa_slave_xmit, |
d442ad4a SH |
951 | .ndo_change_rx_flags = dsa_slave_change_rx_flags, |
952 | .ndo_set_rx_mode = dsa_slave_set_rx_mode, | |
d442ad4a | 953 | .ndo_set_mac_address = dsa_slave_set_mac_address, |
ba14d9eb VD |
954 | .ndo_fdb_add = switchdev_port_fdb_add, |
955 | .ndo_fdb_del = switchdev_port_fdb_del, | |
956 | .ndo_fdb_dump = switchdev_port_fdb_dump, | |
d442ad4a | 957 | .ndo_do_ioctl = dsa_slave_ioctl, |
abd2be00 | 958 | .ndo_get_iflink = dsa_slave_get_iflink, |
04ff53f9 FF |
959 | #ifdef CONFIG_NET_POLL_CONTROLLER |
960 | .ndo_netpoll_setup = dsa_slave_netpoll_setup, | |
961 | .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, | |
962 | .ndo_poll_controller = dsa_slave_poll_controller, | |
963 | #endif | |
11149536 VD |
964 | .ndo_bridge_getlink = switchdev_port_bridge_getlink, |
965 | .ndo_bridge_setlink = switchdev_port_bridge_setlink, | |
966 | .ndo_bridge_dellink = switchdev_port_bridge_dellink, | |
44bb765c | 967 | .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, |
f50f2127 | 968 | .ndo_setup_tc = dsa_slave_setup_tc, |
f613ed66 | 969 | .ndo_get_stats64 = dsa_slave_get_stats64, |
98237d43 SF |
970 | }; |
971 | ||
9d47c0a2 | 972 | static const struct switchdev_ops dsa_slave_switchdev_ops = { |
f8e20a9f | 973 | .switchdev_port_attr_get = dsa_slave_port_attr_get, |
35636062 | 974 | .switchdev_port_attr_set = dsa_slave_port_attr_set, |
ba14d9eb VD |
975 | .switchdev_port_obj_add = dsa_slave_port_obj_add, |
976 | .switchdev_port_obj_del = dsa_slave_port_obj_del, | |
977 | .switchdev_port_obj_dump = dsa_slave_port_obj_dump, | |
d442ad4a | 978 | }; |
91da11f8 | 979 | |
f37db85d FF |
980 | static struct device_type dsa_type = { |
981 | .name = "dsa", | |
982 | }; | |
983 | ||
0d8bcdd3 FF |
984 | static void dsa_slave_adjust_link(struct net_device *dev) |
985 | { | |
986 | struct dsa_slave_priv *p = netdev_priv(dev); | |
afdcf151 | 987 | struct dsa_switch *ds = p->dp->ds; |
0d8bcdd3 FF |
988 | unsigned int status_changed = 0; |
989 | ||
990 | if (p->old_link != p->phy->link) { | |
991 | status_changed = 1; | |
992 | p->old_link = p->phy->link; | |
993 | } | |
994 | ||
995 | if (p->old_duplex != p->phy->duplex) { | |
996 | status_changed = 1; | |
997 | p->old_duplex = p->phy->duplex; | |
998 | } | |
999 | ||
1000 | if (p->old_pause != p->phy->pause) { | |
1001 | status_changed = 1; | |
1002 | p->old_pause = p->phy->pause; | |
1003 | } | |
1004 | ||
9d490b4e | 1005 | if (ds->ops->adjust_link && status_changed) |
afdcf151 | 1006 | ds->ops->adjust_link(ds, p->dp->index, p->phy); |
ec9436ba | 1007 | |
0d8bcdd3 FF |
1008 | if (status_changed) |
1009 | phy_print_status(p->phy); | |
1010 | } | |
1011 | ||
ce31b31c FF |
1012 | static int dsa_slave_fixed_link_update(struct net_device *dev, |
1013 | struct fixed_phy_status *status) | |
1014 | { | |
b71be352 AL |
1015 | struct dsa_slave_priv *p; |
1016 | struct dsa_switch *ds; | |
1017 | ||
1018 | if (dev) { | |
1019 | p = netdev_priv(dev); | |
afdcf151 | 1020 | ds = p->dp->ds; |
9d490b4e | 1021 | if (ds->ops->fixed_link_update) |
afdcf151 | 1022 | ds->ops->fixed_link_update(ds, p->dp->index, status); |
b71be352 | 1023 | } |
ce31b31c FF |
1024 | |
1025 | return 0; | |
1026 | } | |
1027 | ||
91da11f8 | 1028 | /* slave device setup *******************************************************/ |
c305c165 | 1029 | static int dsa_slave_phy_connect(struct dsa_slave_priv *p, |
cd28a1a9 FF |
1030 | struct net_device *slave_dev, |
1031 | int addr) | |
c305c165 | 1032 | { |
afdcf151 | 1033 | struct dsa_switch *ds = p->dp->ds; |
c305c165 | 1034 | |
7f854420 | 1035 | p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr); |
d25b8e74 RK |
1036 | if (!p->phy) { |
1037 | netdev_err(slave_dev, "no phy at %d\n", addr); | |
c305c165 | 1038 | return -ENODEV; |
d25b8e74 | 1039 | } |
c305c165 FF |
1040 | |
1041 | /* Use already configured phy mode */ | |
211c504a FF |
1042 | if (p->phy_interface == PHY_INTERFACE_MODE_NA) |
1043 | p->phy_interface = p->phy->interface; | |
4078b76c FF |
1044 | return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, |
1045 | p->phy_interface); | |
c305c165 FF |
1046 | } |
1047 | ||
9697f1cd | 1048 | static int dsa_slave_phy_setup(struct dsa_slave_priv *p, |
0d8bcdd3 FF |
1049 | struct net_device *slave_dev) |
1050 | { | |
afdcf151 | 1051 | struct dsa_switch *ds = p->dp->ds; |
0d8bcdd3 | 1052 | struct device_node *phy_dn, *port_dn; |
ce31b31c | 1053 | bool phy_is_fixed = false; |
6819563e | 1054 | u32 phy_flags = 0; |
19334920 | 1055 | int mode, ret; |
0d8bcdd3 | 1056 | |
afdcf151 | 1057 | port_dn = p->dp->dn; |
19334920 GR |
1058 | mode = of_get_phy_mode(port_dn); |
1059 | if (mode < 0) | |
1060 | mode = PHY_INTERFACE_MODE_NA; | |
1061 | p->phy_interface = mode; | |
0d8bcdd3 FF |
1062 | |
1063 | phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); | |
0d8f3c67 | 1064 | if (!phy_dn && of_phy_is_fixed_link(port_dn)) { |
0d8bcdd3 FF |
1065 | /* In the case of a fixed PHY, the DT node associated |
1066 | * to the fixed PHY is the Port DT node | |
1067 | */ | |
1068 | ret = of_phy_register_fixed_link(port_dn); | |
1069 | if (ret) { | |
d25b8e74 | 1070 | netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret); |
9697f1cd | 1071 | return ret; |
0d8bcdd3 | 1072 | } |
ce31b31c | 1073 | phy_is_fixed = true; |
0d8f3c67 | 1074 | phy_dn = of_node_get(port_dn); |
0d8bcdd3 FF |
1075 | } |
1076 | ||
9d490b4e | 1077 | if (ds->ops->get_phy_flags) |
afdcf151 | 1078 | phy_flags = ds->ops->get_phy_flags(ds, p->dp->index); |
6819563e | 1079 | |
cd28a1a9 | 1080 | if (phy_dn) { |
d25b8e74 RK |
1081 | int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn); |
1082 | ||
cd28a1a9 FF |
1083 | /* If this PHY address is part of phys_mii_mask, which means |
1084 | * that we need to divert reads and writes to/from it, then we | |
1085 | * want to bind this device using the slave MII bus created by | |
1086 | * DSA to make that happen. | |
1087 | */ | |
d25b8e74 RK |
1088 | if (!phy_is_fixed && phy_id >= 0 && |
1089 | (ds->phys_mii_mask & (1 << phy_id))) { | |
1090 | ret = dsa_slave_phy_connect(p, slave_dev, phy_id); | |
1091 | if (ret) { | |
1092 | netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret); | |
0d8f3c67 | 1093 | of_node_put(phy_dn); |
cd28a1a9 | 1094 | return ret; |
d25b8e74 | 1095 | } |
cd28a1a9 FF |
1096 | } else { |
1097 | p->phy = of_phy_connect(slave_dev, phy_dn, | |
1098 | dsa_slave_adjust_link, | |
1099 | phy_flags, | |
1100 | p->phy_interface); | |
1101 | } | |
0d8f3c67 JH |
1102 | |
1103 | of_node_put(phy_dn); | |
cd28a1a9 | 1104 | } |
0d8bcdd3 | 1105 | |
ce31b31c FF |
1106 | if (p->phy && phy_is_fixed) |
1107 | fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); | |
1108 | ||
0d8bcdd3 FF |
1109 | /* We could not connect to a designated PHY, so use the switch internal |
1110 | * MDIO bus instead | |
1111 | */ | |
b31f65fb | 1112 | if (!p->phy) { |
afdcf151 | 1113 | ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index); |
d25b8e74 | 1114 | if (ret) { |
afdcf151 VD |
1115 | netdev_err(slave_dev, "failed to connect to port %d: %d\n", |
1116 | p->dp->index, ret); | |
881eadab JH |
1117 | if (phy_is_fixed) |
1118 | of_phy_deregister_fixed_link(port_dn); | |
c305c165 | 1119 | return ret; |
d25b8e74 | 1120 | } |
b31f65fb | 1121 | } |
9697f1cd | 1122 | |
2220943a AL |
1123 | phy_attached_info(p->phy); |
1124 | ||
9697f1cd | 1125 | return 0; |
0d8bcdd3 FF |
1126 | } |
1127 | ||
448b4482 AL |
1128 | static struct lock_class_key dsa_slave_netdev_xmit_lock_key; |
1129 | static void dsa_slave_set_lockdep_class_one(struct net_device *dev, | |
1130 | struct netdev_queue *txq, | |
1131 | void *_unused) | |
1132 | { | |
1133 | lockdep_set_class(&txq->_xmit_lock, | |
1134 | &dsa_slave_netdev_xmit_lock_key); | |
1135 | } | |
1136 | ||
24462549 FF |
1137 | int dsa_slave_suspend(struct net_device *slave_dev) |
1138 | { | |
1139 | struct dsa_slave_priv *p = netdev_priv(slave_dev); | |
1140 | ||
f154be24 FF |
1141 | netif_device_detach(slave_dev); |
1142 | ||
24462549 FF |
1143 | if (p->phy) { |
1144 | phy_stop(p->phy); | |
1145 | p->old_pause = -1; | |
1146 | p->old_link = -1; | |
1147 | p->old_duplex = -1; | |
1148 | phy_suspend(p->phy); | |
1149 | } | |
1150 | ||
1151 | return 0; | |
1152 | } | |
1153 | ||
1154 | int dsa_slave_resume(struct net_device *slave_dev) | |
1155 | { | |
1156 | struct dsa_slave_priv *p = netdev_priv(slave_dev); | |
1157 | ||
1158 | netif_device_attach(slave_dev); | |
1159 | ||
1160 | if (p->phy) { | |
1161 | phy_resume(p->phy); | |
1162 | phy_start(p->phy); | |
1163 | } | |
1164 | ||
1165 | return 0; | |
1166 | } | |
1167 | ||
d87d6f44 | 1168 | int dsa_slave_create(struct dsa_switch *ds, struct device *parent, |
83c0afae | 1169 | int port, const char *name) |
91da11f8 | 1170 | { |
badf3ada | 1171 | struct dsa_switch_tree *dst = ds->dst; |
83c0afae | 1172 | struct net_device *master; |
91da11f8 LB |
1173 | struct net_device *slave_dev; |
1174 | struct dsa_slave_priv *p; | |
a29342e7 | 1175 | struct dsa_port *cpu_dp; |
91da11f8 LB |
1176 | int ret; |
1177 | ||
a29342e7 FF |
1178 | cpu_dp = ds->dst->cpu_dp; |
1179 | master = cpu_dp->netdev; | |
83c0afae | 1180 | |
c835a677 TG |
1181 | slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, |
1182 | NET_NAME_UNKNOWN, ether_setup); | |
91da11f8 | 1183 | if (slave_dev == NULL) |
d87d6f44 | 1184 | return -ENOMEM; |
91da11f8 | 1185 | |
f50f2127 FF |
1186 | slave_dev->features = master->vlan_features | NETIF_F_HW_TC; |
1187 | slave_dev->hw_features |= NETIF_F_HW_TC; | |
7ad24ea4 | 1188 | slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; |
2fcc8005 | 1189 | eth_hw_addr_inherit(slave_dev, master); |
0a5f107b | 1190 | slave_dev->priv_flags |= IFF_NO_QUEUE; |
3e8a72d1 | 1191 | slave_dev->netdev_ops = &dsa_slave_netdev_ops; |
9d47c0a2 | 1192 | slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; |
8b1efc0f JW |
1193 | slave_dev->min_mtu = 0; |
1194 | slave_dev->max_mtu = ETH_MAX_MTU; | |
f37db85d | 1195 | SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); |
d442ad4a | 1196 | |
448b4482 AL |
1197 | netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, |
1198 | NULL); | |
1199 | ||
5075314e | 1200 | SET_NETDEV_DEV(slave_dev, parent); |
189b0d93 | 1201 | slave_dev->dev.of_node = ds->ports[port].dn; |
5075314e AD |
1202 | slave_dev->vlan_features = master->vlan_features; |
1203 | ||
1204 | p = netdev_priv(slave_dev); | |
f613ed66 | 1205 | u64_stats_init(&p->stats64.syncp); |
afdcf151 | 1206 | p->dp = &ds->ports[port]; |
f50f2127 | 1207 | INIT_LIST_HEAD(&p->mall_tc_list); |
39a7f2a4 | 1208 | p->xmit = dst->tag_ops->xmit; |
d442ad4a | 1209 | |
0d8bcdd3 FF |
1210 | p->old_pause = -1; |
1211 | p->old_link = -1; | |
1212 | p->old_duplex = -1; | |
1213 | ||
c8b09808 | 1214 | ds->ports[port].netdev = slave_dev; |
91da11f8 LB |
1215 | ret = register_netdev(slave_dev); |
1216 | if (ret) { | |
a2ae6007 JP |
1217 | netdev_err(master, "error %d registering interface %s\n", |
1218 | ret, slave_dev->name); | |
c8b09808 | 1219 | ds->ports[port].netdev = NULL; |
91da11f8 | 1220 | free_netdev(slave_dev); |
d87d6f44 | 1221 | return ret; |
91da11f8 LB |
1222 | } |
1223 | ||
1224 | netif_carrier_off(slave_dev); | |
1225 | ||
0071f56e AL |
1226 | ret = dsa_slave_phy_setup(p, slave_dev); |
1227 | if (ret) { | |
1228 | netdev_err(master, "error %d setting up slave phy\n", ret); | |
73dcb556 | 1229 | unregister_netdev(slave_dev); |
0071f56e AL |
1230 | free_netdev(slave_dev); |
1231 | return ret; | |
1232 | } | |
1233 | ||
d87d6f44 | 1234 | return 0; |
91da11f8 | 1235 | } |
b73adef6 | 1236 | |
cda5c15b NA |
1237 | void dsa_slave_destroy(struct net_device *slave_dev) |
1238 | { | |
1239 | struct dsa_slave_priv *p = netdev_priv(slave_dev); | |
881eadab JH |
1240 | struct device_node *port_dn; |
1241 | ||
afdcf151 | 1242 | port_dn = p->dp->dn; |
cda5c15b NA |
1243 | |
1244 | netif_carrier_off(slave_dev); | |
881eadab | 1245 | if (p->phy) { |
cda5c15b | 1246 | phy_disconnect(p->phy); |
881eadab JH |
1247 | |
1248 | if (of_phy_is_fixed_link(port_dn)) | |
1249 | of_phy_deregister_fixed_link(port_dn); | |
1250 | } | |
cda5c15b NA |
1251 | unregister_netdev(slave_dev); |
1252 | free_netdev(slave_dev); | |
1253 | } | |
1254 | ||
b73adef6 FF |
1255 | static bool dsa_slave_dev_check(struct net_device *dev) |
1256 | { | |
1257 | return dev->netdev_ops == &dsa_slave_netdev_ops; | |
1258 | } | |
1259 | ||
8e92ab3a VD |
1260 | static int dsa_slave_changeupper(struct net_device *dev, |
1261 | struct netdev_notifier_changeupper_info *info) | |
b73adef6 | 1262 | { |
17d7802b VD |
1263 | struct dsa_slave_priv *p = netdev_priv(dev); |
1264 | struct dsa_port *dp = p->dp; | |
8e92ab3a | 1265 | int err = NOTIFY_DONE; |
b73adef6 | 1266 | |
8e92ab3a VD |
1267 | if (netif_is_bridge_master(info->upper_dev)) { |
1268 | if (info->linking) { | |
17d7802b | 1269 | err = dsa_port_bridge_join(dp, info->upper_dev); |
8e92ab3a VD |
1270 | err = notifier_from_errno(err); |
1271 | } else { | |
17d7802b | 1272 | dsa_port_bridge_leave(dp, info->upper_dev); |
8e92ab3a | 1273 | err = NOTIFY_OK; |
6debb68a | 1274 | } |
6debb68a | 1275 | } |
b73adef6 | 1276 | |
8e92ab3a | 1277 | return err; |
6debb68a | 1278 | } |
b73adef6 | 1279 | |
88e4f0ca VD |
1280 | static int dsa_slave_netdevice_event(struct notifier_block *nb, |
1281 | unsigned long event, void *ptr) | |
6debb68a VD |
1282 | { |
1283 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
1284 | ||
8e92ab3a VD |
1285 | if (dev->netdev_ops != &dsa_slave_netdev_ops) |
1286 | return NOTIFY_DONE; | |
1287 | ||
1288 | if (event == NETDEV_CHANGEUPPER) | |
1289 | return dsa_slave_changeupper(dev, ptr); | |
b73adef6 | 1290 | |
b73adef6 FF |
1291 | return NOTIFY_DONE; |
1292 | } | |
88e4f0ca VD |
1293 | |
1294 | static struct notifier_block dsa_slave_nb __read_mostly = { | |
1295 | .notifier_call = dsa_slave_netdevice_event, | |
1296 | }; | |
1297 | ||
1298 | int dsa_slave_register_notifier(void) | |
1299 | { | |
1300 | return register_netdevice_notifier(&dsa_slave_nb); | |
1301 | } | |
1302 | ||
1303 | void dsa_slave_unregister_notifier(void) | |
1304 | { | |
1305 | int err; | |
1306 | ||
1307 | err = unregister_netdevice_notifier(&dsa_slave_nb); | |
1308 | if (err) | |
1309 | pr_err("DSA: failed to unregister slave notifier (%d)\n", err); | |
1310 | } |