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