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