blob: e23ece229c7ed6c3184a304d5b7d873b7309c55d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vivien Didelota40c1752017-05-19 17:00:44 -04002/*
3 * Handling of a single switch port
4 *
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelota40c1752017-05-19 17:00:44 -04007 */
8
9#include <linux/if_bridge.h>
Vivien Didelotcfbed322017-05-19 17:00:45 -040010#include <linux/notifier.h>
Vivien Didelot57ab1ca2017-10-26 10:50:07 -040011#include <linux/of_mdio.h>
12#include <linux/of_net.h>
Vivien Didelota40c1752017-05-19 17:00:44 -040013
14#include "dsa_priv.h"
15
Vladimir Olteanf66a6a62020-05-10 19:37:41 +030016static int dsa_broadcast(unsigned long e, void *v)
17{
18 struct dsa_switch_tree *dst;
19 int err = 0;
20
21 list_for_each_entry(dst, &dsa_tree_list, list) {
22 struct raw_notifier_head *nh = &dst->nh;
23
24 err = raw_notifier_call_chain(nh, e, v);
25 err = notifier_to_errno(err);
26 if (err)
27 break;
28 }
29
30 return err;
31}
32
Andrew Lunnbb9f603172017-11-09 23:11:01 +010033static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
Vivien Didelotcfbed322017-05-19 17:00:45 -040034{
35 struct raw_notifier_head *nh = &dp->ds->dst->nh;
36 int err;
37
38 err = raw_notifier_call_chain(nh, e, v);
39
40 return notifier_to_errno(err);
41}
42
Vivien Didelota40c1752017-05-19 17:00:44 -040043int dsa_port_set_state(struct dsa_port *dp, u8 state,
44 struct switchdev_trans *trans)
45{
46 struct dsa_switch *ds = dp->ds;
47 int port = dp->index;
48
49 if (switchdev_trans_ph_prepare(trans))
50 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
51
52 if (ds->ops->port_stp_state_set)
53 ds->ops->port_stp_state_set(ds, port, state);
54
55 if (ds->ops->port_fast_age) {
56 /* Fast age FDB entries or flush appropriate forwarding database
57 * for the given port, if we are moving it from Learning or
58 * Forwarding state, to Disabled or Blocking or Listening state.
59 */
60
61 if ((dp->stp_state == BR_STATE_LEARNING ||
62 dp->stp_state == BR_STATE_FORWARDING) &&
63 (state == BR_STATE_DISABLED ||
64 state == BR_STATE_BLOCKING ||
65 state == BR_STATE_LISTENING))
66 ds->ops->port_fast_age(ds, port);
67 }
68
69 dp->stp_state = state;
70
71 return 0;
72}
73
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040074static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
Vivien Didelota40c1752017-05-19 17:00:44 -040075{
76 int err;
77
78 err = dsa_port_set_state(dp, state, NULL);
79 if (err)
80 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
81}
Vivien Didelotcfbed322017-05-19 17:00:45 -040082
Russell King8640f8d2020-03-03 15:01:46 +000083int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040084{
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040085 struct dsa_switch *ds = dp->ds;
86 int port = dp->index;
87 int err;
88
89 if (ds->ops->port_enable) {
90 err = ds->ops->port_enable(ds, port, phy);
91 if (err)
92 return err;
93 }
94
Russell King9c2054a2019-02-20 10:32:52 +000095 if (!dp->bridge_dev)
96 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040097
Russell King8640f8d2020-03-03 15:01:46 +000098 if (dp->pl)
99 phylink_start(dp->pl);
100
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400101 return 0;
102}
103
Russell King8640f8d2020-03-03 15:01:46 +0000104int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
105{
106 int err;
107
108 rtnl_lock();
109 err = dsa_port_enable_rt(dp, phy);
110 rtnl_unlock();
111
112 return err;
113}
114
115void dsa_port_disable_rt(struct dsa_port *dp)
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400116{
117 struct dsa_switch *ds = dp->ds;
118 int port = dp->index;
119
Russell King8640f8d2020-03-03 15:01:46 +0000120 if (dp->pl)
121 phylink_stop(dp->pl);
122
Russell King9c2054a2019-02-20 10:32:52 +0000123 if (!dp->bridge_dev)
124 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400125
126 if (ds->ops->port_disable)
Andrew Lunn75104db2019-02-24 20:44:43 +0100127 ds->ops->port_disable(ds, port);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -0400128}
129
Russell King8640f8d2020-03-03 15:01:46 +0000130void dsa_port_disable(struct dsa_port *dp)
131{
132 rtnl_lock();
133 dsa_port_disable_rt(dp);
134 rtnl_unlock();
135}
136
Vivien Didelotcfbed322017-05-19 17:00:45 -0400137int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
138{
139 struct dsa_notifier_bridge_info info = {
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300140 .tree_index = dp->ds->dst->index,
Vivien Didelotcfbed322017-05-19 17:00:45 -0400141 .sw_index = dp->ds->index,
142 .port = dp->index,
143 .br = br,
144 };
145 int err;
146
Russell Kingc1388062019-02-20 15:35:06 -0800147 /* Set the flooding mode before joining the port in the switch */
148 err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL);
149 if (err)
150 return err;
151
152 /* Here the interface is already bridged. Reflect the current
153 * configuration so that drivers can program their chips accordingly.
Vivien Didelotcfbed322017-05-19 17:00:45 -0400154 */
155 dp->bridge_dev = br;
156
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300157 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
Vivien Didelotcfbed322017-05-19 17:00:45 -0400158
159 /* The bridging is rolled back on error */
Russell Kingc1388062019-02-20 15:35:06 -0800160 if (err) {
161 dsa_port_bridge_flags(dp, 0, NULL);
Vivien Didelotcfbed322017-05-19 17:00:45 -0400162 dp->bridge_dev = NULL;
Russell Kingc1388062019-02-20 15:35:06 -0800163 }
Vivien Didelotcfbed322017-05-19 17:00:45 -0400164
165 return err;
166}
167
168void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
169{
170 struct dsa_notifier_bridge_info info = {
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300171 .tree_index = dp->ds->dst->index,
Vivien Didelotcfbed322017-05-19 17:00:45 -0400172 .sw_index = dp->ds->index,
173 .port = dp->index,
174 .br = br,
175 };
176 int err;
177
178 /* Here the port is already unbridged. Reflect the current configuration
179 * so that drivers can program their chips accordingly.
180 */
181 dp->bridge_dev = NULL;
182
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300183 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
Vivien Didelotcfbed322017-05-19 17:00:45 -0400184 if (err)
185 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
186
Russell Kingc1388062019-02-20 15:35:06 -0800187 /* Port is leaving the bridge, disable flooding */
188 dsa_port_bridge_flags(dp, 0, NULL);
189
Vivien Didelotcfbed322017-05-19 17:00:45 -0400190 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
191 * so allow it to be in BR_STATE_FORWARDING to be kept functional
192 */
193 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
194}
Vivien Didelot4d61d302017-05-19 17:00:46 -0400195
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300196static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
197 bool vlan_filtering)
198{
199 struct dsa_switch *ds = dp->ds;
200 int i;
201
202 if (!ds->vlan_filtering_is_global)
203 return true;
204
205 /* For cases where enabling/disabling VLAN awareness is global to the
206 * switch, we need to handle the case where multiple bridges span
207 * different ports of the same switch device and one of them has a
208 * different setting than what is being requested.
209 */
210 for (i = 0; i < ds->num_ports; i++) {
211 struct net_device *other_bridge;
212
213 other_bridge = dsa_to_port(ds, i)->bridge_dev;
214 if (!other_bridge)
215 continue;
216 /* If it's the same bridge, it also has same
217 * vlan_filtering setting => no need to check
218 */
219 if (other_bridge == dp->bridge_dev)
220 continue;
221 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
222 dev_err(ds->dev, "VLAN filtering is a global setting\n");
223 return false;
224 }
225 }
226 return true;
227}
228
Vivien Didelot4d61d302017-05-19 17:00:46 -0400229int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
230 struct switchdev_trans *trans)
231{
232 struct dsa_switch *ds = dp->ds;
Vladimir Oltean33162e92019-04-28 21:45:43 +0300233 int err;
Vivien Didelot4d61d302017-05-19 17:00:46 -0400234
235 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
236 if (switchdev_trans_ph_prepare(trans))
237 return 0;
238
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300239 if (!ds->ops->port_vlan_filtering)
240 return 0;
241
242 if (!dsa_port_can_apply_vlan_filtering(dp, vlan_filtering))
243 return -EINVAL;
244
Vladimir Olteanec9121e2019-04-28 21:45:51 +0300245 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
246 return 0;
247
Vladimir Oltean8f5d16f2019-04-28 21:45:44 +0300248 err = ds->ops->port_vlan_filtering(ds, dp->index,
249 vlan_filtering);
250 if (err)
251 return err;
252
Vladimir Oltean145746762019-04-28 21:45:48 +0300253 if (ds->vlan_filtering_is_global)
254 ds->vlan_filtering = vlan_filtering;
255 else
256 dp->vlan_filtering = vlan_filtering;
Vivien Didelot4d61d302017-05-19 17:00:46 -0400257 return 0;
258}
Vivien Didelotd87bd942017-05-19 17:00:47 -0400259
Russell King54a0ed02020-05-12 20:20:25 +0300260/* This enforces legacy behavior for switch drivers which assume they can't
261 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
262 */
263bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
264{
265 struct dsa_switch *ds = dp->ds;
266
267 if (!dp->bridge_dev)
268 return false;
269
270 return (!ds->configure_vlan_while_not_filtering &&
271 !br_vlan_enabled(dp->bridge_dev));
272}
273
Vivien Didelotd87bd942017-05-19 17:00:47 -0400274int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
275 struct switchdev_trans *trans)
276{
277 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
278 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
Vivien Didelot1faabf72017-05-19 17:00:52 -0400279 struct dsa_notifier_ageing_time_info info = {
280 .ageing_time = ageing_time,
Vivien Didelot1faabf72017-05-19 17:00:52 -0400281 .trans = trans,
282 };
Vivien Didelotd87bd942017-05-19 17:00:47 -0400283
Vivien Didelot1faabf72017-05-19 17:00:52 -0400284 if (switchdev_trans_ph_prepare(trans))
285 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
Vivien Didelotd87bd942017-05-19 17:00:47 -0400286
Vivien Didelotd87bd942017-05-19 17:00:47 -0400287 dp->ageing_time = ageing_time;
Vivien Didelotd87bd942017-05-19 17:00:47 -0400288
Vivien Didelot1faabf72017-05-19 17:00:52 -0400289 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
Vivien Didelotd87bd942017-05-19 17:00:47 -0400290}
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400291
Florian Fainelliea870052019-02-20 16:58:22 -0800292int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags,
293 struct switchdev_trans *trans)
294{
295 struct dsa_switch *ds = dp->ds;
296
297 if (!ds->ops->port_egress_floods ||
298 (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
299 return -EINVAL;
300
301 return 0;
302}
303
Russell King57652792019-02-20 15:35:04 -0800304int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags,
305 struct switchdev_trans *trans)
306{
307 struct dsa_switch *ds = dp->ds;
308 int port = dp->index;
309 int err = 0;
310
311 if (switchdev_trans_ph_prepare(trans))
312 return 0;
313
314 if (ds->ops->port_egress_floods)
315 err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
316 flags & BR_MCAST_FLOOD);
317
318 return err;
319}
320
Vivien Didelot08cc83c2019-07-08 23:31:13 -0400321int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
322 struct switchdev_trans *trans)
323{
324 struct dsa_switch *ds = dp->ds;
325 int port = dp->index;
326
327 if (switchdev_trans_ph_prepare(trans))
328 return ds->ops->port_egress_floods ? 0 : -EOPNOTSUPP;
329
330 return ds->ops->port_egress_floods(ds, port, true, mrouter);
331}
332
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200333int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
334 bool propagate_upstream)
335{
336 struct dsa_notifier_mtu_info info = {
337 .sw_index = dp->ds->index,
338 .propagate_upstream = propagate_upstream,
339 .port = dp->index,
340 .mtu = new_mtu,
341 };
342
343 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
344}
345
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300346int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
347 u16 vid)
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400348{
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400349 struct dsa_notifier_fdb_info info = {
350 .sw_index = dp->ds->index,
351 .port = dp->index,
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300352 .addr = addr,
353 .vid = vid,
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400354 };
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400355
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400356 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400357}
358
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300359int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
360 u16 vid)
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400361{
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400362 struct dsa_notifier_fdb_info info = {
363 .sw_index = dp->ds->index,
364 .port = dp->index,
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300365 .addr = addr,
366 .vid = vid,
367
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400368 };
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400369
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400370 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
Vivien Didelotd1cffff2017-05-19 17:00:48 -0400371}
372
Vivien Didelotde40fc52017-09-20 19:32:14 -0400373int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
374{
375 struct dsa_switch *ds = dp->ds;
376 int port = dp->index;
377
378 if (!ds->ops->port_fdb_dump)
379 return -EOPNOTSUPP;
380
381 return ds->ops->port_fdb_dump(ds, port, cb, data);
382}
383
Andrew Lunnbb9f603172017-11-09 23:11:01 +0100384int dsa_port_mdb_add(const struct dsa_port *dp,
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400385 const struct switchdev_obj_port_mdb *mdb,
386 struct switchdev_trans *trans)
387{
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400388 struct dsa_notifier_mdb_info info = {
389 .sw_index = dp->ds->index,
390 .port = dp->index,
391 .trans = trans,
392 .mdb = mdb,
393 };
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400394
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400395 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400396}
397
Andrew Lunnbb9f603172017-11-09 23:11:01 +0100398int dsa_port_mdb_del(const struct dsa_port *dp,
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400399 const struct switchdev_obj_port_mdb *mdb)
400{
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400401 struct dsa_notifier_mdb_info info = {
402 .sw_index = dp->ds->index,
403 .port = dp->index,
404 .mdb = mdb,
405 };
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400406
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400407 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
Vivien Didelot3a9afea2017-05-19 17:00:49 -0400408}
409
Vivien Didelot076e7132017-05-19 17:00:50 -0400410int dsa_port_vlan_add(struct dsa_port *dp,
411 const struct switchdev_obj_port_vlan *vlan,
412 struct switchdev_trans *trans)
413{
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400414 struct dsa_notifier_vlan_info info = {
415 .sw_index = dp->ds->index,
416 .port = dp->index,
417 .trans = trans,
418 .vlan = vlan,
419 };
Vivien Didelot076e7132017-05-19 17:00:50 -0400420
Vivien Didelotc5335d72019-08-25 13:25:18 -0400421 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
Vivien Didelot076e7132017-05-19 17:00:50 -0400422}
423
424int dsa_port_vlan_del(struct dsa_port *dp,
425 const struct switchdev_obj_port_vlan *vlan)
426{
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400427 struct dsa_notifier_vlan_info info = {
428 .sw_index = dp->ds->index,
429 .port = dp->index,
430 .vlan = vlan,
431 };
Vivien Didelot076e7132017-05-19 17:00:50 -0400432
Vivien Didelotc5335d72019-08-25 13:25:18 -0400433 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
Vivien Didelot076e7132017-05-19 17:00:50 -0400434}
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400435
Vladimir Oltean314f76d2019-04-28 21:45:54 +0300436int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags)
437{
438 struct switchdev_obj_port_vlan vlan = {
439 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
440 .flags = flags,
441 .vid_begin = vid,
442 .vid_end = vid,
443 };
444 struct switchdev_trans trans;
445 int err;
446
447 trans.ph_prepare = true;
448 err = dsa_port_vlan_add(dp, &vlan, &trans);
Vivien Didelotcf360862019-08-25 13:25:16 -0400449 if (err)
450 return err;
Vladimir Oltean314f76d2019-04-28 21:45:54 +0300451
452 trans.ph_prepare = false;
453 return dsa_port_vlan_add(dp, &vlan, &trans);
454}
Vladimir Oltean146c1be2019-05-05 13:19:21 +0300455EXPORT_SYMBOL(dsa_port_vid_add);
Vladimir Oltean314f76d2019-04-28 21:45:54 +0300456
457int dsa_port_vid_del(struct dsa_port *dp, u16 vid)
458{
459 struct switchdev_obj_port_vlan vlan = {
460 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
461 .vid_begin = vid,
462 .vid_end = vid,
463 };
464
465 return dsa_port_vlan_del(dp, &vlan);
466}
Vladimir Oltean146c1be2019-05-05 13:19:21 +0300467EXPORT_SYMBOL(dsa_port_vid_del);
Vladimir Oltean314f76d2019-04-28 21:45:54 +0300468
Florian Fainelli6207a782018-04-25 12:12:51 -0700469static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
470{
471 struct device_node *phy_dn;
472 struct phy_device *phydev;
473
474 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
475 if (!phy_dn)
476 return NULL;
477
478 phydev = of_phy_find_device(phy_dn);
479 if (!phydev) {
480 of_node_put(phy_dn);
481 return ERR_PTR(-EPROBE_DEFER);
482 }
483
Wen Yang9919a362019-02-25 15:22:19 +0800484 of_node_put(phy_dn);
Florian Fainelli6207a782018-04-25 12:12:51 -0700485 return phydev;
486}
487
Florian Fainelli8ae67492019-12-16 10:32:47 -0800488static void dsa_port_phylink_validate(struct phylink_config *config,
489 unsigned long *supported,
490 struct phylink_link_state *state)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300491{
492 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
493 struct dsa_switch *ds = dp->ds;
494
495 if (!ds->ops->phylink_validate)
496 return;
497
498 ds->ops->phylink_validate(ds, dp->index, supported, state);
499}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300500
Florian Fainelli8ae67492019-12-16 10:32:47 -0800501static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
502 struct phylink_link_state *state)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300503{
504 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
505 struct dsa_switch *ds = dp->ds;
Russell King87615c962020-03-14 10:15:28 +0000506 int err;
Ioana Ciornei77373d42019-05-28 20:38:15 +0300507
Russell Kingd46b7e42019-11-21 00:36:22 +0000508 /* Only called for inband modes */
509 if (!ds->ops->phylink_mac_link_state) {
510 state->link = 0;
511 return;
512 }
Ioana Ciornei77373d42019-05-28 20:38:15 +0300513
Russell King87615c962020-03-14 10:15:28 +0000514 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
515 if (err < 0) {
516 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
517 dp->index, err);
Russell Kingd46b7e42019-11-21 00:36:22 +0000518 state->link = 0;
Russell King87615c962020-03-14 10:15:28 +0000519 }
Ioana Ciornei77373d42019-05-28 20:38:15 +0300520}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300521
Florian Fainelli8ae67492019-12-16 10:32:47 -0800522static void dsa_port_phylink_mac_config(struct phylink_config *config,
523 unsigned int mode,
524 const struct phylink_link_state *state)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300525{
526 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
527 struct dsa_switch *ds = dp->ds;
528
529 if (!ds->ops->phylink_mac_config)
530 return;
531
532 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
533}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300534
Florian Fainelli8ae67492019-12-16 10:32:47 -0800535static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300536{
537 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
538 struct dsa_switch *ds = dp->ds;
539
540 if (!ds->ops->phylink_mac_an_restart)
541 return;
542
543 ds->ops->phylink_mac_an_restart(ds, dp->index);
544}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300545
Florian Fainelli8ae67492019-12-16 10:32:47 -0800546static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
547 unsigned int mode,
548 phy_interface_t interface)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300549{
550 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
Ioana Ciornei0e279212019-05-28 20:38:16 +0300551 struct phy_device *phydev = NULL;
Ioana Ciornei77373d42019-05-28 20:38:15 +0300552 struct dsa_switch *ds = dp->ds;
553
Ioana Ciornei0e279212019-05-28 20:38:16 +0300554 if (dsa_is_user_port(ds, dp->index))
555 phydev = dp->slave->phydev;
556
Ioana Ciornei77373d42019-05-28 20:38:15 +0300557 if (!ds->ops->phylink_mac_link_down) {
Ioana Ciornei0e279212019-05-28 20:38:16 +0300558 if (ds->ops->adjust_link && phydev)
559 ds->ops->adjust_link(ds, dp->index, phydev);
Ioana Ciornei77373d42019-05-28 20:38:15 +0300560 return;
561 }
562
563 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
564}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300565
Florian Fainelli8ae67492019-12-16 10:32:47 -0800566static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
Russell King91a208f2020-02-26 10:23:41 +0000567 struct phy_device *phydev,
Florian Fainelli8ae67492019-12-16 10:32:47 -0800568 unsigned int mode,
569 phy_interface_t interface,
Russell King91a208f2020-02-26 10:23:41 +0000570 int speed, int duplex,
571 bool tx_pause, bool rx_pause)
Ioana Ciornei77373d42019-05-28 20:38:15 +0300572{
573 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
Ioana Ciornei77373d42019-05-28 20:38:15 +0300574 struct dsa_switch *ds = dp->ds;
575
576 if (!ds->ops->phylink_mac_link_up) {
Ioana Ciornei0e279212019-05-28 20:38:16 +0300577 if (ds->ops->adjust_link && phydev)
578 ds->ops->adjust_link(ds, dp->index, phydev);
Ioana Ciornei77373d42019-05-28 20:38:15 +0300579 return;
580 }
581
Russell King5b502a72020-02-26 10:23:46 +0000582 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
583 speed, duplex, tx_pause, rx_pause);
Ioana Ciornei77373d42019-05-28 20:38:15 +0300584}
Ioana Ciornei77373d42019-05-28 20:38:15 +0300585
586const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
587 .validate = dsa_port_phylink_validate,
Russell Kingd46b7e42019-11-21 00:36:22 +0000588 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
Ioana Ciornei77373d42019-05-28 20:38:15 +0300589 .mac_config = dsa_port_phylink_mac_config,
590 .mac_an_restart = dsa_port_phylink_mac_an_restart,
591 .mac_link_down = dsa_port_phylink_mac_link_down,
592 .mac_link_up = dsa_port_phylink_mac_link_up,
593};
594
Sebastian Reichel33615362018-01-23 16:03:46 +0100595static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
596{
Sebastian Reichel33615362018-01-23 16:03:46 +0100597 struct dsa_switch *ds = dp->ds;
598 struct phy_device *phydev;
599 int port = dp->index;
600 int err = 0;
601
Florian Fainelli6207a782018-04-25 12:12:51 -0700602 phydev = dsa_port_get_phy_device(dp);
603 if (!phydev)
Sebastian Reichel33615362018-01-23 16:03:46 +0100604 return 0;
605
Florian Fainelli6207a782018-04-25 12:12:51 -0700606 if (IS_ERR(phydev))
607 return PTR_ERR(phydev);
Sebastian Reichel33615362018-01-23 16:03:46 +0100608
609 if (enable) {
Sebastian Reichel33615362018-01-23 16:03:46 +0100610 err = genphy_resume(phydev);
611 if (err < 0)
612 goto err_put_dev;
613
614 err = genphy_read_status(phydev);
615 if (err < 0)
616 goto err_put_dev;
617 } else {
618 err = genphy_suspend(phydev);
619 if (err < 0)
620 goto err_put_dev;
621 }
622
623 if (ds->ops->adjust_link)
624 ds->ops->adjust_link(ds, port, phydev);
625
626 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
627
628err_put_dev:
629 put_device(&phydev->mdio.dev);
Sebastian Reichel33615362018-01-23 16:03:46 +0100630 return err;
631}
632
633static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400634{
635 struct device_node *dn = dp->dn;
636 struct dsa_switch *ds = dp->ds;
637 struct phy_device *phydev;
638 int port = dp->index;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100639 phy_interface_t mode;
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400640 int err;
641
Sebastian Reichel33615362018-01-23 16:03:46 +0100642 err = of_phy_register_fixed_link(dn);
643 if (err) {
644 dev_err(ds->dev,
645 "failed to register the fixed PHY of port %d\n",
646 port);
647 return err;
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400648 }
649
Sebastian Reichel33615362018-01-23 16:03:46 +0100650 phydev = of_phy_find_device(dn);
651
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100652 err = of_get_phy_mode(dn, &mode);
653 if (err)
Sebastian Reichel33615362018-01-23 16:03:46 +0100654 mode = PHY_INTERFACE_MODE_NA;
655 phydev->interface = mode;
656
Sebastian Reichel33615362018-01-23 16:03:46 +0100657 genphy_read_status(phydev);
658
659 if (ds->ops->adjust_link)
660 ds->ops->adjust_link(ds, port, phydev);
661
662 put_device(&phydev->mdio.dev);
663
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400664 return 0;
665}
666
Ioana Ciornei0e279212019-05-28 20:38:16 +0300667static int dsa_port_phylink_register(struct dsa_port *dp)
668{
669 struct dsa_switch *ds = dp->ds;
670 struct device_node *port_dn = dp->dn;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100671 phy_interface_t mode;
672 int err;
Ioana Ciornei0e279212019-05-28 20:38:16 +0300673
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100674 err = of_get_phy_mode(port_dn, &mode);
675 if (err)
Ioana Ciornei0e279212019-05-28 20:38:16 +0300676 mode = PHY_INTERFACE_MODE_NA;
677
678 dp->pl_config.dev = ds->dev;
679 dp->pl_config.type = PHYLINK_DEV;
Vladimir Oltean787cac32020-01-06 03:34:12 +0200680 dp->pl_config.pcs_poll = ds->pcs_poll;
Ioana Ciornei0e279212019-05-28 20:38:16 +0300681
682 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
683 mode, &dsa_port_phylink_mac_ops);
684 if (IS_ERR(dp->pl)) {
685 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
686 return PTR_ERR(dp->pl);
687 }
688
689 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
Florian Fainelli2131fba2019-06-10 12:31:49 -0700690 if (err && err != -ENODEV) {
Ioana Ciornei0e279212019-05-28 20:38:16 +0300691 pr_err("could not attach to PHY: %d\n", err);
692 goto err_phy_connect;
693 }
694
Ioana Ciornei0e279212019-05-28 20:38:16 +0300695 return 0;
696
697err_phy_connect:
698 phylink_destroy(dp->pl);
699 return err;
700}
701
Sebastian Reichel33615362018-01-23 16:03:46 +0100702int dsa_port_link_register_of(struct dsa_port *dp)
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400703{
Ioana Ciornei0e279212019-05-28 20:38:16 +0300704 struct dsa_switch *ds = dp->ds;
Andrew Lunna20f9972020-03-11 16:24:24 +0100705 struct device_node *phy_np;
Andrew Lunn3be98b22020-04-14 02:34:39 +0200706 int port = dp->index;
Ioana Ciornei0e279212019-05-28 20:38:16 +0300707
Andrew Lunna20f9972020-03-11 16:24:24 +0100708 if (!ds->ops->adjust_link) {
709 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
Andrew Lunn3be98b22020-04-14 02:34:39 +0200710 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
711 if (ds->ops->phylink_mac_link_down)
712 ds->ops->phylink_mac_link_down(ds, port,
713 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
Andrew Lunna20f9972020-03-11 16:24:24 +0100714 return dsa_port_phylink_register(dp);
Andrew Lunn3be98b22020-04-14 02:34:39 +0200715 }
Andrew Lunna20f9972020-03-11 16:24:24 +0100716 return 0;
717 }
Ioana Ciornei0e279212019-05-28 20:38:16 +0300718
719 dev_warn(ds->dev,
720 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
721
Sebastian Reichel33615362018-01-23 16:03:46 +0100722 if (of_phy_is_fixed_link(dp->dn))
723 return dsa_port_fixed_link_register_of(dp);
724 else
725 return dsa_port_setup_phy_of(dp, true);
726}
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400727
Sebastian Reichel33615362018-01-23 16:03:46 +0100728void dsa_port_link_unregister_of(struct dsa_port *dp)
729{
Ioana Ciornei0e279212019-05-28 20:38:16 +0300730 struct dsa_switch *ds = dp->ds;
731
Andrew Lunna20f9972020-03-11 16:24:24 +0100732 if (!ds->ops->adjust_link && dp->pl) {
Ioana Ciornei0e279212019-05-28 20:38:16 +0300733 rtnl_lock();
734 phylink_disconnect_phy(dp->pl);
735 rtnl_unlock();
736 phylink_destroy(dp->pl);
Andrew Lunna20f9972020-03-11 16:24:24 +0100737 dp->pl = NULL;
Ioana Ciornei0e279212019-05-28 20:38:16 +0300738 return;
739 }
740
Sebastian Reichel33615362018-01-23 16:03:46 +0100741 if (of_phy_is_fixed_link(dp->dn))
742 of_phy_deregister_fixed_link(dp->dn);
743 else
744 dsa_port_setup_phy_of(dp, false);
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400745}
Florian Fainellicf963572018-04-25 12:12:52 -0700746
747int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
748{
749 struct phy_device *phydev;
750 int ret = -EOPNOTSUPP;
751
752 if (of_phy_is_fixed_link(dp->dn))
753 return ret;
754
755 phydev = dsa_port_get_phy_device(dp);
756 if (IS_ERR_OR_NULL(phydev))
757 return ret;
758
759 ret = phy_ethtool_get_strings(phydev, data);
760 put_device(&phydev->mdio.dev);
761
762 return ret;
763}
764EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
765
766int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
767{
768 struct phy_device *phydev;
769 int ret = -EOPNOTSUPP;
770
771 if (of_phy_is_fixed_link(dp->dn))
772 return ret;
773
774 phydev = dsa_port_get_phy_device(dp);
775 if (IS_ERR_OR_NULL(phydev))
776 return ret;
777
778 ret = phy_ethtool_get_stats(phydev, NULL, data);
779 put_device(&phydev->mdio.dev);
780
781 return ret;
782}
783EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
784
785int dsa_port_get_phy_sset_count(struct dsa_port *dp)
786{
787 struct phy_device *phydev;
788 int ret = -EOPNOTSUPP;
789
790 if (of_phy_is_fixed_link(dp->dn))
791 return ret;
792
793 phydev = dsa_port_get_phy_device(dp);
794 if (IS_ERR_OR_NULL(phydev))
795 return ret;
796
797 ret = phy_ethtool_get_sset_count(phydev);
798 put_device(&phydev->mdio.dev);
799
800 return ret;
801}
802EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);