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