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 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 16 | 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] | 17 | { |
| 18 | struct raw_notifier_head *nh = &dp->ds->dst->nh; |
| 19 | int err; |
| 20 | |
| 21 | err = raw_notifier_call_chain(nh, e, v); |
| 22 | |
| 23 | return notifier_to_errno(err); |
| 24 | } |
| 25 | |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 26 | int dsa_port_set_state(struct dsa_port *dp, u8 state, |
| 27 | struct switchdev_trans *trans) |
| 28 | { |
| 29 | struct dsa_switch *ds = dp->ds; |
| 30 | int port = dp->index; |
| 31 | |
| 32 | if (switchdev_trans_ph_prepare(trans)) |
| 33 | return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP; |
| 34 | |
| 35 | if (ds->ops->port_stp_state_set) |
| 36 | ds->ops->port_stp_state_set(ds, port, state); |
| 37 | |
| 38 | if (ds->ops->port_fast_age) { |
| 39 | /* Fast age FDB entries or flush appropriate forwarding database |
| 40 | * for the given port, if we are moving it from Learning or |
| 41 | * Forwarding state, to Disabled or Blocking or Listening state. |
| 42 | */ |
| 43 | |
| 44 | if ((dp->stp_state == BR_STATE_LEARNING || |
| 45 | dp->stp_state == BR_STATE_FORWARDING) && |
| 46 | (state == BR_STATE_DISABLED || |
| 47 | state == BR_STATE_BLOCKING || |
| 48 | state == BR_STATE_LISTENING)) |
| 49 | ds->ops->port_fast_age(ds, port); |
| 50 | } |
| 51 | |
| 52 | dp->stp_state = state; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 57 | 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] | 58 | { |
| 59 | int err; |
| 60 | |
| 61 | err = dsa_port_set_state(dp, state, NULL); |
| 62 | if (err) |
| 63 | pr_err("DSA: failed to set STP state %u (%d)\n", state, err); |
| 64 | } |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 65 | |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 66 | int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy) |
| 67 | { |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 68 | struct dsa_switch *ds = dp->ds; |
| 69 | int port = dp->index; |
| 70 | int err; |
| 71 | |
| 72 | if (ds->ops->port_enable) { |
| 73 | err = ds->ops->port_enable(ds, port, phy); |
| 74 | if (err) |
| 75 | return err; |
| 76 | } |
| 77 | |
Russell King | 9c2054a | 2019-02-20 10:32:52 +0000 | [diff] [blame] | 78 | if (!dp->bridge_dev) |
| 79 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Andrew Lunn | 75104db | 2019-02-24 20:44:43 +0100 | [diff] [blame] | 84 | void dsa_port_disable(struct dsa_port *dp) |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 85 | { |
| 86 | struct dsa_switch *ds = dp->ds; |
| 87 | int port = dp->index; |
| 88 | |
Russell King | 9c2054a | 2019-02-20 10:32:52 +0000 | [diff] [blame] | 89 | if (!dp->bridge_dev) |
| 90 | dsa_port_set_state_now(dp, BR_STATE_DISABLED); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 91 | |
| 92 | if (ds->ops->port_disable) |
Andrew Lunn | 75104db | 2019-02-24 20:44:43 +0100 | [diff] [blame] | 93 | ds->ops->port_disable(ds, port); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 96 | int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br) |
| 97 | { |
| 98 | struct dsa_notifier_bridge_info info = { |
| 99 | .sw_index = dp->ds->index, |
| 100 | .port = dp->index, |
| 101 | .br = br, |
| 102 | }; |
| 103 | int err; |
| 104 | |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 105 | /* Set the flooding mode before joining the port in the switch */ |
| 106 | err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL); |
| 107 | if (err) |
| 108 | return err; |
| 109 | |
| 110 | /* Here the interface is already bridged. Reflect the current |
| 111 | * configuration so that drivers can program their chips accordingly. |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 112 | */ |
| 113 | dp->bridge_dev = br; |
| 114 | |
| 115 | err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info); |
| 116 | |
| 117 | /* The bridging is rolled back on error */ |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 118 | if (err) { |
| 119 | dsa_port_bridge_flags(dp, 0, NULL); |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 120 | dp->bridge_dev = NULL; |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 121 | } |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 122 | |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) |
| 127 | { |
| 128 | struct dsa_notifier_bridge_info info = { |
| 129 | .sw_index = dp->ds->index, |
| 130 | .port = dp->index, |
| 131 | .br = br, |
| 132 | }; |
| 133 | int err; |
| 134 | |
| 135 | /* Here the port is already unbridged. Reflect the current configuration |
| 136 | * so that drivers can program their chips accordingly. |
| 137 | */ |
| 138 | dp->bridge_dev = NULL; |
| 139 | |
| 140 | err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
| 141 | if (err) |
| 142 | pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n"); |
| 143 | |
Russell King | c138806 | 2019-02-20 15:35:06 -0800 | [diff] [blame] | 144 | /* Port is leaving the bridge, disable flooding */ |
| 145 | dsa_port_bridge_flags(dp, 0, NULL); |
| 146 | |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 147 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
| 148 | * so allow it to be in BR_STATE_FORWARDING to be kept functional |
| 149 | */ |
| 150 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); |
| 151 | } |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 152 | |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 153 | static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp, |
| 154 | bool vlan_filtering) |
| 155 | { |
| 156 | struct dsa_switch *ds = dp->ds; |
| 157 | int i; |
| 158 | |
| 159 | if (!ds->vlan_filtering_is_global) |
| 160 | return true; |
| 161 | |
| 162 | /* For cases where enabling/disabling VLAN awareness is global to the |
| 163 | * switch, we need to handle the case where multiple bridges span |
| 164 | * different ports of the same switch device and one of them has a |
| 165 | * different setting than what is being requested. |
| 166 | */ |
| 167 | for (i = 0; i < ds->num_ports; i++) { |
| 168 | struct net_device *other_bridge; |
| 169 | |
| 170 | other_bridge = dsa_to_port(ds, i)->bridge_dev; |
| 171 | if (!other_bridge) |
| 172 | continue; |
| 173 | /* If it's the same bridge, it also has same |
| 174 | * vlan_filtering setting => no need to check |
| 175 | */ |
| 176 | if (other_bridge == dp->bridge_dev) |
| 177 | continue; |
| 178 | if (br_vlan_enabled(other_bridge) != vlan_filtering) { |
| 179 | dev_err(ds->dev, "VLAN filtering is a global setting\n"); |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 186 | int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering, |
| 187 | struct switchdev_trans *trans) |
| 188 | { |
| 189 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | 33162e9 | 2019-04-28 21:45:43 +0300 | [diff] [blame] | 190 | int err; |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 191 | |
| 192 | /* bridge skips -EOPNOTSUPP, so skip the prepare phase */ |
| 193 | if (switchdev_trans_ph_prepare(trans)) |
| 194 | return 0; |
| 195 | |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 196 | if (!ds->ops->port_vlan_filtering) |
| 197 | return 0; |
| 198 | |
| 199 | if (!dsa_port_can_apply_vlan_filtering(dp, vlan_filtering)) |
| 200 | return -EINVAL; |
| 201 | |
Vladimir Oltean | ec9121e | 2019-04-28 21:45:51 +0300 | [diff] [blame] | 202 | if (dsa_port_is_vlan_filtering(dp) == vlan_filtering) |
| 203 | return 0; |
| 204 | |
Vladimir Oltean | 8f5d16f | 2019-04-28 21:45:44 +0300 | [diff] [blame] | 205 | err = ds->ops->port_vlan_filtering(ds, dp->index, |
| 206 | vlan_filtering); |
| 207 | if (err) |
| 208 | return err; |
| 209 | |
Vladimir Oltean | 14574676 | 2019-04-28 21:45:48 +0300 | [diff] [blame] | 210 | if (ds->vlan_filtering_is_global) |
| 211 | ds->vlan_filtering = vlan_filtering; |
| 212 | else |
| 213 | dp->vlan_filtering = vlan_filtering; |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 214 | return 0; |
| 215 | } |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 216 | |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 217 | int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock, |
| 218 | struct switchdev_trans *trans) |
| 219 | { |
| 220 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); |
| 221 | unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 222 | struct dsa_notifier_ageing_time_info info = { |
| 223 | .ageing_time = ageing_time, |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 224 | .trans = trans, |
| 225 | }; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 226 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 227 | if (switchdev_trans_ph_prepare(trans)) |
| 228 | return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 229 | |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 230 | dp->ageing_time = ageing_time; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 231 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 232 | return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 233 | } |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 234 | |
Florian Fainelli | ea87005 | 2019-02-20 16:58:22 -0800 | [diff] [blame] | 235 | int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags, |
| 236 | struct switchdev_trans *trans) |
| 237 | { |
| 238 | struct dsa_switch *ds = dp->ds; |
| 239 | |
| 240 | if (!ds->ops->port_egress_floods || |
| 241 | (flags & ~(BR_FLOOD | BR_MCAST_FLOOD))) |
| 242 | return -EINVAL; |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Russell King | 5765279 | 2019-02-20 15:35:04 -0800 | [diff] [blame] | 247 | int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags, |
| 248 | struct switchdev_trans *trans) |
| 249 | { |
| 250 | struct dsa_switch *ds = dp->ds; |
| 251 | int port = dp->index; |
| 252 | int err = 0; |
| 253 | |
| 254 | if (switchdev_trans_ph_prepare(trans)) |
| 255 | return 0; |
| 256 | |
| 257 | if (ds->ops->port_egress_floods) |
| 258 | err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD, |
| 259 | flags & BR_MCAST_FLOOD); |
| 260 | |
| 261 | return err; |
| 262 | } |
| 263 | |
Vivien Didelot | 08cc83c | 2019-07-08 23:31:13 -0400 | [diff] [blame^] | 264 | int dsa_port_mrouter(struct dsa_port *dp, bool mrouter, |
| 265 | struct switchdev_trans *trans) |
| 266 | { |
| 267 | struct dsa_switch *ds = dp->ds; |
| 268 | int port = dp->index; |
| 269 | |
| 270 | if (switchdev_trans_ph_prepare(trans)) |
| 271 | return ds->ops->port_egress_floods ? 0 : -EOPNOTSUPP; |
| 272 | |
| 273 | return ds->ops->port_egress_floods(ds, port, true, mrouter); |
| 274 | } |
| 275 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 276 | int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
| 277 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 278 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 279 | struct dsa_notifier_fdb_info info = { |
| 280 | .sw_index = dp->ds->index, |
| 281 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 282 | .addr = addr, |
| 283 | .vid = vid, |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 284 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 285 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 286 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 289 | int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
| 290 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 291 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 292 | struct dsa_notifier_fdb_info info = { |
| 293 | .sw_index = dp->ds->index, |
| 294 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 295 | .addr = addr, |
| 296 | .vid = vid, |
| 297 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 298 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 299 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 300 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Vivien Didelot | de40fc5 | 2017-09-20 19:32:14 -0400 | [diff] [blame] | 303 | int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) |
| 304 | { |
| 305 | struct dsa_switch *ds = dp->ds; |
| 306 | int port = dp->index; |
| 307 | |
| 308 | if (!ds->ops->port_fdb_dump) |
| 309 | return -EOPNOTSUPP; |
| 310 | |
| 311 | return ds->ops->port_fdb_dump(ds, port, cb, data); |
| 312 | } |
| 313 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 314 | int dsa_port_mdb_add(const struct dsa_port *dp, |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 315 | const struct switchdev_obj_port_mdb *mdb, |
| 316 | struct switchdev_trans *trans) |
| 317 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 318 | struct dsa_notifier_mdb_info info = { |
| 319 | .sw_index = dp->ds->index, |
| 320 | .port = dp->index, |
| 321 | .trans = trans, |
| 322 | .mdb = mdb, |
| 323 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 324 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 325 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Andrew Lunn | bb9f60317 | 2017-11-09 23:11:01 +0100 | [diff] [blame] | 328 | int dsa_port_mdb_del(const struct dsa_port *dp, |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 329 | const struct switchdev_obj_port_mdb *mdb) |
| 330 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 331 | struct dsa_notifier_mdb_info info = { |
| 332 | .sw_index = dp->ds->index, |
| 333 | .port = dp->index, |
| 334 | .mdb = mdb, |
| 335 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 336 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 337 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 338 | } |
| 339 | |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 340 | int dsa_port_vlan_add(struct dsa_port *dp, |
| 341 | const struct switchdev_obj_port_vlan *vlan, |
| 342 | struct switchdev_trans *trans) |
| 343 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 344 | struct dsa_notifier_vlan_info info = { |
| 345 | .sw_index = dp->ds->index, |
| 346 | .port = dp->index, |
| 347 | .trans = trans, |
| 348 | .vlan = vlan, |
| 349 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 350 | |
Florian Fainelli | 061f6a5 | 2019-02-20 14:35:39 -0800 | [diff] [blame] | 351 | if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev)) |
Andrew Lunn | 2ea7a67 | 2017-11-07 00:04:24 +0100 | [diff] [blame] | 352 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); |
| 353 | |
| 354 | return 0; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | int dsa_port_vlan_del(struct dsa_port *dp, |
| 358 | const struct switchdev_obj_port_vlan *vlan) |
| 359 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 360 | struct dsa_notifier_vlan_info info = { |
| 361 | .sw_index = dp->ds->index, |
| 362 | .port = dp->index, |
| 363 | .vlan = vlan, |
| 364 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 365 | |
Florian Fainelli | 061f6a5 | 2019-02-20 14:35:39 -0800 | [diff] [blame] | 366 | if (!dp->bridge_dev || br_vlan_enabled(dp->bridge_dev)) |
Andrew Lunn | 2ea7a67 | 2017-11-07 00:04:24 +0100 | [diff] [blame] | 367 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); |
| 368 | |
| 369 | return 0; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 370 | } |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 371 | |
Vladimir Oltean | 314f76d | 2019-04-28 21:45:54 +0300 | [diff] [blame] | 372 | int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags) |
| 373 | { |
| 374 | struct switchdev_obj_port_vlan vlan = { |
| 375 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 376 | .flags = flags, |
| 377 | .vid_begin = vid, |
| 378 | .vid_end = vid, |
| 379 | }; |
| 380 | struct switchdev_trans trans; |
| 381 | int err; |
| 382 | |
| 383 | trans.ph_prepare = true; |
| 384 | err = dsa_port_vlan_add(dp, &vlan, &trans); |
| 385 | if (err == -EOPNOTSUPP) |
| 386 | return 0; |
| 387 | |
| 388 | trans.ph_prepare = false; |
| 389 | return dsa_port_vlan_add(dp, &vlan, &trans); |
| 390 | } |
Vladimir Oltean | 146c1be | 2019-05-05 13:19:21 +0300 | [diff] [blame] | 391 | EXPORT_SYMBOL(dsa_port_vid_add); |
Vladimir Oltean | 314f76d | 2019-04-28 21:45:54 +0300 | [diff] [blame] | 392 | |
| 393 | int dsa_port_vid_del(struct dsa_port *dp, u16 vid) |
| 394 | { |
| 395 | struct switchdev_obj_port_vlan vlan = { |
| 396 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 397 | .vid_begin = vid, |
| 398 | .vid_end = vid, |
| 399 | }; |
| 400 | |
| 401 | return dsa_port_vlan_del(dp, &vlan); |
| 402 | } |
Vladimir Oltean | 146c1be | 2019-05-05 13:19:21 +0300 | [diff] [blame] | 403 | EXPORT_SYMBOL(dsa_port_vid_del); |
Vladimir Oltean | 314f76d | 2019-04-28 21:45:54 +0300 | [diff] [blame] | 404 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 405 | static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp) |
| 406 | { |
| 407 | struct device_node *phy_dn; |
| 408 | struct phy_device *phydev; |
| 409 | |
| 410 | phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0); |
| 411 | if (!phy_dn) |
| 412 | return NULL; |
| 413 | |
| 414 | phydev = of_phy_find_device(phy_dn); |
| 415 | if (!phydev) { |
| 416 | of_node_put(phy_dn); |
| 417 | return ERR_PTR(-EPROBE_DEFER); |
| 418 | } |
| 419 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 420 | of_node_put(phy_dn); |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 421 | return phydev; |
| 422 | } |
| 423 | |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 424 | void dsa_port_phylink_validate(struct phylink_config *config, |
| 425 | unsigned long *supported, |
| 426 | struct phylink_link_state *state) |
| 427 | { |
| 428 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 429 | struct dsa_switch *ds = dp->ds; |
| 430 | |
| 431 | if (!ds->ops->phylink_validate) |
| 432 | return; |
| 433 | |
| 434 | ds->ops->phylink_validate(ds, dp->index, supported, state); |
| 435 | } |
| 436 | EXPORT_SYMBOL_GPL(dsa_port_phylink_validate); |
| 437 | |
| 438 | int dsa_port_phylink_mac_link_state(struct phylink_config *config, |
| 439 | struct phylink_link_state *state) |
| 440 | { |
| 441 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 442 | struct dsa_switch *ds = dp->ds; |
| 443 | |
| 444 | /* Only called for SGMII and 802.3z */ |
| 445 | if (!ds->ops->phylink_mac_link_state) |
| 446 | return -EOPNOTSUPP; |
| 447 | |
| 448 | return ds->ops->phylink_mac_link_state(ds, dp->index, state); |
| 449 | } |
| 450 | EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_state); |
| 451 | |
| 452 | void dsa_port_phylink_mac_config(struct phylink_config *config, |
| 453 | unsigned int mode, |
| 454 | const struct phylink_link_state *state) |
| 455 | { |
| 456 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 457 | struct dsa_switch *ds = dp->ds; |
| 458 | |
| 459 | if (!ds->ops->phylink_mac_config) |
| 460 | return; |
| 461 | |
| 462 | ds->ops->phylink_mac_config(ds, dp->index, mode, state); |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_config); |
| 465 | |
| 466 | void dsa_port_phylink_mac_an_restart(struct phylink_config *config) |
| 467 | { |
| 468 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
| 469 | struct dsa_switch *ds = dp->ds; |
| 470 | |
| 471 | if (!ds->ops->phylink_mac_an_restart) |
| 472 | return; |
| 473 | |
| 474 | ds->ops->phylink_mac_an_restart(ds, dp->index); |
| 475 | } |
| 476 | EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_an_restart); |
| 477 | |
| 478 | void dsa_port_phylink_mac_link_down(struct phylink_config *config, |
| 479 | unsigned int mode, |
| 480 | phy_interface_t interface) |
| 481 | { |
| 482 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 483 | struct phy_device *phydev = NULL; |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 484 | struct dsa_switch *ds = dp->ds; |
| 485 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 486 | if (dsa_is_user_port(ds, dp->index)) |
| 487 | phydev = dp->slave->phydev; |
| 488 | |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 489 | if (!ds->ops->phylink_mac_link_down) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 490 | if (ds->ops->adjust_link && phydev) |
| 491 | ds->ops->adjust_link(ds, dp->index, phydev); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | |
| 495 | ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_down); |
| 498 | |
| 499 | void dsa_port_phylink_mac_link_up(struct phylink_config *config, |
| 500 | unsigned int mode, |
| 501 | phy_interface_t interface, |
| 502 | struct phy_device *phydev) |
| 503 | { |
| 504 | struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 505 | struct dsa_switch *ds = dp->ds; |
| 506 | |
| 507 | if (!ds->ops->phylink_mac_link_up) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 508 | if (ds->ops->adjust_link && phydev) |
| 509 | ds->ops->adjust_link(ds, dp->index, phydev); |
Ioana Ciornei | 77373d4 | 2019-05-28 20:38:15 +0300 | [diff] [blame] | 510 | return; |
| 511 | } |
| 512 | |
| 513 | ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev); |
| 514 | } |
| 515 | EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_up); |
| 516 | |
| 517 | const struct phylink_mac_ops dsa_port_phylink_mac_ops = { |
| 518 | .validate = dsa_port_phylink_validate, |
| 519 | .mac_link_state = dsa_port_phylink_mac_link_state, |
| 520 | .mac_config = dsa_port_phylink_mac_config, |
| 521 | .mac_an_restart = dsa_port_phylink_mac_an_restart, |
| 522 | .mac_link_down = dsa_port_phylink_mac_link_down, |
| 523 | .mac_link_up = dsa_port_phylink_mac_link_up, |
| 524 | }; |
| 525 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 526 | static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) |
| 527 | { |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 528 | struct dsa_switch *ds = dp->ds; |
| 529 | struct phy_device *phydev; |
| 530 | int port = dp->index; |
| 531 | int err = 0; |
| 532 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 533 | phydev = dsa_port_get_phy_device(dp); |
| 534 | if (!phydev) |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 535 | return 0; |
| 536 | |
Florian Fainelli | 6207a78 | 2018-04-25 12:12:51 -0700 | [diff] [blame] | 537 | if (IS_ERR(phydev)) |
| 538 | return PTR_ERR(phydev); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 539 | |
| 540 | if (enable) { |
| 541 | err = genphy_config_init(phydev); |
| 542 | if (err < 0) |
| 543 | goto err_put_dev; |
| 544 | |
| 545 | err = genphy_resume(phydev); |
| 546 | if (err < 0) |
| 547 | goto err_put_dev; |
| 548 | |
| 549 | err = genphy_read_status(phydev); |
| 550 | if (err < 0) |
| 551 | goto err_put_dev; |
| 552 | } else { |
| 553 | err = genphy_suspend(phydev); |
| 554 | if (err < 0) |
| 555 | goto err_put_dev; |
| 556 | } |
| 557 | |
| 558 | if (ds->ops->adjust_link) |
| 559 | ds->ops->adjust_link(ds, port, phydev); |
| 560 | |
| 561 | dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev)); |
| 562 | |
| 563 | err_put_dev: |
| 564 | put_device(&phydev->mdio.dev); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 565 | return err; |
| 566 | } |
| 567 | |
| 568 | static int dsa_port_fixed_link_register_of(struct dsa_port *dp) |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 569 | { |
| 570 | struct device_node *dn = dp->dn; |
| 571 | struct dsa_switch *ds = dp->ds; |
| 572 | struct phy_device *phydev; |
| 573 | int port = dp->index; |
| 574 | int mode; |
| 575 | int err; |
| 576 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 577 | err = of_phy_register_fixed_link(dn); |
| 578 | if (err) { |
| 579 | dev_err(ds->dev, |
| 580 | "failed to register the fixed PHY of port %d\n", |
| 581 | port); |
| 582 | return err; |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 583 | } |
| 584 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 585 | phydev = of_phy_find_device(dn); |
| 586 | |
| 587 | mode = of_get_phy_mode(dn); |
| 588 | if (mode < 0) |
| 589 | mode = PHY_INTERFACE_MODE_NA; |
| 590 | phydev->interface = mode; |
| 591 | |
| 592 | genphy_config_init(phydev); |
| 593 | genphy_read_status(phydev); |
| 594 | |
| 595 | if (ds->ops->adjust_link) |
| 596 | ds->ops->adjust_link(ds, port, phydev); |
| 597 | |
| 598 | put_device(&phydev->mdio.dev); |
| 599 | |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 603 | static int dsa_port_phylink_register(struct dsa_port *dp) |
| 604 | { |
| 605 | struct dsa_switch *ds = dp->ds; |
| 606 | struct device_node *port_dn = dp->dn; |
| 607 | int mode, err; |
| 608 | |
| 609 | mode = of_get_phy_mode(port_dn); |
| 610 | if (mode < 0) |
| 611 | mode = PHY_INTERFACE_MODE_NA; |
| 612 | |
| 613 | dp->pl_config.dev = ds->dev; |
| 614 | dp->pl_config.type = PHYLINK_DEV; |
| 615 | |
| 616 | dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), |
| 617 | mode, &dsa_port_phylink_mac_ops); |
| 618 | if (IS_ERR(dp->pl)) { |
| 619 | pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); |
| 620 | return PTR_ERR(dp->pl); |
| 621 | } |
| 622 | |
| 623 | err = phylink_of_phy_connect(dp->pl, port_dn, 0); |
Florian Fainelli | 2131fba | 2019-06-10 12:31:49 -0700 | [diff] [blame] | 624 | if (err && err != -ENODEV) { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 625 | pr_err("could not attach to PHY: %d\n", err); |
| 626 | goto err_phy_connect; |
| 627 | } |
| 628 | |
| 629 | rtnl_lock(); |
| 630 | phylink_start(dp->pl); |
| 631 | rtnl_unlock(); |
| 632 | |
| 633 | return 0; |
| 634 | |
| 635 | err_phy_connect: |
| 636 | phylink_destroy(dp->pl); |
| 637 | return err; |
| 638 | } |
| 639 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 640 | int dsa_port_link_register_of(struct dsa_port *dp) |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 641 | { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 642 | struct dsa_switch *ds = dp->ds; |
| 643 | |
| 644 | if (!ds->ops->adjust_link) |
| 645 | return dsa_port_phylink_register(dp); |
| 646 | |
| 647 | dev_warn(ds->dev, |
| 648 | "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); |
| 649 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 650 | if (of_phy_is_fixed_link(dp->dn)) |
| 651 | return dsa_port_fixed_link_register_of(dp); |
| 652 | else |
| 653 | return dsa_port_setup_phy_of(dp, true); |
| 654 | } |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 655 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 656 | void dsa_port_link_unregister_of(struct dsa_port *dp) |
| 657 | { |
Ioana Ciornei | 0e27921 | 2019-05-28 20:38:16 +0300 | [diff] [blame] | 658 | struct dsa_switch *ds = dp->ds; |
| 659 | |
| 660 | if (!ds->ops->adjust_link) { |
| 661 | rtnl_lock(); |
| 662 | phylink_disconnect_phy(dp->pl); |
| 663 | rtnl_unlock(); |
| 664 | phylink_destroy(dp->pl); |
| 665 | return; |
| 666 | } |
| 667 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 668 | if (of_phy_is_fixed_link(dp->dn)) |
| 669 | of_phy_deregister_fixed_link(dp->dn); |
| 670 | else |
| 671 | dsa_port_setup_phy_of(dp, false); |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 672 | } |
Florian Fainelli | cf96357 | 2018-04-25 12:12:52 -0700 | [diff] [blame] | 673 | |
| 674 | int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data) |
| 675 | { |
| 676 | struct phy_device *phydev; |
| 677 | int ret = -EOPNOTSUPP; |
| 678 | |
| 679 | if (of_phy_is_fixed_link(dp->dn)) |
| 680 | return ret; |
| 681 | |
| 682 | phydev = dsa_port_get_phy_device(dp); |
| 683 | if (IS_ERR_OR_NULL(phydev)) |
| 684 | return ret; |
| 685 | |
| 686 | ret = phy_ethtool_get_strings(phydev, data); |
| 687 | put_device(&phydev->mdio.dev); |
| 688 | |
| 689 | return ret; |
| 690 | } |
| 691 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings); |
| 692 | |
| 693 | int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data) |
| 694 | { |
| 695 | struct phy_device *phydev; |
| 696 | int ret = -EOPNOTSUPP; |
| 697 | |
| 698 | if (of_phy_is_fixed_link(dp->dn)) |
| 699 | return ret; |
| 700 | |
| 701 | phydev = dsa_port_get_phy_device(dp); |
| 702 | if (IS_ERR_OR_NULL(phydev)) |
| 703 | return ret; |
| 704 | |
| 705 | ret = phy_ethtool_get_stats(phydev, NULL, data); |
| 706 | put_device(&phydev->mdio.dev); |
| 707 | |
| 708 | return ret; |
| 709 | } |
| 710 | EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats); |
| 711 | |
| 712 | int dsa_port_get_phy_sset_count(struct dsa_port *dp) |
| 713 | { |
| 714 | struct phy_device *phydev; |
| 715 | int ret = -EOPNOTSUPP; |
| 716 | |
| 717 | if (of_phy_is_fixed_link(dp->dn)) |
| 718 | return ret; |
| 719 | |
| 720 | phydev = dsa_port_get_phy_device(dp); |
| 721 | if (IS_ERR_OR_NULL(phydev)) |
| 722 | return ret; |
| 723 | |
| 724 | ret = phy_ethtool_get_sset_count(phydev); |
| 725 | put_device(&phydev->mdio.dev); |
| 726 | |
| 727 | return ret; |
| 728 | } |
| 729 | EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count); |