Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Handling of a single switch port |
| 4 | * |
| 5 | * Copyright (c) 2017 Savoir-faire Linux Inc. |
| 6 | * Vivien Didelot <vivien.didelot@savoirfairelinux.com> |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/if_bridge.h> |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 10 | #include <linux/notifier.h> |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 11 | #include <linux/of_mdio.h> |
| 12 | #include <linux/of_net.h> |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 13 | |
| 14 | #include "dsa_priv.h" |
| 15 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame^] | 16 | static 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 Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 33 | static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v) |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 34 | { |
| 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 Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 43 | int 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 Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 74 | static void dsa_port_set_state_now(struct dsa_port *dp, u8 state) |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 75 | { |
| 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 Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 82 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 83 | int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy) |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 84 | { |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 85 | 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 King | 9c2054a | 2019-02-20 10:32:52 +0000 | [diff] [blame] | 95 | if (!dp->bridge_dev) |
| 96 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 97 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 98 | if (dp->pl) |
| 99 | phylink_start(dp->pl); |
| 100 | |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 104 | int 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 | |
| 115 | void dsa_port_disable_rt(struct dsa_port *dp) |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 116 | { |
| 117 | struct dsa_switch *ds = dp->ds; |
| 118 | int port = dp->index; |
| 119 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 120 | if (dp->pl) |
| 121 | phylink_stop(dp->pl); |
| 122 | |
Russell King | 9c2054a | 2019-02-20 10:32:52 +0000 | [diff] [blame] | 123 | if (!dp->bridge_dev) |
| 124 | dsa_port_set_state_now(dp, BR_STATE_DISABLED); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 125 | |
| 126 | if (ds->ops->port_disable) |
Andrew Lunn | 75104db | 2019-02-24 20:44:43 +0100 | [diff] [blame] | 127 | ds->ops->port_disable(ds, port); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Russell King | 8640f8d | 2020-03-03 15:01:46 +0000 | [diff] [blame] | 130 | void dsa_port_disable(struct dsa_port *dp) |
| 131 | { |
| 132 | rtnl_lock(); |
| 133 | dsa_port_disable_rt(dp); |
| 134 | rtnl_unlock(); |
| 135 | } |
| 136 | |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 137 | int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br) |
| 138 | { |
| 139 | struct dsa_notifier_bridge_info info = { |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame^] | 140 | .tree_index = dp->ds->dst->index, |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 141 | .sw_index = dp->ds->index, |
| 142 | .port = dp->index, |
| 143 | .br = br, |
| 144 | }; |
| 145 | int err; |
| 146 | |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 147 | /* 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 Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 154 | */ |
| 155 | dp->bridge_dev = br; |
| 156 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame^] | 157 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 158 | |
| 159 | /* The bridging is rolled back on error */ |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 160 | if (err) { |
| 161 | dsa_port_bridge_flags(dp, 0, NULL); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 162 | dp->bridge_dev = NULL; |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 163 | } |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 164 | |
| 165 | return err; |
| 166 | } |
| 167 | |
| 168 | void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) |
| 169 | { |
| 170 | struct dsa_notifier_bridge_info info = { |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame^] | 171 | .tree_index = dp->ds->dst->index, |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 172 | .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 Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame^] | 183 | err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 184 | if (err) |
| 185 | pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n"); |
| 186 | |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 187 | /* Port is leaving the bridge, disable flooding */ |
| 188 | dsa_port_bridge_flags(dp, 0, NULL); |
| 189 | |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 190 | /* 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 Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 195 | |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 196 | static 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 Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 229 | int 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 Oltean | 33162e9 | 2019-04-28 21:45:43 +0300 | [diff] [blame] | 233 | int err; |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 234 | |
| 235 | /* bridge skips -EOPNOTSUPP, so skip the prepare phase */ |
| 236 | if (switchdev_trans_ph_prepare(trans)) |
| 237 | return 0; |
| 238 | |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 239 | 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 Oltean | ec9121e | 2019-04-28 21:45:51 +0300 | [diff] [blame] | 245 | if (dsa_port_is_vlan_filtering(dp) == vlan_filtering) |
| 246 | return 0; |
| 247 | |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 248 | err = ds->ops->port_vlan_filtering(ds, dp->index, |
| 249 | vlan_filtering); |
| 250 | if (err) |
| 251 | return err; |
| 252 | |
Vladimir Oltean | 14574676 | 2019-04-28 21:45:48 +0300 | [diff] [blame] | 253 | if (ds->vlan_filtering_is_global) |
| 254 | ds->vlan_filtering = vlan_filtering; |
| 255 | else |
| 256 | dp->vlan_filtering = vlan_filtering; |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 257 | return 0; |
| 258 | } |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 259 | |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 260 | int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock, |
| 261 | struct switchdev_trans *trans) |
| 262 | { |
| 263 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); |
| 264 | unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 265 | struct dsa_notifier_ageing_time_info info = { |
| 266 | .ageing_time = ageing_time, |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 267 | .trans = trans, |
| 268 | }; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 269 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 270 | if (switchdev_trans_ph_prepare(trans)) |
| 271 | return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 272 | |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 273 | dp->ageing_time = ageing_time; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 274 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 275 | return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 276 | } |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 277 | |
Florian Fainelli | ea87005 | 2019-02-20 16:58:22 -0800 | [diff] [blame] | 278 | int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags, |
| 279 | struct switchdev_trans *trans) |
| 280 | { |
| 281 | struct dsa_switch *ds = dp->ds; |
| 282 | |
| 283 | if (!ds->ops->port_egress_floods || |
| 284 | (flags & ~(BR_FLOOD | BR_MCAST_FLOOD))) |
| 285 | return -EINVAL; |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 290 | int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags, |
| 291 | struct switchdev_trans *trans) |
| 292 | { |
| 293 | struct dsa_switch *ds = dp->ds; |
| 294 | int port = dp->index; |
| 295 | int err = 0; |
| 296 | |
| 297 | if (switchdev_trans_ph_prepare(trans)) |
| 298 | return 0; |
| 299 | |
| 300 | if (ds->ops->port_egress_floods) |
| 301 | err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD, |
| 302 | flags & BR_MCAST_FLOOD); |
| 303 | |
| 304 | return err; |
| 305 | } |
| 306 | |
Vivien Didelot | 08cc83c | 2019-07-08 23:31:13 -0400 | [diff] [blame] | 307 | int dsa_port_mrouter(struct dsa_port *dp, bool mrouter, |
| 308 | struct switchdev_trans *trans) |
| 309 | { |
| 310 | struct dsa_switch *ds = dp->ds; |
| 311 | int port = dp->index; |
| 312 | |
| 313 | if (switchdev_trans_ph_prepare(trans)) |
| 314 | return ds->ops->port_egress_floods ? 0 : -EOPNOTSUPP; |
| 315 | |
| 316 | return ds->ops->port_egress_floods(ds, port, true, mrouter); |
| 317 | } |
| 318 | |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 319 | int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu, |
| 320 | bool propagate_upstream) |
| 321 | { |
| 322 | struct dsa_notifier_mtu_info info = { |
| 323 | .sw_index = dp->ds->index, |
| 324 | .propagate_upstream = propagate_upstream, |
| 325 | .port = dp->index, |
| 326 | .mtu = new_mtu, |
| 327 | }; |
| 328 | |
| 329 | return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info); |
| 330 | } |
| 331 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 332 | int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
| 333 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 334 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 335 | struct dsa_notifier_fdb_info info = { |
| 336 | .sw_index = dp->ds->index, |
| 337 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 338 | .addr = addr, |
| 339 | .vid = vid, |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 340 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 341 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 342 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 345 | int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
| 346 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 347 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 348 | struct dsa_notifier_fdb_info info = { |
| 349 | .sw_index = dp->ds->index, |
| 350 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 351 | .addr = addr, |
| 352 | .vid = vid, |
| 353 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 354 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 355 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 356 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Vivien Didelot | de40fc5 | 2017-09-20 19:32:14 -0400 | [diff] [blame] | 359 | int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) |
| 360 | { |
| 361 | struct dsa_switch *ds = dp->ds; |
| 362 | int port = dp->index; |
| 363 | |
| 364 | if (!ds->ops->port_fdb_dump) |
| 365 | return -EOPNOTSUPP; |
| 366 | |
| 367 | return ds->ops->port_fdb_dump(ds, port, cb, data); |
| 368 | } |
| 369 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 370 | int dsa_port_mdb_add(const struct dsa_port *dp, |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 371 | const struct switchdev_obj_port_mdb *mdb, |
| 372 | struct switchdev_trans *trans) |
| 373 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 374 | struct dsa_notifier_mdb_info info = { |
| 375 | .sw_index = dp->ds->index, |
| 376 | .port = dp->index, |
| 377 | .trans = trans, |
| 378 | .mdb = mdb, |
| 379 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 380 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 381 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 384 | int dsa_port_mdb_del(const struct dsa_port *dp, |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 385 | const struct switchdev_obj_port_mdb *mdb) |
| 386 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 387 | struct dsa_notifier_mdb_info info = { |
| 388 | .sw_index = dp->ds->index, |
| 389 | .port = dp->index, |
| 390 | .mdb = mdb, |
| 391 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 392 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 393 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 394 | } |
| 395 | |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 396 | int dsa_port_vlan_add(struct dsa_port *dp, |
| 397 | const struct switchdev_obj_port_vlan *vlan, |
| 398 | struct switchdev_trans *trans) |
| 399 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 400 | struct dsa_notifier_vlan_info info = { |
| 401 | .sw_index = dp->ds->index, |
| 402 | .port = dp->index, |
| 403 | .trans = trans, |
| 404 | .vlan = vlan, |
| 405 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 406 | |
Vivien Didelot | c5335d7 | 2019-08-25 13:25:18 -0400 | [diff] [blame] | 407 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | int dsa_port_vlan_del(struct dsa_port *dp, |
| 411 | const struct switchdev_obj_port_vlan *vlan) |
| 412 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 413 | struct dsa_notifier_vlan_info info = { |
| 414 | .sw_index = dp->ds->index, |
| 415 | .port = dp->index, |
| 416 | .vlan = vlan, |
| 417 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 418 | |
Vivien Didelot | c5335d7 | 2019-08-25 13:25:18 -0400 | [diff] [blame] | 419 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 420 | } |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 421 | |
Vladimir Oltean | 314f76d | 2019-04-28 21:45:54 +0300 | [diff] [blame] | 422 | int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags) |
| 423 | { |
| 424 | struct switchdev_obj_port_vlan vlan = { |
| 425 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 426 | .flags = flags, |
| 427 | .vid_begin = vid, |
| 428 | .vid_end = vid, |
| 429 | }; |
| 430 | struct switchdev_trans trans; |
| 431 | int err; |
| 432 | |
| 433 | trans.ph_prepare = true; |
| 434 | err = dsa_port_vlan_add(dp, &vlan, &trans); |
Vivien Didelot | cf36086 | 2019-08-25 13:25:16 -0400 | [diff] [blame] | 435 | if (err) |
| 436 | return err; |
Vladimir Oltean | 314f76d | 2019-04-28 21:45:54 +0300 | [diff] [blame] | 437 | |
| 438 | trans.ph_prepare = false; |
| 439 | return dsa_port_vlan_add(dp, &vlan, &trans); |
| 440 | } |
Vladimir Oltean | 146c1be | 2019-05-05 13:19:21 +0300 | [diff] [blame] | 441 | EXPORT_SYMBOL(dsa_port_vid_add); |
Vladimir Oltean | 314f76d | 2019-04-28 21:45:54 +0300 | [diff] [blame] | 442 | |
| 443 | int dsa_port_vid_del(struct dsa_port *dp, u16 vid) |
| 444 | { |
| 445 | struct switchdev_obj_port_vlan vlan = { |
| 446 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 447 | .vid_begin = vid, |
| 448 | .vid_end = vid, |
| 449 | }; |
| 450 | |
| 451 | return dsa_port_vlan_del(dp, &vlan); |
| 452 | } |
Vladimir Oltean | 146c1be | 2019-05-05 13:19:21 +0300 | [diff] [blame] | 453 | EXPORT_SYMBOL(dsa_port_vid_del); |
Vladimir Oltean | 314f76d | 2019-04-28 21:45:54 +0300 | [diff] [blame] | 454 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 455 | static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp) |
| 456 | { |
| 457 | struct device_node *phy_dn; |
| 458 | struct phy_device *phydev; |
| 459 | |
| 460 | phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0); |
| 461 | if (!phy_dn) |
| 462 | return NULL; |
| 463 | |
| 464 | phydev = of_phy_find_device(phy_dn); |
| 465 | if (!phydev) { |
| 466 | of_node_put(phy_dn); |
| 467 | return ERR_PTR(-EPROBE_DEFER); |
| 468 | } |
| 469 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 470 | of_node_put(phy_dn); |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 471 | return phydev; |
| 472 | } |
| 473 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 474 | static void dsa_port_phylink_validate(struct phylink_config *config, |
| 475 | unsigned long *supported, |
| 476 | struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 477 | { |
| 478 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 479 | struct dsa_switch *ds = dp->ds; |
| 480 | |
| 481 | if (!ds->ops->phylink_validate) |
| 482 | return; |
| 483 | |
| 484 | ds->ops->phylink_validate(ds, dp->index, supported, state); |
| 485 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 486 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 487 | static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config, |
| 488 | struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 489 | { |
| 490 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 491 | struct dsa_switch *ds = dp->ds; |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 492 | int err; |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 493 | |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 494 | /* Only called for inband modes */ |
| 495 | if (!ds->ops->phylink_mac_link_state) { |
| 496 | state->link = 0; |
| 497 | return; |
| 498 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 499 | |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 500 | err = ds->ops->phylink_mac_link_state(ds, dp->index, state); |
| 501 | if (err < 0) { |
| 502 | dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n", |
| 503 | dp->index, err); |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 504 | state->link = 0; |
Russell King | 87615c96 | 2020-03-14 10:15:28 +0000 | [diff] [blame] | 505 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 506 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 507 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 508 | static void dsa_port_phylink_mac_config(struct phylink_config *config, |
| 509 | unsigned int mode, |
| 510 | const struct phylink_link_state *state) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 511 | { |
| 512 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 513 | struct dsa_switch *ds = dp->ds; |
| 514 | |
| 515 | if (!ds->ops->phylink_mac_config) |
| 516 | return; |
| 517 | |
| 518 | ds->ops->phylink_mac_config(ds, dp->index, mode, state); |
| 519 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 520 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 521 | static void dsa_port_phylink_mac_an_restart(struct phylink_config *config) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 522 | { |
| 523 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 524 | struct dsa_switch *ds = dp->ds; |
| 525 | |
| 526 | if (!ds->ops->phylink_mac_an_restart) |
| 527 | return; |
| 528 | |
| 529 | ds->ops->phylink_mac_an_restart(ds, dp->index); |
| 530 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 531 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 532 | static void dsa_port_phylink_mac_link_down(struct phylink_config *config, |
| 533 | unsigned int mode, |
| 534 | phy_interface_t interface) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 535 | { |
| 536 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 537 | struct phy_device *phydev = NULL; |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 538 | struct dsa_switch *ds = dp->ds; |
| 539 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 540 | if (dsa_is_user_port(ds, dp->index)) |
| 541 | phydev = dp->slave->phydev; |
| 542 | |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 543 | if (!ds->ops->phylink_mac_link_down) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 544 | if (ds->ops->adjust_link && phydev) |
| 545 | ds->ops->adjust_link(ds, dp->index, phydev); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 546 | return; |
| 547 | } |
| 548 | |
| 549 | ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); |
| 550 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 551 | |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 552 | static void dsa_port_phylink_mac_link_up(struct phylink_config *config, |
Russell King | 91a208f | 2020-02-26 10:23:41 +0000 | [diff] [blame] | 553 | struct phy_device *phydev, |
Florian Fainelli | 8ae6749 | 2019-12-16 10:32:47 -0800 | [diff] [blame] | 554 | unsigned int mode, |
| 555 | phy_interface_t interface, |
Russell King | 91a208f | 2020-02-26 10:23:41 +0000 | [diff] [blame] | 556 | int speed, int duplex, |
| 557 | bool tx_pause, bool rx_pause) |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 558 | { |
| 559 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 560 | struct dsa_switch *ds = dp->ds; |
| 561 | |
| 562 | if (!ds->ops->phylink_mac_link_up) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 563 | if (ds->ops->adjust_link && phydev) |
| 564 | ds->ops->adjust_link(ds, dp->index, phydev); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
Russell King | 5b502a7 | 2020-02-26 10:23:46 +0000 | [diff] [blame] | 568 | ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev, |
| 569 | speed, duplex, tx_pause, rx_pause); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 570 | } |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 571 | |
| 572 | const struct phylink_mac_ops dsa_port_phylink_mac_ops = { |
| 573 | .validate = dsa_port_phylink_validate, |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 574 | .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state, |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 575 | .mac_config = dsa_port_phylink_mac_config, |
| 576 | .mac_an_restart = dsa_port_phylink_mac_an_restart, |
| 577 | .mac_link_down = dsa_port_phylink_mac_link_down, |
| 578 | .mac_link_up = dsa_port_phylink_mac_link_up, |
| 579 | }; |
| 580 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 581 | static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) |
| 582 | { |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 583 | struct dsa_switch *ds = dp->ds; |
| 584 | struct phy_device *phydev; |
| 585 | int port = dp->index; |
| 586 | int err = 0; |
| 587 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 588 | phydev = dsa_port_get_phy_device(dp); |
| 589 | if (!phydev) |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 590 | return 0; |
| 591 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 592 | if (IS_ERR(phydev)) |
| 593 | return PTR_ERR(phydev); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 594 | |
| 595 | if (enable) { |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 596 | err = genphy_resume(phydev); |
| 597 | if (err < 0) |
| 598 | goto err_put_dev; |
| 599 | |
| 600 | err = genphy_read_status(phydev); |
| 601 | if (err < 0) |
| 602 | goto err_put_dev; |
| 603 | } else { |
| 604 | err = genphy_suspend(phydev); |
| 605 | if (err < 0) |
| 606 | goto err_put_dev; |
| 607 | } |
| 608 | |
| 609 | if (ds->ops->adjust_link) |
| 610 | ds->ops->adjust_link(ds, port, phydev); |
| 611 | |
| 612 | dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev)); |
| 613 | |
| 614 | err_put_dev: |
| 615 | put_device(&phydev->mdio.dev); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 616 | return err; |
| 617 | } |
| 618 | |
| 619 | static int dsa_port_fixed_link_register_of(struct dsa_port *dp) |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 620 | { |
| 621 | struct device_node *dn = dp->dn; |
| 622 | struct dsa_switch *ds = dp->ds; |
| 623 | struct phy_device *phydev; |
| 624 | int port = dp->index; |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 625 | phy_interface_t mode; |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 626 | int err; |
| 627 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 628 | err = of_phy_register_fixed_link(dn); |
| 629 | if (err) { |
| 630 | dev_err(ds->dev, |
| 631 | "failed to register the fixed PHY of port %d\n", |
| 632 | port); |
| 633 | return err; |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 634 | } |
| 635 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 636 | phydev = of_phy_find_device(dn); |
| 637 | |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 638 | err = of_get_phy_mode(dn, &mode); |
| 639 | if (err) |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 640 | mode = PHY_INTERFACE_MODE_NA; |
| 641 | phydev->interface = mode; |
| 642 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 643 | genphy_read_status(phydev); |
| 644 | |
| 645 | if (ds->ops->adjust_link) |
| 646 | ds->ops->adjust_link(ds, port, phydev); |
| 647 | |
| 648 | put_device(&phydev->mdio.dev); |
| 649 | |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 653 | static int dsa_port_phylink_register(struct dsa_port *dp) |
| 654 | { |
| 655 | struct dsa_switch *ds = dp->ds; |
| 656 | struct device_node *port_dn = dp->dn; |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 657 | phy_interface_t mode; |
| 658 | int err; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 659 | |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame] | 660 | err = of_get_phy_mode(port_dn, &mode); |
| 661 | if (err) |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 662 | mode = PHY_INTERFACE_MODE_NA; |
| 663 | |
| 664 | dp->pl_config.dev = ds->dev; |
| 665 | dp->pl_config.type = PHYLINK_DEV; |
Vladimir Oltean | 787cac3 | 2020-01-06 03:34:12 +0200 | [diff] [blame] | 666 | dp->pl_config.pcs_poll = ds->pcs_poll; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 667 | |
| 668 | dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), |
| 669 | mode, &dsa_port_phylink_mac_ops); |
| 670 | if (IS_ERR(dp->pl)) { |
| 671 | pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); |
| 672 | return PTR_ERR(dp->pl); |
| 673 | } |
| 674 | |
| 675 | err = phylink_of_phy_connect(dp->pl, port_dn, 0); |
Florian Fainelli | 2131fba | 2019-06-10 12:31:49 -0700 | [diff] [blame] | 676 | if (err && err != -ENODEV) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 677 | pr_err("could not attach to PHY: %d\n", err); |
| 678 | goto err_phy_connect; |
| 679 | } |
| 680 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 681 | return 0; |
| 682 | |
| 683 | err_phy_connect: |
| 684 | phylink_destroy(dp->pl); |
| 685 | return err; |
| 686 | } |
| 687 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 688 | int dsa_port_link_register_of(struct dsa_port *dp) |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 689 | { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 690 | struct dsa_switch *ds = dp->ds; |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 691 | struct device_node *phy_np; |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 692 | int port = dp->index; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 693 | |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 694 | if (!ds->ops->adjust_link) { |
| 695 | phy_np = of_parse_phandle(dp->dn, "phy-handle", 0); |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 696 | if (of_phy_is_fixed_link(dp->dn) || phy_np) { |
| 697 | if (ds->ops->phylink_mac_link_down) |
| 698 | ds->ops->phylink_mac_link_down(ds, port, |
| 699 | MLO_AN_FIXED, PHY_INTERFACE_MODE_NA); |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 700 | return dsa_port_phylink_register(dp); |
Andrew Lunn | 3be98b2 | 2020-04-14 02:34:39 +0200 | [diff] [blame] | 701 | } |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 702 | return 0; |
| 703 | } |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 704 | |
| 705 | dev_warn(ds->dev, |
| 706 | "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); |
| 707 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 708 | if (of_phy_is_fixed_link(dp->dn)) |
| 709 | return dsa_port_fixed_link_register_of(dp); |
| 710 | else |
| 711 | return dsa_port_setup_phy_of(dp, true); |
| 712 | } |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 713 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 714 | void dsa_port_link_unregister_of(struct dsa_port *dp) |
| 715 | { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 716 | struct dsa_switch *ds = dp->ds; |
| 717 | |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 718 | if (!ds->ops->adjust_link && dp->pl) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 719 | rtnl_lock(); |
| 720 | phylink_disconnect_phy(dp->pl); |
| 721 | rtnl_unlock(); |
| 722 | phylink_destroy(dp->pl); |
Andrew Lunn | a20f997 | 2020-03-11 16:24:24 +0100 | [diff] [blame] | 723 | dp->pl = NULL; |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 724 | return; |
| 725 | } |
| 726 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 727 | if (of_phy_is_fixed_link(dp->dn)) |
| 728 | of_phy_deregister_fixed_link(dp->dn); |
| 729 | else |
| 730 | dsa_port_setup_phy_of(dp, false); |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 731 | } |
Florian Fainelli | cf96357 | 2018-04-25 12:12:52 -0700 | [diff] [blame] | 732 | |
| 733 | int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data) |
| 734 | { |
| 735 | struct phy_device *phydev; |
| 736 | int ret = -EOPNOTSUPP; |
| 737 | |
| 738 | if (of_phy_is_fixed_link(dp->dn)) |
| 739 | return ret; |
| 740 | |
| 741 | phydev = dsa_port_get_phy_device(dp); |
| 742 | if (IS_ERR_OR_NULL(phydev)) |
| 743 | return ret; |
| 744 | |
| 745 | ret = phy_ethtool_get_strings(phydev, data); |
| 746 | put_device(&phydev->mdio.dev); |
| 747 | |
| 748 | return ret; |
| 749 | } |
| 750 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings); |
| 751 | |
| 752 | int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data) |
| 753 | { |
| 754 | struct phy_device *phydev; |
| 755 | int ret = -EOPNOTSUPP; |
| 756 | |
| 757 | if (of_phy_is_fixed_link(dp->dn)) |
| 758 | return ret; |
| 759 | |
| 760 | phydev = dsa_port_get_phy_device(dp); |
| 761 | if (IS_ERR_OR_NULL(phydev)) |
| 762 | return ret; |
| 763 | |
| 764 | ret = phy_ethtool_get_stats(phydev, NULL, data); |
| 765 | put_device(&phydev->mdio.dev); |
| 766 | |
| 767 | return ret; |
| 768 | } |
| 769 | EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats); |
| 770 | |
| 771 | int dsa_port_get_phy_sset_count(struct dsa_port *dp) |
| 772 | { |
| 773 | struct phy_device *phydev; |
| 774 | int ret = -EOPNOTSUPP; |
| 775 | |
| 776 | if (of_phy_is_fixed_link(dp->dn)) |
| 777 | return ret; |
| 778 | |
| 779 | phydev = dsa_port_get_phy_device(dp); |
| 780 | if (IS_ERR_OR_NULL(phydev)) |
| 781 | return ret; |
| 782 | |
| 783 | ret = phy_ethtool_get_sset_count(phydev); |
| 784 | put_device(&phydev->mdio.dev); |
| 785 | |
| 786 | return ret; |
| 787 | } |
| 788 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count); |