Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * phylink models the MAC to optional PHY connection, supporting |
| 3 | * technologies such as SFP cages where the PHY is hot-pluggable. |
| 4 | * |
| 5 | * Copyright (C) 2015 Russell King |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/ethtool.h> |
| 12 | #include <linux/export.h> |
| 13 | #include <linux/gpio/consumer.h> |
| 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/of_mdio.h> |
| 17 | #include <linux/phy.h> |
| 18 | #include <linux/phy_fixed.h> |
| 19 | #include <linux/phylink.h> |
| 20 | #include <linux/rtnetlink.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 24 | #include "sfp.h" |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 25 | #include "swphy.h" |
| 26 | |
| 27 | #define SUPPORTED_INTERFACES \ |
| 28 | (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \ |
| 29 | SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane) |
| 30 | #define ADVERTISED_INTERFACES \ |
| 31 | (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \ |
| 32 | ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane) |
| 33 | |
| 34 | enum { |
| 35 | PHYLINK_DISABLE_STOPPED, |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 36 | PHYLINK_DISABLE_LINK, |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 39 | /** |
| 40 | * struct phylink - internal data type for phylink |
| 41 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 42 | struct phylink { |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 43 | /* private: */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 44 | struct net_device *netdev; |
| 45 | const struct phylink_mac_ops *ops; |
| 46 | |
| 47 | unsigned long phylink_disable_state; /* bitmask of disables */ |
| 48 | struct phy_device *phydev; |
| 49 | phy_interface_t link_interface; /* PHY_INTERFACE_xxx */ |
| 50 | u8 link_an_mode; /* MLO_AN_xxx */ |
| 51 | u8 link_port; /* The current non-phy ethtool port */ |
| 52 | __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); |
| 53 | |
| 54 | /* The link configuration settings */ |
| 55 | struct phylink_link_state link_config; |
| 56 | struct gpio_desc *link_gpio; |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 57 | void (*get_fixed_state)(struct net_device *dev, |
| 58 | struct phylink_link_state *s); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 59 | |
| 60 | struct mutex state_mutex; |
| 61 | struct phylink_link_state phy_state; |
| 62 | struct work_struct resolve; |
| 63 | |
| 64 | bool mac_link_dropped; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 65 | |
| 66 | struct sfp_bus *sfp_bus; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static inline void linkmode_zero(unsigned long *dst) |
| 70 | { |
| 71 | bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 72 | } |
| 73 | |
| 74 | static inline void linkmode_copy(unsigned long *dst, const unsigned long *src) |
| 75 | { |
| 76 | bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 77 | } |
| 78 | |
| 79 | static inline void linkmode_and(unsigned long *dst, const unsigned long *a, |
| 80 | const unsigned long *b) |
| 81 | { |
| 82 | bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 83 | } |
| 84 | |
| 85 | static inline void linkmode_or(unsigned long *dst, const unsigned long *a, |
| 86 | const unsigned long *b) |
| 87 | { |
| 88 | bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 89 | } |
| 90 | |
| 91 | static inline bool linkmode_empty(const unsigned long *src) |
| 92 | { |
| 93 | return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 94 | } |
| 95 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 96 | /** |
| 97 | * phylink_set_port_modes() - set the port type modes in the ethtool mask |
| 98 | * @mask: ethtool link mode mask |
| 99 | * |
| 100 | * Sets all the port type modes in the ethtool mask. MAC drivers should |
| 101 | * use this in their 'validate' callback. |
| 102 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 103 | void phylink_set_port_modes(unsigned long *mask) |
| 104 | { |
| 105 | phylink_set(mask, TP); |
| 106 | phylink_set(mask, AUI); |
| 107 | phylink_set(mask, MII); |
| 108 | phylink_set(mask, FIBRE); |
| 109 | phylink_set(mask, BNC); |
| 110 | phylink_set(mask, Backplane); |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(phylink_set_port_modes); |
| 113 | |
| 114 | static int phylink_is_empty_linkmode(const unsigned long *linkmode) |
| 115 | { |
| 116 | __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, }; |
| 117 | |
| 118 | phylink_set_port_modes(tmp); |
| 119 | phylink_set(tmp, Autoneg); |
| 120 | phylink_set(tmp, Pause); |
| 121 | phylink_set(tmp, Asym_Pause); |
| 122 | |
| 123 | bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 124 | |
| 125 | return linkmode_empty(tmp); |
| 126 | } |
| 127 | |
| 128 | static const char *phylink_an_mode_str(unsigned int mode) |
| 129 | { |
| 130 | static const char *modestr[] = { |
| 131 | [MLO_AN_PHY] = "phy", |
| 132 | [MLO_AN_FIXED] = "fixed", |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 133 | [MLO_AN_INBAND] = "inband", |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown"; |
| 137 | } |
| 138 | |
| 139 | static int phylink_validate(struct phylink *pl, unsigned long *supported, |
| 140 | struct phylink_link_state *state) |
| 141 | { |
| 142 | pl->ops->validate(pl->netdev, supported, state); |
| 143 | |
| 144 | return phylink_is_empty_linkmode(supported) ? -EINVAL : 0; |
| 145 | } |
| 146 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 147 | static int phylink_parse_fixedlink(struct phylink *pl, |
| 148 | struct fwnode_handle *fwnode) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 149 | { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 150 | struct fwnode_handle *fixed_node; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 151 | const struct phy_setting *s; |
| 152 | struct gpio_desc *desc; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 153 | u32 speed; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 154 | int ret; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 155 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 156 | fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 157 | if (fixed_node) { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 158 | ret = fwnode_property_read_u32(fixed_node, "speed", &speed); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 159 | |
| 160 | pl->link_config.speed = speed; |
| 161 | pl->link_config.duplex = DUPLEX_HALF; |
| 162 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 163 | if (fwnode_property_read_bool(fixed_node, "full-duplex")) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 164 | pl->link_config.duplex = DUPLEX_FULL; |
| 165 | |
| 166 | /* We treat the "pause" and "asym-pause" terminology as |
| 167 | * defining the link partner's ability. */ |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 168 | if (fwnode_property_read_bool(fixed_node, "pause")) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 169 | pl->link_config.pause |= MLO_PAUSE_SYM; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 170 | if (fwnode_property_read_bool(fixed_node, "asym-pause")) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 171 | pl->link_config.pause |= MLO_PAUSE_ASYM; |
| 172 | |
| 173 | if (ret == 0) { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 174 | desc = fwnode_get_named_gpiod(fixed_node, "link-gpios", |
| 175 | 0, GPIOD_IN, "?"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 176 | |
| 177 | if (!IS_ERR(desc)) |
| 178 | pl->link_gpio = desc; |
| 179 | else if (desc == ERR_PTR(-EPROBE_DEFER)) |
| 180 | ret = -EPROBE_DEFER; |
| 181 | } |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 182 | fwnode_handle_put(fixed_node); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 183 | |
| 184 | if (ret) |
| 185 | return ret; |
| 186 | } else { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 187 | u32 prop[5]; |
| 188 | |
| 189 | ret = fwnode_property_read_u32_array(fwnode, "fixed-link", |
| 190 | NULL, 0); |
| 191 | if (ret != ARRAY_SIZE(prop)) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 192 | netdev_err(pl->netdev, "broken fixed-link?\n"); |
| 193 | return -EINVAL; |
| 194 | } |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 195 | |
| 196 | ret = fwnode_property_read_u32_array(fwnode, "fixed-link", |
| 197 | prop, ARRAY_SIZE(prop)); |
| 198 | if (!ret) { |
| 199 | pl->link_config.duplex = prop[1] ? |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 200 | DUPLEX_FULL : DUPLEX_HALF; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 201 | pl->link_config.speed = prop[2]; |
| 202 | if (prop[3]) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 203 | pl->link_config.pause |= MLO_PAUSE_SYM; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 204 | if (prop[4]) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 205 | pl->link_config.pause |= MLO_PAUSE_ASYM; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (pl->link_config.speed > SPEED_1000 && |
| 210 | pl->link_config.duplex != DUPLEX_FULL) |
| 211 | netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n", |
| 212 | pl->link_config.speed); |
| 213 | |
| 214 | bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 215 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 216 | phylink_validate(pl, pl->supported, &pl->link_config); |
| 217 | |
| 218 | s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex, |
| 219 | pl->supported, |
| 220 | __ETHTOOL_LINK_MODE_MASK_NBITS, true); |
| 221 | linkmode_zero(pl->supported); |
| 222 | phylink_set(pl->supported, MII); |
| 223 | if (s) { |
| 224 | __set_bit(s->bit, pl->supported); |
| 225 | } else { |
| 226 | netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n", |
| 227 | pl->link_config.duplex == DUPLEX_FULL ? "full" : "half", |
| 228 | pl->link_config.speed); |
| 229 | } |
| 230 | |
| 231 | linkmode_and(pl->link_config.advertising, pl->link_config.advertising, |
| 232 | pl->supported); |
| 233 | |
| 234 | pl->link_config.link = 1; |
| 235 | pl->link_config.an_complete = 1; |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 240 | static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 241 | { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 242 | struct fwnode_handle *dn; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 243 | const char *managed; |
| 244 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 245 | dn = fwnode_get_named_child_node(fwnode, "fixed-link"); |
| 246 | if (dn || fwnode_property_present(fwnode, "fixed-link")) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 247 | pl->link_an_mode = MLO_AN_FIXED; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 248 | fwnode_handle_put(dn); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 249 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 250 | if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 && |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 251 | strcmp(managed, "in-band-status") == 0) { |
| 252 | if (pl->link_an_mode == MLO_AN_FIXED) { |
| 253 | netdev_err(pl->netdev, |
| 254 | "can't use both fixed-link and in-band-status\n"); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | linkmode_zero(pl->supported); |
| 259 | phylink_set(pl->supported, MII); |
| 260 | phylink_set(pl->supported, Autoneg); |
| 261 | phylink_set(pl->supported, Asym_Pause); |
| 262 | phylink_set(pl->supported, Pause); |
| 263 | pl->link_config.an_enabled = true; |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 264 | pl->link_an_mode = MLO_AN_INBAND; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 265 | |
| 266 | switch (pl->link_config.interface) { |
| 267 | case PHY_INTERFACE_MODE_SGMII: |
| 268 | phylink_set(pl->supported, 10baseT_Half); |
| 269 | phylink_set(pl->supported, 10baseT_Full); |
| 270 | phylink_set(pl->supported, 100baseT_Half); |
| 271 | phylink_set(pl->supported, 100baseT_Full); |
| 272 | phylink_set(pl->supported, 1000baseT_Half); |
| 273 | phylink_set(pl->supported, 1000baseT_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 274 | break; |
| 275 | |
| 276 | case PHY_INTERFACE_MODE_1000BASEX: |
| 277 | phylink_set(pl->supported, 1000baseX_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 278 | break; |
| 279 | |
| 280 | case PHY_INTERFACE_MODE_2500BASEX: |
| 281 | phylink_set(pl->supported, 2500baseX_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 282 | break; |
| 283 | |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 284 | case PHY_INTERFACE_MODE_10GKR: |
| 285 | phylink_set(pl->supported, 10baseT_Half); |
| 286 | phylink_set(pl->supported, 10baseT_Full); |
| 287 | phylink_set(pl->supported, 100baseT_Half); |
| 288 | phylink_set(pl->supported, 100baseT_Full); |
| 289 | phylink_set(pl->supported, 1000baseT_Half); |
| 290 | phylink_set(pl->supported, 1000baseT_Full); |
| 291 | phylink_set(pl->supported, 1000baseX_Full); |
| 292 | phylink_set(pl->supported, 10000baseKR_Full); |
| 293 | phylink_set(pl->supported, 10000baseCR_Full); |
| 294 | phylink_set(pl->supported, 10000baseSR_Full); |
| 295 | phylink_set(pl->supported, 10000baseLR_Full); |
| 296 | phylink_set(pl->supported, 10000baseLRM_Full); |
| 297 | phylink_set(pl->supported, 10000baseER_Full); |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 298 | break; |
| 299 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 300 | default: |
| 301 | netdev_err(pl->netdev, |
| 302 | "incorrect link mode %s for in-band status\n", |
| 303 | phy_modes(pl->link_config.interface)); |
| 304 | return -EINVAL; |
| 305 | } |
| 306 | |
| 307 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 308 | |
| 309 | if (phylink_validate(pl, pl->supported, &pl->link_config)) { |
| 310 | netdev_err(pl->netdev, |
| 311 | "failed to validate link configuration for in-band status\n"); |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static void phylink_mac_config(struct phylink *pl, |
| 320 | const struct phylink_link_state *state) |
| 321 | { |
| 322 | netdev_dbg(pl->netdev, |
| 323 | "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n", |
| 324 | __func__, phylink_an_mode_str(pl->link_an_mode), |
| 325 | phy_modes(state->interface), |
| 326 | phy_speed_to_str(state->speed), |
| 327 | phy_duplex_to_str(state->duplex), |
| 328 | __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising, |
| 329 | state->pause, state->link, state->an_enabled); |
| 330 | |
| 331 | pl->ops->mac_config(pl->netdev, pl->link_an_mode, state); |
| 332 | } |
| 333 | |
| 334 | static void phylink_mac_an_restart(struct phylink *pl) |
| 335 | { |
| 336 | if (pl->link_config.an_enabled && |
Russell King | 365c1e6 | 2017-12-01 10:24:16 +0000 | [diff] [blame] | 337 | phy_interface_mode_is_8023z(pl->link_config.interface)) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 338 | pl->ops->mac_an_restart(pl->netdev); |
| 339 | } |
| 340 | |
| 341 | static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state) |
| 342 | { |
| 343 | struct net_device *ndev = pl->netdev; |
| 344 | |
| 345 | linkmode_copy(state->advertising, pl->link_config.advertising); |
| 346 | linkmode_zero(state->lp_advertising); |
| 347 | state->interface = pl->link_config.interface; |
| 348 | state->an_enabled = pl->link_config.an_enabled; |
| 349 | state->link = 1; |
| 350 | |
| 351 | return pl->ops->mac_link_state(ndev, state); |
| 352 | } |
| 353 | |
| 354 | /* The fixed state is... fixed except for the link state, |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 355 | * which may be determined by a GPIO or a callback. |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 356 | */ |
| 357 | static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state) |
| 358 | { |
| 359 | *state = pl->link_config; |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 360 | if (pl->get_fixed_state) |
| 361 | pl->get_fixed_state(pl->netdev, state); |
| 362 | else if (pl->link_gpio) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 363 | state->link = !!gpiod_get_value(pl->link_gpio); |
| 364 | } |
| 365 | |
| 366 | /* Flow control is resolved according to our and the link partners |
| 367 | * advertisments using the following drawn from the 802.3 specs: |
| 368 | * Local device Link partner |
| 369 | * Pause AsymDir Pause AsymDir Result |
| 370 | * 1 X 1 X TX+RX |
| 371 | * 0 1 1 1 RX |
| 372 | * 1 1 0 1 TX |
| 373 | */ |
| 374 | static void phylink_resolve_flow(struct phylink *pl, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 375 | struct phylink_link_state *state) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 376 | { |
| 377 | int new_pause = 0; |
| 378 | |
| 379 | if (pl->link_config.pause & MLO_PAUSE_AN) { |
| 380 | int pause = 0; |
| 381 | |
| 382 | if (phylink_test(pl->link_config.advertising, Pause)) |
| 383 | pause |= MLO_PAUSE_SYM; |
| 384 | if (phylink_test(pl->link_config.advertising, Asym_Pause)) |
| 385 | pause |= MLO_PAUSE_ASYM; |
| 386 | |
| 387 | pause &= state->pause; |
| 388 | |
| 389 | if (pause & MLO_PAUSE_SYM) |
| 390 | new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX; |
| 391 | else if (pause & MLO_PAUSE_ASYM) |
| 392 | new_pause = state->pause & MLO_PAUSE_SYM ? |
| 393 | MLO_PAUSE_RX : MLO_PAUSE_TX; |
| 394 | } else { |
| 395 | new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK; |
| 396 | } |
| 397 | |
| 398 | state->pause &= ~MLO_PAUSE_TXRX_MASK; |
| 399 | state->pause |= new_pause; |
| 400 | } |
| 401 | |
| 402 | static const char *phylink_pause_to_str(int pause) |
| 403 | { |
| 404 | switch (pause & MLO_PAUSE_TXRX_MASK) { |
| 405 | case MLO_PAUSE_TX | MLO_PAUSE_RX: |
| 406 | return "rx/tx"; |
| 407 | case MLO_PAUSE_TX: |
| 408 | return "tx"; |
| 409 | case MLO_PAUSE_RX: |
| 410 | return "rx"; |
| 411 | default: |
| 412 | return "off"; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | static void phylink_resolve(struct work_struct *w) |
| 417 | { |
| 418 | struct phylink *pl = container_of(w, struct phylink, resolve); |
| 419 | struct phylink_link_state link_state; |
| 420 | struct net_device *ndev = pl->netdev; |
| 421 | |
| 422 | mutex_lock(&pl->state_mutex); |
| 423 | if (pl->phylink_disable_state) { |
| 424 | pl->mac_link_dropped = false; |
| 425 | link_state.link = false; |
| 426 | } else if (pl->mac_link_dropped) { |
| 427 | link_state.link = false; |
| 428 | } else { |
| 429 | switch (pl->link_an_mode) { |
| 430 | case MLO_AN_PHY: |
| 431 | link_state = pl->phy_state; |
| 432 | phylink_resolve_flow(pl, &link_state); |
| 433 | phylink_mac_config(pl, &link_state); |
| 434 | break; |
| 435 | |
| 436 | case MLO_AN_FIXED: |
| 437 | phylink_get_fixed_state(pl, &link_state); |
| 438 | phylink_mac_config(pl, &link_state); |
| 439 | break; |
| 440 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 441 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 442 | phylink_get_mac_state(pl, &link_state); |
| 443 | if (pl->phydev) { |
| 444 | bool changed = false; |
| 445 | |
| 446 | link_state.link = link_state.link && |
| 447 | pl->phy_state.link; |
| 448 | |
| 449 | if (pl->phy_state.interface != |
| 450 | link_state.interface) { |
| 451 | link_state.interface = pl->phy_state.interface; |
| 452 | changed = true; |
| 453 | } |
| 454 | |
| 455 | /* Propagate the flow control from the PHY |
| 456 | * to the MAC. Also propagate the interface |
| 457 | * if changed. |
| 458 | */ |
| 459 | if (pl->phy_state.link || changed) { |
| 460 | link_state.pause |= pl->phy_state.pause; |
| 461 | phylink_resolve_flow(pl, &link_state); |
| 462 | |
| 463 | phylink_mac_config(pl, &link_state); |
| 464 | } |
| 465 | } |
| 466 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | if (link_state.link != netif_carrier_ok(ndev)) { |
| 471 | if (!link_state.link) { |
| 472 | netif_carrier_off(ndev); |
| 473 | pl->ops->mac_link_down(ndev, pl->link_an_mode); |
| 474 | netdev_info(ndev, "Link is Down\n"); |
| 475 | } else { |
| 476 | pl->ops->mac_link_up(ndev, pl->link_an_mode, |
| 477 | pl->phydev); |
| 478 | |
| 479 | netif_carrier_on(ndev); |
| 480 | |
| 481 | netdev_info(ndev, |
| 482 | "Link is Up - %s/%s - flow control %s\n", |
| 483 | phy_speed_to_str(link_state.speed), |
| 484 | phy_duplex_to_str(link_state.duplex), |
| 485 | phylink_pause_to_str(link_state.pause)); |
| 486 | } |
| 487 | } |
| 488 | if (!link_state.link && pl->mac_link_dropped) { |
| 489 | pl->mac_link_dropped = false; |
| 490 | queue_work(system_power_efficient_wq, &pl->resolve); |
| 491 | } |
| 492 | mutex_unlock(&pl->state_mutex); |
| 493 | } |
| 494 | |
| 495 | static void phylink_run_resolve(struct phylink *pl) |
| 496 | { |
| 497 | if (!pl->phylink_disable_state) |
| 498 | queue_work(system_power_efficient_wq, &pl->resolve); |
| 499 | } |
| 500 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 501 | static const struct sfp_upstream_ops sfp_phylink_ops; |
| 502 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 503 | static int phylink_register_sfp(struct phylink *pl, |
| 504 | struct fwnode_handle *fwnode) |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 505 | { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 506 | struct fwnode_reference_args ref; |
| 507 | int ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 508 | |
Florian Fainelli | 0247780 | 2017-12-14 15:57:58 -0800 | [diff] [blame] | 509 | if (!fwnode) |
| 510 | return 0; |
| 511 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 512 | ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL, |
| 513 | 0, 0, &ref); |
| 514 | if (ret < 0) { |
| 515 | if (ret == -ENOENT) |
| 516 | return 0; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 517 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 518 | netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n", |
| 519 | ret); |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl, |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 524 | &sfp_phylink_ops); |
| 525 | if (!pl->sfp_bus) |
| 526 | return -ENOMEM; |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 531 | /** |
| 532 | * phylink_create() - create a phylink instance |
| 533 | * @ndev: a pointer to the &struct net_device |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 534 | * @fwnode: a pointer to a &struct fwnode_handle describing the network |
| 535 | * interface |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 536 | * @iface: the desired link mode defined by &typedef phy_interface_t |
| 537 | * @ops: a pointer to a &struct phylink_mac_ops for the MAC. |
| 538 | * |
| 539 | * Create a new phylink instance, and parse the link parameters found in @np. |
| 540 | * This will parse in-band modes, fixed-link or SFP configuration. |
| 541 | * |
| 542 | * Returns a pointer to a &struct phylink, or an error-pointer value. Users |
| 543 | * must use IS_ERR() to check for errors from this function. |
| 544 | */ |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 545 | struct phylink *phylink_create(struct net_device *ndev, |
| 546 | struct fwnode_handle *fwnode, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 547 | phy_interface_t iface, |
| 548 | const struct phylink_mac_ops *ops) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 549 | { |
| 550 | struct phylink *pl; |
| 551 | int ret; |
| 552 | |
| 553 | pl = kzalloc(sizeof(*pl), GFP_KERNEL); |
| 554 | if (!pl) |
| 555 | return ERR_PTR(-ENOMEM); |
| 556 | |
| 557 | mutex_init(&pl->state_mutex); |
| 558 | INIT_WORK(&pl->resolve, phylink_resolve); |
| 559 | pl->netdev = ndev; |
| 560 | pl->phy_state.interface = iface; |
| 561 | pl->link_interface = iface; |
Florian Fainelli | 4be11ef | 2017-12-12 16:00:29 -0800 | [diff] [blame] | 562 | if (iface == PHY_INTERFACE_MODE_MOCA) |
| 563 | pl->link_port = PORT_BNC; |
| 564 | else |
| 565 | pl->link_port = PORT_MII; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 566 | pl->link_config.interface = iface; |
| 567 | pl->link_config.pause = MLO_PAUSE_AN; |
| 568 | pl->link_config.speed = SPEED_UNKNOWN; |
| 569 | pl->link_config.duplex = DUPLEX_UNKNOWN; |
| 570 | pl->ops = ops; |
| 571 | __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); |
| 572 | |
| 573 | bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 574 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 575 | phylink_validate(pl, pl->supported, &pl->link_config); |
| 576 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 577 | ret = phylink_parse_mode(pl, fwnode); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 578 | if (ret < 0) { |
| 579 | kfree(pl); |
| 580 | return ERR_PTR(ret); |
| 581 | } |
| 582 | |
| 583 | if (pl->link_an_mode == MLO_AN_FIXED) { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 584 | ret = phylink_parse_fixedlink(pl, fwnode); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 585 | if (ret < 0) { |
| 586 | kfree(pl); |
| 587 | return ERR_PTR(ret); |
| 588 | } |
| 589 | } |
| 590 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 591 | ret = phylink_register_sfp(pl, fwnode); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 592 | if (ret < 0) { |
| 593 | kfree(pl); |
| 594 | return ERR_PTR(ret); |
| 595 | } |
| 596 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 597 | return pl; |
| 598 | } |
| 599 | EXPORT_SYMBOL_GPL(phylink_create); |
| 600 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 601 | /** |
| 602 | * phylink_destroy() - cleanup and destroy the phylink instance |
| 603 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 604 | * |
| 605 | * Destroy a phylink instance. Any PHY that has been attached must have been |
| 606 | * cleaned up via phylink_disconnect_phy() prior to calling this function. |
| 607 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 608 | void phylink_destroy(struct phylink *pl) |
| 609 | { |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 610 | if (pl->sfp_bus) |
| 611 | sfp_unregister_upstream(pl->sfp_bus); |
| 612 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 613 | cancel_work_sync(&pl->resolve); |
| 614 | kfree(pl); |
| 615 | } |
| 616 | EXPORT_SYMBOL_GPL(phylink_destroy); |
| 617 | |
Wei Yongjun | 1ec6e53 | 2017-11-02 11:14:48 +0000 | [diff] [blame] | 618 | static void phylink_phy_change(struct phy_device *phydev, bool up, |
| 619 | bool do_carrier) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 620 | { |
| 621 | struct phylink *pl = phydev->phylink; |
| 622 | |
| 623 | mutex_lock(&pl->state_mutex); |
| 624 | pl->phy_state.speed = phydev->speed; |
| 625 | pl->phy_state.duplex = phydev->duplex; |
| 626 | pl->phy_state.pause = MLO_PAUSE_NONE; |
| 627 | if (phydev->pause) |
| 628 | pl->phy_state.pause |= MLO_PAUSE_SYM; |
| 629 | if (phydev->asym_pause) |
| 630 | pl->phy_state.pause |= MLO_PAUSE_ASYM; |
| 631 | pl->phy_state.interface = phydev->interface; |
| 632 | pl->phy_state.link = up; |
| 633 | mutex_unlock(&pl->state_mutex); |
| 634 | |
| 635 | phylink_run_resolve(pl); |
| 636 | |
| 637 | netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down", |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 638 | phy_modes(phydev->interface), |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 639 | phy_speed_to_str(phydev->speed), |
| 640 | phy_duplex_to_str(phydev->duplex)); |
| 641 | } |
| 642 | |
| 643 | static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy) |
| 644 | { |
| 645 | struct phylink_link_state config; |
| 646 | __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); |
| 647 | u32 advertising; |
| 648 | int ret; |
| 649 | |
| 650 | memset(&config, 0, sizeof(config)); |
| 651 | ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported); |
| 652 | ethtool_convert_legacy_u32_to_link_mode(config.advertising, |
| 653 | phy->advertising); |
| 654 | config.interface = pl->link_config.interface; |
| 655 | |
| 656 | /* |
| 657 | * This is the new way of dealing with flow control for PHYs, |
| 658 | * as described by Timur Tabi in commit 529ed1275263 ("net: phy: |
| 659 | * phy drivers should not set SUPPORTED_[Asym_]Pause") except |
| 660 | * using our validate call to the MAC, we rely upon the MAC |
| 661 | * clearing the bits from both supported and advertising fields. |
| 662 | */ |
| 663 | if (phylink_test(supported, Pause)) |
| 664 | phylink_set(config.advertising, Pause); |
| 665 | if (phylink_test(supported, Asym_Pause)) |
| 666 | phylink_set(config.advertising, Asym_Pause); |
| 667 | |
| 668 | ret = phylink_validate(pl, supported, &config); |
| 669 | if (ret) |
| 670 | return ret; |
| 671 | |
| 672 | phy->phylink = pl; |
| 673 | phy->phy_link_change = phylink_phy_change; |
| 674 | |
| 675 | netdev_info(pl->netdev, |
| 676 | "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev), |
| 677 | phy->drv->name); |
| 678 | |
| 679 | mutex_lock(&phy->lock); |
| 680 | mutex_lock(&pl->state_mutex); |
| 681 | pl->netdev->phydev = phy; |
| 682 | pl->phydev = phy; |
| 683 | linkmode_copy(pl->supported, supported); |
| 684 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 685 | |
| 686 | /* Restrict the phy advertisment according to the MAC support. */ |
| 687 | ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising); |
| 688 | phy->advertising = advertising; |
| 689 | mutex_unlock(&pl->state_mutex); |
| 690 | mutex_unlock(&phy->lock); |
| 691 | |
| 692 | netdev_dbg(pl->netdev, |
| 693 | "phy: setting supported %*pb advertising 0x%08x\n", |
| 694 | __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported, |
| 695 | phy->advertising); |
| 696 | |
| 697 | phy_start_machine(phy); |
| 698 | if (phy->irq > 0) |
| 699 | phy_start_interrupts(phy); |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 704 | /** |
| 705 | * phylink_connect_phy() - connect a PHY to the phylink instance |
| 706 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 707 | * @phy: a pointer to a &struct phy_device. |
| 708 | * |
| 709 | * Connect @phy to the phylink instance specified by @pl by calling |
| 710 | * phy_attach_direct(). Configure the @phy according to the MAC driver's |
| 711 | * capabilities, start the PHYLIB state machine and enable any interrupts |
| 712 | * that the PHY supports. |
| 713 | * |
| 714 | * This updates the phylink's ethtool supported and advertising link mode |
| 715 | * masks. |
| 716 | * |
| 717 | * Returns 0 on success or a negative errno. |
| 718 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 719 | int phylink_connect_phy(struct phylink *pl, struct phy_device *phy) |
| 720 | { |
| 721 | int ret; |
| 722 | |
Russell King | cf4f267 | 2017-12-01 10:24:21 +0000 | [diff] [blame] | 723 | if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED || |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 724 | (pl->link_an_mode == MLO_AN_INBAND && |
| 725 | phy_interface_mode_is_8023z(pl->link_interface)))) |
Russell King | cf4f267 | 2017-12-01 10:24:21 +0000 | [diff] [blame] | 726 | return -EINVAL; |
| 727 | |
Florian Fainelli | 4904b6e | 2017-12-12 16:00:26 -0800 | [diff] [blame] | 728 | /* Use PHY device/driver interface */ |
| 729 | if (pl->link_interface == PHY_INTERFACE_MODE_NA) { |
| 730 | pl->link_interface = phy->interface; |
| 731 | pl->link_config.interface = pl->link_interface; |
| 732 | } |
| 733 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 734 | ret = phy_attach_direct(pl->netdev, phy, 0, pl->link_interface); |
| 735 | if (ret) |
| 736 | return ret; |
| 737 | |
| 738 | ret = phylink_bringup_phy(pl, phy); |
| 739 | if (ret) |
| 740 | phy_detach(phy); |
| 741 | |
| 742 | return ret; |
| 743 | } |
| 744 | EXPORT_SYMBOL_GPL(phylink_connect_phy); |
| 745 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 746 | /** |
| 747 | * phylink_of_phy_connect() - connect the PHY specified in the DT mode. |
| 748 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 749 | * @dn: a pointer to a &struct device_node. |
Florian Fainelli | 0a62964 | 2017-12-12 16:00:25 -0800 | [diff] [blame] | 750 | * @flags: PHY-specific flags to communicate to the PHY device driver |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 751 | * |
| 752 | * Connect the phy specified in the device node @dn to the phylink instance |
| 753 | * specified by @pl. Actions specified in phylink_connect_phy() will be |
| 754 | * performed. |
| 755 | * |
| 756 | * Returns 0 on success or a negative errno. |
| 757 | */ |
Florian Fainelli | 0a62964 | 2017-12-12 16:00:25 -0800 | [diff] [blame] | 758 | int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn, |
| 759 | u32 flags) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 760 | { |
| 761 | struct device_node *phy_node; |
| 762 | struct phy_device *phy_dev; |
| 763 | int ret; |
| 764 | |
Russell King | cf4f267 | 2017-12-01 10:24:21 +0000 | [diff] [blame] | 765 | /* Fixed links and 802.3z are handled without needing a PHY */ |
| 766 | if (pl->link_an_mode == MLO_AN_FIXED || |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 767 | (pl->link_an_mode == MLO_AN_INBAND && |
| 768 | phy_interface_mode_is_8023z(pl->link_interface))) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 769 | return 0; |
| 770 | |
| 771 | phy_node = of_parse_phandle(dn, "phy-handle", 0); |
| 772 | if (!phy_node) |
| 773 | phy_node = of_parse_phandle(dn, "phy", 0); |
| 774 | if (!phy_node) |
| 775 | phy_node = of_parse_phandle(dn, "phy-device", 0); |
| 776 | |
| 777 | if (!phy_node) { |
Florian Fainelli | d38b4afd | 2017-12-12 16:00:27 -0800 | [diff] [blame] | 778 | if (pl->link_an_mode == MLO_AN_PHY) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 779 | return -ENODEV; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
Florian Fainelli | 0a62964 | 2017-12-12 16:00:25 -0800 | [diff] [blame] | 783 | phy_dev = of_phy_attach(pl->netdev, phy_node, flags, |
| 784 | pl->link_interface); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 785 | /* We're done with the phy_node handle */ |
| 786 | of_node_put(phy_node); |
| 787 | |
| 788 | if (!phy_dev) |
| 789 | return -ENODEV; |
| 790 | |
| 791 | ret = phylink_bringup_phy(pl, phy_dev); |
| 792 | if (ret) |
| 793 | phy_detach(phy_dev); |
| 794 | |
| 795 | return ret; |
| 796 | } |
| 797 | EXPORT_SYMBOL_GPL(phylink_of_phy_connect); |
| 798 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 799 | /** |
| 800 | * phylink_disconnect_phy() - disconnect any PHY attached to the phylink |
| 801 | * instance. |
| 802 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 803 | * |
| 804 | * Disconnect any current PHY from the phylink instance described by @pl. |
| 805 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 806 | void phylink_disconnect_phy(struct phylink *pl) |
| 807 | { |
| 808 | struct phy_device *phy; |
| 809 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 810 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 811 | |
| 812 | phy = pl->phydev; |
| 813 | if (phy) { |
| 814 | mutex_lock(&phy->lock); |
| 815 | mutex_lock(&pl->state_mutex); |
| 816 | pl->netdev->phydev = NULL; |
| 817 | pl->phydev = NULL; |
| 818 | mutex_unlock(&pl->state_mutex); |
| 819 | mutex_unlock(&phy->lock); |
| 820 | flush_work(&pl->resolve); |
| 821 | |
| 822 | phy_disconnect(phy); |
| 823 | } |
| 824 | } |
| 825 | EXPORT_SYMBOL_GPL(phylink_disconnect_phy); |
| 826 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 827 | /** |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 828 | * phylink_fixed_state_cb() - allow setting a fixed link callback |
| 829 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 830 | * @cb: callback to execute to determine the fixed link state. |
| 831 | * |
| 832 | * The MAC driver should call this driver when the state of its link |
| 833 | * can be determined through e.g: an out of band MMIO register. |
| 834 | */ |
| 835 | int phylink_fixed_state_cb(struct phylink *pl, |
| 836 | void (*cb)(struct net_device *dev, |
| 837 | struct phylink_link_state *state)) |
| 838 | { |
| 839 | /* It does not make sense to let the link be overriden unless we use |
| 840 | * MLO_AN_FIXED |
| 841 | */ |
| 842 | if (pl->link_an_mode != MLO_AN_FIXED) |
| 843 | return -EINVAL; |
| 844 | |
| 845 | mutex_lock(&pl->state_mutex); |
| 846 | pl->get_fixed_state = cb; |
| 847 | mutex_unlock(&pl->state_mutex); |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | EXPORT_SYMBOL_GPL(phylink_fixed_state_cb); |
| 852 | |
| 853 | /** |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 854 | * phylink_mac_change() - notify phylink of a change in MAC state |
| 855 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 856 | * @up: indicates whether the link is currently up. |
| 857 | * |
| 858 | * The MAC driver should call this driver when the state of its link |
| 859 | * changes (eg, link failure, new negotiation results, etc.) |
| 860 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 861 | void phylink_mac_change(struct phylink *pl, bool up) |
| 862 | { |
| 863 | if (!up) |
| 864 | pl->mac_link_dropped = true; |
| 865 | phylink_run_resolve(pl); |
| 866 | netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down"); |
| 867 | } |
| 868 | EXPORT_SYMBOL_GPL(phylink_mac_change); |
| 869 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 870 | /** |
| 871 | * phylink_start() - start a phylink instance |
| 872 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 873 | * |
| 874 | * Start the phylink instance specified by @pl, configuring the MAC for the |
| 875 | * desired link mode(s) and negotiation style. This should be called from the |
| 876 | * network device driver's &struct net_device_ops ndo_open() method. |
| 877 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 878 | void phylink_start(struct phylink *pl) |
| 879 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 880 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 881 | |
| 882 | netdev_info(pl->netdev, "configuring for %s/%s link mode\n", |
| 883 | phylink_an_mode_str(pl->link_an_mode), |
| 884 | phy_modes(pl->link_config.interface)); |
| 885 | |
| 886 | /* Apply the link configuration to the MAC when starting. This allows |
| 887 | * a fixed-link to start with the correct parameters, and also |
| 888 | * ensures that we set the appropriate advertisment for Serdes links. |
| 889 | */ |
| 890 | phylink_resolve_flow(pl, &pl->link_config); |
| 891 | phylink_mac_config(pl, &pl->link_config); |
| 892 | |
Russell King | 85b4394 | 2017-12-01 10:24:42 +0000 | [diff] [blame] | 893 | /* Restart autonegotiation if using 802.3z to ensure that the link |
| 894 | * parameters are properly negotiated. This is necessary for DSA |
| 895 | * switches using 802.3z negotiation to ensure they see our modes. |
| 896 | */ |
| 897 | phylink_mac_an_restart(pl); |
| 898 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 899 | clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); |
| 900 | phylink_run_resolve(pl); |
| 901 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 902 | if (pl->sfp_bus) |
| 903 | sfp_upstream_start(pl->sfp_bus); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 904 | if (pl->phydev) |
| 905 | phy_start(pl->phydev); |
| 906 | } |
| 907 | EXPORT_SYMBOL_GPL(phylink_start); |
| 908 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 909 | /** |
| 910 | * phylink_stop() - stop a phylink instance |
| 911 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 912 | * |
| 913 | * Stop the phylink instance specified by @pl. This should be called from the |
| 914 | * network device driver's &struct net_device_ops ndo_stop() method. The |
| 915 | * network device's carrier state should not be changed prior to calling this |
| 916 | * function. |
| 917 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 918 | void phylink_stop(struct phylink *pl) |
| 919 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 920 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 921 | |
| 922 | if (pl->phydev) |
| 923 | phy_stop(pl->phydev); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 924 | if (pl->sfp_bus) |
| 925 | sfp_upstream_stop(pl->sfp_bus); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 926 | |
| 927 | set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); |
Russell King | 2012b7d | 2017-11-30 13:59:26 +0000 | [diff] [blame] | 928 | queue_work(system_power_efficient_wq, &pl->resolve); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 929 | flush_work(&pl->resolve); |
| 930 | } |
| 931 | EXPORT_SYMBOL_GPL(phylink_stop); |
| 932 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 933 | /** |
| 934 | * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY |
| 935 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 936 | * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters |
| 937 | * |
| 938 | * Read the wake on lan parameters from the PHY attached to the phylink |
| 939 | * instance specified by @pl. If no PHY is currently attached, report no |
| 940 | * support for wake on lan. |
| 941 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 942 | void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol) |
| 943 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 944 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 945 | |
| 946 | wol->supported = 0; |
| 947 | wol->wolopts = 0; |
| 948 | |
| 949 | if (pl->phydev) |
| 950 | phy_ethtool_get_wol(pl->phydev, wol); |
| 951 | } |
| 952 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol); |
| 953 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 954 | /** |
| 955 | * phylink_ethtool_set_wol() - set wake on lan parameters |
| 956 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 957 | * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters |
| 958 | * |
| 959 | * Set the wake on lan parameters for the PHY attached to the phylink |
| 960 | * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP |
| 961 | * error. |
| 962 | * |
| 963 | * Returns zero on success or negative errno code. |
| 964 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 965 | int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol) |
| 966 | { |
| 967 | int ret = -EOPNOTSUPP; |
| 968 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 969 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 970 | |
| 971 | if (pl->phydev) |
| 972 | ret = phy_ethtool_set_wol(pl->phydev, wol); |
| 973 | |
| 974 | return ret; |
| 975 | } |
| 976 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol); |
| 977 | |
| 978 | static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b) |
| 979 | { |
| 980 | __ETHTOOL_DECLARE_LINK_MODE_MASK(mask); |
| 981 | |
| 982 | linkmode_zero(mask); |
| 983 | phylink_set_port_modes(mask); |
| 984 | |
| 985 | linkmode_and(dst, dst, mask); |
| 986 | linkmode_or(dst, dst, b); |
| 987 | } |
| 988 | |
| 989 | static void phylink_get_ksettings(const struct phylink_link_state *state, |
| 990 | struct ethtool_link_ksettings *kset) |
| 991 | { |
| 992 | phylink_merge_link_mode(kset->link_modes.advertising, state->advertising); |
| 993 | linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising); |
| 994 | kset->base.speed = state->speed; |
| 995 | kset->base.duplex = state->duplex; |
| 996 | kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE : |
| 997 | AUTONEG_DISABLE; |
| 998 | } |
| 999 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1000 | /** |
| 1001 | * phylink_ethtool_ksettings_get() - get the current link settings |
| 1002 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1003 | * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings |
| 1004 | * |
| 1005 | * Read the current link settings for the phylink instance specified by @pl. |
| 1006 | * This will be the link settings read from the MAC, PHY or fixed link |
| 1007 | * settings depending on the current negotiation mode. |
| 1008 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1009 | int phylink_ethtool_ksettings_get(struct phylink *pl, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 1010 | struct ethtool_link_ksettings *kset) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1011 | { |
| 1012 | struct phylink_link_state link_state; |
| 1013 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1014 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1015 | |
| 1016 | if (pl->phydev) { |
| 1017 | phy_ethtool_ksettings_get(pl->phydev, kset); |
| 1018 | } else { |
| 1019 | kset->base.port = pl->link_port; |
| 1020 | } |
| 1021 | |
| 1022 | linkmode_copy(kset->link_modes.supported, pl->supported); |
| 1023 | |
| 1024 | switch (pl->link_an_mode) { |
| 1025 | case MLO_AN_FIXED: |
| 1026 | /* We are using fixed settings. Report these as the |
| 1027 | * current link settings - and note that these also |
| 1028 | * represent the supported speeds/duplex/pause modes. |
| 1029 | */ |
| 1030 | phylink_get_fixed_state(pl, &link_state); |
| 1031 | phylink_get_ksettings(&link_state, kset); |
| 1032 | break; |
| 1033 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1034 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1035 | /* If there is a phy attached, then use the reported |
| 1036 | * settings from the phy with no modification. |
| 1037 | */ |
| 1038 | if (pl->phydev) |
| 1039 | break; |
| 1040 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1041 | phylink_get_mac_state(pl, &link_state); |
| 1042 | |
| 1043 | /* The MAC is reporting the link results from its own PCS |
| 1044 | * layer via in-band status. Report these as the current |
| 1045 | * link settings. |
| 1046 | */ |
| 1047 | phylink_get_ksettings(&link_state, kset); |
| 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get); |
| 1054 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1055 | /** |
| 1056 | * phylink_ethtool_ksettings_set() - set the link settings |
| 1057 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1058 | * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes |
| 1059 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1060 | int phylink_ethtool_ksettings_set(struct phylink *pl, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 1061 | const struct ethtool_link_ksettings *kset) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1062 | { |
| 1063 | struct ethtool_link_ksettings our_kset; |
| 1064 | struct phylink_link_state config; |
| 1065 | int ret; |
| 1066 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1067 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1068 | |
| 1069 | if (kset->base.autoneg != AUTONEG_DISABLE && |
| 1070 | kset->base.autoneg != AUTONEG_ENABLE) |
| 1071 | return -EINVAL; |
| 1072 | |
| 1073 | config = pl->link_config; |
| 1074 | |
| 1075 | /* Mask out unsupported advertisments */ |
| 1076 | linkmode_and(config.advertising, kset->link_modes.advertising, |
| 1077 | pl->supported); |
| 1078 | |
| 1079 | /* FIXME: should we reject autoneg if phy/mac does not support it? */ |
| 1080 | if (kset->base.autoneg == AUTONEG_DISABLE) { |
| 1081 | const struct phy_setting *s; |
| 1082 | |
| 1083 | /* Autonegotiation disabled, select a suitable speed and |
| 1084 | * duplex. |
| 1085 | */ |
| 1086 | s = phy_lookup_setting(kset->base.speed, kset->base.duplex, |
| 1087 | pl->supported, |
| 1088 | __ETHTOOL_LINK_MODE_MASK_NBITS, false); |
| 1089 | if (!s) |
| 1090 | return -EINVAL; |
| 1091 | |
| 1092 | /* If we have a fixed link (as specified by firmware), refuse |
| 1093 | * to change link parameters. |
| 1094 | */ |
| 1095 | if (pl->link_an_mode == MLO_AN_FIXED && |
| 1096 | (s->speed != pl->link_config.speed || |
| 1097 | s->duplex != pl->link_config.duplex)) |
| 1098 | return -EINVAL; |
| 1099 | |
| 1100 | config.speed = s->speed; |
| 1101 | config.duplex = s->duplex; |
| 1102 | config.an_enabled = false; |
| 1103 | |
| 1104 | __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising); |
| 1105 | } else { |
| 1106 | /* If we have a fixed link, refuse to enable autonegotiation */ |
| 1107 | if (pl->link_an_mode == MLO_AN_FIXED) |
| 1108 | return -EINVAL; |
| 1109 | |
| 1110 | config.speed = SPEED_UNKNOWN; |
| 1111 | config.duplex = DUPLEX_UNKNOWN; |
| 1112 | config.an_enabled = true; |
| 1113 | |
| 1114 | __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising); |
| 1115 | } |
| 1116 | |
| 1117 | if (phylink_validate(pl, pl->supported, &config)) |
| 1118 | return -EINVAL; |
| 1119 | |
| 1120 | /* If autonegotiation is enabled, we must have an advertisment */ |
| 1121 | if (config.an_enabled && phylink_is_empty_linkmode(config.advertising)) |
| 1122 | return -EINVAL; |
| 1123 | |
| 1124 | our_kset = *kset; |
| 1125 | linkmode_copy(our_kset.link_modes.advertising, config.advertising); |
| 1126 | our_kset.base.speed = config.speed; |
| 1127 | our_kset.base.duplex = config.duplex; |
| 1128 | |
| 1129 | /* If we have a PHY, configure the phy */ |
| 1130 | if (pl->phydev) { |
| 1131 | ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset); |
| 1132 | if (ret) |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | mutex_lock(&pl->state_mutex); |
| 1137 | /* Configure the MAC to match the new settings */ |
| 1138 | linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising); |
| 1139 | pl->link_config.speed = our_kset.base.speed; |
| 1140 | pl->link_config.duplex = our_kset.base.duplex; |
| 1141 | pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE; |
| 1142 | |
| 1143 | if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) { |
| 1144 | phylink_mac_config(pl, &pl->link_config); |
| 1145 | phylink_mac_an_restart(pl); |
| 1146 | } |
| 1147 | mutex_unlock(&pl->state_mutex); |
| 1148 | |
Dan Carpenter | d18c2a1 | 2017-08-10 00:35:50 +0300 | [diff] [blame] | 1149 | return 0; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1150 | } |
| 1151 | EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set); |
| 1152 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1153 | /** |
| 1154 | * phylink_ethtool_nway_reset() - restart negotiation |
| 1155 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1156 | * |
| 1157 | * Restart negotiation for the phylink instance specified by @pl. This will |
| 1158 | * cause any attached phy to restart negotiation with the link partner, and |
| 1159 | * if the MAC is in a BaseX mode, the MAC will also be requested to restart |
| 1160 | * negotiation. |
| 1161 | * |
| 1162 | * Returns zero on success, or negative error code. |
| 1163 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1164 | int phylink_ethtool_nway_reset(struct phylink *pl) |
| 1165 | { |
| 1166 | int ret = 0; |
| 1167 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1168 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1169 | |
| 1170 | if (pl->phydev) |
| 1171 | ret = phy_restart_aneg(pl->phydev); |
| 1172 | phylink_mac_an_restart(pl); |
| 1173 | |
| 1174 | return ret; |
| 1175 | } |
| 1176 | EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset); |
| 1177 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1178 | /** |
| 1179 | * phylink_ethtool_get_pauseparam() - get the current pause parameters |
| 1180 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1181 | * @pause: a pointer to a &struct ethtool_pauseparam |
| 1182 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1183 | void phylink_ethtool_get_pauseparam(struct phylink *pl, |
| 1184 | struct ethtool_pauseparam *pause) |
| 1185 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1186 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1187 | |
| 1188 | pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN); |
| 1189 | pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX); |
| 1190 | pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX); |
| 1191 | } |
| 1192 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam); |
| 1193 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1194 | /** |
| 1195 | * phylink_ethtool_set_pauseparam() - set the current pause parameters |
| 1196 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1197 | * @pause: a pointer to a &struct ethtool_pauseparam |
| 1198 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1199 | int phylink_ethtool_set_pauseparam(struct phylink *pl, |
| 1200 | struct ethtool_pauseparam *pause) |
| 1201 | { |
| 1202 | struct phylink_link_state *config = &pl->link_config; |
| 1203 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1204 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1205 | |
| 1206 | if (!phylink_test(pl->supported, Pause) && |
| 1207 | !phylink_test(pl->supported, Asym_Pause)) |
| 1208 | return -EOPNOTSUPP; |
| 1209 | |
| 1210 | if (!phylink_test(pl->supported, Asym_Pause) && |
| 1211 | !pause->autoneg && pause->rx_pause != pause->tx_pause) |
| 1212 | return -EINVAL; |
| 1213 | |
| 1214 | config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK); |
| 1215 | |
| 1216 | if (pause->autoneg) |
| 1217 | config->pause |= MLO_PAUSE_AN; |
| 1218 | if (pause->rx_pause) |
| 1219 | config->pause |= MLO_PAUSE_RX; |
| 1220 | if (pause->tx_pause) |
| 1221 | config->pause |= MLO_PAUSE_TX; |
| 1222 | |
| 1223 | if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) { |
| 1224 | switch (pl->link_an_mode) { |
| 1225 | case MLO_AN_PHY: |
| 1226 | /* Silently mark the carrier down, and then trigger a resolve */ |
| 1227 | netif_carrier_off(pl->netdev); |
| 1228 | phylink_run_resolve(pl); |
| 1229 | break; |
| 1230 | |
| 1231 | case MLO_AN_FIXED: |
| 1232 | /* Should we allow fixed links to change against the config? */ |
| 1233 | phylink_resolve_flow(pl, config); |
| 1234 | phylink_mac_config(pl, config); |
| 1235 | break; |
| 1236 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1237 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1238 | phylink_mac_config(pl, config); |
| 1239 | phylink_mac_an_restart(pl); |
| 1240 | break; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam); |
| 1247 | |
Russell King | 770a1ad | 2017-07-25 15:03:23 +0100 | [diff] [blame] | 1248 | int phylink_ethtool_get_module_info(struct phylink *pl, |
| 1249 | struct ethtool_modinfo *modinfo) |
| 1250 | { |
| 1251 | int ret = -EOPNOTSUPP; |
| 1252 | |
| 1253 | WARN_ON(!lockdep_rtnl_is_held()); |
| 1254 | |
| 1255 | if (pl->sfp_bus) |
| 1256 | ret = sfp_get_module_info(pl->sfp_bus, modinfo); |
| 1257 | |
| 1258 | return ret; |
| 1259 | } |
| 1260 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info); |
| 1261 | |
| 1262 | int phylink_ethtool_get_module_eeprom(struct phylink *pl, |
| 1263 | struct ethtool_eeprom *ee, u8 *buf) |
| 1264 | { |
| 1265 | int ret = -EOPNOTSUPP; |
| 1266 | |
| 1267 | WARN_ON(!lockdep_rtnl_is_held()); |
| 1268 | |
| 1269 | if (pl->sfp_bus) |
| 1270 | ret = sfp_get_module_eeprom(pl->sfp_bus, ee, buf); |
| 1271 | |
| 1272 | return ret; |
| 1273 | } |
| 1274 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom); |
| 1275 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1276 | /** |
| 1277 | * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error |
| 1278 | * counter |
| 1279 | * @pl: a pointer to a &struct phylink returned from phylink_create(). |
| 1280 | * |
| 1281 | * Read the Energy Efficient Ethernet error counter from the PHY associated |
| 1282 | * with the phylink instance specified by @pl. |
| 1283 | * |
| 1284 | * Returns positive error counter value, or negative error code. |
| 1285 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1286 | int phylink_get_eee_err(struct phylink *pl) |
| 1287 | { |
| 1288 | int ret = 0; |
| 1289 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1290 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1291 | |
| 1292 | if (pl->phydev) |
| 1293 | ret = phy_get_eee_err(pl->phydev); |
| 1294 | |
| 1295 | return ret; |
| 1296 | } |
| 1297 | EXPORT_SYMBOL_GPL(phylink_get_eee_err); |
| 1298 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1299 | /** |
| 1300 | * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters |
| 1301 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1302 | * @eee: a pointer to a &struct ethtool_eee for the read parameters |
| 1303 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1304 | int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee) |
| 1305 | { |
| 1306 | int ret = -EOPNOTSUPP; |
| 1307 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1308 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1309 | |
| 1310 | if (pl->phydev) |
| 1311 | ret = phy_ethtool_get_eee(pl->phydev, eee); |
| 1312 | |
| 1313 | return ret; |
| 1314 | } |
| 1315 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee); |
| 1316 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1317 | /** |
| 1318 | * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters |
| 1319 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1320 | * @eee: a pointer to a &struct ethtool_eee for the desired parameters |
| 1321 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1322 | int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee) |
| 1323 | { |
| 1324 | int ret = -EOPNOTSUPP; |
| 1325 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1326 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1327 | |
| 1328 | if (pl->phydev) |
| 1329 | ret = phy_ethtool_set_eee(pl->phydev, eee); |
| 1330 | |
| 1331 | return ret; |
| 1332 | } |
| 1333 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee); |
| 1334 | |
| 1335 | /* This emulates MII registers for a fixed-mode phy operating as per the |
| 1336 | * passed in state. "aneg" defines if we report negotiation is possible. |
| 1337 | * |
| 1338 | * FIXME: should deal with negotiation state too. |
| 1339 | */ |
| 1340 | static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg, |
| 1341 | struct phylink_link_state *state, bool aneg) |
| 1342 | { |
| 1343 | struct fixed_phy_status fs; |
| 1344 | int val; |
| 1345 | |
| 1346 | fs.link = state->link; |
| 1347 | fs.speed = state->speed; |
| 1348 | fs.duplex = state->duplex; |
| 1349 | fs.pause = state->pause & MLO_PAUSE_SYM; |
| 1350 | fs.asym_pause = state->pause & MLO_PAUSE_ASYM; |
| 1351 | |
| 1352 | val = swphy_read_reg(reg, &fs); |
| 1353 | if (reg == MII_BMSR) { |
| 1354 | if (!state->an_complete) |
| 1355 | val &= ~BMSR_ANEGCOMPLETE; |
| 1356 | if (!aneg) |
| 1357 | val &= ~BMSR_ANEGCAPABLE; |
| 1358 | } |
| 1359 | return val; |
| 1360 | } |
| 1361 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1362 | static int phylink_phy_read(struct phylink *pl, unsigned int phy_id, |
| 1363 | unsigned int reg) |
| 1364 | { |
| 1365 | struct phy_device *phydev = pl->phydev; |
| 1366 | int prtad, devad; |
| 1367 | |
| 1368 | if (mdio_phy_id_is_c45(phy_id)) { |
| 1369 | prtad = mdio_phy_id_prtad(phy_id); |
| 1370 | devad = mdio_phy_id_devad(phy_id); |
| 1371 | devad = MII_ADDR_C45 | devad << 16 | reg; |
| 1372 | } else if (phydev->is_c45) { |
| 1373 | switch (reg) { |
| 1374 | case MII_BMCR: |
| 1375 | case MII_BMSR: |
| 1376 | case MII_PHYSID1: |
| 1377 | case MII_PHYSID2: |
| 1378 | devad = __ffs(phydev->c45_ids.devices_in_package); |
| 1379 | break; |
| 1380 | case MII_ADVERTISE: |
| 1381 | case MII_LPA: |
| 1382 | if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN)) |
| 1383 | return -EINVAL; |
| 1384 | devad = MDIO_MMD_AN; |
| 1385 | if (reg == MII_ADVERTISE) |
| 1386 | reg = MDIO_AN_ADVERTISE; |
| 1387 | else |
| 1388 | reg = MDIO_AN_LPA; |
| 1389 | break; |
| 1390 | default: |
| 1391 | return -EINVAL; |
| 1392 | } |
| 1393 | prtad = phy_id; |
| 1394 | devad = MII_ADDR_C45 | devad << 16 | reg; |
| 1395 | } else { |
| 1396 | prtad = phy_id; |
| 1397 | devad = reg; |
| 1398 | } |
| 1399 | return mdiobus_read(pl->phydev->mdio.bus, prtad, devad); |
| 1400 | } |
| 1401 | |
| 1402 | static int phylink_phy_write(struct phylink *pl, unsigned int phy_id, |
| 1403 | unsigned int reg, unsigned int val) |
| 1404 | { |
| 1405 | struct phy_device *phydev = pl->phydev; |
| 1406 | int prtad, devad; |
| 1407 | |
| 1408 | if (mdio_phy_id_is_c45(phy_id)) { |
| 1409 | prtad = mdio_phy_id_prtad(phy_id); |
| 1410 | devad = mdio_phy_id_devad(phy_id); |
| 1411 | devad = MII_ADDR_C45 | devad << 16 | reg; |
| 1412 | } else if (phydev->is_c45) { |
| 1413 | switch (reg) { |
| 1414 | case MII_BMCR: |
| 1415 | case MII_BMSR: |
| 1416 | case MII_PHYSID1: |
| 1417 | case MII_PHYSID2: |
| 1418 | devad = __ffs(phydev->c45_ids.devices_in_package); |
| 1419 | break; |
| 1420 | case MII_ADVERTISE: |
| 1421 | case MII_LPA: |
| 1422 | if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN)) |
| 1423 | return -EINVAL; |
| 1424 | devad = MDIO_MMD_AN; |
| 1425 | if (reg == MII_ADVERTISE) |
| 1426 | reg = MDIO_AN_ADVERTISE; |
| 1427 | else |
| 1428 | reg = MDIO_AN_LPA; |
| 1429 | break; |
| 1430 | default: |
| 1431 | return -EINVAL; |
| 1432 | } |
| 1433 | prtad = phy_id; |
| 1434 | devad = MII_ADDR_C45 | devad << 16 | reg; |
| 1435 | } else { |
| 1436 | prtad = phy_id; |
| 1437 | devad = reg; |
| 1438 | } |
| 1439 | |
| 1440 | return mdiobus_write(phydev->mdio.bus, prtad, devad, val); |
| 1441 | } |
| 1442 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1443 | static int phylink_mii_read(struct phylink *pl, unsigned int phy_id, |
| 1444 | unsigned int reg) |
| 1445 | { |
| 1446 | struct phylink_link_state state; |
| 1447 | int val = 0xffff; |
| 1448 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1449 | switch (pl->link_an_mode) { |
| 1450 | case MLO_AN_FIXED: |
| 1451 | if (phy_id == 0) { |
| 1452 | phylink_get_fixed_state(pl, &state); |
| 1453 | val = phylink_mii_emul_read(pl->netdev, reg, &state, |
| 1454 | true); |
| 1455 | } |
| 1456 | break; |
| 1457 | |
| 1458 | case MLO_AN_PHY: |
| 1459 | return -EOPNOTSUPP; |
| 1460 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1461 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1462 | if (phy_id == 0) { |
| 1463 | val = phylink_get_mac_state(pl, &state); |
| 1464 | if (val < 0) |
| 1465 | return val; |
| 1466 | |
| 1467 | val = phylink_mii_emul_read(pl->netdev, reg, &state, |
| 1468 | true); |
| 1469 | } |
| 1470 | break; |
| 1471 | } |
| 1472 | |
| 1473 | return val & 0xffff; |
| 1474 | } |
| 1475 | |
| 1476 | static int phylink_mii_write(struct phylink *pl, unsigned int phy_id, |
| 1477 | unsigned int reg, unsigned int val) |
| 1478 | { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1479 | switch (pl->link_an_mode) { |
| 1480 | case MLO_AN_FIXED: |
| 1481 | break; |
| 1482 | |
| 1483 | case MLO_AN_PHY: |
| 1484 | return -EOPNOTSUPP; |
| 1485 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1486 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1487 | break; |
| 1488 | } |
| 1489 | |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1493 | /** |
| 1494 | * phylink_mii_ioctl() - generic mii ioctl interface |
| 1495 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1496 | * @ifr: a pointer to a &struct ifreq for socket ioctls |
| 1497 | * @cmd: ioctl cmd to execute |
| 1498 | * |
| 1499 | * Perform the specified MII ioctl on the PHY attached to the phylink instance |
| 1500 | * specified by @pl. If no PHY is attached, emulate the presence of the PHY. |
| 1501 | * |
| 1502 | * Returns: zero on success or negative error code. |
| 1503 | * |
| 1504 | * %SIOCGMIIPHY: |
| 1505 | * read register from the current PHY. |
| 1506 | * %SIOCGMIIREG: |
| 1507 | * read register from the specified PHY. |
| 1508 | * %SIOCSMIIREG: |
| 1509 | * set a register on the specified PHY. |
| 1510 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1511 | int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd) |
| 1512 | { |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1513 | struct mii_ioctl_data *mii = if_mii(ifr); |
| 1514 | int ret; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1515 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1516 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1517 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1518 | if (pl->phydev) { |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1519 | /* PHYs only exist for MLO_AN_PHY and SGMII */ |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1520 | switch (cmd) { |
| 1521 | case SIOCGMIIPHY: |
| 1522 | mii->phy_id = pl->phydev->mdio.addr; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1523 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1524 | case SIOCGMIIREG: |
| 1525 | ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num); |
| 1526 | if (ret >= 0) { |
| 1527 | mii->val_out = ret; |
| 1528 | ret = 0; |
| 1529 | } |
| 1530 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1531 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1532 | case SIOCSMIIREG: |
| 1533 | ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num, |
| 1534 | mii->val_in); |
| 1535 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1536 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1537 | default: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1538 | ret = phy_mii_ioctl(pl->phydev, ifr, cmd); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1539 | break; |
| 1540 | } |
| 1541 | } else { |
| 1542 | switch (cmd) { |
| 1543 | case SIOCGMIIPHY: |
| 1544 | mii->phy_id = 0; |
| 1545 | |
| 1546 | case SIOCGMIIREG: |
| 1547 | ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num); |
| 1548 | if (ret >= 0) { |
| 1549 | mii->val_out = ret; |
| 1550 | ret = 0; |
| 1551 | } |
| 1552 | break; |
| 1553 | |
| 1554 | case SIOCSMIIREG: |
| 1555 | ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num, |
| 1556 | mii->val_in); |
| 1557 | break; |
| 1558 | |
| 1559 | default: |
| 1560 | ret = -EOPNOTSUPP; |
| 1561 | break; |
| 1562 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | return ret; |
| 1566 | } |
| 1567 | EXPORT_SYMBOL_GPL(phylink_mii_ioctl); |
| 1568 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1569 | static int phylink_sfp_module_insert(void *upstream, |
| 1570 | const struct sfp_eeprom_id *id) |
| 1571 | { |
| 1572 | struct phylink *pl = upstream; |
| 1573 | __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, }; |
| 1574 | struct phylink_link_state config; |
| 1575 | phy_interface_t iface; |
| 1576 | int mode, ret = 0; |
| 1577 | bool changed; |
| 1578 | u8 port; |
| 1579 | |
| 1580 | sfp_parse_support(pl->sfp_bus, id, support); |
| 1581 | port = sfp_parse_port(pl->sfp_bus, id, support); |
| 1582 | iface = sfp_parse_interface(pl->sfp_bus, id); |
| 1583 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1584 | ASSERT_RTNL(); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1585 | |
| 1586 | switch (iface) { |
| 1587 | case PHY_INTERFACE_MODE_SGMII: |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1588 | case PHY_INTERFACE_MODE_1000BASEX: |
Russell King | 4336c40 | 2017-12-01 10:24:32 +0000 | [diff] [blame] | 1589 | case PHY_INTERFACE_MODE_2500BASEX: |
| 1590 | case PHY_INTERFACE_MODE_10GKR: |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1591 | mode = MLO_AN_INBAND; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1592 | break; |
| 1593 | default: |
| 1594 | return -EINVAL; |
| 1595 | } |
| 1596 | |
| 1597 | memset(&config, 0, sizeof(config)); |
| 1598 | linkmode_copy(config.advertising, support); |
| 1599 | config.interface = iface; |
| 1600 | config.speed = SPEED_UNKNOWN; |
| 1601 | config.duplex = DUPLEX_UNKNOWN; |
| 1602 | config.pause = MLO_PAUSE_AN; |
| 1603 | config.an_enabled = pl->link_config.an_enabled; |
| 1604 | |
| 1605 | /* Ignore errors if we're expecting a PHY to attach later */ |
| 1606 | ret = phylink_validate(pl, support, &config); |
| 1607 | if (ret) { |
| 1608 | netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n", |
| 1609 | phylink_an_mode_str(mode), phy_modes(config.interface), |
| 1610 | __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); |
| 1611 | return ret; |
| 1612 | } |
| 1613 | |
| 1614 | netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n", |
| 1615 | phylink_an_mode_str(mode), phy_modes(config.interface), |
| 1616 | __ETHTOOL_LINK_MODE_MASK_NBITS, support); |
| 1617 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1618 | if (phy_interface_mode_is_8023z(iface) && pl->phydev) |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1619 | return -EINVAL; |
| 1620 | |
| 1621 | changed = !bitmap_equal(pl->supported, support, |
| 1622 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 1623 | if (changed) { |
| 1624 | linkmode_copy(pl->supported, support); |
| 1625 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 1626 | } |
| 1627 | |
| 1628 | if (pl->link_an_mode != mode || |
| 1629 | pl->link_config.interface != config.interface) { |
| 1630 | pl->link_config.interface = config.interface; |
| 1631 | pl->link_an_mode = mode; |
| 1632 | |
| 1633 | changed = true; |
| 1634 | |
| 1635 | netdev_info(pl->netdev, "switched to %s/%s link mode\n", |
| 1636 | phylink_an_mode_str(mode), |
| 1637 | phy_modes(config.interface)); |
| 1638 | } |
| 1639 | |
| 1640 | pl->link_port = port; |
| 1641 | |
| 1642 | if (changed && !test_bit(PHYLINK_DISABLE_STOPPED, |
| 1643 | &pl->phylink_disable_state)) |
| 1644 | phylink_mac_config(pl, &pl->link_config); |
| 1645 | |
| 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | static void phylink_sfp_link_down(void *upstream) |
| 1650 | { |
| 1651 | struct phylink *pl = upstream; |
| 1652 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1653 | ASSERT_RTNL(); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1654 | |
| 1655 | set_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state); |
| 1656 | flush_work(&pl->resolve); |
| 1657 | |
| 1658 | netif_carrier_off(pl->netdev); |
| 1659 | } |
| 1660 | |
| 1661 | static void phylink_sfp_link_up(void *upstream) |
| 1662 | { |
| 1663 | struct phylink *pl = upstream; |
| 1664 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1665 | ASSERT_RTNL(); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1666 | |
| 1667 | clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state); |
| 1668 | phylink_run_resolve(pl); |
| 1669 | } |
| 1670 | |
| 1671 | static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy) |
| 1672 | { |
| 1673 | return phylink_connect_phy(upstream, phy); |
| 1674 | } |
| 1675 | |
| 1676 | static void phylink_sfp_disconnect_phy(void *upstream) |
| 1677 | { |
| 1678 | phylink_disconnect_phy(upstream); |
| 1679 | } |
| 1680 | |
| 1681 | static const struct sfp_upstream_ops sfp_phylink_ops = { |
| 1682 | .module_insert = phylink_sfp_module_insert, |
| 1683 | .link_up = phylink_sfp_link_up, |
| 1684 | .link_down = phylink_sfp_link_down, |
| 1685 | .connect_phy = phylink_sfp_connect_phy, |
| 1686 | .disconnect_phy = phylink_sfp_disconnect_phy, |
| 1687 | }; |
| 1688 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1689 | MODULE_LICENSE("GPL"); |