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