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