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