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 | */ |
Calvin Johnson | 25396f6 | 2021-06-11 13:53:59 +0300 | [diff] [blame] | 8 | #include <linux/acpi.h> |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 9 | #include <linux/ethtool.h> |
| 10 | #include <linux/export.h> |
| 11 | #include <linux/gpio/consumer.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/of.h> |
| 14 | #include <linux/of_mdio.h> |
| 15 | #include <linux/phy.h> |
| 16 | #include <linux/phy_fixed.h> |
| 17 | #include <linux/phylink.h> |
| 18 | #include <linux/rtnetlink.h> |
| 19 | #include <linux/spinlock.h> |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 20 | #include <linux/timer.h> |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 21 | #include <linux/workqueue.h> |
| 22 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 23 | #include "sfp.h" |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 24 | #include "swphy.h" |
| 25 | |
| 26 | #define SUPPORTED_INTERFACES \ |
| 27 | (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \ |
| 28 | SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane) |
| 29 | #define ADVERTISED_INTERFACES \ |
| 30 | (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \ |
| 31 | ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane) |
| 32 | |
| 33 | enum { |
| 34 | PHYLINK_DISABLE_STOPPED, |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 35 | PHYLINK_DISABLE_LINK, |
Russell King (Oracle) | f974936 | 2021-09-07 18:56:46 +0800 | [diff] [blame] | 36 | PHYLINK_DISABLE_MAC_WOL, |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 39 | /** |
| 40 | * struct phylink - internal data type for phylink |
| 41 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 42 | struct phylink { |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 43 | /* private: */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 44 | struct net_device *netdev; |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 45 | const struct phylink_mac_ops *mac_ops; |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 46 | const struct phylink_pcs_ops *pcs_ops; |
Ioana Ciornei | 44cc27e | 2019-05-28 20:38:12 +0300 | [diff] [blame] | 47 | struct phylink_config *config; |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 48 | struct phylink_pcs *pcs; |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 49 | struct device *dev; |
| 50 | unsigned int old_link_state:1; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 51 | |
| 52 | unsigned long phylink_disable_state; /* bitmask of disables */ |
| 53 | struct phy_device *phydev; |
| 54 | phy_interface_t link_interface; /* PHY_INTERFACE_xxx */ |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 55 | u8 cfg_link_an_mode; /* MLO_AN_xxx */ |
| 56 | u8 cur_link_an_mode; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 57 | u8 link_port; /* The current non-phy ethtool port */ |
| 58 | __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); |
| 59 | |
| 60 | /* The link configuration settings */ |
| 61 | struct phylink_link_state link_config; |
Russell King | c678726 | 2019-05-28 10:27:21 +0100 | [diff] [blame] | 62 | |
| 63 | /* The current settings */ |
| 64 | phy_interface_t cur_interface; |
| 65 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 66 | struct gpio_desc *link_gpio; |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 67 | unsigned int link_irq; |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 68 | struct timer_list link_poll; |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 69 | void (*get_fixed_state)(struct net_device *dev, |
| 70 | struct phylink_link_state *s); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 71 | |
| 72 | struct mutex state_mutex; |
| 73 | struct phylink_link_state phy_state; |
| 74 | struct work_struct resolve; |
| 75 | |
| 76 | bool mac_link_dropped; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 77 | |
| 78 | struct sfp_bus *sfp_bus; |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 79 | bool sfp_may_have_phy; |
| 80 | __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support); |
| 81 | u8 sfp_port; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 84 | #define phylink_printk(level, pl, fmt, ...) \ |
| 85 | do { \ |
| 86 | if ((pl)->config->type == PHYLINK_NETDEV) \ |
| 87 | netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \ |
| 88 | else if ((pl)->config->type == PHYLINK_DEV) \ |
| 89 | dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \ |
| 90 | } while (0) |
| 91 | |
| 92 | #define phylink_err(pl, fmt, ...) \ |
| 93 | phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__) |
| 94 | #define phylink_warn(pl, fmt, ...) \ |
| 95 | phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__) |
| 96 | #define phylink_info(pl, fmt, ...) \ |
| 97 | phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__) |
Florian Fainelli | 9d68db5 | 2019-10-31 15:42:26 -0700 | [diff] [blame] | 98 | #if defined(CONFIG_DYNAMIC_DEBUG) |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 99 | #define phylink_dbg(pl, fmt, ...) \ |
Florian Fainelli | 9d68db5 | 2019-10-31 15:42:26 -0700 | [diff] [blame] | 100 | do { \ |
| 101 | if ((pl)->config->type == PHYLINK_NETDEV) \ |
| 102 | netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \ |
| 103 | else if ((pl)->config->type == PHYLINK_DEV) \ |
| 104 | dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \ |
| 105 | } while (0) |
| 106 | #elif defined(DEBUG) |
| 107 | #define phylink_dbg(pl, fmt, ...) \ |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 108 | phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__) |
Florian Fainelli | 9d68db5 | 2019-10-31 15:42:26 -0700 | [diff] [blame] | 109 | #else |
| 110 | #define phylink_dbg(pl, fmt, ...) \ |
| 111 | ({ \ |
| 112 | if (0) \ |
| 113 | phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \ |
| 114 | }) |
| 115 | #endif |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 116 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 117 | /** |
| 118 | * phylink_set_port_modes() - set the port type modes in the ethtool mask |
| 119 | * @mask: ethtool link mode mask |
| 120 | * |
| 121 | * Sets all the port type modes in the ethtool mask. MAC drivers should |
| 122 | * use this in their 'validate' callback. |
| 123 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 124 | void phylink_set_port_modes(unsigned long *mask) |
| 125 | { |
| 126 | phylink_set(mask, TP); |
| 127 | phylink_set(mask, AUI); |
| 128 | phylink_set(mask, MII); |
| 129 | phylink_set(mask, FIBRE); |
| 130 | phylink_set(mask, BNC); |
| 131 | phylink_set(mask, Backplane); |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(phylink_set_port_modes); |
| 134 | |
Russell King (Oracle) | a2c27a6 | 2021-10-04 12:03:28 +0100 | [diff] [blame] | 135 | void phylink_set_10g_modes(unsigned long *mask) |
| 136 | { |
| 137 | phylink_set(mask, 10000baseT_Full); |
| 138 | phylink_set(mask, 10000baseCR_Full); |
| 139 | phylink_set(mask, 10000baseSR_Full); |
| 140 | phylink_set(mask, 10000baseLR_Full); |
| 141 | phylink_set(mask, 10000baseLRM_Full); |
| 142 | phylink_set(mask, 10000baseER_Full); |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(phylink_set_10g_modes); |
| 145 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 146 | static int phylink_is_empty_linkmode(const unsigned long *linkmode) |
| 147 | { |
| 148 | __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, }; |
| 149 | |
| 150 | phylink_set_port_modes(tmp); |
| 151 | phylink_set(tmp, Autoneg); |
| 152 | phylink_set(tmp, Pause); |
| 153 | phylink_set(tmp, Asym_Pause); |
| 154 | |
Russell King | 554032c | 2019-10-15 11:28:46 +0100 | [diff] [blame] | 155 | return linkmode_subset(linkmode, tmp); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static const char *phylink_an_mode_str(unsigned int mode) |
| 159 | { |
| 160 | static const char *modestr[] = { |
| 161 | [MLO_AN_PHY] = "phy", |
| 162 | [MLO_AN_FIXED] = "fixed", |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 163 | [MLO_AN_INBAND] = "inband", |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown"; |
| 167 | } |
| 168 | |
| 169 | static int phylink_validate(struct phylink *pl, unsigned long *supported, |
| 170 | struct phylink_link_state *state) |
| 171 | { |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 172 | pl->mac_ops->validate(pl->config, supported, state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 173 | |
| 174 | return phylink_is_empty_linkmode(supported) ? -EINVAL : 0; |
| 175 | } |
| 176 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 177 | static int phylink_parse_fixedlink(struct phylink *pl, |
| 178 | struct fwnode_handle *fwnode) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 179 | { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 180 | struct fwnode_handle *fixed_node; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 181 | const struct phy_setting *s; |
| 182 | struct gpio_desc *desc; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 183 | u32 speed; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 184 | int ret; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 185 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 186 | fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 187 | if (fixed_node) { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 188 | ret = fwnode_property_read_u32(fixed_node, "speed", &speed); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 189 | |
| 190 | pl->link_config.speed = speed; |
| 191 | pl->link_config.duplex = DUPLEX_HALF; |
| 192 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 193 | if (fwnode_property_read_bool(fixed_node, "full-duplex")) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 194 | pl->link_config.duplex = DUPLEX_FULL; |
| 195 | |
| 196 | /* We treat the "pause" and "asym-pause" terminology as |
Wenpeng Liang | 1953feb | 2021-06-16 18:01:20 +0800 | [diff] [blame] | 197 | * defining the link partner's ability. |
| 198 | */ |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 199 | if (fwnode_property_read_bool(fixed_node, "pause")) |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 200 | __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 201 | pl->link_config.lp_advertising); |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 202 | if (fwnode_property_read_bool(fixed_node, "asym-pause")) |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 203 | __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 204 | pl->link_config.lp_advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 205 | |
| 206 | if (ret == 0) { |
Dmitry Torokhov | b605c9a | 2020-01-02 17:03:18 -0800 | [diff] [blame] | 207 | desc = fwnode_gpiod_get_index(fixed_node, "link", 0, |
| 208 | GPIOD_IN, "?"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 209 | |
| 210 | if (!IS_ERR(desc)) |
| 211 | pl->link_gpio = desc; |
| 212 | else if (desc == ERR_PTR(-EPROBE_DEFER)) |
| 213 | ret = -EPROBE_DEFER; |
| 214 | } |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 215 | fwnode_handle_put(fixed_node); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 216 | |
| 217 | if (ret) |
| 218 | return ret; |
| 219 | } else { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 220 | u32 prop[5]; |
| 221 | |
| 222 | ret = fwnode_property_read_u32_array(fwnode, "fixed-link", |
| 223 | NULL, 0); |
| 224 | if (ret != ARRAY_SIZE(prop)) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 225 | phylink_err(pl, "broken fixed-link?\n"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 226 | return -EINVAL; |
| 227 | } |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 228 | |
| 229 | ret = fwnode_property_read_u32_array(fwnode, "fixed-link", |
| 230 | prop, ARRAY_SIZE(prop)); |
| 231 | if (!ret) { |
| 232 | pl->link_config.duplex = prop[1] ? |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 233 | DUPLEX_FULL : DUPLEX_HALF; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 234 | pl->link_config.speed = prop[2]; |
| 235 | if (prop[3]) |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 236 | __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 237 | pl->link_config.lp_advertising); |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 238 | if (prop[4]) |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 239 | __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 240 | pl->link_config.lp_advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | if (pl->link_config.speed > SPEED_1000 && |
| 245 | pl->link_config.duplex != DUPLEX_FULL) |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 246 | phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n", |
| 247 | pl->link_config.speed); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 248 | |
| 249 | bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 250 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 251 | phylink_validate(pl, pl->supported, &pl->link_config); |
| 252 | |
| 253 | s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex, |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 254 | pl->supported, true); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 255 | linkmode_zero(pl->supported); |
| 256 | phylink_set(pl->supported, MII); |
René van Dorst | 8aace4f | 2019-07-27 11:40:11 +0200 | [diff] [blame] | 257 | phylink_set(pl->supported, Pause); |
| 258 | phylink_set(pl->supported, Asym_Pause); |
Russell King | 1ceb7ee | 2020-07-21 12:03:45 +0100 | [diff] [blame] | 259 | phylink_set(pl->supported, Autoneg); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 260 | if (s) { |
| 261 | __set_bit(s->bit, pl->supported); |
Russell King | 1ceb7ee | 2020-07-21 12:03:45 +0100 | [diff] [blame] | 262 | __set_bit(s->bit, pl->link_config.lp_advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 263 | } else { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 264 | phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n", |
| 265 | pl->link_config.duplex == DUPLEX_FULL ? "full" : "half", |
| 266 | pl->link_config.speed); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | linkmode_and(pl->link_config.advertising, pl->link_config.advertising, |
| 270 | pl->supported); |
| 271 | |
| 272 | pl->link_config.link = 1; |
| 273 | pl->link_config.an_complete = 1; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 278 | static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 279 | { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 280 | struct fwnode_handle *dn; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 281 | const char *managed; |
| 282 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 283 | dn = fwnode_get_named_child_node(fwnode, "fixed-link"); |
| 284 | if (dn || fwnode_property_present(fwnode, "fixed-link")) |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 285 | pl->cfg_link_an_mode = MLO_AN_FIXED; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 286 | fwnode_handle_put(dn); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 287 | |
Ong Boon Leong | ab39385 | 2021-03-15 13:27:08 +0800 | [diff] [blame] | 288 | if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 && |
| 289 | strcmp(managed, "in-band-status") == 0) || |
| 290 | pl->config->ovr_an_inband) { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 291 | if (pl->cfg_link_an_mode == MLO_AN_FIXED) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 292 | phylink_err(pl, |
| 293 | "can't use both fixed-link and in-band-status\n"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 294 | return -EINVAL; |
| 295 | } |
| 296 | |
| 297 | linkmode_zero(pl->supported); |
| 298 | phylink_set(pl->supported, MII); |
| 299 | phylink_set(pl->supported, Autoneg); |
| 300 | phylink_set(pl->supported, Asym_Pause); |
| 301 | phylink_set(pl->supported, Pause); |
| 302 | pl->link_config.an_enabled = true; |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 303 | pl->cfg_link_an_mode = MLO_AN_INBAND; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 304 | |
| 305 | switch (pl->link_config.interface) { |
| 306 | case PHY_INTERFACE_MODE_SGMII: |
Vladimir Oltean | 3a68ba6 | 2020-01-06 03:34:10 +0200 | [diff] [blame] | 307 | case PHY_INTERFACE_MODE_QSGMII: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 308 | phylink_set(pl->supported, 10baseT_Half); |
| 309 | phylink_set(pl->supported, 10baseT_Full); |
| 310 | phylink_set(pl->supported, 100baseT_Half); |
| 311 | phylink_set(pl->supported, 100baseT_Full); |
| 312 | phylink_set(pl->supported, 1000baseT_Half); |
| 313 | phylink_set(pl->supported, 1000baseT_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 314 | break; |
| 315 | |
| 316 | case PHY_INTERFACE_MODE_1000BASEX: |
| 317 | phylink_set(pl->supported, 1000baseX_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 318 | break; |
| 319 | |
| 320 | case PHY_INTERFACE_MODE_2500BASEX: |
| 321 | phylink_set(pl->supported, 2500baseX_Full); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 322 | break; |
| 323 | |
Marek Behún | f6813bd | 2021-02-16 20:20:54 +0100 | [diff] [blame] | 324 | case PHY_INTERFACE_MODE_5GBASER: |
| 325 | phylink_set(pl->supported, 5000baseT_Full); |
| 326 | break; |
| 327 | |
Steen Hegelund | 21e0c59 | 2021-06-11 14:54:53 +0200 | [diff] [blame] | 328 | case PHY_INTERFACE_MODE_25GBASER: |
| 329 | phylink_set(pl->supported, 25000baseCR_Full); |
| 330 | phylink_set(pl->supported, 25000baseKR_Full); |
| 331 | phylink_set(pl->supported, 25000baseSR_Full); |
| 332 | fallthrough; |
Alex Marginean | 04e2246 | 2020-01-18 14:19:15 +0200 | [diff] [blame] | 333 | case PHY_INTERFACE_MODE_USXGMII: |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 334 | case PHY_INTERFACE_MODE_10GKR: |
Russell King | e0f909b | 2020-01-03 20:43:23 +0000 | [diff] [blame] | 335 | case PHY_INTERFACE_MODE_10GBASER: |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 336 | phylink_set(pl->supported, 10baseT_Half); |
| 337 | phylink_set(pl->supported, 10baseT_Full); |
| 338 | phylink_set(pl->supported, 100baseT_Half); |
| 339 | phylink_set(pl->supported, 100baseT_Full); |
| 340 | phylink_set(pl->supported, 1000baseT_Half); |
| 341 | phylink_set(pl->supported, 1000baseT_Full); |
| 342 | phylink_set(pl->supported, 1000baseX_Full); |
Jose Abreu | c580165 | 2020-03-09 09:36:24 +0100 | [diff] [blame] | 343 | phylink_set(pl->supported, 1000baseKX_Full); |
Vladimir Oltean | 6cbdcf2 | 2020-01-16 19:36:56 +0200 | [diff] [blame] | 344 | phylink_set(pl->supported, 2500baseT_Full); |
| 345 | phylink_set(pl->supported, 2500baseX_Full); |
| 346 | phylink_set(pl->supported, 5000baseT_Full); |
| 347 | phylink_set(pl->supported, 10000baseT_Full); |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 348 | phylink_set(pl->supported, 10000baseKR_Full); |
Jose Abreu | c580165 | 2020-03-09 09:36:24 +0100 | [diff] [blame] | 349 | phylink_set(pl->supported, 10000baseKX4_Full); |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 350 | phylink_set(pl->supported, 10000baseCR_Full); |
| 351 | phylink_set(pl->supported, 10000baseSR_Full); |
| 352 | phylink_set(pl->supported, 10000baseLR_Full); |
| 353 | phylink_set(pl->supported, 10000baseLRM_Full); |
| 354 | phylink_set(pl->supported, 10000baseER_Full); |
Russell King | da7c186 | 2017-07-25 15:03:34 +0100 | [diff] [blame] | 355 | break; |
| 356 | |
Jose Abreu | 1671c42 | 2020-03-12 18:10:10 +0100 | [diff] [blame] | 357 | case PHY_INTERFACE_MODE_XLGMII: |
| 358 | phylink_set(pl->supported, 25000baseCR_Full); |
| 359 | phylink_set(pl->supported, 25000baseKR_Full); |
| 360 | phylink_set(pl->supported, 25000baseSR_Full); |
| 361 | phylink_set(pl->supported, 40000baseKR4_Full); |
| 362 | phylink_set(pl->supported, 40000baseCR4_Full); |
| 363 | phylink_set(pl->supported, 40000baseSR4_Full); |
| 364 | phylink_set(pl->supported, 40000baseLR4_Full); |
| 365 | phylink_set(pl->supported, 50000baseCR2_Full); |
| 366 | phylink_set(pl->supported, 50000baseKR2_Full); |
| 367 | phylink_set(pl->supported, 50000baseSR2_Full); |
| 368 | phylink_set(pl->supported, 50000baseKR_Full); |
| 369 | phylink_set(pl->supported, 50000baseSR_Full); |
| 370 | phylink_set(pl->supported, 50000baseCR_Full); |
| 371 | phylink_set(pl->supported, 50000baseLR_ER_FR_Full); |
| 372 | phylink_set(pl->supported, 50000baseDR_Full); |
| 373 | phylink_set(pl->supported, 100000baseKR4_Full); |
| 374 | phylink_set(pl->supported, 100000baseSR4_Full); |
| 375 | phylink_set(pl->supported, 100000baseCR4_Full); |
| 376 | phylink_set(pl->supported, 100000baseLR4_ER4_Full); |
| 377 | phylink_set(pl->supported, 100000baseKR2_Full); |
| 378 | phylink_set(pl->supported, 100000baseSR2_Full); |
| 379 | phylink_set(pl->supported, 100000baseCR2_Full); |
| 380 | phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full); |
| 381 | phylink_set(pl->supported, 100000baseDR2_Full); |
| 382 | break; |
| 383 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 384 | default: |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 385 | phylink_err(pl, |
| 386 | "incorrect link mode %s for in-band status\n", |
| 387 | phy_modes(pl->link_config.interface)); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 392 | |
| 393 | if (phylink_validate(pl, pl->supported, &pl->link_config)) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 394 | phylink_err(pl, |
| 395 | "failed to validate link configuration for in-band status\n"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 396 | return -EINVAL; |
| 397 | } |
Jose Abreu | 9414819 | 2020-03-09 09:36:25 +0100 | [diff] [blame] | 398 | |
| 399 | /* Check if MAC/PCS also supports Autoneg. */ |
| 400 | pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
Russell King | 2d5fbef | 2020-02-15 15:49:43 +0000 | [diff] [blame] | 406 | static void phylink_apply_manual_flow(struct phylink *pl, |
| 407 | struct phylink_link_state *state) |
| 408 | { |
| 409 | /* If autoneg is disabled, pause AN is also disabled */ |
| 410 | if (!state->an_enabled) |
| 411 | state->pause &= ~MLO_PAUSE_AN; |
| 412 | |
| 413 | /* Manual configuration of pause modes */ |
| 414 | if (!(pl->link_config.pause & MLO_PAUSE_AN)) |
| 415 | state->pause = pl->link_config.pause; |
| 416 | } |
| 417 | |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 418 | static void phylink_resolve_flow(struct phylink_link_state *state) |
| 419 | { |
| 420 | bool tx_pause, rx_pause; |
| 421 | |
| 422 | state->pause = MLO_PAUSE_NONE; |
| 423 | if (state->duplex == DUPLEX_FULL) { |
| 424 | linkmode_resolve_pause(state->advertising, |
| 425 | state->lp_advertising, |
| 426 | &tx_pause, &rx_pause); |
| 427 | if (tx_pause) |
| 428 | state->pause |= MLO_PAUSE_TX; |
| 429 | if (rx_pause) |
| 430 | state->pause |= MLO_PAUSE_RX; |
| 431 | } |
| 432 | } |
| 433 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 434 | static void phylink_mac_config(struct phylink *pl, |
| 435 | const struct phylink_link_state *state) |
| 436 | { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 437 | phylink_dbg(pl, |
| 438 | "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n", |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 439 | __func__, phylink_an_mode_str(pl->cur_link_an_mode), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 440 | phy_modes(state->interface), |
| 441 | phy_speed_to_str(state->speed), |
| 442 | phy_duplex_to_str(state->duplex), |
| 443 | __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising, |
| 444 | state->pause, state->link, state->an_enabled); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 445 | |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 446 | pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 449 | static void phylink_mac_pcs_an_restart(struct phylink *pl) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 450 | { |
| 451 | if (pl->link_config.an_enabled && |
Russell King | 575691b | 2020-06-24 12:30:04 +0100 | [diff] [blame] | 452 | phy_interface_mode_is_8023z(pl->link_config.interface) && |
| 453 | phylink_autoneg_inband(pl->cur_link_an_mode)) { |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 454 | if (pl->pcs_ops) |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 455 | pl->pcs_ops->pcs_an_restart(pl->pcs); |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 456 | else |
| 457 | pl->mac_ops->mac_an_restart(pl->config); |
| 458 | } |
| 459 | } |
| 460 | |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 461 | static void phylink_major_config(struct phylink *pl, bool restart, |
| 462 | const struct phylink_link_state *state) |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 463 | { |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 464 | int err; |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 465 | |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 466 | phylink_dbg(pl, "major config %s\n", phy_modes(state->interface)); |
| 467 | |
| 468 | if (pl->mac_ops->mac_prepare) { |
| 469 | err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode, |
| 470 | state->interface); |
| 471 | if (err < 0) { |
| 472 | phylink_err(pl, "mac_prepare failed: %pe\n", |
| 473 | ERR_PTR(err)); |
| 474 | return; |
| 475 | } |
| 476 | } |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 477 | |
| 478 | phylink_mac_config(pl, state); |
| 479 | |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 480 | if (pl->pcs_ops) { |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 481 | err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode, |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 482 | state->interface, |
| 483 | state->advertising, |
| 484 | !!(pl->link_config.pause & |
| 485 | MLO_PAUSE_AN)); |
| 486 | if (err < 0) |
| 487 | phylink_err(pl, "pcs_config failed: %pe\n", |
| 488 | ERR_PTR(err)); |
| 489 | if (err > 0) |
| 490 | restart = true; |
| 491 | } |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 492 | if (restart) |
| 493 | phylink_mac_pcs_an_restart(pl); |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 494 | |
| 495 | if (pl->mac_ops->mac_finish) { |
| 496 | err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode, |
| 497 | state->interface); |
| 498 | if (err < 0) |
Ong Boon Leong | d82c6c1 | 2021-03-15 12:33:42 +0800 | [diff] [blame] | 499 | phylink_err(pl, "mac_finish failed: %pe\n", |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 500 | ERR_PTR(err)); |
| 501 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Russell King | 1571e70 | 2020-07-21 12:04:36 +0100 | [diff] [blame] | 504 | /* |
| 505 | * Reconfigure for a change of inband advertisement. |
| 506 | * If we have a separate PCS, we only need to call its pcs_config() method, |
| 507 | * and then restart AN if it indicates something changed. Otherwise, we do |
| 508 | * the full MAC reconfiguration. |
| 509 | */ |
| 510 | static int phylink_change_inband_advert(struct phylink *pl) |
| 511 | { |
| 512 | int ret; |
| 513 | |
| 514 | if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) |
| 515 | return 0; |
| 516 | |
| 517 | if (!pl->pcs_ops) { |
| 518 | /* Legacy method */ |
| 519 | phylink_mac_config(pl, &pl->link_config); |
| 520 | phylink_mac_pcs_an_restart(pl); |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__, |
| 525 | phylink_an_mode_str(pl->cur_link_an_mode), |
| 526 | phy_modes(pl->link_config.interface), |
| 527 | __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising, |
| 528 | pl->link_config.pause); |
| 529 | |
| 530 | /* Modern PCS-based method; update the advert at the PCS, and |
| 531 | * restart negotiation if the pcs_config() helper indicates that |
| 532 | * the programmed advertisement has changed. |
| 533 | */ |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 534 | ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode, |
Russell King | 1571e70 | 2020-07-21 12:04:36 +0100 | [diff] [blame] | 535 | pl->link_config.interface, |
| 536 | pl->link_config.advertising, |
| 537 | !!(pl->link_config.pause & MLO_PAUSE_AN)); |
| 538 | if (ret < 0) |
| 539 | return ret; |
| 540 | |
| 541 | if (ret > 0) |
| 542 | phylink_mac_pcs_an_restart(pl); |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 547 | static void phylink_mac_pcs_get_state(struct phylink *pl, |
| 548 | struct phylink_link_state *state) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 549 | { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 550 | linkmode_copy(state->advertising, pl->link_config.advertising); |
| 551 | linkmode_zero(state->lp_advertising); |
| 552 | state->interface = pl->link_config.interface; |
| 553 | state->an_enabled = pl->link_config.an_enabled; |
Heiner Kallweit | d25ed41 | 2019-02-26 19:29:22 +0100 | [diff] [blame] | 554 | state->speed = SPEED_UNKNOWN; |
| 555 | state->duplex = DUPLEX_UNKNOWN; |
| 556 | state->pause = MLO_PAUSE_NONE; |
| 557 | state->an_complete = 0; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 558 | state->link = 1; |
| 559 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 560 | if (pl->pcs_ops) |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 561 | pl->pcs_ops->pcs_get_state(pl->pcs, state); |
Russell King | e859a60 | 2020-08-28 11:53:53 +0100 | [diff] [blame] | 562 | else if (pl->mac_ops->mac_pcs_get_state) |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 563 | pl->mac_ops->mac_pcs_get_state(pl->config, state); |
Russell King | e859a60 | 2020-08-28 11:53:53 +0100 | [diff] [blame] | 564 | else |
| 565 | state->link = 0; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* The fixed state is... fixed except for the link state, |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 569 | * which may be determined by a GPIO or a callback. |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 570 | */ |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 571 | static void phylink_get_fixed_state(struct phylink *pl, |
| 572 | struct phylink_link_state *state) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 573 | { |
| 574 | *state = pl->link_config; |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 575 | if (pl->config->get_fixed_state) |
| 576 | pl->config->get_fixed_state(pl->config, state); |
Florian Fainelli | 1ac63e3 | 2017-12-12 16:00:28 -0800 | [diff] [blame] | 577 | else if (pl->link_gpio) |
Florian Fainelli | bb322a9 | 2018-05-10 13:17:29 -0700 | [diff] [blame] | 578 | state->link = !!gpiod_get_value_cansleep(pl->link_gpio); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 579 | |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 580 | phylink_resolve_flow(state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 583 | static void phylink_mac_initial_config(struct phylink *pl, bool force_restart) |
Russell King | 97fec51 | 2020-02-15 15:50:03 +0000 | [diff] [blame] | 584 | { |
| 585 | struct phylink_link_state link_state; |
| 586 | |
| 587 | switch (pl->cur_link_an_mode) { |
| 588 | case MLO_AN_PHY: |
| 589 | link_state = pl->phy_state; |
| 590 | break; |
| 591 | |
| 592 | case MLO_AN_FIXED: |
| 593 | phylink_get_fixed_state(pl, &link_state); |
| 594 | break; |
| 595 | |
| 596 | case MLO_AN_INBAND: |
| 597 | link_state = pl->link_config; |
| 598 | if (link_state.interface == PHY_INTERFACE_MODE_SGMII) |
| 599 | link_state.pause = MLO_PAUSE_NONE; |
| 600 | break; |
| 601 | |
| 602 | default: /* can't happen */ |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | link_state.link = false; |
| 607 | |
| 608 | phylink_apply_manual_flow(pl, &link_state); |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 609 | phylink_major_config(pl, force_restart, &link_state); |
Russell King | 97fec51 | 2020-02-15 15:50:03 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 612 | static const char *phylink_pause_to_str(int pause) |
| 613 | { |
| 614 | switch (pause & MLO_PAUSE_TXRX_MASK) { |
| 615 | case MLO_PAUSE_TX | MLO_PAUSE_RX: |
| 616 | return "rx/tx"; |
| 617 | case MLO_PAUSE_TX: |
| 618 | return "tx"; |
| 619 | case MLO_PAUSE_RX: |
| 620 | return "rx"; |
| 621 | default: |
| 622 | return "off"; |
| 623 | } |
| 624 | } |
| 625 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 626 | static void phylink_link_up(struct phylink *pl, |
| 627 | struct phylink_link_state link_state) |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 628 | { |
| 629 | struct net_device *ndev = pl->netdev; |
| 630 | |
David S. Miller | b4b12b0 | 2019-05-31 10:49:43 -0700 | [diff] [blame] | 631 | pl->cur_interface = link_state.interface; |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 632 | |
| 633 | if (pl->pcs_ops && pl->pcs_ops->pcs_link_up) |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 634 | pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode, |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 635 | pl->cur_interface, |
| 636 | link_state.speed, link_state.duplex); |
| 637 | |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 638 | pl->mac_ops->mac_link_up(pl->config, pl->phydev, |
| 639 | pl->cur_link_an_mode, pl->cur_interface, |
| 640 | link_state.speed, link_state.duplex, |
| 641 | !!(link_state.pause & MLO_PAUSE_TX), |
| 642 | !!(link_state.pause & MLO_PAUSE_RX)); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 643 | |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 644 | if (ndev) |
| 645 | netif_carrier_on(ndev); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 646 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 647 | phylink_info(pl, |
| 648 | "Link is Up - %s/%s - flow control %s\n", |
| 649 | phy_speed_to_str(link_state.speed), |
| 650 | phy_duplex_to_str(link_state.duplex), |
| 651 | phylink_pause_to_str(link_state.pause)); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 652 | } |
| 653 | |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 654 | static void phylink_link_down(struct phylink *pl) |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 655 | { |
| 656 | struct net_device *ndev = pl->netdev; |
| 657 | |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 658 | if (ndev) |
| 659 | netif_carrier_off(ndev); |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 660 | pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode, |
| 661 | pl->cur_interface); |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 662 | phylink_info(pl, "Link is Down\n"); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 663 | } |
| 664 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 665 | static void phylink_resolve(struct work_struct *w) |
| 666 | { |
| 667 | struct phylink *pl = container_of(w, struct phylink, resolve); |
| 668 | struct phylink_link_state link_state; |
| 669 | struct net_device *ndev = pl->netdev; |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 670 | bool mac_config = false; |
Russell King | b06e5ca | 2020-07-21 12:03:50 +0100 | [diff] [blame] | 671 | bool cur_link_state; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 672 | |
| 673 | mutex_lock(&pl->state_mutex); |
Russell King | b06e5ca | 2020-07-21 12:03:50 +0100 | [diff] [blame] | 674 | if (pl->netdev) |
| 675 | cur_link_state = netif_carrier_ok(ndev); |
| 676 | else |
| 677 | cur_link_state = pl->old_link_state; |
| 678 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 679 | if (pl->phylink_disable_state) { |
| 680 | pl->mac_link_dropped = false; |
| 681 | link_state.link = false; |
| 682 | } else if (pl->mac_link_dropped) { |
| 683 | link_state.link = false; |
| 684 | } else { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 685 | switch (pl->cur_link_an_mode) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 686 | case MLO_AN_PHY: |
| 687 | link_state = pl->phy_state; |
Russell King | 2d5fbef | 2020-02-15 15:49:43 +0000 | [diff] [blame] | 688 | phylink_apply_manual_flow(pl, &link_state); |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 689 | mac_config = link_state.link; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 690 | break; |
| 691 | |
| 692 | case MLO_AN_FIXED: |
| 693 | phylink_get_fixed_state(pl, &link_state); |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 694 | mac_config = link_state.link; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 695 | break; |
| 696 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 697 | case MLO_AN_INBAND: |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 698 | phylink_mac_pcs_get_state(pl, &link_state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 699 | |
Russell King | 406cb0c | 2019-05-20 16:07:20 +0100 | [diff] [blame] | 700 | /* If we have a phy, the "up" state is the union of |
Wenpeng Liang | 1953feb | 2021-06-16 18:01:20 +0800 | [diff] [blame] | 701 | * both the PHY and the MAC |
| 702 | */ |
Russell King | 406cb0c | 2019-05-20 16:07:20 +0100 | [diff] [blame] | 703 | if (pl->phydev) |
| 704 | link_state.link &= pl->phy_state.link; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 705 | |
Russell King | 406cb0c | 2019-05-20 16:07:20 +0100 | [diff] [blame] | 706 | /* Only update if the PHY link is up */ |
| 707 | if (pl->phydev && pl->phy_state.link) { |
| 708 | link_state.interface = pl->phy_state.interface; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 709 | |
Russell King | 406cb0c | 2019-05-20 16:07:20 +0100 | [diff] [blame] | 710 | /* If we have a PHY, we need to update with |
Wenpeng Liang | 1953feb | 2021-06-16 18:01:20 +0800 | [diff] [blame] | 711 | * the PHY flow control bits. |
| 712 | */ |
Russell King | 33faac8 | 2020-02-15 15:49:48 +0000 | [diff] [blame] | 713 | link_state.pause = pl->phy_state.pause; |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 714 | mac_config = true; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 715 | } |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 716 | phylink_apply_manual_flow(pl, &link_state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 717 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
Russell King | 16319a7 | 2020-07-21 12:04:00 +0100 | [diff] [blame] | 721 | if (mac_config) { |
| 722 | if (link_state.interface != pl->link_config.interface) { |
| 723 | /* The interface has changed, force the link down and |
| 724 | * then reconfigure. |
| 725 | */ |
| 726 | if (cur_link_state) { |
| 727 | phylink_link_down(pl); |
| 728 | cur_link_state = false; |
| 729 | } |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 730 | phylink_major_config(pl, false, &link_state); |
Russell King | 5005b16 | 2020-07-21 12:04:05 +0100 | [diff] [blame] | 731 | pl->link_config.interface = link_state.interface; |
Russell King | 7cceb59 | 2020-07-21 12:04:10 +0100 | [diff] [blame] | 732 | } else if (!pl->pcs_ops) { |
Russell King | 5005b16 | 2020-07-21 12:04:05 +0100 | [diff] [blame] | 733 | /* The interface remains unchanged, only the speed, |
| 734 | * duplex or pause settings have changed. Call the |
Russell King | 7cceb59 | 2020-07-21 12:04:10 +0100 | [diff] [blame] | 735 | * old mac_config() method to configure the MAC/PCS |
| 736 | * only if we do not have a PCS installed (an |
| 737 | * unconverted user.) |
Russell King | 5005b16 | 2020-07-21 12:04:05 +0100 | [diff] [blame] | 738 | */ |
| 739 | phylink_mac_config(pl, &link_state); |
Russell King | 16319a7 | 2020-07-21 12:04:00 +0100 | [diff] [blame] | 740 | } |
Russell King | 16319a7 | 2020-07-21 12:04:00 +0100 | [diff] [blame] | 741 | } |
Russell King | 319bfaf | 2020-07-21 12:03:55 +0100 | [diff] [blame] | 742 | |
Russell King | b06e5ca | 2020-07-21 12:03:50 +0100 | [diff] [blame] | 743 | if (link_state.link != cur_link_state) { |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 744 | pl->old_link_state = link_state.link; |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 745 | if (!link_state.link) |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 746 | phylink_link_down(pl); |
Ioana Ciornei | 27755ff | 2019-05-28 20:38:11 +0300 | [diff] [blame] | 747 | else |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 748 | phylink_link_up(pl, link_state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 749 | } |
| 750 | if (!link_state.link && pl->mac_link_dropped) { |
| 751 | pl->mac_link_dropped = false; |
| 752 | queue_work(system_power_efficient_wq, &pl->resolve); |
| 753 | } |
| 754 | mutex_unlock(&pl->state_mutex); |
| 755 | } |
| 756 | |
| 757 | static void phylink_run_resolve(struct phylink *pl) |
| 758 | { |
| 759 | if (!pl->phylink_disable_state) |
| 760 | queue_work(system_power_efficient_wq, &pl->resolve); |
| 761 | } |
| 762 | |
Russell King | 87454b6 | 2019-02-11 15:04:24 +0000 | [diff] [blame] | 763 | static void phylink_run_resolve_and_disable(struct phylink *pl, int bit) |
| 764 | { |
| 765 | unsigned long state = pl->phylink_disable_state; |
| 766 | |
| 767 | set_bit(bit, &pl->phylink_disable_state); |
| 768 | if (state == 0) { |
| 769 | queue_work(system_power_efficient_wq, &pl->resolve); |
| 770 | flush_work(&pl->resolve); |
| 771 | } |
| 772 | } |
| 773 | |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 774 | static void phylink_fixed_poll(struct timer_list *t) |
| 775 | { |
| 776 | struct phylink *pl = container_of(t, struct phylink, link_poll); |
| 777 | |
| 778 | mod_timer(t, jiffies + HZ); |
| 779 | |
| 780 | phylink_run_resolve(pl); |
| 781 | } |
| 782 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 783 | static const struct sfp_upstream_ops sfp_phylink_ops; |
| 784 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 785 | static int phylink_register_sfp(struct phylink *pl, |
| 786 | struct fwnode_handle *fwnode) |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 787 | { |
Russell King | 2203cbf | 2019-10-15 11:38:39 +0100 | [diff] [blame] | 788 | struct sfp_bus *bus; |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 789 | int ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 790 | |
Russell King | eed70fd | 2020-01-03 15:13:56 +0000 | [diff] [blame] | 791 | if (!fwnode) |
| 792 | return 0; |
| 793 | |
Russell King | 727b366 | 2019-11-08 17:39:29 +0000 | [diff] [blame] | 794 | bus = sfp_bus_find_fwnode(fwnode); |
Russell King | 2203cbf | 2019-10-15 11:38:39 +0100 | [diff] [blame] | 795 | if (IS_ERR(bus)) { |
| 796 | ret = PTR_ERR(bus); |
| 797 | phylink_err(pl, "unable to attach SFP bus: %d\n", ret); |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 798 | return ret; |
| 799 | } |
| 800 | |
Russell King | 2203cbf | 2019-10-15 11:38:39 +0100 | [diff] [blame] | 801 | pl->sfp_bus = bus; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 802 | |
Russell King | 727b366 | 2019-11-08 17:39:29 +0000 | [diff] [blame] | 803 | ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops); |
| 804 | sfp_bus_put(bus); |
| 805 | |
| 806 | return ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 807 | } |
| 808 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 809 | /** |
| 810 | * phylink_create() - create a phylink instance |
Randy Dunlap | 9db74e5 | 2019-10-08 08:39:04 -0700 | [diff] [blame] | 811 | * @config: a pointer to the target &struct phylink_config |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 812 | * @fwnode: a pointer to a &struct fwnode_handle describing the network |
| 813 | * interface |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 814 | * @iface: the desired link mode defined by &typedef phy_interface_t |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 815 | * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC. |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 816 | * |
| 817 | * Create a new phylink instance, and parse the link parameters found in @np. |
| 818 | * This will parse in-band modes, fixed-link or SFP configuration. |
| 819 | * |
Russell King | 269a6b5 | 2019-11-19 17:18:52 +0000 | [diff] [blame] | 820 | * Note: the rtnl lock must not be held when calling this function. |
| 821 | * |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 822 | * Returns a pointer to a &struct phylink, or an error-pointer value. Users |
| 823 | * must use IS_ERR() to check for errors from this function. |
| 824 | */ |
Ioana Ciornei | 44cc27e | 2019-05-28 20:38:12 +0300 | [diff] [blame] | 825 | struct phylink *phylink_create(struct phylink_config *config, |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 826 | struct fwnode_handle *fwnode, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 827 | phy_interface_t iface, |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 828 | const struct phylink_mac_ops *mac_ops) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 829 | { |
| 830 | struct phylink *pl; |
| 831 | int ret; |
| 832 | |
| 833 | pl = kzalloc(sizeof(*pl), GFP_KERNEL); |
| 834 | if (!pl) |
| 835 | return ERR_PTR(-ENOMEM); |
| 836 | |
| 837 | mutex_init(&pl->state_mutex); |
| 838 | INIT_WORK(&pl->resolve, phylink_resolve); |
Ioana Ciornei | 44cc27e | 2019-05-28 20:38:12 +0300 | [diff] [blame] | 839 | |
| 840 | pl->config = config; |
| 841 | if (config->type == PHYLINK_NETDEV) { |
| 842 | pl->netdev = to_net_dev(config->dev); |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 843 | } else if (config->type == PHYLINK_DEV) { |
| 844 | pl->dev = config->dev; |
Ioana Ciornei | 44cc27e | 2019-05-28 20:38:12 +0300 | [diff] [blame] | 845 | } else { |
| 846 | kfree(pl); |
| 847 | return ERR_PTR(-EINVAL); |
| 848 | } |
| 849 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 850 | pl->phy_state.interface = iface; |
| 851 | pl->link_interface = iface; |
Florian Fainelli | 4be11ef | 2017-12-12 16:00:29 -0800 | [diff] [blame] | 852 | if (iface == PHY_INTERFACE_MODE_MOCA) |
| 853 | pl->link_port = PORT_BNC; |
| 854 | else |
| 855 | pl->link_port = PORT_MII; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 856 | pl->link_config.interface = iface; |
| 857 | pl->link_config.pause = MLO_PAUSE_AN; |
| 858 | pl->link_config.speed = SPEED_UNKNOWN; |
| 859 | pl->link_config.duplex = DUPLEX_UNKNOWN; |
Russell King | 74ee0e8 | 2017-12-20 23:21:34 +0000 | [diff] [blame] | 860 | pl->link_config.an_enabled = true; |
Russell King | e7765d6 | 2020-03-30 18:44:50 +0100 | [diff] [blame] | 861 | pl->mac_ops = mac_ops; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 862 | __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 863 | timer_setup(&pl->link_poll, phylink_fixed_poll, 0); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 864 | |
| 865 | bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 866 | linkmode_copy(pl->link_config.advertising, pl->supported); |
| 867 | phylink_validate(pl, pl->supported, &pl->link_config); |
| 868 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 869 | ret = phylink_parse_mode(pl, fwnode); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 870 | if (ret < 0) { |
| 871 | kfree(pl); |
| 872 | return ERR_PTR(ret); |
| 873 | } |
| 874 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 875 | if (pl->cfg_link_an_mode == MLO_AN_FIXED) { |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 876 | ret = phylink_parse_fixedlink(pl, fwnode); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 877 | if (ret < 0) { |
| 878 | kfree(pl); |
| 879 | return ERR_PTR(ret); |
| 880 | } |
| 881 | } |
| 882 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 883 | pl->cur_link_an_mode = pl->cfg_link_an_mode; |
| 884 | |
Russell King | 8fa7b9b | 2017-12-01 10:25:09 +0000 | [diff] [blame] | 885 | ret = phylink_register_sfp(pl, fwnode); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 886 | if (ret < 0) { |
| 887 | kfree(pl); |
| 888 | return ERR_PTR(ret); |
| 889 | } |
| 890 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 891 | return pl; |
| 892 | } |
| 893 | EXPORT_SYMBOL_GPL(phylink_create); |
| 894 | |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 895 | /** |
| 896 | * phylink_set_pcs() - set the current PCS for phylink to use |
| 897 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 898 | * @pcs: a pointer to the &struct phylink_pcs |
| 899 | * |
| 900 | * Bind the MAC PCS to phylink. This may be called after phylink_create(), |
| 901 | * in mac_prepare() or mac_config() methods if it is desired to dynamically |
| 902 | * change the PCS. |
| 903 | * |
| 904 | * Please note that there are behavioural changes with the mac_config() |
| 905 | * callback if a PCS is present (denoting a newer setup) so removing a PCS |
| 906 | * is not supported, and if a PCS is going to be used, it must be registered |
| 907 | * by calling phylink_set_pcs() at the latest in the first mac_config() call. |
| 908 | */ |
| 909 | void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs) |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 910 | { |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 911 | pl->pcs = pcs; |
| 912 | pl->pcs_ops = pcs->ops; |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 913 | } |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 914 | EXPORT_SYMBOL_GPL(phylink_set_pcs); |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 915 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 916 | /** |
| 917 | * phylink_destroy() - cleanup and destroy the phylink instance |
| 918 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 919 | * |
| 920 | * Destroy a phylink instance. Any PHY that has been attached must have been |
| 921 | * cleaned up via phylink_disconnect_phy() prior to calling this function. |
Russell King | 269a6b5 | 2019-11-19 17:18:52 +0000 | [diff] [blame] | 922 | * |
| 923 | * Note: the rtnl lock must not be held when calling this function. |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 924 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 925 | void phylink_destroy(struct phylink *pl) |
| 926 | { |
Russell King | 727b366 | 2019-11-08 17:39:29 +0000 | [diff] [blame] | 927 | sfp_bus_del_upstream(pl->sfp_bus); |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 928 | if (pl->link_gpio) |
Florian Fainelli | daab334 | 2018-05-10 13:17:30 -0700 | [diff] [blame] | 929 | gpiod_put(pl->link_gpio); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 930 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 931 | cancel_work_sync(&pl->resolve); |
| 932 | kfree(pl); |
| 933 | } |
| 934 | EXPORT_SYMBOL_GPL(phylink_destroy); |
| 935 | |
Doug Berger | a307593 | 2020-05-18 15:23:59 -0700 | [diff] [blame] | 936 | static void phylink_phy_change(struct phy_device *phydev, bool up) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 937 | { |
| 938 | struct phylink *pl = phydev->phylink; |
Russell King | 33faac8 | 2020-02-15 15:49:48 +0000 | [diff] [blame] | 939 | bool tx_pause, rx_pause; |
| 940 | |
| 941 | phy_get_pause(phydev, &tx_pause, &rx_pause); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 942 | |
| 943 | mutex_lock(&pl->state_mutex); |
| 944 | pl->phy_state.speed = phydev->speed; |
| 945 | pl->phy_state.duplex = phydev->duplex; |
| 946 | pl->phy_state.pause = MLO_PAUSE_NONE; |
Russell King | 33faac8 | 2020-02-15 15:49:48 +0000 | [diff] [blame] | 947 | if (tx_pause) |
| 948 | pl->phy_state.pause |= MLO_PAUSE_TX; |
| 949 | if (rx_pause) |
| 950 | pl->phy_state.pause |= MLO_PAUSE_RX; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 951 | pl->phy_state.interface = phydev->interface; |
| 952 | pl->phy_state.link = up; |
| 953 | mutex_unlock(&pl->state_mutex); |
| 954 | |
| 955 | phylink_run_resolve(pl); |
| 956 | |
Russell King (Oracle) | d34869b | 2021-07-20 12:15:20 +0100 | [diff] [blame] | 957 | phylink_dbg(pl, "phy link %s %s/%s/%s/%s\n", up ? "up" : "down", |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 958 | phy_modes(phydev->interface), |
| 959 | phy_speed_to_str(phydev->speed), |
Russell King (Oracle) | d34869b | 2021-07-20 12:15:20 +0100 | [diff] [blame] | 960 | phy_duplex_to_str(phydev->duplex), |
| 961 | phylink_pause_to_str(pl->phy_state.pause)); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 964 | static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy, |
| 965 | phy_interface_t interface) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 966 | { |
| 967 | struct phylink_link_state config; |
| 968 | __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); |
Florian Fainelli | e27f178 | 2020-01-12 09:35:38 -0800 | [diff] [blame] | 969 | char *irq_str; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 970 | int ret; |
| 971 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 972 | /* |
| 973 | * This is the new way of dealing with flow control for PHYs, |
| 974 | * as described by Timur Tabi in commit 529ed1275263 ("net: phy: |
| 975 | * phy drivers should not set SUPPORTED_[Asym_]Pause") except |
| 976 | * using our validate call to the MAC, we rely upon the MAC |
| 977 | * clearing the bits from both supported and advertising fields. |
| 978 | */ |
Russell King | 725ea4b | 2019-11-15 20:05:45 +0000 | [diff] [blame] | 979 | phy_support_asym_pause(phy); |
| 980 | |
| 981 | memset(&config, 0, sizeof(config)); |
| 982 | linkmode_copy(supported, phy->supported); |
| 983 | linkmode_copy(config.advertising, phy->advertising); |
Russell King | df3f57a | 2019-12-13 18:22:07 +0000 | [diff] [blame] | 984 | |
| 985 | /* Clause 45 PHYs switch their Serdes lane between several different |
| 986 | * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G |
| 987 | * speeds. We really need to know which interface modes the PHY and |
| 988 | * MAC supports to properly work out which linkmodes can be supported. |
| 989 | */ |
| 990 | if (phy->is_c45 && |
| 991 | interface != PHY_INTERFACE_MODE_RXAUI && |
| 992 | interface != PHY_INTERFACE_MODE_XAUI && |
| 993 | interface != PHY_INTERFACE_MODE_USXGMII) |
| 994 | config.interface = PHY_INTERFACE_MODE_NA; |
| 995 | else |
| 996 | config.interface = interface; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 997 | |
| 998 | ret = phylink_validate(pl, supported, &config); |
Hauke Mehrtens | 20d8bb0 | 2020-03-02 00:55:02 +0100 | [diff] [blame] | 999 | if (ret) { |
| 1000 | phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n", |
| 1001 | phy_modes(config.interface), |
| 1002 | __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported, |
| 1003 | __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising, |
| 1004 | ret); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1005 | return ret; |
Hauke Mehrtens | 20d8bb0 | 2020-03-02 00:55:02 +0100 | [diff] [blame] | 1006 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1007 | |
| 1008 | phy->phylink = pl; |
| 1009 | phy->phy_link_change = phylink_phy_change; |
| 1010 | |
Florian Fainelli | e27f178 | 2020-01-12 09:35:38 -0800 | [diff] [blame] | 1011 | irq_str = phy_attached_info_irq(phy); |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1012 | phylink_info(pl, |
Florian Fainelli | e27f178 | 2020-01-12 09:35:38 -0800 | [diff] [blame] | 1013 | "PHY [%s] driver [%s] (irq=%s)\n", |
| 1014 | dev_name(&phy->mdio.dev), phy->drv->name, irq_str); |
| 1015 | kfree(irq_str); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1016 | |
| 1017 | mutex_lock(&phy->lock); |
| 1018 | mutex_lock(&pl->state_mutex); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1019 | pl->phydev = phy; |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 1020 | pl->phy_state.interface = interface; |
Russell King | 97fec51 | 2020-02-15 15:50:03 +0000 | [diff] [blame] | 1021 | pl->phy_state.pause = MLO_PAUSE_NONE; |
| 1022 | pl->phy_state.speed = SPEED_UNKNOWN; |
| 1023 | pl->phy_state.duplex = DUPLEX_UNKNOWN; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1024 | linkmode_copy(pl->supported, supported); |
| 1025 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 1026 | |
Colin Ian King | cc1122b | 2018-03-01 10:23:03 +0000 | [diff] [blame] | 1027 | /* Restrict the phy advertisement according to the MAC support. */ |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1028 | linkmode_copy(phy->advertising, config.advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1029 | mutex_unlock(&pl->state_mutex); |
| 1030 | mutex_unlock(&phy->lock); |
| 1031 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1032 | phylink_dbg(pl, |
| 1033 | "phy: setting supported %*pb advertising %*pb\n", |
| 1034 | __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported, |
| 1035 | __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1036 | |
Heiner Kallweit | 434a431 | 2019-01-23 07:31:58 +0100 | [diff] [blame] | 1037 | if (phy_interrupt_is_valid(phy)) |
| 1038 | phy_request_interrupt(phy); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1039 | |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 1043 | static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy, |
| 1044 | phy_interface_t interface) |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 1045 | { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1046 | if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED || |
| 1047 | (pl->cfg_link_an_mode == MLO_AN_INBAND && |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 1048 | phy_interface_mode_is_8023z(interface)))) |
| 1049 | return -EINVAL; |
| 1050 | |
| 1051 | if (pl->phydev) |
| 1052 | return -EBUSY; |
| 1053 | |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 1054 | return phy_attach_direct(pl->netdev, phy, 0, interface); |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 1055 | } |
| 1056 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1057 | /** |
| 1058 | * phylink_connect_phy() - connect a PHY to the phylink instance |
| 1059 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1060 | * @phy: a pointer to a &struct phy_device. |
| 1061 | * |
| 1062 | * Connect @phy to the phylink instance specified by @pl by calling |
| 1063 | * phy_attach_direct(). Configure the @phy according to the MAC driver's |
| 1064 | * capabilities, start the PHYLIB state machine and enable any interrupts |
| 1065 | * that the PHY supports. |
| 1066 | * |
| 1067 | * This updates the phylink's ethtool supported and advertising link mode |
| 1068 | * masks. |
| 1069 | * |
| 1070 | * Returns 0 on success or a negative errno. |
| 1071 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1072 | int phylink_connect_phy(struct phylink *pl, struct phy_device *phy) |
| 1073 | { |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 1074 | int ret; |
| 1075 | |
Florian Fainelli | 4904b6e | 2017-12-12 16:00:26 -0800 | [diff] [blame] | 1076 | /* Use PHY device/driver interface */ |
| 1077 | if (pl->link_interface == PHY_INTERFACE_MODE_NA) { |
| 1078 | pl->link_interface = phy->interface; |
| 1079 | pl->link_config.interface = pl->link_interface; |
| 1080 | } |
| 1081 | |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 1082 | ret = phylink_attach_phy(pl, phy, pl->link_interface); |
| 1083 | if (ret < 0) |
| 1084 | return ret; |
| 1085 | |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 1086 | ret = phylink_bringup_phy(pl, phy, pl->link_config.interface); |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 1087 | if (ret) |
| 1088 | phy_detach(phy); |
| 1089 | |
| 1090 | return ret; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1091 | } |
| 1092 | EXPORT_SYMBOL_GPL(phylink_connect_phy); |
| 1093 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1094 | /** |
| 1095 | * phylink_of_phy_connect() - connect the PHY specified in the DT mode. |
| 1096 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1097 | * @dn: a pointer to a &struct device_node. |
Florian Fainelli | 0a62964 | 2017-12-12 16:00:25 -0800 | [diff] [blame] | 1098 | * @flags: PHY-specific flags to communicate to the PHY device driver |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1099 | * |
| 1100 | * Connect the phy specified in the device node @dn to the phylink instance |
| 1101 | * specified by @pl. Actions specified in phylink_connect_phy() will be |
| 1102 | * performed. |
| 1103 | * |
| 1104 | * Returns 0 on success or a negative errno. |
| 1105 | */ |
Florian Fainelli | 0a62964 | 2017-12-12 16:00:25 -0800 | [diff] [blame] | 1106 | int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn, |
| 1107 | u32 flags) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1108 | { |
Calvin Johnson | 423e6e8 | 2021-06-11 13:54:00 +0300 | [diff] [blame] | 1109 | return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1110 | } |
| 1111 | EXPORT_SYMBOL_GPL(phylink_of_phy_connect); |
| 1112 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1113 | /** |
Calvin Johnson | 25396f6 | 2021-06-11 13:53:59 +0300 | [diff] [blame] | 1114 | * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode. |
| 1115 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1116 | * @fwnode: a pointer to a &struct fwnode_handle. |
| 1117 | * @flags: PHY-specific flags to communicate to the PHY device driver |
| 1118 | * |
| 1119 | * Connect the phy specified @fwnode to the phylink instance specified |
| 1120 | * by @pl. |
| 1121 | * |
| 1122 | * Returns 0 on success or a negative errno. |
| 1123 | */ |
| 1124 | int phylink_fwnode_phy_connect(struct phylink *pl, |
| 1125 | struct fwnode_handle *fwnode, |
| 1126 | u32 flags) |
| 1127 | { |
| 1128 | struct fwnode_handle *phy_fwnode; |
| 1129 | struct phy_device *phy_dev; |
| 1130 | int ret; |
| 1131 | |
| 1132 | /* Fixed links and 802.3z are handled without needing a PHY */ |
| 1133 | if (pl->cfg_link_an_mode == MLO_AN_FIXED || |
| 1134 | (pl->cfg_link_an_mode == MLO_AN_INBAND && |
| 1135 | phy_interface_mode_is_8023z(pl->link_interface))) |
| 1136 | return 0; |
| 1137 | |
| 1138 | phy_fwnode = fwnode_get_phy_node(fwnode); |
| 1139 | if (IS_ERR(phy_fwnode)) { |
| 1140 | if (pl->cfg_link_an_mode == MLO_AN_PHY) |
| 1141 | return -ENODEV; |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | phy_dev = fwnode_phy_find_device(phy_fwnode); |
| 1146 | /* We're done with the phy_node handle */ |
| 1147 | fwnode_handle_put(phy_fwnode); |
| 1148 | if (!phy_dev) |
| 1149 | return -ENODEV; |
| 1150 | |
| 1151 | ret = phy_attach_direct(pl->netdev, phy_dev, flags, |
| 1152 | pl->link_interface); |
| 1153 | if (ret) { |
| 1154 | phy_device_free(phy_dev); |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface); |
| 1159 | if (ret) |
| 1160 | phy_detach(phy_dev); |
| 1161 | |
| 1162 | return ret; |
| 1163 | } |
| 1164 | EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect); |
| 1165 | |
| 1166 | /** |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1167 | * phylink_disconnect_phy() - disconnect any PHY attached to the phylink |
| 1168 | * instance. |
| 1169 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1170 | * |
| 1171 | * Disconnect any current PHY from the phylink instance described by @pl. |
| 1172 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1173 | void phylink_disconnect_phy(struct phylink *pl) |
| 1174 | { |
| 1175 | struct phy_device *phy; |
| 1176 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1177 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1178 | |
| 1179 | phy = pl->phydev; |
| 1180 | if (phy) { |
| 1181 | mutex_lock(&phy->lock); |
| 1182 | mutex_lock(&pl->state_mutex); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1183 | pl->phydev = NULL; |
| 1184 | mutex_unlock(&pl->state_mutex); |
| 1185 | mutex_unlock(&phy->lock); |
| 1186 | flush_work(&pl->resolve); |
| 1187 | |
| 1188 | phy_disconnect(phy); |
| 1189 | } |
| 1190 | } |
| 1191 | EXPORT_SYMBOL_GPL(phylink_disconnect_phy); |
| 1192 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1193 | /** |
| 1194 | * phylink_mac_change() - notify phylink of a change in MAC state |
| 1195 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1196 | * @up: indicates whether the link is currently up. |
| 1197 | * |
| 1198 | * The MAC driver should call this driver when the state of its link |
| 1199 | * changes (eg, link failure, new negotiation results, etc.) |
| 1200 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1201 | void phylink_mac_change(struct phylink *pl, bool up) |
| 1202 | { |
| 1203 | if (!up) |
| 1204 | pl->mac_link_dropped = true; |
| 1205 | phylink_run_resolve(pl); |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1206 | phylink_dbg(pl, "mac link %s\n", up ? "up" : "down"); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1207 | } |
| 1208 | EXPORT_SYMBOL_GPL(phylink_mac_change); |
| 1209 | |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 1210 | static irqreturn_t phylink_link_handler(int irq, void *data) |
| 1211 | { |
| 1212 | struct phylink *pl = data; |
| 1213 | |
| 1214 | phylink_run_resolve(pl); |
| 1215 | |
| 1216 | return IRQ_HANDLED; |
| 1217 | } |
| 1218 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1219 | /** |
| 1220 | * phylink_start() - start a phylink instance |
| 1221 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1222 | * |
| 1223 | * Start the phylink instance specified by @pl, configuring the MAC for the |
| 1224 | * desired link mode(s) and negotiation style. This should be called from the |
| 1225 | * network device driver's &struct net_device_ops ndo_open() method. |
| 1226 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1227 | void phylink_start(struct phylink *pl) |
| 1228 | { |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 1229 | bool poll = false; |
| 1230 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1231 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1232 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1233 | phylink_info(pl, "configuring for %s/%s link mode\n", |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1234 | phylink_an_mode_str(pl->cur_link_an_mode), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 1235 | phy_modes(pl->link_config.interface)); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1236 | |
Antoine Tenart | aeeb2e8 | 2018-09-19 11:39:31 +0200 | [diff] [blame] | 1237 | /* Always set the carrier off */ |
Ioana Ciornei | 43de619 | 2019-05-28 20:38:13 +0300 | [diff] [blame] | 1238 | if (pl->netdev) |
| 1239 | netif_carrier_off(pl->netdev); |
Antoine Tenart | aeeb2e8 | 2018-09-19 11:39:31 +0200 | [diff] [blame] | 1240 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1241 | /* Apply the link configuration to the MAC when starting. This allows |
| 1242 | * a fixed-link to start with the correct parameters, and also |
Colin Ian King | cc1122b | 2018-03-01 10:23:03 +0000 | [diff] [blame] | 1243 | * ensures that we set the appropriate advertisement for Serdes links. |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 1244 | * |
| 1245 | * Restart autonegotiation if using 802.3z to ensure that the link |
Russell King | 85b4394 | 2017-12-01 10:24:42 +0000 | [diff] [blame] | 1246 | * parameters are properly negotiated. This is necessary for DSA |
| 1247 | * switches using 802.3z negotiation to ensure they see our modes. |
| 1248 | */ |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 1249 | phylink_mac_initial_config(pl, true); |
Russell King | 85b4394 | 2017-12-01 10:24:42 +0000 | [diff] [blame] | 1250 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1251 | clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); |
| 1252 | phylink_run_resolve(pl); |
| 1253 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1254 | if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) { |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 1255 | int irq = gpiod_to_irq(pl->link_gpio); |
| 1256 | |
| 1257 | if (irq > 0) { |
| 1258 | if (!request_irq(irq, phylink_link_handler, |
| 1259 | IRQF_TRIGGER_RISING | |
| 1260 | IRQF_TRIGGER_FALLING, |
| 1261 | "netdev link", pl)) |
| 1262 | pl->link_irq = irq; |
| 1263 | else |
| 1264 | irq = 0; |
| 1265 | } |
| 1266 | if (irq <= 0) |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 1267 | poll = true; |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 1268 | } |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 1269 | |
| 1270 | switch (pl->cfg_link_an_mode) { |
| 1271 | case MLO_AN_FIXED: |
| 1272 | poll |= pl->config->poll_fixed_state; |
| 1273 | break; |
| 1274 | case MLO_AN_INBAND: |
| 1275 | poll |= pl->config->pcs_poll; |
Russell King | 7137e18 | 2020-07-21 12:04:46 +0100 | [diff] [blame] | 1276 | if (pl->pcs) |
| 1277 | poll |= pl->pcs->poll; |
Russell King | 5c05c1d | 2020-04-23 17:02:56 +0100 | [diff] [blame] | 1278 | break; |
| 1279 | } |
| 1280 | if (poll) |
Russell King | 9cd00a8 | 2018-05-10 13:17:31 -0700 | [diff] [blame] | 1281 | mod_timer(&pl->link_poll, jiffies + HZ); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1282 | if (pl->phydev) |
| 1283 | phy_start(pl->phydev); |
Arseny Solokha | c7fa7f5 | 2019-07-24 20:31:39 +0700 | [diff] [blame] | 1284 | if (pl->sfp_bus) |
| 1285 | sfp_upstream_start(pl->sfp_bus); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1286 | } |
| 1287 | EXPORT_SYMBOL_GPL(phylink_start); |
| 1288 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1289 | /** |
| 1290 | * phylink_stop() - stop a phylink instance |
| 1291 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1292 | * |
| 1293 | * Stop the phylink instance specified by @pl. This should be called from the |
| 1294 | * network device driver's &struct net_device_ops ndo_stop() method. The |
| 1295 | * network device's carrier state should not be changed prior to calling this |
| 1296 | * function. |
Russell King (Oracle) | f974936 | 2021-09-07 18:56:46 +0800 | [diff] [blame] | 1297 | * |
| 1298 | * This will synchronously bring down the link if the link is not already |
| 1299 | * down (in other words, it will trigger a mac_link_down() method call.) |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1300 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1301 | void phylink_stop(struct phylink *pl) |
| 1302 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1303 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1304 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 1305 | if (pl->sfp_bus) |
| 1306 | sfp_upstream_stop(pl->sfp_bus); |
Arseny Solokha | c7fa7f5 | 2019-07-24 20:31:39 +0700 | [diff] [blame] | 1307 | if (pl->phydev) |
| 1308 | phy_stop(pl->phydev); |
Russell King | 7b3b0e8 | 2019-05-28 10:57:23 +0100 | [diff] [blame] | 1309 | del_timer_sync(&pl->link_poll); |
| 1310 | if (pl->link_irq) { |
| 1311 | free_irq(pl->link_irq, pl); |
| 1312 | pl->link_irq = 0; |
| 1313 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1314 | |
Russell King | 87454b6 | 2019-02-11 15:04:24 +0000 | [diff] [blame] | 1315 | phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1316 | } |
| 1317 | EXPORT_SYMBOL_GPL(phylink_stop); |
| 1318 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1319 | /** |
Russell King (Oracle) | f974936 | 2021-09-07 18:56:46 +0800 | [diff] [blame] | 1320 | * phylink_suspend() - handle a network device suspend event |
| 1321 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1322 | * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan |
| 1323 | * |
| 1324 | * Handle a network device suspend event. There are several cases: |
| 1325 | * - If Wake-on-Lan is not active, we can bring down the link between |
| 1326 | * the MAC and PHY by calling phylink_stop(). |
| 1327 | * - If Wake-on-Lan is active, and being handled only by the PHY, we |
| 1328 | * can also bring down the link between the MAC and PHY. |
| 1329 | * - If Wake-on-Lan is active, but being handled by the MAC, the MAC |
| 1330 | * still needs to receive packets, so we can not bring the link down. |
| 1331 | */ |
| 1332 | void phylink_suspend(struct phylink *pl, bool mac_wol) |
| 1333 | { |
| 1334 | ASSERT_RTNL(); |
| 1335 | |
| 1336 | if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) { |
| 1337 | /* Wake-on-Lan enabled, MAC handling */ |
| 1338 | mutex_lock(&pl->state_mutex); |
| 1339 | |
| 1340 | /* Stop the resolver bringing the link up */ |
| 1341 | __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state); |
| 1342 | |
| 1343 | /* Disable the carrier, to prevent transmit timeouts, |
| 1344 | * but one would hope all packets have been sent. This |
| 1345 | * also means phylink_resolve() will do nothing. |
| 1346 | */ |
Russell King (Oracle) | cbcca2e | 2021-09-17 14:36:31 +0100 | [diff] [blame] | 1347 | if (pl->netdev) |
| 1348 | netif_carrier_off(pl->netdev); |
| 1349 | else |
| 1350 | pl->old_link_state = false; |
Russell King (Oracle) | f974936 | 2021-09-07 18:56:46 +0800 | [diff] [blame] | 1351 | |
| 1352 | /* We do not call mac_link_down() here as we want the |
| 1353 | * link to remain up to receive the WoL packets. |
| 1354 | */ |
| 1355 | mutex_unlock(&pl->state_mutex); |
| 1356 | } else { |
| 1357 | phylink_stop(pl); |
| 1358 | } |
| 1359 | } |
| 1360 | EXPORT_SYMBOL_GPL(phylink_suspend); |
| 1361 | |
| 1362 | /** |
| 1363 | * phylink_resume() - handle a network device resume event |
| 1364 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1365 | * |
| 1366 | * Undo the effects of phylink_suspend(), returning the link to an |
| 1367 | * operational state. |
| 1368 | */ |
| 1369 | void phylink_resume(struct phylink *pl) |
| 1370 | { |
| 1371 | ASSERT_RTNL(); |
| 1372 | |
| 1373 | if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) { |
| 1374 | /* Wake-on-Lan enabled, MAC handling */ |
| 1375 | |
| 1376 | /* Call mac_link_down() so we keep the overall state balanced. |
| 1377 | * Do this under the state_mutex lock for consistency. This |
| 1378 | * will cause a "Link Down" message to be printed during |
| 1379 | * resume, which is harmless - the true link state will be |
| 1380 | * printed when we run a resolve. |
| 1381 | */ |
| 1382 | mutex_lock(&pl->state_mutex); |
| 1383 | phylink_link_down(pl); |
| 1384 | mutex_unlock(&pl->state_mutex); |
| 1385 | |
| 1386 | /* Re-apply the link parameters so that all the settings get |
| 1387 | * restored to the MAC. |
| 1388 | */ |
| 1389 | phylink_mac_initial_config(pl, true); |
| 1390 | |
| 1391 | /* Re-enable and re-resolve the link parameters */ |
| 1392 | clear_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state); |
| 1393 | phylink_run_resolve(pl); |
| 1394 | } else { |
| 1395 | phylink_start(pl); |
| 1396 | } |
| 1397 | } |
| 1398 | EXPORT_SYMBOL_GPL(phylink_resume); |
| 1399 | |
| 1400 | /** |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1401 | * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY |
| 1402 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1403 | * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters |
| 1404 | * |
| 1405 | * Read the wake on lan parameters from the PHY attached to the phylink |
| 1406 | * instance specified by @pl. If no PHY is currently attached, report no |
| 1407 | * support for wake on lan. |
| 1408 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1409 | void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol) |
| 1410 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1411 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1412 | |
| 1413 | wol->supported = 0; |
| 1414 | wol->wolopts = 0; |
| 1415 | |
| 1416 | if (pl->phydev) |
| 1417 | phy_ethtool_get_wol(pl->phydev, wol); |
| 1418 | } |
| 1419 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol); |
| 1420 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1421 | /** |
| 1422 | * phylink_ethtool_set_wol() - set wake on lan parameters |
| 1423 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1424 | * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters |
| 1425 | * |
| 1426 | * Set the wake on lan parameters for the PHY attached to the phylink |
| 1427 | * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP |
| 1428 | * error. |
| 1429 | * |
| 1430 | * Returns zero on success or negative errno code. |
| 1431 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1432 | int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol) |
| 1433 | { |
| 1434 | int ret = -EOPNOTSUPP; |
| 1435 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1436 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1437 | |
| 1438 | if (pl->phydev) |
| 1439 | ret = phy_ethtool_set_wol(pl->phydev, wol); |
| 1440 | |
| 1441 | return ret; |
| 1442 | } |
| 1443 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol); |
| 1444 | |
| 1445 | static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b) |
| 1446 | { |
| 1447 | __ETHTOOL_DECLARE_LINK_MODE_MASK(mask); |
| 1448 | |
| 1449 | linkmode_zero(mask); |
| 1450 | phylink_set_port_modes(mask); |
| 1451 | |
| 1452 | linkmode_and(dst, dst, mask); |
| 1453 | linkmode_or(dst, dst, b); |
| 1454 | } |
| 1455 | |
| 1456 | static void phylink_get_ksettings(const struct phylink_link_state *state, |
| 1457 | struct ethtool_link_ksettings *kset) |
| 1458 | { |
| 1459 | phylink_merge_link_mode(kset->link_modes.advertising, state->advertising); |
| 1460 | linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising); |
| 1461 | kset->base.speed = state->speed; |
| 1462 | kset->base.duplex = state->duplex; |
| 1463 | kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE : |
| 1464 | AUTONEG_DISABLE; |
| 1465 | } |
| 1466 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1467 | /** |
| 1468 | * phylink_ethtool_ksettings_get() - get the current link settings |
| 1469 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1470 | * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings |
| 1471 | * |
| 1472 | * Read the current link settings for the phylink instance specified by @pl. |
| 1473 | * This will be the link settings read from the MAC, PHY or fixed link |
| 1474 | * settings depending on the current negotiation mode. |
| 1475 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1476 | int phylink_ethtool_ksettings_get(struct phylink *pl, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 1477 | struct ethtool_link_ksettings *kset) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1478 | { |
| 1479 | struct phylink_link_state link_state; |
| 1480 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1481 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1482 | |
Wenpeng Liang | 169d7a4 | 2021-06-16 18:01:23 +0800 | [diff] [blame] | 1483 | if (pl->phydev) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1484 | phy_ethtool_ksettings_get(pl->phydev, kset); |
Wenpeng Liang | 169d7a4 | 2021-06-16 18:01:23 +0800 | [diff] [blame] | 1485 | else |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1486 | kset->base.port = pl->link_port; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1487 | |
| 1488 | linkmode_copy(kset->link_modes.supported, pl->supported); |
| 1489 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1490 | switch (pl->cur_link_an_mode) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1491 | case MLO_AN_FIXED: |
| 1492 | /* We are using fixed settings. Report these as the |
| 1493 | * current link settings - and note that these also |
| 1494 | * represent the supported speeds/duplex/pause modes. |
| 1495 | */ |
| 1496 | phylink_get_fixed_state(pl, &link_state); |
| 1497 | phylink_get_ksettings(&link_state, kset); |
| 1498 | break; |
| 1499 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 1500 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1501 | /* If there is a phy attached, then use the reported |
| 1502 | * settings from the phy with no modification. |
| 1503 | */ |
| 1504 | if (pl->phydev) |
| 1505 | break; |
| 1506 | |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 1507 | phylink_mac_pcs_get_state(pl, &link_state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1508 | |
| 1509 | /* The MAC is reporting the link results from its own PCS |
| 1510 | * layer via in-band status. Report these as the current |
| 1511 | * link settings. |
| 1512 | */ |
| 1513 | phylink_get_ksettings(&link_state, kset); |
| 1514 | break; |
| 1515 | } |
| 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get); |
| 1520 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1521 | /** |
| 1522 | * phylink_ethtool_ksettings_set() - set the link settings |
| 1523 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1524 | * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes |
| 1525 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1526 | int phylink_ethtool_ksettings_set(struct phylink *pl, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 1527 | const struct ethtool_link_ksettings *kset) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1528 | { |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 1529 | __ETHTOOL_DECLARE_LINK_MODE_MASK(support); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1530 | struct phylink_link_state config; |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1531 | const struct phy_setting *s; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1532 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1533 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1534 | |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1535 | if (pl->phydev) { |
| 1536 | /* We can rely on phylib for this update; we also do not need |
| 1537 | * to update the pl->link_config settings: |
| 1538 | * - the configuration returned via ksettings_get() will come |
| 1539 | * from phylib whenever a PHY is present. |
| 1540 | * - link_config.interface will be updated by the PHY calling |
| 1541 | * back via phylink_phy_change() and a subsequent resolve. |
| 1542 | * - initial link configuration for PHY mode comes from the |
| 1543 | * last phy state updated via phylink_phy_change(). |
| 1544 | * - other configuration changes (e.g. pause modes) are |
| 1545 | * performed directly via phylib. |
| 1546 | * - if in in-band mode with a PHY, the link configuration |
| 1547 | * is passed on the link from the PHY, and all of |
| 1548 | * link_config.{speed,duplex,an_enabled,pause} are not used. |
| 1549 | * - the only possible use would be link_config.advertising |
| 1550 | * pause modes when in 1000base-X mode with a PHY, but in |
| 1551 | * the presence of a PHY, this should not be changed as that |
| 1552 | * should be determined from the media side advertisement. |
| 1553 | */ |
| 1554 | return phy_ethtool_ksettings_set(pl->phydev, kset); |
| 1555 | } |
| 1556 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1557 | config = pl->link_config; |
| 1558 | |
Russell King (Oracle) | 7cefb0b | 2021-07-20 12:15:26 +0100 | [diff] [blame] | 1559 | /* Mask out unsupported advertisements */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1560 | linkmode_and(config.advertising, kset->link_modes.advertising, |
Russell King (Oracle) | 7cefb0b | 2021-07-20 12:15:26 +0100 | [diff] [blame] | 1561 | pl->supported); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1562 | |
| 1563 | /* FIXME: should we reject autoneg if phy/mac does not support it? */ |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1564 | switch (kset->base.autoneg) { |
| 1565 | case AUTONEG_DISABLE: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1566 | /* Autonegotiation disabled, select a suitable speed and |
| 1567 | * duplex. |
| 1568 | */ |
| 1569 | s = phy_lookup_setting(kset->base.speed, kset->base.duplex, |
Russell King (Oracle) | 7cefb0b | 2021-07-20 12:15:26 +0100 | [diff] [blame] | 1570 | pl->supported, false); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1571 | if (!s) |
| 1572 | return -EINVAL; |
| 1573 | |
Russell King | 1e1bf14 | 2020-07-21 12:04:31 +0100 | [diff] [blame] | 1574 | /* If we have a fixed link, refuse to change link parameters. |
| 1575 | * If the link parameters match, accept them but do nothing. |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1576 | */ |
Russell King | 1e1bf14 | 2020-07-21 12:04:31 +0100 | [diff] [blame] | 1577 | if (pl->cur_link_an_mode == MLO_AN_FIXED) { |
| 1578 | if (s->speed != pl->link_config.speed || |
| 1579 | s->duplex != pl->link_config.duplex) |
| 1580 | return -EINVAL; |
| 1581 | return 0; |
| 1582 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1583 | |
| 1584 | config.speed = s->speed; |
| 1585 | config.duplex = s->duplex; |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1586 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1587 | |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1588 | case AUTONEG_ENABLE: |
Russell King | 1e1bf14 | 2020-07-21 12:04:31 +0100 | [diff] [blame] | 1589 | /* If we have a fixed link, allow autonegotiation (since that |
| 1590 | * is our default case) but do not allow the advertisement to |
| 1591 | * be changed. If the advertisement matches, simply return. |
| 1592 | */ |
| 1593 | if (pl->cur_link_an_mode == MLO_AN_FIXED) { |
| 1594 | if (!linkmode_equal(config.advertising, |
| 1595 | pl->link_config.advertising)) |
| 1596 | return -EINVAL; |
| 1597 | return 0; |
| 1598 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1599 | |
| 1600 | config.speed = SPEED_UNKNOWN; |
| 1601 | config.duplex = DUPLEX_UNKNOWN; |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1602 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1603 | |
Russell King | c8cab71 | 2020-07-21 12:04:16 +0100 | [diff] [blame] | 1604 | default: |
| 1605 | return -EINVAL; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
Russell King | 1e1bf14 | 2020-07-21 12:04:31 +0100 | [diff] [blame] | 1608 | /* We have ruled out the case with a PHY attached, and the |
| 1609 | * fixed-link cases. All that is left are in-band links. |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1610 | */ |
Russell King (Oracle) | 7cefb0b | 2021-07-20 12:15:26 +0100 | [diff] [blame] | 1611 | config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE; |
| 1612 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising, |
| 1613 | config.an_enabled); |
| 1614 | |
Nathan Rossi | ea269a6 | 2021-09-02 05:14:49 +0000 | [diff] [blame] | 1615 | /* If this link is with an SFP, ensure that changes to advertised modes |
| 1616 | * also cause the associated interface to be selected such that the |
| 1617 | * link can be configured correctly. |
| 1618 | */ |
Russell King (Oracle) | dc90604 | 2021-10-19 11:00:04 +0100 | [diff] [blame^] | 1619 | if (pl->sfp_bus) { |
Nathan Rossi | ea269a6 | 2021-09-02 05:14:49 +0000 | [diff] [blame] | 1620 | config.interface = sfp_select_interface(pl->sfp_bus, |
| 1621 | config.advertising); |
| 1622 | if (config.interface == PHY_INTERFACE_MODE_NA) { |
| 1623 | phylink_err(pl, |
| 1624 | "selection of interface failed, advertisement %*pb\n", |
| 1625 | __ETHTOOL_LINK_MODE_MASK_NBITS, |
| 1626 | config.advertising); |
| 1627 | return -EINVAL; |
| 1628 | } |
| 1629 | |
| 1630 | /* Revalidate with the selected interface */ |
| 1631 | linkmode_copy(support, pl->supported); |
| 1632 | if (phylink_validate(pl, support, &config)) { |
| 1633 | phylink_err(pl, "validation of %s/%s with support %*pb failed\n", |
| 1634 | phylink_an_mode_str(pl->cur_link_an_mode), |
| 1635 | phy_modes(config.interface), |
| 1636 | __ETHTOOL_LINK_MODE_MASK_NBITS, support); |
| 1637 | return -EINVAL; |
| 1638 | } |
Russell King (Oracle) | dc90604 | 2021-10-19 11:00:04 +0100 | [diff] [blame^] | 1639 | } else { |
| 1640 | /* Validate without changing the current supported mask. */ |
| 1641 | linkmode_copy(support, pl->supported); |
| 1642 | if (phylink_validate(pl, support, &config)) |
| 1643 | return -EINVAL; |
Nathan Rossi | ea269a6 | 2021-09-02 05:14:49 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Russell King (Oracle) | dc90604 | 2021-10-19 11:00:04 +0100 | [diff] [blame^] | 1646 | /* If autonegotiation is enabled, we must have an advertisement */ |
| 1647 | if (config.an_enabled && phylink_is_empty_linkmode(config.advertising)) |
| 1648 | return -EINVAL; |
| 1649 | |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1650 | mutex_lock(&pl->state_mutex); |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1651 | pl->link_config.speed = config.speed; |
| 1652 | pl->link_config.duplex = config.duplex; |
Russell King | a83c882 | 2020-07-21 12:04:26 +0100 | [diff] [blame] | 1653 | pl->link_config.an_enabled = config.an_enabled; |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1654 | |
Russell King | b7ad14c | 2020-07-21 12:04:41 +0100 | [diff] [blame] | 1655 | if (pl->link_config.interface != config.interface) { |
| 1656 | /* The interface changed, e.g. 1000base-X <-> 2500base-X */ |
| 1657 | /* We need to force the link down, then change the interface */ |
| 1658 | if (pl->old_link_state) { |
| 1659 | phylink_link_down(pl); |
| 1660 | pl->old_link_state = false; |
| 1661 | } |
| 1662 | if (!test_bit(PHYLINK_DISABLE_STOPPED, |
| 1663 | &pl->phylink_disable_state)) |
| 1664 | phylink_major_config(pl, false, &config); |
| 1665 | pl->link_config.interface = config.interface; |
| 1666 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 1667 | } else if (!linkmode_equal(pl->link_config.advertising, |
| 1668 | config.advertising)) { |
| 1669 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 1670 | phylink_change_inband_advert(pl); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1671 | } |
Russell King | cbc1bb1 | 2020-07-21 12:04:21 +0100 | [diff] [blame] | 1672 | mutex_unlock(&pl->state_mutex); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1673 | |
Dan Carpenter | d18c2a1 | 2017-08-10 00:35:50 +0300 | [diff] [blame] | 1674 | return 0; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1675 | } |
| 1676 | EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set); |
| 1677 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1678 | /** |
| 1679 | * phylink_ethtool_nway_reset() - restart negotiation |
| 1680 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1681 | * |
| 1682 | * Restart negotiation for the phylink instance specified by @pl. This will |
| 1683 | * cause any attached phy to restart negotiation with the link partner, and |
| 1684 | * if the MAC is in a BaseX mode, the MAC will also be requested to restart |
| 1685 | * negotiation. |
| 1686 | * |
| 1687 | * Returns zero on success, or negative error code. |
| 1688 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1689 | int phylink_ethtool_nway_reset(struct phylink *pl) |
| 1690 | { |
| 1691 | int ret = 0; |
| 1692 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1693 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1694 | |
| 1695 | if (pl->phydev) |
| 1696 | ret = phy_restart_aneg(pl->phydev); |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 1697 | phylink_mac_pcs_an_restart(pl); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1698 | |
| 1699 | return ret; |
| 1700 | } |
| 1701 | EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset); |
| 1702 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1703 | /** |
| 1704 | * phylink_ethtool_get_pauseparam() - get the current pause parameters |
| 1705 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1706 | * @pause: a pointer to a &struct ethtool_pauseparam |
| 1707 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1708 | void phylink_ethtool_get_pauseparam(struct phylink *pl, |
| 1709 | struct ethtool_pauseparam *pause) |
| 1710 | { |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1711 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1712 | |
| 1713 | pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN); |
| 1714 | pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX); |
| 1715 | pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX); |
| 1716 | } |
| 1717 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam); |
| 1718 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1719 | /** |
| 1720 | * phylink_ethtool_set_pauseparam() - set the current pause parameters |
| 1721 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1722 | * @pause: a pointer to a &struct ethtool_pauseparam |
| 1723 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1724 | int phylink_ethtool_set_pauseparam(struct phylink *pl, |
| 1725 | struct ethtool_pauseparam *pause) |
| 1726 | { |
| 1727 | struct phylink_link_state *config = &pl->link_config; |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1728 | bool manual_changed; |
| 1729 | int pause_state; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1730 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1731 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1732 | |
Russell King | 8cdfa25 | 2020-02-15 15:49:37 +0000 | [diff] [blame] | 1733 | if (pl->cur_link_an_mode == MLO_AN_FIXED) |
| 1734 | return -EOPNOTSUPP; |
| 1735 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1736 | if (!phylink_test(pl->supported, Pause) && |
| 1737 | !phylink_test(pl->supported, Asym_Pause)) |
| 1738 | return -EOPNOTSUPP; |
| 1739 | |
| 1740 | if (!phylink_test(pl->supported, Asym_Pause) && |
| 1741 | !pause->autoneg && pause->rx_pause != pause->tx_pause) |
| 1742 | return -EINVAL; |
| 1743 | |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1744 | pause_state = 0; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1745 | if (pause->autoneg) |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1746 | pause_state |= MLO_PAUSE_AN; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1747 | if (pause->rx_pause) |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1748 | pause_state |= MLO_PAUSE_RX; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1749 | if (pause->tx_pause) |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1750 | pause_state |= MLO_PAUSE_TX; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1751 | |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1752 | mutex_lock(&pl->state_mutex); |
Russell King | f904f15 | 2020-02-15 15:49:58 +0000 | [diff] [blame] | 1753 | /* |
| 1754 | * See the comments for linkmode_set_pause(), wrt the deficiencies |
| 1755 | * with the current implementation. A solution to this issue would |
| 1756 | * be: |
| 1757 | * ethtool Local device |
| 1758 | * rx tx Pause AsymDir |
| 1759 | * 0 0 0 0 |
| 1760 | * 1 0 1 1 |
| 1761 | * 0 1 0 1 |
| 1762 | * 1 1 1 1 |
| 1763 | * and then use the ethtool rx/tx enablement status to mask the |
| 1764 | * rx/tx pause resolution. |
| 1765 | */ |
| 1766 | linkmode_set_pause(config->advertising, pause->tx_pause, |
| 1767 | pause->rx_pause); |
| 1768 | |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1769 | manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN || |
| 1770 | (!(pause_state & MLO_PAUSE_AN) && |
| 1771 | (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK); |
| 1772 | |
| 1773 | config->pause = pause_state; |
| 1774 | |
Russell King | 1571e70 | 2020-07-21 12:04:36 +0100 | [diff] [blame] | 1775 | /* Update our in-band advertisement, triggering a renegotiation if |
| 1776 | * the advertisement changed. |
| 1777 | */ |
| 1778 | if (!pl->phydev) |
| 1779 | phylink_change_inband_advert(pl); |
Russell King | c718af2 | 2020-06-23 17:47:23 +0100 | [diff] [blame] | 1780 | |
| 1781 | mutex_unlock(&pl->state_mutex); |
| 1782 | |
| 1783 | /* If we have a PHY, a change of the pause frame advertisement will |
| 1784 | * cause phylib to renegotiate (if AN is enabled) which will in turn |
| 1785 | * call our phylink_phy_change() and trigger a resolve. Note that |
| 1786 | * we can't hold our state mutex while calling phy_set_asym_pause(). |
Russell King | d9922c0 | 2019-11-19 17:28:14 +0000 | [diff] [blame] | 1787 | */ |
Russell King | c718af2 | 2020-06-23 17:47:23 +0100 | [diff] [blame] | 1788 | if (pl->phydev) |
Russell King | d9922c0 | 2019-11-19 17:28:14 +0000 | [diff] [blame] | 1789 | phy_set_asym_pause(pl->phydev, pause->rx_pause, |
| 1790 | pause->tx_pause); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1791 | |
Russell King | 2e919bc | 2020-06-23 17:47:29 +0100 | [diff] [blame] | 1792 | /* If the manual pause settings changed, make sure we trigger a |
| 1793 | * resolve to update their state; we can not guarantee that the |
| 1794 | * link will cycle. |
| 1795 | */ |
| 1796 | if (manual_changed) { |
| 1797 | pl->mac_link_dropped = true; |
| 1798 | phylink_run_resolve(pl); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1799 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1800 | |
| 1801 | return 0; |
| 1802 | } |
| 1803 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam); |
| 1804 | |
| 1805 | /** |
Mauro Carvalho Chehab | 6928022 | 2020-11-16 11:17:57 +0100 | [diff] [blame] | 1806 | * phylink_get_eee_err() - read the energy efficient ethernet error |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1807 | * counter |
| 1808 | * @pl: a pointer to a &struct phylink returned from phylink_create(). |
| 1809 | * |
| 1810 | * Read the Energy Efficient Ethernet error counter from the PHY associated |
| 1811 | * with the phylink instance specified by @pl. |
| 1812 | * |
| 1813 | * Returns positive error counter value, or negative error code. |
| 1814 | */ |
| 1815 | int phylink_get_eee_err(struct phylink *pl) |
| 1816 | { |
| 1817 | int ret = 0; |
| 1818 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1819 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1820 | |
| 1821 | if (pl->phydev) |
| 1822 | ret = phy_get_eee_err(pl->phydev); |
| 1823 | |
| 1824 | return ret; |
| 1825 | } |
| 1826 | EXPORT_SYMBOL_GPL(phylink_get_eee_err); |
| 1827 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1828 | /** |
Russell King | 86e5813 | 2019-02-11 11:46:06 +0000 | [diff] [blame] | 1829 | * phylink_init_eee() - init and check the EEE features |
| 1830 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1831 | * @clk_stop_enable: allow PHY to stop receive clock |
| 1832 | * |
| 1833 | * Must be called either with RTNL held or within mac_link_up() |
| 1834 | */ |
| 1835 | int phylink_init_eee(struct phylink *pl, bool clk_stop_enable) |
| 1836 | { |
| 1837 | int ret = -EOPNOTSUPP; |
| 1838 | |
| 1839 | if (pl->phydev) |
| 1840 | ret = phy_init_eee(pl->phydev, clk_stop_enable); |
| 1841 | |
| 1842 | return ret; |
| 1843 | } |
| 1844 | EXPORT_SYMBOL_GPL(phylink_init_eee); |
| 1845 | |
| 1846 | /** |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1847 | * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters |
| 1848 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1849 | * @eee: a pointer to a &struct ethtool_eee for the read parameters |
| 1850 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1851 | int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee) |
| 1852 | { |
| 1853 | int ret = -EOPNOTSUPP; |
| 1854 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1855 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1856 | |
| 1857 | if (pl->phydev) |
| 1858 | ret = phy_ethtool_get_eee(pl->phydev, eee); |
| 1859 | |
| 1860 | return ret; |
| 1861 | } |
| 1862 | EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee); |
| 1863 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 1864 | /** |
| 1865 | * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters |
| 1866 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 1867 | * @eee: a pointer to a &struct ethtool_eee for the desired parameters |
| 1868 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1869 | int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee) |
| 1870 | { |
| 1871 | int ret = -EOPNOTSUPP; |
| 1872 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 1873 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1874 | |
| 1875 | if (pl->phydev) |
| 1876 | ret = phy_ethtool_set_eee(pl->phydev, eee); |
| 1877 | |
| 1878 | return ret; |
| 1879 | } |
| 1880 | EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee); |
| 1881 | |
| 1882 | /* This emulates MII registers for a fixed-mode phy operating as per the |
| 1883 | * passed in state. "aneg" defines if we report negotiation is possible. |
| 1884 | * |
| 1885 | * FIXME: should deal with negotiation state too. |
| 1886 | */ |
Russell King | 7fdc455 | 2019-05-28 10:57:18 +0100 | [diff] [blame] | 1887 | static int phylink_mii_emul_read(unsigned int reg, |
| 1888 | struct phylink_link_state *state) |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1889 | { |
| 1890 | struct fixed_phy_status fs; |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 1891 | unsigned long *lpa = state->lp_advertising; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1892 | int val; |
| 1893 | |
| 1894 | fs.link = state->link; |
| 1895 | fs.speed = state->speed; |
| 1896 | fs.duplex = state->duplex; |
Russell King | 4e5aeb4 | 2020-02-15 15:49:53 +0000 | [diff] [blame] | 1897 | fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa); |
| 1898 | fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1899 | |
| 1900 | val = swphy_read_reg(reg, &fs); |
| 1901 | if (reg == MII_BMSR) { |
| 1902 | if (!state->an_complete) |
| 1903 | val &= ~BMSR_ANEGCOMPLETE; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1904 | } |
| 1905 | return val; |
| 1906 | } |
| 1907 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1908 | static int phylink_phy_read(struct phylink *pl, unsigned int phy_id, |
| 1909 | unsigned int reg) |
| 1910 | { |
| 1911 | struct phy_device *phydev = pl->phydev; |
| 1912 | int prtad, devad; |
| 1913 | |
| 1914 | if (mdio_phy_id_is_c45(phy_id)) { |
| 1915 | prtad = mdio_phy_id_prtad(phy_id); |
| 1916 | devad = mdio_phy_id_devad(phy_id); |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 1917 | devad = mdiobus_c45_addr(devad, reg); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1918 | } else if (phydev->is_c45) { |
| 1919 | switch (reg) { |
| 1920 | case MII_BMCR: |
| 1921 | case MII_BMSR: |
| 1922 | case MII_PHYSID1: |
| 1923 | case MII_PHYSID2: |
Russell King | 320ed3b | 2020-06-18 14:46:08 +0100 | [diff] [blame] | 1924 | devad = __ffs(phydev->c45_ids.mmds_present); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1925 | break; |
| 1926 | case MII_ADVERTISE: |
| 1927 | case MII_LPA: |
Russell King | 320ed3b | 2020-06-18 14:46:08 +0100 | [diff] [blame] | 1928 | if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN)) |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1929 | return -EINVAL; |
| 1930 | devad = MDIO_MMD_AN; |
| 1931 | if (reg == MII_ADVERTISE) |
| 1932 | reg = MDIO_AN_ADVERTISE; |
| 1933 | else |
| 1934 | reg = MDIO_AN_LPA; |
| 1935 | break; |
| 1936 | default: |
| 1937 | return -EINVAL; |
| 1938 | } |
| 1939 | prtad = phy_id; |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 1940 | devad = mdiobus_c45_addr(devad, reg); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1941 | } else { |
| 1942 | prtad = phy_id; |
| 1943 | devad = reg; |
| 1944 | } |
| 1945 | return mdiobus_read(pl->phydev->mdio.bus, prtad, devad); |
| 1946 | } |
| 1947 | |
| 1948 | static int phylink_phy_write(struct phylink *pl, unsigned int phy_id, |
| 1949 | unsigned int reg, unsigned int val) |
| 1950 | { |
| 1951 | struct phy_device *phydev = pl->phydev; |
| 1952 | int prtad, devad; |
| 1953 | |
| 1954 | if (mdio_phy_id_is_c45(phy_id)) { |
| 1955 | prtad = mdio_phy_id_prtad(phy_id); |
| 1956 | devad = mdio_phy_id_devad(phy_id); |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 1957 | devad = mdiobus_c45_addr(devad, reg); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1958 | } else if (phydev->is_c45) { |
| 1959 | switch (reg) { |
| 1960 | case MII_BMCR: |
| 1961 | case MII_BMSR: |
| 1962 | case MII_PHYSID1: |
| 1963 | case MII_PHYSID2: |
Russell King | 320ed3b | 2020-06-18 14:46:08 +0100 | [diff] [blame] | 1964 | devad = __ffs(phydev->c45_ids.mmds_present); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1965 | break; |
| 1966 | case MII_ADVERTISE: |
| 1967 | case MII_LPA: |
Russell King | 320ed3b | 2020-06-18 14:46:08 +0100 | [diff] [blame] | 1968 | if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN)) |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1969 | return -EINVAL; |
| 1970 | devad = MDIO_MMD_AN; |
| 1971 | if (reg == MII_ADVERTISE) |
| 1972 | reg = MDIO_AN_ADVERTISE; |
| 1973 | else |
| 1974 | reg = MDIO_AN_LPA; |
| 1975 | break; |
| 1976 | default: |
| 1977 | return -EINVAL; |
| 1978 | } |
| 1979 | prtad = phy_id; |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 1980 | devad = mdiobus_c45_addr(devad, reg); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 1981 | } else { |
| 1982 | prtad = phy_id; |
| 1983 | devad = reg; |
| 1984 | } |
| 1985 | |
| 1986 | return mdiobus_write(phydev->mdio.bus, prtad, devad, val); |
| 1987 | } |
| 1988 | |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1989 | static int phylink_mii_read(struct phylink *pl, unsigned int phy_id, |
| 1990 | unsigned int reg) |
| 1991 | { |
| 1992 | struct phylink_link_state state; |
| 1993 | int val = 0xffff; |
| 1994 | |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 1995 | switch (pl->cur_link_an_mode) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1996 | case MLO_AN_FIXED: |
| 1997 | if (phy_id == 0) { |
| 1998 | phylink_get_fixed_state(pl, &state); |
Russell King | 7fdc455 | 2019-05-28 10:57:18 +0100 | [diff] [blame] | 1999 | val = phylink_mii_emul_read(reg, &state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2000 | } |
| 2001 | break; |
| 2002 | |
| 2003 | case MLO_AN_PHY: |
| 2004 | return -EOPNOTSUPP; |
| 2005 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 2006 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2007 | if (phy_id == 0) { |
Russell King | d46b7e4 | 2019-11-21 00:36:22 +0000 | [diff] [blame] | 2008 | phylink_mac_pcs_get_state(pl, &state); |
Russell King | 7fdc455 | 2019-05-28 10:57:18 +0100 | [diff] [blame] | 2009 | val = phylink_mii_emul_read(reg, &state); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2010 | } |
| 2011 | break; |
| 2012 | } |
| 2013 | |
| 2014 | return val & 0xffff; |
| 2015 | } |
| 2016 | |
| 2017 | static int phylink_mii_write(struct phylink *pl, unsigned int phy_id, |
| 2018 | unsigned int reg, unsigned int val) |
| 2019 | { |
Russell King | 24cf0e6 | 2019-12-11 10:56:35 +0000 | [diff] [blame] | 2020 | switch (pl->cur_link_an_mode) { |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2021 | case MLO_AN_FIXED: |
| 2022 | break; |
| 2023 | |
| 2024 | case MLO_AN_PHY: |
| 2025 | return -EOPNOTSUPP; |
| 2026 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 2027 | case MLO_AN_INBAND: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2028 | break; |
| 2029 | } |
| 2030 | |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
Russell King | 8796c89 | 2017-12-01 10:24:47 +0000 | [diff] [blame] | 2034 | /** |
| 2035 | * phylink_mii_ioctl() - generic mii ioctl interface |
| 2036 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 2037 | * @ifr: a pointer to a &struct ifreq for socket ioctls |
| 2038 | * @cmd: ioctl cmd to execute |
| 2039 | * |
| 2040 | * Perform the specified MII ioctl on the PHY attached to the phylink instance |
| 2041 | * specified by @pl. If no PHY is attached, emulate the presence of the PHY. |
| 2042 | * |
| 2043 | * Returns: zero on success or negative error code. |
| 2044 | * |
| 2045 | * %SIOCGMIIPHY: |
| 2046 | * read register from the current PHY. |
| 2047 | * %SIOCGMIIREG: |
| 2048 | * read register from the specified PHY. |
| 2049 | * %SIOCSMIIREG: |
| 2050 | * set a register on the specified PHY. |
| 2051 | */ |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2052 | int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd) |
| 2053 | { |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 2054 | struct mii_ioctl_data *mii = if_mii(ifr); |
| 2055 | int ret; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2056 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 2057 | ASSERT_RTNL(); |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2058 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 2059 | if (pl->phydev) { |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 2060 | /* PHYs only exist for MLO_AN_PHY and SGMII */ |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 2061 | switch (cmd) { |
| 2062 | case SIOCGMIIPHY: |
| 2063 | mii->phy_id = pl->phydev->mdio.addr; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2064 | fallthrough; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2065 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 2066 | case SIOCGMIIREG: |
| 2067 | ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num); |
| 2068 | if (ret >= 0) { |
| 2069 | mii->val_out = ret; |
| 2070 | ret = 0; |
| 2071 | } |
| 2072 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2073 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 2074 | case SIOCSMIIREG: |
| 2075 | ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num, |
| 2076 | mii->val_in); |
| 2077 | break; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2078 | |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 2079 | default: |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2080 | ret = phy_mii_ioctl(pl->phydev, ifr, cmd); |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 2081 | break; |
| 2082 | } |
| 2083 | } else { |
| 2084 | switch (cmd) { |
| 2085 | case SIOCGMIIPHY: |
| 2086 | mii->phy_id = 0; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2087 | fallthrough; |
Russell King | ecbd87b | 2017-07-25 15:03:28 +0100 | [diff] [blame] | 2088 | |
| 2089 | case SIOCGMIIREG: |
| 2090 | ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num); |
| 2091 | if (ret >= 0) { |
| 2092 | mii->val_out = ret; |
| 2093 | ret = 0; |
| 2094 | } |
| 2095 | break; |
| 2096 | |
| 2097 | case SIOCSMIIREG: |
| 2098 | ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num, |
| 2099 | mii->val_in); |
| 2100 | break; |
| 2101 | |
| 2102 | default: |
| 2103 | ret = -EOPNOTSUPP; |
| 2104 | break; |
| 2105 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | return ret; |
| 2109 | } |
| 2110 | EXPORT_SYMBOL_GPL(phylink_mii_ioctl); |
| 2111 | |
Russell King | c6d5d84 | 2020-06-24 11:06:54 +0100 | [diff] [blame] | 2112 | /** |
| 2113 | * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both |
| 2114 | * link partners |
| 2115 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 2116 | * @sync: perform action synchronously |
| 2117 | * |
| 2118 | * If we have a PHY that is not part of a SFP module, then set the speed |
| 2119 | * as described in the phy_speed_down() function. Please see this function |
| 2120 | * for a description of the @sync parameter. |
| 2121 | * |
| 2122 | * Returns zero if there is no PHY, otherwise as per phy_speed_down(). |
| 2123 | */ |
| 2124 | int phylink_speed_down(struct phylink *pl, bool sync) |
| 2125 | { |
| 2126 | int ret = 0; |
| 2127 | |
| 2128 | ASSERT_RTNL(); |
| 2129 | |
| 2130 | if (!pl->sfp_bus && pl->phydev) |
| 2131 | ret = phy_speed_down(pl->phydev, sync); |
| 2132 | |
| 2133 | return ret; |
| 2134 | } |
| 2135 | EXPORT_SYMBOL_GPL(phylink_speed_down); |
| 2136 | |
| 2137 | /** |
| 2138 | * phylink_speed_up() - restore the advertised speeds prior to the call to |
| 2139 | * phylink_speed_down() |
| 2140 | * @pl: a pointer to a &struct phylink returned from phylink_create() |
| 2141 | * |
| 2142 | * If we have a PHY that is not part of a SFP module, then restore the |
| 2143 | * PHY speeds as per phy_speed_up(). |
| 2144 | * |
| 2145 | * Returns zero if there is no PHY, otherwise as per phy_speed_up(). |
| 2146 | */ |
| 2147 | int phylink_speed_up(struct phylink *pl) |
| 2148 | { |
| 2149 | int ret = 0; |
| 2150 | |
| 2151 | ASSERT_RTNL(); |
| 2152 | |
| 2153 | if (!pl->sfp_bus && pl->phydev) |
| 2154 | ret = phy_speed_up(pl->phydev); |
| 2155 | |
| 2156 | return ret; |
| 2157 | } |
| 2158 | EXPORT_SYMBOL_GPL(phylink_speed_up); |
| 2159 | |
Russell King | 320587e6 | 2019-05-28 10:57:34 +0100 | [diff] [blame] | 2160 | static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus) |
| 2161 | { |
| 2162 | struct phylink *pl = upstream; |
| 2163 | |
| 2164 | pl->netdev->sfp_bus = bus; |
| 2165 | } |
| 2166 | |
| 2167 | static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus) |
| 2168 | { |
| 2169 | struct phylink *pl = upstream; |
| 2170 | |
| 2171 | pl->netdev->sfp_bus = NULL; |
| 2172 | } |
| 2173 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2174 | static int phylink_sfp_config(struct phylink *pl, u8 mode, |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2175 | const unsigned long *supported, |
| 2176 | const unsigned long *advertising) |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2177 | { |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 2178 | __ETHTOOL_DECLARE_LINK_MODE_MASK(support1); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2179 | __ETHTOOL_DECLARE_LINK_MODE_MASK(support); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2180 | struct phylink_link_state config; |
| 2181 | phy_interface_t iface; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2182 | bool changed; |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2183 | int ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2184 | |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2185 | linkmode_copy(support, supported); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2186 | |
| 2187 | memset(&config, 0, sizeof(config)); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2188 | linkmode_copy(config.advertising, advertising); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 2189 | config.interface = PHY_INTERFACE_MODE_NA; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2190 | config.speed = SPEED_UNKNOWN; |
| 2191 | config.duplex = DUPLEX_UNKNOWN; |
| 2192 | config.pause = MLO_PAUSE_AN; |
| 2193 | config.an_enabled = pl->link_config.an_enabled; |
| 2194 | |
| 2195 | /* Ignore errors if we're expecting a PHY to attach later */ |
| 2196 | ret = phylink_validate(pl, support, &config); |
| 2197 | if (ret) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 2198 | phylink_err(pl, "validation with support %*pb failed: %d\n", |
| 2199 | __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 2200 | return ret; |
| 2201 | } |
| 2202 | |
Russell King | a4516c7 | 2019-12-11 10:55:59 +0000 | [diff] [blame] | 2203 | iface = sfp_select_interface(pl->sfp_bus, config.advertising); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 2204 | if (iface == PHY_INTERFACE_MODE_NA) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 2205 | phylink_err(pl, |
| 2206 | "selection of interface failed, advertisement %*pb\n", |
| 2207 | __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 2208 | return -EINVAL; |
| 2209 | } |
| 2210 | |
| 2211 | config.interface = iface; |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2212 | linkmode_copy(support1, support); |
Russell King | 7731676 | 2019-06-02 15:12:54 +0100 | [diff] [blame] | 2213 | ret = phylink_validate(pl, support1, &config); |
Russell King | a9c7936 | 2018-02-27 15:53:02 +0000 | [diff] [blame] | 2214 | if (ret) { |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 2215 | phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n", |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2216 | phylink_an_mode_str(mode), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 2217 | phy_modes(config.interface), |
| 2218 | __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2219 | return ret; |
| 2220 | } |
| 2221 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 2222 | phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n", |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2223 | phylink_an_mode_str(mode), phy_modes(config.interface), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 2224 | __ETHTOOL_LINK_MODE_MASK_NBITS, support); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2225 | |
Russell King | 86a362c | 2017-12-01 10:24:26 +0000 | [diff] [blame] | 2226 | if (phy_interface_mode_is_8023z(iface) && pl->phydev) |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2227 | return -EINVAL; |
| 2228 | |
Nathan Rossi | ea269a6 | 2021-09-02 05:14:49 +0000 | [diff] [blame] | 2229 | changed = !linkmode_equal(pl->supported, support) || |
| 2230 | !linkmode_equal(pl->link_config.advertising, |
| 2231 | config.advertising); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2232 | if (changed) { |
| 2233 | linkmode_copy(pl->supported, support); |
| 2234 | linkmode_copy(pl->link_config.advertising, config.advertising); |
| 2235 | } |
| 2236 | |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2237 | if (pl->cur_link_an_mode != mode || |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2238 | pl->link_config.interface != config.interface) { |
| 2239 | pl->link_config.interface = config.interface; |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2240 | pl->cur_link_an_mode = mode; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2241 | |
| 2242 | changed = true; |
| 2243 | |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 2244 | phylink_info(pl, "switched to %s/%s link mode\n", |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2245 | phylink_an_mode_str(mode), |
Ioana Ciornei | 17091180 | 2019-05-28 20:38:14 +0300 | [diff] [blame] | 2246 | phy_modes(config.interface)); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2247 | } |
| 2248 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2249 | pl->link_port = pl->sfp_port; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2250 | |
| 2251 | if (changed && !test_bit(PHYLINK_DISABLE_STOPPED, |
| 2252 | &pl->phylink_disable_state)) |
Russell King | 4c0d6d3 | 2020-03-30 18:44:55 +0100 | [diff] [blame] | 2253 | phylink_mac_initial_config(pl, false); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2254 | |
| 2255 | return ret; |
| 2256 | } |
| 2257 | |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2258 | static int phylink_sfp_module_insert(void *upstream, |
| 2259 | const struct sfp_eeprom_id *id) |
| 2260 | { |
| 2261 | struct phylink *pl = upstream; |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2262 | unsigned long *support = pl->sfp_support; |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2263 | |
| 2264 | ASSERT_RTNL(); |
| 2265 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2266 | linkmode_zero(support); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2267 | sfp_parse_support(pl->sfp_bus, id, support); |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2268 | pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2269 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2270 | /* If this module may have a PHY connecting later, defer until later */ |
| 2271 | pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id); |
| 2272 | if (pl->sfp_may_have_phy) |
| 2273 | return 0; |
| 2274 | |
| 2275 | return phylink_sfp_config(pl, MLO_AN_INBAND, support, support); |
Russell King | c0de2f4 | 2019-12-11 10:56:40 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2278 | static int phylink_sfp_module_start(void *upstream) |
| 2279 | { |
| 2280 | struct phylink *pl = upstream; |
| 2281 | |
| 2282 | /* If this SFP module has a PHY, start the PHY now. */ |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2283 | if (pl->phydev) { |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2284 | phy_start(pl->phydev); |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2285 | return 0; |
| 2286 | } |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2287 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2288 | /* If the module may have a PHY but we didn't detect one we |
| 2289 | * need to configure the MAC here. |
| 2290 | */ |
| 2291 | if (!pl->sfp_may_have_phy) |
| 2292 | return 0; |
| 2293 | |
| 2294 | return phylink_sfp_config(pl, MLO_AN_INBAND, |
| 2295 | pl->sfp_support, pl->sfp_support); |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | static void phylink_sfp_module_stop(void *upstream) |
| 2299 | { |
| 2300 | struct phylink *pl = upstream; |
| 2301 | |
| 2302 | /* If this SFP module has a PHY, stop it. */ |
| 2303 | if (pl->phydev) |
| 2304 | phy_stop(pl->phydev); |
| 2305 | } |
| 2306 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2307 | static void phylink_sfp_link_down(void *upstream) |
| 2308 | { |
| 2309 | struct phylink *pl = upstream; |
| 2310 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 2311 | ASSERT_RTNL(); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2312 | |
Russell King | 87454b6 | 2019-02-11 15:04:24 +0000 | [diff] [blame] | 2313 | phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | static void phylink_sfp_link_up(void *upstream) |
| 2317 | { |
| 2318 | struct phylink *pl = upstream; |
| 2319 | |
Russell King | 8b87451 | 2017-12-15 16:09:47 +0000 | [diff] [blame] | 2320 | ASSERT_RTNL(); |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2321 | |
| 2322 | clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state); |
| 2323 | phylink_run_resolve(pl); |
| 2324 | } |
| 2325 | |
Russell King | 7adb5b2 | 2019-12-11 10:56:50 +0000 | [diff] [blame] | 2326 | /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII |
| 2327 | * or 802.3z control word, so inband will not work. |
| 2328 | */ |
| 2329 | static bool phylink_phy_no_inband(struct phy_device *phy) |
| 2330 | { |
| 2331 | return phy->is_c45 && |
| 2332 | (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150; |
| 2333 | } |
| 2334 | |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2335 | static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy) |
| 2336 | { |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 2337 | struct phylink *pl = upstream; |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2338 | phy_interface_t interface; |
Russell King | 7adb5b2 | 2019-12-11 10:56:50 +0000 | [diff] [blame] | 2339 | u8 mode; |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 2340 | int ret; |
Baruch Siach | 7e41837 | 2018-10-03 19:04:49 +0300 | [diff] [blame] | 2341 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2342 | /* |
| 2343 | * This is the new way of dealing with flow control for PHYs, |
| 2344 | * as described by Timur Tabi in commit 529ed1275263 ("net: phy: |
| 2345 | * phy drivers should not set SUPPORTED_[Asym_]Pause") except |
| 2346 | * using our validate call to the MAC, we rely upon the MAC |
| 2347 | * clearing the bits from both supported and advertising fields. |
| 2348 | */ |
| 2349 | phy_support_asym_pause(phy); |
| 2350 | |
Russell King | 7adb5b2 | 2019-12-11 10:56:50 +0000 | [diff] [blame] | 2351 | if (phylink_phy_no_inband(phy)) |
| 2352 | mode = MLO_AN_PHY; |
| 2353 | else |
| 2354 | mode = MLO_AN_INBAND; |
| 2355 | |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2356 | /* Do the initial configuration */ |
Russell King | 7adb5b2 | 2019-12-11 10:56:50 +0000 | [diff] [blame] | 2357 | ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising); |
Russell King | 52c9560 | 2019-12-11 10:56:45 +0000 | [diff] [blame] | 2358 | if (ret < 0) |
| 2359 | return ret; |
| 2360 | |
| 2361 | interface = pl->link_config.interface; |
| 2362 | ret = phylink_attach_phy(pl, phy, interface); |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 2363 | if (ret < 0) |
| 2364 | return ret; |
| 2365 | |
Russell King | e45d1f5 | 2019-12-11 10:56:30 +0000 | [diff] [blame] | 2366 | ret = phylink_bringup_phy(pl, phy, interface); |
Russell King | 938d44c | 2019-12-11 10:56:25 +0000 | [diff] [blame] | 2367 | if (ret) |
| 2368 | phy_detach(phy); |
| 2369 | |
| 2370 | return ret; |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | static void phylink_sfp_disconnect_phy(void *upstream) |
| 2374 | { |
| 2375 | phylink_disconnect_phy(upstream); |
| 2376 | } |
| 2377 | |
| 2378 | static const struct sfp_upstream_ops sfp_phylink_ops = { |
Russell King | 320587e6 | 2019-05-28 10:57:34 +0100 | [diff] [blame] | 2379 | .attach = phylink_sfp_attach, |
| 2380 | .detach = phylink_sfp_detach, |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2381 | .module_insert = phylink_sfp_module_insert, |
Russell King | 4882057 | 2019-12-11 10:56:14 +0000 | [diff] [blame] | 2382 | .module_start = phylink_sfp_module_start, |
| 2383 | .module_stop = phylink_sfp_module_stop, |
Russell King | ce0aa27 | 2017-07-25 15:03:18 +0100 | [diff] [blame] | 2384 | .link_up = phylink_sfp_link_up, |
| 2385 | .link_down = phylink_sfp_link_down, |
| 2386 | .connect_phy = phylink_sfp_connect_phy, |
| 2387 | .disconnect_phy = phylink_sfp_disconnect_phy, |
| 2388 | }; |
| 2389 | |
Russell King | 624c0f0 | 2018-08-09 15:38:38 +0200 | [diff] [blame] | 2390 | /* Helpers for MAC drivers */ |
| 2391 | |
| 2392 | /** |
| 2393 | * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper |
| 2394 | * @state: a pointer to a &struct phylink_link_state |
| 2395 | * |
| 2396 | * Inspect the interface mode, advertising mask or forced speed and |
| 2397 | * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching |
| 2398 | * the interface mode to suit. @state->interface is appropriately |
| 2399 | * updated, and the advertising mask has the "other" baseX_Full flag |
| 2400 | * cleared. |
| 2401 | */ |
| 2402 | void phylink_helper_basex_speed(struct phylink_link_state *state) |
| 2403 | { |
| 2404 | if (phy_interface_mode_is_8023z(state->interface)) { |
| 2405 | bool want_2500 = state->an_enabled ? |
| 2406 | phylink_test(state->advertising, 2500baseX_Full) : |
| 2407 | state->speed == SPEED_2500; |
| 2408 | |
| 2409 | if (want_2500) { |
| 2410 | phylink_clear(state->advertising, 1000baseX_Full); |
| 2411 | state->interface = PHY_INTERFACE_MODE_2500BASEX; |
| 2412 | } else { |
| 2413 | phylink_clear(state->advertising, 2500baseX_Full); |
| 2414 | state->interface = PHY_INTERFACE_MODE_1000BASEX; |
| 2415 | } |
| 2416 | } |
| 2417 | } |
| 2418 | EXPORT_SYMBOL_GPL(phylink_helper_basex_speed); |
| 2419 | |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2420 | static void phylink_decode_c37_word(struct phylink_link_state *state, |
| 2421 | uint16_t config_reg, int speed) |
| 2422 | { |
| 2423 | bool tx_pause, rx_pause; |
| 2424 | int fd_bit; |
| 2425 | |
| 2426 | if (speed == SPEED_2500) |
| 2427 | fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT; |
| 2428 | else |
| 2429 | fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT; |
| 2430 | |
| 2431 | mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit); |
| 2432 | |
| 2433 | if (linkmode_test_bit(fd_bit, state->advertising) && |
| 2434 | linkmode_test_bit(fd_bit, state->lp_advertising)) { |
| 2435 | state->speed = speed; |
| 2436 | state->duplex = DUPLEX_FULL; |
| 2437 | } else { |
| 2438 | /* negotiation failure */ |
| 2439 | state->link = false; |
| 2440 | } |
| 2441 | |
| 2442 | linkmode_resolve_pause(state->advertising, state->lp_advertising, |
| 2443 | &tx_pause, &rx_pause); |
| 2444 | |
| 2445 | if (tx_pause) |
| 2446 | state->pause |= MLO_PAUSE_TX; |
| 2447 | if (rx_pause) |
| 2448 | state->pause |= MLO_PAUSE_RX; |
| 2449 | } |
| 2450 | |
| 2451 | static void phylink_decode_sgmii_word(struct phylink_link_state *state, |
| 2452 | uint16_t config_reg) |
| 2453 | { |
| 2454 | if (!(config_reg & LPA_SGMII_LINK)) { |
| 2455 | state->link = false; |
| 2456 | return; |
| 2457 | } |
| 2458 | |
| 2459 | switch (config_reg & LPA_SGMII_SPD_MASK) { |
| 2460 | case LPA_SGMII_10: |
| 2461 | state->speed = SPEED_10; |
| 2462 | break; |
| 2463 | case LPA_SGMII_100: |
| 2464 | state->speed = SPEED_100; |
| 2465 | break; |
| 2466 | case LPA_SGMII_1000: |
| 2467 | state->speed = SPEED_1000; |
| 2468 | break; |
| 2469 | default: |
| 2470 | state->link = false; |
| 2471 | return; |
| 2472 | } |
| 2473 | if (config_reg & LPA_SGMII_FULL_DUPLEX) |
| 2474 | state->duplex = DUPLEX_FULL; |
| 2475 | else |
| 2476 | state->duplex = DUPLEX_HALF; |
| 2477 | } |
| 2478 | |
| 2479 | /** |
Ioana Ciornei | afd6220 | 2020-08-30 11:33:58 +0300 | [diff] [blame] | 2480 | * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS |
| 2481 | * @state: a pointer to a struct phylink_link_state. |
| 2482 | * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word |
| 2483 | * |
| 2484 | * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation |
| 2485 | * code word. Decode the USXGMII code word and populate the corresponding fields |
| 2486 | * (speed, duplex) into the phylink_link_state structure. |
| 2487 | */ |
| 2488 | void phylink_decode_usxgmii_word(struct phylink_link_state *state, |
| 2489 | uint16_t lpa) |
| 2490 | { |
| 2491 | switch (lpa & MDIO_USXGMII_SPD_MASK) { |
| 2492 | case MDIO_USXGMII_10: |
| 2493 | state->speed = SPEED_10; |
| 2494 | break; |
| 2495 | case MDIO_USXGMII_100: |
| 2496 | state->speed = SPEED_100; |
| 2497 | break; |
| 2498 | case MDIO_USXGMII_1000: |
| 2499 | state->speed = SPEED_1000; |
| 2500 | break; |
| 2501 | case MDIO_USXGMII_2500: |
| 2502 | state->speed = SPEED_2500; |
| 2503 | break; |
| 2504 | case MDIO_USXGMII_5000: |
| 2505 | state->speed = SPEED_5000; |
| 2506 | break; |
| 2507 | case MDIO_USXGMII_10G: |
| 2508 | state->speed = SPEED_10000; |
| 2509 | break; |
| 2510 | default: |
| 2511 | state->link = false; |
| 2512 | return; |
| 2513 | } |
| 2514 | |
| 2515 | if (lpa & MDIO_USXGMII_FULL_DUPLEX) |
| 2516 | state->duplex = DUPLEX_FULL; |
| 2517 | else |
| 2518 | state->duplex = DUPLEX_HALF; |
| 2519 | } |
| 2520 | EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word); |
| 2521 | |
| 2522 | /** |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2523 | * phylink_mii_c22_pcs_get_state() - read the MAC PCS state |
| 2524 | * @pcs: a pointer to a &struct mdio_device. |
| 2525 | * @state: a pointer to a &struct phylink_link_state. |
| 2526 | * |
| 2527 | * Helper for MAC PCS supporting the 802.3 clause 22 register set for |
| 2528 | * clause 37 negotiation and/or SGMII control. |
| 2529 | * |
| 2530 | * Read the MAC PCS state from the MII device configured in @config and |
| 2531 | * parse the Clause 37 or Cisco SGMII link partner negotiation word into |
| 2532 | * the phylink @state structure. This is suitable to be directly plugged |
| 2533 | * into the mac_pcs_get_state() member of the struct phylink_mac_ops |
| 2534 | * structure. |
| 2535 | */ |
| 2536 | void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs, |
| 2537 | struct phylink_link_state *state) |
| 2538 | { |
| 2539 | struct mii_bus *bus = pcs->bus; |
| 2540 | int addr = pcs->addr; |
| 2541 | int bmsr, lpa; |
| 2542 | |
| 2543 | bmsr = mdiobus_read(bus, addr, MII_BMSR); |
| 2544 | lpa = mdiobus_read(bus, addr, MII_LPA); |
| 2545 | if (bmsr < 0 || lpa < 0) { |
| 2546 | state->link = false; |
| 2547 | return; |
| 2548 | } |
| 2549 | |
| 2550 | state->link = !!(bmsr & BMSR_LSTATUS); |
| 2551 | state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE); |
| 2552 | if (!state->link) |
| 2553 | return; |
| 2554 | |
| 2555 | switch (state->interface) { |
| 2556 | case PHY_INTERFACE_MODE_1000BASEX: |
| 2557 | phylink_decode_c37_word(state, lpa, SPEED_1000); |
| 2558 | break; |
| 2559 | |
| 2560 | case PHY_INTERFACE_MODE_2500BASEX: |
| 2561 | phylink_decode_c37_word(state, lpa, SPEED_2500); |
| 2562 | break; |
| 2563 | |
| 2564 | case PHY_INTERFACE_MODE_SGMII: |
Ioana Ciornei | 29f02ee | 2020-08-30 11:33:59 +0300 | [diff] [blame] | 2565 | case PHY_INTERFACE_MODE_QSGMII: |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2566 | phylink_decode_sgmii_word(state, lpa); |
| 2567 | break; |
| 2568 | |
| 2569 | default: |
| 2570 | state->link = false; |
| 2571 | break; |
| 2572 | } |
| 2573 | } |
| 2574 | EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state); |
| 2575 | |
| 2576 | /** |
| 2577 | * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS |
| 2578 | * advertisement |
| 2579 | * @pcs: a pointer to a &struct mdio_device. |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2580 | * @interface: the PHY interface mode being configured |
| 2581 | * @advertising: the ethtool advertisement mask |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2582 | * |
| 2583 | * Helper for MAC PCS supporting the 802.3 clause 22 register set for |
| 2584 | * clause 37 negotiation and/or SGMII control. |
| 2585 | * |
| 2586 | * Configure the clause 37 PCS advertisement as specified by @state. This |
| 2587 | * does not trigger a renegotiation; phylink will do that via the |
| 2588 | * mac_an_restart() method of the struct phylink_mac_ops structure. |
| 2589 | * |
| 2590 | * Returns negative error code on failure to configure the advertisement, |
| 2591 | * zero if no change has been made, or one if the advertisement has changed. |
| 2592 | */ |
| 2593 | int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs, |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2594 | phy_interface_t interface, |
| 2595 | const unsigned long *advertising) |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2596 | { |
| 2597 | struct mii_bus *bus = pcs->bus; |
| 2598 | int addr = pcs->addr; |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2599 | u16 adv; |
| 2600 | |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2601 | switch (interface) { |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2602 | case PHY_INTERFACE_MODE_1000BASEX: |
| 2603 | case PHY_INTERFACE_MODE_2500BASEX: |
| 2604 | adv = ADVERTISE_1000XFULL; |
| 2605 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2606 | advertising)) |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2607 | adv |= ADVERTISE_1000XPAUSE; |
| 2608 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
Russell King | 0bd2740 | 2020-03-30 18:44:44 +0100 | [diff] [blame] | 2609 | advertising)) |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2610 | adv |= ADVERTISE_1000XPSE_ASYM; |
| 2611 | |
Russell King (Oracle) | 078e0b5 | 2021-10-06 13:19:25 +0100 | [diff] [blame] | 2612 | return mdiobus_modify_changed(bus, addr, MII_ADVERTISE, |
| 2613 | 0xffff, adv); |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2614 | |
| 2615 | case PHY_INTERFACE_MODE_SGMII: |
Russell King (Oracle) | 078e0b5 | 2021-10-06 13:19:25 +0100 | [diff] [blame] | 2616 | return mdiobus_modify_changed(bus, addr, MII_ADVERTISE, |
| 2617 | 0xffff, 0x0001); |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2618 | |
| 2619 | default: |
| 2620 | /* Nothing to do for other modes */ |
| 2621 | return 0; |
| 2622 | } |
| 2623 | } |
| 2624 | EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement); |
| 2625 | |
| 2626 | /** |
Russell King | 93eaceb | 2020-07-21 12:04:51 +0100 | [diff] [blame] | 2627 | * phylink_mii_c22_pcs_config() - configure clause 22 PCS |
| 2628 | * @pcs: a pointer to a &struct mdio_device. |
| 2629 | * @mode: link autonegotiation mode |
| 2630 | * @interface: the PHY interface mode being configured |
| 2631 | * @advertising: the ethtool advertisement mask |
| 2632 | * |
| 2633 | * Configure a Clause 22 PCS PHY with the appropriate negotiation |
| 2634 | * parameters for the @mode, @interface and @advertising parameters. |
| 2635 | * Returns negative error number on failure, zero if the advertisement |
| 2636 | * has not changed, or positive if there is a change. |
| 2637 | */ |
| 2638 | int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode, |
| 2639 | phy_interface_t interface, |
| 2640 | const unsigned long *advertising) |
| 2641 | { |
| 2642 | bool changed; |
| 2643 | u16 bmcr; |
| 2644 | int ret; |
| 2645 | |
| 2646 | ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface, |
| 2647 | advertising); |
| 2648 | if (ret < 0) |
| 2649 | return ret; |
| 2650 | |
| 2651 | changed = ret > 0; |
| 2652 | |
Robert Hancock | cd29296 | 2020-10-26 11:58:02 -0600 | [diff] [blame] | 2653 | /* Ensure ISOLATE bit is disabled */ |
Russell King | 93eaceb | 2020-07-21 12:04:51 +0100 | [diff] [blame] | 2654 | bmcr = mode == MLO_AN_INBAND ? BMCR_ANENABLE : 0; |
| 2655 | ret = mdiobus_modify(pcs->bus, pcs->addr, MII_BMCR, |
Robert Hancock | cd29296 | 2020-10-26 11:58:02 -0600 | [diff] [blame] | 2656 | BMCR_ANENABLE | BMCR_ISOLATE, bmcr); |
Russell King | 93eaceb | 2020-07-21 12:04:51 +0100 | [diff] [blame] | 2657 | if (ret < 0) |
| 2658 | return ret; |
| 2659 | |
| 2660 | return changed ? 1 : 0; |
| 2661 | } |
| 2662 | EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config); |
| 2663 | |
| 2664 | /** |
Russell King | 74db1c1 | 2020-03-17 14:52:36 +0000 | [diff] [blame] | 2665 | * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation |
| 2666 | * @pcs: a pointer to a &struct mdio_device. |
| 2667 | * |
| 2668 | * Helper for MAC PCS supporting the 802.3 clause 22 register set for |
| 2669 | * clause 37 negotiation. |
| 2670 | * |
| 2671 | * Restart the clause 37 negotiation with the link partner. This is |
| 2672 | * suitable to be directly plugged into the mac_pcs_get_state() member |
| 2673 | * of the struct phylink_mac_ops structure. |
| 2674 | */ |
| 2675 | void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs) |
| 2676 | { |
| 2677 | struct mii_bus *bus = pcs->bus; |
| 2678 | int val, addr = pcs->addr; |
| 2679 | |
| 2680 | val = mdiobus_read(bus, addr, MII_BMCR); |
| 2681 | if (val >= 0) { |
| 2682 | val |= BMCR_ANRESTART; |
| 2683 | |
| 2684 | mdiobus_write(bus, addr, MII_BMCR, val); |
| 2685 | } |
| 2686 | } |
| 2687 | EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart); |
| 2688 | |
Russell King | b8679ef | 2020-03-17 14:52:41 +0000 | [diff] [blame] | 2689 | void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs, |
| 2690 | struct phylink_link_state *state) |
| 2691 | { |
| 2692 | struct mii_bus *bus = pcs->bus; |
| 2693 | int addr = pcs->addr; |
| 2694 | int stat; |
| 2695 | |
Russell King | 90ce665 | 2020-05-26 16:29:36 +0100 | [diff] [blame] | 2696 | stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1); |
Russell King | b8679ef | 2020-03-17 14:52:41 +0000 | [diff] [blame] | 2697 | if (stat < 0) { |
| 2698 | state->link = false; |
| 2699 | return; |
| 2700 | } |
| 2701 | |
| 2702 | state->link = !!(stat & MDIO_STAT1_LSTATUS); |
| 2703 | if (!state->link) |
| 2704 | return; |
| 2705 | |
| 2706 | switch (state->interface) { |
| 2707 | case PHY_INTERFACE_MODE_10GBASER: |
| 2708 | state->speed = SPEED_10000; |
| 2709 | state->duplex = DUPLEX_FULL; |
| 2710 | break; |
| 2711 | |
| 2712 | default: |
| 2713 | break; |
| 2714 | } |
| 2715 | } |
| 2716 | EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state); |
| 2717 | |
Andrew Lunn | 5f85757 | 2019-01-21 19:10:19 +0100 | [diff] [blame] | 2718 | MODULE_LICENSE("GPL v2"); |