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