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