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