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