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