]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/dsa/port.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / net / dsa / port.c
CommitLineData
a40c175b
VD
1/*
2 * Handling of a single switch port
3 *
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/if_bridge.h>
cfbed329 14#include <linux/notifier.h>
57ab1ca2
VD
15#include <linux/of_mdio.h>
16#include <linux/of_net.h>
a40c175b
VD
17
18#include "dsa_priv.h"
19
bb9f6031 20static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
cfbed329
VD
21{
22 struct raw_notifier_head *nh = &dp->ds->dst->nh;
23 int err;
24
25 err = raw_notifier_call_chain(nh, e, v);
26
27 return notifier_to_errno(err);
28}
29
a40c175b
VD
30int dsa_port_set_state(struct dsa_port *dp, u8 state,
31 struct switchdev_trans *trans)
32{
33 struct dsa_switch *ds = dp->ds;
34 int port = dp->index;
35
36 if (switchdev_trans_ph_prepare(trans))
37 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
38
39 if (ds->ops->port_stp_state_set)
40 ds->ops->port_stp_state_set(ds, port, state);
41
42 if (ds->ops->port_fast_age) {
43 /* Fast age FDB entries or flush appropriate forwarding database
44 * for the given port, if we are moving it from Learning or
45 * Forwarding state, to Disabled or Blocking or Listening state.
46 */
47
48 if ((dp->stp_state == BR_STATE_LEARNING ||
49 dp->stp_state == BR_STATE_FORWARDING) &&
50 (state == BR_STATE_DISABLED ||
51 state == BR_STATE_BLOCKING ||
52 state == BR_STATE_LISTENING))
53 ds->ops->port_fast_age(ds, port);
54 }
55
56 dp->stp_state = state;
57
58 return 0;
59}
60
fb8a6a2b 61static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
a40c175b
VD
62{
63 int err;
64
65 err = dsa_port_set_state(dp, state, NULL);
66 if (err)
67 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
68}
cfbed329 69
fb8a6a2b
VD
70int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
71{
fb8a6a2b
VD
72 struct dsa_switch *ds = dp->ds;
73 int port = dp->index;
74 int err;
75
76 if (ds->ops->port_enable) {
77 err = ds->ops->port_enable(ds, port, phy);
78 if (err)
79 return err;
80 }
81
a9c2e654
RK
82 if (!dp->bridge_dev)
83 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
fb8a6a2b
VD
84
85 return 0;
86}
87
88void dsa_port_disable(struct dsa_port *dp, struct phy_device *phy)
89{
90 struct dsa_switch *ds = dp->ds;
91 int port = dp->index;
92
a9c2e654
RK
93 if (!dp->bridge_dev)
94 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
fb8a6a2b
VD
95
96 if (ds->ops->port_disable)
97 ds->ops->port_disable(ds, port, phy);
98}
99
cfbed329
VD
100int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
101{
102 struct dsa_notifier_bridge_info info = {
103 .sw_index = dp->ds->index,
104 .port = dp->index,
105 .br = br,
106 };
107 int err;
108
109 /* Here the port is already bridged. Reflect the current configuration
110 * so that drivers can program their chips accordingly.
111 */
112 dp->bridge_dev = br;
113
114 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
115
116 /* The bridging is rolled back on error */
117 if (err)
118 dp->bridge_dev = NULL;
119
120 return err;
121}
122
123void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
124{
125 struct dsa_notifier_bridge_info info = {
126 .sw_index = dp->ds->index,
127 .port = dp->index,
128 .br = br,
129 };
130 int err;
131
132 /* Here the port is already unbridged. Reflect the current configuration
133 * so that drivers can program their chips accordingly.
134 */
135 dp->bridge_dev = NULL;
136
137 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
138 if (err)
139 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
140
141 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
142 * so allow it to be in BR_STATE_FORWARDING to be kept functional
143 */
144 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
145}
4d61d304
VD
146
147int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
148 struct switchdev_trans *trans)
149{
150 struct dsa_switch *ds = dp->ds;
151
152 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
153 if (switchdev_trans_ph_prepare(trans))
154 return 0;
155
156 if (ds->ops->port_vlan_filtering)
157 return ds->ops->port_vlan_filtering(ds, dp->index,
158 vlan_filtering);
159
160 return 0;
161}
d87bd94e 162
d87bd94e
VD
163int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
164 struct switchdev_trans *trans)
165{
166 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
167 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
1faabf74
VD
168 struct dsa_notifier_ageing_time_info info = {
169 .ageing_time = ageing_time,
1faabf74
VD
170 .trans = trans,
171 };
d87bd94e 172
1faabf74
VD
173 if (switchdev_trans_ph_prepare(trans))
174 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
d87bd94e 175
d87bd94e 176 dp->ageing_time = ageing_time;
d87bd94e 177
1faabf74 178 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
d87bd94e 179}
d1cffff0 180
2acf4e6a
AS
181int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
182 u16 vid)
d1cffff0 183{
685fb6a4
VD
184 struct dsa_notifier_fdb_info info = {
185 .sw_index = dp->ds->index,
186 .port = dp->index,
2acf4e6a
AS
187 .addr = addr,
188 .vid = vid,
685fb6a4 189 };
d1cffff0 190
685fb6a4 191 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
d1cffff0
VD
192}
193
2acf4e6a
AS
194int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
195 u16 vid)
d1cffff0 196{
685fb6a4
VD
197 struct dsa_notifier_fdb_info info = {
198 .sw_index = dp->ds->index,
199 .port = dp->index,
2acf4e6a
AS
200 .addr = addr,
201 .vid = vid,
202
685fb6a4 203 };
d1cffff0 204
685fb6a4 205 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
d1cffff0
VD
206}
207
de40fc5d
VD
208int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
209{
210 struct dsa_switch *ds = dp->ds;
211 int port = dp->index;
212
213 if (!ds->ops->port_fdb_dump)
214 return -EOPNOTSUPP;
215
216 return ds->ops->port_fdb_dump(ds, port, cb, data);
217}
218
bb9f6031 219int dsa_port_mdb_add(const struct dsa_port *dp,
3a9afea3
VD
220 const struct switchdev_obj_port_mdb *mdb,
221 struct switchdev_trans *trans)
222{
8ae5bcdc
VD
223 struct dsa_notifier_mdb_info info = {
224 .sw_index = dp->ds->index,
225 .port = dp->index,
226 .trans = trans,
227 .mdb = mdb,
228 };
3a9afea3 229
8ae5bcdc 230 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
3a9afea3
VD
231}
232
bb9f6031 233int dsa_port_mdb_del(const struct dsa_port *dp,
3a9afea3
VD
234 const struct switchdev_obj_port_mdb *mdb)
235{
8ae5bcdc
VD
236 struct dsa_notifier_mdb_info info = {
237 .sw_index = dp->ds->index,
238 .port = dp->index,
239 .mdb = mdb,
240 };
3a9afea3 241
8ae5bcdc 242 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
3a9afea3
VD
243}
244
076e7133
VD
245int dsa_port_vlan_add(struct dsa_port *dp,
246 const struct switchdev_obj_port_vlan *vlan,
247 struct switchdev_trans *trans)
248{
d0c627b8
VD
249 struct dsa_notifier_vlan_info info = {
250 .sw_index = dp->ds->index,
251 .port = dp->index,
252 .trans = trans,
253 .vlan = vlan,
254 };
076e7133 255
2ea7a679
AL
256 if (br_vlan_enabled(dp->bridge_dev))
257 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
258
259 return 0;
076e7133
VD
260}
261
262int dsa_port_vlan_del(struct dsa_port *dp,
263 const struct switchdev_obj_port_vlan *vlan)
264{
d0c627b8
VD
265 struct dsa_notifier_vlan_info info = {
266 .sw_index = dp->ds->index,
267 .port = dp->index,
268 .vlan = vlan,
269 };
076e7133 270
2ea7a679
AL
271 if (br_vlan_enabled(dp->bridge_dev))
272 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
273
274 return 0;
076e7133 275}
57ab1ca2
VD
276
277int dsa_port_fixed_link_register_of(struct dsa_port *dp)
278{
279 struct device_node *dn = dp->dn;
280 struct dsa_switch *ds = dp->ds;
281 struct phy_device *phydev;
282 int port = dp->index;
283 int mode;
284 int err;
285
286 if (of_phy_is_fixed_link(dn)) {
287 err = of_phy_register_fixed_link(dn);
288 if (err) {
289 dev_err(ds->dev,
290 "failed to register the fixed PHY of port %d\n",
291 port);
292 return err;
293 }
294
295 phydev = of_phy_find_device(dn);
296
297 mode = of_get_phy_mode(dn);
298 if (mode < 0)
299 mode = PHY_INTERFACE_MODE_NA;
300 phydev->interface = mode;
301
302 genphy_config_init(phydev);
303 genphy_read_status(phydev);
304
305 if (ds->ops->adjust_link)
306 ds->ops->adjust_link(ds, port, phydev);
307
308 put_device(&phydev->mdio.dev);
309 }
310
311 return 0;
312}
313
314void dsa_port_fixed_link_unregister_of(struct dsa_port *dp)
315{
316 struct device_node *dn = dp->dn;
317
318 if (of_phy_is_fixed_link(dn))
319 of_phy_deregister_fixed_link(dn);
320}