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 "phy-caps.h" 24 #include "sfp.h" 25 #include "swphy.h" 26 27 #define SUPPORTED_INTERFACES \ 28 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \ 29 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane) 30 #define ADVERTISED_INTERFACES \ 31 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \ 32 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane) 33 34 enum { 35 PHYLINK_DISABLE_STOPPED, 36 PHYLINK_DISABLE_LINK, 37 PHYLINK_DISABLE_MAC_WOL, 38 39 PCS_STATE_DOWN = 0, 40 PCS_STATE_STARTING, 41 PCS_STATE_STARTED, 42 }; 43 44 /** 45 * struct phylink - internal data type for phylink 46 */ 47 struct phylink { 48 /* private: */ 49 struct net_device *netdev; 50 const struct phylink_mac_ops *mac_ops; 51 struct phylink_config *config; 52 struct phylink_pcs *pcs; 53 struct device *dev; 54 unsigned int old_link_state:1; 55 56 unsigned long phylink_disable_state; /* bitmask of disables */ 57 struct phy_device *phydev; 58 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */ 59 u8 cfg_link_an_mode; /* MLO_AN_xxx */ 60 u8 req_link_an_mode; /* Requested MLO_AN_xxx mode */ 61 u8 act_link_an_mode; /* Active MLO_AN_xxx mode */ 62 u8 link_port; /* The current non-phy ethtool port */ 63 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); 64 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported_lpi); 65 66 /* The link configuration settings */ 67 struct phylink_link_state link_config; 68 69 /* The current settings */ 70 phy_interface_t cur_interface; 71 72 struct gpio_desc *link_gpio; 73 unsigned int link_irq; 74 struct timer_list link_poll; 75 void (*get_fixed_state)(struct net_device *dev, 76 struct phylink_link_state *s); 77 78 struct mutex state_mutex; 79 struct phylink_link_state phy_state; 80 unsigned int phy_ib_mode; 81 struct work_struct resolve; 82 unsigned int pcs_neg_mode; 83 unsigned int pcs_state; 84 85 bool link_failed; 86 bool mac_supports_eee_ops; 87 bool mac_supports_eee; 88 bool phy_enable_tx_lpi; 89 bool mac_enable_tx_lpi; 90 bool mac_tx_clk_stop; 91 u32 mac_tx_lpi_timer; 92 93 struct sfp_bus *sfp_bus; 94 bool sfp_may_have_phy; 95 DECLARE_PHY_INTERFACE_MASK(sfp_interfaces); 96 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support); 97 u8 sfp_port; 98 99 struct eee_config eee_cfg; 100 }; 101 102 #define phylink_printk(level, pl, fmt, ...) \ 103 do { \ 104 if ((pl)->config->type == PHYLINK_NETDEV) \ 105 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \ 106 else if ((pl)->config->type == PHYLINK_DEV) \ 107 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \ 108 } while (0) 109 110 #define phylink_err(pl, fmt, ...) \ 111 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__) 112 #define phylink_warn(pl, fmt, ...) \ 113 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__) 114 #define phylink_info(pl, fmt, ...) \ 115 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__) 116 #if defined(CONFIG_DYNAMIC_DEBUG) 117 #define phylink_dbg(pl, fmt, ...) \ 118 do { \ 119 if ((pl)->config->type == PHYLINK_NETDEV) \ 120 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \ 121 else if ((pl)->config->type == PHYLINK_DEV) \ 122 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \ 123 } while (0) 124 #elif defined(DEBUG) 125 #define phylink_dbg(pl, fmt, ...) \ 126 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__) 127 #else 128 #define phylink_dbg(pl, fmt, ...) \ 129 ({ \ 130 if (0) \ 131 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \ 132 }) 133 #endif 134 135 static const phy_interface_t phylink_sfp_interface_preference[] = { 136 PHY_INTERFACE_MODE_25GBASER, 137 PHY_INTERFACE_MODE_USXGMII, 138 PHY_INTERFACE_MODE_10GBASER, 139 PHY_INTERFACE_MODE_5GBASER, 140 PHY_INTERFACE_MODE_2500BASEX, 141 PHY_INTERFACE_MODE_SGMII, 142 PHY_INTERFACE_MODE_1000BASEX, 143 PHY_INTERFACE_MODE_100BASEX, 144 }; 145 146 static DECLARE_PHY_INTERFACE_MASK(phylink_sfp_interfaces); 147 148 /** 149 * phylink_set_port_modes() - set the port type modes in the ethtool mask 150 * @mask: ethtool link mode mask 151 * 152 * Sets all the port type modes in the ethtool mask. MAC drivers should 153 * use this in their 'validate' callback. 154 */ 155 void phylink_set_port_modes(unsigned long *mask) 156 { 157 phylink_set(mask, TP); 158 phylink_set(mask, AUI); 159 phylink_set(mask, MII); 160 phylink_set(mask, FIBRE); 161 phylink_set(mask, BNC); 162 phylink_set(mask, Backplane); 163 } 164 EXPORT_SYMBOL_GPL(phylink_set_port_modes); 165 166 static int phylink_is_empty_linkmode(const unsigned long *linkmode) 167 { 168 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, }; 169 170 phylink_set_port_modes(tmp); 171 phylink_set(tmp, Autoneg); 172 phylink_set(tmp, Pause); 173 phylink_set(tmp, Asym_Pause); 174 175 return linkmode_subset(linkmode, tmp); 176 } 177 178 static const char *phylink_an_mode_str(unsigned int mode) 179 { 180 static const char *modestr[] = { 181 [MLO_AN_PHY] = "phy", 182 [MLO_AN_FIXED] = "fixed", 183 [MLO_AN_INBAND] = "inband", 184 }; 185 186 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown"; 187 } 188 189 static const char *phylink_pcs_mode_str(unsigned int mode) 190 { 191 if (!mode) 192 return "none"; 193 194 if (mode & PHYLINK_PCS_NEG_OUTBAND) 195 return "outband"; 196 197 if (mode & PHYLINK_PCS_NEG_INBAND) { 198 if (mode & PHYLINK_PCS_NEG_ENABLED) 199 return "inband,an-enabled"; 200 else 201 return "inband,an-disabled"; 202 } 203 204 return "unknown"; 205 } 206 207 static unsigned int phylink_interface_signal_rate(phy_interface_t interface) 208 { 209 switch (interface) { 210 case PHY_INTERFACE_MODE_SGMII: 211 case PHY_INTERFACE_MODE_1000BASEX: /* 1.25Mbd */ 212 return 1250; 213 case PHY_INTERFACE_MODE_2500BASEX: /* 3.125Mbd */ 214 return 3125; 215 case PHY_INTERFACE_MODE_5GBASER: /* 5.15625Mbd */ 216 return 5156; 217 case PHY_INTERFACE_MODE_10GBASER: /* 10.3125Mbd */ 218 return 10313; 219 default: 220 return 0; 221 } 222 } 223 224 /** 225 * phylink_interface_max_speed() - get the maximum speed of a phy interface 226 * @interface: phy interface mode defined by &typedef phy_interface_t 227 * 228 * Determine the maximum speed of a phy interface. This is intended to help 229 * determine the correct speed to pass to the MAC when the phy is performing 230 * rate matching. 231 * 232 * Return: The maximum speed of @interface 233 */ 234 static int phylink_interface_max_speed(phy_interface_t interface) 235 { 236 switch (interface) { 237 case PHY_INTERFACE_MODE_100BASEX: 238 case PHY_INTERFACE_MODE_REVRMII: 239 case PHY_INTERFACE_MODE_RMII: 240 case PHY_INTERFACE_MODE_SMII: 241 case PHY_INTERFACE_MODE_REVMII: 242 case PHY_INTERFACE_MODE_MII: 243 return SPEED_100; 244 245 case PHY_INTERFACE_MODE_TBI: 246 case PHY_INTERFACE_MODE_MOCA: 247 case PHY_INTERFACE_MODE_RTBI: 248 case PHY_INTERFACE_MODE_1000BASEX: 249 case PHY_INTERFACE_MODE_1000BASEKX: 250 case PHY_INTERFACE_MODE_TRGMII: 251 case PHY_INTERFACE_MODE_RGMII_TXID: 252 case PHY_INTERFACE_MODE_RGMII_RXID: 253 case PHY_INTERFACE_MODE_RGMII_ID: 254 case PHY_INTERFACE_MODE_RGMII: 255 case PHY_INTERFACE_MODE_PSGMII: 256 case PHY_INTERFACE_MODE_QSGMII: 257 case PHY_INTERFACE_MODE_QUSGMII: 258 case PHY_INTERFACE_MODE_SGMII: 259 case PHY_INTERFACE_MODE_GMII: 260 return SPEED_1000; 261 262 case PHY_INTERFACE_MODE_2500BASEX: 263 case PHY_INTERFACE_MODE_10G_QXGMII: 264 return SPEED_2500; 265 266 case PHY_INTERFACE_MODE_5GBASER: 267 return SPEED_5000; 268 269 case PHY_INTERFACE_MODE_XGMII: 270 case PHY_INTERFACE_MODE_RXAUI: 271 case PHY_INTERFACE_MODE_XAUI: 272 case PHY_INTERFACE_MODE_10GBASER: 273 case PHY_INTERFACE_MODE_10GKR: 274 case PHY_INTERFACE_MODE_USXGMII: 275 return SPEED_10000; 276 277 case PHY_INTERFACE_MODE_25GBASER: 278 return SPEED_25000; 279 280 case PHY_INTERFACE_MODE_XLGMII: 281 return SPEED_40000; 282 283 case PHY_INTERFACE_MODE_INTERNAL: 284 case PHY_INTERFACE_MODE_NA: 285 case PHY_INTERFACE_MODE_MAX: 286 /* No idea! Garbage in, unknown out */ 287 return SPEED_UNKNOWN; 288 } 289 290 /* If we get here, someone forgot to add an interface mode above */ 291 WARN_ON_ONCE(1); 292 return SPEED_UNKNOWN; 293 } 294 295 static struct { 296 unsigned long mask; 297 int speed; 298 unsigned int duplex; 299 unsigned int caps_bit; 300 } phylink_caps_params[] = { 301 { MAC_400000FD, SPEED_400000, DUPLEX_FULL, BIT(LINK_CAPA_400000FD) }, 302 { MAC_200000FD, SPEED_200000, DUPLEX_FULL, BIT(LINK_CAPA_200000FD) }, 303 { MAC_100000FD, SPEED_100000, DUPLEX_FULL, BIT(LINK_CAPA_100000FD) }, 304 { MAC_56000FD, SPEED_56000, DUPLEX_FULL, BIT(LINK_CAPA_56000FD) }, 305 { MAC_50000FD, SPEED_50000, DUPLEX_FULL, BIT(LINK_CAPA_50000FD) }, 306 { MAC_40000FD, SPEED_40000, DUPLEX_FULL, BIT(LINK_CAPA_40000FD) }, 307 { MAC_25000FD, SPEED_25000, DUPLEX_FULL, BIT(LINK_CAPA_25000FD) }, 308 { MAC_20000FD, SPEED_20000, DUPLEX_FULL, BIT(LINK_CAPA_20000FD) }, 309 { MAC_10000FD, SPEED_10000, DUPLEX_FULL, BIT(LINK_CAPA_10000FD) }, 310 { MAC_5000FD, SPEED_5000, DUPLEX_FULL, BIT(LINK_CAPA_5000FD) }, 311 { MAC_2500FD, SPEED_2500, DUPLEX_FULL, BIT(LINK_CAPA_2500FD) }, 312 { MAC_1000FD, SPEED_1000, DUPLEX_FULL, BIT(LINK_CAPA_1000FD) }, 313 { MAC_1000HD, SPEED_1000, DUPLEX_HALF, BIT(LINK_CAPA_1000HD) }, 314 { MAC_100FD, SPEED_100, DUPLEX_FULL, BIT(LINK_CAPA_100FD) }, 315 { MAC_100HD, SPEED_100, DUPLEX_HALF, BIT(LINK_CAPA_100HD) }, 316 { MAC_10FD, SPEED_10, DUPLEX_FULL, BIT(LINK_CAPA_10FD) }, 317 { MAC_10HD, SPEED_10, DUPLEX_HALF, BIT(LINK_CAPA_10HD) }, 318 }; 319 320 /** 321 * phylink_caps_to_link_caps() - Convert a set of MAC capabilities LINK caps 322 * @caps: A set of MAC capabilities 323 * 324 * Returns: The corresponding set of LINK_CAPA as defined in phy-caps.h 325 */ 326 static unsigned long phylink_caps_to_link_caps(unsigned long caps) 327 { 328 unsigned long link_caps = 0; 329 int i; 330 331 for (i = 0; i < ARRAY_SIZE(phylink_caps_params); i++) 332 if (caps & phylink_caps_params[i].mask) 333 link_caps |= phylink_caps_params[i].caps_bit; 334 335 return link_caps; 336 } 337 338 static unsigned long phylink_link_caps_to_mac_caps(unsigned long link_caps) 339 { 340 unsigned long caps = 0; 341 int i; 342 343 for (i = 0; i < ARRAY_SIZE(phylink_caps_params); i++) 344 if (link_caps & phylink_caps_params[i].caps_bit) 345 caps |= phylink_caps_params[i].mask; 346 347 return caps; 348 } 349 350 /** 351 * phylink_caps_to_linkmodes() - Convert capabilities to ethtool link modes 352 * @linkmodes: ethtool linkmode mask (must be already initialised) 353 * @caps: bitmask of MAC capabilities 354 * 355 * Set all possible pause, speed and duplex linkmodes in @linkmodes that are 356 * supported by the @caps. @linkmodes must have been initialised previously. 357 */ 358 static void phylink_caps_to_linkmodes(unsigned long *linkmodes, 359 unsigned long caps) 360 { 361 unsigned long link_caps = phylink_caps_to_link_caps(caps); 362 363 if (caps & MAC_SYM_PAUSE) 364 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, linkmodes); 365 366 if (caps & MAC_ASYM_PAUSE) 367 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, linkmodes); 368 369 phy_caps_linkmodes(link_caps, linkmodes); 370 } 371 372 /** 373 * phylink_limit_mac_speed - limit the phylink_config to a maximum speed 374 * @config: pointer to a &struct phylink_config 375 * @max_speed: maximum speed 376 * 377 * Mask off MAC capabilities for speeds higher than the @max_speed parameter. 378 * Any further motifications of config.mac_capabilities will override this. 379 */ 380 void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed) 381 { 382 int i; 383 384 for (i = 0; i < ARRAY_SIZE(phylink_caps_params) && 385 phylink_caps_params[i].speed > max_speed; i++) 386 config->mac_capabilities &= ~phylink_caps_params[i].mask; 387 } 388 EXPORT_SYMBOL_GPL(phylink_limit_mac_speed); 389 390 /** 391 * phylink_cap_from_speed_duplex - Get mac capability from speed/duplex 392 * @speed: the speed to search for 393 * @duplex: the duplex to search for 394 * 395 * Find the mac capability for a given speed and duplex. 396 * 397 * Return: A mask with the mac capability patching @speed and @duplex, or 0 if 398 * there were no matches. 399 */ 400 static unsigned long phylink_cap_from_speed_duplex(int speed, 401 unsigned int duplex) 402 { 403 int i; 404 405 for (i = 0; i < ARRAY_SIZE(phylink_caps_params); i++) { 406 if (speed == phylink_caps_params[i].speed && 407 duplex == phylink_caps_params[i].duplex) 408 return phylink_caps_params[i].mask; 409 } 410 411 return 0; 412 } 413 414 /** 415 * phylink_get_capabilities() - get capabilities for a given MAC 416 * @interface: phy interface mode defined by &typedef phy_interface_t 417 * @mac_capabilities: bitmask of MAC capabilities 418 * @rate_matching: type of rate matching being performed 419 * 420 * Get the MAC capabilities that are supported by the @interface mode and 421 * @mac_capabilities. 422 */ 423 static unsigned long phylink_get_capabilities(phy_interface_t interface, 424 unsigned long mac_capabilities, 425 int rate_matching) 426 { 427 unsigned long link_caps = phy_caps_from_interface(interface); 428 int max_speed = phylink_interface_max_speed(interface); 429 unsigned long caps = MAC_SYM_PAUSE | MAC_ASYM_PAUSE; 430 unsigned long matched_caps = 0; 431 432 caps |= phylink_link_caps_to_mac_caps(link_caps); 433 434 switch (rate_matching) { 435 case RATE_MATCH_OPEN_LOOP: 436 /* TODO */ 437 fallthrough; 438 case RATE_MATCH_NONE: 439 matched_caps = 0; 440 break; 441 case RATE_MATCH_PAUSE: { 442 /* The MAC must support asymmetric pause towards the local 443 * device for this. We could allow just symmetric pause, but 444 * then we might have to renegotiate if the link partner 445 * doesn't support pause. This is because there's no way to 446 * accept pause frames without transmitting them if we only 447 * support symmetric pause. 448 */ 449 if (!(mac_capabilities & MAC_SYM_PAUSE) || 450 !(mac_capabilities & MAC_ASYM_PAUSE)) 451 break; 452 453 /* We can't adapt if the MAC doesn't support the interface's 454 * max speed at full duplex. 455 */ 456 if (mac_capabilities & 457 phylink_cap_from_speed_duplex(max_speed, DUPLEX_FULL)) 458 matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD)); 459 break; 460 } 461 case RATE_MATCH_CRS: 462 /* The MAC must support half duplex at the interface's max 463 * speed. 464 */ 465 if (mac_capabilities & 466 phylink_cap_from_speed_duplex(max_speed, DUPLEX_HALF)) { 467 matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD)); 468 matched_caps &= mac_capabilities; 469 } 470 break; 471 } 472 473 return (caps & mac_capabilities) | matched_caps; 474 } 475 476 /** 477 * phylink_validate_mask_caps() - Restrict link modes based on caps 478 * @supported: ethtool bitmask for supported link modes. 479 * @state: pointer to a &struct phylink_link_state. 480 * @mac_capabilities: bitmask of MAC capabilities 481 * 482 * Calculate the supported link modes based on @mac_capabilities, and restrict 483 * @supported and @state based on that. Use this function if your capabiliies 484 * aren't constant, such as if they vary depending on the interface. 485 */ 486 static void phylink_validate_mask_caps(unsigned long *supported, 487 struct phylink_link_state *state, 488 unsigned long mac_capabilities) 489 { 490 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 491 unsigned long caps; 492 493 phylink_set_port_modes(mask); 494 phylink_set(mask, Autoneg); 495 caps = phylink_get_capabilities(state->interface, mac_capabilities, 496 state->rate_matching); 497 phylink_caps_to_linkmodes(mask, caps); 498 499 linkmode_and(supported, supported, mask); 500 linkmode_and(state->advertising, state->advertising, mask); 501 } 502 503 static int phylink_validate_mac_and_pcs(struct phylink *pl, 504 unsigned long *supported, 505 struct phylink_link_state *state) 506 { 507 struct phylink_pcs *pcs = NULL; 508 unsigned long capabilities; 509 int ret; 510 511 /* Get the PCS for this interface mode */ 512 if (pl->mac_ops->mac_select_pcs) { 513 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface); 514 if (IS_ERR(pcs)) 515 return PTR_ERR(pcs); 516 } 517 518 if (pcs) { 519 /* The PCS, if present, must be setup before phylink_create() 520 * has been called. If the ops is not initialised, print an 521 * error and backtrace rather than oopsing the kernel. 522 */ 523 if (!pcs->ops) { 524 phylink_err(pl, "interface %s: uninitialised PCS\n", 525 phy_modes(state->interface)); 526 dump_stack(); 527 return -EINVAL; 528 } 529 530 /* Ensure that this PCS supports the interface which the MAC 531 * returned it for. It is an error for the MAC to return a PCS 532 * that does not support the interface mode. 533 */ 534 if (!phy_interface_empty(pcs->supported_interfaces) && 535 !test_bit(state->interface, pcs->supported_interfaces)) { 536 phylink_err(pl, "MAC returned PCS which does not support %s\n", 537 phy_modes(state->interface)); 538 return -EINVAL; 539 } 540 541 /* Validate the link parameters with the PCS */ 542 if (pcs->ops->pcs_validate) { 543 ret = pcs->ops->pcs_validate(pcs, supported, state); 544 if (ret < 0 || phylink_is_empty_linkmode(supported)) 545 return -EINVAL; 546 547 /* Ensure the advertising mask is a subset of the 548 * supported mask. 549 */ 550 linkmode_and(state->advertising, state->advertising, 551 supported); 552 } 553 } 554 555 /* Then validate the link parameters with the MAC */ 556 if (pl->mac_ops->mac_get_caps) 557 capabilities = pl->mac_ops->mac_get_caps(pl->config, 558 state->interface); 559 else 560 capabilities = pl->config->mac_capabilities; 561 562 phylink_validate_mask_caps(supported, state, capabilities); 563 564 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0; 565 } 566 567 static void phylink_validate_one(struct phylink *pl, struct phy_device *phy, 568 const unsigned long *supported, 569 const struct phylink_link_state *state, 570 phy_interface_t interface, 571 unsigned long *accum_supported, 572 unsigned long *accum_advertising) 573 { 574 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp_supported); 575 struct phylink_link_state tmp_state; 576 577 linkmode_copy(tmp_supported, supported); 578 579 tmp_state = *state; 580 tmp_state.interface = interface; 581 582 if (phy) 583 tmp_state.rate_matching = phy_get_rate_matching(phy, interface); 584 585 if (!phylink_validate_mac_and_pcs(pl, tmp_supported, &tmp_state)) { 586 phylink_dbg(pl, " interface %u (%s) rate match %s supports %*pbl\n", 587 interface, phy_modes(interface), 588 phy_rate_matching_to_str(tmp_state.rate_matching), 589 __ETHTOOL_LINK_MODE_MASK_NBITS, tmp_supported); 590 591 linkmode_or(accum_supported, accum_supported, tmp_supported); 592 linkmode_or(accum_advertising, accum_advertising, 593 tmp_state.advertising); 594 } 595 } 596 597 static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy, 598 unsigned long *supported, 599 struct phylink_link_state *state, 600 const unsigned long *interfaces) 601 { 602 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, }; 603 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, }; 604 int interface; 605 606 for_each_set_bit(interface, interfaces, PHY_INTERFACE_MODE_MAX) 607 phylink_validate_one(pl, phy, supported, state, interface, 608 all_s, all_adv); 609 610 linkmode_copy(supported, all_s); 611 linkmode_copy(state->advertising, all_adv); 612 613 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0; 614 } 615 616 static int phylink_validate(struct phylink *pl, unsigned long *supported, 617 struct phylink_link_state *state) 618 { 619 const unsigned long *interfaces = pl->config->supported_interfaces; 620 621 if (state->interface == PHY_INTERFACE_MODE_NA) 622 return phylink_validate_mask(pl, NULL, supported, state, 623 interfaces); 624 625 if (!test_bit(state->interface, interfaces)) 626 return -EINVAL; 627 628 return phylink_validate_mac_and_pcs(pl, supported, state); 629 } 630 631 static void phylink_fill_fixedlink_supported(unsigned long *supported) 632 { 633 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, supported); 634 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, supported); 635 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, supported); 636 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, supported); 637 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, supported); 638 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, supported); 639 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, supported); 640 linkmode_set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, supported); 641 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, supported); 642 } 643 644 static int phylink_parse_fixedlink(struct phylink *pl, 645 const struct fwnode_handle *fwnode) 646 { 647 __ETHTOOL_DECLARE_LINK_MODE_MASK(match) = { 0, }; 648 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 649 const struct link_capabilities *c; 650 struct fwnode_handle *fixed_node; 651 struct gpio_desc *desc; 652 u32 speed; 653 int ret; 654 655 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link"); 656 if (fixed_node) { 657 ret = fwnode_property_read_u32(fixed_node, "speed", &speed); 658 659 pl->link_config.speed = speed; 660 pl->link_config.duplex = DUPLEX_HALF; 661 662 if (fwnode_property_read_bool(fixed_node, "full-duplex")) 663 pl->link_config.duplex = DUPLEX_FULL; 664 665 /* We treat the "pause" and "asym-pause" terminology as 666 * defining the link partner's ability. 667 */ 668 if (fwnode_property_read_bool(fixed_node, "pause")) 669 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 670 pl->link_config.lp_advertising); 671 if (fwnode_property_read_bool(fixed_node, "asym-pause")) 672 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 673 pl->link_config.lp_advertising); 674 675 if (ret == 0) { 676 desc = fwnode_gpiod_get_index(fixed_node, "link", 0, 677 GPIOD_IN, "?"); 678 679 if (!IS_ERR(desc)) 680 pl->link_gpio = desc; 681 else if (desc == ERR_PTR(-EPROBE_DEFER)) 682 ret = -EPROBE_DEFER; 683 } 684 fwnode_handle_put(fixed_node); 685 686 if (ret) 687 return ret; 688 } else { 689 u32 prop[5]; 690 691 ret = fwnode_property_read_u32_array(fwnode, "fixed-link", 692 NULL, 0); 693 if (ret != ARRAY_SIZE(prop)) { 694 phylink_err(pl, "broken fixed-link?\n"); 695 return -EINVAL; 696 } 697 698 ret = fwnode_property_read_u32_array(fwnode, "fixed-link", 699 prop, ARRAY_SIZE(prop)); 700 if (!ret) { 701 pl->link_config.duplex = prop[1] ? 702 DUPLEX_FULL : DUPLEX_HALF; 703 pl->link_config.speed = prop[2]; 704 if (prop[3]) 705 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 706 pl->link_config.lp_advertising); 707 if (prop[4]) 708 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 709 pl->link_config.lp_advertising); 710 } 711 } 712 713 if (pl->link_config.speed > SPEED_1000 && 714 pl->link_config.duplex != DUPLEX_FULL) 715 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n", 716 pl->link_config.speed); 717 718 linkmode_zero(pl->supported); 719 phylink_fill_fixedlink_supported(pl->supported); 720 721 linkmode_copy(pl->link_config.advertising, pl->supported); 722 phylink_validate(pl, pl->supported, &pl->link_config); 723 724 c = phy_caps_lookup(pl->link_config.speed, pl->link_config.duplex, 725 pl->supported, true); 726 if (c) 727 linkmode_and(match, pl->supported, c->linkmodes); 728 729 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, mask); 730 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, mask); 731 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask); 732 linkmode_and(pl->supported, pl->supported, mask); 733 734 phylink_set(pl->supported, MII); 735 736 if (c) { 737 linkmode_or(pl->supported, pl->supported, match); 738 linkmode_or(pl->link_config.lp_advertising, 739 pl->link_config.lp_advertising, match); 740 } else { 741 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n", 742 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half", 743 pl->link_config.speed); 744 } 745 746 linkmode_and(pl->link_config.advertising, pl->link_config.advertising, 747 pl->supported); 748 749 pl->link_config.link = 1; 750 pl->link_config.an_complete = 1; 751 752 return 0; 753 } 754 755 static int phylink_parse_mode(struct phylink *pl, 756 const struct fwnode_handle *fwnode) 757 { 758 struct fwnode_handle *dn; 759 const char *managed; 760 unsigned long caps; 761 762 if (pl->config->default_an_inband) 763 pl->cfg_link_an_mode = MLO_AN_INBAND; 764 765 dn = fwnode_get_named_child_node(fwnode, "fixed-link"); 766 if (dn || fwnode_property_present(fwnode, "fixed-link")) 767 pl->cfg_link_an_mode = MLO_AN_FIXED; 768 fwnode_handle_put(dn); 769 770 if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 && 771 strcmp(managed, "in-band-status") == 0)) { 772 if (pl->cfg_link_an_mode == MLO_AN_FIXED) { 773 phylink_err(pl, 774 "can't use both fixed-link and in-band-status\n"); 775 return -EINVAL; 776 } 777 778 pl->cfg_link_an_mode = MLO_AN_INBAND; 779 } 780 781 if (pl->cfg_link_an_mode == MLO_AN_INBAND) { 782 linkmode_zero(pl->supported); 783 phylink_set(pl->supported, MII); 784 phylink_set(pl->supported, Autoneg); 785 phylink_set(pl->supported, Asym_Pause); 786 phylink_set(pl->supported, Pause); 787 788 switch (pl->link_config.interface) { 789 case PHY_INTERFACE_MODE_SGMII: 790 case PHY_INTERFACE_MODE_PSGMII: 791 case PHY_INTERFACE_MODE_QSGMII: 792 case PHY_INTERFACE_MODE_QUSGMII: 793 case PHY_INTERFACE_MODE_RGMII: 794 case PHY_INTERFACE_MODE_RGMII_ID: 795 case PHY_INTERFACE_MODE_RGMII_RXID: 796 case PHY_INTERFACE_MODE_RGMII_TXID: 797 case PHY_INTERFACE_MODE_RTBI: 798 case PHY_INTERFACE_MODE_1000BASEX: 799 case PHY_INTERFACE_MODE_2500BASEX: 800 case PHY_INTERFACE_MODE_5GBASER: 801 case PHY_INTERFACE_MODE_25GBASER: 802 case PHY_INTERFACE_MODE_USXGMII: 803 case PHY_INTERFACE_MODE_10G_QXGMII: 804 case PHY_INTERFACE_MODE_10GKR: 805 case PHY_INTERFACE_MODE_10GBASER: 806 case PHY_INTERFACE_MODE_XLGMII: 807 caps = ~(MAC_SYM_PAUSE | MAC_ASYM_PAUSE); 808 caps = phylink_get_capabilities(pl->link_config.interface, caps, 809 RATE_MATCH_NONE); 810 phylink_caps_to_linkmodes(pl->supported, caps); 811 break; 812 813 default: 814 phylink_err(pl, 815 "incorrect link mode %s for in-band status\n", 816 phy_modes(pl->link_config.interface)); 817 return -EINVAL; 818 } 819 820 linkmode_copy(pl->link_config.advertising, pl->supported); 821 822 if (phylink_validate(pl, pl->supported, &pl->link_config)) { 823 phylink_err(pl, 824 "failed to validate link configuration for in-band status\n"); 825 return -EINVAL; 826 } 827 } 828 829 return 0; 830 } 831 832 static void phylink_apply_manual_flow(struct phylink *pl, 833 struct phylink_link_state *state) 834 { 835 /* If autoneg is disabled, pause AN is also disabled */ 836 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 837 state->advertising)) 838 state->pause &= ~MLO_PAUSE_AN; 839 840 /* Manual configuration of pause modes */ 841 if (!(pl->link_config.pause & MLO_PAUSE_AN)) 842 state->pause = pl->link_config.pause; 843 } 844 845 static void phylink_resolve_an_pause(struct phylink_link_state *state) 846 { 847 bool tx_pause, rx_pause; 848 849 if (state->duplex == DUPLEX_FULL) { 850 linkmode_resolve_pause(state->advertising, 851 state->lp_advertising, 852 &tx_pause, &rx_pause); 853 if (tx_pause) 854 state->pause |= MLO_PAUSE_TX; 855 if (rx_pause) 856 state->pause |= MLO_PAUSE_RX; 857 } 858 } 859 860 static unsigned int phylink_pcs_inband_caps(struct phylink_pcs *pcs, 861 phy_interface_t interface) 862 { 863 if (pcs && pcs->ops->pcs_inband_caps) 864 return pcs->ops->pcs_inband_caps(pcs, interface); 865 866 return 0; 867 } 868 869 static void phylink_pcs_pre_config(struct phylink_pcs *pcs, 870 phy_interface_t interface) 871 { 872 if (pcs && pcs->ops->pcs_pre_config) 873 pcs->ops->pcs_pre_config(pcs, interface); 874 } 875 876 static int phylink_pcs_post_config(struct phylink_pcs *pcs, 877 phy_interface_t interface) 878 { 879 int err = 0; 880 881 if (pcs && pcs->ops->pcs_post_config) 882 err = pcs->ops->pcs_post_config(pcs, interface); 883 884 return err; 885 } 886 887 static void phylink_pcs_disable(struct phylink_pcs *pcs) 888 { 889 if (pcs && pcs->ops->pcs_disable) 890 pcs->ops->pcs_disable(pcs); 891 } 892 893 static int phylink_pcs_enable(struct phylink_pcs *pcs) 894 { 895 int err = 0; 896 897 if (pcs && pcs->ops->pcs_enable) 898 err = pcs->ops->pcs_enable(pcs); 899 900 return err; 901 } 902 903 static int phylink_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode, 904 const struct phylink_link_state *state, 905 bool permit_pause_to_mac) 906 { 907 if (!pcs) 908 return 0; 909 910 return pcs->ops->pcs_config(pcs, neg_mode, state->interface, 911 state->advertising, permit_pause_to_mac); 912 } 913 914 static void phylink_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode, 915 phy_interface_t interface, int speed, 916 int duplex) 917 { 918 if (pcs && pcs->ops->pcs_link_up) 919 pcs->ops->pcs_link_up(pcs, neg_mode, interface, speed, duplex); 920 } 921 922 static void phylink_pcs_disable_eee(struct phylink_pcs *pcs) 923 { 924 if (pcs && pcs->ops->pcs_disable_eee) 925 pcs->ops->pcs_disable_eee(pcs); 926 } 927 928 static void phylink_pcs_enable_eee(struct phylink_pcs *pcs) 929 { 930 if (pcs && pcs->ops->pcs_enable_eee) 931 pcs->ops->pcs_enable_eee(pcs); 932 } 933 934 /* Query inband for a specific interface mode, asking the MAC for the 935 * PCS which will be used to handle the interface mode. 936 */ 937 static unsigned int phylink_inband_caps(struct phylink *pl, 938 phy_interface_t interface) 939 { 940 struct phylink_pcs *pcs; 941 942 if (!pl->mac_ops->mac_select_pcs) 943 return 0; 944 945 pcs = pl->mac_ops->mac_select_pcs(pl->config, interface); 946 if (!pcs) 947 return 0; 948 949 return phylink_pcs_inband_caps(pcs, interface); 950 } 951 952 static void phylink_pcs_poll_stop(struct phylink *pl) 953 { 954 if (pl->cfg_link_an_mode == MLO_AN_INBAND) 955 del_timer(&pl->link_poll); 956 } 957 958 static void phylink_pcs_poll_start(struct phylink *pl) 959 { 960 if (pl->pcs && pl->pcs->poll && pl->cfg_link_an_mode == MLO_AN_INBAND) 961 mod_timer(&pl->link_poll, jiffies + HZ); 962 } 963 964 int phylink_pcs_pre_init(struct phylink *pl, struct phylink_pcs *pcs) 965 { 966 int ret = 0; 967 968 /* Signal to PCS driver that MAC requires RX clock for init */ 969 if (pl->config->mac_requires_rxc) 970 pcs->rxc_always_on = true; 971 972 if (pcs->ops->pcs_pre_init) 973 ret = pcs->ops->pcs_pre_init(pcs); 974 975 return ret; 976 } 977 EXPORT_SYMBOL_GPL(phylink_pcs_pre_init); 978 979 static void phylink_mac_config(struct phylink *pl, 980 const struct phylink_link_state *state) 981 { 982 struct phylink_link_state st = *state; 983 984 /* Stop drivers incorrectly using these */ 985 linkmode_zero(st.lp_advertising); 986 st.speed = SPEED_UNKNOWN; 987 st.duplex = DUPLEX_UNKNOWN; 988 st.an_complete = false; 989 st.link = false; 990 991 phylink_dbg(pl, 992 "%s: mode=%s/%s/%s adv=%*pb pause=%02x\n", 993 __func__, phylink_an_mode_str(pl->act_link_an_mode), 994 phy_modes(st.interface), 995 phy_rate_matching_to_str(st.rate_matching), 996 __ETHTOOL_LINK_MODE_MASK_NBITS, st.advertising, 997 st.pause); 998 999 pl->mac_ops->mac_config(pl->config, pl->act_link_an_mode, &st); 1000 } 1001 1002 static void phylink_pcs_an_restart(struct phylink *pl) 1003 { 1004 if (pl->pcs && linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 1005 pl->link_config.advertising) && 1006 phy_interface_mode_is_8023z(pl->link_config.interface) && 1007 phylink_autoneg_inband(pl->act_link_an_mode)) 1008 pl->pcs->ops->pcs_an_restart(pl->pcs); 1009 } 1010 1011 /** 1012 * phylink_pcs_neg_mode() - helper to determine PCS inband mode 1013 * @pl: a pointer to a &struct phylink returned from phylink_create() 1014 * @pcs: a pointer to &struct phylink_pcs 1015 * @interface: interface mode to be used 1016 * @advertising: adertisement ethtool link mode mask 1017 * 1018 * Determines the negotiation mode to be used by the PCS, and returns 1019 * one of: 1020 * 1021 * - %PHYLINK_PCS_NEG_NONE: interface mode does not support inband 1022 * - %PHYLINK_PCS_NEG_OUTBAND: an out of band mode (e.g. reading the PHY) 1023 * will be used. 1024 * - %PHYLINK_PCS_NEG_INBAND_DISABLED: inband mode selected but autoneg 1025 * disabled 1026 * - %PHYLINK_PCS_NEG_INBAND_ENABLED: inband mode selected and autoneg enabled 1027 * 1028 * Note: this is for cases where the PCS itself is involved in negotiation 1029 * (e.g. Clause 37, SGMII and similar) not Clause 73. 1030 */ 1031 static void phylink_pcs_neg_mode(struct phylink *pl, struct phylink_pcs *pcs, 1032 phy_interface_t interface, 1033 const unsigned long *advertising) 1034 { 1035 unsigned int pcs_ib_caps = 0; 1036 unsigned int phy_ib_caps = 0; 1037 unsigned int neg_mode, mode; 1038 enum { 1039 INBAND_CISCO_SGMII, 1040 INBAND_BASEX, 1041 } type; 1042 1043 mode = pl->req_link_an_mode; 1044 1045 pl->phy_ib_mode = 0; 1046 1047 switch (interface) { 1048 case PHY_INTERFACE_MODE_SGMII: 1049 case PHY_INTERFACE_MODE_QSGMII: 1050 case PHY_INTERFACE_MODE_QUSGMII: 1051 case PHY_INTERFACE_MODE_USXGMII: 1052 case PHY_INTERFACE_MODE_10G_QXGMII: 1053 /* These protocols are designed for use with a PHY which 1054 * communicates its negotiation result back to the MAC via 1055 * inband communication. Note: there exist PHYs that run 1056 * with SGMII but do not send the inband data. 1057 */ 1058 type = INBAND_CISCO_SGMII; 1059 break; 1060 1061 case PHY_INTERFACE_MODE_1000BASEX: 1062 case PHY_INTERFACE_MODE_2500BASEX: 1063 /* 1000base-X is designed for use media-side for Fibre 1064 * connections, and thus the Autoneg bit needs to be 1065 * taken into account. We also do this for 2500base-X 1066 * as well, but drivers may not support this, so may 1067 * need to override this. 1068 */ 1069 type = INBAND_BASEX; 1070 break; 1071 1072 default: 1073 pl->pcs_neg_mode = PHYLINK_PCS_NEG_NONE; 1074 pl->act_link_an_mode = mode; 1075 return; 1076 } 1077 1078 if (pcs) 1079 pcs_ib_caps = phylink_pcs_inband_caps(pcs, interface); 1080 1081 if (pl->phydev) 1082 phy_ib_caps = phy_inband_caps(pl->phydev, interface); 1083 1084 phylink_dbg(pl, "interface %s inband modes: pcs=%02x phy=%02x\n", 1085 phy_modes(interface), pcs_ib_caps, phy_ib_caps); 1086 1087 if (!phylink_autoneg_inband(mode)) { 1088 bool pcs_ib_only = false; 1089 bool phy_ib_only = false; 1090 1091 if (pcs_ib_caps && pcs_ib_caps != LINK_INBAND_DISABLE) { 1092 /* PCS supports reporting in-band capabilities, and 1093 * supports more than disable mode. 1094 */ 1095 if (pcs_ib_caps & LINK_INBAND_DISABLE) 1096 neg_mode = PHYLINK_PCS_NEG_OUTBAND; 1097 else if (pcs_ib_caps & LINK_INBAND_ENABLE) 1098 pcs_ib_only = true; 1099 } 1100 1101 if (phy_ib_caps && phy_ib_caps != LINK_INBAND_DISABLE) { 1102 /* PHY supports in-band capabilities, and supports 1103 * more than disable mode. 1104 */ 1105 if (phy_ib_caps & LINK_INBAND_DISABLE) 1106 pl->phy_ib_mode = LINK_INBAND_DISABLE; 1107 else if (phy_ib_caps & LINK_INBAND_BYPASS) 1108 pl->phy_ib_mode = LINK_INBAND_BYPASS; 1109 else if (phy_ib_caps & LINK_INBAND_ENABLE) 1110 phy_ib_only = true; 1111 } 1112 1113 /* If either the PCS or PHY requires inband to be enabled, 1114 * this is an invalid configuration. Provide a diagnostic 1115 * message for this case, but don't try to force the issue. 1116 */ 1117 if (pcs_ib_only || phy_ib_only) 1118 phylink_warn(pl, 1119 "firmware wants %s mode, but %s%s%s requires inband\n", 1120 phylink_an_mode_str(mode), 1121 pcs_ib_only ? "PCS" : "", 1122 pcs_ib_only && phy_ib_only ? " and " : "", 1123 phy_ib_only ? "PHY" : ""); 1124 1125 neg_mode = PHYLINK_PCS_NEG_OUTBAND; 1126 } else if (type == INBAND_CISCO_SGMII || pl->phydev) { 1127 /* For SGMII modes which are designed to be used with PHYs, or 1128 * Base-X with a PHY, we try to use in-band mode where-ever 1129 * possible. However, there are some PHYs e.g. BCM84881 which 1130 * do not support in-band. 1131 */ 1132 const unsigned int inband_ok = LINK_INBAND_ENABLE | 1133 LINK_INBAND_BYPASS; 1134 const unsigned int outband_ok = LINK_INBAND_DISABLE | 1135 LINK_INBAND_BYPASS; 1136 /* PCS PHY 1137 * D E D E 1138 * 0 0 0 0 no information inband enabled 1139 * 1 0 0 0 pcs doesn't support outband 1140 * 0 1 0 0 pcs required inband enabled 1141 * 1 1 0 0 pcs optional inband enabled 1142 * 0 0 1 0 phy doesn't support outband 1143 * 1 0 1 0 pcs+phy doesn't support outband 1144 * 0 1 1 0 pcs required, phy doesn't support, invalid 1145 * 1 1 1 0 pcs optional, phy doesn't support, outband 1146 * 0 0 0 1 phy required inband enabled 1147 * 1 0 0 1 pcs doesn't support, phy required, invalid 1148 * 0 1 0 1 pcs+phy required inband enabled 1149 * 1 1 0 1 pcs optional, phy required inband enabled 1150 * 0 0 1 1 phy optional inband enabled 1151 * 1 0 1 1 pcs doesn't support, phy optional, outband 1152 * 0 1 1 1 pcs required, phy optional inband enabled 1153 * 1 1 1 1 pcs+phy optional inband enabled 1154 */ 1155 if ((!pcs_ib_caps || pcs_ib_caps & inband_ok) && 1156 (!phy_ib_caps || phy_ib_caps & inband_ok)) { 1157 /* In-band supported or unknown at both ends. Enable 1158 * in-band mode with or without bypass at the PHY. 1159 */ 1160 if (phy_ib_caps & LINK_INBAND_ENABLE) 1161 pl->phy_ib_mode = LINK_INBAND_ENABLE; 1162 else if (phy_ib_caps & LINK_INBAND_BYPASS) 1163 pl->phy_ib_mode = LINK_INBAND_BYPASS; 1164 1165 neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED; 1166 } else if ((!pcs_ib_caps || pcs_ib_caps & outband_ok) && 1167 (!phy_ib_caps || phy_ib_caps & outband_ok)) { 1168 /* Either in-band not supported at at least one end. 1169 * In-band bypass at the other end is possible. 1170 */ 1171 if (phy_ib_caps & LINK_INBAND_DISABLE) 1172 pl->phy_ib_mode = LINK_INBAND_DISABLE; 1173 else if (phy_ib_caps & LINK_INBAND_BYPASS) 1174 pl->phy_ib_mode = LINK_INBAND_BYPASS; 1175 1176 neg_mode = PHYLINK_PCS_NEG_OUTBAND; 1177 if (pl->phydev) 1178 mode = MLO_AN_PHY; 1179 } else { 1180 /* invalid */ 1181 phylink_warn(pl, "%s: incompatible in-band capabilities, trying in-band", 1182 phy_modes(interface)); 1183 neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED; 1184 } 1185 } else { 1186 /* For Base-X without a PHY */ 1187 if (pcs_ib_caps == LINK_INBAND_DISABLE) 1188 /* If the PCS doesn't support inband, then inband must 1189 * be disabled. 1190 */ 1191 neg_mode = PHYLINK_PCS_NEG_INBAND_DISABLED; 1192 else if (pcs_ib_caps == LINK_INBAND_ENABLE) 1193 /* If the PCS requires inband, then inband must always 1194 * be enabled. 1195 */ 1196 neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED; 1197 else if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 1198 advertising)) 1199 neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED; 1200 else 1201 neg_mode = PHYLINK_PCS_NEG_INBAND_DISABLED; 1202 } 1203 1204 pl->pcs_neg_mode = neg_mode; 1205 pl->act_link_an_mode = mode; 1206 } 1207 1208 static void phylink_major_config(struct phylink *pl, bool restart, 1209 const struct phylink_link_state *state) 1210 { 1211 struct phylink_pcs *pcs = NULL; 1212 bool pcs_changed = false; 1213 unsigned int rate_kbd; 1214 int err; 1215 1216 phylink_dbg(pl, "major config, requested %s/%s\n", 1217 phylink_an_mode_str(pl->req_link_an_mode), 1218 phy_modes(state->interface)); 1219 1220 if (pl->mac_ops->mac_select_pcs) { 1221 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface); 1222 if (IS_ERR(pcs)) { 1223 phylink_err(pl, 1224 "mac_select_pcs unexpectedly failed: %pe\n", 1225 pcs); 1226 return; 1227 } 1228 1229 pcs_changed = pl->pcs != pcs; 1230 } 1231 1232 phylink_pcs_neg_mode(pl, pcs, state->interface, state->advertising); 1233 1234 phylink_dbg(pl, "major config, active %s/%s/%s\n", 1235 phylink_an_mode_str(pl->act_link_an_mode), 1236 phylink_pcs_mode_str(pl->pcs_neg_mode), 1237 phy_modes(state->interface)); 1238 1239 phylink_pcs_poll_stop(pl); 1240 1241 if (pl->mac_ops->mac_prepare) { 1242 err = pl->mac_ops->mac_prepare(pl->config, pl->act_link_an_mode, 1243 state->interface); 1244 if (err < 0) { 1245 phylink_err(pl, "mac_prepare failed: %pe\n", 1246 ERR_PTR(err)); 1247 return; 1248 } 1249 } 1250 1251 /* If we have a new PCS, switch to the new PCS after preparing the MAC 1252 * for the change. 1253 */ 1254 if (pcs_changed) { 1255 phylink_pcs_disable(pl->pcs); 1256 1257 if (pl->pcs) 1258 pl->pcs->phylink = NULL; 1259 1260 pcs->phylink = pl; 1261 1262 pl->pcs = pcs; 1263 } 1264 1265 if (pl->pcs) 1266 phylink_pcs_pre_config(pl->pcs, state->interface); 1267 1268 phylink_mac_config(pl, state); 1269 1270 if (pl->pcs) 1271 phylink_pcs_post_config(pl->pcs, state->interface); 1272 1273 if (pl->pcs_state == PCS_STATE_STARTING || pcs_changed) 1274 phylink_pcs_enable(pl->pcs); 1275 1276 err = phylink_pcs_config(pl->pcs, pl->pcs_neg_mode, state, 1277 !!(pl->link_config.pause & MLO_PAUSE_AN)); 1278 if (err < 0) 1279 phylink_err(pl, "pcs_config failed: %pe\n", 1280 ERR_PTR(err)); 1281 else if (err > 0) 1282 restart = true; 1283 1284 if (restart) 1285 phylink_pcs_an_restart(pl); 1286 1287 if (pl->mac_ops->mac_finish) { 1288 err = pl->mac_ops->mac_finish(pl->config, pl->act_link_an_mode, 1289 state->interface); 1290 if (err < 0) 1291 phylink_err(pl, "mac_finish failed: %pe\n", 1292 ERR_PTR(err)); 1293 } 1294 1295 if (pl->phydev && pl->phy_ib_mode) { 1296 err = phy_config_inband(pl->phydev, pl->phy_ib_mode); 1297 if (err < 0) 1298 phylink_err(pl, "phy_config_inband: %pe\n", 1299 ERR_PTR(err)); 1300 } 1301 1302 if (pl->sfp_bus) { 1303 rate_kbd = phylink_interface_signal_rate(state->interface); 1304 if (rate_kbd) 1305 sfp_upstream_set_signal_rate(pl->sfp_bus, rate_kbd); 1306 } 1307 1308 phylink_pcs_poll_start(pl); 1309 } 1310 1311 /* 1312 * Reconfigure for a change of inband advertisement. 1313 * If we have a separate PCS, we only need to call its pcs_config() method, 1314 * and then restart AN if it indicates something changed. Otherwise, we do 1315 * the full MAC reconfiguration. 1316 */ 1317 static int phylink_change_inband_advert(struct phylink *pl) 1318 { 1319 int ret; 1320 1321 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) 1322 return 0; 1323 1324 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__, 1325 phylink_an_mode_str(pl->req_link_an_mode), 1326 phy_modes(pl->link_config.interface), 1327 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising, 1328 pl->link_config.pause); 1329 1330 /* Recompute the PCS neg mode */ 1331 phylink_pcs_neg_mode(pl, pl->pcs, pl->link_config.interface, 1332 pl->link_config.advertising); 1333 1334 /* Modern PCS-based method; update the advert at the PCS, and 1335 * restart negotiation if the pcs_config() helper indicates that 1336 * the programmed advertisement has changed. 1337 */ 1338 ret = phylink_pcs_config(pl->pcs, pl->pcs_neg_mode, &pl->link_config, 1339 !!(pl->link_config.pause & MLO_PAUSE_AN)); 1340 if (ret < 0) 1341 return ret; 1342 1343 if (ret > 0) 1344 phylink_pcs_an_restart(pl); 1345 1346 return 0; 1347 } 1348 1349 static void phylink_mac_pcs_get_state(struct phylink *pl, 1350 struct phylink_link_state *state) 1351 { 1352 struct phylink_pcs *pcs; 1353 bool autoneg; 1354 1355 linkmode_copy(state->advertising, pl->link_config.advertising); 1356 linkmode_zero(state->lp_advertising); 1357 state->interface = pl->link_config.interface; 1358 state->rate_matching = pl->link_config.rate_matching; 1359 state->an_complete = 0; 1360 state->link = 1; 1361 1362 autoneg = pl->pcs_neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED; 1363 if (autoneg) { 1364 state->speed = SPEED_UNKNOWN; 1365 state->duplex = DUPLEX_UNKNOWN; 1366 state->pause = MLO_PAUSE_NONE; 1367 } else { 1368 state->speed = pl->link_config.speed; 1369 state->duplex = pl->link_config.duplex; 1370 state->pause = pl->link_config.pause; 1371 } 1372 1373 pcs = pl->pcs; 1374 if (pcs) 1375 pcs->ops->pcs_get_state(pcs, pl->pcs_neg_mode, state); 1376 else 1377 state->link = 0; 1378 } 1379 1380 /* The fixed state is... fixed except for the link state, 1381 * which may be determined by a GPIO or a callback. 1382 */ 1383 static void phylink_get_fixed_state(struct phylink *pl, 1384 struct phylink_link_state *state) 1385 { 1386 *state = pl->link_config; 1387 if (pl->config->get_fixed_state) 1388 pl->config->get_fixed_state(pl->config, state); 1389 else if (pl->link_gpio) 1390 state->link = !!gpiod_get_value_cansleep(pl->link_gpio); 1391 1392 state->pause = MLO_PAUSE_NONE; 1393 phylink_resolve_an_pause(state); 1394 } 1395 1396 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart) 1397 { 1398 struct phylink_link_state link_state; 1399 1400 switch (pl->req_link_an_mode) { 1401 case MLO_AN_PHY: 1402 link_state = pl->phy_state; 1403 break; 1404 1405 case MLO_AN_FIXED: 1406 phylink_get_fixed_state(pl, &link_state); 1407 break; 1408 1409 case MLO_AN_INBAND: 1410 link_state = pl->link_config; 1411 if (link_state.interface == PHY_INTERFACE_MODE_SGMII) 1412 link_state.pause = MLO_PAUSE_NONE; 1413 break; 1414 1415 default: /* can't happen */ 1416 return; 1417 } 1418 1419 link_state.link = false; 1420 1421 phylink_apply_manual_flow(pl, &link_state); 1422 phylink_major_config(pl, force_restart, &link_state); 1423 } 1424 1425 static const char *phylink_pause_to_str(int pause) 1426 { 1427 switch (pause & MLO_PAUSE_TXRX_MASK) { 1428 case MLO_PAUSE_TX | MLO_PAUSE_RX: 1429 return "rx/tx"; 1430 case MLO_PAUSE_TX: 1431 return "tx"; 1432 case MLO_PAUSE_RX: 1433 return "rx"; 1434 default: 1435 return "off"; 1436 } 1437 } 1438 1439 static void phylink_deactivate_lpi(struct phylink *pl) 1440 { 1441 if (pl->mac_enable_tx_lpi) { 1442 pl->mac_enable_tx_lpi = false; 1443 1444 phylink_dbg(pl, "disabling LPI\n"); 1445 1446 pl->mac_ops->mac_disable_tx_lpi(pl->config); 1447 1448 phylink_pcs_disable_eee(pl->pcs); 1449 } 1450 } 1451 1452 static void phylink_activate_lpi(struct phylink *pl) 1453 { 1454 int err; 1455 1456 if (!test_bit(pl->cur_interface, pl->config->lpi_interfaces)) { 1457 phylink_dbg(pl, "MAC does not support LPI with %s\n", 1458 phy_modes(pl->cur_interface)); 1459 return; 1460 } 1461 1462 phylink_dbg(pl, "LPI timer %uus, tx clock stop %u\n", 1463 pl->mac_tx_lpi_timer, pl->mac_tx_clk_stop); 1464 1465 phylink_pcs_enable_eee(pl->pcs); 1466 1467 err = pl->mac_ops->mac_enable_tx_lpi(pl->config, pl->mac_tx_lpi_timer, 1468 pl->mac_tx_clk_stop); 1469 if (err) { 1470 phylink_pcs_disable_eee(pl->pcs); 1471 phylink_err(pl, "%ps() failed: %pe\n", 1472 pl->mac_ops->mac_enable_tx_lpi, ERR_PTR(err)); 1473 return; 1474 } 1475 1476 pl->mac_enable_tx_lpi = true; 1477 } 1478 1479 static void phylink_link_up(struct phylink *pl, 1480 struct phylink_link_state link_state) 1481 { 1482 struct net_device *ndev = pl->netdev; 1483 int speed, duplex; 1484 bool rx_pause; 1485 1486 speed = link_state.speed; 1487 duplex = link_state.duplex; 1488 rx_pause = !!(link_state.pause & MLO_PAUSE_RX); 1489 1490 switch (link_state.rate_matching) { 1491 case RATE_MATCH_PAUSE: 1492 /* The PHY is doing rate matchion from the media rate (in 1493 * the link_state) to the interface speed, and will send 1494 * pause frames to the MAC to limit its transmission speed. 1495 */ 1496 speed = phylink_interface_max_speed(link_state.interface); 1497 duplex = DUPLEX_FULL; 1498 rx_pause = true; 1499 break; 1500 1501 case RATE_MATCH_CRS: 1502 /* The PHY is doing rate matchion from the media rate (in 1503 * the link_state) to the interface speed, and will cause 1504 * collisions to the MAC to limit its transmission speed. 1505 */ 1506 speed = phylink_interface_max_speed(link_state.interface); 1507 duplex = DUPLEX_HALF; 1508 break; 1509 } 1510 1511 pl->cur_interface = link_state.interface; 1512 1513 phylink_pcs_link_up(pl->pcs, pl->pcs_neg_mode, pl->cur_interface, speed, 1514 duplex); 1515 1516 pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->act_link_an_mode, 1517 pl->cur_interface, speed, duplex, 1518 !!(link_state.pause & MLO_PAUSE_TX), rx_pause); 1519 1520 if (pl->mac_supports_eee && pl->phy_enable_tx_lpi) 1521 phylink_activate_lpi(pl); 1522 1523 if (ndev) 1524 netif_carrier_on(ndev); 1525 1526 phylink_info(pl, 1527 "Link is Up - %s/%s - flow control %s\n", 1528 phy_speed_to_str(link_state.speed), 1529 phy_duplex_to_str(link_state.duplex), 1530 phylink_pause_to_str(link_state.pause)); 1531 } 1532 1533 static void phylink_link_down(struct phylink *pl) 1534 { 1535 struct net_device *ndev = pl->netdev; 1536 1537 if (ndev) 1538 netif_carrier_off(ndev); 1539 1540 phylink_deactivate_lpi(pl); 1541 1542 pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode, 1543 pl->cur_interface); 1544 phylink_info(pl, "Link is Down\n"); 1545 } 1546 1547 static bool phylink_link_is_up(struct phylink *pl) 1548 { 1549 return pl->netdev ? netif_carrier_ok(pl->netdev) : pl->old_link_state; 1550 } 1551 1552 static void phylink_resolve(struct work_struct *w) 1553 { 1554 struct phylink *pl = container_of(w, struct phylink, resolve); 1555 struct phylink_link_state link_state; 1556 bool mac_config = false; 1557 bool retrigger = false; 1558 bool cur_link_state; 1559 1560 mutex_lock(&pl->state_mutex); 1561 cur_link_state = phylink_link_is_up(pl); 1562 1563 if (pl->phylink_disable_state) { 1564 pl->link_failed = false; 1565 link_state.link = false; 1566 } else if (pl->link_failed) { 1567 link_state.link = false; 1568 retrigger = true; 1569 } else if (pl->act_link_an_mode == MLO_AN_FIXED) { 1570 phylink_get_fixed_state(pl, &link_state); 1571 mac_config = link_state.link; 1572 } else if (pl->act_link_an_mode == MLO_AN_PHY) { 1573 link_state = pl->phy_state; 1574 mac_config = link_state.link; 1575 } else { 1576 phylink_mac_pcs_get_state(pl, &link_state); 1577 1578 /* The PCS may have a latching link-fail indicator. If the link 1579 * was up, bring the link down and re-trigger the resolve. 1580 * Otherwise, re-read the PCS state to get the current status 1581 * of the link. 1582 */ 1583 if (!link_state.link) { 1584 if (cur_link_state) 1585 retrigger = true; 1586 else 1587 phylink_mac_pcs_get_state(pl, &link_state); 1588 } 1589 1590 /* If we have a phy, the "up" state is the union of both the 1591 * PHY and the MAC 1592 */ 1593 if (pl->phydev) 1594 link_state.link &= pl->phy_state.link; 1595 1596 /* Only update if the PHY link is up */ 1597 if (pl->phydev && pl->phy_state.link) { 1598 /* If the interface has changed, force a link down 1599 * event if the link isn't already down, and re-resolve. 1600 */ 1601 if (link_state.interface != pl->phy_state.interface) { 1602 retrigger = true; 1603 link_state.link = false; 1604 } 1605 1606 link_state.interface = pl->phy_state.interface; 1607 1608 /* If we are doing rate matching, then the link 1609 * speed/duplex comes from the PHY 1610 */ 1611 if (pl->phy_state.rate_matching) { 1612 link_state.rate_matching = 1613 pl->phy_state.rate_matching; 1614 link_state.speed = pl->phy_state.speed; 1615 link_state.duplex = pl->phy_state.duplex; 1616 } 1617 1618 /* If we have a PHY, we need to update with the PHY 1619 * flow control bits. 1620 */ 1621 link_state.pause = pl->phy_state.pause; 1622 mac_config = true; 1623 } 1624 } 1625 1626 if (pl->act_link_an_mode != MLO_AN_FIXED) 1627 phylink_apply_manual_flow(pl, &link_state); 1628 1629 if (mac_config) { 1630 if (link_state.interface != pl->link_config.interface) { 1631 /* The interface has changed, force the link down and 1632 * then reconfigure. 1633 */ 1634 if (cur_link_state) { 1635 phylink_link_down(pl); 1636 cur_link_state = false; 1637 } 1638 phylink_major_config(pl, false, &link_state); 1639 pl->link_config.interface = link_state.interface; 1640 } 1641 } 1642 1643 if (link_state.link != cur_link_state) { 1644 pl->old_link_state = link_state.link; 1645 if (!link_state.link) 1646 phylink_link_down(pl); 1647 else 1648 phylink_link_up(pl, link_state); 1649 } 1650 if (!link_state.link && retrigger) { 1651 pl->link_failed = false; 1652 queue_work(system_power_efficient_wq, &pl->resolve); 1653 } 1654 mutex_unlock(&pl->state_mutex); 1655 } 1656 1657 static void phylink_run_resolve(struct phylink *pl) 1658 { 1659 if (!pl->phylink_disable_state) 1660 queue_work(system_power_efficient_wq, &pl->resolve); 1661 } 1662 1663 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit) 1664 { 1665 unsigned long state = pl->phylink_disable_state; 1666 1667 set_bit(bit, &pl->phylink_disable_state); 1668 if (state == 0) { 1669 queue_work(system_power_efficient_wq, &pl->resolve); 1670 flush_work(&pl->resolve); 1671 } 1672 } 1673 1674 static void phylink_enable_and_run_resolve(struct phylink *pl, int bit) 1675 { 1676 clear_bit(bit, &pl->phylink_disable_state); 1677 phylink_run_resolve(pl); 1678 } 1679 1680 static void phylink_fixed_poll(struct timer_list *t) 1681 { 1682 struct phylink *pl = container_of(t, struct phylink, link_poll); 1683 1684 mod_timer(t, jiffies + HZ); 1685 1686 phylink_run_resolve(pl); 1687 } 1688 1689 static const struct sfp_upstream_ops sfp_phylink_ops; 1690 1691 static int phylink_register_sfp(struct phylink *pl, 1692 const struct fwnode_handle *fwnode) 1693 { 1694 struct sfp_bus *bus; 1695 int ret; 1696 1697 if (!fwnode) 1698 return 0; 1699 1700 bus = sfp_bus_find_fwnode(fwnode); 1701 if (IS_ERR(bus)) { 1702 phylink_err(pl, "unable to attach SFP bus: %pe\n", bus); 1703 return PTR_ERR(bus); 1704 } 1705 1706 pl->sfp_bus = bus; 1707 1708 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops); 1709 sfp_bus_put(bus); 1710 1711 return ret; 1712 } 1713 1714 /** 1715 * phylink_set_fixed_link() - set the fixed link 1716 * @pl: a pointer to a &struct phylink returned from phylink_create() 1717 * @state: a pointer to a struct phylink_link_state. 1718 * 1719 * This function is used when the link parameters are known and do not change, 1720 * making it suitable for certain types of network connections. 1721 * 1722 * Returns: zero on success or negative error code. 1723 */ 1724 int phylink_set_fixed_link(struct phylink *pl, 1725 const struct phylink_link_state *state) 1726 { 1727 const struct link_capabilities *c; 1728 unsigned long *adv; 1729 1730 if (pl->cfg_link_an_mode != MLO_AN_PHY || !state || 1731 !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) 1732 return -EINVAL; 1733 1734 c = phy_caps_lookup(state->speed, state->duplex, 1735 pl->supported, true); 1736 if (!c) 1737 return -EINVAL; 1738 1739 adv = pl->link_config.advertising; 1740 linkmode_and(adv, pl->supported, c->linkmodes); 1741 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv); 1742 1743 pl->link_config.speed = state->speed; 1744 pl->link_config.duplex = state->duplex; 1745 pl->link_config.link = 1; 1746 pl->link_config.an_complete = 1; 1747 1748 pl->cfg_link_an_mode = MLO_AN_FIXED; 1749 pl->req_link_an_mode = pl->cfg_link_an_mode; 1750 1751 return 0; 1752 } 1753 EXPORT_SYMBOL_GPL(phylink_set_fixed_link); 1754 1755 /** 1756 * phylink_create() - create a phylink instance 1757 * @config: a pointer to the target &struct phylink_config 1758 * @fwnode: a pointer to a &struct fwnode_handle describing the network 1759 * interface 1760 * @iface: the desired link mode defined by &typedef phy_interface_t 1761 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC. 1762 * 1763 * Create a new phylink instance, and parse the link parameters found in @np. 1764 * This will parse in-band modes, fixed-link or SFP configuration. 1765 * 1766 * Note: the rtnl lock must not be held when calling this function. 1767 * 1768 * Returns a pointer to a &struct phylink, or an error-pointer value. Users 1769 * must use IS_ERR() to check for errors from this function. 1770 */ 1771 struct phylink *phylink_create(struct phylink_config *config, 1772 const struct fwnode_handle *fwnode, 1773 phy_interface_t iface, 1774 const struct phylink_mac_ops *mac_ops) 1775 { 1776 struct phylink *pl; 1777 int ret; 1778 1779 /* Validate the supplied configuration */ 1780 if (phy_interface_empty(config->supported_interfaces)) { 1781 dev_err(config->dev, 1782 "phylink: error: empty supported_interfaces\n"); 1783 return ERR_PTR(-EINVAL); 1784 } 1785 1786 pl = kzalloc(sizeof(*pl), GFP_KERNEL); 1787 if (!pl) 1788 return ERR_PTR(-ENOMEM); 1789 1790 mutex_init(&pl->state_mutex); 1791 INIT_WORK(&pl->resolve, phylink_resolve); 1792 1793 pl->config = config; 1794 if (config->type == PHYLINK_NETDEV) { 1795 pl->netdev = to_net_dev(config->dev); 1796 netif_carrier_off(pl->netdev); 1797 } else if (config->type == PHYLINK_DEV) { 1798 pl->dev = config->dev; 1799 } else { 1800 kfree(pl); 1801 return ERR_PTR(-EINVAL); 1802 } 1803 1804 pl->mac_supports_eee_ops = phylink_mac_implements_lpi(mac_ops); 1805 pl->mac_supports_eee = pl->mac_supports_eee_ops && 1806 pl->config->lpi_capabilities && 1807 !phy_interface_empty(pl->config->lpi_interfaces); 1808 1809 /* Set the default EEE configuration */ 1810 pl->eee_cfg.eee_enabled = pl->config->eee_enabled_default; 1811 pl->eee_cfg.tx_lpi_enabled = pl->eee_cfg.eee_enabled; 1812 pl->eee_cfg.tx_lpi_timer = pl->config->lpi_timer_default; 1813 1814 pl->phy_state.interface = iface; 1815 pl->link_interface = iface; 1816 if (iface == PHY_INTERFACE_MODE_MOCA) 1817 pl->link_port = PORT_BNC; 1818 else 1819 pl->link_port = PORT_MII; 1820 pl->link_config.interface = iface; 1821 pl->link_config.pause = MLO_PAUSE_AN; 1822 pl->link_config.speed = SPEED_UNKNOWN; 1823 pl->link_config.duplex = DUPLEX_UNKNOWN; 1824 pl->pcs_state = PCS_STATE_DOWN; 1825 pl->mac_ops = mac_ops; 1826 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); 1827 timer_setup(&pl->link_poll, phylink_fixed_poll, 0); 1828 1829 linkmode_fill(pl->supported); 1830 linkmode_copy(pl->link_config.advertising, pl->supported); 1831 phylink_validate(pl, pl->supported, &pl->link_config); 1832 1833 ret = phylink_parse_mode(pl, fwnode); 1834 if (ret < 0) { 1835 kfree(pl); 1836 return ERR_PTR(ret); 1837 } 1838 1839 if (pl->cfg_link_an_mode == MLO_AN_FIXED) { 1840 ret = phylink_parse_fixedlink(pl, fwnode); 1841 if (ret < 0) { 1842 kfree(pl); 1843 return ERR_PTR(ret); 1844 } 1845 } 1846 1847 pl->req_link_an_mode = pl->cfg_link_an_mode; 1848 1849 ret = phylink_register_sfp(pl, fwnode); 1850 if (ret < 0) { 1851 kfree(pl); 1852 return ERR_PTR(ret); 1853 } 1854 1855 return pl; 1856 } 1857 EXPORT_SYMBOL_GPL(phylink_create); 1858 1859 /** 1860 * phylink_destroy() - cleanup and destroy the phylink instance 1861 * @pl: a pointer to a &struct phylink returned from phylink_create() 1862 * 1863 * Destroy a phylink instance. Any PHY that has been attached must have been 1864 * cleaned up via phylink_disconnect_phy() prior to calling this function. 1865 * 1866 * Note: the rtnl lock must not be held when calling this function. 1867 */ 1868 void phylink_destroy(struct phylink *pl) 1869 { 1870 sfp_bus_del_upstream(pl->sfp_bus); 1871 if (pl->link_gpio) 1872 gpiod_put(pl->link_gpio); 1873 1874 cancel_work_sync(&pl->resolve); 1875 kfree(pl); 1876 } 1877 EXPORT_SYMBOL_GPL(phylink_destroy); 1878 1879 /** 1880 * phylink_expects_phy() - Determine if phylink expects a phy to be attached 1881 * @pl: a pointer to a &struct phylink returned from phylink_create() 1882 * 1883 * When using fixed-link mode, or in-band mode with 1000base-X or 2500base-X, 1884 * no PHY is needed. 1885 * 1886 * Returns true if phylink will be expecting a PHY. 1887 */ 1888 bool phylink_expects_phy(struct phylink *pl) 1889 { 1890 if (pl->cfg_link_an_mode == MLO_AN_FIXED || 1891 (pl->cfg_link_an_mode == MLO_AN_INBAND && 1892 phy_interface_mode_is_8023z(pl->link_interface))) 1893 return false; 1894 return true; 1895 } 1896 EXPORT_SYMBOL_GPL(phylink_expects_phy); 1897 1898 static void phylink_phy_change(struct phy_device *phydev, bool up) 1899 { 1900 struct phylink *pl = phydev->phylink; 1901 bool tx_pause, rx_pause; 1902 1903 phy_get_pause(phydev, &tx_pause, &rx_pause); 1904 1905 mutex_lock(&pl->state_mutex); 1906 pl->phy_state.speed = phydev->speed; 1907 pl->phy_state.duplex = phydev->duplex; 1908 pl->phy_state.rate_matching = phydev->rate_matching; 1909 pl->phy_state.pause = MLO_PAUSE_NONE; 1910 if (tx_pause) 1911 pl->phy_state.pause |= MLO_PAUSE_TX; 1912 if (rx_pause) 1913 pl->phy_state.pause |= MLO_PAUSE_RX; 1914 pl->phy_state.interface = phydev->interface; 1915 pl->phy_state.link = up; 1916 if (!up) 1917 pl->link_failed = true; 1918 1919 /* Get the LPI state from phylib */ 1920 pl->phy_enable_tx_lpi = phydev->enable_tx_lpi; 1921 pl->mac_tx_lpi_timer = phydev->eee_cfg.tx_lpi_timer; 1922 mutex_unlock(&pl->state_mutex); 1923 1924 phylink_run_resolve(pl); 1925 1926 phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s/%slpi\n", 1927 up ? "up" : "down", 1928 phy_modes(phydev->interface), 1929 phy_speed_to_str(phydev->speed), 1930 phy_duplex_to_str(phydev->duplex), 1931 phy_rate_matching_to_str(phydev->rate_matching), 1932 phylink_pause_to_str(pl->phy_state.pause), 1933 phydev->enable_tx_lpi ? "" : "no"); 1934 } 1935 1936 static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy, 1937 unsigned long *supported, 1938 struct phylink_link_state *state) 1939 { 1940 DECLARE_PHY_INTERFACE_MASK(interfaces); 1941 1942 /* If the PHY provides a bitmap of the interfaces it will be using 1943 * depending on the negotiated media speeds, use this to validate 1944 * which ethtool link modes can be used. 1945 */ 1946 if (!phy_interface_empty(phy->possible_interfaces)) { 1947 /* We only care about the union of the PHY's interfaces and 1948 * those which the host supports. 1949 */ 1950 phy_interface_and(interfaces, phy->possible_interfaces, 1951 pl->config->supported_interfaces); 1952 1953 if (phy_interface_empty(interfaces)) { 1954 phylink_err(pl, "PHY has no common interfaces\n"); 1955 return -EINVAL; 1956 } 1957 1958 if (phy_on_sfp(phy)) { 1959 /* If the PHY is on a SFP, limit the interfaces to 1960 * those that can be used with a SFP module. 1961 */ 1962 phy_interface_and(interfaces, interfaces, 1963 phylink_sfp_interfaces); 1964 1965 if (phy_interface_empty(interfaces)) { 1966 phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n"); 1967 return -EINVAL; 1968 } 1969 } 1970 1971 phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n", 1972 phydev_name(phy), 1973 (int)PHY_INTERFACE_MODE_MAX, 1974 phy->possible_interfaces, 1975 (int)PHY_INTERFACE_MODE_MAX, interfaces); 1976 1977 return phylink_validate_mask(pl, phy, supported, state, 1978 interfaces); 1979 } 1980 1981 phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n", 1982 phydev_name(phy)); 1983 1984 /* Check whether we would use rate matching for the proposed interface 1985 * mode. 1986 */ 1987 state->rate_matching = phy_get_rate_matching(phy, state->interface); 1988 1989 /* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R, 1990 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching. 1991 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching 1992 * their Serdes is either unnecessary or not reasonable. 1993 * 1994 * For these which switch interface modes, we really need to know which 1995 * interface modes the PHY supports to properly work out which ethtool 1996 * linkmodes can be supported. For now, as a work-around, we validate 1997 * against all interface modes, which may lead to more ethtool link 1998 * modes being advertised than are actually supported. 1999 */ 2000 if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE && 2001 state->interface != PHY_INTERFACE_MODE_RXAUI && 2002 state->interface != PHY_INTERFACE_MODE_XAUI && 2003 state->interface != PHY_INTERFACE_MODE_USXGMII) 2004 state->interface = PHY_INTERFACE_MODE_NA; 2005 2006 return phylink_validate(pl, supported, state); 2007 } 2008 2009 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy, 2010 phy_interface_t interface) 2011 { 2012 struct phylink_link_state config; 2013 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); 2014 char *irq_str; 2015 int ret; 2016 2017 /* 2018 * This is the new way of dealing with flow control for PHYs, 2019 * as described by Timur Tabi in commit 529ed1275263 ("net: phy: 2020 * phy drivers should not set SUPPORTED_[Asym_]Pause") except 2021 * using our validate call to the MAC, we rely upon the MAC 2022 * clearing the bits from both supported and advertising fields. 2023 */ 2024 phy_support_asym_pause(phy); 2025 2026 memset(&config, 0, sizeof(config)); 2027 linkmode_copy(supported, phy->supported); 2028 linkmode_copy(config.advertising, phy->advertising); 2029 config.interface = interface; 2030 2031 ret = phylink_validate_phy(pl, phy, supported, &config); 2032 if (ret) { 2033 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n", 2034 phy_modes(config.interface), 2035 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported, 2036 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising, 2037 ERR_PTR(ret)); 2038 return ret; 2039 } 2040 2041 phy->phylink = pl; 2042 phy->phy_link_change = phylink_phy_change; 2043 2044 irq_str = phy_attached_info_irq(phy); 2045 phylink_info(pl, 2046 "PHY [%s] driver [%s] (irq=%s)\n", 2047 dev_name(&phy->mdio.dev), phy->drv->name, irq_str); 2048 kfree(irq_str); 2049 2050 mutex_lock(&phy->lock); 2051 mutex_lock(&pl->state_mutex); 2052 pl->phydev = phy; 2053 pl->phy_state.interface = interface; 2054 pl->phy_state.pause = MLO_PAUSE_NONE; 2055 pl->phy_state.speed = SPEED_UNKNOWN; 2056 pl->phy_state.duplex = DUPLEX_UNKNOWN; 2057 pl->phy_state.rate_matching = RATE_MATCH_NONE; 2058 linkmode_copy(pl->supported, supported); 2059 linkmode_copy(pl->link_config.advertising, config.advertising); 2060 2061 /* Restrict the phy advertisement according to the MAC support. */ 2062 linkmode_copy(phy->advertising, config.advertising); 2063 2064 /* If the MAC supports phylink managed EEE, restrict the EEE 2065 * advertisement according to the MAC's LPI capabilities. 2066 */ 2067 if (pl->mac_supports_eee) { 2068 /* If EEE is enabled, then we need to call phy_support_eee() 2069 * to ensure that the advertising mask is appropriately set. 2070 * This also enables EEE at the PHY. 2071 */ 2072 if (pl->eee_cfg.eee_enabled) 2073 phy_support_eee(phy); 2074 2075 phy->eee_cfg.tx_lpi_enabled = pl->eee_cfg.tx_lpi_enabled; 2076 phy->eee_cfg.tx_lpi_timer = pl->eee_cfg.tx_lpi_timer; 2077 2078 /* Convert the MAC's LPI capabilities to linkmodes */ 2079 linkmode_zero(pl->supported_lpi); 2080 phylink_caps_to_linkmodes(pl->supported_lpi, 2081 pl->config->lpi_capabilities); 2082 2083 /* Restrict the PHYs EEE support/advertisement to the modes 2084 * that the MAC supports. 2085 */ 2086 linkmode_and(phy->advertising_eee, phy->advertising_eee, 2087 pl->supported_lpi); 2088 } else if (pl->mac_supports_eee_ops) { 2089 /* MAC supports phylink EEE, but wants EEE always disabled. */ 2090 phy_disable_eee(phy); 2091 } 2092 2093 mutex_unlock(&pl->state_mutex); 2094 mutex_unlock(&phy->lock); 2095 2096 phylink_dbg(pl, 2097 "phy: %s setting supported %*pb advertising %*pb\n", 2098 phy_modes(interface), 2099 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported, 2100 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising); 2101 2102 if (phy_interrupt_is_valid(phy)) 2103 phy_request_interrupt(phy); 2104 2105 if (pl->config->mac_managed_pm) 2106 phy->mac_managed_pm = true; 2107 2108 /* Allow the MAC to stop its clock if the PHY has the capability */ 2109 pl->mac_tx_clk_stop = phy_eee_tx_clock_stop_capable(phy) > 0; 2110 2111 if (pl->mac_supports_eee_ops) { 2112 /* Explicitly configure whether the PHY is allowed to stop it's 2113 * receive clock. 2114 */ 2115 ret = phy_eee_rx_clock_stop(phy, 2116 pl->config->eee_rx_clk_stop_enable); 2117 if (ret == -EOPNOTSUPP) 2118 ret = 0; 2119 } 2120 2121 return ret; 2122 } 2123 2124 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy, 2125 phy_interface_t interface) 2126 { 2127 u32 flags = 0; 2128 2129 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED || 2130 (pl->cfg_link_an_mode == MLO_AN_INBAND && 2131 phy_interface_mode_is_8023z(interface) && !pl->sfp_bus))) 2132 return -EINVAL; 2133 2134 if (pl->phydev) 2135 return -EBUSY; 2136 2137 if (pl->config->mac_requires_rxc) 2138 flags |= PHY_F_RXC_ALWAYS_ON; 2139 2140 return phy_attach_direct(pl->netdev, phy, flags, interface); 2141 } 2142 2143 /** 2144 * phylink_connect_phy() - connect a PHY to the phylink instance 2145 * @pl: a pointer to a &struct phylink returned from phylink_create() 2146 * @phy: a pointer to a &struct phy_device. 2147 * 2148 * Connect @phy to the phylink instance specified by @pl by calling 2149 * phy_attach_direct(). Configure the @phy according to the MAC driver's 2150 * capabilities, start the PHYLIB state machine and enable any interrupts 2151 * that the PHY supports. 2152 * 2153 * This updates the phylink's ethtool supported and advertising link mode 2154 * masks. 2155 * 2156 * Returns 0 on success or a negative errno. 2157 */ 2158 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy) 2159 { 2160 int ret; 2161 2162 /* Use PHY device/driver interface */ 2163 if (pl->link_interface == PHY_INTERFACE_MODE_NA) { 2164 pl->link_interface = phy->interface; 2165 pl->link_config.interface = pl->link_interface; 2166 } 2167 2168 ret = phylink_attach_phy(pl, phy, pl->link_interface); 2169 if (ret < 0) 2170 return ret; 2171 2172 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface); 2173 if (ret) 2174 phy_detach(phy); 2175 2176 return ret; 2177 } 2178 EXPORT_SYMBOL_GPL(phylink_connect_phy); 2179 2180 /** 2181 * phylink_of_phy_connect() - connect the PHY specified in the DT mode. 2182 * @pl: a pointer to a &struct phylink returned from phylink_create() 2183 * @dn: a pointer to a &struct device_node. 2184 * @flags: PHY-specific flags to communicate to the PHY device driver 2185 * 2186 * Connect the phy specified in the device node @dn to the phylink instance 2187 * specified by @pl. Actions specified in phylink_connect_phy() will be 2188 * performed. 2189 * 2190 * Returns 0 on success or a negative errno. 2191 */ 2192 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn, 2193 u32 flags) 2194 { 2195 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags); 2196 } 2197 EXPORT_SYMBOL_GPL(phylink_of_phy_connect); 2198 2199 /** 2200 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode. 2201 * @pl: a pointer to a &struct phylink returned from phylink_create() 2202 * @fwnode: a pointer to a &struct fwnode_handle. 2203 * @flags: PHY-specific flags to communicate to the PHY device driver 2204 * 2205 * Connect the phy specified @fwnode to the phylink instance specified 2206 * by @pl. 2207 * 2208 * Returns 0 on success or a negative errno. 2209 */ 2210 int phylink_fwnode_phy_connect(struct phylink *pl, 2211 const struct fwnode_handle *fwnode, 2212 u32 flags) 2213 { 2214 struct fwnode_handle *phy_fwnode; 2215 struct phy_device *phy_dev; 2216 int ret; 2217 2218 /* Fixed links and 802.3z are handled without needing a PHY */ 2219 if (pl->cfg_link_an_mode == MLO_AN_FIXED || 2220 (pl->cfg_link_an_mode == MLO_AN_INBAND && 2221 phy_interface_mode_is_8023z(pl->link_interface))) 2222 return 0; 2223 2224 phy_fwnode = fwnode_get_phy_node(fwnode); 2225 if (IS_ERR(phy_fwnode)) { 2226 if (pl->cfg_link_an_mode == MLO_AN_PHY) 2227 return -ENODEV; 2228 return 0; 2229 } 2230 2231 phy_dev = fwnode_phy_find_device(phy_fwnode); 2232 /* We're done with the phy_node handle */ 2233 fwnode_handle_put(phy_fwnode); 2234 if (!phy_dev) 2235 return -ENODEV; 2236 2237 /* Use PHY device/driver interface */ 2238 if (pl->link_interface == PHY_INTERFACE_MODE_NA) { 2239 pl->link_interface = phy_dev->interface; 2240 pl->link_config.interface = pl->link_interface; 2241 } 2242 2243 if (pl->config->mac_requires_rxc) 2244 flags |= PHY_F_RXC_ALWAYS_ON; 2245 2246 ret = phy_attach_direct(pl->netdev, phy_dev, flags, 2247 pl->link_interface); 2248 phy_device_free(phy_dev); 2249 if (ret) 2250 return ret; 2251 2252 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface); 2253 if (ret) 2254 phy_detach(phy_dev); 2255 2256 return ret; 2257 } 2258 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect); 2259 2260 /** 2261 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink 2262 * instance. 2263 * @pl: a pointer to a &struct phylink returned from phylink_create() 2264 * 2265 * Disconnect any current PHY from the phylink instance described by @pl. 2266 */ 2267 void phylink_disconnect_phy(struct phylink *pl) 2268 { 2269 struct phy_device *phy; 2270 2271 ASSERT_RTNL(); 2272 2273 phy = pl->phydev; 2274 if (phy) { 2275 mutex_lock(&phy->lock); 2276 mutex_lock(&pl->state_mutex); 2277 pl->phydev = NULL; 2278 pl->phy_enable_tx_lpi = false; 2279 pl->mac_tx_clk_stop = false; 2280 mutex_unlock(&pl->state_mutex); 2281 mutex_unlock(&phy->lock); 2282 flush_work(&pl->resolve); 2283 2284 phy_disconnect(phy); 2285 } 2286 } 2287 EXPORT_SYMBOL_GPL(phylink_disconnect_phy); 2288 2289 static void phylink_link_changed(struct phylink *pl, bool up, const char *what) 2290 { 2291 if (!up) 2292 pl->link_failed = true; 2293 phylink_run_resolve(pl); 2294 phylink_dbg(pl, "%s link %s\n", what, up ? "up" : "down"); 2295 } 2296 2297 /** 2298 * phylink_mac_change() - notify phylink of a change in MAC state 2299 * @pl: a pointer to a &struct phylink returned from phylink_create() 2300 * @up: indicates whether the link is currently up. 2301 * 2302 * The MAC driver should call this driver when the state of its link 2303 * changes (eg, link failure, new negotiation results, etc.) 2304 */ 2305 void phylink_mac_change(struct phylink *pl, bool up) 2306 { 2307 phylink_link_changed(pl, up, "mac"); 2308 } 2309 EXPORT_SYMBOL_GPL(phylink_mac_change); 2310 2311 /** 2312 * phylink_pcs_change() - notify phylink of a change to PCS link state 2313 * @pcs: pointer to &struct phylink_pcs 2314 * @up: indicates whether the link is currently up. 2315 * 2316 * The PCS driver should call this when the state of its link changes 2317 * (e.g. link failure, new negotiation results, etc.) Note: it should 2318 * not determine "up" by reading the BMSR. If in doubt about the link 2319 * state at interrupt time, then pass true if pcs_get_state() returns 2320 * the latched link-down state, otherwise pass false. 2321 */ 2322 void phylink_pcs_change(struct phylink_pcs *pcs, bool up) 2323 { 2324 struct phylink *pl = pcs->phylink; 2325 2326 if (pl) 2327 phylink_link_changed(pl, up, "pcs"); 2328 } 2329 EXPORT_SYMBOL_GPL(phylink_pcs_change); 2330 2331 static irqreturn_t phylink_link_handler(int irq, void *data) 2332 { 2333 struct phylink *pl = data; 2334 2335 phylink_run_resolve(pl); 2336 2337 return IRQ_HANDLED; 2338 } 2339 2340 /** 2341 * phylink_start() - start a phylink instance 2342 * @pl: a pointer to a &struct phylink returned from phylink_create() 2343 * 2344 * Start the phylink instance specified by @pl, configuring the MAC for the 2345 * desired link mode(s) and negotiation style. This should be called from the 2346 * network device driver's &struct net_device_ops ndo_open() method. 2347 */ 2348 void phylink_start(struct phylink *pl) 2349 { 2350 bool poll = false; 2351 2352 ASSERT_RTNL(); 2353 2354 phylink_info(pl, "configuring for %s/%s link mode\n", 2355 phylink_an_mode_str(pl->req_link_an_mode), 2356 phy_modes(pl->link_config.interface)); 2357 2358 /* Always set the carrier off */ 2359 if (pl->netdev) 2360 netif_carrier_off(pl->netdev); 2361 2362 pl->pcs_state = PCS_STATE_STARTING; 2363 2364 /* Apply the link configuration to the MAC when starting. This allows 2365 * a fixed-link to start with the correct parameters, and also 2366 * ensures that we set the appropriate advertisement for Serdes links. 2367 * 2368 * Restart autonegotiation if using 802.3z to ensure that the link 2369 * parameters are properly negotiated. This is necessary for DSA 2370 * switches using 802.3z negotiation to ensure they see our modes. 2371 */ 2372 phylink_mac_initial_config(pl, true); 2373 2374 pl->pcs_state = PCS_STATE_STARTED; 2375 2376 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED); 2377 2378 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) { 2379 int irq = gpiod_to_irq(pl->link_gpio); 2380 2381 if (irq > 0) { 2382 if (!request_irq(irq, phylink_link_handler, 2383 IRQF_TRIGGER_RISING | 2384 IRQF_TRIGGER_FALLING, 2385 "netdev link", pl)) 2386 pl->link_irq = irq; 2387 else 2388 irq = 0; 2389 } 2390 if (irq <= 0) 2391 poll = true; 2392 } 2393 2394 if (pl->cfg_link_an_mode == MLO_AN_FIXED) 2395 poll |= pl->config->poll_fixed_state; 2396 2397 if (poll) 2398 mod_timer(&pl->link_poll, jiffies + HZ); 2399 if (pl->phydev) 2400 phy_start(pl->phydev); 2401 if (pl->sfp_bus) 2402 sfp_upstream_start(pl->sfp_bus); 2403 } 2404 EXPORT_SYMBOL_GPL(phylink_start); 2405 2406 /** 2407 * phylink_stop() - stop a phylink instance 2408 * @pl: a pointer to a &struct phylink returned from phylink_create() 2409 * 2410 * Stop the phylink instance specified by @pl. This should be called from the 2411 * network device driver's &struct net_device_ops ndo_stop() method. The 2412 * network device's carrier state should not be changed prior to calling this 2413 * function. 2414 * 2415 * This will synchronously bring down the link if the link is not already 2416 * down (in other words, it will trigger a mac_link_down() method call.) 2417 */ 2418 void phylink_stop(struct phylink *pl) 2419 { 2420 ASSERT_RTNL(); 2421 2422 if (pl->sfp_bus) 2423 sfp_upstream_stop(pl->sfp_bus); 2424 if (pl->phydev) 2425 phy_stop(pl->phydev); 2426 del_timer_sync(&pl->link_poll); 2427 if (pl->link_irq) { 2428 free_irq(pl->link_irq, pl); 2429 pl->link_irq = 0; 2430 } 2431 2432 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED); 2433 2434 pl->pcs_state = PCS_STATE_DOWN; 2435 2436 phylink_pcs_disable(pl->pcs); 2437 } 2438 EXPORT_SYMBOL_GPL(phylink_stop); 2439 2440 /** 2441 * phylink_suspend() - handle a network device suspend event 2442 * @pl: a pointer to a &struct phylink returned from phylink_create() 2443 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan 2444 * 2445 * Handle a network device suspend event. There are several cases: 2446 * 2447 * - If Wake-on-Lan is not active, we can bring down the link between 2448 * the MAC and PHY by calling phylink_stop(). 2449 * - If Wake-on-Lan is active, and being handled only by the PHY, we 2450 * can also bring down the link between the MAC and PHY. 2451 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC 2452 * still needs to receive packets, so we can not bring the link down. 2453 */ 2454 void phylink_suspend(struct phylink *pl, bool mac_wol) 2455 { 2456 ASSERT_RTNL(); 2457 2458 if (mac_wol && (!pl->netdev || pl->netdev->ethtool->wol_enabled)) { 2459 /* Wake-on-Lan enabled, MAC handling */ 2460 mutex_lock(&pl->state_mutex); 2461 2462 /* Stop the resolver bringing the link up */ 2463 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state); 2464 2465 /* Disable the carrier, to prevent transmit timeouts, 2466 * but one would hope all packets have been sent. This 2467 * also means phylink_resolve() will do nothing. 2468 */ 2469 if (pl->netdev) 2470 netif_carrier_off(pl->netdev); 2471 else 2472 pl->old_link_state = false; 2473 2474 /* We do not call mac_link_down() here as we want the 2475 * link to remain up to receive the WoL packets. 2476 */ 2477 mutex_unlock(&pl->state_mutex); 2478 } else { 2479 phylink_stop(pl); 2480 } 2481 } 2482 EXPORT_SYMBOL_GPL(phylink_suspend); 2483 2484 /** 2485 * phylink_resume() - handle a network device resume event 2486 * @pl: a pointer to a &struct phylink returned from phylink_create() 2487 * 2488 * Undo the effects of phylink_suspend(), returning the link to an 2489 * operational state. 2490 */ 2491 void phylink_resume(struct phylink *pl) 2492 { 2493 ASSERT_RTNL(); 2494 2495 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) { 2496 /* Wake-on-Lan enabled, MAC handling */ 2497 2498 /* Call mac_link_down() so we keep the overall state balanced. 2499 * Do this under the state_mutex lock for consistency. This 2500 * will cause a "Link Down" message to be printed during 2501 * resume, which is harmless - the true link state will be 2502 * printed when we run a resolve. 2503 */ 2504 mutex_lock(&pl->state_mutex); 2505 phylink_link_down(pl); 2506 mutex_unlock(&pl->state_mutex); 2507 2508 /* Re-apply the link parameters so that all the settings get 2509 * restored to the MAC. 2510 */ 2511 phylink_mac_initial_config(pl, true); 2512 2513 /* Re-enable and re-resolve the link parameters */ 2514 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL); 2515 } else { 2516 phylink_start(pl); 2517 } 2518 } 2519 EXPORT_SYMBOL_GPL(phylink_resume); 2520 2521 /** 2522 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY 2523 * @pl: a pointer to a &struct phylink returned from phylink_create() 2524 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters 2525 * 2526 * Read the wake on lan parameters from the PHY attached to the phylink 2527 * instance specified by @pl. If no PHY is currently attached, report no 2528 * support for wake on lan. 2529 */ 2530 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol) 2531 { 2532 ASSERT_RTNL(); 2533 2534 wol->supported = 0; 2535 wol->wolopts = 0; 2536 2537 if (pl->phydev) 2538 phy_ethtool_get_wol(pl->phydev, wol); 2539 } 2540 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol); 2541 2542 /** 2543 * phylink_ethtool_set_wol() - set wake on lan parameters 2544 * @pl: a pointer to a &struct phylink returned from phylink_create() 2545 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters 2546 * 2547 * Set the wake on lan parameters for the PHY attached to the phylink 2548 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP 2549 * error. 2550 * 2551 * Returns zero on success or negative errno code. 2552 */ 2553 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol) 2554 { 2555 int ret = -EOPNOTSUPP; 2556 2557 ASSERT_RTNL(); 2558 2559 if (pl->phydev) 2560 ret = phy_ethtool_set_wol(pl->phydev, wol); 2561 2562 return ret; 2563 } 2564 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol); 2565 2566 static phy_interface_t phylink_sfp_select_interface(struct phylink *pl, 2567 const unsigned long *link_modes) 2568 { 2569 phy_interface_t interface; 2570 2571 interface = sfp_select_interface(pl->sfp_bus, link_modes); 2572 if (interface == PHY_INTERFACE_MODE_NA) { 2573 phylink_err(pl, 2574 "selection of interface failed, advertisement %*pb\n", 2575 __ETHTOOL_LINK_MODE_MASK_NBITS, 2576 link_modes); 2577 return interface; 2578 } 2579 2580 if (!test_bit(interface, pl->config->supported_interfaces)) { 2581 phylink_err(pl, 2582 "selection of interface failed, SFP selected %s (%u) but MAC supports %*pbl\n", 2583 phy_modes(interface), interface, 2584 (int)PHY_INTERFACE_MODE_MAX, 2585 pl->config->supported_interfaces); 2586 return PHY_INTERFACE_MODE_NA; 2587 } 2588 2589 return interface; 2590 } 2591 2592 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b) 2593 { 2594 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask); 2595 2596 linkmode_zero(mask); 2597 phylink_set_port_modes(mask); 2598 2599 linkmode_and(dst, dst, mask); 2600 linkmode_or(dst, dst, b); 2601 } 2602 2603 static void phylink_get_ksettings(const struct phylink_link_state *state, 2604 struct ethtool_link_ksettings *kset) 2605 { 2606 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising); 2607 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising); 2608 if (kset->base.rate_matching == RATE_MATCH_NONE) { 2609 kset->base.speed = state->speed; 2610 kset->base.duplex = state->duplex; 2611 } 2612 kset->base.autoneg = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 2613 state->advertising) ? 2614 AUTONEG_ENABLE : AUTONEG_DISABLE; 2615 } 2616 2617 /** 2618 * phylink_ethtool_ksettings_get() - get the current link settings 2619 * @pl: a pointer to a &struct phylink returned from phylink_create() 2620 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings 2621 * 2622 * Read the current link settings for the phylink instance specified by @pl. 2623 * This will be the link settings read from the MAC, PHY or fixed link 2624 * settings depending on the current negotiation mode. 2625 */ 2626 int phylink_ethtool_ksettings_get(struct phylink *pl, 2627 struct ethtool_link_ksettings *kset) 2628 { 2629 struct phylink_link_state link_state; 2630 2631 ASSERT_RTNL(); 2632 2633 if (pl->phydev) 2634 phy_ethtool_ksettings_get(pl->phydev, kset); 2635 else 2636 kset->base.port = pl->link_port; 2637 2638 linkmode_copy(kset->link_modes.supported, pl->supported); 2639 2640 switch (pl->act_link_an_mode) { 2641 case MLO_AN_FIXED: 2642 /* We are using fixed settings. Report these as the 2643 * current link settings - and note that these also 2644 * represent the supported speeds/duplex/pause modes. 2645 */ 2646 phylink_get_fixed_state(pl, &link_state); 2647 phylink_get_ksettings(&link_state, kset); 2648 break; 2649 2650 case MLO_AN_INBAND: 2651 /* If there is a phy attached, then use the reported 2652 * settings from the phy with no modification. 2653 */ 2654 if (pl->phydev) 2655 break; 2656 2657 phylink_mac_pcs_get_state(pl, &link_state); 2658 2659 /* The MAC is reporting the link results from its own PCS 2660 * layer via in-band status. Report these as the current 2661 * link settings. 2662 */ 2663 phylink_get_ksettings(&link_state, kset); 2664 break; 2665 } 2666 2667 return 0; 2668 } 2669 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get); 2670 2671 static bool phylink_validate_pcs_inband_autoneg(struct phylink *pl, 2672 phy_interface_t interface, 2673 unsigned long *adv) 2674 { 2675 unsigned int inband = phylink_inband_caps(pl, interface); 2676 unsigned int mask; 2677 2678 /* If the PCS doesn't implement inband support, be permissive. */ 2679 if (!inband) 2680 return true; 2681 2682 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv)) 2683 mask = LINK_INBAND_ENABLE; 2684 else 2685 mask = LINK_INBAND_DISABLE; 2686 2687 /* Check whether the PCS implements the required mode */ 2688 return !!(inband & mask); 2689 } 2690 2691 /** 2692 * phylink_ethtool_ksettings_set() - set the link settings 2693 * @pl: a pointer to a &struct phylink returned from phylink_create() 2694 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes 2695 */ 2696 int phylink_ethtool_ksettings_set(struct phylink *pl, 2697 const struct ethtool_link_ksettings *kset) 2698 { 2699 __ETHTOOL_DECLARE_LINK_MODE_MASK(support); 2700 const struct link_capabilities *c; 2701 struct phylink_link_state config; 2702 2703 ASSERT_RTNL(); 2704 2705 if (pl->phydev) { 2706 struct ethtool_link_ksettings phy_kset = *kset; 2707 2708 linkmode_and(phy_kset.link_modes.advertising, 2709 phy_kset.link_modes.advertising, 2710 pl->supported); 2711 2712 /* We can rely on phylib for this update; we also do not need 2713 * to update the pl->link_config settings: 2714 * - the configuration returned via ksettings_get() will come 2715 * from phylib whenever a PHY is present. 2716 * - link_config.interface will be updated by the PHY calling 2717 * back via phylink_phy_change() and a subsequent resolve. 2718 * - initial link configuration for PHY mode comes from the 2719 * last phy state updated via phylink_phy_change(). 2720 * - other configuration changes (e.g. pause modes) are 2721 * performed directly via phylib. 2722 * - if in in-band mode with a PHY, the link configuration 2723 * is passed on the link from the PHY, and all of 2724 * link_config.{speed,duplex,an_enabled,pause} are not used. 2725 * - the only possible use would be link_config.advertising 2726 * pause modes when in 1000base-X mode with a PHY, but in 2727 * the presence of a PHY, this should not be changed as that 2728 * should be determined from the media side advertisement. 2729 */ 2730 return phy_ethtool_ksettings_set(pl->phydev, &phy_kset); 2731 } 2732 2733 config = pl->link_config; 2734 /* Mask out unsupported advertisements */ 2735 linkmode_and(config.advertising, kset->link_modes.advertising, 2736 pl->supported); 2737 2738 /* FIXME: should we reject autoneg if phy/mac does not support it? */ 2739 switch (kset->base.autoneg) { 2740 case AUTONEG_DISABLE: 2741 /* Autonegotiation disabled, select a suitable speed and 2742 * duplex. 2743 */ 2744 c = phy_caps_lookup(kset->base.speed, kset->base.duplex, 2745 pl->supported, false); 2746 if (!c) 2747 return -EINVAL; 2748 2749 /* If we have a fixed link, refuse to change link parameters. 2750 * If the link parameters match, accept them but do nothing. 2751 */ 2752 if (pl->req_link_an_mode == MLO_AN_FIXED) { 2753 if (c->speed != pl->link_config.speed || 2754 c->duplex != pl->link_config.duplex) 2755 return -EINVAL; 2756 return 0; 2757 } 2758 2759 config.speed = c->speed; 2760 config.duplex = c->duplex; 2761 break; 2762 2763 case AUTONEG_ENABLE: 2764 /* If we have a fixed link, allow autonegotiation (since that 2765 * is our default case) but do not allow the advertisement to 2766 * be changed. If the advertisement matches, simply return. 2767 */ 2768 if (pl->req_link_an_mode == MLO_AN_FIXED) { 2769 if (!linkmode_equal(config.advertising, 2770 pl->link_config.advertising)) 2771 return -EINVAL; 2772 return 0; 2773 } 2774 2775 config.speed = SPEED_UNKNOWN; 2776 config.duplex = DUPLEX_UNKNOWN; 2777 break; 2778 2779 default: 2780 return -EINVAL; 2781 } 2782 2783 /* We have ruled out the case with a PHY attached, and the 2784 * fixed-link cases. All that is left are in-band links. 2785 */ 2786 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising, 2787 kset->base.autoneg == AUTONEG_ENABLE); 2788 2789 /* If this link is with an SFP, ensure that changes to advertised modes 2790 * also cause the associated interface to be selected such that the 2791 * link can be configured correctly. 2792 */ 2793 if (pl->sfp_bus) { 2794 config.interface = phylink_sfp_select_interface(pl, 2795 config.advertising); 2796 if (config.interface == PHY_INTERFACE_MODE_NA) 2797 return -EINVAL; 2798 2799 /* Revalidate with the selected interface */ 2800 linkmode_copy(support, pl->supported); 2801 if (phylink_validate(pl, support, &config)) { 2802 phylink_err(pl, "validation of %s/%s with support %*pb failed\n", 2803 phylink_an_mode_str(pl->req_link_an_mode), 2804 phy_modes(config.interface), 2805 __ETHTOOL_LINK_MODE_MASK_NBITS, support); 2806 return -EINVAL; 2807 } 2808 } else { 2809 /* Validate without changing the current supported mask. */ 2810 linkmode_copy(support, pl->supported); 2811 if (phylink_validate(pl, support, &config)) 2812 return -EINVAL; 2813 } 2814 2815 /* If autonegotiation is enabled, we must have an advertisement */ 2816 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 2817 config.advertising) && 2818 phylink_is_empty_linkmode(config.advertising)) 2819 return -EINVAL; 2820 2821 /* Validate the autonegotiation state. We don't have a PHY in this 2822 * situation, so the PCS is the media-facing entity. 2823 */ 2824 if (!phylink_validate_pcs_inband_autoneg(pl, config.interface, 2825 config.advertising)) 2826 return -EINVAL; 2827 2828 mutex_lock(&pl->state_mutex); 2829 pl->link_config.speed = config.speed; 2830 pl->link_config.duplex = config.duplex; 2831 2832 if (pl->link_config.interface != config.interface) { 2833 /* The interface changed, e.g. 1000base-X <-> 2500base-X */ 2834 /* We need to force the link down, then change the interface */ 2835 if (pl->old_link_state) { 2836 phylink_link_down(pl); 2837 pl->old_link_state = false; 2838 } 2839 if (!test_bit(PHYLINK_DISABLE_STOPPED, 2840 &pl->phylink_disable_state)) 2841 phylink_major_config(pl, false, &config); 2842 pl->link_config.interface = config.interface; 2843 linkmode_copy(pl->link_config.advertising, config.advertising); 2844 } else if (!linkmode_equal(pl->link_config.advertising, 2845 config.advertising)) { 2846 linkmode_copy(pl->link_config.advertising, config.advertising); 2847 phylink_change_inband_advert(pl); 2848 } 2849 mutex_unlock(&pl->state_mutex); 2850 2851 return 0; 2852 } 2853 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set); 2854 2855 /** 2856 * phylink_ethtool_nway_reset() - restart negotiation 2857 * @pl: a pointer to a &struct phylink returned from phylink_create() 2858 * 2859 * Restart negotiation for the phylink instance specified by @pl. This will 2860 * cause any attached phy to restart negotiation with the link partner, and 2861 * if the MAC is in a BaseX mode, the MAC will also be requested to restart 2862 * negotiation. 2863 * 2864 * Returns zero on success, or negative error code. 2865 */ 2866 int phylink_ethtool_nway_reset(struct phylink *pl) 2867 { 2868 int ret = 0; 2869 2870 ASSERT_RTNL(); 2871 2872 if (pl->phydev) 2873 ret = phy_restart_aneg(pl->phydev); 2874 phylink_pcs_an_restart(pl); 2875 2876 return ret; 2877 } 2878 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset); 2879 2880 /** 2881 * phylink_ethtool_get_pauseparam() - get the current pause parameters 2882 * @pl: a pointer to a &struct phylink returned from phylink_create() 2883 * @pause: a pointer to a &struct ethtool_pauseparam 2884 */ 2885 void phylink_ethtool_get_pauseparam(struct phylink *pl, 2886 struct ethtool_pauseparam *pause) 2887 { 2888 ASSERT_RTNL(); 2889 2890 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN); 2891 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX); 2892 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX); 2893 } 2894 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam); 2895 2896 /** 2897 * phylink_ethtool_set_pauseparam() - set the current pause parameters 2898 * @pl: a pointer to a &struct phylink returned from phylink_create() 2899 * @pause: a pointer to a &struct ethtool_pauseparam 2900 */ 2901 int phylink_ethtool_set_pauseparam(struct phylink *pl, 2902 struct ethtool_pauseparam *pause) 2903 { 2904 struct phylink_link_state *config = &pl->link_config; 2905 bool manual_changed; 2906 int pause_state; 2907 2908 ASSERT_RTNL(); 2909 2910 if (pl->req_link_an_mode == MLO_AN_FIXED) 2911 return -EOPNOTSUPP; 2912 2913 if (!phylink_test(pl->supported, Pause) && 2914 !phylink_test(pl->supported, Asym_Pause)) 2915 return -EOPNOTSUPP; 2916 2917 if (!phylink_test(pl->supported, Asym_Pause) && 2918 pause->rx_pause != pause->tx_pause) 2919 return -EINVAL; 2920 2921 pause_state = 0; 2922 if (pause->autoneg) 2923 pause_state |= MLO_PAUSE_AN; 2924 if (pause->rx_pause) 2925 pause_state |= MLO_PAUSE_RX; 2926 if (pause->tx_pause) 2927 pause_state |= MLO_PAUSE_TX; 2928 2929 mutex_lock(&pl->state_mutex); 2930 /* 2931 * See the comments for linkmode_set_pause(), wrt the deficiencies 2932 * with the current implementation. A solution to this issue would 2933 * be: 2934 * ethtool Local device 2935 * rx tx Pause AsymDir 2936 * 0 0 0 0 2937 * 1 0 1 1 2938 * 0 1 0 1 2939 * 1 1 1 1 2940 * and then use the ethtool rx/tx enablement status to mask the 2941 * rx/tx pause resolution. 2942 */ 2943 linkmode_set_pause(config->advertising, pause->tx_pause, 2944 pause->rx_pause); 2945 2946 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN || 2947 (!(pause_state & MLO_PAUSE_AN) && 2948 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK); 2949 2950 config->pause = pause_state; 2951 2952 /* Update our in-band advertisement, triggering a renegotiation if 2953 * the advertisement changed. 2954 */ 2955 if (!pl->phydev) 2956 phylink_change_inband_advert(pl); 2957 2958 mutex_unlock(&pl->state_mutex); 2959 2960 /* If we have a PHY, a change of the pause frame advertisement will 2961 * cause phylib to renegotiate (if AN is enabled) which will in turn 2962 * call our phylink_phy_change() and trigger a resolve. Note that 2963 * we can't hold our state mutex while calling phy_set_asym_pause(). 2964 */ 2965 if (pl->phydev) 2966 phy_set_asym_pause(pl->phydev, pause->rx_pause, 2967 pause->tx_pause); 2968 2969 /* If the manual pause settings changed, make sure we trigger a 2970 * resolve to update their state; we can not guarantee that the 2971 * link will cycle. 2972 */ 2973 if (manual_changed) { 2974 pl->link_failed = true; 2975 phylink_run_resolve(pl); 2976 } 2977 2978 return 0; 2979 } 2980 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam); 2981 2982 /** 2983 * phylink_get_eee_err() - read the energy efficient ethernet error 2984 * counter 2985 * @pl: a pointer to a &struct phylink returned from phylink_create(). 2986 * 2987 * Read the Energy Efficient Ethernet error counter from the PHY associated 2988 * with the phylink instance specified by @pl. 2989 * 2990 * Returns positive error counter value, or negative error code. 2991 */ 2992 int phylink_get_eee_err(struct phylink *pl) 2993 { 2994 int ret = 0; 2995 2996 ASSERT_RTNL(); 2997 2998 if (pl->phydev) 2999 ret = phy_get_eee_err(pl->phydev); 3000 3001 return ret; 3002 } 3003 EXPORT_SYMBOL_GPL(phylink_get_eee_err); 3004 3005 /** 3006 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters 3007 * @pl: a pointer to a &struct phylink returned from phylink_create() 3008 * @eee: a pointer to a &struct ethtool_keee for the read parameters 3009 */ 3010 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_keee *eee) 3011 { 3012 int ret = -EOPNOTSUPP; 3013 3014 ASSERT_RTNL(); 3015 3016 if (pl->mac_supports_eee_ops && !pl->mac_supports_eee) 3017 return ret; 3018 3019 if (pl->phydev) { 3020 ret = phy_ethtool_get_eee(pl->phydev, eee); 3021 /* Restrict supported linkmode mask */ 3022 if (ret == 0 && pl->mac_supports_eee_ops) 3023 linkmode_and(eee->supported, eee->supported, 3024 pl->supported_lpi); 3025 } 3026 3027 return ret; 3028 } 3029 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee); 3030 3031 /** 3032 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters 3033 * @pl: a pointer to a &struct phylink returned from phylink_create() 3034 * @eee: a pointer to a &struct ethtool_keee for the desired parameters 3035 */ 3036 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_keee *eee) 3037 { 3038 bool mac_eee = pl->mac_supports_eee; 3039 int ret = -EOPNOTSUPP; 3040 3041 ASSERT_RTNL(); 3042 3043 phylink_dbg(pl, "mac %s phylink EEE%s, adv %*pbl, LPI%s timer %uus\n", 3044 mac_eee ? "supports" : "does not support", 3045 eee->eee_enabled ? ", enabled" : "", 3046 __ETHTOOL_LINK_MODE_MASK_NBITS, eee->advertised, 3047 eee->tx_lpi_enabled ? " enabled" : "", eee->tx_lpi_timer); 3048 3049 if (pl->mac_supports_eee_ops && !mac_eee) 3050 return ret; 3051 3052 if (pl->phydev) { 3053 /* Restrict advertisement mask */ 3054 if (pl->mac_supports_eee_ops) 3055 linkmode_and(eee->advertised, eee->advertised, 3056 pl->supported_lpi); 3057 ret = phy_ethtool_set_eee(pl->phydev, eee); 3058 if (ret == 0) 3059 eee_to_eeecfg(&pl->eee_cfg, eee); 3060 } 3061 3062 return ret; 3063 } 3064 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee); 3065 3066 /* This emulates MII registers for a fixed-mode phy operating as per the 3067 * passed in state. "aneg" defines if we report negotiation is possible. 3068 * 3069 * FIXME: should deal with negotiation state too. 3070 */ 3071 static int phylink_mii_emul_read(unsigned int reg, 3072 struct phylink_link_state *state) 3073 { 3074 struct fixed_phy_status fs; 3075 unsigned long *lpa = state->lp_advertising; 3076 int val; 3077 3078 fs.link = state->link; 3079 fs.speed = state->speed; 3080 fs.duplex = state->duplex; 3081 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa); 3082 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa); 3083 3084 val = swphy_read_reg(reg, &fs); 3085 if (reg == MII_BMSR) { 3086 if (!state->an_complete) 3087 val &= ~BMSR_ANEGCOMPLETE; 3088 } 3089 return val; 3090 } 3091 3092 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id, 3093 unsigned int reg) 3094 { 3095 struct phy_device *phydev = pl->phydev; 3096 int prtad, devad; 3097 3098 if (mdio_phy_id_is_c45(phy_id)) { 3099 prtad = mdio_phy_id_prtad(phy_id); 3100 devad = mdio_phy_id_devad(phy_id); 3101 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad, 3102 reg); 3103 } 3104 3105 if (phydev->is_c45) { 3106 switch (reg) { 3107 case MII_BMCR: 3108 case MII_BMSR: 3109 case MII_PHYSID1: 3110 case MII_PHYSID2: 3111 devad = __ffs(phydev->c45_ids.mmds_present); 3112 break; 3113 case MII_ADVERTISE: 3114 case MII_LPA: 3115 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN)) 3116 return -EINVAL; 3117 devad = MDIO_MMD_AN; 3118 if (reg == MII_ADVERTISE) 3119 reg = MDIO_AN_ADVERTISE; 3120 else 3121 reg = MDIO_AN_LPA; 3122 break; 3123 default: 3124 return -EINVAL; 3125 } 3126 prtad = phy_id; 3127 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad, 3128 reg); 3129 } 3130 3131 return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg); 3132 } 3133 3134 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id, 3135 unsigned int reg, unsigned int val) 3136 { 3137 struct phy_device *phydev = pl->phydev; 3138 int prtad, devad; 3139 3140 if (mdio_phy_id_is_c45(phy_id)) { 3141 prtad = mdio_phy_id_prtad(phy_id); 3142 devad = mdio_phy_id_devad(phy_id); 3143 return mdiobus_c45_write(pl->phydev->mdio.bus, prtad, devad, 3144 reg, val); 3145 } 3146 3147 if (phydev->is_c45) { 3148 switch (reg) { 3149 case MII_BMCR: 3150 case MII_BMSR: 3151 case MII_PHYSID1: 3152 case MII_PHYSID2: 3153 devad = __ffs(phydev->c45_ids.mmds_present); 3154 break; 3155 case MII_ADVERTISE: 3156 case MII_LPA: 3157 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN)) 3158 return -EINVAL; 3159 devad = MDIO_MMD_AN; 3160 if (reg == MII_ADVERTISE) 3161 reg = MDIO_AN_ADVERTISE; 3162 else 3163 reg = MDIO_AN_LPA; 3164 break; 3165 default: 3166 return -EINVAL; 3167 } 3168 return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad, 3169 reg, val); 3170 } 3171 3172 return mdiobus_write(phydev->mdio.bus, phy_id, reg, val); 3173 } 3174 3175 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id, 3176 unsigned int reg) 3177 { 3178 struct phylink_link_state state; 3179 int val = 0xffff; 3180 3181 switch (pl->act_link_an_mode) { 3182 case MLO_AN_FIXED: 3183 if (phy_id == 0) { 3184 phylink_get_fixed_state(pl, &state); 3185 val = phylink_mii_emul_read(reg, &state); 3186 } 3187 break; 3188 3189 case MLO_AN_PHY: 3190 return -EOPNOTSUPP; 3191 3192 case MLO_AN_INBAND: 3193 if (phy_id == 0) { 3194 phylink_mac_pcs_get_state(pl, &state); 3195 val = phylink_mii_emul_read(reg, &state); 3196 } 3197 break; 3198 } 3199 3200 return val & 0xffff; 3201 } 3202 3203 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id, 3204 unsigned int reg, unsigned int val) 3205 { 3206 switch (pl->act_link_an_mode) { 3207 case MLO_AN_FIXED: 3208 break; 3209 3210 case MLO_AN_PHY: 3211 return -EOPNOTSUPP; 3212 3213 case MLO_AN_INBAND: 3214 break; 3215 } 3216 3217 return 0; 3218 } 3219 3220 /** 3221 * phylink_mii_ioctl() - generic mii ioctl interface 3222 * @pl: a pointer to a &struct phylink returned from phylink_create() 3223 * @ifr: a pointer to a &struct ifreq for socket ioctls 3224 * @cmd: ioctl cmd to execute 3225 * 3226 * Perform the specified MII ioctl on the PHY attached to the phylink instance 3227 * specified by @pl. If no PHY is attached, emulate the presence of the PHY. 3228 * 3229 * Returns: zero on success or negative error code. 3230 * 3231 * %SIOCGMIIPHY: 3232 * read register from the current PHY. 3233 * %SIOCGMIIREG: 3234 * read register from the specified PHY. 3235 * %SIOCSMIIREG: 3236 * set a register on the specified PHY. 3237 */ 3238 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd) 3239 { 3240 struct mii_ioctl_data *mii = if_mii(ifr); 3241 int ret; 3242 3243 ASSERT_RTNL(); 3244 3245 if (pl->phydev) { 3246 /* PHYs only exist for MLO_AN_PHY and SGMII */ 3247 switch (cmd) { 3248 case SIOCGMIIPHY: 3249 mii->phy_id = pl->phydev->mdio.addr; 3250 fallthrough; 3251 3252 case SIOCGMIIREG: 3253 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num); 3254 if (ret >= 0) { 3255 mii->val_out = ret; 3256 ret = 0; 3257 } 3258 break; 3259 3260 case SIOCSMIIREG: 3261 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num, 3262 mii->val_in); 3263 break; 3264 3265 default: 3266 ret = phy_mii_ioctl(pl->phydev, ifr, cmd); 3267 break; 3268 } 3269 } else { 3270 switch (cmd) { 3271 case SIOCGMIIPHY: 3272 mii->phy_id = 0; 3273 fallthrough; 3274 3275 case SIOCGMIIREG: 3276 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num); 3277 if (ret >= 0) { 3278 mii->val_out = ret; 3279 ret = 0; 3280 } 3281 break; 3282 3283 case SIOCSMIIREG: 3284 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num, 3285 mii->val_in); 3286 break; 3287 3288 default: 3289 ret = -EOPNOTSUPP; 3290 break; 3291 } 3292 } 3293 3294 return ret; 3295 } 3296 EXPORT_SYMBOL_GPL(phylink_mii_ioctl); 3297 3298 /** 3299 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both 3300 * link partners 3301 * @pl: a pointer to a &struct phylink returned from phylink_create() 3302 * @sync: perform action synchronously 3303 * 3304 * If we have a PHY that is not part of a SFP module, then set the speed 3305 * as described in the phy_speed_down() function. Please see this function 3306 * for a description of the @sync parameter. 3307 * 3308 * Returns zero if there is no PHY, otherwise as per phy_speed_down(). 3309 */ 3310 int phylink_speed_down(struct phylink *pl, bool sync) 3311 { 3312 int ret = 0; 3313 3314 ASSERT_RTNL(); 3315 3316 if (!pl->sfp_bus && pl->phydev) 3317 ret = phy_speed_down(pl->phydev, sync); 3318 3319 return ret; 3320 } 3321 EXPORT_SYMBOL_GPL(phylink_speed_down); 3322 3323 /** 3324 * phylink_speed_up() - restore the advertised speeds prior to the call to 3325 * phylink_speed_down() 3326 * @pl: a pointer to a &struct phylink returned from phylink_create() 3327 * 3328 * If we have a PHY that is not part of a SFP module, then restore the 3329 * PHY speeds as per phy_speed_up(). 3330 * 3331 * Returns zero if there is no PHY, otherwise as per phy_speed_up(). 3332 */ 3333 int phylink_speed_up(struct phylink *pl) 3334 { 3335 int ret = 0; 3336 3337 ASSERT_RTNL(); 3338 3339 if (!pl->sfp_bus && pl->phydev) 3340 ret = phy_speed_up(pl->phydev); 3341 3342 return ret; 3343 } 3344 EXPORT_SYMBOL_GPL(phylink_speed_up); 3345 3346 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus) 3347 { 3348 struct phylink *pl = upstream; 3349 3350 pl->netdev->sfp_bus = bus; 3351 } 3352 3353 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus) 3354 { 3355 struct phylink *pl = upstream; 3356 3357 pl->netdev->sfp_bus = NULL; 3358 } 3359 3360 static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl, 3361 const unsigned long *intf) 3362 { 3363 phy_interface_t interface; 3364 size_t i; 3365 3366 interface = PHY_INTERFACE_MODE_NA; 3367 for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++) 3368 if (test_bit(phylink_sfp_interface_preference[i], intf)) { 3369 interface = phylink_sfp_interface_preference[i]; 3370 break; 3371 } 3372 3373 return interface; 3374 } 3375 3376 static void phylink_sfp_set_config(struct phylink *pl, unsigned long *supported, 3377 struct phylink_link_state *state, 3378 bool changed) 3379 { 3380 u8 mode = MLO_AN_INBAND; 3381 3382 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n", 3383 phylink_an_mode_str(mode), phy_modes(state->interface), 3384 __ETHTOOL_LINK_MODE_MASK_NBITS, supported); 3385 3386 if (!linkmode_equal(pl->supported, supported)) { 3387 linkmode_copy(pl->supported, supported); 3388 changed = true; 3389 } 3390 3391 if (!linkmode_equal(pl->link_config.advertising, state->advertising)) { 3392 linkmode_copy(pl->link_config.advertising, state->advertising); 3393 changed = true; 3394 } 3395 3396 if (pl->req_link_an_mode != mode || 3397 pl->link_config.interface != state->interface) { 3398 pl->req_link_an_mode = mode; 3399 pl->link_config.interface = state->interface; 3400 3401 changed = true; 3402 3403 phylink_info(pl, "switched to %s/%s link mode\n", 3404 phylink_an_mode_str(mode), 3405 phy_modes(state->interface)); 3406 } 3407 3408 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED, 3409 &pl->phylink_disable_state)) 3410 phylink_mac_initial_config(pl, false); 3411 } 3412 3413 static int phylink_sfp_config_phy(struct phylink *pl, struct phy_device *phy) 3414 { 3415 __ETHTOOL_DECLARE_LINK_MODE_MASK(support); 3416 struct phylink_link_state config; 3417 int ret; 3418 3419 linkmode_copy(support, phy->supported); 3420 3421 memset(&config, 0, sizeof(config)); 3422 linkmode_copy(config.advertising, phy->advertising); 3423 config.interface = PHY_INTERFACE_MODE_NA; 3424 config.speed = SPEED_UNKNOWN; 3425 config.duplex = DUPLEX_UNKNOWN; 3426 config.pause = MLO_PAUSE_AN; 3427 3428 /* Ignore errors if we're expecting a PHY to attach later */ 3429 ret = phylink_validate(pl, support, &config); 3430 if (ret) { 3431 phylink_err(pl, "validation with support %*pb failed: %pe\n", 3432 __ETHTOOL_LINK_MODE_MASK_NBITS, support, 3433 ERR_PTR(ret)); 3434 return ret; 3435 } 3436 3437 config.interface = phylink_sfp_select_interface(pl, config.advertising); 3438 if (config.interface == PHY_INTERFACE_MODE_NA) 3439 return -EINVAL; 3440 3441 /* Attach the PHY so that the PHY is present when we do the major 3442 * configuration step. 3443 */ 3444 ret = phylink_attach_phy(pl, phy, config.interface); 3445 if (ret < 0) 3446 return ret; 3447 3448 /* This will validate the configuration for us. */ 3449 ret = phylink_bringup_phy(pl, phy, config.interface); 3450 if (ret < 0) { 3451 phy_detach(phy); 3452 return ret; 3453 } 3454 3455 pl->link_port = pl->sfp_port; 3456 3457 phylink_sfp_set_config(pl, support, &config, true); 3458 3459 return 0; 3460 } 3461 3462 static int phylink_sfp_config_optical(struct phylink *pl) 3463 { 3464 __ETHTOOL_DECLARE_LINK_MODE_MASK(support); 3465 DECLARE_PHY_INTERFACE_MASK(interfaces); 3466 struct phylink_link_state config; 3467 phy_interface_t interface; 3468 int ret; 3469 3470 phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n", 3471 (int)PHY_INTERFACE_MODE_MAX, 3472 pl->config->supported_interfaces, 3473 (int)PHY_INTERFACE_MODE_MAX, 3474 pl->sfp_interfaces); 3475 3476 /* Find the union of the supported interfaces by the PCS/MAC and 3477 * the SFP module. 3478 */ 3479 phy_interface_and(interfaces, pl->config->supported_interfaces, 3480 pl->sfp_interfaces); 3481 if (phy_interface_empty(interfaces)) { 3482 phylink_err(pl, "unsupported SFP module: no common interface modes\n"); 3483 return -EINVAL; 3484 } 3485 3486 memset(&config, 0, sizeof(config)); 3487 linkmode_copy(support, pl->sfp_support); 3488 linkmode_copy(config.advertising, pl->sfp_support); 3489 config.speed = SPEED_UNKNOWN; 3490 config.duplex = DUPLEX_UNKNOWN; 3491 config.pause = MLO_PAUSE_AN; 3492 3493 /* For all the interfaces that are supported, reduce the sfp_support 3494 * mask to only those link modes that can be supported. 3495 */ 3496 ret = phylink_validate_mask(pl, NULL, pl->sfp_support, &config, 3497 interfaces); 3498 if (ret) { 3499 phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n", 3500 __ETHTOOL_LINK_MODE_MASK_NBITS, support); 3501 return ret; 3502 } 3503 3504 interface = phylink_choose_sfp_interface(pl, interfaces); 3505 if (interface == PHY_INTERFACE_MODE_NA) { 3506 phylink_err(pl, "failed to select SFP interface\n"); 3507 return -EINVAL; 3508 } 3509 3510 phylink_dbg(pl, "optical SFP: chosen %s interface\n", 3511 phy_modes(interface)); 3512 3513 if (!phylink_validate_pcs_inband_autoneg(pl, interface, 3514 config.advertising)) { 3515 phylink_err(pl, "autoneg setting not compatible with PCS"); 3516 return -EINVAL; 3517 } 3518 3519 config.interface = interface; 3520 3521 /* Ignore errors if we're expecting a PHY to attach later */ 3522 ret = phylink_validate(pl, support, &config); 3523 if (ret) { 3524 phylink_err(pl, "validation with support %*pb failed: %pe\n", 3525 __ETHTOOL_LINK_MODE_MASK_NBITS, support, 3526 ERR_PTR(ret)); 3527 return ret; 3528 } 3529 3530 pl->link_port = pl->sfp_port; 3531 3532 phylink_sfp_set_config(pl, pl->sfp_support, &config, false); 3533 3534 return 0; 3535 } 3536 3537 static int phylink_sfp_module_insert(void *upstream, 3538 const struct sfp_eeprom_id *id) 3539 { 3540 struct phylink *pl = upstream; 3541 3542 ASSERT_RTNL(); 3543 3544 linkmode_zero(pl->sfp_support); 3545 phy_interface_zero(pl->sfp_interfaces); 3546 sfp_parse_support(pl->sfp_bus, id, pl->sfp_support, pl->sfp_interfaces); 3547 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, pl->sfp_support); 3548 3549 /* If this module may have a PHY connecting later, defer until later */ 3550 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id); 3551 if (pl->sfp_may_have_phy) 3552 return 0; 3553 3554 return phylink_sfp_config_optical(pl); 3555 } 3556 3557 static int phylink_sfp_module_start(void *upstream) 3558 { 3559 struct phylink *pl = upstream; 3560 3561 /* If this SFP module has a PHY, start the PHY now. */ 3562 if (pl->phydev) { 3563 phy_start(pl->phydev); 3564 return 0; 3565 } 3566 3567 /* If the module may have a PHY but we didn't detect one we 3568 * need to configure the MAC here. 3569 */ 3570 if (!pl->sfp_may_have_phy) 3571 return 0; 3572 3573 return phylink_sfp_config_optical(pl); 3574 } 3575 3576 static void phylink_sfp_module_stop(void *upstream) 3577 { 3578 struct phylink *pl = upstream; 3579 3580 /* If this SFP module has a PHY, stop it. */ 3581 if (pl->phydev) 3582 phy_stop(pl->phydev); 3583 } 3584 3585 static void phylink_sfp_link_down(void *upstream) 3586 { 3587 struct phylink *pl = upstream; 3588 3589 ASSERT_RTNL(); 3590 3591 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK); 3592 } 3593 3594 static void phylink_sfp_link_up(void *upstream) 3595 { 3596 struct phylink *pl = upstream; 3597 3598 ASSERT_RTNL(); 3599 3600 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK); 3601 } 3602 3603 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy) 3604 { 3605 struct phylink *pl = upstream; 3606 3607 if (!phy->drv) { 3608 phylink_err(pl, "PHY %s (id 0x%.8lx) has no driver loaded\n", 3609 phydev_name(phy), (unsigned long)phy->phy_id); 3610 phylink_err(pl, "Drivers which handle known common cases: CONFIG_BCM84881_PHY, CONFIG_MARVELL_PHY\n"); 3611 return -EINVAL; 3612 } 3613 3614 /* 3615 * This is the new way of dealing with flow control for PHYs, 3616 * as described by Timur Tabi in commit 529ed1275263 ("net: phy: 3617 * phy drivers should not set SUPPORTED_[Asym_]Pause") except 3618 * using our validate call to the MAC, we rely upon the MAC 3619 * clearing the bits from both supported and advertising fields. 3620 */ 3621 phy_support_asym_pause(phy); 3622 3623 /* Set the PHY's host supported interfaces */ 3624 phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces, 3625 pl->config->supported_interfaces); 3626 3627 /* Do the initial configuration */ 3628 return phylink_sfp_config_phy(pl, phy); 3629 } 3630 3631 static void phylink_sfp_disconnect_phy(void *upstream, 3632 struct phy_device *phydev) 3633 { 3634 phylink_disconnect_phy(upstream); 3635 } 3636 3637 static const struct sfp_upstream_ops sfp_phylink_ops = { 3638 .attach = phylink_sfp_attach, 3639 .detach = phylink_sfp_detach, 3640 .module_insert = phylink_sfp_module_insert, 3641 .module_start = phylink_sfp_module_start, 3642 .module_stop = phylink_sfp_module_stop, 3643 .link_up = phylink_sfp_link_up, 3644 .link_down = phylink_sfp_link_down, 3645 .connect_phy = phylink_sfp_connect_phy, 3646 .disconnect_phy = phylink_sfp_disconnect_phy, 3647 }; 3648 3649 /* Helpers for MAC drivers */ 3650 3651 static struct { 3652 int bit; 3653 int speed; 3654 } phylink_c73_priority_resolution[] = { 3655 { ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, SPEED_100000 }, 3656 { ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, SPEED_100000 }, 3657 /* 100GBASE-KP4 and 100GBASE-CR10 not supported */ 3658 { ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, SPEED_40000 }, 3659 { ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, SPEED_40000 }, 3660 { ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, SPEED_10000 }, 3661 { ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, SPEED_10000 }, 3662 /* 5GBASE-KR not supported */ 3663 { ETHTOOL_LINK_MODE_2500baseX_Full_BIT, SPEED_2500 }, 3664 { ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, SPEED_1000 }, 3665 }; 3666 3667 void phylink_resolve_c73(struct phylink_link_state *state) 3668 { 3669 int i; 3670 3671 for (i = 0; i < ARRAY_SIZE(phylink_c73_priority_resolution); i++) { 3672 int bit = phylink_c73_priority_resolution[i].bit; 3673 if (linkmode_test_bit(bit, state->advertising) && 3674 linkmode_test_bit(bit, state->lp_advertising)) 3675 break; 3676 } 3677 3678 if (i < ARRAY_SIZE(phylink_c73_priority_resolution)) { 3679 state->speed = phylink_c73_priority_resolution[i].speed; 3680 state->duplex = DUPLEX_FULL; 3681 } else { 3682 /* negotiation failure */ 3683 state->link = false; 3684 } 3685 3686 phylink_resolve_an_pause(state); 3687 } 3688 EXPORT_SYMBOL_GPL(phylink_resolve_c73); 3689 3690 static void phylink_decode_c37_word(struct phylink_link_state *state, 3691 uint16_t config_reg, int speed) 3692 { 3693 int fd_bit; 3694 3695 if (speed == SPEED_2500) 3696 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT; 3697 else 3698 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT; 3699 3700 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit); 3701 3702 if (linkmode_test_bit(fd_bit, state->advertising) && 3703 linkmode_test_bit(fd_bit, state->lp_advertising)) { 3704 state->speed = speed; 3705 state->duplex = DUPLEX_FULL; 3706 } else { 3707 /* negotiation failure */ 3708 state->link = false; 3709 } 3710 3711 phylink_resolve_an_pause(state); 3712 } 3713 3714 static void phylink_decode_sgmii_word(struct phylink_link_state *state, 3715 uint16_t config_reg) 3716 { 3717 if (!(config_reg & LPA_SGMII_LINK)) { 3718 state->link = false; 3719 return; 3720 } 3721 3722 switch (config_reg & LPA_SGMII_SPD_MASK) { 3723 case LPA_SGMII_10: 3724 state->speed = SPEED_10; 3725 break; 3726 case LPA_SGMII_100: 3727 state->speed = SPEED_100; 3728 break; 3729 case LPA_SGMII_1000: 3730 state->speed = SPEED_1000; 3731 break; 3732 default: 3733 state->link = false; 3734 return; 3735 } 3736 if (config_reg & LPA_SGMII_FULL_DUPLEX) 3737 state->duplex = DUPLEX_FULL; 3738 else 3739 state->duplex = DUPLEX_HALF; 3740 } 3741 3742 /** 3743 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS 3744 * @state: a pointer to a struct phylink_link_state. 3745 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word 3746 * 3747 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation 3748 * code word. Decode the USXGMII code word and populate the corresponding fields 3749 * (speed, duplex) into the phylink_link_state structure. 3750 */ 3751 void phylink_decode_usxgmii_word(struct phylink_link_state *state, 3752 uint16_t lpa) 3753 { 3754 switch (lpa & MDIO_USXGMII_SPD_MASK) { 3755 case MDIO_USXGMII_10: 3756 state->speed = SPEED_10; 3757 break; 3758 case MDIO_USXGMII_100: 3759 state->speed = SPEED_100; 3760 break; 3761 case MDIO_USXGMII_1000: 3762 state->speed = SPEED_1000; 3763 break; 3764 case MDIO_USXGMII_2500: 3765 state->speed = SPEED_2500; 3766 break; 3767 case MDIO_USXGMII_5000: 3768 state->speed = SPEED_5000; 3769 break; 3770 case MDIO_USXGMII_10G: 3771 state->speed = SPEED_10000; 3772 break; 3773 default: 3774 state->link = false; 3775 return; 3776 } 3777 3778 if (lpa & MDIO_USXGMII_FULL_DUPLEX) 3779 state->duplex = DUPLEX_FULL; 3780 else 3781 state->duplex = DUPLEX_HALF; 3782 } 3783 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word); 3784 3785 /** 3786 * phylink_decode_usgmii_word() - decode the USGMII word from a MAC PCS 3787 * @state: a pointer to a struct phylink_link_state. 3788 * @lpa: a 16 bit value which stores the USGMII auto-negotiation word 3789 * 3790 * Helper for MAC PCS supporting the USGMII protocol and the auto-negotiation 3791 * code word. Decode the USGMII code word and populate the corresponding fields 3792 * (speed, duplex) into the phylink_link_state structure. The structure for this 3793 * word is the same as the USXGMII word, except it only supports speeds up to 3794 * 1Gbps. 3795 */ 3796 static void phylink_decode_usgmii_word(struct phylink_link_state *state, 3797 uint16_t lpa) 3798 { 3799 switch (lpa & MDIO_USXGMII_SPD_MASK) { 3800 case MDIO_USXGMII_10: 3801 state->speed = SPEED_10; 3802 break; 3803 case MDIO_USXGMII_100: 3804 state->speed = SPEED_100; 3805 break; 3806 case MDIO_USXGMII_1000: 3807 state->speed = SPEED_1000; 3808 break; 3809 default: 3810 state->link = false; 3811 return; 3812 } 3813 3814 if (lpa & MDIO_USXGMII_FULL_DUPLEX) 3815 state->duplex = DUPLEX_FULL; 3816 else 3817 state->duplex = DUPLEX_HALF; 3818 } 3819 3820 /** 3821 * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers 3822 * @state: a pointer to a &struct phylink_link_state. 3823 * @neg_mode: link negotiation mode (PHYLINK_PCS_NEG_xxx) 3824 * @bmsr: The value of the %MII_BMSR register 3825 * @lpa: The value of the %MII_LPA register 3826 * 3827 * Helper for MAC PCS supporting the 802.3 clause 22 register set for 3828 * clause 37 negotiation and/or SGMII control. 3829 * 3830 * Parse the Clause 37 or Cisco SGMII link partner negotiation word into 3831 * the phylink @state structure. This is suitable to be used for implementing 3832 * the pcs_get_state() member of the struct phylink_pcs_ops structure if 3833 * accessing @bmsr and @lpa cannot be done with MDIO directly. 3834 */ 3835 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state, 3836 unsigned int neg_mode, u16 bmsr, u16 lpa) 3837 { 3838 state->link = !!(bmsr & BMSR_LSTATUS); 3839 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE); 3840 3841 /* If the link is down, the advertisement data is undefined. */ 3842 if (!state->link) 3843 return; 3844 3845 switch (state->interface) { 3846 case PHY_INTERFACE_MODE_1000BASEX: 3847 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) { 3848 phylink_decode_c37_word(state, lpa, SPEED_1000); 3849 } else { 3850 state->speed = SPEED_1000; 3851 state->duplex = DUPLEX_FULL; 3852 state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX; 3853 } 3854 break; 3855 3856 case PHY_INTERFACE_MODE_2500BASEX: 3857 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) { 3858 phylink_decode_c37_word(state, lpa, SPEED_2500); 3859 } else { 3860 state->speed = SPEED_2500; 3861 state->duplex = DUPLEX_FULL; 3862 state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX; 3863 } 3864 break; 3865 3866 case PHY_INTERFACE_MODE_SGMII: 3867 case PHY_INTERFACE_MODE_QSGMII: 3868 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) 3869 phylink_decode_sgmii_word(state, lpa); 3870 break; 3871 3872 case PHY_INTERFACE_MODE_QUSGMII: 3873 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) 3874 phylink_decode_usgmii_word(state, lpa); 3875 break; 3876 3877 default: 3878 state->link = false; 3879 break; 3880 } 3881 } 3882 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state); 3883 3884 /** 3885 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state 3886 * @pcs: a pointer to a &struct mdio_device. 3887 * @neg_mode: link negotiation mode (PHYLINK_PCS_NEG_xxx) 3888 * @state: a pointer to a &struct phylink_link_state. 3889 * 3890 * Helper for MAC PCS supporting the 802.3 clause 22 register set for 3891 * clause 37 negotiation and/or SGMII control. 3892 * 3893 * Read the MAC PCS state from the MII device configured in @config and 3894 * parse the Clause 37 or Cisco SGMII link partner negotiation word into 3895 * the phylink @state structure. This is suitable to be directly plugged 3896 * into the pcs_get_state() member of the struct phylink_pcs_ops 3897 * structure. 3898 */ 3899 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs, 3900 unsigned int neg_mode, 3901 struct phylink_link_state *state) 3902 { 3903 int bmsr, lpa; 3904 3905 bmsr = mdiodev_read(pcs, MII_BMSR); 3906 lpa = mdiodev_read(pcs, MII_LPA); 3907 if (bmsr < 0 || lpa < 0) { 3908 state->link = false; 3909 return; 3910 } 3911 3912 phylink_mii_c22_pcs_decode_state(state, neg_mode, bmsr, lpa); 3913 } 3914 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state); 3915 3916 /** 3917 * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS 3918 * advertisement 3919 * @interface: the PHY interface mode being configured 3920 * @advertising: the ethtool advertisement mask 3921 * 3922 * Helper for MAC PCS supporting the 802.3 clause 22 register set for 3923 * clause 37 negotiation and/or SGMII control. 3924 * 3925 * Encode the clause 37 PCS advertisement as specified by @interface and 3926 * @advertising. 3927 * 3928 * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed. 3929 */ 3930 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface, 3931 const unsigned long *advertising) 3932 { 3933 u16 adv; 3934 3935 switch (interface) { 3936 case PHY_INTERFACE_MODE_1000BASEX: 3937 case PHY_INTERFACE_MODE_2500BASEX: 3938 adv = ADVERTISE_1000XFULL; 3939 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3940 advertising)) 3941 adv |= ADVERTISE_1000XPAUSE; 3942 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3943 advertising)) 3944 adv |= ADVERTISE_1000XPSE_ASYM; 3945 return adv; 3946 case PHY_INTERFACE_MODE_SGMII: 3947 case PHY_INTERFACE_MODE_QSGMII: 3948 return 0x0001; 3949 default: 3950 /* Nothing to do for other modes */ 3951 return -EINVAL; 3952 } 3953 } 3954 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement); 3955 3956 /** 3957 * phylink_mii_c22_pcs_config() - configure clause 22 PCS 3958 * @pcs: a pointer to a &struct mdio_device. 3959 * @interface: the PHY interface mode being configured 3960 * @advertising: the ethtool advertisement mask 3961 * @neg_mode: PCS negotiation mode 3962 * 3963 * Configure a Clause 22 PCS PHY with the appropriate negotiation 3964 * parameters for the @mode, @interface and @advertising parameters. 3965 * Returns negative error number on failure, zero if the advertisement 3966 * has not changed, or positive if there is a change. 3967 */ 3968 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, 3969 phy_interface_t interface, 3970 const unsigned long *advertising, 3971 unsigned int neg_mode) 3972 { 3973 bool changed = 0; 3974 u16 bmcr; 3975 int ret, adv; 3976 3977 adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising); 3978 if (adv >= 0) { 3979 ret = mdiobus_modify_changed(pcs->bus, pcs->addr, 3980 MII_ADVERTISE, 0xffff, adv); 3981 if (ret < 0) 3982 return ret; 3983 changed = ret; 3984 } 3985 3986 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) 3987 bmcr = BMCR_ANENABLE; 3988 else 3989 bmcr = 0; 3990 3991 /* Configure the inband state. Ensure ISOLATE bit is disabled */ 3992 ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr); 3993 if (ret < 0) 3994 return ret; 3995 3996 return changed; 3997 } 3998 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config); 3999 4000 /** 4001 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation 4002 * @pcs: a pointer to a &struct mdio_device. 4003 * 4004 * Helper for MAC PCS supporting the 802.3 clause 22 register set for 4005 * clause 37 negotiation. 4006 * 4007 * Restart the clause 37 negotiation with the link partner. This is 4008 * suitable to be directly plugged into the pcs_get_state() member 4009 * of the struct phylink_pcs_ops structure. 4010 */ 4011 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs) 4012 { 4013 int val = mdiodev_read(pcs, MII_BMCR); 4014 4015 if (val >= 0) { 4016 val |= BMCR_ANRESTART; 4017 4018 mdiodev_write(pcs, MII_BMCR, val); 4019 } 4020 } 4021 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart); 4022 4023 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs, 4024 struct phylink_link_state *state) 4025 { 4026 struct mii_bus *bus = pcs->bus; 4027 int addr = pcs->addr; 4028 int stat; 4029 4030 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1); 4031 if (stat < 0) { 4032 state->link = false; 4033 return; 4034 } 4035 4036 state->link = !!(stat & MDIO_STAT1_LSTATUS); 4037 if (!state->link) 4038 return; 4039 4040 switch (state->interface) { 4041 case PHY_INTERFACE_MODE_10GBASER: 4042 state->speed = SPEED_10000; 4043 state->duplex = DUPLEX_FULL; 4044 break; 4045 4046 default: 4047 break; 4048 } 4049 } 4050 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state); 4051 4052 static int __init phylink_init(void) 4053 { 4054 for (int i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); ++i) 4055 __set_bit(phylink_sfp_interface_preference[i], 4056 phylink_sfp_interfaces); 4057 4058 return 0; 4059 } 4060 4061 module_init(phylink_init); 4062 4063 MODULE_LICENSE("GPL v2"); 4064 MODULE_DESCRIPTION("phylink models the MAC to optional PHY connection"); 4065