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