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