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