1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Framework for finding and configuring PHYs. 3 * Also contains generic PHY driver 4 * 5 * Author: Andy Fleming 6 * 7 * Copyright (c) 2004 Freescale Semiconductor, Inc. 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/acpi.h> 13 #include <linux/bitmap.h> 14 #include <linux/delay.h> 15 #include <linux/errno.h> 16 #include <linux/etherdevice.h> 17 #include <linux/ethtool.h> 18 #include <linux/init.h> 19 #include <linux/interrupt.h> 20 #include <linux/io.h> 21 #include <linux/kernel.h> 22 #include <linux/list.h> 23 #include <linux/mdio.h> 24 #include <linux/mii.h> 25 #include <linux/mm.h> 26 #include <linux/module.h> 27 #include <linux/of.h> 28 #include <linux/netdevice.h> 29 #include <linux/phy.h> 30 #include <linux/phylib_stubs.h> 31 #include <linux/phy_led_triggers.h> 32 #include <linux/phy_link_topology.h> 33 #include <linux/pse-pd/pse.h> 34 #include <linux/property.h> 35 #include <linux/ptp_clock_kernel.h> 36 #include <linux/rtnetlink.h> 37 #include <linux/sfp.h> 38 #include <linux/skbuff.h> 39 #include <linux/slab.h> 40 #include <linux/string.h> 41 #include <linux/uaccess.h> 42 #include <linux/unistd.h> 43 44 #include "phylib-internal.h" 45 #include "phy-caps.h" 46 47 MODULE_DESCRIPTION("PHY library"); 48 MODULE_AUTHOR("Andy Fleming"); 49 MODULE_LICENSE("GPL"); 50 51 #define PHY_ANY_ID "MATCH ANY PHY" 52 #define PHY_ANY_UID 0xffffffff 53 54 struct phy_fixup { 55 struct list_head list; 56 char bus_id[MII_BUS_ID_SIZE + 3]; 57 u32 phy_uid; 58 u32 phy_uid_mask; 59 int (*run)(struct phy_device *phydev); 60 }; 61 62 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init; 63 EXPORT_SYMBOL_GPL(phy_basic_features); 64 65 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init; 66 EXPORT_SYMBOL_GPL(phy_basic_t1_features); 67 68 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init; 69 EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features); 70 71 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init; 72 EXPORT_SYMBOL_GPL(phy_gbit_features); 73 74 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init; 75 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features); 76 77 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init; 78 EXPORT_SYMBOL_GPL(phy_10gbit_features); 79 80 const int phy_basic_ports_array[3] = { 81 ETHTOOL_LINK_MODE_Autoneg_BIT, 82 ETHTOOL_LINK_MODE_TP_BIT, 83 ETHTOOL_LINK_MODE_MII_BIT, 84 }; 85 EXPORT_SYMBOL_GPL(phy_basic_ports_array); 86 87 static const int phy_all_ports_features_array[7] = { 88 ETHTOOL_LINK_MODE_Autoneg_BIT, 89 ETHTOOL_LINK_MODE_TP_BIT, 90 ETHTOOL_LINK_MODE_MII_BIT, 91 ETHTOOL_LINK_MODE_FIBRE_BIT, 92 ETHTOOL_LINK_MODE_AUI_BIT, 93 ETHTOOL_LINK_MODE_BNC_BIT, 94 ETHTOOL_LINK_MODE_Backplane_BIT, 95 }; 96 97 static const int phy_10_100_features_array[4] = { 98 ETHTOOL_LINK_MODE_10baseT_Half_BIT, 99 ETHTOOL_LINK_MODE_10baseT_Full_BIT, 100 ETHTOOL_LINK_MODE_100baseT_Half_BIT, 101 ETHTOOL_LINK_MODE_100baseT_Full_BIT, 102 }; 103 104 static const int phy_basic_t1_features_array[3] = { 105 ETHTOOL_LINK_MODE_TP_BIT, 106 ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, 107 ETHTOOL_LINK_MODE_100baseT1_Full_BIT, 108 }; 109 110 static const int phy_basic_t1s_p2mp_features_array[2] = { 111 ETHTOOL_LINK_MODE_TP_BIT, 112 ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT, 113 }; 114 115 static const int phy_gbit_features_array[2] = { 116 ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 117 ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 118 }; 119 120 static const int phy_eee_cap1_features_array[] = { 121 ETHTOOL_LINK_MODE_100baseT_Full_BIT, 122 ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 123 ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 124 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, 125 ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, 126 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, 127 }; 128 129 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init; 130 EXPORT_SYMBOL_GPL(phy_eee_cap1_features); 131 132 static const int phy_eee_cap2_features_array[] = { 133 ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 134 ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 135 }; 136 137 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap2_features) __ro_after_init; 138 EXPORT_SYMBOL_GPL(phy_eee_cap2_features); 139 140 static void features_init(void) 141 { 142 /* 10/100 half/full*/ 143 linkmode_set_bit_array(phy_basic_ports_array, 144 ARRAY_SIZE(phy_basic_ports_array), 145 phy_basic_features); 146 linkmode_set_bit_array(phy_10_100_features_array, 147 ARRAY_SIZE(phy_10_100_features_array), 148 phy_basic_features); 149 150 /* 100 full, TP */ 151 linkmode_set_bit_array(phy_basic_t1_features_array, 152 ARRAY_SIZE(phy_basic_t1_features_array), 153 phy_basic_t1_features); 154 155 /* 10 half, P2MP, TP */ 156 linkmode_set_bit_array(phy_basic_t1s_p2mp_features_array, 157 ARRAY_SIZE(phy_basic_t1s_p2mp_features_array), 158 phy_basic_t1s_p2mp_features); 159 160 /* 10/100 half/full + 1000 half/full */ 161 linkmode_set_bit_array(phy_basic_ports_array, 162 ARRAY_SIZE(phy_basic_ports_array), 163 phy_gbit_features); 164 linkmode_set_bit_array(phy_10_100_features_array, 165 ARRAY_SIZE(phy_10_100_features_array), 166 phy_gbit_features); 167 linkmode_set_bit_array(phy_gbit_features_array, 168 ARRAY_SIZE(phy_gbit_features_array), 169 phy_gbit_features); 170 171 /* 10/100 half/full + 1000 half/full + fibre*/ 172 linkmode_set_bit_array(phy_basic_ports_array, 173 ARRAY_SIZE(phy_basic_ports_array), 174 phy_gbit_fibre_features); 175 linkmode_set_bit_array(phy_10_100_features_array, 176 ARRAY_SIZE(phy_10_100_features_array), 177 phy_gbit_fibre_features); 178 linkmode_set_bit_array(phy_gbit_features_array, 179 ARRAY_SIZE(phy_gbit_features_array), 180 phy_gbit_fibre_features); 181 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, phy_gbit_fibre_features); 182 183 /* 10/100 half/full + 1000 half/full + 10G full*/ 184 linkmode_set_bit_array(phy_all_ports_features_array, 185 ARRAY_SIZE(phy_all_ports_features_array), 186 phy_10gbit_features); 187 linkmode_set_bit_array(phy_10_100_features_array, 188 ARRAY_SIZE(phy_10_100_features_array), 189 phy_10gbit_features); 190 linkmode_set_bit_array(phy_gbit_features_array, 191 ARRAY_SIZE(phy_gbit_features_array), 192 phy_10gbit_features); 193 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 194 phy_10gbit_features); 195 196 linkmode_set_bit_array(phy_eee_cap1_features_array, 197 ARRAY_SIZE(phy_eee_cap1_features_array), 198 phy_eee_cap1_features); 199 linkmode_set_bit_array(phy_eee_cap2_features_array, 200 ARRAY_SIZE(phy_eee_cap2_features_array), 201 phy_eee_cap2_features); 202 203 } 204 205 void phy_device_free(struct phy_device *phydev) 206 { 207 put_device(&phydev->mdio.dev); 208 } 209 EXPORT_SYMBOL(phy_device_free); 210 211 static void phy_mdio_device_free(struct mdio_device *mdiodev) 212 { 213 struct phy_device *phydev; 214 215 phydev = container_of(mdiodev, struct phy_device, mdio); 216 phy_device_free(phydev); 217 } 218 219 static void phy_device_release(struct device *dev) 220 { 221 fwnode_handle_put(dev->fwnode); 222 kfree(to_phy_device(dev)); 223 } 224 225 static void phy_mdio_device_remove(struct mdio_device *mdiodev) 226 { 227 struct phy_device *phydev; 228 229 phydev = container_of(mdiodev, struct phy_device, mdio); 230 phy_device_remove(phydev); 231 } 232 233 static struct phy_driver genphy_driver; 234 235 static LIST_HEAD(phy_fixup_list); 236 static DEFINE_MUTEX(phy_fixup_lock); 237 238 static bool phy_drv_wol_enabled(struct phy_device *phydev) 239 { 240 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; 241 242 phy_ethtool_get_wol(phydev, &wol); 243 244 return wol.wolopts != 0; 245 } 246 247 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) 248 { 249 struct device_driver *drv = phydev->mdio.dev.driver; 250 struct phy_driver *phydrv = to_phy_driver(drv); 251 struct net_device *netdev = phydev->attached_dev; 252 253 if (!drv || !phydrv->suspend) 254 return false; 255 256 /* If the PHY on the mido bus is not attached but has WOL enabled 257 * we cannot suspend the PHY. 258 */ 259 if (!netdev && phy_drv_wol_enabled(phydev)) 260 return false; 261 262 /* PHY not attached? May suspend if the PHY has not already been 263 * suspended as part of a prior call to phy_disconnect() -> 264 * phy_detach() -> phy_suspend() because the parent netdev might be the 265 * MDIO bus driver and clock gated at this point. 266 */ 267 if (!netdev) 268 goto out; 269 270 if (netdev->ethtool->wol_enabled) 271 return false; 272 273 /* As long as not all affected network drivers support the 274 * wol_enabled flag, let's check for hints that WoL is enabled. 275 * Don't suspend PHY if the attached netdev parent may wake up. 276 * The parent may point to a PCI device, as in tg3 driver. 277 */ 278 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) 279 return false; 280 281 /* Also don't suspend PHY if the netdev itself may wakeup. This 282 * is the case for devices w/o underlaying pwr. mgmt. aware bus, 283 * e.g. SoC devices. 284 */ 285 if (device_may_wakeup(&netdev->dev)) 286 return false; 287 288 out: 289 return !phydev->suspended; 290 } 291 292 static __maybe_unused int mdio_bus_phy_suspend(struct device *dev) 293 { 294 struct phy_device *phydev = to_phy_device(dev); 295 296 if (phydev->mac_managed_pm) 297 return 0; 298 299 /* Wakeup interrupts may occur during the system sleep transition when 300 * the PHY is inaccessible. Set flag to postpone handling until the PHY 301 * has resumed. Wait for concurrent interrupt handler to complete. 302 */ 303 if (phy_interrupt_is_valid(phydev)) { 304 phydev->irq_suspended = 1; 305 synchronize_irq(phydev->irq); 306 } 307 308 /* We must stop the state machine manually, otherwise it stops out of 309 * control, possibly with the phydev->lock held. Upon resume, netdev 310 * may call phy routines that try to grab the same lock, and that may 311 * lead to a deadlock. 312 */ 313 if (phydev->attached_dev && phydev->adjust_link) 314 phy_stop_machine(phydev); 315 316 if (!mdio_bus_phy_may_suspend(phydev)) 317 return 0; 318 319 phydev->suspended_by_mdio_bus = 1; 320 321 return phy_suspend(phydev); 322 } 323 324 static __maybe_unused int mdio_bus_phy_resume(struct device *dev) 325 { 326 struct phy_device *phydev = to_phy_device(dev); 327 int ret; 328 329 if (phydev->mac_managed_pm) 330 return 0; 331 332 if (!phydev->suspended_by_mdio_bus) 333 goto no_resume; 334 335 phydev->suspended_by_mdio_bus = 0; 336 337 /* If we managed to get here with the PHY state machine in a state 338 * neither PHY_HALTED, PHY_READY nor PHY_UP, this is an indication 339 * that something went wrong and we should most likely be using 340 * MAC managed PM, but we are not. 341 */ 342 WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY && 343 phydev->state != PHY_UP); 344 345 ret = phy_init_hw(phydev); 346 if (ret < 0) 347 return ret; 348 349 ret = phy_resume(phydev); 350 if (ret < 0) 351 return ret; 352 no_resume: 353 if (phy_interrupt_is_valid(phydev)) { 354 phydev->irq_suspended = 0; 355 synchronize_irq(phydev->irq); 356 357 /* Rerun interrupts which were postponed by phy_interrupt() 358 * because they occurred during the system sleep transition. 359 */ 360 if (phydev->irq_rerun) { 361 phydev->irq_rerun = 0; 362 enable_irq(phydev->irq); 363 irq_wake_thread(phydev->irq, phydev); 364 } 365 } 366 367 if (phydev->attached_dev && phydev->adjust_link) 368 phy_start_machine(phydev); 369 370 return 0; 371 } 372 373 static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend, 374 mdio_bus_phy_resume); 375 376 /** 377 * phy_register_fixup - creates a new phy_fixup and adds it to the list 378 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID) 379 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) 380 * It can also be PHY_ANY_UID 381 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before 382 * comparison 383 * @run: The actual code to be run when a matching PHY is found 384 */ 385 static int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask, 386 int (*run)(struct phy_device *)) 387 { 388 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); 389 390 if (!fixup) 391 return -ENOMEM; 392 393 strscpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id)); 394 fixup->phy_uid = phy_uid; 395 fixup->phy_uid_mask = phy_uid_mask; 396 fixup->run = run; 397 398 mutex_lock(&phy_fixup_lock); 399 list_add_tail(&fixup->list, &phy_fixup_list); 400 mutex_unlock(&phy_fixup_lock); 401 402 return 0; 403 } 404 405 /* Registers a fixup to be run on any PHY with the UID in phy_uid */ 406 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, 407 int (*run)(struct phy_device *)) 408 { 409 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run); 410 } 411 EXPORT_SYMBOL(phy_register_fixup_for_uid); 412 413 /* Registers a fixup to be run on the PHY with id string bus_id */ 414 int phy_register_fixup_for_id(const char *bus_id, 415 int (*run)(struct phy_device *)) 416 { 417 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run); 418 } 419 EXPORT_SYMBOL(phy_register_fixup_for_id); 420 421 /** 422 * phy_unregister_fixup - remove a phy_fixup from the list 423 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list 424 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list 425 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison 426 */ 427 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask) 428 { 429 struct list_head *pos, *n; 430 struct phy_fixup *fixup; 431 int ret; 432 433 ret = -ENODEV; 434 435 mutex_lock(&phy_fixup_lock); 436 list_for_each_safe(pos, n, &phy_fixup_list) { 437 fixup = list_entry(pos, struct phy_fixup, list); 438 439 if ((!strcmp(fixup->bus_id, bus_id)) && 440 phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) { 441 list_del(&fixup->list); 442 kfree(fixup); 443 ret = 0; 444 break; 445 } 446 } 447 mutex_unlock(&phy_fixup_lock); 448 449 return ret; 450 } 451 EXPORT_SYMBOL(phy_unregister_fixup); 452 453 /* Unregisters a fixup of any PHY with the UID in phy_uid */ 454 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask) 455 { 456 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask); 457 } 458 EXPORT_SYMBOL(phy_unregister_fixup_for_uid); 459 460 /* Unregisters a fixup of the PHY with id string bus_id */ 461 int phy_unregister_fixup_for_id(const char *bus_id) 462 { 463 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff); 464 } 465 EXPORT_SYMBOL(phy_unregister_fixup_for_id); 466 467 /* Returns 1 if fixup matches phydev in bus_id and phy_uid. 468 * Fixups can be set to match any in one or more fields. 469 */ 470 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup) 471 { 472 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0) 473 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) 474 return 0; 475 476 if (!phy_id_compare(phydev->phy_id, fixup->phy_uid, 477 fixup->phy_uid_mask)) 478 if (fixup->phy_uid != PHY_ANY_UID) 479 return 0; 480 481 return 1; 482 } 483 484 /* Runs any matching fixups for this phydev */ 485 static int phy_scan_fixups(struct phy_device *phydev) 486 { 487 struct phy_fixup *fixup; 488 489 mutex_lock(&phy_fixup_lock); 490 list_for_each_entry(fixup, &phy_fixup_list, list) { 491 if (phy_needs_fixup(phydev, fixup)) { 492 int err = fixup->run(phydev); 493 494 if (err < 0) { 495 mutex_unlock(&phy_fixup_lock); 496 return err; 497 } 498 phydev->has_fixups = true; 499 } 500 } 501 mutex_unlock(&phy_fixup_lock); 502 503 return 0; 504 } 505 506 static int phy_bus_match(struct device *dev, const struct device_driver *drv) 507 { 508 struct phy_device *phydev = to_phy_device(dev); 509 const struct phy_driver *phydrv = to_phy_driver(drv); 510 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); 511 int i; 512 513 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)) 514 return 0; 515 516 if (phydrv->match_phy_device) 517 return phydrv->match_phy_device(phydev); 518 519 if (phydev->is_c45) { 520 for (i = 1; i < num_ids; i++) { 521 if (phydev->c45_ids.device_ids[i] == 0xffffffff) 522 continue; 523 524 if (phy_id_compare(phydev->c45_ids.device_ids[i], 525 phydrv->phy_id, phydrv->phy_id_mask)) 526 return 1; 527 } 528 return 0; 529 } else { 530 return phy_id_compare(phydev->phy_id, phydrv->phy_id, 531 phydrv->phy_id_mask); 532 } 533 } 534 535 static ssize_t 536 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) 537 { 538 struct phy_device *phydev = to_phy_device(dev); 539 540 return sysfs_emit(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); 541 } 542 static DEVICE_ATTR_RO(phy_id); 543 544 static ssize_t 545 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) 546 { 547 struct phy_device *phydev = to_phy_device(dev); 548 const char *mode = NULL; 549 550 if (phydev->is_internal) 551 mode = "internal"; 552 else 553 mode = phy_modes(phydev->interface); 554 555 return sysfs_emit(buf, "%s\n", mode); 556 } 557 static DEVICE_ATTR_RO(phy_interface); 558 559 static ssize_t 560 phy_has_fixups_show(struct device *dev, struct device_attribute *attr, 561 char *buf) 562 { 563 struct phy_device *phydev = to_phy_device(dev); 564 565 return sysfs_emit(buf, "%d\n", phydev->has_fixups); 566 } 567 static DEVICE_ATTR_RO(phy_has_fixups); 568 569 static ssize_t phy_dev_flags_show(struct device *dev, 570 struct device_attribute *attr, 571 char *buf) 572 { 573 struct phy_device *phydev = to_phy_device(dev); 574 575 return sysfs_emit(buf, "0x%08x\n", phydev->dev_flags); 576 } 577 static DEVICE_ATTR_RO(phy_dev_flags); 578 579 static struct attribute *phy_dev_attrs[] = { 580 &dev_attr_phy_id.attr, 581 &dev_attr_phy_interface.attr, 582 &dev_attr_phy_has_fixups.attr, 583 &dev_attr_phy_dev_flags.attr, 584 NULL, 585 }; 586 ATTRIBUTE_GROUPS(phy_dev); 587 588 static const struct device_type mdio_bus_phy_type = { 589 .name = "PHY", 590 .groups = phy_dev_groups, 591 .release = phy_device_release, 592 .pm = pm_ptr(&mdio_bus_phy_pm_ops), 593 }; 594 595 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id) 596 { 597 int ret; 598 599 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, 600 MDIO_ID_ARGS(phy_id)); 601 /* We only check for failures in executing the usermode binary, 602 * not whether a PHY driver module exists for the PHY ID. 603 * Accept -ENOENT because this may occur in case no initramfs exists, 604 * then modprobe isn't available. 605 */ 606 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) { 607 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n", 608 ret, (unsigned long)phy_id); 609 return ret; 610 } 611 612 return 0; 613 } 614 615 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id, 616 bool is_c45, 617 struct phy_c45_device_ids *c45_ids) 618 { 619 struct phy_device *dev; 620 struct mdio_device *mdiodev; 621 int ret = 0; 622 623 /* We allocate the device, and initialize the default values */ 624 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 625 if (!dev) 626 return ERR_PTR(-ENOMEM); 627 628 mdiodev = &dev->mdio; 629 mdiodev->dev.parent = &bus->dev; 630 mdiodev->dev.bus = &mdio_bus_type; 631 mdiodev->dev.type = &mdio_bus_phy_type; 632 mdiodev->bus = bus; 633 mdiodev->bus_match = phy_bus_match; 634 mdiodev->addr = addr; 635 mdiodev->flags = MDIO_DEVICE_FLAG_PHY; 636 mdiodev->device_free = phy_mdio_device_free; 637 mdiodev->device_remove = phy_mdio_device_remove; 638 mdiodev->reset_state = -1; 639 640 dev->speed = SPEED_UNKNOWN; 641 dev->duplex = DUPLEX_UNKNOWN; 642 dev->pause = 0; 643 dev->asym_pause = 0; 644 dev->link = 0; 645 dev->port = PORT_TP; 646 dev->interface = PHY_INTERFACE_MODE_GMII; 647 648 dev->autoneg = AUTONEG_ENABLE; 649 650 dev->pma_extable = -ENODATA; 651 dev->is_c45 = is_c45; 652 dev->phy_id = phy_id; 653 if (c45_ids) 654 dev->c45_ids = *c45_ids; 655 dev->irq = bus->irq[addr]; 656 657 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); 658 device_initialize(&mdiodev->dev); 659 660 dev->state = PHY_DOWN; 661 INIT_LIST_HEAD(&dev->leds); 662 663 mutex_init(&dev->lock); 664 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); 665 666 /* Request the appropriate module unconditionally; don't 667 * bother trying to do so only if it isn't already loaded, 668 * because that gets complicated. A hotplug event would have 669 * done an unconditional modprobe anyway. 670 * We don't do normal hotplug because it won't work for MDIO 671 * -- because it relies on the device staying around for long 672 * enough for the driver to get loaded. With MDIO, the NIC 673 * driver will get bored and give up as soon as it finds that 674 * there's no driver _already_ loaded. 675 */ 676 if (is_c45 && c45_ids) { 677 const int num_ids = ARRAY_SIZE(c45_ids->device_ids); 678 int i; 679 680 for (i = 1; i < num_ids; i++) { 681 if (c45_ids->device_ids[i] == 0xffffffff) 682 continue; 683 684 ret = phy_request_driver_module(dev, 685 c45_ids->device_ids[i]); 686 if (ret) 687 break; 688 } 689 } else { 690 ret = phy_request_driver_module(dev, phy_id); 691 } 692 693 if (ret) { 694 put_device(&mdiodev->dev); 695 dev = ERR_PTR(ret); 696 } 697 698 return dev; 699 } 700 EXPORT_SYMBOL(phy_device_create); 701 702 /* phy_c45_probe_present - checks to see if a MMD is present in the package 703 * @bus: the target MII bus 704 * @prtad: PHY package address on the MII bus 705 * @devad: PHY device (MMD) address 706 * 707 * Read the MDIO_STAT2 register, and check whether a device is responding 708 * at this address. 709 * 710 * Returns: negative error number on bus access error, zero if no device 711 * is responding, or positive if a device is present. 712 */ 713 static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad) 714 { 715 int stat2; 716 717 stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2); 718 if (stat2 < 0) 719 return stat2; 720 721 return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL; 722 } 723 724 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers. 725 * @bus: the target MII bus 726 * @addr: PHY address on the MII bus 727 * @dev_addr: MMD address in the PHY. 728 * @devices_in_package: where to store the devices in package information. 729 * 730 * Description: reads devices in package registers of a MMD at @dev_addr 731 * from PHY at @addr on @bus. 732 * 733 * Returns: 0 on success, -EIO on failure. 734 */ 735 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr, 736 u32 *devices_in_package) 737 { 738 int phy_reg; 739 740 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2); 741 if (phy_reg < 0) 742 return -EIO; 743 *devices_in_package = phy_reg << 16; 744 745 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1); 746 if (phy_reg < 0) 747 return -EIO; 748 *devices_in_package |= phy_reg; 749 750 return 0; 751 } 752 753 /** 754 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. 755 * @bus: the target MII bus 756 * @addr: PHY address on the MII bus 757 * @c45_ids: where to store the c45 ID information. 758 * 759 * Read the PHY "devices in package". If this appears to be valid, read 760 * the PHY identifiers for each device. Return the "devices in package" 761 * and identifiers in @c45_ids. 762 * 763 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if 764 * the "devices in package" is invalid or no device responds. 765 */ 766 static int get_phy_c45_ids(struct mii_bus *bus, int addr, 767 struct phy_c45_device_ids *c45_ids) 768 { 769 const int num_ids = ARRAY_SIZE(c45_ids->device_ids); 770 u32 devs_in_pkg = 0; 771 int i, ret, phy_reg; 772 773 /* Find first non-zero Devices In package. Device zero is reserved 774 * for 802.3 c45 complied PHYs, so don't probe it at first. 775 */ 776 for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 || 777 (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) { 778 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) { 779 /* Check that there is a device present at this 780 * address before reading the devices-in-package 781 * register to avoid reading garbage from the PHY. 782 * Some PHYs (88x3310) vendor space is not IEEE802.3 783 * compliant. 784 */ 785 ret = phy_c45_probe_present(bus, addr, i); 786 if (ret < 0) 787 /* returning -ENODEV doesn't stop bus 788 * scanning 789 */ 790 return (phy_reg == -EIO || 791 phy_reg == -ENODEV) ? -ENODEV : -EIO; 792 793 if (!ret) 794 continue; 795 } 796 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg); 797 if (phy_reg < 0) 798 return -EIO; 799 } 800 801 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) { 802 /* If mostly Fs, there is no device there, then let's probe 803 * MMD 0, as some 10G PHYs have zero Devices In package, 804 * e.g. Cortina CS4315/CS4340 PHY. 805 */ 806 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg); 807 if (phy_reg < 0) 808 return -EIO; 809 810 /* no device there, let's get out of here */ 811 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) 812 return -ENODEV; 813 } 814 815 /* Now probe Device Identifiers for each device present. */ 816 for (i = 1; i < num_ids; i++) { 817 if (!(devs_in_pkg & (1 << i))) 818 continue; 819 820 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) { 821 /* Probe the "Device Present" bits for the vendor MMDs 822 * to ignore these if they do not contain IEEE 802.3 823 * registers. 824 */ 825 ret = phy_c45_probe_present(bus, addr, i); 826 if (ret < 0) 827 return ret; 828 829 if (!ret) 830 continue; 831 } 832 833 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1); 834 if (phy_reg < 0) 835 return -EIO; 836 c45_ids->device_ids[i] = phy_reg << 16; 837 838 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2); 839 if (phy_reg < 0) 840 return -EIO; 841 c45_ids->device_ids[i] |= phy_reg; 842 } 843 844 c45_ids->devices_in_package = devs_in_pkg; 845 /* Bit 0 doesn't represent a device, it indicates c22 regs presence */ 846 c45_ids->mmds_present = devs_in_pkg & ~BIT(0); 847 848 return 0; 849 } 850 851 /** 852 * get_phy_c22_id - reads the specified addr for its clause 22 ID. 853 * @bus: the target MII bus 854 * @addr: PHY address on the MII bus 855 * @phy_id: where to store the ID retrieved. 856 * 857 * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus, 858 * placing it in @phy_id. Return zero on successful read and the ID is 859 * valid, %-EIO on bus access error, or %-ENODEV if no device responds 860 * or invalid ID. 861 */ 862 static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id) 863 { 864 int phy_reg; 865 866 /* Grab the bits from PHYIR1, and put them in the upper half */ 867 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); 868 if (phy_reg < 0) { 869 /* returning -ENODEV doesn't stop bus scanning */ 870 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO; 871 } 872 873 *phy_id = phy_reg << 16; 874 875 /* Grab the bits from PHYIR2, and put them in the lower half */ 876 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); 877 if (phy_reg < 0) { 878 /* returning -ENODEV doesn't stop bus scanning */ 879 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO; 880 } 881 882 *phy_id |= phy_reg; 883 884 /* If the phy_id is mostly Fs, there is no device there */ 885 if ((*phy_id & 0x1fffffff) == 0x1fffffff) 886 return -ENODEV; 887 888 return 0; 889 } 890 891 /* Extract the phy ID from the compatible string of the form 892 * ethernet-phy-idAAAA.BBBB. 893 */ 894 int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id) 895 { 896 unsigned int upper, lower; 897 const char *cp; 898 int ret; 899 900 ret = fwnode_property_read_string(fwnode, "compatible", &cp); 901 if (ret) 902 return ret; 903 904 if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2) 905 return -EINVAL; 906 907 *phy_id = ((upper & GENMASK(15, 0)) << 16) | (lower & GENMASK(15, 0)); 908 return 0; 909 } 910 EXPORT_SYMBOL(fwnode_get_phy_id); 911 912 /** 913 * get_phy_device - reads the specified PHY device and returns its @phy_device 914 * struct 915 * @bus: the target MII bus 916 * @addr: PHY address on the MII bus 917 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol 918 * 919 * Probe for a PHY at @addr on @bus. 920 * 921 * When probing for a clause 22 PHY, then read the ID registers. If we find 922 * a valid ID, allocate and return a &struct phy_device. 923 * 924 * When probing for a clause 45 PHY, read the "devices in package" registers. 925 * If the "devices in package" appears valid, read the ID registers for each 926 * MMD, allocate and return a &struct phy_device. 927 * 928 * Returns an allocated &struct phy_device on success, %-ENODEV if there is 929 * no PHY present, or %-EIO on bus access error. 930 */ 931 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45) 932 { 933 struct phy_c45_device_ids c45_ids; 934 u32 phy_id = 0; 935 int r; 936 937 c45_ids.devices_in_package = 0; 938 c45_ids.mmds_present = 0; 939 memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids)); 940 941 if (is_c45) 942 r = get_phy_c45_ids(bus, addr, &c45_ids); 943 else 944 r = get_phy_c22_id(bus, addr, &phy_id); 945 946 if (r) 947 return ERR_PTR(r); 948 949 /* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID 950 * of 0 when probed using get_phy_c22_id() with no error. Proceed to 951 * probe with C45 to see if we're able to get a valid PHY ID in the C45 952 * space, if successful, create the C45 PHY device. 953 */ 954 if (!is_c45 && phy_id == 0 && bus->read_c45) { 955 r = get_phy_c45_ids(bus, addr, &c45_ids); 956 if (!r) 957 return phy_device_create(bus, addr, phy_id, 958 true, &c45_ids); 959 } 960 961 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); 962 } 963 EXPORT_SYMBOL(get_phy_device); 964 965 /** 966 * phy_device_register - Register the phy device on the MDIO bus 967 * @phydev: phy_device structure to be added to the MDIO bus 968 */ 969 int phy_device_register(struct phy_device *phydev) 970 { 971 int err; 972 973 err = mdiobus_register_device(&phydev->mdio); 974 if (err) 975 return err; 976 977 /* Deassert the reset signal */ 978 phy_device_reset(phydev, 0); 979 980 /* Run all of the fixups for this PHY */ 981 err = phy_scan_fixups(phydev); 982 if (err) { 983 phydev_err(phydev, "failed to initialize\n"); 984 goto out; 985 } 986 987 err = device_add(&phydev->mdio.dev); 988 if (err) { 989 phydev_err(phydev, "failed to add\n"); 990 goto out; 991 } 992 993 return 0; 994 995 out: 996 /* Assert the reset signal */ 997 phy_device_reset(phydev, 1); 998 999 mdiobus_unregister_device(&phydev->mdio); 1000 return err; 1001 } 1002 EXPORT_SYMBOL(phy_device_register); 1003 1004 /** 1005 * phy_device_remove - Remove a previously registered phy device from the MDIO bus 1006 * @phydev: phy_device structure to remove 1007 * 1008 * This doesn't free the phy_device itself, it merely reverses the effects 1009 * of phy_device_register(). Use phy_device_free() to free the device 1010 * after calling this function. 1011 */ 1012 void phy_device_remove(struct phy_device *phydev) 1013 { 1014 unregister_mii_timestamper(phydev->mii_ts); 1015 pse_control_put(phydev->psec); 1016 1017 device_del(&phydev->mdio.dev); 1018 1019 /* Assert the reset signal */ 1020 phy_device_reset(phydev, 1); 1021 1022 mdiobus_unregister_device(&phydev->mdio); 1023 } 1024 EXPORT_SYMBOL(phy_device_remove); 1025 1026 /** 1027 * phy_get_c45_ids - Read 802.3-c45 IDs for phy device. 1028 * @phydev: phy_device structure to read 802.3-c45 IDs 1029 * 1030 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if 1031 * the "devices in package" is invalid. 1032 */ 1033 int phy_get_c45_ids(struct phy_device *phydev) 1034 { 1035 return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr, 1036 &phydev->c45_ids); 1037 } 1038 EXPORT_SYMBOL(phy_get_c45_ids); 1039 1040 /** 1041 * phy_find_first - finds the first PHY device on the bus 1042 * @bus: the target MII bus 1043 */ 1044 struct phy_device *phy_find_first(struct mii_bus *bus) 1045 { 1046 struct phy_device *phydev; 1047 int addr; 1048 1049 for (addr = 0; addr < PHY_MAX_ADDR; addr++) { 1050 phydev = mdiobus_get_phy(bus, addr); 1051 if (phydev) 1052 return phydev; 1053 } 1054 return NULL; 1055 } 1056 EXPORT_SYMBOL(phy_find_first); 1057 1058 static void phy_link_change(struct phy_device *phydev, bool up) 1059 { 1060 struct net_device *netdev = phydev->attached_dev; 1061 1062 if (up) 1063 netif_carrier_on(netdev); 1064 else 1065 netif_carrier_off(netdev); 1066 phydev->adjust_link(netdev); 1067 if (phydev->mii_ts && phydev->mii_ts->link_state) 1068 phydev->mii_ts->link_state(phydev->mii_ts, phydev); 1069 } 1070 1071 /** 1072 * phy_prepare_link - prepares the PHY layer to monitor link status 1073 * @phydev: target phy_device struct 1074 * @handler: callback function for link status change notifications 1075 * 1076 * Description: Tells the PHY infrastructure to handle the 1077 * gory details on monitoring link status (whether through 1078 * polling or an interrupt), and to call back to the 1079 * connected device driver when the link status changes. 1080 * If you want to monitor your own link state, don't call 1081 * this function. 1082 */ 1083 static void phy_prepare_link(struct phy_device *phydev, 1084 void (*handler)(struct net_device *)) 1085 { 1086 phydev->adjust_link = handler; 1087 } 1088 1089 /** 1090 * phy_connect_direct - connect an ethernet device to a specific phy_device 1091 * @dev: the network device to connect 1092 * @phydev: the pointer to the phy device 1093 * @handler: callback function for state change notifications 1094 * @interface: PHY device's interface 1095 */ 1096 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, 1097 void (*handler)(struct net_device *), 1098 phy_interface_t interface) 1099 { 1100 int rc; 1101 1102 if (!dev) 1103 return -EINVAL; 1104 1105 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 1106 if (rc) 1107 return rc; 1108 1109 phy_prepare_link(phydev, handler); 1110 if (phy_interrupt_is_valid(phydev)) 1111 phy_request_interrupt(phydev); 1112 1113 return 0; 1114 } 1115 EXPORT_SYMBOL(phy_connect_direct); 1116 1117 /** 1118 * phy_connect - connect an ethernet device to a PHY device 1119 * @dev: the network device to connect 1120 * @bus_id: the id string of the PHY device to connect 1121 * @handler: callback function for state change notifications 1122 * @interface: PHY device's interface 1123 * 1124 * Description: Convenience function for connecting ethernet 1125 * devices to PHY devices. The default behavior is for 1126 * the PHY infrastructure to handle everything, and only notify 1127 * the connected driver when the link status changes. If you 1128 * don't want, or can't use the provided functionality, you may 1129 * choose to call only the subset of functions which provide 1130 * the desired functionality. 1131 */ 1132 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, 1133 void (*handler)(struct net_device *), 1134 phy_interface_t interface) 1135 { 1136 struct phy_device *phydev; 1137 struct device *d; 1138 int rc; 1139 1140 /* Search the list of PHY devices on the mdio bus for the 1141 * PHY with the requested name 1142 */ 1143 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); 1144 if (!d) { 1145 pr_err("PHY %s not found\n", bus_id); 1146 return ERR_PTR(-ENODEV); 1147 } 1148 phydev = to_phy_device(d); 1149 1150 rc = phy_connect_direct(dev, phydev, handler, interface); 1151 put_device(d); 1152 if (rc) 1153 return ERR_PTR(rc); 1154 1155 return phydev; 1156 } 1157 EXPORT_SYMBOL(phy_connect); 1158 1159 /** 1160 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY 1161 * device 1162 * @phydev: target phy_device struct 1163 */ 1164 void phy_disconnect(struct phy_device *phydev) 1165 { 1166 if (phy_is_started(phydev)) 1167 phy_stop(phydev); 1168 1169 if (phy_interrupt_is_valid(phydev)) 1170 phy_free_interrupt(phydev); 1171 1172 phydev->adjust_link = NULL; 1173 1174 phy_detach(phydev); 1175 } 1176 EXPORT_SYMBOL(phy_disconnect); 1177 1178 /** 1179 * phy_poll_reset - Safely wait until a PHY reset has properly completed 1180 * @phydev: The PHY device to poll 1181 * 1182 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as 1183 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR 1184 * register must be polled until the BMCR_RESET bit clears. 1185 * 1186 * Furthermore, any attempts to write to PHY registers may have no effect 1187 * or even generate MDIO bus errors until this is complete. 1188 * 1189 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the 1190 * standard and do not fully reset after the BMCR_RESET bit is set, and may 1191 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an 1192 * effort to support such broken PHYs, this function is separate from the 1193 * standard phy_init_hw() which will zero all the other bits in the BMCR 1194 * and reapply all driver-specific and board-specific fixups. 1195 */ 1196 static int phy_poll_reset(struct phy_device *phydev) 1197 { 1198 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ 1199 int ret, val; 1200 1201 ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET), 1202 50000, 600000, true); 1203 if (ret) 1204 return ret; 1205 /* Some chips (smsc911x) may still need up to another 1ms after the 1206 * BMCR_RESET bit is cleared before they are usable. 1207 */ 1208 msleep(1); 1209 return 0; 1210 } 1211 1212 int phy_init_hw(struct phy_device *phydev) 1213 { 1214 int ret = 0; 1215 1216 /* Deassert the reset signal */ 1217 phy_device_reset(phydev, 0); 1218 1219 if (!phydev->drv) 1220 return 0; 1221 1222 if (phydev->drv->soft_reset) { 1223 ret = phydev->drv->soft_reset(phydev); 1224 if (ret < 0) 1225 return ret; 1226 1227 /* see comment in genphy_soft_reset for an explanation */ 1228 phydev->suspended = 0; 1229 } 1230 1231 ret = phy_scan_fixups(phydev); 1232 if (ret < 0) 1233 return ret; 1234 1235 phy_interface_zero(phydev->possible_interfaces); 1236 1237 if (phydev->drv->config_init) { 1238 ret = phydev->drv->config_init(phydev); 1239 if (ret < 0) 1240 return ret; 1241 } 1242 1243 if (phydev->drv->config_intr) { 1244 ret = phydev->drv->config_intr(phydev); 1245 if (ret < 0) 1246 return ret; 1247 } 1248 1249 return 0; 1250 } 1251 EXPORT_SYMBOL(phy_init_hw); 1252 1253 void phy_attached_info(struct phy_device *phydev) 1254 { 1255 phy_attached_print(phydev, NULL); 1256 } 1257 EXPORT_SYMBOL(phy_attached_info); 1258 1259 #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)" 1260 char *phy_attached_info_irq(struct phy_device *phydev) 1261 { 1262 char *irq_str; 1263 char irq_num[8]; 1264 1265 switch(phydev->irq) { 1266 case PHY_POLL: 1267 irq_str = "POLL"; 1268 break; 1269 case PHY_MAC_INTERRUPT: 1270 irq_str = "MAC"; 1271 break; 1272 default: 1273 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq); 1274 irq_str = irq_num; 1275 break; 1276 } 1277 1278 return kasprintf(GFP_KERNEL, "%s", irq_str); 1279 } 1280 EXPORT_SYMBOL(phy_attached_info_irq); 1281 1282 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) 1283 { 1284 const char *unbound = phydev->drv ? "" : "[unbound] "; 1285 char *irq_str = phy_attached_info_irq(phydev); 1286 1287 if (!fmt) { 1288 phydev_info(phydev, ATTACHED_FMT "\n", unbound, 1289 phydev_name(phydev), irq_str); 1290 } else { 1291 va_list ap; 1292 1293 phydev_info(phydev, ATTACHED_FMT, unbound, 1294 phydev_name(phydev), irq_str); 1295 1296 va_start(ap, fmt); 1297 vprintk(fmt, ap); 1298 va_end(ap); 1299 } 1300 kfree(irq_str); 1301 } 1302 EXPORT_SYMBOL(phy_attached_print); 1303 1304 static void phy_sysfs_create_links(struct phy_device *phydev) 1305 { 1306 struct net_device *dev = phydev->attached_dev; 1307 int err; 1308 1309 if (!dev) 1310 return; 1311 1312 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, 1313 "attached_dev"); 1314 if (err) 1315 return; 1316 1317 err = sysfs_create_link_nowarn(&dev->dev.kobj, 1318 &phydev->mdio.dev.kobj, 1319 "phydev"); 1320 if (err) { 1321 dev_err(&dev->dev, "could not add device link to %s err %d\n", 1322 kobject_name(&phydev->mdio.dev.kobj), 1323 err); 1324 /* non-fatal - some net drivers can use one netdevice 1325 * with more then one phy 1326 */ 1327 } 1328 1329 phydev->sysfs_links = true; 1330 } 1331 1332 static ssize_t 1333 phy_standalone_show(struct device *dev, struct device_attribute *attr, 1334 char *buf) 1335 { 1336 struct phy_device *phydev = to_phy_device(dev); 1337 1338 return sysfs_emit(buf, "%d\n", !phydev->attached_dev); 1339 } 1340 static DEVICE_ATTR_RO(phy_standalone); 1341 1342 /** 1343 * phy_sfp_connect_phy - Connect the SFP module's PHY to the upstream PHY 1344 * @upstream: pointer to the upstream phy device 1345 * @phy: pointer to the SFP module's phy device 1346 * 1347 * This helper allows keeping track of PHY devices on the link. It adds the 1348 * SFP module's phy to the phy namespace of the upstream phy 1349 * 1350 * Return: 0 on success, otherwise a negative error code. 1351 */ 1352 int phy_sfp_connect_phy(void *upstream, struct phy_device *phy) 1353 { 1354 struct phy_device *phydev = upstream; 1355 struct net_device *dev = phydev->attached_dev; 1356 1357 if (dev) 1358 return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev); 1359 1360 return 0; 1361 } 1362 EXPORT_SYMBOL(phy_sfp_connect_phy); 1363 1364 /** 1365 * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY 1366 * @upstream: pointer to the upstream phy device 1367 * @phy: pointer to the SFP module's phy device 1368 * 1369 * This helper allows keeping track of PHY devices on the link. It removes the 1370 * SFP module's phy to the phy namespace of the upstream phy. As the module phy 1371 * will be destroyed, re-inserting the same module will add a new phy with a 1372 * new index. 1373 */ 1374 void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy) 1375 { 1376 struct phy_device *phydev = upstream; 1377 struct net_device *dev = phydev->attached_dev; 1378 1379 if (dev) 1380 phy_link_topo_del_phy(dev, phy); 1381 } 1382 EXPORT_SYMBOL(phy_sfp_disconnect_phy); 1383 1384 /** 1385 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device 1386 * @upstream: pointer to the phy device 1387 * @bus: sfp bus representing cage being attached 1388 * 1389 * This is used to fill in the sfp_upstream_ops .attach member. 1390 */ 1391 void phy_sfp_attach(void *upstream, struct sfp_bus *bus) 1392 { 1393 struct phy_device *phydev = upstream; 1394 1395 if (phydev->attached_dev) 1396 phydev->attached_dev->sfp_bus = bus; 1397 phydev->sfp_bus_attached = true; 1398 } 1399 EXPORT_SYMBOL(phy_sfp_attach); 1400 1401 /** 1402 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device 1403 * @upstream: pointer to the phy device 1404 * @bus: sfp bus representing cage being attached 1405 * 1406 * This is used to fill in the sfp_upstream_ops .detach member. 1407 */ 1408 void phy_sfp_detach(void *upstream, struct sfp_bus *bus) 1409 { 1410 struct phy_device *phydev = upstream; 1411 1412 if (phydev->attached_dev) 1413 phydev->attached_dev->sfp_bus = NULL; 1414 phydev->sfp_bus_attached = false; 1415 } 1416 EXPORT_SYMBOL(phy_sfp_detach); 1417 1418 /** 1419 * phy_sfp_probe - probe for a SFP cage attached to this PHY device 1420 * @phydev: Pointer to phy_device 1421 * @ops: SFP's upstream operations 1422 */ 1423 int phy_sfp_probe(struct phy_device *phydev, 1424 const struct sfp_upstream_ops *ops) 1425 { 1426 struct sfp_bus *bus; 1427 int ret = 0; 1428 1429 if (phydev->mdio.dev.fwnode) { 1430 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode); 1431 if (IS_ERR(bus)) 1432 return PTR_ERR(bus); 1433 1434 phydev->sfp_bus = bus; 1435 1436 ret = sfp_bus_add_upstream(bus, phydev, ops); 1437 sfp_bus_put(bus); 1438 } 1439 return ret; 1440 } 1441 EXPORT_SYMBOL(phy_sfp_probe); 1442 1443 static bool phy_drv_supports_irq(const struct phy_driver *phydrv) 1444 { 1445 return phydrv->config_intr && phydrv->handle_interrupt; 1446 } 1447 1448 /** 1449 * phy_attach_direct - attach a network device to a given PHY device pointer 1450 * @dev: network device to attach 1451 * @phydev: Pointer to phy_device to attach 1452 * @flags: PHY device's dev_flags 1453 * @interface: PHY device's interface 1454 * 1455 * Description: Called by drivers to attach to a particular PHY 1456 * device. The phy_device is found, and properly hooked up 1457 * to the phy_driver. If no driver is attached, then a 1458 * generic driver is used. The phy_device is given a ptr to 1459 * the attaching device, and given a callback for link status 1460 * change. The phy_device is returned to the attaching driver. 1461 * This function takes a reference on the phy device. 1462 */ 1463 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, 1464 u32 flags, phy_interface_t interface) 1465 { 1466 struct mii_bus *bus = phydev->mdio.bus; 1467 struct device *d = &phydev->mdio.dev; 1468 struct module *ndev_owner = NULL; 1469 bool using_genphy = false; 1470 int err; 1471 1472 /* For Ethernet device drivers that register their own MDIO bus, we 1473 * will have bus->owner match ndev_mod, so we do not want to increment 1474 * our own module->refcnt here, otherwise we would not be able to 1475 * unload later on. 1476 */ 1477 if (dev) 1478 ndev_owner = dev->dev.parent->driver->owner; 1479 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { 1480 phydev_err(phydev, "failed to get the bus module\n"); 1481 return -EIO; 1482 } 1483 1484 get_device(d); 1485 1486 /* Assume that if there is no driver, that it doesn't 1487 * exist, and we should use the genphy driver. 1488 */ 1489 if (!d->driver) { 1490 if (phydev->is_c45) 1491 d->driver = &genphy_c45_driver.mdiodrv.driver; 1492 else 1493 d->driver = &genphy_driver.mdiodrv.driver; 1494 1495 using_genphy = true; 1496 } 1497 1498 if (!try_module_get(d->driver->owner)) { 1499 phydev_err(phydev, "failed to get the device driver module\n"); 1500 err = -EIO; 1501 goto error_put_device; 1502 } 1503 1504 if (using_genphy) { 1505 err = d->driver->probe(d); 1506 if (err >= 0) 1507 err = device_bind_driver(d); 1508 1509 if (err) 1510 goto error_module_put; 1511 } 1512 1513 if (phydev->attached_dev) { 1514 dev_err(&dev->dev, "PHY already attached\n"); 1515 err = -EBUSY; 1516 goto error; 1517 } 1518 1519 phydev->phy_link_change = phy_link_change; 1520 if (dev) { 1521 phydev->attached_dev = dev; 1522 dev->phydev = phydev; 1523 1524 if (phydev->sfp_bus_attached) 1525 dev->sfp_bus = phydev->sfp_bus; 1526 1527 err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev); 1528 if (err) 1529 goto error; 1530 } 1531 1532 /* Some Ethernet drivers try to connect to a PHY device before 1533 * calling register_netdevice() -> netdev_register_kobject() and 1534 * does the dev->dev.kobj initialization. Here we only check for 1535 * success which indicates that the network device kobject is 1536 * ready. Once we do that we still need to keep track of whether 1537 * links were successfully set up or not for phy_detach() to 1538 * remove them accordingly. 1539 */ 1540 phydev->sysfs_links = false; 1541 1542 phy_sysfs_create_links(phydev); 1543 1544 if (!phydev->attached_dev) { 1545 err = sysfs_create_file(&phydev->mdio.dev.kobj, 1546 &dev_attr_phy_standalone.attr); 1547 if (err) 1548 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n"); 1549 } 1550 1551 phydev->dev_flags |= flags; 1552 1553 phydev->interface = interface; 1554 1555 phydev->state = PHY_READY; 1556 1557 phydev->interrupts = PHY_INTERRUPT_DISABLED; 1558 1559 /* PHYs can request to use poll mode even though they have an 1560 * associated interrupt line. This could be the case if they 1561 * detect a broken interrupt handling. 1562 */ 1563 if (phydev->dev_flags & PHY_F_NO_IRQ) 1564 phydev->irq = PHY_POLL; 1565 1566 if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev)) 1567 phydev->irq = PHY_POLL; 1568 1569 /* Port is set to PORT_TP by default and the actual PHY driver will set 1570 * it to different value depending on the PHY configuration. If we have 1571 * the generic PHY driver we can't figure it out, thus set the old 1572 * legacy PORT_MII value. 1573 */ 1574 if (using_genphy) 1575 phydev->port = PORT_MII; 1576 1577 /* Initial carrier state is off as the phy is about to be 1578 * (re)initialized. 1579 */ 1580 if (dev) 1581 netif_carrier_off(phydev->attached_dev); 1582 1583 /* Do initial configuration here, now that 1584 * we have certain key parameters 1585 * (dev_flags and interface) 1586 */ 1587 err = phy_init_hw(phydev); 1588 if (err) 1589 goto error; 1590 1591 phy_resume(phydev); 1592 if (!phydev->is_on_sfp_module) 1593 phy_led_triggers_register(phydev); 1594 1595 /** 1596 * If the external phy used by current mac interface is managed by 1597 * another mac interface, so we should create a device link between 1598 * phy dev and mac dev. 1599 */ 1600 if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent) 1601 phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev, 1602 DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS); 1603 1604 return err; 1605 1606 error: 1607 /* phy_detach() does all of the cleanup below */ 1608 phy_detach(phydev); 1609 return err; 1610 1611 error_module_put: 1612 module_put(d->driver->owner); 1613 d->driver = NULL; 1614 error_put_device: 1615 put_device(d); 1616 if (ndev_owner != bus->owner) 1617 module_put(bus->owner); 1618 return err; 1619 } 1620 EXPORT_SYMBOL(phy_attach_direct); 1621 1622 /** 1623 * phy_attach - attach a network device to a particular PHY device 1624 * @dev: network device to attach 1625 * @bus_id: Bus ID of PHY device to attach 1626 * @interface: PHY device's interface 1627 * 1628 * Description: Same as phy_attach_direct() except that a PHY bus_id 1629 * string is passed instead of a pointer to a struct phy_device. 1630 */ 1631 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, 1632 phy_interface_t interface) 1633 { 1634 struct phy_device *phydev; 1635 struct device *d; 1636 int rc; 1637 1638 if (!dev) 1639 return ERR_PTR(-EINVAL); 1640 1641 /* Search the list of PHY devices on the mdio bus for the 1642 * PHY with the requested name 1643 */ 1644 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); 1645 if (!d) { 1646 pr_err("PHY %s not found\n", bus_id); 1647 return ERR_PTR(-ENODEV); 1648 } 1649 phydev = to_phy_device(d); 1650 1651 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 1652 put_device(d); 1653 if (rc) 1654 return ERR_PTR(rc); 1655 1656 return phydev; 1657 } 1658 EXPORT_SYMBOL(phy_attach); 1659 1660 static bool phy_driver_is_genphy_kind(struct phy_device *phydev, 1661 struct device_driver *driver) 1662 { 1663 struct device *d = &phydev->mdio.dev; 1664 bool ret = false; 1665 1666 if (!phydev->drv) 1667 return ret; 1668 1669 get_device(d); 1670 ret = d->driver == driver; 1671 put_device(d); 1672 1673 return ret; 1674 } 1675 1676 bool phy_driver_is_genphy(struct phy_device *phydev) 1677 { 1678 return phy_driver_is_genphy_kind(phydev, 1679 &genphy_driver.mdiodrv.driver); 1680 } 1681 EXPORT_SYMBOL_GPL(phy_driver_is_genphy); 1682 1683 bool phy_driver_is_genphy_10g(struct phy_device *phydev) 1684 { 1685 return phy_driver_is_genphy_kind(phydev, 1686 &genphy_c45_driver.mdiodrv.driver); 1687 } 1688 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); 1689 1690 /** 1691 * phy_detach - detach a PHY device from its network device 1692 * @phydev: target phy_device struct 1693 * 1694 * This detaches the phy device from its network device and the phy 1695 * driver, and drops the reference count taken in phy_attach_direct(). 1696 */ 1697 void phy_detach(struct phy_device *phydev) 1698 { 1699 struct net_device *dev = phydev->attached_dev; 1700 struct module *ndev_owner = NULL; 1701 struct mii_bus *bus; 1702 1703 if (phydev->devlink) 1704 device_link_del(phydev->devlink); 1705 1706 if (phydev->sysfs_links) { 1707 if (dev) 1708 sysfs_remove_link(&dev->dev.kobj, "phydev"); 1709 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); 1710 } 1711 1712 if (!phydev->attached_dev) 1713 sysfs_remove_file(&phydev->mdio.dev.kobj, 1714 &dev_attr_phy_standalone.attr); 1715 1716 phy_suspend(phydev); 1717 if (dev) { 1718 struct hwtstamp_provider *hwprov; 1719 1720 hwprov = rtnl_dereference(dev->hwprov); 1721 /* Disable timestamp if it is the one selected */ 1722 if (hwprov && hwprov->phydev == phydev) { 1723 rcu_assign_pointer(dev->hwprov, NULL); 1724 kfree_rcu(hwprov, rcu_head); 1725 } 1726 1727 phydev->attached_dev->phydev = NULL; 1728 phydev->attached_dev = NULL; 1729 phy_link_topo_del_phy(dev, phydev); 1730 } 1731 phydev->phylink = NULL; 1732 1733 if (!phydev->is_on_sfp_module) 1734 phy_led_triggers_unregister(phydev); 1735 1736 if (phydev->mdio.dev.driver) 1737 module_put(phydev->mdio.dev.driver->owner); 1738 1739 /* If the device had no specific driver before (i.e. - it 1740 * was using the generic driver), we unbind the device 1741 * from the generic driver so that there's a chance a 1742 * real driver could be loaded 1743 */ 1744 if (phy_driver_is_genphy(phydev) || 1745 phy_driver_is_genphy_10g(phydev)) 1746 device_release_driver(&phydev->mdio.dev); 1747 1748 /* Assert the reset signal */ 1749 phy_device_reset(phydev, 1); 1750 1751 /* 1752 * The phydev might go away on the put_device() below, so avoid 1753 * a use-after-free bug by reading the underlying bus first. 1754 */ 1755 bus = phydev->mdio.bus; 1756 1757 put_device(&phydev->mdio.dev); 1758 if (dev) 1759 ndev_owner = dev->dev.parent->driver->owner; 1760 if (ndev_owner != bus->owner) 1761 module_put(bus->owner); 1762 } 1763 EXPORT_SYMBOL(phy_detach); 1764 1765 int phy_suspend(struct phy_device *phydev) 1766 { 1767 struct net_device *netdev = phydev->attached_dev; 1768 const struct phy_driver *phydrv = phydev->drv; 1769 int ret; 1770 1771 if (phydev->suspended || !phydrv) 1772 return 0; 1773 1774 phydev->wol_enabled = phy_drv_wol_enabled(phydev) || 1775 (netdev && netdev->ethtool->wol_enabled); 1776 /* If the device has WOL enabled, we cannot suspend the PHY */ 1777 if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND)) 1778 return -EBUSY; 1779 1780 if (!phydrv->suspend) 1781 return 0; 1782 1783 ret = phydrv->suspend(phydev); 1784 if (!ret) 1785 phydev->suspended = true; 1786 1787 return ret; 1788 } 1789 EXPORT_SYMBOL(phy_suspend); 1790 1791 int __phy_resume(struct phy_device *phydev) 1792 { 1793 const struct phy_driver *phydrv = phydev->drv; 1794 int ret; 1795 1796 lockdep_assert_held(&phydev->lock); 1797 1798 if (!phydrv || !phydrv->resume) 1799 return 0; 1800 1801 ret = phydrv->resume(phydev); 1802 if (!ret) 1803 phydev->suspended = false; 1804 1805 return ret; 1806 } 1807 EXPORT_SYMBOL(__phy_resume); 1808 1809 int phy_resume(struct phy_device *phydev) 1810 { 1811 int ret; 1812 1813 mutex_lock(&phydev->lock); 1814 ret = __phy_resume(phydev); 1815 mutex_unlock(&phydev->lock); 1816 1817 return ret; 1818 } 1819 EXPORT_SYMBOL(phy_resume); 1820 1821 int phy_loopback(struct phy_device *phydev, bool enable) 1822 { 1823 int ret = 0; 1824 1825 if (!phydev->drv) 1826 return -EIO; 1827 1828 mutex_lock(&phydev->lock); 1829 1830 if (enable && phydev->loopback_enabled) { 1831 ret = -EBUSY; 1832 goto out; 1833 } 1834 1835 if (!enable && !phydev->loopback_enabled) { 1836 ret = -EINVAL; 1837 goto out; 1838 } 1839 1840 if (phydev->drv->set_loopback) 1841 ret = phydev->drv->set_loopback(phydev, enable); 1842 else 1843 ret = genphy_loopback(phydev, enable); 1844 1845 if (ret) 1846 goto out; 1847 1848 phydev->loopback_enabled = enable; 1849 1850 out: 1851 mutex_unlock(&phydev->lock); 1852 return ret; 1853 } 1854 EXPORT_SYMBOL(phy_loopback); 1855 1856 /** 1857 * phy_reset_after_clk_enable - perform a PHY reset if needed 1858 * @phydev: target phy_device struct 1859 * 1860 * Description: Some PHYs are known to need a reset after their refclk was 1861 * enabled. This function evaluates the flags and perform the reset if it's 1862 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy 1863 * was reset. 1864 */ 1865 int phy_reset_after_clk_enable(struct phy_device *phydev) 1866 { 1867 if (!phydev || !phydev->drv) 1868 return -ENODEV; 1869 1870 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { 1871 phy_device_reset(phydev, 1); 1872 phy_device_reset(phydev, 0); 1873 return 1; 1874 } 1875 1876 return 0; 1877 } 1878 EXPORT_SYMBOL(phy_reset_after_clk_enable); 1879 1880 /* Generic PHY support and helper functions */ 1881 1882 /** 1883 * genphy_config_advert - sanitize and advertise auto-negotiation parameters 1884 * @phydev: target phy_device struct 1885 * @advert: auto-negotiation parameters to advertise 1886 * 1887 * Description: Writes MII_ADVERTISE with the appropriate values, 1888 * after sanitizing the values to make sure we only advertise 1889 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 1890 * hasn't changed, and > 0 if it has changed. 1891 */ 1892 static int genphy_config_advert(struct phy_device *phydev, 1893 const unsigned long *advert) 1894 { 1895 int err, bmsr, changed = 0; 1896 u32 adv; 1897 1898 adv = linkmode_adv_to_mii_adv_t(advert); 1899 1900 /* Setup standard advertisement */ 1901 err = phy_modify_changed(phydev, MII_ADVERTISE, 1902 ADVERTISE_ALL | ADVERTISE_100BASE4 | 1903 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, 1904 adv); 1905 if (err < 0) 1906 return err; 1907 if (err > 0) 1908 changed = 1; 1909 1910 bmsr = phy_read(phydev, MII_BMSR); 1911 if (bmsr < 0) 1912 return bmsr; 1913 1914 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all 1915 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a 1916 * logical 1. 1917 */ 1918 if (!(bmsr & BMSR_ESTATEN)) 1919 return changed; 1920 1921 adv = linkmode_adv_to_mii_ctrl1000_t(advert); 1922 1923 err = phy_modify_changed(phydev, MII_CTRL1000, 1924 ADVERTISE_1000FULL | ADVERTISE_1000HALF, 1925 adv); 1926 if (err < 0) 1927 return err; 1928 if (err > 0) 1929 changed = 1; 1930 1931 return changed; 1932 } 1933 1934 /** 1935 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters 1936 * @phydev: target phy_device struct 1937 * 1938 * Description: Writes MII_ADVERTISE with the appropriate values, 1939 * after sanitizing the values to make sure we only advertise 1940 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 1941 * hasn't changed, and > 0 if it has changed. This function is intended 1942 * for Clause 37 1000Base-X mode. 1943 */ 1944 static int genphy_c37_config_advert(struct phy_device *phydev) 1945 { 1946 u16 adv = 0; 1947 1948 /* Only allow advertising what this PHY supports */ 1949 linkmode_and(phydev->advertising, phydev->advertising, 1950 phydev->supported); 1951 1952 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 1953 phydev->advertising)) 1954 adv |= ADVERTISE_1000XFULL; 1955 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 1956 phydev->advertising)) 1957 adv |= ADVERTISE_1000XPAUSE; 1958 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 1959 phydev->advertising)) 1960 adv |= ADVERTISE_1000XPSE_ASYM; 1961 1962 return phy_modify_changed(phydev, MII_ADVERTISE, 1963 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 1964 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM, 1965 adv); 1966 } 1967 1968 /** 1969 * genphy_setup_forced - configures/forces speed/duplex from @phydev 1970 * @phydev: target phy_device struct 1971 * 1972 * Description: Configures MII_BMCR to force speed/duplex 1973 * to the values in phydev. Assumes that the values are valid. 1974 * Please see phy_sanitize_settings(). 1975 */ 1976 int genphy_setup_forced(struct phy_device *phydev) 1977 { 1978 u16 ctl; 1979 1980 phydev->pause = 0; 1981 phydev->asym_pause = 0; 1982 1983 ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 1984 1985 return phy_modify(phydev, MII_BMCR, 1986 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); 1987 } 1988 EXPORT_SYMBOL(genphy_setup_forced); 1989 1990 static int genphy_setup_master_slave(struct phy_device *phydev) 1991 { 1992 u16 ctl = 0; 1993 1994 if (!phydev->is_gigabit_capable) 1995 return 0; 1996 1997 switch (phydev->master_slave_set) { 1998 case MASTER_SLAVE_CFG_MASTER_PREFERRED: 1999 ctl |= CTL1000_PREFER_MASTER; 2000 break; 2001 case MASTER_SLAVE_CFG_SLAVE_PREFERRED: 2002 break; 2003 case MASTER_SLAVE_CFG_MASTER_FORCE: 2004 ctl |= CTL1000_AS_MASTER; 2005 fallthrough; 2006 case MASTER_SLAVE_CFG_SLAVE_FORCE: 2007 ctl |= CTL1000_ENABLE_MASTER; 2008 break; 2009 case MASTER_SLAVE_CFG_UNKNOWN: 2010 case MASTER_SLAVE_CFG_UNSUPPORTED: 2011 return 0; 2012 default: 2013 phydev_warn(phydev, "Unsupported Master/Slave mode\n"); 2014 return -EOPNOTSUPP; 2015 } 2016 2017 return phy_modify_changed(phydev, MII_CTRL1000, 2018 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER | 2019 CTL1000_PREFER_MASTER), ctl); 2020 } 2021 2022 int genphy_read_master_slave(struct phy_device *phydev) 2023 { 2024 int cfg, state; 2025 int val; 2026 2027 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN; 2028 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN; 2029 2030 val = phy_read(phydev, MII_CTRL1000); 2031 if (val < 0) 2032 return val; 2033 2034 if (val & CTL1000_ENABLE_MASTER) { 2035 if (val & CTL1000_AS_MASTER) 2036 cfg = MASTER_SLAVE_CFG_MASTER_FORCE; 2037 else 2038 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE; 2039 } else { 2040 if (val & CTL1000_PREFER_MASTER) 2041 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED; 2042 else 2043 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED; 2044 } 2045 2046 val = phy_read(phydev, MII_STAT1000); 2047 if (val < 0) 2048 return val; 2049 2050 if (val & LPA_1000MSFAIL) { 2051 state = MASTER_SLAVE_STATE_ERR; 2052 } else if (phydev->link) { 2053 /* this bits are valid only for active link */ 2054 if (val & LPA_1000MSRES) 2055 state = MASTER_SLAVE_STATE_MASTER; 2056 else 2057 state = MASTER_SLAVE_STATE_SLAVE; 2058 } else { 2059 state = MASTER_SLAVE_STATE_UNKNOWN; 2060 } 2061 2062 phydev->master_slave_get = cfg; 2063 phydev->master_slave_state = state; 2064 2065 return 0; 2066 } 2067 EXPORT_SYMBOL(genphy_read_master_slave); 2068 2069 /** 2070 * genphy_restart_aneg - Enable and Restart Autonegotiation 2071 * @phydev: target phy_device struct 2072 */ 2073 int genphy_restart_aneg(struct phy_device *phydev) 2074 { 2075 /* Don't isolate the PHY if we're negotiating */ 2076 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, 2077 BMCR_ANENABLE | BMCR_ANRESTART); 2078 } 2079 EXPORT_SYMBOL(genphy_restart_aneg); 2080 2081 /** 2082 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation 2083 * @phydev: target phy_device struct 2084 * @restart: whether aneg restart is requested 2085 * 2086 * Check, and restart auto-negotiation if needed. 2087 */ 2088 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart) 2089 { 2090 int ret; 2091 2092 if (!restart) { 2093 /* Advertisement hasn't changed, but maybe aneg was never on to 2094 * begin with? Or maybe phy was isolated? 2095 */ 2096 ret = phy_read(phydev, MII_BMCR); 2097 if (ret < 0) 2098 return ret; 2099 2100 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE)) 2101 restart = true; 2102 } 2103 2104 if (restart) 2105 return genphy_restart_aneg(phydev); 2106 2107 return 0; 2108 } 2109 EXPORT_SYMBOL(genphy_check_and_restart_aneg); 2110 2111 /** 2112 * __genphy_config_aneg - restart auto-negotiation or write BMCR 2113 * @phydev: target phy_device struct 2114 * @changed: whether autoneg is requested 2115 * 2116 * Description: If auto-negotiation is enabled, we configure the 2117 * advertising, and then restart auto-negotiation. If it is not 2118 * enabled, then we write the BMCR. 2119 */ 2120 int __genphy_config_aneg(struct phy_device *phydev, bool changed) 2121 { 2122 __ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert); 2123 const struct link_capabilities *c; 2124 unsigned long *advert; 2125 int err; 2126 2127 err = genphy_c45_an_config_eee_aneg(phydev); 2128 if (err < 0) 2129 return err; 2130 else if (err) 2131 changed = true; 2132 2133 err = genphy_setup_master_slave(phydev); 2134 if (err < 0) 2135 return err; 2136 else if (err) 2137 changed = true; 2138 2139 if (phydev->autoneg == AUTONEG_ENABLE) { 2140 /* Only allow advertising what this PHY supports */ 2141 linkmode_and(phydev->advertising, phydev->advertising, 2142 phydev->supported); 2143 advert = phydev->advertising; 2144 } else if (phydev->speed < SPEED_1000) { 2145 return genphy_setup_forced(phydev); 2146 } else { 2147 linkmode_zero(fixed_advert); 2148 2149 c = phy_caps_lookup(phydev->speed, phydev->duplex, 2150 phydev->supported, true); 2151 if (c) 2152 linkmode_and(fixed_advert, phydev->supported, 2153 c->linkmodes); 2154 2155 advert = fixed_advert; 2156 } 2157 2158 err = genphy_config_advert(phydev, advert); 2159 if (err < 0) /* error */ 2160 return err; 2161 else if (err) 2162 changed = true; 2163 2164 return genphy_check_and_restart_aneg(phydev, changed); 2165 } 2166 EXPORT_SYMBOL(__genphy_config_aneg); 2167 2168 /** 2169 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR 2170 * @phydev: target phy_device struct 2171 * 2172 * Description: If auto-negotiation is enabled, we configure the 2173 * advertising, and then restart auto-negotiation. If it is not 2174 * enabled, then we write the BMCR. This function is intended 2175 * for use with Clause 37 1000Base-X mode. 2176 */ 2177 int genphy_c37_config_aneg(struct phy_device *phydev) 2178 { 2179 int err, changed; 2180 2181 if (phydev->autoneg != AUTONEG_ENABLE) 2182 return genphy_setup_forced(phydev); 2183 2184 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100, 2185 BMCR_SPEED1000); 2186 if (err) 2187 return err; 2188 2189 changed = genphy_c37_config_advert(phydev); 2190 if (changed < 0) /* error */ 2191 return changed; 2192 2193 if (!changed) { 2194 /* Advertisement hasn't changed, but maybe aneg was never on to 2195 * begin with? Or maybe phy was isolated? 2196 */ 2197 int ctl = phy_read(phydev, MII_BMCR); 2198 2199 if (ctl < 0) 2200 return ctl; 2201 2202 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) 2203 changed = 1; /* do restart aneg */ 2204 } 2205 2206 /* Only restart aneg if we are advertising something different 2207 * than we were before. 2208 */ 2209 if (changed > 0) 2210 return genphy_restart_aneg(phydev); 2211 2212 return 0; 2213 } 2214 EXPORT_SYMBOL(genphy_c37_config_aneg); 2215 2216 /** 2217 * genphy_aneg_done - return auto-negotiation status 2218 * @phydev: target phy_device struct 2219 * 2220 * Description: Reads the status register and returns 0 either if 2221 * auto-negotiation is incomplete, or if there was an error. 2222 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. 2223 */ 2224 int genphy_aneg_done(struct phy_device *phydev) 2225 { 2226 int retval = phy_read(phydev, MII_BMSR); 2227 2228 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); 2229 } 2230 EXPORT_SYMBOL(genphy_aneg_done); 2231 2232 /** 2233 * genphy_update_link - update link status in @phydev 2234 * @phydev: target phy_device struct 2235 * 2236 * Description: Update the value in phydev->link to reflect the 2237 * current link value. In order to do this, we need to read 2238 * the status register twice, keeping the second value. 2239 */ 2240 int genphy_update_link(struct phy_device *phydev) 2241 { 2242 int status = 0, bmcr; 2243 2244 bmcr = phy_read(phydev, MII_BMCR); 2245 if (bmcr < 0) 2246 return bmcr; 2247 2248 /* Autoneg is being started, therefore disregard BMSR value and 2249 * report link as down. 2250 */ 2251 if (bmcr & BMCR_ANRESTART) 2252 goto done; 2253 2254 /* The link state is latched low so that momentary link 2255 * drops can be detected. Do not double-read the status 2256 * in polling mode to detect such short link drops except 2257 * the link was already down. 2258 */ 2259 if (!phy_polling_mode(phydev) || !phydev->link) { 2260 status = phy_read(phydev, MII_BMSR); 2261 if (status < 0) 2262 return status; 2263 else if (status & BMSR_LSTATUS) 2264 goto done; 2265 } 2266 2267 /* Read link and autonegotiation status */ 2268 status = phy_read(phydev, MII_BMSR); 2269 if (status < 0) 2270 return status; 2271 done: 2272 phydev->link = status & BMSR_LSTATUS ? 1 : 0; 2273 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0; 2274 2275 /* Consider the case that autoneg was started and "aneg complete" 2276 * bit has been reset, but "link up" bit not yet. 2277 */ 2278 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete) 2279 phydev->link = 0; 2280 2281 return 0; 2282 } 2283 EXPORT_SYMBOL(genphy_update_link); 2284 2285 int genphy_read_lpa(struct phy_device *phydev) 2286 { 2287 int lpa, lpagb; 2288 2289 if (phydev->autoneg == AUTONEG_ENABLE) { 2290 if (!phydev->autoneg_complete) { 2291 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2292 0); 2293 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0); 2294 return 0; 2295 } 2296 2297 if (phydev->is_gigabit_capable) { 2298 lpagb = phy_read(phydev, MII_STAT1000); 2299 if (lpagb < 0) 2300 return lpagb; 2301 2302 if (lpagb & LPA_1000MSFAIL) { 2303 int adv = phy_read(phydev, MII_CTRL1000); 2304 2305 if (adv < 0) 2306 return adv; 2307 2308 if (adv & CTL1000_ENABLE_MASTER) 2309 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); 2310 else 2311 phydev_err(phydev, "Master/Slave resolution failed\n"); 2312 return -ENOLINK; 2313 } 2314 2315 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2316 lpagb); 2317 } 2318 2319 lpa = phy_read(phydev, MII_LPA); 2320 if (lpa < 0) 2321 return lpa; 2322 2323 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); 2324 } else { 2325 linkmode_zero(phydev->lp_advertising); 2326 } 2327 2328 return 0; 2329 } 2330 EXPORT_SYMBOL(genphy_read_lpa); 2331 2332 /** 2333 * genphy_read_status_fixed - read the link parameters for !aneg mode 2334 * @phydev: target phy_device struct 2335 * 2336 * Read the current duplex and speed state for a PHY operating with 2337 * autonegotiation disabled. 2338 */ 2339 int genphy_read_status_fixed(struct phy_device *phydev) 2340 { 2341 int bmcr = phy_read(phydev, MII_BMCR); 2342 2343 if (bmcr < 0) 2344 return bmcr; 2345 2346 if (bmcr & BMCR_FULLDPLX) 2347 phydev->duplex = DUPLEX_FULL; 2348 else 2349 phydev->duplex = DUPLEX_HALF; 2350 2351 if (bmcr & BMCR_SPEED1000) 2352 phydev->speed = SPEED_1000; 2353 else if (bmcr & BMCR_SPEED100) 2354 phydev->speed = SPEED_100; 2355 else 2356 phydev->speed = SPEED_10; 2357 2358 return 0; 2359 } 2360 EXPORT_SYMBOL(genphy_read_status_fixed); 2361 2362 /** 2363 * genphy_read_status - check the link status and update current link state 2364 * @phydev: target phy_device struct 2365 * 2366 * Description: Check the link, then figure out the current state 2367 * by comparing what we advertise with what the link partner 2368 * advertises. Start by checking the gigabit possibilities, 2369 * then move on to 10/100. 2370 */ 2371 int genphy_read_status(struct phy_device *phydev) 2372 { 2373 int err, old_link = phydev->link; 2374 2375 /* Update the link, but return if there was an error */ 2376 err = genphy_update_link(phydev); 2377 if (err) 2378 return err; 2379 2380 /* why bother the PHY if nothing can have changed */ 2381 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) 2382 return 0; 2383 2384 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED; 2385 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED; 2386 phydev->speed = SPEED_UNKNOWN; 2387 phydev->duplex = DUPLEX_UNKNOWN; 2388 phydev->pause = 0; 2389 phydev->asym_pause = 0; 2390 2391 if (phydev->is_gigabit_capable) { 2392 err = genphy_read_master_slave(phydev); 2393 if (err < 0) 2394 return err; 2395 } 2396 2397 err = genphy_read_lpa(phydev); 2398 if (err < 0) 2399 return err; 2400 2401 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2402 phy_resolve_aneg_linkmode(phydev); 2403 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2404 err = genphy_read_status_fixed(phydev); 2405 if (err < 0) 2406 return err; 2407 } 2408 2409 return 0; 2410 } 2411 EXPORT_SYMBOL(genphy_read_status); 2412 2413 /** 2414 * genphy_c37_read_status - check the link status and update current link state 2415 * @phydev: target phy_device struct 2416 * @changed: pointer where to store if link changed 2417 * 2418 * Description: Check the link, then figure out the current state 2419 * by comparing what we advertise with what the link partner 2420 * advertises. This function is for Clause 37 1000Base-X mode. 2421 * 2422 * If link has changed, @changed is set to true, false otherwise. 2423 */ 2424 int genphy_c37_read_status(struct phy_device *phydev, bool *changed) 2425 { 2426 int lpa, err, old_link = phydev->link; 2427 2428 /* Update the link, but return if there was an error */ 2429 err = genphy_update_link(phydev); 2430 if (err) 2431 return err; 2432 2433 /* why bother the PHY if nothing can have changed */ 2434 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) { 2435 *changed = false; 2436 return 0; 2437 } 2438 2439 /* Signal link has changed */ 2440 *changed = true; 2441 phydev->duplex = DUPLEX_UNKNOWN; 2442 phydev->pause = 0; 2443 phydev->asym_pause = 0; 2444 2445 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2446 lpa = phy_read(phydev, MII_LPA); 2447 if (lpa < 0) 2448 return lpa; 2449 2450 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 2451 phydev->lp_advertising, lpa & LPA_LPACK); 2452 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2453 phydev->lp_advertising, lpa & LPA_1000XFULL); 2454 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2455 phydev->lp_advertising, lpa & LPA_1000XPAUSE); 2456 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2457 phydev->lp_advertising, 2458 lpa & LPA_1000XPAUSE_ASYM); 2459 2460 phy_resolve_aneg_linkmode(phydev); 2461 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2462 int bmcr = phy_read(phydev, MII_BMCR); 2463 2464 if (bmcr < 0) 2465 return bmcr; 2466 2467 if (bmcr & BMCR_FULLDPLX) 2468 phydev->duplex = DUPLEX_FULL; 2469 else 2470 phydev->duplex = DUPLEX_HALF; 2471 } 2472 2473 return 0; 2474 } 2475 EXPORT_SYMBOL(genphy_c37_read_status); 2476 2477 /** 2478 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit 2479 * @phydev: target phy_device struct 2480 * 2481 * Description: Perform a software PHY reset using the standard 2482 * BMCR_RESET bit and poll for the reset bit to be cleared. 2483 * 2484 * Returns: 0 on success, < 0 on failure 2485 */ 2486 int genphy_soft_reset(struct phy_device *phydev) 2487 { 2488 u16 res = BMCR_RESET; 2489 int ret; 2490 2491 if (phydev->autoneg == AUTONEG_ENABLE) 2492 res |= BMCR_ANRESTART; 2493 2494 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res); 2495 if (ret < 0) 2496 return ret; 2497 2498 /* Clause 22 states that setting bit BMCR_RESET sets control registers 2499 * to their default value. Therefore the POWER DOWN bit is supposed to 2500 * be cleared after soft reset. 2501 */ 2502 phydev->suspended = 0; 2503 2504 ret = phy_poll_reset(phydev); 2505 if (ret) 2506 return ret; 2507 2508 /* BMCR may be reset to defaults */ 2509 if (phydev->autoneg == AUTONEG_DISABLE) 2510 ret = genphy_setup_forced(phydev); 2511 2512 return ret; 2513 } 2514 EXPORT_SYMBOL(genphy_soft_reset); 2515 2516 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev) 2517 { 2518 /* It seems there are cases where the interrupts are handled by another 2519 * entity (ie an IRQ controller embedded inside the PHY) and do not 2520 * need any other interraction from phylib. In this case, just trigger 2521 * the state machine directly. 2522 */ 2523 phy_trigger_machine(phydev); 2524 2525 return 0; 2526 } 2527 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack); 2528 2529 /** 2530 * genphy_read_abilities - read PHY abilities from Clause 22 registers 2531 * @phydev: target phy_device struct 2532 * 2533 * Description: Reads the PHY's abilities and populates 2534 * phydev->supported accordingly. 2535 * 2536 * Returns: 0 on success, < 0 on failure 2537 */ 2538 int genphy_read_abilities(struct phy_device *phydev) 2539 { 2540 int val; 2541 2542 linkmode_set_bit_array(phy_basic_ports_array, 2543 ARRAY_SIZE(phy_basic_ports_array), 2544 phydev->supported); 2545 2546 val = phy_read(phydev, MII_BMSR); 2547 if (val < 0) 2548 return val; 2549 2550 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported, 2551 val & BMSR_ANEGCAPABLE); 2552 2553 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported, 2554 val & BMSR_100FULL); 2555 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported, 2556 val & BMSR_100HALF); 2557 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported, 2558 val & BMSR_10FULL); 2559 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported, 2560 val & BMSR_10HALF); 2561 2562 if (val & BMSR_ESTATEN) { 2563 val = phy_read(phydev, MII_ESTATUS); 2564 if (val < 0) 2565 return val; 2566 2567 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 2568 phydev->supported, val & ESTATUS_1000_TFULL); 2569 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 2570 phydev->supported, val & ESTATUS_1000_THALF); 2571 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2572 phydev->supported, val & ESTATUS_1000_XFULL); 2573 } 2574 2575 /* This is optional functionality. If not supported, we may get an error 2576 * which should be ignored. 2577 */ 2578 genphy_c45_read_eee_abilities(phydev); 2579 2580 return 0; 2581 } 2582 EXPORT_SYMBOL(genphy_read_abilities); 2583 2584 /* This is used for the phy device which doesn't support the MMD extended 2585 * register access, but it does have side effect when we are trying to access 2586 * the MMD register via indirect method. 2587 */ 2588 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) 2589 { 2590 return -EOPNOTSUPP; 2591 } 2592 EXPORT_SYMBOL(genphy_read_mmd_unsupported); 2593 2594 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, 2595 u16 regnum, u16 val) 2596 { 2597 return -EOPNOTSUPP; 2598 } 2599 EXPORT_SYMBOL(genphy_write_mmd_unsupported); 2600 2601 int genphy_suspend(struct phy_device *phydev) 2602 { 2603 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); 2604 } 2605 EXPORT_SYMBOL(genphy_suspend); 2606 2607 int genphy_resume(struct phy_device *phydev) 2608 { 2609 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); 2610 } 2611 EXPORT_SYMBOL(genphy_resume); 2612 2613 int genphy_loopback(struct phy_device *phydev, bool enable) 2614 { 2615 if (enable) { 2616 u16 ctl = BMCR_LOOPBACK; 2617 int ret, val; 2618 2619 ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2620 2621 phy_modify(phydev, MII_BMCR, ~0, ctl); 2622 2623 ret = phy_read_poll_timeout(phydev, MII_BMSR, val, 2624 val & BMSR_LSTATUS, 2625 5000, 500000, true); 2626 if (ret) 2627 return ret; 2628 } else { 2629 phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0); 2630 2631 phy_config_aneg(phydev); 2632 } 2633 2634 return 0; 2635 } 2636 EXPORT_SYMBOL(genphy_loopback); 2637 2638 /** 2639 * phy_remove_link_mode - Remove a supported link mode 2640 * @phydev: phy_device structure to remove link mode from 2641 * @link_mode: Link mode to be removed 2642 * 2643 * Description: Some MACs don't support all link modes which the PHY 2644 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper 2645 * to remove a link mode. 2646 */ 2647 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) 2648 { 2649 linkmode_clear_bit(link_mode, phydev->supported); 2650 phy_advertise_supported(phydev); 2651 } 2652 EXPORT_SYMBOL(phy_remove_link_mode); 2653 2654 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src) 2655 { 2656 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst, 2657 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src)); 2658 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst, 2659 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src)); 2660 } 2661 2662 /** 2663 * phy_advertise_supported - Advertise all supported modes 2664 * @phydev: target phy_device struct 2665 * 2666 * Description: Called to advertise all supported modes, doesn't touch 2667 * pause mode advertising. 2668 */ 2669 void phy_advertise_supported(struct phy_device *phydev) 2670 { 2671 __ETHTOOL_DECLARE_LINK_MODE_MASK(new); 2672 2673 linkmode_copy(new, phydev->supported); 2674 phy_copy_pause_bits(new, phydev->advertising); 2675 linkmode_copy(phydev->advertising, new); 2676 } 2677 EXPORT_SYMBOL(phy_advertise_supported); 2678 2679 /** 2680 * phy_advertise_eee_all - Advertise all supported EEE modes 2681 * @phydev: target phy_device struct 2682 * 2683 * Description: Per default phylib preserves the EEE advertising at the time of 2684 * phy probing, which might be a subset of the supported EEE modes. Use this 2685 * function when all supported EEE modes should be advertised. This does not 2686 * trigger auto-negotiation, so must be called before phy_start()/ 2687 * phylink_start() which will start auto-negotiation. 2688 */ 2689 void phy_advertise_eee_all(struct phy_device *phydev) 2690 { 2691 linkmode_copy(phydev->advertising_eee, phydev->supported_eee); 2692 } 2693 EXPORT_SYMBOL_GPL(phy_advertise_eee_all); 2694 2695 /** 2696 * phy_support_eee - Set initial EEE policy configuration 2697 * @phydev: Target phy_device struct 2698 * 2699 * This function configures the initial policy for Energy Efficient Ethernet 2700 * (EEE) on the specified PHY device, influencing that EEE capabilities are 2701 * advertised before the link is established. It should be called during PHY 2702 * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs) 2703 * if MAC supports LPI or PHY is capable to compensate missing LPI functionality 2704 * of the MAC. 2705 * 2706 * The function sets default EEE policy parameters, including preparing the PHY 2707 * to advertise EEE capabilities based on hardware support. 2708 * 2709 * It also sets the expected configuration for Low Power Idle (LPI) in the MAC 2710 * driver. If the PHY framework determines that both local and remote 2711 * advertisements support EEE, and the negotiated link mode is compatible with 2712 * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on 2713 * this setting by enabling the LPI timer if enable_tx_lpi is set. 2714 */ 2715 void phy_support_eee(struct phy_device *phydev) 2716 { 2717 linkmode_copy(phydev->advertising_eee, phydev->supported_eee); 2718 phydev->eee_cfg.tx_lpi_enabled = true; 2719 phydev->eee_cfg.eee_enabled = true; 2720 } 2721 EXPORT_SYMBOL(phy_support_eee); 2722 2723 /** 2724 * phy_disable_eee - Disable EEE for the PHY 2725 * @phydev: Target phy_device struct 2726 * 2727 * This function is used by MAC drivers for MAC's which don't support EEE. 2728 * It disables EEE on the PHY layer. 2729 */ 2730 void phy_disable_eee(struct phy_device *phydev) 2731 { 2732 linkmode_zero(phydev->advertising_eee); 2733 phydev->eee_cfg.tx_lpi_enabled = false; 2734 phydev->eee_cfg.eee_enabled = false; 2735 /* don't let userspace re-enable EEE advertisement */ 2736 linkmode_fill(phydev->eee_disabled_modes); 2737 } 2738 EXPORT_SYMBOL_GPL(phy_disable_eee); 2739 2740 /** 2741 * phy_support_sym_pause - Enable support of symmetrical pause 2742 * @phydev: target phy_device struct 2743 * 2744 * Description: Called by the MAC to indicate is supports symmetrical 2745 * Pause, but not asym pause. 2746 */ 2747 void phy_support_sym_pause(struct phy_device *phydev) 2748 { 2749 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 2750 phy_copy_pause_bits(phydev->advertising, phydev->supported); 2751 } 2752 EXPORT_SYMBOL(phy_support_sym_pause); 2753 2754 /** 2755 * phy_support_asym_pause - Enable support of asym pause 2756 * @phydev: target phy_device struct 2757 * 2758 * Description: Called by the MAC to indicate is supports Asym Pause. 2759 */ 2760 void phy_support_asym_pause(struct phy_device *phydev) 2761 { 2762 phy_copy_pause_bits(phydev->advertising, phydev->supported); 2763 } 2764 EXPORT_SYMBOL(phy_support_asym_pause); 2765 2766 /** 2767 * phy_set_sym_pause - Configure symmetric Pause 2768 * @phydev: target phy_device struct 2769 * @rx: Receiver Pause is supported 2770 * @tx: Transmit Pause is supported 2771 * @autoneg: Auto neg should be used 2772 * 2773 * Description: Configure advertised Pause support depending on if 2774 * receiver pause and pause auto neg is supported. Generally called 2775 * from the set_pauseparam .ndo. 2776 */ 2777 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, 2778 bool autoneg) 2779 { 2780 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 2781 2782 if (rx && tx && autoneg) 2783 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2784 phydev->supported); 2785 2786 linkmode_copy(phydev->advertising, phydev->supported); 2787 } 2788 EXPORT_SYMBOL(phy_set_sym_pause); 2789 2790 /** 2791 * phy_set_asym_pause - Configure Pause and Asym Pause 2792 * @phydev: target phy_device struct 2793 * @rx: Receiver Pause is supported 2794 * @tx: Transmit Pause is supported 2795 * 2796 * Description: Configure advertised Pause support depending on if 2797 * transmit and receiver pause is supported. If there has been a 2798 * change in adverting, trigger a new autoneg. Generally called from 2799 * the set_pauseparam .ndo. 2800 */ 2801 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) 2802 { 2803 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); 2804 2805 linkmode_copy(oldadv, phydev->advertising); 2806 linkmode_set_pause(phydev->advertising, tx, rx); 2807 2808 if (!linkmode_equal(oldadv, phydev->advertising) && 2809 phydev->autoneg) 2810 phy_start_aneg(phydev); 2811 } 2812 EXPORT_SYMBOL(phy_set_asym_pause); 2813 2814 /** 2815 * phy_validate_pause - Test if the PHY/MAC support the pause configuration 2816 * @phydev: phy_device struct 2817 * @pp: requested pause configuration 2818 * 2819 * Description: Test if the PHY/MAC combination supports the Pause 2820 * configuration the user is requesting. Returns True if it is 2821 * supported, false otherwise. 2822 */ 2823 bool phy_validate_pause(struct phy_device *phydev, 2824 struct ethtool_pauseparam *pp) 2825 { 2826 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2827 phydev->supported) && pp->rx_pause) 2828 return false; 2829 2830 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2831 phydev->supported) && 2832 pp->rx_pause != pp->tx_pause) 2833 return false; 2834 2835 return true; 2836 } 2837 EXPORT_SYMBOL(phy_validate_pause); 2838 2839 /** 2840 * phy_get_pause - resolve negotiated pause modes 2841 * @phydev: phy_device struct 2842 * @tx_pause: pointer to bool to indicate whether transmit pause should be 2843 * enabled. 2844 * @rx_pause: pointer to bool to indicate whether receive pause should be 2845 * enabled. 2846 * 2847 * Resolve and return the flow control modes according to the negotiation 2848 * result. This includes checking that we are operating in full duplex mode. 2849 * See linkmode_resolve_pause() for further details. 2850 */ 2851 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause) 2852 { 2853 if (phydev->duplex != DUPLEX_FULL) { 2854 *tx_pause = false; 2855 *rx_pause = false; 2856 return; 2857 } 2858 2859 return linkmode_resolve_pause(phydev->advertising, 2860 phydev->lp_advertising, 2861 tx_pause, rx_pause); 2862 } 2863 EXPORT_SYMBOL(phy_get_pause); 2864 2865 #if IS_ENABLED(CONFIG_OF_MDIO) 2866 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 2867 { 2868 return device_property_read_u32(dev, name, val); 2869 } 2870 #else 2871 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 2872 { 2873 return -EINVAL; 2874 } 2875 #endif 2876 2877 /** 2878 * phy_get_internal_delay - returns the index of the internal delay 2879 * @phydev: phy_device struct 2880 * @dev: pointer to the devices device struct 2881 * @delay_values: array of delays the PHY supports 2882 * @size: the size of the delay array 2883 * @is_rx: boolean to indicate to get the rx internal delay 2884 * 2885 * Returns the index within the array of internal delay passed in. 2886 * If the device property is not present then the interface type is checked 2887 * if the interface defines use of internal delay then a 1 is returned otherwise 2888 * a 0 is returned. 2889 * The array must be in ascending order. If PHY does not have an ascending order 2890 * array then size = 0 and the value of the delay property is returned. 2891 * Return -EINVAL if the delay is invalid or cannot be found. 2892 */ 2893 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev, 2894 const int *delay_values, int size, bool is_rx) 2895 { 2896 int i, ret; 2897 u32 delay; 2898 2899 if (is_rx) { 2900 ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay); 2901 if (ret < 0 && size == 0) { 2902 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 2903 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) 2904 return 1; 2905 else 2906 return 0; 2907 } 2908 2909 } else { 2910 ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay); 2911 if (ret < 0 && size == 0) { 2912 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 2913 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) 2914 return 1; 2915 else 2916 return 0; 2917 } 2918 } 2919 2920 if (ret < 0) 2921 return ret; 2922 2923 if (size == 0) 2924 return delay; 2925 2926 if (delay < delay_values[0] || delay > delay_values[size - 1]) { 2927 phydev_err(phydev, "Delay %d is out of range\n", delay); 2928 return -EINVAL; 2929 } 2930 2931 if (delay == delay_values[0]) 2932 return 0; 2933 2934 for (i = 1; i < size; i++) { 2935 if (delay == delay_values[i]) 2936 return i; 2937 2938 /* Find an approximate index by looking up the table */ 2939 if (delay > delay_values[i - 1] && 2940 delay < delay_values[i]) { 2941 if (delay - delay_values[i - 1] < 2942 delay_values[i] - delay) 2943 return i - 1; 2944 else 2945 return i; 2946 } 2947 } 2948 2949 phydev_err(phydev, "error finding internal delay index for %d\n", 2950 delay); 2951 2952 return -EINVAL; 2953 } 2954 EXPORT_SYMBOL(phy_get_internal_delay); 2955 2956 /** 2957 * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val 2958 * @phydev: phy_device struct 2959 * @dev: pointer to the devices device struct 2960 * @linkmode: linkmode for which the tx amplitude gain should be retrieved 2961 * @val: tx amplitude gain 2962 * 2963 * Returns: 0 on success, < 0 on failure 2964 */ 2965 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev, 2966 enum ethtool_link_mode_bit_indices linkmode, 2967 u32 *val) 2968 { 2969 switch (linkmode) { 2970 case ETHTOOL_LINK_MODE_100baseT_Full_BIT: 2971 return phy_get_u32_property(dev, 2972 "tx-amplitude-100base-tx-percent", 2973 val); 2974 default: 2975 return -EINVAL; 2976 } 2977 } 2978 EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain); 2979 2980 static int phy_led_set_brightness(struct led_classdev *led_cdev, 2981 enum led_brightness value) 2982 { 2983 struct phy_led *phyled = to_phy_led(led_cdev); 2984 struct phy_device *phydev = phyled->phydev; 2985 int err; 2986 2987 mutex_lock(&phydev->lock); 2988 err = phydev->drv->led_brightness_set(phydev, phyled->index, value); 2989 mutex_unlock(&phydev->lock); 2990 2991 return err; 2992 } 2993 2994 static int phy_led_blink_set(struct led_classdev *led_cdev, 2995 unsigned long *delay_on, 2996 unsigned long *delay_off) 2997 { 2998 struct phy_led *phyled = to_phy_led(led_cdev); 2999 struct phy_device *phydev = phyled->phydev; 3000 int err; 3001 3002 mutex_lock(&phydev->lock); 3003 err = phydev->drv->led_blink_set(phydev, phyled->index, 3004 delay_on, delay_off); 3005 mutex_unlock(&phydev->lock); 3006 3007 return err; 3008 } 3009 3010 static __maybe_unused struct device * 3011 phy_led_hw_control_get_device(struct led_classdev *led_cdev) 3012 { 3013 struct phy_led *phyled = to_phy_led(led_cdev); 3014 struct phy_device *phydev = phyled->phydev; 3015 3016 if (phydev->attached_dev) 3017 return &phydev->attached_dev->dev; 3018 return NULL; 3019 } 3020 3021 static int __maybe_unused 3022 phy_led_hw_control_get(struct led_classdev *led_cdev, 3023 unsigned long *rules) 3024 { 3025 struct phy_led *phyled = to_phy_led(led_cdev); 3026 struct phy_device *phydev = phyled->phydev; 3027 int err; 3028 3029 mutex_lock(&phydev->lock); 3030 err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules); 3031 mutex_unlock(&phydev->lock); 3032 3033 return err; 3034 } 3035 3036 static int __maybe_unused 3037 phy_led_hw_control_set(struct led_classdev *led_cdev, 3038 unsigned long rules) 3039 { 3040 struct phy_led *phyled = to_phy_led(led_cdev); 3041 struct phy_device *phydev = phyled->phydev; 3042 int err; 3043 3044 mutex_lock(&phydev->lock); 3045 err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules); 3046 mutex_unlock(&phydev->lock); 3047 3048 return err; 3049 } 3050 3051 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev, 3052 unsigned long rules) 3053 { 3054 struct phy_led *phyled = to_phy_led(led_cdev); 3055 struct phy_device *phydev = phyled->phydev; 3056 int err; 3057 3058 mutex_lock(&phydev->lock); 3059 err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules); 3060 mutex_unlock(&phydev->lock); 3061 3062 return err; 3063 } 3064 3065 static void phy_leds_unregister(struct phy_device *phydev) 3066 { 3067 struct phy_led *phyled, *tmp; 3068 3069 list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) { 3070 led_classdev_unregister(&phyled->led_cdev); 3071 list_del(&phyled->list); 3072 } 3073 } 3074 3075 static int of_phy_led(struct phy_device *phydev, 3076 struct device_node *led) 3077 { 3078 struct device *dev = &phydev->mdio.dev; 3079 struct led_init_data init_data = {}; 3080 struct led_classdev *cdev; 3081 unsigned long modes = 0; 3082 struct phy_led *phyled; 3083 u32 index; 3084 int err; 3085 3086 phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL); 3087 if (!phyled) 3088 return -ENOMEM; 3089 3090 cdev = &phyled->led_cdev; 3091 phyled->phydev = phydev; 3092 3093 err = of_property_read_u32(led, "reg", &index); 3094 if (err) 3095 return err; 3096 if (index > U8_MAX) 3097 return -EINVAL; 3098 3099 if (of_property_read_bool(led, "active-high")) 3100 set_bit(PHY_LED_ACTIVE_HIGH, &modes); 3101 if (of_property_read_bool(led, "active-low")) 3102 set_bit(PHY_LED_ACTIVE_LOW, &modes); 3103 if (of_property_read_bool(led, "inactive-high-impedance")) 3104 set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes); 3105 3106 if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) && 3107 modes & BIT(PHY_LED_ACTIVE_HIGH))) 3108 return -EINVAL; 3109 3110 if (modes) { 3111 /* Return error if asked to set polarity modes but not supported */ 3112 if (!phydev->drv->led_polarity_set) 3113 return -EINVAL; 3114 3115 err = phydev->drv->led_polarity_set(phydev, index, modes); 3116 if (err) 3117 return err; 3118 } 3119 3120 phyled->index = index; 3121 if (phydev->drv->led_brightness_set) 3122 cdev->brightness_set_blocking = phy_led_set_brightness; 3123 if (phydev->drv->led_blink_set) 3124 cdev->blink_set = phy_led_blink_set; 3125 3126 #ifdef CONFIG_LEDS_TRIGGERS 3127 if (phydev->drv->led_hw_is_supported && 3128 phydev->drv->led_hw_control_set && 3129 phydev->drv->led_hw_control_get) { 3130 cdev->hw_control_is_supported = phy_led_hw_is_supported; 3131 cdev->hw_control_set = phy_led_hw_control_set; 3132 cdev->hw_control_get = phy_led_hw_control_get; 3133 cdev->hw_control_trigger = "netdev"; 3134 } 3135 3136 cdev->hw_control_get_device = phy_led_hw_control_get_device; 3137 #endif 3138 cdev->max_brightness = 1; 3139 init_data.devicename = dev_name(&phydev->mdio.dev); 3140 init_data.fwnode = of_fwnode_handle(led); 3141 init_data.devname_mandatory = true; 3142 3143 err = led_classdev_register_ext(dev, cdev, &init_data); 3144 if (err) 3145 return err; 3146 3147 list_add(&phyled->list, &phydev->leds); 3148 3149 return 0; 3150 } 3151 3152 static int of_phy_leds(struct phy_device *phydev) 3153 { 3154 struct device_node *node = phydev->mdio.dev.of_node; 3155 struct device_node *leds; 3156 int err; 3157 3158 if (!IS_ENABLED(CONFIG_OF_MDIO)) 3159 return 0; 3160 3161 if (!node) 3162 return 0; 3163 3164 leds = of_get_child_by_name(node, "leds"); 3165 if (!leds) 3166 return 0; 3167 3168 /* Check if the PHY driver have at least an OP to 3169 * set the LEDs. 3170 */ 3171 if (!(phydev->drv->led_brightness_set || 3172 phydev->drv->led_blink_set || 3173 phydev->drv->led_hw_control_set)) { 3174 phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n"); 3175 goto exit; 3176 } 3177 3178 for_each_available_child_of_node_scoped(leds, led) { 3179 err = of_phy_led(phydev, led); 3180 if (err) { 3181 of_node_put(leds); 3182 phy_leds_unregister(phydev); 3183 return err; 3184 } 3185 } 3186 3187 exit: 3188 of_node_put(leds); 3189 return 0; 3190 } 3191 3192 /** 3193 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device 3194 * @fwnode: pointer to the mdio_device's fwnode 3195 * 3196 * If successful, returns a pointer to the mdio_device with the embedded 3197 * struct device refcount incremented by one, or NULL on failure. 3198 * The caller should call put_device() on the mdio_device after its use. 3199 */ 3200 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode) 3201 { 3202 struct device *d; 3203 3204 if (!fwnode) 3205 return NULL; 3206 3207 d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode); 3208 if (!d) 3209 return NULL; 3210 3211 return to_mdio_device(d); 3212 } 3213 EXPORT_SYMBOL(fwnode_mdio_find_device); 3214 3215 /** 3216 * fwnode_phy_find_device - For provided phy_fwnode, find phy_device. 3217 * 3218 * @phy_fwnode: Pointer to the phy's fwnode. 3219 * 3220 * If successful, returns a pointer to the phy_device with the embedded 3221 * struct device refcount incremented by one, or NULL on failure. 3222 */ 3223 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) 3224 { 3225 struct mdio_device *mdiodev; 3226 3227 mdiodev = fwnode_mdio_find_device(phy_fwnode); 3228 if (!mdiodev) 3229 return NULL; 3230 3231 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) 3232 return to_phy_device(&mdiodev->dev); 3233 3234 put_device(&mdiodev->dev); 3235 3236 return NULL; 3237 } 3238 EXPORT_SYMBOL(fwnode_phy_find_device); 3239 3240 /** 3241 * device_phy_find_device - For the given device, get the phy_device 3242 * @dev: Pointer to the given device 3243 * 3244 * Refer return conditions of fwnode_phy_find_device(). 3245 */ 3246 struct phy_device *device_phy_find_device(struct device *dev) 3247 { 3248 return fwnode_phy_find_device(dev_fwnode(dev)); 3249 } 3250 EXPORT_SYMBOL_GPL(device_phy_find_device); 3251 3252 /** 3253 * fwnode_get_phy_node - Get the phy_node using the named reference. 3254 * @fwnode: Pointer to fwnode from which phy_node has to be obtained. 3255 * 3256 * Refer return conditions of fwnode_find_reference(). 3257 * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy" 3258 * and "phy-device" are not supported in ACPI. DT supports all the three 3259 * named references to the phy node. 3260 */ 3261 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode) 3262 { 3263 struct fwnode_handle *phy_node; 3264 3265 /* Only phy-handle is used for ACPI */ 3266 phy_node = fwnode_find_reference(fwnode, "phy-handle", 0); 3267 if (is_acpi_node(fwnode) || !IS_ERR(phy_node)) 3268 return phy_node; 3269 phy_node = fwnode_find_reference(fwnode, "phy", 0); 3270 if (IS_ERR(phy_node)) 3271 phy_node = fwnode_find_reference(fwnode, "phy-device", 0); 3272 return phy_node; 3273 } 3274 EXPORT_SYMBOL_GPL(fwnode_get_phy_node); 3275 3276 /** 3277 * phy_probe - probe and init a PHY device 3278 * @dev: device to probe and init 3279 * 3280 * Take care of setting up the phy_device structure, set the state to READY. 3281 */ 3282 static int phy_probe(struct device *dev) 3283 { 3284 struct phy_device *phydev = to_phy_device(dev); 3285 struct device_driver *drv = phydev->mdio.dev.driver; 3286 struct phy_driver *phydrv = to_phy_driver(drv); 3287 int err = 0; 3288 3289 phydev->drv = phydrv; 3290 3291 /* Disable the interrupt if the PHY doesn't support it 3292 * but the interrupt is still a valid one 3293 */ 3294 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) 3295 phydev->irq = PHY_POLL; 3296 3297 if (phydrv->flags & PHY_IS_INTERNAL) 3298 phydev->is_internal = true; 3299 3300 /* Deassert the reset signal */ 3301 phy_device_reset(phydev, 0); 3302 3303 if (phydev->drv->probe) { 3304 err = phydev->drv->probe(phydev); 3305 if (err) 3306 goto out; 3307 } 3308 3309 phy_disable_interrupts(phydev); 3310 3311 /* Start out supporting everything. Eventually, 3312 * a controller will attach, and may modify one 3313 * or both of these values 3314 */ 3315 if (phydrv->features) { 3316 linkmode_copy(phydev->supported, phydrv->features); 3317 genphy_c45_read_eee_abilities(phydev); 3318 } 3319 else if (phydrv->get_features) 3320 err = phydrv->get_features(phydev); 3321 else if (phydev->is_c45) 3322 err = genphy_c45_pma_read_abilities(phydev); 3323 else 3324 err = genphy_read_abilities(phydev); 3325 3326 if (err) 3327 goto out; 3328 3329 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 3330 phydev->supported)) 3331 phydev->autoneg = 0; 3332 3333 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 3334 phydev->supported)) 3335 phydev->is_gigabit_capable = 1; 3336 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 3337 phydev->supported)) 3338 phydev->is_gigabit_capable = 1; 3339 3340 of_set_phy_supported(phydev); 3341 phy_advertise_supported(phydev); 3342 3343 /* Get PHY default EEE advertising modes and handle them as potentially 3344 * safe initial configuration. 3345 */ 3346 err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee); 3347 if (err) 3348 goto out; 3349 3350 /* Get the EEE modes we want to prohibit. */ 3351 of_set_phy_eee_broken(phydev); 3352 3353 /* Some PHYs may advertise, by default, not support EEE modes. So, 3354 * we need to clean them. In addition remove all disabled EEE modes. 3355 */ 3356 linkmode_and(phydev->advertising_eee, phydev->supported_eee, 3357 phydev->advertising_eee); 3358 linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee, 3359 phydev->eee_disabled_modes); 3360 3361 /* There is no "enabled" flag. If PHY is advertising, assume it is 3362 * kind of enabled. 3363 */ 3364 phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee); 3365 3366 /* Get master/slave strap overrides */ 3367 of_set_phy_timing_role(phydev); 3368 3369 /* The Pause Frame bits indicate that the PHY can support passing 3370 * pause frames. During autonegotiation, the PHYs will determine if 3371 * they should allow pause frames to pass. The MAC driver should then 3372 * use that result to determine whether to enable flow control via 3373 * pause frames. 3374 * 3375 * Normally, PHY drivers should not set the Pause bits, and instead 3376 * allow phylib to do that. However, there may be some situations 3377 * (e.g. hardware erratum) where the driver wants to set only one 3378 * of these bits. 3379 */ 3380 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) && 3381 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) { 3382 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3383 phydev->supported); 3384 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3385 phydev->supported); 3386 } 3387 3388 /* Set the state to READY by default */ 3389 phydev->state = PHY_READY; 3390 3391 /* Get the LEDs from the device tree, and instantiate standard 3392 * LEDs for them. 3393 */ 3394 if (IS_ENABLED(CONFIG_PHYLIB_LEDS)) 3395 err = of_phy_leds(phydev); 3396 3397 out: 3398 /* Re-assert the reset signal on error */ 3399 if (err) 3400 phy_device_reset(phydev, 1); 3401 3402 return err; 3403 } 3404 3405 static int phy_remove(struct device *dev) 3406 { 3407 struct phy_device *phydev = to_phy_device(dev); 3408 3409 cancel_delayed_work_sync(&phydev->state_queue); 3410 3411 if (IS_ENABLED(CONFIG_PHYLIB_LEDS)) 3412 phy_leds_unregister(phydev); 3413 3414 phydev->state = PHY_DOWN; 3415 3416 sfp_bus_del_upstream(phydev->sfp_bus); 3417 phydev->sfp_bus = NULL; 3418 3419 if (phydev->drv && phydev->drv->remove) 3420 phydev->drv->remove(phydev); 3421 3422 /* Assert the reset signal */ 3423 phy_device_reset(phydev, 1); 3424 3425 phydev->drv = NULL; 3426 3427 return 0; 3428 } 3429 3430 /** 3431 * phy_driver_register - register a phy_driver with the PHY layer 3432 * @new_driver: new phy_driver to register 3433 * @owner: module owning this PHY 3434 */ 3435 int phy_driver_register(struct phy_driver *new_driver, struct module *owner) 3436 { 3437 int retval; 3438 3439 /* Either the features are hard coded, or dynamically 3440 * determined. It cannot be both. 3441 */ 3442 if (WARN_ON(new_driver->features && new_driver->get_features)) { 3443 pr_err("%s: features and get_features must not both be set\n", 3444 new_driver->name); 3445 return -EINVAL; 3446 } 3447 3448 /* PHYLIB device drivers must not match using a DT compatible table 3449 * as this bypasses our checks that the mdiodev that is being matched 3450 * is backed by a struct phy_device. If such a case happens, we will 3451 * make out-of-bounds accesses and lockup in phydev->lock. 3452 */ 3453 if (WARN(new_driver->mdiodrv.driver.of_match_table, 3454 "%s: driver must not provide a DT match table\n", 3455 new_driver->name)) 3456 return -EINVAL; 3457 3458 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; 3459 new_driver->mdiodrv.driver.name = new_driver->name; 3460 new_driver->mdiodrv.driver.bus = &mdio_bus_type; 3461 new_driver->mdiodrv.driver.probe = phy_probe; 3462 new_driver->mdiodrv.driver.remove = phy_remove; 3463 new_driver->mdiodrv.driver.owner = owner; 3464 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS; 3465 3466 retval = driver_register(&new_driver->mdiodrv.driver); 3467 if (retval) { 3468 pr_err("%s: Error %d in registering driver\n", 3469 new_driver->name, retval); 3470 3471 return retval; 3472 } 3473 3474 pr_debug("%s: Registered new driver\n", new_driver->name); 3475 3476 return 0; 3477 } 3478 EXPORT_SYMBOL(phy_driver_register); 3479 3480 int phy_drivers_register(struct phy_driver *new_driver, int n, 3481 struct module *owner) 3482 { 3483 int i, ret = 0; 3484 3485 for (i = 0; i < n; i++) { 3486 ret = phy_driver_register(new_driver + i, owner); 3487 if (ret) { 3488 while (i-- > 0) 3489 phy_driver_unregister(new_driver + i); 3490 break; 3491 } 3492 } 3493 return ret; 3494 } 3495 EXPORT_SYMBOL(phy_drivers_register); 3496 3497 void phy_driver_unregister(struct phy_driver *drv) 3498 { 3499 driver_unregister(&drv->mdiodrv.driver); 3500 } 3501 EXPORT_SYMBOL(phy_driver_unregister); 3502 3503 void phy_drivers_unregister(struct phy_driver *drv, int n) 3504 { 3505 int i; 3506 3507 for (i = 0; i < n; i++) 3508 phy_driver_unregister(drv + i); 3509 } 3510 EXPORT_SYMBOL(phy_drivers_unregister); 3511 3512 static struct phy_driver genphy_driver = { 3513 .phy_id = 0xffffffff, 3514 .phy_id_mask = 0xffffffff, 3515 .name = "Generic PHY", 3516 .get_features = genphy_read_abilities, 3517 .suspend = genphy_suspend, 3518 .resume = genphy_resume, 3519 .set_loopback = genphy_loopback, 3520 }; 3521 3522 static const struct ethtool_phy_ops phy_ethtool_phy_ops = { 3523 .get_sset_count = phy_ethtool_get_sset_count, 3524 .get_strings = phy_ethtool_get_strings, 3525 .get_stats = phy_ethtool_get_stats, 3526 .get_plca_cfg = phy_ethtool_get_plca_cfg, 3527 .set_plca_cfg = phy_ethtool_set_plca_cfg, 3528 .get_plca_status = phy_ethtool_get_plca_status, 3529 .start_cable_test = phy_start_cable_test, 3530 .start_cable_test_tdr = phy_start_cable_test_tdr, 3531 }; 3532 3533 static const struct phylib_stubs __phylib_stubs = { 3534 .hwtstamp_get = __phy_hwtstamp_get, 3535 .hwtstamp_set = __phy_hwtstamp_set, 3536 .get_phy_stats = __phy_ethtool_get_phy_stats, 3537 .get_link_ext_stats = __phy_ethtool_get_link_ext_stats, 3538 }; 3539 3540 static void phylib_register_stubs(void) 3541 { 3542 phylib_stubs = &__phylib_stubs; 3543 } 3544 3545 static void phylib_unregister_stubs(void) 3546 { 3547 phylib_stubs = NULL; 3548 } 3549 3550 static int __init phy_init(void) 3551 { 3552 int rc; 3553 3554 rtnl_lock(); 3555 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops); 3556 phylib_register_stubs(); 3557 rtnl_unlock(); 3558 3559 rc = mdio_bus_init(); 3560 if (rc) 3561 goto err_ethtool_phy_ops; 3562 3563 rc = phy_caps_init(); 3564 if (rc) 3565 goto err_mdio_bus; 3566 3567 features_init(); 3568 3569 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE); 3570 if (rc) 3571 goto err_mdio_bus; 3572 3573 rc = phy_driver_register(&genphy_driver, THIS_MODULE); 3574 if (rc) 3575 goto err_c45; 3576 3577 return 0; 3578 3579 err_c45: 3580 phy_driver_unregister(&genphy_c45_driver); 3581 err_mdio_bus: 3582 mdio_bus_exit(); 3583 err_ethtool_phy_ops: 3584 rtnl_lock(); 3585 phylib_unregister_stubs(); 3586 ethtool_set_ethtool_phy_ops(NULL); 3587 rtnl_unlock(); 3588 3589 return rc; 3590 } 3591 3592 static void __exit phy_exit(void) 3593 { 3594 phy_driver_unregister(&genphy_c45_driver); 3595 phy_driver_unregister(&genphy_driver); 3596 mdio_bus_exit(); 3597 rtnl_lock(); 3598 phylib_unregister_stubs(); 3599 ethtool_set_ethtool_phy_ops(NULL); 3600 rtnl_unlock(); 3601 } 3602 3603 subsys_initcall(phy_init); 3604 module_exit(phy_exit); 3605