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