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