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