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