Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

net: phy: factor out provider part from mdio_bus.c

After 52358dd63e34 ("net: phy: remove function stubs") there's a
problem if CONFIG_MDIO_BUS is set, but CONFIG_PHYLIB is not.
mdiobus_scan() uses phylib functions like get_phy_device().
Bringing back the stub wouldn't make much sense, because it would
allow to compile mdiobus_scan(), but the function would be unusable.
The stub returned NULL, and we have the following in mdiobus_scan():

phydev = get_phy_device(bus, addr, c45);
if (IS_ERR(phydev))
return phydev;

So calling mdiobus_scan() w/o CONFIG_PHYLIB would cause a crash later in
mdiobus_scan(). In general the PHYLIB functionality isn't optional here.
Consequently, MDIO bus providers depend on PHYLIB.
Therefore factor it out and build it together with the libphy core
modules. In addition make all MDIO bus providers under /drivers/net/mdio
depend on PHYLIB. Same applies to enetc MDIO bus provider. Note that
PHYLIB selects MDIO_DEVRES, therefore we can omit this here.

Fixes: 52358dd63e34 ("net: phy: remove function stubs")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202504270639.mT0lh2o1-lkp@intel.com/
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
Link: https://patch.msgid.link/c74772a9-dab6-44bf-a657-389df89d85c2@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Heiner Kallweit and committed by
Jakub Kicinski
a3e1c0ad 51cf06dd

+494 -474
+1 -2
drivers/net/ethernet/freescale/enetc/Kconfig
··· 73 73 74 74 config FSL_ENETC_MDIO 75 75 tristate "ENETC MDIO driver" 76 - depends on PCI && MDIO_BUS 77 - select MDIO_DEVRES 76 + depends on PCI && PHYLIB 78 77 help 79 78 This driver supports NXP ENETC Central MDIO controller as a PCIe 80 79 physical function (PF) device.
+5 -11
drivers/net/mdio/Kconfig
··· 19 19 reflects whether the mdio_bus/mdio_device code is built as a 20 20 loadable module or built-in. 21 21 22 + if PHYLIB 23 + 22 24 config FWNODE_MDIO 23 - def_tristate PHYLIB 24 - depends on (ACPI || OF) || COMPILE_TEST 25 + def_tristate (ACPI || OF) || COMPILE_TEST 25 26 select FIXED_PHY 26 27 help 27 28 FWNODE MDIO bus (Ethernet PHY) accessors 28 29 29 30 config OF_MDIO 30 - def_tristate PHYLIB 31 - depends on OF 32 - depends on PHYLIB 31 + def_tristate OF 33 32 select FIXED_PHY 34 33 help 35 34 OpenFirmware MDIO bus (Ethernet PHY) accessors 36 35 37 36 config ACPI_MDIO 38 - def_tristate PHYLIB 39 - depends on ACPI 40 - depends on PHYLIB 37 + def_tristate ACPI 41 38 help 42 39 ACPI MDIO bus (Ethernet PHY) accessors 43 - 44 - if MDIO_BUS 45 40 46 41 config MDIO_DEVRES 47 42 tristate ··· 52 57 config MDIO_XGENE 53 58 tristate "APM X-Gene SoC MDIO bus controller" 54 59 depends on ARCH_XGENE || COMPILE_TEST 55 - depends on PHYLIB 56 60 help 57 61 This module provides a driver for the MDIO busses found in the 58 62 APM X-Gene SoC's.
+1 -1
drivers/net/phy/Makefile
··· 3 3 4 4 libphy-y := phy.o phy-c45.o phy-core.o phy_device.o \ 5 5 linkmode.o phy_link_topology.o \ 6 - phy_package.o phy_caps.o 6 + phy_package.o phy_caps.o mdio_bus_provider.o 7 7 mdio-bus-y += mdio_bus.o mdio_device.o 8 8 9 9 ifdef CONFIG_MDIO_DEVICE
+2 -460
drivers/net/phy/mdio_bus.c
··· 8 8 9 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 10 11 - #include <linux/delay.h> 12 11 #include <linux/device.h> 13 12 #include <linux/errno.h> 14 13 #include <linux/etherdevice.h> 15 14 #include <linux/ethtool.h> 16 15 #include <linux/gpio/consumer.h> 17 16 #include <linux/init.h> 18 - #include <linux/interrupt.h> 19 17 #include <linux/io.h> 20 18 #include <linux/kernel.h> 21 - #include <linux/micrel_phy.h> 22 19 #include <linux/mii.h> 23 20 #include <linux/mm.h> 24 21 #include <linux/module.h> ··· 24 27 #include <linux/of_mdio.h> 25 28 #include <linux/phy.h> 26 29 #include <linux/reset.h> 27 - #include <linux/skbuff.h> 28 30 #include <linux/slab.h> 29 31 #include <linux/spinlock.h> 30 32 #include <linux/string.h> ··· 32 36 33 37 #define CREATE_TRACE_POINTS 34 38 #include <trace/events/mdio.h> 35 - 36 - #include "mdio-boardinfo.h" 37 39 38 40 static int mdiobus_register_gpiod(struct mdio_device *mdiodev) 39 41 { ··· 129 135 return mdiobus_find_device(bus, addr) != NULL; 130 136 } 131 137 EXPORT_SYMBOL(mdiobus_is_registered_device); 132 - 133 - /** 134 - * mdiobus_alloc_size - allocate a mii_bus structure 135 - * @size: extra amount of memory to allocate for private storage. 136 - * If non-zero, then bus->priv is points to that memory. 137 - * 138 - * Description: called by a bus driver to allocate an mii_bus 139 - * structure to fill in. 140 - */ 141 - struct mii_bus *mdiobus_alloc_size(size_t size) 142 - { 143 - struct mii_bus *bus; 144 - size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); 145 - size_t alloc_size; 146 - int i; 147 - 148 - /* If we alloc extra space, it should be aligned */ 149 - if (size) 150 - alloc_size = aligned_size + size; 151 - else 152 - alloc_size = sizeof(*bus); 153 - 154 - bus = kzalloc(alloc_size, GFP_KERNEL); 155 - if (!bus) 156 - return NULL; 157 - 158 - bus->state = MDIOBUS_ALLOCATED; 159 - if (size) 160 - bus->priv = (void *)bus + aligned_size; 161 - 162 - /* Initialise the interrupts to polling and 64-bit seqcounts */ 163 - for (i = 0; i < PHY_MAX_ADDR; i++) { 164 - bus->irq[i] = PHY_POLL; 165 - u64_stats_init(&bus->stats[i].syncp); 166 - } 167 - 168 - return bus; 169 - } 170 - EXPORT_SYMBOL(mdiobus_alloc_size); 171 138 172 139 /** 173 140 * mdiobus_release - mii_bus device release callback ··· 358 403 NULL, 359 404 }; 360 405 361 - static struct class mdio_bus_class = { 406 + const struct class mdio_bus_class = { 362 407 .name = "mdio_bus", 363 408 .dev_release = mdiobus_release, 364 409 .dev_groups = mdio_bus_groups, 365 410 }; 411 + EXPORT_SYMBOL_GPL(mdio_bus_class); 366 412 367 413 /** 368 414 * mdio_find_bus - Given the name of a mdiobus, find the mii_bus. ··· 407 451 return d ? to_mii_bus(d) : NULL; 408 452 } 409 453 EXPORT_SYMBOL(of_mdio_find_bus); 410 - 411 - /* Walk the list of subnodes of a mdio bus and look for a node that 412 - * matches the mdio device's address with its 'reg' property. If 413 - * found, set the of_node pointer for the mdio device. This allows 414 - * auto-probed phy devices to be supplied with information passed in 415 - * via DT. 416 - * If a PHY package is found, PHY is searched also there. 417 - */ 418 - static int of_mdiobus_find_phy(struct device *dev, struct mdio_device *mdiodev, 419 - struct device_node *np) 420 - { 421 - struct device_node *child; 422 - 423 - for_each_available_child_of_node(np, child) { 424 - int addr; 425 - 426 - if (of_node_name_eq(child, "ethernet-phy-package")) { 427 - /* Validate PHY package reg presence */ 428 - if (!of_property_present(child, "reg")) { 429 - of_node_put(child); 430 - return -EINVAL; 431 - } 432 - 433 - if (!of_mdiobus_find_phy(dev, mdiodev, child)) { 434 - /* The refcount for the PHY package will be 435 - * incremented later when PHY join the Package. 436 - */ 437 - of_node_put(child); 438 - return 0; 439 - } 440 - 441 - continue; 442 - } 443 - 444 - addr = of_mdio_parse_addr(dev, child); 445 - if (addr < 0) 446 - continue; 447 - 448 - if (addr == mdiodev->addr) { 449 - device_set_node(dev, of_fwnode_handle(child)); 450 - /* The refcount on "child" is passed to the mdio 451 - * device. Do _not_ use of_node_put(child) here. 452 - */ 453 - return 0; 454 - } 455 - } 456 - 457 - return -ENODEV; 458 - } 459 - 460 - static void of_mdiobus_link_mdiodev(struct mii_bus *bus, 461 - struct mdio_device *mdiodev) 462 - { 463 - struct device *dev = &mdiodev->dev; 464 - 465 - if (dev->of_node || !bus->dev.of_node) 466 - return; 467 - 468 - of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node); 469 - } 470 - #else /* !IS_ENABLED(CONFIG_OF_MDIO) */ 471 - static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio, 472 - struct mdio_device *mdiodev) 473 - { 474 - } 475 454 #endif 476 - 477 - /** 478 - * mdiobus_create_device - create a full MDIO device given 479 - * a mdio_board_info structure 480 - * @bus: MDIO bus to create the devices on 481 - * @bi: mdio_board_info structure describing the devices 482 - * 483 - * Returns 0 on success or < 0 on error. 484 - */ 485 - static int mdiobus_create_device(struct mii_bus *bus, 486 - struct mdio_board_info *bi) 487 - { 488 - struct mdio_device *mdiodev; 489 - int ret = 0; 490 - 491 - mdiodev = mdio_device_create(bus, bi->mdio_addr); 492 - if (IS_ERR(mdiodev)) 493 - return -ENODEV; 494 - 495 - strscpy(mdiodev->modalias, bi->modalias, 496 - sizeof(mdiodev->modalias)); 497 - mdiodev->bus_match = mdio_device_bus_match; 498 - mdiodev->dev.platform_data = (void *)bi->platform_data; 499 - 500 - ret = mdio_device_register(mdiodev); 501 - if (ret) 502 - mdio_device_free(mdiodev); 503 - 504 - return ret; 505 - } 506 - 507 - static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45) 508 - { 509 - struct phy_device *phydev = ERR_PTR(-ENODEV); 510 - struct fwnode_handle *fwnode; 511 - char node_name[16]; 512 - int err; 513 - 514 - phydev = get_phy_device(bus, addr, c45); 515 - if (IS_ERR(phydev)) 516 - return phydev; 517 - 518 - /* For DT, see if the auto-probed phy has a corresponding child 519 - * in the bus node, and set the of_node pointer in this case. 520 - */ 521 - of_mdiobus_link_mdiodev(bus, &phydev->mdio); 522 - 523 - /* Search for a swnode for the phy in the swnode hierarchy of the bus. 524 - * If there is no swnode for the phy provided, just ignore it. 525 - */ 526 - if (dev_fwnode(&bus->dev) && !dev_fwnode(&phydev->mdio.dev)) { 527 - snprintf(node_name, sizeof(node_name), "ethernet-phy@%d", 528 - addr); 529 - fwnode = fwnode_get_named_child_node(dev_fwnode(&bus->dev), 530 - node_name); 531 - if (fwnode) 532 - device_set_node(&phydev->mdio.dev, fwnode); 533 - } 534 - 535 - err = phy_device_register(phydev); 536 - if (err) { 537 - phy_device_free(phydev); 538 - return ERR_PTR(-ENODEV); 539 - } 540 - 541 - return phydev; 542 - } 543 - 544 - /** 545 - * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices. 546 - * @bus: mii_bus to scan 547 - * @addr: address on bus to scan 548 - * 549 - * This function scans one address on the MDIO bus, looking for 550 - * devices which can be identified using a vendor/product ID in 551 - * registers 2 and 3. Not all MDIO devices have such registers, but 552 - * PHY devices typically do. Hence this function assumes anything 553 - * found is a PHY, or can be treated as a PHY. Other MDIO devices, 554 - * such as switches, will probably not be found during the scan. 555 - */ 556 - struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr) 557 - { 558 - return mdiobus_scan(bus, addr, false); 559 - } 560 - EXPORT_SYMBOL(mdiobus_scan_c22); 561 - 562 - /** 563 - * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices. 564 - * @bus: mii_bus to scan 565 - * @addr: address on bus to scan 566 - * 567 - * This function scans one address on the MDIO bus, looking for 568 - * devices which can be identified using a vendor/product ID in 569 - * registers 2 and 3. Not all MDIO devices have such registers, but 570 - * PHY devices typically do. Hence this function assumes anything 571 - * found is a PHY, or can be treated as a PHY. Other MDIO devices, 572 - * such as switches, will probably not be found during the scan. 573 - */ 574 - static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr) 575 - { 576 - return mdiobus_scan(bus, addr, true); 577 - } 578 - 579 - static int mdiobus_scan_bus_c22(struct mii_bus *bus) 580 - { 581 - int i; 582 - 583 - for (i = 0; i < PHY_MAX_ADDR; i++) { 584 - if ((bus->phy_mask & BIT(i)) == 0) { 585 - struct phy_device *phydev; 586 - 587 - phydev = mdiobus_scan_c22(bus, i); 588 - if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 589 - return PTR_ERR(phydev); 590 - } 591 - } 592 - return 0; 593 - } 594 - 595 - static int mdiobus_scan_bus_c45(struct mii_bus *bus) 596 - { 597 - int i; 598 - 599 - for (i = 0; i < PHY_MAX_ADDR; i++) { 600 - if ((bus->phy_mask & BIT(i)) == 0) { 601 - struct phy_device *phydev; 602 - 603 - /* Don't scan C45 if we already have a C22 device */ 604 - if (bus->mdio_map[i]) 605 - continue; 606 - 607 - phydev = mdiobus_scan_c45(bus, i); 608 - if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 609 - return PTR_ERR(phydev); 610 - } 611 - } 612 - return 0; 613 - } 614 - 615 - /* There are some C22 PHYs which do bad things when where is a C45 616 - * transaction on the bus, like accepting a read themselves, and 617 - * stomping over the true devices reply, to performing a write to 618 - * themselves which was intended for another device. Now that C22 619 - * devices have been found, see if any of them are bad for C45, and if we 620 - * should skip the C45 scan. 621 - */ 622 - static bool mdiobus_prevent_c45_scan(struct mii_bus *bus) 623 - { 624 - int i; 625 - 626 - for (i = 0; i < PHY_MAX_ADDR; i++) { 627 - struct phy_device *phydev; 628 - u32 oui; 629 - 630 - phydev = mdiobus_get_phy(bus, i); 631 - if (!phydev) 632 - continue; 633 - oui = phydev->phy_id >> 10; 634 - 635 - if (oui == MICREL_OUI) 636 - return true; 637 - } 638 - return false; 639 - } 640 - 641 - /** 642 - * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus 643 - * @bus: target mii_bus 644 - * @owner: module containing bus accessor functions 645 - * 646 - * Description: Called by a bus driver to bring up all the PHYs 647 - * on a given bus, and attach them to the bus. Drivers should use 648 - * mdiobus_register() rather than __mdiobus_register() unless they 649 - * need to pass a specific owner module. MDIO devices which are not 650 - * PHYs will not be brought up by this function. They are expected 651 - * to be explicitly listed in DT and instantiated by of_mdiobus_register(). 652 - * 653 - * Returns 0 on success or < 0 on error. 654 - */ 655 - int __mdiobus_register(struct mii_bus *bus, struct module *owner) 656 - { 657 - struct mdio_device *mdiodev; 658 - struct gpio_desc *gpiod; 659 - bool prevent_c45_scan; 660 - int i, err; 661 - 662 - if (!bus || !bus->name) 663 - return -EINVAL; 664 - 665 - /* An access method always needs both read and write operations */ 666 - if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45) 667 - return -EINVAL; 668 - 669 - /* At least one method is mandatory */ 670 - if (!bus->read && !bus->read_c45) 671 - return -EINVAL; 672 - 673 - if (bus->parent && bus->parent->of_node) 674 - bus->parent->of_node->fwnode.flags |= 675 - FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD; 676 - 677 - WARN(bus->state != MDIOBUS_ALLOCATED && 678 - bus->state != MDIOBUS_UNREGISTERED, 679 - "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id); 680 - 681 - bus->owner = owner; 682 - bus->dev.parent = bus->parent; 683 - bus->dev.class = &mdio_bus_class; 684 - bus->dev.groups = NULL; 685 - dev_set_name(&bus->dev, "%s", bus->id); 686 - 687 - /* If the bus state is allocated, we're registering a fresh bus 688 - * that may have a fwnode associated with it. Grab a reference 689 - * to the fwnode. This will be dropped when the bus is released. 690 - * If the bus was set to unregistered, it means that the bus was 691 - * previously registered, and we've already grabbed a reference. 692 - */ 693 - if (bus->state == MDIOBUS_ALLOCATED) 694 - fwnode_handle_get(dev_fwnode(&bus->dev)); 695 - 696 - /* We need to set state to MDIOBUS_UNREGISTERED to correctly release 697 - * the device in mdiobus_free() 698 - * 699 - * State will be updated later in this function in case of success 700 - */ 701 - bus->state = MDIOBUS_UNREGISTERED; 702 - 703 - err = device_register(&bus->dev); 704 - if (err) { 705 - pr_err("mii_bus %s failed to register\n", bus->id); 706 - return -EINVAL; 707 - } 708 - 709 - mutex_init(&bus->mdio_lock); 710 - mutex_init(&bus->shared_lock); 711 - 712 - /* assert bus level PHY GPIO reset */ 713 - gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH); 714 - if (IS_ERR(gpiod)) { 715 - err = dev_err_probe(&bus->dev, PTR_ERR(gpiod), 716 - "mii_bus %s couldn't get reset GPIO\n", 717 - bus->id); 718 - device_del(&bus->dev); 719 - return err; 720 - } else if (gpiod) { 721 - bus->reset_gpiod = gpiod; 722 - fsleep(bus->reset_delay_us); 723 - gpiod_set_value_cansleep(gpiod, 0); 724 - if (bus->reset_post_delay_us > 0) 725 - fsleep(bus->reset_post_delay_us); 726 - } 727 - 728 - if (bus->reset) { 729 - err = bus->reset(bus); 730 - if (err) 731 - goto error_reset_gpiod; 732 - } 733 - 734 - if (bus->read) { 735 - err = mdiobus_scan_bus_c22(bus); 736 - if (err) 737 - goto error; 738 - } 739 - 740 - prevent_c45_scan = mdiobus_prevent_c45_scan(bus); 741 - 742 - if (!prevent_c45_scan && bus->read_c45) { 743 - err = mdiobus_scan_bus_c45(bus); 744 - if (err) 745 - goto error; 746 - } 747 - 748 - mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device); 749 - 750 - bus->state = MDIOBUS_REGISTERED; 751 - dev_dbg(&bus->dev, "probed\n"); 752 - return 0; 753 - 754 - error: 755 - for (i = 0; i < PHY_MAX_ADDR; i++) { 756 - mdiodev = bus->mdio_map[i]; 757 - if (!mdiodev) 758 - continue; 759 - 760 - mdiodev->device_remove(mdiodev); 761 - mdiodev->device_free(mdiodev); 762 - } 763 - error_reset_gpiod: 764 - /* Put PHYs in RESET to save power */ 765 - if (bus->reset_gpiod) 766 - gpiod_set_value_cansleep(bus->reset_gpiod, 1); 767 - 768 - device_del(&bus->dev); 769 - return err; 770 - } 771 - EXPORT_SYMBOL(__mdiobus_register); 772 - 773 - void mdiobus_unregister(struct mii_bus *bus) 774 - { 775 - struct mdio_device *mdiodev; 776 - int i; 777 - 778 - if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED)) 779 - return; 780 - bus->state = MDIOBUS_UNREGISTERED; 781 - 782 - for (i = 0; i < PHY_MAX_ADDR; i++) { 783 - mdiodev = bus->mdio_map[i]; 784 - if (!mdiodev) 785 - continue; 786 - 787 - if (mdiodev->reset_gpio) 788 - gpiod_put(mdiodev->reset_gpio); 789 - 790 - mdiodev->device_remove(mdiodev); 791 - mdiodev->device_free(mdiodev); 792 - } 793 - 794 - /* Put PHYs in RESET to save power */ 795 - if (bus->reset_gpiod) 796 - gpiod_set_value_cansleep(bus->reset_gpiod, 1); 797 - 798 - device_del(&bus->dev); 799 - } 800 - EXPORT_SYMBOL(mdiobus_unregister); 801 - 802 - /** 803 - * mdiobus_free - free a struct mii_bus 804 - * @bus: mii_bus to free 805 - * 806 - * This function releases the reference to the underlying device 807 - * object in the mii_bus. If this is the last reference, the mii_bus 808 - * will be freed. 809 - */ 810 - void mdiobus_free(struct mii_bus *bus) 811 - { 812 - /* For compatibility with error handling in drivers. */ 813 - if (bus->state == MDIOBUS_ALLOCATED) { 814 - kfree(bus); 815 - return; 816 - } 817 - 818 - WARN(bus->state != MDIOBUS_UNREGISTERED, 819 - "%s: not in UNREGISTERED state\n", bus->id); 820 - bus->state = MDIOBUS_RELEASED; 821 - 822 - put_device(&bus->dev); 823 - } 824 - EXPORT_SYMBOL(mdiobus_free); 825 455 826 456 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret) 827 457 {
+484
drivers/net/phy/mdio_bus_provider.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* MDIO Bus provider interface 3 + * 4 + * Author: Andy Fleming 5 + * 6 + * Copyright (c) 2004 Freescale Semiconductor, Inc. 7 + */ 8 + 9 + #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 + 11 + #include <linux/delay.h> 12 + #include <linux/device.h> 13 + #include <linux/errno.h> 14 + #include <linux/etherdevice.h> 15 + #include <linux/ethtool.h> 16 + #include <linux/gpio/consumer.h> 17 + #include <linux/init.h> 18 + #include <linux/io.h> 19 + #include <linux/kernel.h> 20 + #include <linux/micrel_phy.h> 21 + #include <linux/mii.h> 22 + #include <linux/mm.h> 23 + #include <linux/netdevice.h> 24 + #include <linux/of_device.h> 25 + #include <linux/of_mdio.h> 26 + #include <linux/phy.h> 27 + #include <linux/slab.h> 28 + #include <linux/string.h> 29 + #include <linux/uaccess.h> 30 + #include <linux/unistd.h> 31 + 32 + #include "mdio-boardinfo.h" 33 + 34 + /** 35 + * mdiobus_alloc_size - allocate a mii_bus structure 36 + * @size: extra amount of memory to allocate for private storage. 37 + * If non-zero, then bus->priv is points to that memory. 38 + * 39 + * Description: called by a bus driver to allocate an mii_bus 40 + * structure to fill in. 41 + */ 42 + struct mii_bus *mdiobus_alloc_size(size_t size) 43 + { 44 + struct mii_bus *bus; 45 + size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); 46 + size_t alloc_size; 47 + int i; 48 + 49 + /* If we alloc extra space, it should be aligned */ 50 + if (size) 51 + alloc_size = aligned_size + size; 52 + else 53 + alloc_size = sizeof(*bus); 54 + 55 + bus = kzalloc(alloc_size, GFP_KERNEL); 56 + if (!bus) 57 + return NULL; 58 + 59 + bus->state = MDIOBUS_ALLOCATED; 60 + if (size) 61 + bus->priv = (void *)bus + aligned_size; 62 + 63 + /* Initialise the interrupts to polling and 64-bit seqcounts */ 64 + for (i = 0; i < PHY_MAX_ADDR; i++) { 65 + bus->irq[i] = PHY_POLL; 66 + u64_stats_init(&bus->stats[i].syncp); 67 + } 68 + 69 + return bus; 70 + } 71 + EXPORT_SYMBOL(mdiobus_alloc_size); 72 + 73 + #if IS_ENABLED(CONFIG_OF_MDIO) 74 + /* Walk the list of subnodes of a mdio bus and look for a node that 75 + * matches the mdio device's address with its 'reg' property. If 76 + * found, set the of_node pointer for the mdio device. This allows 77 + * auto-probed phy devices to be supplied with information passed in 78 + * via DT. 79 + * If a PHY package is found, PHY is searched also there. 80 + */ 81 + static int of_mdiobus_find_phy(struct device *dev, struct mdio_device *mdiodev, 82 + struct device_node *np) 83 + { 84 + struct device_node *child; 85 + 86 + for_each_available_child_of_node(np, child) { 87 + int addr; 88 + 89 + if (of_node_name_eq(child, "ethernet-phy-package")) { 90 + /* Validate PHY package reg presence */ 91 + if (!of_property_present(child, "reg")) { 92 + of_node_put(child); 93 + return -EINVAL; 94 + } 95 + 96 + if (!of_mdiobus_find_phy(dev, mdiodev, child)) { 97 + /* The refcount for the PHY package will be 98 + * incremented later when PHY join the Package. 99 + */ 100 + of_node_put(child); 101 + return 0; 102 + } 103 + 104 + continue; 105 + } 106 + 107 + addr = of_mdio_parse_addr(dev, child); 108 + if (addr < 0) 109 + continue; 110 + 111 + if (addr == mdiodev->addr) { 112 + device_set_node(dev, of_fwnode_handle(child)); 113 + /* The refcount on "child" is passed to the mdio 114 + * device. Do _not_ use of_node_put(child) here. 115 + */ 116 + return 0; 117 + } 118 + } 119 + 120 + return -ENODEV; 121 + } 122 + 123 + static void of_mdiobus_link_mdiodev(struct mii_bus *bus, 124 + struct mdio_device *mdiodev) 125 + { 126 + struct device *dev = &mdiodev->dev; 127 + 128 + if (dev->of_node || !bus->dev.of_node) 129 + return; 130 + 131 + of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node); 132 + } 133 + #endif 134 + 135 + /** 136 + * mdiobus_create_device - create a full MDIO device given 137 + * a mdio_board_info structure 138 + * @bus: MDIO bus to create the devices on 139 + * @bi: mdio_board_info structure describing the devices 140 + * 141 + * Returns 0 on success or < 0 on error. 142 + */ 143 + static int mdiobus_create_device(struct mii_bus *bus, 144 + struct mdio_board_info *bi) 145 + { 146 + struct mdio_device *mdiodev; 147 + int ret = 0; 148 + 149 + mdiodev = mdio_device_create(bus, bi->mdio_addr); 150 + if (IS_ERR(mdiodev)) 151 + return -ENODEV; 152 + 153 + strscpy(mdiodev->modalias, bi->modalias, 154 + sizeof(mdiodev->modalias)); 155 + mdiodev->bus_match = mdio_device_bus_match; 156 + mdiodev->dev.platform_data = (void *)bi->platform_data; 157 + 158 + ret = mdio_device_register(mdiodev); 159 + if (ret) 160 + mdio_device_free(mdiodev); 161 + 162 + return ret; 163 + } 164 + 165 + static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45) 166 + { 167 + struct phy_device *phydev = ERR_PTR(-ENODEV); 168 + struct fwnode_handle *fwnode; 169 + char node_name[16]; 170 + int err; 171 + 172 + phydev = get_phy_device(bus, addr, c45); 173 + if (IS_ERR(phydev)) 174 + return phydev; 175 + 176 + #if IS_ENABLED(CONFIG_OF_MDIO) 177 + /* For DT, see if the auto-probed phy has a corresponding child 178 + * in the bus node, and set the of_node pointer in this case. 179 + */ 180 + of_mdiobus_link_mdiodev(bus, &phydev->mdio); 181 + #endif 182 + 183 + /* Search for a swnode for the phy in the swnode hierarchy of the bus. 184 + * If there is no swnode for the phy provided, just ignore it. 185 + */ 186 + if (dev_fwnode(&bus->dev) && !dev_fwnode(&phydev->mdio.dev)) { 187 + snprintf(node_name, sizeof(node_name), "ethernet-phy@%d", 188 + addr); 189 + fwnode = fwnode_get_named_child_node(dev_fwnode(&bus->dev), 190 + node_name); 191 + if (fwnode) 192 + device_set_node(&phydev->mdio.dev, fwnode); 193 + } 194 + 195 + err = phy_device_register(phydev); 196 + if (err) { 197 + phy_device_free(phydev); 198 + return ERR_PTR(-ENODEV); 199 + } 200 + 201 + return phydev; 202 + } 203 + 204 + /** 205 + * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices. 206 + * @bus: mii_bus to scan 207 + * @addr: address on bus to scan 208 + * 209 + * This function scans one address on the MDIO bus, looking for 210 + * devices which can be identified using a vendor/product ID in 211 + * registers 2 and 3. Not all MDIO devices have such registers, but 212 + * PHY devices typically do. Hence this function assumes anything 213 + * found is a PHY, or can be treated as a PHY. Other MDIO devices, 214 + * such as switches, will probably not be found during the scan. 215 + */ 216 + struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr) 217 + { 218 + return mdiobus_scan(bus, addr, false); 219 + } 220 + EXPORT_SYMBOL(mdiobus_scan_c22); 221 + 222 + /** 223 + * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices. 224 + * @bus: mii_bus to scan 225 + * @addr: address on bus to scan 226 + * 227 + * This function scans one address on the MDIO bus, looking for 228 + * devices which can be identified using a vendor/product ID in 229 + * registers 2 and 3. Not all MDIO devices have such registers, but 230 + * PHY devices typically do. Hence this function assumes anything 231 + * found is a PHY, or can be treated as a PHY. Other MDIO devices, 232 + * such as switches, will probably not be found during the scan. 233 + */ 234 + static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr) 235 + { 236 + return mdiobus_scan(bus, addr, true); 237 + } 238 + 239 + static int mdiobus_scan_bus_c22(struct mii_bus *bus) 240 + { 241 + int i; 242 + 243 + for (i = 0; i < PHY_MAX_ADDR; i++) { 244 + if ((bus->phy_mask & BIT(i)) == 0) { 245 + struct phy_device *phydev; 246 + 247 + phydev = mdiobus_scan_c22(bus, i); 248 + if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 249 + return PTR_ERR(phydev); 250 + } 251 + } 252 + return 0; 253 + } 254 + 255 + static int mdiobus_scan_bus_c45(struct mii_bus *bus) 256 + { 257 + int i; 258 + 259 + for (i = 0; i < PHY_MAX_ADDR; i++) { 260 + if ((bus->phy_mask & BIT(i)) == 0) { 261 + struct phy_device *phydev; 262 + 263 + /* Don't scan C45 if we already have a C22 device */ 264 + if (bus->mdio_map[i]) 265 + continue; 266 + 267 + phydev = mdiobus_scan_c45(bus, i); 268 + if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 269 + return PTR_ERR(phydev); 270 + } 271 + } 272 + return 0; 273 + } 274 + 275 + /* There are some C22 PHYs which do bad things when where is a C45 276 + * transaction on the bus, like accepting a read themselves, and 277 + * stomping over the true devices reply, to performing a write to 278 + * themselves which was intended for another device. Now that C22 279 + * devices have been found, see if any of them are bad for C45, and if we 280 + * should skip the C45 scan. 281 + */ 282 + static bool mdiobus_prevent_c45_scan(struct mii_bus *bus) 283 + { 284 + int i; 285 + 286 + for (i = 0; i < PHY_MAX_ADDR; i++) { 287 + struct phy_device *phydev; 288 + u32 oui; 289 + 290 + phydev = mdiobus_get_phy(bus, i); 291 + if (!phydev) 292 + continue; 293 + oui = phydev->phy_id >> 10; 294 + 295 + if (oui == MICREL_OUI) 296 + return true; 297 + } 298 + return false; 299 + } 300 + 301 + /** 302 + * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus 303 + * @bus: target mii_bus 304 + * @owner: module containing bus accessor functions 305 + * 306 + * Description: Called by a bus driver to bring up all the PHYs 307 + * on a given bus, and attach them to the bus. Drivers should use 308 + * mdiobus_register() rather than __mdiobus_register() unless they 309 + * need to pass a specific owner module. MDIO devices which are not 310 + * PHYs will not be brought up by this function. They are expected 311 + * to be explicitly listed in DT and instantiated by of_mdiobus_register(). 312 + * 313 + * Returns 0 on success or < 0 on error. 314 + */ 315 + int __mdiobus_register(struct mii_bus *bus, struct module *owner) 316 + { 317 + struct mdio_device *mdiodev; 318 + struct gpio_desc *gpiod; 319 + bool prevent_c45_scan; 320 + int i, err; 321 + 322 + if (!bus || !bus->name) 323 + return -EINVAL; 324 + 325 + /* An access method always needs both read and write operations */ 326 + if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45) 327 + return -EINVAL; 328 + 329 + /* At least one method is mandatory */ 330 + if (!bus->read && !bus->read_c45) 331 + return -EINVAL; 332 + 333 + if (bus->parent && bus->parent->of_node) 334 + bus->parent->of_node->fwnode.flags |= 335 + FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD; 336 + 337 + WARN(bus->state != MDIOBUS_ALLOCATED && 338 + bus->state != MDIOBUS_UNREGISTERED, 339 + "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id); 340 + 341 + bus->owner = owner; 342 + bus->dev.parent = bus->parent; 343 + bus->dev.class = &mdio_bus_class; 344 + bus->dev.groups = NULL; 345 + dev_set_name(&bus->dev, "%s", bus->id); 346 + 347 + /* If the bus state is allocated, we're registering a fresh bus 348 + * that may have a fwnode associated with it. Grab a reference 349 + * to the fwnode. This will be dropped when the bus is released. 350 + * If the bus was set to unregistered, it means that the bus was 351 + * previously registered, and we've already grabbed a reference. 352 + */ 353 + if (bus->state == MDIOBUS_ALLOCATED) 354 + fwnode_handle_get(dev_fwnode(&bus->dev)); 355 + 356 + /* We need to set state to MDIOBUS_UNREGISTERED to correctly release 357 + * the device in mdiobus_free() 358 + * 359 + * State will be updated later in this function in case of success 360 + */ 361 + bus->state = MDIOBUS_UNREGISTERED; 362 + 363 + err = device_register(&bus->dev); 364 + if (err) { 365 + pr_err("mii_bus %s failed to register\n", bus->id); 366 + return -EINVAL; 367 + } 368 + 369 + mutex_init(&bus->mdio_lock); 370 + mutex_init(&bus->shared_lock); 371 + 372 + /* assert bus level PHY GPIO reset */ 373 + gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH); 374 + if (IS_ERR(gpiod)) { 375 + err = dev_err_probe(&bus->dev, PTR_ERR(gpiod), 376 + "mii_bus %s couldn't get reset GPIO\n", 377 + bus->id); 378 + device_del(&bus->dev); 379 + return err; 380 + } else if (gpiod) { 381 + bus->reset_gpiod = gpiod; 382 + fsleep(bus->reset_delay_us); 383 + gpiod_set_value_cansleep(gpiod, 0); 384 + if (bus->reset_post_delay_us > 0) 385 + fsleep(bus->reset_post_delay_us); 386 + } 387 + 388 + if (bus->reset) { 389 + err = bus->reset(bus); 390 + if (err) 391 + goto error_reset_gpiod; 392 + } 393 + 394 + if (bus->read) { 395 + err = mdiobus_scan_bus_c22(bus); 396 + if (err) 397 + goto error; 398 + } 399 + 400 + prevent_c45_scan = mdiobus_prevent_c45_scan(bus); 401 + 402 + if (!prevent_c45_scan && bus->read_c45) { 403 + err = mdiobus_scan_bus_c45(bus); 404 + if (err) 405 + goto error; 406 + } 407 + 408 + mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device); 409 + 410 + bus->state = MDIOBUS_REGISTERED; 411 + dev_dbg(&bus->dev, "probed\n"); 412 + return 0; 413 + 414 + error: 415 + for (i = 0; i < PHY_MAX_ADDR; i++) { 416 + mdiodev = bus->mdio_map[i]; 417 + if (!mdiodev) 418 + continue; 419 + 420 + mdiodev->device_remove(mdiodev); 421 + mdiodev->device_free(mdiodev); 422 + } 423 + error_reset_gpiod: 424 + /* Put PHYs in RESET to save power */ 425 + if (bus->reset_gpiod) 426 + gpiod_set_value_cansleep(bus->reset_gpiod, 1); 427 + 428 + device_del(&bus->dev); 429 + return err; 430 + } 431 + EXPORT_SYMBOL(__mdiobus_register); 432 + 433 + void mdiobus_unregister(struct mii_bus *bus) 434 + { 435 + struct mdio_device *mdiodev; 436 + int i; 437 + 438 + if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED)) 439 + return; 440 + bus->state = MDIOBUS_UNREGISTERED; 441 + 442 + for (i = 0; i < PHY_MAX_ADDR; i++) { 443 + mdiodev = bus->mdio_map[i]; 444 + if (!mdiodev) 445 + continue; 446 + 447 + if (mdiodev->reset_gpio) 448 + gpiod_put(mdiodev->reset_gpio); 449 + 450 + mdiodev->device_remove(mdiodev); 451 + mdiodev->device_free(mdiodev); 452 + } 453 + 454 + /* Put PHYs in RESET to save power */ 455 + if (bus->reset_gpiod) 456 + gpiod_set_value_cansleep(bus->reset_gpiod, 1); 457 + 458 + device_del(&bus->dev); 459 + } 460 + EXPORT_SYMBOL(mdiobus_unregister); 461 + 462 + /** 463 + * mdiobus_free - free a struct mii_bus 464 + * @bus: mii_bus to free 465 + * 466 + * This function releases the reference to the underlying device 467 + * object in the mii_bus. If this is the last reference, the mii_bus 468 + * will be freed. 469 + */ 470 + void mdiobus_free(struct mii_bus *bus) 471 + { 472 + /* For compatibility with error handling in drivers. */ 473 + if (bus->state == MDIOBUS_ALLOCATED) { 474 + kfree(bus); 475 + return; 476 + } 477 + 478 + WARN(bus->state != MDIOBUS_UNREGISTERED, 479 + "%s: not in UNREGISTERED state\n", bus->id); 480 + bus->state = MDIOBUS_RELEASED; 481 + 482 + put_device(&bus->dev); 483 + } 484 + EXPORT_SYMBOL(mdiobus_free);
+1
include/linux/phy.h
··· 2062 2062 struct netlink_ext_ack *extack); 2063 2063 2064 2064 extern const struct bus_type mdio_bus_type; 2065 + extern const struct class mdio_bus_class; 2065 2066 2066 2067 struct mdio_board_info { 2067 2068 const char *bus_id;