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