1 /* Framework for finding and configuring PHYs. 2 * Also contains generic PHY driver 3 * 4 * Author: Andy Fleming 5 * 6 * Copyright (c) 2004 Freescale Semiconductor, Inc. 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2 of the License, or (at your 11 * option) any later version. 12 * 13 */ 14 15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16 17 #include <linux/kernel.h> 18 #include <linux/string.h> 19 #include <linux/errno.h> 20 #include <linux/unistd.h> 21 #include <linux/slab.h> 22 #include <linux/interrupt.h> 23 #include <linux/init.h> 24 #include <linux/delay.h> 25 #include <linux/netdevice.h> 26 #include <linux/etherdevice.h> 27 #include <linux/skbuff.h> 28 #include <linux/mm.h> 29 #include <linux/module.h> 30 #include <linux/mii.h> 31 #include <linux/ethtool.h> 32 #include <linux/bitmap.h> 33 #include <linux/phy.h> 34 #include <linux/phy_led_triggers.h> 35 #include <linux/mdio.h> 36 #include <linux/io.h> 37 #include <linux/uaccess.h> 38 #include <linux/of.h> 39 40 MODULE_DESCRIPTION("PHY library"); 41 MODULE_AUTHOR("Andy Fleming"); 42 MODULE_LICENSE("GPL"); 43 44 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init; 45 EXPORT_SYMBOL_GPL(phy_basic_features); 46 47 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init; 48 EXPORT_SYMBOL_GPL(phy_basic_t1_features); 49 50 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init; 51 EXPORT_SYMBOL_GPL(phy_gbit_features); 52 53 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init; 54 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features); 55 56 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init; 57 EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features); 58 59 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init; 60 EXPORT_SYMBOL_GPL(phy_10gbit_features); 61 62 static const int phy_basic_ports_array[] = { 63 ETHTOOL_LINK_MODE_Autoneg_BIT, 64 ETHTOOL_LINK_MODE_TP_BIT, 65 ETHTOOL_LINK_MODE_MII_BIT, 66 }; 67 EXPORT_SYMBOL_GPL(phy_basic_ports_array); 68 69 static const int phy_fibre_port_array[] = { 70 ETHTOOL_LINK_MODE_FIBRE_BIT, 71 }; 72 EXPORT_SYMBOL_GPL(phy_fibre_port_array); 73 74 static const int phy_all_ports_features_array[] = { 75 ETHTOOL_LINK_MODE_Autoneg_BIT, 76 ETHTOOL_LINK_MODE_TP_BIT, 77 ETHTOOL_LINK_MODE_MII_BIT, 78 ETHTOOL_LINK_MODE_FIBRE_BIT, 79 ETHTOOL_LINK_MODE_AUI_BIT, 80 ETHTOOL_LINK_MODE_BNC_BIT, 81 ETHTOOL_LINK_MODE_Backplane_BIT, 82 }; 83 EXPORT_SYMBOL_GPL(phy_all_ports_features_array); 84 85 const int phy_10_100_features_array[4] = { 86 ETHTOOL_LINK_MODE_10baseT_Half_BIT, 87 ETHTOOL_LINK_MODE_10baseT_Full_BIT, 88 ETHTOOL_LINK_MODE_100baseT_Half_BIT, 89 ETHTOOL_LINK_MODE_100baseT_Full_BIT, 90 }; 91 EXPORT_SYMBOL_GPL(phy_10_100_features_array); 92 93 const int phy_basic_t1_features_array[2] = { 94 ETHTOOL_LINK_MODE_TP_BIT, 95 ETHTOOL_LINK_MODE_100baseT_Full_BIT, 96 }; 97 EXPORT_SYMBOL_GPL(phy_basic_t1_features_array); 98 99 const int phy_gbit_features_array[2] = { 100 ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 101 ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 102 }; 103 EXPORT_SYMBOL_GPL(phy_gbit_features_array); 104 105 const int phy_10gbit_features_array[1] = { 106 ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 107 }; 108 EXPORT_SYMBOL_GPL(phy_10gbit_features_array); 109 110 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init; 111 EXPORT_SYMBOL_GPL(phy_10gbit_full_features); 112 113 static const int phy_10gbit_full_features_array[] = { 114 ETHTOOL_LINK_MODE_10baseT_Full_BIT, 115 ETHTOOL_LINK_MODE_100baseT_Full_BIT, 116 ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 117 ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 118 }; 119 120 static void features_init(void) 121 { 122 /* 10/100 half/full*/ 123 linkmode_set_bit_array(phy_basic_ports_array, 124 ARRAY_SIZE(phy_basic_ports_array), 125 phy_basic_features); 126 linkmode_set_bit_array(phy_10_100_features_array, 127 ARRAY_SIZE(phy_10_100_features_array), 128 phy_basic_features); 129 130 /* 100 full, TP */ 131 linkmode_set_bit_array(phy_basic_t1_features_array, 132 ARRAY_SIZE(phy_basic_t1_features_array), 133 phy_basic_t1_features); 134 135 /* 10/100 half/full + 1000 half/full */ 136 linkmode_set_bit_array(phy_basic_ports_array, 137 ARRAY_SIZE(phy_basic_ports_array), 138 phy_gbit_features); 139 linkmode_set_bit_array(phy_10_100_features_array, 140 ARRAY_SIZE(phy_10_100_features_array), 141 phy_gbit_features); 142 linkmode_set_bit_array(phy_gbit_features_array, 143 ARRAY_SIZE(phy_gbit_features_array), 144 phy_gbit_features); 145 146 /* 10/100 half/full + 1000 half/full + fibre*/ 147 linkmode_set_bit_array(phy_basic_ports_array, 148 ARRAY_SIZE(phy_basic_ports_array), 149 phy_gbit_fibre_features); 150 linkmode_set_bit_array(phy_10_100_features_array, 151 ARRAY_SIZE(phy_10_100_features_array), 152 phy_gbit_fibre_features); 153 linkmode_set_bit_array(phy_gbit_features_array, 154 ARRAY_SIZE(phy_gbit_features_array), 155 phy_gbit_fibre_features); 156 linkmode_set_bit_array(phy_fibre_port_array, 157 ARRAY_SIZE(phy_fibre_port_array), 158 phy_gbit_fibre_features); 159 160 /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/ 161 linkmode_set_bit_array(phy_all_ports_features_array, 162 ARRAY_SIZE(phy_all_ports_features_array), 163 phy_gbit_all_ports_features); 164 linkmode_set_bit_array(phy_10_100_features_array, 165 ARRAY_SIZE(phy_10_100_features_array), 166 phy_gbit_all_ports_features); 167 linkmode_set_bit_array(phy_gbit_features_array, 168 ARRAY_SIZE(phy_gbit_features_array), 169 phy_gbit_all_ports_features); 170 171 /* 10/100 half/full + 1000 half/full + 10G full*/ 172 linkmode_set_bit_array(phy_all_ports_features_array, 173 ARRAY_SIZE(phy_all_ports_features_array), 174 phy_10gbit_features); 175 linkmode_set_bit_array(phy_10_100_features_array, 176 ARRAY_SIZE(phy_10_100_features_array), 177 phy_10gbit_features); 178 linkmode_set_bit_array(phy_gbit_features_array, 179 ARRAY_SIZE(phy_gbit_features_array), 180 phy_10gbit_features); 181 linkmode_set_bit_array(phy_10gbit_features_array, 182 ARRAY_SIZE(phy_10gbit_features_array), 183 phy_10gbit_features); 184 185 /* 10/100/1000/10G full */ 186 linkmode_set_bit_array(phy_all_ports_features_array, 187 ARRAY_SIZE(phy_all_ports_features_array), 188 phy_10gbit_full_features); 189 linkmode_set_bit_array(phy_10gbit_full_features_array, 190 ARRAY_SIZE(phy_10gbit_full_features_array), 191 phy_10gbit_full_features); 192 } 193 194 void phy_device_free(struct phy_device *phydev) 195 { 196 put_device(&phydev->mdio.dev); 197 } 198 EXPORT_SYMBOL(phy_device_free); 199 200 static void phy_mdio_device_free(struct mdio_device *mdiodev) 201 { 202 struct phy_device *phydev; 203 204 phydev = container_of(mdiodev, struct phy_device, mdio); 205 phy_device_free(phydev); 206 } 207 208 static void phy_device_release(struct device *dev) 209 { 210 kfree(to_phy_device(dev)); 211 } 212 213 static void phy_mdio_device_remove(struct mdio_device *mdiodev) 214 { 215 struct phy_device *phydev; 216 217 phydev = container_of(mdiodev, struct phy_device, mdio); 218 phy_device_remove(phydev); 219 } 220 221 static struct phy_driver genphy_driver; 222 extern struct phy_driver genphy_10g_driver; 223 224 static LIST_HEAD(phy_fixup_list); 225 static DEFINE_MUTEX(phy_fixup_lock); 226 227 #ifdef CONFIG_PM 228 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) 229 { 230 struct device_driver *drv = phydev->mdio.dev.driver; 231 struct phy_driver *phydrv = to_phy_driver(drv); 232 struct net_device *netdev = phydev->attached_dev; 233 234 if (!drv || !phydrv->suspend) 235 return false; 236 237 /* PHY not attached? May suspend if the PHY has not already been 238 * suspended as part of a prior call to phy_disconnect() -> 239 * phy_detach() -> phy_suspend() because the parent netdev might be the 240 * MDIO bus driver and clock gated at this point. 241 */ 242 if (!netdev) 243 return !phydev->suspended; 244 245 if (netdev->wol_enabled) 246 return false; 247 248 /* As long as not all affected network drivers support the 249 * wol_enabled flag, let's check for hints that WoL is enabled. 250 * Don't suspend PHY if the attached netdev parent may wake up. 251 * The parent may point to a PCI device, as in tg3 driver. 252 */ 253 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) 254 return false; 255 256 /* Also don't suspend PHY if the netdev itself may wakeup. This 257 * is the case for devices w/o underlaying pwr. mgmt. aware bus, 258 * e.g. SoC devices. 259 */ 260 if (device_may_wakeup(&netdev->dev)) 261 return false; 262 263 return true; 264 } 265 266 static int mdio_bus_phy_suspend(struct device *dev) 267 { 268 struct phy_device *phydev = to_phy_device(dev); 269 270 /* We must stop the state machine manually, otherwise it stops out of 271 * control, possibly with the phydev->lock held. Upon resume, netdev 272 * may call phy routines that try to grab the same lock, and that may 273 * lead to a deadlock. 274 */ 275 if (phydev->attached_dev && phydev->adjust_link) 276 phy_stop_machine(phydev); 277 278 if (!mdio_bus_phy_may_suspend(phydev)) 279 return 0; 280 281 return phy_suspend(phydev); 282 } 283 284 static int mdio_bus_phy_resume(struct device *dev) 285 { 286 struct phy_device *phydev = to_phy_device(dev); 287 int ret; 288 289 if (!mdio_bus_phy_may_suspend(phydev)) 290 goto no_resume; 291 292 ret = phy_resume(phydev); 293 if (ret < 0) 294 return ret; 295 296 no_resume: 297 if (phydev->attached_dev && phydev->adjust_link) 298 phy_start_machine(phydev); 299 300 return 0; 301 } 302 303 static int mdio_bus_phy_restore(struct device *dev) 304 { 305 struct phy_device *phydev = to_phy_device(dev); 306 struct net_device *netdev = phydev->attached_dev; 307 int ret; 308 309 if (!netdev) 310 return 0; 311 312 ret = phy_init_hw(phydev); 313 if (ret < 0) 314 return ret; 315 316 if (phydev->attached_dev && phydev->adjust_link) 317 phy_start_machine(phydev); 318 319 return 0; 320 } 321 322 static const struct dev_pm_ops mdio_bus_phy_pm_ops = { 323 .suspend = mdio_bus_phy_suspend, 324 .resume = mdio_bus_phy_resume, 325 .freeze = mdio_bus_phy_suspend, 326 .thaw = mdio_bus_phy_resume, 327 .restore = mdio_bus_phy_restore, 328 }; 329 330 #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops) 331 332 #else 333 334 #define MDIO_BUS_PHY_PM_OPS NULL 335 336 #endif /* CONFIG_PM */ 337 338 /** 339 * phy_register_fixup - creates a new phy_fixup and adds it to the list 340 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID) 341 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) 342 * It can also be PHY_ANY_UID 343 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before 344 * comparison 345 * @run: The actual code to be run when a matching PHY is found 346 */ 347 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask, 348 int (*run)(struct phy_device *)) 349 { 350 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); 351 352 if (!fixup) 353 return -ENOMEM; 354 355 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id)); 356 fixup->phy_uid = phy_uid; 357 fixup->phy_uid_mask = phy_uid_mask; 358 fixup->run = run; 359 360 mutex_lock(&phy_fixup_lock); 361 list_add_tail(&fixup->list, &phy_fixup_list); 362 mutex_unlock(&phy_fixup_lock); 363 364 return 0; 365 } 366 EXPORT_SYMBOL(phy_register_fixup); 367 368 /* Registers a fixup to be run on any PHY with the UID in phy_uid */ 369 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, 370 int (*run)(struct phy_device *)) 371 { 372 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run); 373 } 374 EXPORT_SYMBOL(phy_register_fixup_for_uid); 375 376 /* Registers a fixup to be run on the PHY with id string bus_id */ 377 int phy_register_fixup_for_id(const char *bus_id, 378 int (*run)(struct phy_device *)) 379 { 380 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run); 381 } 382 EXPORT_SYMBOL(phy_register_fixup_for_id); 383 384 /** 385 * phy_unregister_fixup - remove a phy_fixup from the list 386 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list 387 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list 388 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison 389 */ 390 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask) 391 { 392 struct list_head *pos, *n; 393 struct phy_fixup *fixup; 394 int ret; 395 396 ret = -ENODEV; 397 398 mutex_lock(&phy_fixup_lock); 399 list_for_each_safe(pos, n, &phy_fixup_list) { 400 fixup = list_entry(pos, struct phy_fixup, list); 401 402 if ((!strcmp(fixup->bus_id, bus_id)) && 403 ((fixup->phy_uid & phy_uid_mask) == 404 (phy_uid & phy_uid_mask))) { 405 list_del(&fixup->list); 406 kfree(fixup); 407 ret = 0; 408 break; 409 } 410 } 411 mutex_unlock(&phy_fixup_lock); 412 413 return ret; 414 } 415 EXPORT_SYMBOL(phy_unregister_fixup); 416 417 /* Unregisters a fixup of any PHY with the UID in phy_uid */ 418 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask) 419 { 420 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask); 421 } 422 EXPORT_SYMBOL(phy_unregister_fixup_for_uid); 423 424 /* Unregisters a fixup of the PHY with id string bus_id */ 425 int phy_unregister_fixup_for_id(const char *bus_id) 426 { 427 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff); 428 } 429 EXPORT_SYMBOL(phy_unregister_fixup_for_id); 430 431 /* Returns 1 if fixup matches phydev in bus_id and phy_uid. 432 * Fixups can be set to match any in one or more fields. 433 */ 434 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup) 435 { 436 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0) 437 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) 438 return 0; 439 440 if ((fixup->phy_uid & fixup->phy_uid_mask) != 441 (phydev->phy_id & fixup->phy_uid_mask)) 442 if (fixup->phy_uid != PHY_ANY_UID) 443 return 0; 444 445 return 1; 446 } 447 448 /* Runs any matching fixups for this phydev */ 449 static int phy_scan_fixups(struct phy_device *phydev) 450 { 451 struct phy_fixup *fixup; 452 453 mutex_lock(&phy_fixup_lock); 454 list_for_each_entry(fixup, &phy_fixup_list, list) { 455 if (phy_needs_fixup(phydev, fixup)) { 456 int err = fixup->run(phydev); 457 458 if (err < 0) { 459 mutex_unlock(&phy_fixup_lock); 460 return err; 461 } 462 phydev->has_fixups = true; 463 } 464 } 465 mutex_unlock(&phy_fixup_lock); 466 467 return 0; 468 } 469 470 static int phy_bus_match(struct device *dev, struct device_driver *drv) 471 { 472 struct phy_device *phydev = to_phy_device(dev); 473 struct phy_driver *phydrv = to_phy_driver(drv); 474 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); 475 int i; 476 477 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)) 478 return 0; 479 480 if (phydrv->match_phy_device) 481 return phydrv->match_phy_device(phydev); 482 483 if (phydev->is_c45) { 484 for (i = 1; i < num_ids; i++) { 485 if (!(phydev->c45_ids.devices_in_package & (1 << i))) 486 continue; 487 488 if ((phydrv->phy_id & phydrv->phy_id_mask) == 489 (phydev->c45_ids.device_ids[i] & 490 phydrv->phy_id_mask)) 491 return 1; 492 } 493 return 0; 494 } else { 495 return (phydrv->phy_id & phydrv->phy_id_mask) == 496 (phydev->phy_id & phydrv->phy_id_mask); 497 } 498 } 499 500 static ssize_t 501 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) 502 { 503 struct phy_device *phydev = to_phy_device(dev); 504 505 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); 506 } 507 static DEVICE_ATTR_RO(phy_id); 508 509 static ssize_t 510 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) 511 { 512 struct phy_device *phydev = to_phy_device(dev); 513 const char *mode = NULL; 514 515 if (phy_is_internal(phydev)) 516 mode = "internal"; 517 else 518 mode = phy_modes(phydev->interface); 519 520 return sprintf(buf, "%s\n", mode); 521 } 522 static DEVICE_ATTR_RO(phy_interface); 523 524 static ssize_t 525 phy_has_fixups_show(struct device *dev, struct device_attribute *attr, 526 char *buf) 527 { 528 struct phy_device *phydev = to_phy_device(dev); 529 530 return sprintf(buf, "%d\n", phydev->has_fixups); 531 } 532 static DEVICE_ATTR_RO(phy_has_fixups); 533 534 static struct attribute *phy_dev_attrs[] = { 535 &dev_attr_phy_id.attr, 536 &dev_attr_phy_interface.attr, 537 &dev_attr_phy_has_fixups.attr, 538 NULL, 539 }; 540 ATTRIBUTE_GROUPS(phy_dev); 541 542 static const struct device_type mdio_bus_phy_type = { 543 .name = "PHY", 544 .groups = phy_dev_groups, 545 .release = phy_device_release, 546 .pm = MDIO_BUS_PHY_PM_OPS, 547 }; 548 549 static int phy_request_driver_module(struct phy_device *dev, int phy_id) 550 { 551 int ret; 552 553 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, 554 MDIO_ID_ARGS(phy_id)); 555 /* we only check for failures in executing the usermode binary, 556 * not whether a PHY driver module exists for the PHY ID 557 */ 558 if (IS_ENABLED(CONFIG_MODULES) && ret < 0) { 559 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08x\n", 560 ret, phy_id); 561 return ret; 562 } 563 564 return 0; 565 } 566 567 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id, 568 bool is_c45, 569 struct phy_c45_device_ids *c45_ids) 570 { 571 struct phy_device *dev; 572 struct mdio_device *mdiodev; 573 int ret = 0; 574 575 /* We allocate the device, and initialize the default values */ 576 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 577 if (!dev) 578 return ERR_PTR(-ENOMEM); 579 580 mdiodev = &dev->mdio; 581 mdiodev->dev.parent = &bus->dev; 582 mdiodev->dev.bus = &mdio_bus_type; 583 mdiodev->dev.type = &mdio_bus_phy_type; 584 mdiodev->bus = bus; 585 mdiodev->bus_match = phy_bus_match; 586 mdiodev->addr = addr; 587 mdiodev->flags = MDIO_DEVICE_FLAG_PHY; 588 mdiodev->device_free = phy_mdio_device_free; 589 mdiodev->device_remove = phy_mdio_device_remove; 590 591 dev->speed = 0; 592 dev->duplex = -1; 593 dev->pause = 0; 594 dev->asym_pause = 0; 595 dev->link = 0; 596 dev->interface = PHY_INTERFACE_MODE_GMII; 597 598 dev->autoneg = AUTONEG_ENABLE; 599 600 dev->is_c45 = is_c45; 601 dev->phy_id = phy_id; 602 if (c45_ids) 603 dev->c45_ids = *c45_ids; 604 dev->irq = bus->irq[addr]; 605 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); 606 607 dev->state = PHY_DOWN; 608 609 mutex_init(&dev->lock); 610 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); 611 612 /* Request the appropriate module unconditionally; don't 613 * bother trying to do so only if it isn't already loaded, 614 * because that gets complicated. A hotplug event would have 615 * done an unconditional modprobe anyway. 616 * We don't do normal hotplug because it won't work for MDIO 617 * -- because it relies on the device staying around for long 618 * enough for the driver to get loaded. With MDIO, the NIC 619 * driver will get bored and give up as soon as it finds that 620 * there's no driver _already_ loaded. 621 */ 622 if (is_c45 && c45_ids) { 623 const int num_ids = ARRAY_SIZE(c45_ids->device_ids); 624 int i; 625 626 for (i = 1; i < num_ids; i++) { 627 if (!(c45_ids->devices_in_package & (1 << i))) 628 continue; 629 630 ret = phy_request_driver_module(dev, 631 c45_ids->device_ids[i]); 632 if (ret) 633 break; 634 } 635 } else { 636 ret = phy_request_driver_module(dev, phy_id); 637 } 638 639 if (!ret) { 640 device_initialize(&mdiodev->dev); 641 } else { 642 kfree(dev); 643 dev = ERR_PTR(ret); 644 } 645 646 return dev; 647 } 648 EXPORT_SYMBOL(phy_device_create); 649 650 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers. 651 * @bus: the target MII bus 652 * @addr: PHY address on the MII bus 653 * @dev_addr: MMD address in the PHY. 654 * @devices_in_package: where to store the devices in package information. 655 * 656 * Description: reads devices in package registers of a MMD at @dev_addr 657 * from PHY at @addr on @bus. 658 * 659 * Returns: 0 on success, -EIO on failure. 660 */ 661 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr, 662 u32 *devices_in_package) 663 { 664 int phy_reg, reg_addr; 665 666 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2; 667 phy_reg = mdiobus_read(bus, addr, reg_addr); 668 if (phy_reg < 0) 669 return -EIO; 670 *devices_in_package = (phy_reg & 0xffff) << 16; 671 672 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1; 673 phy_reg = mdiobus_read(bus, addr, reg_addr); 674 if (phy_reg < 0) 675 return -EIO; 676 *devices_in_package |= (phy_reg & 0xffff); 677 678 return 0; 679 } 680 681 /** 682 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. 683 * @bus: the target MII bus 684 * @addr: PHY address on the MII bus 685 * @phy_id: where to store the ID retrieved. 686 * @c45_ids: where to store the c45 ID information. 687 * 688 * If the PHY devices-in-package appears to be valid, it and the 689 * corresponding identifiers are stored in @c45_ids, zero is stored 690 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns 691 * zero on success. 692 * 693 */ 694 static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id, 695 struct phy_c45_device_ids *c45_ids) { 696 int phy_reg; 697 int i, reg_addr; 698 const int num_ids = ARRAY_SIZE(c45_ids->device_ids); 699 u32 *devs = &c45_ids->devices_in_package; 700 701 /* Find first non-zero Devices In package. Device zero is reserved 702 * for 802.3 c45 complied PHYs, so don't probe it at first. 703 */ 704 for (i = 1; i < num_ids && *devs == 0; i++) { 705 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs); 706 if (phy_reg < 0) 707 return -EIO; 708 709 if ((*devs & 0x1fffffff) == 0x1fffffff) { 710 /* If mostly Fs, there is no device there, 711 * then let's continue to probe more, as some 712 * 10G PHYs have zero Devices In package, 713 * e.g. Cortina CS4315/CS4340 PHY. 714 */ 715 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs); 716 if (phy_reg < 0) 717 return -EIO; 718 /* no device there, let's get out of here */ 719 if ((*devs & 0x1fffffff) == 0x1fffffff) { 720 *phy_id = 0xffffffff; 721 return 0; 722 } else { 723 break; 724 } 725 } 726 } 727 728 /* Now probe Device Identifiers for each device present. */ 729 for (i = 1; i < num_ids; i++) { 730 if (!(c45_ids->devices_in_package & (1 << i))) 731 continue; 732 733 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1; 734 phy_reg = mdiobus_read(bus, addr, reg_addr); 735 if (phy_reg < 0) 736 return -EIO; 737 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16; 738 739 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2; 740 phy_reg = mdiobus_read(bus, addr, reg_addr); 741 if (phy_reg < 0) 742 return -EIO; 743 c45_ids->device_ids[i] |= (phy_reg & 0xffff); 744 } 745 *phy_id = 0; 746 return 0; 747 } 748 749 /** 750 * get_phy_id - reads the specified addr for its ID. 751 * @bus: the target MII bus 752 * @addr: PHY address on the MII bus 753 * @phy_id: where to store the ID retrieved. 754 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol 755 * @c45_ids: where to store the c45 ID information. 756 * 757 * Description: In the case of a 802.3-c22 PHY, reads the ID registers 758 * of the PHY at @addr on the @bus, stores it in @phy_id and returns 759 * zero on success. 760 * 761 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and 762 * its return value is in turn returned. 763 * 764 */ 765 static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id, 766 bool is_c45, struct phy_c45_device_ids *c45_ids) 767 { 768 int phy_reg; 769 770 if (is_c45) 771 return get_phy_c45_ids(bus, addr, phy_id, c45_ids); 772 773 /* Grab the bits from PHYIR1, and put them in the upper half */ 774 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); 775 if (phy_reg < 0) { 776 /* if there is no device, return without an error so scanning 777 * the bus works properly 778 */ 779 if (phy_reg == -EIO || phy_reg == -ENODEV) { 780 *phy_id = 0xffffffff; 781 return 0; 782 } 783 784 return -EIO; 785 } 786 787 *phy_id = (phy_reg & 0xffff) << 16; 788 789 /* Grab the bits from PHYIR2, and put them in the lower half */ 790 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); 791 if (phy_reg < 0) 792 return -EIO; 793 794 *phy_id |= (phy_reg & 0xffff); 795 796 return 0; 797 } 798 799 /** 800 * get_phy_device - reads the specified PHY device and returns its @phy_device 801 * struct 802 * @bus: the target MII bus 803 * @addr: PHY address on the MII bus 804 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol 805 * 806 * Description: Reads the ID registers of the PHY at @addr on the 807 * @bus, then allocates and returns the phy_device to represent it. 808 */ 809 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45) 810 { 811 struct phy_c45_device_ids c45_ids = {0}; 812 u32 phy_id = 0; 813 int r; 814 815 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids); 816 if (r) 817 return ERR_PTR(r); 818 819 /* If the phy_id is mostly Fs, there is no device there */ 820 if ((phy_id & 0x1fffffff) == 0x1fffffff) 821 return ERR_PTR(-ENODEV); 822 823 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); 824 } 825 EXPORT_SYMBOL(get_phy_device); 826 827 /** 828 * phy_device_register - Register the phy device on the MDIO bus 829 * @phydev: phy_device structure to be added to the MDIO bus 830 */ 831 int phy_device_register(struct phy_device *phydev) 832 { 833 int err; 834 835 err = mdiobus_register_device(&phydev->mdio); 836 if (err) 837 return err; 838 839 /* Deassert the reset signal */ 840 phy_device_reset(phydev, 0); 841 842 /* Run all of the fixups for this PHY */ 843 err = phy_scan_fixups(phydev); 844 if (err) { 845 phydev_err(phydev, "failed to initialize\n"); 846 goto out; 847 } 848 849 err = device_add(&phydev->mdio.dev); 850 if (err) { 851 phydev_err(phydev, "failed to add\n"); 852 goto out; 853 } 854 855 return 0; 856 857 out: 858 /* Assert the reset signal */ 859 phy_device_reset(phydev, 1); 860 861 mdiobus_unregister_device(&phydev->mdio); 862 return err; 863 } 864 EXPORT_SYMBOL(phy_device_register); 865 866 /** 867 * phy_device_remove - Remove a previously registered phy device from the MDIO bus 868 * @phydev: phy_device structure to remove 869 * 870 * This doesn't free the phy_device itself, it merely reverses the effects 871 * of phy_device_register(). Use phy_device_free() to free the device 872 * after calling this function. 873 */ 874 void phy_device_remove(struct phy_device *phydev) 875 { 876 device_del(&phydev->mdio.dev); 877 878 /* Assert the reset signal */ 879 phy_device_reset(phydev, 1); 880 881 mdiobus_unregister_device(&phydev->mdio); 882 } 883 EXPORT_SYMBOL(phy_device_remove); 884 885 /** 886 * phy_find_first - finds the first PHY device on the bus 887 * @bus: the target MII bus 888 */ 889 struct phy_device *phy_find_first(struct mii_bus *bus) 890 { 891 struct phy_device *phydev; 892 int addr; 893 894 for (addr = 0; addr < PHY_MAX_ADDR; addr++) { 895 phydev = mdiobus_get_phy(bus, addr); 896 if (phydev) 897 return phydev; 898 } 899 return NULL; 900 } 901 EXPORT_SYMBOL(phy_find_first); 902 903 static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier) 904 { 905 struct net_device *netdev = phydev->attached_dev; 906 907 if (do_carrier) { 908 if (up) 909 netif_carrier_on(netdev); 910 else 911 netif_carrier_off(netdev); 912 } 913 phydev->adjust_link(netdev); 914 } 915 916 /** 917 * phy_prepare_link - prepares the PHY layer to monitor link status 918 * @phydev: target phy_device struct 919 * @handler: callback function for link status change notifications 920 * 921 * Description: Tells the PHY infrastructure to handle the 922 * gory details on monitoring link status (whether through 923 * polling or an interrupt), and to call back to the 924 * connected device driver when the link status changes. 925 * If you want to monitor your own link state, don't call 926 * this function. 927 */ 928 static void phy_prepare_link(struct phy_device *phydev, 929 void (*handler)(struct net_device *)) 930 { 931 phydev->adjust_link = handler; 932 } 933 934 /** 935 * phy_connect_direct - connect an ethernet device to a specific phy_device 936 * @dev: the network device to connect 937 * @phydev: the pointer to the phy device 938 * @handler: callback function for state change notifications 939 * @interface: PHY device's interface 940 */ 941 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, 942 void (*handler)(struct net_device *), 943 phy_interface_t interface) 944 { 945 int rc; 946 947 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 948 if (rc) 949 return rc; 950 951 phy_prepare_link(phydev, handler); 952 phy_start_machine(phydev); 953 if (phydev->irq > 0) 954 phy_start_interrupts(phydev); 955 956 return 0; 957 } 958 EXPORT_SYMBOL(phy_connect_direct); 959 960 /** 961 * phy_connect - connect an ethernet device to a PHY device 962 * @dev: the network device to connect 963 * @bus_id: the id string of the PHY device to connect 964 * @handler: callback function for state change notifications 965 * @interface: PHY device's interface 966 * 967 * Description: Convenience function for connecting ethernet 968 * devices to PHY devices. The default behavior is for 969 * the PHY infrastructure to handle everything, and only notify 970 * the connected driver when the link status changes. If you 971 * don't want, or can't use the provided functionality, you may 972 * choose to call only the subset of functions which provide 973 * the desired functionality. 974 */ 975 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, 976 void (*handler)(struct net_device *), 977 phy_interface_t interface) 978 { 979 struct phy_device *phydev; 980 struct device *d; 981 int rc; 982 983 /* Search the list of PHY devices on the mdio bus for the 984 * PHY with the requested name 985 */ 986 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); 987 if (!d) { 988 pr_err("PHY %s not found\n", bus_id); 989 return ERR_PTR(-ENODEV); 990 } 991 phydev = to_phy_device(d); 992 993 rc = phy_connect_direct(dev, phydev, handler, interface); 994 put_device(d); 995 if (rc) 996 return ERR_PTR(rc); 997 998 return phydev; 999 } 1000 EXPORT_SYMBOL(phy_connect); 1001 1002 /** 1003 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY 1004 * device 1005 * @phydev: target phy_device struct 1006 */ 1007 void phy_disconnect(struct phy_device *phydev) 1008 { 1009 if (phydev->irq > 0) 1010 phy_stop_interrupts(phydev); 1011 1012 phy_stop_machine(phydev); 1013 1014 phydev->adjust_link = NULL; 1015 1016 phy_detach(phydev); 1017 } 1018 EXPORT_SYMBOL(phy_disconnect); 1019 1020 /** 1021 * phy_poll_reset - Safely wait until a PHY reset has properly completed 1022 * @phydev: The PHY device to poll 1023 * 1024 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as 1025 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR 1026 * register must be polled until the BMCR_RESET bit clears. 1027 * 1028 * Furthermore, any attempts to write to PHY registers may have no effect 1029 * or even generate MDIO bus errors until this is complete. 1030 * 1031 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the 1032 * standard and do not fully reset after the BMCR_RESET bit is set, and may 1033 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an 1034 * effort to support such broken PHYs, this function is separate from the 1035 * standard phy_init_hw() which will zero all the other bits in the BMCR 1036 * and reapply all driver-specific and board-specific fixups. 1037 */ 1038 static int phy_poll_reset(struct phy_device *phydev) 1039 { 1040 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ 1041 unsigned int retries = 12; 1042 int ret; 1043 1044 do { 1045 msleep(50); 1046 ret = phy_read(phydev, MII_BMCR); 1047 if (ret < 0) 1048 return ret; 1049 } while (ret & BMCR_RESET && --retries); 1050 if (ret & BMCR_RESET) 1051 return -ETIMEDOUT; 1052 1053 /* Some chips (smsc911x) may still need up to another 1ms after the 1054 * BMCR_RESET bit is cleared before they are usable. 1055 */ 1056 msleep(1); 1057 return 0; 1058 } 1059 1060 int phy_init_hw(struct phy_device *phydev) 1061 { 1062 int ret = 0; 1063 1064 /* Deassert the reset signal */ 1065 phy_device_reset(phydev, 0); 1066 1067 if (!phydev->drv || !phydev->drv->config_init) 1068 return 0; 1069 1070 if (phydev->drv->soft_reset) 1071 ret = phydev->drv->soft_reset(phydev); 1072 1073 if (ret < 0) 1074 return ret; 1075 1076 ret = phy_scan_fixups(phydev); 1077 if (ret < 0) 1078 return ret; 1079 1080 return phydev->drv->config_init(phydev); 1081 } 1082 EXPORT_SYMBOL(phy_init_hw); 1083 1084 void phy_attached_info(struct phy_device *phydev) 1085 { 1086 phy_attached_print(phydev, NULL); 1087 } 1088 EXPORT_SYMBOL(phy_attached_info); 1089 1090 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)" 1091 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) 1092 { 1093 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound"; 1094 char *irq_str; 1095 char irq_num[8]; 1096 1097 switch(phydev->irq) { 1098 case PHY_POLL: 1099 irq_str = "POLL"; 1100 break; 1101 case PHY_IGNORE_INTERRUPT: 1102 irq_str = "IGNORE"; 1103 break; 1104 default: 1105 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq); 1106 irq_str = irq_num; 1107 break; 1108 } 1109 1110 1111 if (!fmt) { 1112 phydev_info(phydev, ATTACHED_FMT "\n", 1113 drv_name, phydev_name(phydev), 1114 irq_str); 1115 } else { 1116 va_list ap; 1117 1118 phydev_info(phydev, ATTACHED_FMT, 1119 drv_name, phydev_name(phydev), 1120 irq_str); 1121 1122 va_start(ap, fmt); 1123 vprintk(fmt, ap); 1124 va_end(ap); 1125 } 1126 } 1127 EXPORT_SYMBOL(phy_attached_print); 1128 1129 /** 1130 * phy_attach_direct - attach a network device to a given PHY device pointer 1131 * @dev: network device to attach 1132 * @phydev: Pointer to phy_device to attach 1133 * @flags: PHY device's dev_flags 1134 * @interface: PHY device's interface 1135 * 1136 * Description: Called by drivers to attach to a particular PHY 1137 * device. The phy_device is found, and properly hooked up 1138 * to the phy_driver. If no driver is attached, then a 1139 * generic driver is used. The phy_device is given a ptr to 1140 * the attaching device, and given a callback for link status 1141 * change. The phy_device is returned to the attaching driver. 1142 * This function takes a reference on the phy device. 1143 */ 1144 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, 1145 u32 flags, phy_interface_t interface) 1146 { 1147 struct module *ndev_owner = dev->dev.parent->driver->owner; 1148 struct mii_bus *bus = phydev->mdio.bus; 1149 struct device *d = &phydev->mdio.dev; 1150 bool using_genphy = false; 1151 int err; 1152 1153 /* For Ethernet device drivers that register their own MDIO bus, we 1154 * will have bus->owner match ndev_mod, so we do not want to increment 1155 * our own module->refcnt here, otherwise we would not be able to 1156 * unload later on. 1157 */ 1158 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { 1159 dev_err(&dev->dev, "failed to get the bus module\n"); 1160 return -EIO; 1161 } 1162 1163 get_device(d); 1164 1165 /* Assume that if there is no driver, that it doesn't 1166 * exist, and we should use the genphy driver. 1167 */ 1168 if (!d->driver) { 1169 if (phydev->is_c45) 1170 d->driver = &genphy_10g_driver.mdiodrv.driver; 1171 else 1172 d->driver = &genphy_driver.mdiodrv.driver; 1173 1174 using_genphy = true; 1175 } 1176 1177 if (!try_module_get(d->driver->owner)) { 1178 dev_err(&dev->dev, "failed to get the device driver module\n"); 1179 err = -EIO; 1180 goto error_put_device; 1181 } 1182 1183 if (using_genphy) { 1184 err = d->driver->probe(d); 1185 if (err >= 0) 1186 err = device_bind_driver(d); 1187 1188 if (err) 1189 goto error_module_put; 1190 } 1191 1192 if (phydev->attached_dev) { 1193 dev_err(&dev->dev, "PHY already attached\n"); 1194 err = -EBUSY; 1195 goto error; 1196 } 1197 1198 phydev->phy_link_change = phy_link_change; 1199 phydev->attached_dev = dev; 1200 dev->phydev = phydev; 1201 1202 /* Some Ethernet drivers try to connect to a PHY device before 1203 * calling register_netdevice() -> netdev_register_kobject() and 1204 * does the dev->dev.kobj initialization. Here we only check for 1205 * success which indicates that the network device kobject is 1206 * ready. Once we do that we still need to keep track of whether 1207 * links were successfully set up or not for phy_detach() to 1208 * remove them accordingly. 1209 */ 1210 phydev->sysfs_links = false; 1211 1212 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, 1213 "attached_dev"); 1214 if (!err) { 1215 err = sysfs_create_link_nowarn(&dev->dev.kobj, 1216 &phydev->mdio.dev.kobj, 1217 "phydev"); 1218 if (err) { 1219 dev_err(&dev->dev, "could not add device link to %s err %d\n", 1220 kobject_name(&phydev->mdio.dev.kobj), 1221 err); 1222 /* non-fatal - some net drivers can use one netdevice 1223 * with more then one phy 1224 */ 1225 } 1226 1227 phydev->sysfs_links = true; 1228 } 1229 1230 phydev->dev_flags = flags; 1231 1232 phydev->interface = interface; 1233 1234 phydev->state = PHY_READY; 1235 1236 /* Initial carrier state is off as the phy is about to be 1237 * (re)initialized. 1238 */ 1239 netif_carrier_off(phydev->attached_dev); 1240 1241 /* Do initial configuration here, now that 1242 * we have certain key parameters 1243 * (dev_flags and interface) 1244 */ 1245 err = phy_init_hw(phydev); 1246 if (err) 1247 goto error; 1248 1249 phy_resume(phydev); 1250 phy_led_triggers_register(phydev); 1251 1252 return err; 1253 1254 error: 1255 /* phy_detach() does all of the cleanup below */ 1256 phy_detach(phydev); 1257 return err; 1258 1259 error_module_put: 1260 module_put(d->driver->owner); 1261 error_put_device: 1262 put_device(d); 1263 if (ndev_owner != bus->owner) 1264 module_put(bus->owner); 1265 return err; 1266 } 1267 EXPORT_SYMBOL(phy_attach_direct); 1268 1269 /** 1270 * phy_attach - attach a network device to a particular PHY device 1271 * @dev: network device to attach 1272 * @bus_id: Bus ID of PHY device to attach 1273 * @interface: PHY device's interface 1274 * 1275 * Description: Same as phy_attach_direct() except that a PHY bus_id 1276 * string is passed instead of a pointer to a struct phy_device. 1277 */ 1278 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, 1279 phy_interface_t interface) 1280 { 1281 struct bus_type *bus = &mdio_bus_type; 1282 struct phy_device *phydev; 1283 struct device *d; 1284 int rc; 1285 1286 /* Search the list of PHY devices on the mdio bus for the 1287 * PHY with the requested name 1288 */ 1289 d = bus_find_device_by_name(bus, NULL, bus_id); 1290 if (!d) { 1291 pr_err("PHY %s not found\n", bus_id); 1292 return ERR_PTR(-ENODEV); 1293 } 1294 phydev = to_phy_device(d); 1295 1296 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 1297 put_device(d); 1298 if (rc) 1299 return ERR_PTR(rc); 1300 1301 return phydev; 1302 } 1303 EXPORT_SYMBOL(phy_attach); 1304 1305 static bool phy_driver_is_genphy_kind(struct phy_device *phydev, 1306 struct device_driver *driver) 1307 { 1308 struct device *d = &phydev->mdio.dev; 1309 bool ret = false; 1310 1311 if (!phydev->drv) 1312 return ret; 1313 1314 get_device(d); 1315 ret = d->driver == driver; 1316 put_device(d); 1317 1318 return ret; 1319 } 1320 1321 bool phy_driver_is_genphy(struct phy_device *phydev) 1322 { 1323 return phy_driver_is_genphy_kind(phydev, 1324 &genphy_driver.mdiodrv.driver); 1325 } 1326 EXPORT_SYMBOL_GPL(phy_driver_is_genphy); 1327 1328 bool phy_driver_is_genphy_10g(struct phy_device *phydev) 1329 { 1330 return phy_driver_is_genphy_kind(phydev, 1331 &genphy_10g_driver.mdiodrv.driver); 1332 } 1333 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); 1334 1335 /** 1336 * phy_detach - detach a PHY device from its network device 1337 * @phydev: target phy_device struct 1338 * 1339 * This detaches the phy device from its network device and the phy 1340 * driver, and drops the reference count taken in phy_attach_direct(). 1341 */ 1342 void phy_detach(struct phy_device *phydev) 1343 { 1344 struct net_device *dev = phydev->attached_dev; 1345 struct module *ndev_owner = dev->dev.parent->driver->owner; 1346 struct mii_bus *bus; 1347 1348 if (phydev->sysfs_links) { 1349 sysfs_remove_link(&dev->dev.kobj, "phydev"); 1350 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); 1351 } 1352 phy_suspend(phydev); 1353 phydev->attached_dev->phydev = NULL; 1354 phydev->attached_dev = NULL; 1355 phydev->phylink = NULL; 1356 1357 phy_led_triggers_unregister(phydev); 1358 1359 module_put(phydev->mdio.dev.driver->owner); 1360 1361 /* If the device had no specific driver before (i.e. - it 1362 * was using the generic driver), we unbind the device 1363 * from the generic driver so that there's a chance a 1364 * real driver could be loaded 1365 */ 1366 if (phy_driver_is_genphy(phydev) || 1367 phy_driver_is_genphy_10g(phydev)) 1368 device_release_driver(&phydev->mdio.dev); 1369 1370 /* 1371 * The phydev might go away on the put_device() below, so avoid 1372 * a use-after-free bug by reading the underlying bus first. 1373 */ 1374 bus = phydev->mdio.bus; 1375 1376 put_device(&phydev->mdio.dev); 1377 if (ndev_owner != bus->owner) 1378 module_put(bus->owner); 1379 1380 /* Assert the reset signal */ 1381 phy_device_reset(phydev, 1); 1382 } 1383 EXPORT_SYMBOL(phy_detach); 1384 1385 int phy_suspend(struct phy_device *phydev) 1386 { 1387 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); 1388 struct net_device *netdev = phydev->attached_dev; 1389 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; 1390 int ret = 0; 1391 1392 /* If the device has WOL enabled, we cannot suspend the PHY */ 1393 phy_ethtool_get_wol(phydev, &wol); 1394 if (wol.wolopts || (netdev && netdev->wol_enabled)) 1395 return -EBUSY; 1396 1397 if (phydev->drv && phydrv->suspend) 1398 ret = phydrv->suspend(phydev); 1399 1400 if (ret) 1401 return ret; 1402 1403 phydev->suspended = true; 1404 1405 return ret; 1406 } 1407 EXPORT_SYMBOL(phy_suspend); 1408 1409 int __phy_resume(struct phy_device *phydev) 1410 { 1411 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); 1412 int ret = 0; 1413 1414 WARN_ON(!mutex_is_locked(&phydev->lock)); 1415 1416 if (phydev->drv && phydrv->resume) 1417 ret = phydrv->resume(phydev); 1418 1419 if (ret) 1420 return ret; 1421 1422 phydev->suspended = false; 1423 1424 return ret; 1425 } 1426 EXPORT_SYMBOL(__phy_resume); 1427 1428 int phy_resume(struct phy_device *phydev) 1429 { 1430 int ret; 1431 1432 mutex_lock(&phydev->lock); 1433 ret = __phy_resume(phydev); 1434 mutex_unlock(&phydev->lock); 1435 1436 return ret; 1437 } 1438 EXPORT_SYMBOL(phy_resume); 1439 1440 int phy_loopback(struct phy_device *phydev, bool enable) 1441 { 1442 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); 1443 int ret = 0; 1444 1445 mutex_lock(&phydev->lock); 1446 1447 if (enable && phydev->loopback_enabled) { 1448 ret = -EBUSY; 1449 goto out; 1450 } 1451 1452 if (!enable && !phydev->loopback_enabled) { 1453 ret = -EINVAL; 1454 goto out; 1455 } 1456 1457 if (phydev->drv && phydrv->set_loopback) 1458 ret = phydrv->set_loopback(phydev, enable); 1459 else 1460 ret = -EOPNOTSUPP; 1461 1462 if (ret) 1463 goto out; 1464 1465 phydev->loopback_enabled = enable; 1466 1467 out: 1468 mutex_unlock(&phydev->lock); 1469 return ret; 1470 } 1471 EXPORT_SYMBOL(phy_loopback); 1472 1473 /** 1474 * phy_reset_after_clk_enable - perform a PHY reset if needed 1475 * @phydev: target phy_device struct 1476 * 1477 * Description: Some PHYs are known to need a reset after their refclk was 1478 * enabled. This function evaluates the flags and perform the reset if it's 1479 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy 1480 * was reset. 1481 */ 1482 int phy_reset_after_clk_enable(struct phy_device *phydev) 1483 { 1484 if (!phydev || !phydev->drv) 1485 return -ENODEV; 1486 1487 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { 1488 phy_device_reset(phydev, 1); 1489 phy_device_reset(phydev, 0); 1490 return 1; 1491 } 1492 1493 return 0; 1494 } 1495 EXPORT_SYMBOL(phy_reset_after_clk_enable); 1496 1497 /* Generic PHY support and helper functions */ 1498 1499 /** 1500 * genphy_config_advert - sanitize and advertise auto-negotiation parameters 1501 * @phydev: target phy_device struct 1502 * 1503 * Description: Writes MII_ADVERTISE with the appropriate values, 1504 * after sanitizing the values to make sure we only advertise 1505 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 1506 * hasn't changed, and > 0 if it has changed. 1507 */ 1508 static int genphy_config_advert(struct phy_device *phydev) 1509 { 1510 u32 advertise; 1511 int oldadv, adv, bmsr; 1512 int err, changed = 0; 1513 1514 /* Only allow advertising what this PHY supports */ 1515 linkmode_and(phydev->advertising, phydev->advertising, 1516 phydev->supported); 1517 if (!ethtool_convert_link_mode_to_legacy_u32(&advertise, 1518 phydev->advertising)) 1519 phydev_warn(phydev, "PHY advertising (%*pb) more modes than genphy supports, some modes not advertised.\n", 1520 __ETHTOOL_LINK_MODE_MASK_NBITS, 1521 phydev->advertising); 1522 1523 /* Setup standard advertisement */ 1524 adv = phy_read(phydev, MII_ADVERTISE); 1525 if (adv < 0) 1526 return adv; 1527 1528 oldadv = adv; 1529 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | 1530 ADVERTISE_PAUSE_ASYM); 1531 adv |= ethtool_adv_to_mii_adv_t(advertise); 1532 1533 if (adv != oldadv) { 1534 err = phy_write(phydev, MII_ADVERTISE, adv); 1535 1536 if (err < 0) 1537 return err; 1538 changed = 1; 1539 } 1540 1541 bmsr = phy_read(phydev, MII_BMSR); 1542 if (bmsr < 0) 1543 return bmsr; 1544 1545 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all 1546 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a 1547 * logical 1. 1548 */ 1549 if (!(bmsr & BMSR_ESTATEN)) 1550 return changed; 1551 1552 /* Configure gigabit if it's supported */ 1553 adv = phy_read(phydev, MII_CTRL1000); 1554 if (adv < 0) 1555 return adv; 1556 1557 oldadv = adv; 1558 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); 1559 1560 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 1561 phydev->supported) || 1562 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 1563 phydev->supported)) 1564 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise); 1565 1566 if (adv != oldadv) 1567 changed = 1; 1568 1569 err = phy_write(phydev, MII_CTRL1000, adv); 1570 if (err < 0) 1571 return err; 1572 1573 return changed; 1574 } 1575 1576 /** 1577 * genphy_config_eee_advert - disable unwanted eee mode advertisement 1578 * @phydev: target phy_device struct 1579 * 1580 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy 1581 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't 1582 * changed, and 1 if it has changed. 1583 */ 1584 static int genphy_config_eee_advert(struct phy_device *phydev) 1585 { 1586 int broken = phydev->eee_broken_modes; 1587 int old_adv, adv; 1588 1589 /* Nothing to disable */ 1590 if (!broken) 1591 return 0; 1592 1593 /* If the following call fails, we assume that EEE is not 1594 * supported by the phy. If we read 0, EEE is not advertised 1595 * In both case, we don't need to continue 1596 */ 1597 adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 1598 if (adv <= 0) 1599 return 0; 1600 1601 old_adv = adv; 1602 adv &= ~broken; 1603 1604 /* Advertising remains unchanged with the broken mask */ 1605 if (old_adv == adv) 1606 return 0; 1607 1608 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv); 1609 1610 return 1; 1611 } 1612 1613 /** 1614 * genphy_setup_forced - configures/forces speed/duplex from @phydev 1615 * @phydev: target phy_device struct 1616 * 1617 * Description: Configures MII_BMCR to force speed/duplex 1618 * to the values in phydev. Assumes that the values are valid. 1619 * Please see phy_sanitize_settings(). 1620 */ 1621 int genphy_setup_forced(struct phy_device *phydev) 1622 { 1623 u16 ctl = 0; 1624 1625 phydev->pause = 0; 1626 phydev->asym_pause = 0; 1627 1628 if (SPEED_1000 == phydev->speed) 1629 ctl |= BMCR_SPEED1000; 1630 else if (SPEED_100 == phydev->speed) 1631 ctl |= BMCR_SPEED100; 1632 1633 if (DUPLEX_FULL == phydev->duplex) 1634 ctl |= BMCR_FULLDPLX; 1635 1636 return phy_modify(phydev, MII_BMCR, 1637 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); 1638 } 1639 EXPORT_SYMBOL(genphy_setup_forced); 1640 1641 /** 1642 * genphy_restart_aneg - Enable and Restart Autonegotiation 1643 * @phydev: target phy_device struct 1644 */ 1645 int genphy_restart_aneg(struct phy_device *phydev) 1646 { 1647 /* Don't isolate the PHY if we're negotiating */ 1648 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, 1649 BMCR_ANENABLE | BMCR_ANRESTART); 1650 } 1651 EXPORT_SYMBOL(genphy_restart_aneg); 1652 1653 /** 1654 * genphy_config_aneg - restart auto-negotiation or write BMCR 1655 * @phydev: target phy_device struct 1656 * 1657 * Description: If auto-negotiation is enabled, we configure the 1658 * advertising, and then restart auto-negotiation. If it is not 1659 * enabled, then we write the BMCR. 1660 */ 1661 int genphy_config_aneg(struct phy_device *phydev) 1662 { 1663 int err, changed; 1664 1665 changed = genphy_config_eee_advert(phydev); 1666 1667 if (AUTONEG_ENABLE != phydev->autoneg) 1668 return genphy_setup_forced(phydev); 1669 1670 err = genphy_config_advert(phydev); 1671 if (err < 0) /* error */ 1672 return err; 1673 1674 changed |= err; 1675 1676 if (changed == 0) { 1677 /* Advertisement hasn't changed, but maybe aneg was never on to 1678 * begin with? Or maybe phy was isolated? 1679 */ 1680 int ctl = phy_read(phydev, MII_BMCR); 1681 1682 if (ctl < 0) 1683 return ctl; 1684 1685 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) 1686 changed = 1; /* do restart aneg */ 1687 } 1688 1689 /* Only restart aneg if we are advertising something different 1690 * than we were before. 1691 */ 1692 if (changed > 0) 1693 return genphy_restart_aneg(phydev); 1694 1695 return 0; 1696 } 1697 EXPORT_SYMBOL(genphy_config_aneg); 1698 1699 /** 1700 * genphy_aneg_done - return auto-negotiation status 1701 * @phydev: target phy_device struct 1702 * 1703 * Description: Reads the status register and returns 0 either if 1704 * auto-negotiation is incomplete, or if there was an error. 1705 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. 1706 */ 1707 int genphy_aneg_done(struct phy_device *phydev) 1708 { 1709 int retval = phy_read(phydev, MII_BMSR); 1710 1711 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); 1712 } 1713 EXPORT_SYMBOL(genphy_aneg_done); 1714 1715 /** 1716 * genphy_update_link - update link status in @phydev 1717 * @phydev: target phy_device struct 1718 * 1719 * Description: Update the value in phydev->link to reflect the 1720 * current link value. In order to do this, we need to read 1721 * the status register twice, keeping the second value. 1722 */ 1723 int genphy_update_link(struct phy_device *phydev) 1724 { 1725 int status; 1726 1727 /* Do a fake read */ 1728 status = phy_read(phydev, MII_BMSR); 1729 if (status < 0) 1730 return status; 1731 1732 /* Read link and autonegotiation status */ 1733 status = phy_read(phydev, MII_BMSR); 1734 if (status < 0) 1735 return status; 1736 1737 if ((status & BMSR_LSTATUS) == 0) 1738 phydev->link = 0; 1739 else 1740 phydev->link = 1; 1741 1742 return 0; 1743 } 1744 EXPORT_SYMBOL(genphy_update_link); 1745 1746 /** 1747 * genphy_read_status - check the link status and update current link state 1748 * @phydev: target phy_device struct 1749 * 1750 * Description: Check the link, then figure out the current state 1751 * by comparing what we advertise with what the link partner 1752 * advertises. Start by checking the gigabit possibilities, 1753 * then move on to 10/100. 1754 */ 1755 int genphy_read_status(struct phy_device *phydev) 1756 { 1757 int adv; 1758 int err; 1759 int lpa; 1760 int lpagb = 0; 1761 int common_adv; 1762 int common_adv_gb = 0; 1763 1764 /* Update the link, but return if there was an error */ 1765 err = genphy_update_link(phydev); 1766 if (err) 1767 return err; 1768 1769 linkmode_zero(phydev->lp_advertising); 1770 1771 if (AUTONEG_ENABLE == phydev->autoneg) { 1772 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 1773 phydev->supported) || 1774 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 1775 phydev->supported)) { 1776 lpagb = phy_read(phydev, MII_STAT1000); 1777 if (lpagb < 0) 1778 return lpagb; 1779 1780 adv = phy_read(phydev, MII_CTRL1000); 1781 if (adv < 0) 1782 return adv; 1783 1784 if (lpagb & LPA_1000MSFAIL) { 1785 if (adv & CTL1000_ENABLE_MASTER) 1786 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); 1787 else 1788 phydev_err(phydev, "Master/Slave resolution failed\n"); 1789 return -ENOLINK; 1790 } 1791 1792 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 1793 lpagb); 1794 common_adv_gb = lpagb & adv << 2; 1795 } 1796 1797 lpa = phy_read(phydev, MII_LPA); 1798 if (lpa < 0) 1799 return lpa; 1800 1801 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); 1802 1803 adv = phy_read(phydev, MII_ADVERTISE); 1804 if (adv < 0) 1805 return adv; 1806 1807 common_adv = lpa & adv; 1808 1809 phydev->speed = SPEED_10; 1810 phydev->duplex = DUPLEX_HALF; 1811 phydev->pause = 0; 1812 phydev->asym_pause = 0; 1813 1814 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) { 1815 phydev->speed = SPEED_1000; 1816 1817 if (common_adv_gb & LPA_1000FULL) 1818 phydev->duplex = DUPLEX_FULL; 1819 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) { 1820 phydev->speed = SPEED_100; 1821 1822 if (common_adv & LPA_100FULL) 1823 phydev->duplex = DUPLEX_FULL; 1824 } else 1825 if (common_adv & LPA_10FULL) 1826 phydev->duplex = DUPLEX_FULL; 1827 1828 if (phydev->duplex == DUPLEX_FULL) { 1829 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0; 1830 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0; 1831 } 1832 } else { 1833 int bmcr = phy_read(phydev, MII_BMCR); 1834 1835 if (bmcr < 0) 1836 return bmcr; 1837 1838 if (bmcr & BMCR_FULLDPLX) 1839 phydev->duplex = DUPLEX_FULL; 1840 else 1841 phydev->duplex = DUPLEX_HALF; 1842 1843 if (bmcr & BMCR_SPEED1000) 1844 phydev->speed = SPEED_1000; 1845 else if (bmcr & BMCR_SPEED100) 1846 phydev->speed = SPEED_100; 1847 else 1848 phydev->speed = SPEED_10; 1849 1850 phydev->pause = 0; 1851 phydev->asym_pause = 0; 1852 } 1853 1854 return 0; 1855 } 1856 EXPORT_SYMBOL(genphy_read_status); 1857 1858 /** 1859 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit 1860 * @phydev: target phy_device struct 1861 * 1862 * Description: Perform a software PHY reset using the standard 1863 * BMCR_RESET bit and poll for the reset bit to be cleared. 1864 * 1865 * Returns: 0 on success, < 0 on failure 1866 */ 1867 int genphy_soft_reset(struct phy_device *phydev) 1868 { 1869 int ret; 1870 1871 ret = phy_write(phydev, MII_BMCR, BMCR_RESET); 1872 if (ret < 0) 1873 return ret; 1874 1875 return phy_poll_reset(phydev); 1876 } 1877 EXPORT_SYMBOL(genphy_soft_reset); 1878 1879 int genphy_config_init(struct phy_device *phydev) 1880 { 1881 int val; 1882 __ETHTOOL_DECLARE_LINK_MODE_MASK(features) = { 0, }; 1883 1884 linkmode_set_bit_array(phy_basic_ports_array, 1885 ARRAY_SIZE(phy_basic_ports_array), 1886 features); 1887 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, features); 1888 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, features); 1889 1890 /* Do we support autonegotiation? */ 1891 val = phy_read(phydev, MII_BMSR); 1892 if (val < 0) 1893 return val; 1894 1895 if (val & BMSR_ANEGCAPABLE) 1896 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, features); 1897 1898 if (val & BMSR_100FULL) 1899 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, features); 1900 if (val & BMSR_100HALF) 1901 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, features); 1902 if (val & BMSR_10FULL) 1903 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, features); 1904 if (val & BMSR_10HALF) 1905 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, features); 1906 1907 if (val & BMSR_ESTATEN) { 1908 val = phy_read(phydev, MII_ESTATUS); 1909 if (val < 0) 1910 return val; 1911 1912 if (val & ESTATUS_1000_TFULL) 1913 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 1914 features); 1915 if (val & ESTATUS_1000_THALF) 1916 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 1917 features); 1918 } 1919 1920 linkmode_and(phydev->supported, phydev->supported, features); 1921 linkmode_and(phydev->advertising, phydev->advertising, features); 1922 1923 return 0; 1924 } 1925 EXPORT_SYMBOL(genphy_config_init); 1926 1927 /* This is used for the phy device which doesn't support the MMD extended 1928 * register access, but it does have side effect when we are trying to access 1929 * the MMD register via indirect method. 1930 */ 1931 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) 1932 { 1933 return -EOPNOTSUPP; 1934 } 1935 EXPORT_SYMBOL(genphy_read_mmd_unsupported); 1936 1937 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, 1938 u16 regnum, u16 val) 1939 { 1940 return -EOPNOTSUPP; 1941 } 1942 EXPORT_SYMBOL(genphy_write_mmd_unsupported); 1943 1944 int genphy_suspend(struct phy_device *phydev) 1945 { 1946 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); 1947 } 1948 EXPORT_SYMBOL(genphy_suspend); 1949 1950 int genphy_resume(struct phy_device *phydev) 1951 { 1952 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); 1953 } 1954 EXPORT_SYMBOL(genphy_resume); 1955 1956 int genphy_loopback(struct phy_device *phydev, bool enable) 1957 { 1958 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 1959 enable ? BMCR_LOOPBACK : 0); 1960 } 1961 EXPORT_SYMBOL(genphy_loopback); 1962 1963 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed) 1964 { 1965 switch (max_speed) { 1966 case SPEED_10: 1967 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 1968 phydev->supported); 1969 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 1970 phydev->supported); 1971 /* fall through */ 1972 case SPEED_100: 1973 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 1974 phydev->supported); 1975 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 1976 phydev->supported); 1977 break; 1978 case SPEED_1000: 1979 break; 1980 default: 1981 return -ENOTSUPP; 1982 } 1983 1984 return 0; 1985 } 1986 1987 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed) 1988 { 1989 int err; 1990 1991 err = __set_phy_supported(phydev, max_speed); 1992 if (err) 1993 return err; 1994 1995 linkmode_copy(phydev->advertising, phydev->supported); 1996 1997 return 0; 1998 } 1999 EXPORT_SYMBOL(phy_set_max_speed); 2000 2001 /** 2002 * phy_remove_link_mode - Remove a supported link mode 2003 * @phydev: phy_device structure to remove link mode from 2004 * @link_mode: Link mode to be removed 2005 * 2006 * Description: Some MACs don't support all link modes which the PHY 2007 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper 2008 * to remove a link mode. 2009 */ 2010 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) 2011 { 2012 linkmode_clear_bit(link_mode, phydev->supported); 2013 linkmode_copy(phydev->advertising, phydev->supported); 2014 } 2015 EXPORT_SYMBOL(phy_remove_link_mode); 2016 2017 /** 2018 * phy_support_sym_pause - Enable support of symmetrical pause 2019 * @phydev: target phy_device struct 2020 * 2021 * Description: Called by the MAC to indicate is supports symmetrical 2022 * Pause, but not asym pause. 2023 */ 2024 void phy_support_sym_pause(struct phy_device *phydev) 2025 { 2026 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 2027 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 2028 linkmode_copy(phydev->advertising, phydev->supported); 2029 } 2030 EXPORT_SYMBOL(phy_support_sym_pause); 2031 2032 /** 2033 * phy_support_asym_pause - Enable support of asym pause 2034 * @phydev: target phy_device struct 2035 * 2036 * Description: Called by the MAC to indicate is supports Asym Pause. 2037 */ 2038 void phy_support_asym_pause(struct phy_device *phydev) 2039 { 2040 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 2041 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 2042 linkmode_copy(phydev->advertising, phydev->supported); 2043 } 2044 EXPORT_SYMBOL(phy_support_asym_pause); 2045 2046 /** 2047 * phy_set_sym_pause - Configure symmetric Pause 2048 * @phydev: target phy_device struct 2049 * @rx: Receiver Pause is supported 2050 * @tx: Transmit Pause is supported 2051 * @autoneg: Auto neg should be used 2052 * 2053 * Description: Configure advertised Pause support depending on if 2054 * receiver pause and pause auto neg is supported. Generally called 2055 * from the set_pauseparam .ndo. 2056 */ 2057 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, 2058 bool autoneg) 2059 { 2060 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 2061 2062 if (rx && tx && autoneg) 2063 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2064 phydev->supported); 2065 2066 linkmode_copy(phydev->advertising, phydev->supported); 2067 } 2068 EXPORT_SYMBOL(phy_set_sym_pause); 2069 2070 /** 2071 * phy_set_asym_pause - Configure Pause and Asym Pause 2072 * @phydev: target phy_device struct 2073 * @rx: Receiver Pause is supported 2074 * @tx: Transmit Pause is supported 2075 * 2076 * Description: Configure advertised Pause support depending on if 2077 * transmit and receiver pause is supported. If there has been a 2078 * change in adverting, trigger a new autoneg. Generally called from 2079 * the set_pauseparam .ndo. 2080 */ 2081 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) 2082 { 2083 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); 2084 2085 linkmode_copy(oldadv, phydev->advertising); 2086 2087 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2088 phydev->advertising); 2089 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2090 phydev->advertising); 2091 2092 if (rx) { 2093 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2094 phydev->advertising); 2095 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2096 phydev->advertising); 2097 } 2098 2099 if (tx) 2100 linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2101 phydev->advertising); 2102 2103 if (!linkmode_equal(oldadv, phydev->advertising) && 2104 phydev->autoneg) 2105 phy_start_aneg(phydev); 2106 } 2107 EXPORT_SYMBOL(phy_set_asym_pause); 2108 2109 /** 2110 * phy_validate_pause - Test if the PHY/MAC support the pause configuration 2111 * @phydev: phy_device struct 2112 * @pp: requested pause configuration 2113 * 2114 * Description: Test if the PHY/MAC combination supports the Pause 2115 * configuration the user is requesting. Returns True if it is 2116 * supported, false otherwise. 2117 */ 2118 bool phy_validate_pause(struct phy_device *phydev, 2119 struct ethtool_pauseparam *pp) 2120 { 2121 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2122 phydev->supported) || 2123 (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2124 phydev->supported) && 2125 pp->rx_pause != pp->tx_pause)) 2126 return false; 2127 return true; 2128 } 2129 EXPORT_SYMBOL(phy_validate_pause); 2130 2131 static void of_set_phy_supported(struct phy_device *phydev) 2132 { 2133 struct device_node *node = phydev->mdio.dev.of_node; 2134 u32 max_speed; 2135 2136 if (!IS_ENABLED(CONFIG_OF_MDIO)) 2137 return; 2138 2139 if (!node) 2140 return; 2141 2142 if (!of_property_read_u32(node, "max-speed", &max_speed)) 2143 __set_phy_supported(phydev, max_speed); 2144 } 2145 2146 static void of_set_phy_eee_broken(struct phy_device *phydev) 2147 { 2148 struct device_node *node = phydev->mdio.dev.of_node; 2149 u32 broken = 0; 2150 2151 if (!IS_ENABLED(CONFIG_OF_MDIO)) 2152 return; 2153 2154 if (!node) 2155 return; 2156 2157 if (of_property_read_bool(node, "eee-broken-100tx")) 2158 broken |= MDIO_EEE_100TX; 2159 if (of_property_read_bool(node, "eee-broken-1000t")) 2160 broken |= MDIO_EEE_1000T; 2161 if (of_property_read_bool(node, "eee-broken-10gt")) 2162 broken |= MDIO_EEE_10GT; 2163 if (of_property_read_bool(node, "eee-broken-1000kx")) 2164 broken |= MDIO_EEE_1000KX; 2165 if (of_property_read_bool(node, "eee-broken-10gkx4")) 2166 broken |= MDIO_EEE_10GKX4; 2167 if (of_property_read_bool(node, "eee-broken-10gkr")) 2168 broken |= MDIO_EEE_10GKR; 2169 2170 phydev->eee_broken_modes = broken; 2171 } 2172 2173 static bool phy_drv_supports_irq(struct phy_driver *phydrv) 2174 { 2175 return phydrv->config_intr && phydrv->ack_interrupt; 2176 } 2177 2178 /** 2179 * phy_probe - probe and init a PHY device 2180 * @dev: device to probe and init 2181 * 2182 * Description: Take care of setting up the phy_device structure, 2183 * set the state to READY (the driver's init function should 2184 * set it to STARTING if needed). 2185 */ 2186 static int phy_probe(struct device *dev) 2187 { 2188 struct phy_device *phydev = to_phy_device(dev); 2189 struct device_driver *drv = phydev->mdio.dev.driver; 2190 struct phy_driver *phydrv = to_phy_driver(drv); 2191 int err = 0; 2192 2193 phydev->drv = phydrv; 2194 2195 /* Disable the interrupt if the PHY doesn't support it 2196 * but the interrupt is still a valid one 2197 */ 2198 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) 2199 phydev->irq = PHY_POLL; 2200 2201 if (phydrv->flags & PHY_IS_INTERNAL) 2202 phydev->is_internal = true; 2203 2204 mutex_lock(&phydev->lock); 2205 2206 /* Start out supporting everything. Eventually, 2207 * a controller will attach, and may modify one 2208 * or both of these values 2209 */ 2210 linkmode_copy(phydev->supported, phydrv->features); 2211 of_set_phy_supported(phydev); 2212 linkmode_copy(phydev->advertising, phydev->supported); 2213 2214 /* Get the EEE modes we want to prohibit. We will ask 2215 * the PHY stop advertising these mode later on 2216 */ 2217 of_set_phy_eee_broken(phydev); 2218 2219 /* The Pause Frame bits indicate that the PHY can support passing 2220 * pause frames. During autonegotiation, the PHYs will determine if 2221 * they should allow pause frames to pass. The MAC driver should then 2222 * use that result to determine whether to enable flow control via 2223 * pause frames. 2224 * 2225 * Normally, PHY drivers should not set the Pause bits, and instead 2226 * allow phylib to do that. However, there may be some situations 2227 * (e.g. hardware erratum) where the driver wants to set only one 2228 * of these bits. 2229 */ 2230 if (test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydrv->features) || 2231 test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydrv->features)) { 2232 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2233 phydev->supported); 2234 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2235 phydev->supported); 2236 if (test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydrv->features)) 2237 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2238 phydev->supported); 2239 if (test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2240 phydrv->features)) 2241 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2242 phydev->supported); 2243 } else { 2244 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2245 phydev->supported); 2246 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2247 phydev->supported); 2248 } 2249 2250 /* Set the state to READY by default */ 2251 phydev->state = PHY_READY; 2252 2253 if (phydev->drv->probe) { 2254 /* Deassert the reset signal */ 2255 phy_device_reset(phydev, 0); 2256 2257 err = phydev->drv->probe(phydev); 2258 if (err) { 2259 /* Assert the reset signal */ 2260 phy_device_reset(phydev, 1); 2261 } 2262 } 2263 2264 mutex_unlock(&phydev->lock); 2265 2266 return err; 2267 } 2268 2269 static int phy_remove(struct device *dev) 2270 { 2271 struct phy_device *phydev = to_phy_device(dev); 2272 2273 cancel_delayed_work_sync(&phydev->state_queue); 2274 2275 mutex_lock(&phydev->lock); 2276 phydev->state = PHY_DOWN; 2277 mutex_unlock(&phydev->lock); 2278 2279 if (phydev->drv && phydev->drv->remove) { 2280 phydev->drv->remove(phydev); 2281 2282 /* Assert the reset signal */ 2283 phy_device_reset(phydev, 1); 2284 } 2285 phydev->drv = NULL; 2286 2287 return 0; 2288 } 2289 2290 /** 2291 * phy_driver_register - register a phy_driver with the PHY layer 2292 * @new_driver: new phy_driver to register 2293 * @owner: module owning this PHY 2294 */ 2295 int phy_driver_register(struct phy_driver *new_driver, struct module *owner) 2296 { 2297 int retval; 2298 2299 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; 2300 new_driver->mdiodrv.driver.name = new_driver->name; 2301 new_driver->mdiodrv.driver.bus = &mdio_bus_type; 2302 new_driver->mdiodrv.driver.probe = phy_probe; 2303 new_driver->mdiodrv.driver.remove = phy_remove; 2304 new_driver->mdiodrv.driver.owner = owner; 2305 2306 retval = driver_register(&new_driver->mdiodrv.driver); 2307 if (retval) { 2308 pr_err("%s: Error %d in registering driver\n", 2309 new_driver->name, retval); 2310 2311 return retval; 2312 } 2313 2314 pr_debug("%s: Registered new driver\n", new_driver->name); 2315 2316 return 0; 2317 } 2318 EXPORT_SYMBOL(phy_driver_register); 2319 2320 int phy_drivers_register(struct phy_driver *new_driver, int n, 2321 struct module *owner) 2322 { 2323 int i, ret = 0; 2324 2325 for (i = 0; i < n; i++) { 2326 ret = phy_driver_register(new_driver + i, owner); 2327 if (ret) { 2328 while (i-- > 0) 2329 phy_driver_unregister(new_driver + i); 2330 break; 2331 } 2332 } 2333 return ret; 2334 } 2335 EXPORT_SYMBOL(phy_drivers_register); 2336 2337 void phy_driver_unregister(struct phy_driver *drv) 2338 { 2339 driver_unregister(&drv->mdiodrv.driver); 2340 } 2341 EXPORT_SYMBOL(phy_driver_unregister); 2342 2343 void phy_drivers_unregister(struct phy_driver *drv, int n) 2344 { 2345 int i; 2346 2347 for (i = 0; i < n; i++) 2348 phy_driver_unregister(drv + i); 2349 } 2350 EXPORT_SYMBOL(phy_drivers_unregister); 2351 2352 static struct phy_driver genphy_driver = { 2353 .phy_id = 0xffffffff, 2354 .phy_id_mask = 0xffffffff, 2355 .name = "Generic PHY", 2356 .soft_reset = genphy_no_soft_reset, 2357 .config_init = genphy_config_init, 2358 .features = PHY_GBIT_ALL_PORTS_FEATURES, 2359 .aneg_done = genphy_aneg_done, 2360 .suspend = genphy_suspend, 2361 .resume = genphy_resume, 2362 .set_loopback = genphy_loopback, 2363 }; 2364 2365 static int __init phy_init(void) 2366 { 2367 int rc; 2368 2369 rc = mdio_bus_init(); 2370 if (rc) 2371 return rc; 2372 2373 features_init(); 2374 2375 rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE); 2376 if (rc) 2377 goto err_10g; 2378 2379 rc = phy_driver_register(&genphy_driver, THIS_MODULE); 2380 if (rc) { 2381 phy_driver_unregister(&genphy_10g_driver); 2382 err_10g: 2383 mdio_bus_exit(); 2384 } 2385 2386 return rc; 2387 } 2388 2389 static void __exit phy_exit(void) 2390 { 2391 phy_driver_unregister(&genphy_10g_driver); 2392 phy_driver_unregister(&genphy_driver); 2393 mdio_bus_exit(); 2394 } 2395 2396 subsys_initcall(phy_init); 2397 module_exit(phy_exit); 2398