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